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->boot_services = table->boottime; \
36 c->text_output = table->con_out; \
41 static inline efi_status_t __open_volume32(void *__image, void **__fh)
43 efi_file_io_interface_t *io;
44 efi_loaded_image_32_t *image = __image;
45 efi_file_handle_32_t *fh;
46 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
48 void *handle = (void *)(unsigned long)image->device_handle;
51 status = efi_call_early(handle_protocol, handle,
52 &fs_proto, (void **)&io);
53 if (status != EFI_SUCCESS) {
54 efi_printk(sys_table, "Failed to handle fs_proto\n");
58 func = (unsigned long)io->open_volume;
59 status = efi_early->call(func, io, &fh);
60 if (status != EFI_SUCCESS)
61 efi_printk(sys_table, "Failed to open volume\n");
67 static inline efi_status_t __open_volume64(void *__image, void **__fh)
69 efi_file_io_interface_t *io;
70 efi_loaded_image_64_t *image = __image;
71 efi_file_handle_64_t *fh;
72 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
74 void *handle = (void *)(unsigned long)image->device_handle;
77 status = efi_call_early(handle_protocol, handle,
78 &fs_proto, (void **)&io);
79 if (status != EFI_SUCCESS) {
80 efi_printk(sys_table, "Failed to handle fs_proto\n");
84 func = (unsigned long)io->open_volume;
85 status = efi_early->call(func, io, &fh);
86 if (status != EFI_SUCCESS)
87 efi_printk(sys_table, "Failed to open volume\n");
94 efi_open_volume(efi_system_table_t *sys_table, void *__image, void **__fh)
97 return __open_volume64(__image, __fh);
99 return __open_volume32(__image, __fh);
102 void efi_char16_printk(efi_system_table_t *table, efi_char16_t *str)
104 unsigned long output_string;
107 if (efi_early->is64) {
108 struct efi_simple_text_output_protocol_64 *out;
111 offset = offsetof(typeof(*out), output_string);
112 output_string = efi_early->text_output + offset;
113 out = (typeof(out))(unsigned long)efi_early->text_output;
114 func = (u64 *)output_string;
116 efi_early->call(*func, out, str);
118 struct efi_simple_text_output_protocol_32 *out;
121 offset = offsetof(typeof(*out), output_string);
122 output_string = efi_early->text_output + offset;
123 out = (typeof(out))(unsigned long)efi_early->text_output;
124 func = (u32 *)output_string;
126 efi_early->call(*func, out, str);
131 __setup_efi_pci32(efi_pci_io_protocol_32 *pci, struct pci_setup_rom **__rom)
133 struct pci_setup_rom *rom = NULL;
138 status = efi_early->call(pci->attributes, pci,
139 EfiPciIoAttributeOperationGet, 0, 0,
141 if (status != EFI_SUCCESS)
144 if (!pci->romimage || !pci->romsize)
145 return EFI_INVALID_PARAMETER;
147 size = pci->romsize + sizeof(*rom);
149 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
150 if (status != EFI_SUCCESS) {
151 efi_printk(sys_table, "Failed to alloc mem for rom\n");
155 memset(rom, 0, sizeof(*rom));
157 rom->data.type = SETUP_PCI;
158 rom->data.len = size - sizeof(struct setup_data);
160 rom->pcilen = pci->romsize;
163 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
164 PCI_VENDOR_ID, 1, &(rom->vendor));
166 if (status != EFI_SUCCESS) {
167 efi_printk(sys_table, "Failed to read rom->vendor\n");
171 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
172 PCI_DEVICE_ID, 1, &(rom->devid));
174 if (status != EFI_SUCCESS) {
175 efi_printk(sys_table, "Failed to read rom->devid\n");
179 status = efi_early->call(pci->get_location, pci, &(rom->segment),
180 &(rom->bus), &(rom->device), &(rom->function));
182 if (status != EFI_SUCCESS)
185 memcpy(rom->romdata, pci->romimage, pci->romsize);
189 efi_call_early(free_pool, rom);
194 setup_efi_pci32(struct boot_params *params, void **pci_handle,
197 efi_pci_io_protocol_32 *pci = NULL;
198 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
199 u32 *handles = (u32 *)(unsigned long)pci_handle;
201 unsigned long nr_pci;
202 struct setup_data *data;
205 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
207 while (data && data->next)
208 data = (struct setup_data *)(unsigned long)data->next;
210 nr_pci = size / sizeof(u32);
211 for (i = 0; i < nr_pci; i++) {
212 struct pci_setup_rom *rom = NULL;
215 status = efi_call_early(handle_protocol, h,
216 &pci_proto, (void **)&pci);
218 if (status != EFI_SUCCESS)
224 status = __setup_efi_pci32(pci, &rom);
225 if (status != EFI_SUCCESS)
229 data->next = (unsigned long)rom;
231 params->hdr.setup_data = (unsigned long)rom;
233 data = (struct setup_data *)rom;
239 __setup_efi_pci64(efi_pci_io_protocol_64 *pci, struct pci_setup_rom **__rom)
241 struct pci_setup_rom *rom;
246 status = efi_early->call(pci->attributes, pci,
247 EfiPciIoAttributeOperationGet, 0,
249 if (status != EFI_SUCCESS)
252 if (!pci->romimage || !pci->romsize)
253 return EFI_INVALID_PARAMETER;
255 size = pci->romsize + sizeof(*rom);
257 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
258 if (status != EFI_SUCCESS) {
259 efi_printk(sys_table, "Failed to alloc mem for rom\n");
263 rom->data.type = SETUP_PCI;
264 rom->data.len = size - sizeof(struct setup_data);
266 rom->pcilen = pci->romsize;
269 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
270 PCI_VENDOR_ID, 1, &(rom->vendor));
272 if (status != EFI_SUCCESS) {
273 efi_printk(sys_table, "Failed to read rom->vendor\n");
277 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
278 PCI_DEVICE_ID, 1, &(rom->devid));
280 if (status != EFI_SUCCESS) {
281 efi_printk(sys_table, "Failed to read rom->devid\n");
285 status = efi_early->call(pci->get_location, pci, &(rom->segment),
286 &(rom->bus), &(rom->device), &(rom->function));
288 if (status != EFI_SUCCESS)
291 memcpy(rom->romdata, pci->romimage, pci->romsize);
295 efi_call_early(free_pool, rom);
301 setup_efi_pci64(struct boot_params *params, void **pci_handle,
304 efi_pci_io_protocol_64 *pci = NULL;
305 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
306 u64 *handles = (u64 *)(unsigned long)pci_handle;
308 unsigned long nr_pci;
309 struct setup_data *data;
312 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
314 while (data && data->next)
315 data = (struct setup_data *)(unsigned long)data->next;
317 nr_pci = size / sizeof(u64);
318 for (i = 0; i < nr_pci; i++) {
319 struct pci_setup_rom *rom = NULL;
322 status = efi_call_early(handle_protocol, h,
323 &pci_proto, (void **)&pci);
325 if (status != EFI_SUCCESS)
331 status = __setup_efi_pci64(pci, &rom);
332 if (status != EFI_SUCCESS)
336 data->next = (unsigned long)rom;
338 params->hdr.setup_data = (unsigned long)rom;
340 data = (struct setup_data *)rom;
346 * There's no way to return an informative status from this function,
347 * because any analysis (and printing of error messages) needs to be
348 * done directly at the EFI function call-site.
350 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
351 * just didn't find any PCI devices, but there's no way to tell outside
352 * the context of the call.
354 static void setup_efi_pci(struct boot_params *params)
357 void **pci_handle = NULL;
358 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
359 unsigned long size = 0;
361 status = efi_call_early(locate_handle,
362 EFI_LOCATE_BY_PROTOCOL,
363 &pci_proto, NULL, &size, pci_handle);
365 if (status == EFI_BUFFER_TOO_SMALL) {
366 status = efi_call_early(allocate_pool,
368 size, (void **)&pci_handle);
370 if (status != EFI_SUCCESS) {
371 efi_printk(sys_table, "Failed to alloc mem for pci_handle\n");
375 status = efi_call_early(locate_handle,
376 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
377 NULL, &size, pci_handle);
380 if (status != EFI_SUCCESS)
384 setup_efi_pci64(params, pci_handle, size);
386 setup_efi_pci32(params, pci_handle, size);
389 efi_call_early(free_pool, pci_handle);
392 static void retrieve_apple_device_properties(struct boot_params *boot_params)
394 efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
395 struct setup_data *data, *new;
400 status = efi_call_early(locate_protocol, &guid, NULL, &p);
401 if (status != EFI_SUCCESS)
404 if (efi_table_attr(apple_properties_protocol, version, p) != 0x10000) {
405 efi_printk(sys_table, "Unsupported properties proto version\n");
409 efi_call_proto(apple_properties_protocol, get_all, p, NULL, &size);
414 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
415 size + sizeof(struct setup_data), &new);
416 if (status != EFI_SUCCESS) {
417 efi_printk(sys_table,
418 "Failed to alloc mem for properties\n");
422 status = efi_call_proto(apple_properties_protocol, get_all, p,
425 if (status == EFI_BUFFER_TOO_SMALL)
426 efi_call_early(free_pool, new);
427 } while (status == EFI_BUFFER_TOO_SMALL);
429 new->type = SETUP_APPLE_PROPERTIES;
433 data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
435 boot_params->hdr.setup_data = (unsigned long)new;
438 data = (struct setup_data *)(unsigned long)data->next;
439 data->next = (unsigned long)new;
443 static void setup_quirks(struct boot_params *boot_params)
445 efi_char16_t const apple[] = { 'A', 'p', 'p', 'l', 'e', 0 };
446 efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
447 efi_table_attr(efi_system_table, fw_vendor, sys_table);
449 if (!memcmp(fw_vendor, apple, sizeof(apple))) {
450 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
451 retrieve_apple_device_properties(boot_params);
456 setup_uga32(void **uga_handle, unsigned long size, u32 *width, u32 *height)
458 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
459 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
460 unsigned long nr_ugas;
461 u32 *handles = (u32 *)uga_handle;;
462 efi_status_t status = EFI_INVALID_PARAMETER;
466 nr_ugas = size / sizeof(u32);
467 for (i = 0; i < nr_ugas; i++) {
468 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
469 u32 w, h, depth, refresh;
471 u32 handle = handles[i];
473 status = efi_call_early(handle_protocol, handle,
474 &uga_proto, (void **)&uga);
475 if (status != EFI_SUCCESS)
478 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
480 status = efi_early->call((unsigned long)uga->get_mode, uga,
481 &w, &h, &depth, &refresh);
482 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
487 * Once we've found a UGA supporting PCIIO,
488 * don't bother looking any further.
501 setup_uga64(void **uga_handle, unsigned long size, u32 *width, u32 *height)
503 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
504 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
505 unsigned long nr_ugas;
506 u64 *handles = (u64 *)uga_handle;;
507 efi_status_t status = EFI_INVALID_PARAMETER;
511 nr_ugas = size / sizeof(u64);
512 for (i = 0; i < nr_ugas; i++) {
513 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
514 u32 w, h, depth, refresh;
516 u64 handle = handles[i];
518 status = efi_call_early(handle_protocol, handle,
519 &uga_proto, (void **)&uga);
520 if (status != EFI_SUCCESS)
523 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
525 status = efi_early->call((unsigned long)uga->get_mode, uga,
526 &w, &h, &depth, &refresh);
527 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
532 * Once we've found a UGA supporting PCIIO,
533 * don't bother looking any further.
546 * See if we have Universal Graphics Adapter (UGA) protocol
548 static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
553 void **uga_handle = NULL;
555 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
556 size, (void **)&uga_handle);
557 if (status != EFI_SUCCESS)
560 status = efi_call_early(locate_handle,
561 EFI_LOCATE_BY_PROTOCOL,
562 uga_proto, NULL, &size, uga_handle);
563 if (status != EFI_SUCCESS)
570 status = setup_uga64(uga_handle, size, &width, &height);
572 status = setup_uga32(uga_handle, size, &width, &height);
574 if (!width && !height)
577 /* EFI framebuffer */
578 si->orig_video_isVGA = VIDEO_TYPE_EFI;
581 si->lfb_width = width;
582 si->lfb_height = height;
594 efi_call_early(free_pool, uga_handle);
598 void setup_graphics(struct boot_params *boot_params)
600 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
601 struct screen_info *si;
602 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
605 void **gop_handle = NULL;
606 void **uga_handle = NULL;
608 si = &boot_params->screen_info;
609 memset(si, 0, sizeof(*si));
612 status = efi_call_early(locate_handle,
613 EFI_LOCATE_BY_PROTOCOL,
614 &graphics_proto, NULL, &size, gop_handle);
615 if (status == EFI_BUFFER_TOO_SMALL)
616 status = efi_setup_gop(NULL, si, &graphics_proto, size);
618 if (status != EFI_SUCCESS) {
620 status = efi_call_early(locate_handle,
621 EFI_LOCATE_BY_PROTOCOL,
622 &uga_proto, NULL, &size, uga_handle);
623 if (status == EFI_BUFFER_TOO_SMALL)
624 setup_uga(si, &uga_proto, size);
629 * Because the x86 boot code expects to be passed a boot_params we
630 * need to create one ourselves (usually the bootloader would create
633 * The caller is responsible for filling out ->code32_start in the
634 * returned boot_params.
636 struct boot_params *make_boot_params(struct efi_config *c)
638 struct boot_params *boot_params;
639 struct apm_bios_info *bi;
640 struct setup_header *hdr;
641 efi_loaded_image_t *image;
642 void *options, *handle;
643 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
644 int options_size = 0;
650 unsigned long ramdisk_addr;
651 unsigned long ramdisk_size;
654 sys_table = (efi_system_table_t *)(unsigned long)efi_early->table;
655 handle = (void *)(unsigned long)efi_early->image_handle;
657 /* Check if we were booted by the EFI firmware */
658 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
662 setup_boot_services64(efi_early);
664 setup_boot_services32(efi_early);
666 status = efi_call_early(handle_protocol, handle,
667 &proto, (void *)&image);
668 if (status != EFI_SUCCESS) {
669 efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
673 status = efi_low_alloc(sys_table, 0x4000, 1,
674 (unsigned long *)&boot_params);
675 if (status != EFI_SUCCESS) {
676 efi_printk(sys_table, "Failed to alloc lowmem for boot params\n");
680 memset(boot_params, 0x0, 0x4000);
682 hdr = &boot_params->hdr;
683 bi = &boot_params->apm_bios_info;
685 /* Copy the second sector to boot_params */
686 memcpy(&hdr->jump, image->image_base + 512, 512);
689 * Fill out some of the header fields ourselves because the
690 * EFI firmware loader doesn't load the first sector.
693 hdr->vid_mode = 0xffff;
694 hdr->boot_flag = 0xAA55;
696 hdr->type_of_loader = 0x21;
698 /* Convert unicode cmdline to ascii */
699 cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size);
702 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
703 /* Fill in upper bits of command line address, NOP on 32 bit */
704 boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32;
706 hdr->ramdisk_image = 0;
707 hdr->ramdisk_size = 0;
709 /* Clear APM BIOS info */
710 memset(bi, 0, sizeof(*bi));
712 status = efi_parse_options(cmdline_ptr);
713 if (status != EFI_SUCCESS)
716 status = handle_cmdline_files(sys_table, image,
717 (char *)(unsigned long)hdr->cmd_line_ptr,
718 "initrd=", hdr->initrd_addr_max,
719 &ramdisk_addr, &ramdisk_size);
721 if (status != EFI_SUCCESS &&
722 hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) {
723 efi_printk(sys_table, "Trying to load files to higher address\n");
724 status = handle_cmdline_files(sys_table, image,
725 (char *)(unsigned long)hdr->cmd_line_ptr,
727 &ramdisk_addr, &ramdisk_size);
730 if (status != EFI_SUCCESS)
732 hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
733 hdr->ramdisk_size = ramdisk_size & 0xffffffff;
734 boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
735 boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32;
739 efi_free(sys_table, options_size, hdr->cmd_line_ptr);
741 efi_free(sys_table, 0x4000, (unsigned long)boot_params);
745 static void add_e820ext(struct boot_params *params,
746 struct setup_data *e820ext, u32 nr_entries)
748 struct setup_data *data;
752 e820ext->type = SETUP_E820_EXT;
753 e820ext->len = nr_entries * sizeof(struct e820entry);
756 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
758 while (data && data->next)
759 data = (struct setup_data *)(unsigned long)data->next;
762 data->next = (unsigned long)e820ext;
764 params->hdr.setup_data = (unsigned long)e820ext;
767 static efi_status_t setup_e820(struct boot_params *params,
768 struct setup_data *e820ext, u32 e820ext_size)
770 struct e820entry *e820_map = ¶ms->e820_map[0];
771 struct efi_info *efi = ¶ms->efi_info;
772 struct e820entry *prev = NULL;
778 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
780 for (i = 0; i < nr_desc; i++) {
781 efi_memory_desc_t *d;
782 unsigned int e820_type = 0;
783 unsigned long m = efi->efi_memmap;
786 m |= (u64)efi->efi_memmap_hi << 32;
789 d = (efi_memory_desc_t *)(m + (i * efi->efi_memdesc_size));
791 case EFI_RESERVED_TYPE:
792 case EFI_RUNTIME_SERVICES_CODE:
793 case EFI_RUNTIME_SERVICES_DATA:
794 case EFI_MEMORY_MAPPED_IO:
795 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
797 e820_type = E820_RESERVED;
800 case EFI_UNUSABLE_MEMORY:
801 e820_type = E820_UNUSABLE;
804 case EFI_ACPI_RECLAIM_MEMORY:
805 e820_type = E820_ACPI;
808 case EFI_LOADER_CODE:
809 case EFI_LOADER_DATA:
810 case EFI_BOOT_SERVICES_CODE:
811 case EFI_BOOT_SERVICES_DATA:
812 case EFI_CONVENTIONAL_MEMORY:
813 e820_type = E820_RAM;
816 case EFI_ACPI_MEMORY_NVS:
817 e820_type = E820_NVS;
820 case EFI_PERSISTENT_MEMORY:
821 e820_type = E820_PMEM;
828 /* Merge adjacent mappings */
829 if (prev && prev->type == e820_type &&
830 (prev->addr + prev->size) == d->phys_addr) {
831 prev->size += d->num_pages << 12;
835 if (nr_entries == ARRAY_SIZE(params->e820_map)) {
836 u32 need = (nr_desc - i) * sizeof(struct e820entry) +
837 sizeof(struct setup_data);
839 if (!e820ext || e820ext_size < need)
840 return EFI_BUFFER_TOO_SMALL;
842 /* boot_params map full, switch to e820 extended */
843 e820_map = (struct e820entry *)e820ext->data;
846 e820_map->addr = d->phys_addr;
847 e820_map->size = d->num_pages << PAGE_SHIFT;
848 e820_map->type = e820_type;
853 if (nr_entries > ARRAY_SIZE(params->e820_map)) {
854 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_map);
856 add_e820ext(params, e820ext, nr_e820ext);
857 nr_entries -= nr_e820ext;
860 params->e820_entries = (u8)nr_entries;
865 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
871 size = sizeof(struct setup_data) +
872 sizeof(struct e820entry) * nr_desc;
875 efi_call_early(free_pool, *e820ext);
880 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
881 size, (void **)e820ext);
882 if (status == EFI_SUCCESS)
883 *e820ext_size = size;
888 struct exit_boot_struct {
889 struct boot_params *boot_params;
890 struct efi_info *efi;
891 struct setup_data *e820ext;
896 static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg,
897 struct efi_boot_memmap *map,
900 static bool first = true;
901 const char *signature;
904 struct exit_boot_struct *p = priv;
907 nr_desc = *map->buff_size / *map->desc_size;
908 if (nr_desc > ARRAY_SIZE(p->boot_params->e820_map)) {
909 u32 nr_e820ext = nr_desc -
910 ARRAY_SIZE(p->boot_params->e820_map);
912 status = alloc_e820ext(nr_e820ext, &p->e820ext,
914 if (status != EFI_SUCCESS)
920 signature = p->is64 ? EFI64_LOADER_SIGNATURE : EFI32_LOADER_SIGNATURE;
921 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
923 p->efi->efi_systab = (unsigned long)sys_table_arg;
924 p->efi->efi_memdesc_size = *map->desc_size;
925 p->efi->efi_memdesc_version = *map->desc_ver;
926 p->efi->efi_memmap = (unsigned long)*map->map;
927 p->efi->efi_memmap_size = *map->map_size;
930 p->efi->efi_systab_hi = (unsigned long)sys_table_arg >> 32;
931 p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32;
937 static efi_status_t exit_boot(struct boot_params *boot_params,
938 void *handle, bool is64)
940 unsigned long map_sz, key, desc_size, buff_size;
941 efi_memory_desc_t *mem_map;
942 struct setup_data *e820ext;
946 struct efi_boot_memmap map;
947 struct exit_boot_struct priv;
950 map.map_size = &map_sz;
951 map.desc_size = &desc_size;
952 map.desc_ver = &desc_version;
954 map.buff_size = &buff_size;
955 priv.boot_params = boot_params;
956 priv.efi = &boot_params->efi_info;
958 priv.e820ext_size = 0;
961 /* Might as well exit boot services now */
962 status = efi_exit_boot_services(sys_table, handle, &map, &priv,
964 if (status != EFI_SUCCESS)
967 e820ext = priv.e820ext;
968 e820ext_size = priv.e820ext_size;
970 boot_params->alt_mem_k = 32 * 1024;
972 status = setup_e820(boot_params, e820ext, e820ext_size);
973 if (status != EFI_SUCCESS)
980 * On success we return a pointer to a boot_params structure, and NULL
983 struct boot_params *efi_main(struct efi_config *c,
984 struct boot_params *boot_params)
986 struct desc_ptr *gdt = NULL;
987 efi_loaded_image_t *image;
988 struct setup_header *hdr = &boot_params->hdr;
990 struct desc_struct *desc;
992 efi_system_table_t *_table;
997 _table = (efi_system_table_t *)(unsigned long)efi_early->table;
998 handle = (void *)(unsigned long)efi_early->image_handle;
999 is64 = efi_early->is64;
1003 /* Check if we were booted by the EFI firmware */
1004 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
1008 setup_boot_services64(efi_early);
1010 setup_boot_services32(efi_early);
1012 setup_graphics(boot_params);
1014 setup_efi_pci(boot_params);
1016 setup_quirks(boot_params);
1018 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
1019 sizeof(*gdt), (void **)&gdt);
1020 if (status != EFI_SUCCESS) {
1021 efi_printk(sys_table, "Failed to alloc mem for gdt structure\n");
1026 status = efi_low_alloc(sys_table, gdt->size, 8,
1027 (unsigned long *)&gdt->address);
1028 if (status != EFI_SUCCESS) {
1029 efi_printk(sys_table, "Failed to alloc mem for gdt\n");
1034 * If the kernel isn't already loaded at the preferred load
1035 * address, relocate it.
1037 if (hdr->pref_address != hdr->code32_start) {
1038 unsigned long bzimage_addr = hdr->code32_start;
1039 status = efi_relocate_kernel(sys_table, &bzimage_addr,
1040 hdr->init_size, hdr->init_size,
1042 hdr->kernel_alignment);
1043 if (status != EFI_SUCCESS) {
1044 efi_printk(sys_table, "efi_relocate_kernel() failed!\n");
1048 hdr->pref_address = hdr->code32_start;
1049 hdr->code32_start = bzimage_addr;
1052 status = exit_boot(boot_params, handle, is64);
1053 if (status != EFI_SUCCESS) {
1054 efi_printk(sys_table, "exit_boot() failed!\n");
1058 memset((char *)gdt->address, 0x0, gdt->size);
1059 desc = (struct desc_struct *)gdt->address;
1061 /* The first GDT is a dummy and the second is unused. */
1064 desc->limit0 = 0xffff;
1065 desc->base0 = 0x0000;
1066 desc->base1 = 0x0000;
1067 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1068 desc->s = DESC_TYPE_CODE_DATA;
1074 desc->d = SEG_OP_SIZE_32BIT;
1075 desc->g = SEG_GRANULARITY_4KB;
1079 desc->limit0 = 0xffff;
1080 desc->base0 = 0x0000;
1081 desc->base1 = 0x0000;
1082 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
1083 desc->s = DESC_TYPE_CODE_DATA;
1089 desc->d = SEG_OP_SIZE_32BIT;
1090 desc->g = SEG_GRANULARITY_4KB;
1093 #ifdef CONFIG_X86_64
1094 /* Task segment value */
1096 desc->limit0 = 0x0000;
1097 desc->base0 = 0x0000;
1098 desc->base1 = 0x0000;
1099 desc->type = SEG_TYPE_TSS;
1107 desc->g = SEG_GRANULARITY_4KB;
1109 #endif /* CONFIG_X86_64 */
1111 asm volatile("cli");
1112 asm volatile ("lgdt %0" : : "m" (*gdt));
1116 efi_printk(sys_table, "efi_main() failed!\n");