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 static inline efi_status_t __open_volume32(void *__image, void **__fh)
46 efi_file_io_interface_t *io;
47 efi_loaded_image_32_t *image = __image;
48 efi_file_handle_32_t *fh;
49 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
51 void *handle = (void *)(unsigned long)image->device_handle;
54 status = efi_call_early(handle_protocol, handle,
55 &fs_proto, (void **)&io);
56 if (status != EFI_SUCCESS) {
57 efi_printk(sys_table, "Failed to handle fs_proto\n");
61 func = (unsigned long)io->open_volume;
62 status = efi_early->call(func, io, &fh);
63 if (status != EFI_SUCCESS)
64 efi_printk(sys_table, "Failed to open volume\n");
70 static inline efi_status_t __open_volume64(void *__image, void **__fh)
72 efi_file_io_interface_t *io;
73 efi_loaded_image_64_t *image = __image;
74 efi_file_handle_64_t *fh;
75 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
77 void *handle = (void *)(unsigned long)image->device_handle;
80 status = efi_call_early(handle_protocol, handle,
81 &fs_proto, (void **)&io);
82 if (status != EFI_SUCCESS) {
83 efi_printk(sys_table, "Failed to handle fs_proto\n");
87 func = (unsigned long)io->open_volume;
88 status = efi_early->call(func, io, &fh);
89 if (status != EFI_SUCCESS)
90 efi_printk(sys_table, "Failed to open volume\n");
97 efi_open_volume(efi_system_table_t *sys_table, void *__image, void **__fh)
100 return __open_volume64(__image, __fh);
102 return __open_volume32(__image, __fh);
105 void efi_char16_printk(efi_system_table_t *table, efi_char16_t *str)
107 efi_call_proto(efi_simple_text_output_protocol, output_string,
108 efi_early->text_output, str);
112 __setup_efi_pci(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
114 struct pci_setup_rom *rom = NULL;
117 uint64_t attributes, romsize;
120 status = efi_call_proto(efi_pci_io_protocol, attributes, pci,
121 EfiPciIoAttributeOperationGet, 0, 0,
123 if (status != EFI_SUCCESS)
127 * Some firmware images contain EFI function pointers at the place where the
128 * romimage and romsize fields are supposed to be. Typically the EFI
129 * code is mapped at high addresses, translating to an unrealistically
130 * large romsize. The UEFI spec limits the size of option ROMs to 16
131 * MiB so we reject any ROMs over 16 MiB in size to catch this.
133 romimage = (void *)(unsigned long)efi_table_attr(efi_pci_io_protocol,
135 romsize = efi_table_attr(efi_pci_io_protocol, romsize, pci);
136 if (!romimage || !romsize || romsize > SZ_16M)
137 return EFI_INVALID_PARAMETER;
139 size = romsize + sizeof(*rom);
141 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
142 if (status != EFI_SUCCESS) {
143 efi_printk(sys_table, "Failed to alloc mem for rom\n");
147 memset(rom, 0, sizeof(*rom));
149 rom->data.type = SETUP_PCI;
150 rom->data.len = size - sizeof(struct setup_data);
152 rom->pcilen = pci->romsize;
155 status = efi_call_proto(efi_pci_io_protocol, pci.read, pci,
156 EfiPciIoWidthUint16, PCI_VENDOR_ID, 1,
159 if (status != EFI_SUCCESS) {
160 efi_printk(sys_table, "Failed to read rom->vendor\n");
164 status = efi_call_proto(efi_pci_io_protocol, pci.read, pci,
165 EfiPciIoWidthUint16, PCI_DEVICE_ID, 1,
168 if (status != EFI_SUCCESS) {
169 efi_printk(sys_table, "Failed to read rom->devid\n");
173 status = efi_call_proto(efi_pci_io_protocol, get_location, pci,
174 &rom->segment, &rom->bus, &rom->device,
177 if (status != EFI_SUCCESS)
180 memcpy(rom->romdata, romimage, romsize);
184 efi_call_early(free_pool, rom);
189 setup_efi_pci32(struct boot_params *params, void **pci_handle,
192 efi_pci_io_protocol_t *pci = NULL;
193 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
194 u32 *handles = (u32 *)(unsigned long)pci_handle;
196 unsigned long nr_pci;
197 struct setup_data *data;
200 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
202 while (data && data->next)
203 data = (struct setup_data *)(unsigned long)data->next;
205 nr_pci = size / sizeof(u32);
206 for (i = 0; i < nr_pci; i++) {
207 struct pci_setup_rom *rom = NULL;
210 status = efi_call_early(handle_protocol, h,
211 &pci_proto, (void **)&pci);
213 if (status != EFI_SUCCESS)
219 status = __setup_efi_pci(pci, &rom);
220 if (status != EFI_SUCCESS)
224 data->next = (unsigned long)rom;
226 params->hdr.setup_data = (unsigned long)rom;
228 data = (struct setup_data *)rom;
234 setup_efi_pci64(struct boot_params *params, void **pci_handle,
237 efi_pci_io_protocol_t *pci = NULL;
238 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
239 u64 *handles = (u64 *)(unsigned long)pci_handle;
241 unsigned long nr_pci;
242 struct setup_data *data;
245 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
247 while (data && data->next)
248 data = (struct setup_data *)(unsigned long)data->next;
250 nr_pci = size / sizeof(u64);
251 for (i = 0; i < nr_pci; i++) {
252 struct pci_setup_rom *rom = NULL;
255 status = efi_call_early(handle_protocol, h,
256 &pci_proto, (void **)&pci);
258 if (status != EFI_SUCCESS)
264 status = __setup_efi_pci(pci, &rom);
265 if (status != EFI_SUCCESS)
269 data->next = (unsigned long)rom;
271 params->hdr.setup_data = (unsigned long)rom;
273 data = (struct setup_data *)rom;
279 * There's no way to return an informative status from this function,
280 * because any analysis (and printing of error messages) needs to be
281 * done directly at the EFI function call-site.
283 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
284 * just didn't find any PCI devices, but there's no way to tell outside
285 * the context of the call.
287 static void setup_efi_pci(struct boot_params *params)
290 void **pci_handle = NULL;
291 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
292 unsigned long size = 0;
294 status = efi_call_early(locate_handle,
295 EFI_LOCATE_BY_PROTOCOL,
296 &pci_proto, NULL, &size, pci_handle);
298 if (status == EFI_BUFFER_TOO_SMALL) {
299 status = efi_call_early(allocate_pool,
301 size, (void **)&pci_handle);
303 if (status != EFI_SUCCESS) {
304 efi_printk(sys_table, "Failed to alloc mem for pci_handle\n");
308 status = efi_call_early(locate_handle,
309 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
310 NULL, &size, pci_handle);
313 if (status != EFI_SUCCESS)
317 setup_efi_pci64(params, pci_handle, size);
319 setup_efi_pci32(params, pci_handle, size);
322 efi_call_early(free_pool, pci_handle);
325 static void retrieve_apple_device_properties(struct boot_params *boot_params)
327 efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
328 struct setup_data *data, *new;
333 status = efi_call_early(locate_protocol, &guid, NULL, &p);
334 if (status != EFI_SUCCESS)
337 if (efi_table_attr(apple_properties_protocol, version, p) != 0x10000) {
338 efi_printk(sys_table, "Unsupported properties proto version\n");
342 efi_call_proto(apple_properties_protocol, get_all, p, NULL, &size);
347 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
348 size + sizeof(struct setup_data), &new);
349 if (status != EFI_SUCCESS) {
350 efi_printk(sys_table,
351 "Failed to alloc mem for properties\n");
355 status = efi_call_proto(apple_properties_protocol, get_all, p,
358 if (status == EFI_BUFFER_TOO_SMALL)
359 efi_call_early(free_pool, new);
360 } while (status == EFI_BUFFER_TOO_SMALL);
362 new->type = SETUP_APPLE_PROPERTIES;
366 data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
368 boot_params->hdr.setup_data = (unsigned long)new;
371 data = (struct setup_data *)(unsigned long)data->next;
372 data->next = (unsigned long)new;
376 static const efi_char16_t apple[] = L"Apple";
378 static void setup_quirks(struct boot_params *boot_params)
380 efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
381 efi_table_attr(efi_system_table, fw_vendor, sys_table);
383 if (!memcmp(fw_vendor, apple, sizeof(apple))) {
384 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
385 retrieve_apple_device_properties(boot_params);
390 setup_uga32(void **uga_handle, unsigned long size, u32 *width, u32 *height)
392 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
393 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
394 unsigned long nr_ugas;
395 u32 *handles = (u32 *)uga_handle;
396 efi_status_t status = EFI_INVALID_PARAMETER;
400 nr_ugas = size / sizeof(u32);
401 for (i = 0; i < nr_ugas; i++) {
402 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
403 u32 w, h, depth, refresh;
405 u32 handle = handles[i];
407 status = efi_call_early(handle_protocol, handle,
408 &uga_proto, (void **)&uga);
409 if (status != EFI_SUCCESS)
412 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
414 status = efi_early->call((unsigned long)uga->get_mode, uga,
415 &w, &h, &depth, &refresh);
416 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
421 * Once we've found a UGA supporting PCIIO,
422 * don't bother looking any further.
435 setup_uga64(void **uga_handle, unsigned long size, u32 *width, u32 *height)
437 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
438 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
439 unsigned long nr_ugas;
440 u64 *handles = (u64 *)uga_handle;
441 efi_status_t status = EFI_INVALID_PARAMETER;
445 nr_ugas = size / sizeof(u64);
446 for (i = 0; i < nr_ugas; i++) {
447 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
448 u32 w, h, depth, refresh;
450 u64 handle = handles[i];
452 status = efi_call_early(handle_protocol, handle,
453 &uga_proto, (void **)&uga);
454 if (status != EFI_SUCCESS)
457 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
459 status = efi_early->call((unsigned long)uga->get_mode, uga,
460 &w, &h, &depth, &refresh);
461 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
466 * Once we've found a UGA supporting PCIIO,
467 * don't bother looking any further.
480 * See if we have Universal Graphics Adapter (UGA) protocol
482 static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
487 void **uga_handle = NULL;
489 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
490 size, (void **)&uga_handle);
491 if (status != EFI_SUCCESS)
494 status = efi_call_early(locate_handle,
495 EFI_LOCATE_BY_PROTOCOL,
496 uga_proto, NULL, &size, uga_handle);
497 if (status != EFI_SUCCESS)
504 status = setup_uga64(uga_handle, size, &width, &height);
506 status = setup_uga32(uga_handle, size, &width, &height);
508 if (!width && !height)
511 /* EFI framebuffer */
512 si->orig_video_isVGA = VIDEO_TYPE_EFI;
515 si->lfb_width = width;
516 si->lfb_height = height;
528 efi_call_early(free_pool, uga_handle);
532 void setup_graphics(struct boot_params *boot_params)
534 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
535 struct screen_info *si;
536 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
539 void **gop_handle = NULL;
540 void **uga_handle = NULL;
542 si = &boot_params->screen_info;
543 memset(si, 0, sizeof(*si));
546 status = efi_call_early(locate_handle,
547 EFI_LOCATE_BY_PROTOCOL,
548 &graphics_proto, NULL, &size, gop_handle);
549 if (status == EFI_BUFFER_TOO_SMALL)
550 status = efi_setup_gop(NULL, si, &graphics_proto, size);
552 if (status != EFI_SUCCESS) {
554 status = efi_call_early(locate_handle,
555 EFI_LOCATE_BY_PROTOCOL,
556 &uga_proto, NULL, &size, uga_handle);
557 if (status == EFI_BUFFER_TOO_SMALL)
558 setup_uga(si, &uga_proto, size);
563 * Because the x86 boot code expects to be passed a boot_params we
564 * need to create one ourselves (usually the bootloader would create
567 * The caller is responsible for filling out ->code32_start in the
568 * returned boot_params.
570 struct boot_params *make_boot_params(struct efi_config *c)
572 struct boot_params *boot_params;
573 struct apm_bios_info *bi;
574 struct setup_header *hdr;
575 efi_loaded_image_t *image;
576 void *options, *handle;
577 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
578 int options_size = 0;
584 unsigned long ramdisk_addr;
585 unsigned long ramdisk_size;
588 sys_table = (efi_system_table_t *)(unsigned long)efi_early->table;
589 handle = (void *)(unsigned long)efi_early->image_handle;
591 /* Check if we were booted by the EFI firmware */
592 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
596 setup_boot_services64(efi_early);
598 setup_boot_services32(efi_early);
600 status = efi_call_early(handle_protocol, handle,
601 &proto, (void *)&image);
602 if (status != EFI_SUCCESS) {
603 efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
607 status = efi_low_alloc(sys_table, 0x4000, 1,
608 (unsigned long *)&boot_params);
609 if (status != EFI_SUCCESS) {
610 efi_printk(sys_table, "Failed to alloc lowmem for boot params\n");
614 memset(boot_params, 0x0, 0x4000);
616 hdr = &boot_params->hdr;
617 bi = &boot_params->apm_bios_info;
619 /* Copy the second sector to boot_params */
620 memcpy(&hdr->jump, image->image_base + 512, 512);
623 * Fill out some of the header fields ourselves because the
624 * EFI firmware loader doesn't load the first sector.
627 hdr->vid_mode = 0xffff;
628 hdr->boot_flag = 0xAA55;
630 hdr->type_of_loader = 0x21;
632 /* Convert unicode cmdline to ascii */
633 cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size);
636 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
637 /* Fill in upper bits of command line address, NOP on 32 bit */
638 boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32;
640 hdr->ramdisk_image = 0;
641 hdr->ramdisk_size = 0;
643 /* Clear APM BIOS info */
644 memset(bi, 0, sizeof(*bi));
646 status = efi_parse_options(cmdline_ptr);
647 if (status != EFI_SUCCESS)
650 status = handle_cmdline_files(sys_table, image,
651 (char *)(unsigned long)hdr->cmd_line_ptr,
652 "initrd=", hdr->initrd_addr_max,
653 &ramdisk_addr, &ramdisk_size);
655 if (status != EFI_SUCCESS &&
656 hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) {
657 efi_printk(sys_table, "Trying to load files to higher address\n");
658 status = handle_cmdline_files(sys_table, image,
659 (char *)(unsigned long)hdr->cmd_line_ptr,
661 &ramdisk_addr, &ramdisk_size);
664 if (status != EFI_SUCCESS)
666 hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
667 hdr->ramdisk_size = ramdisk_size & 0xffffffff;
668 boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
669 boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32;
673 efi_free(sys_table, options_size, hdr->cmd_line_ptr);
675 efi_free(sys_table, 0x4000, (unsigned long)boot_params);
679 static void add_e820ext(struct boot_params *params,
680 struct setup_data *e820ext, u32 nr_entries)
682 struct setup_data *data;
686 e820ext->type = SETUP_E820_EXT;
687 e820ext->len = nr_entries * sizeof(struct boot_e820_entry);
690 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
692 while (data && data->next)
693 data = (struct setup_data *)(unsigned long)data->next;
696 data->next = (unsigned long)e820ext;
698 params->hdr.setup_data = (unsigned long)e820ext;
701 static efi_status_t setup_e820(struct boot_params *params,
702 struct setup_data *e820ext, u32 e820ext_size)
704 struct boot_e820_entry *entry = params->e820_table;
705 struct efi_info *efi = ¶ms->efi_info;
706 struct boot_e820_entry *prev = NULL;
712 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
714 for (i = 0; i < nr_desc; i++) {
715 efi_memory_desc_t *d;
716 unsigned int e820_type = 0;
717 unsigned long m = efi->efi_memmap;
720 m |= (u64)efi->efi_memmap_hi << 32;
723 d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
725 case EFI_RESERVED_TYPE:
726 case EFI_RUNTIME_SERVICES_CODE:
727 case EFI_RUNTIME_SERVICES_DATA:
728 case EFI_MEMORY_MAPPED_IO:
729 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
731 e820_type = E820_TYPE_RESERVED;
734 case EFI_UNUSABLE_MEMORY:
735 e820_type = E820_TYPE_UNUSABLE;
738 case EFI_ACPI_RECLAIM_MEMORY:
739 e820_type = E820_TYPE_ACPI;
742 case EFI_LOADER_CODE:
743 case EFI_LOADER_DATA:
744 case EFI_BOOT_SERVICES_CODE:
745 case EFI_BOOT_SERVICES_DATA:
746 case EFI_CONVENTIONAL_MEMORY:
747 e820_type = E820_TYPE_RAM;
750 case EFI_ACPI_MEMORY_NVS:
751 e820_type = E820_TYPE_NVS;
754 case EFI_PERSISTENT_MEMORY:
755 e820_type = E820_TYPE_PMEM;
762 /* Merge adjacent mappings */
763 if (prev && prev->type == e820_type &&
764 (prev->addr + prev->size) == d->phys_addr) {
765 prev->size += d->num_pages << 12;
769 if (nr_entries == ARRAY_SIZE(params->e820_table)) {
770 u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
771 sizeof(struct setup_data);
773 if (!e820ext || e820ext_size < need)
774 return EFI_BUFFER_TOO_SMALL;
776 /* boot_params map full, switch to e820 extended */
777 entry = (struct boot_e820_entry *)e820ext->data;
780 entry->addr = d->phys_addr;
781 entry->size = d->num_pages << PAGE_SHIFT;
782 entry->type = e820_type;
787 if (nr_entries > ARRAY_SIZE(params->e820_table)) {
788 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
790 add_e820ext(params, e820ext, nr_e820ext);
791 nr_entries -= nr_e820ext;
794 params->e820_entries = (u8)nr_entries;
799 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
805 size = sizeof(struct setup_data) +
806 sizeof(struct e820_entry) * nr_desc;
809 efi_call_early(free_pool, *e820ext);
814 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
815 size, (void **)e820ext);
816 if (status == EFI_SUCCESS)
817 *e820ext_size = size;
822 struct exit_boot_struct {
823 struct boot_params *boot_params;
824 struct efi_info *efi;
825 struct setup_data *e820ext;
830 static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg,
831 struct efi_boot_memmap *map,
834 static bool first = true;
835 const char *signature;
838 struct exit_boot_struct *p = priv;
841 nr_desc = *map->buff_size / *map->desc_size;
842 if (nr_desc > ARRAY_SIZE(p->boot_params->e820_table)) {
843 u32 nr_e820ext = nr_desc -
844 ARRAY_SIZE(p->boot_params->e820_table);
846 status = alloc_e820ext(nr_e820ext, &p->e820ext,
848 if (status != EFI_SUCCESS)
854 signature = p->is64 ? EFI64_LOADER_SIGNATURE : EFI32_LOADER_SIGNATURE;
855 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
857 p->efi->efi_systab = (unsigned long)sys_table_arg;
858 p->efi->efi_memdesc_size = *map->desc_size;
859 p->efi->efi_memdesc_version = *map->desc_ver;
860 p->efi->efi_memmap = (unsigned long)*map->map;
861 p->efi->efi_memmap_size = *map->map_size;
864 p->efi->efi_systab_hi = (unsigned long)sys_table_arg >> 32;
865 p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32;
871 static efi_status_t exit_boot(struct boot_params *boot_params,
872 void *handle, bool is64)
874 unsigned long map_sz, key, desc_size, buff_size;
875 efi_memory_desc_t *mem_map;
876 struct setup_data *e820ext;
880 struct efi_boot_memmap map;
881 struct exit_boot_struct priv;
884 map.map_size = &map_sz;
885 map.desc_size = &desc_size;
886 map.desc_ver = &desc_version;
888 map.buff_size = &buff_size;
889 priv.boot_params = boot_params;
890 priv.efi = &boot_params->efi_info;
892 priv.e820ext_size = 0;
895 /* Might as well exit boot services now */
896 status = efi_exit_boot_services(sys_table, handle, &map, &priv,
898 if (status != EFI_SUCCESS)
901 e820ext = priv.e820ext;
902 e820ext_size = priv.e820ext_size;
904 boot_params->alt_mem_k = 32 * 1024;
906 status = setup_e820(boot_params, e820ext, e820ext_size);
907 if (status != EFI_SUCCESS)
914 * On success we return a pointer to a boot_params structure, and NULL
917 struct boot_params *efi_main(struct efi_config *c,
918 struct boot_params *boot_params)
920 struct desc_ptr *gdt = NULL;
921 efi_loaded_image_t *image;
922 struct setup_header *hdr = &boot_params->hdr;
924 struct desc_struct *desc;
926 efi_system_table_t *_table;
931 _table = (efi_system_table_t *)(unsigned long)efi_early->table;
932 handle = (void *)(unsigned long)efi_early->image_handle;
933 is64 = efi_early->is64;
937 /* Check if we were booted by the EFI firmware */
938 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
942 setup_boot_services64(efi_early);
944 setup_boot_services32(efi_early);
947 * If the boot loader gave us a value for secure_boot then we use that,
948 * otherwise we ask the BIOS.
950 if (boot_params->secure_boot == efi_secureboot_mode_unset)
951 boot_params->secure_boot = efi_get_secureboot(sys_table);
953 /* Ask the firmware to clear memory on unclean shutdown */
954 efi_enable_reset_attack_mitigation(sys_table);
955 efi_retrieve_tpm2_eventlog(sys_table);
957 setup_graphics(boot_params);
959 setup_efi_pci(boot_params);
961 setup_quirks(boot_params);
963 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
964 sizeof(*gdt), (void **)&gdt);
965 if (status != EFI_SUCCESS) {
966 efi_printk(sys_table, "Failed to alloc mem for gdt structure\n");
971 status = efi_low_alloc(sys_table, gdt->size, 8,
972 (unsigned long *)&gdt->address);
973 if (status != EFI_SUCCESS) {
974 efi_printk(sys_table, "Failed to alloc mem for gdt\n");
979 * If the kernel isn't already loaded at the preferred load
980 * address, relocate it.
982 if (hdr->pref_address != hdr->code32_start) {
983 unsigned long bzimage_addr = hdr->code32_start;
984 status = efi_relocate_kernel(sys_table, &bzimage_addr,
985 hdr->init_size, hdr->init_size,
987 hdr->kernel_alignment);
988 if (status != EFI_SUCCESS) {
989 efi_printk(sys_table, "efi_relocate_kernel() failed!\n");
993 hdr->pref_address = hdr->code32_start;
994 hdr->code32_start = bzimage_addr;
997 status = exit_boot(boot_params, handle, is64);
998 if (status != EFI_SUCCESS) {
999 efi_printk(sys_table, "exit_boot() failed!\n");
1003 memset((char *)gdt->address, 0x0, gdt->size);
1004 desc = (struct desc_struct *)gdt->address;
1006 /* The first GDT is a dummy. */
1009 if (IS_ENABLED(CONFIG_X86_64)) {
1011 desc->limit0 = 0xffff;
1012 desc->base0 = 0x0000;
1013 desc->base1 = 0x0000;
1014 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1015 desc->s = DESC_TYPE_CODE_DATA;
1021 desc->d = SEG_OP_SIZE_32BIT;
1022 desc->g = SEG_GRANULARITY_4KB;
1026 /* Second entry is unused on 32-bit */
1031 desc->limit0 = 0xffff;
1032 desc->base0 = 0x0000;
1033 desc->base1 = 0x0000;
1034 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1035 desc->s = DESC_TYPE_CODE_DATA;
1040 if (IS_ENABLED(CONFIG_X86_64)) {
1045 desc->d = SEG_OP_SIZE_32BIT;
1047 desc->g = SEG_GRANULARITY_4KB;
1052 desc->limit0 = 0xffff;
1053 desc->base0 = 0x0000;
1054 desc->base1 = 0x0000;
1055 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
1056 desc->s = DESC_TYPE_CODE_DATA;
1062 desc->d = SEG_OP_SIZE_32BIT;
1063 desc->g = SEG_GRANULARITY_4KB;
1067 if (IS_ENABLED(CONFIG_X86_64)) {
1068 /* Task segment value */
1069 desc->limit0 = 0x0000;
1070 desc->base0 = 0x0000;
1071 desc->base1 = 0x0000;
1072 desc->type = SEG_TYPE_TSS;
1080 desc->g = SEG_GRANULARITY_4KB;
1085 asm volatile("cli");
1086 asm volatile ("lgdt %0" : : "m" (*gdt));
1090 efi_printk(sys_table, "efi_main() failed!\n");