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 const efi_system_table_t *efi_system_table;
25 const efi_dxe_services_table_t *efi_dxe_table;
26 static efi_loaded_image_t *image = NULL;
27 static efi_memory_attribute_protocol_t *memattr;
29 typedef union sev_memory_acceptance_protocol sev_memory_acceptance_protocol_t;
30 union sev_memory_acceptance_protocol {
32 efi_status_t (__efiapi * allow_unaccepted_memory)(
33 sev_memory_acceptance_protocol_t *);
36 u32 allow_unaccepted_memory;
41 preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
43 struct pci_setup_rom *rom = NULL;
50 * Some firmware images contain EFI function pointers at the place where
51 * the romimage and romsize fields are supposed to be. Typically the EFI
52 * code is mapped at high addresses, translating to an unrealistically
53 * large romsize. The UEFI spec limits the size of option ROMs to 16
54 * MiB so we reject any ROMs over 16 MiB in size to catch this.
56 romimage = efi_table_attr(pci, romimage);
57 romsize = efi_table_attr(pci, romsize);
58 if (!romimage || !romsize || romsize > SZ_16M)
59 return EFI_INVALID_PARAMETER;
61 size = romsize + sizeof(*rom);
63 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
65 if (status != EFI_SUCCESS) {
66 efi_err("Failed to allocate memory for 'rom'\n");
70 memset(rom, 0, sizeof(*rom));
72 rom->data.type = SETUP_PCI;
73 rom->data.len = size - sizeof(struct setup_data);
75 rom->pcilen = romsize;
78 status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
79 PCI_VENDOR_ID, 1, &rom->vendor);
81 if (status != EFI_SUCCESS) {
82 efi_err("Failed to read rom->vendor\n");
86 status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
87 PCI_DEVICE_ID, 1, &rom->devid);
89 if (status != EFI_SUCCESS) {
90 efi_err("Failed to read rom->devid\n");
94 status = efi_call_proto(pci, get_location, &rom->segment, &rom->bus,
95 &rom->device, &rom->function);
97 if (status != EFI_SUCCESS)
100 memcpy(rom->romdata, romimage, romsize);
104 efi_bs_call(free_pool, rom);
109 * There's no way to return an informative status from this function,
110 * because any analysis (and printing of error messages) needs to be
111 * done directly at the EFI function call-site.
113 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
114 * just didn't find any PCI devices, but there's no way to tell outside
115 * the context of the call.
117 static void setup_efi_pci(struct boot_params *params)
120 void **pci_handle = NULL;
121 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
122 unsigned long size = 0;
123 struct setup_data *data;
127 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
128 &pci_proto, NULL, &size, pci_handle);
130 if (status == EFI_BUFFER_TOO_SMALL) {
131 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
132 (void **)&pci_handle);
134 if (status != EFI_SUCCESS) {
135 efi_err("Failed to allocate memory for 'pci_handle'\n");
139 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
140 &pci_proto, NULL, &size, pci_handle);
143 if (status != EFI_SUCCESS)
146 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
148 while (data && data->next)
149 data = (struct setup_data *)(unsigned long)data->next;
151 for_each_efi_handle(h, pci_handle, size, i) {
152 efi_pci_io_protocol_t *pci = NULL;
153 struct pci_setup_rom *rom;
155 status = efi_bs_call(handle_protocol, h, &pci_proto,
157 if (status != EFI_SUCCESS || !pci)
160 status = preserve_pci_rom_image(pci, &rom);
161 if (status != EFI_SUCCESS)
165 data->next = (unsigned long)rom;
167 params->hdr.setup_data = (unsigned long)rom;
169 data = (struct setup_data *)rom;
173 efi_bs_call(free_pool, pci_handle);
176 static void retrieve_apple_device_properties(struct boot_params *boot_params)
178 efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
179 struct setup_data *data, *new;
182 apple_properties_protocol_t *p;
184 status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&p);
185 if (status != EFI_SUCCESS)
188 if (efi_table_attr(p, version) != 0x10000) {
189 efi_err("Unsupported properties proto version\n");
193 efi_call_proto(p, get_all, NULL, &size);
198 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
199 size + sizeof(struct setup_data),
201 if (status != EFI_SUCCESS) {
202 efi_err("Failed to allocate memory for 'properties'\n");
206 status = efi_call_proto(p, get_all, new->data, &size);
208 if (status == EFI_BUFFER_TOO_SMALL)
209 efi_bs_call(free_pool, new);
210 } while (status == EFI_BUFFER_TOO_SMALL);
212 new->type = SETUP_APPLE_PROPERTIES;
216 data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
218 boot_params->hdr.setup_data = (unsigned long)new;
221 data = (struct setup_data *)(unsigned long)data->next;
222 data->next = (unsigned long)new;
226 void efi_adjust_memory_range_protection(unsigned long start,
230 efi_gcd_memory_space_desc_t desc;
231 unsigned long end, next;
232 unsigned long rounded_start, rounded_end;
233 unsigned long unprotect_start, unprotect_size;
235 rounded_start = rounddown(start, EFI_PAGE_SIZE);
236 rounded_end = roundup(start + size, EFI_PAGE_SIZE);
238 if (memattr != NULL) {
239 efi_call_proto(memattr, clear_memory_attributes, rounded_start,
240 rounded_end - rounded_start, EFI_MEMORY_XP);
244 if (efi_dxe_table == NULL)
248 * Don't modify memory region attributes, they are
249 * already suitable, to lower the possibility to
250 * encounter firmware bugs.
253 for (end = start + size; start < end; start = next) {
255 status = efi_dxe_call(get_memory_space_descriptor, start, &desc);
257 if (status != EFI_SUCCESS)
260 next = desc.base_address + desc.length;
263 * Only system memory is suitable for trampoline/kernel image placement,
264 * so only this type of memory needs its attributes to be modified.
267 if (desc.gcd_memory_type != EfiGcdMemoryTypeSystemMemory ||
268 (desc.attributes & (EFI_MEMORY_RO | EFI_MEMORY_XP)) == 0)
271 unprotect_start = max(rounded_start, (unsigned long)desc.base_address);
272 unprotect_size = min(rounded_end, next) - unprotect_start;
274 status = efi_dxe_call(set_memory_space_attributes,
275 unprotect_start, unprotect_size,
278 if (status != EFI_SUCCESS) {
279 efi_warn("Unable to unprotect memory range [%08lx,%08lx]: %lx\n",
281 unprotect_start + unprotect_size,
287 static void setup_unaccepted_memory(void)
289 efi_guid_t mem_acceptance_proto = OVMF_SEV_MEMORY_ACCEPTANCE_PROTOCOL_GUID;
290 sev_memory_acceptance_protocol_t *proto;
293 if (!IS_ENABLED(CONFIG_UNACCEPTED_MEMORY))
297 * Enable unaccepted memory before calling exit boot services in order
298 * for the UEFI to not accept all memory on EBS.
300 status = efi_bs_call(locate_protocol, &mem_acceptance_proto, NULL,
302 if (status != EFI_SUCCESS)
305 status = efi_call_proto(proto, allow_unaccepted_memory);
306 if (status != EFI_SUCCESS)
307 efi_err("Memory acceptance protocol failed\n");
310 static efi_char16_t *efistub_fw_vendor(void)
312 unsigned long vendor = efi_table_attr(efi_system_table, fw_vendor);
314 return (efi_char16_t *)vendor;
317 static const efi_char16_t apple[] = L"Apple";
319 static void setup_quirks(struct boot_params *boot_params)
321 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES) &&
322 !memcmp(efistub_fw_vendor(), apple, sizeof(apple)))
323 retrieve_apple_device_properties(boot_params);
327 * See if we have Universal Graphics Adapter (UGA) protocol
330 setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size)
334 void **uga_handle = NULL;
335 efi_uga_draw_protocol_t *uga = NULL, *first_uga;
339 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
340 (void **)&uga_handle);
341 if (status != EFI_SUCCESS)
344 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
345 uga_proto, NULL, &size, uga_handle);
346 if (status != EFI_SUCCESS)
353 for_each_efi_handle(handle, uga_handle, size, i) {
354 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
355 u32 w, h, depth, refresh;
358 status = efi_bs_call(handle_protocol, handle, uga_proto,
360 if (status != EFI_SUCCESS)
364 efi_bs_call(handle_protocol, handle, &pciio_proto, &pciio);
366 status = efi_call_proto(uga, get_mode, &w, &h, &depth, &refresh);
367 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
372 * Once we've found a UGA supporting PCIIO,
373 * don't bother looking any further.
382 if (!width && !height)
385 /* EFI framebuffer */
386 si->orig_video_isVGA = VIDEO_TYPE_EFI;
389 si->lfb_width = width;
390 si->lfb_height = height;
402 efi_bs_call(free_pool, uga_handle);
407 static void setup_graphics(struct boot_params *boot_params)
409 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
410 struct screen_info *si;
411 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
414 void **gop_handle = NULL;
415 void **uga_handle = NULL;
417 si = &boot_params->screen_info;
418 memset(si, 0, sizeof(*si));
421 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
422 &graphics_proto, NULL, &size, gop_handle);
423 if (status == EFI_BUFFER_TOO_SMALL)
424 status = efi_setup_gop(si, &graphics_proto, size);
426 if (status != EFI_SUCCESS) {
428 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
429 &uga_proto, NULL, &size, uga_handle);
430 if (status == EFI_BUFFER_TOO_SMALL)
431 setup_uga(si, &uga_proto, size);
436 static void __noreturn efi_exit(efi_handle_t handle, efi_status_t status)
438 efi_bs_call(exit, handle, status, 0, NULL);
443 void __noreturn efi_stub_entry(efi_handle_t handle,
444 efi_system_table_t *sys_table_arg,
445 struct boot_params *boot_params);
448 * Because the x86 boot code expects to be passed a boot_params we
449 * need to create one ourselves (usually the bootloader would create
452 efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
453 efi_system_table_t *sys_table_arg)
455 struct boot_params *boot_params;
456 struct setup_header *hdr;
458 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
459 int options_size = 0;
463 efi_system_table = sys_table_arg;
465 /* Check if we were booted by the EFI firmware */
466 if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
467 efi_exit(handle, EFI_INVALID_PARAMETER);
469 status = efi_bs_call(handle_protocol, handle, &proto, (void **)&image);
470 if (status != EFI_SUCCESS) {
471 efi_err("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
472 efi_exit(handle, status);
475 image_base = efi_table_attr(image, image_base);
477 status = efi_allocate_pages(sizeof(struct boot_params),
478 (unsigned long *)&boot_params, ULONG_MAX);
479 if (status != EFI_SUCCESS) {
480 efi_err("Failed to allocate lowmem for boot params\n");
481 efi_exit(handle, status);
484 memset(boot_params, 0x0, sizeof(struct boot_params));
486 hdr = &boot_params->hdr;
488 /* Copy the setup header from the second sector to boot_params */
489 memcpy(&hdr->jump, image_base + 512,
490 sizeof(struct setup_header) - offsetof(struct setup_header, jump));
493 * Fill out some of the header fields ourselves because the
494 * EFI firmware loader doesn't load the first sector.
497 hdr->vid_mode = 0xffff;
498 hdr->boot_flag = 0xAA55;
500 hdr->type_of_loader = 0x21;
502 /* Convert unicode cmdline to ascii */
503 cmdline_ptr = efi_convert_cmdline(image, &options_size);
507 efi_set_u64_split((unsigned long)cmdline_ptr,
508 &hdr->cmd_line_ptr, &boot_params->ext_cmd_line_ptr);
510 hdr->ramdisk_image = 0;
511 hdr->ramdisk_size = 0;
514 * Disregard any setup data that was provided by the bootloader:
515 * setup_data could be pointing anywhere, and we have no way of
516 * authenticating or validating the payload.
520 efi_stub_entry(handle, sys_table_arg, boot_params);
524 efi_free(sizeof(struct boot_params), (unsigned long)boot_params);
526 efi_exit(handle, status);
529 static void add_e820ext(struct boot_params *params,
530 struct setup_data *e820ext, u32 nr_entries)
532 struct setup_data *data;
534 e820ext->type = SETUP_E820_EXT;
535 e820ext->len = nr_entries * sizeof(struct boot_e820_entry);
538 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
540 while (data && data->next)
541 data = (struct setup_data *)(unsigned long)data->next;
544 data->next = (unsigned long)e820ext;
546 params->hdr.setup_data = (unsigned long)e820ext;
550 setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size)
552 struct boot_e820_entry *entry = params->e820_table;
553 struct efi_info *efi = ¶ms->efi_info;
554 struct boot_e820_entry *prev = NULL;
560 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
562 for (i = 0; i < nr_desc; i++) {
563 efi_memory_desc_t *d;
564 unsigned int e820_type = 0;
565 unsigned long m = efi->efi_memmap;
568 m |= (u64)efi->efi_memmap_hi << 32;
571 d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
573 case EFI_RESERVED_TYPE:
574 case EFI_RUNTIME_SERVICES_CODE:
575 case EFI_RUNTIME_SERVICES_DATA:
576 case EFI_MEMORY_MAPPED_IO:
577 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
579 e820_type = E820_TYPE_RESERVED;
582 case EFI_UNUSABLE_MEMORY:
583 e820_type = E820_TYPE_UNUSABLE;
586 case EFI_ACPI_RECLAIM_MEMORY:
587 e820_type = E820_TYPE_ACPI;
590 case EFI_LOADER_CODE:
591 case EFI_LOADER_DATA:
592 case EFI_BOOT_SERVICES_CODE:
593 case EFI_BOOT_SERVICES_DATA:
594 case EFI_CONVENTIONAL_MEMORY:
595 if (efi_soft_reserve_enabled() &&
596 (d->attribute & EFI_MEMORY_SP))
597 e820_type = E820_TYPE_SOFT_RESERVED;
599 e820_type = E820_TYPE_RAM;
602 case EFI_ACPI_MEMORY_NVS:
603 e820_type = E820_TYPE_NVS;
606 case EFI_PERSISTENT_MEMORY:
607 e820_type = E820_TYPE_PMEM;
610 case EFI_UNACCEPTED_MEMORY:
611 if (!IS_ENABLED(CONFIG_UNACCEPTED_MEMORY))
613 e820_type = E820_TYPE_RAM;
614 process_unaccepted_memory(d->phys_addr,
615 d->phys_addr + PAGE_SIZE * d->num_pages);
621 /* Merge adjacent mappings */
622 if (prev && prev->type == e820_type &&
623 (prev->addr + prev->size) == d->phys_addr) {
624 prev->size += d->num_pages << 12;
628 if (nr_entries == ARRAY_SIZE(params->e820_table)) {
629 u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
630 sizeof(struct setup_data);
632 if (!e820ext || e820ext_size < need)
633 return EFI_BUFFER_TOO_SMALL;
635 /* boot_params map full, switch to e820 extended */
636 entry = (struct boot_e820_entry *)e820ext->data;
639 entry->addr = d->phys_addr;
640 entry->size = d->num_pages << PAGE_SHIFT;
641 entry->type = e820_type;
646 if (nr_entries > ARRAY_SIZE(params->e820_table)) {
647 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
649 add_e820ext(params, e820ext, nr_e820ext);
650 nr_entries -= nr_e820ext;
653 params->e820_entries = (u8)nr_entries;
658 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
664 size = sizeof(struct setup_data) +
665 sizeof(struct e820_entry) * nr_desc;
668 efi_bs_call(free_pool, *e820ext);
673 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
675 if (status == EFI_SUCCESS)
676 *e820ext_size = size;
681 static efi_status_t allocate_e820(struct boot_params *params,
682 struct setup_data **e820ext,
685 struct efi_boot_memmap *map;
689 status = efi_get_memory_map(&map, false);
690 if (status != EFI_SUCCESS)
693 nr_desc = map->map_size / map->desc_size;
694 if (nr_desc > ARRAY_SIZE(params->e820_table) - EFI_MMAP_NR_SLACK_SLOTS) {
695 u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table) +
696 EFI_MMAP_NR_SLACK_SLOTS;
698 status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size);
701 if (IS_ENABLED(CONFIG_UNACCEPTED_MEMORY) && status == EFI_SUCCESS)
702 status = allocate_unaccepted_bitmap(nr_desc, map);
704 efi_bs_call(free_pool, map);
708 struct exit_boot_struct {
709 struct boot_params *boot_params;
710 struct efi_info *efi;
713 static efi_status_t exit_boot_func(struct efi_boot_memmap *map,
716 const char *signature;
717 struct exit_boot_struct *p = priv;
719 signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE
720 : EFI32_LOADER_SIGNATURE;
721 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
723 efi_set_u64_split((unsigned long)efi_system_table,
724 &p->efi->efi_systab, &p->efi->efi_systab_hi);
725 p->efi->efi_memdesc_size = map->desc_size;
726 p->efi->efi_memdesc_version = map->desc_ver;
727 efi_set_u64_split((unsigned long)map->map,
728 &p->efi->efi_memmap, &p->efi->efi_memmap_hi);
729 p->efi->efi_memmap_size = map->map_size;
734 static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)
736 struct setup_data *e820ext = NULL;
737 __u32 e820ext_size = 0;
739 struct exit_boot_struct priv;
741 priv.boot_params = boot_params;
742 priv.efi = &boot_params->efi_info;
744 status = allocate_e820(boot_params, &e820ext, &e820ext_size);
745 if (status != EFI_SUCCESS)
748 /* Might as well exit boot services now */
749 status = efi_exit_boot_services(handle, &priv, exit_boot_func);
750 if (status != EFI_SUCCESS)
754 boot_params->alt_mem_k = 32 * 1024;
756 status = setup_e820(boot_params, e820ext, e820ext_size);
757 if (status != EFI_SUCCESS)
763 static bool have_unsupported_snp_features(void)
767 unsupported = snp_get_unsupported_features(sev_get_status());
769 efi_err("Unsupported SEV-SNP features detected: 0x%llx\n",
776 static void efi_get_seed(void *seed, int size)
778 efi_get_random_bytes(size, seed);
781 * This only updates seed[0] when running on 32-bit, but in that case,
782 * seed[1] is not used anyway, as there is no virtual KASLR on 32-bit.
784 *(unsigned long *)seed ^= kaslr_get_random_long("EFI");
787 static void error(char *str)
789 efi_warn("Decompression failed: %s\n", str);
792 static efi_status_t efi_decompress_kernel(unsigned long *kernel_entry)
794 unsigned long virt_addr = LOAD_PHYSICAL_ADDR;
795 unsigned long addr, alloc_size, entry;
799 /* determine the required size of the allocation */
800 alloc_size = ALIGN(max_t(unsigned long, output_len, kernel_total_size),
803 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && !efi_nokaslr) {
804 u64 range = KERNEL_IMAGE_SIZE - LOAD_PHYSICAL_ADDR - kernel_total_size;
805 static const efi_char16_t ami[] = L"American Megatrends";
807 efi_get_seed(seed, sizeof(seed));
809 virt_addr += (range * seed[1]) >> 32;
810 virt_addr &= ~(CONFIG_PHYSICAL_ALIGN - 1);
813 * Older Dell systems with AMI UEFI firmware v2.0 may hang
814 * while decompressing the kernel if physical address
815 * randomization is enabled.
817 * https://bugzilla.kernel.org/show_bug.cgi?id=218173
819 if (efi_system_table->hdr.revision <= EFI_2_00_SYSTEM_TABLE_REVISION &&
820 !memcmp(efistub_fw_vendor(), ami, sizeof(ami))) {
821 efi_debug("AMI firmware v2.0 or older detected - disabling physical KASLR\n");
826 status = efi_random_alloc(alloc_size, CONFIG_PHYSICAL_ALIGN, &addr,
827 seed[0], EFI_LOADER_CODE,
828 EFI_X86_KERNEL_ALLOC_LIMIT);
829 if (status != EFI_SUCCESS)
832 entry = decompress_kernel((void *)addr, virt_addr, error);
833 if (entry == ULONG_MAX) {
834 efi_free(alloc_size, addr);
835 return EFI_LOAD_ERROR;
838 *kernel_entry = addr + entry;
840 efi_adjust_memory_range_protection(addr, kernel_total_size);
845 static void __noreturn enter_kernel(unsigned long kernel_addr,
846 struct boot_params *boot_params)
848 /* enter decompressed kernel with boot_params pointer in RSI/ESI */
849 asm("jmp *%0"::"r"(kernel_addr), "S"(boot_params));
855 * On success, this routine will jump to the relocated image directly and never
856 * return. On failure, it will exit to the firmware via efi_exit() instead of
859 void __noreturn efi_stub_entry(efi_handle_t handle,
860 efi_system_table_t *sys_table_arg,
861 struct boot_params *boot_params)
863 efi_guid_t guid = EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID;
864 struct setup_header *hdr = &boot_params->hdr;
865 const struct linux_efi_initrd *initrd = NULL;
866 unsigned long kernel_entry;
869 boot_params_pointer = boot_params;
871 efi_system_table = sys_table_arg;
872 /* Check if we were booted by the EFI firmware */
873 if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
874 efi_exit(handle, EFI_INVALID_PARAMETER);
876 if (have_unsupported_snp_features())
877 efi_exit(handle, EFI_UNSUPPORTED);
879 if (IS_ENABLED(CONFIG_EFI_DXE_MEM_ATTRIBUTES)) {
880 efi_dxe_table = get_efi_config_table(EFI_DXE_SERVICES_TABLE_GUID);
882 efi_dxe_table->hdr.signature != EFI_DXE_SERVICES_TABLE_SIGNATURE) {
883 efi_warn("Ignoring DXE services table: invalid signature\n");
884 efi_dxe_table = NULL;
888 /* grab the memory attributes protocol if it exists */
889 efi_bs_call(locate_protocol, &guid, NULL, (void **)&memattr);
891 status = efi_setup_5level_paging();
892 if (status != EFI_SUCCESS) {
893 efi_err("efi_setup_5level_paging() failed!\n");
897 #ifdef CONFIG_CMDLINE_BOOL
898 status = efi_parse_options(CONFIG_CMDLINE);
899 if (status != EFI_SUCCESS) {
900 efi_err("Failed to parse options\n");
904 if (!IS_ENABLED(CONFIG_CMDLINE_OVERRIDE)) {
905 unsigned long cmdline_paddr = ((u64)hdr->cmd_line_ptr |
906 ((u64)boot_params->ext_cmd_line_ptr << 32));
907 status = efi_parse_options((char *)cmdline_paddr);
908 if (status != EFI_SUCCESS) {
909 efi_err("Failed to parse options\n");
914 status = efi_decompress_kernel(&kernel_entry);
915 if (status != EFI_SUCCESS) {
916 efi_err("Failed to decompress kernel\n");
921 * At this point, an initrd may already have been loaded by the
922 * bootloader and passed via bootparams. We permit an initrd loaded
923 * from the LINUX_EFI_INITRD_MEDIA_GUID device path to supersede it.
925 * If the device path is not present, any command-line initrd=
926 * arguments will be processed only if image is not NULL, which will be
927 * the case only if we were loaded via the PE entry point.
929 status = efi_load_initrd(image, hdr->initrd_addr_max, ULONG_MAX,
931 if (status != EFI_SUCCESS)
933 if (initrd && initrd->size > 0) {
934 efi_set_u64_split(initrd->base, &hdr->ramdisk_image,
935 &boot_params->ext_ramdisk_image);
936 efi_set_u64_split(initrd->size, &hdr->ramdisk_size,
937 &boot_params->ext_ramdisk_size);
942 * If the boot loader gave us a value for secure_boot then we use that,
943 * otherwise we ask the BIOS.
945 if (boot_params->secure_boot == efi_secureboot_mode_unset)
946 boot_params->secure_boot = efi_get_secureboot();
948 /* Ask the firmware to clear memory on unclean shutdown */
949 efi_enable_reset_attack_mitigation();
951 efi_random_get_seed();
953 efi_retrieve_tpm2_eventlog();
955 setup_graphics(boot_params);
957 setup_efi_pci(boot_params);
959 setup_quirks(boot_params);
961 setup_unaccepted_memory();
963 status = exit_boot(boot_params, handle);
964 if (status != EFI_SUCCESS) {
965 efi_err("exit_boot() failed!\n");
970 * Call the SEV init code while still running with the firmware's
971 * GDT/IDT, so #VC exceptions will be handled by EFI.
973 sev_enable(boot_params);
977 enter_kernel(kernel_entry, boot_params);
979 efi_err("efi_stub_entry() failed!\n");
981 efi_exit(handle, status);
984 #ifdef CONFIG_EFI_HANDOVER_PROTOCOL
985 void efi_handover_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,
986 struct boot_params *boot_params)
988 extern char _bss[], _ebss[];
990 memset(_bss, 0, _ebss - _bss);
991 efi_stub_entry(handle, sys_table_arg, boot_params);
994 #ifndef CONFIG_EFI_MIXED
995 extern __alias(efi_handover_entry)
996 void efi32_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,
997 struct boot_params *boot_params);
999 extern __alias(efi_handover_entry)
1000 void efi64_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,
1001 struct boot_params *boot_params);