1 // SPDX-License-Identifier: GPL-2.0-only
3 /* -----------------------------------------------------------------------
5 * Copyright 2011 Intel Corporation; author Matt Fleming
7 * ----------------------------------------------------------------------- */
10 #include <linux/pci.h>
11 #include <linux/stddef.h>
14 #include <asm/e820/types.h>
15 #include <asm/setup.h>
18 #include <asm/kaslr.h>
24 extern char _bss
[], _ebss
[];
26 const efi_system_table_t
*efi_system_table
;
27 const efi_dxe_services_table_t
*efi_dxe_table
;
28 static efi_loaded_image_t
*image
= NULL
;
29 static efi_memory_attribute_protocol_t
*memattr
;
31 typedef union sev_memory_acceptance_protocol sev_memory_acceptance_protocol_t
;
32 union sev_memory_acceptance_protocol
{
34 efi_status_t (__efiapi
* allow_unaccepted_memory
)(
35 sev_memory_acceptance_protocol_t
*);
38 u32 allow_unaccepted_memory
;
43 preserve_pci_rom_image(efi_pci_io_protocol_t
*pci
, struct pci_setup_rom
**__rom
)
45 struct pci_setup_rom
*rom
= NULL
;
52 * Some firmware images contain EFI function pointers at the place where
53 * the romimage and romsize fields are supposed to be. Typically the EFI
54 * code is mapped at high addresses, translating to an unrealistically
55 * large romsize. The UEFI spec limits the size of option ROMs to 16
56 * MiB so we reject any ROMs over 16 MiB in size to catch this.
58 romimage
= efi_table_attr(pci
, romimage
);
59 romsize
= efi_table_attr(pci
, romsize
);
60 if (!romimage
|| !romsize
|| romsize
> SZ_16M
)
61 return EFI_INVALID_PARAMETER
;
63 size
= romsize
+ sizeof(*rom
);
65 status
= efi_bs_call(allocate_pool
, EFI_LOADER_DATA
, size
,
67 if (status
!= EFI_SUCCESS
) {
68 efi_err("Failed to allocate memory for 'rom'\n");
72 memset(rom
, 0, sizeof(*rom
));
74 rom
->data
.type
= SETUP_PCI
;
75 rom
->data
.len
= size
- sizeof(struct setup_data
);
77 rom
->pcilen
= romsize
;
80 status
= efi_call_proto(pci
, pci
.read
, EfiPciIoWidthUint16
,
81 PCI_VENDOR_ID
, 1, &rom
->vendor
);
83 if (status
!= EFI_SUCCESS
) {
84 efi_err("Failed to read rom->vendor\n");
88 status
= efi_call_proto(pci
, pci
.read
, EfiPciIoWidthUint16
,
89 PCI_DEVICE_ID
, 1, &rom
->devid
);
91 if (status
!= EFI_SUCCESS
) {
92 efi_err("Failed to read rom->devid\n");
96 status
= efi_call_proto(pci
, get_location
, &rom
->segment
, &rom
->bus
,
97 &rom
->device
, &rom
->function
);
99 if (status
!= EFI_SUCCESS
)
102 memcpy(rom
->romdata
, romimage
, romsize
);
106 efi_bs_call(free_pool
, rom
);
111 * There's no way to return an informative status from this function,
112 * because any analysis (and printing of error messages) needs to be
113 * done directly at the EFI function call-site.
115 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
116 * just didn't find any PCI devices, but there's no way to tell outside
117 * the context of the call.
119 static void setup_efi_pci(struct boot_params
*params
)
122 void **pci_handle
= NULL
;
123 efi_guid_t pci_proto
= EFI_PCI_IO_PROTOCOL_GUID
;
124 unsigned long size
= 0;
125 struct setup_data
*data
;
129 status
= efi_bs_call(locate_handle
, EFI_LOCATE_BY_PROTOCOL
,
130 &pci_proto
, NULL
, &size
, pci_handle
);
132 if (status
== EFI_BUFFER_TOO_SMALL
) {
133 status
= efi_bs_call(allocate_pool
, EFI_LOADER_DATA
, size
,
134 (void **)&pci_handle
);
136 if (status
!= EFI_SUCCESS
) {
137 efi_err("Failed to allocate memory for 'pci_handle'\n");
141 status
= efi_bs_call(locate_handle
, EFI_LOCATE_BY_PROTOCOL
,
142 &pci_proto
, NULL
, &size
, pci_handle
);
145 if (status
!= EFI_SUCCESS
)
148 data
= (struct setup_data
*)(unsigned long)params
->hdr
.setup_data
;
150 while (data
&& data
->next
)
151 data
= (struct setup_data
*)(unsigned long)data
->next
;
153 for_each_efi_handle(h
, pci_handle
, size
, i
) {
154 efi_pci_io_protocol_t
*pci
= NULL
;
155 struct pci_setup_rom
*rom
;
157 status
= efi_bs_call(handle_protocol
, h
, &pci_proto
,
159 if (status
!= EFI_SUCCESS
|| !pci
)
162 status
= preserve_pci_rom_image(pci
, &rom
);
163 if (status
!= EFI_SUCCESS
)
167 data
->next
= (unsigned long)rom
;
169 params
->hdr
.setup_data
= (unsigned long)rom
;
171 data
= (struct setup_data
*)rom
;
175 efi_bs_call(free_pool
, pci_handle
);
178 static void retrieve_apple_device_properties(struct boot_params
*boot_params
)
180 efi_guid_t guid
= APPLE_PROPERTIES_PROTOCOL_GUID
;
181 struct setup_data
*data
, *new;
184 apple_properties_protocol_t
*p
;
186 status
= efi_bs_call(locate_protocol
, &guid
, NULL
, (void **)&p
);
187 if (status
!= EFI_SUCCESS
)
190 if (efi_table_attr(p
, version
) != 0x10000) {
191 efi_err("Unsupported properties proto version\n");
195 efi_call_proto(p
, get_all
, NULL
, &size
);
200 status
= efi_bs_call(allocate_pool
, EFI_LOADER_DATA
,
201 size
+ sizeof(struct setup_data
),
203 if (status
!= EFI_SUCCESS
) {
204 efi_err("Failed to allocate memory for 'properties'\n");
208 status
= efi_call_proto(p
, get_all
, new->data
, &size
);
210 if (status
== EFI_BUFFER_TOO_SMALL
)
211 efi_bs_call(free_pool
, new);
212 } while (status
== EFI_BUFFER_TOO_SMALL
);
214 new->type
= SETUP_APPLE_PROPERTIES
;
218 data
= (struct setup_data
*)(unsigned long)boot_params
->hdr
.setup_data
;
220 boot_params
->hdr
.setup_data
= (unsigned long)new;
223 data
= (struct setup_data
*)(unsigned long)data
->next
;
224 data
->next
= (unsigned long)new;
228 static bool apple_match_product_name(void)
230 static const char type1_product_matches
[][15] = {
240 const struct efi_smbios_type1_record
*record
;
243 record
= (struct efi_smbios_type1_record
*)efi_get_smbios_record(1);
247 product
= efi_get_smbios_string(record
, product_name
);
251 for (int i
= 0; i
< ARRAY_SIZE(type1_product_matches
); i
++) {
252 if (!strcmp(product
, type1_product_matches
[i
]))
259 static void apple_set_os(void)
262 unsigned long version
;
263 efi_status_t (__efiapi
*set_os_version
)(const char *);
264 efi_status_t (__efiapi
*set_os_vendor
)(const char *);
268 if (!efi_is_64bit() || !apple_match_product_name())
271 status
= efi_bs_call(locate_protocol
, &APPLE_SET_OS_PROTOCOL_GUID
, NULL
,
273 if (status
!= EFI_SUCCESS
)
276 if (set_os
->version
>= 2) {
277 status
= set_os
->set_os_vendor("Apple Inc.");
278 if (status
!= EFI_SUCCESS
)
279 efi_err("Failed to set OS vendor via apple_set_os\n");
282 if (set_os
->version
> 0) {
283 /* The version being set doesn't seem to matter */
284 status
= set_os
->set_os_version("Mac OS X 10.9");
285 if (status
!= EFI_SUCCESS
)
286 efi_err("Failed to set OS version via apple_set_os\n");
290 efi_status_t
efi_adjust_memory_range_protection(unsigned long start
,
294 efi_gcd_memory_space_desc_t desc
;
295 unsigned long end
, next
;
296 unsigned long rounded_start
, rounded_end
;
297 unsigned long unprotect_start
, unprotect_size
;
299 rounded_start
= rounddown(start
, EFI_PAGE_SIZE
);
300 rounded_end
= roundup(start
+ size
, EFI_PAGE_SIZE
);
302 if (memattr
!= NULL
) {
303 status
= efi_call_proto(memattr
, set_memory_attributes
,
305 rounded_end
- rounded_start
,
307 if (status
!= EFI_SUCCESS
) {
308 efi_warn("Failed to set EFI_MEMORY_RO attribute\n");
312 status
= efi_call_proto(memattr
, clear_memory_attributes
,
314 rounded_end
- rounded_start
,
316 if (status
!= EFI_SUCCESS
)
317 efi_warn("Failed to clear EFI_MEMORY_XP attribute\n");
321 if (efi_dxe_table
== NULL
)
325 * Don't modify memory region attributes, they are
326 * already suitable, to lower the possibility to
327 * encounter firmware bugs.
330 for (end
= start
+ size
; start
< end
; start
= next
) {
332 status
= efi_dxe_call(get_memory_space_descriptor
, start
, &desc
);
334 if (status
!= EFI_SUCCESS
)
337 next
= desc
.base_address
+ desc
.length
;
340 * Only system memory is suitable for trampoline/kernel image placement,
341 * so only this type of memory needs its attributes to be modified.
344 if (desc
.gcd_memory_type
!= EfiGcdMemoryTypeSystemMemory
||
345 (desc
.attributes
& (EFI_MEMORY_RO
| EFI_MEMORY_XP
)) == 0)
348 unprotect_start
= max(rounded_start
, (unsigned long)desc
.base_address
);
349 unprotect_size
= min(rounded_end
, next
) - unprotect_start
;
351 status
= efi_dxe_call(set_memory_space_attributes
,
352 unprotect_start
, unprotect_size
,
355 if (status
!= EFI_SUCCESS
) {
356 efi_warn("Unable to unprotect memory range [%08lx,%08lx]: %lx\n",
358 unprotect_start
+ unprotect_size
,
366 static void setup_unaccepted_memory(void)
368 efi_guid_t mem_acceptance_proto
= OVMF_SEV_MEMORY_ACCEPTANCE_PROTOCOL_GUID
;
369 sev_memory_acceptance_protocol_t
*proto
;
372 if (!IS_ENABLED(CONFIG_UNACCEPTED_MEMORY
))
376 * Enable unaccepted memory before calling exit boot services in order
377 * for the UEFI to not accept all memory on EBS.
379 status
= efi_bs_call(locate_protocol
, &mem_acceptance_proto
, NULL
,
381 if (status
!= EFI_SUCCESS
)
384 status
= efi_call_proto(proto
, allow_unaccepted_memory
);
385 if (status
!= EFI_SUCCESS
)
386 efi_err("Memory acceptance protocol failed\n");
389 static efi_char16_t
*efistub_fw_vendor(void)
391 unsigned long vendor
= efi_table_attr(efi_system_table
, fw_vendor
);
393 return (efi_char16_t
*)vendor
;
396 static const efi_char16_t apple
[] = L
"Apple";
398 static void setup_quirks(struct boot_params
*boot_params
)
400 if (!memcmp(efistub_fw_vendor(), apple
, sizeof(apple
))) {
401 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES
))
402 retrieve_apple_device_properties(boot_params
);
409 * See if we have Universal Graphics Adapter (UGA) protocol
412 setup_uga(struct screen_info
*si
, efi_guid_t
*uga_proto
, unsigned long size
)
416 void **uga_handle
= NULL
;
417 efi_uga_draw_protocol_t
*uga
= NULL
, *first_uga
;
421 status
= efi_bs_call(allocate_pool
, EFI_LOADER_DATA
, size
,
422 (void **)&uga_handle
);
423 if (status
!= EFI_SUCCESS
)
426 status
= efi_bs_call(locate_handle
, EFI_LOCATE_BY_PROTOCOL
,
427 uga_proto
, NULL
, &size
, uga_handle
);
428 if (status
!= EFI_SUCCESS
)
435 for_each_efi_handle(handle
, uga_handle
, size
, i
) {
436 efi_guid_t pciio_proto
= EFI_PCI_IO_PROTOCOL_GUID
;
437 u32 w
, h
, depth
, refresh
;
440 status
= efi_bs_call(handle_protocol
, handle
, uga_proto
,
442 if (status
!= EFI_SUCCESS
)
446 efi_bs_call(handle_protocol
, handle
, &pciio_proto
, &pciio
);
448 status
= efi_call_proto(uga
, get_mode
, &w
, &h
, &depth
, &refresh
);
449 if (status
== EFI_SUCCESS
&& (!first_uga
|| pciio
)) {
454 * Once we've found a UGA supporting PCIIO,
455 * don't bother looking any further.
464 if (!width
&& !height
)
467 /* EFI framebuffer */
468 si
->orig_video_isVGA
= VIDEO_TYPE_EFI
;
471 si
->lfb_width
= width
;
472 si
->lfb_height
= height
;
484 efi_bs_call(free_pool
, uga_handle
);
489 static void setup_graphics(struct boot_params
*boot_params
)
491 efi_guid_t graphics_proto
= EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID
;
492 struct screen_info
*si
;
493 efi_guid_t uga_proto
= EFI_UGA_PROTOCOL_GUID
;
496 void **gop_handle
= NULL
;
497 void **uga_handle
= NULL
;
499 si
= &boot_params
->screen_info
;
500 memset(si
, 0, sizeof(*si
));
503 status
= efi_bs_call(locate_handle
, EFI_LOCATE_BY_PROTOCOL
,
504 &graphics_proto
, NULL
, &size
, gop_handle
);
505 if (status
== EFI_BUFFER_TOO_SMALL
)
506 status
= efi_setup_gop(si
, &graphics_proto
, size
);
508 if (status
!= EFI_SUCCESS
) {
510 status
= efi_bs_call(locate_handle
, EFI_LOCATE_BY_PROTOCOL
,
511 &uga_proto
, NULL
, &size
, uga_handle
);
512 if (status
== EFI_BUFFER_TOO_SMALL
)
513 setup_uga(si
, &uga_proto
, size
);
518 static void __noreturn
efi_exit(efi_handle_t handle
, efi_status_t status
)
520 efi_bs_call(exit
, handle
, status
, 0, NULL
);
525 void __noreturn
efi_stub_entry(efi_handle_t handle
,
526 efi_system_table_t
*sys_table_arg
,
527 struct boot_params
*boot_params
);
530 * Because the x86 boot code expects to be passed a boot_params we
531 * need to create one ourselves (usually the bootloader would create
534 efi_status_t __efiapi
efi_pe_entry(efi_handle_t handle
,
535 efi_system_table_t
*sys_table_arg
)
537 efi_guid_t proto
= LOADED_IMAGE_PROTOCOL_GUID
;
538 struct boot_params
*boot_params
;
539 struct setup_header
*hdr
;
544 efi_system_table
= sys_table_arg
;
546 /* Check if we were booted by the EFI firmware */
547 if (efi_system_table
->hdr
.signature
!= EFI_SYSTEM_TABLE_SIGNATURE
)
548 efi_exit(handle
, EFI_INVALID_PARAMETER
);
550 status
= efi_bs_call(handle_protocol
, handle
, &proto
, (void **)&image
);
551 if (status
!= EFI_SUCCESS
) {
552 efi_err("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
553 efi_exit(handle
, status
);
556 status
= efi_allocate_pages(PARAM_SIZE
, &alloc
, ULONG_MAX
);
557 if (status
!= EFI_SUCCESS
)
558 efi_exit(handle
, status
);
560 boot_params
= memset((void *)alloc
, 0x0, PARAM_SIZE
);
561 hdr
= &boot_params
->hdr
;
563 /* Assign the setup_header fields that the kernel actually cares about */
565 hdr
->vid_mode
= 0xffff;
567 hdr
->type_of_loader
= 0x21;
568 hdr
->initrd_addr_max
= INT_MAX
;
570 /* Convert unicode cmdline to ascii */
571 cmdline_ptr
= efi_convert_cmdline(image
);
573 efi_free(PARAM_SIZE
, alloc
);
574 efi_exit(handle
, EFI_OUT_OF_RESOURCES
);
577 efi_set_u64_split((unsigned long)cmdline_ptr
, &hdr
->cmd_line_ptr
,
578 &boot_params
->ext_cmd_line_ptr
);
580 efi_stub_entry(handle
, sys_table_arg
, boot_params
);
584 static void add_e820ext(struct boot_params
*params
,
585 struct setup_data
*e820ext
, u32 nr_entries
)
587 struct setup_data
*data
;
589 e820ext
->type
= SETUP_E820_EXT
;
590 e820ext
->len
= nr_entries
* sizeof(struct boot_e820_entry
);
593 data
= (struct setup_data
*)(unsigned long)params
->hdr
.setup_data
;
595 while (data
&& data
->next
)
596 data
= (struct setup_data
*)(unsigned long)data
->next
;
599 data
->next
= (unsigned long)e820ext
;
601 params
->hdr
.setup_data
= (unsigned long)e820ext
;
605 setup_e820(struct boot_params
*params
, struct setup_data
*e820ext
, u32 e820ext_size
)
607 struct boot_e820_entry
*entry
= params
->e820_table
;
608 struct efi_info
*efi
= ¶ms
->efi_info
;
609 struct boot_e820_entry
*prev
= NULL
;
615 nr_desc
= efi
->efi_memmap_size
/ efi
->efi_memdesc_size
;
617 for (i
= 0; i
< nr_desc
; i
++) {
618 efi_memory_desc_t
*d
;
619 unsigned int e820_type
= 0;
620 unsigned long m
= efi
->efi_memmap
;
623 m
|= (u64
)efi
->efi_memmap_hi
<< 32;
626 d
= efi_memdesc_ptr(m
, efi
->efi_memdesc_size
, i
);
628 case EFI_RESERVED_TYPE
:
629 case EFI_RUNTIME_SERVICES_CODE
:
630 case EFI_RUNTIME_SERVICES_DATA
:
631 case EFI_MEMORY_MAPPED_IO
:
632 case EFI_MEMORY_MAPPED_IO_PORT_SPACE
:
634 e820_type
= E820_TYPE_RESERVED
;
637 case EFI_UNUSABLE_MEMORY
:
638 e820_type
= E820_TYPE_UNUSABLE
;
641 case EFI_ACPI_RECLAIM_MEMORY
:
642 e820_type
= E820_TYPE_ACPI
;
645 case EFI_LOADER_CODE
:
646 case EFI_LOADER_DATA
:
647 case EFI_BOOT_SERVICES_CODE
:
648 case EFI_BOOT_SERVICES_DATA
:
649 case EFI_CONVENTIONAL_MEMORY
:
650 if (efi_soft_reserve_enabled() &&
651 (d
->attribute
& EFI_MEMORY_SP
))
652 e820_type
= E820_TYPE_SOFT_RESERVED
;
654 e820_type
= E820_TYPE_RAM
;
657 case EFI_ACPI_MEMORY_NVS
:
658 e820_type
= E820_TYPE_NVS
;
661 case EFI_PERSISTENT_MEMORY
:
662 e820_type
= E820_TYPE_PMEM
;
665 case EFI_UNACCEPTED_MEMORY
:
666 if (!IS_ENABLED(CONFIG_UNACCEPTED_MEMORY
))
668 e820_type
= E820_TYPE_RAM
;
669 process_unaccepted_memory(d
->phys_addr
,
670 d
->phys_addr
+ PAGE_SIZE
* d
->num_pages
);
676 /* Merge adjacent mappings */
677 if (prev
&& prev
->type
== e820_type
&&
678 (prev
->addr
+ prev
->size
) == d
->phys_addr
) {
679 prev
->size
+= d
->num_pages
<< 12;
683 if (nr_entries
== ARRAY_SIZE(params
->e820_table
)) {
684 u32 need
= (nr_desc
- i
) * sizeof(struct e820_entry
) +
685 sizeof(struct setup_data
);
687 if (!e820ext
|| e820ext_size
< need
)
688 return EFI_BUFFER_TOO_SMALL
;
690 /* boot_params map full, switch to e820 extended */
691 entry
= (struct boot_e820_entry
*)e820ext
->data
;
694 entry
->addr
= d
->phys_addr
;
695 entry
->size
= d
->num_pages
<< PAGE_SHIFT
;
696 entry
->type
= e820_type
;
701 if (nr_entries
> ARRAY_SIZE(params
->e820_table
)) {
702 u32 nr_e820ext
= nr_entries
- ARRAY_SIZE(params
->e820_table
);
704 add_e820ext(params
, e820ext
, nr_e820ext
);
705 nr_entries
-= nr_e820ext
;
708 params
->e820_entries
= (u8
)nr_entries
;
713 static efi_status_t
alloc_e820ext(u32 nr_desc
, struct setup_data
**e820ext
,
719 size
= sizeof(struct setup_data
) +
720 sizeof(struct e820_entry
) * nr_desc
;
723 efi_bs_call(free_pool
, *e820ext
);
728 status
= efi_bs_call(allocate_pool
, EFI_LOADER_DATA
, size
,
730 if (status
== EFI_SUCCESS
)
731 *e820ext_size
= size
;
736 static efi_status_t
allocate_e820(struct boot_params
*params
,
737 struct setup_data
**e820ext
,
740 struct efi_boot_memmap
*map
;
744 status
= efi_get_memory_map(&map
, false);
745 if (status
!= EFI_SUCCESS
)
748 nr_desc
= map
->map_size
/ map
->desc_size
;
749 if (nr_desc
> ARRAY_SIZE(params
->e820_table
) - EFI_MMAP_NR_SLACK_SLOTS
) {
750 u32 nr_e820ext
= nr_desc
- ARRAY_SIZE(params
->e820_table
) +
751 EFI_MMAP_NR_SLACK_SLOTS
;
753 status
= alloc_e820ext(nr_e820ext
, e820ext
, e820ext_size
);
756 if (IS_ENABLED(CONFIG_UNACCEPTED_MEMORY
) && status
== EFI_SUCCESS
)
757 status
= allocate_unaccepted_bitmap(nr_desc
, map
);
759 efi_bs_call(free_pool
, map
);
763 struct exit_boot_struct
{
764 struct boot_params
*boot_params
;
765 struct efi_info
*efi
;
768 static efi_status_t
exit_boot_func(struct efi_boot_memmap
*map
,
771 const char *signature
;
772 struct exit_boot_struct
*p
= priv
;
774 signature
= efi_is_64bit() ? EFI64_LOADER_SIGNATURE
775 : EFI32_LOADER_SIGNATURE
;
776 memcpy(&p
->efi
->efi_loader_signature
, signature
, sizeof(__u32
));
778 efi_set_u64_split((unsigned long)efi_system_table
,
779 &p
->efi
->efi_systab
, &p
->efi
->efi_systab_hi
);
780 p
->efi
->efi_memdesc_size
= map
->desc_size
;
781 p
->efi
->efi_memdesc_version
= map
->desc_ver
;
782 efi_set_u64_split((unsigned long)map
->map
,
783 &p
->efi
->efi_memmap
, &p
->efi
->efi_memmap_hi
);
784 p
->efi
->efi_memmap_size
= map
->map_size
;
789 static efi_status_t
exit_boot(struct boot_params
*boot_params
, void *handle
)
791 struct setup_data
*e820ext
= NULL
;
792 __u32 e820ext_size
= 0;
794 struct exit_boot_struct priv
;
796 priv
.boot_params
= boot_params
;
797 priv
.efi
= &boot_params
->efi_info
;
799 status
= allocate_e820(boot_params
, &e820ext
, &e820ext_size
);
800 if (status
!= EFI_SUCCESS
)
803 /* Might as well exit boot services now */
804 status
= efi_exit_boot_services(handle
, &priv
, exit_boot_func
);
805 if (status
!= EFI_SUCCESS
)
809 boot_params
->alt_mem_k
= 32 * 1024;
811 status
= setup_e820(boot_params
, e820ext
, e820ext_size
);
812 if (status
!= EFI_SUCCESS
)
818 static bool have_unsupported_snp_features(void)
822 unsupported
= snp_get_unsupported_features(sev_get_status());
824 efi_err("Unsupported SEV-SNP features detected: 0x%llx\n",
831 static void efi_get_seed(void *seed
, int size
)
833 efi_get_random_bytes(size
, seed
);
836 * This only updates seed[0] when running on 32-bit, but in that case,
837 * seed[1] is not used anyway, as there is no virtual KASLR on 32-bit.
839 *(unsigned long *)seed
^= kaslr_get_random_long("EFI");
842 static void error(char *str
)
844 efi_warn("Decompression failed: %s\n", str
);
847 static const char *cmdline_memmap_override
;
849 static efi_status_t
parse_options(const char *cmdline
)
851 static const char opts
[][14] = {
852 "mem=", "memmap=", "hugepages="
855 for (int i
= 0; i
< ARRAY_SIZE(opts
); i
++) {
856 const char *p
= strstr(cmdline
, opts
[i
]);
858 if (p
== cmdline
|| (p
> cmdline
&& isspace(p
[-1]))) {
859 cmdline_memmap_override
= opts
[i
];
864 return efi_parse_options(cmdline
);
867 static efi_status_t
efi_decompress_kernel(unsigned long *kernel_entry
)
869 unsigned long virt_addr
= LOAD_PHYSICAL_ADDR
;
870 unsigned long addr
, alloc_size
, entry
;
874 /* determine the required size of the allocation */
875 alloc_size
= ALIGN(max_t(unsigned long, output_len
, kernel_total_size
),
878 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE
) && !efi_nokaslr
) {
879 u64 range
= KERNEL_IMAGE_SIZE
- LOAD_PHYSICAL_ADDR
- kernel_total_size
;
880 static const efi_char16_t ami
[] = L
"American Megatrends";
882 efi_get_seed(seed
, sizeof(seed
));
884 virt_addr
+= (range
* seed
[1]) >> 32;
885 virt_addr
&= ~(CONFIG_PHYSICAL_ALIGN
- 1);
888 * Older Dell systems with AMI UEFI firmware v2.0 may hang
889 * while decompressing the kernel if physical address
890 * randomization is enabled.
892 * https://bugzilla.kernel.org/show_bug.cgi?id=218173
894 if (efi_system_table
->hdr
.revision
<= EFI_2_00_SYSTEM_TABLE_REVISION
&&
895 !memcmp(efistub_fw_vendor(), ami
, sizeof(ami
))) {
896 efi_debug("AMI firmware v2.0 or older detected - disabling physical KASLR\n");
898 } else if (cmdline_memmap_override
) {
899 efi_info("%s detected on the kernel command line - disabling physical KASLR\n",
900 cmdline_memmap_override
);
904 boot_params_ptr
->hdr
.loadflags
|= KASLR_FLAG
;
907 status
= efi_random_alloc(alloc_size
, CONFIG_PHYSICAL_ALIGN
, &addr
,
908 seed
[0], EFI_LOADER_CODE
,
910 EFI_X86_KERNEL_ALLOC_LIMIT
);
911 if (status
!= EFI_SUCCESS
)
914 entry
= decompress_kernel((void *)addr
, virt_addr
, error
);
915 if (entry
== ULONG_MAX
) {
916 efi_free(alloc_size
, addr
);
917 return EFI_LOAD_ERROR
;
920 *kernel_entry
= addr
+ entry
;
922 return efi_adjust_memory_range_protection(addr
, kernel_text_size
);
925 static void __noreturn
enter_kernel(unsigned long kernel_addr
,
926 struct boot_params
*boot_params
)
928 /* enter decompressed kernel with boot_params pointer in RSI/ESI */
929 asm("jmp *%0"::"r"(kernel_addr
), "S"(boot_params
));
935 * On success, this routine will jump to the relocated image directly and never
936 * return. On failure, it will exit to the firmware via efi_exit() instead of
939 void __noreturn
efi_stub_entry(efi_handle_t handle
,
940 efi_system_table_t
*sys_table_arg
,
941 struct boot_params
*boot_params
)
943 efi_guid_t guid
= EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID
;
944 struct setup_header
*hdr
= &boot_params
->hdr
;
945 const struct linux_efi_initrd
*initrd
= NULL
;
946 unsigned long kernel_entry
;
949 boot_params_ptr
= boot_params
;
951 efi_system_table
= sys_table_arg
;
952 /* Check if we were booted by the EFI firmware */
953 if (efi_system_table
->hdr
.signature
!= EFI_SYSTEM_TABLE_SIGNATURE
)
954 efi_exit(handle
, EFI_INVALID_PARAMETER
);
956 if (have_unsupported_snp_features())
957 efi_exit(handle
, EFI_UNSUPPORTED
);
959 if (IS_ENABLED(CONFIG_EFI_DXE_MEM_ATTRIBUTES
)) {
960 efi_dxe_table
= get_efi_config_table(EFI_DXE_SERVICES_TABLE_GUID
);
962 efi_dxe_table
->hdr
.signature
!= EFI_DXE_SERVICES_TABLE_SIGNATURE
) {
963 efi_warn("Ignoring DXE services table: invalid signature\n");
964 efi_dxe_table
= NULL
;
968 /* grab the memory attributes protocol if it exists */
969 efi_bs_call(locate_protocol
, &guid
, NULL
, (void **)&memattr
);
971 status
= efi_setup_5level_paging();
972 if (status
!= EFI_SUCCESS
) {
973 efi_err("efi_setup_5level_paging() failed!\n");
977 #ifdef CONFIG_CMDLINE_BOOL
978 status
= parse_options(CONFIG_CMDLINE
);
979 if (status
!= EFI_SUCCESS
) {
980 efi_err("Failed to parse options\n");
984 if (!IS_ENABLED(CONFIG_CMDLINE_OVERRIDE
)) {
985 unsigned long cmdline_paddr
= ((u64
)hdr
->cmd_line_ptr
|
986 ((u64
)boot_params
->ext_cmd_line_ptr
<< 32));
987 status
= parse_options((char *)cmdline_paddr
);
988 if (status
!= EFI_SUCCESS
) {
989 efi_err("Failed to parse options\n");
994 if (efi_mem_encrypt
> 0)
995 hdr
->xloadflags
|= XLF_MEM_ENCRYPTION
;
997 status
= efi_decompress_kernel(&kernel_entry
);
998 if (status
!= EFI_SUCCESS
) {
999 efi_err("Failed to decompress kernel\n");
1004 * At this point, an initrd may already have been loaded by the
1005 * bootloader and passed via bootparams. We permit an initrd loaded
1006 * from the LINUX_EFI_INITRD_MEDIA_GUID device path to supersede it.
1008 * If the device path is not present, any command-line initrd=
1009 * arguments will be processed only if image is not NULL, which will be
1010 * the case only if we were loaded via the PE entry point.
1012 status
= efi_load_initrd(image
, hdr
->initrd_addr_max
, ULONG_MAX
,
1014 if (status
!= EFI_SUCCESS
)
1016 if (initrd
&& initrd
->size
> 0) {
1017 efi_set_u64_split(initrd
->base
, &hdr
->ramdisk_image
,
1018 &boot_params
->ext_ramdisk_image
);
1019 efi_set_u64_split(initrd
->size
, &hdr
->ramdisk_size
,
1020 &boot_params
->ext_ramdisk_size
);
1025 * If the boot loader gave us a value for secure_boot then we use that,
1026 * otherwise we ask the BIOS.
1028 if (boot_params
->secure_boot
== efi_secureboot_mode_unset
)
1029 boot_params
->secure_boot
= efi_get_secureboot();
1031 /* Ask the firmware to clear memory on unclean shutdown */
1032 efi_enable_reset_attack_mitigation();
1034 efi_random_get_seed();
1036 efi_retrieve_eventlog();
1038 setup_graphics(boot_params
);
1040 setup_efi_pci(boot_params
);
1042 setup_quirks(boot_params
);
1044 setup_unaccepted_memory();
1046 status
= exit_boot(boot_params
, handle
);
1047 if (status
!= EFI_SUCCESS
) {
1048 efi_err("exit_boot() failed!\n");
1053 * Call the SEV init code while still running with the firmware's
1054 * GDT/IDT, so #VC exceptions will be handled by EFI.
1056 sev_enable(boot_params
);
1058 efi_5level_switch();
1060 enter_kernel(kernel_entry
, boot_params
);
1062 efi_err("efi_stub_entry() failed!\n");
1064 efi_exit(handle
, status
);
1067 #ifdef CONFIG_EFI_HANDOVER_PROTOCOL
1068 void efi_handover_entry(efi_handle_t handle
, efi_system_table_t
*sys_table_arg
,
1069 struct boot_params
*boot_params
)
1071 memset(_bss
, 0, _ebss
- _bss
);
1072 efi_stub_entry(handle
, sys_table_arg
, boot_params
);
1075 #ifndef CONFIG_EFI_MIXED
1076 extern __alias(efi_handover_entry
)
1077 void efi32_stub_entry(efi_handle_t handle
, efi_system_table_t
*sys_table_arg
,
1078 struct boot_params
*boot_params
);
1080 extern __alias(efi_handover_entry
)
1081 void efi64_stub_entry(efi_handle_t handle
, efi_system_table_t
*sys_table_arg
,
1082 struct boot_params
*boot_params
);