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 void efi_char16_printk(efi_system_table_t *, efi_char16_t *);
44 __file_size32(void *__fh, efi_char16_t *filename_16,
45 void **handle, u64 *file_sz)
47 efi_file_handle_32_t *h, *fh = __fh;
48 efi_file_info_t *info;
50 efi_guid_t info_guid = EFI_FILE_INFO_ID;
53 status = efi_early->call((unsigned long)fh->open, fh, &h, filename_16,
54 EFI_FILE_MODE_READ, (u64)0);
55 if (status != EFI_SUCCESS) {
56 efi_printk(sys_table, "Failed to open file: ");
57 efi_char16_printk(sys_table, filename_16);
58 efi_printk(sys_table, "\n");
65 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
67 if (status != EFI_BUFFER_TOO_SMALL) {
68 efi_printk(sys_table, "Failed to get file info size\n");
73 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
74 info_sz, (void **)&info);
75 if (status != EFI_SUCCESS) {
76 efi_printk(sys_table, "Failed to alloc mem for file info\n");
80 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
82 if (status == EFI_BUFFER_TOO_SMALL) {
83 efi_call_early(free_pool, info);
87 *file_sz = info->file_size;
88 efi_call_early(free_pool, info);
90 if (status != EFI_SUCCESS)
91 efi_printk(sys_table, "Failed to get initrd info\n");
97 __file_size64(void *__fh, efi_char16_t *filename_16,
98 void **handle, u64 *file_sz)
100 efi_file_handle_64_t *h, *fh = __fh;
101 efi_file_info_t *info;
103 efi_guid_t info_guid = EFI_FILE_INFO_ID;
106 status = efi_early->call((unsigned long)fh->open, fh, &h, filename_16,
107 EFI_FILE_MODE_READ, (u64)0);
108 if (status != EFI_SUCCESS) {
109 efi_printk(sys_table, "Failed to open file: ");
110 efi_char16_printk(sys_table, filename_16);
111 efi_printk(sys_table, "\n");
118 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
120 if (status != EFI_BUFFER_TOO_SMALL) {
121 efi_printk(sys_table, "Failed to get file info size\n");
126 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
127 info_sz, (void **)&info);
128 if (status != EFI_SUCCESS) {
129 efi_printk(sys_table, "Failed to alloc mem for file info\n");
133 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
135 if (status == EFI_BUFFER_TOO_SMALL) {
136 efi_call_early(free_pool, info);
140 *file_sz = info->file_size;
141 efi_call_early(free_pool, info);
143 if (status != EFI_SUCCESS)
144 efi_printk(sys_table, "Failed to get initrd info\n");
149 efi_file_size(efi_system_table_t *sys_table, void *__fh,
150 efi_char16_t *filename_16, void **handle, u64 *file_sz)
153 return __file_size64(__fh, filename_16, handle, file_sz);
155 return __file_size32(__fh, filename_16, handle, file_sz);
159 efi_file_read(void *handle, unsigned long *size, void *addr)
163 if (efi_early->is64) {
164 efi_file_handle_64_t *fh = handle;
166 func = (unsigned long)fh->read;
167 return efi_early->call(func, handle, size, addr);
169 efi_file_handle_32_t *fh = handle;
171 func = (unsigned long)fh->read;
172 return efi_early->call(func, handle, size, addr);
176 efi_status_t efi_file_close(void *handle)
178 if (efi_early->is64) {
179 efi_file_handle_64_t *fh = handle;
181 return efi_early->call((unsigned long)fh->close, handle);
183 efi_file_handle_32_t *fh = handle;
185 return efi_early->call((unsigned long)fh->close, handle);
189 static inline efi_status_t __open_volume32(void *__image, void **__fh)
191 efi_file_io_interface_t *io;
192 efi_loaded_image_32_t *image = __image;
193 efi_file_handle_32_t *fh;
194 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
196 void *handle = (void *)(unsigned long)image->device_handle;
199 status = efi_call_early(handle_protocol, handle,
200 &fs_proto, (void **)&io);
201 if (status != EFI_SUCCESS) {
202 efi_printk(sys_table, "Failed to handle fs_proto\n");
206 func = (unsigned long)io->open_volume;
207 status = efi_early->call(func, io, &fh);
208 if (status != EFI_SUCCESS)
209 efi_printk(sys_table, "Failed to open volume\n");
215 static inline efi_status_t __open_volume64(void *__image, void **__fh)
217 efi_file_io_interface_t *io;
218 efi_loaded_image_64_t *image = __image;
219 efi_file_handle_64_t *fh;
220 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
222 void *handle = (void *)(unsigned long)image->device_handle;
225 status = efi_call_early(handle_protocol, handle,
226 &fs_proto, (void **)&io);
227 if (status != EFI_SUCCESS) {
228 efi_printk(sys_table, "Failed to handle fs_proto\n");
232 func = (unsigned long)io->open_volume;
233 status = efi_early->call(func, io, &fh);
234 if (status != EFI_SUCCESS)
235 efi_printk(sys_table, "Failed to open volume\n");
242 efi_open_volume(efi_system_table_t *sys_table, void *__image, void **__fh)
245 return __open_volume64(__image, __fh);
247 return __open_volume32(__image, __fh);
250 void efi_char16_printk(efi_system_table_t *table, efi_char16_t *str)
252 unsigned long output_string;
255 if (efi_early->is64) {
256 struct efi_simple_text_output_protocol_64 *out;
259 offset = offsetof(typeof(*out), output_string);
260 output_string = efi_early->text_output + offset;
261 out = (typeof(out))(unsigned long)efi_early->text_output;
262 func = (u64 *)output_string;
264 efi_early->call(*func, out, str);
266 struct efi_simple_text_output_protocol_32 *out;
269 offset = offsetof(typeof(*out), output_string);
270 output_string = efi_early->text_output + offset;
271 out = (typeof(out))(unsigned long)efi_early->text_output;
272 func = (u32 *)output_string;
274 efi_early->call(*func, out, str);
279 __setup_efi_pci32(efi_pci_io_protocol_32 *pci, struct pci_setup_rom **__rom)
281 struct pci_setup_rom *rom = NULL;
286 status = efi_early->call(pci->attributes, pci,
287 EfiPciIoAttributeOperationGet, 0, 0,
289 if (status != EFI_SUCCESS)
292 if (!pci->romimage || !pci->romsize)
293 return EFI_INVALID_PARAMETER;
295 size = pci->romsize + sizeof(*rom);
297 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
298 if (status != EFI_SUCCESS) {
299 efi_printk(sys_table, "Failed to alloc mem for rom\n");
303 memset(rom, 0, sizeof(*rom));
305 rom->data.type = SETUP_PCI;
306 rom->data.len = size - sizeof(struct setup_data);
308 rom->pcilen = pci->romsize;
311 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
312 PCI_VENDOR_ID, 1, &(rom->vendor));
314 if (status != EFI_SUCCESS) {
315 efi_printk(sys_table, "Failed to read rom->vendor\n");
319 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
320 PCI_DEVICE_ID, 1, &(rom->devid));
322 if (status != EFI_SUCCESS) {
323 efi_printk(sys_table, "Failed to read rom->devid\n");
327 status = efi_early->call(pci->get_location, pci, &(rom->segment),
328 &(rom->bus), &(rom->device), &(rom->function));
330 if (status != EFI_SUCCESS)
333 memcpy(rom->romdata, pci->romimage, pci->romsize);
337 efi_call_early(free_pool, rom);
342 setup_efi_pci32(struct boot_params *params, void **pci_handle,
345 efi_pci_io_protocol_32 *pci = NULL;
346 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
347 u32 *handles = (u32 *)(unsigned long)pci_handle;
349 unsigned long nr_pci;
350 struct setup_data *data;
353 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
355 while (data && data->next)
356 data = (struct setup_data *)(unsigned long)data->next;
358 nr_pci = size / sizeof(u32);
359 for (i = 0; i < nr_pci; i++) {
360 struct pci_setup_rom *rom = NULL;
363 status = efi_call_early(handle_protocol, h,
364 &pci_proto, (void **)&pci);
366 if (status != EFI_SUCCESS)
372 status = __setup_efi_pci32(pci, &rom);
373 if (status != EFI_SUCCESS)
377 data->next = (unsigned long)rom;
379 params->hdr.setup_data = (unsigned long)rom;
381 data = (struct setup_data *)rom;
387 __setup_efi_pci64(efi_pci_io_protocol_64 *pci, struct pci_setup_rom **__rom)
389 struct pci_setup_rom *rom;
394 status = efi_early->call(pci->attributes, pci,
395 EfiPciIoAttributeOperationGet, 0,
397 if (status != EFI_SUCCESS)
400 if (!pci->romimage || !pci->romsize)
401 return EFI_INVALID_PARAMETER;
403 size = pci->romsize + sizeof(*rom);
405 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
406 if (status != EFI_SUCCESS) {
407 efi_printk(sys_table, "Failed to alloc mem for rom\n");
411 rom->data.type = SETUP_PCI;
412 rom->data.len = size - sizeof(struct setup_data);
414 rom->pcilen = pci->romsize;
417 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
418 PCI_VENDOR_ID, 1, &(rom->vendor));
420 if (status != EFI_SUCCESS) {
421 efi_printk(sys_table, "Failed to read rom->vendor\n");
425 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
426 PCI_DEVICE_ID, 1, &(rom->devid));
428 if (status != EFI_SUCCESS) {
429 efi_printk(sys_table, "Failed to read rom->devid\n");
433 status = efi_early->call(pci->get_location, pci, &(rom->segment),
434 &(rom->bus), &(rom->device), &(rom->function));
436 if (status != EFI_SUCCESS)
439 memcpy(rom->romdata, pci->romimage, pci->romsize);
443 efi_call_early(free_pool, rom);
449 setup_efi_pci64(struct boot_params *params, void **pci_handle,
452 efi_pci_io_protocol_64 *pci = NULL;
453 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
454 u64 *handles = (u64 *)(unsigned long)pci_handle;
456 unsigned long nr_pci;
457 struct setup_data *data;
460 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
462 while (data && data->next)
463 data = (struct setup_data *)(unsigned long)data->next;
465 nr_pci = size / sizeof(u64);
466 for (i = 0; i < nr_pci; i++) {
467 struct pci_setup_rom *rom = NULL;
470 status = efi_call_early(handle_protocol, h,
471 &pci_proto, (void **)&pci);
473 if (status != EFI_SUCCESS)
479 status = __setup_efi_pci64(pci, &rom);
480 if (status != EFI_SUCCESS)
484 data->next = (unsigned long)rom;
486 params->hdr.setup_data = (unsigned long)rom;
488 data = (struct setup_data *)rom;
494 * There's no way to return an informative status from this function,
495 * because any analysis (and printing of error messages) needs to be
496 * done directly at the EFI function call-site.
498 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
499 * just didn't find any PCI devices, but there's no way to tell outside
500 * the context of the call.
502 static void setup_efi_pci(struct boot_params *params)
505 void **pci_handle = NULL;
506 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
507 unsigned long size = 0;
509 status = efi_call_early(locate_handle,
510 EFI_LOCATE_BY_PROTOCOL,
511 &pci_proto, NULL, &size, pci_handle);
513 if (status == EFI_BUFFER_TOO_SMALL) {
514 status = efi_call_early(allocate_pool,
516 size, (void **)&pci_handle);
518 if (status != EFI_SUCCESS) {
519 efi_printk(sys_table, "Failed to alloc mem for pci_handle\n");
523 status = efi_call_early(locate_handle,
524 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
525 NULL, &size, pci_handle);
528 if (status != EFI_SUCCESS)
532 setup_efi_pci64(params, pci_handle, size);
534 setup_efi_pci32(params, pci_handle, size);
537 efi_call_early(free_pool, pci_handle);
540 static void retrieve_apple_device_properties(struct boot_params *boot_params)
542 efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
543 struct setup_data *data, *new;
548 status = efi_call_early(locate_protocol, &guid, NULL, &p);
549 if (status != EFI_SUCCESS)
552 if (efi_table_attr(apple_properties_protocol, version, p) != 0x10000) {
553 efi_printk(sys_table, "Unsupported properties proto version\n");
557 efi_call_proto(apple_properties_protocol, get_all, p, NULL, &size);
562 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
563 size + sizeof(struct setup_data), &new);
564 if (status != EFI_SUCCESS) {
565 efi_printk(sys_table,
566 "Failed to alloc mem for properties\n");
570 status = efi_call_proto(apple_properties_protocol, get_all, p,
573 if (status == EFI_BUFFER_TOO_SMALL)
574 efi_call_early(free_pool, new);
575 } while (status == EFI_BUFFER_TOO_SMALL);
577 new->type = SETUP_APPLE_PROPERTIES;
581 data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
583 boot_params->hdr.setup_data = (unsigned long)new;
586 data = (struct setup_data *)(unsigned long)data->next;
587 data->next = (unsigned long)new;
591 static void setup_quirks(struct boot_params *boot_params)
593 efi_char16_t const apple[] = { 'A', 'p', 'p', 'l', 'e', 0 };
594 efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
595 efi_table_attr(efi_system_table, fw_vendor, sys_table);
597 if (!memcmp(fw_vendor, apple, sizeof(apple))) {
598 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
599 retrieve_apple_device_properties(boot_params);
604 setup_uga32(void **uga_handle, unsigned long size, u32 *width, u32 *height)
606 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
607 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
608 unsigned long nr_ugas;
609 u32 *handles = (u32 *)uga_handle;;
610 efi_status_t status = EFI_INVALID_PARAMETER;
614 nr_ugas = size / sizeof(u32);
615 for (i = 0; i < nr_ugas; i++) {
616 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
617 u32 w, h, depth, refresh;
619 u32 handle = handles[i];
621 status = efi_call_early(handle_protocol, handle,
622 &uga_proto, (void **)&uga);
623 if (status != EFI_SUCCESS)
626 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
628 status = efi_early->call((unsigned long)uga->get_mode, uga,
629 &w, &h, &depth, &refresh);
630 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
635 * Once we've found a UGA supporting PCIIO,
636 * don't bother looking any further.
649 setup_uga64(void **uga_handle, unsigned long size, u32 *width, u32 *height)
651 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
652 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
653 unsigned long nr_ugas;
654 u64 *handles = (u64 *)uga_handle;;
655 efi_status_t status = EFI_INVALID_PARAMETER;
659 nr_ugas = size / sizeof(u64);
660 for (i = 0; i < nr_ugas; i++) {
661 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
662 u32 w, h, depth, refresh;
664 u64 handle = handles[i];
666 status = efi_call_early(handle_protocol, handle,
667 &uga_proto, (void **)&uga);
668 if (status != EFI_SUCCESS)
671 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
673 status = efi_early->call((unsigned long)uga->get_mode, uga,
674 &w, &h, &depth, &refresh);
675 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
680 * Once we've found a UGA supporting PCIIO,
681 * don't bother looking any further.
694 * See if we have Universal Graphics Adapter (UGA) protocol
696 static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
701 void **uga_handle = NULL;
703 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
704 size, (void **)&uga_handle);
705 if (status != EFI_SUCCESS)
708 status = efi_call_early(locate_handle,
709 EFI_LOCATE_BY_PROTOCOL,
710 uga_proto, NULL, &size, uga_handle);
711 if (status != EFI_SUCCESS)
718 status = setup_uga64(uga_handle, size, &width, &height);
720 status = setup_uga32(uga_handle, size, &width, &height);
722 if (!width && !height)
725 /* EFI framebuffer */
726 si->orig_video_isVGA = VIDEO_TYPE_EFI;
729 si->lfb_width = width;
730 si->lfb_height = height;
742 efi_call_early(free_pool, uga_handle);
746 void setup_graphics(struct boot_params *boot_params)
748 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
749 struct screen_info *si;
750 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
753 void **gop_handle = NULL;
754 void **uga_handle = NULL;
756 si = &boot_params->screen_info;
757 memset(si, 0, sizeof(*si));
760 status = efi_call_early(locate_handle,
761 EFI_LOCATE_BY_PROTOCOL,
762 &graphics_proto, NULL, &size, gop_handle);
763 if (status == EFI_BUFFER_TOO_SMALL)
764 status = efi_setup_gop(NULL, si, &graphics_proto, size);
766 if (status != EFI_SUCCESS) {
768 status = efi_call_early(locate_handle,
769 EFI_LOCATE_BY_PROTOCOL,
770 &uga_proto, NULL, &size, uga_handle);
771 if (status == EFI_BUFFER_TOO_SMALL)
772 setup_uga(si, &uga_proto, size);
777 * Because the x86 boot code expects to be passed a boot_params we
778 * need to create one ourselves (usually the bootloader would create
781 * The caller is responsible for filling out ->code32_start in the
782 * returned boot_params.
784 struct boot_params *make_boot_params(struct efi_config *c)
786 struct boot_params *boot_params;
787 struct apm_bios_info *bi;
788 struct setup_header *hdr;
789 efi_loaded_image_t *image;
790 void *options, *handle;
791 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
792 int options_size = 0;
798 unsigned long ramdisk_addr;
799 unsigned long ramdisk_size;
802 sys_table = (efi_system_table_t *)(unsigned long)efi_early->table;
803 handle = (void *)(unsigned long)efi_early->image_handle;
805 /* Check if we were booted by the EFI firmware */
806 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
810 setup_boot_services64(efi_early);
812 setup_boot_services32(efi_early);
814 status = efi_call_early(handle_protocol, handle,
815 &proto, (void *)&image);
816 if (status != EFI_SUCCESS) {
817 efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
821 status = efi_low_alloc(sys_table, 0x4000, 1,
822 (unsigned long *)&boot_params);
823 if (status != EFI_SUCCESS) {
824 efi_printk(sys_table, "Failed to alloc lowmem for boot params\n");
828 memset(boot_params, 0x0, 0x4000);
830 hdr = &boot_params->hdr;
831 bi = &boot_params->apm_bios_info;
833 /* Copy the second sector to boot_params */
834 memcpy(&hdr->jump, image->image_base + 512, 512);
837 * Fill out some of the header fields ourselves because the
838 * EFI firmware loader doesn't load the first sector.
841 hdr->vid_mode = 0xffff;
842 hdr->boot_flag = 0xAA55;
844 hdr->type_of_loader = 0x21;
846 /* Convert unicode cmdline to ascii */
847 cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size);
850 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
851 /* Fill in upper bits of command line address, NOP on 32 bit */
852 boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32;
854 hdr->ramdisk_image = 0;
855 hdr->ramdisk_size = 0;
857 /* Clear APM BIOS info */
858 memset(bi, 0, sizeof(*bi));
860 status = efi_parse_options(cmdline_ptr);
861 if (status != EFI_SUCCESS)
864 status = handle_cmdline_files(sys_table, image,
865 (char *)(unsigned long)hdr->cmd_line_ptr,
866 "initrd=", hdr->initrd_addr_max,
867 &ramdisk_addr, &ramdisk_size);
869 if (status != EFI_SUCCESS &&
870 hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) {
871 efi_printk(sys_table, "Trying to load files to higher address\n");
872 status = handle_cmdline_files(sys_table, image,
873 (char *)(unsigned long)hdr->cmd_line_ptr,
875 &ramdisk_addr, &ramdisk_size);
878 if (status != EFI_SUCCESS)
880 hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
881 hdr->ramdisk_size = ramdisk_size & 0xffffffff;
882 boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
883 boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32;
887 efi_free(sys_table, options_size, hdr->cmd_line_ptr);
889 efi_free(sys_table, 0x4000, (unsigned long)boot_params);
893 static void add_e820ext(struct boot_params *params,
894 struct setup_data *e820ext, u32 nr_entries)
896 struct setup_data *data;
900 e820ext->type = SETUP_E820_EXT;
901 e820ext->len = nr_entries * sizeof(struct e820entry);
904 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
906 while (data && data->next)
907 data = (struct setup_data *)(unsigned long)data->next;
910 data->next = (unsigned long)e820ext;
912 params->hdr.setup_data = (unsigned long)e820ext;
915 static efi_status_t setup_e820(struct boot_params *params,
916 struct setup_data *e820ext, u32 e820ext_size)
918 struct e820entry *e820_map = ¶ms->e820_map[0];
919 struct efi_info *efi = ¶ms->efi_info;
920 struct e820entry *prev = NULL;
926 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
928 for (i = 0; i < nr_desc; i++) {
929 efi_memory_desc_t *d;
930 unsigned int e820_type = 0;
931 unsigned long m = efi->efi_memmap;
934 m |= (u64)efi->efi_memmap_hi << 32;
937 d = (efi_memory_desc_t *)(m + (i * efi->efi_memdesc_size));
939 case EFI_RESERVED_TYPE:
940 case EFI_RUNTIME_SERVICES_CODE:
941 case EFI_RUNTIME_SERVICES_DATA:
942 case EFI_MEMORY_MAPPED_IO:
943 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
945 e820_type = E820_RESERVED;
948 case EFI_UNUSABLE_MEMORY:
949 e820_type = E820_UNUSABLE;
952 case EFI_ACPI_RECLAIM_MEMORY:
953 e820_type = E820_ACPI;
956 case EFI_LOADER_CODE:
957 case EFI_LOADER_DATA:
958 case EFI_BOOT_SERVICES_CODE:
959 case EFI_BOOT_SERVICES_DATA:
960 case EFI_CONVENTIONAL_MEMORY:
961 e820_type = E820_RAM;
964 case EFI_ACPI_MEMORY_NVS:
965 e820_type = E820_NVS;
968 case EFI_PERSISTENT_MEMORY:
969 e820_type = E820_PMEM;
976 /* Merge adjacent mappings */
977 if (prev && prev->type == e820_type &&
978 (prev->addr + prev->size) == d->phys_addr) {
979 prev->size += d->num_pages << 12;
983 if (nr_entries == ARRAY_SIZE(params->e820_map)) {
984 u32 need = (nr_desc - i) * sizeof(struct e820entry) +
985 sizeof(struct setup_data);
987 if (!e820ext || e820ext_size < need)
988 return EFI_BUFFER_TOO_SMALL;
990 /* boot_params map full, switch to e820 extended */
991 e820_map = (struct e820entry *)e820ext->data;
994 e820_map->addr = d->phys_addr;
995 e820_map->size = d->num_pages << PAGE_SHIFT;
996 e820_map->type = e820_type;
1001 if (nr_entries > ARRAY_SIZE(params->e820_map)) {
1002 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_map);
1004 add_e820ext(params, e820ext, nr_e820ext);
1005 nr_entries -= nr_e820ext;
1008 params->e820_entries = (u8)nr_entries;
1013 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
1016 efi_status_t status;
1019 size = sizeof(struct setup_data) +
1020 sizeof(struct e820entry) * nr_desc;
1023 efi_call_early(free_pool, *e820ext);
1028 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
1029 size, (void **)e820ext);
1030 if (status == EFI_SUCCESS)
1031 *e820ext_size = size;
1036 struct exit_boot_struct {
1037 struct boot_params *boot_params;
1038 struct efi_info *efi;
1039 struct setup_data *e820ext;
1044 static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg,
1045 struct efi_boot_memmap *map,
1048 static bool first = true;
1049 const char *signature;
1051 efi_status_t status;
1052 struct exit_boot_struct *p = priv;
1055 nr_desc = *map->buff_size / *map->desc_size;
1056 if (nr_desc > ARRAY_SIZE(p->boot_params->e820_map)) {
1057 u32 nr_e820ext = nr_desc -
1058 ARRAY_SIZE(p->boot_params->e820_map);
1060 status = alloc_e820ext(nr_e820ext, &p->e820ext,
1062 if (status != EFI_SUCCESS)
1068 signature = p->is64 ? EFI64_LOADER_SIGNATURE : EFI32_LOADER_SIGNATURE;
1069 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
1071 p->efi->efi_systab = (unsigned long)sys_table_arg;
1072 p->efi->efi_memdesc_size = *map->desc_size;
1073 p->efi->efi_memdesc_version = *map->desc_ver;
1074 p->efi->efi_memmap = (unsigned long)*map->map;
1075 p->efi->efi_memmap_size = *map->map_size;
1077 #ifdef CONFIG_X86_64
1078 p->efi->efi_systab_hi = (unsigned long)sys_table_arg >> 32;
1079 p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32;
1085 static efi_status_t exit_boot(struct boot_params *boot_params,
1086 void *handle, bool is64)
1088 unsigned long map_sz, key, desc_size, buff_size;
1089 efi_memory_desc_t *mem_map;
1090 struct setup_data *e820ext;
1092 efi_status_t status;
1094 struct efi_boot_memmap map;
1095 struct exit_boot_struct priv;
1098 map.map_size = &map_sz;
1099 map.desc_size = &desc_size;
1100 map.desc_ver = &desc_version;
1102 map.buff_size = &buff_size;
1103 priv.boot_params = boot_params;
1104 priv.efi = &boot_params->efi_info;
1105 priv.e820ext = NULL;
1106 priv.e820ext_size = 0;
1109 /* Might as well exit boot services now */
1110 status = efi_exit_boot_services(sys_table, handle, &map, &priv,
1112 if (status != EFI_SUCCESS)
1115 e820ext = priv.e820ext;
1116 e820ext_size = priv.e820ext_size;
1118 boot_params->alt_mem_k = 32 * 1024;
1120 status = setup_e820(boot_params, e820ext, e820ext_size);
1121 if (status != EFI_SUCCESS)
1128 * On success we return a pointer to a boot_params structure, and NULL
1131 struct boot_params *efi_main(struct efi_config *c,
1132 struct boot_params *boot_params)
1134 struct desc_ptr *gdt = NULL;
1135 efi_loaded_image_t *image;
1136 struct setup_header *hdr = &boot_params->hdr;
1137 efi_status_t status;
1138 struct desc_struct *desc;
1140 efi_system_table_t *_table;
1145 _table = (efi_system_table_t *)(unsigned long)efi_early->table;
1146 handle = (void *)(unsigned long)efi_early->image_handle;
1147 is64 = efi_early->is64;
1151 /* Check if we were booted by the EFI firmware */
1152 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
1156 setup_boot_services64(efi_early);
1158 setup_boot_services32(efi_early);
1160 setup_graphics(boot_params);
1162 setup_efi_pci(boot_params);
1164 setup_quirks(boot_params);
1166 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
1167 sizeof(*gdt), (void **)&gdt);
1168 if (status != EFI_SUCCESS) {
1169 efi_printk(sys_table, "Failed to alloc mem for gdt structure\n");
1174 status = efi_low_alloc(sys_table, gdt->size, 8,
1175 (unsigned long *)&gdt->address);
1176 if (status != EFI_SUCCESS) {
1177 efi_printk(sys_table, "Failed to alloc mem for gdt\n");
1182 * If the kernel isn't already loaded at the preferred load
1183 * address, relocate it.
1185 if (hdr->pref_address != hdr->code32_start) {
1186 unsigned long bzimage_addr = hdr->code32_start;
1187 status = efi_relocate_kernel(sys_table, &bzimage_addr,
1188 hdr->init_size, hdr->init_size,
1190 hdr->kernel_alignment);
1191 if (status != EFI_SUCCESS) {
1192 efi_printk(sys_table, "efi_relocate_kernel() failed!\n");
1196 hdr->pref_address = hdr->code32_start;
1197 hdr->code32_start = bzimage_addr;
1200 status = exit_boot(boot_params, handle, is64);
1201 if (status != EFI_SUCCESS) {
1202 efi_printk(sys_table, "exit_boot() failed!\n");
1206 memset((char *)gdt->address, 0x0, gdt->size);
1207 desc = (struct desc_struct *)gdt->address;
1209 /* The first GDT is a dummy and the second is unused. */
1212 desc->limit0 = 0xffff;
1213 desc->base0 = 0x0000;
1214 desc->base1 = 0x0000;
1215 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1216 desc->s = DESC_TYPE_CODE_DATA;
1222 desc->d = SEG_OP_SIZE_32BIT;
1223 desc->g = SEG_GRANULARITY_4KB;
1227 desc->limit0 = 0xffff;
1228 desc->base0 = 0x0000;
1229 desc->base1 = 0x0000;
1230 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
1231 desc->s = DESC_TYPE_CODE_DATA;
1237 desc->d = SEG_OP_SIZE_32BIT;
1238 desc->g = SEG_GRANULARITY_4KB;
1241 #ifdef CONFIG_X86_64
1242 /* Task segment value */
1244 desc->limit0 = 0x0000;
1245 desc->base0 = 0x0000;
1246 desc->base1 = 0x0000;
1247 desc->type = SEG_TYPE_TSS;
1255 desc->g = SEG_GRANULARITY_4KB;
1257 #endif /* CONFIG_X86_64 */
1259 asm volatile("cli");
1260 asm volatile ("lgdt %0" : : "m" (*gdt));
1264 efi_printk(sys_table, "efi_main() failed!\n");