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 efi_status_t efi_adjust_memory_range_protection(unsigned long start,
232 efi_gcd_memory_space_desc_t desc;
233 unsigned long end, next;
234 unsigned long rounded_start, rounded_end;
235 unsigned long unprotect_start, unprotect_size;
237 rounded_start = rounddown(start, EFI_PAGE_SIZE);
238 rounded_end = roundup(start + size, EFI_PAGE_SIZE);
240 if (memattr != NULL) {
241 status = efi_call_proto(memattr, set_memory_attributes,
243 rounded_end - rounded_start,
245 if (status != EFI_SUCCESS) {
246 efi_warn("Failed to set EFI_MEMORY_RO attribute\n");
250 status = efi_call_proto(memattr, clear_memory_attributes,
252 rounded_end - rounded_start,
254 if (status != EFI_SUCCESS)
255 efi_warn("Failed to clear EFI_MEMORY_XP attribute\n");
259 if (efi_dxe_table == NULL)
263 * Don't modify memory region attributes, they are
264 * already suitable, to lower the possibility to
265 * encounter firmware bugs.
268 for (end = start + size; start < end; start = next) {
270 status = efi_dxe_call(get_memory_space_descriptor, start, &desc);
272 if (status != EFI_SUCCESS)
275 next = desc.base_address + desc.length;
278 * Only system memory is suitable for trampoline/kernel image placement,
279 * so only this type of memory needs its attributes to be modified.
282 if (desc.gcd_memory_type != EfiGcdMemoryTypeSystemMemory ||
283 (desc.attributes & (EFI_MEMORY_RO | EFI_MEMORY_XP)) == 0)
286 unprotect_start = max(rounded_start, (unsigned long)desc.base_address);
287 unprotect_size = min(rounded_end, next) - unprotect_start;
289 status = efi_dxe_call(set_memory_space_attributes,
290 unprotect_start, unprotect_size,
293 if (status != EFI_SUCCESS) {
294 efi_warn("Unable to unprotect memory range [%08lx,%08lx]: %lx\n",
296 unprotect_start + unprotect_size,
304 static void setup_unaccepted_memory(void)
306 efi_guid_t mem_acceptance_proto = OVMF_SEV_MEMORY_ACCEPTANCE_PROTOCOL_GUID;
307 sev_memory_acceptance_protocol_t *proto;
310 if (!IS_ENABLED(CONFIG_UNACCEPTED_MEMORY))
314 * Enable unaccepted memory before calling exit boot services in order
315 * for the UEFI to not accept all memory on EBS.
317 status = efi_bs_call(locate_protocol, &mem_acceptance_proto, NULL,
319 if (status != EFI_SUCCESS)
322 status = efi_call_proto(proto, allow_unaccepted_memory);
323 if (status != EFI_SUCCESS)
324 efi_err("Memory acceptance protocol failed\n");
327 static efi_char16_t *efistub_fw_vendor(void)
329 unsigned long vendor = efi_table_attr(efi_system_table, fw_vendor);
331 return (efi_char16_t *)vendor;
334 static const efi_char16_t apple[] = L"Apple";
336 static void setup_quirks(struct boot_params *boot_params)
338 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES) &&
339 !memcmp(efistub_fw_vendor(), apple, sizeof(apple)))
340 retrieve_apple_device_properties(boot_params);
344 * See if we have Universal Graphics Adapter (UGA) protocol
347 setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size)
351 void **uga_handle = NULL;
352 efi_uga_draw_protocol_t *uga = NULL, *first_uga;
356 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
357 (void **)&uga_handle);
358 if (status != EFI_SUCCESS)
361 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
362 uga_proto, NULL, &size, uga_handle);
363 if (status != EFI_SUCCESS)
370 for_each_efi_handle(handle, uga_handle, size, i) {
371 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
372 u32 w, h, depth, refresh;
375 status = efi_bs_call(handle_protocol, handle, uga_proto,
377 if (status != EFI_SUCCESS)
381 efi_bs_call(handle_protocol, handle, &pciio_proto, &pciio);
383 status = efi_call_proto(uga, get_mode, &w, &h, &depth, &refresh);
384 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
389 * Once we've found a UGA supporting PCIIO,
390 * don't bother looking any further.
399 if (!width && !height)
402 /* EFI framebuffer */
403 si->orig_video_isVGA = VIDEO_TYPE_EFI;
406 si->lfb_width = width;
407 si->lfb_height = height;
419 efi_bs_call(free_pool, uga_handle);
424 static void setup_graphics(struct boot_params *boot_params)
426 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
427 struct screen_info *si;
428 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
431 void **gop_handle = NULL;
432 void **uga_handle = NULL;
434 si = &boot_params->screen_info;
435 memset(si, 0, sizeof(*si));
438 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
439 &graphics_proto, NULL, &size, gop_handle);
440 if (status == EFI_BUFFER_TOO_SMALL)
441 status = efi_setup_gop(si, &graphics_proto, size);
443 if (status != EFI_SUCCESS) {
445 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
446 &uga_proto, NULL, &size, uga_handle);
447 if (status == EFI_BUFFER_TOO_SMALL)
448 setup_uga(si, &uga_proto, size);
453 static void __noreturn efi_exit(efi_handle_t handle, efi_status_t status)
455 efi_bs_call(exit, handle, status, 0, NULL);
460 void __noreturn efi_stub_entry(efi_handle_t handle,
461 efi_system_table_t *sys_table_arg,
462 struct boot_params *boot_params);
465 * Because the x86 boot code expects to be passed a boot_params we
466 * need to create one ourselves (usually the bootloader would create
469 efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
470 efi_system_table_t *sys_table_arg)
472 static struct boot_params boot_params __page_aligned_bss;
473 struct setup_header *hdr = &boot_params.hdr;
474 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
475 int options_size = 0;
480 memset(_bss, 0, _ebss - _bss);
482 efi_system_table = sys_table_arg;
484 /* Check if we were booted by the EFI firmware */
485 if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
486 efi_exit(handle, EFI_INVALID_PARAMETER);
488 status = efi_bs_call(handle_protocol, handle, &proto, (void **)&image);
489 if (status != EFI_SUCCESS) {
490 efi_err("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
491 efi_exit(handle, status);
494 /* Assign the setup_header fields that the kernel actually cares about */
496 hdr->vid_mode = 0xffff;
498 hdr->type_of_loader = 0x21;
500 /* Convert unicode cmdline to ascii */
501 cmdline_ptr = efi_convert_cmdline(image, &options_size);
505 efi_set_u64_split((unsigned long)cmdline_ptr, &hdr->cmd_line_ptr,
506 &boot_params.ext_cmd_line_ptr);
508 efi_stub_entry(handle, sys_table_arg, &boot_params);
512 efi_exit(handle, status);
515 static void add_e820ext(struct boot_params *params,
516 struct setup_data *e820ext, u32 nr_entries)
518 struct setup_data *data;
520 e820ext->type = SETUP_E820_EXT;
521 e820ext->len = nr_entries * sizeof(struct boot_e820_entry);
524 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
526 while (data && data->next)
527 data = (struct setup_data *)(unsigned long)data->next;
530 data->next = (unsigned long)e820ext;
532 params->hdr.setup_data = (unsigned long)e820ext;
536 setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size)
538 struct boot_e820_entry *entry = params->e820_table;
539 struct efi_info *efi = ¶ms->efi_info;
540 struct boot_e820_entry *prev = NULL;
546 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
548 for (i = 0; i < nr_desc; i++) {
549 efi_memory_desc_t *d;
550 unsigned int e820_type = 0;
551 unsigned long m = efi->efi_memmap;
554 m |= (u64)efi->efi_memmap_hi << 32;
557 d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
559 case EFI_RESERVED_TYPE:
560 case EFI_RUNTIME_SERVICES_CODE:
561 case EFI_RUNTIME_SERVICES_DATA:
562 case EFI_MEMORY_MAPPED_IO:
563 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
565 e820_type = E820_TYPE_RESERVED;
568 case EFI_UNUSABLE_MEMORY:
569 e820_type = E820_TYPE_UNUSABLE;
572 case EFI_ACPI_RECLAIM_MEMORY:
573 e820_type = E820_TYPE_ACPI;
576 case EFI_LOADER_CODE:
577 case EFI_LOADER_DATA:
578 case EFI_BOOT_SERVICES_CODE:
579 case EFI_BOOT_SERVICES_DATA:
580 case EFI_CONVENTIONAL_MEMORY:
581 if (efi_soft_reserve_enabled() &&
582 (d->attribute & EFI_MEMORY_SP))
583 e820_type = E820_TYPE_SOFT_RESERVED;
585 e820_type = E820_TYPE_RAM;
588 case EFI_ACPI_MEMORY_NVS:
589 e820_type = E820_TYPE_NVS;
592 case EFI_PERSISTENT_MEMORY:
593 e820_type = E820_TYPE_PMEM;
596 case EFI_UNACCEPTED_MEMORY:
597 if (!IS_ENABLED(CONFIG_UNACCEPTED_MEMORY))
599 e820_type = E820_TYPE_RAM;
600 process_unaccepted_memory(d->phys_addr,
601 d->phys_addr + PAGE_SIZE * d->num_pages);
607 /* Merge adjacent mappings */
608 if (prev && prev->type == e820_type &&
609 (prev->addr + prev->size) == d->phys_addr) {
610 prev->size += d->num_pages << 12;
614 if (nr_entries == ARRAY_SIZE(params->e820_table)) {
615 u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
616 sizeof(struct setup_data);
618 if (!e820ext || e820ext_size < need)
619 return EFI_BUFFER_TOO_SMALL;
621 /* boot_params map full, switch to e820 extended */
622 entry = (struct boot_e820_entry *)e820ext->data;
625 entry->addr = d->phys_addr;
626 entry->size = d->num_pages << PAGE_SHIFT;
627 entry->type = e820_type;
632 if (nr_entries > ARRAY_SIZE(params->e820_table)) {
633 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
635 add_e820ext(params, e820ext, nr_e820ext);
636 nr_entries -= nr_e820ext;
639 params->e820_entries = (u8)nr_entries;
644 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
650 size = sizeof(struct setup_data) +
651 sizeof(struct e820_entry) * nr_desc;
654 efi_bs_call(free_pool, *e820ext);
659 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
661 if (status == EFI_SUCCESS)
662 *e820ext_size = size;
667 static efi_status_t allocate_e820(struct boot_params *params,
668 struct setup_data **e820ext,
671 struct efi_boot_memmap *map;
675 status = efi_get_memory_map(&map, false);
676 if (status != EFI_SUCCESS)
679 nr_desc = map->map_size / map->desc_size;
680 if (nr_desc > ARRAY_SIZE(params->e820_table) - EFI_MMAP_NR_SLACK_SLOTS) {
681 u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table) +
682 EFI_MMAP_NR_SLACK_SLOTS;
684 status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size);
687 if (IS_ENABLED(CONFIG_UNACCEPTED_MEMORY) && status == EFI_SUCCESS)
688 status = allocate_unaccepted_bitmap(nr_desc, map);
690 efi_bs_call(free_pool, map);
694 struct exit_boot_struct {
695 struct boot_params *boot_params;
696 struct efi_info *efi;
699 static efi_status_t exit_boot_func(struct efi_boot_memmap *map,
702 const char *signature;
703 struct exit_boot_struct *p = priv;
705 signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE
706 : EFI32_LOADER_SIGNATURE;
707 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
709 efi_set_u64_split((unsigned long)efi_system_table,
710 &p->efi->efi_systab, &p->efi->efi_systab_hi);
711 p->efi->efi_memdesc_size = map->desc_size;
712 p->efi->efi_memdesc_version = map->desc_ver;
713 efi_set_u64_split((unsigned long)map->map,
714 &p->efi->efi_memmap, &p->efi->efi_memmap_hi);
715 p->efi->efi_memmap_size = map->map_size;
720 static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)
722 struct setup_data *e820ext = NULL;
723 __u32 e820ext_size = 0;
725 struct exit_boot_struct priv;
727 priv.boot_params = boot_params;
728 priv.efi = &boot_params->efi_info;
730 status = allocate_e820(boot_params, &e820ext, &e820ext_size);
731 if (status != EFI_SUCCESS)
734 /* Might as well exit boot services now */
735 status = efi_exit_boot_services(handle, &priv, exit_boot_func);
736 if (status != EFI_SUCCESS)
740 boot_params->alt_mem_k = 32 * 1024;
742 status = setup_e820(boot_params, e820ext, e820ext_size);
743 if (status != EFI_SUCCESS)
749 static bool have_unsupported_snp_features(void)
753 unsupported = snp_get_unsupported_features(sev_get_status());
755 efi_err("Unsupported SEV-SNP features detected: 0x%llx\n",
762 static void efi_get_seed(void *seed, int size)
764 efi_get_random_bytes(size, seed);
767 * This only updates seed[0] when running on 32-bit, but in that case,
768 * seed[1] is not used anyway, as there is no virtual KASLR on 32-bit.
770 *(unsigned long *)seed ^= kaslr_get_random_long("EFI");
773 static void error(char *str)
775 efi_warn("Decompression failed: %s\n", str);
778 static efi_status_t efi_decompress_kernel(unsigned long *kernel_entry)
780 unsigned long virt_addr = LOAD_PHYSICAL_ADDR;
781 unsigned long addr, alloc_size, entry;
785 /* determine the required size of the allocation */
786 alloc_size = ALIGN(max_t(unsigned long, output_len, kernel_total_size),
789 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && !efi_nokaslr) {
790 u64 range = KERNEL_IMAGE_SIZE - LOAD_PHYSICAL_ADDR - kernel_total_size;
791 static const efi_char16_t ami[] = L"American Megatrends";
793 efi_get_seed(seed, sizeof(seed));
795 virt_addr += (range * seed[1]) >> 32;
796 virt_addr &= ~(CONFIG_PHYSICAL_ALIGN - 1);
799 * Older Dell systems with AMI UEFI firmware v2.0 may hang
800 * while decompressing the kernel if physical address
801 * randomization is enabled.
803 * https://bugzilla.kernel.org/show_bug.cgi?id=218173
805 if (efi_system_table->hdr.revision <= EFI_2_00_SYSTEM_TABLE_REVISION &&
806 !memcmp(efistub_fw_vendor(), ami, sizeof(ami))) {
807 efi_debug("AMI firmware v2.0 or older detected - disabling physical KASLR\n");
811 boot_params_ptr->hdr.loadflags |= KASLR_FLAG;
814 status = efi_random_alloc(alloc_size, CONFIG_PHYSICAL_ALIGN, &addr,
815 seed[0], EFI_LOADER_CODE,
817 EFI_X86_KERNEL_ALLOC_LIMIT);
818 if (status != EFI_SUCCESS)
821 entry = decompress_kernel((void *)addr, virt_addr, error);
822 if (entry == ULONG_MAX) {
823 efi_free(alloc_size, addr);
824 return EFI_LOAD_ERROR;
827 *kernel_entry = addr + entry;
829 return efi_adjust_memory_range_protection(addr, kernel_text_size);
832 static void __noreturn enter_kernel(unsigned long kernel_addr,
833 struct boot_params *boot_params)
835 /* enter decompressed kernel with boot_params pointer in RSI/ESI */
836 asm("jmp *%0"::"r"(kernel_addr), "S"(boot_params));
842 * On success, this routine will jump to the relocated image directly and never
843 * return. On failure, it will exit to the firmware via efi_exit() instead of
846 void __noreturn efi_stub_entry(efi_handle_t handle,
847 efi_system_table_t *sys_table_arg,
848 struct boot_params *boot_params)
850 efi_guid_t guid = EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID;
851 struct setup_header *hdr = &boot_params->hdr;
852 const struct linux_efi_initrd *initrd = NULL;
853 unsigned long kernel_entry;
856 boot_params_ptr = boot_params;
858 efi_system_table = sys_table_arg;
859 /* Check if we were booted by the EFI firmware */
860 if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
861 efi_exit(handle, EFI_INVALID_PARAMETER);
863 if (have_unsupported_snp_features())
864 efi_exit(handle, EFI_UNSUPPORTED);
866 if (IS_ENABLED(CONFIG_EFI_DXE_MEM_ATTRIBUTES)) {
867 efi_dxe_table = get_efi_config_table(EFI_DXE_SERVICES_TABLE_GUID);
869 efi_dxe_table->hdr.signature != EFI_DXE_SERVICES_TABLE_SIGNATURE) {
870 efi_warn("Ignoring DXE services table: invalid signature\n");
871 efi_dxe_table = NULL;
875 /* grab the memory attributes protocol if it exists */
876 efi_bs_call(locate_protocol, &guid, NULL, (void **)&memattr);
878 status = efi_setup_5level_paging();
879 if (status != EFI_SUCCESS) {
880 efi_err("efi_setup_5level_paging() failed!\n");
884 #ifdef CONFIG_CMDLINE_BOOL
885 status = efi_parse_options(CONFIG_CMDLINE);
886 if (status != EFI_SUCCESS) {
887 efi_err("Failed to parse options\n");
891 if (!IS_ENABLED(CONFIG_CMDLINE_OVERRIDE)) {
892 unsigned long cmdline_paddr = ((u64)hdr->cmd_line_ptr |
893 ((u64)boot_params->ext_cmd_line_ptr << 32));
894 status = efi_parse_options((char *)cmdline_paddr);
895 if (status != EFI_SUCCESS) {
896 efi_err("Failed to parse options\n");
901 if (efi_mem_encrypt > 0)
902 hdr->xloadflags |= XLF_MEM_ENCRYPTION;
904 status = efi_decompress_kernel(&kernel_entry);
905 if (status != EFI_SUCCESS) {
906 efi_err("Failed to decompress kernel\n");
911 * At this point, an initrd may already have been loaded by the
912 * bootloader and passed via bootparams. We permit an initrd loaded
913 * from the LINUX_EFI_INITRD_MEDIA_GUID device path to supersede it.
915 * If the device path is not present, any command-line initrd=
916 * arguments will be processed only if image is not NULL, which will be
917 * the case only if we were loaded via the PE entry point.
919 status = efi_load_initrd(image, hdr->initrd_addr_max, ULONG_MAX,
921 if (status != EFI_SUCCESS)
923 if (initrd && initrd->size > 0) {
924 efi_set_u64_split(initrd->base, &hdr->ramdisk_image,
925 &boot_params->ext_ramdisk_image);
926 efi_set_u64_split(initrd->size, &hdr->ramdisk_size,
927 &boot_params->ext_ramdisk_size);
932 * If the boot loader gave us a value for secure_boot then we use that,
933 * otherwise we ask the BIOS.
935 if (boot_params->secure_boot == efi_secureboot_mode_unset)
936 boot_params->secure_boot = efi_get_secureboot();
938 /* Ask the firmware to clear memory on unclean shutdown */
939 efi_enable_reset_attack_mitigation();
941 efi_random_get_seed();
943 efi_retrieve_eventlog();
945 setup_graphics(boot_params);
947 setup_efi_pci(boot_params);
949 setup_quirks(boot_params);
951 setup_unaccepted_memory();
953 status = exit_boot(boot_params, handle);
954 if (status != EFI_SUCCESS) {
955 efi_err("exit_boot() failed!\n");
960 * Call the SEV init code while still running with the firmware's
961 * GDT/IDT, so #VC exceptions will be handled by EFI.
963 sev_enable(boot_params);
967 enter_kernel(kernel_entry, boot_params);
969 efi_err("efi_stub_entry() failed!\n");
971 efi_exit(handle, status);
974 #ifdef CONFIG_EFI_HANDOVER_PROTOCOL
975 void efi_handover_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,
976 struct boot_params *boot_params)
978 memset(_bss, 0, _ebss - _bss);
979 efi_stub_entry(handle, sys_table_arg, boot_params);
982 #ifndef CONFIG_EFI_MIXED
983 extern __alias(efi_handover_entry)
984 void efi32_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,
985 struct boot_params *boot_params);
987 extern __alias(efi_handover_entry)
988 void efi64_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,
989 struct boot_params *boot_params);