1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2018 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
13 #include <asm/global_data.h>
14 #include <u-boot/crc.h>
17 * A bloblist is a single contiguous chunk of memory with a header
18 * (struct bloblist_hdr) and a number of blobs in it.
20 * Each blob starts on a BLOBLIST_ALIGN boundary relative to the start of the
21 * bloblist and consists of a struct bloblist_rec, some padding to the required
22 * alignment for the blog and then the actual data. The padding ensures that the
23 * start address of the data in each blob is aligned as required. Note that
24 * each blob's *data* is aligned to BLOBLIST_ALIGN regardless of the alignment
25 * of the bloblist itself or the blob header.
27 * So far, only BLOBLIST_ALIGN alignment is supported.
30 DECLARE_GLOBAL_DATA_PTR;
32 static struct tag_name {
33 enum bloblist_tag_t tag;
36 { BLOBLISTT_NONE, "(none)" },
38 /* BLOBLISTT_AREA_FIRMWARE_TOP */
40 /* BLOBLISTT_AREA_FIRMWARE */
41 { BLOBLISTT_ACPI_GNVS, "ACPI GNVS" },
42 { BLOBLISTT_INTEL_VBT, "Intel Video-BIOS table" },
43 { BLOBLISTT_TPM2_TCG_LOG, "TPM v2 log space" },
44 { BLOBLISTT_TCPA_LOG, "TPM log space" },
45 { BLOBLISTT_ACPI_TABLES, "ACPI tables for x86" },
46 { BLOBLISTT_SMBIOS_TABLES, "SMBIOS tables for x86" },
47 { BLOBLISTT_VBOOT_CTX, "Chrome OS vboot context" },
49 /* BLOBLISTT_PROJECT_AREA */
50 { BLOBLISTT_U_BOOT_SPL_HANDOFF, "SPL hand-off" },
52 /* BLOBLISTT_VENDOR_AREA */
55 const char *bloblist_tag_name(enum bloblist_tag_t tag)
59 for (i = 0; i < ARRAY_SIZE(tag_name); i++) {
60 if (tag_name[i].tag == tag)
61 return tag_name[i].name;
67 static struct bloblist_rec *bloblist_first_blob(struct bloblist_hdr *hdr)
69 if (hdr->alloced <= hdr->hdr_size)
71 return (struct bloblist_rec *)((void *)hdr + hdr->hdr_size);
74 static ulong bloblist_blob_end_ofs(struct bloblist_hdr *hdr,
75 struct bloblist_rec *rec)
79 offset = (void *)rec - (void *)hdr;
80 offset += rec->hdr_size + ALIGN(rec->size, BLOBLIST_ALIGN);
85 static struct bloblist_rec *bloblist_next_blob(struct bloblist_hdr *hdr,
86 struct bloblist_rec *rec)
88 ulong offset = bloblist_blob_end_ofs(hdr, rec);
90 if (offset >= hdr->alloced)
92 return (struct bloblist_rec *)((void *)hdr + offset);
95 #define foreach_rec(_rec, _hdr) \
96 for (_rec = bloblist_first_blob(_hdr); \
98 _rec = bloblist_next_blob(_hdr, _rec))
100 static struct bloblist_rec *bloblist_findrec(uint tag)
102 struct bloblist_hdr *hdr = gd->bloblist;
103 struct bloblist_rec *rec;
108 foreach_rec(rec, hdr) {
116 static int bloblist_addrec(uint tag, int size, int align,
117 struct bloblist_rec **recp)
119 struct bloblist_hdr *hdr = gd->bloblist;
120 struct bloblist_rec *rec;
121 int data_start, new_alloced;
124 align = BLOBLIST_ALIGN;
126 /* Figure out where the new data will start */
127 data_start = map_to_sysmem(hdr) + hdr->alloced + sizeof(*rec);
129 /* Align the address and then calculate the offset from ->alloced */
130 data_start = ALIGN(data_start, align) - map_to_sysmem(hdr);
132 /* Calculate the new allocated total */
133 new_alloced = data_start + ALIGN(size, align);
135 if (new_alloced > hdr->size) {
136 log(LOGC_BLOBLIST, LOGL_ERR,
137 "Failed to allocate %x bytes size=%x, need size=%x\n",
138 size, hdr->size, new_alloced);
139 return log_msg_ret("bloblist add", -ENOSPC);
141 rec = (void *)hdr + hdr->alloced;
144 rec->hdr_size = data_start - hdr->alloced;
148 /* Zero the record data */
149 memset((void *)rec + rec->hdr_size, '\0', rec->size);
151 hdr->alloced = new_alloced;
157 static int bloblist_ensurerec(uint tag, struct bloblist_rec **recp, int size,
160 struct bloblist_rec *rec;
162 rec = bloblist_findrec(tag);
164 if (size && size != rec->size) {
171 ret = bloblist_addrec(tag, size, align, &rec);
180 void *bloblist_find(uint tag, int size)
182 struct bloblist_rec *rec;
184 rec = bloblist_findrec(tag);
187 if (size && size != rec->size)
190 return (void *)rec + rec->hdr_size;
193 void *bloblist_add(uint tag, int size, int align)
195 struct bloblist_rec *rec;
197 if (bloblist_addrec(tag, size, align, &rec))
200 return (void *)rec + rec->hdr_size;
203 int bloblist_ensure_size(uint tag, int size, int align, void **blobp)
205 struct bloblist_rec *rec;
208 ret = bloblist_ensurerec(tag, &rec, size, align);
211 *blobp = (void *)rec + rec->hdr_size;
216 void *bloblist_ensure(uint tag, int size)
218 struct bloblist_rec *rec;
220 if (bloblist_ensurerec(tag, &rec, size, 0))
223 return (void *)rec + rec->hdr_size;
226 int bloblist_ensure_size_ret(uint tag, int *sizep, void **blobp)
228 struct bloblist_rec *rec;
231 ret = bloblist_ensurerec(tag, &rec, *sizep, 0);
236 *blobp = (void *)rec + rec->hdr_size;
241 static int bloblist_resize_rec(struct bloblist_hdr *hdr,
242 struct bloblist_rec *rec,
245 int expand_by; /* Number of bytes to expand by (-ve to contract) */
246 int new_alloced; /* New value for @hdr->alloced */
247 ulong next_ofs; /* Offset of the record after @rec */
249 expand_by = ALIGN(new_size - rec->size, BLOBLIST_ALIGN);
250 new_alloced = ALIGN(hdr->alloced + expand_by, BLOBLIST_ALIGN);
252 log(LOGC_BLOBLIST, LOGL_DEBUG,
253 "Attempt to shrink blob size below 0 (%x)\n", new_size);
254 return log_msg_ret("size", -EINVAL);
256 if (new_alloced > hdr->size) {
257 log(LOGC_BLOBLIST, LOGL_ERR,
258 "Failed to allocate %x bytes size=%x, need size=%x\n",
259 new_size, hdr->size, new_alloced);
260 return log_msg_ret("alloc", -ENOSPC);
263 /* Move the following blobs up or down, if this is not the last */
264 next_ofs = bloblist_blob_end_ofs(hdr, rec);
265 if (next_ofs != hdr->alloced) {
266 memmove((void *)hdr + next_ofs + expand_by,
267 (void *)hdr + next_ofs, new_alloced - next_ofs);
269 hdr->alloced = new_alloced;
271 /* Zero the new part of the blob */
273 memset((void *)rec + rec->hdr_size + rec->size, '\0',
274 new_size - rec->size);
277 /* Update the size of this blob */
278 rec->size = new_size;
283 int bloblist_resize(uint tag, int new_size)
285 struct bloblist_hdr *hdr = gd->bloblist;
286 struct bloblist_rec *rec;
289 rec = bloblist_findrec(tag);
291 return log_msg_ret("find", -ENOENT);
292 ret = bloblist_resize_rec(hdr, rec, new_size);
294 return log_msg_ret("resize", ret);
299 static u32 bloblist_calc_chksum(struct bloblist_hdr *hdr)
301 struct bloblist_rec *rec;
304 chksum = crc32(0, (unsigned char *)hdr,
305 offsetof(struct bloblist_hdr, chksum));
306 foreach_rec(rec, hdr) {
307 chksum = crc32(chksum, (void *)rec, rec->hdr_size);
308 chksum = crc32(chksum, (void *)rec + rec->hdr_size, rec->size);
314 int bloblist_new(ulong addr, uint size, uint flags)
316 struct bloblist_hdr *hdr;
318 if (size < sizeof(*hdr))
319 return log_ret(-ENOSPC);
320 if (addr & (BLOBLIST_ALIGN - 1))
321 return log_ret(-EFAULT);
322 hdr = map_sysmem(addr, size);
323 memset(hdr, '\0', sizeof(*hdr));
324 hdr->version = BLOBLIST_VERSION;
325 hdr->hdr_size = sizeof(*hdr);
327 hdr->magic = BLOBLIST_MAGIC;
329 hdr->alloced = hdr->hdr_size;
336 int bloblist_check(ulong addr, uint size)
338 struct bloblist_hdr *hdr;
341 hdr = map_sysmem(addr, sizeof(*hdr));
342 if (hdr->magic != BLOBLIST_MAGIC)
343 return log_msg_ret("Bad magic", -ENOENT);
344 if (hdr->version != BLOBLIST_VERSION)
345 return log_msg_ret("Bad version", -EPROTONOSUPPORT);
346 if (size && hdr->size != size)
347 return log_msg_ret("Bad size", -EFBIG);
348 chksum = bloblist_calc_chksum(hdr);
349 if (hdr->chksum != chksum) {
350 log(LOGC_BLOBLIST, LOGL_ERR, "Checksum %x != %x\n", hdr->chksum,
352 return log_msg_ret("Bad checksum", -EIO);
359 int bloblist_finish(void)
361 struct bloblist_hdr *hdr = gd->bloblist;
363 hdr->chksum = bloblist_calc_chksum(hdr);
368 void bloblist_get_stats(ulong *basep, ulong *sizep, ulong *allocedp)
370 struct bloblist_hdr *hdr = gd->bloblist;
372 *basep = map_to_sysmem(gd->bloblist);
374 *allocedp = hdr->alloced;
377 static void show_value(const char *prompt, ulong value)
379 printf("%s:%*s %-5lx ", prompt, 8 - (int)strlen(prompt), "", value);
380 print_size(value, "\n");
383 void bloblist_show_stats(void)
385 ulong base, size, alloced;
387 bloblist_get_stats(&base, &size, &alloced);
388 printf("base: %lx\n", base);
389 show_value("size", size);
390 show_value("alloced", alloced);
391 show_value("free", size - alloced);
394 void bloblist_show_list(void)
396 struct bloblist_hdr *hdr = gd->bloblist;
397 struct bloblist_rec *rec;
399 printf("%-8s %8s Tag Name\n", "Address", "Size");
400 for (rec = bloblist_first_blob(hdr); rec;
401 rec = bloblist_next_blob(hdr, rec)) {
402 printf("%08lx %8x %4x %s\n",
403 (ulong)map_to_sysmem((void *)rec + rec->hdr_size),
404 rec->size, rec->tag, bloblist_tag_name(rec->tag));
408 void bloblist_reloc(void *to, uint to_size, void *from, uint from_size)
410 struct bloblist_hdr *hdr;
412 memcpy(to, from, from_size);
417 int bloblist_init(void)
423 * Wed expect to find an existing bloblist in the first phase of U-Boot
426 expected = !u_boot_first_phase();
427 if (spl_prev_phase() == PHASE_TPL && !IS_ENABLED(CONFIG_TPL_BLOBLIST))
430 ret = bloblist_check(CONFIG_BLOBLIST_ADDR,
431 CONFIG_BLOBLIST_SIZE);
435 log(LOGC_BLOBLIST, expected ? LOGL_WARNING : LOGL_DEBUG,
436 "Existing bloblist not found: creating new bloblist\n");
437 if (IS_ENABLED(CONFIG_BLOBLIST_ALLOC)) {
438 void *ptr = memalign(BLOBLIST_ALIGN,
439 CONFIG_BLOBLIST_SIZE);
442 return log_msg_ret("alloc", -ENOMEM);
443 addr = map_to_sysmem(ptr);
445 addr = CONFIG_BLOBLIST_ADDR;
447 ret = bloblist_new(addr, CONFIG_BLOBLIST_SIZE, 0);
449 log(LOGC_BLOBLIST, LOGL_DEBUG, "Found existing bloblist\n");