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>
15 #include <asm/bootparam_utils.h>
17 #include "../string.h"
20 static efi_system_table_t
*sys_table
;
22 static struct efi_config
*efi_early
;
24 __pure
const struct efi_config
*__efi_early(void)
29 #define BOOT_SERVICES(bits) \
30 static void setup_boot_services##bits(struct efi_config *c) \
32 efi_system_table_##bits##_t *table; \
34 table = (typeof(table))sys_table; \
36 c->boot_services = table->boottime; \
37 c->text_output = table->con_out; \
42 void efi_char16_printk(efi_system_table_t
*, efi_char16_t
*);
45 __file_size32(void *__fh
, efi_char16_t
*filename_16
,
46 void **handle
, u64
*file_sz
)
48 efi_file_handle_32_t
*h
, *fh
= __fh
;
49 efi_file_info_t
*info
;
51 efi_guid_t info_guid
= EFI_FILE_INFO_ID
;
54 status
= efi_early
->call((unsigned long)fh
->open
, fh
, &h
, filename_16
,
55 EFI_FILE_MODE_READ
, (u64
)0);
56 if (status
!= EFI_SUCCESS
) {
57 efi_printk(sys_table
, "Failed to open file: ");
58 efi_char16_printk(sys_table
, filename_16
);
59 efi_printk(sys_table
, "\n");
66 status
= efi_early
->call((unsigned long)h
->get_info
, h
, &info_guid
,
68 if (status
!= EFI_BUFFER_TOO_SMALL
) {
69 efi_printk(sys_table
, "Failed to get file info size\n");
74 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
,
75 info_sz
, (void **)&info
);
76 if (status
!= EFI_SUCCESS
) {
77 efi_printk(sys_table
, "Failed to alloc mem for file info\n");
81 status
= efi_early
->call((unsigned long)h
->get_info
, h
, &info_guid
,
83 if (status
== EFI_BUFFER_TOO_SMALL
) {
84 efi_call_early(free_pool
, info
);
88 *file_sz
= info
->file_size
;
89 efi_call_early(free_pool
, info
);
91 if (status
!= EFI_SUCCESS
)
92 efi_printk(sys_table
, "Failed to get initrd info\n");
98 __file_size64(void *__fh
, efi_char16_t
*filename_16
,
99 void **handle
, u64
*file_sz
)
101 efi_file_handle_64_t
*h
, *fh
= __fh
;
102 efi_file_info_t
*info
;
104 efi_guid_t info_guid
= EFI_FILE_INFO_ID
;
107 status
= efi_early
->call((unsigned long)fh
->open
, fh
, &h
, filename_16
,
108 EFI_FILE_MODE_READ
, (u64
)0);
109 if (status
!= EFI_SUCCESS
) {
110 efi_printk(sys_table
, "Failed to open file: ");
111 efi_char16_printk(sys_table
, filename_16
);
112 efi_printk(sys_table
, "\n");
119 status
= efi_early
->call((unsigned long)h
->get_info
, h
, &info_guid
,
121 if (status
!= EFI_BUFFER_TOO_SMALL
) {
122 efi_printk(sys_table
, "Failed to get file info size\n");
127 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
,
128 info_sz
, (void **)&info
);
129 if (status
!= EFI_SUCCESS
) {
130 efi_printk(sys_table
, "Failed to alloc mem for file info\n");
134 status
= efi_early
->call((unsigned long)h
->get_info
, h
, &info_guid
,
136 if (status
== EFI_BUFFER_TOO_SMALL
) {
137 efi_call_early(free_pool
, info
);
141 *file_sz
= info
->file_size
;
142 efi_call_early(free_pool
, info
);
144 if (status
!= EFI_SUCCESS
)
145 efi_printk(sys_table
, "Failed to get initrd info\n");
150 efi_file_size(efi_system_table_t
*sys_table
, void *__fh
,
151 efi_char16_t
*filename_16
, void **handle
, u64
*file_sz
)
154 return __file_size64(__fh
, filename_16
, handle
, file_sz
);
156 return __file_size32(__fh
, filename_16
, handle
, file_sz
);
160 efi_file_read(void *handle
, unsigned long *size
, void *addr
)
164 if (efi_early
->is64
) {
165 efi_file_handle_64_t
*fh
= handle
;
167 func
= (unsigned long)fh
->read
;
168 return efi_early
->call(func
, handle
, size
, addr
);
170 efi_file_handle_32_t
*fh
= handle
;
172 func
= (unsigned long)fh
->read
;
173 return efi_early
->call(func
, handle
, size
, addr
);
177 efi_status_t
efi_file_close(void *handle
)
179 if (efi_early
->is64
) {
180 efi_file_handle_64_t
*fh
= handle
;
182 return efi_early
->call((unsigned long)fh
->close
, handle
);
184 efi_file_handle_32_t
*fh
= handle
;
186 return efi_early
->call((unsigned long)fh
->close
, handle
);
190 static inline efi_status_t
__open_volume32(void *__image
, void **__fh
)
192 efi_file_io_interface_t
*io
;
193 efi_loaded_image_32_t
*image
= __image
;
194 efi_file_handle_32_t
*fh
;
195 efi_guid_t fs_proto
= EFI_FILE_SYSTEM_GUID
;
197 void *handle
= (void *)(unsigned long)image
->device_handle
;
200 status
= efi_call_early(handle_protocol
, handle
,
201 &fs_proto
, (void **)&io
);
202 if (status
!= EFI_SUCCESS
) {
203 efi_printk(sys_table
, "Failed to handle fs_proto\n");
207 func
= (unsigned long)io
->open_volume
;
208 status
= efi_early
->call(func
, io
, &fh
);
209 if (status
!= EFI_SUCCESS
)
210 efi_printk(sys_table
, "Failed to open volume\n");
216 static inline efi_status_t
__open_volume64(void *__image
, void **__fh
)
218 efi_file_io_interface_t
*io
;
219 efi_loaded_image_64_t
*image
= __image
;
220 efi_file_handle_64_t
*fh
;
221 efi_guid_t fs_proto
= EFI_FILE_SYSTEM_GUID
;
223 void *handle
= (void *)(unsigned long)image
->device_handle
;
226 status
= efi_call_early(handle_protocol
, handle
,
227 &fs_proto
, (void **)&io
);
228 if (status
!= EFI_SUCCESS
) {
229 efi_printk(sys_table
, "Failed to handle fs_proto\n");
233 func
= (unsigned long)io
->open_volume
;
234 status
= efi_early
->call(func
, io
, &fh
);
235 if (status
!= EFI_SUCCESS
)
236 efi_printk(sys_table
, "Failed to open volume\n");
243 efi_open_volume(efi_system_table_t
*sys_table
, void *__image
, void **__fh
)
246 return __open_volume64(__image
, __fh
);
248 return __open_volume32(__image
, __fh
);
251 void efi_char16_printk(efi_system_table_t
*table
, efi_char16_t
*str
)
253 unsigned long output_string
;
256 if (efi_early
->is64
) {
257 struct efi_simple_text_output_protocol_64
*out
;
260 offset
= offsetof(typeof(*out
), output_string
);
261 output_string
= efi_early
->text_output
+ offset
;
262 out
= (typeof(out
))(unsigned long)efi_early
->text_output
;
263 func
= (u64
*)output_string
;
265 efi_early
->call(*func
, out
, str
);
267 struct efi_simple_text_output_protocol_32
*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
= (u32
*)output_string
;
275 efi_early
->call(*func
, out
, str
);
280 __setup_efi_pci32(efi_pci_io_protocol_32
*pci
, struct pci_setup_rom
**__rom
)
282 struct pci_setup_rom
*rom
= NULL
;
287 status
= efi_early
->call(pci
->attributes
, pci
,
288 EfiPciIoAttributeOperationGet
, 0, 0,
290 if (status
!= EFI_SUCCESS
)
293 if (!pci
->romimage
|| !pci
->romsize
)
294 return EFI_INVALID_PARAMETER
;
296 size
= pci
->romsize
+ sizeof(*rom
);
298 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
, size
, &rom
);
299 if (status
!= EFI_SUCCESS
) {
300 efi_printk(sys_table
, "Failed to alloc mem for rom\n");
304 memset(rom
, 0, sizeof(*rom
));
306 rom
->data
.type
= SETUP_PCI
;
307 rom
->data
.len
= size
- sizeof(struct setup_data
);
309 rom
->pcilen
= pci
->romsize
;
312 status
= efi_early
->call(pci
->pci
.read
, pci
, EfiPciIoWidthUint16
,
313 PCI_VENDOR_ID
, 1, &(rom
->vendor
));
315 if (status
!= EFI_SUCCESS
) {
316 efi_printk(sys_table
, "Failed to read rom->vendor\n");
320 status
= efi_early
->call(pci
->pci
.read
, pci
, EfiPciIoWidthUint16
,
321 PCI_DEVICE_ID
, 1, &(rom
->devid
));
323 if (status
!= EFI_SUCCESS
) {
324 efi_printk(sys_table
, "Failed to read rom->devid\n");
328 status
= efi_early
->call(pci
->get_location
, pci
, &(rom
->segment
),
329 &(rom
->bus
), &(rom
->device
), &(rom
->function
));
331 if (status
!= EFI_SUCCESS
)
334 memcpy(rom
->romdata
, pci
->romimage
, pci
->romsize
);
338 efi_call_early(free_pool
, rom
);
343 setup_efi_pci32(struct boot_params
*params
, void **pci_handle
,
346 efi_pci_io_protocol_32
*pci
= NULL
;
347 efi_guid_t pci_proto
= EFI_PCI_IO_PROTOCOL_GUID
;
348 u32
*handles
= (u32
*)(unsigned long)pci_handle
;
350 unsigned long nr_pci
;
351 struct setup_data
*data
;
354 data
= (struct setup_data
*)(unsigned long)params
->hdr
.setup_data
;
356 while (data
&& data
->next
)
357 data
= (struct setup_data
*)(unsigned long)data
->next
;
359 nr_pci
= size
/ sizeof(u32
);
360 for (i
= 0; i
< nr_pci
; i
++) {
361 struct pci_setup_rom
*rom
= NULL
;
364 status
= efi_call_early(handle_protocol
, h
,
365 &pci_proto
, (void **)&pci
);
367 if (status
!= EFI_SUCCESS
)
373 status
= __setup_efi_pci32(pci
, &rom
);
374 if (status
!= EFI_SUCCESS
)
378 data
->next
= (unsigned long)rom
;
380 params
->hdr
.setup_data
= (unsigned long)rom
;
382 data
= (struct setup_data
*)rom
;
388 __setup_efi_pci64(efi_pci_io_protocol_64
*pci
, struct pci_setup_rom
**__rom
)
390 struct pci_setup_rom
*rom
;
395 status
= efi_early
->call(pci
->attributes
, pci
,
396 EfiPciIoAttributeOperationGet
, 0,
398 if (status
!= EFI_SUCCESS
)
401 if (!pci
->romimage
|| !pci
->romsize
)
402 return EFI_INVALID_PARAMETER
;
404 size
= pci
->romsize
+ sizeof(*rom
);
406 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
, size
, &rom
);
407 if (status
!= EFI_SUCCESS
) {
408 efi_printk(sys_table
, "Failed to alloc mem for rom\n");
412 rom
->data
.type
= SETUP_PCI
;
413 rom
->data
.len
= size
- sizeof(struct setup_data
);
415 rom
->pcilen
= pci
->romsize
;
418 status
= efi_early
->call(pci
->pci
.read
, pci
, EfiPciIoWidthUint16
,
419 PCI_VENDOR_ID
, 1, &(rom
->vendor
));
421 if (status
!= EFI_SUCCESS
) {
422 efi_printk(sys_table
, "Failed to read rom->vendor\n");
426 status
= efi_early
->call(pci
->pci
.read
, pci
, EfiPciIoWidthUint16
,
427 PCI_DEVICE_ID
, 1, &(rom
->devid
));
429 if (status
!= EFI_SUCCESS
) {
430 efi_printk(sys_table
, "Failed to read rom->devid\n");
434 status
= efi_early
->call(pci
->get_location
, pci
, &(rom
->segment
),
435 &(rom
->bus
), &(rom
->device
), &(rom
->function
));
437 if (status
!= EFI_SUCCESS
)
440 memcpy(rom
->romdata
, pci
->romimage
, pci
->romsize
);
444 efi_call_early(free_pool
, rom
);
450 setup_efi_pci64(struct boot_params
*params
, void **pci_handle
,
453 efi_pci_io_protocol_64
*pci
= NULL
;
454 efi_guid_t pci_proto
= EFI_PCI_IO_PROTOCOL_GUID
;
455 u64
*handles
= (u64
*)(unsigned long)pci_handle
;
457 unsigned long nr_pci
;
458 struct setup_data
*data
;
461 data
= (struct setup_data
*)(unsigned long)params
->hdr
.setup_data
;
463 while (data
&& data
->next
)
464 data
= (struct setup_data
*)(unsigned long)data
->next
;
466 nr_pci
= size
/ sizeof(u64
);
467 for (i
= 0; i
< nr_pci
; i
++) {
468 struct pci_setup_rom
*rom
= NULL
;
471 status
= efi_call_early(handle_protocol
, h
,
472 &pci_proto
, (void **)&pci
);
474 if (status
!= EFI_SUCCESS
)
480 status
= __setup_efi_pci64(pci
, &rom
);
481 if (status
!= EFI_SUCCESS
)
485 data
->next
= (unsigned long)rom
;
487 params
->hdr
.setup_data
= (unsigned long)rom
;
489 data
= (struct setup_data
*)rom
;
495 * There's no way to return an informative status from this function,
496 * because any analysis (and printing of error messages) needs to be
497 * done directly at the EFI function call-site.
499 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
500 * just didn't find any PCI devices, but there's no way to tell outside
501 * the context of the call.
503 static void setup_efi_pci(struct boot_params
*params
)
506 void **pci_handle
= NULL
;
507 efi_guid_t pci_proto
= EFI_PCI_IO_PROTOCOL_GUID
;
508 unsigned long size
= 0;
510 status
= efi_call_early(locate_handle
,
511 EFI_LOCATE_BY_PROTOCOL
,
512 &pci_proto
, NULL
, &size
, pci_handle
);
514 if (status
== EFI_BUFFER_TOO_SMALL
) {
515 status
= efi_call_early(allocate_pool
,
517 size
, (void **)&pci_handle
);
519 if (status
!= EFI_SUCCESS
) {
520 efi_printk(sys_table
, "Failed to alloc mem for pci_handle\n");
524 status
= efi_call_early(locate_handle
,
525 EFI_LOCATE_BY_PROTOCOL
, &pci_proto
,
526 NULL
, &size
, pci_handle
);
529 if (status
!= EFI_SUCCESS
)
533 setup_efi_pci64(params
, pci_handle
, size
);
535 setup_efi_pci32(params
, pci_handle
, size
);
538 efi_call_early(free_pool
, pci_handle
);
541 static void retrieve_apple_device_properties(struct boot_params
*boot_params
)
543 efi_guid_t guid
= APPLE_PROPERTIES_PROTOCOL_GUID
;
544 struct setup_data
*data
, *new;
549 status
= efi_call_early(locate_protocol
, &guid
, NULL
, &p
);
550 if (status
!= EFI_SUCCESS
)
553 if (efi_table_attr(apple_properties_protocol
, version
, p
) != 0x10000) {
554 efi_printk(sys_table
, "Unsupported properties proto version\n");
558 efi_call_proto(apple_properties_protocol
, get_all
, p
, NULL
, &size
);
563 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
,
564 size
+ sizeof(struct setup_data
), &new);
565 if (status
!= EFI_SUCCESS
) {
566 efi_printk(sys_table
,
567 "Failed to alloc mem for properties\n");
571 status
= efi_call_proto(apple_properties_protocol
, get_all
, p
,
574 if (status
== EFI_BUFFER_TOO_SMALL
)
575 efi_call_early(free_pool
, new);
576 } while (status
== EFI_BUFFER_TOO_SMALL
);
578 new->type
= SETUP_APPLE_PROPERTIES
;
582 data
= (struct setup_data
*)(unsigned long)boot_params
->hdr
.setup_data
;
584 boot_params
->hdr
.setup_data
= (unsigned long)new;
587 data
= (struct setup_data
*)(unsigned long)data
->next
;
588 data
->next
= (unsigned long)new;
592 static void setup_quirks(struct boot_params
*boot_params
)
594 efi_char16_t
const apple
[] = { 'A', 'p', 'p', 'l', 'e', 0 };
595 efi_char16_t
*fw_vendor
= (efi_char16_t
*)(unsigned long)
596 efi_table_attr(efi_system_table
, fw_vendor
, sys_table
);
598 if (!memcmp(fw_vendor
, apple
, sizeof(apple
))) {
599 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES
))
600 retrieve_apple_device_properties(boot_params
);
604 static int get_secure_boot(void)
606 u8 sb
, setup
, moksbstate
;
607 unsigned long datasize
= sizeof(sb
);
609 efi_guid_t var_guid
= EFI_GLOBAL_VARIABLE_GUID
;
612 status
= efi_early
->call((unsigned long)sys_table
->runtime
->get_variable
,
613 L
"SecureBoot", &var_guid
, NULL
, &datasize
, &sb
);
615 if (status
!= EFI_SUCCESS
)
622 status
= efi_early
->call((unsigned long)sys_table
->runtime
->get_variable
,
623 L
"SetupMode", &var_guid
, NULL
, &datasize
,
626 if (status
!= EFI_SUCCESS
)
632 /* See if a user has put shim into insecure_mode. If so, and the variable
633 * doesn't have the runtime attribute set, we might as well honor that.
635 var_guid
= EFI_SHIM_LOCK_GUID
;
636 status
= efi_early
->call((unsigned long)sys_table
->runtime
->get_variable
,
637 L
"MokSBState", &var_guid
, &attr
, &datasize
,
640 /* If it fails, we don't care why. Default to secure */
641 if (status
!= EFI_SUCCESS
)
644 if (!(attr
& EFI_VARIABLE_RUNTIME_ACCESS
)) {
654 * See if we have Graphics Output Protocol
656 static efi_status_t
setup_gop(struct screen_info
*si
, efi_guid_t
*proto
,
660 void **gop_handle
= NULL
;
662 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
,
663 size
, (void **)&gop_handle
);
664 if (status
!= EFI_SUCCESS
)
667 status
= efi_call_early(locate_handle
,
668 EFI_LOCATE_BY_PROTOCOL
,
669 proto
, NULL
, &size
, gop_handle
);
670 if (status
!= EFI_SUCCESS
)
674 status
= setup_gop64(si
, proto
, size
, gop_handle
);
676 status
= setup_gop32(si
, proto
, size
, gop_handle
);
679 efi_call_early(free_pool
, gop_handle
);
684 setup_uga32(void **uga_handle
, unsigned long size
, u32
*width
, u32
*height
)
686 struct efi_uga_draw_protocol
*uga
= NULL
, *first_uga
;
687 efi_guid_t uga_proto
= EFI_UGA_PROTOCOL_GUID
;
688 unsigned long nr_ugas
;
689 u32
*handles
= (u32
*)uga_handle
;;
690 efi_status_t status
= EFI_INVALID_PARAMETER
;
694 nr_ugas
= size
/ sizeof(u32
);
695 for (i
= 0; i
< nr_ugas
; i
++) {
696 efi_guid_t pciio_proto
= EFI_PCI_IO_PROTOCOL_GUID
;
697 u32 w
, h
, depth
, refresh
;
699 u32 handle
= handles
[i
];
701 status
= efi_call_early(handle_protocol
, handle
,
702 &uga_proto
, (void **)&uga
);
703 if (status
!= EFI_SUCCESS
)
706 efi_call_early(handle_protocol
, handle
, &pciio_proto
, &pciio
);
708 status
= efi_early
->call((unsigned long)uga
->get_mode
, uga
,
709 &w
, &h
, &depth
, &refresh
);
710 if (status
== EFI_SUCCESS
&& (!first_uga
|| pciio
)) {
715 * Once we've found a UGA supporting PCIIO,
716 * don't bother looking any further.
729 setup_uga64(void **uga_handle
, unsigned long size
, u32
*width
, u32
*height
)
731 struct efi_uga_draw_protocol
*uga
= NULL
, *first_uga
;
732 efi_guid_t uga_proto
= EFI_UGA_PROTOCOL_GUID
;
733 unsigned long nr_ugas
;
734 u64
*handles
= (u64
*)uga_handle
;;
735 efi_status_t status
= EFI_INVALID_PARAMETER
;
739 nr_ugas
= size
/ sizeof(u64
);
740 for (i
= 0; i
< nr_ugas
; i
++) {
741 efi_guid_t pciio_proto
= EFI_PCI_IO_PROTOCOL_GUID
;
742 u32 w
, h
, depth
, refresh
;
744 u64 handle
= handles
[i
];
746 status
= efi_call_early(handle_protocol
, handle
,
747 &uga_proto
, (void **)&uga
);
748 if (status
!= EFI_SUCCESS
)
751 efi_call_early(handle_protocol
, handle
, &pciio_proto
, &pciio
);
753 status
= efi_early
->call((unsigned long)uga
->get_mode
, uga
,
754 &w
, &h
, &depth
, &refresh
);
755 if (status
== EFI_SUCCESS
&& (!first_uga
|| pciio
)) {
760 * Once we've found a UGA supporting PCIIO,
761 * don't bother looking any further.
774 * See if we have Universal Graphics Adapter (UGA) protocol
776 static efi_status_t
setup_uga(struct screen_info
*si
, efi_guid_t
*uga_proto
,
781 void **uga_handle
= NULL
;
783 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
,
784 size
, (void **)&uga_handle
);
785 if (status
!= EFI_SUCCESS
)
788 status
= efi_call_early(locate_handle
,
789 EFI_LOCATE_BY_PROTOCOL
,
790 uga_proto
, NULL
, &size
, uga_handle
);
791 if (status
!= EFI_SUCCESS
)
798 status
= setup_uga64(uga_handle
, size
, &width
, &height
);
800 status
= setup_uga32(uga_handle
, size
, &width
, &height
);
802 if (!width
&& !height
)
805 /* EFI framebuffer */
806 si
->orig_video_isVGA
= VIDEO_TYPE_EFI
;
809 si
->lfb_width
= width
;
810 si
->lfb_height
= height
;
822 efi_call_early(free_pool
, uga_handle
);
826 void setup_graphics(struct boot_params
*boot_params
)
828 efi_guid_t graphics_proto
= EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID
;
829 struct screen_info
*si
;
830 efi_guid_t uga_proto
= EFI_UGA_PROTOCOL_GUID
;
833 void **gop_handle
= NULL
;
834 void **uga_handle
= NULL
;
836 si
= &boot_params
->screen_info
;
837 memset(si
, 0, sizeof(*si
));
840 status
= efi_call_early(locate_handle
,
841 EFI_LOCATE_BY_PROTOCOL
,
842 &graphics_proto
, NULL
, &size
, gop_handle
);
843 if (status
== EFI_BUFFER_TOO_SMALL
)
844 status
= efi_setup_gop(NULL
, si
, &graphics_proto
, size
);
846 if (status
!= EFI_SUCCESS
) {
848 status
= efi_call_early(locate_handle
,
849 EFI_LOCATE_BY_PROTOCOL
,
850 &uga_proto
, NULL
, &size
, uga_handle
);
851 if (status
== EFI_BUFFER_TOO_SMALL
)
852 setup_uga(si
, &uga_proto
, size
);
857 * Because the x86 boot code expects to be passed a boot_params we
858 * need to create one ourselves (usually the bootloader would create
861 * The caller is responsible for filling out ->code32_start in the
862 * returned boot_params.
864 struct boot_params
*make_boot_params(struct efi_config
*c
)
866 struct boot_params
*boot_params
;
867 struct apm_bios_info
*bi
;
868 struct setup_header
*hdr
;
869 efi_loaded_image_t
*image
;
870 void *options
, *handle
;
871 efi_guid_t proto
= LOADED_IMAGE_PROTOCOL_GUID
;
872 int options_size
= 0;
878 unsigned long ramdisk_addr
;
879 unsigned long ramdisk_size
;
882 sys_table
= (efi_system_table_t
*)(unsigned long)efi_early
->table
;
883 handle
= (void *)(unsigned long)efi_early
->image_handle
;
885 /* Check if we were booted by the EFI firmware */
886 if (sys_table
->hdr
.signature
!= EFI_SYSTEM_TABLE_SIGNATURE
)
890 setup_boot_services64(efi_early
);
892 setup_boot_services32(efi_early
);
894 status
= efi_call_early(handle_protocol
, handle
,
895 &proto
, (void *)&image
);
896 if (status
!= EFI_SUCCESS
) {
897 efi_printk(sys_table
, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
901 status
= efi_low_alloc(sys_table
, 0x4000, 1,
902 (unsigned long *)&boot_params
);
903 if (status
!= EFI_SUCCESS
) {
904 efi_printk(sys_table
, "Failed to alloc lowmem for boot params\n");
908 memset(boot_params
, 0x0, 0x4000);
910 hdr
= &boot_params
->hdr
;
911 bi
= &boot_params
->apm_bios_info
;
913 /* Copy the second sector to boot_params */
914 memcpy(&hdr
->jump
, image
->image_base
+ 512, 512);
917 * Fill out some of the header fields ourselves because the
918 * EFI firmware loader doesn't load the first sector.
921 hdr
->vid_mode
= 0xffff;
922 hdr
->boot_flag
= 0xAA55;
924 hdr
->type_of_loader
= 0x21;
926 /* Convert unicode cmdline to ascii */
927 cmdline_ptr
= efi_convert_cmdline(sys_table
, image
, &options_size
);
930 hdr
->cmd_line_ptr
= (unsigned long)cmdline_ptr
;
931 /* Fill in upper bits of command line address, NOP on 32 bit */
932 boot_params
->ext_cmd_line_ptr
= (u64
)(unsigned long)cmdline_ptr
>> 32;
934 hdr
->ramdisk_image
= 0;
935 hdr
->ramdisk_size
= 0;
937 /* Clear APM BIOS info */
938 memset(bi
, 0, sizeof(*bi
));
940 status
= efi_parse_options(cmdline_ptr
);
941 if (status
!= EFI_SUCCESS
)
944 status
= handle_cmdline_files(sys_table
, image
,
945 (char *)(unsigned long)hdr
->cmd_line_ptr
,
946 "initrd=", hdr
->initrd_addr_max
,
947 &ramdisk_addr
, &ramdisk_size
);
949 if (status
!= EFI_SUCCESS
&&
950 hdr
->xloadflags
& XLF_CAN_BE_LOADED_ABOVE_4G
) {
951 efi_printk(sys_table
, "Trying to load files to higher address\n");
952 status
= handle_cmdline_files(sys_table
, image
,
953 (char *)(unsigned long)hdr
->cmd_line_ptr
,
955 &ramdisk_addr
, &ramdisk_size
);
958 if (status
!= EFI_SUCCESS
)
960 hdr
->ramdisk_image
= ramdisk_addr
& 0xffffffff;
961 hdr
->ramdisk_size
= ramdisk_size
& 0xffffffff;
962 boot_params
->ext_ramdisk_image
= (u64
)ramdisk_addr
>> 32;
963 boot_params
->ext_ramdisk_size
= (u64
)ramdisk_size
>> 32;
967 efi_free(sys_table
, options_size
, hdr
->cmd_line_ptr
);
969 efi_free(sys_table
, 0x4000, (unsigned long)boot_params
);
973 static void add_e820ext(struct boot_params
*params
,
974 struct setup_data
*e820ext
, u32 nr_entries
)
976 struct setup_data
*data
;
980 e820ext
->type
= SETUP_E820_EXT
;
981 e820ext
->len
= nr_entries
* sizeof(struct e820entry
);
984 data
= (struct setup_data
*)(unsigned long)params
->hdr
.setup_data
;
986 while (data
&& data
->next
)
987 data
= (struct setup_data
*)(unsigned long)data
->next
;
990 data
->next
= (unsigned long)e820ext
;
992 params
->hdr
.setup_data
= (unsigned long)e820ext
;
995 static efi_status_t
setup_e820(struct boot_params
*params
,
996 struct setup_data
*e820ext
, u32 e820ext_size
)
998 struct e820entry
*e820_map
= ¶ms
->e820_map
[0];
999 struct efi_info
*efi
= ¶ms
->efi_info
;
1000 struct e820entry
*prev
= NULL
;
1006 nr_desc
= efi
->efi_memmap_size
/ efi
->efi_memdesc_size
;
1008 for (i
= 0; i
< nr_desc
; i
++) {
1009 efi_memory_desc_t
*d
;
1010 unsigned int e820_type
= 0;
1011 unsigned long m
= efi
->efi_memmap
;
1013 #ifdef CONFIG_X86_64
1014 m
|= (u64
)efi
->efi_memmap_hi
<< 32;
1017 d
= (efi_memory_desc_t
*)(m
+ (i
* efi
->efi_memdesc_size
));
1019 case EFI_RESERVED_TYPE
:
1020 case EFI_RUNTIME_SERVICES_CODE
:
1021 case EFI_RUNTIME_SERVICES_DATA
:
1022 case EFI_MEMORY_MAPPED_IO
:
1023 case EFI_MEMORY_MAPPED_IO_PORT_SPACE
:
1025 e820_type
= E820_RESERVED
;
1028 case EFI_UNUSABLE_MEMORY
:
1029 e820_type
= E820_UNUSABLE
;
1032 case EFI_ACPI_RECLAIM_MEMORY
:
1033 e820_type
= E820_ACPI
;
1036 case EFI_LOADER_CODE
:
1037 case EFI_LOADER_DATA
:
1038 case EFI_BOOT_SERVICES_CODE
:
1039 case EFI_BOOT_SERVICES_DATA
:
1040 case EFI_CONVENTIONAL_MEMORY
:
1041 e820_type
= E820_RAM
;
1044 case EFI_ACPI_MEMORY_NVS
:
1045 e820_type
= E820_NVS
;
1048 case EFI_PERSISTENT_MEMORY
:
1049 e820_type
= E820_PMEM
;
1056 /* Merge adjacent mappings */
1057 if (prev
&& prev
->type
== e820_type
&&
1058 (prev
->addr
+ prev
->size
) == d
->phys_addr
) {
1059 prev
->size
+= d
->num_pages
<< 12;
1063 if (nr_entries
== ARRAY_SIZE(params
->e820_map
)) {
1064 u32 need
= (nr_desc
- i
) * sizeof(struct e820entry
) +
1065 sizeof(struct setup_data
);
1067 if (!e820ext
|| e820ext_size
< need
)
1068 return EFI_BUFFER_TOO_SMALL
;
1070 /* boot_params map full, switch to e820 extended */
1071 e820_map
= (struct e820entry
*)e820ext
->data
;
1074 e820_map
->addr
= d
->phys_addr
;
1075 e820_map
->size
= d
->num_pages
<< PAGE_SHIFT
;
1076 e820_map
->type
= e820_type
;
1081 if (nr_entries
> ARRAY_SIZE(params
->e820_map
)) {
1082 u32 nr_e820ext
= nr_entries
- ARRAY_SIZE(params
->e820_map
);
1084 add_e820ext(params
, e820ext
, nr_e820ext
);
1085 nr_entries
-= nr_e820ext
;
1088 params
->e820_entries
= (u8
)nr_entries
;
1093 static efi_status_t
alloc_e820ext(u32 nr_desc
, struct setup_data
**e820ext
,
1096 efi_status_t status
;
1099 size
= sizeof(struct setup_data
) +
1100 sizeof(struct e820entry
) * nr_desc
;
1103 efi_call_early(free_pool
, *e820ext
);
1108 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
,
1109 size
, (void **)e820ext
);
1110 if (status
== EFI_SUCCESS
)
1111 *e820ext_size
= size
;
1116 struct exit_boot_struct
{
1117 struct boot_params
*boot_params
;
1118 struct efi_info
*efi
;
1119 struct setup_data
*e820ext
;
1124 static efi_status_t
exit_boot_func(efi_system_table_t
*sys_table_arg
,
1125 struct efi_boot_memmap
*map
,
1128 static bool first
= true;
1129 const char *signature
;
1131 efi_status_t status
;
1132 struct exit_boot_struct
*p
= priv
;
1135 nr_desc
= *map
->buff_size
/ *map
->desc_size
;
1136 if (nr_desc
> ARRAY_SIZE(p
->boot_params
->e820_map
)) {
1137 u32 nr_e820ext
= nr_desc
-
1138 ARRAY_SIZE(p
->boot_params
->e820_map
);
1140 status
= alloc_e820ext(nr_e820ext
, &p
->e820ext
,
1142 if (status
!= EFI_SUCCESS
)
1148 signature
= p
->is64
? EFI64_LOADER_SIGNATURE
: EFI32_LOADER_SIGNATURE
;
1149 memcpy(&p
->efi
->efi_loader_signature
, signature
, sizeof(__u32
));
1151 p
->efi
->efi_systab
= (unsigned long)sys_table_arg
;
1152 p
->efi
->efi_memdesc_size
= *map
->desc_size
;
1153 p
->efi
->efi_memdesc_version
= *map
->desc_ver
;
1154 p
->efi
->efi_memmap
= (unsigned long)*map
->map
;
1155 p
->efi
->efi_memmap_size
= *map
->map_size
;
1157 #ifdef CONFIG_X86_64
1158 p
->efi
->efi_systab_hi
= (unsigned long)sys_table_arg
>> 32;
1159 p
->efi
->efi_memmap_hi
= (unsigned long)*map
->map
>> 32;
1165 static efi_status_t
exit_boot(struct boot_params
*boot_params
,
1166 void *handle
, bool is64
)
1168 unsigned long map_sz
, key
, desc_size
, buff_size
;
1169 efi_memory_desc_t
*mem_map
;
1170 struct setup_data
*e820ext
;
1172 efi_status_t status
;
1174 struct efi_boot_memmap map
;
1175 struct exit_boot_struct priv
;
1178 map
.map_size
= &map_sz
;
1179 map
.desc_size
= &desc_size
;
1180 map
.desc_ver
= &desc_version
;
1182 map
.buff_size
= &buff_size
;
1183 priv
.boot_params
= boot_params
;
1184 priv
.efi
= &boot_params
->efi_info
;
1185 priv
.e820ext
= NULL
;
1186 priv
.e820ext_size
= 0;
1189 /* Might as well exit boot services now */
1190 status
= efi_exit_boot_services(sys_table
, handle
, &map
, &priv
,
1192 if (status
!= EFI_SUCCESS
)
1195 e820ext
= priv
.e820ext
;
1196 e820ext_size
= priv
.e820ext_size
;
1198 boot_params
->alt_mem_k
= 32 * 1024;
1200 status
= setup_e820(boot_params
, e820ext
, e820ext_size
);
1201 if (status
!= EFI_SUCCESS
)
1208 * On success we return a pointer to a boot_params structure, and NULL
1211 struct boot_params
*efi_main(struct efi_config
*c
,
1212 struct boot_params
*boot_params
)
1214 struct desc_ptr
*gdt
= NULL
;
1215 efi_loaded_image_t
*image
;
1216 struct setup_header
*hdr
= &boot_params
->hdr
;
1217 efi_status_t status
;
1218 struct desc_struct
*desc
;
1220 efi_system_table_t
*_table
;
1225 _table
= (efi_system_table_t
*)(unsigned long)efi_early
->table
;
1226 handle
= (void *)(unsigned long)efi_early
->image_handle
;
1227 is64
= efi_early
->is64
;
1231 /* Check if we were booted by the EFI firmware */
1232 if (sys_table
->hdr
.signature
!= EFI_SYSTEM_TABLE_SIGNATURE
)
1236 setup_boot_services64(efi_early
);
1238 setup_boot_services32(efi_early
);
1240 sanitize_boot_params(boot_params
);
1242 boot_params
->secure_boot
= get_secure_boot();
1244 setup_graphics(boot_params
);
1246 setup_efi_pci(boot_params
);
1248 setup_quirks(boot_params
);
1250 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
,
1251 sizeof(*gdt
), (void **)&gdt
);
1252 if (status
!= EFI_SUCCESS
) {
1253 efi_printk(sys_table
, "Failed to alloc mem for gdt structure\n");
1258 status
= efi_low_alloc(sys_table
, gdt
->size
, 8,
1259 (unsigned long *)&gdt
->address
);
1260 if (status
!= EFI_SUCCESS
) {
1261 efi_printk(sys_table
, "Failed to alloc mem for gdt\n");
1266 * If the kernel isn't already loaded at the preferred load
1267 * address, relocate it.
1269 if (hdr
->pref_address
!= hdr
->code32_start
) {
1270 unsigned long bzimage_addr
= hdr
->code32_start
;
1271 status
= efi_relocate_kernel(sys_table
, &bzimage_addr
,
1272 hdr
->init_size
, hdr
->init_size
,
1274 hdr
->kernel_alignment
);
1275 if (status
!= EFI_SUCCESS
) {
1276 efi_printk(sys_table
, "efi_relocate_kernel() failed!\n");
1280 hdr
->pref_address
= hdr
->code32_start
;
1281 hdr
->code32_start
= bzimage_addr
;
1284 status
= exit_boot(boot_params
, handle
, is64
);
1285 if (status
!= EFI_SUCCESS
) {
1286 efi_printk(sys_table
, "exit_boot() failed!\n");
1290 memset((char *)gdt
->address
, 0x0, gdt
->size
);
1291 desc
= (struct desc_struct
*)gdt
->address
;
1293 /* The first GDT is a dummy and the second is unused. */
1296 desc
->limit0
= 0xffff;
1297 desc
->base0
= 0x0000;
1298 desc
->base1
= 0x0000;
1299 desc
->type
= SEG_TYPE_CODE
| SEG_TYPE_EXEC_READ
;
1300 desc
->s
= DESC_TYPE_CODE_DATA
;
1306 desc
->d
= SEG_OP_SIZE_32BIT
;
1307 desc
->g
= SEG_GRANULARITY_4KB
;
1311 desc
->limit0
= 0xffff;
1312 desc
->base0
= 0x0000;
1313 desc
->base1
= 0x0000;
1314 desc
->type
= SEG_TYPE_DATA
| SEG_TYPE_READ_WRITE
;
1315 desc
->s
= DESC_TYPE_CODE_DATA
;
1321 desc
->d
= SEG_OP_SIZE_32BIT
;
1322 desc
->g
= SEG_GRANULARITY_4KB
;
1325 #ifdef CONFIG_X86_64
1326 /* Task segment value */
1328 desc
->limit0
= 0x0000;
1329 desc
->base0
= 0x0000;
1330 desc
->base1
= 0x0000;
1331 desc
->type
= SEG_TYPE_TSS
;
1339 desc
->g
= SEG_GRANULARITY_4KB
;
1341 #endif /* CONFIG_X86_64 */
1343 asm volatile("cli");
1344 asm volatile ("lgdt %0" : : "m" (*gdt
));
1348 efi_printk(sys_table
, "efi_main() failed!\n");