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, (void *)(unsigned long)pci->romimage,
338 efi_call_early(free_pool, rom);
343 setup_efi_pci32(struct boot_params *params, void **pci_handle,
346 efi_pci_io_protocol_32 *pci = NULL;
347 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
348 u32 *handles = (u32 *)(unsigned long)pci_handle;
350 unsigned long nr_pci;
351 struct setup_data *data;
354 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
356 while (data && data->next)
357 data = (struct setup_data *)(unsigned long)data->next;
359 nr_pci = size / sizeof(u32);
360 for (i = 0; i < nr_pci; i++) {
361 struct pci_setup_rom *rom = NULL;
364 status = efi_call_early(handle_protocol, h,
365 &pci_proto, (void **)&pci);
367 if (status != EFI_SUCCESS)
373 status = __setup_efi_pci32(pci, &rom);
374 if (status != EFI_SUCCESS)
378 data->next = (unsigned long)rom;
380 params->hdr.setup_data = (unsigned long)rom;
382 data = (struct setup_data *)rom;
388 __setup_efi_pci64(efi_pci_io_protocol_64 *pci, struct pci_setup_rom **__rom)
390 struct pci_setup_rom *rom;
395 status = efi_early->call(pci->attributes, pci,
396 EfiPciIoAttributeOperationGet, 0,
398 if (status != EFI_SUCCESS)
401 if (!pci->romimage || !pci->romsize)
402 return EFI_INVALID_PARAMETER;
404 size = pci->romsize + sizeof(*rom);
406 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
407 if (status != EFI_SUCCESS) {
408 efi_printk(sys_table, "Failed to alloc mem for rom\n");
412 rom->data.type = SETUP_PCI;
413 rom->data.len = size - sizeof(struct setup_data);
415 rom->pcilen = pci->romsize;
418 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
419 PCI_VENDOR_ID, 1, &(rom->vendor));
421 if (status != EFI_SUCCESS) {
422 efi_printk(sys_table, "Failed to read rom->vendor\n");
426 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
427 PCI_DEVICE_ID, 1, &(rom->devid));
429 if (status != EFI_SUCCESS) {
430 efi_printk(sys_table, "Failed to read rom->devid\n");
434 status = efi_early->call(pci->get_location, pci, &(rom->segment),
435 &(rom->bus), &(rom->device), &(rom->function));
437 if (status != EFI_SUCCESS)
440 memcpy(rom->romdata, (void *)(unsigned long)pci->romimage,
445 efi_call_early(free_pool, rom);
451 setup_efi_pci64(struct boot_params *params, void **pci_handle,
454 efi_pci_io_protocol_64 *pci = NULL;
455 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
456 u64 *handles = (u64 *)(unsigned long)pci_handle;
458 unsigned long nr_pci;
459 struct setup_data *data;
462 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
464 while (data && data->next)
465 data = (struct setup_data *)(unsigned long)data->next;
467 nr_pci = size / sizeof(u64);
468 for (i = 0; i < nr_pci; i++) {
469 struct pci_setup_rom *rom = NULL;
472 status = efi_call_early(handle_protocol, h,
473 &pci_proto, (void **)&pci);
475 if (status != EFI_SUCCESS)
481 status = __setup_efi_pci64(pci, &rom);
482 if (status != EFI_SUCCESS)
486 data->next = (unsigned long)rom;
488 params->hdr.setup_data = (unsigned long)rom;
490 data = (struct setup_data *)rom;
496 * There's no way to return an informative status from this function,
497 * because any analysis (and printing of error messages) needs to be
498 * done directly at the EFI function call-site.
500 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
501 * just didn't find any PCI devices, but there's no way to tell outside
502 * the context of the call.
504 static void setup_efi_pci(struct boot_params *params)
507 void **pci_handle = NULL;
508 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
509 unsigned long size = 0;
511 status = efi_call_early(locate_handle,
512 EFI_LOCATE_BY_PROTOCOL,
513 &pci_proto, NULL, &size, pci_handle);
515 if (status == EFI_BUFFER_TOO_SMALL) {
516 status = efi_call_early(allocate_pool,
518 size, (void **)&pci_handle);
520 if (status != EFI_SUCCESS) {
521 efi_printk(sys_table, "Failed to alloc mem for pci_handle\n");
525 status = efi_call_early(locate_handle,
526 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
527 NULL, &size, pci_handle);
530 if (status != EFI_SUCCESS)
534 setup_efi_pci64(params, pci_handle, size);
536 setup_efi_pci32(params, pci_handle, size);
539 efi_call_early(free_pool, pci_handle);
543 setup_uga32(void **uga_handle, unsigned long size, u32 *width, u32 *height)
545 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
546 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
547 unsigned long nr_ugas;
548 u32 *handles = (u32 *)uga_handle;;
549 efi_status_t status = EFI_INVALID_PARAMETER;
553 nr_ugas = size / sizeof(u32);
554 for (i = 0; i < nr_ugas; i++) {
555 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
556 u32 w, h, depth, refresh;
558 u32 handle = handles[i];
560 status = efi_call_early(handle_protocol, handle,
561 &uga_proto, (void **)&uga);
562 if (status != EFI_SUCCESS)
565 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
567 status = efi_early->call((unsigned long)uga->get_mode, uga,
568 &w, &h, &depth, &refresh);
569 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
574 * Once we've found a UGA supporting PCIIO,
575 * don't bother looking any further.
588 setup_uga64(void **uga_handle, unsigned long size, u32 *width, u32 *height)
590 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
591 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
592 unsigned long nr_ugas;
593 u64 *handles = (u64 *)uga_handle;;
594 efi_status_t status = EFI_INVALID_PARAMETER;
598 nr_ugas = size / sizeof(u64);
599 for (i = 0; i < nr_ugas; i++) {
600 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
601 u32 w, h, depth, refresh;
603 u64 handle = handles[i];
605 status = efi_call_early(handle_protocol, handle,
606 &uga_proto, (void **)&uga);
607 if (status != EFI_SUCCESS)
610 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
612 status = efi_early->call((unsigned long)uga->get_mode, uga,
613 &w, &h, &depth, &refresh);
614 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
619 * Once we've found a UGA supporting PCIIO,
620 * don't bother looking any further.
633 * See if we have Universal Graphics Adapter (UGA) protocol
635 static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
640 void **uga_handle = NULL;
642 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
643 size, (void **)&uga_handle);
644 if (status != EFI_SUCCESS)
647 status = efi_call_early(locate_handle,
648 EFI_LOCATE_BY_PROTOCOL,
649 uga_proto, NULL, &size, uga_handle);
650 if (status != EFI_SUCCESS)
657 status = setup_uga64(uga_handle, size, &width, &height);
659 status = setup_uga32(uga_handle, size, &width, &height);
661 if (!width && !height)
664 /* EFI framebuffer */
665 si->orig_video_isVGA = VIDEO_TYPE_EFI;
668 si->lfb_width = width;
669 si->lfb_height = height;
681 efi_call_early(free_pool, uga_handle);
685 void setup_graphics(struct boot_params *boot_params)
687 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
688 struct screen_info *si;
689 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
692 void **gop_handle = NULL;
693 void **uga_handle = NULL;
695 si = &boot_params->screen_info;
696 memset(si, 0, sizeof(*si));
699 status = efi_call_early(locate_handle,
700 EFI_LOCATE_BY_PROTOCOL,
701 &graphics_proto, NULL, &size, gop_handle);
702 if (status == EFI_BUFFER_TOO_SMALL)
703 status = efi_setup_gop(NULL, si, &graphics_proto, size);
705 if (status != EFI_SUCCESS) {
707 status = efi_call_early(locate_handle,
708 EFI_LOCATE_BY_PROTOCOL,
709 &uga_proto, NULL, &size, uga_handle);
710 if (status == EFI_BUFFER_TOO_SMALL)
711 setup_uga(si, &uga_proto, size);
716 * Because the x86 boot code expects to be passed a boot_params we
717 * need to create one ourselves (usually the bootloader would create
720 * The caller is responsible for filling out ->code32_start in the
721 * returned boot_params.
723 struct boot_params *make_boot_params(struct efi_config *c)
725 struct boot_params *boot_params;
726 struct apm_bios_info *bi;
727 struct setup_header *hdr;
728 efi_loaded_image_t *image;
729 void *options, *handle;
730 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
731 int options_size = 0;
737 unsigned long ramdisk_addr;
738 unsigned long ramdisk_size;
741 sys_table = (efi_system_table_t *)(unsigned long)efi_early->table;
742 handle = (void *)(unsigned long)efi_early->image_handle;
744 /* Check if we were booted by the EFI firmware */
745 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
749 setup_boot_services64(efi_early);
751 setup_boot_services32(efi_early);
753 status = efi_call_early(handle_protocol, handle,
754 &proto, (void *)&image);
755 if (status != EFI_SUCCESS) {
756 efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
760 status = efi_low_alloc(sys_table, 0x4000, 1,
761 (unsigned long *)&boot_params);
762 if (status != EFI_SUCCESS) {
763 efi_printk(sys_table, "Failed to alloc lowmem for boot params\n");
767 memset(boot_params, 0x0, 0x4000);
769 hdr = &boot_params->hdr;
770 bi = &boot_params->apm_bios_info;
772 /* Copy the second sector to boot_params */
773 memcpy(&hdr->jump, image->image_base + 512, 512);
776 * Fill out some of the header fields ourselves because the
777 * EFI firmware loader doesn't load the first sector.
780 hdr->vid_mode = 0xffff;
781 hdr->boot_flag = 0xAA55;
783 hdr->type_of_loader = 0x21;
785 /* Convert unicode cmdline to ascii */
786 cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size);
789 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
790 /* Fill in upper bits of command line address, NOP on 32 bit */
791 boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32;
793 hdr->ramdisk_image = 0;
794 hdr->ramdisk_size = 0;
796 /* Clear APM BIOS info */
797 memset(bi, 0, sizeof(*bi));
799 status = efi_parse_options(cmdline_ptr);
800 if (status != EFI_SUCCESS)
803 status = handle_cmdline_files(sys_table, image,
804 (char *)(unsigned long)hdr->cmd_line_ptr,
805 "initrd=", hdr->initrd_addr_max,
806 &ramdisk_addr, &ramdisk_size);
808 if (status != EFI_SUCCESS &&
809 hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) {
810 efi_printk(sys_table, "Trying to load files to higher address\n");
811 status = handle_cmdline_files(sys_table, image,
812 (char *)(unsigned long)hdr->cmd_line_ptr,
814 &ramdisk_addr, &ramdisk_size);
817 if (status != EFI_SUCCESS)
819 hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
820 hdr->ramdisk_size = ramdisk_size & 0xffffffff;
821 boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
822 boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32;
826 efi_free(sys_table, options_size, hdr->cmd_line_ptr);
828 efi_free(sys_table, 0x4000, (unsigned long)boot_params);
832 static void add_e820ext(struct boot_params *params,
833 struct setup_data *e820ext, u32 nr_entries)
835 struct setup_data *data;
839 e820ext->type = SETUP_E820_EXT;
840 e820ext->len = nr_entries * sizeof(struct e820entry);
843 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
845 while (data && data->next)
846 data = (struct setup_data *)(unsigned long)data->next;
849 data->next = (unsigned long)e820ext;
851 params->hdr.setup_data = (unsigned long)e820ext;
854 static efi_status_t setup_e820(struct boot_params *params,
855 struct setup_data *e820ext, u32 e820ext_size)
857 struct e820entry *e820_map = ¶ms->e820_map[0];
858 struct efi_info *efi = ¶ms->efi_info;
859 struct e820entry *prev = NULL;
865 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
867 for (i = 0; i < nr_desc; i++) {
868 efi_memory_desc_t *d;
869 unsigned int e820_type = 0;
870 unsigned long m = efi->efi_memmap;
873 m |= (u64)efi->efi_memmap_hi << 32;
876 d = (efi_memory_desc_t *)(m + (i * efi->efi_memdesc_size));
878 case EFI_RESERVED_TYPE:
879 case EFI_RUNTIME_SERVICES_CODE:
880 case EFI_RUNTIME_SERVICES_DATA:
881 case EFI_MEMORY_MAPPED_IO:
882 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
884 e820_type = E820_RESERVED;
887 case EFI_UNUSABLE_MEMORY:
888 e820_type = E820_UNUSABLE;
891 case EFI_ACPI_RECLAIM_MEMORY:
892 e820_type = E820_ACPI;
895 case EFI_LOADER_CODE:
896 case EFI_LOADER_DATA:
897 case EFI_BOOT_SERVICES_CODE:
898 case EFI_BOOT_SERVICES_DATA:
899 case EFI_CONVENTIONAL_MEMORY:
900 e820_type = E820_RAM;
903 case EFI_ACPI_MEMORY_NVS:
904 e820_type = E820_NVS;
907 case EFI_PERSISTENT_MEMORY:
908 e820_type = E820_PMEM;
915 /* Merge adjacent mappings */
916 if (prev && prev->type == e820_type &&
917 (prev->addr + prev->size) == d->phys_addr) {
918 prev->size += d->num_pages << 12;
922 if (nr_entries == ARRAY_SIZE(params->e820_map)) {
923 u32 need = (nr_desc - i) * sizeof(struct e820entry) +
924 sizeof(struct setup_data);
926 if (!e820ext || e820ext_size < need)
927 return EFI_BUFFER_TOO_SMALL;
929 /* boot_params map full, switch to e820 extended */
930 e820_map = (struct e820entry *)e820ext->data;
933 e820_map->addr = d->phys_addr;
934 e820_map->size = d->num_pages << PAGE_SHIFT;
935 e820_map->type = e820_type;
940 if (nr_entries > ARRAY_SIZE(params->e820_map)) {
941 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_map);
943 add_e820ext(params, e820ext, nr_e820ext);
944 nr_entries -= nr_e820ext;
947 params->e820_entries = (u8)nr_entries;
952 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
958 size = sizeof(struct setup_data) +
959 sizeof(struct e820entry) * nr_desc;
962 efi_call_early(free_pool, *e820ext);
967 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
968 size, (void **)e820ext);
969 if (status == EFI_SUCCESS)
970 *e820ext_size = size;
975 struct exit_boot_struct {
976 struct boot_params *boot_params;
977 struct efi_info *efi;
978 struct setup_data *e820ext;
983 static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg,
984 struct efi_boot_memmap *map,
987 static bool first = true;
988 const char *signature;
991 struct exit_boot_struct *p = priv;
994 nr_desc = *map->buff_size / *map->desc_size;
995 if (nr_desc > ARRAY_SIZE(p->boot_params->e820_map)) {
996 u32 nr_e820ext = nr_desc -
997 ARRAY_SIZE(p->boot_params->e820_map);
999 status = alloc_e820ext(nr_e820ext, &p->e820ext,
1001 if (status != EFI_SUCCESS)
1007 signature = p->is64 ? EFI64_LOADER_SIGNATURE : EFI32_LOADER_SIGNATURE;
1008 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
1010 p->efi->efi_systab = (unsigned long)sys_table_arg;
1011 p->efi->efi_memdesc_size = *map->desc_size;
1012 p->efi->efi_memdesc_version = *map->desc_ver;
1013 p->efi->efi_memmap = (unsigned long)*map->map;
1014 p->efi->efi_memmap_size = *map->map_size;
1016 #ifdef CONFIG_X86_64
1017 p->efi->efi_systab_hi = (unsigned long)sys_table_arg >> 32;
1018 p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32;
1024 static efi_status_t exit_boot(struct boot_params *boot_params,
1025 void *handle, bool is64)
1027 unsigned long map_sz, key, desc_size, buff_size;
1028 efi_memory_desc_t *mem_map;
1029 struct setup_data *e820ext;
1031 efi_status_t status;
1033 struct efi_boot_memmap map;
1034 struct exit_boot_struct priv;
1037 map.map_size = &map_sz;
1038 map.desc_size = &desc_size;
1039 map.desc_ver = &desc_version;
1041 map.buff_size = &buff_size;
1042 priv.boot_params = boot_params;
1043 priv.efi = &boot_params->efi_info;
1044 priv.e820ext = NULL;
1045 priv.e820ext_size = 0;
1048 /* Might as well exit boot services now */
1049 status = efi_exit_boot_services(sys_table, handle, &map, &priv,
1051 if (status != EFI_SUCCESS)
1054 e820ext = priv.e820ext;
1055 e820ext_size = priv.e820ext_size;
1057 boot_params->alt_mem_k = 32 * 1024;
1059 status = setup_e820(boot_params, e820ext, e820ext_size);
1060 if (status != EFI_SUCCESS)
1067 * On success we return a pointer to a boot_params structure, and NULL
1070 struct boot_params *efi_main(struct efi_config *c,
1071 struct boot_params *boot_params)
1073 struct desc_ptr *gdt = NULL;
1074 efi_loaded_image_t *image;
1075 struct setup_header *hdr = &boot_params->hdr;
1076 efi_status_t status;
1077 struct desc_struct *desc;
1079 efi_system_table_t *_table;
1084 _table = (efi_system_table_t *)(unsigned long)efi_early->table;
1085 handle = (void *)(unsigned long)efi_early->image_handle;
1086 is64 = efi_early->is64;
1090 /* Check if we were booted by the EFI firmware */
1091 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
1095 setup_boot_services64(efi_early);
1097 setup_boot_services32(efi_early);
1099 setup_graphics(boot_params);
1101 setup_efi_pci(boot_params);
1103 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
1104 sizeof(*gdt), (void **)&gdt);
1105 if (status != EFI_SUCCESS) {
1106 efi_printk(sys_table, "Failed to alloc mem for gdt structure\n");
1111 status = efi_low_alloc(sys_table, gdt->size, 8,
1112 (unsigned long *)&gdt->address);
1113 if (status != EFI_SUCCESS) {
1114 efi_printk(sys_table, "Failed to alloc mem for gdt\n");
1119 * If the kernel isn't already loaded at the preferred load
1120 * address, relocate it.
1122 if (hdr->pref_address != hdr->code32_start) {
1123 unsigned long bzimage_addr = hdr->code32_start;
1124 status = efi_relocate_kernel(sys_table, &bzimage_addr,
1125 hdr->init_size, hdr->init_size,
1127 hdr->kernel_alignment);
1128 if (status != EFI_SUCCESS) {
1129 efi_printk(sys_table, "efi_relocate_kernel() failed!\n");
1133 hdr->pref_address = hdr->code32_start;
1134 hdr->code32_start = bzimage_addr;
1137 status = exit_boot(boot_params, handle, is64);
1138 if (status != EFI_SUCCESS) {
1139 efi_printk(sys_table, "exit_boot() failed!\n");
1143 memset((char *)gdt->address, 0x0, gdt->size);
1144 desc = (struct desc_struct *)gdt->address;
1146 /* The first GDT is a dummy and the second is unused. */
1149 desc->limit0 = 0xffff;
1150 desc->base0 = 0x0000;
1151 desc->base1 = 0x0000;
1152 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1153 desc->s = DESC_TYPE_CODE_DATA;
1159 desc->d = SEG_OP_SIZE_32BIT;
1160 desc->g = SEG_GRANULARITY_4KB;
1164 desc->limit0 = 0xffff;
1165 desc->base0 = 0x0000;
1166 desc->base1 = 0x0000;
1167 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
1168 desc->s = DESC_TYPE_CODE_DATA;
1174 desc->d = SEG_OP_SIZE_32BIT;
1175 desc->g = SEG_GRANULARITY_4KB;
1178 #ifdef CONFIG_X86_64
1179 /* Task segment value */
1181 desc->limit0 = 0x0000;
1182 desc->base0 = 0x0000;
1183 desc->base1 = 0x0000;
1184 desc->type = SEG_TYPE_TSS;
1192 desc->g = SEG_GRANULARITY_4KB;
1194 #endif /* CONFIG_X86_64 */
1196 asm volatile("cli");
1197 asm volatile ("lgdt %0" : : "m" (*gdt));
1201 efi_printk(sys_table, "efi_main() failed!\n");