]> git.ipfire.org Git - thirdparty/u-boot.git/blob - common/bloblist.c
Merge tag 'u-boot-at91-2022.04-a' of https://source.denx.de/u-boot/custodians/u-boot...
[thirdparty/u-boot.git] / common / bloblist.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright 2018 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
5 */
6
7 #include <common.h>
8 #include <bloblist.h>
9 #include <log.h>
10 #include <malloc.h>
11 #include <mapmem.h>
12 #include <spl.h>
13 #include <asm/global_data.h>
14 #include <u-boot/crc.h>
15
16 /*
17 * A bloblist is a single contiguous chunk of memory with a header
18 * (struct bloblist_hdr) and a number of blobs in it.
19 *
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.
26 *
27 * So far, only BLOBLIST_ALIGN alignment is supported.
28 */
29
30 DECLARE_GLOBAL_DATA_PTR;
31
32 static const char *const tag_name[] = {
33 [BLOBLISTT_NONE] = "(none)",
34 [BLOBLISTT_EC_HOSTEVENT] = "EC host event",
35 [BLOBLISTT_SPL_HANDOFF] = "SPL hand-off",
36 [BLOBLISTT_VBOOT_CTX] = "Chrome OS vboot context",
37 [BLOBLISTT_VBOOT_HANDOFF] = "Chrome OS vboot hand-off",
38 [BLOBLISTT_ACPI_GNVS] = "ACPI GNVS",
39 [BLOBLISTT_INTEL_VBT] = "Intel Video-BIOS table",
40 [BLOBLISTT_TPM2_TCG_LOG] = "TPM v2 log space",
41 [BLOBLISTT_TCPA_LOG] = "TPM log space",
42 [BLOBLISTT_ACPI_TABLES] = "ACPI tables for x86",
43 [BLOBLISTT_SMBIOS_TABLES] = "SMBIOS tables for x86",
44 };
45
46 const char *bloblist_tag_name(enum bloblist_tag_t tag)
47 {
48 if (tag < 0 || tag >= BLOBLISTT_COUNT)
49 return "invalid";
50
51 return tag_name[tag];
52 }
53
54 static struct bloblist_rec *bloblist_first_blob(struct bloblist_hdr *hdr)
55 {
56 if (hdr->alloced <= hdr->hdr_size)
57 return NULL;
58 return (struct bloblist_rec *)((void *)hdr + hdr->hdr_size);
59 }
60
61 static ulong bloblist_blob_end_ofs(struct bloblist_hdr *hdr,
62 struct bloblist_rec *rec)
63 {
64 ulong offset;
65
66 offset = (void *)rec - (void *)hdr;
67 offset += rec->hdr_size + ALIGN(rec->size, BLOBLIST_ALIGN);
68
69 return offset;
70 }
71
72 static struct bloblist_rec *bloblist_next_blob(struct bloblist_hdr *hdr,
73 struct bloblist_rec *rec)
74 {
75 ulong offset = bloblist_blob_end_ofs(hdr, rec);
76
77 if (offset >= hdr->alloced)
78 return NULL;
79 return (struct bloblist_rec *)((void *)hdr + offset);
80 }
81
82 #define foreach_rec(_rec, _hdr) \
83 for (_rec = bloblist_first_blob(_hdr); \
84 _rec; \
85 _rec = bloblist_next_blob(_hdr, _rec))
86
87 static struct bloblist_rec *bloblist_findrec(uint tag)
88 {
89 struct bloblist_hdr *hdr = gd->bloblist;
90 struct bloblist_rec *rec;
91
92 if (!hdr)
93 return NULL;
94
95 foreach_rec(rec, hdr) {
96 if (rec->tag == tag)
97 return rec;
98 }
99
100 return NULL;
101 }
102
103 static int bloblist_addrec(uint tag, int size, int align,
104 struct bloblist_rec **recp)
105 {
106 struct bloblist_hdr *hdr = gd->bloblist;
107 struct bloblist_rec *rec;
108 int data_start, new_alloced;
109
110 if (!align)
111 align = BLOBLIST_ALIGN;
112
113 /* Figure out where the new data will start */
114 data_start = map_to_sysmem(hdr) + hdr->alloced + sizeof(*rec);
115
116 /* Align the address and then calculate the offset from ->alloced */
117 data_start = ALIGN(data_start, align) - map_to_sysmem(hdr);
118
119 /* Calculate the new allocated total */
120 new_alloced = data_start + ALIGN(size, align);
121
122 if (new_alloced > hdr->size) {
123 log(LOGC_BLOBLIST, LOGL_ERR,
124 "Failed to allocate %x bytes size=%x, need size=%x\n",
125 size, hdr->size, new_alloced);
126 return log_msg_ret("bloblist add", -ENOSPC);
127 }
128 rec = (void *)hdr + hdr->alloced;
129
130 rec->tag = tag;
131 rec->hdr_size = data_start - hdr->alloced;
132 rec->size = size;
133 rec->spare = 0;
134
135 /* Zero the record data */
136 memset((void *)rec + rec->hdr_size, '\0', rec->size);
137
138 hdr->alloced = new_alloced;
139 *recp = rec;
140
141 return 0;
142 }
143
144 static int bloblist_ensurerec(uint tag, struct bloblist_rec **recp, int size,
145 int align)
146 {
147 struct bloblist_rec *rec;
148
149 rec = bloblist_findrec(tag);
150 if (rec) {
151 if (size && size != rec->size) {
152 *recp = rec;
153 return -ESPIPE;
154 }
155 } else {
156 int ret;
157
158 ret = bloblist_addrec(tag, size, align, &rec);
159 if (ret)
160 return ret;
161 }
162 *recp = rec;
163
164 return 0;
165 }
166
167 void *bloblist_find(uint tag, int size)
168 {
169 struct bloblist_rec *rec;
170
171 rec = bloblist_findrec(tag);
172 if (!rec)
173 return NULL;
174 if (size && size != rec->size)
175 return NULL;
176
177 return (void *)rec + rec->hdr_size;
178 }
179
180 void *bloblist_add(uint tag, int size, int align)
181 {
182 struct bloblist_rec *rec;
183
184 if (bloblist_addrec(tag, size, align, &rec))
185 return NULL;
186
187 return (void *)rec + rec->hdr_size;
188 }
189
190 int bloblist_ensure_size(uint tag, int size, int align, void **blobp)
191 {
192 struct bloblist_rec *rec;
193 int ret;
194
195 ret = bloblist_ensurerec(tag, &rec, size, align);
196 if (ret)
197 return ret;
198 *blobp = (void *)rec + rec->hdr_size;
199
200 return 0;
201 }
202
203 void *bloblist_ensure(uint tag, int size)
204 {
205 struct bloblist_rec *rec;
206
207 if (bloblist_ensurerec(tag, &rec, size, 0))
208 return NULL;
209
210 return (void *)rec + rec->hdr_size;
211 }
212
213 int bloblist_ensure_size_ret(uint tag, int *sizep, void **blobp)
214 {
215 struct bloblist_rec *rec;
216 int ret;
217
218 ret = bloblist_ensurerec(tag, &rec, *sizep, 0);
219 if (ret == -ESPIPE)
220 *sizep = rec->size;
221 else if (ret)
222 return ret;
223 *blobp = (void *)rec + rec->hdr_size;
224
225 return 0;
226 }
227
228 static int bloblist_resize_rec(struct bloblist_hdr *hdr,
229 struct bloblist_rec *rec,
230 int new_size)
231 {
232 int expand_by; /* Number of bytes to expand by (-ve to contract) */
233 int new_alloced; /* New value for @hdr->alloced */
234 ulong next_ofs; /* Offset of the record after @rec */
235
236 expand_by = ALIGN(new_size - rec->size, BLOBLIST_ALIGN);
237 new_alloced = ALIGN(hdr->alloced + expand_by, BLOBLIST_ALIGN);
238 if (new_size < 0) {
239 log(LOGC_BLOBLIST, LOGL_DEBUG,
240 "Attempt to shrink blob size below 0 (%x)\n", new_size);
241 return log_msg_ret("size", -EINVAL);
242 }
243 if (new_alloced > hdr->size) {
244 log(LOGC_BLOBLIST, LOGL_ERR,
245 "Failed to allocate %x bytes size=%x, need size=%x\n",
246 new_size, hdr->size, new_alloced);
247 return log_msg_ret("alloc", -ENOSPC);
248 }
249
250 /* Move the following blobs up or down, if this is not the last */
251 next_ofs = bloblist_blob_end_ofs(hdr, rec);
252 if (next_ofs != hdr->alloced) {
253 memmove((void *)hdr + next_ofs + expand_by,
254 (void *)hdr + next_ofs, new_alloced - next_ofs);
255 }
256 hdr->alloced = new_alloced;
257
258 /* Zero the new part of the blob */
259 if (expand_by > 0) {
260 memset((void *)rec + rec->hdr_size + rec->size, '\0',
261 new_size - rec->size);
262 }
263
264 /* Update the size of this blob */
265 rec->size = new_size;
266
267 return 0;
268 }
269
270 int bloblist_resize(uint tag, int new_size)
271 {
272 struct bloblist_hdr *hdr = gd->bloblist;
273 struct bloblist_rec *rec;
274 int ret;
275
276 rec = bloblist_findrec(tag);
277 if (!rec)
278 return log_msg_ret("find", -ENOENT);
279 ret = bloblist_resize_rec(hdr, rec, new_size);
280 if (ret)
281 return log_msg_ret("resize", ret);
282
283 return 0;
284 }
285
286 static u32 bloblist_calc_chksum(struct bloblist_hdr *hdr)
287 {
288 struct bloblist_rec *rec;
289 u32 chksum;
290
291 chksum = crc32(0, (unsigned char *)hdr,
292 offsetof(struct bloblist_hdr, chksum));
293 foreach_rec(rec, hdr) {
294 chksum = crc32(chksum, (void *)rec, rec->hdr_size);
295 chksum = crc32(chksum, (void *)rec + rec->hdr_size, rec->size);
296 }
297
298 return chksum;
299 }
300
301 int bloblist_new(ulong addr, uint size, uint flags)
302 {
303 struct bloblist_hdr *hdr;
304
305 if (size < sizeof(*hdr))
306 return log_ret(-ENOSPC);
307 if (addr & (BLOBLIST_ALIGN - 1))
308 return log_ret(-EFAULT);
309 hdr = map_sysmem(addr, size);
310 memset(hdr, '\0', sizeof(*hdr));
311 hdr->version = BLOBLIST_VERSION;
312 hdr->hdr_size = sizeof(*hdr);
313 hdr->flags = flags;
314 hdr->magic = BLOBLIST_MAGIC;
315 hdr->size = size;
316 hdr->alloced = hdr->hdr_size;
317 hdr->chksum = 0;
318 gd->bloblist = hdr;
319
320 return 0;
321 }
322
323 int bloblist_check(ulong addr, uint size)
324 {
325 struct bloblist_hdr *hdr;
326 u32 chksum;
327
328 hdr = map_sysmem(addr, sizeof(*hdr));
329 if (hdr->magic != BLOBLIST_MAGIC)
330 return log_msg_ret("Bad magic", -ENOENT);
331 if (hdr->version != BLOBLIST_VERSION)
332 return log_msg_ret("Bad version", -EPROTONOSUPPORT);
333 if (size && hdr->size != size)
334 return log_msg_ret("Bad size", -EFBIG);
335 chksum = bloblist_calc_chksum(hdr);
336 if (hdr->chksum != chksum) {
337 log(LOGC_BLOBLIST, LOGL_ERR, "Checksum %x != %x\n", hdr->chksum,
338 chksum);
339 return log_msg_ret("Bad checksum", -EIO);
340 }
341 gd->bloblist = hdr;
342
343 return 0;
344 }
345
346 int bloblist_finish(void)
347 {
348 struct bloblist_hdr *hdr = gd->bloblist;
349
350 hdr->chksum = bloblist_calc_chksum(hdr);
351
352 return 0;
353 }
354
355 void bloblist_get_stats(ulong *basep, ulong *sizep, ulong *allocedp)
356 {
357 struct bloblist_hdr *hdr = gd->bloblist;
358
359 *basep = map_to_sysmem(gd->bloblist);
360 *sizep = hdr->size;
361 *allocedp = hdr->alloced;
362 }
363
364 static void show_value(const char *prompt, ulong value)
365 {
366 printf("%s:%*s %-5lx ", prompt, 8 - (int)strlen(prompt), "", value);
367 print_size(value, "\n");
368 }
369
370 void bloblist_show_stats(void)
371 {
372 ulong base, size, alloced;
373
374 bloblist_get_stats(&base, &size, &alloced);
375 printf("base: %lx\n", base);
376 show_value("size", size);
377 show_value("alloced", alloced);
378 show_value("free", size - alloced);
379 }
380
381 void bloblist_show_list(void)
382 {
383 struct bloblist_hdr *hdr = gd->bloblist;
384 struct bloblist_rec *rec;
385
386 printf("%-8s %8s Tag Name\n", "Address", "Size");
387 for (rec = bloblist_first_blob(hdr); rec;
388 rec = bloblist_next_blob(hdr, rec)) {
389 printf("%08lx %8x %3d %s\n",
390 (ulong)map_to_sysmem((void *)rec + rec->hdr_size),
391 rec->size, rec->tag, bloblist_tag_name(rec->tag));
392 }
393 }
394
395 void bloblist_reloc(void *to, uint to_size, void *from, uint from_size)
396 {
397 struct bloblist_hdr *hdr;
398
399 memcpy(to, from, from_size);
400 hdr = to;
401 hdr->size = to_size;
402 }
403
404 int bloblist_init(void)
405 {
406 bool expected;
407 int ret = -ENOENT;
408
409 /**
410 * Wed expect to find an existing bloblist in the first phase of U-Boot
411 * that runs
412 */
413 expected = !u_boot_first_phase();
414 if (spl_prev_phase() == PHASE_TPL && !IS_ENABLED(CONFIG_TPL_BLOBLIST))
415 expected = false;
416 if (expected)
417 ret = bloblist_check(CONFIG_BLOBLIST_ADDR,
418 CONFIG_BLOBLIST_SIZE);
419 if (ret) {
420 ulong addr;
421
422 log(LOGC_BLOBLIST, expected ? LOGL_WARNING : LOGL_DEBUG,
423 "Existing bloblist not found: creating new bloblist\n");
424 if (IS_ENABLED(CONFIG_BLOBLIST_ALLOC)) {
425 void *ptr = memalign(BLOBLIST_ALIGN,
426 CONFIG_BLOBLIST_SIZE);
427
428 if (!ptr)
429 return log_msg_ret("alloc", -ENOMEM);
430 addr = map_to_sysmem(ptr);
431 } else {
432 addr = CONFIG_BLOBLIST_ADDR;
433 }
434 ret = bloblist_new(addr, CONFIG_BLOBLIST_SIZE, 0);
435 } else {
436 log(LOGC_BLOBLIST, LOGL_DEBUG, "Found existing bloblist\n");
437 }
438
439 return ret;
440 }