1 // SPDX-License-Identifier: GPL-2.0-only
3 /* -----------------------------------------------------------------------
5 * Copyright 2011 Intel Corporation; author Matt Fleming
7 * ----------------------------------------------------------------------- */
9 #pragma GCC visibility push(hidden)
11 #include <linux/efi.h>
12 #include <linux/pci.h>
15 #include <asm/e820/types.h>
16 #include <asm/setup.h>
20 #include "../string.h"
23 static efi_system_table_t *sys_table;
24 extern const bool efi_is64;
26 __pure efi_system_table_t *efi_system_table(void)
31 __attribute_const__ bool efi_is_64bit(void)
33 if (IS_ENABLED(CONFIG_EFI_MIXED))
35 return IS_ENABLED(CONFIG_X86_64);
39 preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
41 struct pci_setup_rom *rom = NULL;
48 * Some firmware images contain EFI function pointers at the place where
49 * the romimage and romsize fields are supposed to be. Typically the EFI
50 * code is mapped at high addresses, translating to an unrealistically
51 * large romsize. The UEFI spec limits the size of option ROMs to 16
52 * MiB so we reject any ROMs over 16 MiB in size to catch this.
54 romimage = efi_table_attr(pci, romimage);
55 romsize = efi_table_attr(pci, romsize);
56 if (!romimage || !romsize || romsize > SZ_16M)
57 return EFI_INVALID_PARAMETER;
59 size = romsize + sizeof(*rom);
61 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
63 if (status != EFI_SUCCESS) {
64 efi_printk("Failed to allocate memory for 'rom'\n");
68 memset(rom, 0, sizeof(*rom));
70 rom->data.type = SETUP_PCI;
71 rom->data.len = size - sizeof(struct setup_data);
73 rom->pcilen = pci->romsize;
76 status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
77 PCI_VENDOR_ID, 1, &rom->vendor);
79 if (status != EFI_SUCCESS) {
80 efi_printk("Failed to read rom->vendor\n");
84 status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
85 PCI_DEVICE_ID, 1, &rom->devid);
87 if (status != EFI_SUCCESS) {
88 efi_printk("Failed to read rom->devid\n");
92 status = efi_call_proto(pci, get_location, &rom->segment, &rom->bus,
93 &rom->device, &rom->function);
95 if (status != EFI_SUCCESS)
98 memcpy(rom->romdata, romimage, romsize);
102 efi_bs_call(free_pool, rom);
107 * There's no way to return an informative status from this function,
108 * because any analysis (and printing of error messages) needs to be
109 * done directly at the EFI function call-site.
111 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
112 * just didn't find any PCI devices, but there's no way to tell outside
113 * the context of the call.
115 static void setup_efi_pci(struct boot_params *params)
118 void **pci_handle = NULL;
119 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
120 unsigned long size = 0;
121 struct setup_data *data;
125 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
126 &pci_proto, NULL, &size, pci_handle);
128 if (status == EFI_BUFFER_TOO_SMALL) {
129 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
130 (void **)&pci_handle);
132 if (status != EFI_SUCCESS) {
133 efi_printk("Failed to allocate memory for 'pci_handle'\n");
137 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
138 &pci_proto, NULL, &size, pci_handle);
141 if (status != EFI_SUCCESS)
144 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
146 while (data && data->next)
147 data = (struct setup_data *)(unsigned long)data->next;
149 for_each_efi_handle(h, pci_handle, size, i) {
150 efi_pci_io_protocol_t *pci = NULL;
151 struct pci_setup_rom *rom;
153 status = efi_bs_call(handle_protocol, h, &pci_proto,
155 if (status != EFI_SUCCESS || !pci)
158 status = preserve_pci_rom_image(pci, &rom);
159 if (status != EFI_SUCCESS)
163 data->next = (unsigned long)rom;
165 params->hdr.setup_data = (unsigned long)rom;
167 data = (struct setup_data *)rom;
171 efi_bs_call(free_pool, pci_handle);
174 static void retrieve_apple_device_properties(struct boot_params *boot_params)
176 efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
177 struct setup_data *data, *new;
180 apple_properties_protocol_t *p;
182 status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&p);
183 if (status != EFI_SUCCESS)
186 if (efi_table_attr(p, version) != 0x10000) {
187 efi_printk("Unsupported properties proto version\n");
191 efi_call_proto(p, get_all, NULL, &size);
196 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
197 size + sizeof(struct setup_data),
199 if (status != EFI_SUCCESS) {
200 efi_printk("Failed to allocate memory for 'properties'\n");
204 status = efi_call_proto(p, get_all, new->data, &size);
206 if (status == EFI_BUFFER_TOO_SMALL)
207 efi_bs_call(free_pool, new);
208 } while (status == EFI_BUFFER_TOO_SMALL);
210 new->type = SETUP_APPLE_PROPERTIES;
214 data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
216 boot_params->hdr.setup_data = (unsigned long)new;
219 data = (struct setup_data *)(unsigned long)data->next;
220 data->next = (unsigned long)new;
224 static const efi_char16_t apple[] = L"Apple";
226 static void setup_quirks(struct boot_params *boot_params)
228 efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
229 efi_table_attr(efi_system_table(), fw_vendor);
231 if (!memcmp(fw_vendor, apple, sizeof(apple))) {
232 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
233 retrieve_apple_device_properties(boot_params);
238 * See if we have Universal Graphics Adapter (UGA) protocol
241 setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size)
245 void **uga_handle = NULL;
246 efi_uga_draw_protocol_t *uga = NULL, *first_uga;
250 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
251 (void **)&uga_handle);
252 if (status != EFI_SUCCESS)
255 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
256 uga_proto, NULL, &size, uga_handle);
257 if (status != EFI_SUCCESS)
264 for_each_efi_handle(handle, uga_handle, size, i) {
265 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
266 u32 w, h, depth, refresh;
269 status = efi_bs_call(handle_protocol, handle, uga_proto,
271 if (status != EFI_SUCCESS)
275 efi_bs_call(handle_protocol, handle, &pciio_proto, &pciio);
277 status = efi_call_proto(uga, get_mode, &w, &h, &depth, &refresh);
278 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
283 * Once we've found a UGA supporting PCIIO,
284 * don't bother looking any further.
293 if (!width && !height)
296 /* EFI framebuffer */
297 si->orig_video_isVGA = VIDEO_TYPE_EFI;
300 si->lfb_width = width;
301 si->lfb_height = height;
313 efi_bs_call(free_pool, uga_handle);
318 void setup_graphics(struct boot_params *boot_params)
320 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
321 struct screen_info *si;
322 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
325 void **gop_handle = NULL;
326 void **uga_handle = NULL;
328 si = &boot_params->screen_info;
329 memset(si, 0, sizeof(*si));
332 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
333 &graphics_proto, NULL, &size, gop_handle);
334 if (status == EFI_BUFFER_TOO_SMALL)
335 status = efi_setup_gop(si, &graphics_proto, size);
337 if (status != EFI_SUCCESS) {
339 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
340 &uga_proto, NULL, &size, uga_handle);
341 if (status == EFI_BUFFER_TOO_SMALL)
342 setup_uga(si, &uga_proto, size);
346 void startup_32(struct boot_params *boot_params);
348 void __noreturn efi_stub_entry(efi_handle_t handle,
349 efi_system_table_t *sys_table_arg,
350 struct boot_params *boot_params);
353 * Because the x86 boot code expects to be passed a boot_params we
354 * need to create one ourselves (usually the bootloader would create
357 efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
358 efi_system_table_t *sys_table_arg)
360 struct boot_params *boot_params;
361 struct apm_bios_info *bi;
362 struct setup_header *hdr;
363 efi_loaded_image_t *image;
364 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
365 int options_size = 0;
368 unsigned long ramdisk_addr;
369 unsigned long ramdisk_size;
371 sys_table = sys_table_arg;
373 /* Check if we were booted by the EFI firmware */
374 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
375 return EFI_INVALID_PARAMETER;
377 status = efi_bs_call(handle_protocol, handle, &proto, (void *)&image);
378 if (status != EFI_SUCCESS) {
379 efi_printk("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
383 status = efi_low_alloc(0x4000, 1, (unsigned long *)&boot_params);
384 if (status != EFI_SUCCESS) {
385 efi_printk("Failed to allocate lowmem for boot params\n");
389 memset(boot_params, 0x0, 0x4000);
391 hdr = &boot_params->hdr;
392 bi = &boot_params->apm_bios_info;
394 /* Copy the second sector to boot_params */
395 memcpy(&hdr->jump, image->image_base + 512, 512);
398 * Fill out some of the header fields ourselves because the
399 * EFI firmware loader doesn't load the first sector.
402 hdr->vid_mode = 0xffff;
403 hdr->boot_flag = 0xAA55;
405 hdr->type_of_loader = 0x21;
407 /* Convert unicode cmdline to ascii */
408 cmdline_ptr = efi_convert_cmdline(image, &options_size);
412 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
413 /* Fill in upper bits of command line address, NOP on 32 bit */
414 boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32;
416 hdr->ramdisk_image = 0;
417 hdr->ramdisk_size = 0;
419 /* Clear APM BIOS info */
420 memset(bi, 0, sizeof(*bi));
422 status = efi_parse_options(cmdline_ptr);
423 if (status != EFI_SUCCESS)
426 status = handle_cmdline_files(image,
427 (char *)(unsigned long)hdr->cmd_line_ptr,
428 "initrd=", hdr->initrd_addr_max,
429 &ramdisk_addr, &ramdisk_size);
431 if (status != EFI_SUCCESS &&
432 hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) {
433 efi_printk("Trying to load files to higher address\n");
434 status = handle_cmdline_files(image,
435 (char *)(unsigned long)hdr->cmd_line_ptr,
437 &ramdisk_addr, &ramdisk_size);
440 if (status != EFI_SUCCESS)
442 hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
443 hdr->ramdisk_size = ramdisk_size & 0xffffffff;
444 boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
445 boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32;
447 hdr->code32_start = (u32)(unsigned long)startup_32;
449 efi_stub_entry(handle, sys_table, boot_params);
453 efi_free(options_size, hdr->cmd_line_ptr);
455 efi_free(0x4000, (unsigned long)boot_params);
460 static void add_e820ext(struct boot_params *params,
461 struct setup_data *e820ext, u32 nr_entries)
463 struct setup_data *data;
465 e820ext->type = SETUP_E820_EXT;
466 e820ext->len = nr_entries * sizeof(struct boot_e820_entry);
469 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
471 while (data && data->next)
472 data = (struct setup_data *)(unsigned long)data->next;
475 data->next = (unsigned long)e820ext;
477 params->hdr.setup_data = (unsigned long)e820ext;
481 setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size)
483 struct boot_e820_entry *entry = params->e820_table;
484 struct efi_info *efi = ¶ms->efi_info;
485 struct boot_e820_entry *prev = NULL;
491 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
493 for (i = 0; i < nr_desc; i++) {
494 efi_memory_desc_t *d;
495 unsigned int e820_type = 0;
496 unsigned long m = efi->efi_memmap;
499 m |= (u64)efi->efi_memmap_hi << 32;
502 d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
504 case EFI_RESERVED_TYPE:
505 case EFI_RUNTIME_SERVICES_CODE:
506 case EFI_RUNTIME_SERVICES_DATA:
507 case EFI_MEMORY_MAPPED_IO:
508 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
510 e820_type = E820_TYPE_RESERVED;
513 case EFI_UNUSABLE_MEMORY:
514 e820_type = E820_TYPE_UNUSABLE;
517 case EFI_ACPI_RECLAIM_MEMORY:
518 e820_type = E820_TYPE_ACPI;
521 case EFI_LOADER_CODE:
522 case EFI_LOADER_DATA:
523 case EFI_BOOT_SERVICES_CODE:
524 case EFI_BOOT_SERVICES_DATA:
525 case EFI_CONVENTIONAL_MEMORY:
526 if (efi_soft_reserve_enabled() &&
527 (d->attribute & EFI_MEMORY_SP))
528 e820_type = E820_TYPE_SOFT_RESERVED;
530 e820_type = E820_TYPE_RAM;
533 case EFI_ACPI_MEMORY_NVS:
534 e820_type = E820_TYPE_NVS;
537 case EFI_PERSISTENT_MEMORY:
538 e820_type = E820_TYPE_PMEM;
545 /* Merge adjacent mappings */
546 if (prev && prev->type == e820_type &&
547 (prev->addr + prev->size) == d->phys_addr) {
548 prev->size += d->num_pages << 12;
552 if (nr_entries == ARRAY_SIZE(params->e820_table)) {
553 u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
554 sizeof(struct setup_data);
556 if (!e820ext || e820ext_size < need)
557 return EFI_BUFFER_TOO_SMALL;
559 /* boot_params map full, switch to e820 extended */
560 entry = (struct boot_e820_entry *)e820ext->data;
563 entry->addr = d->phys_addr;
564 entry->size = d->num_pages << PAGE_SHIFT;
565 entry->type = e820_type;
570 if (nr_entries > ARRAY_SIZE(params->e820_table)) {
571 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
573 add_e820ext(params, e820ext, nr_e820ext);
574 nr_entries -= nr_e820ext;
577 params->e820_entries = (u8)nr_entries;
582 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
588 size = sizeof(struct setup_data) +
589 sizeof(struct e820_entry) * nr_desc;
592 efi_bs_call(free_pool, *e820ext);
597 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
599 if (status == EFI_SUCCESS)
600 *e820ext_size = size;
605 static efi_status_t allocate_e820(struct boot_params *params,
606 struct setup_data **e820ext,
609 unsigned long map_size, desc_size, buff_size;
610 struct efi_boot_memmap boot_map;
611 efi_memory_desc_t *map;
616 boot_map.map_size = &map_size;
617 boot_map.desc_size = &desc_size;
618 boot_map.desc_ver = NULL;
619 boot_map.key_ptr = NULL;
620 boot_map.buff_size = &buff_size;
622 status = efi_get_memory_map(&boot_map);
623 if (status != EFI_SUCCESS)
626 nr_desc = buff_size / desc_size;
628 if (nr_desc > ARRAY_SIZE(params->e820_table)) {
629 u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table);
631 status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size);
632 if (status != EFI_SUCCESS)
639 struct exit_boot_struct {
640 struct boot_params *boot_params;
641 struct efi_info *efi;
644 static efi_status_t exit_boot_func(struct efi_boot_memmap *map,
647 const char *signature;
648 struct exit_boot_struct *p = priv;
650 signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE
651 : EFI32_LOADER_SIGNATURE;
652 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
654 p->efi->efi_systab = (unsigned long)efi_system_table();
655 p->efi->efi_memdesc_size = *map->desc_size;
656 p->efi->efi_memdesc_version = *map->desc_ver;
657 p->efi->efi_memmap = (unsigned long)*map->map;
658 p->efi->efi_memmap_size = *map->map_size;
661 p->efi->efi_systab_hi = (unsigned long)efi_system_table() >> 32;
662 p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32;
668 static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)
670 unsigned long map_sz, key, desc_size, buff_size;
671 efi_memory_desc_t *mem_map;
672 struct setup_data *e820ext = NULL;
673 __u32 e820ext_size = 0;
676 struct efi_boot_memmap map;
677 struct exit_boot_struct priv;
680 map.map_size = &map_sz;
681 map.desc_size = &desc_size;
682 map.desc_ver = &desc_version;
684 map.buff_size = &buff_size;
685 priv.boot_params = boot_params;
686 priv.efi = &boot_params->efi_info;
688 status = allocate_e820(boot_params, &e820ext, &e820ext_size);
689 if (status != EFI_SUCCESS)
692 /* Might as well exit boot services now */
693 status = efi_exit_boot_services(handle, &map, &priv, exit_boot_func);
694 if (status != EFI_SUCCESS)
698 boot_params->alt_mem_k = 32 * 1024;
700 status = setup_e820(boot_params, e820ext, e820ext_size);
701 if (status != EFI_SUCCESS)
708 * On success we return a pointer to a boot_params structure, and NULL
711 struct boot_params *efi_main(efi_handle_t handle,
712 efi_system_table_t *sys_table_arg,
713 struct boot_params *boot_params)
715 struct desc_ptr *gdt = NULL;
716 struct setup_header *hdr = &boot_params->hdr;
718 struct desc_struct *desc;
719 unsigned long cmdline_paddr;
721 sys_table = sys_table_arg;
723 /* Check if we were booted by the EFI firmware */
724 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
728 * make_boot_params() may have been called before efi_main(), in which
729 * case this is the second time we parse the cmdline. This is ok,
730 * parsing the cmdline multiple times does not have side-effects.
732 cmdline_paddr = ((u64)hdr->cmd_line_ptr |
733 ((u64)boot_params->ext_cmd_line_ptr << 32));
734 efi_parse_options((char *)cmdline_paddr);
737 * If the boot loader gave us a value for secure_boot then we use that,
738 * otherwise we ask the BIOS.
740 if (boot_params->secure_boot == efi_secureboot_mode_unset)
741 boot_params->secure_boot = efi_get_secureboot();
743 /* Ask the firmware to clear memory on unclean shutdown */
744 efi_enable_reset_attack_mitigation();
746 efi_random_get_seed();
748 efi_retrieve_tpm2_eventlog();
750 setup_graphics(boot_params);
752 setup_efi_pci(boot_params);
754 setup_quirks(boot_params);
756 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, sizeof(*gdt),
758 if (status != EFI_SUCCESS) {
759 efi_printk("Failed to allocate memory for 'gdt' structure\n");
764 status = efi_low_alloc(gdt->size, 8, (unsigned long *)&gdt->address);
765 if (status != EFI_SUCCESS) {
766 efi_printk("Failed to allocate memory for 'gdt'\n");
771 * If the kernel isn't already loaded at the preferred load
772 * address, relocate it.
774 if (hdr->pref_address != hdr->code32_start) {
775 unsigned long bzimage_addr = hdr->code32_start;
776 status = efi_relocate_kernel(&bzimage_addr,
777 hdr->init_size, hdr->init_size,
779 hdr->kernel_alignment,
781 if (status != EFI_SUCCESS) {
782 efi_printk("efi_relocate_kernel() failed!\n");
786 hdr->pref_address = hdr->code32_start;
787 hdr->code32_start = bzimage_addr;
790 status = exit_boot(boot_params, handle);
791 if (status != EFI_SUCCESS) {
792 efi_printk("exit_boot() failed!\n");
796 memset((char *)gdt->address, 0x0, gdt->size);
797 desc = (struct desc_struct *)gdt->address;
799 /* The first GDT is a dummy. */
802 if (IS_ENABLED(CONFIG_X86_64)) {
804 desc->limit0 = 0xffff;
805 desc->base0 = 0x0000;
806 desc->base1 = 0x0000;
807 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
808 desc->s = DESC_TYPE_CODE_DATA;
814 desc->d = SEG_OP_SIZE_32BIT;
815 desc->g = SEG_GRANULARITY_4KB;
820 /* Second entry is unused on 32-bit */
825 desc->limit0 = 0xffff;
826 desc->base0 = 0x0000;
827 desc->base1 = 0x0000;
828 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
829 desc->s = DESC_TYPE_CODE_DATA;
835 if (IS_ENABLED(CONFIG_X86_64)) {
840 desc->d = SEG_OP_SIZE_32BIT;
842 desc->g = SEG_GRANULARITY_4KB;
847 desc->limit0 = 0xffff;
848 desc->base0 = 0x0000;
849 desc->base1 = 0x0000;
850 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
851 desc->s = DESC_TYPE_CODE_DATA;
857 desc->d = SEG_OP_SIZE_32BIT;
858 desc->g = SEG_GRANULARITY_4KB;
862 if (IS_ENABLED(CONFIG_X86_64)) {
863 /* Task segment value */
864 desc->limit0 = 0x0000;
865 desc->base0 = 0x0000;
866 desc->base1 = 0x0000;
867 desc->type = SEG_TYPE_TSS;
875 desc->g = SEG_GRANULARITY_4KB;
881 asm volatile ("lgdt %0" : : "m" (*gdt));
885 efi_printk("efi_main() failed!\n");