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)
93 ret = bloblist_addrec(tag, size, &rec);
102 void *bloblist_find(uint tag, int size)
104 struct bloblist_rec *rec;
106 rec = bloblist_findrec(tag);
109 if (size && size != rec->size)
112 return (void *)rec + rec->hdr_size;
115 void *bloblist_add(uint tag, int size)
117 struct bloblist_rec *rec;
119 if (bloblist_addrec(tag, size, &rec))
125 int bloblist_ensure_size(uint tag, int size, void **blobp)
127 struct bloblist_rec *rec;
130 ret = bloblist_ensurerec(tag, &rec, size);
133 *blobp = (void *)rec + rec->hdr_size;
138 void *bloblist_ensure(uint tag, int size)
140 struct bloblist_rec *rec;
142 if (bloblist_ensurerec(tag, &rec, size))
145 return (void *)rec + rec->hdr_size;
148 static u32 bloblist_calc_chksum(struct bloblist_hdr *hdr)
150 struct bloblist_rec *rec;
153 chksum = crc32(0, (unsigned char *)hdr,
154 offsetof(struct bloblist_hdr, chksum));
155 foreach_rec(rec, hdr) {
156 chksum = crc32(chksum, (void *)rec, rec->hdr_size);
157 chksum = crc32(chksum, (void *)rec + rec->hdr_size, rec->size);
163 int bloblist_new(ulong addr, uint size, uint flags)
165 struct bloblist_hdr *hdr;
167 if (size < sizeof(*hdr))
168 return log_ret(-ENOSPC);
169 if (addr & (BLOBLIST_ALIGN - 1))
170 return log_ret(-EFAULT);
171 hdr = map_sysmem(addr, size);
172 memset(hdr, '\0', sizeof(*hdr));
173 hdr->version = BLOBLIST_VERSION;
174 hdr->hdr_size = sizeof(*hdr);
176 hdr->magic = BLOBLIST_MAGIC;
178 hdr->alloced = hdr->hdr_size;
185 int bloblist_check(ulong addr, uint size)
187 struct bloblist_hdr *hdr;
190 hdr = map_sysmem(addr, sizeof(*hdr));
191 if (hdr->magic != BLOBLIST_MAGIC)
192 return log_msg_ret("Bad magic", -ENOENT);
193 if (hdr->version != BLOBLIST_VERSION)
194 return log_msg_ret("Bad version", -EPROTONOSUPPORT);
195 if (size && hdr->size != size)
196 return log_msg_ret("Bad size", -EFBIG);
197 chksum = bloblist_calc_chksum(hdr);
198 if (hdr->chksum != chksum) {
199 log(LOGC_BLOBLIST, LOGL_ERR, "Checksum %x != %x\n", hdr->chksum,
201 return log_msg_ret("Bad checksum", -EIO);
208 int bloblist_finish(void)
210 struct bloblist_hdr *hdr = gd->bloblist;
212 hdr->chksum = bloblist_calc_chksum(hdr);
217 int bloblist_init(void)
223 * Wed expect to find an existing bloblist in the first phase of U-Boot
226 expected = !u_boot_first_phase();
228 ret = bloblist_check(CONFIG_BLOBLIST_ADDR,
229 CONFIG_BLOBLIST_SIZE);
231 log(LOGC_BLOBLIST, expected ? LOGL_WARNING : LOGL_DEBUG,
232 "Existing bloblist not found: creating new bloblist\n");
233 ret = bloblist_new(CONFIG_BLOBLIST_ADDR, CONFIG_BLOBLIST_SIZE,
236 log(LOGC_BLOBLIST, LOGL_DEBUG, "Found existing bloblist\n");