1 /* -----------------------------------------------------------------------
3 * Copyright 2011 Intel Corporation; author Matt Fleming
5 * This file is part of the Linux kernel, and is made available under
6 * the terms of the GNU General Public License version 2.
8 * ----------------------------------------------------------------------- */
10 #include <linux/efi.h>
11 #include <linux/pci.h>
14 #include <asm/e820/types.h>
15 #include <asm/setup.h>
18 #include "../string.h"
21 static efi_system_table_t *sys_table;
23 static struct efi_config *efi_early;
25 __pure const struct efi_config *__efi_early(void)
30 #define BOOT_SERVICES(bits) \
31 static void setup_boot_services##bits(struct efi_config *c) \
33 efi_system_table_##bits##_t *table; \
35 table = (typeof(table))sys_table; \
37 c->runtime_services = table->runtime; \
38 c->boot_services = table->boottime; \
39 c->text_output = table->con_out; \
44 void efi_char16_printk(efi_system_table_t *table, efi_char16_t *str)
46 efi_call_proto(efi_simple_text_output_protocol, output_string,
47 efi_early->text_output, str);
51 preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
53 struct pci_setup_rom *rom = NULL;
60 * Some firmware images contain EFI function pointers at the place where
61 * the romimage and romsize fields are supposed to be. Typically the EFI
62 * code is mapped at high addresses, translating to an unrealistically
63 * large romsize. The UEFI spec limits the size of option ROMs to 16
64 * MiB so we reject any ROMs over 16 MiB in size to catch this.
66 romimage = (void *)(unsigned long)efi_table_attr(efi_pci_io_protocol,
68 romsize = efi_table_attr(efi_pci_io_protocol, romsize, pci);
69 if (!romimage || !romsize || romsize > SZ_16M)
70 return EFI_INVALID_PARAMETER;
72 size = romsize + sizeof(*rom);
74 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
75 if (status != EFI_SUCCESS) {
76 efi_printk(sys_table, "Failed to allocate memory for 'rom'\n");
80 memset(rom, 0, sizeof(*rom));
82 rom->data.type = SETUP_PCI;
83 rom->data.len = size - sizeof(struct setup_data);
85 rom->pcilen = pci->romsize;
88 status = efi_call_proto(efi_pci_io_protocol, pci.read, pci,
89 EfiPciIoWidthUint16, PCI_VENDOR_ID, 1,
92 if (status != EFI_SUCCESS) {
93 efi_printk(sys_table, "Failed to read rom->vendor\n");
97 status = efi_call_proto(efi_pci_io_protocol, pci.read, pci,
98 EfiPciIoWidthUint16, PCI_DEVICE_ID, 1,
101 if (status != EFI_SUCCESS) {
102 efi_printk(sys_table, "Failed to read rom->devid\n");
106 status = efi_call_proto(efi_pci_io_protocol, get_location, pci,
107 &rom->segment, &rom->bus, &rom->device,
110 if (status != EFI_SUCCESS)
113 memcpy(rom->romdata, romimage, romsize);
117 efi_call_early(free_pool, rom);
122 * There's no way to return an informative status from this function,
123 * because any analysis (and printing of error messages) needs to be
124 * done directly at the EFI function call-site.
126 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
127 * just didn't find any PCI devices, but there's no way to tell outside
128 * the context of the call.
130 static void setup_efi_pci(struct boot_params *params)
133 void **pci_handle = NULL;
134 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
135 unsigned long size = 0;
136 unsigned long nr_pci;
137 struct setup_data *data;
140 status = efi_call_early(locate_handle,
141 EFI_LOCATE_BY_PROTOCOL,
142 &pci_proto, NULL, &size, pci_handle);
144 if (status == EFI_BUFFER_TOO_SMALL) {
145 status = efi_call_early(allocate_pool,
147 size, (void **)&pci_handle);
149 if (status != EFI_SUCCESS) {
150 efi_printk(sys_table, "Failed to allocate memory for 'pci_handle'\n");
154 status = efi_call_early(locate_handle,
155 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
156 NULL, &size, pci_handle);
159 if (status != EFI_SUCCESS)
162 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
164 while (data && data->next)
165 data = (struct setup_data *)(unsigned long)data->next;
167 nr_pci = size / (efi_is_64bit() ? sizeof(u64) : sizeof(u32));
168 for (i = 0; i < nr_pci; i++) {
169 efi_pci_io_protocol_t *pci = NULL;
170 struct pci_setup_rom *rom;
172 status = efi_call_early(handle_protocol,
173 efi_is_64bit() ? ((u64 *)pci_handle)[i]
174 : ((u32 *)pci_handle)[i],
175 &pci_proto, (void **)&pci);
176 if (status != EFI_SUCCESS || !pci)
179 status = preserve_pci_rom_image(pci, &rom);
180 if (status != EFI_SUCCESS)
184 data->next = (unsigned long)rom;
186 params->hdr.setup_data = (unsigned long)rom;
188 data = (struct setup_data *)rom;
192 efi_call_early(free_pool, pci_handle);
195 static void retrieve_apple_device_properties(struct boot_params *boot_params)
197 efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
198 struct setup_data *data, *new;
203 status = efi_call_early(locate_protocol, &guid, NULL, &p);
204 if (status != EFI_SUCCESS)
207 if (efi_table_attr(apple_properties_protocol, version, p) != 0x10000) {
208 efi_printk(sys_table, "Unsupported properties proto version\n");
212 efi_call_proto(apple_properties_protocol, get_all, p, NULL, &size);
217 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
218 size + sizeof(struct setup_data), &new);
219 if (status != EFI_SUCCESS) {
220 efi_printk(sys_table, "Failed to allocate memory for 'properties'\n");
224 status = efi_call_proto(apple_properties_protocol, get_all, p,
227 if (status == EFI_BUFFER_TOO_SMALL)
228 efi_call_early(free_pool, new);
229 } while (status == EFI_BUFFER_TOO_SMALL);
231 new->type = SETUP_APPLE_PROPERTIES;
235 data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
237 boot_params->hdr.setup_data = (unsigned long)new;
240 data = (struct setup_data *)(unsigned long)data->next;
241 data->next = (unsigned long)new;
245 static const efi_char16_t apple[] = L"Apple";
247 static void setup_quirks(struct boot_params *boot_params)
249 efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
250 efi_table_attr(efi_system_table, fw_vendor, sys_table);
252 if (!memcmp(fw_vendor, apple, sizeof(apple))) {
253 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
254 retrieve_apple_device_properties(boot_params);
259 * See if we have Universal Graphics Adapter (UGA) protocol
262 setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size)
266 void **uga_handle = NULL;
267 efi_uga_draw_protocol_t *uga = NULL, *first_uga;
268 unsigned long nr_ugas;
271 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
272 size, (void **)&uga_handle);
273 if (status != EFI_SUCCESS)
276 status = efi_call_early(locate_handle,
277 EFI_LOCATE_BY_PROTOCOL,
278 uga_proto, NULL, &size, uga_handle);
279 if (status != EFI_SUCCESS)
286 nr_ugas = size / (efi_is_64bit() ? sizeof(u64) : sizeof(u32));
287 for (i = 0; i < nr_ugas; i++) {
288 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
289 u32 w, h, depth, refresh;
291 unsigned long handle = efi_is_64bit() ? ((u64 *)uga_handle)[i]
292 : ((u32 *)uga_handle)[i];
294 status = efi_call_early(handle_protocol, handle,
295 uga_proto, (void **)&uga);
296 if (status != EFI_SUCCESS)
300 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
302 status = efi_call_proto(efi_uga_draw_protocol, get_mode, uga,
303 &w, &h, &depth, &refresh);
304 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
309 * Once we've found a UGA supporting PCIIO,
310 * don't bother looking any further.
319 if (!width && !height)
322 /* EFI framebuffer */
323 si->orig_video_isVGA = VIDEO_TYPE_EFI;
326 si->lfb_width = width;
327 si->lfb_height = height;
339 efi_call_early(free_pool, uga_handle);
344 void setup_graphics(struct boot_params *boot_params)
346 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
347 struct screen_info *si;
348 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
351 void **gop_handle = NULL;
352 void **uga_handle = NULL;
354 si = &boot_params->screen_info;
355 memset(si, 0, sizeof(*si));
358 status = efi_call_early(locate_handle,
359 EFI_LOCATE_BY_PROTOCOL,
360 &graphics_proto, NULL, &size, gop_handle);
361 if (status == EFI_BUFFER_TOO_SMALL)
362 status = efi_setup_gop(NULL, si, &graphics_proto, size);
364 if (status != EFI_SUCCESS) {
366 status = efi_call_early(locate_handle,
367 EFI_LOCATE_BY_PROTOCOL,
368 &uga_proto, NULL, &size, uga_handle);
369 if (status == EFI_BUFFER_TOO_SMALL)
370 setup_uga(si, &uga_proto, size);
375 * Because the x86 boot code expects to be passed a boot_params we
376 * need to create one ourselves (usually the bootloader would create
379 * The caller is responsible for filling out ->code32_start in the
380 * returned boot_params.
382 struct boot_params *make_boot_params(struct efi_config *c)
384 struct boot_params *boot_params;
385 struct apm_bios_info *bi;
386 struct setup_header *hdr;
387 efi_loaded_image_t *image;
388 void *options, *handle;
389 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
390 int options_size = 0;
396 unsigned long ramdisk_addr;
397 unsigned long ramdisk_size;
400 sys_table = (efi_system_table_t *)(unsigned long)efi_early->table;
401 handle = (void *)(unsigned long)efi_early->image_handle;
403 /* Check if we were booted by the EFI firmware */
404 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
408 setup_boot_services64(efi_early);
410 setup_boot_services32(efi_early);
412 status = efi_call_early(handle_protocol, handle,
413 &proto, (void *)&image);
414 if (status != EFI_SUCCESS) {
415 efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
419 status = efi_low_alloc(sys_table, 0x4000, 1,
420 (unsigned long *)&boot_params);
421 if (status != EFI_SUCCESS) {
422 efi_printk(sys_table, "Failed to allocate lowmem for boot params\n");
426 memset(boot_params, 0x0, 0x4000);
428 hdr = &boot_params->hdr;
429 bi = &boot_params->apm_bios_info;
431 /* Copy the second sector to boot_params */
432 memcpy(&hdr->jump, image->image_base + 512, 512);
435 * Fill out some of the header fields ourselves because the
436 * EFI firmware loader doesn't load the first sector.
439 hdr->vid_mode = 0xffff;
440 hdr->boot_flag = 0xAA55;
442 hdr->type_of_loader = 0x21;
444 /* Convert unicode cmdline to ascii */
445 cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size);
449 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
450 /* Fill in upper bits of command line address, NOP on 32 bit */
451 boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32;
453 hdr->ramdisk_image = 0;
454 hdr->ramdisk_size = 0;
456 /* Clear APM BIOS info */
457 memset(bi, 0, sizeof(*bi));
459 status = efi_parse_options(cmdline_ptr);
460 if (status != EFI_SUCCESS)
463 status = handle_cmdline_files(sys_table, image,
464 (char *)(unsigned long)hdr->cmd_line_ptr,
465 "initrd=", hdr->initrd_addr_max,
466 &ramdisk_addr, &ramdisk_size);
468 if (status != EFI_SUCCESS &&
469 hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) {
470 efi_printk(sys_table, "Trying to load files to higher address\n");
471 status = handle_cmdline_files(sys_table, image,
472 (char *)(unsigned long)hdr->cmd_line_ptr,
474 &ramdisk_addr, &ramdisk_size);
477 if (status != EFI_SUCCESS)
479 hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
480 hdr->ramdisk_size = ramdisk_size & 0xffffffff;
481 boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
482 boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32;
487 efi_free(sys_table, options_size, hdr->cmd_line_ptr);
489 efi_free(sys_table, 0x4000, (unsigned long)boot_params);
494 static void add_e820ext(struct boot_params *params,
495 struct setup_data *e820ext, u32 nr_entries)
497 struct setup_data *data;
501 e820ext->type = SETUP_E820_EXT;
502 e820ext->len = nr_entries * sizeof(struct boot_e820_entry);
505 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
507 while (data && data->next)
508 data = (struct setup_data *)(unsigned long)data->next;
511 data->next = (unsigned long)e820ext;
513 params->hdr.setup_data = (unsigned long)e820ext;
517 setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size)
519 struct boot_e820_entry *entry = params->e820_table;
520 struct efi_info *efi = ¶ms->efi_info;
521 struct boot_e820_entry *prev = NULL;
527 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
529 for (i = 0; i < nr_desc; i++) {
530 efi_memory_desc_t *d;
531 unsigned int e820_type = 0;
532 unsigned long m = efi->efi_memmap;
535 m |= (u64)efi->efi_memmap_hi << 32;
538 d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
540 case EFI_RESERVED_TYPE:
541 case EFI_RUNTIME_SERVICES_CODE:
542 case EFI_RUNTIME_SERVICES_DATA:
543 case EFI_MEMORY_MAPPED_IO:
544 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
546 e820_type = E820_TYPE_RESERVED;
549 case EFI_UNUSABLE_MEMORY:
550 e820_type = E820_TYPE_UNUSABLE;
553 case EFI_ACPI_RECLAIM_MEMORY:
554 e820_type = E820_TYPE_ACPI;
557 case EFI_LOADER_CODE:
558 case EFI_LOADER_DATA:
559 case EFI_BOOT_SERVICES_CODE:
560 case EFI_BOOT_SERVICES_DATA:
561 case EFI_CONVENTIONAL_MEMORY:
562 e820_type = E820_TYPE_RAM;
565 case EFI_ACPI_MEMORY_NVS:
566 e820_type = E820_TYPE_NVS;
569 case EFI_PERSISTENT_MEMORY:
570 e820_type = E820_TYPE_PMEM;
577 /* Merge adjacent mappings */
578 if (prev && prev->type == e820_type &&
579 (prev->addr + prev->size) == d->phys_addr) {
580 prev->size += d->num_pages << 12;
584 if (nr_entries == ARRAY_SIZE(params->e820_table)) {
585 u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
586 sizeof(struct setup_data);
588 if (!e820ext || e820ext_size < need)
589 return EFI_BUFFER_TOO_SMALL;
591 /* boot_params map full, switch to e820 extended */
592 entry = (struct boot_e820_entry *)e820ext->data;
595 entry->addr = d->phys_addr;
596 entry->size = d->num_pages << PAGE_SHIFT;
597 entry->type = e820_type;
602 if (nr_entries > ARRAY_SIZE(params->e820_table)) {
603 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
605 add_e820ext(params, e820ext, nr_e820ext);
606 nr_entries -= nr_e820ext;
609 params->e820_entries = (u8)nr_entries;
614 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
620 size = sizeof(struct setup_data) +
621 sizeof(struct e820_entry) * nr_desc;
624 efi_call_early(free_pool, *e820ext);
629 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
630 size, (void **)e820ext);
631 if (status == EFI_SUCCESS)
632 *e820ext_size = size;
637 struct exit_boot_struct {
638 struct boot_params *boot_params;
639 struct efi_info *efi;
640 struct setup_data *e820ext;
644 static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg,
645 struct efi_boot_memmap *map,
648 static bool first = true;
649 const char *signature;
652 struct exit_boot_struct *p = priv;
655 nr_desc = *map->buff_size / *map->desc_size;
656 if (nr_desc > ARRAY_SIZE(p->boot_params->e820_table)) {
657 u32 nr_e820ext = nr_desc -
658 ARRAY_SIZE(p->boot_params->e820_table);
660 status = alloc_e820ext(nr_e820ext, &p->e820ext,
662 if (status != EFI_SUCCESS)
668 signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE
669 : EFI32_LOADER_SIGNATURE;
670 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
672 p->efi->efi_systab = (unsigned long)sys_table_arg;
673 p->efi->efi_memdesc_size = *map->desc_size;
674 p->efi->efi_memdesc_version = *map->desc_ver;
675 p->efi->efi_memmap = (unsigned long)*map->map;
676 p->efi->efi_memmap_size = *map->map_size;
679 p->efi->efi_systab_hi = (unsigned long)sys_table_arg >> 32;
680 p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32;
686 static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)
688 unsigned long map_sz, key, desc_size, buff_size;
689 efi_memory_desc_t *mem_map;
690 struct setup_data *e820ext;
694 struct efi_boot_memmap map;
695 struct exit_boot_struct priv;
698 map.map_size = &map_sz;
699 map.desc_size = &desc_size;
700 map.desc_ver = &desc_version;
702 map.buff_size = &buff_size;
703 priv.boot_params = boot_params;
704 priv.efi = &boot_params->efi_info;
706 priv.e820ext_size = 0;
708 /* Might as well exit boot services now */
709 status = efi_exit_boot_services(sys_table, handle, &map, &priv,
711 if (status != EFI_SUCCESS)
714 e820ext = priv.e820ext;
715 e820ext_size = priv.e820ext_size;
718 boot_params->alt_mem_k = 32 * 1024;
720 status = setup_e820(boot_params, e820ext, e820ext_size);
721 if (status != EFI_SUCCESS)
728 * On success we return a pointer to a boot_params structure, and NULL
732 efi_main(struct efi_config *c, struct boot_params *boot_params)
734 struct desc_ptr *gdt = NULL;
735 efi_loaded_image_t *image;
736 struct setup_header *hdr = &boot_params->hdr;
738 struct desc_struct *desc;
740 efi_system_table_t *_table;
744 _table = (efi_system_table_t *)(unsigned long)efi_early->table;
745 handle = (void *)(unsigned long)efi_early->image_handle;
749 /* Check if we were booted by the EFI firmware */
750 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
754 setup_boot_services64(efi_early);
756 setup_boot_services32(efi_early);
759 * If the boot loader gave us a value for secure_boot then we use that,
760 * otherwise we ask the BIOS.
762 if (boot_params->secure_boot == efi_secureboot_mode_unset)
763 boot_params->secure_boot = efi_get_secureboot(sys_table);
765 /* Ask the firmware to clear memory on unclean shutdown */
766 efi_enable_reset_attack_mitigation(sys_table);
767 efi_retrieve_tpm2_eventlog(sys_table);
769 setup_graphics(boot_params);
771 setup_efi_pci(boot_params);
773 setup_quirks(boot_params);
775 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
776 sizeof(*gdt), (void **)&gdt);
777 if (status != EFI_SUCCESS) {
778 efi_printk(sys_table, "Failed to allocate memory for 'gdt' structure\n");
783 status = efi_low_alloc(sys_table, gdt->size, 8,
784 (unsigned long *)&gdt->address);
785 if (status != EFI_SUCCESS) {
786 efi_printk(sys_table, "Failed to allocate memory for 'gdt'\n");
791 * If the kernel isn't already loaded at the preferred load
792 * address, relocate it.
794 if (hdr->pref_address != hdr->code32_start) {
795 unsigned long bzimage_addr = hdr->code32_start;
796 status = efi_relocate_kernel(sys_table, &bzimage_addr,
797 hdr->init_size, hdr->init_size,
799 hdr->kernel_alignment);
800 if (status != EFI_SUCCESS) {
801 efi_printk(sys_table, "efi_relocate_kernel() failed!\n");
805 hdr->pref_address = hdr->code32_start;
806 hdr->code32_start = bzimage_addr;
809 status = exit_boot(boot_params, handle);
810 if (status != EFI_SUCCESS) {
811 efi_printk(sys_table, "exit_boot() failed!\n");
815 memset((char *)gdt->address, 0x0, gdt->size);
816 desc = (struct desc_struct *)gdt->address;
818 /* The first GDT is a dummy. */
821 if (IS_ENABLED(CONFIG_X86_64)) {
823 desc->limit0 = 0xffff;
824 desc->base0 = 0x0000;
825 desc->base1 = 0x0000;
826 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
827 desc->s = DESC_TYPE_CODE_DATA;
833 desc->d = SEG_OP_SIZE_32BIT;
834 desc->g = SEG_GRANULARITY_4KB;
839 /* Second entry is unused on 32-bit */
844 desc->limit0 = 0xffff;
845 desc->base0 = 0x0000;
846 desc->base1 = 0x0000;
847 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
848 desc->s = DESC_TYPE_CODE_DATA;
854 if (IS_ENABLED(CONFIG_X86_64)) {
859 desc->d = SEG_OP_SIZE_32BIT;
861 desc->g = SEG_GRANULARITY_4KB;
866 desc->limit0 = 0xffff;
867 desc->base0 = 0x0000;
868 desc->base1 = 0x0000;
869 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
870 desc->s = DESC_TYPE_CODE_DATA;
876 desc->d = SEG_OP_SIZE_32BIT;
877 desc->g = SEG_GRANULARITY_4KB;
881 if (IS_ENABLED(CONFIG_X86_64)) {
882 /* Task segment value */
883 desc->limit0 = 0x0000;
884 desc->base0 = 0x0000;
885 desc->base1 = 0x0000;
886 desc->type = SEG_TYPE_TSS;
894 desc->g = SEG_GRANULARITY_4KB;
900 asm volatile ("lgdt %0" : : "m" (*gdt));
904 efi_printk(sys_table, "efi_main() failed!\n");