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