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_printk(char *str)
26 for (s8 = str; *s8; s8++) {
27 struct efi_simple_text_output_protocol *out;
28 efi_char16_t ch[2] = { 0 };
31 out = (struct efi_simple_text_output_protocol *)sys_table->con_out;
34 efi_char16_t nl[2] = { '\r', 0 };
35 efi_call_phys2(out->output_string, out, nl);
38 efi_call_phys2(out->output_string, out, ch);
42 static efi_status_t __get_map(efi_memory_desc_t **map, unsigned long *map_size,
43 unsigned long *desc_size)
45 efi_memory_desc_t *m = NULL;
50 *map_size = sizeof(*m) * 32;
53 * Add an additional efi_memory_desc_t because we're doing an
54 * allocation which may be in a new descriptor region.
56 *map_size += sizeof(*m);
57 status = efi_call_phys3(sys_table->boottime->allocate_pool,
58 EFI_LOADER_DATA, *map_size, (void **)&m);
59 if (status != EFI_SUCCESS)
62 status = efi_call_phys5(sys_table->boottime->get_memory_map, map_size,
63 m, &key, desc_size, &desc_version);
64 if (status == EFI_BUFFER_TOO_SMALL) {
65 efi_call_phys1(sys_table->boottime->free_pool, m);
69 if (status != EFI_SUCCESS)
70 efi_call_phys1(sys_table->boottime->free_pool, m);
78 * Allocate at the highest possible address that is not above 'max'.
80 static efi_status_t high_alloc(unsigned long size, unsigned long align,
81 unsigned long *addr, unsigned long max)
83 unsigned long map_size, desc_size;
84 efi_memory_desc_t *map;
86 unsigned long nr_pages;
90 status = __get_map(&map, &map_size, &desc_size);
91 if (status != EFI_SUCCESS)
94 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
96 for (i = 0; i < map_size / desc_size; i++) {
97 efi_memory_desc_t *desc;
98 unsigned long m = (unsigned long)map;
101 desc = (efi_memory_desc_t *)(m + (i * desc_size));
102 if (desc->type != EFI_CONVENTIONAL_MEMORY)
105 if (desc->num_pages < nr_pages)
108 start = desc->phys_addr;
109 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
111 if ((start + size) > end || (start + size) > max)
114 if (end - size > max)
117 if (round_down(end - size, align) < start)
120 start = round_down(end - size, align);
123 * Don't allocate at 0x0. It will confuse code that
124 * checks pointers against NULL.
129 if (start > max_addr)
134 status = EFI_NOT_FOUND;
136 status = efi_call_phys4(sys_table->boottime->allocate_pages,
137 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
138 nr_pages, &max_addr);
139 if (status != EFI_SUCCESS) {
149 efi_call_phys1(sys_table->boottime->free_pool, map);
156 * Allocate at the lowest possible address.
158 static efi_status_t low_alloc(unsigned long size, unsigned long align,
161 unsigned long map_size, desc_size;
162 efi_memory_desc_t *map;
164 unsigned long nr_pages;
167 status = __get_map(&map, &map_size, &desc_size);
168 if (status != EFI_SUCCESS)
171 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
172 for (i = 0; i < map_size / desc_size; i++) {
173 efi_memory_desc_t *desc;
174 unsigned long m = (unsigned long)map;
177 desc = (efi_memory_desc_t *)(m + (i * desc_size));
179 if (desc->type != EFI_CONVENTIONAL_MEMORY)
182 if (desc->num_pages < nr_pages)
185 start = desc->phys_addr;
186 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
189 * Don't allocate at 0x0. It will confuse code that
190 * checks pointers against NULL. Skip the first 8
191 * bytes so we start at a nice even number.
196 start = round_up(start, align);
197 if ((start + size) > end)
200 status = efi_call_phys4(sys_table->boottime->allocate_pages,
201 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
203 if (status == EFI_SUCCESS) {
209 if (i == map_size / desc_size)
210 status = EFI_NOT_FOUND;
213 efi_call_phys1(sys_table->boottime->free_pool, map);
218 static void low_free(unsigned long size, unsigned long addr)
220 unsigned long nr_pages;
222 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
223 efi_call_phys2(sys_table->boottime->free_pages, addr, size);
226 static void find_bits(unsigned long mask, u8 *pos, u8 *size)
234 while (!(mask & 0x1)) {
249 static efi_status_t setup_efi_pci(struct boot_params *params)
251 efi_pci_io_protocol *pci;
254 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
255 unsigned long nr_pci, size = 0;
257 struct setup_data *data;
259 data = (struct setup_data *)params->hdr.setup_data;
261 while (data && data->next)
262 data = (struct setup_data *)data->next;
264 status = efi_call_phys5(sys_table->boottime->locate_handle,
265 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
266 NULL, &size, pci_handle);
268 if (status == EFI_BUFFER_TOO_SMALL) {
269 status = efi_call_phys3(sys_table->boottime->allocate_pool,
270 EFI_LOADER_DATA, size, &pci_handle);
272 if (status != EFI_SUCCESS)
275 status = efi_call_phys5(sys_table->boottime->locate_handle,
276 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
277 NULL, &size, pci_handle);
280 if (status != EFI_SUCCESS)
283 nr_pci = size / sizeof(void *);
284 for (i = 0; i < nr_pci; i++) {
285 void *h = pci_handle[i];
287 struct pci_setup_rom *rom;
289 status = efi_call_phys3(sys_table->boottime->handle_protocol,
290 h, &pci_proto, &pci);
292 if (status != EFI_SUCCESS)
299 status = efi_call_phys4(pci->attributes, pci,
300 EfiPciIoAttributeOperationGet, 0,
303 status = efi_call_phys5(pci->attributes, pci,
304 EfiPciIoAttributeOperationGet, 0, 0,
307 if (status != EFI_SUCCESS)
310 if (!(attributes & EFI_PCI_IO_ATTRIBUTE_EMBEDDED_ROM))
313 if (!pci->romimage || !pci->romsize)
316 size = pci->romsize + sizeof(*rom);
318 status = efi_call_phys3(sys_table->boottime->allocate_pool,
319 EFI_LOADER_DATA, size, &rom);
321 if (status != EFI_SUCCESS)
324 rom->data.type = SETUP_PCI;
325 rom->data.len = size - sizeof(struct setup_data);
327 rom->pcilen = pci->romsize;
329 status = efi_call_phys5(pci->pci.read, pci,
330 EfiPciIoWidthUint16, PCI_VENDOR_ID,
333 if (status != EFI_SUCCESS)
336 status = efi_call_phys5(pci->pci.read, pci,
337 EfiPciIoWidthUint16, PCI_DEVICE_ID,
340 if (status != EFI_SUCCESS)
343 status = efi_call_phys5(pci->get_location, pci,
344 &(rom->segment), &(rom->bus),
345 &(rom->device), &(rom->function));
347 if (status != EFI_SUCCESS)
350 memcpy(rom->romdata, pci->romimage, pci->romsize);
353 data->next = (uint64_t)rom;
355 params->hdr.setup_data = (uint64_t)rom;
357 data = (struct setup_data *)rom;
361 efi_call_phys1(sys_table->boottime->free_pool, rom);
365 efi_call_phys1(sys_table->boottime->free_pool, pci_handle);
370 * See if we have Graphics Output Protocol
372 static efi_status_t setup_gop(struct screen_info *si, efi_guid_t *proto,
375 struct efi_graphics_output_protocol *gop, *first_gop;
376 struct efi_pixel_bitmask pixel_info;
377 unsigned long nr_gops;
381 u32 fb_base, fb_size;
382 u32 pixels_per_scan_line;
386 status = efi_call_phys3(sys_table->boottime->allocate_pool,
387 EFI_LOADER_DATA, size, &gop_handle);
388 if (status != EFI_SUCCESS)
391 status = efi_call_phys5(sys_table->boottime->locate_handle,
392 EFI_LOCATE_BY_PROTOCOL, proto,
393 NULL, &size, gop_handle);
394 if (status != EFI_SUCCESS)
399 nr_gops = size / sizeof(void *);
400 for (i = 0; i < nr_gops; i++) {
401 struct efi_graphics_output_mode_info *info;
402 efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
403 bool conout_found = false;
405 void *h = gop_handle[i];
407 status = efi_call_phys3(sys_table->boottime->handle_protocol,
409 if (status != EFI_SUCCESS)
412 status = efi_call_phys3(sys_table->boottime->handle_protocol,
413 h, &conout_proto, &dummy);
415 if (status == EFI_SUCCESS)
418 status = efi_call_phys4(gop->query_mode, gop,
419 gop->mode->mode, &size, &info);
420 if (status == EFI_SUCCESS && (!first_gop || conout_found)) {
422 * Systems that use the UEFI Console Splitter may
423 * provide multiple GOP devices, not all of which are
424 * backed by real hardware. The workaround is to search
425 * for a GOP implementing the ConOut protocol, and if
426 * one isn't found, to just fall back to the first GOP.
428 width = info->horizontal_resolution;
429 height = info->vertical_resolution;
430 fb_base = gop->mode->frame_buffer_base;
431 fb_size = gop->mode->frame_buffer_size;
432 pixel_format = info->pixel_format;
433 pixel_info = info->pixel_information;
434 pixels_per_scan_line = info->pixels_per_scan_line;
437 * Once we've found a GOP supporting ConOut,
438 * don't bother looking any further.
446 /* Did we find any GOPs? */
450 /* EFI framebuffer */
451 si->orig_video_isVGA = VIDEO_TYPE_EFI;
453 si->lfb_width = width;
454 si->lfb_height = height;
455 si->lfb_base = fb_base;
458 if (pixel_format == PIXEL_RGB_RESERVED_8BIT_PER_COLOR) {
460 si->lfb_linelength = pixels_per_scan_line * 4;
469 } else if (pixel_format == PIXEL_BGR_RESERVED_8BIT_PER_COLOR) {
471 si->lfb_linelength = pixels_per_scan_line * 4;
480 } else if (pixel_format == PIXEL_BIT_MASK) {
481 find_bits(pixel_info.red_mask, &si->red_pos, &si->red_size);
482 find_bits(pixel_info.green_mask, &si->green_pos,
484 find_bits(pixel_info.blue_mask, &si->blue_pos, &si->blue_size);
485 find_bits(pixel_info.reserved_mask, &si->rsvd_pos,
487 si->lfb_depth = si->red_size + si->green_size +
488 si->blue_size + si->rsvd_size;
489 si->lfb_linelength = (pixels_per_scan_line * si->lfb_depth) / 8;
492 si->lfb_linelength = si->lfb_width / 2;
503 si->lfb_size = si->lfb_linelength * si->lfb_height;
505 si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
508 efi_call_phys1(sys_table->boottime->free_pool, gop_handle);
513 * See if we have Universal Graphics Adapter (UGA) protocol
515 static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
518 struct efi_uga_draw_protocol *uga, *first_uga;
519 unsigned long nr_ugas;
522 void **uga_handle = NULL;
525 status = efi_call_phys3(sys_table->boottime->allocate_pool,
526 EFI_LOADER_DATA, size, &uga_handle);
527 if (status != EFI_SUCCESS)
530 status = efi_call_phys5(sys_table->boottime->locate_handle,
531 EFI_LOCATE_BY_PROTOCOL, uga_proto,
532 NULL, &size, uga_handle);
533 if (status != EFI_SUCCESS)
538 nr_ugas = size / sizeof(void *);
539 for (i = 0; i < nr_ugas; i++) {
540 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
541 void *handle = uga_handle[i];
542 u32 w, h, depth, refresh;
545 status = efi_call_phys3(sys_table->boottime->handle_protocol,
546 handle, uga_proto, &uga);
547 if (status != EFI_SUCCESS)
550 efi_call_phys3(sys_table->boottime->handle_protocol,
551 handle, &pciio_proto, &pciio);
553 status = efi_call_phys5(uga->get_mode, uga, &w, &h,
555 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
560 * Once we've found a UGA supporting PCIIO,
561 * don't bother looking any further.
573 /* EFI framebuffer */
574 si->orig_video_isVGA = VIDEO_TYPE_EFI;
577 si->lfb_width = width;
578 si->lfb_height = height;
591 efi_call_phys1(sys_table->boottime->free_pool, uga_handle);
595 void setup_graphics(struct boot_params *boot_params)
597 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
598 struct screen_info *si;
599 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
602 void **gop_handle = NULL;
603 void **uga_handle = NULL;
605 si = &boot_params->screen_info;
606 memset(si, 0, sizeof(*si));
609 status = efi_call_phys5(sys_table->boottime->locate_handle,
610 EFI_LOCATE_BY_PROTOCOL, &graphics_proto,
611 NULL, &size, gop_handle);
612 if (status == EFI_BUFFER_TOO_SMALL)
613 status = setup_gop(si, &graphics_proto, size);
615 if (status != EFI_SUCCESS) {
617 status = efi_call_phys5(sys_table->boottime->locate_handle,
618 EFI_LOCATE_BY_PROTOCOL, &uga_proto,
619 NULL, &size, uga_handle);
620 if (status == EFI_BUFFER_TOO_SMALL)
621 setup_uga(si, &uga_proto, size);
626 efi_file_handle_t *handle;
631 * Check the cmdline for a LILO-style initrd= arguments.
633 * We only support loading an initrd from the same filesystem as the
636 static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
637 struct setup_header *hdr)
639 struct initrd *initrds;
640 unsigned long initrd_addr;
641 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
643 efi_file_io_interface_t *io;
644 efi_file_handle_t *fh;
653 str = (char *)(unsigned long)hdr->cmd_line_ptr;
655 j = 0; /* See close_handles */
660 for (nr_initrds = 0; *str; nr_initrds++) {
661 str = strstr(str, "initrd=");
667 /* Skip any leading slashes */
668 while (*str == '/' || *str == '\\')
671 while (*str && *str != ' ' && *str != '\n')
678 status = efi_call_phys3(sys_table->boottime->allocate_pool,
680 nr_initrds * sizeof(*initrds),
682 if (status != EFI_SUCCESS) {
683 efi_printk("Failed to alloc mem for initrds\n");
687 str = (char *)(unsigned long)hdr->cmd_line_ptr;
688 for (i = 0; i < nr_initrds; i++) {
689 struct initrd *initrd;
690 efi_file_handle_t *h;
691 efi_file_info_t *info;
692 efi_char16_t filename_16[256];
693 unsigned long info_sz;
694 efi_guid_t info_guid = EFI_FILE_INFO_ID;
698 str = strstr(str, "initrd=");
704 initrd = &initrds[i];
707 /* Skip any leading slashes */
708 while (*str == '/' || *str == '\\')
711 while (*str && *str != ' ' && *str != '\n') {
712 if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
720 /* Only open the volume once. */
722 efi_boot_services_t *boottime;
724 boottime = sys_table->boottime;
726 status = efi_call_phys3(boottime->handle_protocol,
727 image->device_handle, &fs_proto, &io);
728 if (status != EFI_SUCCESS) {
729 efi_printk("Failed to handle fs_proto\n");
733 status = efi_call_phys2(io->open_volume, io, &fh);
734 if (status != EFI_SUCCESS) {
735 efi_printk("Failed to open volume\n");
740 status = efi_call_phys5(fh->open, fh, &h, filename_16,
741 EFI_FILE_MODE_READ, (u64)0);
742 if (status != EFI_SUCCESS) {
743 efi_printk("Failed to open initrd file\n");
750 status = efi_call_phys4(h->get_info, h, &info_guid,
752 if (status != EFI_BUFFER_TOO_SMALL) {
753 efi_printk("Failed to get initrd info size\n");
758 status = efi_call_phys3(sys_table->boottime->allocate_pool,
759 EFI_LOADER_DATA, info_sz, &info);
760 if (status != EFI_SUCCESS) {
761 efi_printk("Failed to alloc mem for initrd info\n");
765 status = efi_call_phys4(h->get_info, h, &info_guid,
767 if (status == EFI_BUFFER_TOO_SMALL) {
768 efi_call_phys1(sys_table->boottime->free_pool, info);
772 file_sz = info->file_size;
773 efi_call_phys1(sys_table->boottime->free_pool, info);
775 if (status != EFI_SUCCESS) {
776 efi_printk("Failed to get initrd info\n");
780 initrd->size = file_sz;
781 initrd_total += file_sz;
788 * Multiple initrd's need to be at consecutive
789 * addresses in memory, so allocate enough memory for
792 status = high_alloc(initrd_total, 0x1000,
793 &initrd_addr, hdr->initrd_addr_max);
794 if (status != EFI_SUCCESS) {
795 efi_printk("Failed to alloc highmem for initrds\n");
799 /* We've run out of free low memory. */
800 if (initrd_addr > hdr->initrd_addr_max) {
801 efi_printk("We've run out of free low memory\n");
802 status = EFI_INVALID_PARAMETER;
803 goto free_initrd_total;
807 for (j = 0; j < nr_initrds; j++) {
810 size = initrds[j].size;
813 if (size > EFI_READ_CHUNK_SIZE)
814 chunksize = EFI_READ_CHUNK_SIZE;
817 status = efi_call_phys3(fh->read,
820 if (status != EFI_SUCCESS) {
821 efi_printk("Failed to read initrd\n");
822 goto free_initrd_total;
828 efi_call_phys1(fh->close, initrds[j].handle);
833 efi_call_phys1(sys_table->boottime->free_pool, initrds);
835 hdr->ramdisk_image = initrd_addr;
836 hdr->ramdisk_size = initrd_total;
841 low_free(initrd_total, initrd_addr);
844 for (k = j; k < i; k++)
845 efi_call_phys1(fh->close, initrds[k].handle);
847 efi_call_phys1(sys_table->boottime->free_pool, initrds);
849 hdr->ramdisk_image = 0;
850 hdr->ramdisk_size = 0;
856 * Because the x86 boot code expects to be passed a boot_params we
857 * need to create one ourselves (usually the bootloader would create
860 struct boot_params *make_boot_params(void *handle, efi_system_table_t *_table)
862 struct boot_params *boot_params;
863 struct sys_desc_table *sdt;
864 struct apm_bios_info *bi;
865 struct setup_header *hdr;
866 struct efi_info *efi;
867 efi_loaded_image_t *image;
869 u32 load_options_size;
870 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
871 int options_size = 0;
873 unsigned long cmdline;
880 /* Check if we were booted by the EFI firmware */
881 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
884 status = efi_call_phys3(sys_table->boottime->handle_protocol,
885 handle, &proto, (void *)&image);
886 if (status != EFI_SUCCESS) {
887 efi_printk("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
891 status = low_alloc(0x4000, 1, (unsigned long *)&boot_params);
892 if (status != EFI_SUCCESS) {
893 efi_printk("Failed to alloc lowmem for boot params\n");
897 memset(boot_params, 0x0, 0x4000);
899 hdr = &boot_params->hdr;
900 efi = &boot_params->efi_info;
901 bi = &boot_params->apm_bios_info;
902 sdt = &boot_params->sys_desc_table;
904 /* Copy the second sector to boot_params */
905 memcpy(&hdr->jump, image->image_base + 512, 512);
908 * Fill out some of the header fields ourselves because the
909 * EFI firmware loader doesn't load the first sector.
912 hdr->vid_mode = 0xffff;
913 hdr->boot_flag = 0xAA55;
915 hdr->code32_start = (__u64)(unsigned long)image->image_base;
917 hdr->type_of_loader = 0x21;
919 /* Convert unicode cmdline to ascii */
920 options = image->load_options;
921 load_options_size = image->load_options_size / 2; /* ASCII */
926 while (*s2 && *s2 != '\n' && options_size < load_options_size) {
932 if (options_size > hdr->cmdline_size)
933 options_size = hdr->cmdline_size;
935 options_size++; /* NUL termination */
937 status = low_alloc(options_size, 1, &cmdline);
938 if (status != EFI_SUCCESS) {
939 efi_printk("Failed to alloc mem for cmdline\n");
943 s1 = (u8 *)(unsigned long)cmdline;
946 for (i = 0; i < options_size - 1; i++)
953 hdr->cmd_line_ptr = cmdline;
955 hdr->ramdisk_image = 0;
956 hdr->ramdisk_size = 0;
958 /* Clear APM BIOS info */
959 memset(bi, 0, sizeof(*bi));
961 memset(sdt, 0, sizeof(*sdt));
963 status = handle_ramdisks(image, hdr);
964 if (status != EFI_SUCCESS)
970 low_free(options_size, hdr->cmd_line_ptr);
972 low_free(0x4000, (unsigned long)boot_params);
976 static efi_status_t exit_boot(struct boot_params *boot_params,
979 struct efi_info *efi = &boot_params->efi_info;
980 struct e820entry *e820_map = &boot_params->e820_map[0];
981 struct e820entry *prev = NULL;
982 unsigned long size, key, desc_size, _size;
983 efi_memory_desc_t *mem_map;
989 size = sizeof(*mem_map) * 32;
992 size += sizeof(*mem_map);
994 status = low_alloc(size, 1, (unsigned long *)&mem_map);
995 if (status != EFI_SUCCESS)
998 status = efi_call_phys5(sys_table->boottime->get_memory_map, &size,
999 mem_map, &key, &desc_size, &desc_version);
1000 if (status == EFI_BUFFER_TOO_SMALL) {
1001 low_free(_size, (unsigned long)mem_map);
1005 if (status != EFI_SUCCESS)
1008 memcpy(&efi->efi_loader_signature, EFI_LOADER_SIGNATURE, sizeof(__u32));
1009 efi->efi_systab = (unsigned long)sys_table;
1010 efi->efi_memdesc_size = desc_size;
1011 efi->efi_memdesc_version = desc_version;
1012 efi->efi_memmap = (unsigned long)mem_map;
1013 efi->efi_memmap_size = size;
1015 #ifdef CONFIG_X86_64
1016 efi->efi_systab_hi = (unsigned long)sys_table >> 32;
1017 efi->efi_memmap_hi = (unsigned long)mem_map >> 32;
1020 /* Might as well exit boot services now */
1021 status = efi_call_phys2(sys_table->boottime->exit_boot_services,
1023 if (status != EFI_SUCCESS)
1027 boot_params->alt_mem_k = 32 * 1024;
1030 * Convert the EFI memory map to E820.
1033 for (i = 0; i < size / desc_size; i++) {
1034 efi_memory_desc_t *d;
1035 unsigned int e820_type = 0;
1036 unsigned long m = (unsigned long)mem_map;
1038 d = (efi_memory_desc_t *)(m + (i * desc_size));
1040 case EFI_RESERVED_TYPE:
1041 case EFI_RUNTIME_SERVICES_CODE:
1042 case EFI_RUNTIME_SERVICES_DATA:
1043 case EFI_MEMORY_MAPPED_IO:
1044 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
1046 e820_type = E820_RESERVED;
1049 case EFI_UNUSABLE_MEMORY:
1050 e820_type = E820_UNUSABLE;
1053 case EFI_ACPI_RECLAIM_MEMORY:
1054 e820_type = E820_ACPI;
1057 case EFI_LOADER_CODE:
1058 case EFI_LOADER_DATA:
1059 case EFI_BOOT_SERVICES_CODE:
1060 case EFI_BOOT_SERVICES_DATA:
1061 case EFI_CONVENTIONAL_MEMORY:
1062 e820_type = E820_RAM;
1065 case EFI_ACPI_MEMORY_NVS:
1066 e820_type = E820_NVS;
1073 /* Merge adjacent mappings */
1074 if (prev && prev->type == e820_type &&
1075 (prev->addr + prev->size) == d->phys_addr)
1076 prev->size += d->num_pages << 12;
1078 e820_map->addr = d->phys_addr;
1079 e820_map->size = d->num_pages << 12;
1080 e820_map->type = e820_type;
1086 boot_params->e820_entries = nr_entries;
1091 low_free(_size, (unsigned long)mem_map);
1095 static efi_status_t relocate_kernel(struct setup_header *hdr)
1097 unsigned long start, nr_pages;
1098 efi_status_t status;
1101 * The EFI firmware loader could have placed the kernel image
1102 * anywhere in memory, but the kernel has various restrictions
1103 * on the max physical address it can run at. Attempt to move
1104 * the kernel to boot_params.pref_address, or as low as
1107 start = hdr->pref_address;
1108 nr_pages = round_up(hdr->init_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
1110 status = efi_call_phys4(sys_table->boottime->allocate_pages,
1111 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
1113 if (status != EFI_SUCCESS) {
1114 status = low_alloc(hdr->init_size, hdr->kernel_alignment,
1116 if (status != EFI_SUCCESS)
1117 efi_printk("Failed to alloc mem for kernel\n");
1120 if (status == EFI_SUCCESS)
1121 memcpy((void *)start, (void *)(unsigned long)hdr->code32_start,
1124 hdr->pref_address = hdr->code32_start;
1125 hdr->code32_start = (__u32)start;
1131 * On success we return a pointer to a boot_params structure, and NULL
1134 struct boot_params *efi_main(void *handle, efi_system_table_t *_table,
1135 struct boot_params *boot_params)
1137 struct desc_ptr *gdt, *idt;
1138 efi_loaded_image_t *image;
1139 struct setup_header *hdr = &boot_params->hdr;
1140 efi_status_t status;
1141 struct desc_struct *desc;
1145 /* Check if we were booted by the EFI firmware */
1146 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
1149 setup_graphics(boot_params);
1151 setup_efi_pci(boot_params);
1153 status = efi_call_phys3(sys_table->boottime->allocate_pool,
1154 EFI_LOADER_DATA, sizeof(*gdt),
1156 if (status != EFI_SUCCESS) {
1157 efi_printk("Failed to alloc mem for gdt structure\n");
1162 status = low_alloc(gdt->size, 8, (unsigned long *)&gdt->address);
1163 if (status != EFI_SUCCESS) {
1164 efi_printk("Failed to alloc mem for gdt\n");
1168 status = efi_call_phys3(sys_table->boottime->allocate_pool,
1169 EFI_LOADER_DATA, sizeof(*idt),
1171 if (status != EFI_SUCCESS) {
1172 efi_printk("Failed to alloc mem for idt structure\n");
1180 * If the kernel isn't already loaded at the preferred load
1181 * address, relocate it.
1183 if (hdr->pref_address != hdr->code32_start) {
1184 status = relocate_kernel(hdr);
1186 if (status != EFI_SUCCESS)
1190 status = exit_boot(boot_params, handle);
1191 if (status != EFI_SUCCESS)
1194 memset((char *)gdt->address, 0x0, gdt->size);
1195 desc = (struct desc_struct *)gdt->address;
1197 /* The first GDT is a dummy and the second is unused. */
1200 desc->limit0 = 0xffff;
1201 desc->base0 = 0x0000;
1202 desc->base1 = 0x0000;
1203 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1204 desc->s = DESC_TYPE_CODE_DATA;
1210 desc->d = SEG_OP_SIZE_32BIT;
1211 desc->g = SEG_GRANULARITY_4KB;
1215 desc->limit0 = 0xffff;
1216 desc->base0 = 0x0000;
1217 desc->base1 = 0x0000;
1218 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
1219 desc->s = DESC_TYPE_CODE_DATA;
1225 desc->d = SEG_OP_SIZE_32BIT;
1226 desc->g = SEG_GRANULARITY_4KB;
1229 #ifdef CONFIG_X86_64
1230 /* Task segment value */
1232 desc->limit0 = 0x0000;
1233 desc->base0 = 0x0000;
1234 desc->base1 = 0x0000;
1235 desc->type = SEG_TYPE_TSS;
1243 desc->g = SEG_GRANULARITY_4KB;
1245 #endif /* CONFIG_X86_64 */
1247 asm volatile ("lidt %0" : : "m" (*idt));
1248 asm volatile ("lgdt %0" : : "m" (*gdt));
1250 asm volatile("cli");