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)
336 memset(rom, 0, sizeof(*rom));
338 rom->data.type = SETUP_PCI;
339 rom->data.len = size - sizeof(struct setup_data);
341 rom->pcilen = pci->romsize;
344 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
345 PCI_VENDOR_ID, 1, &(rom->vendor));
347 if (status != EFI_SUCCESS)
350 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
351 PCI_DEVICE_ID, 1, &(rom->devid));
353 if (status != EFI_SUCCESS)
356 status = efi_early->call(pci->get_location, pci, &(rom->segment),
357 &(rom->bus), &(rom->device), &(rom->function));
359 if (status != EFI_SUCCESS)
362 memcpy(rom->romdata, pci->romimage, pci->romsize);
366 efi_call_early(free_pool, rom);
371 setup_efi_pci32(struct boot_params *params, void **pci_handle,
374 efi_pci_io_protocol_32 *pci = NULL;
375 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
376 u32 *handles = (u32 *)(unsigned long)pci_handle;
378 unsigned long nr_pci;
379 struct setup_data *data;
382 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
384 while (data && data->next)
385 data = (struct setup_data *)(unsigned long)data->next;
387 nr_pci = size / sizeof(u32);
388 for (i = 0; i < nr_pci; i++) {
389 struct pci_setup_rom *rom = NULL;
392 status = efi_call_early(handle_protocol, h,
393 &pci_proto, (void **)&pci);
395 if (status != EFI_SUCCESS)
401 status = __setup_efi_pci32(pci, &rom);
402 if (status != EFI_SUCCESS)
406 data->next = (unsigned long)rom;
408 params->hdr.setup_data = (unsigned long)rom;
410 data = (struct setup_data *)rom;
416 __setup_efi_pci64(efi_pci_io_protocol_64 *pci, struct pci_setup_rom **__rom)
418 struct pci_setup_rom *rom;
423 status = efi_early->call(pci->attributes, pci,
424 EfiPciIoAttributeOperationGet, 0,
426 if (status != EFI_SUCCESS)
429 if (!pci->romimage || !pci->romsize)
430 return EFI_INVALID_PARAMETER;
432 size = pci->romsize + sizeof(*rom);
434 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
435 if (status != EFI_SUCCESS)
438 rom->data.type = SETUP_PCI;
439 rom->data.len = size - sizeof(struct setup_data);
441 rom->pcilen = pci->romsize;
444 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
445 PCI_VENDOR_ID, 1, &(rom->vendor));
447 if (status != EFI_SUCCESS)
450 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
451 PCI_DEVICE_ID, 1, &(rom->devid));
453 if (status != EFI_SUCCESS)
456 status = efi_early->call(pci->get_location, pci, &(rom->segment),
457 &(rom->bus), &(rom->device), &(rom->function));
459 if (status != EFI_SUCCESS)
462 memcpy(rom->romdata, pci->romimage, pci->romsize);
466 efi_call_early(free_pool, rom);
472 setup_efi_pci64(struct boot_params *params, void **pci_handle,
475 efi_pci_io_protocol_64 *pci = NULL;
476 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
477 u64 *handles = (u64 *)(unsigned long)pci_handle;
479 unsigned long nr_pci;
480 struct setup_data *data;
483 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
485 while (data && data->next)
486 data = (struct setup_data *)(unsigned long)data->next;
488 nr_pci = size / sizeof(u64);
489 for (i = 0; i < nr_pci; i++) {
490 struct pci_setup_rom *rom = NULL;
493 status = efi_call_early(handle_protocol, h,
494 &pci_proto, (void **)&pci);
496 if (status != EFI_SUCCESS)
502 status = __setup_efi_pci64(pci, &rom);
503 if (status != EFI_SUCCESS)
507 data->next = (unsigned long)rom;
509 params->hdr.setup_data = (unsigned long)rom;
511 data = (struct setup_data *)rom;
517 * There's no way to return an informative status from this function,
518 * because any analysis (and printing of error messages) needs to be
519 * done directly at the EFI function call-site.
521 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
522 * just didn't find any PCI devices, but there's no way to tell outside
523 * the context of the call.
525 static void setup_efi_pci(struct boot_params *params)
528 void **pci_handle = NULL;
529 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
530 unsigned long size = 0;
532 status = efi_call_early(locate_handle,
533 EFI_LOCATE_BY_PROTOCOL,
534 &pci_proto, NULL, &size, pci_handle);
536 if (status == EFI_BUFFER_TOO_SMALL) {
537 status = efi_call_early(allocate_pool,
539 size, (void **)&pci_handle);
541 if (status != EFI_SUCCESS)
544 status = efi_call_early(locate_handle,
545 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
546 NULL, &size, pci_handle);
549 if (status != EFI_SUCCESS)
553 setup_efi_pci64(params, pci_handle, size);
555 setup_efi_pci32(params, pci_handle, size);
558 efi_call_early(free_pool, pci_handle);
562 setup_pixel_info(struct screen_info *si, u32 pixels_per_scan_line,
563 struct efi_pixel_bitmask pixel_info, int pixel_format)
565 if (pixel_format == PIXEL_RGB_RESERVED_8BIT_PER_COLOR) {
567 si->lfb_linelength = pixels_per_scan_line * 4;
576 } else if (pixel_format == PIXEL_BGR_RESERVED_8BIT_PER_COLOR) {
578 si->lfb_linelength = pixels_per_scan_line * 4;
587 } else if (pixel_format == PIXEL_BIT_MASK) {
588 find_bits(pixel_info.red_mask, &si->red_pos, &si->red_size);
589 find_bits(pixel_info.green_mask, &si->green_pos,
591 find_bits(pixel_info.blue_mask, &si->blue_pos, &si->blue_size);
592 find_bits(pixel_info.reserved_mask, &si->rsvd_pos,
594 si->lfb_depth = si->red_size + si->green_size +
595 si->blue_size + si->rsvd_size;
596 si->lfb_linelength = (pixels_per_scan_line * si->lfb_depth) / 8;
599 si->lfb_linelength = si->lfb_width / 2;
612 __gop_query32(struct efi_graphics_output_protocol_32 *gop32,
613 struct efi_graphics_output_mode_info **info,
614 unsigned long *size, u32 *fb_base)
616 struct efi_graphics_output_protocol_mode_32 *mode;
621 mode = (struct efi_graphics_output_protocol_mode_32 *)m;
623 status = efi_early->call(gop32->query_mode, gop32,
624 mode->mode, size, info);
625 if (status != EFI_SUCCESS)
628 *fb_base = mode->frame_buffer_base;
633 setup_gop32(struct screen_info *si, efi_guid_t *proto,
634 unsigned long size, void **gop_handle)
636 struct efi_graphics_output_protocol_32 *gop32, *first_gop;
637 unsigned long nr_gops;
639 u32 pixels_per_scan_line;
641 struct efi_pixel_bitmask pixel_info;
644 u32 *handles = (u32 *)(unsigned long)gop_handle;
650 nr_gops = size / sizeof(u32);
651 for (i = 0; i < nr_gops; i++) {
652 struct efi_graphics_output_mode_info *info = NULL;
653 efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
654 bool conout_found = false;
658 status = efi_call_early(handle_protocol, h,
659 proto, (void **)&gop32);
660 if (status != EFI_SUCCESS)
663 status = efi_call_early(handle_protocol, h,
664 &conout_proto, &dummy);
665 if (status == EFI_SUCCESS)
668 status = __gop_query32(gop32, &info, &size, &fb_base);
669 if (status == EFI_SUCCESS && (!first_gop || conout_found)) {
671 * Systems that use the UEFI Console Splitter may
672 * provide multiple GOP devices, not all of which are
673 * backed by real hardware. The workaround is to search
674 * for a GOP implementing the ConOut protocol, and if
675 * one isn't found, to just fall back to the first GOP.
677 width = info->horizontal_resolution;
678 height = info->vertical_resolution;
679 pixel_format = info->pixel_format;
680 pixel_info = info->pixel_information;
681 pixels_per_scan_line = info->pixels_per_scan_line;
684 * Once we've found a GOP supporting ConOut,
685 * don't bother looking any further.
693 /* Did we find any GOPs? */
697 /* EFI framebuffer */
698 si->orig_video_isVGA = VIDEO_TYPE_EFI;
700 si->lfb_width = width;
701 si->lfb_height = height;
702 si->lfb_base = fb_base;
705 setup_pixel_info(si, pixels_per_scan_line, pixel_info, pixel_format);
707 si->lfb_size = si->lfb_linelength * si->lfb_height;
709 si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
715 __gop_query64(struct efi_graphics_output_protocol_64 *gop64,
716 struct efi_graphics_output_mode_info **info,
717 unsigned long *size, u32 *fb_base)
719 struct efi_graphics_output_protocol_mode_64 *mode;
724 mode = (struct efi_graphics_output_protocol_mode_64 *)m;
726 status = efi_early->call(gop64->query_mode, gop64,
727 mode->mode, size, info);
728 if (status != EFI_SUCCESS)
731 *fb_base = mode->frame_buffer_base;
736 setup_gop64(struct screen_info *si, efi_guid_t *proto,
737 unsigned long size, void **gop_handle)
739 struct efi_graphics_output_protocol_64 *gop64, *first_gop;
740 unsigned long nr_gops;
742 u32 pixels_per_scan_line;
744 struct efi_pixel_bitmask pixel_info;
747 u64 *handles = (u64 *)(unsigned long)gop_handle;
753 nr_gops = size / sizeof(u64);
754 for (i = 0; i < nr_gops; i++) {
755 struct efi_graphics_output_mode_info *info = NULL;
756 efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
757 bool conout_found = false;
761 status = efi_call_early(handle_protocol, h,
762 proto, (void **)&gop64);
763 if (status != EFI_SUCCESS)
766 status = efi_call_early(handle_protocol, h,
767 &conout_proto, &dummy);
768 if (status == EFI_SUCCESS)
771 status = __gop_query64(gop64, &info, &size, &fb_base);
772 if (status == EFI_SUCCESS && (!first_gop || conout_found)) {
774 * Systems that use the UEFI Console Splitter may
775 * provide multiple GOP devices, not all of which are
776 * backed by real hardware. The workaround is to search
777 * for a GOP implementing the ConOut protocol, and if
778 * one isn't found, to just fall back to the first GOP.
780 width = info->horizontal_resolution;
781 height = info->vertical_resolution;
782 pixel_format = info->pixel_format;
783 pixel_info = info->pixel_information;
784 pixels_per_scan_line = info->pixels_per_scan_line;
787 * Once we've found a GOP supporting ConOut,
788 * don't bother looking any further.
796 /* Did we find any GOPs? */
800 /* EFI framebuffer */
801 si->orig_video_isVGA = VIDEO_TYPE_EFI;
803 si->lfb_width = width;
804 si->lfb_height = height;
805 si->lfb_base = fb_base;
808 setup_pixel_info(si, pixels_per_scan_line, pixel_info, pixel_format);
810 si->lfb_size = si->lfb_linelength * si->lfb_height;
812 si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
818 * See if we have Graphics Output Protocol
820 static efi_status_t setup_gop(struct screen_info *si, efi_guid_t *proto,
824 void **gop_handle = NULL;
826 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
827 size, (void **)&gop_handle);
828 if (status != EFI_SUCCESS)
831 status = efi_call_early(locate_handle,
832 EFI_LOCATE_BY_PROTOCOL,
833 proto, NULL, &size, gop_handle);
834 if (status != EFI_SUCCESS)
838 status = setup_gop64(si, proto, size, gop_handle);
840 status = setup_gop32(si, proto, size, gop_handle);
843 efi_call_early(free_pool, gop_handle);
848 setup_uga32(void **uga_handle, unsigned long size, u32 *width, u32 *height)
850 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
851 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
852 unsigned long nr_ugas;
853 u32 *handles = (u32 *)uga_handle;;
858 nr_ugas = size / sizeof(u32);
859 for (i = 0; i < nr_ugas; i++) {
860 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
861 u32 w, h, depth, refresh;
863 u32 handle = handles[i];
865 status = efi_call_early(handle_protocol, handle,
866 &uga_proto, (void **)&uga);
867 if (status != EFI_SUCCESS)
870 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
872 status = efi_early->call((unsigned long)uga->get_mode, uga,
873 &w, &h, &depth, &refresh);
874 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
879 * Once we've found a UGA supporting PCIIO,
880 * don't bother looking any further.
893 setup_uga64(void **uga_handle, unsigned long size, u32 *width, u32 *height)
895 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
896 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
897 unsigned long nr_ugas;
898 u64 *handles = (u64 *)uga_handle;;
903 nr_ugas = size / sizeof(u64);
904 for (i = 0; i < nr_ugas; i++) {
905 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
906 u32 w, h, depth, refresh;
908 u64 handle = handles[i];
910 status = efi_call_early(handle_protocol, handle,
911 &uga_proto, (void **)&uga);
912 if (status != EFI_SUCCESS)
915 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
917 status = efi_early->call((unsigned long)uga->get_mode, uga,
918 &w, &h, &depth, &refresh);
919 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
924 * Once we've found a UGA supporting PCIIO,
925 * don't bother looking any further.
938 * See if we have Universal Graphics Adapter (UGA) protocol
940 static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
945 void **uga_handle = NULL;
947 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
948 size, (void **)&uga_handle);
949 if (status != EFI_SUCCESS)
952 status = efi_call_early(locate_handle,
953 EFI_LOCATE_BY_PROTOCOL,
954 uga_proto, NULL, &size, uga_handle);
955 if (status != EFI_SUCCESS)
962 status = setup_uga64(uga_handle, size, &width, &height);
964 status = setup_uga32(uga_handle, size, &width, &height);
966 if (!width && !height)
969 /* EFI framebuffer */
970 si->orig_video_isVGA = VIDEO_TYPE_EFI;
973 si->lfb_width = width;
974 si->lfb_height = height;
986 efi_call_early(free_pool, uga_handle);
990 void setup_graphics(struct boot_params *boot_params)
992 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
993 struct screen_info *si;
994 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
997 void **gop_handle = NULL;
998 void **uga_handle = NULL;
1000 si = &boot_params->screen_info;
1001 memset(si, 0, sizeof(*si));
1004 status = efi_call_early(locate_handle,
1005 EFI_LOCATE_BY_PROTOCOL,
1006 &graphics_proto, NULL, &size, gop_handle);
1007 if (status == EFI_BUFFER_TOO_SMALL)
1008 status = setup_gop(si, &graphics_proto, size);
1010 if (status != EFI_SUCCESS) {
1012 status = efi_call_early(locate_handle,
1013 EFI_LOCATE_BY_PROTOCOL,
1014 &uga_proto, NULL, &size, uga_handle);
1015 if (status == EFI_BUFFER_TOO_SMALL)
1016 setup_uga(si, &uga_proto, size);
1021 * Because the x86 boot code expects to be passed a boot_params we
1022 * need to create one ourselves (usually the bootloader would create
1025 * The caller is responsible for filling out ->code32_start in the
1026 * returned boot_params.
1028 struct boot_params *make_boot_params(struct efi_config *c)
1030 struct boot_params *boot_params;
1031 struct sys_desc_table *sdt;
1032 struct apm_bios_info *bi;
1033 struct setup_header *hdr;
1034 struct efi_info *efi;
1035 efi_loaded_image_t *image;
1036 void *options, *handle;
1037 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
1038 int options_size = 0;
1039 efi_status_t status;
1044 unsigned long ramdisk_addr;
1045 unsigned long ramdisk_size;
1048 sys_table = (efi_system_table_t *)(unsigned long)efi_early->table;
1049 handle = (void *)(unsigned long)efi_early->image_handle;
1051 /* Check if we were booted by the EFI firmware */
1052 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
1055 if (efi_early->is64)
1056 setup_boot_services64(efi_early);
1058 setup_boot_services32(efi_early);
1060 status = efi_call_early(handle_protocol, handle,
1061 &proto, (void *)&image);
1062 if (status != EFI_SUCCESS) {
1063 efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
1067 status = efi_low_alloc(sys_table, 0x4000, 1,
1068 (unsigned long *)&boot_params);
1069 if (status != EFI_SUCCESS) {
1070 efi_printk(sys_table, "Failed to alloc lowmem for boot params\n");
1074 memset(boot_params, 0x0, 0x4000);
1076 hdr = &boot_params->hdr;
1077 efi = &boot_params->efi_info;
1078 bi = &boot_params->apm_bios_info;
1079 sdt = &boot_params->sys_desc_table;
1081 /* Copy the second sector to boot_params */
1082 memcpy(&hdr->jump, image->image_base + 512, 512);
1085 * Fill out some of the header fields ourselves because the
1086 * EFI firmware loader doesn't load the first sector.
1088 hdr->root_flags = 1;
1089 hdr->vid_mode = 0xffff;
1090 hdr->boot_flag = 0xAA55;
1092 hdr->type_of_loader = 0x21;
1094 /* Convert unicode cmdline to ascii */
1095 cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size);
1098 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
1100 hdr->ramdisk_image = 0;
1101 hdr->ramdisk_size = 0;
1103 /* Clear APM BIOS info */
1104 memset(bi, 0, sizeof(*bi));
1106 memset(sdt, 0, sizeof(*sdt));
1108 status = handle_cmdline_files(sys_table, image,
1109 (char *)(unsigned long)hdr->cmd_line_ptr,
1110 "initrd=", hdr->initrd_addr_max,
1111 &ramdisk_addr, &ramdisk_size);
1113 if (status != EFI_SUCCESS &&
1114 hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) {
1115 efi_printk(sys_table, "Trying to load files to higher address\n");
1116 status = handle_cmdline_files(sys_table, image,
1117 (char *)(unsigned long)hdr->cmd_line_ptr,
1119 &ramdisk_addr, &ramdisk_size);
1122 if (status != EFI_SUCCESS)
1124 hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
1125 hdr->ramdisk_size = ramdisk_size & 0xffffffff;
1126 boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
1127 boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32;
1131 efi_free(sys_table, options_size, hdr->cmd_line_ptr);
1133 efi_free(sys_table, 0x4000, (unsigned long)boot_params);
1137 static void add_e820ext(struct boot_params *params,
1138 struct setup_data *e820ext, u32 nr_entries)
1140 struct setup_data *data;
1141 efi_status_t status;
1144 e820ext->type = SETUP_E820_EXT;
1145 e820ext->len = nr_entries * sizeof(struct e820entry);
1148 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
1150 while (data && data->next)
1151 data = (struct setup_data *)(unsigned long)data->next;
1154 data->next = (unsigned long)e820ext;
1156 params->hdr.setup_data = (unsigned long)e820ext;
1159 static efi_status_t setup_e820(struct boot_params *params,
1160 struct setup_data *e820ext, u32 e820ext_size)
1162 struct e820entry *e820_map = ¶ms->e820_map[0];
1163 struct efi_info *efi = ¶ms->efi_info;
1164 struct e820entry *prev = NULL;
1170 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
1172 for (i = 0; i < nr_desc; i++) {
1173 efi_memory_desc_t *d;
1174 unsigned int e820_type = 0;
1175 unsigned long m = efi->efi_memmap;
1177 d = (efi_memory_desc_t *)(m + (i * efi->efi_memdesc_size));
1179 case EFI_RESERVED_TYPE:
1180 case EFI_RUNTIME_SERVICES_CODE:
1181 case EFI_RUNTIME_SERVICES_DATA:
1182 case EFI_MEMORY_MAPPED_IO:
1183 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
1185 e820_type = E820_RESERVED;
1188 case EFI_UNUSABLE_MEMORY:
1189 e820_type = E820_UNUSABLE;
1192 case EFI_ACPI_RECLAIM_MEMORY:
1193 e820_type = E820_ACPI;
1196 case EFI_LOADER_CODE:
1197 case EFI_LOADER_DATA:
1198 case EFI_BOOT_SERVICES_CODE:
1199 case EFI_BOOT_SERVICES_DATA:
1200 case EFI_CONVENTIONAL_MEMORY:
1201 e820_type = E820_RAM;
1204 case EFI_ACPI_MEMORY_NVS:
1205 e820_type = E820_NVS;
1212 /* Merge adjacent mappings */
1213 if (prev && prev->type == e820_type &&
1214 (prev->addr + prev->size) == d->phys_addr) {
1215 prev->size += d->num_pages << 12;
1219 if (nr_entries == ARRAY_SIZE(params->e820_map)) {
1220 u32 need = (nr_desc - i) * sizeof(struct e820entry) +
1221 sizeof(struct setup_data);
1223 if (!e820ext || e820ext_size < need)
1224 return EFI_BUFFER_TOO_SMALL;
1226 /* boot_params map full, switch to e820 extended */
1227 e820_map = (struct e820entry *)e820ext->data;
1230 e820_map->addr = d->phys_addr;
1231 e820_map->size = d->num_pages << PAGE_SHIFT;
1232 e820_map->type = e820_type;
1237 if (nr_entries > ARRAY_SIZE(params->e820_map)) {
1238 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_map);
1240 add_e820ext(params, e820ext, nr_e820ext);
1241 nr_entries -= nr_e820ext;
1244 params->e820_entries = (u8)nr_entries;
1249 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
1252 efi_status_t status;
1255 size = sizeof(struct setup_data) +
1256 sizeof(struct e820entry) * nr_desc;
1259 efi_call_early(free_pool, *e820ext);
1264 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
1265 size, (void **)e820ext);
1266 if (status == EFI_SUCCESS)
1267 *e820ext_size = size;
1272 static efi_status_t exit_boot(struct boot_params *boot_params,
1273 void *handle, bool is64)
1275 struct efi_info *efi = &boot_params->efi_info;
1276 unsigned long map_sz, key, desc_size;
1277 efi_memory_desc_t *mem_map;
1278 struct setup_data *e820ext;
1279 const char *signature;
1281 __u32 nr_desc, prev_nr_desc;
1282 efi_status_t status;
1284 bool called_exit = false;
1293 status = efi_get_memory_map(sys_table, &mem_map, &map_sz, &desc_size,
1294 &desc_version, &key);
1296 if (status != EFI_SUCCESS)
1299 prev_nr_desc = nr_desc;
1300 nr_desc = map_sz / desc_size;
1301 if (nr_desc > prev_nr_desc &&
1302 nr_desc > ARRAY_SIZE(boot_params->e820_map)) {
1303 u32 nr_e820ext = nr_desc - ARRAY_SIZE(boot_params->e820_map);
1305 status = alloc_e820ext(nr_e820ext, &e820ext, &e820ext_size);
1306 if (status != EFI_SUCCESS)
1309 efi_call_early(free_pool, mem_map);
1310 goto get_map; /* Allocated memory, get map again */
1313 signature = is64 ? EFI64_LOADER_SIGNATURE : EFI32_LOADER_SIGNATURE;
1314 memcpy(&efi->efi_loader_signature, signature, sizeof(__u32));
1316 efi->efi_systab = (unsigned long)sys_table;
1317 efi->efi_memdesc_size = desc_size;
1318 efi->efi_memdesc_version = desc_version;
1319 efi->efi_memmap = (unsigned long)mem_map;
1320 efi->efi_memmap_size = map_sz;
1322 #ifdef CONFIG_X86_64
1323 efi->efi_systab_hi = (unsigned long)sys_table >> 32;
1324 efi->efi_memmap_hi = (unsigned long)mem_map >> 32;
1327 /* Might as well exit boot services now */
1328 status = efi_call_early(exit_boot_services, handle, key);
1329 if (status != EFI_SUCCESS) {
1331 * ExitBootServices() will fail if any of the event
1332 * handlers change the memory map. In which case, we
1333 * must be prepared to retry, but only once so that
1334 * we're guaranteed to exit on repeated failures instead
1335 * of spinning forever.
1341 efi_call_early(free_pool, mem_map);
1346 boot_params->alt_mem_k = 32 * 1024;
1348 status = setup_e820(boot_params, e820ext, e820ext_size);
1349 if (status != EFI_SUCCESS)
1355 efi_call_early(free_pool, mem_map);
1360 * On success we return a pointer to a boot_params structure, and NULL
1363 struct boot_params *efi_main(struct efi_config *c,
1364 struct boot_params *boot_params)
1366 struct desc_ptr *gdt = NULL;
1367 efi_loaded_image_t *image;
1368 struct setup_header *hdr = &boot_params->hdr;
1369 efi_status_t status;
1370 struct desc_struct *desc;
1372 efi_system_table_t *_table;
1377 _table = (efi_system_table_t *)(unsigned long)efi_early->table;
1378 handle = (void *)(unsigned long)efi_early->image_handle;
1379 is64 = efi_early->is64;
1383 /* Check if we were booted by the EFI firmware */
1384 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
1388 setup_boot_services64(efi_early);
1390 setup_boot_services32(efi_early);
1392 setup_graphics(boot_params);
1394 setup_efi_pci(boot_params);
1396 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
1397 sizeof(*gdt), (void **)&gdt);
1398 if (status != EFI_SUCCESS) {
1399 efi_printk(sys_table, "Failed to alloc mem for gdt structure\n");
1404 status = efi_low_alloc(sys_table, gdt->size, 8,
1405 (unsigned long *)&gdt->address);
1406 if (status != EFI_SUCCESS) {
1407 efi_printk(sys_table, "Failed to alloc mem for gdt\n");
1412 * If the kernel isn't already loaded at the preferred load
1413 * address, relocate it.
1415 if (hdr->pref_address != hdr->code32_start) {
1416 unsigned long bzimage_addr = hdr->code32_start;
1417 status = efi_relocate_kernel(sys_table, &bzimage_addr,
1418 hdr->init_size, hdr->init_size,
1420 hdr->kernel_alignment);
1421 if (status != EFI_SUCCESS) {
1422 efi_printk(sys_table, "efi_relocate_kernel() failed!\n");
1426 hdr->pref_address = hdr->code32_start;
1427 hdr->code32_start = bzimage_addr;
1430 status = exit_boot(boot_params, handle, is64);
1431 if (status != EFI_SUCCESS) {
1432 efi_printk(sys_table, "exit_boot() failed!\n");
1436 memset((char *)gdt->address, 0x0, gdt->size);
1437 desc = (struct desc_struct *)gdt->address;
1439 /* The first GDT is a dummy and the second is unused. */
1442 desc->limit0 = 0xffff;
1443 desc->base0 = 0x0000;
1444 desc->base1 = 0x0000;
1445 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1446 desc->s = DESC_TYPE_CODE_DATA;
1452 desc->d = SEG_OP_SIZE_32BIT;
1453 desc->g = SEG_GRANULARITY_4KB;
1457 desc->limit0 = 0xffff;
1458 desc->base0 = 0x0000;
1459 desc->base1 = 0x0000;
1460 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
1461 desc->s = DESC_TYPE_CODE_DATA;
1467 desc->d = SEG_OP_SIZE_32BIT;
1468 desc->g = SEG_GRANULARITY_4KB;
1471 #ifdef CONFIG_X86_64
1472 /* Task segment value */
1474 desc->limit0 = 0x0000;
1475 desc->base0 = 0x0000;
1476 desc->base1 = 0x0000;
1477 desc->type = SEG_TYPE_TSS;
1485 desc->g = SEG_GRANULARITY_4KB;
1487 #endif /* CONFIG_X86_64 */
1489 asm volatile("cli");
1490 asm volatile ("lgdt %0" : : "m" (*gdt));
1494 efi_printk(sys_table, "efi_main() failed!\n");