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