1 // SPDX-License-Identifier: GPL-2.0+ BSD-3-Clause
3 * Copyright 2018 Google, Inc
7 #define LOG_CATEGORY LOGC_BLOBLIST
10 #include <display_options.h>
15 #include <tables_csum.h>
16 #include <asm/global_data.h>
17 #include <u-boot/crc.h>
20 * A bloblist is a single contiguous chunk of memory with a header
21 * (struct bloblist_hdr) and a number of blobs in it.
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.
31 DECLARE_GLOBAL_DATA_PTR;
33 static struct tag_name {
34 enum bloblist_tag_t tag;
37 { BLOBLISTT_VOID, "(void)" },
39 /* BLOBLISTT_AREA_FIRMWARE_TOP */
40 { BLOBLISTT_CONTROL_FDT, "Control FDT" },
41 { BLOBLISTT_HOB_BLOCK, "HOB block" },
42 { BLOBLISTT_HOB_LIST, "HOB list" },
43 { BLOBLISTT_ACPI_TABLES, "ACPI tables for x86" },
44 { BLOBLISTT_TPM_EVLOG, "TPM event log defined by TCG EFI" },
45 { BLOBLISTT_TPM_CRB_BASE, "TPM Command Response Buffer address" },
47 /* BLOBLISTT_AREA_FIRMWARE */
48 { BLOBLISTT_TPM2_TCG_LOG, "TPM v2 log space" },
49 { BLOBLISTT_TCPA_LOG, "TPM log space" },
50 { BLOBLISTT_ACPI_GNVS, "ACPI GNVS" },
52 /* BLOBLISTT_AREA_TF */
53 { BLOBLISTT_OPTEE_PAGABLE_PART, "OP-TEE pagable part" },
55 /* BLOBLISTT_AREA_OTHER */
56 { BLOBLISTT_INTEL_VBT, "Intel Video-BIOS table" },
57 { BLOBLISTT_SMBIOS_TABLES, "SMBIOS tables for x86" },
58 { BLOBLISTT_VBOOT_CTX, "Chrome OS vboot context" },
60 /* BLOBLISTT_PROJECT_AREA */
61 { BLOBLISTT_U_BOOT_SPL_HANDOFF, "SPL hand-off" },
62 { BLOBLISTT_VBE, "VBE" },
63 { BLOBLISTT_U_BOOT_VIDEO, "SPL video handoff" },
65 /* BLOBLISTT_VENDOR_AREA */
68 const char *bloblist_tag_name(enum bloblist_tag_t tag)
72 for (i = 0; i < ARRAY_SIZE(tag_name); i++) {
73 if (tag_name[i].tag == tag)
74 return tag_name[i].name;
80 static struct bloblist_rec *bloblist_first_blob(struct bloblist_hdr *hdr)
82 if (hdr->used_size <= hdr->hdr_size)
84 return (struct bloblist_rec *)((void *)hdr + hdr->hdr_size);
87 static inline uint rec_hdr_size(struct bloblist_rec *rec)
89 return (rec->tag_and_hdr_size & BLOBLISTR_HDR_SIZE_MASK) >>
90 BLOBLISTR_HDR_SIZE_SHIFT;
93 static inline uint rec_tag(struct bloblist_rec *rec)
95 return (rec->tag_and_hdr_size & BLOBLISTR_TAG_MASK) >>
99 static ulong bloblist_blob_end_ofs(struct bloblist_hdr *hdr,
100 struct bloblist_rec *rec)
104 offset = (void *)rec - (void *)hdr;
106 * The data section of next TE should start from an address aligned
107 * to 1 << hdr->align_log2.
109 offset += rec_hdr_size(rec) + rec->size;
110 offset = round_up(offset + rec_hdr_size(rec), 1 << hdr->align_log2);
111 offset -= rec_hdr_size(rec);
116 static struct bloblist_rec *bloblist_next_blob(struct bloblist_hdr *hdr,
117 struct bloblist_rec *rec)
119 ulong offset = bloblist_blob_end_ofs(hdr, rec);
121 if (offset >= hdr->used_size)
123 return (struct bloblist_rec *)((void *)hdr + offset);
126 #define foreach_rec(_rec, _hdr) \
127 for (_rec = bloblist_first_blob(_hdr); \
129 _rec = bloblist_next_blob(_hdr, _rec))
131 static struct bloblist_rec *bloblist_findrec(uint tag)
133 struct bloblist_hdr *hdr = gd->bloblist;
134 struct bloblist_rec *rec;
139 foreach_rec(rec, hdr) {
140 if (rec_tag(rec) == tag)
147 static int bloblist_addrec(uint tag, int size, int align_log2,
148 struct bloblist_rec **recp)
150 struct bloblist_hdr *hdr = gd->bloblist;
151 struct bloblist_rec *rec;
152 int data_start, aligned_start, new_alloced;
155 align_log2 = BLOBLIST_BLOB_ALIGN_LOG2;
157 /* Figure out where the new data will start */
158 data_start = map_to_sysmem(hdr) + hdr->used_size + sizeof(*rec);
160 /* Align the address and then calculate the offset from used size */
161 aligned_start = ALIGN(data_start, 1U << align_log2) - data_start;
163 /* If we need to create a dummy record, create it */
165 int void_size = aligned_start - sizeof(*rec);
166 struct bloblist_rec *vrec;
169 ret = bloblist_addrec(BLOBLISTT_VOID, void_size, 0, &vrec);
171 return log_msg_ret("void", ret);
173 /* start the record after that */
174 data_start = map_to_sysmem(hdr) + hdr->used_size + sizeof(*vrec);
177 /* Calculate the new allocated total */
178 new_alloced = data_start - map_to_sysmem(hdr) +
179 ALIGN(size, 1U << align_log2);
181 if (new_alloced > hdr->total_size) {
182 log_err("Failed to allocate %x bytes\n", size);
183 log_err("Used size=%x, total size=%x\n",
184 hdr->used_size, hdr->total_size);
185 return log_msg_ret("bloblist add", -ENOSPC);
187 rec = (void *)hdr + hdr->used_size;
189 rec->tag_and_hdr_size = tag | sizeof(*rec) << BLOBLISTR_HDR_SIZE_SHIFT;
192 /* Zero the record data */
193 memset((void *)rec + rec_hdr_size(rec), '\0', rec->size);
195 hdr->used_size = new_alloced;
201 static int bloblist_ensurerec(uint tag, struct bloblist_rec **recp, int size,
204 struct bloblist_rec *rec;
206 rec = bloblist_findrec(tag);
208 if (size && size != rec->size) {
215 ret = bloblist_addrec(tag, size, align_log2, &rec);
224 void *bloblist_find(uint tag, int size)
226 struct bloblist_rec *rec;
228 rec = bloblist_findrec(tag);
231 if (size && size != rec->size)
234 return (void *)rec + rec_hdr_size(rec);
237 void *bloblist_add(uint tag, int size, int align_log2)
239 struct bloblist_rec *rec;
241 if (bloblist_addrec(tag, size, align_log2, &rec))
244 return (void *)rec + rec_hdr_size(rec);
247 int bloblist_ensure_size(uint tag, int size, int align_log2, void **blobp)
249 struct bloblist_rec *rec;
252 ret = bloblist_ensurerec(tag, &rec, size, align_log2);
255 *blobp = (void *)rec + rec_hdr_size(rec);
260 void *bloblist_ensure(uint tag, int size)
262 struct bloblist_rec *rec;
264 if (bloblist_ensurerec(tag, &rec, size, 0))
267 return (void *)rec + rec_hdr_size(rec);
270 int bloblist_ensure_size_ret(uint tag, int *sizep, void **blobp)
272 struct bloblist_rec *rec;
275 ret = bloblist_ensurerec(tag, &rec, *sizep, 0);
280 *blobp = (void *)rec + rec_hdr_size(rec);
285 static int bloblist_resize_rec(struct bloblist_hdr *hdr,
286 struct bloblist_rec *rec,
289 int expand_by; /* Number of bytes to expand by (-ve to contract) */
291 ulong next_ofs; /* Offset of the record after @rec */
293 expand_by = ALIGN(new_size - rec->size, BLOBLIST_BLOB_ALIGN);
294 new_alloced = ALIGN(hdr->used_size + expand_by, BLOBLIST_BLOB_ALIGN);
296 log_debug("Attempt to shrink blob size below 0 (%x)\n",
298 return log_msg_ret("size", -EINVAL);
300 if (new_alloced > hdr->total_size) {
301 log_err("Failed to allocate %x bytes\n", new_size);
302 log_err("Used size=%x, total size=%x\n",
303 hdr->used_size, hdr->total_size);
304 return log_msg_ret("alloc", -ENOSPC);
307 /* Move the following blobs up or down, if this is not the last */
308 next_ofs = bloblist_blob_end_ofs(hdr, rec);
309 if (next_ofs != hdr->used_size) {
310 memmove((void *)hdr + next_ofs + expand_by,
311 (void *)hdr + next_ofs, new_alloced - next_ofs);
313 hdr->used_size = new_alloced;
315 /* Zero the new part of the blob */
317 memset((void *)rec + rec_hdr_size(rec) + rec->size, '\0',
318 new_size - rec->size);
321 /* Update the size of this blob */
322 rec->size = new_size;
327 int bloblist_resize(uint tag, int new_size)
329 struct bloblist_hdr *hdr = gd->bloblist;
330 struct bloblist_rec *rec;
333 rec = bloblist_findrec(tag);
335 return log_msg_ret("find", -ENOENT);
336 ret = bloblist_resize_rec(hdr, rec, new_size);
338 return log_msg_ret("resize", ret);
343 static u32 bloblist_calc_chksum(struct bloblist_hdr *hdr)
347 chksum = table_compute_checksum(hdr, hdr->used_size);
348 chksum += hdr->chksum;
353 int bloblist_new(ulong addr, uint size, uint flags, uint align_log2)
355 struct bloblist_hdr *hdr;
357 if (size < sizeof(*hdr))
358 return log_ret(-ENOSPC);
359 if (addr & (BLOBLIST_ALIGN - 1))
360 return log_ret(-EFAULT);
361 hdr = map_sysmem(addr, size);
362 memset(hdr, '\0', sizeof(*hdr));
363 hdr->version = BLOBLIST_VERSION;
364 hdr->hdr_size = sizeof(*hdr);
366 hdr->magic = BLOBLIST_MAGIC;
367 hdr->used_size = hdr->hdr_size;
368 hdr->total_size = size;
369 hdr->align_log2 = align_log2 ? align_log2 : BLOBLIST_BLOB_ALIGN_LOG2;
376 int bloblist_check(ulong addr, uint size)
378 struct bloblist_hdr *hdr;
381 hdr = map_sysmem(addr, sizeof(*hdr));
382 if (hdr->magic != BLOBLIST_MAGIC)
383 return log_msg_ret("Bad magic", -ENOENT);
384 if (hdr->version != BLOBLIST_VERSION)
385 return log_msg_ret("Bad version", -EPROTONOSUPPORT);
386 if (!hdr->total_size || (size && hdr->total_size > size))
387 return log_msg_ret("Bad total size", -EFBIG);
388 if (hdr->used_size > hdr->total_size)
389 return log_msg_ret("Bad used size", -ENOENT);
390 if (hdr->hdr_size != sizeof(struct bloblist_hdr))
391 return log_msg_ret("Bad header size", -ENOENT);
393 chksum = bloblist_calc_chksum(hdr);
394 if (hdr->chksum != chksum) {
395 log_err("Checksum %x != %x\n", hdr->chksum, chksum);
396 return log_msg_ret("Bad checksum", -EIO);
403 int bloblist_finish(void)
405 struct bloblist_hdr *hdr = gd->bloblist;
407 hdr->chksum = bloblist_calc_chksum(hdr);
408 log_debug("Finished bloblist size %lx at %lx\n", (ulong)hdr->used_size,
409 (ulong)map_to_sysmem(hdr));
414 ulong bloblist_get_base(void)
416 return map_to_sysmem(gd->bloblist);
419 ulong bloblist_get_size(void)
421 struct bloblist_hdr *hdr = gd->bloblist;
423 return hdr->used_size;
426 ulong bloblist_get_total_size(void)
428 struct bloblist_hdr *hdr = gd->bloblist;
430 return hdr->total_size;
433 void bloblist_get_stats(ulong *basep, ulong *tsizep, ulong *usizep)
435 struct bloblist_hdr *hdr = gd->bloblist;
437 *basep = map_to_sysmem(gd->bloblist);
438 *tsizep = hdr->total_size;
439 *usizep = hdr->used_size;
442 static void show_value(const char *prompt, ulong value)
444 printf("%s:%*s %-5lx ", prompt, 10 - (int)strlen(prompt), "", value);
445 print_size(value, "\n");
448 void bloblist_show_stats(void)
450 ulong base, tsize, usize;
452 bloblist_get_stats(&base, &tsize, &usize);
453 printf("base: %lx\n", base);
454 show_value("total size", tsize);
455 show_value("used size", usize);
456 show_value("free", tsize - usize);
459 void bloblist_show_list(void)
461 struct bloblist_hdr *hdr = gd->bloblist;
462 struct bloblist_rec *rec;
464 printf("%-8s %8s Tag Name\n", "Address", "Size");
465 for (rec = bloblist_first_blob(hdr); rec;
466 rec = bloblist_next_blob(hdr, rec)) {
467 printf("%08lx %8x %4x %s\n",
468 (ulong)map_to_sysmem((void *)rec + rec_hdr_size(rec)),
469 rec->size, rec_tag(rec),
470 bloblist_tag_name(rec_tag(rec)));
474 int bloblist_reloc(void *to, uint to_size)
476 struct bloblist_hdr *hdr;
478 if (to_size < gd->bloblist->total_size)
481 memcpy(to, gd->bloblist, gd->bloblist->total_size);
483 hdr->total_size = to_size;
490 * Weak default function for getting bloblist from boot args.
492 int __weak xferlist_from_boot_arg(ulong __always_unused addr,
493 ulong __always_unused size)
498 int bloblist_init(void)
500 bool fixed = IS_ENABLED(CONFIG_BLOBLIST_FIXED);
502 ulong addr = 0, size;
504 * If U-Boot is not in the first phase, an existing bloblist must be
505 * at a fixed address.
507 bool from_addr = fixed && !xpl_is_first_phase();
509 * If U-Boot is in the first phase that an arch custom routine should
510 * install the bloblist passed from previous loader to this fixed
513 bool from_boot_arg = fixed && xpl_is_first_phase();
515 if (xpl_prev_phase() == PHASE_TPL && !IS_ENABLED(CONFIG_TPL_BLOBLIST))
518 addr = IF_ENABLED_INT(CONFIG_BLOBLIST_FIXED,
519 CONFIG_BLOBLIST_ADDR);
520 size = CONFIG_BLOBLIST_SIZE;
523 ret = xferlist_from_boot_arg(addr, size);
525 ret = bloblist_check(addr, size);
528 log_warning("Bloblist at %lx not found (err=%d)\n",
531 /* Get the real size */
532 size = gd->bloblist->total_size;
536 * If we don't have a bloblist from a fixed address, or the one
537 * in the fixed address is not valid. we must allocate the
540 if (CONFIG_IS_ENABLED(BLOBLIST_ALLOC)) {
541 void *ptr = memalign(BLOBLIST_ALIGN, size);
544 return log_msg_ret("alloc", -ENOMEM);
545 addr = map_to_sysmem(ptr);
547 return log_msg_ret("BLOBLIST_FIXED is not enabled",
550 log_debug("Creating new bloblist size %lx at %lx\n", size,
552 ret = bloblist_new(addr, size, 0, 0);
554 log_debug("Found existing bloblist size %lx at %lx\n", size,
558 return log_msg_ret("ini", ret);
559 gd->flags |= GD_FLG_BLOBLIST_READY;
562 bloblist_show_stats();
563 bloblist_show_list();
569 int bloblist_maybe_init(void)
571 if (CONFIG_IS_ENABLED(BLOBLIST) && !(gd->flags & GD_FLG_BLOBLIST_READY))
572 return bloblist_init();
577 int bloblist_check_reg_conv(ulong rfdt, ulong rzero, ulong rsig)
579 ulong version = BLOBLIST_REGCONV_VER;
582 sigval = (IS_ENABLED(CONFIG_64BIT)) ?
583 ((BLOBLIST_MAGIC & ((1UL << BLOBLIST_REGCONV_SHIFT_64) - 1)) |
584 ((version & BLOBLIST_REGCONV_MASK) << BLOBLIST_REGCONV_SHIFT_64)) :
585 ((BLOBLIST_MAGIC & ((1UL << BLOBLIST_REGCONV_SHIFT_32) - 1)) |
586 ((version & BLOBLIST_REGCONV_MASK) << BLOBLIST_REGCONV_SHIFT_32));
588 if (rzero || rsig != sigval ||
589 rfdt != (ulong)bloblist_find(BLOBLISTT_CONTROL_FDT, 0)) {
590 gd->bloblist = NULL; /* Reset the gd bloblist pointer */