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 struct efi_config
*efi_early
;
24 #define BOOT_SERVICES(bits) \
25 static void setup_boot_services##bits(struct efi_config *c) \
27 efi_system_table_##bits##_t *table; \
28 efi_boot_services_##bits##_t *bt; \
30 table = (typeof(table))sys_table; \
32 c->text_output = table->con_out; \
34 bt = (typeof(bt))(unsigned long)(table->boottime); \
36 c->allocate_pool = bt->allocate_pool; \
37 c->allocate_pages = bt->allocate_pages; \
38 c->get_memory_map = bt->get_memory_map; \
39 c->free_pool = bt->free_pool; \
40 c->free_pages = bt->free_pages; \
41 c->locate_handle = bt->locate_handle; \
42 c->handle_protocol = bt->handle_protocol; \
43 c->exit_boot_services = bt->exit_boot_services; \
48 void efi_char16_printk(efi_system_table_t
*, efi_char16_t
*);
51 __file_size32(void *__fh
, efi_char16_t
*filename_16
,
52 void **handle
, u64
*file_sz
)
54 efi_file_handle_32_t
*h
, *fh
= __fh
;
55 efi_file_info_t
*info
;
57 efi_guid_t info_guid
= EFI_FILE_INFO_ID
;
60 status
= efi_early
->call((unsigned long)fh
->open
, fh
, &h
, filename_16
,
61 EFI_FILE_MODE_READ
, (u64
)0);
62 if (status
!= EFI_SUCCESS
) {
63 efi_printk(sys_table
, "Failed to open file: ");
64 efi_char16_printk(sys_table
, filename_16
);
65 efi_printk(sys_table
, "\n");
72 status
= efi_early
->call((unsigned long)h
->get_info
, h
, &info_guid
,
74 if (status
!= EFI_BUFFER_TOO_SMALL
) {
75 efi_printk(sys_table
, "Failed to get file info size\n");
80 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
,
81 info_sz
, (void **)&info
);
82 if (status
!= EFI_SUCCESS
) {
83 efi_printk(sys_table
, "Failed to alloc mem for file info\n");
87 status
= efi_early
->call((unsigned long)h
->get_info
, h
, &info_guid
,
89 if (status
== EFI_BUFFER_TOO_SMALL
) {
90 efi_call_early(free_pool
, info
);
94 *file_sz
= info
->file_size
;
95 efi_call_early(free_pool
, info
);
97 if (status
!= EFI_SUCCESS
)
98 efi_printk(sys_table
, "Failed to get initrd info\n");
104 __file_size64(void *__fh
, efi_char16_t
*filename_16
,
105 void **handle
, u64
*file_sz
)
107 efi_file_handle_64_t
*h
, *fh
= __fh
;
108 efi_file_info_t
*info
;
110 efi_guid_t info_guid
= EFI_FILE_INFO_ID
;
113 status
= efi_early
->call((unsigned long)fh
->open
, fh
, &h
, filename_16
,
114 EFI_FILE_MODE_READ
, (u64
)0);
115 if (status
!= EFI_SUCCESS
) {
116 efi_printk(sys_table
, "Failed to open file: ");
117 efi_char16_printk(sys_table
, filename_16
);
118 efi_printk(sys_table
, "\n");
125 status
= efi_early
->call((unsigned long)h
->get_info
, h
, &info_guid
,
127 if (status
!= EFI_BUFFER_TOO_SMALL
) {
128 efi_printk(sys_table
, "Failed to get file info size\n");
133 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
,
134 info_sz
, (void **)&info
);
135 if (status
!= EFI_SUCCESS
) {
136 efi_printk(sys_table
, "Failed to alloc mem for file info\n");
140 status
= efi_early
->call((unsigned long)h
->get_info
, h
, &info_guid
,
142 if (status
== EFI_BUFFER_TOO_SMALL
) {
143 efi_call_early(free_pool
, info
);
147 *file_sz
= info
->file_size
;
148 efi_call_early(free_pool
, info
);
150 if (status
!= EFI_SUCCESS
)
151 efi_printk(sys_table
, "Failed to get initrd info\n");
156 efi_file_size(efi_system_table_t
*sys_table
, void *__fh
,
157 efi_char16_t
*filename_16
, void **handle
, u64
*file_sz
)
160 return __file_size64(__fh
, filename_16
, handle
, file_sz
);
162 return __file_size32(__fh
, filename_16
, handle
, file_sz
);
166 efi_file_read(void *handle
, unsigned long *size
, void *addr
)
170 if (efi_early
->is64
) {
171 efi_file_handle_64_t
*fh
= handle
;
173 func
= (unsigned long)fh
->read
;
174 return efi_early
->call(func
, handle
, size
, addr
);
176 efi_file_handle_32_t
*fh
= handle
;
178 func
= (unsigned long)fh
->read
;
179 return efi_early
->call(func
, handle
, size
, addr
);
183 efi_status_t
efi_file_close(void *handle
)
185 if (efi_early
->is64
) {
186 efi_file_handle_64_t
*fh
= handle
;
188 return efi_early
->call((unsigned long)fh
->close
, handle
);
190 efi_file_handle_32_t
*fh
= handle
;
192 return efi_early
->call((unsigned long)fh
->close
, handle
);
196 static inline efi_status_t
__open_volume32(void *__image
, void **__fh
)
198 efi_file_io_interface_t
*io
;
199 efi_loaded_image_32_t
*image
= __image
;
200 efi_file_handle_32_t
*fh
;
201 efi_guid_t fs_proto
= EFI_FILE_SYSTEM_GUID
;
203 void *handle
= (void *)(unsigned long)image
->device_handle
;
206 status
= efi_call_early(handle_protocol
, handle
,
207 &fs_proto
, (void **)&io
);
208 if (status
!= EFI_SUCCESS
) {
209 efi_printk(sys_table
, "Failed to handle fs_proto\n");
213 func
= (unsigned long)io
->open_volume
;
214 status
= efi_early
->call(func
, io
, &fh
);
215 if (status
!= EFI_SUCCESS
)
216 efi_printk(sys_table
, "Failed to open volume\n");
222 static inline efi_status_t
__open_volume64(void *__image
, void **__fh
)
224 efi_file_io_interface_t
*io
;
225 efi_loaded_image_64_t
*image
= __image
;
226 efi_file_handle_64_t
*fh
;
227 efi_guid_t fs_proto
= EFI_FILE_SYSTEM_GUID
;
229 void *handle
= (void *)(unsigned long)image
->device_handle
;
232 status
= efi_call_early(handle_protocol
, handle
,
233 &fs_proto
, (void **)&io
);
234 if (status
!= EFI_SUCCESS
) {
235 efi_printk(sys_table
, "Failed to handle fs_proto\n");
239 func
= (unsigned long)io
->open_volume
;
240 status
= efi_early
->call(func
, io
, &fh
);
241 if (status
!= EFI_SUCCESS
)
242 efi_printk(sys_table
, "Failed to open volume\n");
249 efi_open_volume(efi_system_table_t
*sys_table
, void *__image
, void **__fh
)
252 return __open_volume64(__image
, __fh
);
254 return __open_volume32(__image
, __fh
);
257 void efi_char16_printk(efi_system_table_t
*table
, efi_char16_t
*str
)
259 unsigned long output_string
;
262 if (efi_early
->is64
) {
263 struct efi_simple_text_output_protocol_64
*out
;
266 offset
= offsetof(typeof(*out
), output_string
);
267 output_string
= efi_early
->text_output
+ offset
;
268 func
= (u64
*)output_string
;
270 efi_early
->call(*func
, efi_early
->text_output
, str
);
272 struct efi_simple_text_output_protocol_32
*out
;
275 offset
= offsetof(typeof(*out
), output_string
);
276 output_string
= efi_early
->text_output
+ offset
;
277 func
= (u32
*)output_string
;
279 efi_early
->call(*func
, efi_early
->text_output
, str
);
283 static void find_bits(unsigned long mask
, u8
*pos
, u8
*size
)
291 while (!(mask
& 0x1)) {
307 __setup_efi_pci32(efi_pci_io_protocol_32
*pci
, struct pci_setup_rom
**__rom
)
309 struct pci_setup_rom
*rom
= NULL
;
314 status
= efi_early
->call(pci
->attributes
, pci
,
315 EfiPciIoAttributeOperationGet
, 0, 0,
317 if (status
!= EFI_SUCCESS
)
320 if (!pci
->romimage
|| !pci
->romsize
)
321 return EFI_INVALID_PARAMETER
;
323 size
= pci
->romsize
+ sizeof(*rom
);
325 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
, size
, &rom
);
326 if (status
!= EFI_SUCCESS
)
329 memset(rom
, 0, sizeof(*rom
));
331 rom
->data
.type
= SETUP_PCI
;
332 rom
->data
.len
= size
- sizeof(struct setup_data
);
334 rom
->pcilen
= pci
->romsize
;
337 status
= efi_early
->call(pci
->pci
.read
, pci
, EfiPciIoWidthUint16
,
338 PCI_VENDOR_ID
, 1, &(rom
->vendor
));
340 if (status
!= EFI_SUCCESS
)
343 status
= efi_early
->call(pci
->pci
.read
, pci
, EfiPciIoWidthUint16
,
344 PCI_DEVICE_ID
, 1, &(rom
->devid
));
346 if (status
!= EFI_SUCCESS
)
349 status
= efi_early
->call(pci
->get_location
, pci
, &(rom
->segment
),
350 &(rom
->bus
), &(rom
->device
), &(rom
->function
));
352 if (status
!= EFI_SUCCESS
)
355 memcpy(rom
->romdata
, pci
->romimage
, pci
->romsize
);
359 efi_call_early(free_pool
, rom
);
364 setup_efi_pci32(struct boot_params
*params
, void **pci_handle
,
367 efi_pci_io_protocol_32
*pci
= NULL
;
368 efi_guid_t pci_proto
= EFI_PCI_IO_PROTOCOL_GUID
;
369 u32
*handles
= (u32
*)(unsigned long)pci_handle
;
371 unsigned long nr_pci
;
372 struct setup_data
*data
;
375 data
= (struct setup_data
*)(unsigned long)params
->hdr
.setup_data
;
377 while (data
&& data
->next
)
378 data
= (struct setup_data
*)(unsigned long)data
->next
;
380 nr_pci
= size
/ sizeof(u32
);
381 for (i
= 0; i
< nr_pci
; i
++) {
382 struct pci_setup_rom
*rom
= NULL
;
385 status
= efi_call_early(handle_protocol
, h
,
386 &pci_proto
, (void **)&pci
);
388 if (status
!= EFI_SUCCESS
)
394 status
= __setup_efi_pci32(pci
, &rom
);
395 if (status
!= EFI_SUCCESS
)
399 data
->next
= (unsigned long)rom
;
401 params
->hdr
.setup_data
= (unsigned long)rom
;
403 data
= (struct setup_data
*)rom
;
411 __setup_efi_pci64(efi_pci_io_protocol_64
*pci
, struct pci_setup_rom
**__rom
)
413 struct pci_setup_rom
*rom
;
418 status
= efi_early
->call(pci
->attributes
, pci
,
419 EfiPciIoAttributeOperationGet
, 0,
421 if (status
!= EFI_SUCCESS
)
424 if (!pci
->romimage
|| !pci
->romsize
)
425 return EFI_INVALID_PARAMETER
;
427 size
= pci
->romsize
+ sizeof(*rom
);
429 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
, size
, &rom
);
430 if (status
!= EFI_SUCCESS
)
433 rom
->data
.type
= SETUP_PCI
;
434 rom
->data
.len
= size
- sizeof(struct setup_data
);
436 rom
->pcilen
= pci
->romsize
;
439 status
= efi_early
->call(pci
->pci
.read
, pci
, EfiPciIoWidthUint16
,
440 PCI_VENDOR_ID
, 1, &(rom
->vendor
));
442 if (status
!= EFI_SUCCESS
)
445 status
= efi_early
->call(pci
->pci
.read
, pci
, EfiPciIoWidthUint16
,
446 PCI_DEVICE_ID
, 1, &(rom
->devid
));
448 if (status
!= EFI_SUCCESS
)
451 status
= efi_early
->call(pci
->get_location
, pci
, &(rom
->segment
),
452 &(rom
->bus
), &(rom
->device
), &(rom
->function
));
454 if (status
!= EFI_SUCCESS
)
457 memcpy(rom
->romdata
, pci
->romimage
, pci
->romsize
);
461 efi_call_early(free_pool
, rom
);
467 setup_efi_pci64(struct boot_params
*params
, void **pci_handle
,
470 efi_pci_io_protocol_64
*pci
= NULL
;
471 efi_guid_t pci_proto
= EFI_PCI_IO_PROTOCOL_GUID
;
472 u64
*handles
= (u64
*)(unsigned long)pci_handle
;
474 unsigned long nr_pci
;
475 struct setup_data
*data
;
478 data
= (struct setup_data
*)(unsigned long)params
->hdr
.setup_data
;
480 while (data
&& data
->next
)
481 data
= (struct setup_data
*)(unsigned long)data
->next
;
483 nr_pci
= size
/ sizeof(u64
);
484 for (i
= 0; i
< nr_pci
; i
++) {
485 struct pci_setup_rom
*rom
= NULL
;
488 status
= efi_call_early(handle_protocol
, h
,
489 &pci_proto
, (void **)&pci
);
491 if (status
!= EFI_SUCCESS
)
497 status
= __setup_efi_pci64(pci
, &rom
);
498 if (status
!= EFI_SUCCESS
)
502 data
->next
= (unsigned long)rom
;
504 params
->hdr
.setup_data
= (unsigned long)rom
;
506 data
= (struct setup_data
*)rom
;
513 static efi_status_t
setup_efi_pci(struct boot_params
*params
)
516 void **pci_handle
= NULL
;
517 efi_guid_t pci_proto
= EFI_PCI_IO_PROTOCOL_GUID
;
518 unsigned long size
= 0;
520 status
= efi_call_early(locate_handle
,
521 EFI_LOCATE_BY_PROTOCOL
,
522 &pci_proto
, NULL
, &size
, pci_handle
);
524 if (status
== EFI_BUFFER_TOO_SMALL
) {
525 status
= efi_call_early(allocate_pool
,
527 size
, (void **)&pci_handle
);
529 if (status
!= EFI_SUCCESS
)
532 status
= efi_call_early(locate_handle
,
533 EFI_LOCATE_BY_PROTOCOL
, &pci_proto
,
534 NULL
, &size
, pci_handle
);
537 if (status
!= EFI_SUCCESS
)
541 status
= setup_efi_pci64(params
, pci_handle
, size
);
543 status
= setup_efi_pci32(params
, pci_handle
, size
);
546 efi_call_early(free_pool
, pci_handle
);
551 setup_pixel_info(struct screen_info
*si
, u32 pixels_per_scan_line
,
552 struct efi_pixel_bitmask pixel_info
, int pixel_format
)
554 if (pixel_format
== PIXEL_RGB_RESERVED_8BIT_PER_COLOR
) {
556 si
->lfb_linelength
= pixels_per_scan_line
* 4;
565 } else if (pixel_format
== PIXEL_BGR_RESERVED_8BIT_PER_COLOR
) {
567 si
->lfb_linelength
= pixels_per_scan_line
* 4;
576 } else if (pixel_format
== PIXEL_BIT_MASK
) {
577 find_bits(pixel_info
.red_mask
, &si
->red_pos
, &si
->red_size
);
578 find_bits(pixel_info
.green_mask
, &si
->green_pos
,
580 find_bits(pixel_info
.blue_mask
, &si
->blue_pos
, &si
->blue_size
);
581 find_bits(pixel_info
.reserved_mask
, &si
->rsvd_pos
,
583 si
->lfb_depth
= si
->red_size
+ si
->green_size
+
584 si
->blue_size
+ si
->rsvd_size
;
585 si
->lfb_linelength
= (pixels_per_scan_line
* si
->lfb_depth
) / 8;
588 si
->lfb_linelength
= si
->lfb_width
/ 2;
601 __gop_query32(struct efi_graphics_output_protocol_32
*gop32
,
602 struct efi_graphics_output_mode_info
**info
,
603 unsigned long *size
, u32
*fb_base
)
605 struct efi_graphics_output_protocol_mode_32
*mode
;
610 mode
= (struct efi_graphics_output_protocol_mode_32
*)m
;
612 status
= efi_early
->call(gop32
->query_mode
, gop32
,
613 mode
->mode
, size
, info
);
614 if (status
!= EFI_SUCCESS
)
617 *fb_base
= mode
->frame_buffer_base
;
622 setup_gop32(struct screen_info
*si
, efi_guid_t
*proto
,
623 unsigned long size
, void **gop_handle
)
625 struct efi_graphics_output_protocol_32
*gop32
, *first_gop
;
626 unsigned long nr_gops
;
628 u32 pixels_per_scan_line
;
630 struct efi_pixel_bitmask pixel_info
;
633 u32
*handles
= (u32
*)(unsigned long)gop_handle
;
639 nr_gops
= size
/ sizeof(u32
);
640 for (i
= 0; i
< nr_gops
; i
++) {
641 struct efi_graphics_output_mode_info
*info
= NULL
;
642 efi_guid_t conout_proto
= EFI_CONSOLE_OUT_DEVICE_GUID
;
643 bool conout_found
= false;
647 status
= efi_call_early(handle_protocol
, h
,
648 proto
, (void **)&gop32
);
649 if (status
!= EFI_SUCCESS
)
652 status
= efi_call_early(handle_protocol
, h
,
653 &conout_proto
, &dummy
);
654 if (status
== EFI_SUCCESS
)
657 status
= __gop_query32(gop32
, &info
, &size
, &fb_base
);
658 if (status
== EFI_SUCCESS
&& (!first_gop
|| conout_found
)) {
660 * Systems that use the UEFI Console Splitter may
661 * provide multiple GOP devices, not all of which are
662 * backed by real hardware. The workaround is to search
663 * for a GOP implementing the ConOut protocol, and if
664 * one isn't found, to just fall back to the first GOP.
666 width
= info
->horizontal_resolution
;
667 height
= info
->vertical_resolution
;
668 pixel_format
= info
->pixel_format
;
669 pixel_info
= info
->pixel_information
;
670 pixels_per_scan_line
= info
->pixels_per_scan_line
;
673 * Once we've found a GOP supporting ConOut,
674 * don't bother looking any further.
682 /* Did we find any GOPs? */
686 /* EFI framebuffer */
687 si
->orig_video_isVGA
= VIDEO_TYPE_EFI
;
689 si
->lfb_width
= width
;
690 si
->lfb_height
= height
;
691 si
->lfb_base
= fb_base
;
694 setup_pixel_info(si
, pixels_per_scan_line
, pixel_info
, pixel_format
);
696 si
->lfb_size
= si
->lfb_linelength
* si
->lfb_height
;
698 si
->capabilities
|= VIDEO_CAPABILITY_SKIP_QUIRKS
;
704 __gop_query64(struct efi_graphics_output_protocol_64
*gop64
,
705 struct efi_graphics_output_mode_info
**info
,
706 unsigned long *size
, u32
*fb_base
)
708 struct efi_graphics_output_protocol_mode_64
*mode
;
713 mode
= (struct efi_graphics_output_protocol_mode_64
*)m
;
715 status
= efi_early
->call(gop64
->query_mode
, gop64
,
716 mode
->mode
, size
, info
);
717 if (status
!= EFI_SUCCESS
)
720 *fb_base
= mode
->frame_buffer_base
;
725 setup_gop64(struct screen_info
*si
, efi_guid_t
*proto
,
726 unsigned long size
, void **gop_handle
)
728 struct efi_graphics_output_protocol_64
*gop64
, *first_gop
;
729 unsigned long nr_gops
;
731 u32 pixels_per_scan_line
;
733 struct efi_pixel_bitmask pixel_info
;
736 u64
*handles
= (u64
*)(unsigned long)gop_handle
;
742 nr_gops
= size
/ sizeof(u64
);
743 for (i
= 0; i
< nr_gops
; i
++) {
744 struct efi_graphics_output_mode_info
*info
= NULL
;
745 efi_guid_t conout_proto
= EFI_CONSOLE_OUT_DEVICE_GUID
;
746 bool conout_found
= false;
750 status
= efi_call_early(handle_protocol
, h
,
751 proto
, (void **)&gop64
);
752 if (status
!= EFI_SUCCESS
)
755 status
= efi_call_early(handle_protocol
, h
,
756 &conout_proto
, &dummy
);
757 if (status
== EFI_SUCCESS
)
760 status
= __gop_query64(gop64
, &info
, &size
, &fb_base
);
761 if (status
== EFI_SUCCESS
&& (!first_gop
|| conout_found
)) {
763 * Systems that use the UEFI Console Splitter may
764 * provide multiple GOP devices, not all of which are
765 * backed by real hardware. The workaround is to search
766 * for a GOP implementing the ConOut protocol, and if
767 * one isn't found, to just fall back to the first GOP.
769 width
= info
->horizontal_resolution
;
770 height
= info
->vertical_resolution
;
771 pixel_format
= info
->pixel_format
;
772 pixel_info
= info
->pixel_information
;
773 pixels_per_scan_line
= info
->pixels_per_scan_line
;
776 * Once we've found a GOP supporting ConOut,
777 * don't bother looking any further.
785 /* Did we find any GOPs? */
789 /* EFI framebuffer */
790 si
->orig_video_isVGA
= VIDEO_TYPE_EFI
;
792 si
->lfb_width
= width
;
793 si
->lfb_height
= height
;
794 si
->lfb_base
= fb_base
;
797 setup_pixel_info(si
, pixels_per_scan_line
, pixel_info
, pixel_format
);
799 si
->lfb_size
= si
->lfb_linelength
* si
->lfb_height
;
801 si
->capabilities
|= VIDEO_CAPABILITY_SKIP_QUIRKS
;
807 * See if we have Graphics Output Protocol
809 static efi_status_t
setup_gop(struct screen_info
*si
, efi_guid_t
*proto
,
813 void **gop_handle
= NULL
;
815 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
,
816 size
, (void **)&gop_handle
);
817 if (status
!= EFI_SUCCESS
)
820 status
= efi_call_early(locate_handle
,
821 EFI_LOCATE_BY_PROTOCOL
,
822 proto
, NULL
, &size
, gop_handle
);
823 if (status
!= EFI_SUCCESS
)
827 status
= setup_gop64(si
, proto
, size
, gop_handle
);
829 status
= setup_gop32(si
, proto
, size
, gop_handle
);
832 efi_call_early(free_pool
, gop_handle
);
837 setup_uga32(void **uga_handle
, unsigned long size
, u32
*width
, u32
*height
)
839 struct efi_uga_draw_protocol
*uga
= NULL
, *first_uga
;
840 efi_guid_t uga_proto
= EFI_UGA_PROTOCOL_GUID
;
841 unsigned long nr_ugas
;
842 u32
*handles
= (u32
*)uga_handle
;;
847 nr_ugas
= size
/ sizeof(u32
);
848 for (i
= 0; i
< nr_ugas
; i
++) {
849 efi_guid_t pciio_proto
= EFI_PCI_IO_PROTOCOL_GUID
;
850 u32 w
, h
, depth
, refresh
;
852 u32 handle
= handles
[i
];
854 status
= efi_call_early(handle_protocol
, handle
,
855 &uga_proto
, (void **)&uga
);
856 if (status
!= EFI_SUCCESS
)
859 efi_call_early(handle_protocol
, handle
, &pciio_proto
, &pciio
);
861 status
= efi_early
->call((unsigned long)uga
->get_mode
, uga
,
862 &w
, &h
, &depth
, &refresh
);
863 if (status
== EFI_SUCCESS
&& (!first_uga
|| pciio
)) {
868 * Once we've found a UGA supporting PCIIO,
869 * don't bother looking any further.
882 setup_uga64(void **uga_handle
, unsigned long size
, u32
*width
, u32
*height
)
884 struct efi_uga_draw_protocol
*uga
= NULL
, *first_uga
;
885 efi_guid_t uga_proto
= EFI_UGA_PROTOCOL_GUID
;
886 unsigned long nr_ugas
;
887 u64
*handles
= (u64
*)uga_handle
;;
892 nr_ugas
= size
/ sizeof(u64
);
893 for (i
= 0; i
< nr_ugas
; i
++) {
894 efi_guid_t pciio_proto
= EFI_PCI_IO_PROTOCOL_GUID
;
895 u32 w
, h
, depth
, refresh
;
897 u64 handle
= handles
[i
];
899 status
= efi_call_early(handle_protocol
, handle
,
900 &uga_proto
, (void **)&uga
);
901 if (status
!= EFI_SUCCESS
)
904 efi_call_early(handle_protocol
, handle
, &pciio_proto
, &pciio
);
906 status
= efi_early
->call((unsigned long)uga
->get_mode
, uga
,
907 &w
, &h
, &depth
, &refresh
);
908 if (status
== EFI_SUCCESS
&& (!first_uga
|| pciio
)) {
913 * Once we've found a UGA supporting PCIIO,
914 * don't bother looking any further.
927 * See if we have Universal Graphics Adapter (UGA) protocol
929 static efi_status_t
setup_uga(struct screen_info
*si
, efi_guid_t
*uga_proto
,
934 void **uga_handle
= NULL
;
936 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
,
937 size
, (void **)&uga_handle
);
938 if (status
!= EFI_SUCCESS
)
941 status
= efi_call_early(locate_handle
,
942 EFI_LOCATE_BY_PROTOCOL
,
943 uga_proto
, NULL
, &size
, uga_handle
);
944 if (status
!= EFI_SUCCESS
)
951 status
= setup_uga64(uga_handle
, size
, &width
, &height
);
953 status
= setup_uga32(uga_handle
, size
, &width
, &height
);
955 if (!width
&& !height
)
958 /* EFI framebuffer */
959 si
->orig_video_isVGA
= VIDEO_TYPE_EFI
;
962 si
->lfb_width
= width
;
963 si
->lfb_height
= height
;
975 efi_call_early(free_pool
, uga_handle
);
979 void setup_graphics(struct boot_params
*boot_params
)
981 efi_guid_t graphics_proto
= EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID
;
982 struct screen_info
*si
;
983 efi_guid_t uga_proto
= EFI_UGA_PROTOCOL_GUID
;
986 void **gop_handle
= NULL
;
987 void **uga_handle
= NULL
;
989 si
= &boot_params
->screen_info
;
990 memset(si
, 0, sizeof(*si
));
993 status
= efi_call_early(locate_handle
,
994 EFI_LOCATE_BY_PROTOCOL
,
995 &graphics_proto
, NULL
, &size
, gop_handle
);
996 if (status
== EFI_BUFFER_TOO_SMALL
)
997 status
= setup_gop(si
, &graphics_proto
, size
);
999 if (status
!= EFI_SUCCESS
) {
1001 status
= efi_call_early(locate_handle
,
1002 EFI_LOCATE_BY_PROTOCOL
,
1003 &uga_proto
, NULL
, &size
, uga_handle
);
1004 if (status
== EFI_BUFFER_TOO_SMALL
)
1005 setup_uga(si
, &uga_proto
, size
);
1010 * Because the x86 boot code expects to be passed a boot_params we
1011 * need to create one ourselves (usually the bootloader would create
1014 * The caller is responsible for filling out ->code32_start in the
1015 * returned boot_params.
1017 struct boot_params
*make_boot_params(struct efi_config
*c
)
1019 struct boot_params
*boot_params
;
1020 struct sys_desc_table
*sdt
;
1021 struct apm_bios_info
*bi
;
1022 struct setup_header
*hdr
;
1023 struct efi_info
*efi
;
1024 efi_loaded_image_t
*image
;
1025 void *options
, *handle
;
1026 efi_guid_t proto
= LOADED_IMAGE_PROTOCOL_GUID
;
1027 int options_size
= 0;
1028 efi_status_t status
;
1033 unsigned long ramdisk_addr
;
1034 unsigned long ramdisk_size
;
1035 unsigned long initrd_addr_max
;
1038 sys_table
= (efi_system_table_t
*)(unsigned long)efi_early
->table
;
1039 handle
= (void *)(unsigned long)efi_early
->image_handle
;
1041 /* Check if we were booted by the EFI firmware */
1042 if (sys_table
->hdr
.signature
!= EFI_SYSTEM_TABLE_SIGNATURE
)
1045 if (efi_early
->is64
)
1046 setup_boot_services64(efi_early
);
1048 setup_boot_services32(efi_early
);
1050 status
= efi_call_early(handle_protocol
, handle
,
1051 &proto
, (void *)&image
);
1052 if (status
!= EFI_SUCCESS
) {
1053 efi_printk(sys_table
, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
1057 status
= efi_low_alloc(sys_table
, 0x4000, 1,
1058 (unsigned long *)&boot_params
);
1059 if (status
!= EFI_SUCCESS
) {
1060 efi_printk(sys_table
, "Failed to alloc lowmem for boot params\n");
1064 memset(boot_params
, 0x0, 0x4000);
1066 hdr
= &boot_params
->hdr
;
1067 efi
= &boot_params
->efi_info
;
1068 bi
= &boot_params
->apm_bios_info
;
1069 sdt
= &boot_params
->sys_desc_table
;
1071 /* Copy the second sector to boot_params */
1072 memcpy(&hdr
->jump
, image
->image_base
+ 512, 512);
1075 * Fill out some of the header fields ourselves because the
1076 * EFI firmware loader doesn't load the first sector.
1078 hdr
->root_flags
= 1;
1079 hdr
->vid_mode
= 0xffff;
1080 hdr
->boot_flag
= 0xAA55;
1082 hdr
->type_of_loader
= 0x21;
1084 /* Convert unicode cmdline to ascii */
1085 cmdline_ptr
= efi_convert_cmdline(sys_table
, image
, &options_size
);
1088 hdr
->cmd_line_ptr
= (unsigned long)cmdline_ptr
;
1090 hdr
->ramdisk_image
= 0;
1091 hdr
->ramdisk_size
= 0;
1093 /* Clear APM BIOS info */
1094 memset(bi
, 0, sizeof(*bi
));
1096 memset(sdt
, 0, sizeof(*sdt
));
1098 if (hdr
->xloadflags
& XLF_CAN_BE_LOADED_ABOVE_4G
)
1099 initrd_addr_max
= -1UL;
1101 initrd_addr_max
= hdr
->initrd_addr_max
;
1103 status
= handle_cmdline_files(sys_table
, image
,
1104 (char *)(unsigned long)hdr
->cmd_line_ptr
,
1105 "initrd=", initrd_addr_max
,
1106 &ramdisk_addr
, &ramdisk_size
);
1107 if (status
!= EFI_SUCCESS
)
1109 hdr
->ramdisk_image
= ramdisk_addr
& 0xffffffff;
1110 hdr
->ramdisk_size
= ramdisk_size
& 0xffffffff;
1111 boot_params
->ext_ramdisk_image
= (u64
)ramdisk_addr
>> 32;
1112 boot_params
->ext_ramdisk_size
= (u64
)ramdisk_size
>> 32;
1116 efi_free(sys_table
, options_size
, hdr
->cmd_line_ptr
);
1118 efi_free(sys_table
, 0x4000, (unsigned long)boot_params
);
1122 static void add_e820ext(struct boot_params
*params
,
1123 struct setup_data
*e820ext
, u32 nr_entries
)
1125 struct setup_data
*data
;
1126 efi_status_t status
;
1129 e820ext
->type
= SETUP_E820_EXT
;
1130 e820ext
->len
= nr_entries
* sizeof(struct e820entry
);
1133 data
= (struct setup_data
*)(unsigned long)params
->hdr
.setup_data
;
1135 while (data
&& data
->next
)
1136 data
= (struct setup_data
*)(unsigned long)data
->next
;
1139 data
->next
= (unsigned long)e820ext
;
1141 params
->hdr
.setup_data
= (unsigned long)e820ext
;
1144 static efi_status_t
setup_e820(struct boot_params
*params
,
1145 struct setup_data
*e820ext
, u32 e820ext_size
)
1147 struct e820entry
*e820_map
= ¶ms
->e820_map
[0];
1148 struct efi_info
*efi
= ¶ms
->efi_info
;
1149 struct e820entry
*prev
= NULL
;
1155 nr_desc
= efi
->efi_memmap_size
/ efi
->efi_memdesc_size
;
1157 for (i
= 0; i
< nr_desc
; i
++) {
1158 efi_memory_desc_t
*d
;
1159 unsigned int e820_type
= 0;
1160 unsigned long m
= efi
->efi_memmap
;
1162 d
= (efi_memory_desc_t
*)(m
+ (i
* efi
->efi_memdesc_size
));
1164 case EFI_RESERVED_TYPE
:
1165 case EFI_RUNTIME_SERVICES_CODE
:
1166 case EFI_RUNTIME_SERVICES_DATA
:
1167 case EFI_MEMORY_MAPPED_IO
:
1168 case EFI_MEMORY_MAPPED_IO_PORT_SPACE
:
1170 e820_type
= E820_RESERVED
;
1173 case EFI_UNUSABLE_MEMORY
:
1174 e820_type
= E820_UNUSABLE
;
1177 case EFI_ACPI_RECLAIM_MEMORY
:
1178 e820_type
= E820_ACPI
;
1181 case EFI_LOADER_CODE
:
1182 case EFI_LOADER_DATA
:
1183 case EFI_BOOT_SERVICES_CODE
:
1184 case EFI_BOOT_SERVICES_DATA
:
1185 case EFI_CONVENTIONAL_MEMORY
:
1186 e820_type
= E820_RAM
;
1189 case EFI_ACPI_MEMORY_NVS
:
1190 e820_type
= E820_NVS
;
1197 /* Merge adjacent mappings */
1198 if (prev
&& prev
->type
== e820_type
&&
1199 (prev
->addr
+ prev
->size
) == d
->phys_addr
) {
1200 prev
->size
+= d
->num_pages
<< 12;
1204 if (nr_entries
== ARRAY_SIZE(params
->e820_map
)) {
1205 u32 need
= (nr_desc
- i
) * sizeof(struct e820entry
) +
1206 sizeof(struct setup_data
);
1208 if (!e820ext
|| e820ext_size
< need
)
1209 return EFI_BUFFER_TOO_SMALL
;
1211 /* boot_params map full, switch to e820 extended */
1212 e820_map
= (struct e820entry
*)e820ext
->data
;
1215 e820_map
->addr
= d
->phys_addr
;
1216 e820_map
->size
= d
->num_pages
<< PAGE_SHIFT
;
1217 e820_map
->type
= e820_type
;
1222 if (nr_entries
> ARRAY_SIZE(params
->e820_map
)) {
1223 u32 nr_e820ext
= nr_entries
- ARRAY_SIZE(params
->e820_map
);
1225 add_e820ext(params
, e820ext
, nr_e820ext
);
1226 nr_entries
-= nr_e820ext
;
1229 params
->e820_entries
= (u8
)nr_entries
;
1234 static efi_status_t
alloc_e820ext(u32 nr_desc
, struct setup_data
**e820ext
,
1237 efi_status_t status
;
1240 size
= sizeof(struct setup_data
) +
1241 sizeof(struct e820entry
) * nr_desc
;
1244 efi_call_early(free_pool
, *e820ext
);
1249 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
,
1250 size
, (void **)e820ext
);
1251 if (status
== EFI_SUCCESS
)
1252 *e820ext_size
= size
;
1257 static efi_status_t
exit_boot(struct boot_params
*boot_params
,
1258 void *handle
, bool is64
)
1260 struct efi_info
*efi
= &boot_params
->efi_info
;
1261 unsigned long map_sz
, key
, desc_size
;
1262 efi_memory_desc_t
*mem_map
;
1263 struct setup_data
*e820ext
;
1264 const char *signature
;
1266 __u32 nr_desc
, prev_nr_desc
;
1267 efi_status_t status
;
1269 bool called_exit
= false;
1278 status
= efi_get_memory_map(sys_table
, &mem_map
, &map_sz
, &desc_size
,
1279 &desc_version
, &key
);
1281 if (status
!= EFI_SUCCESS
)
1284 prev_nr_desc
= nr_desc
;
1285 nr_desc
= map_sz
/ desc_size
;
1286 if (nr_desc
> prev_nr_desc
&&
1287 nr_desc
> ARRAY_SIZE(boot_params
->e820_map
)) {
1288 u32 nr_e820ext
= nr_desc
- ARRAY_SIZE(boot_params
->e820_map
);
1290 status
= alloc_e820ext(nr_e820ext
, &e820ext
, &e820ext_size
);
1291 if (status
!= EFI_SUCCESS
)
1294 efi_call_early(free_pool
, mem_map
);
1295 goto get_map
; /* Allocated memory, get map again */
1298 signature
= is64
? EFI64_LOADER_SIGNATURE
: EFI32_LOADER_SIGNATURE
;
1299 memcpy(&efi
->efi_loader_signature
, signature
, sizeof(__u32
));
1301 efi
->efi_systab
= (unsigned long)sys_table
;
1302 efi
->efi_memdesc_size
= desc_size
;
1303 efi
->efi_memdesc_version
= desc_version
;
1304 efi
->efi_memmap
= (unsigned long)mem_map
;
1305 efi
->efi_memmap_size
= map_sz
;
1307 #ifdef CONFIG_X86_64
1308 efi
->efi_systab_hi
= (unsigned long)sys_table
>> 32;
1309 efi
->efi_memmap_hi
= (unsigned long)mem_map
>> 32;
1312 /* Might as well exit boot services now */
1313 status
= efi_call_early(exit_boot_services
, handle
, key
);
1314 if (status
!= EFI_SUCCESS
) {
1316 * ExitBootServices() will fail if any of the event
1317 * handlers change the memory map. In which case, we
1318 * must be prepared to retry, but only once so that
1319 * we're guaranteed to exit on repeated failures instead
1320 * of spinning forever.
1326 efi_call_early(free_pool
, mem_map
);
1331 boot_params
->alt_mem_k
= 32 * 1024;
1333 status
= setup_e820(boot_params
, e820ext
, e820ext_size
);
1334 if (status
!= EFI_SUCCESS
)
1340 efi_call_early(free_pool
, mem_map
);
1345 * On success we return a pointer to a boot_params structure, and NULL
1348 struct boot_params
*efi_main(struct efi_config
*c
,
1349 struct boot_params
*boot_params
)
1351 struct desc_ptr
*gdt
= NULL
;
1352 efi_loaded_image_t
*image
;
1353 struct setup_header
*hdr
= &boot_params
->hdr
;
1354 efi_status_t status
;
1355 struct desc_struct
*desc
;
1357 efi_system_table_t
*_table
;
1362 _table
= (efi_system_table_t
*)(unsigned long)efi_early
->table
;
1363 handle
= (void *)(unsigned long)efi_early
->image_handle
;
1364 is64
= efi_early
->is64
;
1368 /* Check if we were booted by the EFI firmware */
1369 if (sys_table
->hdr
.signature
!= EFI_SYSTEM_TABLE_SIGNATURE
)
1373 setup_boot_services64(efi_early
);
1375 setup_boot_services32(efi_early
);
1377 setup_graphics(boot_params
);
1379 status
= setup_efi_pci(boot_params
);
1380 if (status
!= EFI_SUCCESS
) {
1381 efi_printk(sys_table
, "setup_efi_pci() failed!\n");
1384 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
,
1385 sizeof(*gdt
), (void **)&gdt
);
1386 if (status
!= EFI_SUCCESS
) {
1387 efi_printk(sys_table
, "Failed to alloc mem for gdt structure\n");
1392 status
= efi_low_alloc(sys_table
, gdt
->size
, 8,
1393 (unsigned long *)&gdt
->address
);
1394 if (status
!= EFI_SUCCESS
) {
1395 efi_printk(sys_table
, "Failed to alloc mem for gdt\n");
1400 * If the kernel isn't already loaded at the preferred load
1401 * address, relocate it.
1403 if (hdr
->pref_address
!= hdr
->code32_start
) {
1404 unsigned long bzimage_addr
= hdr
->code32_start
;
1405 status
= efi_relocate_kernel(sys_table
, &bzimage_addr
,
1406 hdr
->init_size
, hdr
->init_size
,
1408 hdr
->kernel_alignment
);
1409 if (status
!= EFI_SUCCESS
) {
1410 efi_printk(sys_table
, "efi_relocate_kernel() failed!\n");
1414 hdr
->pref_address
= hdr
->code32_start
;
1415 hdr
->code32_start
= bzimage_addr
;
1418 status
= exit_boot(boot_params
, handle
, is64
);
1419 if (status
!= EFI_SUCCESS
) {
1420 efi_printk(sys_table
, "exit_boot() failed!\n");
1424 memset((char *)gdt
->address
, 0x0, gdt
->size
);
1425 desc
= (struct desc_struct
*)gdt
->address
;
1427 /* The first GDT is a dummy and the second is unused. */
1430 desc
->limit0
= 0xffff;
1431 desc
->base0
= 0x0000;
1432 desc
->base1
= 0x0000;
1433 desc
->type
= SEG_TYPE_CODE
| SEG_TYPE_EXEC_READ
;
1434 desc
->s
= DESC_TYPE_CODE_DATA
;
1440 desc
->d
= SEG_OP_SIZE_32BIT
;
1441 desc
->g
= SEG_GRANULARITY_4KB
;
1445 desc
->limit0
= 0xffff;
1446 desc
->base0
= 0x0000;
1447 desc
->base1
= 0x0000;
1448 desc
->type
= SEG_TYPE_DATA
| SEG_TYPE_READ_WRITE
;
1449 desc
->s
= DESC_TYPE_CODE_DATA
;
1455 desc
->d
= SEG_OP_SIZE_32BIT
;
1456 desc
->g
= SEG_GRANULARITY_4KB
;
1459 #ifdef CONFIG_X86_64
1460 /* Task segment value */
1462 desc
->limit0
= 0x0000;
1463 desc
->base0
= 0x0000;
1464 desc
->base1
= 0x0000;
1465 desc
->type
= SEG_TYPE_TSS
;
1473 desc
->g
= SEG_GRANULARITY_4KB
;
1475 #endif /* CONFIG_X86_64 */
1477 asm volatile("cli");
1478 asm volatile ("lgdt %0" : : "m" (*gdt
));
1482 efi_printk(sys_table
, "efi_main() failed!\n");