1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2018 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
12 #include <asm/global_data.h>
13 #include <u-boot/crc.h>
16 * A bloblist is a single contiguous chunk of memory with a header
17 * (struct bloblist_hdr) and a number of blobs in it.
19 * Each blob starts on a BLOBLIST_ALIGN boundary relative to the start of the
20 * bloblist and consists of a struct bloblist_rec, some padding to the required
21 * alignment for the blog and then the actual data. The padding ensures that the
22 * start address of the data in each blob is aligned as required. Note that
23 * each blob's *data* is aligned to BLOBLIST_ALIGN regardless of the alignment
24 * of the bloblist itself or the blob header.
26 * So far, only BLOBLIST_ALIGN alignment is supported.
29 DECLARE_GLOBAL_DATA_PTR;
31 static const char *const tag_name[] = {
32 [BLOBLISTT_NONE] = "(none)",
33 [BLOBLISTT_EC_HOSTEVENT] = "EC host event",
34 [BLOBLISTT_SPL_HANDOFF] = "SPL hand-off",
35 [BLOBLISTT_VBOOT_CTX] = "Chrome OS vboot context",
36 [BLOBLISTT_VBOOT_HANDOFF] = "Chrome OS vboot hand-off",
37 [BLOBLISTT_ACPI_GNVS] = "ACPI GNVS",
38 [BLOBLISTT_INTEL_VBT] = "Intel Video-BIOS table",
39 [BLOBLISTT_TPM2_TCG_LOG] = "TPM v2 log space",
40 [BLOBLISTT_TCPA_LOG] = "TPM log space",
41 [BLOBLISTT_ACPI_TABLES] = "ACPI tables for x86",
42 [BLOBLISTT_SMBIOS_TABLES] = "SMBIOS tables for x86",
45 const char *bloblist_tag_name(enum bloblist_tag_t tag)
47 if (tag < 0 || tag >= BLOBLISTT_COUNT)
53 static struct bloblist_rec *bloblist_first_blob(struct bloblist_hdr *hdr)
55 if (hdr->alloced <= hdr->hdr_size)
57 return (struct bloblist_rec *)((void *)hdr + hdr->hdr_size);
60 static ulong bloblist_blob_end_ofs(struct bloblist_hdr *hdr,
61 struct bloblist_rec *rec)
65 offset = (void *)rec - (void *)hdr;
66 offset += rec->hdr_size + ALIGN(rec->size, BLOBLIST_ALIGN);
71 static struct bloblist_rec *bloblist_next_blob(struct bloblist_hdr *hdr,
72 struct bloblist_rec *rec)
74 ulong offset = bloblist_blob_end_ofs(hdr, rec);
76 if (offset >= hdr->alloced)
78 return (struct bloblist_rec *)((void *)hdr + offset);
81 #define foreach_rec(_rec, _hdr) \
82 for (_rec = bloblist_first_blob(_hdr); \
84 _rec = bloblist_next_blob(_hdr, _rec))
86 static struct bloblist_rec *bloblist_findrec(uint tag)
88 struct bloblist_hdr *hdr = gd->bloblist;
89 struct bloblist_rec *rec;
94 foreach_rec(rec, hdr) {
102 static int bloblist_addrec(uint tag, int size, int align,
103 struct bloblist_rec **recp)
105 struct bloblist_hdr *hdr = gd->bloblist;
106 struct bloblist_rec *rec;
107 int data_start, new_alloced;
110 align = BLOBLIST_ALIGN;
112 /* Figure out where the new data will start */
113 data_start = map_to_sysmem(hdr) + hdr->alloced + sizeof(*rec);
115 /* Align the address and then calculate the offset from ->alloced */
116 data_start = ALIGN(data_start, align) - map_to_sysmem(hdr);
118 /* Calculate the new allocated total */
119 new_alloced = data_start + ALIGN(size, align);
121 if (new_alloced > hdr->size) {
122 log(LOGC_BLOBLIST, LOGL_ERR,
123 "Failed to allocate %x bytes size=%x, need size=%x\n",
124 size, hdr->size, new_alloced);
125 return log_msg_ret("bloblist add", -ENOSPC);
127 rec = (void *)hdr + hdr->alloced;
130 rec->hdr_size = data_start - hdr->alloced;
134 /* Zero the record data */
135 memset((void *)rec + rec->hdr_size, '\0', rec->size);
137 hdr->alloced = new_alloced;
143 static int bloblist_ensurerec(uint tag, struct bloblist_rec **recp, int size,
146 struct bloblist_rec *rec;
148 rec = bloblist_findrec(tag);
150 if (size && size != rec->size) {
157 ret = bloblist_addrec(tag, size, align, &rec);
166 void *bloblist_find(uint tag, int size)
168 struct bloblist_rec *rec;
170 rec = bloblist_findrec(tag);
173 if (size && size != rec->size)
176 return (void *)rec + rec->hdr_size;
179 void *bloblist_add(uint tag, int size, int align)
181 struct bloblist_rec *rec;
183 if (bloblist_addrec(tag, size, align, &rec))
186 return (void *)rec + rec->hdr_size;
189 int bloblist_ensure_size(uint tag, int size, int align, void **blobp)
191 struct bloblist_rec *rec;
194 ret = bloblist_ensurerec(tag, &rec, size, align);
197 *blobp = (void *)rec + rec->hdr_size;
202 void *bloblist_ensure(uint tag, int size)
204 struct bloblist_rec *rec;
206 if (bloblist_ensurerec(tag, &rec, size, 0))
209 return (void *)rec + rec->hdr_size;
212 int bloblist_ensure_size_ret(uint tag, int *sizep, void **blobp)
214 struct bloblist_rec *rec;
217 ret = bloblist_ensurerec(tag, &rec, *sizep, 0);
222 *blobp = (void *)rec + rec->hdr_size;
227 static int bloblist_resize_rec(struct bloblist_hdr *hdr,
228 struct bloblist_rec *rec,
231 int expand_by; /* Number of bytes to expand by (-ve to contract) */
232 int new_alloced; /* New value for @hdr->alloced */
233 ulong next_ofs; /* Offset of the record after @rec */
235 expand_by = ALIGN(new_size - rec->size, BLOBLIST_ALIGN);
236 new_alloced = ALIGN(hdr->alloced + expand_by, BLOBLIST_ALIGN);
238 log(LOGC_BLOBLIST, LOGL_DEBUG,
239 "Attempt to shrink blob size below 0 (%x)\n", new_size);
240 return log_msg_ret("size", -EINVAL);
242 if (new_alloced > hdr->size) {
243 log(LOGC_BLOBLIST, LOGL_ERR,
244 "Failed to allocate %x bytes size=%x, need size=%x\n",
245 new_size, hdr->size, new_alloced);
246 return log_msg_ret("alloc", -ENOSPC);
249 /* Move the following blobs up or down, if this is not the last */
250 next_ofs = bloblist_blob_end_ofs(hdr, rec);
251 if (next_ofs != hdr->alloced) {
252 memmove((void *)hdr + next_ofs + expand_by,
253 (void *)hdr + next_ofs, new_alloced - next_ofs);
255 hdr->alloced = new_alloced;
257 /* Zero the new part of the blob */
259 memset((void *)rec + rec->hdr_size + rec->size, '\0',
260 new_size - rec->size);
263 /* Update the size of this blob */
264 rec->size = new_size;
269 int bloblist_resize(uint tag, int new_size)
271 struct bloblist_hdr *hdr = gd->bloblist;
272 struct bloblist_rec *rec;
275 rec = bloblist_findrec(tag);
277 return log_msg_ret("find", -ENOENT);
278 ret = bloblist_resize_rec(hdr, rec, new_size);
280 return log_msg_ret("resize", ret);
285 static u32 bloblist_calc_chksum(struct bloblist_hdr *hdr)
287 struct bloblist_rec *rec;
290 chksum = crc32(0, (unsigned char *)hdr,
291 offsetof(struct bloblist_hdr, chksum));
292 foreach_rec(rec, hdr) {
293 chksum = crc32(chksum, (void *)rec, rec->hdr_size);
294 chksum = crc32(chksum, (void *)rec + rec->hdr_size, rec->size);
300 int bloblist_new(ulong addr, uint size, uint flags)
302 struct bloblist_hdr *hdr;
304 if (size < sizeof(*hdr))
305 return log_ret(-ENOSPC);
306 if (addr & (BLOBLIST_ALIGN - 1))
307 return log_ret(-EFAULT);
308 hdr = map_sysmem(addr, size);
309 memset(hdr, '\0', sizeof(*hdr));
310 hdr->version = BLOBLIST_VERSION;
311 hdr->hdr_size = sizeof(*hdr);
313 hdr->magic = BLOBLIST_MAGIC;
315 hdr->alloced = hdr->hdr_size;
322 int bloblist_check(ulong addr, uint size)
324 struct bloblist_hdr *hdr;
327 hdr = map_sysmem(addr, sizeof(*hdr));
328 if (hdr->magic != BLOBLIST_MAGIC)
329 return log_msg_ret("Bad magic", -ENOENT);
330 if (hdr->version != BLOBLIST_VERSION)
331 return log_msg_ret("Bad version", -EPROTONOSUPPORT);
332 if (size && hdr->size != size)
333 return log_msg_ret("Bad size", -EFBIG);
334 chksum = bloblist_calc_chksum(hdr);
335 if (hdr->chksum != chksum) {
336 log(LOGC_BLOBLIST, LOGL_ERR, "Checksum %x != %x\n", hdr->chksum,
338 return log_msg_ret("Bad checksum", -EIO);
345 int bloblist_finish(void)
347 struct bloblist_hdr *hdr = gd->bloblist;
349 hdr->chksum = bloblist_calc_chksum(hdr);
354 void bloblist_get_stats(ulong *basep, ulong *sizep, ulong *allocedp)
356 struct bloblist_hdr *hdr = gd->bloblist;
358 *basep = map_to_sysmem(gd->bloblist);
360 *allocedp = hdr->alloced;
363 static void show_value(const char *prompt, ulong value)
365 printf("%s:%*s %-5lx ", prompt, 8 - (int)strlen(prompt), "", value);
366 print_size(value, "\n");
369 void bloblist_show_stats(void)
371 ulong base, size, alloced;
373 bloblist_get_stats(&base, &size, &alloced);
374 printf("base: %lx\n", base);
375 show_value("size", size);
376 show_value("alloced", alloced);
377 show_value("free", size - alloced);
380 void bloblist_show_list(void)
382 struct bloblist_hdr *hdr = gd->bloblist;
383 struct bloblist_rec *rec;
385 printf("%-8s %8s Tag Name\n", "Address", "Size");
386 for (rec = bloblist_first_blob(hdr); rec;
387 rec = bloblist_next_blob(hdr, rec)) {
388 printf("%08lx %8x %3d %s\n",
389 (ulong)map_to_sysmem((void *)rec + rec->hdr_size),
390 rec->size, rec->tag, bloblist_tag_name(rec->tag));
394 void bloblist_reloc(void *to, uint to_size, void *from, uint from_size)
396 struct bloblist_hdr *hdr;
398 memcpy(to, from, from_size);
403 int bloblist_init(void)
409 * Wed expect to find an existing bloblist in the first phase of U-Boot
412 expected = !u_boot_first_phase();
413 if (spl_prev_phase() == PHASE_TPL && !IS_ENABLED(CONFIG_TPL_BLOBLIST))
416 ret = bloblist_check(CONFIG_BLOBLIST_ADDR,
417 CONFIG_BLOBLIST_SIZE);
419 log(LOGC_BLOBLIST, expected ? LOGL_WARNING : LOGL_DEBUG,
420 "Existing bloblist not found: creating new bloblist\n");
421 ret = bloblist_new(CONFIG_BLOBLIST_ADDR, CONFIG_BLOBLIST_SIZE,
424 log(LOGC_BLOBLIST, LOGL_DEBUG, "Found existing bloblist\n");