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 #undef memcpy /* Use memcpy from misc.c */
20 static efi_system_table_t *sys_table;
22 static void efi_char16_printk(efi_char16_t *str)
24 struct efi_simple_text_output_protocol *out;
26 out = (struct efi_simple_text_output_protocol *)sys_table->con_out;
27 efi_call_phys2(out->output_string, out, str);
30 static void efi_printk(char *str)
34 for (s8 = str; *s8; s8++) {
35 efi_char16_t ch[2] = { 0 };
39 efi_char16_t nl[2] = { '\r', 0 };
40 efi_char16_printk(nl);
43 efi_char16_printk(ch);
47 static efi_status_t __get_map(efi_memory_desc_t **map, unsigned long *map_size,
48 unsigned long *desc_size)
50 efi_memory_desc_t *m = NULL;
55 *map_size = sizeof(*m) * 32;
58 * Add an additional efi_memory_desc_t because we're doing an
59 * allocation which may be in a new descriptor region.
61 *map_size += sizeof(*m);
62 status = efi_call_phys3(sys_table->boottime->allocate_pool,
63 EFI_LOADER_DATA, *map_size, (void **)&m);
64 if (status != EFI_SUCCESS)
67 status = efi_call_phys5(sys_table->boottime->get_memory_map, map_size,
68 m, &key, desc_size, &desc_version);
69 if (status == EFI_BUFFER_TOO_SMALL) {
70 efi_call_phys1(sys_table->boottime->free_pool, m);
74 if (status != EFI_SUCCESS)
75 efi_call_phys1(sys_table->boottime->free_pool, m);
83 * Allocate at the highest possible address that is not above 'max'.
85 static efi_status_t high_alloc(unsigned long size, unsigned long align,
86 unsigned long *addr, unsigned long max)
88 unsigned long map_size, desc_size;
89 efi_memory_desc_t *map;
91 unsigned long nr_pages;
95 status = __get_map(&map, &map_size, &desc_size);
96 if (status != EFI_SUCCESS)
99 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
101 for (i = 0; i < map_size / desc_size; i++) {
102 efi_memory_desc_t *desc;
103 unsigned long m = (unsigned long)map;
106 desc = (efi_memory_desc_t *)(m + (i * desc_size));
107 if (desc->type != EFI_CONVENTIONAL_MEMORY)
110 if (desc->num_pages < nr_pages)
113 start = desc->phys_addr;
114 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
116 if ((start + size) > end || (start + size) > max)
119 if (end - size > max)
122 if (round_down(end - size, align) < start)
125 start = round_down(end - size, align);
128 * Don't allocate at 0x0. It will confuse code that
129 * checks pointers against NULL.
134 if (start > max_addr)
139 status = EFI_NOT_FOUND;
141 status = efi_call_phys4(sys_table->boottime->allocate_pages,
142 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
143 nr_pages, &max_addr);
144 if (status != EFI_SUCCESS) {
154 efi_call_phys1(sys_table->boottime->free_pool, map);
161 * Allocate at the lowest possible address.
163 static efi_status_t low_alloc(unsigned long size, unsigned long align,
166 unsigned long map_size, desc_size;
167 efi_memory_desc_t *map;
169 unsigned long nr_pages;
172 status = __get_map(&map, &map_size, &desc_size);
173 if (status != EFI_SUCCESS)
176 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
177 for (i = 0; i < map_size / desc_size; i++) {
178 efi_memory_desc_t *desc;
179 unsigned long m = (unsigned long)map;
182 desc = (efi_memory_desc_t *)(m + (i * desc_size));
184 if (desc->type != EFI_CONVENTIONAL_MEMORY)
187 if (desc->num_pages < nr_pages)
190 start = desc->phys_addr;
191 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
194 * Don't allocate at 0x0. It will confuse code that
195 * checks pointers against NULL. Skip the first 8
196 * bytes so we start at a nice even number.
201 start = round_up(start, align);
202 if ((start + size) > end)
205 status = efi_call_phys4(sys_table->boottime->allocate_pages,
206 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
208 if (status == EFI_SUCCESS) {
214 if (i == map_size / desc_size)
215 status = EFI_NOT_FOUND;
218 efi_call_phys1(sys_table->boottime->free_pool, map);
223 static void low_free(unsigned long size, unsigned long addr)
225 unsigned long nr_pages;
227 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
228 efi_call_phys2(sys_table->boottime->free_pages, addr, size);
231 static void find_bits(unsigned long mask, u8 *pos, u8 *size)
239 while (!(mask & 0x1)) {
254 static efi_status_t setup_efi_vars(struct boot_params *params)
256 struct setup_data *data;
257 struct efi_var_bootdata *efidata;
258 u64 store_size, remaining_size, var_size;
261 if (sys_table->runtime->hdr.revision < EFI_2_00_SYSTEM_TABLE_REVISION)
262 return EFI_UNSUPPORTED;
264 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
266 while (data && data->next)
267 data = (struct setup_data *)(unsigned long)data->next;
269 status = efi_call_phys4((void *)sys_table->runtime->query_variable_info,
270 EFI_VARIABLE_NON_VOLATILE |
271 EFI_VARIABLE_BOOTSERVICE_ACCESS |
272 EFI_VARIABLE_RUNTIME_ACCESS, &store_size,
273 &remaining_size, &var_size);
275 if (status != EFI_SUCCESS)
278 status = efi_call_phys3(sys_table->boottime->allocate_pool,
279 EFI_LOADER_DATA, sizeof(*efidata), &efidata);
281 if (status != EFI_SUCCESS)
284 efidata->data.type = SETUP_EFI_VARS;
285 efidata->data.len = sizeof(struct efi_var_bootdata) -
286 sizeof(struct setup_data);
287 efidata->data.next = 0;
288 efidata->store_size = store_size;
289 efidata->remaining_size = remaining_size;
290 efidata->max_var_size = var_size;
293 data->next = (unsigned long)efidata;
295 params->hdr.setup_data = (unsigned long)efidata;
299 static efi_status_t setup_efi_pci(struct boot_params *params)
301 efi_pci_io_protocol *pci;
304 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
305 unsigned long nr_pci, size = 0;
307 struct setup_data *data;
309 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
311 while (data && data->next)
312 data = (struct setup_data *)(unsigned long)data->next;
314 status = efi_call_phys5(sys_table->boottime->locate_handle,
315 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
316 NULL, &size, pci_handle);
318 if (status == EFI_BUFFER_TOO_SMALL) {
319 status = efi_call_phys3(sys_table->boottime->allocate_pool,
320 EFI_LOADER_DATA, size, &pci_handle);
322 if (status != EFI_SUCCESS)
325 status = efi_call_phys5(sys_table->boottime->locate_handle,
326 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
327 NULL, &size, pci_handle);
330 if (status != EFI_SUCCESS)
333 nr_pci = size / sizeof(void *);
334 for (i = 0; i < nr_pci; i++) {
335 void *h = pci_handle[i];
337 struct pci_setup_rom *rom;
339 status = efi_call_phys3(sys_table->boottime->handle_protocol,
340 h, &pci_proto, &pci);
342 if (status != EFI_SUCCESS)
349 status = efi_call_phys4(pci->attributes, pci,
350 EfiPciIoAttributeOperationGet, 0,
353 status = efi_call_phys5(pci->attributes, pci,
354 EfiPciIoAttributeOperationGet, 0, 0,
357 if (status != EFI_SUCCESS)
360 if (!pci->romimage || !pci->romsize)
363 size = pci->romsize + sizeof(*rom);
365 status = efi_call_phys3(sys_table->boottime->allocate_pool,
366 EFI_LOADER_DATA, size, &rom);
368 if (status != EFI_SUCCESS)
371 rom->data.type = SETUP_PCI;
372 rom->data.len = size - sizeof(struct setup_data);
374 rom->pcilen = pci->romsize;
376 status = efi_call_phys5(pci->pci.read, pci,
377 EfiPciIoWidthUint16, PCI_VENDOR_ID,
380 if (status != EFI_SUCCESS)
383 status = efi_call_phys5(pci->pci.read, pci,
384 EfiPciIoWidthUint16, PCI_DEVICE_ID,
387 if (status != EFI_SUCCESS)
390 status = efi_call_phys5(pci->get_location, pci,
391 &(rom->segment), &(rom->bus),
392 &(rom->device), &(rom->function));
394 if (status != EFI_SUCCESS)
397 memcpy(rom->romdata, pci->romimage, pci->romsize);
400 data->next = (unsigned long)rom;
402 params->hdr.setup_data = (unsigned long)rom;
404 data = (struct setup_data *)rom;
408 efi_call_phys1(sys_table->boottime->free_pool, rom);
412 efi_call_phys1(sys_table->boottime->free_pool, pci_handle);
417 * See if we have Graphics Output Protocol
419 static efi_status_t setup_gop(struct screen_info *si, efi_guid_t *proto,
422 struct efi_graphics_output_protocol *gop, *first_gop;
423 struct efi_pixel_bitmask pixel_info;
424 unsigned long nr_gops;
428 u32 fb_base, fb_size;
429 u32 pixels_per_scan_line;
433 status = efi_call_phys3(sys_table->boottime->allocate_pool,
434 EFI_LOADER_DATA, size, &gop_handle);
435 if (status != EFI_SUCCESS)
438 status = efi_call_phys5(sys_table->boottime->locate_handle,
439 EFI_LOCATE_BY_PROTOCOL, proto,
440 NULL, &size, gop_handle);
441 if (status != EFI_SUCCESS)
446 nr_gops = size / sizeof(void *);
447 for (i = 0; i < nr_gops; i++) {
448 struct efi_graphics_output_mode_info *info;
449 efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
450 bool conout_found = false;
452 void *h = gop_handle[i];
454 status = efi_call_phys3(sys_table->boottime->handle_protocol,
456 if (status != EFI_SUCCESS)
459 status = efi_call_phys3(sys_table->boottime->handle_protocol,
460 h, &conout_proto, &dummy);
462 if (status == EFI_SUCCESS)
465 status = efi_call_phys4(gop->query_mode, gop,
466 gop->mode->mode, &size, &info);
467 if (status == EFI_SUCCESS && (!first_gop || conout_found)) {
469 * Systems that use the UEFI Console Splitter may
470 * provide multiple GOP devices, not all of which are
471 * backed by real hardware. The workaround is to search
472 * for a GOP implementing the ConOut protocol, and if
473 * one isn't found, to just fall back to the first GOP.
475 width = info->horizontal_resolution;
476 height = info->vertical_resolution;
477 fb_base = gop->mode->frame_buffer_base;
478 fb_size = gop->mode->frame_buffer_size;
479 pixel_format = info->pixel_format;
480 pixel_info = info->pixel_information;
481 pixels_per_scan_line = info->pixels_per_scan_line;
484 * Once we've found a GOP supporting ConOut,
485 * don't bother looking any further.
493 /* Did we find any GOPs? */
497 /* EFI framebuffer */
498 si->orig_video_isVGA = VIDEO_TYPE_EFI;
500 si->lfb_width = width;
501 si->lfb_height = height;
502 si->lfb_base = fb_base;
505 if (pixel_format == PIXEL_RGB_RESERVED_8BIT_PER_COLOR) {
507 si->lfb_linelength = pixels_per_scan_line * 4;
516 } else if (pixel_format == PIXEL_BGR_RESERVED_8BIT_PER_COLOR) {
518 si->lfb_linelength = pixels_per_scan_line * 4;
527 } else if (pixel_format == PIXEL_BIT_MASK) {
528 find_bits(pixel_info.red_mask, &si->red_pos, &si->red_size);
529 find_bits(pixel_info.green_mask, &si->green_pos,
531 find_bits(pixel_info.blue_mask, &si->blue_pos, &si->blue_size);
532 find_bits(pixel_info.reserved_mask, &si->rsvd_pos,
534 si->lfb_depth = si->red_size + si->green_size +
535 si->blue_size + si->rsvd_size;
536 si->lfb_linelength = (pixels_per_scan_line * si->lfb_depth) / 8;
539 si->lfb_linelength = si->lfb_width / 2;
550 si->lfb_size = si->lfb_linelength * si->lfb_height;
552 si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
555 efi_call_phys1(sys_table->boottime->free_pool, gop_handle);
560 * See if we have Universal Graphics Adapter (UGA) protocol
562 static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
565 struct efi_uga_draw_protocol *uga, *first_uga;
566 unsigned long nr_ugas;
569 void **uga_handle = NULL;
572 status = efi_call_phys3(sys_table->boottime->allocate_pool,
573 EFI_LOADER_DATA, size, &uga_handle);
574 if (status != EFI_SUCCESS)
577 status = efi_call_phys5(sys_table->boottime->locate_handle,
578 EFI_LOCATE_BY_PROTOCOL, uga_proto,
579 NULL, &size, uga_handle);
580 if (status != EFI_SUCCESS)
585 nr_ugas = size / sizeof(void *);
586 for (i = 0; i < nr_ugas; i++) {
587 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
588 void *handle = uga_handle[i];
589 u32 w, h, depth, refresh;
592 status = efi_call_phys3(sys_table->boottime->handle_protocol,
593 handle, uga_proto, &uga);
594 if (status != EFI_SUCCESS)
597 efi_call_phys3(sys_table->boottime->handle_protocol,
598 handle, &pciio_proto, &pciio);
600 status = efi_call_phys5(uga->get_mode, uga, &w, &h,
602 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
607 * Once we've found a UGA supporting PCIIO,
608 * don't bother looking any further.
620 /* EFI framebuffer */
621 si->orig_video_isVGA = VIDEO_TYPE_EFI;
624 si->lfb_width = width;
625 si->lfb_height = height;
638 efi_call_phys1(sys_table->boottime->free_pool, uga_handle);
642 void setup_graphics(struct boot_params *boot_params)
644 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
645 struct screen_info *si;
646 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
649 void **gop_handle = NULL;
650 void **uga_handle = NULL;
652 si = &boot_params->screen_info;
653 memset(si, 0, sizeof(*si));
656 status = efi_call_phys5(sys_table->boottime->locate_handle,
657 EFI_LOCATE_BY_PROTOCOL, &graphics_proto,
658 NULL, &size, gop_handle);
659 if (status == EFI_BUFFER_TOO_SMALL)
660 status = setup_gop(si, &graphics_proto, size);
662 if (status != EFI_SUCCESS) {
664 status = efi_call_phys5(sys_table->boottime->locate_handle,
665 EFI_LOCATE_BY_PROTOCOL, &uga_proto,
666 NULL, &size, uga_handle);
667 if (status == EFI_BUFFER_TOO_SMALL)
668 setup_uga(si, &uga_proto, size);
673 efi_file_handle_t *handle;
678 * Check the cmdline for a LILO-style initrd= arguments.
680 * We only support loading an initrd from the same filesystem as the
683 static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
684 struct setup_header *hdr)
686 struct initrd *initrds;
687 unsigned long initrd_addr;
688 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
690 efi_file_io_interface_t *io;
691 efi_file_handle_t *fh;
700 str = (char *)(unsigned long)hdr->cmd_line_ptr;
702 j = 0; /* See close_handles */
707 for (nr_initrds = 0; *str; nr_initrds++) {
708 str = strstr(str, "initrd=");
714 /* Skip any leading slashes */
715 while (*str == '/' || *str == '\\')
718 while (*str && *str != ' ' && *str != '\n')
725 status = efi_call_phys3(sys_table->boottime->allocate_pool,
727 nr_initrds * sizeof(*initrds),
729 if (status != EFI_SUCCESS) {
730 efi_printk("Failed to alloc mem for initrds\n");
734 str = (char *)(unsigned long)hdr->cmd_line_ptr;
735 for (i = 0; i < nr_initrds; i++) {
736 struct initrd *initrd;
737 efi_file_handle_t *h;
738 efi_file_info_t *info;
739 efi_char16_t filename_16[256];
740 unsigned long info_sz;
741 efi_guid_t info_guid = EFI_FILE_INFO_ID;
745 str = strstr(str, "initrd=");
751 initrd = &initrds[i];
754 /* Skip any leading slashes */
755 while (*str == '/' || *str == '\\')
758 while (*str && *str != ' ' && *str != '\n') {
759 if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
772 /* Only open the volume once. */
774 efi_boot_services_t *boottime;
776 boottime = sys_table->boottime;
778 status = efi_call_phys3(boottime->handle_protocol,
779 image->device_handle, &fs_proto, &io);
780 if (status != EFI_SUCCESS) {
781 efi_printk("Failed to handle fs_proto\n");
785 status = efi_call_phys2(io->open_volume, io, &fh);
786 if (status != EFI_SUCCESS) {
787 efi_printk("Failed to open volume\n");
792 status = efi_call_phys5(fh->open, fh, &h, filename_16,
793 EFI_FILE_MODE_READ, (u64)0);
794 if (status != EFI_SUCCESS) {
795 efi_printk("Failed to open initrd file: ");
796 efi_char16_printk(filename_16);
804 status = efi_call_phys4(h->get_info, h, &info_guid,
806 if (status != EFI_BUFFER_TOO_SMALL) {
807 efi_printk("Failed to get initrd info size\n");
812 status = efi_call_phys3(sys_table->boottime->allocate_pool,
813 EFI_LOADER_DATA, info_sz, &info);
814 if (status != EFI_SUCCESS) {
815 efi_printk("Failed to alloc mem for initrd info\n");
819 status = efi_call_phys4(h->get_info, h, &info_guid,
821 if (status == EFI_BUFFER_TOO_SMALL) {
822 efi_call_phys1(sys_table->boottime->free_pool, info);
826 file_sz = info->file_size;
827 efi_call_phys1(sys_table->boottime->free_pool, info);
829 if (status != EFI_SUCCESS) {
830 efi_printk("Failed to get initrd info\n");
834 initrd->size = file_sz;
835 initrd_total += file_sz;
842 * Multiple initrd's need to be at consecutive
843 * addresses in memory, so allocate enough memory for
846 status = high_alloc(initrd_total, 0x1000,
847 &initrd_addr, hdr->initrd_addr_max);
848 if (status != EFI_SUCCESS) {
849 efi_printk("Failed to alloc highmem for initrds\n");
853 /* We've run out of free low memory. */
854 if (initrd_addr > hdr->initrd_addr_max) {
855 efi_printk("We've run out of free low memory\n");
856 status = EFI_INVALID_PARAMETER;
857 goto free_initrd_total;
861 for (j = 0; j < nr_initrds; j++) {
864 size = initrds[j].size;
867 if (size > EFI_READ_CHUNK_SIZE)
868 chunksize = EFI_READ_CHUNK_SIZE;
871 status = efi_call_phys3(fh->read,
874 if (status != EFI_SUCCESS) {
875 efi_printk("Failed to read initrd\n");
876 goto free_initrd_total;
882 efi_call_phys1(fh->close, initrds[j].handle);
887 efi_call_phys1(sys_table->boottime->free_pool, initrds);
889 hdr->ramdisk_image = initrd_addr;
890 hdr->ramdisk_size = initrd_total;
895 low_free(initrd_total, initrd_addr);
898 for (k = j; k < i; k++)
899 efi_call_phys1(fh->close, initrds[k].handle);
901 efi_call_phys1(sys_table->boottime->free_pool, initrds);
903 hdr->ramdisk_image = 0;
904 hdr->ramdisk_size = 0;
910 * Because the x86 boot code expects to be passed a boot_params we
911 * need to create one ourselves (usually the bootloader would create
914 struct boot_params *make_boot_params(void *handle, efi_system_table_t *_table)
916 struct boot_params *boot_params;
917 struct sys_desc_table *sdt;
918 struct apm_bios_info *bi;
919 struct setup_header *hdr;
920 struct efi_info *efi;
921 efi_loaded_image_t *image;
923 u32 load_options_size;
924 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
925 int options_size = 0;
927 unsigned long cmdline;
934 /* Check if we were booted by the EFI firmware */
935 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
938 status = efi_call_phys3(sys_table->boottime->handle_protocol,
939 handle, &proto, (void *)&image);
940 if (status != EFI_SUCCESS) {
941 efi_printk("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
945 status = low_alloc(0x4000, 1, (unsigned long *)&boot_params);
946 if (status != EFI_SUCCESS) {
947 efi_printk("Failed to alloc lowmem for boot params\n");
951 memset(boot_params, 0x0, 0x4000);
953 hdr = &boot_params->hdr;
954 efi = &boot_params->efi_info;
955 bi = &boot_params->apm_bios_info;
956 sdt = &boot_params->sys_desc_table;
958 /* Copy the second sector to boot_params */
959 memcpy(&hdr->jump, image->image_base + 512, 512);
962 * Fill out some of the header fields ourselves because the
963 * EFI firmware loader doesn't load the first sector.
966 hdr->vid_mode = 0xffff;
967 hdr->boot_flag = 0xAA55;
969 hdr->code32_start = (__u64)(unsigned long)image->image_base;
971 hdr->type_of_loader = 0x21;
973 /* Convert unicode cmdline to ascii */
974 options = image->load_options;
975 load_options_size = image->load_options_size / 2; /* ASCII */
980 while (*s2 && *s2 != '\n' && options_size < load_options_size) {
986 if (options_size > hdr->cmdline_size)
987 options_size = hdr->cmdline_size;
989 options_size++; /* NUL termination */
991 status = low_alloc(options_size, 1, &cmdline);
992 if (status != EFI_SUCCESS) {
993 efi_printk("Failed to alloc mem for cmdline\n");
997 s1 = (u8 *)(unsigned long)cmdline;
1000 for (i = 0; i < options_size - 1; i++)
1007 hdr->cmd_line_ptr = cmdline;
1009 hdr->ramdisk_image = 0;
1010 hdr->ramdisk_size = 0;
1012 /* Clear APM BIOS info */
1013 memset(bi, 0, sizeof(*bi));
1015 memset(sdt, 0, sizeof(*sdt));
1017 status = handle_ramdisks(image, hdr);
1018 if (status != EFI_SUCCESS)
1024 low_free(options_size, hdr->cmd_line_ptr);
1026 low_free(0x4000, (unsigned long)boot_params);
1030 static efi_status_t exit_boot(struct boot_params *boot_params,
1033 struct efi_info *efi = &boot_params->efi_info;
1034 struct e820entry *e820_map = &boot_params->e820_map[0];
1035 struct e820entry *prev = NULL;
1036 unsigned long size, key, desc_size, _size;
1037 efi_memory_desc_t *mem_map;
1038 efi_status_t status;
1043 size = sizeof(*mem_map) * 32;
1046 size += sizeof(*mem_map);
1048 status = low_alloc(size, 1, (unsigned long *)&mem_map);
1049 if (status != EFI_SUCCESS)
1052 status = efi_call_phys5(sys_table->boottime->get_memory_map, &size,
1053 mem_map, &key, &desc_size, &desc_version);
1054 if (status == EFI_BUFFER_TOO_SMALL) {
1055 low_free(_size, (unsigned long)mem_map);
1059 if (status != EFI_SUCCESS)
1062 memcpy(&efi->efi_loader_signature, EFI_LOADER_SIGNATURE, sizeof(__u32));
1063 efi->efi_systab = (unsigned long)sys_table;
1064 efi->efi_memdesc_size = desc_size;
1065 efi->efi_memdesc_version = desc_version;
1066 efi->efi_memmap = (unsigned long)mem_map;
1067 efi->efi_memmap_size = size;
1069 #ifdef CONFIG_X86_64
1070 efi->efi_systab_hi = (unsigned long)sys_table >> 32;
1071 efi->efi_memmap_hi = (unsigned long)mem_map >> 32;
1074 /* Might as well exit boot services now */
1075 status = efi_call_phys2(sys_table->boottime->exit_boot_services,
1077 if (status != EFI_SUCCESS)
1081 boot_params->alt_mem_k = 32 * 1024;
1084 * Convert the EFI memory map to E820.
1087 for (i = 0; i < size / desc_size; i++) {
1088 efi_memory_desc_t *d;
1089 unsigned int e820_type = 0;
1090 unsigned long m = (unsigned long)mem_map;
1092 d = (efi_memory_desc_t *)(m + (i * desc_size));
1094 case EFI_RESERVED_TYPE:
1095 case EFI_RUNTIME_SERVICES_CODE:
1096 case EFI_RUNTIME_SERVICES_DATA:
1097 case EFI_MEMORY_MAPPED_IO:
1098 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
1100 e820_type = E820_RESERVED;
1103 case EFI_UNUSABLE_MEMORY:
1104 e820_type = E820_UNUSABLE;
1107 case EFI_ACPI_RECLAIM_MEMORY:
1108 e820_type = E820_ACPI;
1111 case EFI_LOADER_CODE:
1112 case EFI_LOADER_DATA:
1113 case EFI_BOOT_SERVICES_CODE:
1114 case EFI_BOOT_SERVICES_DATA:
1115 case EFI_CONVENTIONAL_MEMORY:
1116 e820_type = E820_RAM;
1119 case EFI_ACPI_MEMORY_NVS:
1120 e820_type = E820_NVS;
1127 /* Merge adjacent mappings */
1128 if (prev && prev->type == e820_type &&
1129 (prev->addr + prev->size) == d->phys_addr)
1130 prev->size += d->num_pages << 12;
1132 e820_map->addr = d->phys_addr;
1133 e820_map->size = d->num_pages << 12;
1134 e820_map->type = e820_type;
1140 boot_params->e820_entries = nr_entries;
1145 low_free(_size, (unsigned long)mem_map);
1149 static efi_status_t relocate_kernel(struct setup_header *hdr)
1151 unsigned long start, nr_pages;
1152 efi_status_t status;
1155 * The EFI firmware loader could have placed the kernel image
1156 * anywhere in memory, but the kernel has various restrictions
1157 * on the max physical address it can run at. Attempt to move
1158 * the kernel to boot_params.pref_address, or as low as
1161 start = hdr->pref_address;
1162 nr_pages = round_up(hdr->init_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
1164 status = efi_call_phys4(sys_table->boottime->allocate_pages,
1165 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
1167 if (status != EFI_SUCCESS) {
1168 status = low_alloc(hdr->init_size, hdr->kernel_alignment,
1170 if (status != EFI_SUCCESS)
1171 efi_printk("Failed to alloc mem for kernel\n");
1174 if (status == EFI_SUCCESS)
1175 memcpy((void *)start, (void *)(unsigned long)hdr->code32_start,
1178 hdr->pref_address = hdr->code32_start;
1179 hdr->code32_start = (__u32)start;
1185 * On success we return a pointer to a boot_params structure, and NULL
1188 struct boot_params *efi_main(void *handle, efi_system_table_t *_table,
1189 struct boot_params *boot_params)
1191 struct desc_ptr *gdt, *idt;
1192 efi_loaded_image_t *image;
1193 struct setup_header *hdr = &boot_params->hdr;
1194 efi_status_t status;
1195 struct desc_struct *desc;
1199 /* Check if we were booted by the EFI firmware */
1200 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
1203 setup_graphics(boot_params);
1205 setup_efi_vars(boot_params);
1207 setup_efi_pci(boot_params);
1209 status = efi_call_phys3(sys_table->boottime->allocate_pool,
1210 EFI_LOADER_DATA, sizeof(*gdt),
1212 if (status != EFI_SUCCESS) {
1213 efi_printk("Failed to alloc mem for gdt structure\n");
1218 status = low_alloc(gdt->size, 8, (unsigned long *)&gdt->address);
1219 if (status != EFI_SUCCESS) {
1220 efi_printk("Failed to alloc mem for gdt\n");
1224 status = efi_call_phys3(sys_table->boottime->allocate_pool,
1225 EFI_LOADER_DATA, sizeof(*idt),
1227 if (status != EFI_SUCCESS) {
1228 efi_printk("Failed to alloc mem for idt structure\n");
1236 * If the kernel isn't already loaded at the preferred load
1237 * address, relocate it.
1239 if (hdr->pref_address != hdr->code32_start) {
1240 status = relocate_kernel(hdr);
1242 if (status != EFI_SUCCESS)
1246 status = exit_boot(boot_params, handle);
1247 if (status != EFI_SUCCESS)
1250 memset((char *)gdt->address, 0x0, gdt->size);
1251 desc = (struct desc_struct *)gdt->address;
1253 /* The first GDT is a dummy and the second is unused. */
1256 desc->limit0 = 0xffff;
1257 desc->base0 = 0x0000;
1258 desc->base1 = 0x0000;
1259 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1260 desc->s = DESC_TYPE_CODE_DATA;
1266 desc->d = SEG_OP_SIZE_32BIT;
1267 desc->g = SEG_GRANULARITY_4KB;
1271 desc->limit0 = 0xffff;
1272 desc->base0 = 0x0000;
1273 desc->base1 = 0x0000;
1274 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
1275 desc->s = DESC_TYPE_CODE_DATA;
1281 desc->d = SEG_OP_SIZE_32BIT;
1282 desc->g = SEG_GRANULARITY_4KB;
1285 #ifdef CONFIG_X86_64
1286 /* Task segment value */
1288 desc->limit0 = 0x0000;
1289 desc->base0 = 0x0000;
1290 desc->base1 = 0x0000;
1291 desc->type = SEG_TYPE_TSS;
1299 desc->g = SEG_GRANULARITY_4KB;
1301 #endif /* CONFIG_X86_64 */
1303 asm volatile ("lidt %0" : : "m" (*idt));
1304 asm volatile ("lgdt %0" : : "m" (*gdt));
1306 asm volatile("cli");