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;
23 #include "../../../../drivers/firmware/efi/efi-stub-helper.c"
27 static void find_bits(unsigned long mask, u8 *pos, u8 *size)
35 while (!(mask & 0x1)) {
50 static efi_status_t setup_efi_pci(struct boot_params *params)
52 efi_pci_io_protocol *pci;
55 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
56 unsigned long nr_pci, size = 0;
58 struct setup_data *data;
60 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
62 while (data && data->next)
63 data = (struct setup_data *)(unsigned long)data->next;
65 status = efi_call_phys5(sys_table->boottime->locate_handle,
66 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
67 NULL, &size, pci_handle);
69 if (status == EFI_BUFFER_TOO_SMALL) {
70 status = efi_call_phys3(sys_table->boottime->allocate_pool,
71 EFI_LOADER_DATA, size, &pci_handle);
73 if (status != EFI_SUCCESS)
76 status = efi_call_phys5(sys_table->boottime->locate_handle,
77 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
78 NULL, &size, pci_handle);
81 if (status != EFI_SUCCESS)
84 nr_pci = size / sizeof(void *);
85 for (i = 0; i < nr_pci; i++) {
86 void *h = pci_handle[i];
88 struct pci_setup_rom *rom;
90 status = efi_call_phys3(sys_table->boottime->handle_protocol,
93 if (status != EFI_SUCCESS)
100 status = efi_call_phys4(pci->attributes, pci,
101 EfiPciIoAttributeOperationGet, 0,
104 status = efi_call_phys5(pci->attributes, pci,
105 EfiPciIoAttributeOperationGet, 0, 0,
108 if (status != EFI_SUCCESS)
111 if (!pci->romimage || !pci->romsize)
114 size = pci->romsize + sizeof(*rom);
116 status = efi_call_phys3(sys_table->boottime->allocate_pool,
117 EFI_LOADER_DATA, size, &rom);
119 if (status != EFI_SUCCESS)
122 rom->data.type = SETUP_PCI;
123 rom->data.len = size - sizeof(struct setup_data);
125 rom->pcilen = pci->romsize;
127 status = efi_call_phys5(pci->pci.read, pci,
128 EfiPciIoWidthUint16, PCI_VENDOR_ID,
131 if (status != EFI_SUCCESS)
134 status = efi_call_phys5(pci->pci.read, pci,
135 EfiPciIoWidthUint16, PCI_DEVICE_ID,
138 if (status != EFI_SUCCESS)
141 status = efi_call_phys5(pci->get_location, pci,
142 &(rom->segment), &(rom->bus),
143 &(rom->device), &(rom->function));
145 if (status != EFI_SUCCESS)
148 memcpy(rom->romdata, pci->romimage, pci->romsize);
151 data->next = (unsigned long)rom;
153 params->hdr.setup_data = (unsigned long)rom;
155 data = (struct setup_data *)rom;
159 efi_call_phys1(sys_table->boottime->free_pool, rom);
163 efi_call_phys1(sys_table->boottime->free_pool, pci_handle);
168 * See if we have Graphics Output Protocol
170 static efi_status_t setup_gop(struct screen_info *si, efi_guid_t *proto,
173 struct efi_graphics_output_protocol *gop, *first_gop;
174 struct efi_pixel_bitmask pixel_info;
175 unsigned long nr_gops;
179 u32 fb_base, fb_size;
180 u32 pixels_per_scan_line;
184 status = efi_call_phys3(sys_table->boottime->allocate_pool,
185 EFI_LOADER_DATA, size, &gop_handle);
186 if (status != EFI_SUCCESS)
189 status = efi_call_phys5(sys_table->boottime->locate_handle,
190 EFI_LOCATE_BY_PROTOCOL, proto,
191 NULL, &size, gop_handle);
192 if (status != EFI_SUCCESS)
197 nr_gops = size / sizeof(void *);
198 for (i = 0; i < nr_gops; i++) {
199 struct efi_graphics_output_mode_info *info;
200 efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
201 bool conout_found = false;
203 void *h = gop_handle[i];
205 status = efi_call_phys3(sys_table->boottime->handle_protocol,
207 if (status != EFI_SUCCESS)
210 status = efi_call_phys3(sys_table->boottime->handle_protocol,
211 h, &conout_proto, &dummy);
213 if (status == EFI_SUCCESS)
216 status = efi_call_phys4(gop->query_mode, gop,
217 gop->mode->mode, &size, &info);
218 if (status == EFI_SUCCESS && (!first_gop || conout_found)) {
220 * Systems that use the UEFI Console Splitter may
221 * provide multiple GOP devices, not all of which are
222 * backed by real hardware. The workaround is to search
223 * for a GOP implementing the ConOut protocol, and if
224 * one isn't found, to just fall back to the first GOP.
226 width = info->horizontal_resolution;
227 height = info->vertical_resolution;
228 fb_base = gop->mode->frame_buffer_base;
229 fb_size = gop->mode->frame_buffer_size;
230 pixel_format = info->pixel_format;
231 pixel_info = info->pixel_information;
232 pixels_per_scan_line = info->pixels_per_scan_line;
235 * Once we've found a GOP supporting ConOut,
236 * don't bother looking any further.
244 /* Did we find any GOPs? */
248 /* EFI framebuffer */
249 si->orig_video_isVGA = VIDEO_TYPE_EFI;
251 si->lfb_width = width;
252 si->lfb_height = height;
253 si->lfb_base = fb_base;
256 if (pixel_format == PIXEL_RGB_RESERVED_8BIT_PER_COLOR) {
258 si->lfb_linelength = pixels_per_scan_line * 4;
267 } else if (pixel_format == PIXEL_BGR_RESERVED_8BIT_PER_COLOR) {
269 si->lfb_linelength = pixels_per_scan_line * 4;
278 } else if (pixel_format == PIXEL_BIT_MASK) {
279 find_bits(pixel_info.red_mask, &si->red_pos, &si->red_size);
280 find_bits(pixel_info.green_mask, &si->green_pos,
282 find_bits(pixel_info.blue_mask, &si->blue_pos, &si->blue_size);
283 find_bits(pixel_info.reserved_mask, &si->rsvd_pos,
285 si->lfb_depth = si->red_size + si->green_size +
286 si->blue_size + si->rsvd_size;
287 si->lfb_linelength = (pixels_per_scan_line * si->lfb_depth) / 8;
290 si->lfb_linelength = si->lfb_width / 2;
301 si->lfb_size = si->lfb_linelength * si->lfb_height;
303 si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
306 efi_call_phys1(sys_table->boottime->free_pool, gop_handle);
311 * See if we have Universal Graphics Adapter (UGA) protocol
313 static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
316 struct efi_uga_draw_protocol *uga, *first_uga;
317 unsigned long nr_ugas;
320 void **uga_handle = NULL;
323 status = efi_call_phys3(sys_table->boottime->allocate_pool,
324 EFI_LOADER_DATA, size, &uga_handle);
325 if (status != EFI_SUCCESS)
328 status = efi_call_phys5(sys_table->boottime->locate_handle,
329 EFI_LOCATE_BY_PROTOCOL, uga_proto,
330 NULL, &size, uga_handle);
331 if (status != EFI_SUCCESS)
336 nr_ugas = size / sizeof(void *);
337 for (i = 0; i < nr_ugas; i++) {
338 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
339 void *handle = uga_handle[i];
340 u32 w, h, depth, refresh;
343 status = efi_call_phys3(sys_table->boottime->handle_protocol,
344 handle, uga_proto, &uga);
345 if (status != EFI_SUCCESS)
348 efi_call_phys3(sys_table->boottime->handle_protocol,
349 handle, &pciio_proto, &pciio);
351 status = efi_call_phys5(uga->get_mode, uga, &w, &h,
353 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
358 * Once we've found a UGA supporting PCIIO,
359 * don't bother looking any further.
371 /* EFI framebuffer */
372 si->orig_video_isVGA = VIDEO_TYPE_EFI;
375 si->lfb_width = width;
376 si->lfb_height = height;
389 efi_call_phys1(sys_table->boottime->free_pool, uga_handle);
393 void setup_graphics(struct boot_params *boot_params)
395 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
396 struct screen_info *si;
397 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
400 void **gop_handle = NULL;
401 void **uga_handle = NULL;
403 si = &boot_params->screen_info;
404 memset(si, 0, sizeof(*si));
407 status = efi_call_phys5(sys_table->boottime->locate_handle,
408 EFI_LOCATE_BY_PROTOCOL, &graphics_proto,
409 NULL, &size, gop_handle);
410 if (status == EFI_BUFFER_TOO_SMALL)
411 status = setup_gop(si, &graphics_proto, size);
413 if (status != EFI_SUCCESS) {
415 status = efi_call_phys5(sys_table->boottime->locate_handle,
416 EFI_LOCATE_BY_PROTOCOL, &uga_proto,
417 NULL, &size, uga_handle);
418 if (status == EFI_BUFFER_TOO_SMALL)
419 setup_uga(si, &uga_proto, size);
425 * Because the x86 boot code expects to be passed a boot_params we
426 * need to create one ourselves (usually the bootloader would create
429 * The caller is responsible for filling out ->code32_start in the
430 * returned boot_params.
432 struct boot_params *make_boot_params(void *handle, efi_system_table_t *_table)
434 struct boot_params *boot_params;
435 struct sys_desc_table *sdt;
436 struct apm_bios_info *bi;
437 struct setup_header *hdr;
438 struct efi_info *efi;
439 efi_loaded_image_t *image;
441 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
442 int options_size = 0;
448 unsigned long ramdisk_addr;
449 unsigned long ramdisk_size;
453 /* Check if we were booted by the EFI firmware */
454 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
457 status = efi_call_phys3(sys_table->boottime->handle_protocol,
458 handle, &proto, (void *)&image);
459 if (status != EFI_SUCCESS) {
460 efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
464 status = efi_low_alloc(sys_table, 0x4000, 1,
465 (unsigned long *)&boot_params);
466 if (status != EFI_SUCCESS) {
467 efi_printk(sys_table, "Failed to alloc lowmem for boot params\n");
471 memset(boot_params, 0x0, 0x4000);
473 hdr = &boot_params->hdr;
474 efi = &boot_params->efi_info;
475 bi = &boot_params->apm_bios_info;
476 sdt = &boot_params->sys_desc_table;
478 /* Copy the second sector to boot_params */
479 memcpy(&hdr->jump, image->image_base + 512, 512);
482 * Fill out some of the header fields ourselves because the
483 * EFI firmware loader doesn't load the first sector.
486 hdr->vid_mode = 0xffff;
487 hdr->boot_flag = 0xAA55;
489 hdr->type_of_loader = 0x21;
491 /* Convert unicode cmdline to ascii */
492 cmdline_ptr = efi_convert_cmdline_to_ascii(sys_table, image,
496 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
498 hdr->ramdisk_image = 0;
499 hdr->ramdisk_size = 0;
501 /* Clear APM BIOS info */
502 memset(bi, 0, sizeof(*bi));
504 memset(sdt, 0, sizeof(*sdt));
506 status = handle_cmdline_files(sys_table, image,
507 (char *)(unsigned long)hdr->cmd_line_ptr,
508 "initrd=", hdr->initrd_addr_max,
509 &ramdisk_addr, &ramdisk_size);
510 if (status != EFI_SUCCESS)
512 hdr->ramdisk_image = ramdisk_addr;
513 hdr->ramdisk_size = ramdisk_size;
517 efi_free(sys_table, options_size, hdr->cmd_line_ptr);
519 efi_free(sys_table, 0x4000, (unsigned long)boot_params);
523 static void add_e820ext(struct boot_params *params,
524 struct setup_data *e820ext, u32 nr_entries)
526 struct setup_data *data;
530 e820ext->type = SETUP_E820_EXT;
531 e820ext->len = nr_entries * sizeof(struct e820entry);
534 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
536 while (data && data->next)
537 data = (struct setup_data *)(unsigned long)data->next;
540 data->next = (unsigned long)e820ext;
542 params->hdr.setup_data = (unsigned long)e820ext;
545 static efi_status_t setup_e820(struct boot_params *params,
546 struct setup_data *e820ext, u32 e820ext_size)
548 struct e820entry *e820_map = ¶ms->e820_map[0];
549 struct efi_info *efi = ¶ms->efi_info;
550 struct e820entry *prev = NULL;
556 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
558 for (i = 0; i < nr_desc; i++) {
559 efi_memory_desc_t *d;
560 unsigned int e820_type = 0;
561 unsigned long m = efi->efi_memmap;
563 d = (efi_memory_desc_t *)(m + (i * efi->efi_memdesc_size));
565 case EFI_RESERVED_TYPE:
566 case EFI_RUNTIME_SERVICES_CODE:
567 case EFI_RUNTIME_SERVICES_DATA:
568 case EFI_MEMORY_MAPPED_IO:
569 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
571 e820_type = E820_RESERVED;
574 case EFI_UNUSABLE_MEMORY:
575 e820_type = E820_UNUSABLE;
578 case EFI_ACPI_RECLAIM_MEMORY:
579 e820_type = E820_ACPI;
582 case EFI_LOADER_CODE:
583 case EFI_LOADER_DATA:
584 case EFI_BOOT_SERVICES_CODE:
585 case EFI_BOOT_SERVICES_DATA:
586 case EFI_CONVENTIONAL_MEMORY:
587 e820_type = E820_RAM;
590 case EFI_ACPI_MEMORY_NVS:
591 e820_type = E820_NVS;
598 /* Merge adjacent mappings */
599 if (prev && prev->type == e820_type &&
600 (prev->addr + prev->size) == d->phys_addr) {
601 prev->size += d->num_pages << 12;
605 if (nr_entries == ARRAY_SIZE(params->e820_map)) {
606 u32 need = (nr_desc - i) * sizeof(struct e820entry) +
607 sizeof(struct setup_data);
609 if (!e820ext || e820ext_size < need)
610 return EFI_BUFFER_TOO_SMALL;
612 /* boot_params map full, switch to e820 extended */
613 e820_map = (struct e820entry *)e820ext->data;
616 e820_map->addr = d->phys_addr;
617 e820_map->size = d->num_pages << PAGE_SHIFT;
618 e820_map->type = e820_type;
623 if (nr_entries > ARRAY_SIZE(params->e820_map)) {
624 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_map);
626 add_e820ext(params, e820ext, nr_e820ext);
627 nr_entries -= nr_e820ext;
630 params->e820_entries = (u8)nr_entries;
635 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
641 size = sizeof(struct setup_data) +
642 sizeof(struct e820entry) * nr_desc;
645 efi_call_phys1(sys_table->boottime->free_pool, *e820ext);
650 status = efi_call_phys3(sys_table->boottime->allocate_pool,
651 EFI_LOADER_DATA, size, e820ext);
653 if (status == EFI_SUCCESS)
654 *e820ext_size = size;
659 static efi_status_t exit_boot(struct boot_params *boot_params,
662 struct efi_info *efi = &boot_params->efi_info;
663 unsigned long map_sz, key, desc_size;
664 efi_memory_desc_t *mem_map;
665 struct setup_data *e820ext;
667 __u32 nr_desc, prev_nr_desc;
670 bool called_exit = false;
679 status = efi_get_memory_map(sys_table, &mem_map, &map_sz, &desc_size,
680 &desc_version, &key);
682 if (status != EFI_SUCCESS)
685 prev_nr_desc = nr_desc;
686 nr_desc = map_sz / desc_size;
687 if (nr_desc > prev_nr_desc &&
688 nr_desc > ARRAY_SIZE(boot_params->e820_map)) {
689 u32 nr_e820ext = nr_desc - ARRAY_SIZE(boot_params->e820_map);
691 status = alloc_e820ext(nr_e820ext, &e820ext, &e820ext_size);
692 if (status != EFI_SUCCESS)
695 efi_call_phys1(sys_table->boottime->free_pool, mem_map);
696 goto get_map; /* Allocated memory, get map again */
699 memcpy(&efi->efi_loader_signature, EFI_LOADER_SIGNATURE, sizeof(__u32));
700 efi->efi_systab = (unsigned long)sys_table;
701 efi->efi_memdesc_size = desc_size;
702 efi->efi_memdesc_version = desc_version;
703 efi->efi_memmap = (unsigned long)mem_map;
704 efi->efi_memmap_size = map_sz;
707 efi->efi_systab_hi = (unsigned long)sys_table >> 32;
708 efi->efi_memmap_hi = (unsigned long)mem_map >> 32;
711 /* Might as well exit boot services now */
712 status = efi_call_phys2(sys_table->boottime->exit_boot_services,
714 if (status != EFI_SUCCESS) {
716 * ExitBootServices() will fail if any of the event
717 * handlers change the memory map. In which case, we
718 * must be prepared to retry, but only once so that
719 * we're guaranteed to exit on repeated failures instead
720 * of spinning forever.
726 efi_call_phys1(sys_table->boottime->free_pool, mem_map);
731 boot_params->alt_mem_k = 32 * 1024;
733 status = setup_e820(boot_params, e820ext, e820ext_size);
734 if (status != EFI_SUCCESS)
740 efi_call_phys1(sys_table->boottime->free_pool, mem_map);
746 * On success we return a pointer to a boot_params structure, and NULL
749 struct boot_params *efi_main(void *handle, efi_system_table_t *_table,
750 struct boot_params *boot_params)
752 struct desc_ptr *gdt;
753 efi_loaded_image_t *image;
754 struct setup_header *hdr = &boot_params->hdr;
756 struct desc_struct *desc;
760 /* Check if we were booted by the EFI firmware */
761 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
764 setup_graphics(boot_params);
766 setup_efi_pci(boot_params);
768 status = efi_call_phys3(sys_table->boottime->allocate_pool,
769 EFI_LOADER_DATA, sizeof(*gdt),
771 if (status != EFI_SUCCESS) {
772 efi_printk(sys_table, "Failed to alloc mem for gdt structure\n");
777 status = efi_low_alloc(sys_table, gdt->size, 8,
778 (unsigned long *)&gdt->address);
779 if (status != EFI_SUCCESS) {
780 efi_printk(sys_table, "Failed to alloc mem for gdt\n");
785 * If the kernel isn't already loaded at the preferred load
786 * address, relocate it.
788 if (hdr->pref_address != hdr->code32_start) {
789 unsigned long bzimage_addr = hdr->code32_start;
790 status = efi_relocate_kernel(sys_table, &bzimage_addr,
791 hdr->init_size, hdr->init_size,
793 hdr->kernel_alignment);
794 if (status != EFI_SUCCESS)
797 hdr->pref_address = hdr->code32_start;
798 hdr->code32_start = bzimage_addr;
801 status = exit_boot(boot_params, handle);
802 if (status != EFI_SUCCESS)
805 memset((char *)gdt->address, 0x0, gdt->size);
806 desc = (struct desc_struct *)gdt->address;
808 /* The first GDT is a dummy and the second is unused. */
811 desc->limit0 = 0xffff;
812 desc->base0 = 0x0000;
813 desc->base1 = 0x0000;
814 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
815 desc->s = DESC_TYPE_CODE_DATA;
821 desc->d = SEG_OP_SIZE_32BIT;
822 desc->g = SEG_GRANULARITY_4KB;
826 desc->limit0 = 0xffff;
827 desc->base0 = 0x0000;
828 desc->base1 = 0x0000;
829 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
830 desc->s = DESC_TYPE_CODE_DATA;
836 desc->d = SEG_OP_SIZE_32BIT;
837 desc->g = SEG_GRANULARITY_4KB;
841 /* Task segment value */
843 desc->limit0 = 0x0000;
844 desc->base0 = 0x0000;
845 desc->base1 = 0x0000;
846 desc->type = SEG_TYPE_TSS;
854 desc->g = SEG_GRANULARITY_4KB;
856 #endif /* CONFIG_X86_64 */
859 asm volatile ("lgdt %0" : : "m" (*gdt));