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>
14 DECLARE_GLOBAL_DATA_PTR;
16 struct bloblist_rec *bloblist_first_blob(struct bloblist_hdr *hdr)
18 if (hdr->alloced <= hdr->hdr_size)
20 return (struct bloblist_rec *)((void *)hdr + hdr->hdr_size);
23 struct bloblist_rec *bloblist_next_blob(struct bloblist_hdr *hdr,
24 struct bloblist_rec *rec)
28 offset = (void *)rec - (void *)hdr;
29 offset += rec->hdr_size + ALIGN(rec->size, BLOBLIST_ALIGN);
30 if (offset >= hdr->alloced)
32 return (struct bloblist_rec *)((void *)hdr + offset);
35 #define foreach_rec(_rec, _hdr) \
36 for (_rec = bloblist_first_blob(_hdr); \
38 _rec = bloblist_next_blob(_hdr, _rec))
40 static struct bloblist_rec *bloblist_findrec(uint tag)
42 struct bloblist_hdr *hdr = gd->bloblist;
43 struct bloblist_rec *rec;
48 foreach_rec(rec, hdr) {
56 static int bloblist_addrec(uint tag, int size, struct bloblist_rec **recp)
58 struct bloblist_hdr *hdr = gd->bloblist;
59 struct bloblist_rec *rec;
62 new_alloced = hdr->alloced + sizeof(*rec) +
63 ALIGN(size, BLOBLIST_ALIGN);
64 if (new_alloced >= hdr->size) {
65 log(LOGC_BLOBLIST, LOGL_ERR,
66 "Failed to allocate %x bytes size=%x, need size>=%x\n",
67 size, hdr->size, new_alloced);
68 return log_msg_ret("bloblist add", -ENOSPC);
70 rec = (void *)hdr + hdr->alloced;
71 hdr->alloced = new_alloced;
74 rec->hdr_size = sizeof(*rec);
82 static int bloblist_ensurerec(uint tag, struct bloblist_rec **recp, int size)
84 struct bloblist_rec *rec;
86 rec = bloblist_findrec(tag);
88 if (size && size != rec->size) {
95 ret = bloblist_addrec(tag, size, &rec);
104 void *bloblist_find(uint tag, int size)
106 struct bloblist_rec *rec;
108 rec = bloblist_findrec(tag);
111 if (size && size != rec->size)
114 return (void *)rec + rec->hdr_size;
117 void *bloblist_add(uint tag, int size)
119 struct bloblist_rec *rec;
121 if (bloblist_addrec(tag, size, &rec))
127 int bloblist_ensure_size(uint tag, int size, void **blobp)
129 struct bloblist_rec *rec;
132 ret = bloblist_ensurerec(tag, &rec, size);
135 *blobp = (void *)rec + rec->hdr_size;
140 void *bloblist_ensure(uint tag, int size)
142 struct bloblist_rec *rec;
144 if (bloblist_ensurerec(tag, &rec, size))
147 return (void *)rec + rec->hdr_size;
150 int bloblist_ensure_size_ret(uint tag, int *sizep, void **blobp)
152 struct bloblist_rec *rec;
155 ret = bloblist_ensurerec(tag, &rec, *sizep);
160 *blobp = (void *)rec + rec->hdr_size;
165 static u32 bloblist_calc_chksum(struct bloblist_hdr *hdr)
167 struct bloblist_rec *rec;
170 chksum = crc32(0, (unsigned char *)hdr,
171 offsetof(struct bloblist_hdr, chksum));
172 foreach_rec(rec, hdr) {
173 chksum = crc32(chksum, (void *)rec, rec->hdr_size);
174 chksum = crc32(chksum, (void *)rec + rec->hdr_size, rec->size);
180 int bloblist_new(ulong addr, uint size, uint flags)
182 struct bloblist_hdr *hdr;
184 if (size < sizeof(*hdr))
185 return log_ret(-ENOSPC);
186 if (addr & (BLOBLIST_ALIGN - 1))
187 return log_ret(-EFAULT);
188 hdr = map_sysmem(addr, size);
189 memset(hdr, '\0', sizeof(*hdr));
190 hdr->version = BLOBLIST_VERSION;
191 hdr->hdr_size = sizeof(*hdr);
193 hdr->magic = BLOBLIST_MAGIC;
195 hdr->alloced = hdr->hdr_size;
202 int bloblist_check(ulong addr, uint size)
204 struct bloblist_hdr *hdr;
207 hdr = map_sysmem(addr, sizeof(*hdr));
208 if (hdr->magic != BLOBLIST_MAGIC)
209 return log_msg_ret("Bad magic", -ENOENT);
210 if (hdr->version != BLOBLIST_VERSION)
211 return log_msg_ret("Bad version", -EPROTONOSUPPORT);
212 if (size && hdr->size != size)
213 return log_msg_ret("Bad size", -EFBIG);
214 chksum = bloblist_calc_chksum(hdr);
215 if (hdr->chksum != chksum) {
216 log(LOGC_BLOBLIST, LOGL_ERR, "Checksum %x != %x\n", hdr->chksum,
218 return log_msg_ret("Bad checksum", -EIO);
225 int bloblist_finish(void)
227 struct bloblist_hdr *hdr = gd->bloblist;
229 hdr->chksum = bloblist_calc_chksum(hdr);
234 int bloblist_init(void)
240 * Wed expect to find an existing bloblist in the first phase of U-Boot
243 expected = !u_boot_first_phase();
245 ret = bloblist_check(CONFIG_BLOBLIST_ADDR,
246 CONFIG_BLOBLIST_SIZE);
248 log(LOGC_BLOBLIST, expected ? LOGL_WARNING : LOGL_DEBUG,
249 "Existing bloblist not found: creating new bloblist\n");
250 ret = bloblist_new(CONFIG_BLOBLIST_ADDR, CONFIG_BLOBLIST_SIZE,
253 log(LOGC_BLOBLIST, LOGL_DEBUG, "Found existing bloblist\n");