Path: blob/master/drivers/firmware/efi/libstub/relocate.c
26483 views
// SPDX-License-Identifier: GPL-2.012#include <linux/efi.h>3#include <asm/efi.h>45#include "efistub.h"67/**8* efi_low_alloc_above() - allocate pages at or above given address9* @size: size of the memory area to allocate10* @align: minimum alignment of the allocated memory area. It should11* a power of two.12* @addr: on exit the address of the allocated memory13* @min: minimum address to used for the memory allocation14*15* Allocate at the lowest possible address that is not below @min as16* EFI_LOADER_DATA. The allocated pages are aligned according to @align but at17* least EFI_ALLOC_ALIGN. The first allocated page will not below the address18* given by @min.19*20* Return: status code21*/22efi_status_t efi_low_alloc_above(unsigned long size, unsigned long align,23unsigned long *addr, unsigned long min)24{25struct efi_boot_memmap *map __free(efi_pool) = NULL;26efi_status_t status;27unsigned long nr_pages;28int i;2930status = efi_get_memory_map(&map, false);31if (status != EFI_SUCCESS)32return status;3334/*35* Enforce minimum alignment that EFI or Linux requires when36* requesting a specific address. We are doing page-based (or37* larger) allocations, and both the address and size must meet38* alignment constraints.39*/40if (align < EFI_ALLOC_ALIGN)41align = EFI_ALLOC_ALIGN;4243size = round_up(size, EFI_ALLOC_ALIGN);44nr_pages = size / EFI_PAGE_SIZE;45for (i = 0; i < map->map_size / map->desc_size; i++) {46efi_memory_desc_t *desc;47unsigned long m = (unsigned long)map->map;48u64 start, end;4950desc = efi_memdesc_ptr(m, map->desc_size, i);5152if (desc->type != EFI_CONVENTIONAL_MEMORY)53continue;5455if (desc->attribute & EFI_MEMORY_HOT_PLUGGABLE)56continue;5758if (efi_soft_reserve_enabled() &&59(desc->attribute & EFI_MEMORY_SP))60continue;6162if (desc->num_pages < nr_pages)63continue;6465start = desc->phys_addr;66end = start + desc->num_pages * EFI_PAGE_SIZE;6768if (start < min)69start = min;7071start = round_up(start, align);72if ((start + size) > end)73continue;7475status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS,76EFI_LOADER_DATA, nr_pages, &start);77if (status == EFI_SUCCESS) {78*addr = start;79break;80}81}8283if (i == map->map_size / map->desc_size)84return EFI_NOT_FOUND;8586return EFI_SUCCESS;87}8889/**90* efi_relocate_kernel() - copy memory area91* @image_addr: pointer to address of memory area to copy92* @image_size: size of memory area to copy93* @alloc_size: minimum size of memory to allocate, must be greater or94* equal to image_size95* @preferred_addr: preferred target address96* @alignment: minimum alignment of the allocated memory area. It97* should be a power of two.98* @min_addr: minimum target address99*100* Copy a memory area to a newly allocated memory area aligned according101* to @alignment but at least EFI_ALLOC_ALIGN. If the preferred address102* is not available, the allocated address will not be below @min_addr.103* On exit, @image_addr is updated to the target copy address that was used.104*105* This function is used to copy the Linux kernel verbatim. It does not apply106* any relocation changes.107*108* Return: status code109*/110efi_status_t efi_relocate_kernel(unsigned long *image_addr,111unsigned long image_size,112unsigned long alloc_size,113unsigned long preferred_addr,114unsigned long alignment,115unsigned long min_addr)116{117unsigned long cur_image_addr;118unsigned long new_addr = 0;119efi_status_t status;120unsigned long nr_pages;121efi_physical_addr_t efi_addr = preferred_addr;122123if (!image_addr || !image_size || !alloc_size)124return EFI_INVALID_PARAMETER;125if (alloc_size < image_size)126return EFI_INVALID_PARAMETER;127128cur_image_addr = *image_addr;129130/*131* The EFI firmware loader could have placed the kernel image132* anywhere in memory, but the kernel has restrictions on the133* max physical address it can run at. Some architectures134* also have a preferred address, so first try to relocate135* to the preferred address. If that fails, allocate as low136* as possible while respecting the required alignment.137*/138nr_pages = round_up(alloc_size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;139status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS,140EFI_LOADER_DATA, nr_pages, &efi_addr);141new_addr = efi_addr;142/*143* If preferred address allocation failed allocate as low as144* possible.145*/146if (status != EFI_SUCCESS) {147status = efi_low_alloc_above(alloc_size, alignment, &new_addr,148min_addr);149}150if (status != EFI_SUCCESS) {151efi_err("Failed to allocate usable memory for kernel.\n");152return status;153}154155/*156* We know source/dest won't overlap since both memory ranges157* have been allocated by UEFI, so we can safely use memcpy.158*/159memcpy((void *)new_addr, (void *)cur_image_addr, image_size);160161/* Return the new address of the relocated image. */162*image_addr = new_addr;163164return status;165}166167168