1 // SPDX-License-Identifier: GPL-2.0-only
3 /* -----------------------------------------------------------------------
5 * Copyright 2011 Intel Corporation; author Matt Fleming
7 * ----------------------------------------------------------------------- */
10 #include <linux/pci.h>
11 #include <linux/stddef.h>
14 #include <asm/e820/types.h>
15 #include <asm/setup.h>
21 /* Maximum physical address for 64-bit kernel with 4-level paging */
22 #define MAXMEM_X86_64_4LEVEL (1ull << 46)
24 const efi_system_table_t *efi_system_table;
25 extern u32 image_offset;
26 static efi_loaded_image_t *image = NULL;
29 preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
31 struct pci_setup_rom *rom = NULL;
38 * Some firmware images contain EFI function pointers at the place where
39 * the romimage and romsize fields are supposed to be. Typically the EFI
40 * code is mapped at high addresses, translating to an unrealistically
41 * large romsize. The UEFI spec limits the size of option ROMs to 16
42 * MiB so we reject any ROMs over 16 MiB in size to catch this.
44 romimage = efi_table_attr(pci, romimage);
45 romsize = efi_table_attr(pci, romsize);
46 if (!romimage || !romsize || romsize > SZ_16M)
47 return EFI_INVALID_PARAMETER;
49 size = romsize + sizeof(*rom);
51 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
53 if (status != EFI_SUCCESS) {
54 efi_err("Failed to allocate memory for 'rom'\n");
58 memset(rom, 0, sizeof(*rom));
60 rom->data.type = SETUP_PCI;
61 rom->data.len = size - sizeof(struct setup_data);
63 rom->pcilen = pci->romsize;
66 status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
67 PCI_VENDOR_ID, 1, &rom->vendor);
69 if (status != EFI_SUCCESS) {
70 efi_err("Failed to read rom->vendor\n");
74 status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
75 PCI_DEVICE_ID, 1, &rom->devid);
77 if (status != EFI_SUCCESS) {
78 efi_err("Failed to read rom->devid\n");
82 status = efi_call_proto(pci, get_location, &rom->segment, &rom->bus,
83 &rom->device, &rom->function);
85 if (status != EFI_SUCCESS)
88 memcpy(rom->romdata, romimage, romsize);
92 efi_bs_call(free_pool, rom);
97 * There's no way to return an informative status from this function,
98 * because any analysis (and printing of error messages) needs to be
99 * done directly at the EFI function call-site.
101 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
102 * just didn't find any PCI devices, but there's no way to tell outside
103 * the context of the call.
105 static void setup_efi_pci(struct boot_params *params)
108 void **pci_handle = NULL;
109 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
110 unsigned long size = 0;
111 struct setup_data *data;
115 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
116 &pci_proto, NULL, &size, pci_handle);
118 if (status == EFI_BUFFER_TOO_SMALL) {
119 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
120 (void **)&pci_handle);
122 if (status != EFI_SUCCESS) {
123 efi_err("Failed to allocate memory for 'pci_handle'\n");
127 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
128 &pci_proto, NULL, &size, pci_handle);
131 if (status != EFI_SUCCESS)
134 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
136 while (data && data->next)
137 data = (struct setup_data *)(unsigned long)data->next;
139 for_each_efi_handle(h, pci_handle, size, i) {
140 efi_pci_io_protocol_t *pci = NULL;
141 struct pci_setup_rom *rom;
143 status = efi_bs_call(handle_protocol, h, &pci_proto,
145 if (status != EFI_SUCCESS || !pci)
148 status = preserve_pci_rom_image(pci, &rom);
149 if (status != EFI_SUCCESS)
153 data->next = (unsigned long)rom;
155 params->hdr.setup_data = (unsigned long)rom;
157 data = (struct setup_data *)rom;
161 efi_bs_call(free_pool, pci_handle);
164 static void retrieve_apple_device_properties(struct boot_params *boot_params)
166 efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
167 struct setup_data *data, *new;
170 apple_properties_protocol_t *p;
172 status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&p);
173 if (status != EFI_SUCCESS)
176 if (efi_table_attr(p, version) != 0x10000) {
177 efi_err("Unsupported properties proto version\n");
181 efi_call_proto(p, get_all, NULL, &size);
186 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
187 size + sizeof(struct setup_data),
189 if (status != EFI_SUCCESS) {
190 efi_err("Failed to allocate memory for 'properties'\n");
194 status = efi_call_proto(p, get_all, new->data, &size);
196 if (status == EFI_BUFFER_TOO_SMALL)
197 efi_bs_call(free_pool, new);
198 } while (status == EFI_BUFFER_TOO_SMALL);
200 new->type = SETUP_APPLE_PROPERTIES;
204 data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
206 boot_params->hdr.setup_data = (unsigned long)new;
209 data = (struct setup_data *)(unsigned long)data->next;
210 data->next = (unsigned long)new;
214 static const efi_char16_t apple[] = L"Apple";
216 static void setup_quirks(struct boot_params *boot_params)
218 efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
219 efi_table_attr(efi_system_table, fw_vendor);
221 if (!memcmp(fw_vendor, apple, sizeof(apple))) {
222 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
223 retrieve_apple_device_properties(boot_params);
228 * See if we have Universal Graphics Adapter (UGA) protocol
231 setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size)
235 void **uga_handle = NULL;
236 efi_uga_draw_protocol_t *uga = NULL, *first_uga;
240 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
241 (void **)&uga_handle);
242 if (status != EFI_SUCCESS)
245 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
246 uga_proto, NULL, &size, uga_handle);
247 if (status != EFI_SUCCESS)
254 for_each_efi_handle(handle, uga_handle, size, i) {
255 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
256 u32 w, h, depth, refresh;
259 status = efi_bs_call(handle_protocol, handle, uga_proto,
261 if (status != EFI_SUCCESS)
265 efi_bs_call(handle_protocol, handle, &pciio_proto, &pciio);
267 status = efi_call_proto(uga, get_mode, &w, &h, &depth, &refresh);
268 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
273 * Once we've found a UGA supporting PCIIO,
274 * don't bother looking any further.
283 if (!width && !height)
286 /* EFI framebuffer */
287 si->orig_video_isVGA = VIDEO_TYPE_EFI;
290 si->lfb_width = width;
291 si->lfb_height = height;
303 efi_bs_call(free_pool, uga_handle);
308 static void setup_graphics(struct boot_params *boot_params)
310 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
311 struct screen_info *si;
312 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
315 void **gop_handle = NULL;
316 void **uga_handle = NULL;
318 si = &boot_params->screen_info;
319 memset(si, 0, sizeof(*si));
322 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
323 &graphics_proto, NULL, &size, gop_handle);
324 if (status == EFI_BUFFER_TOO_SMALL)
325 status = efi_setup_gop(si, &graphics_proto, size);
327 if (status != EFI_SUCCESS) {
329 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
330 &uga_proto, NULL, &size, uga_handle);
331 if (status == EFI_BUFFER_TOO_SMALL)
332 setup_uga(si, &uga_proto, size);
337 static void __noreturn efi_exit(efi_handle_t handle, efi_status_t status)
339 efi_bs_call(exit, handle, status, 0, NULL);
344 void startup_32(struct boot_params *boot_params);
346 void __noreturn efi_stub_entry(efi_handle_t handle,
347 efi_system_table_t *sys_table_arg,
348 struct boot_params *boot_params);
351 * Because the x86 boot code expects to be passed a boot_params we
352 * need to create one ourselves (usually the bootloader would create
355 efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
356 efi_system_table_t *sys_table_arg)
358 struct boot_params *boot_params;
359 struct setup_header *hdr;
361 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
362 int options_size = 0;
366 efi_system_table = sys_table_arg;
368 /* Check if we were booted by the EFI firmware */
369 if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
370 efi_exit(handle, EFI_INVALID_PARAMETER);
372 status = efi_bs_call(handle_protocol, handle, &proto, (void **)&image);
373 if (status != EFI_SUCCESS) {
374 efi_err("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
375 efi_exit(handle, status);
378 image_base = efi_table_attr(image, image_base);
379 image_offset = (void *)startup_32 - image_base;
381 status = efi_allocate_pages(sizeof(struct boot_params),
382 (unsigned long *)&boot_params, ULONG_MAX);
383 if (status != EFI_SUCCESS) {
384 efi_err("Failed to allocate lowmem for boot params\n");
385 efi_exit(handle, status);
388 memset(boot_params, 0x0, sizeof(struct boot_params));
390 hdr = &boot_params->hdr;
392 /* Copy the setup header from the second sector to boot_params */
393 memcpy(&hdr->jump, image_base + 512,
394 sizeof(struct setup_header) - offsetof(struct setup_header, jump));
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);
411 efi_set_u64_split((unsigned long)cmdline_ptr,
412 &hdr->cmd_line_ptr, &boot_params->ext_cmd_line_ptr);
414 hdr->ramdisk_image = 0;
415 hdr->ramdisk_size = 0;
417 efi_stub_entry(handle, sys_table_arg, boot_params);
421 efi_free(sizeof(struct boot_params), (unsigned long)boot_params);
423 efi_exit(handle, status);
426 static void add_e820ext(struct boot_params *params,
427 struct setup_data *e820ext, u32 nr_entries)
429 struct setup_data *data;
431 e820ext->type = SETUP_E820_EXT;
432 e820ext->len = nr_entries * sizeof(struct boot_e820_entry);
435 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
437 while (data && data->next)
438 data = (struct setup_data *)(unsigned long)data->next;
441 data->next = (unsigned long)e820ext;
443 params->hdr.setup_data = (unsigned long)e820ext;
447 setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size)
449 struct boot_e820_entry *entry = params->e820_table;
450 struct efi_info *efi = ¶ms->efi_info;
451 struct boot_e820_entry *prev = NULL;
457 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
459 for (i = 0; i < nr_desc; i++) {
460 efi_memory_desc_t *d;
461 unsigned int e820_type = 0;
462 unsigned long m = efi->efi_memmap;
465 m |= (u64)efi->efi_memmap_hi << 32;
468 d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
470 case EFI_RESERVED_TYPE:
471 case EFI_RUNTIME_SERVICES_CODE:
472 case EFI_RUNTIME_SERVICES_DATA:
473 case EFI_MEMORY_MAPPED_IO:
474 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
476 e820_type = E820_TYPE_RESERVED;
479 case EFI_UNUSABLE_MEMORY:
480 e820_type = E820_TYPE_UNUSABLE;
483 case EFI_ACPI_RECLAIM_MEMORY:
484 e820_type = E820_TYPE_ACPI;
487 case EFI_LOADER_CODE:
488 case EFI_LOADER_DATA:
489 case EFI_BOOT_SERVICES_CODE:
490 case EFI_BOOT_SERVICES_DATA:
491 case EFI_CONVENTIONAL_MEMORY:
492 if (efi_soft_reserve_enabled() &&
493 (d->attribute & EFI_MEMORY_SP))
494 e820_type = E820_TYPE_SOFT_RESERVED;
496 e820_type = E820_TYPE_RAM;
499 case EFI_ACPI_MEMORY_NVS:
500 e820_type = E820_TYPE_NVS;
503 case EFI_PERSISTENT_MEMORY:
504 e820_type = E820_TYPE_PMEM;
511 /* Merge adjacent mappings */
512 if (prev && prev->type == e820_type &&
513 (prev->addr + prev->size) == d->phys_addr) {
514 prev->size += d->num_pages << 12;
518 if (nr_entries == ARRAY_SIZE(params->e820_table)) {
519 u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
520 sizeof(struct setup_data);
522 if (!e820ext || e820ext_size < need)
523 return EFI_BUFFER_TOO_SMALL;
525 /* boot_params map full, switch to e820 extended */
526 entry = (struct boot_e820_entry *)e820ext->data;
529 entry->addr = d->phys_addr;
530 entry->size = d->num_pages << PAGE_SHIFT;
531 entry->type = e820_type;
536 if (nr_entries > ARRAY_SIZE(params->e820_table)) {
537 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
539 add_e820ext(params, e820ext, nr_e820ext);
540 nr_entries -= nr_e820ext;
543 params->e820_entries = (u8)nr_entries;
548 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
554 size = sizeof(struct setup_data) +
555 sizeof(struct e820_entry) * nr_desc;
558 efi_bs_call(free_pool, *e820ext);
563 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
565 if (status == EFI_SUCCESS)
566 *e820ext_size = size;
571 static efi_status_t allocate_e820(struct boot_params *params,
572 struct setup_data **e820ext,
575 unsigned long map_size, desc_size, map_key;
577 __u32 nr_desc, desc_version;
579 /* Only need the size of the mem map and size of each mem descriptor */
581 status = efi_bs_call(get_memory_map, &map_size, NULL, &map_key,
582 &desc_size, &desc_version);
583 if (status != EFI_BUFFER_TOO_SMALL)
584 return (status != EFI_SUCCESS) ? status : EFI_UNSUPPORTED;
586 nr_desc = map_size / desc_size + EFI_MMAP_NR_SLACK_SLOTS;
588 if (nr_desc > ARRAY_SIZE(params->e820_table)) {
589 u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table);
591 status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size);
592 if (status != EFI_SUCCESS)
599 struct exit_boot_struct {
600 struct boot_params *boot_params;
601 struct efi_info *efi;
604 static efi_status_t exit_boot_func(struct efi_boot_memmap *map,
607 const char *signature;
608 struct exit_boot_struct *p = priv;
610 signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE
611 : EFI32_LOADER_SIGNATURE;
612 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
614 efi_set_u64_split((unsigned long)efi_system_table,
615 &p->efi->efi_systab, &p->efi->efi_systab_hi);
616 p->efi->efi_memdesc_size = *map->desc_size;
617 p->efi->efi_memdesc_version = *map->desc_ver;
618 efi_set_u64_split((unsigned long)*map->map,
619 &p->efi->efi_memmap, &p->efi->efi_memmap_hi);
620 p->efi->efi_memmap_size = *map->map_size;
625 static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)
627 unsigned long map_sz, key, desc_size, buff_size;
628 efi_memory_desc_t *mem_map;
629 struct setup_data *e820ext = NULL;
630 __u32 e820ext_size = 0;
633 struct efi_boot_memmap map;
634 struct exit_boot_struct priv;
637 map.map_size = &map_sz;
638 map.desc_size = &desc_size;
639 map.desc_ver = &desc_version;
641 map.buff_size = &buff_size;
642 priv.boot_params = boot_params;
643 priv.efi = &boot_params->efi_info;
645 status = allocate_e820(boot_params, &e820ext, &e820ext_size);
646 if (status != EFI_SUCCESS)
649 /* Might as well exit boot services now */
650 status = efi_exit_boot_services(handle, &map, &priv, exit_boot_func);
651 if (status != EFI_SUCCESS)
655 boot_params->alt_mem_k = 32 * 1024;
657 status = setup_e820(boot_params, e820ext, e820ext_size);
658 if (status != EFI_SUCCESS)
665 * On success, we return the address of startup_32, which has potentially been
666 * relocated by efi_relocate_kernel.
667 * On failure, we exit to the firmware via efi_exit instead of returning.
669 unsigned long efi_main(efi_handle_t handle,
670 efi_system_table_t *sys_table_arg,
671 struct boot_params *boot_params)
673 unsigned long bzimage_addr = (unsigned long)startup_32;
674 unsigned long buffer_start, buffer_end;
675 struct setup_header *hdr = &boot_params->hdr;
678 efi_system_table = sys_table_arg;
680 /* Check if we were booted by the EFI firmware */
681 if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
682 efi_exit(handle, EFI_INVALID_PARAMETER);
685 * If the kernel isn't already loaded at a suitable address,
688 * It must be loaded above LOAD_PHYSICAL_ADDR.
690 * The maximum address for 64-bit is 1 << 46 for 4-level paging. This
691 * is defined as the macro MAXMEM, but unfortunately that is not a
692 * compile-time constant if 5-level paging is configured, so we instead
693 * define our own macro for use here.
695 * For 32-bit, the maximum address is complicated to figure out, for
696 * now use KERNEL_IMAGE_SIZE, which will be 512MiB, the same as what
699 * Also relocate it if image_offset is zero, i.e. the kernel wasn't
700 * loaded by LoadImage, but rather by a bootloader that called the
701 * handover entry. The reason we must always relocate in this case is
702 * to handle the case of systemd-boot booting a unified kernel image,
703 * which is a PE executable that contains the bzImage and an initrd as
704 * COFF sections. The initrd section is placed after the bzImage
705 * without ensuring that there are at least init_size bytes available
706 * for the bzImage, and thus the compressed kernel's startup code may
707 * overwrite the initrd unless it is moved out of the way.
710 buffer_start = ALIGN(bzimage_addr - image_offset,
711 hdr->kernel_alignment);
712 buffer_end = buffer_start + hdr->init_size;
714 if ((buffer_start < LOAD_PHYSICAL_ADDR) ||
715 (IS_ENABLED(CONFIG_X86_32) && buffer_end > KERNEL_IMAGE_SIZE) ||
716 (IS_ENABLED(CONFIG_X86_64) && buffer_end > MAXMEM_X86_64_4LEVEL) ||
717 (image_offset == 0)) {
718 status = efi_relocate_kernel(&bzimage_addr,
719 hdr->init_size, hdr->init_size,
721 hdr->kernel_alignment,
723 if (status != EFI_SUCCESS) {
724 efi_err("efi_relocate_kernel() failed!\n");
728 * Now that we've copied the kernel elsewhere, we no longer
729 * have a set up block before startup_32(), so reset image_offset
730 * to zero in case it was set earlier.
735 #ifdef CONFIG_CMDLINE_BOOL
736 status = efi_parse_options(CONFIG_CMDLINE);
737 if (status != EFI_SUCCESS) {
738 efi_err("Failed to parse options\n");
742 if (!IS_ENABLED(CONFIG_CMDLINE_OVERRIDE)) {
743 unsigned long cmdline_paddr = ((u64)hdr->cmd_line_ptr |
744 ((u64)boot_params->ext_cmd_line_ptr << 32));
745 status = efi_parse_options((char *)cmdline_paddr);
746 if (status != EFI_SUCCESS) {
747 efi_err("Failed to parse options\n");
753 * At this point, an initrd may already have been loaded by the
754 * bootloader and passed via bootparams. We permit an initrd loaded
755 * from the LINUX_EFI_INITRD_MEDIA_GUID device path to supersede it.
757 * If the device path is not present, any command-line initrd=
758 * arguments will be processed only if image is not NULL, which will be
759 * the case only if we were loaded via the PE entry point.
762 unsigned long addr, size;
764 status = efi_load_initrd(image, &addr, &size,
765 hdr->initrd_addr_max, ULONG_MAX);
767 if (status != EFI_SUCCESS) {
768 efi_err("Failed to load initrd!\n");
772 efi_set_u64_split(addr, &hdr->ramdisk_image,
773 &boot_params->ext_ramdisk_image);
774 efi_set_u64_split(size, &hdr->ramdisk_size,
775 &boot_params->ext_ramdisk_size);
780 * If the boot loader gave us a value for secure_boot then we use that,
781 * otherwise we ask the BIOS.
783 if (boot_params->secure_boot == efi_secureboot_mode_unset)
784 boot_params->secure_boot = efi_get_secureboot();
786 /* Ask the firmware to clear memory on unclean shutdown */
787 efi_enable_reset_attack_mitigation();
789 efi_random_get_seed();
791 efi_retrieve_tpm2_eventlog();
793 setup_graphics(boot_params);
795 setup_efi_pci(boot_params);
797 setup_quirks(boot_params);
799 status = exit_boot(boot_params, handle);
800 if (status != EFI_SUCCESS) {
801 efi_err("exit_boot() failed!\n");
807 efi_err("efi_main() failed!\n");
809 efi_exit(handle, status);