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 struct efi_config *efi_early;
24 #define efi_call_early(f, ...) \
25 efi_early->call(efi_early->f, __VA_ARGS__);
27 #define BOOT_SERVICES(bits) \
28 static void setup_boot_services##bits(struct efi_config *c) \
30 efi_system_table_##bits##_t *table; \
31 efi_boot_services_##bits##_t *bt; \
33 table = (typeof(table))sys_table; \
35 c->text_output = table->con_out; \
37 bt = (typeof(bt))(unsigned long)(table->boottime); \
39 c->allocate_pool = bt->allocate_pool; \
40 c->allocate_pages = bt->allocate_pages; \
41 c->get_memory_map = bt->get_memory_map; \
42 c->free_pool = bt->free_pool; \
43 c->free_pages = bt->free_pages; \
44 c->locate_handle = bt->locate_handle; \
45 c->handle_protocol = bt->handle_protocol; \
46 c->exit_boot_services = bt->exit_boot_services; \
51 void efi_char16_printk(efi_system_table_t *, efi_char16_t *);
54 __file_size32(void *__fh, efi_char16_t *filename_16,
55 void **handle, u64 *file_sz)
57 efi_file_handle_32_t *h, *fh = __fh;
58 efi_file_info_t *info;
60 efi_guid_t info_guid = EFI_FILE_INFO_ID;
63 status = efi_early->call((unsigned long)fh->open, fh, &h, filename_16,
64 EFI_FILE_MODE_READ, (u64)0);
65 if (status != EFI_SUCCESS) {
66 efi_printk(sys_table, "Failed to open file: ");
67 efi_char16_printk(sys_table, filename_16);
68 efi_printk(sys_table, "\n");
75 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
77 if (status != EFI_BUFFER_TOO_SMALL) {
78 efi_printk(sys_table, "Failed to get file info size\n");
83 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
84 info_sz, (void **)&info);
85 if (status != EFI_SUCCESS) {
86 efi_printk(sys_table, "Failed to alloc mem for file info\n");
90 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
92 if (status == EFI_BUFFER_TOO_SMALL) {
93 efi_call_early(free_pool, info);
97 *file_sz = info->file_size;
98 efi_call_early(free_pool, info);
100 if (status != EFI_SUCCESS)
101 efi_printk(sys_table, "Failed to get initrd info\n");
107 __file_size64(void *__fh, efi_char16_t *filename_16,
108 void **handle, u64 *file_sz)
110 efi_file_handle_64_t *h, *fh = __fh;
111 efi_file_info_t *info;
113 efi_guid_t info_guid = EFI_FILE_INFO_ID;
116 status = efi_early->call((unsigned long)fh->open, fh, &h, filename_16,
117 EFI_FILE_MODE_READ, (u64)0);
118 if (status != EFI_SUCCESS) {
119 efi_printk(sys_table, "Failed to open file: ");
120 efi_char16_printk(sys_table, filename_16);
121 efi_printk(sys_table, "\n");
128 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
130 if (status != EFI_BUFFER_TOO_SMALL) {
131 efi_printk(sys_table, "Failed to get file info size\n");
136 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
137 info_sz, (void **)&info);
138 if (status != EFI_SUCCESS) {
139 efi_printk(sys_table, "Failed to alloc mem for file info\n");
143 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
145 if (status == EFI_BUFFER_TOO_SMALL) {
146 efi_call_early(free_pool, info);
150 *file_sz = info->file_size;
151 efi_call_early(free_pool, info);
153 if (status != EFI_SUCCESS)
154 efi_printk(sys_table, "Failed to get initrd info\n");
159 efi_file_size(efi_system_table_t *sys_table, void *__fh,
160 efi_char16_t *filename_16, void **handle, u64 *file_sz)
163 return __file_size64(__fh, filename_16, handle, file_sz);
165 return __file_size32(__fh, filename_16, handle, file_sz);
169 efi_file_read(void *handle, unsigned long *size, void *addr)
173 if (efi_early->is64) {
174 efi_file_handle_64_t *fh = handle;
176 func = (unsigned long)fh->read;
177 return efi_early->call(func, handle, size, addr);
179 efi_file_handle_32_t *fh = handle;
181 func = (unsigned long)fh->read;
182 return efi_early->call(func, handle, size, addr);
186 efi_status_t efi_file_close(void *handle)
188 if (efi_early->is64) {
189 efi_file_handle_64_t *fh = handle;
191 return efi_early->call((unsigned long)fh->close, handle);
193 efi_file_handle_32_t *fh = handle;
195 return efi_early->call((unsigned long)fh->close, handle);
199 static inline efi_status_t __open_volume32(void *__image, void **__fh)
201 efi_file_io_interface_t *io;
202 efi_loaded_image_32_t *image = __image;
203 efi_file_handle_32_t *fh;
204 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
206 void *handle = (void *)(unsigned long)image->device_handle;
209 status = efi_call_early(handle_protocol, handle,
210 &fs_proto, (void **)&io);
211 if (status != EFI_SUCCESS) {
212 efi_printk(sys_table, "Failed to handle fs_proto\n");
216 func = (unsigned long)io->open_volume;
217 status = efi_early->call(func, io, &fh);
218 if (status != EFI_SUCCESS)
219 efi_printk(sys_table, "Failed to open volume\n");
225 static inline efi_status_t __open_volume64(void *__image, void **__fh)
227 efi_file_io_interface_t *io;
228 efi_loaded_image_64_t *image = __image;
229 efi_file_handle_64_t *fh;
230 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
232 void *handle = (void *)(unsigned long)image->device_handle;
235 status = efi_call_early(handle_protocol, handle,
236 &fs_proto, (void **)&io);
237 if (status != EFI_SUCCESS) {
238 efi_printk(sys_table, "Failed to handle fs_proto\n");
242 func = (unsigned long)io->open_volume;
243 status = efi_early->call(func, io, &fh);
244 if (status != EFI_SUCCESS)
245 efi_printk(sys_table, "Failed to open volume\n");
252 efi_open_volume(efi_system_table_t *sys_table, void *__image, void **__fh)
255 return __open_volume64(__image, __fh);
257 return __open_volume32(__image, __fh);
260 void efi_char16_printk(efi_system_table_t *table, efi_char16_t *str)
262 unsigned long output_string;
265 if (efi_early->is64) {
266 struct efi_simple_text_output_protocol_64 *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 = (u64 *)output_string;
274 efi_early->call(*func, out, str);
276 struct efi_simple_text_output_protocol_32 *out;
279 offset = offsetof(typeof(*out), output_string);
280 output_string = efi_early->text_output + offset;
281 out = (typeof(out))(unsigned long)efi_early->text_output;
282 func = (u32 *)output_string;
284 efi_early->call(*func, out, str);
288 #include "../../../../drivers/firmware/efi/libstub/efi-stub-helper.c"
290 static void find_bits(unsigned long mask, u8 *pos, u8 *size)
298 while (!(mask & 0x1)) {
314 __setup_efi_pci32(efi_pci_io_protocol_32 *pci, struct pci_setup_rom **__rom)
316 struct pci_setup_rom *rom = NULL;
321 status = efi_early->call(pci->attributes, pci,
322 EfiPciIoAttributeOperationGet, 0, 0,
324 if (status != EFI_SUCCESS)
327 if (!pci->romimage || !pci->romsize)
328 return EFI_INVALID_PARAMETER;
330 size = pci->romsize + sizeof(*rom);
332 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
333 if (status != EFI_SUCCESS) {
334 efi_printk(sys_table, "Failed to alloc mem for rom\n");
338 memset(rom, 0, sizeof(*rom));
340 rom->data.type = SETUP_PCI;
341 rom->data.len = size - sizeof(struct setup_data);
343 rom->pcilen = pci->romsize;
346 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
347 PCI_VENDOR_ID, 1, &(rom->vendor));
349 if (status != EFI_SUCCESS) {
350 efi_printk(sys_table, "Failed to read rom->vendor\n");
354 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
355 PCI_DEVICE_ID, 1, &(rom->devid));
357 if (status != EFI_SUCCESS) {
358 efi_printk(sys_table, "Failed to read rom->devid\n");
362 status = efi_early->call(pci->get_location, pci, &(rom->segment),
363 &(rom->bus), &(rom->device), &(rom->function));
365 if (status != EFI_SUCCESS)
368 memcpy(rom->romdata, pci->romimage, pci->romsize);
372 efi_call_early(free_pool, rom);
377 setup_efi_pci32(struct boot_params *params, void **pci_handle,
380 efi_pci_io_protocol_32 *pci = NULL;
381 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
382 u32 *handles = (u32 *)(unsigned long)pci_handle;
384 unsigned long nr_pci;
385 struct setup_data *data;
388 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
390 while (data && data->next)
391 data = (struct setup_data *)(unsigned long)data->next;
393 nr_pci = size / sizeof(u32);
394 for (i = 0; i < nr_pci; i++) {
395 struct pci_setup_rom *rom = NULL;
398 status = efi_call_early(handle_protocol, h,
399 &pci_proto, (void **)&pci);
401 if (status != EFI_SUCCESS)
407 status = __setup_efi_pci32(pci, &rom);
408 if (status != EFI_SUCCESS)
412 data->next = (unsigned long)rom;
414 params->hdr.setup_data = (unsigned long)rom;
416 data = (struct setup_data *)rom;
422 __setup_efi_pci64(efi_pci_io_protocol_64 *pci, struct pci_setup_rom **__rom)
424 struct pci_setup_rom *rom;
429 status = efi_early->call(pci->attributes, pci,
430 EfiPciIoAttributeOperationGet, 0,
432 if (status != EFI_SUCCESS)
435 if (!pci->romimage || !pci->romsize)
436 return EFI_INVALID_PARAMETER;
438 size = pci->romsize + sizeof(*rom);
440 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
441 if (status != EFI_SUCCESS) {
442 efi_printk(sys_table, "Failed to alloc mem for rom\n");
446 rom->data.type = SETUP_PCI;
447 rom->data.len = size - sizeof(struct setup_data);
449 rom->pcilen = pci->romsize;
452 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
453 PCI_VENDOR_ID, 1, &(rom->vendor));
455 if (status != EFI_SUCCESS) {
456 efi_printk(sys_table, "Failed to read rom->vendor\n");
460 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
461 PCI_DEVICE_ID, 1, &(rom->devid));
463 if (status != EFI_SUCCESS) {
464 efi_printk(sys_table, "Failed to read rom->devid\n");
468 status = efi_early->call(pci->get_location, pci, &(rom->segment),
469 &(rom->bus), &(rom->device), &(rom->function));
471 if (status != EFI_SUCCESS)
474 memcpy(rom->romdata, pci->romimage, pci->romsize);
478 efi_call_early(free_pool, rom);
484 setup_efi_pci64(struct boot_params *params, void **pci_handle,
487 efi_pci_io_protocol_64 *pci = NULL;
488 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
489 u64 *handles = (u64 *)(unsigned long)pci_handle;
491 unsigned long nr_pci;
492 struct setup_data *data;
495 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
497 while (data && data->next)
498 data = (struct setup_data *)(unsigned long)data->next;
500 nr_pci = size / sizeof(u64);
501 for (i = 0; i < nr_pci; i++) {
502 struct pci_setup_rom *rom = NULL;
505 status = efi_call_early(handle_protocol, h,
506 &pci_proto, (void **)&pci);
508 if (status != EFI_SUCCESS)
514 status = __setup_efi_pci64(pci, &rom);
515 if (status != EFI_SUCCESS)
519 data->next = (unsigned long)rom;
521 params->hdr.setup_data = (unsigned long)rom;
523 data = (struct setup_data *)rom;
529 * There's no way to return an informative status from this function,
530 * because any analysis (and printing of error messages) needs to be
531 * done directly at the EFI function call-site.
533 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
534 * just didn't find any PCI devices, but there's no way to tell outside
535 * the context of the call.
537 static void setup_efi_pci(struct boot_params *params)
540 void **pci_handle = NULL;
541 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
542 unsigned long size = 0;
544 status = efi_call_early(locate_handle,
545 EFI_LOCATE_BY_PROTOCOL,
546 &pci_proto, NULL, &size, pci_handle);
548 if (status == EFI_BUFFER_TOO_SMALL) {
549 status = efi_call_early(allocate_pool,
551 size, (void **)&pci_handle);
553 if (status != EFI_SUCCESS) {
554 efi_printk(sys_table, "Failed to alloc mem for pci_handle\n");
558 status = efi_call_early(locate_handle,
559 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
560 NULL, &size, pci_handle);
563 if (status != EFI_SUCCESS)
567 setup_efi_pci64(params, pci_handle, size);
569 setup_efi_pci32(params, pci_handle, size);
572 efi_call_early(free_pool, pci_handle);
576 setup_pixel_info(struct screen_info *si, u32 pixels_per_scan_line,
577 struct efi_pixel_bitmask pixel_info, int pixel_format)
579 if (pixel_format == PIXEL_RGB_RESERVED_8BIT_PER_COLOR) {
581 si->lfb_linelength = pixels_per_scan_line * 4;
590 } else if (pixel_format == PIXEL_BGR_RESERVED_8BIT_PER_COLOR) {
592 si->lfb_linelength = pixels_per_scan_line * 4;
601 } else if (pixel_format == PIXEL_BIT_MASK) {
602 find_bits(pixel_info.red_mask, &si->red_pos, &si->red_size);
603 find_bits(pixel_info.green_mask, &si->green_pos,
605 find_bits(pixel_info.blue_mask, &si->blue_pos, &si->blue_size);
606 find_bits(pixel_info.reserved_mask, &si->rsvd_pos,
608 si->lfb_depth = si->red_size + si->green_size +
609 si->blue_size + si->rsvd_size;
610 si->lfb_linelength = (pixels_per_scan_line * si->lfb_depth) / 8;
613 si->lfb_linelength = si->lfb_width / 2;
626 __gop_query32(struct efi_graphics_output_protocol_32 *gop32,
627 struct efi_graphics_output_mode_info **info,
628 unsigned long *size, u32 *fb_base)
630 struct efi_graphics_output_protocol_mode_32 *mode;
635 mode = (struct efi_graphics_output_protocol_mode_32 *)m;
637 status = efi_early->call(gop32->query_mode, gop32,
638 mode->mode, size, info);
639 if (status != EFI_SUCCESS)
642 *fb_base = mode->frame_buffer_base;
647 setup_gop32(struct screen_info *si, efi_guid_t *proto,
648 unsigned long size, void **gop_handle)
650 struct efi_graphics_output_protocol_32 *gop32, *first_gop;
651 unsigned long nr_gops;
653 u32 pixels_per_scan_line;
655 struct efi_pixel_bitmask pixel_info;
658 u32 *handles = (u32 *)(unsigned long)gop_handle;
664 nr_gops = size / sizeof(u32);
665 for (i = 0; i < nr_gops; i++) {
666 struct efi_graphics_output_mode_info *info = NULL;
667 efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
668 bool conout_found = false;
672 status = efi_call_early(handle_protocol, h,
673 proto, (void **)&gop32);
674 if (status != EFI_SUCCESS)
677 status = efi_call_early(handle_protocol, h,
678 &conout_proto, &dummy);
679 if (status == EFI_SUCCESS)
682 status = __gop_query32(gop32, &info, &size, &fb_base);
683 if (status == EFI_SUCCESS && (!first_gop || conout_found)) {
685 * Systems that use the UEFI Console Splitter may
686 * provide multiple GOP devices, not all of which are
687 * backed by real hardware. The workaround is to search
688 * for a GOP implementing the ConOut protocol, and if
689 * one isn't found, to just fall back to the first GOP.
691 width = info->horizontal_resolution;
692 height = info->vertical_resolution;
693 pixel_format = info->pixel_format;
694 pixel_info = info->pixel_information;
695 pixels_per_scan_line = info->pixels_per_scan_line;
698 * Once we've found a GOP supporting ConOut,
699 * don't bother looking any further.
707 /* Did we find any GOPs? */
711 /* EFI framebuffer */
712 si->orig_video_isVGA = VIDEO_TYPE_EFI;
714 si->lfb_width = width;
715 si->lfb_height = height;
716 si->lfb_base = fb_base;
719 setup_pixel_info(si, pixels_per_scan_line, pixel_info, pixel_format);
721 si->lfb_size = si->lfb_linelength * si->lfb_height;
723 si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
729 __gop_query64(struct efi_graphics_output_protocol_64 *gop64,
730 struct efi_graphics_output_mode_info **info,
731 unsigned long *size, u32 *fb_base)
733 struct efi_graphics_output_protocol_mode_64 *mode;
738 mode = (struct efi_graphics_output_protocol_mode_64 *)m;
740 status = efi_early->call(gop64->query_mode, gop64,
741 mode->mode, size, info);
742 if (status != EFI_SUCCESS)
745 *fb_base = mode->frame_buffer_base;
750 setup_gop64(struct screen_info *si, efi_guid_t *proto,
751 unsigned long size, void **gop_handle)
753 struct efi_graphics_output_protocol_64 *gop64, *first_gop;
754 unsigned long nr_gops;
756 u32 pixels_per_scan_line;
758 struct efi_pixel_bitmask pixel_info;
761 u64 *handles = (u64 *)(unsigned long)gop_handle;
767 nr_gops = size / sizeof(u64);
768 for (i = 0; i < nr_gops; i++) {
769 struct efi_graphics_output_mode_info *info = NULL;
770 efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
771 bool conout_found = false;
775 status = efi_call_early(handle_protocol, h,
776 proto, (void **)&gop64);
777 if (status != EFI_SUCCESS)
780 status = efi_call_early(handle_protocol, h,
781 &conout_proto, &dummy);
782 if (status == EFI_SUCCESS)
785 status = __gop_query64(gop64, &info, &size, &fb_base);
786 if (status == EFI_SUCCESS && (!first_gop || conout_found)) {
788 * Systems that use the UEFI Console Splitter may
789 * provide multiple GOP devices, not all of which are
790 * backed by real hardware. The workaround is to search
791 * for a GOP implementing the ConOut protocol, and if
792 * one isn't found, to just fall back to the first GOP.
794 width = info->horizontal_resolution;
795 height = info->vertical_resolution;
796 pixel_format = info->pixel_format;
797 pixel_info = info->pixel_information;
798 pixels_per_scan_line = info->pixels_per_scan_line;
801 * Once we've found a GOP supporting ConOut,
802 * don't bother looking any further.
810 /* Did we find any GOPs? */
814 /* EFI framebuffer */
815 si->orig_video_isVGA = VIDEO_TYPE_EFI;
817 si->lfb_width = width;
818 si->lfb_height = height;
819 si->lfb_base = fb_base;
822 setup_pixel_info(si, pixels_per_scan_line, pixel_info, pixel_format);
824 si->lfb_size = si->lfb_linelength * si->lfb_height;
826 si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
832 * See if we have Graphics Output Protocol
834 static efi_status_t setup_gop(struct screen_info *si, efi_guid_t *proto,
838 void **gop_handle = NULL;
840 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
841 size, (void **)&gop_handle);
842 if (status != EFI_SUCCESS)
845 status = efi_call_early(locate_handle,
846 EFI_LOCATE_BY_PROTOCOL,
847 proto, NULL, &size, gop_handle);
848 if (status != EFI_SUCCESS)
852 status = setup_gop64(si, proto, size, gop_handle);
854 status = setup_gop32(si, proto, size, gop_handle);
857 efi_call_early(free_pool, gop_handle);
862 setup_uga32(void **uga_handle, unsigned long size, u32 *width, u32 *height)
864 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
865 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
866 unsigned long nr_ugas;
867 u32 *handles = (u32 *)uga_handle;;
872 nr_ugas = size / sizeof(u32);
873 for (i = 0; i < nr_ugas; i++) {
874 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
875 u32 w, h, depth, refresh;
877 u32 handle = handles[i];
879 status = efi_call_early(handle_protocol, handle,
880 &uga_proto, (void **)&uga);
881 if (status != EFI_SUCCESS)
884 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
886 status = efi_early->call((unsigned long)uga->get_mode, uga,
887 &w, &h, &depth, &refresh);
888 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
893 * Once we've found a UGA supporting PCIIO,
894 * don't bother looking any further.
907 setup_uga64(void **uga_handle, unsigned long size, u32 *width, u32 *height)
909 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
910 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
911 unsigned long nr_ugas;
912 u64 *handles = (u64 *)uga_handle;;
917 nr_ugas = size / sizeof(u64);
918 for (i = 0; i < nr_ugas; i++) {
919 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
920 u32 w, h, depth, refresh;
922 u64 handle = handles[i];
924 status = efi_call_early(handle_protocol, handle,
925 &uga_proto, (void **)&uga);
926 if (status != EFI_SUCCESS)
929 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
931 status = efi_early->call((unsigned long)uga->get_mode, uga,
932 &w, &h, &depth, &refresh);
933 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
938 * Once we've found a UGA supporting PCIIO,
939 * don't bother looking any further.
952 * See if we have Universal Graphics Adapter (UGA) protocol
954 static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
959 void **uga_handle = NULL;
961 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
962 size, (void **)&uga_handle);
963 if (status != EFI_SUCCESS)
966 status = efi_call_early(locate_handle,
967 EFI_LOCATE_BY_PROTOCOL,
968 uga_proto, NULL, &size, uga_handle);
969 if (status != EFI_SUCCESS)
976 status = setup_uga64(uga_handle, size, &width, &height);
978 status = setup_uga32(uga_handle, size, &width, &height);
980 if (!width && !height)
983 /* EFI framebuffer */
984 si->orig_video_isVGA = VIDEO_TYPE_EFI;
987 si->lfb_width = width;
988 si->lfb_height = height;
1000 efi_call_early(free_pool, uga_handle);
1004 void setup_graphics(struct boot_params *boot_params)
1006 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
1007 struct screen_info *si;
1008 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
1009 efi_status_t status;
1011 void **gop_handle = NULL;
1012 void **uga_handle = NULL;
1014 si = &boot_params->screen_info;
1015 memset(si, 0, sizeof(*si));
1018 status = efi_call_early(locate_handle,
1019 EFI_LOCATE_BY_PROTOCOL,
1020 &graphics_proto, NULL, &size, gop_handle);
1021 if (status == EFI_BUFFER_TOO_SMALL)
1022 status = setup_gop(si, &graphics_proto, size);
1024 if (status != EFI_SUCCESS) {
1026 status = efi_call_early(locate_handle,
1027 EFI_LOCATE_BY_PROTOCOL,
1028 &uga_proto, NULL, &size, uga_handle);
1029 if (status == EFI_BUFFER_TOO_SMALL)
1030 setup_uga(si, &uga_proto, size);
1035 * Because the x86 boot code expects to be passed a boot_params we
1036 * need to create one ourselves (usually the bootloader would create
1039 * The caller is responsible for filling out ->code32_start in the
1040 * returned boot_params.
1042 struct boot_params *make_boot_params(struct efi_config *c)
1044 struct boot_params *boot_params;
1045 struct sys_desc_table *sdt;
1046 struct apm_bios_info *bi;
1047 struct setup_header *hdr;
1048 struct efi_info *efi;
1049 efi_loaded_image_t *image;
1050 void *options, *handle;
1051 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
1052 int options_size = 0;
1053 efi_status_t status;
1058 unsigned long ramdisk_addr;
1059 unsigned long ramdisk_size;
1062 sys_table = (efi_system_table_t *)(unsigned long)efi_early->table;
1063 handle = (void *)(unsigned long)efi_early->image_handle;
1065 /* Check if we were booted by the EFI firmware */
1066 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
1069 if (efi_early->is64)
1070 setup_boot_services64(efi_early);
1072 setup_boot_services32(efi_early);
1074 status = efi_call_early(handle_protocol, handle,
1075 &proto, (void *)&image);
1076 if (status != EFI_SUCCESS) {
1077 efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
1081 status = efi_low_alloc(sys_table, 0x4000, 1,
1082 (unsigned long *)&boot_params);
1083 if (status != EFI_SUCCESS) {
1084 efi_printk(sys_table, "Failed to alloc lowmem for boot params\n");
1088 memset(boot_params, 0x0, 0x4000);
1090 hdr = &boot_params->hdr;
1091 efi = &boot_params->efi_info;
1092 bi = &boot_params->apm_bios_info;
1093 sdt = &boot_params->sys_desc_table;
1095 /* Copy the second sector to boot_params */
1096 memcpy(&hdr->jump, image->image_base + 512, 512);
1099 * Fill out some of the header fields ourselves because the
1100 * EFI firmware loader doesn't load the first sector.
1102 hdr->root_flags = 1;
1103 hdr->vid_mode = 0xffff;
1104 hdr->boot_flag = 0xAA55;
1106 hdr->type_of_loader = 0x21;
1108 /* Convert unicode cmdline to ascii */
1109 cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size);
1112 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
1114 hdr->ramdisk_image = 0;
1115 hdr->ramdisk_size = 0;
1117 /* Clear APM BIOS info */
1118 memset(bi, 0, sizeof(*bi));
1120 memset(sdt, 0, sizeof(*sdt));
1122 status = efi_parse_options(cmdline_ptr);
1123 if (status != EFI_SUCCESS)
1126 status = handle_cmdline_files(sys_table, image,
1127 (char *)(unsigned long)hdr->cmd_line_ptr,
1128 "initrd=", hdr->initrd_addr_max,
1129 &ramdisk_addr, &ramdisk_size);
1131 if (status != EFI_SUCCESS &&
1132 hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) {
1133 efi_printk(sys_table, "Trying to load files to higher address\n");
1134 status = handle_cmdline_files(sys_table, image,
1135 (char *)(unsigned long)hdr->cmd_line_ptr,
1137 &ramdisk_addr, &ramdisk_size);
1140 if (status != EFI_SUCCESS)
1142 hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
1143 hdr->ramdisk_size = ramdisk_size & 0xffffffff;
1144 boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
1145 boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32;
1149 efi_free(sys_table, options_size, hdr->cmd_line_ptr);
1151 efi_free(sys_table, 0x4000, (unsigned long)boot_params);
1155 static void add_e820ext(struct boot_params *params,
1156 struct setup_data *e820ext, u32 nr_entries)
1158 struct setup_data *data;
1159 efi_status_t status;
1162 e820ext->type = SETUP_E820_EXT;
1163 e820ext->len = nr_entries * sizeof(struct e820entry);
1166 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
1168 while (data && data->next)
1169 data = (struct setup_data *)(unsigned long)data->next;
1172 data->next = (unsigned long)e820ext;
1174 params->hdr.setup_data = (unsigned long)e820ext;
1177 static efi_status_t setup_e820(struct boot_params *params,
1178 struct setup_data *e820ext, u32 e820ext_size)
1180 struct e820entry *e820_map = ¶ms->e820_map[0];
1181 struct efi_info *efi = ¶ms->efi_info;
1182 struct e820entry *prev = NULL;
1188 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
1190 for (i = 0; i < nr_desc; i++) {
1191 efi_memory_desc_t *d;
1192 unsigned int e820_type = 0;
1193 unsigned long m = efi->efi_memmap;
1195 d = (efi_memory_desc_t *)(m + (i * efi->efi_memdesc_size));
1197 case EFI_RESERVED_TYPE:
1198 case EFI_RUNTIME_SERVICES_CODE:
1199 case EFI_RUNTIME_SERVICES_DATA:
1200 case EFI_MEMORY_MAPPED_IO:
1201 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
1203 e820_type = E820_RESERVED;
1206 case EFI_UNUSABLE_MEMORY:
1207 e820_type = E820_UNUSABLE;
1210 case EFI_ACPI_RECLAIM_MEMORY:
1211 e820_type = E820_ACPI;
1214 case EFI_LOADER_CODE:
1215 case EFI_LOADER_DATA:
1216 case EFI_BOOT_SERVICES_CODE:
1217 case EFI_BOOT_SERVICES_DATA:
1218 case EFI_CONVENTIONAL_MEMORY:
1219 e820_type = E820_RAM;
1222 case EFI_ACPI_MEMORY_NVS:
1223 e820_type = E820_NVS;
1230 /* Merge adjacent mappings */
1231 if (prev && prev->type == e820_type &&
1232 (prev->addr + prev->size) == d->phys_addr) {
1233 prev->size += d->num_pages << 12;
1237 if (nr_entries == ARRAY_SIZE(params->e820_map)) {
1238 u32 need = (nr_desc - i) * sizeof(struct e820entry) +
1239 sizeof(struct setup_data);
1241 if (!e820ext || e820ext_size < need)
1242 return EFI_BUFFER_TOO_SMALL;
1244 /* boot_params map full, switch to e820 extended */
1245 e820_map = (struct e820entry *)e820ext->data;
1248 e820_map->addr = d->phys_addr;
1249 e820_map->size = d->num_pages << PAGE_SHIFT;
1250 e820_map->type = e820_type;
1255 if (nr_entries > ARRAY_SIZE(params->e820_map)) {
1256 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_map);
1258 add_e820ext(params, e820ext, nr_e820ext);
1259 nr_entries -= nr_e820ext;
1262 params->e820_entries = (u8)nr_entries;
1267 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
1270 efi_status_t status;
1273 size = sizeof(struct setup_data) +
1274 sizeof(struct e820entry) * nr_desc;
1277 efi_call_early(free_pool, *e820ext);
1282 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
1283 size, (void **)e820ext);
1284 if (status == EFI_SUCCESS)
1285 *e820ext_size = size;
1290 static efi_status_t exit_boot(struct boot_params *boot_params,
1291 void *handle, bool is64)
1293 struct efi_info *efi = &boot_params->efi_info;
1294 unsigned long map_sz, key, desc_size;
1295 efi_memory_desc_t *mem_map;
1296 struct setup_data *e820ext;
1297 const char *signature;
1299 __u32 nr_desc, prev_nr_desc;
1300 efi_status_t status;
1302 bool called_exit = false;
1311 status = efi_get_memory_map(sys_table, &mem_map, &map_sz, &desc_size,
1312 &desc_version, &key);
1314 if (status != EFI_SUCCESS)
1317 prev_nr_desc = nr_desc;
1318 nr_desc = map_sz / desc_size;
1319 if (nr_desc > prev_nr_desc &&
1320 nr_desc > ARRAY_SIZE(boot_params->e820_map)) {
1321 u32 nr_e820ext = nr_desc - ARRAY_SIZE(boot_params->e820_map);
1323 status = alloc_e820ext(nr_e820ext, &e820ext, &e820ext_size);
1324 if (status != EFI_SUCCESS)
1327 efi_call_early(free_pool, mem_map);
1328 goto get_map; /* Allocated memory, get map again */
1331 signature = is64 ? EFI64_LOADER_SIGNATURE : EFI32_LOADER_SIGNATURE;
1332 memcpy(&efi->efi_loader_signature, signature, sizeof(__u32));
1334 efi->efi_systab = (unsigned long)sys_table;
1335 efi->efi_memdesc_size = desc_size;
1336 efi->efi_memdesc_version = desc_version;
1337 efi->efi_memmap = (unsigned long)mem_map;
1338 efi->efi_memmap_size = map_sz;
1340 #ifdef CONFIG_X86_64
1341 efi->efi_systab_hi = (unsigned long)sys_table >> 32;
1342 efi->efi_memmap_hi = (unsigned long)mem_map >> 32;
1345 /* Might as well exit boot services now */
1346 status = efi_call_early(exit_boot_services, handle, key);
1347 if (status != EFI_SUCCESS) {
1349 * ExitBootServices() will fail if any of the event
1350 * handlers change the memory map. In which case, we
1351 * must be prepared to retry, but only once so that
1352 * we're guaranteed to exit on repeated failures instead
1353 * of spinning forever.
1359 efi_call_early(free_pool, mem_map);
1364 boot_params->alt_mem_k = 32 * 1024;
1366 status = setup_e820(boot_params, e820ext, e820ext_size);
1367 if (status != EFI_SUCCESS)
1373 efi_call_early(free_pool, mem_map);
1378 * On success we return a pointer to a boot_params structure, and NULL
1381 struct boot_params *efi_main(struct efi_config *c,
1382 struct boot_params *boot_params)
1384 struct desc_ptr *gdt = NULL;
1385 efi_loaded_image_t *image;
1386 struct setup_header *hdr = &boot_params->hdr;
1387 efi_status_t status;
1388 struct desc_struct *desc;
1390 efi_system_table_t *_table;
1395 _table = (efi_system_table_t *)(unsigned long)efi_early->table;
1396 handle = (void *)(unsigned long)efi_early->image_handle;
1397 is64 = efi_early->is64;
1401 /* Check if we were booted by the EFI firmware */
1402 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
1406 setup_boot_services64(efi_early);
1408 setup_boot_services32(efi_early);
1410 setup_graphics(boot_params);
1412 setup_efi_pci(boot_params);
1414 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
1415 sizeof(*gdt), (void **)&gdt);
1416 if (status != EFI_SUCCESS) {
1417 efi_printk(sys_table, "Failed to alloc mem for gdt structure\n");
1422 status = efi_low_alloc(sys_table, gdt->size, 8,
1423 (unsigned long *)&gdt->address);
1424 if (status != EFI_SUCCESS) {
1425 efi_printk(sys_table, "Failed to alloc mem for gdt\n");
1430 * If the kernel isn't already loaded at the preferred load
1431 * address, relocate it.
1433 if (hdr->pref_address != hdr->code32_start) {
1434 unsigned long bzimage_addr = hdr->code32_start;
1435 status = efi_relocate_kernel(sys_table, &bzimage_addr,
1436 hdr->init_size, hdr->init_size,
1438 hdr->kernel_alignment);
1439 if (status != EFI_SUCCESS) {
1440 efi_printk(sys_table, "efi_relocate_kernel() failed!\n");
1444 hdr->pref_address = hdr->code32_start;
1445 hdr->code32_start = bzimage_addr;
1448 status = exit_boot(boot_params, handle, is64);
1449 if (status != EFI_SUCCESS) {
1450 efi_printk(sys_table, "exit_boot() failed!\n");
1454 memset((char *)gdt->address, 0x0, gdt->size);
1455 desc = (struct desc_struct *)gdt->address;
1457 /* The first GDT is a dummy and the second is unused. */
1460 desc->limit0 = 0xffff;
1461 desc->base0 = 0x0000;
1462 desc->base1 = 0x0000;
1463 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1464 desc->s = DESC_TYPE_CODE_DATA;
1470 desc->d = SEG_OP_SIZE_32BIT;
1471 desc->g = SEG_GRANULARITY_4KB;
1475 desc->limit0 = 0xffff;
1476 desc->base0 = 0x0000;
1477 desc->base1 = 0x0000;
1478 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
1479 desc->s = DESC_TYPE_CODE_DATA;
1485 desc->d = SEG_OP_SIZE_32BIT;
1486 desc->g = SEG_GRANULARITY_4KB;
1489 #ifdef CONFIG_X86_64
1490 /* Task segment value */
1492 desc->limit0 = 0x0000;
1493 desc->base0 = 0x0000;
1494 desc->base1 = 0x0000;
1495 desc->type = SEG_TYPE_TSS;
1503 desc->g = SEG_GRANULARITY_4KB;
1505 #endif /* CONFIG_X86_64 */
1507 asm volatile("cli");
1508 asm volatile ("lgdt %0" : : "m" (*gdt));
1512 efi_printk(sys_table, "efi_main() failed!\n");