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_pci(struct boot_params *params)
256 efi_pci_io_protocol *pci;
259 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
260 unsigned long nr_pci, size = 0;
262 struct setup_data *data;
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_phys5(sys_table->boottime->locate_handle,
270 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
271 NULL, &size, pci_handle);
273 if (status == EFI_BUFFER_TOO_SMALL) {
274 status = efi_call_phys3(sys_table->boottime->allocate_pool,
275 EFI_LOADER_DATA, size, &pci_handle);
277 if (status != EFI_SUCCESS)
280 status = efi_call_phys5(sys_table->boottime->locate_handle,
281 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
282 NULL, &size, pci_handle);
285 if (status != EFI_SUCCESS)
288 nr_pci = size / sizeof(void *);
289 for (i = 0; i < nr_pci; i++) {
290 void *h = pci_handle[i];
292 struct pci_setup_rom *rom;
294 status = efi_call_phys3(sys_table->boottime->handle_protocol,
295 h, &pci_proto, &pci);
297 if (status != EFI_SUCCESS)
304 status = efi_call_phys4(pci->attributes, pci,
305 EfiPciIoAttributeOperationGet, 0,
308 status = efi_call_phys5(pci->attributes, pci,
309 EfiPciIoAttributeOperationGet, 0, 0,
312 if (status != EFI_SUCCESS)
315 if (!pci->romimage || !pci->romsize)
318 size = pci->romsize + sizeof(*rom);
320 status = efi_call_phys3(sys_table->boottime->allocate_pool,
321 EFI_LOADER_DATA, size, &rom);
323 if (status != EFI_SUCCESS)
326 rom->data.type = SETUP_PCI;
327 rom->data.len = size - sizeof(struct setup_data);
329 rom->pcilen = pci->romsize;
331 status = efi_call_phys5(pci->pci.read, pci,
332 EfiPciIoWidthUint16, PCI_VENDOR_ID,
335 if (status != EFI_SUCCESS)
338 status = efi_call_phys5(pci->pci.read, pci,
339 EfiPciIoWidthUint16, PCI_DEVICE_ID,
342 if (status != EFI_SUCCESS)
345 status = efi_call_phys5(pci->get_location, pci,
346 &(rom->segment), &(rom->bus),
347 &(rom->device), &(rom->function));
349 if (status != EFI_SUCCESS)
352 memcpy(rom->romdata, pci->romimage, pci->romsize);
355 data->next = (unsigned long)rom;
357 params->hdr.setup_data = (unsigned long)rom;
359 data = (struct setup_data *)rom;
363 efi_call_phys1(sys_table->boottime->free_pool, rom);
367 efi_call_phys1(sys_table->boottime->free_pool, pci_handle);
372 * See if we have Graphics Output Protocol
374 static efi_status_t setup_gop(struct screen_info *si, efi_guid_t *proto,
377 struct efi_graphics_output_protocol *gop, *first_gop;
378 struct efi_pixel_bitmask pixel_info;
379 unsigned long nr_gops;
383 u32 fb_base, fb_size;
384 u32 pixels_per_scan_line;
388 status = efi_call_phys3(sys_table->boottime->allocate_pool,
389 EFI_LOADER_DATA, size, &gop_handle);
390 if (status != EFI_SUCCESS)
393 status = efi_call_phys5(sys_table->boottime->locate_handle,
394 EFI_LOCATE_BY_PROTOCOL, proto,
395 NULL, &size, gop_handle);
396 if (status != EFI_SUCCESS)
401 nr_gops = size / sizeof(void *);
402 for (i = 0; i < nr_gops; i++) {
403 struct efi_graphics_output_mode_info *info;
404 efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
405 bool conout_found = false;
407 void *h = gop_handle[i];
409 status = efi_call_phys3(sys_table->boottime->handle_protocol,
411 if (status != EFI_SUCCESS)
414 status = efi_call_phys3(sys_table->boottime->handle_protocol,
415 h, &conout_proto, &dummy);
417 if (status == EFI_SUCCESS)
420 status = efi_call_phys4(gop->query_mode, gop,
421 gop->mode->mode, &size, &info);
422 if (status == EFI_SUCCESS && (!first_gop || conout_found)) {
424 * Systems that use the UEFI Console Splitter may
425 * provide multiple GOP devices, not all of which are
426 * backed by real hardware. The workaround is to search
427 * for a GOP implementing the ConOut protocol, and if
428 * one isn't found, to just fall back to the first GOP.
430 width = info->horizontal_resolution;
431 height = info->vertical_resolution;
432 fb_base = gop->mode->frame_buffer_base;
433 fb_size = gop->mode->frame_buffer_size;
434 pixel_format = info->pixel_format;
435 pixel_info = info->pixel_information;
436 pixels_per_scan_line = info->pixels_per_scan_line;
439 * Once we've found a GOP supporting ConOut,
440 * don't bother looking any further.
448 /* Did we find any GOPs? */
452 /* EFI framebuffer */
453 si->orig_video_isVGA = VIDEO_TYPE_EFI;
455 si->lfb_width = width;
456 si->lfb_height = height;
457 si->lfb_base = fb_base;
460 if (pixel_format == PIXEL_RGB_RESERVED_8BIT_PER_COLOR) {
462 si->lfb_linelength = pixels_per_scan_line * 4;
471 } else if (pixel_format == PIXEL_BGR_RESERVED_8BIT_PER_COLOR) {
473 si->lfb_linelength = pixels_per_scan_line * 4;
482 } else if (pixel_format == PIXEL_BIT_MASK) {
483 find_bits(pixel_info.red_mask, &si->red_pos, &si->red_size);
484 find_bits(pixel_info.green_mask, &si->green_pos,
486 find_bits(pixel_info.blue_mask, &si->blue_pos, &si->blue_size);
487 find_bits(pixel_info.reserved_mask, &si->rsvd_pos,
489 si->lfb_depth = si->red_size + si->green_size +
490 si->blue_size + si->rsvd_size;
491 si->lfb_linelength = (pixels_per_scan_line * si->lfb_depth) / 8;
494 si->lfb_linelength = si->lfb_width / 2;
505 si->lfb_size = si->lfb_linelength * si->lfb_height;
507 si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
510 efi_call_phys1(sys_table->boottime->free_pool, gop_handle);
515 * See if we have Universal Graphics Adapter (UGA) protocol
517 static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
520 struct efi_uga_draw_protocol *uga, *first_uga;
521 unsigned long nr_ugas;
524 void **uga_handle = NULL;
527 status = efi_call_phys3(sys_table->boottime->allocate_pool,
528 EFI_LOADER_DATA, size, &uga_handle);
529 if (status != EFI_SUCCESS)
532 status = efi_call_phys5(sys_table->boottime->locate_handle,
533 EFI_LOCATE_BY_PROTOCOL, uga_proto,
534 NULL, &size, uga_handle);
535 if (status != EFI_SUCCESS)
540 nr_ugas = size / sizeof(void *);
541 for (i = 0; i < nr_ugas; i++) {
542 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
543 void *handle = uga_handle[i];
544 u32 w, h, depth, refresh;
547 status = efi_call_phys3(sys_table->boottime->handle_protocol,
548 handle, uga_proto, &uga);
549 if (status != EFI_SUCCESS)
552 efi_call_phys3(sys_table->boottime->handle_protocol,
553 handle, &pciio_proto, &pciio);
555 status = efi_call_phys5(uga->get_mode, uga, &w, &h,
557 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
562 * Once we've found a UGA supporting PCIIO,
563 * don't bother looking any further.
575 /* EFI framebuffer */
576 si->orig_video_isVGA = VIDEO_TYPE_EFI;
579 si->lfb_width = width;
580 si->lfb_height = height;
593 efi_call_phys1(sys_table->boottime->free_pool, uga_handle);
597 void setup_graphics(struct boot_params *boot_params)
599 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
600 struct screen_info *si;
601 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
604 void **gop_handle = NULL;
605 void **uga_handle = NULL;
607 si = &boot_params->screen_info;
608 memset(si, 0, sizeof(*si));
611 status = efi_call_phys5(sys_table->boottime->locate_handle,
612 EFI_LOCATE_BY_PROTOCOL, &graphics_proto,
613 NULL, &size, gop_handle);
614 if (status == EFI_BUFFER_TOO_SMALL)
615 status = setup_gop(si, &graphics_proto, size);
617 if (status != EFI_SUCCESS) {
619 status = efi_call_phys5(sys_table->boottime->locate_handle,
620 EFI_LOCATE_BY_PROTOCOL, &uga_proto,
621 NULL, &size, uga_handle);
622 if (status == EFI_BUFFER_TOO_SMALL)
623 setup_uga(si, &uga_proto, size);
628 efi_file_handle_t *handle;
633 * Check the cmdline for a LILO-style initrd= arguments.
635 * We only support loading an initrd from the same filesystem as the
638 static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
639 struct setup_header *hdr)
641 struct initrd *initrds;
642 unsigned long initrd_addr;
643 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
645 efi_file_io_interface_t *io;
646 efi_file_handle_t *fh;
655 str = (char *)(unsigned long)hdr->cmd_line_ptr;
657 j = 0; /* See close_handles */
662 for (nr_initrds = 0; *str; nr_initrds++) {
663 str = strstr(str, "initrd=");
669 /* Skip any leading slashes */
670 while (*str == '/' || *str == '\\')
673 while (*str && *str != ' ' && *str != '\n')
680 status = efi_call_phys3(sys_table->boottime->allocate_pool,
682 nr_initrds * sizeof(*initrds),
684 if (status != EFI_SUCCESS) {
685 efi_printk("Failed to alloc mem for initrds\n");
689 str = (char *)(unsigned long)hdr->cmd_line_ptr;
690 for (i = 0; i < nr_initrds; i++) {
691 struct initrd *initrd;
692 efi_file_handle_t *h;
693 efi_file_info_t *info;
694 efi_char16_t filename_16[256];
695 unsigned long info_sz;
696 efi_guid_t info_guid = EFI_FILE_INFO_ID;
700 str = strstr(str, "initrd=");
706 initrd = &initrds[i];
709 /* Skip any leading slashes */
710 while (*str == '/' || *str == '\\')
713 while (*str && *str != ' ' && *str != '\n') {
714 if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
727 /* Only open the volume once. */
729 efi_boot_services_t *boottime;
731 boottime = sys_table->boottime;
733 status = efi_call_phys3(boottime->handle_protocol,
734 image->device_handle, &fs_proto, &io);
735 if (status != EFI_SUCCESS) {
736 efi_printk("Failed to handle fs_proto\n");
740 status = efi_call_phys2(io->open_volume, io, &fh);
741 if (status != EFI_SUCCESS) {
742 efi_printk("Failed to open volume\n");
747 status = efi_call_phys5(fh->open, fh, &h, filename_16,
748 EFI_FILE_MODE_READ, (u64)0);
749 if (status != EFI_SUCCESS) {
750 efi_printk("Failed to open initrd file: ");
751 efi_char16_printk(filename_16);
759 status = efi_call_phys4(h->get_info, h, &info_guid,
761 if (status != EFI_BUFFER_TOO_SMALL) {
762 efi_printk("Failed to get initrd info size\n");
767 status = efi_call_phys3(sys_table->boottime->allocate_pool,
768 EFI_LOADER_DATA, info_sz, &info);
769 if (status != EFI_SUCCESS) {
770 efi_printk("Failed to alloc mem for initrd info\n");
774 status = efi_call_phys4(h->get_info, h, &info_guid,
776 if (status == EFI_BUFFER_TOO_SMALL) {
777 efi_call_phys1(sys_table->boottime->free_pool, info);
781 file_sz = info->file_size;
782 efi_call_phys1(sys_table->boottime->free_pool, info);
784 if (status != EFI_SUCCESS) {
785 efi_printk("Failed to get initrd info\n");
789 initrd->size = file_sz;
790 initrd_total += file_sz;
797 * Multiple initrd's need to be at consecutive
798 * addresses in memory, so allocate enough memory for
801 status = high_alloc(initrd_total, 0x1000,
802 &initrd_addr, hdr->initrd_addr_max);
803 if (status != EFI_SUCCESS) {
804 efi_printk("Failed to alloc highmem for initrds\n");
808 /* We've run out of free low memory. */
809 if (initrd_addr > hdr->initrd_addr_max) {
810 efi_printk("We've run out of free low memory\n");
811 status = EFI_INVALID_PARAMETER;
812 goto free_initrd_total;
816 for (j = 0; j < nr_initrds; j++) {
819 size = initrds[j].size;
822 if (size > EFI_READ_CHUNK_SIZE)
823 chunksize = EFI_READ_CHUNK_SIZE;
826 status = efi_call_phys3(fh->read,
829 if (status != EFI_SUCCESS) {
830 efi_printk("Failed to read initrd\n");
831 goto free_initrd_total;
837 efi_call_phys1(fh->close, initrds[j].handle);
842 efi_call_phys1(sys_table->boottime->free_pool, initrds);
844 hdr->ramdisk_image = initrd_addr;
845 hdr->ramdisk_size = initrd_total;
850 low_free(initrd_total, initrd_addr);
853 for (k = j; k < i; k++)
854 efi_call_phys1(fh->close, initrds[k].handle);
856 efi_call_phys1(sys_table->boottime->free_pool, initrds);
858 hdr->ramdisk_image = 0;
859 hdr->ramdisk_size = 0;
865 * Because the x86 boot code expects to be passed a boot_params we
866 * need to create one ourselves (usually the bootloader would create
869 struct boot_params *make_boot_params(void *handle, efi_system_table_t *_table)
871 struct boot_params *boot_params;
872 struct sys_desc_table *sdt;
873 struct apm_bios_info *bi;
874 struct setup_header *hdr;
875 struct efi_info *efi;
876 efi_loaded_image_t *image;
878 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
879 int options_size = 0;
885 unsigned long ramdisk_addr;
886 unsigned long ramdisk_size;
890 /* Check if we were booted by the EFI firmware */
891 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
894 status = efi_call_phys3(sys_table->boottime->handle_protocol,
895 handle, &proto, (void *)&image);
896 if (status != EFI_SUCCESS) {
897 efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
901 status = efi_low_alloc(sys_table, 0x4000, 1,
902 (unsigned long *)&boot_params);
903 if (status != EFI_SUCCESS) {
904 efi_printk(sys_table, "Failed to alloc lowmem for boot params\n");
908 memset(boot_params, 0x0, 0x4000);
910 hdr = &boot_params->hdr;
911 efi = &boot_params->efi_info;
912 bi = &boot_params->apm_bios_info;
913 sdt = &boot_params->sys_desc_table;
915 /* Copy the second sector to boot_params */
916 memcpy(&hdr->jump, image->image_base + 512, 512);
919 * Fill out some of the header fields ourselves because the
920 * EFI firmware loader doesn't load the first sector.
923 hdr->vid_mode = 0xffff;
924 hdr->boot_flag = 0xAA55;
926 hdr->code32_start = (__u64)(unsigned long)image->image_base;
928 hdr->type_of_loader = 0x21;
930 /* Convert unicode cmdline to ascii */
931 cmdline_ptr = efi_convert_cmdline_to_ascii(sys_table, image,
935 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
937 hdr->ramdisk_image = 0;
938 hdr->ramdisk_size = 0;
940 /* Clear APM BIOS info */
941 memset(bi, 0, sizeof(*bi));
943 memset(sdt, 0, sizeof(*sdt));
945 status = handle_cmdline_files(sys_table, image,
946 (char *)(unsigned long)hdr->cmd_line_ptr,
947 "initrd=", hdr->initrd_addr_max,
948 &ramdisk_addr, &ramdisk_size);
949 if (status != EFI_SUCCESS)
951 hdr->ramdisk_image = ramdisk_addr;
952 hdr->ramdisk_size = ramdisk_size;
956 efi_free(sys_table, options_size, hdr->cmd_line_ptr);
958 efi_free(sys_table, 0x4000, (unsigned long)boot_params);
962 static efi_status_t exit_boot(struct boot_params *boot_params,
965 struct efi_info *efi = &boot_params->efi_info;
966 struct e820entry *e820_map = &boot_params->e820_map[0];
967 struct e820entry *prev = NULL;
968 unsigned long size, key, desc_size, _size;
969 efi_memory_desc_t *mem_map;
975 size = sizeof(*mem_map) * 32;
978 size += sizeof(*mem_map);
980 status = efi_low_alloc(sys_table, size, 1, (unsigned long *)&mem_map);
981 if (status != EFI_SUCCESS)
984 status = efi_call_phys5(sys_table->boottime->get_memory_map, &size,
985 mem_map, &key, &desc_size, &desc_version);
986 if (status == EFI_BUFFER_TOO_SMALL) {
987 efi_free(sys_table, _size, (unsigned long)mem_map);
991 if (status != EFI_SUCCESS)
994 memcpy(&efi->efi_loader_signature, EFI_LOADER_SIGNATURE, sizeof(__u32));
995 efi->efi_systab = (unsigned long)sys_table;
996 efi->efi_memdesc_size = desc_size;
997 efi->efi_memdesc_version = desc_version;
998 efi->efi_memmap = (unsigned long)mem_map;
999 efi->efi_memmap_size = size;
1001 #ifdef CONFIG_X86_64
1002 efi->efi_systab_hi = (unsigned long)sys_table >> 32;
1003 efi->efi_memmap_hi = (unsigned long)mem_map >> 32;
1006 /* Might as well exit boot services now */
1007 status = efi_call_phys2(sys_table->boottime->exit_boot_services,
1009 if (status != EFI_SUCCESS)
1013 boot_params->alt_mem_k = 32 * 1024;
1016 * Convert the EFI memory map to E820.
1019 for (i = 0; i < size / desc_size; i++) {
1020 efi_memory_desc_t *d;
1021 unsigned int e820_type = 0;
1022 unsigned long m = (unsigned long)mem_map;
1024 d = (efi_memory_desc_t *)(m + (i * desc_size));
1026 case EFI_RESERVED_TYPE:
1027 case EFI_RUNTIME_SERVICES_CODE:
1028 case EFI_RUNTIME_SERVICES_DATA:
1029 case EFI_MEMORY_MAPPED_IO:
1030 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
1032 e820_type = E820_RESERVED;
1035 case EFI_UNUSABLE_MEMORY:
1036 e820_type = E820_UNUSABLE;
1039 case EFI_ACPI_RECLAIM_MEMORY:
1040 e820_type = E820_ACPI;
1043 case EFI_LOADER_CODE:
1044 case EFI_LOADER_DATA:
1045 case EFI_BOOT_SERVICES_CODE:
1046 case EFI_BOOT_SERVICES_DATA:
1047 case EFI_CONVENTIONAL_MEMORY:
1048 e820_type = E820_RAM;
1051 case EFI_ACPI_MEMORY_NVS:
1052 e820_type = E820_NVS;
1059 /* Merge adjacent mappings */
1060 if (prev && prev->type == e820_type &&
1061 (prev->addr + prev->size) == d->phys_addr)
1062 prev->size += d->num_pages << 12;
1064 e820_map->addr = d->phys_addr;
1065 e820_map->size = d->num_pages << 12;
1066 e820_map->type = e820_type;
1072 boot_params->e820_entries = nr_entries;
1077 efi_free(sys_table, _size, (unsigned long)mem_map);
1083 * On success we return a pointer to a boot_params structure, and NULL
1086 struct boot_params *efi_main(void *handle, efi_system_table_t *_table,
1087 struct boot_params *boot_params)
1089 struct desc_ptr *gdt, *idt;
1090 efi_loaded_image_t *image;
1091 struct setup_header *hdr = &boot_params->hdr;
1092 efi_status_t status;
1093 struct desc_struct *desc;
1097 /* Check if we were booted by the EFI firmware */
1098 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
1101 setup_graphics(boot_params);
1103 setup_efi_pci(boot_params);
1105 status = efi_call_phys3(sys_table->boottime->allocate_pool,
1106 EFI_LOADER_DATA, sizeof(*gdt),
1108 if (status != EFI_SUCCESS) {
1109 efi_printk(sys_table, "Failed to alloc mem for gdt structure\n");
1114 status = efi_low_alloc(sys_table, gdt->size, 8,
1115 (unsigned long *)&gdt->address);
1116 if (status != EFI_SUCCESS) {
1117 efi_printk(sys_table, "Failed to alloc mem for gdt\n");
1121 status = efi_call_phys3(sys_table->boottime->allocate_pool,
1122 EFI_LOADER_DATA, sizeof(*idt),
1124 if (status != EFI_SUCCESS) {
1125 efi_printk(sys_table, "Failed to alloc mem for idt structure\n");
1133 * If the kernel isn't already loaded at the preferred load
1134 * address, relocate it.
1136 if (hdr->pref_address != hdr->code32_start) {
1137 unsigned long bzimage_addr = hdr->code32_start;
1138 status = efi_relocate_kernel(sys_table, &bzimage_addr,
1139 hdr->init_size, hdr->init_size,
1141 hdr->kernel_alignment);
1142 if (status != EFI_SUCCESS)
1145 hdr->pref_address = hdr->code32_start;
1146 hdr->code32_start = bzimage_addr;
1149 status = exit_boot(boot_params, handle);
1150 if (status != EFI_SUCCESS)
1153 memset((char *)gdt->address, 0x0, gdt->size);
1154 desc = (struct desc_struct *)gdt->address;
1156 /* The first GDT is a dummy and the second is unused. */
1159 desc->limit0 = 0xffff;
1160 desc->base0 = 0x0000;
1161 desc->base1 = 0x0000;
1162 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1163 desc->s = DESC_TYPE_CODE_DATA;
1169 desc->d = SEG_OP_SIZE_32BIT;
1170 desc->g = SEG_GRANULARITY_4KB;
1174 desc->limit0 = 0xffff;
1175 desc->base0 = 0x0000;
1176 desc->base1 = 0x0000;
1177 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
1178 desc->s = DESC_TYPE_CODE_DATA;
1184 desc->d = SEG_OP_SIZE_32BIT;
1185 desc->g = SEG_GRANULARITY_4KB;
1188 #ifdef CONFIG_X86_64
1189 /* Task segment value */
1191 desc->limit0 = 0x0000;
1192 desc->base0 = 0x0000;
1193 desc->base1 = 0x0000;
1194 desc->type = SEG_TYPE_TSS;
1202 desc->g = SEG_GRANULARITY_4KB;
1204 #endif /* CONFIG_X86_64 */
1206 asm volatile ("lidt %0" : : "m" (*idt));
1207 asm volatile ("lgdt %0" : : "m" (*gdt));
1209 asm volatile("cli");