2018-05-07 21:02:21 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2016-03-04 00:10:04 +00:00
|
|
|
/*
|
|
|
|
* EFI application memory management
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016 Alexander Graf
|
|
|
|
*/
|
|
|
|
|
2023-03-19 08:20:22 +00:00
|
|
|
#define LOG_CATEGORY LOGC_EFI
|
|
|
|
|
2016-03-04 00:10:04 +00:00
|
|
|
#include <common.h>
|
|
|
|
#include <efi_loader.h>
|
2019-11-14 19:57:45 +00:00
|
|
|
#include <init.h>
|
2023-03-19 08:20:22 +00:00
|
|
|
#include <log.h>
|
2016-03-04 00:10:04 +00:00
|
|
|
#include <malloc.h>
|
2018-06-18 15:23:15 +00:00
|
|
|
#include <mapmem.h>
|
2018-03-08 20:53:27 +00:00
|
|
|
#include <watchdog.h>
|
2020-05-10 17:39:56 +00:00
|
|
|
#include <asm/cache.h>
|
2020-10-31 03:38:53 +00:00
|
|
|
#include <asm/global_data.h>
|
2016-03-30 14:38:29 +00:00
|
|
|
#include <linux/list_sort.h>
|
2018-09-17 11:54:33 +00:00
|
|
|
#include <linux/sizes.h>
|
2016-03-04 00:10:04 +00:00
|
|
|
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2019-03-26 04:31:41 +00:00
|
|
|
/* Magic number identifying memory allocated from pool */
|
|
|
|
#define EFI_ALLOC_POOL_MAGIC 0x1fe67ddf6491caa2
|
|
|
|
|
2018-07-02 10:53:55 +00:00
|
|
|
efi_uintn_t efi_memory_map_key;
|
|
|
|
|
2016-03-04 00:10:04 +00:00
|
|
|
struct efi_mem_list {
|
|
|
|
struct list_head link;
|
|
|
|
struct efi_mem_desc desc;
|
|
|
|
};
|
|
|
|
|
2016-05-27 10:25:03 +00:00
|
|
|
#define EFI_CARVE_NO_OVERLAP -1
|
|
|
|
#define EFI_CARVE_LOOP_AGAIN -2
|
|
|
|
#define EFI_CARVE_OVERLAPS_NONRAM -3
|
2023-07-30 10:27:03 +00:00
|
|
|
#define EFI_CARVE_OUT_OF_RESOURCES -4
|
2016-05-27 10:25:03 +00:00
|
|
|
|
2016-03-04 00:10:04 +00:00
|
|
|
/* This list contains all memory map items */
|
2023-04-05 12:15:18 +00:00
|
|
|
static LIST_HEAD(efi_mem);
|
2016-03-04 00:10:04 +00:00
|
|
|
|
2016-05-11 16:25:48 +00:00
|
|
|
#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
|
|
|
|
void *efi_bounce_buffer;
|
|
|
|
#endif
|
|
|
|
|
2019-03-26 04:31:41 +00:00
|
|
|
/**
|
2019-07-14 10:28:56 +00:00
|
|
|
* struct efi_pool_allocation - memory block allocated from pool
|
2019-03-26 04:31:41 +00:00
|
|
|
*
|
|
|
|
* @num_pages: number of pages allocated
|
|
|
|
* @checksum: checksum
|
2019-07-14 10:28:56 +00:00
|
|
|
* @data: allocated pool memory
|
2019-03-26 04:31:41 +00:00
|
|
|
*
|
2019-07-14 10:28:56 +00:00
|
|
|
* U-Boot services each UEFI AllocatePool() request as a separate
|
|
|
|
* (multiple) page allocation. We have to track the number of pages
|
2016-10-09 20:17:26 +00:00
|
|
|
* to be able to free the correct amount later.
|
2019-07-14 10:28:56 +00:00
|
|
|
*
|
|
|
|
* The checksum calculated in function checksum() is used in FreePool() to avoid
|
|
|
|
* freeing memory not allocated by AllocatePool() and duplicate freeing.
|
|
|
|
*
|
2016-10-09 20:17:26 +00:00
|
|
|
* EFI requires 8 byte alignment for pool allocations, so we can
|
2019-07-14 10:28:56 +00:00
|
|
|
* prepend each allocation with these header fields.
|
2016-10-09 20:17:26 +00:00
|
|
|
*/
|
|
|
|
struct efi_pool_allocation {
|
|
|
|
u64 num_pages;
|
2019-03-26 04:31:41 +00:00
|
|
|
u64 checksum;
|
2017-09-13 22:05:36 +00:00
|
|
|
char data[] __aligned(ARCH_DMA_MINALIGN);
|
2016-10-09 20:17:26 +00:00
|
|
|
};
|
|
|
|
|
2019-03-26 04:31:41 +00:00
|
|
|
/**
|
|
|
|
* checksum() - calculate checksum for memory allocated from pool
|
|
|
|
*
|
|
|
|
* @alloc: allocation header
|
|
|
|
* Return: checksum, always non-zero
|
|
|
|
*/
|
|
|
|
static u64 checksum(struct efi_pool_allocation *alloc)
|
|
|
|
{
|
|
|
|
u64 addr = (uintptr_t)alloc;
|
|
|
|
u64 ret = (addr >> 32) ^ (addr << 32) ^ alloc->num_pages ^
|
|
|
|
EFI_ALLOC_POOL_MAGIC;
|
|
|
|
if (!ret)
|
|
|
|
++ret;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-01-08 00:00:00 +00:00
|
|
|
/**
|
|
|
|
* efi_mem_cmp() - comparator function for sorting memory map
|
|
|
|
*
|
2016-03-30 14:38:29 +00:00
|
|
|
* Sorts the memory list from highest address to lowest address
|
|
|
|
*
|
|
|
|
* When allocating memory we should always start from the highest
|
|
|
|
* address chunk, so sort the memory list such that the first list
|
|
|
|
* iterator gets the highest address and goes lower from there.
|
2023-01-08 00:00:00 +00:00
|
|
|
*
|
|
|
|
* @priv: unused
|
|
|
|
* @a: first memory area
|
|
|
|
* @b: second memory area
|
|
|
|
* Return: 1 if @a is before @b, -1 if @b is before @a, 0 if equal
|
2016-03-30 14:38:29 +00:00
|
|
|
*/
|
|
|
|
static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b)
|
|
|
|
{
|
|
|
|
struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link);
|
|
|
|
struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link);
|
|
|
|
|
|
|
|
if (mema->desc.physical_start == memb->desc.physical_start)
|
|
|
|
return 0;
|
|
|
|
else if (mema->desc.physical_start < memb->desc.physical_start)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-01-08 00:00:00 +00:00
|
|
|
/**
|
|
|
|
* desc_get_end() - get end address of memory area
|
|
|
|
*
|
|
|
|
* @desc: memory descriptor
|
|
|
|
* Return: end address + 1
|
|
|
|
*/
|
2018-09-16 15:05:29 +00:00
|
|
|
static uint64_t desc_get_end(struct efi_mem_desc *desc)
|
|
|
|
{
|
|
|
|
return desc->physical_start + (desc->num_pages << EFI_PAGE_SHIFT);
|
|
|
|
}
|
|
|
|
|
2023-01-08 00:00:00 +00:00
|
|
|
/**
|
|
|
|
* efi_mem_sort() - sort memory map
|
|
|
|
*
|
|
|
|
* Sort the memory map and then try to merge adjacent memory areas.
|
|
|
|
*/
|
2016-03-30 14:38:29 +00:00
|
|
|
static void efi_mem_sort(void)
|
|
|
|
{
|
2018-09-16 15:05:29 +00:00
|
|
|
struct list_head *lhandle;
|
|
|
|
struct efi_mem_list *prevmem = NULL;
|
|
|
|
bool merge_again = true;
|
|
|
|
|
2016-03-30 14:38:29 +00:00
|
|
|
list_sort(NULL, &efi_mem, efi_mem_cmp);
|
2018-09-16 15:05:29 +00:00
|
|
|
|
|
|
|
/* Now merge entries that can be merged */
|
|
|
|
while (merge_again) {
|
|
|
|
merge_again = false;
|
|
|
|
list_for_each(lhandle, &efi_mem) {
|
|
|
|
struct efi_mem_list *lmem;
|
|
|
|
struct efi_mem_desc *prev = &prevmem->desc;
|
|
|
|
struct efi_mem_desc *cur;
|
|
|
|
uint64_t pages;
|
|
|
|
|
|
|
|
lmem = list_entry(lhandle, struct efi_mem_list, link);
|
|
|
|
if (!prevmem) {
|
|
|
|
prevmem = lmem;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
cur = &lmem->desc;
|
|
|
|
|
|
|
|
if ((desc_get_end(cur) == prev->physical_start) &&
|
|
|
|
(prev->type == cur->type) &&
|
|
|
|
(prev->attribute == cur->attribute)) {
|
|
|
|
/* There is an existing map before, reuse it */
|
|
|
|
pages = cur->num_pages;
|
|
|
|
prev->num_pages += pages;
|
|
|
|
prev->physical_start -= pages << EFI_PAGE_SHIFT;
|
|
|
|
prev->virtual_start -= pages << EFI_PAGE_SHIFT;
|
|
|
|
list_del(&lmem->link);
|
|
|
|
free(lmem);
|
|
|
|
|
|
|
|
merge_again = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
prevmem = lmem;
|
|
|
|
}
|
|
|
|
}
|
2016-03-30 14:38:29 +00:00
|
|
|
}
|
|
|
|
|
2023-01-08 00:00:00 +00:00
|
|
|
/**
|
|
|
|
* efi_mem_carve_out() - unmap memory region
|
2018-05-27 14:45:09 +00:00
|
|
|
*
|
|
|
|
* @map: memory map
|
|
|
|
* @carve_desc: memory region to unmap
|
|
|
|
* @overlap_only_ram: the carved out region may only overlap RAM
|
2023-01-08 00:00:00 +00:00
|
|
|
* Return: the number of overlapping pages which have been
|
2018-05-27 14:45:09 +00:00
|
|
|
* removed from the map,
|
|
|
|
* EFI_CARVE_NO_OVERLAP, if the regions don't overlap,
|
|
|
|
* EFI_CARVE_OVERLAPS_NONRAM, if the carve and map overlap,
|
|
|
|
* and the map contains anything but free ram
|
|
|
|
* (only when overlap_only_ram is true),
|
|
|
|
* EFI_CARVE_LOOP_AGAIN, if the mapping list should be
|
|
|
|
* traversed again, as it has been altered.
|
2016-03-04 00:10:04 +00:00
|
|
|
*
|
2018-05-27 14:45:09 +00:00
|
|
|
* Unmaps all memory occupied by the carve_desc region from the list entry
|
|
|
|
* pointed to by map.
|
2016-10-01 21:32:23 +00:00
|
|
|
*
|
|
|
|
* In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility
|
2018-05-27 14:45:09 +00:00
|
|
|
* to re-add the already carved out pages to the mapping.
|
2016-03-04 00:10:04 +00:00
|
|
|
*/
|
2018-05-27 14:45:09 +00:00
|
|
|
static s64 efi_mem_carve_out(struct efi_mem_list *map,
|
2016-03-04 00:10:04 +00:00
|
|
|
struct efi_mem_desc *carve_desc,
|
|
|
|
bool overlap_only_ram)
|
|
|
|
{
|
|
|
|
struct efi_mem_list *newmap;
|
|
|
|
struct efi_mem_desc *map_desc = &map->desc;
|
|
|
|
uint64_t map_start = map_desc->physical_start;
|
|
|
|
uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT);
|
|
|
|
uint64_t carve_start = carve_desc->physical_start;
|
|
|
|
uint64_t carve_end = carve_start +
|
|
|
|
(carve_desc->num_pages << EFI_PAGE_SHIFT);
|
|
|
|
|
|
|
|
/* check whether we're overlapping */
|
|
|
|
if ((carve_end <= map_start) || (carve_start >= map_end))
|
2016-05-27 10:25:03 +00:00
|
|
|
return EFI_CARVE_NO_OVERLAP;
|
2016-03-04 00:10:04 +00:00
|
|
|
|
|
|
|
/* We're overlapping with non-RAM, warn the caller if desired */
|
|
|
|
if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY))
|
2016-05-27 10:25:03 +00:00
|
|
|
return EFI_CARVE_OVERLAPS_NONRAM;
|
2016-03-04 00:10:04 +00:00
|
|
|
|
|
|
|
/* Sanitize carve_start and carve_end to lie within our bounds */
|
|
|
|
carve_start = max(carve_start, map_start);
|
|
|
|
carve_end = min(carve_end, map_end);
|
|
|
|
|
|
|
|
/* Carving at the beginning of our map? Just move it! */
|
|
|
|
if (carve_start == map_start) {
|
|
|
|
if (map_end == carve_end) {
|
|
|
|
/* Full overlap, just remove map */
|
|
|
|
list_del(&map->link);
|
2016-10-01 21:32:29 +00:00
|
|
|
free(map);
|
|
|
|
} else {
|
|
|
|
map->desc.physical_start = carve_end;
|
2019-04-11 18:08:54 +00:00
|
|
|
map->desc.virtual_start = carve_end;
|
2016-10-01 21:32:29 +00:00
|
|
|
map->desc.num_pages = (map_end - carve_end)
|
|
|
|
>> EFI_PAGE_SHIFT;
|
2016-03-04 00:10:04 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 10:25:03 +00:00
|
|
|
return (carve_end - carve_start) >> EFI_PAGE_SHIFT;
|
2016-03-04 00:10:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Overlapping maps, just split the list map at carve_start,
|
|
|
|
* it will get moved or removed in the next iteration.
|
|
|
|
*
|
|
|
|
* [ map_desc |__carve_start__| newmap ]
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Create a new map from [ carve_start ... map_end ] */
|
|
|
|
newmap = calloc(1, sizeof(*newmap));
|
2023-07-30 10:27:03 +00:00
|
|
|
if (!newmap)
|
|
|
|
return EFI_CARVE_OUT_OF_RESOURCES;
|
2016-03-04 00:10:04 +00:00
|
|
|
newmap->desc = map->desc;
|
|
|
|
newmap->desc.physical_start = carve_start;
|
2019-04-11 18:08:54 +00:00
|
|
|
newmap->desc.virtual_start = carve_start;
|
2016-03-04 00:10:04 +00:00
|
|
|
newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT;
|
2016-10-01 21:32:28 +00:00
|
|
|
/* Insert before current entry (descending address order) */
|
|
|
|
list_add_tail(&newmap->link, &map->link);
|
2016-03-04 00:10:04 +00:00
|
|
|
|
|
|
|
/* Shrink the map to [ map_start ... carve_start ] */
|
|
|
|
map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT;
|
|
|
|
|
2016-05-27 10:25:03 +00:00
|
|
|
return EFI_CARVE_LOOP_AGAIN;
|
2016-03-04 00:10:04 +00:00
|
|
|
}
|
|
|
|
|
2019-07-15 11:00:39 +00:00
|
|
|
/**
|
2020-05-17 10:29:19 +00:00
|
|
|
* efi_add_memory_map_pg() - add pages to the memory map
|
2019-07-15 11:00:39 +00:00
|
|
|
*
|
|
|
|
* @start: start address, must be a multiple of EFI_PAGE_SIZE
|
|
|
|
* @pages: number of pages to add
|
|
|
|
* @memory_type: type of memory added
|
2020-08-28 19:47:41 +00:00
|
|
|
* @overlap_only_ram: region may only overlap RAM
|
2019-07-15 11:00:39 +00:00
|
|
|
* Return: status code
|
|
|
|
*/
|
2020-05-17 10:29:19 +00:00
|
|
|
static efi_status_t efi_add_memory_map_pg(u64 start, u64 pages,
|
|
|
|
int memory_type,
|
|
|
|
bool overlap_only_ram)
|
2016-03-04 00:10:04 +00:00
|
|
|
{
|
|
|
|
struct list_head *lhandle;
|
|
|
|
struct efi_mem_list *newlist;
|
2016-05-27 10:25:03 +00:00
|
|
|
bool carve_again;
|
|
|
|
uint64_t carved_pages = 0;
|
2019-06-04 18:55:12 +00:00
|
|
|
struct efi_event *evt;
|
2016-03-04 00:10:04 +00:00
|
|
|
|
2019-04-04 19:50:02 +00:00
|
|
|
EFI_PRINT("%s: 0x%llx 0x%llx %d %s\n", __func__,
|
|
|
|
start, pages, memory_type, overlap_only_ram ? "yes" : "no");
|
2016-07-17 04:57:11 +00:00
|
|
|
|
2018-07-02 10:53:55 +00:00
|
|
|
if (memory_type >= EFI_MAX_MEMORY_TYPE)
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
|
2016-03-04 00:10:04 +00:00
|
|
|
if (!pages)
|
2019-07-15 11:00:39 +00:00
|
|
|
return EFI_SUCCESS;
|
2016-03-04 00:10:04 +00:00
|
|
|
|
2018-07-02 10:53:55 +00:00
|
|
|
++efi_memory_map_key;
|
2016-03-04 00:10:04 +00:00
|
|
|
newlist = calloc(1, sizeof(*newlist));
|
2023-07-30 10:36:17 +00:00
|
|
|
if (!newlist)
|
|
|
|
return EFI_OUT_OF_RESOURCES;
|
2016-03-04 00:10:04 +00:00
|
|
|
newlist->desc.type = memory_type;
|
|
|
|
newlist->desc.physical_start = start;
|
|
|
|
newlist->desc.virtual_start = start;
|
|
|
|
newlist->desc.num_pages = pages;
|
|
|
|
|
|
|
|
switch (memory_type) {
|
|
|
|
case EFI_RUNTIME_SERVICES_CODE:
|
|
|
|
case EFI_RUNTIME_SERVICES_DATA:
|
2018-07-14 20:53:30 +00:00
|
|
|
newlist->desc.attribute = EFI_MEMORY_WB | EFI_MEMORY_RUNTIME;
|
2016-03-04 00:10:04 +00:00
|
|
|
break;
|
|
|
|
case EFI_MMAP_IO:
|
2018-07-14 20:53:30 +00:00
|
|
|
newlist->desc.attribute = EFI_MEMORY_RUNTIME;
|
2016-03-04 00:10:04 +00:00
|
|
|
break;
|
|
|
|
default:
|
2018-07-14 20:53:30 +00:00
|
|
|
newlist->desc.attribute = EFI_MEMORY_WB;
|
2016-03-04 00:10:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add our new map */
|
|
|
|
do {
|
2016-05-27 10:25:03 +00:00
|
|
|
carve_again = false;
|
2016-03-04 00:10:04 +00:00
|
|
|
list_for_each(lhandle, &efi_mem) {
|
|
|
|
struct efi_mem_list *lmem;
|
2018-05-27 14:45:09 +00:00
|
|
|
s64 r;
|
2016-03-04 00:10:04 +00:00
|
|
|
|
|
|
|
lmem = list_entry(lhandle, struct efi_mem_list, link);
|
|
|
|
r = efi_mem_carve_out(lmem, &newlist->desc,
|
|
|
|
overlap_only_ram);
|
2016-05-27 10:25:03 +00:00
|
|
|
switch (r) {
|
2023-07-30 10:27:03 +00:00
|
|
|
case EFI_CARVE_OUT_OF_RESOURCES:
|
|
|
|
free(newlist);
|
|
|
|
return EFI_OUT_OF_RESOURCES;
|
2016-05-27 10:25:03 +00:00
|
|
|
case EFI_CARVE_OVERLAPS_NONRAM:
|
|
|
|
/*
|
|
|
|
* The user requested to only have RAM overlaps,
|
|
|
|
* but we hit a non-RAM region. Error out.
|
|
|
|
*/
|
2023-07-30 10:59:32 +00:00
|
|
|
free(newlist);
|
2019-07-15 11:00:39 +00:00
|
|
|
return EFI_NO_MAPPING;
|
2016-05-27 10:25:03 +00:00
|
|
|
case EFI_CARVE_NO_OVERLAP:
|
|
|
|
/* Just ignore this list entry */
|
|
|
|
break;
|
|
|
|
case EFI_CARVE_LOOP_AGAIN:
|
|
|
|
/*
|
|
|
|
* We split an entry, but need to loop through
|
|
|
|
* the list again to actually carve it.
|
|
|
|
*/
|
|
|
|
carve_again = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* We carved a number of pages */
|
|
|
|
carved_pages += r;
|
|
|
|
carve_again = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (carve_again) {
|
|
|
|
/* The list changed, we need to start over */
|
2016-03-04 00:10:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-05-27 10:25:03 +00:00
|
|
|
} while (carve_again);
|
|
|
|
|
|
|
|
if (overlap_only_ram && (carved_pages != pages)) {
|
|
|
|
/*
|
|
|
|
* The payload wanted to have RAM overlaps, but we overlapped
|
|
|
|
* with an unallocated region. Error out.
|
|
|
|
*/
|
2023-07-30 10:59:32 +00:00
|
|
|
free(newlist);
|
2019-07-15 11:00:39 +00:00
|
|
|
return EFI_NO_MAPPING;
|
2016-05-27 10:25:03 +00:00
|
|
|
}
|
2016-03-04 00:10:04 +00:00
|
|
|
|
|
|
|
/* Add our new map */
|
|
|
|
list_add_tail(&newlist->link, &efi_mem);
|
|
|
|
|
2016-03-30 14:38:29 +00:00
|
|
|
/* And make sure memory is listed in descending order */
|
|
|
|
efi_mem_sort();
|
|
|
|
|
2019-06-04 18:55:12 +00:00
|
|
|
/* Notify that the memory map was changed */
|
|
|
|
list_for_each_entry(evt, &efi_events, link) {
|
|
|
|
if (evt->group &&
|
|
|
|
!guidcmp(evt->group,
|
|
|
|
&efi_guid_event_group_memory_map_change)) {
|
2019-06-07 04:47:01 +00:00
|
|
|
efi_signal_event(evt);
|
2019-06-04 18:55:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-15 11:00:39 +00:00
|
|
|
return EFI_SUCCESS;
|
2016-03-04 00:10:04 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 10:29:19 +00:00
|
|
|
/**
|
|
|
|
* efi_add_memory_map() - add memory area to the memory map
|
|
|
|
*
|
|
|
|
* @start: start address of the memory area
|
|
|
|
* @size: length in bytes of the memory area
|
|
|
|
* @memory_type: type of memory added
|
|
|
|
*
|
|
|
|
* Return: status code
|
|
|
|
*
|
|
|
|
* This function automatically aligns the start and size of the memory area
|
|
|
|
* to EFI_PAGE_SIZE.
|
|
|
|
*/
|
|
|
|
efi_status_t efi_add_memory_map(u64 start, u64 size, int memory_type)
|
|
|
|
{
|
|
|
|
u64 pages;
|
|
|
|
|
|
|
|
pages = efi_size_in_pages(size + (start & EFI_PAGE_MASK));
|
|
|
|
start &= ~EFI_PAGE_MASK;
|
|
|
|
|
|
|
|
return efi_add_memory_map_pg(start, pages, memory_type, false);
|
|
|
|
}
|
|
|
|
|
2019-05-10 19:21:30 +00:00
|
|
|
/**
|
|
|
|
* efi_check_allocated() - validate address to be freed
|
|
|
|
*
|
|
|
|
* Check that the address is within allocated memory:
|
|
|
|
*
|
|
|
|
* * The address must be in a range of the memory map.
|
|
|
|
* * The address may not point to EFI_CONVENTIONAL_MEMORY.
|
|
|
|
*
|
|
|
|
* Page alignment is not checked as this is not a requirement of
|
|
|
|
* efi_free_pool().
|
|
|
|
*
|
2019-05-11 06:21:17 +00:00
|
|
|
* @addr: address of page to be freed
|
|
|
|
* @must_be_allocated: return success if the page is allocated
|
|
|
|
* Return: status code
|
2019-05-10 19:21:30 +00:00
|
|
|
*/
|
2019-05-11 06:21:17 +00:00
|
|
|
static efi_status_t efi_check_allocated(u64 addr, bool must_be_allocated)
|
2019-05-10 19:21:30 +00:00
|
|
|
{
|
|
|
|
struct efi_mem_list *item;
|
|
|
|
|
|
|
|
list_for_each_entry(item, &efi_mem, link) {
|
|
|
|
u64 start = item->desc.physical_start;
|
|
|
|
u64 end = start + (item->desc.num_pages << EFI_PAGE_SHIFT);
|
|
|
|
|
|
|
|
if (addr >= start && addr < end) {
|
2019-05-11 06:21:17 +00:00
|
|
|
if (must_be_allocated ^
|
|
|
|
(item->desc.type == EFI_CONVENTIONAL_MEMORY))
|
2019-05-10 19:21:30 +00:00
|
|
|
return EFI_SUCCESS;
|
|
|
|
else
|
|
|
|
return EFI_NOT_FOUND;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return EFI_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
2023-01-08 00:00:00 +00:00
|
|
|
/**
|
|
|
|
* efi_find_free_memory() - find free memory pages
|
|
|
|
*
|
|
|
|
* @len: size of memory area needed
|
|
|
|
* @max_addr: highest address to allocate
|
|
|
|
* Return: pointer to free memory area or 0
|
|
|
|
*/
|
2016-03-04 00:10:04 +00:00
|
|
|
static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
|
|
|
|
{
|
|
|
|
struct list_head *lhandle;
|
|
|
|
|
2018-11-04 23:30:46 +00:00
|
|
|
/*
|
|
|
|
* Prealign input max address, so we simplify our matching
|
|
|
|
* logic below and can just reuse it as return pointer.
|
|
|
|
*/
|
|
|
|
max_addr &= ~EFI_PAGE_MASK;
|
|
|
|
|
2016-03-04 00:10:04 +00:00
|
|
|
list_for_each(lhandle, &efi_mem) {
|
|
|
|
struct efi_mem_list *lmem = list_entry(lhandle,
|
|
|
|
struct efi_mem_list, link);
|
|
|
|
struct efi_mem_desc *desc = &lmem->desc;
|
|
|
|
uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT;
|
|
|
|
uint64_t desc_end = desc->physical_start + desc_len;
|
|
|
|
uint64_t curmax = min(max_addr, desc_end);
|
|
|
|
uint64_t ret = curmax - len;
|
|
|
|
|
|
|
|
/* We only take memory from free RAM */
|
|
|
|
if (desc->type != EFI_CONVENTIONAL_MEMORY)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Out of bounds for max_addr */
|
|
|
|
if ((ret + len) > max_addr)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Out of bounds for upper map limit */
|
|
|
|
if ((ret + len) > desc_end)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Out of bounds for lower map limit */
|
|
|
|
if (ret < desc->physical_start)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Return the highest address in this map within bounds */
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-01-08 00:00:00 +00:00
|
|
|
/**
|
|
|
|
* efi_allocate_pages - allocate memory pages
|
2017-12-04 19:52:41 +00:00
|
|
|
*
|
2023-01-08 00:00:00 +00:00
|
|
|
* @type: type of allocation to be performed
|
|
|
|
* @memory_type: usage type of the allocated memory
|
|
|
|
* @pages: number of pages to be allocated
|
|
|
|
* @memory: allocated memory
|
2022-01-19 17:05:50 +00:00
|
|
|
* Return: status code
|
2017-12-04 19:52:41 +00:00
|
|
|
*/
|
2021-08-17 13:05:31 +00:00
|
|
|
efi_status_t efi_allocate_pages(enum efi_allocate_type type,
|
|
|
|
enum efi_memory_type memory_type,
|
2017-11-06 20:17:48 +00:00
|
|
|
efi_uintn_t pages, uint64_t *memory)
|
2016-03-04 00:10:04 +00:00
|
|
|
{
|
2023-07-30 09:31:08 +00:00
|
|
|
u64 len;
|
2019-05-11 06:43:30 +00:00
|
|
|
efi_status_t ret;
|
2016-03-04 00:10:04 +00:00
|
|
|
uint64_t addr;
|
|
|
|
|
2019-04-22 22:30:53 +00:00
|
|
|
/* Check import parameters */
|
|
|
|
if (memory_type >= EFI_PERSISTENT_MEMORY_TYPE &&
|
|
|
|
memory_type <= 0x6FFFFFFF)
|
|
|
|
return EFI_INVALID_PARAMETER;
|
2018-07-02 10:53:53 +00:00
|
|
|
if (!memory)
|
|
|
|
return EFI_INVALID_PARAMETER;
|
2023-07-30 09:31:08 +00:00
|
|
|
len = (u64)pages << EFI_PAGE_SHIFT;
|
|
|
|
/* Catch possible overflow on 64bit systems */
|
|
|
|
if (sizeof(efi_uintn_t) == sizeof(u64) &&
|
|
|
|
(len >> EFI_PAGE_SHIFT) != (u64)pages)
|
|
|
|
return EFI_OUT_OF_RESOURCES;
|
2018-07-02 10:53:53 +00:00
|
|
|
|
2016-03-04 00:10:04 +00:00
|
|
|
switch (type) {
|
2018-01-30 20:08:00 +00:00
|
|
|
case EFI_ALLOCATE_ANY_PAGES:
|
2016-03-04 00:10:04 +00:00
|
|
|
/* Any page */
|
2018-08-30 21:43:45 +00:00
|
|
|
addr = efi_find_free_memory(len, -1ULL);
|
2019-05-11 06:43:30 +00:00
|
|
|
if (!addr)
|
|
|
|
return EFI_OUT_OF_RESOURCES;
|
2016-03-04 00:10:04 +00:00
|
|
|
break;
|
2018-01-30 20:08:00 +00:00
|
|
|
case EFI_ALLOCATE_MAX_ADDRESS:
|
2016-03-04 00:10:04 +00:00
|
|
|
/* Max address */
|
|
|
|
addr = efi_find_free_memory(len, *memory);
|
2019-05-11 06:43:30 +00:00
|
|
|
if (!addr)
|
|
|
|
return EFI_OUT_OF_RESOURCES;
|
2016-03-04 00:10:04 +00:00
|
|
|
break;
|
2018-01-30 20:08:00 +00:00
|
|
|
case EFI_ALLOCATE_ADDRESS:
|
2022-11-06 00:52:13 +00:00
|
|
|
if (*memory & EFI_PAGE_MASK)
|
|
|
|
return EFI_NOT_FOUND;
|
2016-03-04 00:10:04 +00:00
|
|
|
/* Exact address, reserve it. The addr is already in *memory. */
|
2019-05-11 06:43:30 +00:00
|
|
|
ret = efi_check_allocated(*memory, false);
|
|
|
|
if (ret != EFI_SUCCESS)
|
|
|
|
return EFI_NOT_FOUND;
|
2016-03-04 00:10:04 +00:00
|
|
|
addr = *memory;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* UEFI doesn't specify other allocation types */
|
2019-05-11 06:43:30 +00:00
|
|
|
return EFI_INVALID_PARAMETER;
|
2016-03-04 00:10:04 +00:00
|
|
|
}
|
|
|
|
|
2019-05-11 06:43:30 +00:00
|
|
|
/* Reserve that map in our memory maps */
|
2020-05-17 10:29:19 +00:00
|
|
|
ret = efi_add_memory_map_pg(addr, pages, memory_type, true);
|
|
|
|
if (ret != EFI_SUCCESS)
|
2019-05-11 06:43:30 +00:00
|
|
|
/* Map would overlap, bail out */
|
|
|
|
return EFI_OUT_OF_RESOURCES;
|
2016-03-04 00:10:04 +00:00
|
|
|
|
2019-05-11 06:43:30 +00:00
|
|
|
*memory = addr;
|
2016-03-04 00:10:04 +00:00
|
|
|
|
2019-05-11 06:43:30 +00:00
|
|
|
return EFI_SUCCESS;
|
2016-03-04 00:10:04 +00:00
|
|
|
}
|
|
|
|
|
2019-03-26 04:31:41 +00:00
|
|
|
/**
|
|
|
|
* efi_free_pages() - free memory pages
|
2017-12-04 19:52:41 +00:00
|
|
|
*
|
2019-03-26 04:31:41 +00:00
|
|
|
* @memory: start of the memory area to be freed
|
|
|
|
* @pages: number of pages to be freed
|
|
|
|
* Return: status code
|
2017-12-04 19:52:41 +00:00
|
|
|
*/
|
2017-11-06 20:17:48 +00:00
|
|
|
efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
|
2016-03-04 00:10:04 +00:00
|
|
|
{
|
2019-05-10 19:21:30 +00:00
|
|
|
efi_status_t ret;
|
|
|
|
|
2019-05-11 06:21:17 +00:00
|
|
|
ret = efi_check_allocated(memory, true);
|
2019-05-10 19:21:30 +00:00
|
|
|
if (ret != EFI_SUCCESS)
|
|
|
|
return ret;
|
2016-10-01 21:32:27 +00:00
|
|
|
|
2019-03-26 04:31:41 +00:00
|
|
|
/* Sanity check */
|
2019-04-25 16:41:40 +00:00
|
|
|
if (!memory || (memory & EFI_PAGE_MASK) || !pages) {
|
2019-03-26 04:31:41 +00:00
|
|
|
printf("%s: illegal free 0x%llx, 0x%zx\n", __func__,
|
|
|
|
memory, pages);
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
2020-05-17 10:29:19 +00:00
|
|
|
ret = efi_add_memory_map_pg(memory, pages, EFI_CONVENTIONAL_MEMORY,
|
|
|
|
false);
|
2019-07-15 11:00:39 +00:00
|
|
|
if (ret != EFI_SUCCESS)
|
|
|
|
return EFI_NOT_FOUND;
|
2016-10-01 21:32:27 +00:00
|
|
|
|
2019-07-15 11:00:39 +00:00
|
|
|
return ret;
|
2016-03-04 00:10:04 +00:00
|
|
|
}
|
|
|
|
|
2021-10-11 12:10:23 +00:00
|
|
|
/**
|
2023-01-08 00:00:00 +00:00
|
|
|
* efi_alloc_aligned_pages() - allocate aligned memory pages
|
2021-10-11 12:10:23 +00:00
|
|
|
*
|
|
|
|
* @len: len in bytes
|
|
|
|
* @memory_type: usage type of the allocated memory
|
|
|
|
* @align: alignment in bytes
|
|
|
|
* Return: aligned memory or NULL
|
|
|
|
*/
|
|
|
|
void *efi_alloc_aligned_pages(u64 len, int memory_type, size_t align)
|
|
|
|
{
|
|
|
|
u64 req_pages = efi_size_in_pages(len);
|
|
|
|
u64 true_pages = req_pages + efi_size_in_pages(align) - 1;
|
|
|
|
u64 free_pages;
|
|
|
|
u64 aligned_mem;
|
|
|
|
efi_status_t r;
|
|
|
|
u64 mem;
|
|
|
|
|
|
|
|
/* align must be zero or a power of two */
|
|
|
|
if (align & (align - 1))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Check for overflow */
|
|
|
|
if (true_pages < req_pages)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (align < EFI_PAGE_SIZE) {
|
|
|
|
r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type,
|
|
|
|
req_pages, &mem);
|
|
|
|
return (r == EFI_SUCCESS) ? (void *)(uintptr_t)mem : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type,
|
|
|
|
true_pages, &mem);
|
|
|
|
if (r != EFI_SUCCESS)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
aligned_mem = ALIGN(mem, align);
|
|
|
|
/* Free pages before alignment */
|
|
|
|
free_pages = efi_size_in_pages(aligned_mem - mem);
|
|
|
|
if (free_pages)
|
|
|
|
efi_free_pages(mem, free_pages);
|
|
|
|
|
|
|
|
/* Free trailing pages */
|
|
|
|
free_pages = true_pages - (req_pages + free_pages);
|
|
|
|
if (free_pages) {
|
|
|
|
mem = aligned_mem + req_pages * EFI_PAGE_SIZE;
|
|
|
|
efi_free_pages(mem, free_pages);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (void *)(uintptr_t)aligned_mem;
|
|
|
|
}
|
|
|
|
|
2019-03-26 04:31:41 +00:00
|
|
|
/**
|
|
|
|
* efi_allocate_pool - allocate memory from pool
|
2017-12-04 19:52:41 +00:00
|
|
|
*
|
2019-03-26 04:31:41 +00:00
|
|
|
* @pool_type: type of the pool from which memory is to be allocated
|
|
|
|
* @size: number of bytes to be allocated
|
|
|
|
* @buffer: allocated memory
|
|
|
|
* Return: status code
|
2017-12-04 19:52:41 +00:00
|
|
|
*/
|
2021-08-17 13:05:31 +00:00
|
|
|
efi_status_t efi_allocate_pool(enum efi_memory_type pool_type, efi_uintn_t size, void **buffer)
|
2016-10-09 20:17:18 +00:00
|
|
|
{
|
|
|
|
efi_status_t r;
|
2019-03-18 19:01:59 +00:00
|
|
|
u64 addr;
|
2018-06-18 15:23:15 +00:00
|
|
|
struct efi_pool_allocation *alloc;
|
2018-11-18 16:58:49 +00:00
|
|
|
u64 num_pages = efi_size_in_pages(size +
|
|
|
|
sizeof(struct efi_pool_allocation));
|
2016-10-09 20:17:26 +00:00
|
|
|
|
2018-07-02 10:53:53 +00:00
|
|
|
if (!buffer)
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
|
2016-10-09 20:17:26 +00:00
|
|
|
if (size == 0) {
|
|
|
|
*buffer = NULL;
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
2016-10-09 20:17:18 +00:00
|
|
|
|
2018-05-26 08:32:27 +00:00
|
|
|
r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages,
|
2019-03-18 19:01:59 +00:00
|
|
|
&addr);
|
2016-10-09 20:17:26 +00:00
|
|
|
if (r == EFI_SUCCESS) {
|
2019-03-18 19:01:59 +00:00
|
|
|
alloc = (struct efi_pool_allocation *)(uintptr_t)addr;
|
2016-10-09 20:17:26 +00:00
|
|
|
alloc->num_pages = num_pages;
|
2019-03-26 04:31:41 +00:00
|
|
|
alloc->checksum = checksum(alloc);
|
2016-10-09 20:17:26 +00:00
|
|
|
*buffer = alloc->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2023-03-19 08:20:22 +00:00
|
|
|
/**
|
|
|
|
* efi_alloc() - allocate boot services data pool memory
|
|
|
|
*
|
|
|
|
* Allocate memory from pool and zero it out.
|
|
|
|
*
|
|
|
|
* @size: number of bytes to allocate
|
|
|
|
* Return: pointer to allocated memory or NULL
|
|
|
|
*/
|
|
|
|
void *efi_alloc(size_t size)
|
|
|
|
{
|
|
|
|
void *buf;
|
|
|
|
|
|
|
|
if (efi_allocate_pool(EFI_BOOT_SERVICES_DATA, size, &buf) !=
|
|
|
|
EFI_SUCCESS) {
|
|
|
|
log_err("out of memory");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memset(buf, 0, size);
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2019-03-26 04:31:41 +00:00
|
|
|
/**
|
|
|
|
* efi_free_pool() - free memory from pool
|
2017-12-04 19:52:41 +00:00
|
|
|
*
|
2019-03-26 04:31:41 +00:00
|
|
|
* @buffer: start of memory to be freed
|
|
|
|
* Return: status code
|
2017-12-04 19:52:41 +00:00
|
|
|
*/
|
2016-10-09 20:17:26 +00:00
|
|
|
efi_status_t efi_free_pool(void *buffer)
|
|
|
|
{
|
2019-05-10 19:21:30 +00:00
|
|
|
efi_status_t ret;
|
2016-10-09 20:17:26 +00:00
|
|
|
struct efi_pool_allocation *alloc;
|
|
|
|
|
2019-06-12 05:17:04 +00:00
|
|
|
if (!buffer)
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
|
2019-05-11 06:21:17 +00:00
|
|
|
ret = efi_check_allocated((uintptr_t)buffer, true);
|
2019-05-10 19:21:30 +00:00
|
|
|
if (ret != EFI_SUCCESS)
|
|
|
|
return ret;
|
2017-07-14 17:12:39 +00:00
|
|
|
|
2016-10-09 20:17:26 +00:00
|
|
|
alloc = container_of(buffer, struct efi_pool_allocation, data);
|
2019-03-26 04:31:41 +00:00
|
|
|
|
|
|
|
/* Check that this memory was allocated by efi_allocate_pool() */
|
|
|
|
if (((uintptr_t)alloc & EFI_PAGE_MASK) ||
|
|
|
|
alloc->checksum != checksum(alloc)) {
|
|
|
|
printf("%s: illegal free 0x%p\n", __func__, buffer);
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
/* Avoid double free */
|
|
|
|
alloc->checksum = 0;
|
2016-10-09 20:17:26 +00:00
|
|
|
|
2019-05-10 19:21:30 +00:00
|
|
|
ret = efi_free_pages((uintptr_t)alloc, alloc->num_pages);
|
2016-10-09 20:17:18 +00:00
|
|
|
|
2019-05-10 19:21:30 +00:00
|
|
|
return ret;
|
2016-10-09 20:17:18 +00:00
|
|
|
}
|
|
|
|
|
2023-01-08 00:00:00 +00:00
|
|
|
/**
|
|
|
|
* efi_get_memory_map() - get map describing memory usage.
|
2017-12-04 19:52:41 +00:00
|
|
|
*
|
2023-01-08 00:00:00 +00:00
|
|
|
* @memory_map_size: on entry the size, in bytes, of the memory map buffer,
|
2017-12-04 19:52:41 +00:00
|
|
|
* on exit the size of the copied memory map
|
2023-01-08 00:00:00 +00:00
|
|
|
* @memory_map: buffer to which the memory map is written
|
|
|
|
* @map_key: key for the memory map
|
|
|
|
* @descriptor_size: size of an individual memory descriptor
|
|
|
|
* @descriptor_version: version number of the memory descriptor structure
|
2022-01-19 17:05:50 +00:00
|
|
|
* Return: status code
|
2017-12-04 19:52:41 +00:00
|
|
|
*/
|
2017-11-06 20:17:48 +00:00
|
|
|
efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
|
|
|
|
struct efi_mem_desc *memory_map,
|
|
|
|
efi_uintn_t *map_key,
|
|
|
|
efi_uintn_t *descriptor_size,
|
|
|
|
uint32_t *descriptor_version)
|
2016-03-04 00:10:04 +00:00
|
|
|
{
|
2017-11-06 20:17:48 +00:00
|
|
|
efi_uintn_t map_size = 0;
|
2016-04-11 21:51:02 +00:00
|
|
|
int map_entries = 0;
|
2016-03-04 00:10:04 +00:00
|
|
|
struct list_head *lhandle;
|
2018-08-06 20:28:18 +00:00
|
|
|
efi_uintn_t provided_map_size;
|
2016-03-04 00:10:04 +00:00
|
|
|
|
2018-07-02 10:53:54 +00:00
|
|
|
if (!memory_map_size)
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
|
2018-08-06 20:28:18 +00:00
|
|
|
provided_map_size = *memory_map_size;
|
|
|
|
|
2016-03-04 00:10:04 +00:00
|
|
|
list_for_each(lhandle, &efi_mem)
|
2016-04-11 21:51:02 +00:00
|
|
|
map_entries++;
|
|
|
|
|
|
|
|
map_size = map_entries * sizeof(struct efi_mem_desc);
|
2016-03-04 00:10:04 +00:00
|
|
|
|
2017-07-26 18:34:05 +00:00
|
|
|
*memory_map_size = map_size;
|
|
|
|
|
2016-03-04 00:10:04 +00:00
|
|
|
if (descriptor_size)
|
|
|
|
*descriptor_size = sizeof(struct efi_mem_desc);
|
|
|
|
|
2016-09-05 21:59:22 +00:00
|
|
|
if (descriptor_version)
|
|
|
|
*descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION;
|
|
|
|
|
efi_loader: get_memory_map: return parameters whenever possible
Currently, if GetMemoryMap API returns EFI_BUFFER_TOO_SMALL, it doesn't
set valid values to other parameters, descriptor_size and
descriptor_version, except memory_map_size.
Some efi applications, however, may use those value; in particular,
xen uses descriptor_size to calculate a size of buffer to be allocated.
While UEFI specification is ambiguous in this point, it would be better
to address this issue proactively to maximize the compatibility with
existing efi applications.
With this patch, for example, xen.efi (and hence linux kernel) can be
started via bootefi without modification.
Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
2020-03-11 06:18:18 +00:00
|
|
|
if (provided_map_size < map_size)
|
|
|
|
return EFI_BUFFER_TOO_SMALL;
|
|
|
|
|
|
|
|
if (!memory_map)
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
|
2016-03-04 00:10:04 +00:00
|
|
|
/* Copy list into array */
|
2018-07-02 10:53:54 +00:00
|
|
|
/* Return the list in ascending order */
|
|
|
|
memory_map = &memory_map[map_entries - 1];
|
|
|
|
list_for_each(lhandle, &efi_mem) {
|
|
|
|
struct efi_mem_list *lmem;
|
2016-03-04 00:10:04 +00:00
|
|
|
|
2018-07-02 10:53:54 +00:00
|
|
|
lmem = list_entry(lhandle, struct efi_mem_list, link);
|
|
|
|
*memory_map = lmem->desc;
|
|
|
|
memory_map--;
|
2016-03-04 00:10:04 +00:00
|
|
|
}
|
|
|
|
|
2018-07-02 10:53:54 +00:00
|
|
|
if (map_key)
|
2018-07-02 10:53:55 +00:00
|
|
|
*map_key = efi_memory_map_key;
|
2017-07-21 17:05:44 +00:00
|
|
|
|
2016-03-04 00:10:04 +00:00
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2023-01-05 17:26:01 +00:00
|
|
|
/**
|
|
|
|
* efi_get_memory_map_alloc() - allocate map describing memory usage
|
|
|
|
*
|
|
|
|
* The caller is responsible for calling FreePool() if the call succeeds.
|
|
|
|
*
|
2023-01-08 00:00:00 +00:00
|
|
|
* @map_size: size of the memory map
|
|
|
|
* @memory_map: buffer to which the memory map is written
|
2023-01-05 17:26:01 +00:00
|
|
|
* Return: status code
|
|
|
|
*/
|
|
|
|
efi_status_t efi_get_memory_map_alloc(efi_uintn_t *map_size,
|
|
|
|
struct efi_mem_desc **memory_map)
|
|
|
|
{
|
|
|
|
efi_status_t ret;
|
|
|
|
|
|
|
|
*memory_map = NULL;
|
|
|
|
*map_size = 0;
|
|
|
|
ret = efi_get_memory_map(map_size, *memory_map, NULL, NULL, NULL);
|
|
|
|
if (ret == EFI_BUFFER_TOO_SMALL) {
|
|
|
|
*map_size += sizeof(struct efi_mem_desc); /* for the map */
|
|
|
|
ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, *map_size,
|
|
|
|
(void **)memory_map);
|
|
|
|
if (ret != EFI_SUCCESS)
|
|
|
|
return ret;
|
|
|
|
ret = efi_get_memory_map(map_size, *memory_map,
|
|
|
|
NULL, NULL, NULL);
|
|
|
|
if (ret != EFI_SUCCESS) {
|
|
|
|
efi_free_pool(*memory_map);
|
|
|
|
*memory_map = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-09-03 17:43:43 +00:00
|
|
|
/**
|
|
|
|
* efi_add_conventional_memory_map() - add a RAM memory area to the map
|
|
|
|
*
|
|
|
|
* @ram_start: start address of a RAM memory area
|
|
|
|
* @ram_end: end address of a RAM memory area
|
|
|
|
* @ram_top: max address to be used as conventional memory
|
|
|
|
* Return: status code
|
|
|
|
*/
|
|
|
|
efi_status_t efi_add_conventional_memory_map(u64 ram_start, u64 ram_end,
|
|
|
|
u64 ram_top)
|
|
|
|
{
|
|
|
|
u64 pages;
|
|
|
|
|
|
|
|
/* Remove partial pages */
|
|
|
|
ram_end &= ~EFI_PAGE_MASK;
|
|
|
|
ram_start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
|
|
|
|
|
|
|
|
if (ram_end <= ram_start) {
|
|
|
|
/* Invalid mapping */
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
|
|
|
pages = (ram_end - ram_start) >> EFI_PAGE_SHIFT;
|
|
|
|
|
2020-05-17 10:29:19 +00:00
|
|
|
efi_add_memory_map_pg(ram_start, pages,
|
|
|
|
EFI_CONVENTIONAL_MEMORY, false);
|
2019-09-03 17:43:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Boards may indicate to the U-Boot memory core that they
|
|
|
|
* can not support memory above ram_top. Let's honor this
|
|
|
|
* in the efi_loader subsystem too by declaring any memory
|
|
|
|
* above ram_top as "already occupied by firmware".
|
|
|
|
*/
|
|
|
|
if (ram_top < ram_start) {
|
|
|
|
/* ram_top is before this region, reserve all */
|
2020-05-17 10:29:19 +00:00
|
|
|
efi_add_memory_map_pg(ram_start, pages,
|
|
|
|
EFI_BOOT_SERVICES_DATA, true);
|
2022-04-25 21:54:48 +00:00
|
|
|
} else if (ram_top < ram_end) {
|
2019-09-03 17:43:43 +00:00
|
|
|
/* ram_top is inside this region, reserve parts */
|
|
|
|
pages = (ram_end - ram_top) >> EFI_PAGE_SHIFT;
|
|
|
|
|
2020-05-17 10:29:19 +00:00
|
|
|
efi_add_memory_map_pg(ram_top, pages,
|
|
|
|
EFI_BOOT_SERVICES_DATA, true);
|
2019-09-03 17:43:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2023-01-08 00:00:00 +00:00
|
|
|
/**
|
|
|
|
* efi_add_known_memory() - add memory banks to map
|
|
|
|
*
|
|
|
|
* This function may be overridden for specific architectures.
|
|
|
|
*/
|
2017-03-06 17:02:27 +00:00
|
|
|
__weak void efi_add_known_memory(void)
|
2016-03-04 00:10:04 +00:00
|
|
|
{
|
2023-08-14 05:50:53 +00:00
|
|
|
u64 ram_top = gd->ram_top & ~EFI_PAGE_MASK;
|
2016-03-04 00:10:04 +00:00
|
|
|
int i;
|
|
|
|
|
2019-01-05 22:41:36 +00:00
|
|
|
/*
|
|
|
|
* ram_top is just outside mapped memory. So use an offset of one for
|
|
|
|
* mapping the sandbox address.
|
|
|
|
*/
|
|
|
|
ram_top = (uintptr_t)map_sysmem(ram_top - 1, 0) + 1;
|
|
|
|
|
2018-11-30 20:24:56 +00:00
|
|
|
/* Fix for 32bit targets with ram_top at 4G */
|
|
|
|
if (!ram_top)
|
|
|
|
ram_top = 0x100000000ULL;
|
|
|
|
|
2016-03-04 00:10:04 +00:00
|
|
|
/* Add RAM */
|
|
|
|
for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
|
2019-09-03 17:43:43 +00:00
|
|
|
u64 ram_end, ram_start;
|
2016-03-04 00:10:04 +00:00
|
|
|
|
2018-11-18 16:58:46 +00:00
|
|
|
ram_start = (uintptr_t)map_sysmem(gd->bd->bi_dram[i].start, 0);
|
2018-11-12 17:55:24 +00:00
|
|
|
ram_end = ram_start + gd->bd->bi_dram[i].size;
|
|
|
|
|
2019-09-03 17:43:43 +00:00
|
|
|
efi_add_conventional_memory_map(ram_start, ram_end, ram_top);
|
2016-03-04 00:10:04 +00:00
|
|
|
}
|
2017-03-06 17:02:27 +00:00
|
|
|
}
|
|
|
|
|
2023-01-08 00:00:00 +00:00
|
|
|
/**
|
|
|
|
* add_u_boot_and_runtime() - add U-Boot code to memory map
|
|
|
|
*
|
|
|
|
* Add memory regions for U-Boot's memory and for the runtime services code.
|
|
|
|
*/
|
2018-06-18 15:23:12 +00:00
|
|
|
static void add_u_boot_and_runtime(void)
|
2017-03-06 17:02:27 +00:00
|
|
|
{
|
|
|
|
unsigned long runtime_start, runtime_end, runtime_pages;
|
2018-09-17 11:54:33 +00:00
|
|
|
unsigned long runtime_mask = EFI_PAGE_MASK;
|
2017-03-06 17:02:27 +00:00
|
|
|
unsigned long uboot_start, uboot_pages;
|
2020-07-29 10:37:35 +00:00
|
|
|
unsigned long uboot_stack_size = CONFIG_STACK_SIZE;
|
2017-03-06 17:02:27 +00:00
|
|
|
|
2016-03-04 00:10:04 +00:00
|
|
|
/* Add U-Boot */
|
2019-11-08 19:42:53 +00:00
|
|
|
uboot_start = ((uintptr_t)map_sysmem(gd->start_addr_sp, 0) -
|
|
|
|
uboot_stack_size) & ~EFI_PAGE_MASK;
|
|
|
|
uboot_pages = ((uintptr_t)map_sysmem(gd->ram_top - 1, 0) -
|
|
|
|
uboot_start + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
|
2022-11-29 15:00:41 +00:00
|
|
|
efi_add_memory_map_pg(uboot_start, uboot_pages, EFI_BOOT_SERVICES_CODE,
|
2020-05-17 10:29:19 +00:00
|
|
|
false);
|
2016-03-04 00:10:04 +00:00
|
|
|
|
2018-09-17 11:54:33 +00:00
|
|
|
#if defined(__aarch64__)
|
|
|
|
/*
|
|
|
|
* Runtime Services must be 64KiB aligned according to the
|
|
|
|
* "AArch64 Platforms" section in the UEFI spec (2.7+).
|
|
|
|
*/
|
|
|
|
|
|
|
|
runtime_mask = SZ_64K - 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add Runtime Services. We mark surrounding boottime code as runtime as
|
|
|
|
* well to fulfill the runtime alignment constraints but avoid padding.
|
|
|
|
*/
|
|
|
|
runtime_start = (ulong)&__efi_runtime_start & ~runtime_mask;
|
2016-03-04 00:10:04 +00:00
|
|
|
runtime_end = (ulong)&__efi_runtime_stop;
|
2018-09-17 11:54:33 +00:00
|
|
|
runtime_end = (runtime_end + runtime_mask) & ~runtime_mask;
|
2016-03-04 00:10:04 +00:00
|
|
|
runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
|
2020-05-17 10:29:19 +00:00
|
|
|
efi_add_memory_map_pg(runtime_start, runtime_pages,
|
|
|
|
EFI_RUNTIME_SERVICES_CODE, false);
|
2018-06-18 15:23:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int efi_memory_init(void)
|
|
|
|
{
|
|
|
|
efi_add_known_memory();
|
|
|
|
|
2019-11-08 19:42:53 +00:00
|
|
|
add_u_boot_and_runtime();
|
2016-03-04 00:10:04 +00:00
|
|
|
|
2016-05-11 16:25:48 +00:00
|
|
|
#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
|
|
|
|
/* Request a 32bit 64MB bounce buffer region */
|
|
|
|
uint64_t efi_bounce_buffer_addr = 0xffffffff;
|
|
|
|
|
2022-11-29 15:00:41 +00:00
|
|
|
if (efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, EFI_BOOT_SERVICES_DATA,
|
2016-05-11 16:25:48 +00:00
|
|
|
(64 * 1024 * 1024) >> EFI_PAGE_SHIFT,
|
|
|
|
&efi_bounce_buffer_addr) != EFI_SUCCESS)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr;
|
|
|
|
#endif
|
|
|
|
|
2016-03-04 00:10:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|