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