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)
126 romimage = (void *)(unsigned long)efi_table_attr(efi_pci_io_protocol,
128 romsize = efi_table_attr(efi_pci_io_protocol, romsize, pci);
129 if (!romimage || !romsize)
130 return EFI_INVALID_PARAMETER;
132 size = romsize + sizeof(*rom);
134 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
135 if (status != EFI_SUCCESS) {
136 efi_printk(sys_table, "Failed to alloc mem for rom\n");
140 memset(rom, 0, sizeof(*rom));
142 rom->data.type = SETUP_PCI;
143 rom->data.len = size - sizeof(struct setup_data);
145 rom->pcilen = pci->romsize;
148 status = efi_call_proto(efi_pci_io_protocol, pci.read, pci,
149 EfiPciIoWidthUint16, PCI_VENDOR_ID, 1,
152 if (status != EFI_SUCCESS) {
153 efi_printk(sys_table, "Failed to read rom->vendor\n");
157 status = efi_call_proto(efi_pci_io_protocol, pci.read, pci,
158 EfiPciIoWidthUint16, PCI_DEVICE_ID, 1,
161 if (status != EFI_SUCCESS) {
162 efi_printk(sys_table, "Failed to read rom->devid\n");
166 status = efi_call_proto(efi_pci_io_protocol, get_location, pci,
167 &rom->segment, &rom->bus, &rom->device,
170 if (status != EFI_SUCCESS)
173 memcpy(rom->romdata, romimage, romsize);
177 efi_call_early(free_pool, rom);
182 setup_efi_pci32(struct boot_params *params, void **pci_handle,
185 efi_pci_io_protocol_t *pci = NULL;
186 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
187 u32 *handles = (u32 *)(unsigned long)pci_handle;
189 unsigned long nr_pci;
190 struct setup_data *data;
193 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
195 while (data && data->next)
196 data = (struct setup_data *)(unsigned long)data->next;
198 nr_pci = size / sizeof(u32);
199 for (i = 0; i < nr_pci; i++) {
200 struct pci_setup_rom *rom = NULL;
203 status = efi_call_early(handle_protocol, h,
204 &pci_proto, (void **)&pci);
206 if (status != EFI_SUCCESS)
212 status = __setup_efi_pci(pci, &rom);
213 if (status != EFI_SUCCESS)
217 data->next = (unsigned long)rom;
219 params->hdr.setup_data = (unsigned long)rom;
221 data = (struct setup_data *)rom;
227 setup_efi_pci64(struct boot_params *params, void **pci_handle,
230 efi_pci_io_protocol_t *pci = NULL;
231 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
232 u64 *handles = (u64 *)(unsigned long)pci_handle;
234 unsigned long nr_pci;
235 struct setup_data *data;
238 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
240 while (data && data->next)
241 data = (struct setup_data *)(unsigned long)data->next;
243 nr_pci = size / sizeof(u64);
244 for (i = 0; i < nr_pci; i++) {
245 struct pci_setup_rom *rom = NULL;
248 status = efi_call_early(handle_protocol, h,
249 &pci_proto, (void **)&pci);
251 if (status != EFI_SUCCESS)
257 status = __setup_efi_pci(pci, &rom);
258 if (status != EFI_SUCCESS)
262 data->next = (unsigned long)rom;
264 params->hdr.setup_data = (unsigned long)rom;
266 data = (struct setup_data *)rom;
272 * There's no way to return an informative status from this function,
273 * because any analysis (and printing of error messages) needs to be
274 * done directly at the EFI function call-site.
276 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
277 * just didn't find any PCI devices, but there's no way to tell outside
278 * the context of the call.
280 static void setup_efi_pci(struct boot_params *params)
283 void **pci_handle = NULL;
284 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
285 unsigned long size = 0;
287 status = efi_call_early(locate_handle,
288 EFI_LOCATE_BY_PROTOCOL,
289 &pci_proto, NULL, &size, pci_handle);
291 if (status == EFI_BUFFER_TOO_SMALL) {
292 status = efi_call_early(allocate_pool,
294 size, (void **)&pci_handle);
296 if (status != EFI_SUCCESS) {
297 efi_printk(sys_table, "Failed to alloc mem for pci_handle\n");
301 status = efi_call_early(locate_handle,
302 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
303 NULL, &size, pci_handle);
306 if (status != EFI_SUCCESS)
310 setup_efi_pci64(params, pci_handle, size);
312 setup_efi_pci32(params, pci_handle, size);
315 efi_call_early(free_pool, pci_handle);
318 static void retrieve_apple_device_properties(struct boot_params *boot_params)
320 efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
321 struct setup_data *data, *new;
326 status = efi_call_early(locate_protocol, &guid, NULL, &p);
327 if (status != EFI_SUCCESS)
330 if (efi_table_attr(apple_properties_protocol, version, p) != 0x10000) {
331 efi_printk(sys_table, "Unsupported properties proto version\n");
335 efi_call_proto(apple_properties_protocol, get_all, p, NULL, &size);
340 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
341 size + sizeof(struct setup_data), &new);
342 if (status != EFI_SUCCESS) {
343 efi_printk(sys_table,
344 "Failed to alloc mem for properties\n");
348 status = efi_call_proto(apple_properties_protocol, get_all, p,
351 if (status == EFI_BUFFER_TOO_SMALL)
352 efi_call_early(free_pool, new);
353 } while (status == EFI_BUFFER_TOO_SMALL);
355 new->type = SETUP_APPLE_PROPERTIES;
359 data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
361 boot_params->hdr.setup_data = (unsigned long)new;
364 data = (struct setup_data *)(unsigned long)data->next;
365 data->next = (unsigned long)new;
369 static const efi_char16_t apple[] = L"Apple";
371 static void setup_quirks(struct boot_params *boot_params)
373 efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
374 efi_table_attr(efi_system_table, fw_vendor, sys_table);
376 if (!memcmp(fw_vendor, apple, sizeof(apple))) {
377 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
378 retrieve_apple_device_properties(boot_params);
383 setup_uga32(void **uga_handle, unsigned long size, u32 *width, u32 *height)
385 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
386 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
387 unsigned long nr_ugas;
388 u32 *handles = (u32 *)uga_handle;
389 efi_status_t status = EFI_INVALID_PARAMETER;
393 nr_ugas = size / sizeof(u32);
394 for (i = 0; i < nr_ugas; i++) {
395 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
396 u32 w, h, depth, refresh;
398 u32 handle = handles[i];
400 status = efi_call_early(handle_protocol, handle,
401 &uga_proto, (void **)&uga);
402 if (status != EFI_SUCCESS)
405 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
407 status = efi_early->call((unsigned long)uga->get_mode, uga,
408 &w, &h, &depth, &refresh);
409 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
414 * Once we've found a UGA supporting PCIIO,
415 * don't bother looking any further.
428 setup_uga64(void **uga_handle, unsigned long size, u32 *width, u32 *height)
430 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
431 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
432 unsigned long nr_ugas;
433 u64 *handles = (u64 *)uga_handle;
434 efi_status_t status = EFI_INVALID_PARAMETER;
438 nr_ugas = size / sizeof(u64);
439 for (i = 0; i < nr_ugas; i++) {
440 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
441 u32 w, h, depth, refresh;
443 u64 handle = handles[i];
445 status = efi_call_early(handle_protocol, handle,
446 &uga_proto, (void **)&uga);
447 if (status != EFI_SUCCESS)
450 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
452 status = efi_early->call((unsigned long)uga->get_mode, uga,
453 &w, &h, &depth, &refresh);
454 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
459 * Once we've found a UGA supporting PCIIO,
460 * don't bother looking any further.
473 * See if we have Universal Graphics Adapter (UGA) protocol
475 static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
480 void **uga_handle = NULL;
482 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
483 size, (void **)&uga_handle);
484 if (status != EFI_SUCCESS)
487 status = efi_call_early(locate_handle,
488 EFI_LOCATE_BY_PROTOCOL,
489 uga_proto, NULL, &size, uga_handle);
490 if (status != EFI_SUCCESS)
497 status = setup_uga64(uga_handle, size, &width, &height);
499 status = setup_uga32(uga_handle, size, &width, &height);
501 if (!width && !height)
504 /* EFI framebuffer */
505 si->orig_video_isVGA = VIDEO_TYPE_EFI;
508 si->lfb_width = width;
509 si->lfb_height = height;
521 efi_call_early(free_pool, uga_handle);
525 void setup_graphics(struct boot_params *boot_params)
527 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
528 struct screen_info *si;
529 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
532 void **gop_handle = NULL;
533 void **uga_handle = NULL;
535 si = &boot_params->screen_info;
536 memset(si, 0, sizeof(*si));
539 status = efi_call_early(locate_handle,
540 EFI_LOCATE_BY_PROTOCOL,
541 &graphics_proto, NULL, &size, gop_handle);
542 if (status == EFI_BUFFER_TOO_SMALL)
543 status = efi_setup_gop(NULL, si, &graphics_proto, size);
545 if (status != EFI_SUCCESS) {
547 status = efi_call_early(locate_handle,
548 EFI_LOCATE_BY_PROTOCOL,
549 &uga_proto, NULL, &size, uga_handle);
550 if (status == EFI_BUFFER_TOO_SMALL)
551 setup_uga(si, &uga_proto, size);
556 * Because the x86 boot code expects to be passed a boot_params we
557 * need to create one ourselves (usually the bootloader would create
560 * The caller is responsible for filling out ->code32_start in the
561 * returned boot_params.
563 struct boot_params *make_boot_params(struct efi_config *c)
565 struct boot_params *boot_params;
566 struct apm_bios_info *bi;
567 struct setup_header *hdr;
568 efi_loaded_image_t *image;
569 void *options, *handle;
570 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
571 int options_size = 0;
577 unsigned long ramdisk_addr;
578 unsigned long ramdisk_size;
581 sys_table = (efi_system_table_t *)(unsigned long)efi_early->table;
582 handle = (void *)(unsigned long)efi_early->image_handle;
584 /* Check if we were booted by the EFI firmware */
585 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
589 setup_boot_services64(efi_early);
591 setup_boot_services32(efi_early);
593 status = efi_call_early(handle_protocol, handle,
594 &proto, (void *)&image);
595 if (status != EFI_SUCCESS) {
596 efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
600 status = efi_low_alloc(sys_table, 0x4000, 1,
601 (unsigned long *)&boot_params);
602 if (status != EFI_SUCCESS) {
603 efi_printk(sys_table, "Failed to alloc lowmem for boot params\n");
607 memset(boot_params, 0x0, 0x4000);
609 hdr = &boot_params->hdr;
610 bi = &boot_params->apm_bios_info;
612 /* Copy the second sector to boot_params */
613 memcpy(&hdr->jump, image->image_base + 512, 512);
616 * Fill out some of the header fields ourselves because the
617 * EFI firmware loader doesn't load the first sector.
620 hdr->vid_mode = 0xffff;
621 hdr->boot_flag = 0xAA55;
623 hdr->type_of_loader = 0x21;
625 /* Convert unicode cmdline to ascii */
626 cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size);
629 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
630 /* Fill in upper bits of command line address, NOP on 32 bit */
631 boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32;
633 hdr->ramdisk_image = 0;
634 hdr->ramdisk_size = 0;
636 /* Clear APM BIOS info */
637 memset(bi, 0, sizeof(*bi));
639 status = efi_parse_options(cmdline_ptr);
640 if (status != EFI_SUCCESS)
643 status = handle_cmdline_files(sys_table, image,
644 (char *)(unsigned long)hdr->cmd_line_ptr,
645 "initrd=", hdr->initrd_addr_max,
646 &ramdisk_addr, &ramdisk_size);
648 if (status != EFI_SUCCESS &&
649 hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) {
650 efi_printk(sys_table, "Trying to load files to higher address\n");
651 status = handle_cmdline_files(sys_table, image,
652 (char *)(unsigned long)hdr->cmd_line_ptr,
654 &ramdisk_addr, &ramdisk_size);
657 if (status != EFI_SUCCESS)
659 hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
660 hdr->ramdisk_size = ramdisk_size & 0xffffffff;
661 boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
662 boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32;
666 efi_free(sys_table, options_size, hdr->cmd_line_ptr);
668 efi_free(sys_table, 0x4000, (unsigned long)boot_params);
672 static void add_e820ext(struct boot_params *params,
673 struct setup_data *e820ext, u32 nr_entries)
675 struct setup_data *data;
679 e820ext->type = SETUP_E820_EXT;
680 e820ext->len = nr_entries * sizeof(struct boot_e820_entry);
683 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
685 while (data && data->next)
686 data = (struct setup_data *)(unsigned long)data->next;
689 data->next = (unsigned long)e820ext;
691 params->hdr.setup_data = (unsigned long)e820ext;
694 static efi_status_t setup_e820(struct boot_params *params,
695 struct setup_data *e820ext, u32 e820ext_size)
697 struct boot_e820_entry *entry = params->e820_table;
698 struct efi_info *efi = ¶ms->efi_info;
699 struct boot_e820_entry *prev = NULL;
705 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
707 for (i = 0; i < nr_desc; i++) {
708 efi_memory_desc_t *d;
709 unsigned int e820_type = 0;
710 unsigned long m = efi->efi_memmap;
713 m |= (u64)efi->efi_memmap_hi << 32;
716 d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
718 case EFI_RESERVED_TYPE:
719 case EFI_RUNTIME_SERVICES_CODE:
720 case EFI_RUNTIME_SERVICES_DATA:
721 case EFI_MEMORY_MAPPED_IO:
722 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
724 e820_type = E820_TYPE_RESERVED;
727 case EFI_UNUSABLE_MEMORY:
728 e820_type = E820_TYPE_UNUSABLE;
731 case EFI_ACPI_RECLAIM_MEMORY:
732 e820_type = E820_TYPE_ACPI;
735 case EFI_LOADER_CODE:
736 case EFI_LOADER_DATA:
737 case EFI_BOOT_SERVICES_CODE:
738 case EFI_BOOT_SERVICES_DATA:
739 case EFI_CONVENTIONAL_MEMORY:
740 e820_type = E820_TYPE_RAM;
743 case EFI_ACPI_MEMORY_NVS:
744 e820_type = E820_TYPE_NVS;
747 case EFI_PERSISTENT_MEMORY:
748 e820_type = E820_TYPE_PMEM;
755 /* Merge adjacent mappings */
756 if (prev && prev->type == e820_type &&
757 (prev->addr + prev->size) == d->phys_addr) {
758 prev->size += d->num_pages << 12;
762 if (nr_entries == ARRAY_SIZE(params->e820_table)) {
763 u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
764 sizeof(struct setup_data);
766 if (!e820ext || e820ext_size < need)
767 return EFI_BUFFER_TOO_SMALL;
769 /* boot_params map full, switch to e820 extended */
770 entry = (struct boot_e820_entry *)e820ext->data;
773 entry->addr = d->phys_addr;
774 entry->size = d->num_pages << PAGE_SHIFT;
775 entry->type = e820_type;
780 if (nr_entries > ARRAY_SIZE(params->e820_table)) {
781 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
783 add_e820ext(params, e820ext, nr_e820ext);
784 nr_entries -= nr_e820ext;
787 params->e820_entries = (u8)nr_entries;
792 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
798 size = sizeof(struct setup_data) +
799 sizeof(struct e820_entry) * nr_desc;
802 efi_call_early(free_pool, *e820ext);
807 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
808 size, (void **)e820ext);
809 if (status == EFI_SUCCESS)
810 *e820ext_size = size;
815 struct exit_boot_struct {
816 struct boot_params *boot_params;
817 struct efi_info *efi;
818 struct setup_data *e820ext;
823 static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg,
824 struct efi_boot_memmap *map,
827 static bool first = true;
828 const char *signature;
831 struct exit_boot_struct *p = priv;
834 nr_desc = *map->buff_size / *map->desc_size;
835 if (nr_desc > ARRAY_SIZE(p->boot_params->e820_table)) {
836 u32 nr_e820ext = nr_desc -
837 ARRAY_SIZE(p->boot_params->e820_table);
839 status = alloc_e820ext(nr_e820ext, &p->e820ext,
841 if (status != EFI_SUCCESS)
847 signature = p->is64 ? EFI64_LOADER_SIGNATURE : EFI32_LOADER_SIGNATURE;
848 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
850 p->efi->efi_systab = (unsigned long)sys_table_arg;
851 p->efi->efi_memdesc_size = *map->desc_size;
852 p->efi->efi_memdesc_version = *map->desc_ver;
853 p->efi->efi_memmap = (unsigned long)*map->map;
854 p->efi->efi_memmap_size = *map->map_size;
857 p->efi->efi_systab_hi = (unsigned long)sys_table_arg >> 32;
858 p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32;
864 static efi_status_t exit_boot(struct boot_params *boot_params,
865 void *handle, bool is64)
867 unsigned long map_sz, key, desc_size, buff_size;
868 efi_memory_desc_t *mem_map;
869 struct setup_data *e820ext;
873 struct efi_boot_memmap map;
874 struct exit_boot_struct priv;
877 map.map_size = &map_sz;
878 map.desc_size = &desc_size;
879 map.desc_ver = &desc_version;
881 map.buff_size = &buff_size;
882 priv.boot_params = boot_params;
883 priv.efi = &boot_params->efi_info;
885 priv.e820ext_size = 0;
888 /* Might as well exit boot services now */
889 status = efi_exit_boot_services(sys_table, handle, &map, &priv,
891 if (status != EFI_SUCCESS)
894 e820ext = priv.e820ext;
895 e820ext_size = priv.e820ext_size;
897 boot_params->alt_mem_k = 32 * 1024;
899 status = setup_e820(boot_params, e820ext, e820ext_size);
900 if (status != EFI_SUCCESS)
907 * On success we return a pointer to a boot_params structure, and NULL
910 struct boot_params *efi_main(struct efi_config *c,
911 struct boot_params *boot_params)
913 struct desc_ptr *gdt = NULL;
914 efi_loaded_image_t *image;
915 struct setup_header *hdr = &boot_params->hdr;
917 struct desc_struct *desc;
919 efi_system_table_t *_table;
924 _table = (efi_system_table_t *)(unsigned long)efi_early->table;
925 handle = (void *)(unsigned long)efi_early->image_handle;
926 is64 = efi_early->is64;
930 /* Check if we were booted by the EFI firmware */
931 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
935 setup_boot_services64(efi_early);
937 setup_boot_services32(efi_early);
940 * If the boot loader gave us a value for secure_boot then we use that,
941 * otherwise we ask the BIOS.
943 if (boot_params->secure_boot == efi_secureboot_mode_unset)
944 boot_params->secure_boot = efi_get_secureboot(sys_table);
946 /* Ask the firmware to clear memory on unclean shutdown */
947 efi_enable_reset_attack_mitigation(sys_table);
948 efi_retrieve_tpm2_eventlog(sys_table);
950 setup_graphics(boot_params);
952 setup_efi_pci(boot_params);
954 setup_quirks(boot_params);
956 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
957 sizeof(*gdt), (void **)&gdt);
958 if (status != EFI_SUCCESS) {
959 efi_printk(sys_table, "Failed to alloc mem for gdt structure\n");
964 status = efi_low_alloc(sys_table, gdt->size, 8,
965 (unsigned long *)&gdt->address);
966 if (status != EFI_SUCCESS) {
967 efi_printk(sys_table, "Failed to alloc mem for gdt\n");
972 * If the kernel isn't already loaded at the preferred load
973 * address, relocate it.
975 if (hdr->pref_address != hdr->code32_start) {
976 unsigned long bzimage_addr = hdr->code32_start;
977 status = efi_relocate_kernel(sys_table, &bzimage_addr,
978 hdr->init_size, hdr->init_size,
980 hdr->kernel_alignment);
981 if (status != EFI_SUCCESS) {
982 efi_printk(sys_table, "efi_relocate_kernel() failed!\n");
986 hdr->pref_address = hdr->code32_start;
987 hdr->code32_start = bzimage_addr;
990 status = exit_boot(boot_params, handle, is64);
991 if (status != EFI_SUCCESS) {
992 efi_printk(sys_table, "exit_boot() failed!\n");
996 memset((char *)gdt->address, 0x0, gdt->size);
997 desc = (struct desc_struct *)gdt->address;
999 /* The first GDT is a dummy. */
1002 if (IS_ENABLED(CONFIG_X86_64)) {
1004 desc->limit0 = 0xffff;
1005 desc->base0 = 0x0000;
1006 desc->base1 = 0x0000;
1007 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1008 desc->s = DESC_TYPE_CODE_DATA;
1014 desc->d = SEG_OP_SIZE_32BIT;
1015 desc->g = SEG_GRANULARITY_4KB;
1019 /* Second entry is unused on 32-bit */
1024 desc->limit0 = 0xffff;
1025 desc->base0 = 0x0000;
1026 desc->base1 = 0x0000;
1027 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1028 desc->s = DESC_TYPE_CODE_DATA;
1033 if (IS_ENABLED(CONFIG_X86_64)) {
1038 desc->d = SEG_OP_SIZE_32BIT;
1040 desc->g = SEG_GRANULARITY_4KB;
1045 desc->limit0 = 0xffff;
1046 desc->base0 = 0x0000;
1047 desc->base1 = 0x0000;
1048 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
1049 desc->s = DESC_TYPE_CODE_DATA;
1055 desc->d = SEG_OP_SIZE_32BIT;
1056 desc->g = SEG_GRANULARITY_4KB;
1060 if (IS_ENABLED(CONFIG_X86_64)) {
1061 /* Task segment value */
1062 desc->limit0 = 0x0000;
1063 desc->base0 = 0x0000;
1064 desc->base1 = 0x0000;
1065 desc->type = SEG_TYPE_TSS;
1073 desc->g = SEG_GRANULARITY_4KB;
1078 asm volatile("cli");
1079 asm volatile ("lgdt %0" : : "m" (*gdt));
1083 efi_printk(sys_table, "efi_main() failed!\n");