Path: blob/master/drivers/firmware/efi/libstub/arm32-stub.c
26483 views
// SPDX-License-Identifier: GPL-2.01/*2* Copyright (C) 2013 Linaro Ltd; <[email protected]>3*/4#include <linux/efi.h>5#include <asm/efi.h>67#include "efistub.h"89static efi_guid_t cpu_state_guid = LINUX_EFI_ARM_CPU_STATE_TABLE_GUID;1011struct efi_arm_entry_state *efi_entry_state;1213static void get_cpu_state(u32 *cpsr, u32 *sctlr)14{15asm("mrs %0, cpsr" : "=r"(*cpsr));16if ((*cpsr & MODE_MASK) == HYP_MODE)17asm("mrc p15, 4, %0, c1, c0, 0" : "=r"(*sctlr));18else19asm("mrc p15, 0, %0, c1, c0, 0" : "=r"(*sctlr));20}2122efi_status_t check_platform_features(void)23{24efi_status_t status;25u32 cpsr, sctlr;26int block;2728get_cpu_state(&cpsr, &sctlr);2930efi_info("Entering in %s mode with MMU %sabled\n",31((cpsr & MODE_MASK) == HYP_MODE) ? "HYP" : "SVC",32(sctlr & 1) ? "en" : "dis");3334status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,35sizeof(*efi_entry_state),36(void **)&efi_entry_state);37if (status != EFI_SUCCESS) {38efi_err("allocate_pool() failed\n");39return status;40}4142efi_entry_state->cpsr_before_ebs = cpsr;43efi_entry_state->sctlr_before_ebs = sctlr;4445status = efi_bs_call(install_configuration_table, &cpu_state_guid,46efi_entry_state);47if (status != EFI_SUCCESS) {48efi_err("install_configuration_table() failed\n");49goto free_state;50}5152/* non-LPAE kernels can run anywhere */53if (!IS_ENABLED(CONFIG_ARM_LPAE))54return EFI_SUCCESS;5556/* LPAE kernels need compatible hardware */57block = cpuid_feature_extract(CPUID_EXT_MMFR0, 0);58if (block < 5) {59efi_err("This LPAE kernel is not supported by your CPU\n");60status = EFI_UNSUPPORTED;61goto drop_table;62}63return EFI_SUCCESS;6465drop_table:66efi_bs_call(install_configuration_table, &cpu_state_guid, NULL);67free_state:68efi_bs_call(free_pool, efi_entry_state);69return status;70}7172void efi_handle_post_ebs_state(void)73{74get_cpu_state(&efi_entry_state->cpsr_after_ebs,75&efi_entry_state->sctlr_after_ebs);76}7778efi_status_t handle_kernel_image(unsigned long *image_addr,79unsigned long *image_size,80unsigned long *reserve_addr,81unsigned long *reserve_size,82efi_loaded_image_t *image,83efi_handle_t image_handle)84{85const int slack = TEXT_OFFSET - 5 * PAGE_SIZE;86int alloc_size = MAX_UNCOMP_KERNEL_SIZE + EFI_PHYS_ALIGN;87unsigned long alloc_base, kernel_base;88efi_status_t status;8990/*91* Allocate space for the decompressed kernel as low as possible.92* The region should be 16 MiB aligned, but the first 'slack' bytes93* are not used by Linux, so we allow those to be occupied by the94* firmware.95*/96status = efi_low_alloc_above(alloc_size, EFI_PAGE_SIZE, &alloc_base, 0x0);97if (status != EFI_SUCCESS) {98efi_err("Unable to allocate memory for uncompressed kernel.\n");99return status;100}101102if ((alloc_base % EFI_PHYS_ALIGN) > slack) {103/*104* More than 'slack' bytes are already occupied at the base of105* the allocation, so we need to advance to the next 16 MiB block.106*/107kernel_base = round_up(alloc_base, EFI_PHYS_ALIGN);108efi_info("Free memory starts at 0x%lx, setting kernel_base to 0x%lx\n",109alloc_base, kernel_base);110} else {111kernel_base = round_down(alloc_base, EFI_PHYS_ALIGN);112}113114*reserve_addr = kernel_base + slack;115*reserve_size = MAX_UNCOMP_KERNEL_SIZE;116117/* now free the parts that we will not use */118if (*reserve_addr > alloc_base) {119efi_bs_call(free_pages, alloc_base,120(*reserve_addr - alloc_base) / EFI_PAGE_SIZE);121alloc_size -= *reserve_addr - alloc_base;122}123efi_bs_call(free_pages, *reserve_addr + MAX_UNCOMP_KERNEL_SIZE,124(alloc_size - MAX_UNCOMP_KERNEL_SIZE) / EFI_PAGE_SIZE);125126*image_addr = kernel_base + TEXT_OFFSET;127*image_size = 0;128129efi_debug("image addr == 0x%lx, reserve_addr == 0x%lx\n",130*image_addr, *reserve_addr);131132return EFI_SUCCESS;133}134135136