Tom Rini | 70df9d6 | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 2 | /* |
| 3 | * EFI application memory management |
| 4 | * |
| 5 | * Copyright (c) 2016 Alexander Graf |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 8 | #include <common.h> |
| 9 | #include <efi_loader.h> |
Simon Glass | 6980b6b | 2019-11-14 12:57:45 -0700 | [diff] [blame] | 10 | #include <init.h> |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 11 | #include <malloc.h> |
Alexander Graf | cd40517 | 2018-06-18 17:23:15 +0200 | [diff] [blame] | 12 | #include <mapmem.h> |
Simon Glass | 3e85217 | 2018-03-08 21:53:27 +0100 | [diff] [blame] | 13 | #include <watchdog.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 14 | #include <asm/cache.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 15 | #include <asm/global_data.h> |
Alexander Graf | de2a13b | 2016-03-30 16:38:29 +0200 | [diff] [blame] | 16 | #include <linux/list_sort.h> |
Alexander Graf | 15937a2 | 2018-09-17 13:54:33 +0200 | [diff] [blame] | 17 | #include <linux/sizes.h> |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 18 | |
| 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 21 | /* Magic number identifying memory allocated from pool */ |
| 22 | #define EFI_ALLOC_POOL_MAGIC 0x1fe67ddf6491caa2 |
| 23 | |
Heinrich Schuchardt | 0f233c4 | 2018-07-02 12:53:55 +0200 | [diff] [blame] | 24 | efi_uintn_t efi_memory_map_key; |
| 25 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 26 | struct efi_mem_list { |
| 27 | struct list_head link; |
| 28 | struct efi_mem_desc desc; |
| 29 | }; |
| 30 | |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 31 | #define EFI_CARVE_NO_OVERLAP -1 |
| 32 | #define EFI_CARVE_LOOP_AGAIN -2 |
| 33 | #define EFI_CARVE_OVERLAPS_NONRAM -3 |
| 34 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 35 | /* This list contains all memory map items */ |
| 36 | LIST_HEAD(efi_mem); |
| 37 | |
Alexander Graf | 7c00a3c | 2016-05-11 18:25:48 +0200 | [diff] [blame] | 38 | #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER |
| 39 | void *efi_bounce_buffer; |
| 40 | #endif |
| 41 | |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 42 | /** |
Heinrich Schuchardt | 79f882a | 2019-07-14 12:28:56 +0200 | [diff] [blame] | 43 | * struct efi_pool_allocation - memory block allocated from pool |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 44 | * |
| 45 | * @num_pages: number of pages allocated |
| 46 | * @checksum: checksum |
Heinrich Schuchardt | 79f882a | 2019-07-14 12:28:56 +0200 | [diff] [blame] | 47 | * @data: allocated pool memory |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 48 | * |
Heinrich Schuchardt | 79f882a | 2019-07-14 12:28:56 +0200 | [diff] [blame] | 49 | * U-Boot services each UEFI AllocatePool() request as a separate |
| 50 | * (multiple) page allocation. We have to track the number of pages |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 51 | * to be able to free the correct amount later. |
Heinrich Schuchardt | 79f882a | 2019-07-14 12:28:56 +0200 | [diff] [blame] | 52 | * |
| 53 | * The checksum calculated in function checksum() is used in FreePool() to avoid |
| 54 | * freeing memory not allocated by AllocatePool() and duplicate freeing. |
| 55 | * |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 56 | * EFI requires 8 byte alignment for pool allocations, so we can |
Heinrich Schuchardt | 79f882a | 2019-07-14 12:28:56 +0200 | [diff] [blame] | 57 | * prepend each allocation with these header fields. |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 58 | */ |
| 59 | struct efi_pool_allocation { |
| 60 | u64 num_pages; |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 61 | u64 checksum; |
Rob Clark | e597fd4 | 2017-09-13 18:05:36 -0400 | [diff] [blame] | 62 | char data[] __aligned(ARCH_DMA_MINALIGN); |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 63 | }; |
| 64 | |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 65 | /** |
| 66 | * checksum() - calculate checksum for memory allocated from pool |
| 67 | * |
| 68 | * @alloc: allocation header |
| 69 | * Return: checksum, always non-zero |
| 70 | */ |
| 71 | static u64 checksum(struct efi_pool_allocation *alloc) |
| 72 | { |
| 73 | u64 addr = (uintptr_t)alloc; |
| 74 | u64 ret = (addr >> 32) ^ (addr << 32) ^ alloc->num_pages ^ |
| 75 | EFI_ALLOC_POOL_MAGIC; |
| 76 | if (!ret) |
| 77 | ++ret; |
| 78 | return ret; |
| 79 | } |
| 80 | |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 81 | /** |
| 82 | * efi_mem_cmp() - comparator function for sorting memory map |
| 83 | * |
Alexander Graf | de2a13b | 2016-03-30 16:38:29 +0200 | [diff] [blame] | 84 | * Sorts the memory list from highest address to lowest address |
| 85 | * |
| 86 | * When allocating memory we should always start from the highest |
| 87 | * address chunk, so sort the memory list such that the first list |
| 88 | * iterator gets the highest address and goes lower from there. |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 89 | * |
| 90 | * @priv: unused |
| 91 | * @a: first memory area |
| 92 | * @b: second memory area |
| 93 | * Return: 1 if @a is before @b, -1 if @b is before @a, 0 if equal |
Alexander Graf | de2a13b | 2016-03-30 16:38:29 +0200 | [diff] [blame] | 94 | */ |
| 95 | static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 96 | { |
| 97 | struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link); |
| 98 | struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link); |
| 99 | |
| 100 | if (mema->desc.physical_start == memb->desc.physical_start) |
| 101 | return 0; |
| 102 | else if (mema->desc.physical_start < memb->desc.physical_start) |
| 103 | return 1; |
| 104 | else |
| 105 | return -1; |
| 106 | } |
| 107 | |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 108 | /** |
| 109 | * desc_get_end() - get end address of memory area |
| 110 | * |
| 111 | * @desc: memory descriptor |
| 112 | * Return: end address + 1 |
| 113 | */ |
Alexander Graf | d728814 | 2018-09-16 17:05:29 +0200 | [diff] [blame] | 114 | static uint64_t desc_get_end(struct efi_mem_desc *desc) |
| 115 | { |
| 116 | return desc->physical_start + (desc->num_pages << EFI_PAGE_SHIFT); |
| 117 | } |
| 118 | |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 119 | /** |
| 120 | * efi_mem_sort() - sort memory map |
| 121 | * |
| 122 | * Sort the memory map and then try to merge adjacent memory areas. |
| 123 | */ |
Alexander Graf | de2a13b | 2016-03-30 16:38:29 +0200 | [diff] [blame] | 124 | static void efi_mem_sort(void) |
| 125 | { |
Alexander Graf | d728814 | 2018-09-16 17:05:29 +0200 | [diff] [blame] | 126 | struct list_head *lhandle; |
| 127 | struct efi_mem_list *prevmem = NULL; |
| 128 | bool merge_again = true; |
| 129 | |
Alexander Graf | de2a13b | 2016-03-30 16:38:29 +0200 | [diff] [blame] | 130 | list_sort(NULL, &efi_mem, efi_mem_cmp); |
Alexander Graf | d728814 | 2018-09-16 17:05:29 +0200 | [diff] [blame] | 131 | |
| 132 | /* Now merge entries that can be merged */ |
| 133 | while (merge_again) { |
| 134 | merge_again = false; |
| 135 | list_for_each(lhandle, &efi_mem) { |
| 136 | struct efi_mem_list *lmem; |
| 137 | struct efi_mem_desc *prev = &prevmem->desc; |
| 138 | struct efi_mem_desc *cur; |
| 139 | uint64_t pages; |
| 140 | |
| 141 | lmem = list_entry(lhandle, struct efi_mem_list, link); |
| 142 | if (!prevmem) { |
| 143 | prevmem = lmem; |
| 144 | continue; |
| 145 | } |
| 146 | |
| 147 | cur = &lmem->desc; |
| 148 | |
| 149 | if ((desc_get_end(cur) == prev->physical_start) && |
| 150 | (prev->type == cur->type) && |
| 151 | (prev->attribute == cur->attribute)) { |
| 152 | /* There is an existing map before, reuse it */ |
| 153 | pages = cur->num_pages; |
| 154 | prev->num_pages += pages; |
| 155 | prev->physical_start -= pages << EFI_PAGE_SHIFT; |
| 156 | prev->virtual_start -= pages << EFI_PAGE_SHIFT; |
| 157 | list_del(&lmem->link); |
| 158 | free(lmem); |
| 159 | |
| 160 | merge_again = true; |
| 161 | break; |
| 162 | } |
| 163 | |
| 164 | prevmem = lmem; |
| 165 | } |
| 166 | } |
Alexander Graf | de2a13b | 2016-03-30 16:38:29 +0200 | [diff] [blame] | 167 | } |
| 168 | |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 169 | /** |
| 170 | * efi_mem_carve_out() - unmap memory region |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 171 | * |
Heinrich Schuchardt | f00caa5 | 2018-05-27 16:45:09 +0200 | [diff] [blame] | 172 | * @map: memory map |
| 173 | * @carve_desc: memory region to unmap |
| 174 | * @overlap_only_ram: the carved out region may only overlap RAM |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 175 | * Return: the number of overlapping pages which have been |
Heinrich Schuchardt | f00caa5 | 2018-05-27 16:45:09 +0200 | [diff] [blame] | 176 | * removed from the map, |
| 177 | * EFI_CARVE_NO_OVERLAP, if the regions don't overlap, |
| 178 | * EFI_CARVE_OVERLAPS_NONRAM, if the carve and map overlap, |
| 179 | * and the map contains anything but free ram |
| 180 | * (only when overlap_only_ram is true), |
| 181 | * EFI_CARVE_LOOP_AGAIN, if the mapping list should be |
| 182 | * traversed again, as it has been altered. |
| 183 | * |
| 184 | * Unmaps all memory occupied by the carve_desc region from the list entry |
| 185 | * pointed to by map. |
Stefan Brüns | 1b00dc1 | 2016-10-01 23:32:23 +0200 | [diff] [blame] | 186 | * |
| 187 | * In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility |
Heinrich Schuchardt | f00caa5 | 2018-05-27 16:45:09 +0200 | [diff] [blame] | 188 | * to re-add the already carved out pages to the mapping. |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 189 | */ |
Heinrich Schuchardt | f00caa5 | 2018-05-27 16:45:09 +0200 | [diff] [blame] | 190 | static s64 efi_mem_carve_out(struct efi_mem_list *map, |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 191 | struct efi_mem_desc *carve_desc, |
| 192 | bool overlap_only_ram) |
| 193 | { |
| 194 | struct efi_mem_list *newmap; |
| 195 | struct efi_mem_desc *map_desc = &map->desc; |
| 196 | uint64_t map_start = map_desc->physical_start; |
| 197 | uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT); |
| 198 | uint64_t carve_start = carve_desc->physical_start; |
| 199 | uint64_t carve_end = carve_start + |
| 200 | (carve_desc->num_pages << EFI_PAGE_SHIFT); |
| 201 | |
| 202 | /* check whether we're overlapping */ |
| 203 | if ((carve_end <= map_start) || (carve_start >= map_end)) |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 204 | return EFI_CARVE_NO_OVERLAP; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 205 | |
| 206 | /* We're overlapping with non-RAM, warn the caller if desired */ |
| 207 | if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY)) |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 208 | return EFI_CARVE_OVERLAPS_NONRAM; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 209 | |
| 210 | /* Sanitize carve_start and carve_end to lie within our bounds */ |
| 211 | carve_start = max(carve_start, map_start); |
| 212 | carve_end = min(carve_end, map_end); |
| 213 | |
| 214 | /* Carving at the beginning of our map? Just move it! */ |
| 215 | if (carve_start == map_start) { |
| 216 | if (map_end == carve_end) { |
| 217 | /* Full overlap, just remove map */ |
| 218 | list_del(&map->link); |
Stefan Brüns | 4e9ad8d | 2016-10-01 23:32:29 +0200 | [diff] [blame] | 219 | free(map); |
| 220 | } else { |
| 221 | map->desc.physical_start = carve_end; |
Heinrich Schuchardt | e83aafa | 2019-04-11 20:08:54 +0200 | [diff] [blame] | 222 | map->desc.virtual_start = carve_end; |
Stefan Brüns | 4e9ad8d | 2016-10-01 23:32:29 +0200 | [diff] [blame] | 223 | map->desc.num_pages = (map_end - carve_end) |
| 224 | >> EFI_PAGE_SHIFT; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 225 | } |
| 226 | |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 227 | return (carve_end - carve_start) >> EFI_PAGE_SHIFT; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | /* |
| 231 | * Overlapping maps, just split the list map at carve_start, |
| 232 | * it will get moved or removed in the next iteration. |
| 233 | * |
| 234 | * [ map_desc |__carve_start__| newmap ] |
| 235 | */ |
| 236 | |
| 237 | /* Create a new map from [ carve_start ... map_end ] */ |
| 238 | newmap = calloc(1, sizeof(*newmap)); |
| 239 | newmap->desc = map->desc; |
| 240 | newmap->desc.physical_start = carve_start; |
Heinrich Schuchardt | e83aafa | 2019-04-11 20:08:54 +0200 | [diff] [blame] | 241 | newmap->desc.virtual_start = carve_start; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 242 | newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT; |
Stefan Brüns | 9d65651 | 2016-10-01 23:32:28 +0200 | [diff] [blame] | 243 | /* Insert before current entry (descending address order) */ |
| 244 | list_add_tail(&newmap->link, &map->link); |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 245 | |
| 246 | /* Shrink the map to [ map_start ... carve_start ] */ |
| 247 | map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT; |
| 248 | |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 249 | return EFI_CARVE_LOOP_AGAIN; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 250 | } |
| 251 | |
Bryan O'Donoghue | 89a22b4 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 252 | /** |
Michael Walle | 282d386 | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 253 | * efi_add_memory_map_pg() - add pages to the memory map |
Bryan O'Donoghue | 89a22b4 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 254 | * |
| 255 | * @start: start address, must be a multiple of EFI_PAGE_SIZE |
| 256 | * @pages: number of pages to add |
| 257 | * @memory_type: type of memory added |
Maxim Uvarov | ec631ea | 2020-08-28 22:47:41 +0300 | [diff] [blame] | 258 | * @overlap_only_ram: region may only overlap RAM |
Bryan O'Donoghue | 89a22b4 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 259 | * Return: status code |
| 260 | */ |
Michael Walle | 282d386 | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 261 | static efi_status_t efi_add_memory_map_pg(u64 start, u64 pages, |
| 262 | int memory_type, |
| 263 | bool overlap_only_ram) |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 264 | { |
| 265 | struct list_head *lhandle; |
| 266 | struct efi_mem_list *newlist; |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 267 | bool carve_again; |
| 268 | uint64_t carved_pages = 0; |
Heinrich Schuchardt | 485975b | 2019-06-04 20:55:12 +0200 | [diff] [blame] | 269 | struct efi_event *evt; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 270 | |
Heinrich Schuchardt | 706a2fe | 2019-04-04 21:50:02 +0200 | [diff] [blame] | 271 | EFI_PRINT("%s: 0x%llx 0x%llx %d %s\n", __func__, |
| 272 | start, pages, memory_type, overlap_only_ram ? "yes" : "no"); |
Andreas Färber | 102f8bd | 2016-07-17 06:57:11 +0200 | [diff] [blame] | 273 | |
Heinrich Schuchardt | 0f233c4 | 2018-07-02 12:53:55 +0200 | [diff] [blame] | 274 | if (memory_type >= EFI_MAX_MEMORY_TYPE) |
| 275 | return EFI_INVALID_PARAMETER; |
| 276 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 277 | if (!pages) |
Bryan O'Donoghue | 89a22b4 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 278 | return EFI_SUCCESS; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 279 | |
Heinrich Schuchardt | 0f233c4 | 2018-07-02 12:53:55 +0200 | [diff] [blame] | 280 | ++efi_memory_map_key; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 281 | newlist = calloc(1, sizeof(*newlist)); |
| 282 | newlist->desc.type = memory_type; |
| 283 | newlist->desc.physical_start = start; |
| 284 | newlist->desc.virtual_start = start; |
| 285 | newlist->desc.num_pages = pages; |
| 286 | |
| 287 | switch (memory_type) { |
| 288 | case EFI_RUNTIME_SERVICES_CODE: |
| 289 | case EFI_RUNTIME_SERVICES_DATA: |
Eugeniu Rosca | c86c081 | 2018-07-14 22:53:30 +0200 | [diff] [blame] | 290 | newlist->desc.attribute = EFI_MEMORY_WB | EFI_MEMORY_RUNTIME; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 291 | break; |
| 292 | case EFI_MMAP_IO: |
Eugeniu Rosca | c86c081 | 2018-07-14 22:53:30 +0200 | [diff] [blame] | 293 | newlist->desc.attribute = EFI_MEMORY_RUNTIME; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 294 | break; |
| 295 | default: |
Eugeniu Rosca | c86c081 | 2018-07-14 22:53:30 +0200 | [diff] [blame] | 296 | newlist->desc.attribute = EFI_MEMORY_WB; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 297 | break; |
| 298 | } |
| 299 | |
| 300 | /* Add our new map */ |
| 301 | do { |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 302 | carve_again = false; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 303 | list_for_each(lhandle, &efi_mem) { |
| 304 | struct efi_mem_list *lmem; |
Heinrich Schuchardt | f00caa5 | 2018-05-27 16:45:09 +0200 | [diff] [blame] | 305 | s64 r; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 306 | |
| 307 | lmem = list_entry(lhandle, struct efi_mem_list, link); |
| 308 | r = efi_mem_carve_out(lmem, &newlist->desc, |
| 309 | overlap_only_ram); |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 310 | switch (r) { |
| 311 | case EFI_CARVE_OVERLAPS_NONRAM: |
| 312 | /* |
| 313 | * The user requested to only have RAM overlaps, |
| 314 | * but we hit a non-RAM region. Error out. |
| 315 | */ |
Bryan O'Donoghue | 89a22b4 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 316 | return EFI_NO_MAPPING; |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 317 | case EFI_CARVE_NO_OVERLAP: |
| 318 | /* Just ignore this list entry */ |
| 319 | break; |
| 320 | case EFI_CARVE_LOOP_AGAIN: |
| 321 | /* |
| 322 | * We split an entry, but need to loop through |
| 323 | * the list again to actually carve it. |
| 324 | */ |
| 325 | carve_again = true; |
| 326 | break; |
| 327 | default: |
| 328 | /* We carved a number of pages */ |
| 329 | carved_pages += r; |
| 330 | carve_again = true; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 331 | break; |
| 332 | } |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 333 | |
| 334 | if (carve_again) { |
| 335 | /* The list changed, we need to start over */ |
| 336 | break; |
| 337 | } |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 338 | } |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 339 | } while (carve_again); |
| 340 | |
| 341 | if (overlap_only_ram && (carved_pages != pages)) { |
| 342 | /* |
| 343 | * The payload wanted to have RAM overlaps, but we overlapped |
| 344 | * with an unallocated region. Error out. |
| 345 | */ |
Bryan O'Donoghue | 89a22b4 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 346 | return EFI_NO_MAPPING; |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 347 | } |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 348 | |
| 349 | /* Add our new map */ |
| 350 | list_add_tail(&newlist->link, &efi_mem); |
| 351 | |
Alexander Graf | de2a13b | 2016-03-30 16:38:29 +0200 | [diff] [blame] | 352 | /* And make sure memory is listed in descending order */ |
| 353 | efi_mem_sort(); |
| 354 | |
Heinrich Schuchardt | 485975b | 2019-06-04 20:55:12 +0200 | [diff] [blame] | 355 | /* Notify that the memory map was changed */ |
| 356 | list_for_each_entry(evt, &efi_events, link) { |
| 357 | if (evt->group && |
| 358 | !guidcmp(evt->group, |
| 359 | &efi_guid_event_group_memory_map_change)) { |
Heinrich Schuchardt | 7b4b8d86 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 360 | efi_signal_event(evt); |
Heinrich Schuchardt | 485975b | 2019-06-04 20:55:12 +0200 | [diff] [blame] | 361 | break; |
| 362 | } |
| 363 | } |
| 364 | |
Bryan O'Donoghue | 89a22b4 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 365 | return EFI_SUCCESS; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 366 | } |
| 367 | |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 368 | /** |
Michael Walle | 282d386 | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 369 | * efi_add_memory_map() - add memory area to the memory map |
| 370 | * |
| 371 | * @start: start address of the memory area |
| 372 | * @size: length in bytes of the memory area |
| 373 | * @memory_type: type of memory added |
| 374 | * |
| 375 | * Return: status code |
| 376 | * |
| 377 | * This function automatically aligns the start and size of the memory area |
| 378 | * to EFI_PAGE_SIZE. |
| 379 | */ |
| 380 | efi_status_t efi_add_memory_map(u64 start, u64 size, int memory_type) |
| 381 | { |
| 382 | u64 pages; |
| 383 | |
| 384 | pages = efi_size_in_pages(size + (start & EFI_PAGE_MASK)); |
| 385 | start &= ~EFI_PAGE_MASK; |
| 386 | |
| 387 | return efi_add_memory_map_pg(start, pages, memory_type, false); |
| 388 | } |
| 389 | |
| 390 | /** |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 391 | * efi_check_allocated() - validate address to be freed |
| 392 | * |
| 393 | * Check that the address is within allocated memory: |
| 394 | * |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 395 | * * The address must be in a range of the memory map. |
| 396 | * * The address may not point to EFI_CONVENTIONAL_MEMORY. |
| 397 | * |
| 398 | * Page alignment is not checked as this is not a requirement of |
| 399 | * efi_free_pool(). |
| 400 | * |
Heinrich Schuchardt | 906f685 | 2019-05-11 08:21:17 +0200 | [diff] [blame] | 401 | * @addr: address of page to be freed |
| 402 | * @must_be_allocated: return success if the page is allocated |
| 403 | * Return: status code |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 404 | */ |
Heinrich Schuchardt | 906f685 | 2019-05-11 08:21:17 +0200 | [diff] [blame] | 405 | static efi_status_t efi_check_allocated(u64 addr, bool must_be_allocated) |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 406 | { |
| 407 | struct efi_mem_list *item; |
| 408 | |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 409 | list_for_each_entry(item, &efi_mem, link) { |
| 410 | u64 start = item->desc.physical_start; |
| 411 | u64 end = start + (item->desc.num_pages << EFI_PAGE_SHIFT); |
| 412 | |
| 413 | if (addr >= start && addr < end) { |
Heinrich Schuchardt | 906f685 | 2019-05-11 08:21:17 +0200 | [diff] [blame] | 414 | if (must_be_allocated ^ |
| 415 | (item->desc.type == EFI_CONVENTIONAL_MEMORY)) |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 416 | return EFI_SUCCESS; |
| 417 | else |
| 418 | return EFI_NOT_FOUND; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | return EFI_NOT_FOUND; |
| 423 | } |
| 424 | |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 425 | /** |
| 426 | * efi_find_free_memory() - find free memory pages |
| 427 | * |
| 428 | * @len: size of memory area needed |
| 429 | * @max_addr: highest address to allocate |
| 430 | * Return: pointer to free memory area or 0 |
| 431 | */ |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 432 | static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr) |
| 433 | { |
| 434 | struct list_head *lhandle; |
| 435 | |
Alexander Graf | c3b2a25 | 2018-11-05 00:30:46 +0100 | [diff] [blame] | 436 | /* |
| 437 | * Prealign input max address, so we simplify our matching |
| 438 | * logic below and can just reuse it as return pointer. |
| 439 | */ |
| 440 | max_addr &= ~EFI_PAGE_MASK; |
| 441 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 442 | list_for_each(lhandle, &efi_mem) { |
| 443 | struct efi_mem_list *lmem = list_entry(lhandle, |
| 444 | struct efi_mem_list, link); |
| 445 | struct efi_mem_desc *desc = &lmem->desc; |
| 446 | uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT; |
| 447 | uint64_t desc_end = desc->physical_start + desc_len; |
| 448 | uint64_t curmax = min(max_addr, desc_end); |
| 449 | uint64_t ret = curmax - len; |
| 450 | |
| 451 | /* We only take memory from free RAM */ |
| 452 | if (desc->type != EFI_CONVENTIONAL_MEMORY) |
| 453 | continue; |
| 454 | |
| 455 | /* Out of bounds for max_addr */ |
| 456 | if ((ret + len) > max_addr) |
| 457 | continue; |
| 458 | |
| 459 | /* Out of bounds for upper map limit */ |
| 460 | if ((ret + len) > desc_end) |
| 461 | continue; |
| 462 | |
| 463 | /* Out of bounds for lower map limit */ |
| 464 | if (ret < desc->physical_start) |
| 465 | continue; |
| 466 | |
| 467 | /* Return the highest address in this map within bounds */ |
| 468 | return ret; |
| 469 | } |
| 470 | |
| 471 | return 0; |
| 472 | } |
| 473 | |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 474 | /** |
| 475 | * efi_allocate_pages - allocate memory pages |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 476 | * |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 477 | * @type: type of allocation to be performed |
| 478 | * @memory_type: usage type of the allocated memory |
| 479 | * @pages: number of pages to be allocated |
| 480 | * @memory: allocated memory |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 481 | * Return: status code |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 482 | */ |
Heinrich Schuchardt | 9822894 | 2021-08-17 15:05:31 +0200 | [diff] [blame] | 483 | efi_status_t efi_allocate_pages(enum efi_allocate_type type, |
| 484 | enum efi_memory_type memory_type, |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 485 | efi_uintn_t pages, uint64_t *memory) |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 486 | { |
| 487 | u64 len = pages << EFI_PAGE_SHIFT; |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 488 | efi_status_t ret; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 489 | uint64_t addr; |
| 490 | |
Heinrich Schuchardt | f727233 | 2019-04-23 00:30:53 +0200 | [diff] [blame] | 491 | /* Check import parameters */ |
| 492 | if (memory_type >= EFI_PERSISTENT_MEMORY_TYPE && |
| 493 | memory_type <= 0x6FFFFFFF) |
| 494 | return EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | a137c96 | 2018-07-02 12:53:53 +0200 | [diff] [blame] | 495 | if (!memory) |
| 496 | return EFI_INVALID_PARAMETER; |
| 497 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 498 | switch (type) { |
Heinrich Schuchardt | 3b7b3b6 | 2018-01-30 21:08:00 +0100 | [diff] [blame] | 499 | case EFI_ALLOCATE_ANY_PAGES: |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 500 | /* Any page */ |
Stephen Warren | 66208b9 | 2018-08-30 15:43:45 -0600 | [diff] [blame] | 501 | addr = efi_find_free_memory(len, -1ULL); |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 502 | if (!addr) |
| 503 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 504 | break; |
Heinrich Schuchardt | 3b7b3b6 | 2018-01-30 21:08:00 +0100 | [diff] [blame] | 505 | case EFI_ALLOCATE_MAX_ADDRESS: |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 506 | /* Max address */ |
| 507 | addr = efi_find_free_memory(len, *memory); |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 508 | if (!addr) |
| 509 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 510 | break; |
Heinrich Schuchardt | 3b7b3b6 | 2018-01-30 21:08:00 +0100 | [diff] [blame] | 511 | case EFI_ALLOCATE_ADDRESS: |
Heinrich Schuchardt | 1c180b8 | 2022-11-06 01:52:13 +0100 | [diff] [blame] | 512 | if (*memory & EFI_PAGE_MASK) |
| 513 | return EFI_NOT_FOUND; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 514 | /* Exact address, reserve it. The addr is already in *memory. */ |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 515 | ret = efi_check_allocated(*memory, false); |
| 516 | if (ret != EFI_SUCCESS) |
| 517 | return EFI_NOT_FOUND; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 518 | addr = *memory; |
| 519 | break; |
| 520 | default: |
| 521 | /* UEFI doesn't specify other allocation types */ |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 522 | return EFI_INVALID_PARAMETER; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 523 | } |
| 524 | |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 525 | /* Reserve that map in our memory maps */ |
Michael Walle | 282d386 | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 526 | ret = efi_add_memory_map_pg(addr, pages, memory_type, true); |
| 527 | if (ret != EFI_SUCCESS) |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 528 | /* Map would overlap, bail out */ |
| 529 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 530 | |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 531 | *memory = addr; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 532 | |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 533 | return EFI_SUCCESS; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 534 | } |
| 535 | |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 536 | /** |
| 537 | * efi_alloc() - allocate memory pages |
| 538 | * |
| 539 | * @len: size of the memory to be allocated |
| 540 | * @memory_type: usage type of the allocated memory |
| 541 | * Return: pointer to the allocated memory area or NULL |
| 542 | */ |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 543 | void *efi_alloc(uint64_t len, int memory_type) |
| 544 | { |
| 545 | uint64_t ret = 0; |
Heinrich Schuchardt | dcdca29 | 2018-11-18 17:58:49 +0100 | [diff] [blame] | 546 | uint64_t pages = efi_size_in_pages(len); |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 547 | efi_status_t r; |
| 548 | |
Heinrich Schuchardt | 0f7186b | 2018-05-26 10:32:27 +0200 | [diff] [blame] | 549 | r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type, pages, |
| 550 | &ret); |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 551 | if (r == EFI_SUCCESS) |
| 552 | return (void*)(uintptr_t)ret; |
| 553 | |
| 554 | return NULL; |
| 555 | } |
| 556 | |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 557 | /** |
| 558 | * efi_free_pages() - free memory pages |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 559 | * |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 560 | * @memory: start of the memory area to be freed |
| 561 | * @pages: number of pages to be freed |
| 562 | * Return: status code |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 563 | */ |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 564 | efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages) |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 565 | { |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 566 | efi_status_t ret; |
| 567 | |
Heinrich Schuchardt | 906f685 | 2019-05-11 08:21:17 +0200 | [diff] [blame] | 568 | ret = efi_check_allocated(memory, true); |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 569 | if (ret != EFI_SUCCESS) |
| 570 | return ret; |
Stefan Brüns | a4f4302 | 2016-10-01 23:32:27 +0200 | [diff] [blame] | 571 | |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 572 | /* Sanity check */ |
Heinrich Schuchardt | 7bf4bc9 | 2019-04-25 18:41:40 +0200 | [diff] [blame] | 573 | if (!memory || (memory & EFI_PAGE_MASK) || !pages) { |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 574 | printf("%s: illegal free 0x%llx, 0x%zx\n", __func__, |
| 575 | memory, pages); |
| 576 | return EFI_INVALID_PARAMETER; |
| 577 | } |
| 578 | |
Michael Walle | 282d386 | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 579 | ret = efi_add_memory_map_pg(memory, pages, EFI_CONVENTIONAL_MEMORY, |
| 580 | false); |
Bryan O'Donoghue | 89a22b4 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 581 | if (ret != EFI_SUCCESS) |
| 582 | return EFI_NOT_FOUND; |
Stefan Brüns | a4f4302 | 2016-10-01 23:32:27 +0200 | [diff] [blame] | 583 | |
Bryan O'Donoghue | 89a22b4 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 584 | return ret; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 585 | } |
| 586 | |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 587 | /** |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 588 | * efi_alloc_aligned_pages() - allocate aligned memory pages |
Ilias Apalodimas | 6e01c23 | 2021-10-11 15:10:23 +0300 | [diff] [blame] | 589 | * |
| 590 | * @len: len in bytes |
| 591 | * @memory_type: usage type of the allocated memory |
| 592 | * @align: alignment in bytes |
| 593 | * Return: aligned memory or NULL |
| 594 | */ |
| 595 | void *efi_alloc_aligned_pages(u64 len, int memory_type, size_t align) |
| 596 | { |
| 597 | u64 req_pages = efi_size_in_pages(len); |
| 598 | u64 true_pages = req_pages + efi_size_in_pages(align) - 1; |
| 599 | u64 free_pages; |
| 600 | u64 aligned_mem; |
| 601 | efi_status_t r; |
| 602 | u64 mem; |
| 603 | |
| 604 | /* align must be zero or a power of two */ |
| 605 | if (align & (align - 1)) |
| 606 | return NULL; |
| 607 | |
| 608 | /* Check for overflow */ |
| 609 | if (true_pages < req_pages) |
| 610 | return NULL; |
| 611 | |
| 612 | if (align < EFI_PAGE_SIZE) { |
| 613 | r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type, |
| 614 | req_pages, &mem); |
| 615 | return (r == EFI_SUCCESS) ? (void *)(uintptr_t)mem : NULL; |
| 616 | } |
| 617 | |
| 618 | r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type, |
| 619 | true_pages, &mem); |
| 620 | if (r != EFI_SUCCESS) |
| 621 | return NULL; |
| 622 | |
| 623 | aligned_mem = ALIGN(mem, align); |
| 624 | /* Free pages before alignment */ |
| 625 | free_pages = efi_size_in_pages(aligned_mem - mem); |
| 626 | if (free_pages) |
| 627 | efi_free_pages(mem, free_pages); |
| 628 | |
| 629 | /* Free trailing pages */ |
| 630 | free_pages = true_pages - (req_pages + free_pages); |
| 631 | if (free_pages) { |
| 632 | mem = aligned_mem + req_pages * EFI_PAGE_SIZE; |
| 633 | efi_free_pages(mem, free_pages); |
| 634 | } |
| 635 | |
| 636 | return (void *)(uintptr_t)aligned_mem; |
| 637 | } |
| 638 | |
| 639 | /** |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 640 | * efi_allocate_pool - allocate memory from pool |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 641 | * |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 642 | * @pool_type: type of the pool from which memory is to be allocated |
| 643 | * @size: number of bytes to be allocated |
| 644 | * @buffer: allocated memory |
| 645 | * Return: status code |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 646 | */ |
Heinrich Schuchardt | 9822894 | 2021-08-17 15:05:31 +0200 | [diff] [blame] | 647 | efi_status_t efi_allocate_pool(enum efi_memory_type pool_type, efi_uintn_t size, void **buffer) |
Stefan Brüns | 5a09aef | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 648 | { |
| 649 | efi_status_t r; |
Heinrich Schuchardt | 657e979 | 2019-03-18 20:01:59 +0100 | [diff] [blame] | 650 | u64 addr; |
Alexander Graf | cd40517 | 2018-06-18 17:23:15 +0200 | [diff] [blame] | 651 | struct efi_pool_allocation *alloc; |
Heinrich Schuchardt | dcdca29 | 2018-11-18 17:58:49 +0100 | [diff] [blame] | 652 | u64 num_pages = efi_size_in_pages(size + |
| 653 | sizeof(struct efi_pool_allocation)); |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 654 | |
Heinrich Schuchardt | a137c96 | 2018-07-02 12:53:53 +0200 | [diff] [blame] | 655 | if (!buffer) |
| 656 | return EFI_INVALID_PARAMETER; |
| 657 | |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 658 | if (size == 0) { |
| 659 | *buffer = NULL; |
| 660 | return EFI_SUCCESS; |
| 661 | } |
Stefan Brüns | 5a09aef | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 662 | |
Heinrich Schuchardt | 0f7186b | 2018-05-26 10:32:27 +0200 | [diff] [blame] | 663 | r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages, |
Heinrich Schuchardt | 657e979 | 2019-03-18 20:01:59 +0100 | [diff] [blame] | 664 | &addr); |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 665 | if (r == EFI_SUCCESS) { |
Heinrich Schuchardt | 657e979 | 2019-03-18 20:01:59 +0100 | [diff] [blame] | 666 | alloc = (struct efi_pool_allocation *)(uintptr_t)addr; |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 667 | alloc->num_pages = num_pages; |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 668 | alloc->checksum = checksum(alloc); |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 669 | *buffer = alloc->data; |
| 670 | } |
| 671 | |
| 672 | return r; |
| 673 | } |
| 674 | |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 675 | /** |
| 676 | * efi_free_pool() - free memory from pool |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 677 | * |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 678 | * @buffer: start of memory to be freed |
| 679 | * Return: status code |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 680 | */ |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 681 | efi_status_t efi_free_pool(void *buffer) |
| 682 | { |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 683 | efi_status_t ret; |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 684 | struct efi_pool_allocation *alloc; |
| 685 | |
Heinrich Schuchardt | 56b587f7 | 2019-06-12 07:17:04 +0200 | [diff] [blame] | 686 | if (!buffer) |
| 687 | return EFI_INVALID_PARAMETER; |
| 688 | |
Heinrich Schuchardt | 906f685 | 2019-05-11 08:21:17 +0200 | [diff] [blame] | 689 | ret = efi_check_allocated((uintptr_t)buffer, true); |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 690 | if (ret != EFI_SUCCESS) |
| 691 | return ret; |
xypron.glpk@gmx.de | 34ddae7 | 2017-07-14 19:12:39 +0200 | [diff] [blame] | 692 | |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 693 | alloc = container_of(buffer, struct efi_pool_allocation, data); |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 694 | |
| 695 | /* Check that this memory was allocated by efi_allocate_pool() */ |
| 696 | if (((uintptr_t)alloc & EFI_PAGE_MASK) || |
| 697 | alloc->checksum != checksum(alloc)) { |
| 698 | printf("%s: illegal free 0x%p\n", __func__, buffer); |
| 699 | return EFI_INVALID_PARAMETER; |
| 700 | } |
| 701 | /* Avoid double free */ |
| 702 | alloc->checksum = 0; |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 703 | |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 704 | ret = efi_free_pages((uintptr_t)alloc, alloc->num_pages); |
Stefan Brüns | 5a09aef | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 705 | |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 706 | return ret; |
Stefan Brüns | 5a09aef | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 707 | } |
| 708 | |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 709 | /** |
| 710 | * efi_get_memory_map() - get map describing memory usage. |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 711 | * |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 712 | * @memory_map_size: on entry the size, in bytes, of the memory map buffer, |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 713 | * on exit the size of the copied memory map |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 714 | * @memory_map: buffer to which the memory map is written |
| 715 | * @map_key: key for the memory map |
| 716 | * @descriptor_size: size of an individual memory descriptor |
| 717 | * @descriptor_version: version number of the memory descriptor structure |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 718 | * Return: status code |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 719 | */ |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 720 | efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size, |
| 721 | struct efi_mem_desc *memory_map, |
| 722 | efi_uintn_t *map_key, |
| 723 | efi_uintn_t *descriptor_size, |
| 724 | uint32_t *descriptor_version) |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 725 | { |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 726 | efi_uintn_t map_size = 0; |
Alexander Graf | 9b59102 | 2016-04-11 23:51:02 +0200 | [diff] [blame] | 727 | int map_entries = 0; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 728 | struct list_head *lhandle; |
Heinrich Schuchardt | 9b16646 | 2018-08-06 22:28:18 +0200 | [diff] [blame] | 729 | efi_uintn_t provided_map_size; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 730 | |
Heinrich Schuchardt | e1a1a09 | 2018-07-02 12:53:54 +0200 | [diff] [blame] | 731 | if (!memory_map_size) |
| 732 | return EFI_INVALID_PARAMETER; |
| 733 | |
Heinrich Schuchardt | 9b16646 | 2018-08-06 22:28:18 +0200 | [diff] [blame] | 734 | provided_map_size = *memory_map_size; |
| 735 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 736 | list_for_each(lhandle, &efi_mem) |
Alexander Graf | 9b59102 | 2016-04-11 23:51:02 +0200 | [diff] [blame] | 737 | map_entries++; |
| 738 | |
| 739 | map_size = map_entries * sizeof(struct efi_mem_desc); |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 740 | |
Rob Clark | a84c974 | 2017-07-26 14:34:05 -0400 | [diff] [blame] | 741 | *memory_map_size = map_size; |
| 742 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 743 | if (descriptor_size) |
| 744 | *descriptor_size = sizeof(struct efi_mem_desc); |
| 745 | |
Mian Yousaf Kaukab | 338cf56 | 2016-09-05 23:59:22 +0200 | [diff] [blame] | 746 | if (descriptor_version) |
| 747 | *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION; |
| 748 | |
AKASHI Takahiro | 3ee138d | 2020-03-11 15:18:18 +0900 | [diff] [blame] | 749 | if (provided_map_size < map_size) |
| 750 | return EFI_BUFFER_TOO_SMALL; |
| 751 | |
| 752 | if (!memory_map) |
| 753 | return EFI_INVALID_PARAMETER; |
| 754 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 755 | /* Copy list into array */ |
Heinrich Schuchardt | e1a1a09 | 2018-07-02 12:53:54 +0200 | [diff] [blame] | 756 | /* Return the list in ascending order */ |
| 757 | memory_map = &memory_map[map_entries - 1]; |
| 758 | list_for_each(lhandle, &efi_mem) { |
| 759 | struct efi_mem_list *lmem; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 760 | |
Heinrich Schuchardt | e1a1a09 | 2018-07-02 12:53:54 +0200 | [diff] [blame] | 761 | lmem = list_entry(lhandle, struct efi_mem_list, link); |
| 762 | *memory_map = lmem->desc; |
| 763 | memory_map--; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 764 | } |
| 765 | |
Heinrich Schuchardt | e1a1a09 | 2018-07-02 12:53:54 +0200 | [diff] [blame] | 766 | if (map_key) |
Heinrich Schuchardt | 0f233c4 | 2018-07-02 12:53:55 +0200 | [diff] [blame] | 767 | *map_key = efi_memory_map_key; |
xypron.glpk@gmx.de | e436e63 | 2017-07-21 19:05:44 +0200 | [diff] [blame] | 768 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 769 | return EFI_SUCCESS; |
| 770 | } |
| 771 | |
Park, Aiden | e009e7a | 2019-09-03 17:43:43 +0000 | [diff] [blame] | 772 | /** |
Heinrich Schuchardt | 13ce2d9 | 2023-01-05 18:26:01 +0100 | [diff] [blame] | 773 | * efi_get_memory_map_alloc() - allocate map describing memory usage |
| 774 | * |
| 775 | * The caller is responsible for calling FreePool() if the call succeeds. |
| 776 | * |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 777 | * @map_size: size of the memory map |
| 778 | * @memory_map: buffer to which the memory map is written |
Heinrich Schuchardt | 13ce2d9 | 2023-01-05 18:26:01 +0100 | [diff] [blame] | 779 | * Return: status code |
| 780 | */ |
| 781 | efi_status_t efi_get_memory_map_alloc(efi_uintn_t *map_size, |
| 782 | struct efi_mem_desc **memory_map) |
| 783 | { |
| 784 | efi_status_t ret; |
| 785 | |
| 786 | *memory_map = NULL; |
| 787 | *map_size = 0; |
| 788 | ret = efi_get_memory_map(map_size, *memory_map, NULL, NULL, NULL); |
| 789 | if (ret == EFI_BUFFER_TOO_SMALL) { |
| 790 | *map_size += sizeof(struct efi_mem_desc); /* for the map */ |
| 791 | ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, *map_size, |
| 792 | (void **)memory_map); |
| 793 | if (ret != EFI_SUCCESS) |
| 794 | return ret; |
| 795 | ret = efi_get_memory_map(map_size, *memory_map, |
| 796 | NULL, NULL, NULL); |
| 797 | if (ret != EFI_SUCCESS) { |
| 798 | efi_free_pool(*memory_map); |
| 799 | *memory_map = NULL; |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | return ret; |
| 804 | } |
| 805 | |
| 806 | /** |
Park, Aiden | e009e7a | 2019-09-03 17:43:43 +0000 | [diff] [blame] | 807 | * efi_add_conventional_memory_map() - add a RAM memory area to the map |
| 808 | * |
| 809 | * @ram_start: start address of a RAM memory area |
| 810 | * @ram_end: end address of a RAM memory area |
| 811 | * @ram_top: max address to be used as conventional memory |
| 812 | * Return: status code |
| 813 | */ |
| 814 | efi_status_t efi_add_conventional_memory_map(u64 ram_start, u64 ram_end, |
| 815 | u64 ram_top) |
| 816 | { |
| 817 | u64 pages; |
| 818 | |
| 819 | /* Remove partial pages */ |
| 820 | ram_end &= ~EFI_PAGE_MASK; |
| 821 | ram_start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK; |
| 822 | |
| 823 | if (ram_end <= ram_start) { |
| 824 | /* Invalid mapping */ |
| 825 | return EFI_INVALID_PARAMETER; |
| 826 | } |
| 827 | |
| 828 | pages = (ram_end - ram_start) >> EFI_PAGE_SHIFT; |
| 829 | |
Michael Walle | 282d386 | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 830 | efi_add_memory_map_pg(ram_start, pages, |
| 831 | EFI_CONVENTIONAL_MEMORY, false); |
Park, Aiden | e009e7a | 2019-09-03 17:43:43 +0000 | [diff] [blame] | 832 | |
| 833 | /* |
| 834 | * Boards may indicate to the U-Boot memory core that they |
| 835 | * can not support memory above ram_top. Let's honor this |
| 836 | * in the efi_loader subsystem too by declaring any memory |
| 837 | * above ram_top as "already occupied by firmware". |
| 838 | */ |
| 839 | if (ram_top < ram_start) { |
| 840 | /* ram_top is before this region, reserve all */ |
Michael Walle | 282d386 | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 841 | efi_add_memory_map_pg(ram_start, pages, |
| 842 | EFI_BOOT_SERVICES_DATA, true); |
Heinrich Schuchardt | 3546eb2 | 2022-04-25 23:54:48 +0200 | [diff] [blame] | 843 | } else if (ram_top < ram_end) { |
Park, Aiden | e009e7a | 2019-09-03 17:43:43 +0000 | [diff] [blame] | 844 | /* ram_top is inside this region, reserve parts */ |
| 845 | pages = (ram_end - ram_top) >> EFI_PAGE_SHIFT; |
| 846 | |
Michael Walle | 282d386 | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 847 | efi_add_memory_map_pg(ram_top, pages, |
| 848 | EFI_BOOT_SERVICES_DATA, true); |
Park, Aiden | e009e7a | 2019-09-03 17:43:43 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | return EFI_SUCCESS; |
| 852 | } |
| 853 | |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 854 | /** |
| 855 | * efi_add_known_memory() - add memory banks to map |
| 856 | * |
| 857 | * This function may be overridden for specific architectures. |
| 858 | */ |
York Sun | a8e9f0a | 2017-03-06 09:02:27 -0800 | [diff] [blame] | 859 | __weak void efi_add_known_memory(void) |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 860 | { |
Alexander Graf | c214edf | 2018-11-30 21:24:56 +0100 | [diff] [blame] | 861 | u64 ram_top = board_get_usable_ram_top(0) & ~EFI_PAGE_MASK; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 862 | int i; |
| 863 | |
Heinrich Schuchardt | eb8edcf | 2019-01-05 23:41:36 +0100 | [diff] [blame] | 864 | /* |
| 865 | * ram_top is just outside mapped memory. So use an offset of one for |
| 866 | * mapping the sandbox address. |
| 867 | */ |
| 868 | ram_top = (uintptr_t)map_sysmem(ram_top - 1, 0) + 1; |
| 869 | |
Alexander Graf | c214edf | 2018-11-30 21:24:56 +0100 | [diff] [blame] | 870 | /* Fix for 32bit targets with ram_top at 4G */ |
| 871 | if (!ram_top) |
| 872 | ram_top = 0x100000000ULL; |
| 873 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 874 | /* Add RAM */ |
| 875 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
Park, Aiden | e009e7a | 2019-09-03 17:43:43 +0000 | [diff] [blame] | 876 | u64 ram_end, ram_start; |
Heinrich Schuchardt | 084091a | 2018-11-12 18:55:24 +0100 | [diff] [blame] | 877 | |
Heinrich Schuchardt | 6738fcd | 2018-11-18 17:58:46 +0100 | [diff] [blame] | 878 | ram_start = (uintptr_t)map_sysmem(gd->bd->bi_dram[i].start, 0); |
Heinrich Schuchardt | 084091a | 2018-11-12 18:55:24 +0100 | [diff] [blame] | 879 | ram_end = ram_start + gd->bd->bi_dram[i].size; |
| 880 | |
Park, Aiden | e009e7a | 2019-09-03 17:43:43 +0000 | [diff] [blame] | 881 | efi_add_conventional_memory_map(ram_start, ram_end, ram_top); |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 882 | } |
York Sun | a8e9f0a | 2017-03-06 09:02:27 -0800 | [diff] [blame] | 883 | } |
| 884 | |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 885 | /** |
| 886 | * add_u_boot_and_runtime() - add U-Boot code to memory map |
| 887 | * |
| 888 | * Add memory regions for U-Boot's memory and for the runtime services code. |
| 889 | */ |
Simon Glass | 7863943 | 2018-06-18 17:23:12 +0200 | [diff] [blame] | 890 | static void add_u_boot_and_runtime(void) |
York Sun | a8e9f0a | 2017-03-06 09:02:27 -0800 | [diff] [blame] | 891 | { |
| 892 | unsigned long runtime_start, runtime_end, runtime_pages; |
Alexander Graf | 15937a2 | 2018-09-17 13:54:33 +0200 | [diff] [blame] | 893 | unsigned long runtime_mask = EFI_PAGE_MASK; |
York Sun | a8e9f0a | 2017-03-06 09:02:27 -0800 | [diff] [blame] | 894 | unsigned long uboot_start, uboot_pages; |
Heinrich Schuchardt | c6dcbe1 | 2020-07-29 12:37:35 +0200 | [diff] [blame] | 895 | unsigned long uboot_stack_size = CONFIG_STACK_SIZE; |
York Sun | a8e9f0a | 2017-03-06 09:02:27 -0800 | [diff] [blame] | 896 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 897 | /* Add U-Boot */ |
Heinrich Schuchardt | a69976e | 2019-11-08 20:42:53 +0100 | [diff] [blame] | 898 | uboot_start = ((uintptr_t)map_sysmem(gd->start_addr_sp, 0) - |
| 899 | uboot_stack_size) & ~EFI_PAGE_MASK; |
| 900 | uboot_pages = ((uintptr_t)map_sysmem(gd->ram_top - 1, 0) - |
| 901 | uboot_start + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT; |
Heinrich Schuchardt | 296b55c | 2022-11-29 16:00:41 +0100 | [diff] [blame] | 902 | efi_add_memory_map_pg(uboot_start, uboot_pages, EFI_BOOT_SERVICES_CODE, |
Michael Walle | 282d386 | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 903 | false); |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 904 | |
Alexander Graf | 15937a2 | 2018-09-17 13:54:33 +0200 | [diff] [blame] | 905 | #if defined(__aarch64__) |
| 906 | /* |
| 907 | * Runtime Services must be 64KiB aligned according to the |
| 908 | * "AArch64 Platforms" section in the UEFI spec (2.7+). |
| 909 | */ |
| 910 | |
| 911 | runtime_mask = SZ_64K - 1; |
| 912 | #endif |
| 913 | |
| 914 | /* |
| 915 | * Add Runtime Services. We mark surrounding boottime code as runtime as |
| 916 | * well to fulfill the runtime alignment constraints but avoid padding. |
| 917 | */ |
| 918 | runtime_start = (ulong)&__efi_runtime_start & ~runtime_mask; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 919 | runtime_end = (ulong)&__efi_runtime_stop; |
Alexander Graf | 15937a2 | 2018-09-17 13:54:33 +0200 | [diff] [blame] | 920 | runtime_end = (runtime_end + runtime_mask) & ~runtime_mask; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 921 | runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT; |
Michael Walle | 282d386 | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 922 | efi_add_memory_map_pg(runtime_start, runtime_pages, |
| 923 | EFI_RUNTIME_SERVICES_CODE, false); |
Simon Glass | 7863943 | 2018-06-18 17:23:12 +0200 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | int efi_memory_init(void) |
| 927 | { |
| 928 | efi_add_known_memory(); |
| 929 | |
Heinrich Schuchardt | a69976e | 2019-11-08 20:42:53 +0100 | [diff] [blame] | 930 | add_u_boot_and_runtime(); |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 931 | |
Alexander Graf | 7c00a3c | 2016-05-11 18:25:48 +0200 | [diff] [blame] | 932 | #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER |
| 933 | /* Request a 32bit 64MB bounce buffer region */ |
| 934 | uint64_t efi_bounce_buffer_addr = 0xffffffff; |
| 935 | |
Heinrich Schuchardt | 296b55c | 2022-11-29 16:00:41 +0100 | [diff] [blame] | 936 | if (efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, EFI_BOOT_SERVICES_DATA, |
Alexander Graf | 7c00a3c | 2016-05-11 18:25:48 +0200 | [diff] [blame] | 937 | (64 * 1024 * 1024) >> EFI_PAGE_SHIFT, |
| 938 | &efi_bounce_buffer_addr) != EFI_SUCCESS) |
| 939 | return -1; |
| 940 | |
| 941 | efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr; |
| 942 | #endif |
| 943 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 944 | return 0; |
| 945 | } |