Simon Glass | bbd7bda | 2022-01-12 19:26:25 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ BSD-3-Clause |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2018 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
| 5 | */ |
| 6 | |
Simon Glass | c6fcb94 | 2022-01-12 19:26:22 -0700 | [diff] [blame] | 7 | #define LOG_DEBUG |
Simon Glass | 963bf32 | 2022-01-12 19:26:20 -0700 | [diff] [blame] | 8 | #define LOG_CATEGORY LOGC_BLOBLIST |
| 9 | |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 10 | #include <common.h> |
| 11 | #include <bloblist.h> |
Simon Glass | 1ab1692 | 2022-07-31 12:28:48 -0600 | [diff] [blame^] | 12 | #include <display_options.h> |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | 5d2199d | 2021-11-03 21:09:20 -0600 | [diff] [blame] | 14 | #include <malloc.h> |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 15 | #include <mapmem.h> |
| 16 | #include <spl.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 17 | #include <asm/global_data.h> |
Simon Glass | 48b6c6b | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 18 | #include <u-boot/crc.h> |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 19 | |
Simon Glass | d7985e6 | 2020-09-19 18:49:28 -0600 | [diff] [blame] | 20 | /* |
| 21 | * A bloblist is a single contiguous chunk of memory with a header |
| 22 | * (struct bloblist_hdr) and a number of blobs in it. |
| 23 | * |
| 24 | * Each blob starts on a BLOBLIST_ALIGN boundary relative to the start of the |
| 25 | * bloblist and consists of a struct bloblist_rec, some padding to the required |
| 26 | * alignment for the blog and then the actual data. The padding ensures that the |
| 27 | * start address of the data in each blob is aligned as required. Note that |
| 28 | * each blob's *data* is aligned to BLOBLIST_ALIGN regardless of the alignment |
| 29 | * of the bloblist itself or the blob header. |
| 30 | * |
| 31 | * So far, only BLOBLIST_ALIGN alignment is supported. |
| 32 | */ |
| 33 | |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 34 | DECLARE_GLOBAL_DATA_PTR; |
| 35 | |
Simon Glass | fd20df4 | 2022-01-12 19:26:19 -0700 | [diff] [blame] | 36 | static struct tag_name { |
| 37 | enum bloblist_tag_t tag; |
| 38 | const char *name; |
| 39 | } tag_name[] = { |
| 40 | { BLOBLISTT_NONE, "(none)" }, |
| 41 | |
| 42 | /* BLOBLISTT_AREA_FIRMWARE_TOP */ |
| 43 | |
| 44 | /* BLOBLISTT_AREA_FIRMWARE */ |
| 45 | { BLOBLISTT_ACPI_GNVS, "ACPI GNVS" }, |
| 46 | { BLOBLISTT_INTEL_VBT, "Intel Video-BIOS table" }, |
| 47 | { BLOBLISTT_TPM2_TCG_LOG, "TPM v2 log space" }, |
| 48 | { BLOBLISTT_TCPA_LOG, "TPM log space" }, |
| 49 | { BLOBLISTT_ACPI_TABLES, "ACPI tables for x86" }, |
| 50 | { BLOBLISTT_SMBIOS_TABLES, "SMBIOS tables for x86" }, |
| 51 | { BLOBLISTT_VBOOT_CTX, "Chrome OS vboot context" }, |
| 52 | |
| 53 | /* BLOBLISTT_PROJECT_AREA */ |
| 54 | { BLOBLISTT_U_BOOT_SPL_HANDOFF, "SPL hand-off" }, |
| 55 | |
| 56 | /* BLOBLISTT_VENDOR_AREA */ |
Simon Glass | b936a97 | 2020-09-19 18:49:26 -0600 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | const char *bloblist_tag_name(enum bloblist_tag_t tag) |
| 60 | { |
Simon Glass | fd20df4 | 2022-01-12 19:26:19 -0700 | [diff] [blame] | 61 | int i; |
| 62 | |
| 63 | for (i = 0; i < ARRAY_SIZE(tag_name); i++) { |
| 64 | if (tag_name[i].tag == tag) |
| 65 | return tag_name[i].name; |
| 66 | } |
Simon Glass | b936a97 | 2020-09-19 18:49:26 -0600 | [diff] [blame] | 67 | |
Simon Glass | fd20df4 | 2022-01-12 19:26:19 -0700 | [diff] [blame] | 68 | return "invalid"; |
Simon Glass | b936a97 | 2020-09-19 18:49:26 -0600 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | static struct bloblist_rec *bloblist_first_blob(struct bloblist_hdr *hdr) |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 72 | { |
| 73 | if (hdr->alloced <= hdr->hdr_size) |
| 74 | return NULL; |
| 75 | return (struct bloblist_rec *)((void *)hdr + hdr->hdr_size); |
| 76 | } |
| 77 | |
Simon Glass | f20a113 | 2021-07-05 16:32:53 -0600 | [diff] [blame] | 78 | static ulong bloblist_blob_end_ofs(struct bloblist_hdr *hdr, |
| 79 | struct bloblist_rec *rec) |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 80 | { |
| 81 | ulong offset; |
| 82 | |
| 83 | offset = (void *)rec - (void *)hdr; |
| 84 | offset += rec->hdr_size + ALIGN(rec->size, BLOBLIST_ALIGN); |
Simon Glass | f20a113 | 2021-07-05 16:32:53 -0600 | [diff] [blame] | 85 | |
| 86 | return offset; |
| 87 | } |
| 88 | |
| 89 | static struct bloblist_rec *bloblist_next_blob(struct bloblist_hdr *hdr, |
| 90 | struct bloblist_rec *rec) |
| 91 | { |
| 92 | ulong offset = bloblist_blob_end_ofs(hdr, rec); |
| 93 | |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 94 | if (offset >= hdr->alloced) |
| 95 | return NULL; |
| 96 | return (struct bloblist_rec *)((void *)hdr + offset); |
| 97 | } |
| 98 | |
| 99 | #define foreach_rec(_rec, _hdr) \ |
| 100 | for (_rec = bloblist_first_blob(_hdr); \ |
| 101 | _rec; \ |
| 102 | _rec = bloblist_next_blob(_hdr, _rec)) |
| 103 | |
| 104 | static struct bloblist_rec *bloblist_findrec(uint tag) |
| 105 | { |
| 106 | struct bloblist_hdr *hdr = gd->bloblist; |
| 107 | struct bloblist_rec *rec; |
| 108 | |
| 109 | if (!hdr) |
| 110 | return NULL; |
| 111 | |
| 112 | foreach_rec(rec, hdr) { |
| 113 | if (rec->tag == tag) |
| 114 | return rec; |
| 115 | } |
| 116 | |
| 117 | return NULL; |
| 118 | } |
| 119 | |
Simon Glass | 0637397 | 2020-09-19 18:49:29 -0600 | [diff] [blame] | 120 | static int bloblist_addrec(uint tag, int size, int align, |
| 121 | struct bloblist_rec **recp) |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 122 | { |
| 123 | struct bloblist_hdr *hdr = gd->bloblist; |
| 124 | struct bloblist_rec *rec; |
Simon Glass | d7985e6 | 2020-09-19 18:49:28 -0600 | [diff] [blame] | 125 | int data_start, new_alloced; |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 126 | |
Simon Glass | 0637397 | 2020-09-19 18:49:29 -0600 | [diff] [blame] | 127 | if (!align) |
| 128 | align = BLOBLIST_ALIGN; |
| 129 | |
Simon Glass | d7985e6 | 2020-09-19 18:49:28 -0600 | [diff] [blame] | 130 | /* Figure out where the new data will start */ |
Simon Glass | 0637397 | 2020-09-19 18:49:29 -0600 | [diff] [blame] | 131 | data_start = map_to_sysmem(hdr) + hdr->alloced + sizeof(*rec); |
| 132 | |
| 133 | /* Align the address and then calculate the offset from ->alloced */ |
| 134 | data_start = ALIGN(data_start, align) - map_to_sysmem(hdr); |
Simon Glass | d7985e6 | 2020-09-19 18:49:28 -0600 | [diff] [blame] | 135 | |
| 136 | /* Calculate the new allocated total */ |
Simon Glass | 0637397 | 2020-09-19 18:49:29 -0600 | [diff] [blame] | 137 | new_alloced = data_start + ALIGN(size, align); |
| 138 | |
Simon Glass | 3e26de5 | 2021-07-05 16:32:55 -0600 | [diff] [blame] | 139 | if (new_alloced > hdr->size) { |
Simon Glass | 963bf32 | 2022-01-12 19:26:20 -0700 | [diff] [blame] | 140 | log_err("Failed to allocate %x bytes size=%x, need size=%x\n", |
| 141 | size, hdr->size, new_alloced); |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 142 | return log_msg_ret("bloblist add", -ENOSPC); |
| 143 | } |
| 144 | rec = (void *)hdr + hdr->alloced; |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 145 | |
| 146 | rec->tag = tag; |
Simon Glass | d7985e6 | 2020-09-19 18:49:28 -0600 | [diff] [blame] | 147 | rec->hdr_size = data_start - hdr->alloced; |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 148 | rec->size = size; |
| 149 | rec->spare = 0; |
Simon Glass | 3e85ea0 | 2020-01-27 08:49:52 -0700 | [diff] [blame] | 150 | |
| 151 | /* Zero the record data */ |
Simon Glass | d7985e6 | 2020-09-19 18:49:28 -0600 | [diff] [blame] | 152 | memset((void *)rec + rec->hdr_size, '\0', rec->size); |
| 153 | |
| 154 | hdr->alloced = new_alloced; |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 155 | *recp = rec; |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
Simon Glass | 0637397 | 2020-09-19 18:49:29 -0600 | [diff] [blame] | 160 | static int bloblist_ensurerec(uint tag, struct bloblist_rec **recp, int size, |
| 161 | int align) |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 162 | { |
| 163 | struct bloblist_rec *rec; |
| 164 | |
| 165 | rec = bloblist_findrec(tag); |
| 166 | if (rec) { |
Simon Glass | 1e0304e | 2020-01-27 08:49:50 -0700 | [diff] [blame] | 167 | if (size && size != rec->size) { |
| 168 | *recp = rec; |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 169 | return -ESPIPE; |
Simon Glass | 1e0304e | 2020-01-27 08:49:50 -0700 | [diff] [blame] | 170 | } |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 171 | } else { |
| 172 | int ret; |
| 173 | |
Simon Glass | 0637397 | 2020-09-19 18:49:29 -0600 | [diff] [blame] | 174 | ret = bloblist_addrec(tag, size, align, &rec); |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 175 | if (ret) |
| 176 | return ret; |
| 177 | } |
| 178 | *recp = rec; |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | void *bloblist_find(uint tag, int size) |
| 184 | { |
| 185 | struct bloblist_rec *rec; |
| 186 | |
| 187 | rec = bloblist_findrec(tag); |
| 188 | if (!rec) |
| 189 | return NULL; |
| 190 | if (size && size != rec->size) |
| 191 | return NULL; |
| 192 | |
| 193 | return (void *)rec + rec->hdr_size; |
| 194 | } |
| 195 | |
Simon Glass | 0637397 | 2020-09-19 18:49:29 -0600 | [diff] [blame] | 196 | void *bloblist_add(uint tag, int size, int align) |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 197 | { |
| 198 | struct bloblist_rec *rec; |
| 199 | |
Simon Glass | 0637397 | 2020-09-19 18:49:29 -0600 | [diff] [blame] | 200 | if (bloblist_addrec(tag, size, align, &rec)) |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 201 | return NULL; |
| 202 | |
Simon Glass | d7985e6 | 2020-09-19 18:49:28 -0600 | [diff] [blame] | 203 | return (void *)rec + rec->hdr_size; |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Simon Glass | 0637397 | 2020-09-19 18:49:29 -0600 | [diff] [blame] | 206 | int bloblist_ensure_size(uint tag, int size, int align, void **blobp) |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 207 | { |
| 208 | struct bloblist_rec *rec; |
| 209 | int ret; |
| 210 | |
Simon Glass | 0637397 | 2020-09-19 18:49:29 -0600 | [diff] [blame] | 211 | ret = bloblist_ensurerec(tag, &rec, size, align); |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 212 | if (ret) |
| 213 | return ret; |
| 214 | *blobp = (void *)rec + rec->hdr_size; |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | void *bloblist_ensure(uint tag, int size) |
| 220 | { |
| 221 | struct bloblist_rec *rec; |
| 222 | |
Simon Glass | 0637397 | 2020-09-19 18:49:29 -0600 | [diff] [blame] | 223 | if (bloblist_ensurerec(tag, &rec, size, 0)) |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 224 | return NULL; |
| 225 | |
| 226 | return (void *)rec + rec->hdr_size; |
| 227 | } |
| 228 | |
Simon Glass | 1e0304e | 2020-01-27 08:49:50 -0700 | [diff] [blame] | 229 | int bloblist_ensure_size_ret(uint tag, int *sizep, void **blobp) |
| 230 | { |
| 231 | struct bloblist_rec *rec; |
| 232 | int ret; |
| 233 | |
Simon Glass | 0637397 | 2020-09-19 18:49:29 -0600 | [diff] [blame] | 234 | ret = bloblist_ensurerec(tag, &rec, *sizep, 0); |
Simon Glass | 1e0304e | 2020-01-27 08:49:50 -0700 | [diff] [blame] | 235 | if (ret == -ESPIPE) |
| 236 | *sizep = rec->size; |
| 237 | else if (ret) |
| 238 | return ret; |
| 239 | *blobp = (void *)rec + rec->hdr_size; |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
Simon Glass | f20a113 | 2021-07-05 16:32:53 -0600 | [diff] [blame] | 244 | static int bloblist_resize_rec(struct bloblist_hdr *hdr, |
| 245 | struct bloblist_rec *rec, |
| 246 | int new_size) |
| 247 | { |
| 248 | int expand_by; /* Number of bytes to expand by (-ve to contract) */ |
| 249 | int new_alloced; /* New value for @hdr->alloced */ |
| 250 | ulong next_ofs; /* Offset of the record after @rec */ |
| 251 | |
| 252 | expand_by = ALIGN(new_size - rec->size, BLOBLIST_ALIGN); |
| 253 | new_alloced = ALIGN(hdr->alloced + expand_by, BLOBLIST_ALIGN); |
| 254 | if (new_size < 0) { |
Simon Glass | 963bf32 | 2022-01-12 19:26:20 -0700 | [diff] [blame] | 255 | log_debug("Attempt to shrink blob size below 0 (%x)\n", |
| 256 | new_size); |
Simon Glass | f20a113 | 2021-07-05 16:32:53 -0600 | [diff] [blame] | 257 | return log_msg_ret("size", -EINVAL); |
| 258 | } |
| 259 | if (new_alloced > hdr->size) { |
Simon Glass | 963bf32 | 2022-01-12 19:26:20 -0700 | [diff] [blame] | 260 | log_err("Failed to allocate %x bytes size=%x, need size=%x\n", |
| 261 | new_size, hdr->size, new_alloced); |
Simon Glass | f20a113 | 2021-07-05 16:32:53 -0600 | [diff] [blame] | 262 | return log_msg_ret("alloc", -ENOSPC); |
| 263 | } |
| 264 | |
| 265 | /* Move the following blobs up or down, if this is not the last */ |
| 266 | next_ofs = bloblist_blob_end_ofs(hdr, rec); |
| 267 | if (next_ofs != hdr->alloced) { |
| 268 | memmove((void *)hdr + next_ofs + expand_by, |
| 269 | (void *)hdr + next_ofs, new_alloced - next_ofs); |
| 270 | } |
| 271 | hdr->alloced = new_alloced; |
| 272 | |
| 273 | /* Zero the new part of the blob */ |
| 274 | if (expand_by > 0) { |
| 275 | memset((void *)rec + rec->hdr_size + rec->size, '\0', |
| 276 | new_size - rec->size); |
| 277 | } |
| 278 | |
| 279 | /* Update the size of this blob */ |
| 280 | rec->size = new_size; |
| 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | int bloblist_resize(uint tag, int new_size) |
| 286 | { |
| 287 | struct bloblist_hdr *hdr = gd->bloblist; |
| 288 | struct bloblist_rec *rec; |
| 289 | int ret; |
| 290 | |
| 291 | rec = bloblist_findrec(tag); |
| 292 | if (!rec) |
| 293 | return log_msg_ret("find", -ENOENT); |
| 294 | ret = bloblist_resize_rec(hdr, rec, new_size); |
| 295 | if (ret) |
| 296 | return log_msg_ret("resize", ret); |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 301 | static u32 bloblist_calc_chksum(struct bloblist_hdr *hdr) |
| 302 | { |
| 303 | struct bloblist_rec *rec; |
| 304 | u32 chksum; |
| 305 | |
| 306 | chksum = crc32(0, (unsigned char *)hdr, |
| 307 | offsetof(struct bloblist_hdr, chksum)); |
| 308 | foreach_rec(rec, hdr) { |
| 309 | chksum = crc32(chksum, (void *)rec, rec->hdr_size); |
| 310 | chksum = crc32(chksum, (void *)rec + rec->hdr_size, rec->size); |
| 311 | } |
| 312 | |
| 313 | return chksum; |
| 314 | } |
| 315 | |
| 316 | int bloblist_new(ulong addr, uint size, uint flags) |
| 317 | { |
| 318 | struct bloblist_hdr *hdr; |
| 319 | |
| 320 | if (size < sizeof(*hdr)) |
| 321 | return log_ret(-ENOSPC); |
| 322 | if (addr & (BLOBLIST_ALIGN - 1)) |
| 323 | return log_ret(-EFAULT); |
| 324 | hdr = map_sysmem(addr, size); |
| 325 | memset(hdr, '\0', sizeof(*hdr)); |
| 326 | hdr->version = BLOBLIST_VERSION; |
| 327 | hdr->hdr_size = sizeof(*hdr); |
| 328 | hdr->flags = flags; |
| 329 | hdr->magic = BLOBLIST_MAGIC; |
| 330 | hdr->size = size; |
| 331 | hdr->alloced = hdr->hdr_size; |
| 332 | hdr->chksum = 0; |
| 333 | gd->bloblist = hdr; |
| 334 | |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | int bloblist_check(ulong addr, uint size) |
| 339 | { |
| 340 | struct bloblist_hdr *hdr; |
| 341 | u32 chksum; |
| 342 | |
| 343 | hdr = map_sysmem(addr, sizeof(*hdr)); |
| 344 | if (hdr->magic != BLOBLIST_MAGIC) |
| 345 | return log_msg_ret("Bad magic", -ENOENT); |
| 346 | if (hdr->version != BLOBLIST_VERSION) |
| 347 | return log_msg_ret("Bad version", -EPROTONOSUPPORT); |
| 348 | if (size && hdr->size != size) |
| 349 | return log_msg_ret("Bad size", -EFBIG); |
| 350 | chksum = bloblist_calc_chksum(hdr); |
| 351 | if (hdr->chksum != chksum) { |
Simon Glass | 963bf32 | 2022-01-12 19:26:20 -0700 | [diff] [blame] | 352 | log_err("Checksum %x != %x\n", hdr->chksum, chksum); |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 353 | return log_msg_ret("Bad checksum", -EIO); |
| 354 | } |
| 355 | gd->bloblist = hdr; |
| 356 | |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | int bloblist_finish(void) |
| 361 | { |
| 362 | struct bloblist_hdr *hdr = gd->bloblist; |
| 363 | |
| 364 | hdr->chksum = bloblist_calc_chksum(hdr); |
Simon Glass | c6fcb94 | 2022-01-12 19:26:22 -0700 | [diff] [blame] | 365 | log_debug("Finished bloblist size %lx at %lx\n", (ulong)hdr->size, |
| 366 | (ulong)map_to_sysmem(hdr)); |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 367 | |
| 368 | return 0; |
| 369 | } |
| 370 | |
Simon Glass | 01fd56a | 2022-01-12 19:26:23 -0700 | [diff] [blame] | 371 | ulong bloblist_get_base(void) |
| 372 | { |
| 373 | return map_to_sysmem(gd->bloblist); |
| 374 | } |
| 375 | |
| 376 | ulong bloblist_get_size(void) |
| 377 | { |
| 378 | struct bloblist_hdr *hdr = gd->bloblist; |
| 379 | |
| 380 | return hdr->size; |
| 381 | } |
| 382 | |
Simon Glass | b936a97 | 2020-09-19 18:49:26 -0600 | [diff] [blame] | 383 | void bloblist_get_stats(ulong *basep, ulong *sizep, ulong *allocedp) |
| 384 | { |
| 385 | struct bloblist_hdr *hdr = gd->bloblist; |
| 386 | |
| 387 | *basep = map_to_sysmem(gd->bloblist); |
| 388 | *sizep = hdr->size; |
| 389 | *allocedp = hdr->alloced; |
| 390 | } |
| 391 | |
| 392 | static void show_value(const char *prompt, ulong value) |
| 393 | { |
| 394 | printf("%s:%*s %-5lx ", prompt, 8 - (int)strlen(prompt), "", value); |
| 395 | print_size(value, "\n"); |
| 396 | } |
| 397 | |
| 398 | void bloblist_show_stats(void) |
| 399 | { |
| 400 | ulong base, size, alloced; |
| 401 | |
| 402 | bloblist_get_stats(&base, &size, &alloced); |
| 403 | printf("base: %lx\n", base); |
| 404 | show_value("size", size); |
| 405 | show_value("alloced", alloced); |
| 406 | show_value("free", size - alloced); |
| 407 | } |
| 408 | |
| 409 | void bloblist_show_list(void) |
| 410 | { |
| 411 | struct bloblist_hdr *hdr = gd->bloblist; |
| 412 | struct bloblist_rec *rec; |
| 413 | |
Simon Glass | fd20df4 | 2022-01-12 19:26:19 -0700 | [diff] [blame] | 414 | printf("%-8s %8s Tag Name\n", "Address", "Size"); |
Simon Glass | b936a97 | 2020-09-19 18:49:26 -0600 | [diff] [blame] | 415 | for (rec = bloblist_first_blob(hdr); rec; |
| 416 | rec = bloblist_next_blob(hdr, rec)) { |
Simon Glass | fd20df4 | 2022-01-12 19:26:19 -0700 | [diff] [blame] | 417 | printf("%08lx %8x %4x %s\n", |
Simon Glass | b936a97 | 2020-09-19 18:49:26 -0600 | [diff] [blame] | 418 | (ulong)map_to_sysmem((void *)rec + rec->hdr_size), |
| 419 | rec->size, rec->tag, bloblist_tag_name(rec->tag)); |
| 420 | } |
| 421 | } |
| 422 | |
Simon Glass | ab7e746 | 2021-01-13 20:29:43 -0700 | [diff] [blame] | 423 | void bloblist_reloc(void *to, uint to_size, void *from, uint from_size) |
| 424 | { |
| 425 | struct bloblist_hdr *hdr; |
| 426 | |
| 427 | memcpy(to, from, from_size); |
| 428 | hdr = to; |
| 429 | hdr->size = to_size; |
| 430 | } |
| 431 | |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 432 | int bloblist_init(void) |
| 433 | { |
Simon Glass | ef70ef7 | 2022-01-22 05:07:27 -0700 | [diff] [blame] | 434 | bool fixed = IS_ENABLED(CONFIG_BLOBLIST_FIXED); |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 435 | int ret = -ENOENT; |
Simon Glass | c6fcb94 | 2022-01-12 19:26:22 -0700 | [diff] [blame] | 436 | ulong addr, size; |
| 437 | bool expected; |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 438 | |
| 439 | /** |
Simon Glass | ef70ef7 | 2022-01-22 05:07:27 -0700 | [diff] [blame] | 440 | * We don't expect to find an existing bloblist in the first phase of |
| 441 | * U-Boot that runs. Also we have no way to receive the address of an |
| 442 | * allocated bloblist from a previous stage, so it must be at a fixed |
| 443 | * address. |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 444 | */ |
Simon Glass | ef70ef7 | 2022-01-22 05:07:27 -0700 | [diff] [blame] | 445 | expected = fixed && !u_boot_first_phase(); |
Simon Glass | ab7e746 | 2021-01-13 20:29:43 -0700 | [diff] [blame] | 446 | if (spl_prev_phase() == PHASE_TPL && !IS_ENABLED(CONFIG_TPL_BLOBLIST)) |
| 447 | expected = false; |
Simon Glass | ef70ef7 | 2022-01-22 05:07:27 -0700 | [diff] [blame] | 448 | if (fixed) |
| 449 | addr = IF_ENABLED_INT(CONFIG_BLOBLIST_FIXED, |
| 450 | CONFIG_BLOBLIST_ADDR); |
Simon Glass | c6fcb94 | 2022-01-12 19:26:22 -0700 | [diff] [blame] | 451 | size = CONFIG_BLOBLIST_SIZE; |
| 452 | if (expected) { |
| 453 | ret = bloblist_check(addr, size); |
| 454 | if (ret) { |
| 455 | log_warning("Expected bloblist at %lx not found (err=%d)\n", |
| 456 | addr, ret); |
| 457 | } else { |
| 458 | /* Get the real size, if it is not what we expected */ |
| 459 | size = gd->bloblist->size; |
| 460 | } |
| 461 | } |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 462 | if (ret) { |
Simon Glass | c6fcb94 | 2022-01-12 19:26:22 -0700 | [diff] [blame] | 463 | if (CONFIG_IS_ENABLED(BLOBLIST_ALLOC)) { |
| 464 | void *ptr = memalign(BLOBLIST_ALIGN, size); |
Simon Glass | 5d2199d | 2021-11-03 21:09:20 -0600 | [diff] [blame] | 465 | |
| 466 | if (!ptr) |
| 467 | return log_msg_ret("alloc", -ENOMEM); |
| 468 | addr = map_to_sysmem(ptr); |
Simon Glass | ef70ef7 | 2022-01-22 05:07:27 -0700 | [diff] [blame] | 469 | } else if (!fixed) { |
| 470 | return log_msg_ret("!fixed", ret); |
Simon Glass | 5d2199d | 2021-11-03 21:09:20 -0600 | [diff] [blame] | 471 | } |
Simon Glass | c6fcb94 | 2022-01-12 19:26:22 -0700 | [diff] [blame] | 472 | log_debug("Creating new bloblist size %lx at %lx\n", size, |
| 473 | addr); |
| 474 | ret = bloblist_new(addr, size, 0); |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 475 | } else { |
Simon Glass | c6fcb94 | 2022-01-12 19:26:22 -0700 | [diff] [blame] | 476 | log_debug("Found existing bloblist size %lx at %lx\n", size, |
| 477 | addr); |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | return ret; |
| 481 | } |