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;
121 * Some firmware images contain EFI function pointers at the place where
122 * the romimage and romsize fields are supposed to be. Typically the EFI
123 * code is mapped at high addresses, translating to an unrealistically
124 * large romsize. The UEFI spec limits the size of option ROMs to 16
125 * MiB so we reject any ROMs over 16 MiB in size to catch this.
127 romimage = (void *)(unsigned long)efi_table_attr(efi_pci_io_protocol,
129 romsize = efi_table_attr(efi_pci_io_protocol, romsize, pci);
130 if (!romimage || !romsize || romsize > SZ_16M)
131 return EFI_INVALID_PARAMETER;
133 size = romsize + sizeof(*rom);
135 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
136 if (status != EFI_SUCCESS) {
137 efi_printk(sys_table, "Failed to alloc mem for rom\n");
141 memset(rom, 0, sizeof(*rom));
143 rom->data.type = SETUP_PCI;
144 rom->data.len = size - sizeof(struct setup_data);
146 rom->pcilen = pci->romsize;
149 status = efi_call_proto(efi_pci_io_protocol, pci.read, pci,
150 EfiPciIoWidthUint16, PCI_VENDOR_ID, 1,
153 if (status != EFI_SUCCESS) {
154 efi_printk(sys_table, "Failed to read rom->vendor\n");
158 status = efi_call_proto(efi_pci_io_protocol, pci.read, pci,
159 EfiPciIoWidthUint16, PCI_DEVICE_ID, 1,
162 if (status != EFI_SUCCESS) {
163 efi_printk(sys_table, "Failed to read rom->devid\n");
167 status = efi_call_proto(efi_pci_io_protocol, get_location, pci,
168 &rom->segment, &rom->bus, &rom->device,
171 if (status != EFI_SUCCESS)
174 memcpy(rom->romdata, romimage, romsize);
178 efi_call_early(free_pool, rom);
183 setup_efi_pci32(struct boot_params *params, void **pci_handle,
186 efi_pci_io_protocol_t *pci = NULL;
187 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
188 u32 *handles = (u32 *)(unsigned long)pci_handle;
190 unsigned long nr_pci;
191 struct setup_data *data;
194 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
196 while (data && data->next)
197 data = (struct setup_data *)(unsigned long)data->next;
199 nr_pci = size / sizeof(u32);
200 for (i = 0; i < nr_pci; i++) {
201 struct pci_setup_rom *rom = NULL;
204 status = efi_call_early(handle_protocol, h,
205 &pci_proto, (void **)&pci);
207 if (status != EFI_SUCCESS)
213 status = __setup_efi_pci(pci, &rom);
214 if (status != EFI_SUCCESS)
218 data->next = (unsigned long)rom;
220 params->hdr.setup_data = (unsigned long)rom;
222 data = (struct setup_data *)rom;
228 setup_efi_pci64(struct boot_params *params, void **pci_handle,
231 efi_pci_io_protocol_t *pci = NULL;
232 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
233 u64 *handles = (u64 *)(unsigned long)pci_handle;
235 unsigned long nr_pci;
236 struct setup_data *data;
239 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
241 while (data && data->next)
242 data = (struct setup_data *)(unsigned long)data->next;
244 nr_pci = size / sizeof(u64);
245 for (i = 0; i < nr_pci; i++) {
246 struct pci_setup_rom *rom = NULL;
249 status = efi_call_early(handle_protocol, h,
250 &pci_proto, (void **)&pci);
252 if (status != EFI_SUCCESS)
258 status = __setup_efi_pci(pci, &rom);
259 if (status != EFI_SUCCESS)
263 data->next = (unsigned long)rom;
265 params->hdr.setup_data = (unsigned long)rom;
267 data = (struct setup_data *)rom;
273 * There's no way to return an informative status from this function,
274 * because any analysis (and printing of error messages) needs to be
275 * done directly at the EFI function call-site.
277 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
278 * just didn't find any PCI devices, but there's no way to tell outside
279 * the context of the call.
281 static void setup_efi_pci(struct boot_params *params)
284 void **pci_handle = NULL;
285 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
286 unsigned long size = 0;
288 status = efi_call_early(locate_handle,
289 EFI_LOCATE_BY_PROTOCOL,
290 &pci_proto, NULL, &size, pci_handle);
292 if (status == EFI_BUFFER_TOO_SMALL) {
293 status = efi_call_early(allocate_pool,
295 size, (void **)&pci_handle);
297 if (status != EFI_SUCCESS) {
298 efi_printk(sys_table, "Failed to alloc mem for pci_handle\n");
302 status = efi_call_early(locate_handle,
303 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
304 NULL, &size, pci_handle);
307 if (status != EFI_SUCCESS)
311 setup_efi_pci64(params, pci_handle, size);
313 setup_efi_pci32(params, pci_handle, size);
316 efi_call_early(free_pool, pci_handle);
319 static void retrieve_apple_device_properties(struct boot_params *boot_params)
321 efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
322 struct setup_data *data, *new;
327 status = efi_call_early(locate_protocol, &guid, NULL, &p);
328 if (status != EFI_SUCCESS)
331 if (efi_table_attr(apple_properties_protocol, version, p) != 0x10000) {
332 efi_printk(sys_table, "Unsupported properties proto version\n");
336 efi_call_proto(apple_properties_protocol, get_all, p, NULL, &size);
341 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
342 size + sizeof(struct setup_data), &new);
343 if (status != EFI_SUCCESS) {
344 efi_printk(sys_table,
345 "Failed to alloc mem for properties\n");
349 status = efi_call_proto(apple_properties_protocol, get_all, p,
352 if (status == EFI_BUFFER_TOO_SMALL)
353 efi_call_early(free_pool, new);
354 } while (status == EFI_BUFFER_TOO_SMALL);
356 new->type = SETUP_APPLE_PROPERTIES;
360 data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
362 boot_params->hdr.setup_data = (unsigned long)new;
365 data = (struct setup_data *)(unsigned long)data->next;
366 data->next = (unsigned long)new;
370 static const efi_char16_t apple[] = L"Apple";
372 static void setup_quirks(struct boot_params *boot_params)
374 efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
375 efi_table_attr(efi_system_table, fw_vendor, sys_table);
377 if (!memcmp(fw_vendor, apple, sizeof(apple))) {
378 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
379 retrieve_apple_device_properties(boot_params);
384 setup_uga32(void **uga_handle, unsigned long size, u32 *width, u32 *height)
386 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
387 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
388 unsigned long nr_ugas;
389 u32 *handles = (u32 *)uga_handle;
390 efi_status_t status = EFI_INVALID_PARAMETER;
394 nr_ugas = size / sizeof(u32);
395 for (i = 0; i < nr_ugas; i++) {
396 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
397 u32 w, h, depth, refresh;
399 u32 handle = handles[i];
401 status = efi_call_early(handle_protocol, handle,
402 &uga_proto, (void **)&uga);
403 if (status != EFI_SUCCESS)
406 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
408 status = efi_early->call((unsigned long)uga->get_mode, uga,
409 &w, &h, &depth, &refresh);
410 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
415 * Once we've found a UGA supporting PCIIO,
416 * don't bother looking any further.
429 setup_uga64(void **uga_handle, unsigned long size, u32 *width, u32 *height)
431 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
432 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
433 unsigned long nr_ugas;
434 u64 *handles = (u64 *)uga_handle;
435 efi_status_t status = EFI_INVALID_PARAMETER;
439 nr_ugas = size / sizeof(u64);
440 for (i = 0; i < nr_ugas; i++) {
441 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
442 u32 w, h, depth, refresh;
444 u64 handle = handles[i];
446 status = efi_call_early(handle_protocol, handle,
447 &uga_proto, (void **)&uga);
448 if (status != EFI_SUCCESS)
451 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
453 status = efi_early->call((unsigned long)uga->get_mode, uga,
454 &w, &h, &depth, &refresh);
455 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
460 * Once we've found a UGA supporting PCIIO,
461 * don't bother looking any further.
474 * See if we have Universal Graphics Adapter (UGA) protocol
476 static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
481 void **uga_handle = NULL;
483 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
484 size, (void **)&uga_handle);
485 if (status != EFI_SUCCESS)
488 status = efi_call_early(locate_handle,
489 EFI_LOCATE_BY_PROTOCOL,
490 uga_proto, NULL, &size, uga_handle);
491 if (status != EFI_SUCCESS)
498 status = setup_uga64(uga_handle, size, &width, &height);
500 status = setup_uga32(uga_handle, size, &width, &height);
502 if (!width && !height)
505 /* EFI framebuffer */
506 si->orig_video_isVGA = VIDEO_TYPE_EFI;
509 si->lfb_width = width;
510 si->lfb_height = height;
522 efi_call_early(free_pool, uga_handle);
526 void setup_graphics(struct boot_params *boot_params)
528 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
529 struct screen_info *si;
530 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
533 void **gop_handle = NULL;
534 void **uga_handle = NULL;
536 si = &boot_params->screen_info;
537 memset(si, 0, sizeof(*si));
540 status = efi_call_early(locate_handle,
541 EFI_LOCATE_BY_PROTOCOL,
542 &graphics_proto, NULL, &size, gop_handle);
543 if (status == EFI_BUFFER_TOO_SMALL)
544 status = efi_setup_gop(NULL, si, &graphics_proto, size);
546 if (status != EFI_SUCCESS) {
548 status = efi_call_early(locate_handle,
549 EFI_LOCATE_BY_PROTOCOL,
550 &uga_proto, NULL, &size, uga_handle);
551 if (status == EFI_BUFFER_TOO_SMALL)
552 setup_uga(si, &uga_proto, size);
557 * Because the x86 boot code expects to be passed a boot_params we
558 * need to create one ourselves (usually the bootloader would create
561 * The caller is responsible for filling out ->code32_start in the
562 * returned boot_params.
564 struct boot_params *make_boot_params(struct efi_config *c)
566 struct boot_params *boot_params;
567 struct apm_bios_info *bi;
568 struct setup_header *hdr;
569 efi_loaded_image_t *image;
570 void *options, *handle;
571 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
572 int options_size = 0;
578 unsigned long ramdisk_addr;
579 unsigned long ramdisk_size;
582 sys_table = (efi_system_table_t *)(unsigned long)efi_early->table;
583 handle = (void *)(unsigned long)efi_early->image_handle;
585 /* Check if we were booted by the EFI firmware */
586 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
590 setup_boot_services64(efi_early);
592 setup_boot_services32(efi_early);
594 status = efi_call_early(handle_protocol, handle,
595 &proto, (void *)&image);
596 if (status != EFI_SUCCESS) {
597 efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
601 status = efi_low_alloc(sys_table, 0x4000, 1,
602 (unsigned long *)&boot_params);
603 if (status != EFI_SUCCESS) {
604 efi_printk(sys_table, "Failed to alloc lowmem for boot params\n");
608 memset(boot_params, 0x0, 0x4000);
610 hdr = &boot_params->hdr;
611 bi = &boot_params->apm_bios_info;
613 /* Copy the second sector to boot_params */
614 memcpy(&hdr->jump, image->image_base + 512, 512);
617 * Fill out some of the header fields ourselves because the
618 * EFI firmware loader doesn't load the first sector.
621 hdr->vid_mode = 0xffff;
622 hdr->boot_flag = 0xAA55;
624 hdr->type_of_loader = 0x21;
626 /* Convert unicode cmdline to ascii */
627 cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size);
630 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
631 /* Fill in upper bits of command line address, NOP on 32 bit */
632 boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32;
634 hdr->ramdisk_image = 0;
635 hdr->ramdisk_size = 0;
637 /* Clear APM BIOS info */
638 memset(bi, 0, sizeof(*bi));
640 status = efi_parse_options(cmdline_ptr);
641 if (status != EFI_SUCCESS)
644 status = handle_cmdline_files(sys_table, image,
645 (char *)(unsigned long)hdr->cmd_line_ptr,
646 "initrd=", hdr->initrd_addr_max,
647 &ramdisk_addr, &ramdisk_size);
649 if (status != EFI_SUCCESS &&
650 hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) {
651 efi_printk(sys_table, "Trying to load files to higher address\n");
652 status = handle_cmdline_files(sys_table, image,
653 (char *)(unsigned long)hdr->cmd_line_ptr,
655 &ramdisk_addr, &ramdisk_size);
658 if (status != EFI_SUCCESS)
660 hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
661 hdr->ramdisk_size = ramdisk_size & 0xffffffff;
662 boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
663 boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32;
667 efi_free(sys_table, options_size, hdr->cmd_line_ptr);
669 efi_free(sys_table, 0x4000, (unsigned long)boot_params);
673 static void add_e820ext(struct boot_params *params,
674 struct setup_data *e820ext, u32 nr_entries)
676 struct setup_data *data;
680 e820ext->type = SETUP_E820_EXT;
681 e820ext->len = nr_entries * sizeof(struct boot_e820_entry);
684 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
686 while (data && data->next)
687 data = (struct setup_data *)(unsigned long)data->next;
690 data->next = (unsigned long)e820ext;
692 params->hdr.setup_data = (unsigned long)e820ext;
695 static efi_status_t setup_e820(struct boot_params *params,
696 struct setup_data *e820ext, u32 e820ext_size)
698 struct boot_e820_entry *entry = params->e820_table;
699 struct efi_info *efi = ¶ms->efi_info;
700 struct boot_e820_entry *prev = NULL;
706 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
708 for (i = 0; i < nr_desc; i++) {
709 efi_memory_desc_t *d;
710 unsigned int e820_type = 0;
711 unsigned long m = efi->efi_memmap;
714 m |= (u64)efi->efi_memmap_hi << 32;
717 d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
719 case EFI_RESERVED_TYPE:
720 case EFI_RUNTIME_SERVICES_CODE:
721 case EFI_RUNTIME_SERVICES_DATA:
722 case EFI_MEMORY_MAPPED_IO:
723 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
725 e820_type = E820_TYPE_RESERVED;
728 case EFI_UNUSABLE_MEMORY:
729 e820_type = E820_TYPE_UNUSABLE;
732 case EFI_ACPI_RECLAIM_MEMORY:
733 e820_type = E820_TYPE_ACPI;
736 case EFI_LOADER_CODE:
737 case EFI_LOADER_DATA:
738 case EFI_BOOT_SERVICES_CODE:
739 case EFI_BOOT_SERVICES_DATA:
740 case EFI_CONVENTIONAL_MEMORY:
741 e820_type = E820_TYPE_RAM;
744 case EFI_ACPI_MEMORY_NVS:
745 e820_type = E820_TYPE_NVS;
748 case EFI_PERSISTENT_MEMORY:
749 e820_type = E820_TYPE_PMEM;
756 /* Merge adjacent mappings */
757 if (prev && prev->type == e820_type &&
758 (prev->addr + prev->size) == d->phys_addr) {
759 prev->size += d->num_pages << 12;
763 if (nr_entries == ARRAY_SIZE(params->e820_table)) {
764 u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
765 sizeof(struct setup_data);
767 if (!e820ext || e820ext_size < need)
768 return EFI_BUFFER_TOO_SMALL;
770 /* boot_params map full, switch to e820 extended */
771 entry = (struct boot_e820_entry *)e820ext->data;
774 entry->addr = d->phys_addr;
775 entry->size = d->num_pages << PAGE_SHIFT;
776 entry->type = e820_type;
781 if (nr_entries > ARRAY_SIZE(params->e820_table)) {
782 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
784 add_e820ext(params, e820ext, nr_e820ext);
785 nr_entries -= nr_e820ext;
788 params->e820_entries = (u8)nr_entries;
793 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
799 size = sizeof(struct setup_data) +
800 sizeof(struct e820_entry) * nr_desc;
803 efi_call_early(free_pool, *e820ext);
808 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
809 size, (void **)e820ext);
810 if (status == EFI_SUCCESS)
811 *e820ext_size = size;
816 struct exit_boot_struct {
817 struct boot_params *boot_params;
818 struct efi_info *efi;
819 struct setup_data *e820ext;
824 static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg,
825 struct efi_boot_memmap *map,
828 static bool first = true;
829 const char *signature;
832 struct exit_boot_struct *p = priv;
835 nr_desc = *map->buff_size / *map->desc_size;
836 if (nr_desc > ARRAY_SIZE(p->boot_params->e820_table)) {
837 u32 nr_e820ext = nr_desc -
838 ARRAY_SIZE(p->boot_params->e820_table);
840 status = alloc_e820ext(nr_e820ext, &p->e820ext,
842 if (status != EFI_SUCCESS)
848 signature = p->is64 ? EFI64_LOADER_SIGNATURE : EFI32_LOADER_SIGNATURE;
849 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
851 p->efi->efi_systab = (unsigned long)sys_table_arg;
852 p->efi->efi_memdesc_size = *map->desc_size;
853 p->efi->efi_memdesc_version = *map->desc_ver;
854 p->efi->efi_memmap = (unsigned long)*map->map;
855 p->efi->efi_memmap_size = *map->map_size;
858 p->efi->efi_systab_hi = (unsigned long)sys_table_arg >> 32;
859 p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32;
865 static efi_status_t exit_boot(struct boot_params *boot_params,
866 void *handle, bool is64)
868 unsigned long map_sz, key, desc_size, buff_size;
869 efi_memory_desc_t *mem_map;
870 struct setup_data *e820ext;
874 struct efi_boot_memmap map;
875 struct exit_boot_struct priv;
878 map.map_size = &map_sz;
879 map.desc_size = &desc_size;
880 map.desc_ver = &desc_version;
882 map.buff_size = &buff_size;
883 priv.boot_params = boot_params;
884 priv.efi = &boot_params->efi_info;
886 priv.e820ext_size = 0;
889 /* Might as well exit boot services now */
890 status = efi_exit_boot_services(sys_table, handle, &map, &priv,
892 if (status != EFI_SUCCESS)
895 e820ext = priv.e820ext;
896 e820ext_size = priv.e820ext_size;
898 boot_params->alt_mem_k = 32 * 1024;
900 status = setup_e820(boot_params, e820ext, e820ext_size);
901 if (status != EFI_SUCCESS)
908 * On success we return a pointer to a boot_params structure, and NULL
911 struct boot_params *efi_main(struct efi_config *c,
912 struct boot_params *boot_params)
914 struct desc_ptr *gdt = NULL;
915 efi_loaded_image_t *image;
916 struct setup_header *hdr = &boot_params->hdr;
918 struct desc_struct *desc;
920 efi_system_table_t *_table;
925 _table = (efi_system_table_t *)(unsigned long)efi_early->table;
926 handle = (void *)(unsigned long)efi_early->image_handle;
927 is64 = efi_early->is64;
931 /* Check if we were booted by the EFI firmware */
932 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
936 setup_boot_services64(efi_early);
938 setup_boot_services32(efi_early);
941 * If the boot loader gave us a value for secure_boot then we use that,
942 * otherwise we ask the BIOS.
944 if (boot_params->secure_boot == efi_secureboot_mode_unset)
945 boot_params->secure_boot = efi_get_secureboot(sys_table);
947 /* Ask the firmware to clear memory on unclean shutdown */
948 efi_enable_reset_attack_mitigation(sys_table);
949 efi_retrieve_tpm2_eventlog(sys_table);
951 setup_graphics(boot_params);
953 setup_efi_pci(boot_params);
955 setup_quirks(boot_params);
957 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
958 sizeof(*gdt), (void **)&gdt);
959 if (status != EFI_SUCCESS) {
960 efi_printk(sys_table, "Failed to alloc mem for gdt structure\n");
965 status = efi_low_alloc(sys_table, gdt->size, 8,
966 (unsigned long *)&gdt->address);
967 if (status != EFI_SUCCESS) {
968 efi_printk(sys_table, "Failed to alloc mem for gdt\n");
973 * If the kernel isn't already loaded at the preferred load
974 * address, relocate it.
976 if (hdr->pref_address != hdr->code32_start) {
977 unsigned long bzimage_addr = hdr->code32_start;
978 status = efi_relocate_kernel(sys_table, &bzimage_addr,
979 hdr->init_size, hdr->init_size,
981 hdr->kernel_alignment);
982 if (status != EFI_SUCCESS) {
983 efi_printk(sys_table, "efi_relocate_kernel() failed!\n");
987 hdr->pref_address = hdr->code32_start;
988 hdr->code32_start = bzimage_addr;
991 status = exit_boot(boot_params, handle, is64);
992 if (status != EFI_SUCCESS) {
993 efi_printk(sys_table, "exit_boot() failed!\n");
997 memset((char *)gdt->address, 0x0, gdt->size);
998 desc = (struct desc_struct *)gdt->address;
1000 /* The first GDT is a dummy. */
1003 if (IS_ENABLED(CONFIG_X86_64)) {
1005 desc->limit0 = 0xffff;
1006 desc->base0 = 0x0000;
1007 desc->base1 = 0x0000;
1008 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1009 desc->s = DESC_TYPE_CODE_DATA;
1015 desc->d = SEG_OP_SIZE_32BIT;
1016 desc->g = SEG_GRANULARITY_4KB;
1020 /* Second entry is unused on 32-bit */
1025 desc->limit0 = 0xffff;
1026 desc->base0 = 0x0000;
1027 desc->base1 = 0x0000;
1028 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1029 desc->s = DESC_TYPE_CODE_DATA;
1034 if (IS_ENABLED(CONFIG_X86_64)) {
1039 desc->d = SEG_OP_SIZE_32BIT;
1041 desc->g = SEG_GRANULARITY_4KB;
1046 desc->limit0 = 0xffff;
1047 desc->base0 = 0x0000;
1048 desc->base1 = 0x0000;
1049 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
1050 desc->s = DESC_TYPE_CODE_DATA;
1056 desc->d = SEG_OP_SIZE_32BIT;
1057 desc->g = SEG_GRANULARITY_4KB;
1061 if (IS_ENABLED(CONFIG_X86_64)) {
1062 /* Task segment value */
1063 desc->limit0 = 0x0000;
1064 desc->base0 = 0x0000;
1065 desc->base1 = 0x0000;
1066 desc->type = SEG_TYPE_TSS;
1074 desc->g = SEG_GRANULARITY_4KB;
1079 asm volatile("cli");
1080 asm volatile ("lgdt %0" : : "m" (*gdt));
1084 efi_printk(sys_table, "efi_main() failed!\n");