1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2018 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
12 #include <u-boot/crc.h>
15 * A bloblist is a single contiguous chunk of memory with a header
16 * (struct bloblist_hdr) and a number of blobs in it.
18 * Each blob starts on a BLOBLIST_ALIGN boundary relative to the start of the
19 * bloblist and consists of a struct bloblist_rec, some padding to the required
20 * alignment for the blog and then the actual data. The padding ensures that the
21 * start address of the data in each blob is aligned as required. Note that
22 * each blob's *data* is aligned to BLOBLIST_ALIGN regardless of the alignment
23 * of the bloblist itself or the blob header.
25 * So far, only BLOBLIST_ALIGN alignment is supported.
28 DECLARE_GLOBAL_DATA_PTR;
30 static const char *const tag_name[] = {
31 [BLOBLISTT_NONE] = "(none)",
32 [BLOBLISTT_EC_HOSTEVENT] = "EC host event",
33 [BLOBLISTT_SPL_HANDOFF] = "SPL hand-off",
34 [BLOBLISTT_VBOOT_CTX] = "Chrome OS vboot context",
35 [BLOBLISTT_VBOOT_HANDOFF] = "Chrome OS vboot hand-off",
38 const char *bloblist_tag_name(enum bloblist_tag_t tag)
40 if (tag < 0 || tag >= BLOBLISTT_COUNT)
46 static struct bloblist_rec *bloblist_first_blob(struct bloblist_hdr *hdr)
48 if (hdr->alloced <= hdr->hdr_size)
50 return (struct bloblist_rec *)((void *)hdr + hdr->hdr_size);
53 static struct bloblist_rec *bloblist_next_blob(struct bloblist_hdr *hdr,
54 struct bloblist_rec *rec)
58 offset = (void *)rec - (void *)hdr;
59 offset += rec->hdr_size + ALIGN(rec->size, BLOBLIST_ALIGN);
60 if (offset >= hdr->alloced)
62 return (struct bloblist_rec *)((void *)hdr + offset);
65 #define foreach_rec(_rec, _hdr) \
66 for (_rec = bloblist_first_blob(_hdr); \
68 _rec = bloblist_next_blob(_hdr, _rec))
70 static struct bloblist_rec *bloblist_findrec(uint tag)
72 struct bloblist_hdr *hdr = gd->bloblist;
73 struct bloblist_rec *rec;
78 foreach_rec(rec, hdr) {
86 static int bloblist_addrec(uint tag, int size, int align,
87 struct bloblist_rec **recp)
89 struct bloblist_hdr *hdr = gd->bloblist;
90 struct bloblist_rec *rec;
91 int data_start, new_alloced;
94 align = BLOBLIST_ALIGN;
96 /* Figure out where the new data will start */
97 data_start = map_to_sysmem(hdr) + hdr->alloced + sizeof(*rec);
99 /* Align the address and then calculate the offset from ->alloced */
100 data_start = ALIGN(data_start, align) - map_to_sysmem(hdr);
102 /* Calculate the new allocated total */
103 new_alloced = data_start + ALIGN(size, align);
105 if (new_alloced >= hdr->size) {
106 log(LOGC_BLOBLIST, LOGL_ERR,
107 "Failed to allocate %x bytes size=%x, need size=%x\n",
108 size, hdr->size, new_alloced);
109 return log_msg_ret("bloblist add", -ENOSPC);
111 rec = (void *)hdr + hdr->alloced;
114 rec->hdr_size = data_start - hdr->alloced;
118 /* Zero the record data */
119 memset((void *)rec + rec->hdr_size, '\0', rec->size);
121 hdr->alloced = new_alloced;
127 static int bloblist_ensurerec(uint tag, struct bloblist_rec **recp, int size,
130 struct bloblist_rec *rec;
132 rec = bloblist_findrec(tag);
134 if (size && size != rec->size) {
141 ret = bloblist_addrec(tag, size, align, &rec);
150 void *bloblist_find(uint tag, int size)
152 struct bloblist_rec *rec;
154 rec = bloblist_findrec(tag);
157 if (size && size != rec->size)
160 return (void *)rec + rec->hdr_size;
163 void *bloblist_add(uint tag, int size, int align)
165 struct bloblist_rec *rec;
167 if (bloblist_addrec(tag, size, align, &rec))
170 return (void *)rec + rec->hdr_size;
173 int bloblist_ensure_size(uint tag, int size, int align, void **blobp)
175 struct bloblist_rec *rec;
178 ret = bloblist_ensurerec(tag, &rec, size, align);
181 *blobp = (void *)rec + rec->hdr_size;
186 void *bloblist_ensure(uint tag, int size)
188 struct bloblist_rec *rec;
190 if (bloblist_ensurerec(tag, &rec, size, 0))
193 return (void *)rec + rec->hdr_size;
196 int bloblist_ensure_size_ret(uint tag, int *sizep, void **blobp)
198 struct bloblist_rec *rec;
201 ret = bloblist_ensurerec(tag, &rec, *sizep, 0);
206 *blobp = (void *)rec + rec->hdr_size;
211 static u32 bloblist_calc_chksum(struct bloblist_hdr *hdr)
213 struct bloblist_rec *rec;
216 chksum = crc32(0, (unsigned char *)hdr,
217 offsetof(struct bloblist_hdr, chksum));
218 foreach_rec(rec, hdr) {
219 chksum = crc32(chksum, (void *)rec, rec->hdr_size);
220 chksum = crc32(chksum, (void *)rec + rec->hdr_size, rec->size);
226 int bloblist_new(ulong addr, uint size, uint flags)
228 struct bloblist_hdr *hdr;
230 if (size < sizeof(*hdr))
231 return log_ret(-ENOSPC);
232 if (addr & (BLOBLIST_ALIGN - 1))
233 return log_ret(-EFAULT);
234 hdr = map_sysmem(addr, size);
235 memset(hdr, '\0', sizeof(*hdr));
236 hdr->version = BLOBLIST_VERSION;
237 hdr->hdr_size = sizeof(*hdr);
239 hdr->magic = BLOBLIST_MAGIC;
241 hdr->alloced = hdr->hdr_size;
248 int bloblist_check(ulong addr, uint size)
250 struct bloblist_hdr *hdr;
253 hdr = map_sysmem(addr, sizeof(*hdr));
254 if (hdr->magic != BLOBLIST_MAGIC)
255 return log_msg_ret("Bad magic", -ENOENT);
256 if (hdr->version != BLOBLIST_VERSION)
257 return log_msg_ret("Bad version", -EPROTONOSUPPORT);
258 if (size && hdr->size != size)
259 return log_msg_ret("Bad size", -EFBIG);
260 chksum = bloblist_calc_chksum(hdr);
261 if (hdr->chksum != chksum) {
262 log(LOGC_BLOBLIST, LOGL_ERR, "Checksum %x != %x\n", hdr->chksum,
264 return log_msg_ret("Bad checksum", -EIO);
271 int bloblist_finish(void)
273 struct bloblist_hdr *hdr = gd->bloblist;
275 hdr->chksum = bloblist_calc_chksum(hdr);
280 void bloblist_get_stats(ulong *basep, ulong *sizep, ulong *allocedp)
282 struct bloblist_hdr *hdr = gd->bloblist;
284 *basep = map_to_sysmem(gd->bloblist);
286 *allocedp = hdr->alloced;
289 static void show_value(const char *prompt, ulong value)
291 printf("%s:%*s %-5lx ", prompt, 8 - (int)strlen(prompt), "", value);
292 print_size(value, "\n");
295 void bloblist_show_stats(void)
297 ulong base, size, alloced;
299 bloblist_get_stats(&base, &size, &alloced);
300 printf("base: %lx\n", base);
301 show_value("size", size);
302 show_value("alloced", alloced);
303 show_value("free", size - alloced);
306 void bloblist_show_list(void)
308 struct bloblist_hdr *hdr = gd->bloblist;
309 struct bloblist_rec *rec;
311 printf("%-8s %8s Tag Name\n", "Address", "Size");
312 for (rec = bloblist_first_blob(hdr); rec;
313 rec = bloblist_next_blob(hdr, rec)) {
314 printf("%08lx %8x %3d %s\n",
315 (ulong)map_to_sysmem((void *)rec + rec->hdr_size),
316 rec->size, rec->tag, bloblist_tag_name(rec->tag));
320 int bloblist_init(void)
326 * Wed expect to find an existing bloblist in the first phase of U-Boot
329 expected = !u_boot_first_phase();
331 ret = bloblist_check(CONFIG_BLOBLIST_ADDR,
332 CONFIG_BLOBLIST_SIZE);
334 log(LOGC_BLOBLIST, expected ? LOGL_WARNING : LOGL_DEBUG,
335 "Existing bloblist not found: creating new bloblist\n");
336 ret = bloblist_new(CONFIG_BLOBLIST_ADDR, CONFIG_BLOBLIST_SIZE,
339 log(LOGC_BLOBLIST, LOGL_DEBUG, "Found existing bloblist\n");