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 static efi_system_table_t *sys_table;
24 extern const bool efi_is64;
25 extern u32 image_offset;
27 __pure efi_system_table_t *efi_system_table(void)
32 __attribute_const__ bool efi_is_64bit(void)
34 if (IS_ENABLED(CONFIG_EFI_MIXED))
36 return IS_ENABLED(CONFIG_X86_64);
40 preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
42 struct pci_setup_rom *rom = NULL;
49 * Some firmware images contain EFI function pointers at the place where
50 * the romimage and romsize fields are supposed to be. Typically the EFI
51 * code is mapped at high addresses, translating to an unrealistically
52 * large romsize. The UEFI spec limits the size of option ROMs to 16
53 * MiB so we reject any ROMs over 16 MiB in size to catch this.
55 romimage = efi_table_attr(pci, romimage);
56 romsize = efi_table_attr(pci, romsize);
57 if (!romimage || !romsize || romsize > SZ_16M)
58 return EFI_INVALID_PARAMETER;
60 size = romsize + sizeof(*rom);
62 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
64 if (status != EFI_SUCCESS) {
65 efi_printk("Failed to allocate memory for 'rom'\n");
69 memset(rom, 0, sizeof(*rom));
71 rom->data.type = SETUP_PCI;
72 rom->data.len = size - sizeof(struct setup_data);
74 rom->pcilen = pci->romsize;
77 status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
78 PCI_VENDOR_ID, 1, &rom->vendor);
80 if (status != EFI_SUCCESS) {
81 efi_printk("Failed to read rom->vendor\n");
85 status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
86 PCI_DEVICE_ID, 1, &rom->devid);
88 if (status != EFI_SUCCESS) {
89 efi_printk("Failed to read rom->devid\n");
93 status = efi_call_proto(pci, get_location, &rom->segment, &rom->bus,
94 &rom->device, &rom->function);
96 if (status != EFI_SUCCESS)
99 memcpy(rom->romdata, romimage, romsize);
103 efi_bs_call(free_pool, rom);
108 * There's no way to return an informative status from this function,
109 * because any analysis (and printing of error messages) needs to be
110 * done directly at the EFI function call-site.
112 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
113 * just didn't find any PCI devices, but there's no way to tell outside
114 * the context of the call.
116 static void setup_efi_pci(struct boot_params *params)
119 void **pci_handle = NULL;
120 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
121 unsigned long size = 0;
122 struct setup_data *data;
126 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
127 &pci_proto, NULL, &size, pci_handle);
129 if (status == EFI_BUFFER_TOO_SMALL) {
130 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
131 (void **)&pci_handle);
133 if (status != EFI_SUCCESS) {
134 efi_printk("Failed to allocate memory for 'pci_handle'\n");
138 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
139 &pci_proto, NULL, &size, pci_handle);
142 if (status != EFI_SUCCESS)
145 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
147 while (data && data->next)
148 data = (struct setup_data *)(unsigned long)data->next;
150 for_each_efi_handle(h, pci_handle, size, i) {
151 efi_pci_io_protocol_t *pci = NULL;
152 struct pci_setup_rom *rom;
154 status = efi_bs_call(handle_protocol, h, &pci_proto,
156 if (status != EFI_SUCCESS || !pci)
159 status = preserve_pci_rom_image(pci, &rom);
160 if (status != EFI_SUCCESS)
164 data->next = (unsigned long)rom;
166 params->hdr.setup_data = (unsigned long)rom;
168 data = (struct setup_data *)rom;
172 efi_bs_call(free_pool, pci_handle);
175 static void retrieve_apple_device_properties(struct boot_params *boot_params)
177 efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
178 struct setup_data *data, *new;
181 apple_properties_protocol_t *p;
183 status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&p);
184 if (status != EFI_SUCCESS)
187 if (efi_table_attr(p, version) != 0x10000) {
188 efi_printk("Unsupported properties proto version\n");
192 efi_call_proto(p, get_all, NULL, &size);
197 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
198 size + sizeof(struct setup_data),
200 if (status != EFI_SUCCESS) {
201 efi_printk("Failed to allocate memory for 'properties'\n");
205 status = efi_call_proto(p, get_all, new->data, &size);
207 if (status == EFI_BUFFER_TOO_SMALL)
208 efi_bs_call(free_pool, new);
209 } while (status == EFI_BUFFER_TOO_SMALL);
211 new->type = SETUP_APPLE_PROPERTIES;
215 data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
217 boot_params->hdr.setup_data = (unsigned long)new;
220 data = (struct setup_data *)(unsigned long)data->next;
221 data->next = (unsigned long)new;
225 static const efi_char16_t apple[] = L"Apple";
227 static void setup_quirks(struct boot_params *boot_params)
229 efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
230 efi_table_attr(efi_system_table(), fw_vendor);
232 if (!memcmp(fw_vendor, apple, sizeof(apple))) {
233 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
234 retrieve_apple_device_properties(boot_params);
239 * See if we have Universal Graphics Adapter (UGA) protocol
242 setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size)
246 void **uga_handle = NULL;
247 efi_uga_draw_protocol_t *uga = NULL, *first_uga;
251 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
252 (void **)&uga_handle);
253 if (status != EFI_SUCCESS)
256 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
257 uga_proto, NULL, &size, uga_handle);
258 if (status != EFI_SUCCESS)
265 for_each_efi_handle(handle, uga_handle, size, i) {
266 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
267 u32 w, h, depth, refresh;
270 status = efi_bs_call(handle_protocol, handle, uga_proto,
272 if (status != EFI_SUCCESS)
276 efi_bs_call(handle_protocol, handle, &pciio_proto, &pciio);
278 status = efi_call_proto(uga, get_mode, &w, &h, &depth, &refresh);
279 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
284 * Once we've found a UGA supporting PCIIO,
285 * don't bother looking any further.
294 if (!width && !height)
297 /* EFI framebuffer */
298 si->orig_video_isVGA = VIDEO_TYPE_EFI;
301 si->lfb_width = width;
302 si->lfb_height = height;
314 efi_bs_call(free_pool, uga_handle);
319 static void setup_graphics(struct boot_params *boot_params)
321 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
322 struct screen_info *si;
323 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
326 void **gop_handle = NULL;
327 void **uga_handle = NULL;
329 si = &boot_params->screen_info;
330 memset(si, 0, sizeof(*si));
333 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
334 &graphics_proto, NULL, &size, gop_handle);
335 if (status == EFI_BUFFER_TOO_SMALL)
336 status = efi_setup_gop(si, &graphics_proto, size);
338 if (status != EFI_SUCCESS) {
340 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
341 &uga_proto, NULL, &size, uga_handle);
342 if (status == EFI_BUFFER_TOO_SMALL)
343 setup_uga(si, &uga_proto, size);
348 static void __noreturn efi_exit(efi_handle_t handle, efi_status_t status)
350 efi_bs_call(exit, handle, status, 0, NULL);
355 void startup_32(struct boot_params *boot_params);
357 void __noreturn efi_stub_entry(efi_handle_t handle,
358 efi_system_table_t *sys_table_arg,
359 struct boot_params *boot_params);
362 * Because the x86 boot code expects to be passed a boot_params we
363 * need to create one ourselves (usually the bootloader would create
366 efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
367 efi_system_table_t *sys_table_arg)
369 struct boot_params *boot_params;
370 struct setup_header *hdr;
371 efi_loaded_image_t *image;
373 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
374 int options_size = 0;
377 unsigned long ramdisk_addr;
378 unsigned long ramdisk_size;
380 sys_table = sys_table_arg;
382 /* Check if we were booted by the EFI firmware */
383 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
384 efi_exit(handle, EFI_INVALID_PARAMETER);
386 status = efi_bs_call(handle_protocol, handle, &proto, (void **)&image);
387 if (status != EFI_SUCCESS) {
388 efi_printk("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
389 efi_exit(handle, status);
392 image_base = efi_table_attr(image, image_base);
393 image_offset = (void *)startup_32 - image_base;
395 hdr = &((struct boot_params *)image_base)->hdr;
397 status = efi_allocate_pages(0x4000, (unsigned long *)&boot_params, ULONG_MAX);
398 if (status != EFI_SUCCESS) {
399 efi_printk("Failed to allocate lowmem for boot params\n");
400 efi_exit(handle, status);
403 memset(boot_params, 0x0, 0x4000);
405 hdr = &boot_params->hdr;
407 /* Copy the second sector to boot_params */
408 memcpy(&hdr->jump, image_base + 512, 512);
411 * Fill out some of the header fields ourselves because the
412 * EFI firmware loader doesn't load the first sector.
415 hdr->vid_mode = 0xffff;
416 hdr->boot_flag = 0xAA55;
418 hdr->type_of_loader = 0x21;
420 /* Convert unicode cmdline to ascii */
421 cmdline_ptr = efi_convert_cmdline(image, &options_size, ULONG_MAX);
425 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
426 /* Fill in upper bits of command line address, NOP on 32 bit */
427 boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32;
429 hdr->ramdisk_image = 0;
430 hdr->ramdisk_size = 0;
432 if (efi_is_native()) {
433 status = efi_parse_options(cmdline_ptr);
434 if (status != EFI_SUCCESS)
438 status = efi_load_initrd(image, &ramdisk_addr,
440 hdr->initrd_addr_max,
442 if (status != EFI_SUCCESS)
444 hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
445 hdr->ramdisk_size = ramdisk_size & 0xffffffff;
446 boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
447 boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32;
451 efi_stub_entry(handle, sys_table, boot_params);
455 efi_free(options_size, (unsigned long)cmdline_ptr);
457 efi_free(0x4000, (unsigned long)boot_params);
459 efi_exit(handle, status);
462 static void add_e820ext(struct boot_params *params,
463 struct setup_data *e820ext, u32 nr_entries)
465 struct setup_data *data;
467 e820ext->type = SETUP_E820_EXT;
468 e820ext->len = nr_entries * sizeof(struct boot_e820_entry);
471 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
473 while (data && data->next)
474 data = (struct setup_data *)(unsigned long)data->next;
477 data->next = (unsigned long)e820ext;
479 params->hdr.setup_data = (unsigned long)e820ext;
483 setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size)
485 struct boot_e820_entry *entry = params->e820_table;
486 struct efi_info *efi = ¶ms->efi_info;
487 struct boot_e820_entry *prev = NULL;
493 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
495 for (i = 0; i < nr_desc; i++) {
496 efi_memory_desc_t *d;
497 unsigned int e820_type = 0;
498 unsigned long m = efi->efi_memmap;
501 m |= (u64)efi->efi_memmap_hi << 32;
504 d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
506 case EFI_RESERVED_TYPE:
507 case EFI_RUNTIME_SERVICES_CODE:
508 case EFI_RUNTIME_SERVICES_DATA:
509 case EFI_MEMORY_MAPPED_IO:
510 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
512 e820_type = E820_TYPE_RESERVED;
515 case EFI_UNUSABLE_MEMORY:
516 e820_type = E820_TYPE_UNUSABLE;
519 case EFI_ACPI_RECLAIM_MEMORY:
520 e820_type = E820_TYPE_ACPI;
523 case EFI_LOADER_CODE:
524 case EFI_LOADER_DATA:
525 case EFI_BOOT_SERVICES_CODE:
526 case EFI_BOOT_SERVICES_DATA:
527 case EFI_CONVENTIONAL_MEMORY:
528 if (efi_soft_reserve_enabled() &&
529 (d->attribute & EFI_MEMORY_SP))
530 e820_type = E820_TYPE_SOFT_RESERVED;
532 e820_type = E820_TYPE_RAM;
535 case EFI_ACPI_MEMORY_NVS:
536 e820_type = E820_TYPE_NVS;
539 case EFI_PERSISTENT_MEMORY:
540 e820_type = E820_TYPE_PMEM;
547 /* Merge adjacent mappings */
548 if (prev && prev->type == e820_type &&
549 (prev->addr + prev->size) == d->phys_addr) {
550 prev->size += d->num_pages << 12;
554 if (nr_entries == ARRAY_SIZE(params->e820_table)) {
555 u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
556 sizeof(struct setup_data);
558 if (!e820ext || e820ext_size < need)
559 return EFI_BUFFER_TOO_SMALL;
561 /* boot_params map full, switch to e820 extended */
562 entry = (struct boot_e820_entry *)e820ext->data;
565 entry->addr = d->phys_addr;
566 entry->size = d->num_pages << PAGE_SHIFT;
567 entry->type = e820_type;
572 if (nr_entries > ARRAY_SIZE(params->e820_table)) {
573 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
575 add_e820ext(params, e820ext, nr_e820ext);
576 nr_entries -= nr_e820ext;
579 params->e820_entries = (u8)nr_entries;
584 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
590 size = sizeof(struct setup_data) +
591 sizeof(struct e820_entry) * nr_desc;
594 efi_bs_call(free_pool, *e820ext);
599 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
601 if (status == EFI_SUCCESS)
602 *e820ext_size = size;
607 static efi_status_t allocate_e820(struct boot_params *params,
608 struct setup_data **e820ext,
611 unsigned long map_size, desc_size, buff_size;
612 struct efi_boot_memmap boot_map;
613 efi_memory_desc_t *map;
618 boot_map.map_size = &map_size;
619 boot_map.desc_size = &desc_size;
620 boot_map.desc_ver = NULL;
621 boot_map.key_ptr = NULL;
622 boot_map.buff_size = &buff_size;
624 status = efi_get_memory_map(&boot_map);
625 if (status != EFI_SUCCESS)
628 nr_desc = buff_size / desc_size;
630 if (nr_desc > ARRAY_SIZE(params->e820_table)) {
631 u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table);
633 status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size);
634 if (status != EFI_SUCCESS)
641 struct exit_boot_struct {
642 struct boot_params *boot_params;
643 struct efi_info *efi;
646 static efi_status_t exit_boot_func(struct efi_boot_memmap *map,
649 const char *signature;
650 struct exit_boot_struct *p = priv;
652 signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE
653 : EFI32_LOADER_SIGNATURE;
654 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
656 p->efi->efi_systab = (unsigned long)efi_system_table();
657 p->efi->efi_memdesc_size = *map->desc_size;
658 p->efi->efi_memdesc_version = *map->desc_ver;
659 p->efi->efi_memmap = (unsigned long)*map->map;
660 p->efi->efi_memmap_size = *map->map_size;
663 p->efi->efi_systab_hi = (unsigned long)efi_system_table() >> 32;
664 p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32;
670 static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)
672 unsigned long map_sz, key, desc_size, buff_size;
673 efi_memory_desc_t *mem_map;
674 struct setup_data *e820ext = NULL;
675 __u32 e820ext_size = 0;
678 struct efi_boot_memmap map;
679 struct exit_boot_struct priv;
682 map.map_size = &map_sz;
683 map.desc_size = &desc_size;
684 map.desc_ver = &desc_version;
686 map.buff_size = &buff_size;
687 priv.boot_params = boot_params;
688 priv.efi = &boot_params->efi_info;
690 status = allocate_e820(boot_params, &e820ext, &e820ext_size);
691 if (status != EFI_SUCCESS)
694 /* Might as well exit boot services now */
695 status = efi_exit_boot_services(handle, &map, &priv, exit_boot_func);
696 if (status != EFI_SUCCESS)
700 boot_params->alt_mem_k = 32 * 1024;
702 status = setup_e820(boot_params, e820ext, e820ext_size);
703 if (status != EFI_SUCCESS)
710 * On success, we return the address of startup_32, which has potentially been
711 * relocated by efi_relocate_kernel.
712 * On failure, we exit to the firmware via efi_exit instead of returning.
714 unsigned long efi_main(efi_handle_t handle,
715 efi_system_table_t *sys_table_arg,
716 struct boot_params *boot_params)
718 unsigned long bzimage_addr = (unsigned long)startup_32;
719 unsigned long buffer_start, buffer_end;
720 struct setup_header *hdr = &boot_params->hdr;
722 unsigned long cmdline_paddr;
724 sys_table = sys_table_arg;
726 /* Check if we were booted by the EFI firmware */
727 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
728 efi_exit(handle, EFI_INVALID_PARAMETER);
731 * If the kernel isn't already loaded at a suitable address,
734 * It must be loaded above LOAD_PHYSICAL_ADDR.
736 * The maximum address for 64-bit is 1 << 46 for 4-level paging. This
737 * is defined as the macro MAXMEM, but unfortunately that is not a
738 * compile-time constant if 5-level paging is configured, so we instead
739 * define our own macro for use here.
741 * For 32-bit, the maximum address is complicated to figure out, for
742 * now use KERNEL_IMAGE_SIZE, which will be 512MiB, the same as what
745 * Also relocate it if image_offset is zero, i.e. we weren't loaded by
746 * LoadImage, but we are not aligned correctly.
749 buffer_start = ALIGN(bzimage_addr - image_offset,
750 hdr->kernel_alignment);
751 buffer_end = buffer_start + hdr->init_size;
753 if ((buffer_start < LOAD_PHYSICAL_ADDR) ||
754 (IS_ENABLED(CONFIG_X86_32) && buffer_end > KERNEL_IMAGE_SIZE) ||
755 (IS_ENABLED(CONFIG_X86_64) && buffer_end > MAXMEM_X86_64_4LEVEL) ||
756 (image_offset == 0 && !IS_ALIGNED(bzimage_addr,
757 hdr->kernel_alignment))) {
758 status = efi_relocate_kernel(&bzimage_addr,
759 hdr->init_size, hdr->init_size,
761 hdr->kernel_alignment,
763 if (status != EFI_SUCCESS) {
764 efi_printk("efi_relocate_kernel() failed!\n");
768 * Now that we've copied the kernel elsewhere, we no longer
769 * have a set up block before startup_32(), so reset image_offset
770 * to zero in case it was set earlier.
776 * efi_pe_entry() may have been called before efi_main(), in which
777 * case this is the second time we parse the cmdline. This is ok,
778 * parsing the cmdline multiple times does not have side-effects.
780 cmdline_paddr = ((u64)hdr->cmd_line_ptr |
781 ((u64)boot_params->ext_cmd_line_ptr << 32));
782 efi_parse_options((char *)cmdline_paddr);
785 * At this point, an initrd may already have been loaded, either by
786 * the bootloader and passed via bootparams, or loaded from a initrd=
787 * command line option by efi_pe_entry() above. In either case, we
788 * permit an initrd loaded from the LINUX_EFI_INITRD_MEDIA_GUID device
789 * path to supersede it.
792 unsigned long addr, size;
794 status = efi_load_initrd_dev_path(&addr, &size, ULONG_MAX);
795 if (status == EFI_SUCCESS) {
796 hdr->ramdisk_image = (u32)addr;
797 hdr->ramdisk_size = (u32)size;
798 boot_params->ext_ramdisk_image = (u64)addr >> 32;
799 boot_params->ext_ramdisk_size = (u64)size >> 32;
800 } else if (status != EFI_NOT_FOUND) {
801 efi_printk("efi_load_initrd_dev_path() failed!\n");
807 * If the boot loader gave us a value for secure_boot then we use that,
808 * otherwise we ask the BIOS.
810 if (boot_params->secure_boot == efi_secureboot_mode_unset)
811 boot_params->secure_boot = efi_get_secureboot();
813 /* Ask the firmware to clear memory on unclean shutdown */
814 efi_enable_reset_attack_mitigation();
816 efi_random_get_seed();
818 efi_retrieve_tpm2_eventlog();
820 setup_graphics(boot_params);
822 setup_efi_pci(boot_params);
824 setup_quirks(boot_params);
826 status = exit_boot(boot_params, handle);
827 if (status != EFI_SUCCESS) {
828 efi_printk("exit_boot() failed!\n");
834 efi_printk("efi_main() failed!\n");
836 efi_exit(handle, status);