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 #include "../../../../drivers/firmware/efi/efi-stub-helper.c"
285 static void find_bits(unsigned long mask
, u8
*pos
, u8
*size
)
293 while (!(mask
& 0x1)) {
309 __setup_efi_pci32(efi_pci_io_protocol_32
*pci
, struct pci_setup_rom
**__rom
)
311 struct pci_setup_rom
*rom
= NULL
;
316 status
= efi_early
->call(pci
->attributes
, pci
,
317 EfiPciIoAttributeOperationGet
, 0, 0,
319 if (status
!= EFI_SUCCESS
)
322 if (!pci
->romimage
|| !pci
->romsize
)
323 return EFI_INVALID_PARAMETER
;
325 size
= pci
->romsize
+ sizeof(*rom
);
327 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
, size
, &rom
);
328 if (status
!= EFI_SUCCESS
)
331 memset(rom
, 0, sizeof(*rom
));
333 rom
->data
.type
= SETUP_PCI
;
334 rom
->data
.len
= size
- sizeof(struct setup_data
);
336 rom
->pcilen
= pci
->romsize
;
339 status
= efi_early
->call(pci
->pci
.read
, pci
, EfiPciIoWidthUint16
,
340 PCI_VENDOR_ID
, 1, &(rom
->vendor
));
342 if (status
!= EFI_SUCCESS
)
345 status
= efi_early
->call(pci
->pci
.read
, pci
, EfiPciIoWidthUint16
,
346 PCI_DEVICE_ID
, 1, &(rom
->devid
));
348 if (status
!= EFI_SUCCESS
)
351 status
= efi_early
->call(pci
->get_location
, pci
, &(rom
->segment
),
352 &(rom
->bus
), &(rom
->device
), &(rom
->function
));
354 if (status
!= EFI_SUCCESS
)
357 memcpy(rom
->romdata
, pci
->romimage
, pci
->romsize
);
361 efi_call_early(free_pool
, rom
);
366 setup_efi_pci32(struct boot_params
*params
, void **pci_handle
,
369 efi_pci_io_protocol_32
*pci
= NULL
;
370 efi_guid_t pci_proto
= EFI_PCI_IO_PROTOCOL_GUID
;
371 u32
*handles
= (u32
*)(unsigned long)pci_handle
;
373 unsigned long nr_pci
;
374 struct setup_data
*data
;
377 data
= (struct setup_data
*)(unsigned long)params
->hdr
.setup_data
;
379 while (data
&& data
->next
)
380 data
= (struct setup_data
*)(unsigned long)data
->next
;
382 nr_pci
= size
/ sizeof(u32
);
383 for (i
= 0; i
< nr_pci
; i
++) {
384 struct pci_setup_rom
*rom
= NULL
;
387 status
= efi_call_early(handle_protocol
, h
,
388 &pci_proto
, (void **)&pci
);
390 if (status
!= EFI_SUCCESS
)
396 status
= __setup_efi_pci32(pci
, &rom
);
397 if (status
!= EFI_SUCCESS
)
401 data
->next
= (unsigned long)rom
;
403 params
->hdr
.setup_data
= (unsigned long)rom
;
405 data
= (struct setup_data
*)rom
;
413 __setup_efi_pci64(efi_pci_io_protocol_64
*pci
, struct pci_setup_rom
**__rom
)
415 struct pci_setup_rom
*rom
;
420 status
= efi_early
->call(pci
->attributes
, pci
,
421 EfiPciIoAttributeOperationGet
, 0,
423 if (status
!= EFI_SUCCESS
)
426 if (!pci
->romimage
|| !pci
->romsize
)
427 return EFI_INVALID_PARAMETER
;
429 size
= pci
->romsize
+ sizeof(*rom
);
431 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
, size
, &rom
);
432 if (status
!= EFI_SUCCESS
)
435 rom
->data
.type
= SETUP_PCI
;
436 rom
->data
.len
= size
- sizeof(struct setup_data
);
438 rom
->pcilen
= pci
->romsize
;
441 status
= efi_early
->call(pci
->pci
.read
, pci
, EfiPciIoWidthUint16
,
442 PCI_VENDOR_ID
, 1, &(rom
->vendor
));
444 if (status
!= EFI_SUCCESS
)
447 status
= efi_early
->call(pci
->pci
.read
, pci
, EfiPciIoWidthUint16
,
448 PCI_DEVICE_ID
, 1, &(rom
->devid
));
450 if (status
!= EFI_SUCCESS
)
453 status
= efi_early
->call(pci
->get_location
, pci
, &(rom
->segment
),
454 &(rom
->bus
), &(rom
->device
), &(rom
->function
));
456 if (status
!= EFI_SUCCESS
)
459 memcpy(rom
->romdata
, pci
->romimage
, pci
->romsize
);
463 efi_call_early(free_pool
, rom
);
469 setup_efi_pci64(struct boot_params
*params
, void **pci_handle
,
472 efi_pci_io_protocol_64
*pci
= NULL
;
473 efi_guid_t pci_proto
= EFI_PCI_IO_PROTOCOL_GUID
;
474 u64
*handles
= (u64
*)(unsigned long)pci_handle
;
476 unsigned long nr_pci
;
477 struct setup_data
*data
;
480 data
= (struct setup_data
*)(unsigned long)params
->hdr
.setup_data
;
482 while (data
&& data
->next
)
483 data
= (struct setup_data
*)(unsigned long)data
->next
;
485 nr_pci
= size
/ sizeof(u64
);
486 for (i
= 0; i
< nr_pci
; i
++) {
487 struct pci_setup_rom
*rom
= NULL
;
490 status
= efi_call_early(handle_protocol
, h
,
491 &pci_proto
, (void **)&pci
);
493 if (status
!= EFI_SUCCESS
)
499 status
= __setup_efi_pci64(pci
, &rom
);
500 if (status
!= EFI_SUCCESS
)
504 data
->next
= (unsigned long)rom
;
506 params
->hdr
.setup_data
= (unsigned long)rom
;
508 data
= (struct setup_data
*)rom
;
515 static efi_status_t
setup_efi_pci(struct boot_params
*params
)
518 void **pci_handle
= NULL
;
519 efi_guid_t pci_proto
= EFI_PCI_IO_PROTOCOL_GUID
;
520 unsigned long size
= 0;
522 status
= efi_call_early(locate_handle
,
523 EFI_LOCATE_BY_PROTOCOL
,
524 &pci_proto
, NULL
, &size
, pci_handle
);
526 if (status
== EFI_BUFFER_TOO_SMALL
) {
527 status
= efi_call_early(allocate_pool
,
529 size
, (void **)&pci_handle
);
531 if (status
!= EFI_SUCCESS
)
534 status
= efi_call_early(locate_handle
,
535 EFI_LOCATE_BY_PROTOCOL
, &pci_proto
,
536 NULL
, &size
, pci_handle
);
539 if (status
!= EFI_SUCCESS
)
543 status
= setup_efi_pci64(params
, pci_handle
, size
);
545 status
= setup_efi_pci32(params
, pci_handle
, size
);
548 efi_call_early(free_pool
, pci_handle
);
553 setup_pixel_info(struct screen_info
*si
, u32 pixels_per_scan_line
,
554 struct efi_pixel_bitmask pixel_info
, int pixel_format
)
556 if (pixel_format
== PIXEL_RGB_RESERVED_8BIT_PER_COLOR
) {
558 si
->lfb_linelength
= pixels_per_scan_line
* 4;
567 } else if (pixel_format
== PIXEL_BGR_RESERVED_8BIT_PER_COLOR
) {
569 si
->lfb_linelength
= pixels_per_scan_line
* 4;
578 } else if (pixel_format
== PIXEL_BIT_MASK
) {
579 find_bits(pixel_info
.red_mask
, &si
->red_pos
, &si
->red_size
);
580 find_bits(pixel_info
.green_mask
, &si
->green_pos
,
582 find_bits(pixel_info
.blue_mask
, &si
->blue_pos
, &si
->blue_size
);
583 find_bits(pixel_info
.reserved_mask
, &si
->rsvd_pos
,
585 si
->lfb_depth
= si
->red_size
+ si
->green_size
+
586 si
->blue_size
+ si
->rsvd_size
;
587 si
->lfb_linelength
= (pixels_per_scan_line
* si
->lfb_depth
) / 8;
590 si
->lfb_linelength
= si
->lfb_width
/ 2;
603 __gop_query32(struct efi_graphics_output_protocol_32
*gop32
,
604 struct efi_graphics_output_mode_info
**info
,
605 unsigned long *size
, u32
*fb_base
)
607 struct efi_graphics_output_protocol_mode_32
*mode
;
612 mode
= (struct efi_graphics_output_protocol_mode_32
*)m
;
614 status
= efi_early
->call(gop32
->query_mode
, gop32
,
615 mode
->mode
, size
, info
);
616 if (status
!= EFI_SUCCESS
)
619 *fb_base
= mode
->frame_buffer_base
;
624 setup_gop32(struct screen_info
*si
, efi_guid_t
*proto
,
625 unsigned long size
, void **gop_handle
)
627 struct efi_graphics_output_protocol_32
*gop32
, *first_gop
;
628 unsigned long nr_gops
;
630 u32 pixels_per_scan_line
;
632 struct efi_pixel_bitmask pixel_info
;
635 u32
*handles
= (u32
*)(unsigned long)gop_handle
;
641 nr_gops
= size
/ sizeof(u32
);
642 for (i
= 0; i
< nr_gops
; i
++) {
643 struct efi_graphics_output_mode_info
*info
= NULL
;
644 efi_guid_t conout_proto
= EFI_CONSOLE_OUT_DEVICE_GUID
;
645 bool conout_found
= false;
649 status
= efi_call_early(handle_protocol
, h
,
650 proto
, (void **)&gop32
);
651 if (status
!= EFI_SUCCESS
)
654 status
= efi_call_early(handle_protocol
, h
,
655 &conout_proto
, &dummy
);
656 if (status
== EFI_SUCCESS
)
659 status
= __gop_query32(gop32
, &info
, &size
, &fb_base
);
660 if (status
== EFI_SUCCESS
&& (!first_gop
|| conout_found
)) {
662 * Systems that use the UEFI Console Splitter may
663 * provide multiple GOP devices, not all of which are
664 * backed by real hardware. The workaround is to search
665 * for a GOP implementing the ConOut protocol, and if
666 * one isn't found, to just fall back to the first GOP.
668 width
= info
->horizontal_resolution
;
669 height
= info
->vertical_resolution
;
670 pixel_format
= info
->pixel_format
;
671 pixel_info
= info
->pixel_information
;
672 pixels_per_scan_line
= info
->pixels_per_scan_line
;
675 * Once we've found a GOP supporting ConOut,
676 * don't bother looking any further.
684 /* Did we find any GOPs? */
688 /* EFI framebuffer */
689 si
->orig_video_isVGA
= VIDEO_TYPE_EFI
;
691 si
->lfb_width
= width
;
692 si
->lfb_height
= height
;
693 si
->lfb_base
= fb_base
;
696 setup_pixel_info(si
, pixels_per_scan_line
, pixel_info
, pixel_format
);
698 si
->lfb_size
= si
->lfb_linelength
* si
->lfb_height
;
700 si
->capabilities
|= VIDEO_CAPABILITY_SKIP_QUIRKS
;
706 __gop_query64(struct efi_graphics_output_protocol_64
*gop64
,
707 struct efi_graphics_output_mode_info
**info
,
708 unsigned long *size
, u32
*fb_base
)
710 struct efi_graphics_output_protocol_mode_64
*mode
;
715 mode
= (struct efi_graphics_output_protocol_mode_64
*)m
;
717 status
= efi_early
->call(gop64
->query_mode
, gop64
,
718 mode
->mode
, size
, info
);
719 if (status
!= EFI_SUCCESS
)
722 *fb_base
= mode
->frame_buffer_base
;
727 setup_gop64(struct screen_info
*si
, efi_guid_t
*proto
,
728 unsigned long size
, void **gop_handle
)
730 struct efi_graphics_output_protocol_64
*gop64
, *first_gop
;
731 unsigned long nr_gops
;
733 u32 pixels_per_scan_line
;
735 struct efi_pixel_bitmask pixel_info
;
738 u64
*handles
= (u64
*)(unsigned long)gop_handle
;
744 nr_gops
= size
/ sizeof(u64
);
745 for (i
= 0; i
< nr_gops
; i
++) {
746 struct efi_graphics_output_mode_info
*info
= NULL
;
747 efi_guid_t conout_proto
= EFI_CONSOLE_OUT_DEVICE_GUID
;
748 bool conout_found
= false;
752 status
= efi_call_early(handle_protocol
, h
,
753 proto
, (void **)&gop64
);
754 if (status
!= EFI_SUCCESS
)
757 status
= efi_call_early(handle_protocol
, h
,
758 &conout_proto
, &dummy
);
759 if (status
== EFI_SUCCESS
)
762 status
= __gop_query64(gop64
, &info
, &size
, &fb_base
);
763 if (status
== EFI_SUCCESS
&& (!first_gop
|| conout_found
)) {
765 * Systems that use the UEFI Console Splitter may
766 * provide multiple GOP devices, not all of which are
767 * backed by real hardware. The workaround is to search
768 * for a GOP implementing the ConOut protocol, and if
769 * one isn't found, to just fall back to the first GOP.
771 width
= info
->horizontal_resolution
;
772 height
= info
->vertical_resolution
;
773 pixel_format
= info
->pixel_format
;
774 pixel_info
= info
->pixel_information
;
775 pixels_per_scan_line
= info
->pixels_per_scan_line
;
778 * Once we've found a GOP supporting ConOut,
779 * don't bother looking any further.
787 /* Did we find any GOPs? */
791 /* EFI framebuffer */
792 si
->orig_video_isVGA
= VIDEO_TYPE_EFI
;
794 si
->lfb_width
= width
;
795 si
->lfb_height
= height
;
796 si
->lfb_base
= fb_base
;
799 setup_pixel_info(si
, pixels_per_scan_line
, pixel_info
, pixel_format
);
801 si
->lfb_size
= si
->lfb_linelength
* si
->lfb_height
;
803 si
->capabilities
|= VIDEO_CAPABILITY_SKIP_QUIRKS
;
809 * See if we have Graphics Output Protocol
811 static efi_status_t
setup_gop(struct screen_info
*si
, efi_guid_t
*proto
,
815 void **gop_handle
= NULL
;
817 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
,
818 size
, (void **)&gop_handle
);
819 if (status
!= EFI_SUCCESS
)
822 status
= efi_call_early(locate_handle
,
823 EFI_LOCATE_BY_PROTOCOL
,
824 proto
, NULL
, &size
, gop_handle
);
825 if (status
!= EFI_SUCCESS
)
829 status
= setup_gop64(si
, proto
, size
, gop_handle
);
831 status
= setup_gop32(si
, proto
, size
, gop_handle
);
834 efi_call_early(free_pool
, gop_handle
);
839 setup_uga32(void **uga_handle
, unsigned long size
, u32
*width
, u32
*height
)
841 struct efi_uga_draw_protocol
*uga
= NULL
, *first_uga
;
842 efi_guid_t uga_proto
= EFI_UGA_PROTOCOL_GUID
;
843 unsigned long nr_ugas
;
844 u32
*handles
= (u32
*)uga_handle
;;
849 nr_ugas
= size
/ sizeof(u32
);
850 for (i
= 0; i
< nr_ugas
; i
++) {
851 efi_guid_t pciio_proto
= EFI_PCI_IO_PROTOCOL_GUID
;
852 u32 w
, h
, depth
, refresh
;
854 u32 handle
= handles
[i
];
856 status
= efi_call_early(handle_protocol
, handle
,
857 &uga_proto
, (void **)&uga
);
858 if (status
!= EFI_SUCCESS
)
861 efi_call_early(handle_protocol
, handle
, &pciio_proto
, &pciio
);
863 status
= efi_early
->call((unsigned long)uga
->get_mode
, uga
,
864 &w
, &h
, &depth
, &refresh
);
865 if (status
== EFI_SUCCESS
&& (!first_uga
|| pciio
)) {
870 * Once we've found a UGA supporting PCIIO,
871 * don't bother looking any further.
884 setup_uga64(void **uga_handle
, unsigned long size
, u32
*width
, u32
*height
)
886 struct efi_uga_draw_protocol
*uga
= NULL
, *first_uga
;
887 efi_guid_t uga_proto
= EFI_UGA_PROTOCOL_GUID
;
888 unsigned long nr_ugas
;
889 u64
*handles
= (u64
*)uga_handle
;;
894 nr_ugas
= size
/ sizeof(u64
);
895 for (i
= 0; i
< nr_ugas
; i
++) {
896 efi_guid_t pciio_proto
= EFI_PCI_IO_PROTOCOL_GUID
;
897 u32 w
, h
, depth
, refresh
;
899 u64 handle
= handles
[i
];
901 status
= efi_call_early(handle_protocol
, handle
,
902 &uga_proto
, (void **)&uga
);
903 if (status
!= EFI_SUCCESS
)
906 efi_call_early(handle_protocol
, handle
, &pciio_proto
, &pciio
);
908 status
= efi_early
->call((unsigned long)uga
->get_mode
, uga
,
909 &w
, &h
, &depth
, &refresh
);
910 if (status
== EFI_SUCCESS
&& (!first_uga
|| pciio
)) {
915 * Once we've found a UGA supporting PCIIO,
916 * don't bother looking any further.
929 * See if we have Universal Graphics Adapter (UGA) protocol
931 static efi_status_t
setup_uga(struct screen_info
*si
, efi_guid_t
*uga_proto
,
936 void **uga_handle
= NULL
;
938 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
,
939 size
, (void **)&uga_handle
);
940 if (status
!= EFI_SUCCESS
)
943 status
= efi_call_early(locate_handle
,
944 EFI_LOCATE_BY_PROTOCOL
,
945 uga_proto
, NULL
, &size
, uga_handle
);
946 if (status
!= EFI_SUCCESS
)
953 status
= setup_uga64(uga_handle
, size
, &width
, &height
);
955 status
= setup_uga32(uga_handle
, size
, &width
, &height
);
957 if (!width
&& !height
)
960 /* EFI framebuffer */
961 si
->orig_video_isVGA
= VIDEO_TYPE_EFI
;
964 si
->lfb_width
= width
;
965 si
->lfb_height
= height
;
977 efi_call_early(free_pool
, uga_handle
);
981 void setup_graphics(struct boot_params
*boot_params
)
983 efi_guid_t graphics_proto
= EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID
;
984 struct screen_info
*si
;
985 efi_guid_t uga_proto
= EFI_UGA_PROTOCOL_GUID
;
988 void **gop_handle
= NULL
;
989 void **uga_handle
= NULL
;
991 si
= &boot_params
->screen_info
;
992 memset(si
, 0, sizeof(*si
));
995 status
= efi_call_early(locate_handle
,
996 EFI_LOCATE_BY_PROTOCOL
,
997 &graphics_proto
, NULL
, &size
, gop_handle
);
998 if (status
== EFI_BUFFER_TOO_SMALL
)
999 status
= setup_gop(si
, &graphics_proto
, size
);
1001 if (status
!= EFI_SUCCESS
) {
1003 status
= efi_call_early(locate_handle
,
1004 EFI_LOCATE_BY_PROTOCOL
,
1005 &uga_proto
, NULL
, &size
, uga_handle
);
1006 if (status
== EFI_BUFFER_TOO_SMALL
)
1007 setup_uga(si
, &uga_proto
, size
);
1012 * Because the x86 boot code expects to be passed a boot_params we
1013 * need to create one ourselves (usually the bootloader would create
1016 * The caller is responsible for filling out ->code32_start in the
1017 * returned boot_params.
1019 struct boot_params
*make_boot_params(struct efi_config
*c
)
1021 struct boot_params
*boot_params
;
1022 struct sys_desc_table
*sdt
;
1023 struct apm_bios_info
*bi
;
1024 struct setup_header
*hdr
;
1025 struct efi_info
*efi
;
1026 efi_loaded_image_t
*image
;
1027 void *options
, *handle
;
1028 efi_guid_t proto
= LOADED_IMAGE_PROTOCOL_GUID
;
1029 int options_size
= 0;
1030 efi_status_t status
;
1035 unsigned long ramdisk_addr
;
1036 unsigned long ramdisk_size
;
1037 unsigned long initrd_addr_max
;
1040 sys_table
= (efi_system_table_t
*)(unsigned long)efi_early
->table
;
1041 handle
= (void *)(unsigned long)efi_early
->image_handle
;
1043 /* Check if we were booted by the EFI firmware */
1044 if (sys_table
->hdr
.signature
!= EFI_SYSTEM_TABLE_SIGNATURE
)
1047 if (efi_early
->is64
)
1048 setup_boot_services64(efi_early
);
1050 setup_boot_services32(efi_early
);
1052 status
= efi_call_early(handle_protocol
, handle
,
1053 &proto
, (void *)&image
);
1054 if (status
!= EFI_SUCCESS
) {
1055 efi_printk(sys_table
, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
1059 status
= efi_low_alloc(sys_table
, 0x4000, 1,
1060 (unsigned long *)&boot_params
);
1061 if (status
!= EFI_SUCCESS
) {
1062 efi_printk(sys_table
, "Failed to alloc lowmem for boot params\n");
1066 memset(boot_params
, 0x0, 0x4000);
1068 hdr
= &boot_params
->hdr
;
1069 efi
= &boot_params
->efi_info
;
1070 bi
= &boot_params
->apm_bios_info
;
1071 sdt
= &boot_params
->sys_desc_table
;
1073 /* Copy the second sector to boot_params */
1074 memcpy(&hdr
->jump
, image
->image_base
+ 512, 512);
1077 * Fill out some of the header fields ourselves because the
1078 * EFI firmware loader doesn't load the first sector.
1080 hdr
->root_flags
= 1;
1081 hdr
->vid_mode
= 0xffff;
1082 hdr
->boot_flag
= 0xAA55;
1084 hdr
->type_of_loader
= 0x21;
1086 /* Convert unicode cmdline to ascii */
1087 cmdline_ptr
= efi_convert_cmdline(sys_table
, image
, &options_size
);
1090 hdr
->cmd_line_ptr
= (unsigned long)cmdline_ptr
;
1092 hdr
->ramdisk_image
= 0;
1093 hdr
->ramdisk_size
= 0;
1095 /* Clear APM BIOS info */
1096 memset(bi
, 0, sizeof(*bi
));
1098 memset(sdt
, 0, sizeof(*sdt
));
1100 if (hdr
->xloadflags
& XLF_CAN_BE_LOADED_ABOVE_4G
)
1101 initrd_addr_max
= -1UL;
1103 initrd_addr_max
= hdr
->initrd_addr_max
;
1105 status
= handle_cmdline_files(sys_table
, image
,
1106 (char *)(unsigned long)hdr
->cmd_line_ptr
,
1107 "initrd=", initrd_addr_max
,
1108 &ramdisk_addr
, &ramdisk_size
);
1109 if (status
!= EFI_SUCCESS
)
1111 hdr
->ramdisk_image
= ramdisk_addr
& 0xffffffff;
1112 hdr
->ramdisk_size
= ramdisk_size
& 0xffffffff;
1113 boot_params
->ext_ramdisk_image
= (u64
)ramdisk_addr
>> 32;
1114 boot_params
->ext_ramdisk_size
= (u64
)ramdisk_size
>> 32;
1118 efi_free(sys_table
, options_size
, hdr
->cmd_line_ptr
);
1120 efi_free(sys_table
, 0x4000, (unsigned long)boot_params
);
1124 static void add_e820ext(struct boot_params
*params
,
1125 struct setup_data
*e820ext
, u32 nr_entries
)
1127 struct setup_data
*data
;
1128 efi_status_t status
;
1131 e820ext
->type
= SETUP_E820_EXT
;
1132 e820ext
->len
= nr_entries
* sizeof(struct e820entry
);
1135 data
= (struct setup_data
*)(unsigned long)params
->hdr
.setup_data
;
1137 while (data
&& data
->next
)
1138 data
= (struct setup_data
*)(unsigned long)data
->next
;
1141 data
->next
= (unsigned long)e820ext
;
1143 params
->hdr
.setup_data
= (unsigned long)e820ext
;
1146 static efi_status_t
setup_e820(struct boot_params
*params
,
1147 struct setup_data
*e820ext
, u32 e820ext_size
)
1149 struct e820entry
*e820_map
= ¶ms
->e820_map
[0];
1150 struct efi_info
*efi
= ¶ms
->efi_info
;
1151 struct e820entry
*prev
= NULL
;
1157 nr_desc
= efi
->efi_memmap_size
/ efi
->efi_memdesc_size
;
1159 for (i
= 0; i
< nr_desc
; i
++) {
1160 efi_memory_desc_t
*d
;
1161 unsigned int e820_type
= 0;
1162 unsigned long m
= efi
->efi_memmap
;
1164 d
= (efi_memory_desc_t
*)(m
+ (i
* efi
->efi_memdesc_size
));
1166 case EFI_RESERVED_TYPE
:
1167 case EFI_RUNTIME_SERVICES_CODE
:
1168 case EFI_RUNTIME_SERVICES_DATA
:
1169 case EFI_MEMORY_MAPPED_IO
:
1170 case EFI_MEMORY_MAPPED_IO_PORT_SPACE
:
1172 e820_type
= E820_RESERVED
;
1175 case EFI_UNUSABLE_MEMORY
:
1176 e820_type
= E820_UNUSABLE
;
1179 case EFI_ACPI_RECLAIM_MEMORY
:
1180 e820_type
= E820_ACPI
;
1183 case EFI_LOADER_CODE
:
1184 case EFI_LOADER_DATA
:
1185 case EFI_BOOT_SERVICES_CODE
:
1186 case EFI_BOOT_SERVICES_DATA
:
1187 case EFI_CONVENTIONAL_MEMORY
:
1188 e820_type
= E820_RAM
;
1191 case EFI_ACPI_MEMORY_NVS
:
1192 e820_type
= E820_NVS
;
1199 /* Merge adjacent mappings */
1200 if (prev
&& prev
->type
== e820_type
&&
1201 (prev
->addr
+ prev
->size
) == d
->phys_addr
) {
1202 prev
->size
+= d
->num_pages
<< 12;
1206 if (nr_entries
== ARRAY_SIZE(params
->e820_map
)) {
1207 u32 need
= (nr_desc
- i
) * sizeof(struct e820entry
) +
1208 sizeof(struct setup_data
);
1210 if (!e820ext
|| e820ext_size
< need
)
1211 return EFI_BUFFER_TOO_SMALL
;
1213 /* boot_params map full, switch to e820 extended */
1214 e820_map
= (struct e820entry
*)e820ext
->data
;
1217 e820_map
->addr
= d
->phys_addr
;
1218 e820_map
->size
= d
->num_pages
<< PAGE_SHIFT
;
1219 e820_map
->type
= e820_type
;
1224 if (nr_entries
> ARRAY_SIZE(params
->e820_map
)) {
1225 u32 nr_e820ext
= nr_entries
- ARRAY_SIZE(params
->e820_map
);
1227 add_e820ext(params
, e820ext
, nr_e820ext
);
1228 nr_entries
-= nr_e820ext
;
1231 params
->e820_entries
= (u8
)nr_entries
;
1236 static efi_status_t
alloc_e820ext(u32 nr_desc
, struct setup_data
**e820ext
,
1239 efi_status_t status
;
1242 size
= sizeof(struct setup_data
) +
1243 sizeof(struct e820entry
) * nr_desc
;
1246 efi_call_early(free_pool
, *e820ext
);
1251 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
,
1252 size
, (void **)e820ext
);
1253 if (status
== EFI_SUCCESS
)
1254 *e820ext_size
= size
;
1259 static efi_status_t
exit_boot(struct boot_params
*boot_params
,
1260 void *handle
, bool is64
)
1262 struct efi_info
*efi
= &boot_params
->efi_info
;
1263 unsigned long map_sz
, key
, desc_size
;
1264 efi_memory_desc_t
*mem_map
;
1265 struct setup_data
*e820ext
;
1266 const char *signature
;
1268 __u32 nr_desc
, prev_nr_desc
;
1269 efi_status_t status
;
1271 bool called_exit
= false;
1280 status
= efi_get_memory_map(sys_table
, &mem_map
, &map_sz
, &desc_size
,
1281 &desc_version
, &key
);
1283 if (status
!= EFI_SUCCESS
)
1286 prev_nr_desc
= nr_desc
;
1287 nr_desc
= map_sz
/ desc_size
;
1288 if (nr_desc
> prev_nr_desc
&&
1289 nr_desc
> ARRAY_SIZE(boot_params
->e820_map
)) {
1290 u32 nr_e820ext
= nr_desc
- ARRAY_SIZE(boot_params
->e820_map
);
1292 status
= alloc_e820ext(nr_e820ext
, &e820ext
, &e820ext_size
);
1293 if (status
!= EFI_SUCCESS
)
1296 efi_call_early(free_pool
, mem_map
);
1297 goto get_map
; /* Allocated memory, get map again */
1300 signature
= is64
? EFI64_LOADER_SIGNATURE
: EFI32_LOADER_SIGNATURE
;
1301 memcpy(&efi
->efi_loader_signature
, signature
, sizeof(__u32
));
1303 efi
->efi_systab
= (unsigned long)sys_table
;
1304 efi
->efi_memdesc_size
= desc_size
;
1305 efi
->efi_memdesc_version
= desc_version
;
1306 efi
->efi_memmap
= (unsigned long)mem_map
;
1307 efi
->efi_memmap_size
= map_sz
;
1309 #ifdef CONFIG_X86_64
1310 efi
->efi_systab_hi
= (unsigned long)sys_table
>> 32;
1311 efi
->efi_memmap_hi
= (unsigned long)mem_map
>> 32;
1314 /* Might as well exit boot services now */
1315 status
= efi_call_early(exit_boot_services
, handle
, key
);
1316 if (status
!= EFI_SUCCESS
) {
1318 * ExitBootServices() will fail if any of the event
1319 * handlers change the memory map. In which case, we
1320 * must be prepared to retry, but only once so that
1321 * we're guaranteed to exit on repeated failures instead
1322 * of spinning forever.
1328 efi_call_early(free_pool
, mem_map
);
1333 boot_params
->alt_mem_k
= 32 * 1024;
1335 status
= setup_e820(boot_params
, e820ext
, e820ext_size
);
1336 if (status
!= EFI_SUCCESS
)
1342 efi_call_early(free_pool
, mem_map
);
1347 * On success we return a pointer to a boot_params structure, and NULL
1350 struct boot_params
*efi_main(struct efi_config
*c
,
1351 struct boot_params
*boot_params
)
1353 struct desc_ptr
*gdt
= NULL
;
1354 efi_loaded_image_t
*image
;
1355 struct setup_header
*hdr
= &boot_params
->hdr
;
1356 efi_status_t status
;
1357 struct desc_struct
*desc
;
1359 efi_system_table_t
*_table
;
1364 _table
= (efi_system_table_t
*)(unsigned long)efi_early
->table
;
1365 handle
= (void *)(unsigned long)efi_early
->image_handle
;
1366 is64
= efi_early
->is64
;
1370 /* Check if we were booted by the EFI firmware */
1371 if (sys_table
->hdr
.signature
!= EFI_SYSTEM_TABLE_SIGNATURE
)
1375 setup_boot_services64(efi_early
);
1377 setup_boot_services32(efi_early
);
1379 setup_graphics(boot_params
);
1381 setup_efi_pci(boot_params
);
1383 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
,
1384 sizeof(*gdt
), (void **)&gdt
);
1385 if (status
!= EFI_SUCCESS
) {
1386 efi_printk(sys_table
, "Failed to alloc mem for gdt structure\n");
1391 status
= efi_low_alloc(sys_table
, gdt
->size
, 8,
1392 (unsigned long *)&gdt
->address
);
1393 if (status
!= EFI_SUCCESS
) {
1394 efi_printk(sys_table
, "Failed to alloc mem for gdt\n");
1399 * If the kernel isn't already loaded at the preferred load
1400 * address, relocate it.
1402 if (hdr
->pref_address
!= hdr
->code32_start
) {
1403 unsigned long bzimage_addr
= hdr
->code32_start
;
1404 status
= efi_relocate_kernel(sys_table
, &bzimage_addr
,
1405 hdr
->init_size
, hdr
->init_size
,
1407 hdr
->kernel_alignment
);
1408 if (status
!= EFI_SUCCESS
)
1411 hdr
->pref_address
= hdr
->code32_start
;
1412 hdr
->code32_start
= bzimage_addr
;
1415 status
= exit_boot(boot_params
, handle
, is64
);
1416 if (status
!= EFI_SUCCESS
)
1419 memset((char *)gdt
->address
, 0x0, gdt
->size
);
1420 desc
= (struct desc_struct
*)gdt
->address
;
1422 /* The first GDT is a dummy and the second is unused. */
1425 desc
->limit0
= 0xffff;
1426 desc
->base0
= 0x0000;
1427 desc
->base1
= 0x0000;
1428 desc
->type
= SEG_TYPE_CODE
| SEG_TYPE_EXEC_READ
;
1429 desc
->s
= DESC_TYPE_CODE_DATA
;
1435 desc
->d
= SEG_OP_SIZE_32BIT
;
1436 desc
->g
= SEG_GRANULARITY_4KB
;
1440 desc
->limit0
= 0xffff;
1441 desc
->base0
= 0x0000;
1442 desc
->base1
= 0x0000;
1443 desc
->type
= SEG_TYPE_DATA
| SEG_TYPE_READ_WRITE
;
1444 desc
->s
= DESC_TYPE_CODE_DATA
;
1450 desc
->d
= SEG_OP_SIZE_32BIT
;
1451 desc
->g
= SEG_GRANULARITY_4KB
;
1454 #ifdef CONFIG_X86_64
1455 /* Task segment value */
1457 desc
->limit0
= 0x0000;
1458 desc
->base0
= 0x0000;
1459 desc
->base1
= 0x0000;
1460 desc
->type
= SEG_TYPE_TSS
;
1468 desc
->g
= SEG_GRANULARITY_4KB
;
1470 #endif /* CONFIG_X86_64 */
1472 asm volatile("cli");
1473 asm volatile ("lgdt %0" : : "m" (*gdt
));