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 static efi_system_table_t *sys_table;
21 extern const bool efi_is64;
23 __pure efi_system_table_t *efi_system_table(void)
28 __attribute_const__ bool efi_is_64bit(void)
30 if (IS_ENABLED(CONFIG_EFI_MIXED))
32 return IS_ENABLED(CONFIG_X86_64);
36 preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
38 struct pci_setup_rom *rom = NULL;
45 * Some firmware images contain EFI function pointers at the place where
46 * the romimage and romsize fields are supposed to be. Typically the EFI
47 * code is mapped at high addresses, translating to an unrealistically
48 * large romsize. The UEFI spec limits the size of option ROMs to 16
49 * MiB so we reject any ROMs over 16 MiB in size to catch this.
51 romimage = efi_table_attr(pci, romimage);
52 romsize = efi_table_attr(pci, romsize);
53 if (!romimage || !romsize || romsize > SZ_16M)
54 return EFI_INVALID_PARAMETER;
56 size = romsize + sizeof(*rom);
58 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
60 if (status != EFI_SUCCESS) {
61 efi_printk("Failed to allocate memory for 'rom'\n");
65 memset(rom, 0, sizeof(*rom));
67 rom->data.type = SETUP_PCI;
68 rom->data.len = size - sizeof(struct setup_data);
70 rom->pcilen = pci->romsize;
73 status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
74 PCI_VENDOR_ID, 1, &rom->vendor);
76 if (status != EFI_SUCCESS) {
77 efi_printk("Failed to read rom->vendor\n");
81 status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
82 PCI_DEVICE_ID, 1, &rom->devid);
84 if (status != EFI_SUCCESS) {
85 efi_printk("Failed to read rom->devid\n");
89 status = efi_call_proto(pci, get_location, &rom->segment, &rom->bus,
90 &rom->device, &rom->function);
92 if (status != EFI_SUCCESS)
95 memcpy(rom->romdata, romimage, romsize);
99 efi_bs_call(free_pool, rom);
104 * There's no way to return an informative status from this function,
105 * because any analysis (and printing of error messages) needs to be
106 * done directly at the EFI function call-site.
108 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
109 * just didn't find any PCI devices, but there's no way to tell outside
110 * the context of the call.
112 static void setup_efi_pci(struct boot_params *params)
115 void **pci_handle = NULL;
116 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
117 unsigned long size = 0;
118 struct setup_data *data;
122 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
123 &pci_proto, NULL, &size, pci_handle);
125 if (status == EFI_BUFFER_TOO_SMALL) {
126 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
127 (void **)&pci_handle);
129 if (status != EFI_SUCCESS) {
130 efi_printk("Failed to allocate memory for 'pci_handle'\n");
134 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
135 &pci_proto, NULL, &size, pci_handle);
138 if (status != EFI_SUCCESS)
141 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
143 while (data && data->next)
144 data = (struct setup_data *)(unsigned long)data->next;
146 for_each_efi_handle(h, pci_handle, size, i) {
147 efi_pci_io_protocol_t *pci = NULL;
148 struct pci_setup_rom *rom;
150 status = efi_bs_call(handle_protocol, h, &pci_proto,
152 if (status != EFI_SUCCESS || !pci)
155 status = preserve_pci_rom_image(pci, &rom);
156 if (status != EFI_SUCCESS)
160 data->next = (unsigned long)rom;
162 params->hdr.setup_data = (unsigned long)rom;
164 data = (struct setup_data *)rom;
168 efi_bs_call(free_pool, pci_handle);
171 static void retrieve_apple_device_properties(struct boot_params *boot_params)
173 efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
174 struct setup_data *data, *new;
177 apple_properties_protocol_t *p;
179 status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&p);
180 if (status != EFI_SUCCESS)
183 if (efi_table_attr(p, version) != 0x10000) {
184 efi_printk("Unsupported properties proto version\n");
188 efi_call_proto(p, get_all, NULL, &size);
193 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
194 size + sizeof(struct setup_data),
196 if (status != EFI_SUCCESS) {
197 efi_printk("Failed to allocate memory for 'properties'\n");
201 status = efi_call_proto(p, get_all, new->data, &size);
203 if (status == EFI_BUFFER_TOO_SMALL)
204 efi_bs_call(free_pool, new);
205 } while (status == EFI_BUFFER_TOO_SMALL);
207 new->type = SETUP_APPLE_PROPERTIES;
211 data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
213 boot_params->hdr.setup_data = (unsigned long)new;
216 data = (struct setup_data *)(unsigned long)data->next;
217 data->next = (unsigned long)new;
221 static const efi_char16_t apple[] = L"Apple";
223 static void setup_quirks(struct boot_params *boot_params)
225 efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
226 efi_table_attr(efi_system_table(), fw_vendor);
228 if (!memcmp(fw_vendor, apple, sizeof(apple))) {
229 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
230 retrieve_apple_device_properties(boot_params);
235 * See if we have Universal Graphics Adapter (UGA) protocol
238 setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size)
242 void **uga_handle = NULL;
243 efi_uga_draw_protocol_t *uga = NULL, *first_uga;
247 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
248 (void **)&uga_handle);
249 if (status != EFI_SUCCESS)
252 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
253 uga_proto, NULL, &size, uga_handle);
254 if (status != EFI_SUCCESS)
261 for_each_efi_handle(handle, uga_handle, size, i) {
262 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
263 u32 w, h, depth, refresh;
266 status = efi_bs_call(handle_protocol, handle, uga_proto,
268 if (status != EFI_SUCCESS)
272 efi_bs_call(handle_protocol, handle, &pciio_proto, &pciio);
274 status = efi_call_proto(uga, get_mode, &w, &h, &depth, &refresh);
275 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
280 * Once we've found a UGA supporting PCIIO,
281 * don't bother looking any further.
290 if (!width && !height)
293 /* EFI framebuffer */
294 si->orig_video_isVGA = VIDEO_TYPE_EFI;
297 si->lfb_width = width;
298 si->lfb_height = height;
310 efi_bs_call(free_pool, uga_handle);
315 static void setup_graphics(struct boot_params *boot_params)
317 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
318 struct screen_info *si;
319 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
322 void **gop_handle = NULL;
323 void **uga_handle = NULL;
325 si = &boot_params->screen_info;
326 memset(si, 0, sizeof(*si));
329 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
330 &graphics_proto, NULL, &size, gop_handle);
331 if (status == EFI_BUFFER_TOO_SMALL)
332 status = efi_setup_gop(si, &graphics_proto, size);
334 if (status != EFI_SUCCESS) {
336 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
337 &uga_proto, NULL, &size, uga_handle);
338 if (status == EFI_BUFFER_TOO_SMALL)
339 setup_uga(si, &uga_proto, size);
344 static void __noreturn efi_exit(efi_handle_t handle, efi_status_t status)
346 efi_bs_call(exit, handle, status, 0, NULL);
350 void startup_32(struct boot_params *boot_params);
352 void __noreturn efi_stub_entry(efi_handle_t handle,
353 efi_system_table_t *sys_table_arg,
354 struct boot_params *boot_params);
357 * Because the x86 boot code expects to be passed a boot_params we
358 * need to create one ourselves (usually the bootloader would create
361 efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
362 efi_system_table_t *sys_table_arg)
364 struct boot_params *boot_params;
365 struct setup_header *hdr;
366 efi_loaded_image_t *image;
367 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
368 int options_size = 0;
371 unsigned long ramdisk_addr;
372 unsigned long ramdisk_size;
375 sys_table = sys_table_arg;
377 /* Check if we were booted by the EFI firmware */
378 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
379 efi_exit(handle, EFI_INVALID_PARAMETER);
381 status = efi_bs_call(handle_protocol, handle, &proto, (void *)&image);
382 if (status != EFI_SUCCESS) {
383 efi_printk("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
384 efi_exit(handle, status);
387 hdr = &((struct boot_params *)efi_table_attr(image, image_base))->hdr;
388 above4g = hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G;
390 status = efi_allocate_pages(0x4000, (unsigned long *)&boot_params,
391 above4g ? ULONG_MAX : UINT_MAX);
392 if (status != EFI_SUCCESS) {
393 efi_printk("Failed to allocate lowmem for boot params\n");
394 efi_exit(handle, status);
397 memset(boot_params, 0x0, 0x4000);
399 hdr = &boot_params->hdr;
401 /* Copy the second sector to boot_params */
402 memcpy(&hdr->jump, efi_table_attr(image, image_base) + 512, 512);
405 * Fill out some of the header fields ourselves because the
406 * EFI firmware loader doesn't load the first sector.
409 hdr->vid_mode = 0xffff;
410 hdr->boot_flag = 0xAA55;
412 hdr->type_of_loader = 0x21;
414 /* Convert unicode cmdline to ascii */
415 cmdline_ptr = efi_convert_cmdline(image, &options_size,
416 above4g ? ULONG_MAX : UINT_MAX);
420 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
421 /* Fill in upper bits of command line address, NOP on 32 bit */
422 boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32;
424 hdr->ramdisk_image = 0;
425 hdr->ramdisk_size = 0;
427 status = efi_parse_options(cmdline_ptr);
428 if (status != EFI_SUCCESS)
432 status = efi_load_initrd(image, &ramdisk_addr, &ramdisk_size,
433 hdr->initrd_addr_max,
435 : hdr->initrd_addr_max);
436 if (status != EFI_SUCCESS)
438 hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
439 hdr->ramdisk_size = ramdisk_size & 0xffffffff;
440 boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
441 boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32;
444 efi_stub_entry(handle, sys_table, boot_params);
448 efi_free(options_size, (unsigned long)cmdline_ptr);
450 efi_free(0x4000, (unsigned long)boot_params);
452 efi_exit(handle, status);
455 static void add_e820ext(struct boot_params *params,
456 struct setup_data *e820ext, u32 nr_entries)
458 struct setup_data *data;
460 e820ext->type = SETUP_E820_EXT;
461 e820ext->len = nr_entries * sizeof(struct boot_e820_entry);
464 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
466 while (data && data->next)
467 data = (struct setup_data *)(unsigned long)data->next;
470 data->next = (unsigned long)e820ext;
472 params->hdr.setup_data = (unsigned long)e820ext;
476 setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size)
478 struct boot_e820_entry *entry = params->e820_table;
479 struct efi_info *efi = ¶ms->efi_info;
480 struct boot_e820_entry *prev = NULL;
486 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
488 for (i = 0; i < nr_desc; i++) {
489 efi_memory_desc_t *d;
490 unsigned int e820_type = 0;
491 unsigned long m = efi->efi_memmap;
494 m |= (u64)efi->efi_memmap_hi << 32;
497 d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
499 case EFI_RESERVED_TYPE:
500 case EFI_RUNTIME_SERVICES_CODE:
501 case EFI_RUNTIME_SERVICES_DATA:
502 case EFI_MEMORY_MAPPED_IO:
503 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
505 e820_type = E820_TYPE_RESERVED;
508 case EFI_UNUSABLE_MEMORY:
509 e820_type = E820_TYPE_UNUSABLE;
512 case EFI_ACPI_RECLAIM_MEMORY:
513 e820_type = E820_TYPE_ACPI;
516 case EFI_LOADER_CODE:
517 case EFI_LOADER_DATA:
518 case EFI_BOOT_SERVICES_CODE:
519 case EFI_BOOT_SERVICES_DATA:
520 case EFI_CONVENTIONAL_MEMORY:
521 if (efi_soft_reserve_enabled() &&
522 (d->attribute & EFI_MEMORY_SP))
523 e820_type = E820_TYPE_SOFT_RESERVED;
525 e820_type = E820_TYPE_RAM;
528 case EFI_ACPI_MEMORY_NVS:
529 e820_type = E820_TYPE_NVS;
532 case EFI_PERSISTENT_MEMORY:
533 e820_type = E820_TYPE_PMEM;
540 /* Merge adjacent mappings */
541 if (prev && prev->type == e820_type &&
542 (prev->addr + prev->size) == d->phys_addr) {
543 prev->size += d->num_pages << 12;
547 if (nr_entries == ARRAY_SIZE(params->e820_table)) {
548 u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
549 sizeof(struct setup_data);
551 if (!e820ext || e820ext_size < need)
552 return EFI_BUFFER_TOO_SMALL;
554 /* boot_params map full, switch to e820 extended */
555 entry = (struct boot_e820_entry *)e820ext->data;
558 entry->addr = d->phys_addr;
559 entry->size = d->num_pages << PAGE_SHIFT;
560 entry->type = e820_type;
565 if (nr_entries > ARRAY_SIZE(params->e820_table)) {
566 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
568 add_e820ext(params, e820ext, nr_e820ext);
569 nr_entries -= nr_e820ext;
572 params->e820_entries = (u8)nr_entries;
577 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
583 size = sizeof(struct setup_data) +
584 sizeof(struct e820_entry) * nr_desc;
587 efi_bs_call(free_pool, *e820ext);
592 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
594 if (status == EFI_SUCCESS)
595 *e820ext_size = size;
600 static efi_status_t allocate_e820(struct boot_params *params,
601 struct setup_data **e820ext,
604 unsigned long map_size, desc_size, buff_size;
605 struct efi_boot_memmap boot_map;
606 efi_memory_desc_t *map;
611 boot_map.map_size = &map_size;
612 boot_map.desc_size = &desc_size;
613 boot_map.desc_ver = NULL;
614 boot_map.key_ptr = NULL;
615 boot_map.buff_size = &buff_size;
617 status = efi_get_memory_map(&boot_map);
618 if (status != EFI_SUCCESS)
621 nr_desc = buff_size / desc_size;
623 if (nr_desc > ARRAY_SIZE(params->e820_table)) {
624 u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table);
626 status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size);
627 if (status != EFI_SUCCESS)
634 struct exit_boot_struct {
635 struct boot_params *boot_params;
636 struct efi_info *efi;
639 static efi_status_t exit_boot_func(struct efi_boot_memmap *map,
642 const char *signature;
643 struct exit_boot_struct *p = priv;
645 signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE
646 : EFI32_LOADER_SIGNATURE;
647 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
649 p->efi->efi_systab = (unsigned long)efi_system_table();
650 p->efi->efi_memdesc_size = *map->desc_size;
651 p->efi->efi_memdesc_version = *map->desc_ver;
652 p->efi->efi_memmap = (unsigned long)*map->map;
653 p->efi->efi_memmap_size = *map->map_size;
656 p->efi->efi_systab_hi = (unsigned long)efi_system_table() >> 32;
657 p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32;
663 static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)
665 unsigned long map_sz, key, desc_size, buff_size;
666 efi_memory_desc_t *mem_map;
667 struct setup_data *e820ext = NULL;
668 __u32 e820ext_size = 0;
671 struct efi_boot_memmap map;
672 struct exit_boot_struct priv;
675 map.map_size = &map_sz;
676 map.desc_size = &desc_size;
677 map.desc_ver = &desc_version;
679 map.buff_size = &buff_size;
680 priv.boot_params = boot_params;
681 priv.efi = &boot_params->efi_info;
683 status = allocate_e820(boot_params, &e820ext, &e820ext_size);
684 if (status != EFI_SUCCESS)
687 /* Might as well exit boot services now */
688 status = efi_exit_boot_services(handle, &map, &priv, exit_boot_func);
689 if (status != EFI_SUCCESS)
693 boot_params->alt_mem_k = 32 * 1024;
695 status = setup_e820(boot_params, e820ext, e820ext_size);
696 if (status != EFI_SUCCESS)
703 * On success we return a pointer to a boot_params structure, and NULL
706 struct boot_params *efi_main(efi_handle_t handle,
707 efi_system_table_t *sys_table_arg,
708 struct boot_params *boot_params)
710 unsigned long bzimage_addr = (unsigned long)startup_32;
711 struct setup_header *hdr = &boot_params->hdr;
713 unsigned long cmdline_paddr;
715 sys_table = sys_table_arg;
717 /* Check if we were booted by the EFI firmware */
718 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
719 efi_exit(handle, EFI_INVALID_PARAMETER);
722 * If the kernel isn't already loaded at the preferred load
723 * address, relocate it.
725 if (bzimage_addr != hdr->pref_address) {
726 status = efi_relocate_kernel(&bzimage_addr,
727 hdr->init_size, hdr->init_size,
729 hdr->kernel_alignment,
731 if (status != EFI_SUCCESS) {
732 efi_printk("efi_relocate_kernel() failed!\n");
736 hdr->code32_start = (u32)bzimage_addr;
739 * efi_pe_entry() may have been called before efi_main(), in which
740 * case this is the second time we parse the cmdline. This is ok,
741 * parsing the cmdline multiple times does not have side-effects.
743 cmdline_paddr = ((u64)hdr->cmd_line_ptr |
744 ((u64)boot_params->ext_cmd_line_ptr << 32));
745 efi_parse_options((char *)cmdline_paddr);
748 * At this point, an initrd may already have been loaded, either by
749 * the bootloader and passed via bootparams, or loaded from a initrd=
750 * command line option by efi_pe_entry() above. In either case, we
751 * permit an initrd loaded from the LINUX_EFI_INITRD_MEDIA_GUID device
752 * path to supersede it.
755 unsigned long addr, size;
756 unsigned long max_addr = hdr->initrd_addr_max;
758 if (hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G)
759 max_addr = ULONG_MAX;
761 status = efi_load_initrd_dev_path(&addr, &size, max_addr);
762 if (status == EFI_SUCCESS) {
763 hdr->ramdisk_image = (u32)addr;
764 hdr->ramdisk_size = (u32)size;
765 boot_params->ext_ramdisk_image = (u64)addr >> 32;
766 boot_params->ext_ramdisk_size = (u64)size >> 32;
767 } else if (status != EFI_NOT_FOUND) {
768 efi_printk("efi_load_initrd_dev_path() failed!\n");
774 * If the boot loader gave us a value for secure_boot then we use that,
775 * otherwise we ask the BIOS.
777 if (boot_params->secure_boot == efi_secureboot_mode_unset)
778 boot_params->secure_boot = efi_get_secureboot();
780 /* Ask the firmware to clear memory on unclean shutdown */
781 efi_enable_reset_attack_mitigation();
783 efi_random_get_seed();
785 efi_retrieve_tpm2_eventlog();
787 setup_graphics(boot_params);
789 setup_efi_pci(boot_params);
791 setup_quirks(boot_params);
793 status = exit_boot(boot_params, handle);
794 if (status != EFI_SUCCESS) {
795 efi_printk("exit_boot() failed!\n");
801 efi_printk("efi_main() failed!\n");
803 efi_exit(handle, status);