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>
13 #include <asm/setup.h>
16 #include "../string.h"
19 static efi_system_table_t *sys_table;
21 static struct efi_config *efi_early;
23 __pure const struct efi_config *__efi_early(void)
28 #define BOOT_SERVICES(bits) \
29 static void setup_boot_services##bits(struct efi_config *c) \
31 efi_system_table_##bits##_t *table; \
33 table = (typeof(table))sys_table; \
35 c->runtime_services = table->runtime; \
36 c->boot_services = table->boottime; \
37 c->text_output = table->con_out; \
42 static inline efi_status_t __open_volume32(void *__image, void **__fh)
44 efi_file_io_interface_t *io;
45 efi_loaded_image_32_t *image = __image;
46 efi_file_handle_32_t *fh;
47 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
49 void *handle = (void *)(unsigned long)image->device_handle;
52 status = efi_call_early(handle_protocol, handle,
53 &fs_proto, (void **)&io);
54 if (status != EFI_SUCCESS) {
55 efi_printk(sys_table, "Failed to handle fs_proto\n");
59 func = (unsigned long)io->open_volume;
60 status = efi_early->call(func, io, &fh);
61 if (status != EFI_SUCCESS)
62 efi_printk(sys_table, "Failed to open volume\n");
68 static inline efi_status_t __open_volume64(void *__image, void **__fh)
70 efi_file_io_interface_t *io;
71 efi_loaded_image_64_t *image = __image;
72 efi_file_handle_64_t *fh;
73 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
75 void *handle = (void *)(unsigned long)image->device_handle;
78 status = efi_call_early(handle_protocol, handle,
79 &fs_proto, (void **)&io);
80 if (status != EFI_SUCCESS) {
81 efi_printk(sys_table, "Failed to handle fs_proto\n");
85 func = (unsigned long)io->open_volume;
86 status = efi_early->call(func, io, &fh);
87 if (status != EFI_SUCCESS)
88 efi_printk(sys_table, "Failed to open volume\n");
95 efi_open_volume(efi_system_table_t *sys_table, void *__image, void **__fh)
98 return __open_volume64(__image, __fh);
100 return __open_volume32(__image, __fh);
103 void efi_char16_printk(efi_system_table_t *table, efi_char16_t *str)
105 efi_call_proto(efi_simple_text_output_protocol, output_string,
106 efi_early->text_output, str);
110 __setup_efi_pci32(efi_pci_io_protocol_32 *pci, struct pci_setup_rom **__rom)
112 struct pci_setup_rom *rom = NULL;
117 status = efi_early->call(pci->attributes, pci,
118 EfiPciIoAttributeOperationGet, 0, 0,
120 if (status != EFI_SUCCESS)
123 if (!pci->romimage || !pci->romsize)
124 return EFI_INVALID_PARAMETER;
126 size = pci->romsize + sizeof(*rom);
128 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
129 if (status != EFI_SUCCESS) {
130 efi_printk(sys_table, "Failed to alloc mem for rom\n");
134 memset(rom, 0, sizeof(*rom));
136 rom->data.type = SETUP_PCI;
137 rom->data.len = size - sizeof(struct setup_data);
139 rom->pcilen = pci->romsize;
142 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
143 PCI_VENDOR_ID, 1, &(rom->vendor));
145 if (status != EFI_SUCCESS) {
146 efi_printk(sys_table, "Failed to read rom->vendor\n");
150 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
151 PCI_DEVICE_ID, 1, &(rom->devid));
153 if (status != EFI_SUCCESS) {
154 efi_printk(sys_table, "Failed to read rom->devid\n");
158 status = efi_early->call(pci->get_location, pci, &(rom->segment),
159 &(rom->bus), &(rom->device), &(rom->function));
161 if (status != EFI_SUCCESS)
164 memcpy(rom->romdata, pci->romimage, pci->romsize);
168 efi_call_early(free_pool, rom);
173 setup_efi_pci32(struct boot_params *params, void **pci_handle,
176 efi_pci_io_protocol_32 *pci = NULL;
177 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
178 u32 *handles = (u32 *)(unsigned long)pci_handle;
180 unsigned long nr_pci;
181 struct setup_data *data;
184 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
186 while (data && data->next)
187 data = (struct setup_data *)(unsigned long)data->next;
189 nr_pci = size / sizeof(u32);
190 for (i = 0; i < nr_pci; i++) {
191 struct pci_setup_rom *rom = NULL;
194 status = efi_call_early(handle_protocol, h,
195 &pci_proto, (void **)&pci);
197 if (status != EFI_SUCCESS)
203 status = __setup_efi_pci32(pci, &rom);
204 if (status != EFI_SUCCESS)
208 data->next = (unsigned long)rom;
210 params->hdr.setup_data = (unsigned long)rom;
212 data = (struct setup_data *)rom;
218 __setup_efi_pci64(efi_pci_io_protocol_64 *pci, struct pci_setup_rom **__rom)
220 struct pci_setup_rom *rom;
225 status = efi_early->call(pci->attributes, pci,
226 EfiPciIoAttributeOperationGet, 0,
228 if (status != EFI_SUCCESS)
231 if (!pci->romimage || !pci->romsize)
232 return EFI_INVALID_PARAMETER;
234 size = pci->romsize + sizeof(*rom);
236 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
237 if (status != EFI_SUCCESS) {
238 efi_printk(sys_table, "Failed to alloc mem for rom\n");
242 rom->data.type = SETUP_PCI;
243 rom->data.len = size - sizeof(struct setup_data);
245 rom->pcilen = pci->romsize;
248 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
249 PCI_VENDOR_ID, 1, &(rom->vendor));
251 if (status != EFI_SUCCESS) {
252 efi_printk(sys_table, "Failed to read rom->vendor\n");
256 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
257 PCI_DEVICE_ID, 1, &(rom->devid));
259 if (status != EFI_SUCCESS) {
260 efi_printk(sys_table, "Failed to read rom->devid\n");
264 status = efi_early->call(pci->get_location, pci, &(rom->segment),
265 &(rom->bus), &(rom->device), &(rom->function));
267 if (status != EFI_SUCCESS)
270 memcpy(rom->romdata, pci->romimage, pci->romsize);
274 efi_call_early(free_pool, rom);
280 setup_efi_pci64(struct boot_params *params, void **pci_handle,
283 efi_pci_io_protocol_64 *pci = NULL;
284 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
285 u64 *handles = (u64 *)(unsigned long)pci_handle;
287 unsigned long nr_pci;
288 struct setup_data *data;
291 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
293 while (data && data->next)
294 data = (struct setup_data *)(unsigned long)data->next;
296 nr_pci = size / sizeof(u64);
297 for (i = 0; i < nr_pci; i++) {
298 struct pci_setup_rom *rom = NULL;
301 status = efi_call_early(handle_protocol, h,
302 &pci_proto, (void **)&pci);
304 if (status != EFI_SUCCESS)
310 status = __setup_efi_pci64(pci, &rom);
311 if (status != EFI_SUCCESS)
315 data->next = (unsigned long)rom;
317 params->hdr.setup_data = (unsigned long)rom;
319 data = (struct setup_data *)rom;
325 * There's no way to return an informative status from this function,
326 * because any analysis (and printing of error messages) needs to be
327 * done directly at the EFI function call-site.
329 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
330 * just didn't find any PCI devices, but there's no way to tell outside
331 * the context of the call.
333 static void setup_efi_pci(struct boot_params *params)
336 void **pci_handle = NULL;
337 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
338 unsigned long size = 0;
340 status = efi_call_early(locate_handle,
341 EFI_LOCATE_BY_PROTOCOL,
342 &pci_proto, NULL, &size, pci_handle);
344 if (status == EFI_BUFFER_TOO_SMALL) {
345 status = efi_call_early(allocate_pool,
347 size, (void **)&pci_handle);
349 if (status != EFI_SUCCESS) {
350 efi_printk(sys_table, "Failed to alloc mem for pci_handle\n");
354 status = efi_call_early(locate_handle,
355 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
356 NULL, &size, pci_handle);
359 if (status != EFI_SUCCESS)
363 setup_efi_pci64(params, pci_handle, size);
365 setup_efi_pci32(params, pci_handle, size);
368 efi_call_early(free_pool, pci_handle);
371 static void retrieve_apple_device_properties(struct boot_params *boot_params)
373 efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
374 struct setup_data *data, *new;
379 status = efi_call_early(locate_protocol, &guid, NULL, &p);
380 if (status != EFI_SUCCESS)
383 if (efi_table_attr(apple_properties_protocol, version, p) != 0x10000) {
384 efi_printk(sys_table, "Unsupported properties proto version\n");
388 efi_call_proto(apple_properties_protocol, get_all, p, NULL, &size);
393 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
394 size + sizeof(struct setup_data), &new);
395 if (status != EFI_SUCCESS) {
396 efi_printk(sys_table,
397 "Failed to alloc mem for properties\n");
401 status = efi_call_proto(apple_properties_protocol, get_all, p,
404 if (status == EFI_BUFFER_TOO_SMALL)
405 efi_call_early(free_pool, new);
406 } while (status == EFI_BUFFER_TOO_SMALL);
408 new->type = SETUP_APPLE_PROPERTIES;
412 data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
414 boot_params->hdr.setup_data = (unsigned long)new;
417 data = (struct setup_data *)(unsigned long)data->next;
418 data->next = (unsigned long)new;
422 static void setup_quirks(struct boot_params *boot_params)
424 efi_char16_t const apple[] = { 'A', 'p', 'p', 'l', 'e', 0 };
425 efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
426 efi_table_attr(efi_system_table, fw_vendor, sys_table);
428 if (!memcmp(fw_vendor, apple, sizeof(apple))) {
429 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
430 retrieve_apple_device_properties(boot_params);
435 setup_uga32(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 u32 *handles = (u32 *)uga_handle;;
441 efi_status_t status = EFI_INVALID_PARAMETER;
445 nr_ugas = size / sizeof(u32);
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 u32 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 setup_uga64(void **uga_handle, unsigned long size, u32 *width, u32 *height)
482 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
483 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
484 unsigned long nr_ugas;
485 u64 *handles = (u64 *)uga_handle;;
486 efi_status_t status = EFI_INVALID_PARAMETER;
490 nr_ugas = size / sizeof(u64);
491 for (i = 0; i < nr_ugas; i++) {
492 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
493 u32 w, h, depth, refresh;
495 u64 handle = handles[i];
497 status = efi_call_early(handle_protocol, handle,
498 &uga_proto, (void **)&uga);
499 if (status != EFI_SUCCESS)
502 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
504 status = efi_early->call((unsigned long)uga->get_mode, uga,
505 &w, &h, &depth, &refresh);
506 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
511 * Once we've found a UGA supporting PCIIO,
512 * don't bother looking any further.
525 * See if we have Universal Graphics Adapter (UGA) protocol
527 static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
532 void **uga_handle = NULL;
534 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
535 size, (void **)&uga_handle);
536 if (status != EFI_SUCCESS)
539 status = efi_call_early(locate_handle,
540 EFI_LOCATE_BY_PROTOCOL,
541 uga_proto, NULL, &size, uga_handle);
542 if (status != EFI_SUCCESS)
549 status = setup_uga64(uga_handle, size, &width, &height);
551 status = setup_uga32(uga_handle, size, &width, &height);
553 if (!width && !height)
556 /* EFI framebuffer */
557 si->orig_video_isVGA = VIDEO_TYPE_EFI;
560 si->lfb_width = width;
561 si->lfb_height = height;
573 efi_call_early(free_pool, uga_handle);
577 void setup_graphics(struct boot_params *boot_params)
579 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
580 struct screen_info *si;
581 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
584 void **gop_handle = NULL;
585 void **uga_handle = NULL;
587 si = &boot_params->screen_info;
588 memset(si, 0, sizeof(*si));
591 status = efi_call_early(locate_handle,
592 EFI_LOCATE_BY_PROTOCOL,
593 &graphics_proto, NULL, &size, gop_handle);
594 if (status == EFI_BUFFER_TOO_SMALL)
595 status = efi_setup_gop(NULL, si, &graphics_proto, size);
597 if (status != EFI_SUCCESS) {
599 status = efi_call_early(locate_handle,
600 EFI_LOCATE_BY_PROTOCOL,
601 &uga_proto, NULL, &size, uga_handle);
602 if (status == EFI_BUFFER_TOO_SMALL)
603 setup_uga(si, &uga_proto, size);
608 * Because the x86 boot code expects to be passed a boot_params we
609 * need to create one ourselves (usually the bootloader would create
612 * The caller is responsible for filling out ->code32_start in the
613 * returned boot_params.
615 struct boot_params *make_boot_params(struct efi_config *c)
617 struct boot_params *boot_params;
618 struct apm_bios_info *bi;
619 struct setup_header *hdr;
620 efi_loaded_image_t *image;
621 void *options, *handle;
622 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
623 int options_size = 0;
629 unsigned long ramdisk_addr;
630 unsigned long ramdisk_size;
633 sys_table = (efi_system_table_t *)(unsigned long)efi_early->table;
634 handle = (void *)(unsigned long)efi_early->image_handle;
636 /* Check if we were booted by the EFI firmware */
637 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
641 setup_boot_services64(efi_early);
643 setup_boot_services32(efi_early);
645 status = efi_call_early(handle_protocol, handle,
646 &proto, (void *)&image);
647 if (status != EFI_SUCCESS) {
648 efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
652 status = efi_low_alloc(sys_table, 0x4000, 1,
653 (unsigned long *)&boot_params);
654 if (status != EFI_SUCCESS) {
655 efi_printk(sys_table, "Failed to alloc lowmem for boot params\n");
659 memset(boot_params, 0x0, 0x4000);
661 hdr = &boot_params->hdr;
662 bi = &boot_params->apm_bios_info;
664 /* Copy the second sector to boot_params */
665 memcpy(&hdr->jump, image->image_base + 512, 512);
668 * Fill out some of the header fields ourselves because the
669 * EFI firmware loader doesn't load the first sector.
672 hdr->vid_mode = 0xffff;
673 hdr->boot_flag = 0xAA55;
675 hdr->type_of_loader = 0x21;
677 /* Convert unicode cmdline to ascii */
678 cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size);
681 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
682 /* Fill in upper bits of command line address, NOP on 32 bit */
683 boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32;
685 hdr->ramdisk_image = 0;
686 hdr->ramdisk_size = 0;
688 /* Clear APM BIOS info */
689 memset(bi, 0, sizeof(*bi));
691 status = efi_parse_options(cmdline_ptr);
692 if (status != EFI_SUCCESS)
695 status = handle_cmdline_files(sys_table, image,
696 (char *)(unsigned long)hdr->cmd_line_ptr,
697 "initrd=", hdr->initrd_addr_max,
698 &ramdisk_addr, &ramdisk_size);
700 if (status != EFI_SUCCESS &&
701 hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) {
702 efi_printk(sys_table, "Trying to load files to higher address\n");
703 status = handle_cmdline_files(sys_table, image,
704 (char *)(unsigned long)hdr->cmd_line_ptr,
706 &ramdisk_addr, &ramdisk_size);
709 if (status != EFI_SUCCESS)
711 hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
712 hdr->ramdisk_size = ramdisk_size & 0xffffffff;
713 boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
714 boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32;
718 efi_free(sys_table, options_size, hdr->cmd_line_ptr);
720 efi_free(sys_table, 0x4000, (unsigned long)boot_params);
724 static void add_e820ext(struct boot_params *params,
725 struct setup_data *e820ext, u32 nr_entries)
727 struct setup_data *data;
731 e820ext->type = SETUP_E820_EXT;
732 e820ext->len = nr_entries * sizeof(struct e820entry);
735 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
737 while (data && data->next)
738 data = (struct setup_data *)(unsigned long)data->next;
741 data->next = (unsigned long)e820ext;
743 params->hdr.setup_data = (unsigned long)e820ext;
746 static efi_status_t setup_e820(struct boot_params *params,
747 struct setup_data *e820ext, u32 e820ext_size)
749 struct e820entry *e820_map = ¶ms->e820_map[0];
750 struct efi_info *efi = ¶ms->efi_info;
751 struct e820entry *prev = NULL;
757 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
759 for (i = 0; i < nr_desc; i++) {
760 efi_memory_desc_t *d;
761 unsigned int e820_type = 0;
762 unsigned long m = efi->efi_memmap;
765 m |= (u64)efi->efi_memmap_hi << 32;
768 d = (efi_memory_desc_t *)(m + (i * efi->efi_memdesc_size));
770 case EFI_RESERVED_TYPE:
771 case EFI_RUNTIME_SERVICES_CODE:
772 case EFI_RUNTIME_SERVICES_DATA:
773 case EFI_MEMORY_MAPPED_IO:
774 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
776 e820_type = E820_RESERVED;
779 case EFI_UNUSABLE_MEMORY:
780 e820_type = E820_UNUSABLE;
783 case EFI_ACPI_RECLAIM_MEMORY:
784 e820_type = E820_ACPI;
787 case EFI_LOADER_CODE:
788 case EFI_LOADER_DATA:
789 case EFI_BOOT_SERVICES_CODE:
790 case EFI_BOOT_SERVICES_DATA:
791 case EFI_CONVENTIONAL_MEMORY:
792 e820_type = E820_RAM;
795 case EFI_ACPI_MEMORY_NVS:
796 e820_type = E820_NVS;
799 case EFI_PERSISTENT_MEMORY:
800 e820_type = E820_PMEM;
807 /* Merge adjacent mappings */
808 if (prev && prev->type == e820_type &&
809 (prev->addr + prev->size) == d->phys_addr) {
810 prev->size += d->num_pages << 12;
814 if (nr_entries == ARRAY_SIZE(params->e820_map)) {
815 u32 need = (nr_desc - i) * sizeof(struct e820entry) +
816 sizeof(struct setup_data);
818 if (!e820ext || e820ext_size < need)
819 return EFI_BUFFER_TOO_SMALL;
821 /* boot_params map full, switch to e820 extended */
822 e820_map = (struct e820entry *)e820ext->data;
825 e820_map->addr = d->phys_addr;
826 e820_map->size = d->num_pages << PAGE_SHIFT;
827 e820_map->type = e820_type;
832 if (nr_entries > ARRAY_SIZE(params->e820_map)) {
833 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_map);
835 add_e820ext(params, e820ext, nr_e820ext);
836 nr_entries -= nr_e820ext;
839 params->e820_entries = (u8)nr_entries;
844 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
850 size = sizeof(struct setup_data) +
851 sizeof(struct e820entry) * nr_desc;
854 efi_call_early(free_pool, *e820ext);
859 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
860 size, (void **)e820ext);
861 if (status == EFI_SUCCESS)
862 *e820ext_size = size;
867 struct exit_boot_struct {
868 struct boot_params *boot_params;
869 struct efi_info *efi;
870 struct setup_data *e820ext;
875 static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg,
876 struct efi_boot_memmap *map,
879 static bool first = true;
880 const char *signature;
883 struct exit_boot_struct *p = priv;
886 nr_desc = *map->buff_size / *map->desc_size;
887 if (nr_desc > ARRAY_SIZE(p->boot_params->e820_map)) {
888 u32 nr_e820ext = nr_desc -
889 ARRAY_SIZE(p->boot_params->e820_map);
891 status = alloc_e820ext(nr_e820ext, &p->e820ext,
893 if (status != EFI_SUCCESS)
899 signature = p->is64 ? EFI64_LOADER_SIGNATURE : EFI32_LOADER_SIGNATURE;
900 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
902 p->efi->efi_systab = (unsigned long)sys_table_arg;
903 p->efi->efi_memdesc_size = *map->desc_size;
904 p->efi->efi_memdesc_version = *map->desc_ver;
905 p->efi->efi_memmap = (unsigned long)*map->map;
906 p->efi->efi_memmap_size = *map->map_size;
909 p->efi->efi_systab_hi = (unsigned long)sys_table_arg >> 32;
910 p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32;
916 static efi_status_t exit_boot(struct boot_params *boot_params,
917 void *handle, bool is64)
919 unsigned long map_sz, key, desc_size, buff_size;
920 efi_memory_desc_t *mem_map;
921 struct setup_data *e820ext;
925 struct efi_boot_memmap map;
926 struct exit_boot_struct priv;
929 map.map_size = &map_sz;
930 map.desc_size = &desc_size;
931 map.desc_ver = &desc_version;
933 map.buff_size = &buff_size;
934 priv.boot_params = boot_params;
935 priv.efi = &boot_params->efi_info;
937 priv.e820ext_size = 0;
940 /* Might as well exit boot services now */
941 status = efi_exit_boot_services(sys_table, handle, &map, &priv,
943 if (status != EFI_SUCCESS)
946 e820ext = priv.e820ext;
947 e820ext_size = priv.e820ext_size;
949 boot_params->alt_mem_k = 32 * 1024;
951 status = setup_e820(boot_params, e820ext, e820ext_size);
952 if (status != EFI_SUCCESS)
959 * On success we return a pointer to a boot_params structure, and NULL
962 struct boot_params *efi_main(struct efi_config *c,
963 struct boot_params *boot_params)
965 struct desc_ptr *gdt = NULL;
966 efi_loaded_image_t *image;
967 struct setup_header *hdr = &boot_params->hdr;
969 struct desc_struct *desc;
971 efi_system_table_t *_table;
976 _table = (efi_system_table_t *)(unsigned long)efi_early->table;
977 handle = (void *)(unsigned long)efi_early->image_handle;
978 is64 = efi_early->is64;
982 /* Check if we were booted by the EFI firmware */
983 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
987 setup_boot_services64(efi_early);
989 setup_boot_services32(efi_early);
992 * If the boot loader gave us a value for secure_boot then we use that,
993 * otherwise we ask the BIOS.
995 if (boot_params->secure_boot == efi_secureboot_mode_unset)
996 boot_params->secure_boot = efi_get_secureboot(sys_table);
998 setup_graphics(boot_params);
1000 setup_efi_pci(boot_params);
1002 setup_quirks(boot_params);
1004 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
1005 sizeof(*gdt), (void **)&gdt);
1006 if (status != EFI_SUCCESS) {
1007 efi_printk(sys_table, "Failed to alloc mem for gdt structure\n");
1012 status = efi_low_alloc(sys_table, gdt->size, 8,
1013 (unsigned long *)&gdt->address);
1014 if (status != EFI_SUCCESS) {
1015 efi_printk(sys_table, "Failed to alloc mem for gdt\n");
1020 * If the kernel isn't already loaded at the preferred load
1021 * address, relocate it.
1023 if (hdr->pref_address != hdr->code32_start) {
1024 unsigned long bzimage_addr = hdr->code32_start;
1025 status = efi_relocate_kernel(sys_table, &bzimage_addr,
1026 hdr->init_size, hdr->init_size,
1028 hdr->kernel_alignment);
1029 if (status != EFI_SUCCESS) {
1030 efi_printk(sys_table, "efi_relocate_kernel() failed!\n");
1034 hdr->pref_address = hdr->code32_start;
1035 hdr->code32_start = bzimage_addr;
1038 status = exit_boot(boot_params, handle, is64);
1039 if (status != EFI_SUCCESS) {
1040 efi_printk(sys_table, "exit_boot() failed!\n");
1044 memset((char *)gdt->address, 0x0, gdt->size);
1045 desc = (struct desc_struct *)gdt->address;
1047 /* The first GDT is a dummy and the second is unused. */
1050 desc->limit0 = 0xffff;
1051 desc->base0 = 0x0000;
1052 desc->base1 = 0x0000;
1053 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1054 desc->s = DESC_TYPE_CODE_DATA;
1060 desc->d = SEG_OP_SIZE_32BIT;
1061 desc->g = SEG_GRANULARITY_4KB;
1065 desc->limit0 = 0xffff;
1066 desc->base0 = 0x0000;
1067 desc->base1 = 0x0000;
1068 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
1069 desc->s = DESC_TYPE_CODE_DATA;
1075 desc->d = SEG_OP_SIZE_32BIT;
1076 desc->g = SEG_GRANULARITY_4KB;
1079 #ifdef CONFIG_X86_64
1080 /* Task segment value */
1082 desc->limit0 = 0x0000;
1083 desc->base0 = 0x0000;
1084 desc->base1 = 0x0000;
1085 desc->type = SEG_TYPE_TSS;
1093 desc->g = SEG_GRANULARITY_4KB;
1095 #endif /* CONFIG_X86_64 */
1097 asm volatile("cli");
1098 asm volatile ("lgdt %0" : : "m" (*gdt));
1102 efi_printk(sys_table, "efi_main() failed!\n");