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 #include "../string.h"
19 static efi_system_table_t *sys_table;
21 static struct efi_config *efi_early;
23 __pure const struct efi_config *__efi_early(void)
28 #define BOOT_SERVICES(bits) \
29 static void setup_boot_services##bits(struct efi_config *c) \
31 efi_system_table_##bits##_t *table; \
32 efi_boot_services_##bits##_t *bt; \
34 table = (typeof(table))sys_table; \
36 c->text_output = table->con_out; \
38 bt = (typeof(bt))(unsigned long)(table->boottime); \
40 c->allocate_pool = bt->allocate_pool; \
41 c->allocate_pages = bt->allocate_pages; \
42 c->get_memory_map = bt->get_memory_map; \
43 c->free_pool = bt->free_pool; \
44 c->free_pages = bt->free_pages; \
45 c->locate_handle = bt->locate_handle; \
46 c->handle_protocol = bt->handle_protocol; \
47 c->exit_boot_services = bt->exit_boot_services; \
52 void efi_char16_printk(efi_system_table_t *, efi_char16_t *);
55 __file_size32(void *__fh, efi_char16_t *filename_16,
56 void **handle, u64 *file_sz)
58 efi_file_handle_32_t *h, *fh = __fh;
59 efi_file_info_t *info;
61 efi_guid_t info_guid = EFI_FILE_INFO_ID;
64 status = efi_early->call((unsigned long)fh->open, fh, &h, filename_16,
65 EFI_FILE_MODE_READ, (u64)0);
66 if (status != EFI_SUCCESS) {
67 efi_printk(sys_table, "Failed to open file: ");
68 efi_char16_printk(sys_table, filename_16);
69 efi_printk(sys_table, "\n");
76 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
78 if (status != EFI_BUFFER_TOO_SMALL) {
79 efi_printk(sys_table, "Failed to get file info size\n");
84 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
85 info_sz, (void **)&info);
86 if (status != EFI_SUCCESS) {
87 efi_printk(sys_table, "Failed to alloc mem for file info\n");
91 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
93 if (status == EFI_BUFFER_TOO_SMALL) {
94 efi_call_early(free_pool, info);
98 *file_sz = info->file_size;
99 efi_call_early(free_pool, info);
101 if (status != EFI_SUCCESS)
102 efi_printk(sys_table, "Failed to get initrd info\n");
108 __file_size64(void *__fh, efi_char16_t *filename_16,
109 void **handle, u64 *file_sz)
111 efi_file_handle_64_t *h, *fh = __fh;
112 efi_file_info_t *info;
114 efi_guid_t info_guid = EFI_FILE_INFO_ID;
117 status = efi_early->call((unsigned long)fh->open, fh, &h, filename_16,
118 EFI_FILE_MODE_READ, (u64)0);
119 if (status != EFI_SUCCESS) {
120 efi_printk(sys_table, "Failed to open file: ");
121 efi_char16_printk(sys_table, filename_16);
122 efi_printk(sys_table, "\n");
129 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
131 if (status != EFI_BUFFER_TOO_SMALL) {
132 efi_printk(sys_table, "Failed to get file info size\n");
137 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
138 info_sz, (void **)&info);
139 if (status != EFI_SUCCESS) {
140 efi_printk(sys_table, "Failed to alloc mem for file info\n");
144 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
146 if (status == EFI_BUFFER_TOO_SMALL) {
147 efi_call_early(free_pool, info);
151 *file_sz = info->file_size;
152 efi_call_early(free_pool, info);
154 if (status != EFI_SUCCESS)
155 efi_printk(sys_table, "Failed to get initrd info\n");
160 efi_file_size(efi_system_table_t *sys_table, void *__fh,
161 efi_char16_t *filename_16, void **handle, u64 *file_sz)
164 return __file_size64(__fh, filename_16, handle, file_sz);
166 return __file_size32(__fh, filename_16, handle, file_sz);
170 efi_file_read(void *handle, unsigned long *size, void *addr)
174 if (efi_early->is64) {
175 efi_file_handle_64_t *fh = handle;
177 func = (unsigned long)fh->read;
178 return efi_early->call(func, handle, size, addr);
180 efi_file_handle_32_t *fh = handle;
182 func = (unsigned long)fh->read;
183 return efi_early->call(func, handle, size, addr);
187 efi_status_t efi_file_close(void *handle)
189 if (efi_early->is64) {
190 efi_file_handle_64_t *fh = handle;
192 return efi_early->call((unsigned long)fh->close, handle);
194 efi_file_handle_32_t *fh = handle;
196 return efi_early->call((unsigned long)fh->close, handle);
200 static inline efi_status_t __open_volume32(void *__image, void **__fh)
202 efi_file_io_interface_t *io;
203 efi_loaded_image_32_t *image = __image;
204 efi_file_handle_32_t *fh;
205 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
207 void *handle = (void *)(unsigned long)image->device_handle;
210 status = efi_call_early(handle_protocol, handle,
211 &fs_proto, (void **)&io);
212 if (status != EFI_SUCCESS) {
213 efi_printk(sys_table, "Failed to handle fs_proto\n");
217 func = (unsigned long)io->open_volume;
218 status = efi_early->call(func, io, &fh);
219 if (status != EFI_SUCCESS)
220 efi_printk(sys_table, "Failed to open volume\n");
226 static inline efi_status_t __open_volume64(void *__image, void **__fh)
228 efi_file_io_interface_t *io;
229 efi_loaded_image_64_t *image = __image;
230 efi_file_handle_64_t *fh;
231 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
233 void *handle = (void *)(unsigned long)image->device_handle;
236 status = efi_call_early(handle_protocol, handle,
237 &fs_proto, (void **)&io);
238 if (status != EFI_SUCCESS) {
239 efi_printk(sys_table, "Failed to handle fs_proto\n");
243 func = (unsigned long)io->open_volume;
244 status = efi_early->call(func, io, &fh);
245 if (status != EFI_SUCCESS)
246 efi_printk(sys_table, "Failed to open volume\n");
253 efi_open_volume(efi_system_table_t *sys_table, void *__image, void **__fh)
256 return __open_volume64(__image, __fh);
258 return __open_volume32(__image, __fh);
261 void efi_char16_printk(efi_system_table_t *table, efi_char16_t *str)
263 unsigned long output_string;
266 if (efi_early->is64) {
267 struct efi_simple_text_output_protocol_64 *out;
270 offset = offsetof(typeof(*out), output_string);
271 output_string = efi_early->text_output + offset;
272 out = (typeof(out))(unsigned long)efi_early->text_output;
273 func = (u64 *)output_string;
275 efi_early->call(*func, out, str);
277 struct efi_simple_text_output_protocol_32 *out;
280 offset = offsetof(typeof(*out), output_string);
281 output_string = efi_early->text_output + offset;
282 out = (typeof(out))(unsigned long)efi_early->text_output;
283 func = (u32 *)output_string;
285 efi_early->call(*func, out, str);
289 static void find_bits(unsigned long mask, u8 *pos, u8 *size)
297 while (!(mask & 0x1)) {
313 __setup_efi_pci32(efi_pci_io_protocol_32 *pci, struct pci_setup_rom **__rom)
315 struct pci_setup_rom *rom = NULL;
320 status = efi_early->call(pci->attributes, pci,
321 EfiPciIoAttributeOperationGet, 0, 0,
323 if (status != EFI_SUCCESS)
326 if (!pci->romimage || !pci->romsize)
327 return EFI_INVALID_PARAMETER;
329 size = pci->romsize + sizeof(*rom);
331 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
332 if (status != EFI_SUCCESS) {
333 efi_printk(sys_table, "Failed to alloc mem for rom\n");
337 memset(rom, 0, sizeof(*rom));
339 rom->data.type = SETUP_PCI;
340 rom->data.len = size - sizeof(struct setup_data);
342 rom->pcilen = pci->romsize;
345 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
346 PCI_VENDOR_ID, 1, &(rom->vendor));
348 if (status != EFI_SUCCESS) {
349 efi_printk(sys_table, "Failed to read rom->vendor\n");
353 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
354 PCI_DEVICE_ID, 1, &(rom->devid));
356 if (status != EFI_SUCCESS) {
357 efi_printk(sys_table, "Failed to read rom->devid\n");
361 status = efi_early->call(pci->get_location, pci, &(rom->segment),
362 &(rom->bus), &(rom->device), &(rom->function));
364 if (status != EFI_SUCCESS)
367 memcpy(rom->romdata, pci->romimage, pci->romsize);
371 efi_call_early(free_pool, rom);
376 setup_efi_pci32(struct boot_params *params, void **pci_handle,
379 efi_pci_io_protocol_32 *pci = NULL;
380 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
381 u32 *handles = (u32 *)(unsigned long)pci_handle;
383 unsigned long nr_pci;
384 struct setup_data *data;
387 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
389 while (data && data->next)
390 data = (struct setup_data *)(unsigned long)data->next;
392 nr_pci = size / sizeof(u32);
393 for (i = 0; i < nr_pci; i++) {
394 struct pci_setup_rom *rom = NULL;
397 status = efi_call_early(handle_protocol, h,
398 &pci_proto, (void **)&pci);
400 if (status != EFI_SUCCESS)
406 status = __setup_efi_pci32(pci, &rom);
407 if (status != EFI_SUCCESS)
411 data->next = (unsigned long)rom;
413 params->hdr.setup_data = (unsigned long)rom;
415 data = (struct setup_data *)rom;
421 __setup_efi_pci64(efi_pci_io_protocol_64 *pci, struct pci_setup_rom **__rom)
423 struct pci_setup_rom *rom;
428 status = efi_early->call(pci->attributes, pci,
429 EfiPciIoAttributeOperationGet, 0,
431 if (status != EFI_SUCCESS)
434 if (!pci->romimage || !pci->romsize)
435 return EFI_INVALID_PARAMETER;
437 size = pci->romsize + sizeof(*rom);
439 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
440 if (status != EFI_SUCCESS) {
441 efi_printk(sys_table, "Failed to alloc mem for rom\n");
445 rom->data.type = SETUP_PCI;
446 rom->data.len = size - sizeof(struct setup_data);
448 rom->pcilen = pci->romsize;
451 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
452 PCI_VENDOR_ID, 1, &(rom->vendor));
454 if (status != EFI_SUCCESS) {
455 efi_printk(sys_table, "Failed to read rom->vendor\n");
459 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
460 PCI_DEVICE_ID, 1, &(rom->devid));
462 if (status != EFI_SUCCESS) {
463 efi_printk(sys_table, "Failed to read rom->devid\n");
467 status = efi_early->call(pci->get_location, pci, &(rom->segment),
468 &(rom->bus), &(rom->device), &(rom->function));
470 if (status != EFI_SUCCESS)
473 memcpy(rom->romdata, pci->romimage, pci->romsize);
477 efi_call_early(free_pool, rom);
483 setup_efi_pci64(struct boot_params *params, void **pci_handle,
486 efi_pci_io_protocol_64 *pci = NULL;
487 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
488 u64 *handles = (u64 *)(unsigned long)pci_handle;
490 unsigned long nr_pci;
491 struct setup_data *data;
494 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
496 while (data && data->next)
497 data = (struct setup_data *)(unsigned long)data->next;
499 nr_pci = size / sizeof(u64);
500 for (i = 0; i < nr_pci; i++) {
501 struct pci_setup_rom *rom = NULL;
504 status = efi_call_early(handle_protocol, h,
505 &pci_proto, (void **)&pci);
507 if (status != EFI_SUCCESS)
513 status = __setup_efi_pci64(pci, &rom);
514 if (status != EFI_SUCCESS)
518 data->next = (unsigned long)rom;
520 params->hdr.setup_data = (unsigned long)rom;
522 data = (struct setup_data *)rom;
528 * There's no way to return an informative status from this function,
529 * because any analysis (and printing of error messages) needs to be
530 * done directly at the EFI function call-site.
532 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
533 * just didn't find any PCI devices, but there's no way to tell outside
534 * the context of the call.
536 static void setup_efi_pci(struct boot_params *params)
539 void **pci_handle = NULL;
540 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
541 unsigned long size = 0;
543 status = efi_call_early(locate_handle,
544 EFI_LOCATE_BY_PROTOCOL,
545 &pci_proto, NULL, &size, pci_handle);
547 if (status == EFI_BUFFER_TOO_SMALL) {
548 status = efi_call_early(allocate_pool,
550 size, (void **)&pci_handle);
552 if (status != EFI_SUCCESS) {
553 efi_printk(sys_table, "Failed to alloc mem for pci_handle\n");
557 status = efi_call_early(locate_handle,
558 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
559 NULL, &size, pci_handle);
562 if (status != EFI_SUCCESS)
566 setup_efi_pci64(params, pci_handle, size);
568 setup_efi_pci32(params, pci_handle, size);
571 efi_call_early(free_pool, pci_handle);
575 setup_pixel_info(struct screen_info *si, u32 pixels_per_scan_line,
576 struct efi_pixel_bitmask pixel_info, int pixel_format)
578 if (pixel_format == PIXEL_RGB_RESERVED_8BIT_PER_COLOR) {
580 si->lfb_linelength = pixels_per_scan_line * 4;
589 } else if (pixel_format == PIXEL_BGR_RESERVED_8BIT_PER_COLOR) {
591 si->lfb_linelength = pixels_per_scan_line * 4;
600 } else if (pixel_format == PIXEL_BIT_MASK) {
601 find_bits(pixel_info.red_mask, &si->red_pos, &si->red_size);
602 find_bits(pixel_info.green_mask, &si->green_pos,
604 find_bits(pixel_info.blue_mask, &si->blue_pos, &si->blue_size);
605 find_bits(pixel_info.reserved_mask, &si->rsvd_pos,
607 si->lfb_depth = si->red_size + si->green_size +
608 si->blue_size + si->rsvd_size;
609 si->lfb_linelength = (pixels_per_scan_line * si->lfb_depth) / 8;
612 si->lfb_linelength = si->lfb_width / 2;
625 __gop_query32(struct efi_graphics_output_protocol_32 *gop32,
626 struct efi_graphics_output_mode_info **info,
627 unsigned long *size, u32 *fb_base)
629 struct efi_graphics_output_protocol_mode_32 *mode;
634 mode = (struct efi_graphics_output_protocol_mode_32 *)m;
636 status = efi_early->call(gop32->query_mode, gop32,
637 mode->mode, size, info);
638 if (status != EFI_SUCCESS)
641 *fb_base = mode->frame_buffer_base;
646 setup_gop32(struct screen_info *si, efi_guid_t *proto,
647 unsigned long size, void **gop_handle)
649 struct efi_graphics_output_protocol_32 *gop32, *first_gop;
650 unsigned long nr_gops;
652 u32 pixels_per_scan_line;
654 struct efi_pixel_bitmask pixel_info;
657 u32 *handles = (u32 *)(unsigned long)gop_handle;
663 nr_gops = size / sizeof(u32);
664 for (i = 0; i < nr_gops; i++) {
665 struct efi_graphics_output_mode_info *info = NULL;
666 efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
667 bool conout_found = false;
671 status = efi_call_early(handle_protocol, h,
672 proto, (void **)&gop32);
673 if (status != EFI_SUCCESS)
676 status = efi_call_early(handle_protocol, h,
677 &conout_proto, &dummy);
678 if (status == EFI_SUCCESS)
681 status = __gop_query32(gop32, &info, &size, &fb_base);
682 if (status == EFI_SUCCESS && (!first_gop || conout_found)) {
684 * Systems that use the UEFI Console Splitter may
685 * provide multiple GOP devices, not all of which are
686 * backed by real hardware. The workaround is to search
687 * for a GOP implementing the ConOut protocol, and if
688 * one isn't found, to just fall back to the first GOP.
690 width = info->horizontal_resolution;
691 height = info->vertical_resolution;
692 pixel_format = info->pixel_format;
693 pixel_info = info->pixel_information;
694 pixels_per_scan_line = info->pixels_per_scan_line;
697 * Once we've found a GOP supporting ConOut,
698 * don't bother looking any further.
706 /* Did we find any GOPs? */
710 /* EFI framebuffer */
711 si->orig_video_isVGA = VIDEO_TYPE_EFI;
713 si->lfb_width = width;
714 si->lfb_height = height;
715 si->lfb_base = fb_base;
718 setup_pixel_info(si, pixels_per_scan_line, pixel_info, pixel_format);
720 si->lfb_size = si->lfb_linelength * si->lfb_height;
722 si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
728 __gop_query64(struct efi_graphics_output_protocol_64 *gop64,
729 struct efi_graphics_output_mode_info **info,
730 unsigned long *size, u32 *fb_base)
732 struct efi_graphics_output_protocol_mode_64 *mode;
737 mode = (struct efi_graphics_output_protocol_mode_64 *)m;
739 status = efi_early->call(gop64->query_mode, gop64,
740 mode->mode, size, info);
741 if (status != EFI_SUCCESS)
744 *fb_base = mode->frame_buffer_base;
749 setup_gop64(struct screen_info *si, efi_guid_t *proto,
750 unsigned long size, void **gop_handle)
752 struct efi_graphics_output_protocol_64 *gop64, *first_gop;
753 unsigned long nr_gops;
755 u32 pixels_per_scan_line;
757 struct efi_pixel_bitmask pixel_info;
760 u64 *handles = (u64 *)(unsigned long)gop_handle;
766 nr_gops = size / sizeof(u64);
767 for (i = 0; i < nr_gops; i++) {
768 struct efi_graphics_output_mode_info *info = NULL;
769 efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
770 bool conout_found = false;
774 status = efi_call_early(handle_protocol, h,
775 proto, (void **)&gop64);
776 if (status != EFI_SUCCESS)
779 status = efi_call_early(handle_protocol, h,
780 &conout_proto, &dummy);
781 if (status == EFI_SUCCESS)
784 status = __gop_query64(gop64, &info, &size, &fb_base);
785 if (status == EFI_SUCCESS && (!first_gop || conout_found)) {
787 * Systems that use the UEFI Console Splitter may
788 * provide multiple GOP devices, not all of which are
789 * backed by real hardware. The workaround is to search
790 * for a GOP implementing the ConOut protocol, and if
791 * one isn't found, to just fall back to the first GOP.
793 width = info->horizontal_resolution;
794 height = info->vertical_resolution;
795 pixel_format = info->pixel_format;
796 pixel_info = info->pixel_information;
797 pixels_per_scan_line = info->pixels_per_scan_line;
800 * Once we've found a GOP supporting ConOut,
801 * don't bother looking any further.
809 /* Did we find any GOPs? */
813 /* EFI framebuffer */
814 si->orig_video_isVGA = VIDEO_TYPE_EFI;
816 si->lfb_width = width;
817 si->lfb_height = height;
818 si->lfb_base = fb_base;
821 setup_pixel_info(si, pixels_per_scan_line, pixel_info, pixel_format);
823 si->lfb_size = si->lfb_linelength * si->lfb_height;
825 si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
831 * See if we have Graphics Output Protocol
833 static efi_status_t setup_gop(struct screen_info *si, efi_guid_t *proto,
837 void **gop_handle = NULL;
839 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
840 size, (void **)&gop_handle);
841 if (status != EFI_SUCCESS)
844 status = efi_call_early(locate_handle,
845 EFI_LOCATE_BY_PROTOCOL,
846 proto, NULL, &size, gop_handle);
847 if (status != EFI_SUCCESS)
851 status = setup_gop64(si, proto, size, gop_handle);
853 status = setup_gop32(si, proto, size, gop_handle);
856 efi_call_early(free_pool, gop_handle);
861 setup_uga32(void **uga_handle, unsigned long size, u32 *width, u32 *height)
863 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
864 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
865 unsigned long nr_ugas;
866 u32 *handles = (u32 *)uga_handle;;
871 nr_ugas = size / sizeof(u32);
872 for (i = 0; i < nr_ugas; i++) {
873 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
874 u32 w, h, depth, refresh;
876 u32 handle = handles[i];
878 status = efi_call_early(handle_protocol, handle,
879 &uga_proto, (void **)&uga);
880 if (status != EFI_SUCCESS)
883 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
885 status = efi_early->call((unsigned long)uga->get_mode, uga,
886 &w, &h, &depth, &refresh);
887 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
892 * Once we've found a UGA supporting PCIIO,
893 * don't bother looking any further.
906 setup_uga64(void **uga_handle, unsigned long size, u32 *width, u32 *height)
908 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
909 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
910 unsigned long nr_ugas;
911 u64 *handles = (u64 *)uga_handle;;
916 nr_ugas = size / sizeof(u64);
917 for (i = 0; i < nr_ugas; i++) {
918 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
919 u32 w, h, depth, refresh;
921 u64 handle = handles[i];
923 status = efi_call_early(handle_protocol, handle,
924 &uga_proto, (void **)&uga);
925 if (status != EFI_SUCCESS)
928 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
930 status = efi_early->call((unsigned long)uga->get_mode, uga,
931 &w, &h, &depth, &refresh);
932 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
937 * Once we've found a UGA supporting PCIIO,
938 * don't bother looking any further.
951 * See if we have Universal Graphics Adapter (UGA) protocol
953 static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
958 void **uga_handle = NULL;
960 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
961 size, (void **)&uga_handle);
962 if (status != EFI_SUCCESS)
965 status = efi_call_early(locate_handle,
966 EFI_LOCATE_BY_PROTOCOL,
967 uga_proto, NULL, &size, uga_handle);
968 if (status != EFI_SUCCESS)
975 status = setup_uga64(uga_handle, size, &width, &height);
977 status = setup_uga32(uga_handle, size, &width, &height);
979 if (!width && !height)
982 /* EFI framebuffer */
983 si->orig_video_isVGA = VIDEO_TYPE_EFI;
986 si->lfb_width = width;
987 si->lfb_height = height;
999 efi_call_early(free_pool, uga_handle);
1003 void setup_graphics(struct boot_params *boot_params)
1005 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
1006 struct screen_info *si;
1007 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
1008 efi_status_t status;
1010 void **gop_handle = NULL;
1011 void **uga_handle = NULL;
1013 si = &boot_params->screen_info;
1014 memset(si, 0, sizeof(*si));
1017 status = efi_call_early(locate_handle,
1018 EFI_LOCATE_BY_PROTOCOL,
1019 &graphics_proto, NULL, &size, gop_handle);
1020 if (status == EFI_BUFFER_TOO_SMALL)
1021 status = setup_gop(si, &graphics_proto, size);
1023 if (status != EFI_SUCCESS) {
1025 status = efi_call_early(locate_handle,
1026 EFI_LOCATE_BY_PROTOCOL,
1027 &uga_proto, NULL, &size, uga_handle);
1028 if (status == EFI_BUFFER_TOO_SMALL)
1029 setup_uga(si, &uga_proto, size);
1034 * Because the x86 boot code expects to be passed a boot_params we
1035 * need to create one ourselves (usually the bootloader would create
1038 * The caller is responsible for filling out ->code32_start in the
1039 * returned boot_params.
1041 struct boot_params *make_boot_params(struct efi_config *c)
1043 struct boot_params *boot_params;
1044 struct sys_desc_table *sdt;
1045 struct apm_bios_info *bi;
1046 struct setup_header *hdr;
1047 struct efi_info *efi;
1048 efi_loaded_image_t *image;
1049 void *options, *handle;
1050 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
1051 int options_size = 0;
1052 efi_status_t status;
1057 unsigned long ramdisk_addr;
1058 unsigned long ramdisk_size;
1061 sys_table = (efi_system_table_t *)(unsigned long)efi_early->table;
1062 handle = (void *)(unsigned long)efi_early->image_handle;
1064 /* Check if we were booted by the EFI firmware */
1065 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
1068 if (efi_early->is64)
1069 setup_boot_services64(efi_early);
1071 setup_boot_services32(efi_early);
1073 status = efi_call_early(handle_protocol, handle,
1074 &proto, (void *)&image);
1075 if (status != EFI_SUCCESS) {
1076 efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
1080 status = efi_low_alloc(sys_table, 0x4000, 1,
1081 (unsigned long *)&boot_params);
1082 if (status != EFI_SUCCESS) {
1083 efi_printk(sys_table, "Failed to alloc lowmem for boot params\n");
1087 memset(boot_params, 0x0, 0x4000);
1089 hdr = &boot_params->hdr;
1090 efi = &boot_params->efi_info;
1091 bi = &boot_params->apm_bios_info;
1092 sdt = &boot_params->sys_desc_table;
1094 /* Copy the second sector to boot_params */
1095 memcpy(&hdr->jump, image->image_base + 512, 512);
1098 * Fill out some of the header fields ourselves because the
1099 * EFI firmware loader doesn't load the first sector.
1101 hdr->root_flags = 1;
1102 hdr->vid_mode = 0xffff;
1103 hdr->boot_flag = 0xAA55;
1105 hdr->type_of_loader = 0x21;
1107 /* Convert unicode cmdline to ascii */
1108 cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size);
1111 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
1112 /* Fill in upper bits of command line address, NOP on 32 bit */
1113 boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32;
1115 hdr->ramdisk_image = 0;
1116 hdr->ramdisk_size = 0;
1118 /* Clear APM BIOS info */
1119 memset(bi, 0, sizeof(*bi));
1121 memset(sdt, 0, sizeof(*sdt));
1123 status = efi_parse_options(cmdline_ptr);
1124 if (status != EFI_SUCCESS)
1127 status = handle_cmdline_files(sys_table, image,
1128 (char *)(unsigned long)hdr->cmd_line_ptr,
1129 "initrd=", hdr->initrd_addr_max,
1130 &ramdisk_addr, &ramdisk_size);
1132 if (status != EFI_SUCCESS &&
1133 hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) {
1134 efi_printk(sys_table, "Trying to load files to higher address\n");
1135 status = handle_cmdline_files(sys_table, image,
1136 (char *)(unsigned long)hdr->cmd_line_ptr,
1138 &ramdisk_addr, &ramdisk_size);
1141 if (status != EFI_SUCCESS)
1143 hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
1144 hdr->ramdisk_size = ramdisk_size & 0xffffffff;
1145 boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
1146 boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32;
1150 efi_free(sys_table, options_size, hdr->cmd_line_ptr);
1152 efi_free(sys_table, 0x4000, (unsigned long)boot_params);
1156 static void add_e820ext(struct boot_params *params,
1157 struct setup_data *e820ext, u32 nr_entries)
1159 struct setup_data *data;
1160 efi_status_t status;
1163 e820ext->type = SETUP_E820_EXT;
1164 e820ext->len = nr_entries * sizeof(struct e820entry);
1167 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
1169 while (data && data->next)
1170 data = (struct setup_data *)(unsigned long)data->next;
1173 data->next = (unsigned long)e820ext;
1175 params->hdr.setup_data = (unsigned long)e820ext;
1178 static efi_status_t setup_e820(struct boot_params *params,
1179 struct setup_data *e820ext, u32 e820ext_size)
1181 struct e820entry *e820_map = ¶ms->e820_map[0];
1182 struct efi_info *efi = ¶ms->efi_info;
1183 struct e820entry *prev = NULL;
1189 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
1191 for (i = 0; i < nr_desc; i++) {
1192 efi_memory_desc_t *d;
1193 unsigned int e820_type = 0;
1194 unsigned long m = efi->efi_memmap;
1196 d = (efi_memory_desc_t *)(m + (i * efi->efi_memdesc_size));
1198 case EFI_RESERVED_TYPE:
1199 case EFI_RUNTIME_SERVICES_CODE:
1200 case EFI_RUNTIME_SERVICES_DATA:
1201 case EFI_MEMORY_MAPPED_IO:
1202 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
1204 e820_type = E820_RESERVED;
1207 case EFI_UNUSABLE_MEMORY:
1208 e820_type = E820_UNUSABLE;
1211 case EFI_ACPI_RECLAIM_MEMORY:
1212 e820_type = E820_ACPI;
1215 case EFI_LOADER_CODE:
1216 case EFI_LOADER_DATA:
1217 case EFI_BOOT_SERVICES_CODE:
1218 case EFI_BOOT_SERVICES_DATA:
1219 case EFI_CONVENTIONAL_MEMORY:
1220 e820_type = E820_RAM;
1223 case EFI_ACPI_MEMORY_NVS:
1224 e820_type = E820_NVS;
1227 case EFI_PERSISTENT_MEMORY:
1228 e820_type = E820_PMEM;
1235 /* Merge adjacent mappings */
1236 if (prev && prev->type == e820_type &&
1237 (prev->addr + prev->size) == d->phys_addr) {
1238 prev->size += d->num_pages << 12;
1242 if (nr_entries == ARRAY_SIZE(params->e820_map)) {
1243 u32 need = (nr_desc - i) * sizeof(struct e820entry) +
1244 sizeof(struct setup_data);
1246 if (!e820ext || e820ext_size < need)
1247 return EFI_BUFFER_TOO_SMALL;
1249 /* boot_params map full, switch to e820 extended */
1250 e820_map = (struct e820entry *)e820ext->data;
1253 e820_map->addr = d->phys_addr;
1254 e820_map->size = d->num_pages << PAGE_SHIFT;
1255 e820_map->type = e820_type;
1260 if (nr_entries > ARRAY_SIZE(params->e820_map)) {
1261 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_map);
1263 add_e820ext(params, e820ext, nr_e820ext);
1264 nr_entries -= nr_e820ext;
1267 params->e820_entries = (u8)nr_entries;
1272 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
1275 efi_status_t status;
1278 size = sizeof(struct setup_data) +
1279 sizeof(struct e820entry) * nr_desc;
1282 efi_call_early(free_pool, *e820ext);
1287 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
1288 size, (void **)e820ext);
1289 if (status == EFI_SUCCESS)
1290 *e820ext_size = size;
1295 static efi_status_t exit_boot(struct boot_params *boot_params,
1296 void *handle, bool is64)
1298 struct efi_info *efi = &boot_params->efi_info;
1299 unsigned long map_sz, key, desc_size;
1300 efi_memory_desc_t *mem_map;
1301 struct setup_data *e820ext;
1302 const char *signature;
1304 __u32 nr_desc, prev_nr_desc;
1305 efi_status_t status;
1307 bool called_exit = false;
1316 status = efi_get_memory_map(sys_table, &mem_map, &map_sz, &desc_size,
1317 &desc_version, &key);
1319 if (status != EFI_SUCCESS)
1322 prev_nr_desc = nr_desc;
1323 nr_desc = map_sz / desc_size;
1324 if (nr_desc > prev_nr_desc &&
1325 nr_desc > ARRAY_SIZE(boot_params->e820_map)) {
1326 u32 nr_e820ext = nr_desc - ARRAY_SIZE(boot_params->e820_map);
1328 status = alloc_e820ext(nr_e820ext, &e820ext, &e820ext_size);
1329 if (status != EFI_SUCCESS)
1332 efi_call_early(free_pool, mem_map);
1333 goto get_map; /* Allocated memory, get map again */
1336 signature = is64 ? EFI64_LOADER_SIGNATURE : EFI32_LOADER_SIGNATURE;
1337 memcpy(&efi->efi_loader_signature, signature, sizeof(__u32));
1339 efi->efi_systab = (unsigned long)sys_table;
1340 efi->efi_memdesc_size = desc_size;
1341 efi->efi_memdesc_version = desc_version;
1342 efi->efi_memmap = (unsigned long)mem_map;
1343 efi->efi_memmap_size = map_sz;
1345 #ifdef CONFIG_X86_64
1346 efi->efi_systab_hi = (unsigned long)sys_table >> 32;
1347 efi->efi_memmap_hi = (unsigned long)mem_map >> 32;
1350 /* Might as well exit boot services now */
1351 status = efi_call_early(exit_boot_services, handle, key);
1352 if (status != EFI_SUCCESS) {
1354 * ExitBootServices() will fail if any of the event
1355 * handlers change the memory map. In which case, we
1356 * must be prepared to retry, but only once so that
1357 * we're guaranteed to exit on repeated failures instead
1358 * of spinning forever.
1364 efi_call_early(free_pool, mem_map);
1369 boot_params->alt_mem_k = 32 * 1024;
1371 status = setup_e820(boot_params, e820ext, e820ext_size);
1372 if (status != EFI_SUCCESS)
1378 efi_call_early(free_pool, mem_map);
1383 * On success we return a pointer to a boot_params structure, and NULL
1386 struct boot_params *efi_main(struct efi_config *c,
1387 struct boot_params *boot_params)
1389 struct desc_ptr *gdt = NULL;
1390 efi_loaded_image_t *image;
1391 struct setup_header *hdr = &boot_params->hdr;
1392 efi_status_t status;
1393 struct desc_struct *desc;
1395 efi_system_table_t *_table;
1400 _table = (efi_system_table_t *)(unsigned long)efi_early->table;
1401 handle = (void *)(unsigned long)efi_early->image_handle;
1402 is64 = efi_early->is64;
1406 /* Check if we were booted by the EFI firmware */
1407 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
1411 setup_boot_services64(efi_early);
1413 setup_boot_services32(efi_early);
1415 setup_graphics(boot_params);
1417 setup_efi_pci(boot_params);
1419 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
1420 sizeof(*gdt), (void **)&gdt);
1421 if (status != EFI_SUCCESS) {
1422 efi_printk(sys_table, "Failed to alloc mem for gdt structure\n");
1427 status = efi_low_alloc(sys_table, gdt->size, 8,
1428 (unsigned long *)&gdt->address);
1429 if (status != EFI_SUCCESS) {
1430 efi_printk(sys_table, "Failed to alloc mem for gdt\n");
1435 * If the kernel isn't already loaded at the preferred load
1436 * address, relocate it.
1438 if (hdr->pref_address != hdr->code32_start) {
1439 unsigned long bzimage_addr = hdr->code32_start;
1440 status = efi_relocate_kernel(sys_table, &bzimage_addr,
1441 hdr->init_size, hdr->init_size,
1443 hdr->kernel_alignment);
1444 if (status != EFI_SUCCESS) {
1445 efi_printk(sys_table, "efi_relocate_kernel() failed!\n");
1449 hdr->pref_address = hdr->code32_start;
1450 hdr->code32_start = bzimage_addr;
1453 status = exit_boot(boot_params, handle, is64);
1454 if (status != EFI_SUCCESS) {
1455 efi_printk(sys_table, "exit_boot() failed!\n");
1459 memset((char *)gdt->address, 0x0, gdt->size);
1460 desc = (struct desc_struct *)gdt->address;
1462 /* The first GDT is a dummy and the second is unused. */
1465 desc->limit0 = 0xffff;
1466 desc->base0 = 0x0000;
1467 desc->base1 = 0x0000;
1468 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1469 desc->s = DESC_TYPE_CODE_DATA;
1475 desc->d = SEG_OP_SIZE_32BIT;
1476 desc->g = SEG_GRANULARITY_4KB;
1480 desc->limit0 = 0xffff;
1481 desc->base0 = 0x0000;
1482 desc->base1 = 0x0000;
1483 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
1484 desc->s = DESC_TYPE_CODE_DATA;
1490 desc->d = SEG_OP_SIZE_32BIT;
1491 desc->g = SEG_GRANULARITY_4KB;
1494 #ifdef CONFIG_X86_64
1495 /* Task segment value */
1497 desc->limit0 = 0x0000;
1498 desc->base0 = 0x0000;
1499 desc->base1 = 0x0000;
1500 desc->type = SEG_TYPE_TSS;
1508 desc->g = SEG_GRANULARITY_4KB;
1510 #endif /* CONFIG_X86_64 */
1512 asm volatile("cli");
1513 asm volatile ("lgdt %0" : : "m" (*gdt));
1517 efi_printk(sys_table, "efi_main() failed!\n");