Path: blob/master/drivers/firmware/efi/libstub/x86-stub.c
26483 views
// SPDX-License-Identifier: GPL-2.0-only12/* -----------------------------------------------------------------------3*4* Copyright 2011 Intel Corporation; author Matt Fleming5*6* ----------------------------------------------------------------------- */78#include <linux/efi.h>9#include <linux/pci.h>10#include <linux/stddef.h>1112#include <asm/efi.h>13#include <asm/e820/types.h>14#include <asm/setup.h>15#include <asm/desc.h>16#include <asm/boot.h>17#include <asm/kaslr.h>18#include <asm/sev.h>1920#include "efistub.h"21#include "x86-stub.h"2223extern char _bss[], _ebss[];2425const efi_system_table_t *efi_system_table;26const efi_dxe_services_table_t *efi_dxe_table;27static efi_loaded_image_t *image = NULL;28static efi_memory_attribute_protocol_t *memattr;2930typedef union sev_memory_acceptance_protocol sev_memory_acceptance_protocol_t;31union sev_memory_acceptance_protocol {32struct {33efi_status_t (__efiapi * allow_unaccepted_memory)(34sev_memory_acceptance_protocol_t *);35};36struct {37u32 allow_unaccepted_memory;38} mixed_mode;39};4041static efi_status_t42preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)43{44struct pci_setup_rom *rom __free(efi_pool) = NULL;45efi_status_t status;46unsigned long size;47uint64_t romsize;48void *romimage;4950/*51* Some firmware images contain EFI function pointers at the place where52* the romimage and romsize fields are supposed to be. Typically the EFI53* code is mapped at high addresses, translating to an unrealistically54* large romsize. The UEFI spec limits the size of option ROMs to 1655* MiB so we reject any ROMs over 16 MiB in size to catch this.56*/57romimage = efi_table_attr(pci, romimage);58romsize = efi_table_attr(pci, romsize);59if (!romimage || !romsize || romsize > SZ_16M)60return EFI_INVALID_PARAMETER;6162size = romsize + sizeof(*rom);6364status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,65(void **)&rom);66if (status != EFI_SUCCESS) {67efi_err("Failed to allocate memory for 'rom'\n");68return status;69}7071memset(rom, 0, sizeof(*rom));7273rom->data.type = SETUP_PCI;74rom->data.len = size - sizeof(struct setup_data);75rom->data.next = 0;76rom->pcilen = romsize;7778status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,79PCI_VENDOR_ID, 1, &rom->vendor);8081if (status != EFI_SUCCESS) {82efi_err("Failed to read rom->vendor\n");83return status;84}8586status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,87PCI_DEVICE_ID, 1, &rom->devid);8889if (status != EFI_SUCCESS) {90efi_err("Failed to read rom->devid\n");91return status;92}9394status = efi_call_proto(pci, get_location, &rom->segment, &rom->bus,95&rom->device, &rom->function);9697if (status != EFI_SUCCESS)98return status;99100memcpy(rom->romdata, romimage, romsize);101*__rom = no_free_ptr(rom);102return EFI_SUCCESS;103}104105/*106* There's no way to return an informative status from this function,107* because any analysis (and printing of error messages) needs to be108* done directly at the EFI function call-site.109*110* For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we111* just didn't find any PCI devices, but there's no way to tell outside112* the context of the call.113*/114static void setup_efi_pci(struct boot_params *params)115{116efi_status_t status;117efi_handle_t *pci_handle __free(efi_pool) = NULL;118efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;119struct setup_data *data;120unsigned long num;121efi_handle_t h;122123status = efi_bs_call(locate_handle_buffer, EFI_LOCATE_BY_PROTOCOL,124&pci_proto, NULL, &num, &pci_handle);125if (status != EFI_SUCCESS)126return;127128data = (struct setup_data *)(unsigned long)params->hdr.setup_data;129130while (data && data->next)131data = (struct setup_data *)(unsigned long)data->next;132133for_each_efi_handle(h, pci_handle, num) {134efi_pci_io_protocol_t *pci = NULL;135struct pci_setup_rom *rom;136137status = efi_bs_call(handle_protocol, h, &pci_proto,138(void **)&pci);139if (status != EFI_SUCCESS || !pci)140continue;141142status = preserve_pci_rom_image(pci, &rom);143if (status != EFI_SUCCESS)144continue;145146if (data)147data->next = (unsigned long)rom;148else149params->hdr.setup_data = (unsigned long)rom;150151data = (struct setup_data *)rom;152}153}154155static void retrieve_apple_device_properties(struct boot_params *boot_params)156{157efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;158struct setup_data *data, *new;159efi_status_t status;160u32 size = 0;161apple_properties_protocol_t *p;162163status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&p);164if (status != EFI_SUCCESS)165return;166167if (efi_table_attr(p, version) != 0x10000) {168efi_err("Unsupported properties proto version\n");169return;170}171172efi_call_proto(p, get_all, NULL, &size);173if (!size)174return;175176do {177status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,178size + sizeof(struct setup_data),179(void **)&new);180if (status != EFI_SUCCESS) {181efi_err("Failed to allocate memory for 'properties'\n");182return;183}184185status = efi_call_proto(p, get_all, new->data, &size);186187if (status == EFI_BUFFER_TOO_SMALL)188efi_bs_call(free_pool, new);189} while (status == EFI_BUFFER_TOO_SMALL);190191new->type = SETUP_APPLE_PROPERTIES;192new->len = size;193new->next = 0;194195data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;196if (!data) {197boot_params->hdr.setup_data = (unsigned long)new;198} else {199while (data->next)200data = (struct setup_data *)(unsigned long)data->next;201data->next = (unsigned long)new;202}203}204205static bool apple_match_product_name(void)206{207static const char type1_product_matches[][15] = {208"MacBookPro11,3",209"MacBookPro11,5",210"MacBookPro13,3",211"MacBookPro14,3",212"MacBookPro15,1",213"MacBookPro15,3",214"MacBookPro16,1",215"MacBookPro16,4",216};217const struct efi_smbios_type1_record *record;218const u8 *product;219220record = (struct efi_smbios_type1_record *)efi_get_smbios_record(1);221if (!record)222return false;223224product = efi_get_smbios_string(record, product_name);225if (!product)226return false;227228for (int i = 0; i < ARRAY_SIZE(type1_product_matches); i++) {229if (!strcmp(product, type1_product_matches[i]))230return true;231}232233return false;234}235236static void apple_set_os(void)237{238struct {239unsigned long version;240efi_status_t (__efiapi *set_os_version)(const char *);241efi_status_t (__efiapi *set_os_vendor)(const char *);242} *set_os;243efi_status_t status;244245if (!efi_is_64bit() || !apple_match_product_name())246return;247248status = efi_bs_call(locate_protocol, &APPLE_SET_OS_PROTOCOL_GUID, NULL,249(void **)&set_os);250if (status != EFI_SUCCESS)251return;252253if (set_os->version >= 2) {254status = set_os->set_os_vendor("Apple Inc.");255if (status != EFI_SUCCESS)256efi_err("Failed to set OS vendor via apple_set_os\n");257}258259if (set_os->version > 0) {260/* The version being set doesn't seem to matter */261status = set_os->set_os_version("Mac OS X 10.9");262if (status != EFI_SUCCESS)263efi_err("Failed to set OS version via apple_set_os\n");264}265}266267efi_status_t efi_adjust_memory_range_protection(unsigned long start,268unsigned long size)269{270efi_status_t status;271efi_gcd_memory_space_desc_t desc;272unsigned long end, next;273unsigned long rounded_start, rounded_end;274unsigned long unprotect_start, unprotect_size;275276rounded_start = rounddown(start, EFI_PAGE_SIZE);277rounded_end = roundup(start + size, EFI_PAGE_SIZE);278279if (memattr != NULL) {280status = efi_call_proto(memattr, set_memory_attributes,281rounded_start,282rounded_end - rounded_start,283EFI_MEMORY_RO);284if (status != EFI_SUCCESS) {285efi_warn("Failed to set EFI_MEMORY_RO attribute\n");286return status;287}288289status = efi_call_proto(memattr, clear_memory_attributes,290rounded_start,291rounded_end - rounded_start,292EFI_MEMORY_XP);293if (status != EFI_SUCCESS)294efi_warn("Failed to clear EFI_MEMORY_XP attribute\n");295return status;296}297298if (efi_dxe_table == NULL)299return EFI_SUCCESS;300301/*302* Don't modify memory region attributes, they are303* already suitable, to lower the possibility to304* encounter firmware bugs.305*/306307for (end = start + size; start < end; start = next) {308309status = efi_dxe_call(get_memory_space_descriptor, start, &desc);310311if (status != EFI_SUCCESS)312break;313314next = desc.base_address + desc.length;315316/*317* Only system memory is suitable for trampoline/kernel image placement,318* so only this type of memory needs its attributes to be modified.319*/320321if (desc.gcd_memory_type != EfiGcdMemoryTypeSystemMemory ||322(desc.attributes & (EFI_MEMORY_RO | EFI_MEMORY_XP)) == 0)323continue;324325unprotect_start = max(rounded_start, (unsigned long)desc.base_address);326unprotect_size = min(rounded_end, next) - unprotect_start;327328status = efi_dxe_call(set_memory_space_attributes,329unprotect_start, unprotect_size,330EFI_MEMORY_WB);331332if (status != EFI_SUCCESS) {333efi_warn("Unable to unprotect memory range [%08lx,%08lx]: %lx\n",334unprotect_start,335unprotect_start + unprotect_size,336status);337break;338}339}340return EFI_SUCCESS;341}342343static void setup_unaccepted_memory(void)344{345efi_guid_t mem_acceptance_proto = OVMF_SEV_MEMORY_ACCEPTANCE_PROTOCOL_GUID;346sev_memory_acceptance_protocol_t *proto;347efi_status_t status;348349if (!IS_ENABLED(CONFIG_UNACCEPTED_MEMORY))350return;351352/*353* Enable unaccepted memory before calling exit boot services in order354* for the UEFI to not accept all memory on EBS.355*/356status = efi_bs_call(locate_protocol, &mem_acceptance_proto, NULL,357(void **)&proto);358if (status != EFI_SUCCESS)359return;360361status = efi_call_proto(proto, allow_unaccepted_memory);362if (status != EFI_SUCCESS)363efi_err("Memory acceptance protocol failed\n");364}365366static efi_char16_t *efistub_fw_vendor(void)367{368unsigned long vendor = efi_table_attr(efi_system_table, fw_vendor);369370return (efi_char16_t *)vendor;371}372373static const efi_char16_t apple[] = L"Apple";374375static void setup_quirks(struct boot_params *boot_params)376{377if (!memcmp(efistub_fw_vendor(), apple, sizeof(apple))) {378if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))379retrieve_apple_device_properties(boot_params);380381apple_set_os();382}383}384385static void setup_graphics(struct boot_params *boot_params)386{387struct screen_info *si = memset(&boot_params->screen_info, 0, sizeof(*si));388389efi_setup_gop(si);390}391392static void __noreturn efi_exit(efi_handle_t handle, efi_status_t status)393{394efi_bs_call(exit, handle, status, 0, NULL);395for(;;)396asm("hlt");397}398399/*400* Because the x86 boot code expects to be passed a boot_params we401* need to create one ourselves (usually the bootloader would create402* one for us).403*/404static efi_status_t efi_allocate_bootparams(efi_handle_t handle,405struct boot_params **bp)406{407efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;408struct boot_params *boot_params;409struct setup_header *hdr;410efi_status_t status;411unsigned long alloc;412char *cmdline_ptr;413414status = efi_bs_call(handle_protocol, handle, &proto, (void **)&image);415if (status != EFI_SUCCESS) {416efi_err("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");417return status;418}419420status = efi_allocate_pages(PARAM_SIZE, &alloc, ULONG_MAX);421if (status != EFI_SUCCESS)422return status;423424boot_params = memset((void *)alloc, 0x0, PARAM_SIZE);425hdr = &boot_params->hdr;426427/* Assign the setup_header fields that the kernel actually cares about */428hdr->root_flags = 1;429hdr->vid_mode = 0xffff;430431hdr->type_of_loader = 0x21;432hdr->initrd_addr_max = INT_MAX;433434/* Convert unicode cmdline to ascii */435cmdline_ptr = efi_convert_cmdline(image);436if (!cmdline_ptr) {437efi_free(PARAM_SIZE, alloc);438return EFI_OUT_OF_RESOURCES;439}440441efi_set_u64_split((unsigned long)cmdline_ptr, &hdr->cmd_line_ptr,442&boot_params->ext_cmd_line_ptr);443444*bp = boot_params;445return EFI_SUCCESS;446}447448static void add_e820ext(struct boot_params *params,449struct setup_data *e820ext, u32 nr_entries)450{451struct setup_data *data;452453e820ext->type = SETUP_E820_EXT;454e820ext->len = nr_entries * sizeof(struct boot_e820_entry);455e820ext->next = 0;456457data = (struct setup_data *)(unsigned long)params->hdr.setup_data;458459while (data && data->next)460data = (struct setup_data *)(unsigned long)data->next;461462if (data)463data->next = (unsigned long)e820ext;464else465params->hdr.setup_data = (unsigned long)e820ext;466}467468static efi_status_t469setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size)470{471struct boot_e820_entry *entry = params->e820_table;472struct efi_info *efi = ¶ms->efi_info;473struct boot_e820_entry *prev = NULL;474u32 nr_entries;475u32 nr_desc;476int i;477478nr_entries = 0;479nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;480481for (i = 0; i < nr_desc; i++) {482efi_memory_desc_t *d;483unsigned int e820_type = 0;484unsigned long m = efi->efi_memmap;485486#ifdef CONFIG_X86_64487m |= (u64)efi->efi_memmap_hi << 32;488#endif489490d = efi_memdesc_ptr(m, efi->efi_memdesc_size, i);491switch (d->type) {492case EFI_RESERVED_TYPE:493case EFI_RUNTIME_SERVICES_CODE:494case EFI_RUNTIME_SERVICES_DATA:495case EFI_MEMORY_MAPPED_IO:496case EFI_MEMORY_MAPPED_IO_PORT_SPACE:497case EFI_PAL_CODE:498e820_type = E820_TYPE_RESERVED;499break;500501case EFI_UNUSABLE_MEMORY:502e820_type = E820_TYPE_UNUSABLE;503break;504505case EFI_ACPI_RECLAIM_MEMORY:506e820_type = E820_TYPE_ACPI;507break;508509case EFI_LOADER_CODE:510case EFI_LOADER_DATA:511case EFI_BOOT_SERVICES_CODE:512case EFI_BOOT_SERVICES_DATA:513case EFI_CONVENTIONAL_MEMORY:514if (efi_soft_reserve_enabled() &&515(d->attribute & EFI_MEMORY_SP))516e820_type = E820_TYPE_SOFT_RESERVED;517else518e820_type = E820_TYPE_RAM;519break;520521case EFI_ACPI_MEMORY_NVS:522e820_type = E820_TYPE_NVS;523break;524525case EFI_PERSISTENT_MEMORY:526e820_type = E820_TYPE_PMEM;527break;528529case EFI_UNACCEPTED_MEMORY:530if (!IS_ENABLED(CONFIG_UNACCEPTED_MEMORY))531continue;532e820_type = E820_TYPE_RAM;533process_unaccepted_memory(d->phys_addr,534d->phys_addr + PAGE_SIZE * d->num_pages);535break;536default:537continue;538}539540/* Merge adjacent mappings */541if (prev && prev->type == e820_type &&542(prev->addr + prev->size) == d->phys_addr) {543prev->size += d->num_pages << 12;544continue;545}546547if (nr_entries == ARRAY_SIZE(params->e820_table)) {548u32 need = (nr_desc - i) * sizeof(struct e820_entry) +549sizeof(struct setup_data);550551if (!e820ext || e820ext_size < need)552return EFI_BUFFER_TOO_SMALL;553554/* boot_params map full, switch to e820 extended */555entry = (struct boot_e820_entry *)e820ext->data;556}557558entry->addr = d->phys_addr;559entry->size = d->num_pages << PAGE_SHIFT;560entry->type = e820_type;561prev = entry++;562nr_entries++;563}564565if (nr_entries > ARRAY_SIZE(params->e820_table)) {566u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);567568add_e820ext(params, e820ext, nr_e820ext);569nr_entries -= nr_e820ext;570}571572params->e820_entries = (u8)nr_entries;573574return EFI_SUCCESS;575}576577static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,578u32 *e820ext_size)579{580efi_status_t status;581unsigned long size;582583size = sizeof(struct setup_data) +584sizeof(struct e820_entry) * nr_desc;585586if (*e820ext) {587efi_bs_call(free_pool, *e820ext);588*e820ext = NULL;589*e820ext_size = 0;590}591592status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,593(void **)e820ext);594if (status == EFI_SUCCESS)595*e820ext_size = size;596597return status;598}599600static efi_status_t allocate_e820(struct boot_params *params,601struct setup_data **e820ext,602u32 *e820ext_size)603{604struct efi_boot_memmap *map __free(efi_pool) = NULL;605efi_status_t status;606__u32 nr_desc;607608status = efi_get_memory_map(&map, false);609if (status != EFI_SUCCESS)610return status;611612nr_desc = map->map_size / map->desc_size;613if (nr_desc > ARRAY_SIZE(params->e820_table) - EFI_MMAP_NR_SLACK_SLOTS) {614u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table) +615EFI_MMAP_NR_SLACK_SLOTS;616617status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size);618if (status != EFI_SUCCESS)619return status;620}621622if (IS_ENABLED(CONFIG_UNACCEPTED_MEMORY))623return allocate_unaccepted_bitmap(nr_desc, map);624625return EFI_SUCCESS;626}627628struct exit_boot_struct {629struct boot_params *boot_params;630struct efi_info *efi;631};632633static efi_status_t exit_boot_func(struct efi_boot_memmap *map,634void *priv)635{636const char *signature;637struct exit_boot_struct *p = priv;638639signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE640: EFI32_LOADER_SIGNATURE;641memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));642643efi_set_u64_split((unsigned long)efi_system_table,644&p->efi->efi_systab, &p->efi->efi_systab_hi);645p->efi->efi_memdesc_size = map->desc_size;646p->efi->efi_memdesc_version = map->desc_ver;647efi_set_u64_split((unsigned long)map->map,648&p->efi->efi_memmap, &p->efi->efi_memmap_hi);649p->efi->efi_memmap_size = map->map_size;650651return EFI_SUCCESS;652}653654static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)655{656struct setup_data *e820ext = NULL;657__u32 e820ext_size = 0;658efi_status_t status;659struct exit_boot_struct priv;660661priv.boot_params = boot_params;662priv.efi = &boot_params->efi_info;663664status = allocate_e820(boot_params, &e820ext, &e820ext_size);665if (status != EFI_SUCCESS)666return status;667668/* Might as well exit boot services now */669status = efi_exit_boot_services(handle, &priv, exit_boot_func);670if (status != EFI_SUCCESS)671return status;672673/* Historic? */674boot_params->alt_mem_k = 32 * 1024;675676status = setup_e820(boot_params, e820ext, e820ext_size);677if (status != EFI_SUCCESS)678return status;679680return EFI_SUCCESS;681}682683static bool have_unsupported_snp_features(void)684{685u64 unsupported;686687unsupported = snp_get_unsupported_features(sev_get_status());688if (unsupported) {689efi_err("Unsupported SEV-SNP features detected: 0x%llx\n",690unsupported);691return true;692}693return false;694}695696static void efi_get_seed(void *seed, int size)697{698efi_get_random_bytes(size, seed);699700/*701* This only updates seed[0] when running on 32-bit, but in that case,702* seed[1] is not used anyway, as there is no virtual KASLR on 32-bit.703*/704*(unsigned long *)seed ^= kaslr_get_random_long("EFI");705}706707static void error(char *str)708{709efi_warn("Decompression failed: %s\n", str);710}711712static const char *cmdline_memmap_override;713714static efi_status_t parse_options(const char *cmdline)715{716static const char opts[][14] = {717"mem=", "memmap=", "hugepages="718};719720for (int i = 0; i < ARRAY_SIZE(opts); i++) {721const char *p = strstr(cmdline, opts[i]);722723if (p == cmdline || (p > cmdline && isspace(p[-1]))) {724cmdline_memmap_override = opts[i];725break;726}727}728729return efi_parse_options(cmdline);730}731732static efi_status_t efi_decompress_kernel(unsigned long *kernel_entry,733struct boot_params *boot_params)734{735unsigned long virt_addr = LOAD_PHYSICAL_ADDR;736unsigned long addr, alloc_size, entry;737efi_status_t status;738u32 seed[2] = {};739740boot_params_ptr = boot_params;741742/* determine the required size of the allocation */743alloc_size = ALIGN(max_t(unsigned long, output_len, kernel_total_size),744MIN_KERNEL_ALIGN);745746if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && !efi_nokaslr) {747u64 range = KERNEL_IMAGE_SIZE - LOAD_PHYSICAL_ADDR - kernel_total_size;748static const efi_char16_t ami[] = L"American Megatrends";749750efi_get_seed(seed, sizeof(seed));751752virt_addr += (range * seed[1]) >> 32;753virt_addr &= ~(CONFIG_PHYSICAL_ALIGN - 1);754755/*756* Older Dell systems with AMI UEFI firmware v2.0 may hang757* while decompressing the kernel if physical address758* randomization is enabled.759*760* https://bugzilla.kernel.org/show_bug.cgi?id=218173761*/762if (efi_system_table->hdr.revision <= EFI_2_00_SYSTEM_TABLE_REVISION &&763!memcmp(efistub_fw_vendor(), ami, sizeof(ami))) {764efi_debug("AMI firmware v2.0 or older detected - disabling physical KASLR\n");765seed[0] = 0;766} else if (cmdline_memmap_override) {767efi_info("%s detected on the kernel command line - disabling physical KASLR\n",768cmdline_memmap_override);769seed[0] = 0;770}771772boot_params->hdr.loadflags |= KASLR_FLAG;773}774775status = efi_random_alloc(alloc_size, CONFIG_PHYSICAL_ALIGN, &addr,776seed[0], EFI_LOADER_CODE,777LOAD_PHYSICAL_ADDR,778EFI_X86_KERNEL_ALLOC_LIMIT);779if (status != EFI_SUCCESS)780return status;781782entry = decompress_kernel((void *)addr, virt_addr, error);783if (entry == ULONG_MAX) {784efi_free(alloc_size, addr);785return EFI_LOAD_ERROR;786}787788*kernel_entry = addr + entry;789790return efi_adjust_memory_range_protection(addr, kernel_text_size);791}792793static void __noreturn enter_kernel(unsigned long kernel_addr,794struct boot_params *boot_params)795{796/* enter decompressed kernel with boot_params pointer in RSI/ESI */797asm("jmp *%0"::"r"(kernel_addr), "S"(boot_params));798799unreachable();800}801802/*803* On success, this routine will jump to the relocated image directly and never804* return. On failure, it will exit to the firmware via efi_exit() instead of805* returning.806*/807void __noreturn efi_stub_entry(efi_handle_t handle,808efi_system_table_t *sys_table_arg,809struct boot_params *boot_params)810811{812efi_guid_t guid = EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID;813const struct linux_efi_initrd *initrd = NULL;814unsigned long kernel_entry;815struct setup_header *hdr;816efi_status_t status;817818efi_system_table = sys_table_arg;819/* Check if we were booted by the EFI firmware */820if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)821efi_exit(handle, EFI_INVALID_PARAMETER);822823if (!IS_ENABLED(CONFIG_EFI_HANDOVER_PROTOCOL) || !boot_params) {824status = efi_allocate_bootparams(handle, &boot_params);825if (status != EFI_SUCCESS)826efi_exit(handle, status);827}828829hdr = &boot_params->hdr;830831if (have_unsupported_snp_features())832efi_exit(handle, EFI_UNSUPPORTED);833834if (IS_ENABLED(CONFIG_EFI_DXE_MEM_ATTRIBUTES)) {835efi_dxe_table = get_efi_config_table(EFI_DXE_SERVICES_TABLE_GUID);836if (efi_dxe_table &&837efi_dxe_table->hdr.signature != EFI_DXE_SERVICES_TABLE_SIGNATURE) {838efi_warn("Ignoring DXE services table: invalid signature\n");839efi_dxe_table = NULL;840}841}842843/* grab the memory attributes protocol if it exists */844efi_bs_call(locate_protocol, &guid, NULL, (void **)&memattr);845846status = efi_setup_5level_paging();847if (status != EFI_SUCCESS) {848efi_err("efi_setup_5level_paging() failed!\n");849goto fail;850}851852#ifdef CONFIG_CMDLINE_BOOL853status = parse_options(CONFIG_CMDLINE);854if (status != EFI_SUCCESS) {855efi_err("Failed to parse options\n");856goto fail;857}858#endif859if (!IS_ENABLED(CONFIG_CMDLINE_OVERRIDE)) {860unsigned long cmdline_paddr = ((u64)hdr->cmd_line_ptr |861((u64)boot_params->ext_cmd_line_ptr << 32));862status = parse_options((char *)cmdline_paddr);863if (status != EFI_SUCCESS) {864efi_err("Failed to parse options\n");865goto fail;866}867}868869if (efi_mem_encrypt > 0)870hdr->xloadflags |= XLF_MEM_ENCRYPTION;871872status = efi_decompress_kernel(&kernel_entry, boot_params);873if (status != EFI_SUCCESS) {874efi_err("Failed to decompress kernel\n");875goto fail;876}877878/*879* At this point, an initrd may already have been loaded by the880* bootloader and passed via bootparams. We permit an initrd loaded881* from the LINUX_EFI_INITRD_MEDIA_GUID device path to supersede it.882*883* If the device path is not present, any command-line initrd=884* arguments will be processed only if image is not NULL, which will be885* the case only if we were loaded via the PE entry point.886*/887status = efi_load_initrd(image, hdr->initrd_addr_max, ULONG_MAX,888&initrd);889if (status != EFI_SUCCESS)890goto fail;891if (initrd && initrd->size > 0) {892efi_set_u64_split(initrd->base, &hdr->ramdisk_image,893&boot_params->ext_ramdisk_image);894efi_set_u64_split(initrd->size, &hdr->ramdisk_size,895&boot_params->ext_ramdisk_size);896}897898899/*900* If the boot loader gave us a value for secure_boot then we use that,901* otherwise we ask the BIOS.902*/903if (boot_params->secure_boot == efi_secureboot_mode_unset)904boot_params->secure_boot = efi_get_secureboot();905906/* Ask the firmware to clear memory on unclean shutdown */907efi_enable_reset_attack_mitigation();908909efi_random_get_seed();910911efi_retrieve_eventlog();912913setup_graphics(boot_params);914915setup_efi_pci(boot_params);916917setup_quirks(boot_params);918919setup_unaccepted_memory();920921status = exit_boot(boot_params, handle);922if (status != EFI_SUCCESS) {923efi_err("exit_boot() failed!\n");924goto fail;925}926927/*928* Call the SEV init code while still running with the firmware's929* GDT/IDT, so #VC exceptions will be handled by EFI.930*/931sev_enable(boot_params);932933efi_5level_switch();934935enter_kernel(kernel_entry, boot_params);936fail:937efi_err("efi_stub_entry() failed!\n");938939efi_exit(handle, status);940}941942efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,943efi_system_table_t *sys_table_arg)944{945efi_stub_entry(handle, sys_table_arg, NULL);946}947948#ifdef CONFIG_EFI_HANDOVER_PROTOCOL949void efi_handover_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,950struct boot_params *boot_params)951{952memset(_bss, 0, _ebss - _bss);953efi_stub_entry(handle, sys_table_arg, boot_params);954}955956#ifndef CONFIG_EFI_MIXED957extern __alias(efi_handover_entry)958void efi32_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,959struct boot_params *boot_params);960961extern __alias(efi_handover_entry)962void efi64_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,963struct boot_params *boot_params);964#endif965#endif966967968