brintos

brintos / linux-shallow public Read only

0
0
Text · 4.7 KiB · d694bcf Raw
166 lines · c
1// SPDX-License-Identifier: GPL-2.02 3#include <linux/efi.h>4#include <asm/efi.h>5 6#include "efistub.h"7 8/**9 * efi_low_alloc_above() - allocate pages at or above given address10 * @size:	size of the memory area to allocate11 * @align:	minimum alignment of the allocated memory area. It should12 *		a power of two.13 * @addr:	on exit the address of the allocated memory14 * @min:	minimum address to used for the memory allocation15 *16 * Allocate at the lowest possible address that is not below @min as17 * EFI_LOADER_DATA. The allocated pages are aligned according to @align but at18 * least EFI_ALLOC_ALIGN. The first allocated page will not below the address19 * given by @min.20 *21 * Return:	status code22 */23efi_status_t efi_low_alloc_above(unsigned long size, unsigned long align,24				 unsigned long *addr, unsigned long min)25{26	struct efi_boot_memmap *map;27	efi_status_t status;28	unsigned long nr_pages;29	int i;30 31	status = efi_get_memory_map(&map, false);32	if (status != EFI_SUCCESS)33		goto fail;34 35	/*36	 * Enforce minimum alignment that EFI or Linux requires when37	 * requesting a specific address.  We are doing page-based (or38	 * larger) allocations, and both the address and size must meet39	 * alignment constraints.40	 */41	if (align < EFI_ALLOC_ALIGN)42		align = EFI_ALLOC_ALIGN;43 44	size = round_up(size, EFI_ALLOC_ALIGN);45	nr_pages = size / EFI_PAGE_SIZE;46	for (i = 0; i < map->map_size / map->desc_size; i++) {47		efi_memory_desc_t *desc;48		unsigned long m = (unsigned long)map->map;49		u64 start, end;50 51		desc = efi_memdesc_ptr(m, map->desc_size, i);52 53		if (desc->type != EFI_CONVENTIONAL_MEMORY)54			continue;55 56		if (efi_soft_reserve_enabled() &&57		    (desc->attribute & EFI_MEMORY_SP))58			continue;59 60		if (desc->num_pages < nr_pages)61			continue;62 63		start = desc->phys_addr;64		end = start + desc->num_pages * EFI_PAGE_SIZE;65 66		if (start < min)67			start = min;68 69		start = round_up(start, align);70		if ((start + size) > end)71			continue;72 73		status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS,74				     EFI_LOADER_DATA, nr_pages, &start);75		if (status == EFI_SUCCESS) {76			*addr = start;77			break;78		}79	}80 81	if (i == map->map_size / map->desc_size)82		status = EFI_NOT_FOUND;83 84	efi_bs_call(free_pool, map);85fail:86	return status;87}88 89/**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,111				 unsigned long image_size,112				 unsigned long alloc_size,113				 unsigned long preferred_addr,114				 unsigned long alignment,115				 unsigned long min_addr)116{117	unsigned long cur_image_addr;118	unsigned long new_addr = 0;119	efi_status_t status;120	unsigned long nr_pages;121	efi_physical_addr_t efi_addr = preferred_addr;122 123	if (!image_addr || !image_size || !alloc_size)124		return EFI_INVALID_PARAMETER;125	if (alloc_size < image_size)126		return EFI_INVALID_PARAMETER;127 128	cur_image_addr = *image_addr;129 130	/*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	 */138	nr_pages = round_up(alloc_size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;139	status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS,140			     EFI_LOADER_DATA, nr_pages, &efi_addr);141	new_addr = efi_addr;142	/*143	 * If preferred address allocation failed allocate as low as144	 * possible.145	 */146	if (status != EFI_SUCCESS) {147		status = efi_low_alloc_above(alloc_size, alignment, &new_addr,148					     min_addr);149	}150	if (status != EFI_SUCCESS) {151		efi_err("Failed to allocate usable memory for kernel.\n");152		return status;153	}154 155	/*156	 * We know source/dest won't overlap since both memory ranges157	 * have been allocated by UEFI, so we can safely use memcpy.158	 */159	memcpy((void *)new_addr, (void *)cur_image_addr, image_size);160 161	/* Return the new address of the relocated image. */162	*image_addr = new_addr;163 164	return status;165}166