1 // SPDX-License-Identifier: GPL-2.0-only
3 /* -----------------------------------------------------------------------
5 * Copyright 2011 Intel Corporation; author Matt Fleming
7 * ----------------------------------------------------------------------- */
10 #include <linux/pci.h>
13 #include <asm/e820/types.h>
14 #include <asm/setup.h>
20 /* Maximum physical address for 64-bit kernel with 4-level paging */
21 #define MAXMEM_X86_64_4LEVEL (1ull << 46)
23 const efi_system_table_t *efi_system_table;
24 extern u32 image_offset;
27 preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
29 struct pci_setup_rom *rom = NULL;
36 * Some firmware images contain EFI function pointers at the place where
37 * the romimage and romsize fields are supposed to be. Typically the EFI
38 * code is mapped at high addresses, translating to an unrealistically
39 * large romsize. The UEFI spec limits the size of option ROMs to 16
40 * MiB so we reject any ROMs over 16 MiB in size to catch this.
42 romimage = efi_table_attr(pci, romimage);
43 romsize = efi_table_attr(pci, romsize);
44 if (!romimage || !romsize || romsize > SZ_16M)
45 return EFI_INVALID_PARAMETER;
47 size = romsize + sizeof(*rom);
49 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
51 if (status != EFI_SUCCESS) {
52 efi_printk("Failed to allocate memory for 'rom'\n");
56 memset(rom, 0, sizeof(*rom));
58 rom->data.type = SETUP_PCI;
59 rom->data.len = size - sizeof(struct setup_data);
61 rom->pcilen = pci->romsize;
64 status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
65 PCI_VENDOR_ID, 1, &rom->vendor);
67 if (status != EFI_SUCCESS) {
68 efi_printk("Failed to read rom->vendor\n");
72 status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
73 PCI_DEVICE_ID, 1, &rom->devid);
75 if (status != EFI_SUCCESS) {
76 efi_printk("Failed to read rom->devid\n");
80 status = efi_call_proto(pci, get_location, &rom->segment, &rom->bus,
81 &rom->device, &rom->function);
83 if (status != EFI_SUCCESS)
86 memcpy(rom->romdata, romimage, romsize);
90 efi_bs_call(free_pool, rom);
95 * There's no way to return an informative status from this function,
96 * because any analysis (and printing of error messages) needs to be
97 * done directly at the EFI function call-site.
99 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
100 * just didn't find any PCI devices, but there's no way to tell outside
101 * the context of the call.
103 static void setup_efi_pci(struct boot_params *params)
106 void **pci_handle = NULL;
107 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
108 unsigned long size = 0;
109 struct setup_data *data;
113 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
114 &pci_proto, NULL, &size, pci_handle);
116 if (status == EFI_BUFFER_TOO_SMALL) {
117 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
118 (void **)&pci_handle);
120 if (status != EFI_SUCCESS) {
121 efi_printk("Failed to allocate memory for 'pci_handle'\n");
125 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
126 &pci_proto, NULL, &size, pci_handle);
129 if (status != EFI_SUCCESS)
132 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
134 while (data && data->next)
135 data = (struct setup_data *)(unsigned long)data->next;
137 for_each_efi_handle(h, pci_handle, size, i) {
138 efi_pci_io_protocol_t *pci = NULL;
139 struct pci_setup_rom *rom;
141 status = efi_bs_call(handle_protocol, h, &pci_proto,
143 if (status != EFI_SUCCESS || !pci)
146 status = preserve_pci_rom_image(pci, &rom);
147 if (status != EFI_SUCCESS)
151 data->next = (unsigned long)rom;
153 params->hdr.setup_data = (unsigned long)rom;
155 data = (struct setup_data *)rom;
159 efi_bs_call(free_pool, pci_handle);
162 static void retrieve_apple_device_properties(struct boot_params *boot_params)
164 efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
165 struct setup_data *data, *new;
168 apple_properties_protocol_t *p;
170 status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&p);
171 if (status != EFI_SUCCESS)
174 if (efi_table_attr(p, version) != 0x10000) {
175 efi_printk("Unsupported properties proto version\n");
179 efi_call_proto(p, get_all, NULL, &size);
184 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
185 size + sizeof(struct setup_data),
187 if (status != EFI_SUCCESS) {
188 efi_printk("Failed to allocate memory for 'properties'\n");
192 status = efi_call_proto(p, get_all, new->data, &size);
194 if (status == EFI_BUFFER_TOO_SMALL)
195 efi_bs_call(free_pool, new);
196 } while (status == EFI_BUFFER_TOO_SMALL);
198 new->type = SETUP_APPLE_PROPERTIES;
202 data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
204 boot_params->hdr.setup_data = (unsigned long)new;
207 data = (struct setup_data *)(unsigned long)data->next;
208 data->next = (unsigned long)new;
212 static const efi_char16_t apple[] = L"Apple";
214 static void setup_quirks(struct boot_params *boot_params)
216 efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
217 efi_table_attr(efi_system_table, fw_vendor);
219 if (!memcmp(fw_vendor, apple, sizeof(apple))) {
220 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
221 retrieve_apple_device_properties(boot_params);
226 * See if we have Universal Graphics Adapter (UGA) protocol
229 setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size)
233 void **uga_handle = NULL;
234 efi_uga_draw_protocol_t *uga = NULL, *first_uga;
238 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
239 (void **)&uga_handle);
240 if (status != EFI_SUCCESS)
243 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
244 uga_proto, NULL, &size, uga_handle);
245 if (status != EFI_SUCCESS)
252 for_each_efi_handle(handle, uga_handle, size, i) {
253 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
254 u32 w, h, depth, refresh;
257 status = efi_bs_call(handle_protocol, handle, uga_proto,
259 if (status != EFI_SUCCESS)
263 efi_bs_call(handle_protocol, handle, &pciio_proto, &pciio);
265 status = efi_call_proto(uga, get_mode, &w, &h, &depth, &refresh);
266 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
271 * Once we've found a UGA supporting PCIIO,
272 * don't bother looking any further.
281 if (!width && !height)
284 /* EFI framebuffer */
285 si->orig_video_isVGA = VIDEO_TYPE_EFI;
288 si->lfb_width = width;
289 si->lfb_height = height;
301 efi_bs_call(free_pool, uga_handle);
306 static void setup_graphics(struct boot_params *boot_params)
308 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
309 struct screen_info *si;
310 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
313 void **gop_handle = NULL;
314 void **uga_handle = NULL;
316 si = &boot_params->screen_info;
317 memset(si, 0, sizeof(*si));
320 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
321 &graphics_proto, NULL, &size, gop_handle);
322 if (status == EFI_BUFFER_TOO_SMALL)
323 status = efi_setup_gop(si, &graphics_proto, size);
325 if (status != EFI_SUCCESS) {
327 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
328 &uga_proto, NULL, &size, uga_handle);
329 if (status == EFI_BUFFER_TOO_SMALL)
330 setup_uga(si, &uga_proto, size);
335 static void __noreturn efi_exit(efi_handle_t handle, efi_status_t status)
337 efi_bs_call(exit, handle, status, 0, NULL);
342 void startup_32(struct boot_params *boot_params);
344 void __noreturn efi_stub_entry(efi_handle_t handle,
345 efi_system_table_t *sys_table_arg,
346 struct boot_params *boot_params);
349 * Because the x86 boot code expects to be passed a boot_params we
350 * need to create one ourselves (usually the bootloader would create
353 efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
354 efi_system_table_t *sys_table_arg)
356 struct boot_params *boot_params;
357 struct setup_header *hdr;
358 efi_loaded_image_t *image;
360 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
361 int options_size = 0;
364 unsigned long ramdisk_addr;
365 unsigned long ramdisk_size;
367 efi_system_table = sys_table_arg;
369 /* Check if we were booted by the EFI firmware */
370 if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
371 efi_exit(handle, EFI_INVALID_PARAMETER);
373 status = efi_bs_call(handle_protocol, handle, &proto, (void **)&image);
374 if (status != EFI_SUCCESS) {
375 efi_printk("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
376 efi_exit(handle, status);
379 image_base = efi_table_attr(image, image_base);
380 image_offset = (void *)startup_32 - image_base;
382 status = efi_allocate_pages(sizeof(struct boot_params),
383 (unsigned long *)&boot_params, ULONG_MAX);
384 if (status != EFI_SUCCESS) {
385 efi_printk("Failed to allocate lowmem for boot params\n");
386 efi_exit(handle, status);
389 memset(boot_params, 0x0, sizeof(struct boot_params));
391 hdr = &boot_params->hdr;
393 /* Copy the second sector to boot_params */
394 memcpy(&hdr->jump, image_base + 512, 512);
397 * Fill out some of the header fields ourselves because the
398 * EFI firmware loader doesn't load the first sector.
401 hdr->vid_mode = 0xffff;
402 hdr->boot_flag = 0xAA55;
404 hdr->type_of_loader = 0x21;
406 /* Convert unicode cmdline to ascii */
407 cmdline_ptr = efi_convert_cmdline(image, &options_size, ULONG_MAX);
411 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
412 /* Fill in upper bits of command line address, NOP on 32 bit */
413 boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32;
415 hdr->ramdisk_image = 0;
416 hdr->ramdisk_size = 0;
418 if (efi_is_native()) {
419 status = efi_parse_options(cmdline_ptr);
420 if (status != EFI_SUCCESS)
424 status = efi_load_initrd(image, &ramdisk_addr,
426 hdr->initrd_addr_max,
428 if (status != EFI_SUCCESS)
430 hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
431 hdr->ramdisk_size = ramdisk_size & 0xffffffff;
432 boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
433 boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32;
437 efi_stub_entry(handle, sys_table_arg, boot_params);
441 efi_free(options_size, (unsigned long)cmdline_ptr);
443 efi_free(sizeof(struct boot_params), (unsigned long)boot_params);
445 efi_exit(handle, status);
448 static void add_e820ext(struct boot_params *params,
449 struct setup_data *e820ext, u32 nr_entries)
451 struct setup_data *data;
453 e820ext->type = SETUP_E820_EXT;
454 e820ext->len = nr_entries * sizeof(struct boot_e820_entry);
457 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
459 while (data && data->next)
460 data = (struct setup_data *)(unsigned long)data->next;
463 data->next = (unsigned long)e820ext;
465 params->hdr.setup_data = (unsigned long)e820ext;
469 setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size)
471 struct boot_e820_entry *entry = params->e820_table;
472 struct efi_info *efi = ¶ms->efi_info;
473 struct boot_e820_entry *prev = NULL;
479 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
481 for (i = 0; i < nr_desc; i++) {
482 efi_memory_desc_t *d;
483 unsigned int e820_type = 0;
484 unsigned long m = efi->efi_memmap;
487 m |= (u64)efi->efi_memmap_hi << 32;
490 d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
492 case EFI_RESERVED_TYPE:
493 case EFI_RUNTIME_SERVICES_CODE:
494 case EFI_RUNTIME_SERVICES_DATA:
495 case EFI_MEMORY_MAPPED_IO:
496 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
498 e820_type = E820_TYPE_RESERVED;
501 case EFI_UNUSABLE_MEMORY:
502 e820_type = E820_TYPE_UNUSABLE;
505 case EFI_ACPI_RECLAIM_MEMORY:
506 e820_type = E820_TYPE_ACPI;
509 case EFI_LOADER_CODE:
510 case EFI_LOADER_DATA:
511 case EFI_BOOT_SERVICES_CODE:
512 case EFI_BOOT_SERVICES_DATA:
513 case EFI_CONVENTIONAL_MEMORY:
514 if (efi_soft_reserve_enabled() &&
515 (d->attribute & EFI_MEMORY_SP))
516 e820_type = E820_TYPE_SOFT_RESERVED;
518 e820_type = E820_TYPE_RAM;
521 case EFI_ACPI_MEMORY_NVS:
522 e820_type = E820_TYPE_NVS;
525 case EFI_PERSISTENT_MEMORY:
526 e820_type = E820_TYPE_PMEM;
533 /* Merge adjacent mappings */
534 if (prev && prev->type == e820_type &&
535 (prev->addr + prev->size) == d->phys_addr) {
536 prev->size += d->num_pages << 12;
540 if (nr_entries == ARRAY_SIZE(params->e820_table)) {
541 u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
542 sizeof(struct setup_data);
544 if (!e820ext || e820ext_size < need)
545 return EFI_BUFFER_TOO_SMALL;
547 /* boot_params map full, switch to e820 extended */
548 entry = (struct boot_e820_entry *)e820ext->data;
551 entry->addr = d->phys_addr;
552 entry->size = d->num_pages << PAGE_SHIFT;
553 entry->type = e820_type;
558 if (nr_entries > ARRAY_SIZE(params->e820_table)) {
559 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
561 add_e820ext(params, e820ext, nr_e820ext);
562 nr_entries -= nr_e820ext;
565 params->e820_entries = (u8)nr_entries;
570 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
576 size = sizeof(struct setup_data) +
577 sizeof(struct e820_entry) * nr_desc;
580 efi_bs_call(free_pool, *e820ext);
585 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
587 if (status == EFI_SUCCESS)
588 *e820ext_size = size;
593 static efi_status_t allocate_e820(struct boot_params *params,
594 struct setup_data **e820ext,
597 unsigned long map_size, desc_size, buff_size;
598 struct efi_boot_memmap boot_map;
599 efi_memory_desc_t *map;
604 boot_map.map_size = &map_size;
605 boot_map.desc_size = &desc_size;
606 boot_map.desc_ver = NULL;
607 boot_map.key_ptr = NULL;
608 boot_map.buff_size = &buff_size;
610 status = efi_get_memory_map(&boot_map);
611 if (status != EFI_SUCCESS)
614 nr_desc = buff_size / desc_size;
616 if (nr_desc > ARRAY_SIZE(params->e820_table)) {
617 u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table);
619 status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size);
620 if (status != EFI_SUCCESS)
627 struct exit_boot_struct {
628 struct boot_params *boot_params;
629 struct efi_info *efi;
632 static efi_status_t exit_boot_func(struct efi_boot_memmap *map,
635 const char *signature;
636 struct exit_boot_struct *p = priv;
638 signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE
639 : EFI32_LOADER_SIGNATURE;
640 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
642 p->efi->efi_systab = (unsigned long)efi_system_table;
643 p->efi->efi_memdesc_size = *map->desc_size;
644 p->efi->efi_memdesc_version = *map->desc_ver;
645 p->efi->efi_memmap = (unsigned long)*map->map;
646 p->efi->efi_memmap_size = *map->map_size;
649 p->efi->efi_systab_hi = (unsigned long)efi_system_table >> 32;
650 p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32;
656 static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)
658 unsigned long map_sz, key, desc_size, buff_size;
659 efi_memory_desc_t *mem_map;
660 struct setup_data *e820ext = NULL;
661 __u32 e820ext_size = 0;
664 struct efi_boot_memmap map;
665 struct exit_boot_struct priv;
668 map.map_size = &map_sz;
669 map.desc_size = &desc_size;
670 map.desc_ver = &desc_version;
672 map.buff_size = &buff_size;
673 priv.boot_params = boot_params;
674 priv.efi = &boot_params->efi_info;
676 status = allocate_e820(boot_params, &e820ext, &e820ext_size);
677 if (status != EFI_SUCCESS)
680 /* Might as well exit boot services now */
681 status = efi_exit_boot_services(handle, &map, &priv, exit_boot_func);
682 if (status != EFI_SUCCESS)
686 boot_params->alt_mem_k = 32 * 1024;
688 status = setup_e820(boot_params, e820ext, e820ext_size);
689 if (status != EFI_SUCCESS)
696 * On success, we return the address of startup_32, which has potentially been
697 * relocated by efi_relocate_kernel.
698 * On failure, we exit to the firmware via efi_exit instead of returning.
700 unsigned long efi_main(efi_handle_t handle,
701 efi_system_table_t *sys_table_arg,
702 struct boot_params *boot_params)
704 unsigned long bzimage_addr = (unsigned long)startup_32;
705 unsigned long buffer_start, buffer_end;
706 struct setup_header *hdr = &boot_params->hdr;
708 unsigned long cmdline_paddr;
710 efi_system_table = sys_table_arg;
712 /* Check if we were booted by the EFI firmware */
713 if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
714 efi_exit(handle, EFI_INVALID_PARAMETER);
717 * If the kernel isn't already loaded at a suitable address,
720 * It must be loaded above LOAD_PHYSICAL_ADDR.
722 * The maximum address for 64-bit is 1 << 46 for 4-level paging. This
723 * is defined as the macro MAXMEM, but unfortunately that is not a
724 * compile-time constant if 5-level paging is configured, so we instead
725 * define our own macro for use here.
727 * For 32-bit, the maximum address is complicated to figure out, for
728 * now use KERNEL_IMAGE_SIZE, which will be 512MiB, the same as what
731 * Also relocate it if image_offset is zero, i.e. the kernel wasn't
732 * loaded by LoadImage, but rather by a bootloader that called the
733 * handover entry. The reason we must always relocate in this case is
734 * to handle the case of systemd-boot booting a unified kernel image,
735 * which is a PE executable that contains the bzImage and an initrd as
736 * COFF sections. The initrd section is placed after the bzImage
737 * without ensuring that there are at least init_size bytes available
738 * for the bzImage, and thus the compressed kernel's startup code may
739 * overwrite the initrd unless it is moved out of the way.
742 buffer_start = ALIGN(bzimage_addr - image_offset,
743 hdr->kernel_alignment);
744 buffer_end = buffer_start + hdr->init_size;
746 if ((buffer_start < LOAD_PHYSICAL_ADDR) ||
747 (IS_ENABLED(CONFIG_X86_32) && buffer_end > KERNEL_IMAGE_SIZE) ||
748 (IS_ENABLED(CONFIG_X86_64) && buffer_end > MAXMEM_X86_64_4LEVEL) ||
749 (image_offset == 0)) {
750 status = efi_relocate_kernel(&bzimage_addr,
751 hdr->init_size, hdr->init_size,
753 hdr->kernel_alignment,
755 if (status != EFI_SUCCESS) {
756 efi_printk("efi_relocate_kernel() failed!\n");
760 * Now that we've copied the kernel elsewhere, we no longer
761 * have a set up block before startup_32(), so reset image_offset
762 * to zero in case it was set earlier.
768 * efi_pe_entry() may have been called before efi_main(), in which
769 * case this is the second time we parse the cmdline. This is ok,
770 * parsing the cmdline multiple times does not have side-effects.
772 cmdline_paddr = ((u64)hdr->cmd_line_ptr |
773 ((u64)boot_params->ext_cmd_line_ptr << 32));
774 efi_parse_options((char *)cmdline_paddr);
777 * At this point, an initrd may already have been loaded, either by
778 * the bootloader and passed via bootparams, or loaded from a initrd=
779 * command line option by efi_pe_entry() above. In either case, we
780 * permit an initrd loaded from the LINUX_EFI_INITRD_MEDIA_GUID device
781 * path to supersede it.
784 unsigned long addr, size;
786 status = efi_load_initrd_dev_path(&addr, &size, ULONG_MAX);
787 if (status == EFI_SUCCESS) {
788 hdr->ramdisk_image = (u32)addr;
789 hdr->ramdisk_size = (u32)size;
790 boot_params->ext_ramdisk_image = (u64)addr >> 32;
791 boot_params->ext_ramdisk_size = (u64)size >> 32;
792 } else if (status != EFI_NOT_FOUND) {
793 efi_printk("efi_load_initrd_dev_path() failed!\n");
799 * If the boot loader gave us a value for secure_boot then we use that,
800 * otherwise we ask the BIOS.
802 if (boot_params->secure_boot == efi_secureboot_mode_unset)
803 boot_params->secure_boot = efi_get_secureboot();
805 /* Ask the firmware to clear memory on unclean shutdown */
806 efi_enable_reset_attack_mitigation();
808 efi_random_get_seed();
810 efi_retrieve_tpm2_eventlog();
812 setup_graphics(boot_params);
814 setup_efi_pci(boot_params);
816 setup_quirks(boot_params);
818 status = exit_boot(boot_params, handle);
819 if (status != EFI_SUCCESS) {
820 efi_printk("exit_boot() failed!\n");
826 efi_printk("efi_main() failed!\n");
828 efi_exit(handle, status);