Path: blob/master/drivers/firmware/efi/libstub/x86-stub.c
51706 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}204205struct smbios_entry_point {206u8 anchor[4];207u8 ep_checksum;208u8 ep_length;209u8 major_version;210u8 minor_version;211u16 max_size_entry;212u8 ep_rev;213u8 reserved[5];214215struct __packed {216u8 anchor[5];217u8 checksum;218u16 st_length;219u32 st_address;220u16 number_of_entries;221u8 bcd_rev;222} intm;223};224225static bool verify_ep_checksum(const void *ptr, int length)226{227u8 sum = 0;228229for (int i = 0; i < length; i++)230sum += ((u8 *)ptr)[i];231232return sum == 0;233}234235static bool verify_ep_integrity(const struct smbios_entry_point *ep)236{237if (memcmp(ep->anchor, "_SM_", sizeof(ep->anchor)) != 0)238return false;239240if (memcmp(ep->intm.anchor, "_DMI_", sizeof(ep->intm.anchor)) != 0)241return false;242243if (!verify_ep_checksum(ep, ep->ep_length) ||244!verify_ep_checksum(&ep->intm, sizeof(ep->intm)))245return false;246247return true;248}249250static const struct efi_smbios_record *search_record(void *table, u32 length,251u8 type)252{253const u8 *p, *end;254255p = (u8 *)table;256end = p + length;257258while (p + sizeof(struct efi_smbios_record) < end) {259const struct efi_smbios_record *hdr =260(struct efi_smbios_record *)p;261const u8 *next;262263if (hdr->type == type)264return hdr;265266/* Type 127 = End-of-Table */267if (hdr->type == 0x7F)268return NULL;269270/* Jumping to the unformed section */271next = p + hdr->length;272273/* Unformed section ends with 0000h */274while ((next[0] != 0 || next[1] != 0) && next + 1 < end)275next++;276277next += 2;278p = next;279}280281return NULL;282}283284static const struct efi_smbios_record *get_table_record(u8 type)285{286const struct smbios_entry_point *ep;287288/*289* Locate the legacy 32-bit SMBIOS entrypoint in memory, and parse it290* directly. Needed by some Macs that do not implement the EFI protocol.291*/292ep = get_efi_config_table(SMBIOS_TABLE_GUID);293if (!ep)294return NULL;295296if (!verify_ep_integrity(ep))297return NULL;298299return search_record((void *)(unsigned long)ep->intm.st_address,300ep->intm.st_length, type);301}302303static bool apple_match_product_name(void)304{305static const char type1_product_matches[][15] = {306"MacBookPro11,3",307"MacBookPro11,5",308"MacBookPro13,3",309"MacBookPro14,3",310"MacBookPro15,1",311"MacBookPro15,3",312"MacBookPro16,1",313"MacBookPro16,4",314};315const struct efi_smbios_type1_record *record;316const u8 *product;317318record = (struct efi_smbios_type1_record *)319(efi_get_smbios_record(1) ?: get_table_record(1));320if (!record)321return false;322323product = efi_get_smbios_string(record, product_name);324if (!product)325return false;326327for (int i = 0; i < ARRAY_SIZE(type1_product_matches); i++) {328if (!strcmp(product, type1_product_matches[i]))329return true;330}331332return false;333}334335static void apple_set_os(void)336{337struct {338unsigned long version;339efi_status_t (__efiapi *set_os_version)(const char *);340efi_status_t (__efiapi *set_os_vendor)(const char *);341} *set_os;342efi_status_t status;343344if (!efi_is_64bit() || !apple_match_product_name())345return;346347status = efi_bs_call(locate_protocol, &APPLE_SET_OS_PROTOCOL_GUID, NULL,348(void **)&set_os);349if (status != EFI_SUCCESS)350return;351352if (set_os->version >= 2) {353status = set_os->set_os_vendor("Apple Inc.");354if (status != EFI_SUCCESS)355efi_err("Failed to set OS vendor via apple_set_os\n");356}357358if (set_os->version > 0) {359/* The version being set doesn't seem to matter */360status = set_os->set_os_version("Mac OS X 10.9");361if (status != EFI_SUCCESS)362efi_err("Failed to set OS version via apple_set_os\n");363}364}365366efi_status_t efi_adjust_memory_range_protection(unsigned long start,367unsigned long size)368{369efi_status_t status;370efi_gcd_memory_space_desc_t desc;371unsigned long end, next;372unsigned long rounded_start, rounded_end;373unsigned long unprotect_start, unprotect_size;374375rounded_start = rounddown(start, EFI_PAGE_SIZE);376rounded_end = roundup(start + size, EFI_PAGE_SIZE);377378if (memattr != NULL) {379status = efi_call_proto(memattr, set_memory_attributes,380rounded_start,381rounded_end - rounded_start,382EFI_MEMORY_RO);383if (status != EFI_SUCCESS) {384efi_warn("Failed to set EFI_MEMORY_RO attribute\n");385return status;386}387388status = efi_call_proto(memattr, clear_memory_attributes,389rounded_start,390rounded_end - rounded_start,391EFI_MEMORY_XP);392if (status != EFI_SUCCESS)393efi_warn("Failed to clear EFI_MEMORY_XP attribute\n");394return status;395}396397if (efi_dxe_table == NULL)398return EFI_SUCCESS;399400/*401* Don't modify memory region attributes, if they are402* already suitable, to lower the possibility to403* encounter firmware bugs.404*/405406for (end = start + size; start < end; start = next) {407408status = efi_dxe_call(get_memory_space_descriptor, start, &desc);409410if (status != EFI_SUCCESS)411break;412413next = desc.base_address + desc.length;414415/*416* Only system memory and more reliable memory are suitable for417* trampoline/kernel image placement. So only those memory types418* may need to have attributes modified.419*/420421if ((desc.gcd_memory_type != EfiGcdMemoryTypeSystemMemory &&422desc.gcd_memory_type != EfiGcdMemoryTypeMoreReliable) ||423(desc.attributes & (EFI_MEMORY_RO | EFI_MEMORY_XP)) == 0)424continue;425426unprotect_start = max(rounded_start, (unsigned long)desc.base_address);427unprotect_size = min(rounded_end, next) - unprotect_start;428429status = efi_dxe_call(set_memory_space_attributes,430unprotect_start, unprotect_size,431EFI_MEMORY_WB);432433if (status != EFI_SUCCESS) {434efi_warn("Unable to unprotect memory range [%08lx,%08lx]: %lx\n",435unprotect_start,436unprotect_start + unprotect_size,437status);438break;439}440}441return EFI_SUCCESS;442}443444static void setup_unaccepted_memory(void)445{446efi_guid_t mem_acceptance_proto = OVMF_SEV_MEMORY_ACCEPTANCE_PROTOCOL_GUID;447sev_memory_acceptance_protocol_t *proto;448efi_status_t status;449450if (!IS_ENABLED(CONFIG_UNACCEPTED_MEMORY))451return;452453/*454* Enable unaccepted memory before calling exit boot services in order455* for the UEFI to not accept all memory on EBS.456*/457status = efi_bs_call(locate_protocol, &mem_acceptance_proto, NULL,458(void **)&proto);459if (status != EFI_SUCCESS)460return;461462status = efi_call_proto(proto, allow_unaccepted_memory);463if (status != EFI_SUCCESS)464efi_err("Memory acceptance protocol failed\n");465}466467static efi_char16_t *efistub_fw_vendor(void)468{469unsigned long vendor = efi_table_attr(efi_system_table, fw_vendor);470471return (efi_char16_t *)vendor;472}473474static const efi_char16_t apple[] = L"Apple";475476static void setup_quirks(struct boot_params *boot_params)477{478if (!memcmp(efistub_fw_vendor(), apple, sizeof(apple))) {479if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))480retrieve_apple_device_properties(boot_params);481482apple_set_os();483}484}485486static void setup_graphics(struct boot_params *boot_params)487{488struct screen_info *si = memset(&boot_params->screen_info, 0, sizeof(*si));489struct edid_info *edid = memset(&boot_params->edid_info, 0, sizeof(*edid));490491efi_setup_graphics(si, edid);492}493494static void __noreturn efi_exit(efi_handle_t handle, efi_status_t status)495{496efi_bs_call(exit, handle, status, 0, NULL);497for(;;)498asm("hlt");499}500501/*502* Because the x86 boot code expects to be passed a boot_params we503* need to create one ourselves (usually the bootloader would create504* one for us).505*/506static efi_status_t efi_allocate_bootparams(efi_handle_t handle,507struct boot_params **bp)508{509efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;510struct boot_params *boot_params;511struct setup_header *hdr;512efi_status_t status;513unsigned long alloc;514char *cmdline_ptr;515516status = efi_bs_call(handle_protocol, handle, &proto, (void **)&image);517if (status != EFI_SUCCESS) {518efi_err("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");519return status;520}521522status = efi_allocate_pages(PARAM_SIZE, &alloc, ULONG_MAX);523if (status != EFI_SUCCESS)524return status;525526boot_params = memset((void *)alloc, 0x0, PARAM_SIZE);527hdr = &boot_params->hdr;528529/* Assign the setup_header fields that the kernel actually cares about */530hdr->root_flags = 1;531hdr->vid_mode = 0xffff;532533hdr->type_of_loader = 0x21;534hdr->initrd_addr_max = INT_MAX;535536/* Convert unicode cmdline to ascii */537cmdline_ptr = efi_convert_cmdline(image);538if (!cmdline_ptr) {539efi_free(PARAM_SIZE, alloc);540return EFI_OUT_OF_RESOURCES;541}542543efi_set_u64_split((unsigned long)cmdline_ptr, &hdr->cmd_line_ptr,544&boot_params->ext_cmd_line_ptr);545546*bp = boot_params;547return EFI_SUCCESS;548}549550static void add_e820ext(struct boot_params *params,551struct setup_data *e820ext, u32 nr_entries)552{553struct setup_data *data;554555e820ext->type = SETUP_E820_EXT;556e820ext->len = nr_entries * sizeof(struct boot_e820_entry);557e820ext->next = 0;558559data = (struct setup_data *)(unsigned long)params->hdr.setup_data;560561while (data && data->next)562data = (struct setup_data *)(unsigned long)data->next;563564if (data)565data->next = (unsigned long)e820ext;566else567params->hdr.setup_data = (unsigned long)e820ext;568}569570static efi_status_t571setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size)572{573struct boot_e820_entry *entry = params->e820_table;574struct efi_info *efi = ¶ms->efi_info;575struct boot_e820_entry *prev = NULL;576u32 nr_entries;577u32 nr_desc;578int i;579580nr_entries = 0;581nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;582583for (i = 0; i < nr_desc; i++) {584efi_memory_desc_t *d;585unsigned int e820_type = 0;586unsigned long m = efi->efi_memmap;587588#ifdef CONFIG_X86_64589m |= (u64)efi->efi_memmap_hi << 32;590#endif591592d = efi_memdesc_ptr(m, efi->efi_memdesc_size, i);593switch (d->type) {594case EFI_RESERVED_TYPE:595case EFI_RUNTIME_SERVICES_CODE:596case EFI_RUNTIME_SERVICES_DATA:597case EFI_MEMORY_MAPPED_IO:598case EFI_MEMORY_MAPPED_IO_PORT_SPACE:599case EFI_PAL_CODE:600e820_type = E820_TYPE_RESERVED;601break;602603case EFI_UNUSABLE_MEMORY:604e820_type = E820_TYPE_UNUSABLE;605break;606607case EFI_ACPI_RECLAIM_MEMORY:608e820_type = E820_TYPE_ACPI;609break;610611case EFI_LOADER_CODE:612case EFI_LOADER_DATA:613case EFI_BOOT_SERVICES_CODE:614case EFI_BOOT_SERVICES_DATA:615case EFI_CONVENTIONAL_MEMORY:616if (efi_soft_reserve_enabled() &&617(d->attribute & EFI_MEMORY_SP))618e820_type = E820_TYPE_SOFT_RESERVED;619else620e820_type = E820_TYPE_RAM;621break;622623case EFI_ACPI_MEMORY_NVS:624e820_type = E820_TYPE_NVS;625break;626627case EFI_PERSISTENT_MEMORY:628e820_type = E820_TYPE_PMEM;629break;630631case EFI_UNACCEPTED_MEMORY:632if (!IS_ENABLED(CONFIG_UNACCEPTED_MEMORY))633continue;634e820_type = E820_TYPE_RAM;635process_unaccepted_memory(d->phys_addr,636d->phys_addr + PAGE_SIZE * d->num_pages);637break;638default:639continue;640}641642/* Merge adjacent mappings */643if (prev && prev->type == e820_type &&644(prev->addr + prev->size) == d->phys_addr) {645prev->size += d->num_pages << 12;646continue;647}648649if (nr_entries == ARRAY_SIZE(params->e820_table)) {650u32 need = (nr_desc - i) * sizeof(struct e820_entry) +651sizeof(struct setup_data);652653if (!e820ext || e820ext_size < need)654return EFI_BUFFER_TOO_SMALL;655656/* boot_params map full, switch to e820 extended */657entry = (struct boot_e820_entry *)e820ext->data;658}659660entry->addr = d->phys_addr;661entry->size = d->num_pages << PAGE_SHIFT;662entry->type = e820_type;663prev = entry++;664nr_entries++;665}666667if (nr_entries > ARRAY_SIZE(params->e820_table)) {668u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);669670add_e820ext(params, e820ext, nr_e820ext);671nr_entries -= nr_e820ext;672}673674params->e820_entries = (u8)nr_entries;675676return EFI_SUCCESS;677}678679static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,680u32 *e820ext_size)681{682efi_status_t status;683unsigned long size;684685size = sizeof(struct setup_data) +686sizeof(struct e820_entry) * nr_desc;687688if (*e820ext) {689efi_bs_call(free_pool, *e820ext);690*e820ext = NULL;691*e820ext_size = 0;692}693694status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,695(void **)e820ext);696if (status == EFI_SUCCESS)697*e820ext_size = size;698699return status;700}701702static efi_status_t allocate_e820(struct boot_params *params,703struct setup_data **e820ext,704u32 *e820ext_size)705{706struct efi_boot_memmap *map __free(efi_pool) = NULL;707efi_status_t status;708__u32 nr_desc;709710status = efi_get_memory_map(&map, false);711if (status != EFI_SUCCESS)712return status;713714nr_desc = map->map_size / map->desc_size;715if (nr_desc > ARRAY_SIZE(params->e820_table) - EFI_MMAP_NR_SLACK_SLOTS) {716u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table) +717EFI_MMAP_NR_SLACK_SLOTS;718719status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size);720if (status != EFI_SUCCESS)721return status;722}723724if (IS_ENABLED(CONFIG_UNACCEPTED_MEMORY))725return allocate_unaccepted_bitmap(nr_desc, map);726727return EFI_SUCCESS;728}729730struct exit_boot_struct {731struct boot_params *boot_params;732struct efi_info *efi;733};734735static efi_status_t exit_boot_func(struct efi_boot_memmap *map,736void *priv)737{738const char *signature;739struct exit_boot_struct *p = priv;740741signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE742: EFI32_LOADER_SIGNATURE;743memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));744745efi_set_u64_split((unsigned long)efi_system_table,746&p->efi->efi_systab, &p->efi->efi_systab_hi);747p->efi->efi_memdesc_size = map->desc_size;748p->efi->efi_memdesc_version = map->desc_ver;749efi_set_u64_split((unsigned long)map->map,750&p->efi->efi_memmap, &p->efi->efi_memmap_hi);751p->efi->efi_memmap_size = map->map_size;752753return EFI_SUCCESS;754}755756static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)757{758struct setup_data *e820ext = NULL;759__u32 e820ext_size = 0;760efi_status_t status;761struct exit_boot_struct priv;762763priv.boot_params = boot_params;764priv.efi = &boot_params->efi_info;765766status = allocate_e820(boot_params, &e820ext, &e820ext_size);767if (status != EFI_SUCCESS)768return status;769770/* Might as well exit boot services now */771status = efi_exit_boot_services(handle, &priv, exit_boot_func);772if (status != EFI_SUCCESS)773return status;774775/* Historic? */776boot_params->alt_mem_k = 32 * 1024;777778status = setup_e820(boot_params, e820ext, e820ext_size);779if (status != EFI_SUCCESS)780return status;781782return EFI_SUCCESS;783}784785static bool have_unsupported_snp_features(void)786{787u64 unsupported;788789unsupported = snp_get_unsupported_features(sev_get_status());790if (unsupported) {791efi_err("Unsupported SEV-SNP features detected: 0x%llx\n",792unsupported);793return true;794}795return false;796}797798static void efi_get_seed(void *seed, int size)799{800efi_get_random_bytes(size, seed);801802/*803* This only updates seed[0] when running on 32-bit, but in that case,804* seed[1] is not used anyway, as there is no virtual KASLR on 32-bit.805*/806*(unsigned long *)seed ^= kaslr_get_random_long("EFI");807}808809static void error(char *str)810{811efi_warn("Decompression failed: %s\n", str);812}813814static const char *cmdline_memmap_override;815816static efi_status_t parse_options(const char *cmdline)817{818static const char opts[][14] = {819"mem=", "memmap=", "hugepages="820};821822for (int i = 0; i < ARRAY_SIZE(opts); i++) {823const char *p = strstr(cmdline, opts[i]);824825if (p == cmdline || (p > cmdline && isspace(p[-1]))) {826cmdline_memmap_override = opts[i];827break;828}829}830831return efi_parse_options(cmdline);832}833834static efi_status_t efi_decompress_kernel(unsigned long *kernel_entry,835struct boot_params *boot_params)836{837unsigned long virt_addr = LOAD_PHYSICAL_ADDR;838unsigned long addr, alloc_size, entry;839efi_status_t status;840u32 seed[2] = {};841842boot_params_ptr = boot_params;843844/* determine the required size of the allocation */845alloc_size = ALIGN(max_t(unsigned long, output_len, kernel_total_size),846MIN_KERNEL_ALIGN);847848if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && !efi_nokaslr) {849u64 range = KERNEL_IMAGE_SIZE - LOAD_PHYSICAL_ADDR - kernel_total_size;850static const efi_char16_t ami[] = L"American Megatrends";851852efi_get_seed(seed, sizeof(seed));853854virt_addr += (range * seed[1]) >> 32;855virt_addr &= ~(CONFIG_PHYSICAL_ALIGN - 1);856857/*858* Older Dell systems with AMI UEFI firmware v2.0 may hang859* while decompressing the kernel if physical address860* randomization is enabled.861*862* https://bugzilla.kernel.org/show_bug.cgi?id=218173863*/864if (efi_system_table->hdr.revision <= EFI_2_00_SYSTEM_TABLE_REVISION &&865!memcmp(efistub_fw_vendor(), ami, sizeof(ami))) {866efi_debug("AMI firmware v2.0 or older detected - disabling physical KASLR\n");867seed[0] = 0;868} else if (cmdline_memmap_override) {869efi_info("%s detected on the kernel command line - disabling physical KASLR\n",870cmdline_memmap_override);871seed[0] = 0;872}873874boot_params->hdr.loadflags |= KASLR_FLAG;875}876877status = efi_random_alloc(alloc_size, CONFIG_PHYSICAL_ALIGN, &addr,878seed[0], EFI_LOADER_CODE,879LOAD_PHYSICAL_ADDR,880EFI_X86_KERNEL_ALLOC_LIMIT);881if (status != EFI_SUCCESS)882return status;883884entry = decompress_kernel((void *)addr, virt_addr, error);885if (entry == ULONG_MAX) {886efi_free(alloc_size, addr);887return EFI_LOAD_ERROR;888}889890*kernel_entry = addr + entry;891892return efi_adjust_memory_range_protection(addr, kernel_text_size) ?:893efi_adjust_memory_range_protection(addr + kernel_inittext_offset,894kernel_inittext_size);895}896897static void __noreturn enter_kernel(unsigned long kernel_addr,898struct boot_params *boot_params)899{900/* enter decompressed kernel with boot_params pointer in RSI/ESI */901asm("jmp *%0"::"r"(kernel_addr), "S"(boot_params));902903unreachable();904}905906/*907* On success, this routine will jump to the relocated image directly and never908* return. On failure, it will exit to the firmware via efi_exit() instead of909* returning.910*/911void __noreturn efi_stub_entry(efi_handle_t handle,912efi_system_table_t *sys_table_arg,913struct boot_params *boot_params)914915{916efi_guid_t guid = EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID;917const struct linux_efi_initrd *initrd = NULL;918unsigned long kernel_entry;919struct setup_header *hdr;920efi_status_t status;921922efi_system_table = sys_table_arg;923/* Check if we were booted by the EFI firmware */924if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)925efi_exit(handle, EFI_INVALID_PARAMETER);926927if (!IS_ENABLED(CONFIG_EFI_HANDOVER_PROTOCOL) || !boot_params) {928status = efi_allocate_bootparams(handle, &boot_params);929if (status != EFI_SUCCESS)930efi_exit(handle, status);931}932933hdr = &boot_params->hdr;934935if (have_unsupported_snp_features())936efi_exit(handle, EFI_UNSUPPORTED);937938if (IS_ENABLED(CONFIG_EFI_DXE_MEM_ATTRIBUTES)) {939efi_dxe_table = get_efi_config_table(EFI_DXE_SERVICES_TABLE_GUID);940if (efi_dxe_table &&941efi_dxe_table->hdr.signature != EFI_DXE_SERVICES_TABLE_SIGNATURE) {942efi_warn("Ignoring DXE services table: invalid signature\n");943efi_dxe_table = NULL;944}945}946947/* grab the memory attributes protocol if it exists */948efi_bs_call(locate_protocol, &guid, NULL, (void **)&memattr);949950status = efi_setup_5level_paging();951if (status != EFI_SUCCESS) {952efi_err("efi_setup_5level_paging() failed!\n");953goto fail;954}955956#ifdef CONFIG_CMDLINE_BOOL957status = parse_options(CONFIG_CMDLINE);958if (status != EFI_SUCCESS) {959efi_err("Failed to parse options\n");960goto fail;961}962#endif963if (!IS_ENABLED(CONFIG_CMDLINE_OVERRIDE)) {964unsigned long cmdline_paddr = ((u64)hdr->cmd_line_ptr |965((u64)boot_params->ext_cmd_line_ptr << 32));966status = parse_options((char *)cmdline_paddr);967if (status != EFI_SUCCESS) {968efi_err("Failed to parse options\n");969goto fail;970}971}972973if (efi_mem_encrypt > 0)974hdr->xloadflags |= XLF_MEM_ENCRYPTION;975976status = efi_decompress_kernel(&kernel_entry, boot_params);977if (status != EFI_SUCCESS) {978efi_err("Failed to decompress kernel\n");979goto fail;980}981982/*983* At this point, an initrd may already have been loaded by the984* bootloader and passed via bootparams. We permit an initrd loaded985* from the LINUX_EFI_INITRD_MEDIA_GUID device path to supersede it.986*987* If the device path is not present, any command-line initrd=988* arguments will be processed only if image is not NULL, which will be989* the case only if we were loaded via the PE entry point.990*/991status = efi_load_initrd(image, hdr->initrd_addr_max, ULONG_MAX,992&initrd);993if (status != EFI_SUCCESS)994goto fail;995if (initrd && initrd->size > 0) {996efi_set_u64_split(initrd->base, &hdr->ramdisk_image,997&boot_params->ext_ramdisk_image);998efi_set_u64_split(initrd->size, &hdr->ramdisk_size,999&boot_params->ext_ramdisk_size);1000}100110021003/*1004* If the boot loader gave us a value for secure_boot then we use that,1005* otherwise we ask the BIOS.1006*/1007if (boot_params->secure_boot == efi_secureboot_mode_unset)1008boot_params->secure_boot = efi_get_secureboot();10091010/* Ask the firmware to clear memory on unclean shutdown */1011efi_enable_reset_attack_mitigation();10121013efi_random_get_seed();10141015efi_retrieve_eventlog();10161017setup_graphics(boot_params);10181019setup_efi_pci(boot_params);10201021setup_quirks(boot_params);10221023setup_unaccepted_memory();10241025status = exit_boot(boot_params, handle);1026if (status != EFI_SUCCESS) {1027efi_err("exit_boot() failed!\n");1028goto fail;1029}10301031/*1032* Call the SEV init code while still running with the firmware's1033* GDT/IDT, so #VC exceptions will be handled by EFI.1034*/1035sev_enable(boot_params);10361037efi_5level_switch();10381039enter_kernel(kernel_entry, boot_params);1040fail:1041efi_err("efi_stub_entry() failed!\n");10421043efi_exit(handle, status);1044}10451046efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,1047efi_system_table_t *sys_table_arg)1048{1049efi_stub_entry(handle, sys_table_arg, NULL);1050}10511052#ifdef CONFIG_EFI_HANDOVER_PROTOCOL1053void efi_handover_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,1054struct boot_params *boot_params)1055{1056memset(_bss, 0, _ebss - _bss);1057efi_stub_entry(handle, sys_table_arg, boot_params);1058}10591060#ifndef CONFIG_EFI_MIXED1061extern __alias(efi_handover_entry)1062void efi32_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,1063struct boot_params *boot_params);10641065extern __alias(efi_handover_entry)1066void efi64_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,1067struct boot_params *boot_params);1068#endif1069#endif107010711072