blob: a22f6c12b0c74469837e1b16ef303d300ada72fd [file] [log] [blame]
Simon Glass6c9e3d12022-01-12 19:26:25 -07001// SPDX-License-Identifier: GPL-2.0+ BSD-3-Clause
Simon Glass9f407d42018-11-15 18:43:50 -07002/*
3 * Copyright 2018 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
5 */
6
Simon Glass1d8bbd72022-01-12 19:26:20 -07007#define LOG_CATEGORY LOGC_BLOBLIST
8
Simon Glass9f407d42018-11-15 18:43:50 -07009#include <common.h>
10#include <bloblist.h>
Simon Glass4e4bf942022-07-31 12:28:48 -060011#include <display_options.h>
Simon Glass9f407d42018-11-15 18:43:50 -070012#include <log.h>
Simon Glassd5b6e912021-11-03 21:09:20 -060013#include <malloc.h>
Simon Glass9f407d42018-11-15 18:43:50 -070014#include <mapmem.h>
15#include <spl.h>
Simon Glass401d1c42020-10-30 21:38:53 -060016#include <asm/global_data.h>
Simon Glass3db71102019-11-14 12:57:16 -070017#include <u-boot/crc.h>
Simon Glass9f407d42018-11-15 18:43:50 -070018
Simon Glass751b7c72020-09-19 18:49:28 -060019/*
20 * A bloblist is a single contiguous chunk of memory with a header
21 * (struct bloblist_hdr) and a number of blobs in it.
22 *
23 * Each blob starts on a BLOBLIST_ALIGN boundary relative to the start of the
24 * bloblist and consists of a struct bloblist_rec, some padding to the required
25 * alignment for the blog and then the actual data. The padding ensures that the
26 * start address of the data in each blob is aligned as required. Note that
27 * each blob's *data* is aligned to BLOBLIST_ALIGN regardless of the alignment
28 * of the bloblist itself or the blob header.
29 *
30 * So far, only BLOBLIST_ALIGN alignment is supported.
31 */
32
Simon Glass9f407d42018-11-15 18:43:50 -070033DECLARE_GLOBAL_DATA_PTR;
34
Simon Glassf16ec772022-01-12 19:26:19 -070035static struct tag_name {
36 enum bloblist_tag_t tag;
37 const char *name;
38} tag_name[] = {
39 { BLOBLISTT_NONE, "(none)" },
40
41 /* BLOBLISTT_AREA_FIRMWARE_TOP */
42
43 /* BLOBLISTT_AREA_FIRMWARE */
44 { BLOBLISTT_ACPI_GNVS, "ACPI GNVS" },
45 { BLOBLISTT_INTEL_VBT, "Intel Video-BIOS table" },
46 { BLOBLISTT_TPM2_TCG_LOG, "TPM v2 log space" },
47 { BLOBLISTT_TCPA_LOG, "TPM log space" },
48 { BLOBLISTT_ACPI_TABLES, "ACPI tables for x86" },
49 { BLOBLISTT_SMBIOS_TABLES, "SMBIOS tables for x86" },
50 { BLOBLISTT_VBOOT_CTX, "Chrome OS vboot context" },
51
52 /* BLOBLISTT_PROJECT_AREA */
53 { BLOBLISTT_U_BOOT_SPL_HANDOFF, "SPL hand-off" },
Simon Glass14d9f632023-09-26 08:14:52 -060054 { BLOBLISTT_VBE, "VBE" },
Simon Glass03fe79c2023-07-15 21:38:59 -060055 { BLOBLISTT_U_BOOT_VIDEO, "SPL video handoff" },
Simon Glassf16ec772022-01-12 19:26:19 -070056
57 /* BLOBLISTT_VENDOR_AREA */
Simon Glass4aed2272020-09-19 18:49:26 -060058};
59
60const char *bloblist_tag_name(enum bloblist_tag_t tag)
61{
Simon Glassf16ec772022-01-12 19:26:19 -070062 int i;
Simon Glass4aed2272020-09-19 18:49:26 -060063
Simon Glassf16ec772022-01-12 19:26:19 -070064 for (i = 0; i < ARRAY_SIZE(tag_name); i++) {
65 if (tag_name[i].tag == tag)
66 return tag_name[i].name;
67 }
68
69 return "invalid";
Simon Glass4aed2272020-09-19 18:49:26 -060070}
71
72static struct bloblist_rec *bloblist_first_blob(struct bloblist_hdr *hdr)
Simon Glass9f407d42018-11-15 18:43:50 -070073{
74 if (hdr->alloced <= hdr->hdr_size)
75 return NULL;
76 return (struct bloblist_rec *)((void *)hdr + hdr->hdr_size);
77}
78
Simon Glass1fe59372021-07-05 16:32:53 -060079static ulong bloblist_blob_end_ofs(struct bloblist_hdr *hdr,
80 struct bloblist_rec *rec)
Simon Glass9f407d42018-11-15 18:43:50 -070081{
82 ulong offset;
83
84 offset = (void *)rec - (void *)hdr;
85 offset += rec->hdr_size + ALIGN(rec->size, BLOBLIST_ALIGN);
Simon Glass1fe59372021-07-05 16:32:53 -060086
87 return offset;
88}
89
90static struct bloblist_rec *bloblist_next_blob(struct bloblist_hdr *hdr,
91 struct bloblist_rec *rec)
92{
93 ulong offset = bloblist_blob_end_ofs(hdr, rec);
94
Simon Glass9f407d42018-11-15 18:43:50 -070095 if (offset >= hdr->alloced)
96 return NULL;
97 return (struct bloblist_rec *)((void *)hdr + offset);
98}
99
100#define foreach_rec(_rec, _hdr) \
101 for (_rec = bloblist_first_blob(_hdr); \
102 _rec; \
103 _rec = bloblist_next_blob(_hdr, _rec))
104
105static struct bloblist_rec *bloblist_findrec(uint tag)
106{
107 struct bloblist_hdr *hdr = gd->bloblist;
108 struct bloblist_rec *rec;
109
110 if (!hdr)
111 return NULL;
112
113 foreach_rec(rec, hdr) {
114 if (rec->tag == tag)
115 return rec;
116 }
117
118 return NULL;
119}
120
Simon Glass4c1497e2020-09-19 18:49:29 -0600121static int bloblist_addrec(uint tag, int size, int align,
122 struct bloblist_rec **recp)
Simon Glass9f407d42018-11-15 18:43:50 -0700123{
124 struct bloblist_hdr *hdr = gd->bloblist;
125 struct bloblist_rec *rec;
Simon Glass751b7c72020-09-19 18:49:28 -0600126 int data_start, new_alloced;
Simon Glass9f407d42018-11-15 18:43:50 -0700127
Simon Glass4c1497e2020-09-19 18:49:29 -0600128 if (!align)
129 align = BLOBLIST_ALIGN;
130
Simon Glass751b7c72020-09-19 18:49:28 -0600131 /* Figure out where the new data will start */
Simon Glass4c1497e2020-09-19 18:49:29 -0600132 data_start = map_to_sysmem(hdr) + hdr->alloced + sizeof(*rec);
133
134 /* Align the address and then calculate the offset from ->alloced */
135 data_start = ALIGN(data_start, align) - map_to_sysmem(hdr);
Simon Glass751b7c72020-09-19 18:49:28 -0600136
137 /* Calculate the new allocated total */
Simon Glass4c1497e2020-09-19 18:49:29 -0600138 new_alloced = data_start + ALIGN(size, align);
139
Simon Glass1f618d52021-07-05 16:32:55 -0600140 if (new_alloced > hdr->size) {
Simon Glass1d8bbd72022-01-12 19:26:20 -0700141 log_err("Failed to allocate %x bytes size=%x, need size=%x\n",
142 size, hdr->size, new_alloced);
Simon Glass9f407d42018-11-15 18:43:50 -0700143 return log_msg_ret("bloblist add", -ENOSPC);
144 }
145 rec = (void *)hdr + hdr->alloced;
Simon Glass9f407d42018-11-15 18:43:50 -0700146
147 rec->tag = tag;
Simon Glass751b7c72020-09-19 18:49:28 -0600148 rec->hdr_size = data_start - hdr->alloced;
Simon Glass9f407d42018-11-15 18:43:50 -0700149 rec->size = size;
150 rec->spare = 0;
Simon Glassb83994d2020-01-27 08:49:52 -0700151
152 /* Zero the record data */
Simon Glass751b7c72020-09-19 18:49:28 -0600153 memset((void *)rec + rec->hdr_size, '\0', rec->size);
154
155 hdr->alloced = new_alloced;
Simon Glass9f407d42018-11-15 18:43:50 -0700156 *recp = rec;
157
158 return 0;
159}
160
Simon Glass4c1497e2020-09-19 18:49:29 -0600161static int bloblist_ensurerec(uint tag, struct bloblist_rec **recp, int size,
162 int align)
Simon Glass9f407d42018-11-15 18:43:50 -0700163{
164 struct bloblist_rec *rec;
165
166 rec = bloblist_findrec(tag);
167 if (rec) {
Simon Glass5b044542020-01-27 08:49:50 -0700168 if (size && size != rec->size) {
169 *recp = rec;
Simon Glass9f407d42018-11-15 18:43:50 -0700170 return -ESPIPE;
Simon Glass5b044542020-01-27 08:49:50 -0700171 }
Simon Glass9f407d42018-11-15 18:43:50 -0700172 } else {
173 int ret;
174
Simon Glass4c1497e2020-09-19 18:49:29 -0600175 ret = bloblist_addrec(tag, size, align, &rec);
Simon Glass9f407d42018-11-15 18:43:50 -0700176 if (ret)
177 return ret;
178 }
179 *recp = rec;
180
181 return 0;
182}
183
184void *bloblist_find(uint tag, int size)
185{
186 struct bloblist_rec *rec;
187
188 rec = bloblist_findrec(tag);
189 if (!rec)
190 return NULL;
191 if (size && size != rec->size)
192 return NULL;
193
194 return (void *)rec + rec->hdr_size;
195}
196
Simon Glass4c1497e2020-09-19 18:49:29 -0600197void *bloblist_add(uint tag, int size, int align)
Simon Glass9f407d42018-11-15 18:43:50 -0700198{
199 struct bloblist_rec *rec;
200
Simon Glass4c1497e2020-09-19 18:49:29 -0600201 if (bloblist_addrec(tag, size, align, &rec))
Simon Glass9f407d42018-11-15 18:43:50 -0700202 return NULL;
203
Simon Glass751b7c72020-09-19 18:49:28 -0600204 return (void *)rec + rec->hdr_size;
Simon Glass9f407d42018-11-15 18:43:50 -0700205}
206
Simon Glass4c1497e2020-09-19 18:49:29 -0600207int bloblist_ensure_size(uint tag, int size, int align, void **blobp)
Simon Glass9f407d42018-11-15 18:43:50 -0700208{
209 struct bloblist_rec *rec;
210 int ret;
211
Simon Glass4c1497e2020-09-19 18:49:29 -0600212 ret = bloblist_ensurerec(tag, &rec, size, align);
Simon Glass9f407d42018-11-15 18:43:50 -0700213 if (ret)
214 return ret;
215 *blobp = (void *)rec + rec->hdr_size;
216
217 return 0;
218}
219
220void *bloblist_ensure(uint tag, int size)
221{
222 struct bloblist_rec *rec;
223
Simon Glass4c1497e2020-09-19 18:49:29 -0600224 if (bloblist_ensurerec(tag, &rec, size, 0))
Simon Glass9f407d42018-11-15 18:43:50 -0700225 return NULL;
226
227 return (void *)rec + rec->hdr_size;
228}
229
Simon Glass5b044542020-01-27 08:49:50 -0700230int bloblist_ensure_size_ret(uint tag, int *sizep, void **blobp)
231{
232 struct bloblist_rec *rec;
233 int ret;
234
Simon Glass4c1497e2020-09-19 18:49:29 -0600235 ret = bloblist_ensurerec(tag, &rec, *sizep, 0);
Simon Glass5b044542020-01-27 08:49:50 -0700236 if (ret == -ESPIPE)
237 *sizep = rec->size;
238 else if (ret)
239 return ret;
240 *blobp = (void *)rec + rec->hdr_size;
241
242 return 0;
243}
244
Simon Glass1fe59372021-07-05 16:32:53 -0600245static int bloblist_resize_rec(struct bloblist_hdr *hdr,
246 struct bloblist_rec *rec,
247 int new_size)
248{
249 int expand_by; /* Number of bytes to expand by (-ve to contract) */
250 int new_alloced; /* New value for @hdr->alloced */
251 ulong next_ofs; /* Offset of the record after @rec */
252
253 expand_by = ALIGN(new_size - rec->size, BLOBLIST_ALIGN);
254 new_alloced = ALIGN(hdr->alloced + expand_by, BLOBLIST_ALIGN);
255 if (new_size < 0) {
Simon Glass1d8bbd72022-01-12 19:26:20 -0700256 log_debug("Attempt to shrink blob size below 0 (%x)\n",
257 new_size);
Simon Glass1fe59372021-07-05 16:32:53 -0600258 return log_msg_ret("size", -EINVAL);
259 }
260 if (new_alloced > hdr->size) {
Simon Glass1d8bbd72022-01-12 19:26:20 -0700261 log_err("Failed to allocate %x bytes size=%x, need size=%x\n",
262 new_size, hdr->size, new_alloced);
Simon Glass1fe59372021-07-05 16:32:53 -0600263 return log_msg_ret("alloc", -ENOSPC);
264 }
265
266 /* Move the following blobs up or down, if this is not the last */
267 next_ofs = bloblist_blob_end_ofs(hdr, rec);
268 if (next_ofs != hdr->alloced) {
269 memmove((void *)hdr + next_ofs + expand_by,
270 (void *)hdr + next_ofs, new_alloced - next_ofs);
271 }
272 hdr->alloced = new_alloced;
273
274 /* Zero the new part of the blob */
275 if (expand_by > 0) {
276 memset((void *)rec + rec->hdr_size + rec->size, '\0',
277 new_size - rec->size);
278 }
279
280 /* Update the size of this blob */
281 rec->size = new_size;
282
283 return 0;
284}
285
286int bloblist_resize(uint tag, int new_size)
287{
288 struct bloblist_hdr *hdr = gd->bloblist;
289 struct bloblist_rec *rec;
290 int ret;
291
292 rec = bloblist_findrec(tag);
293 if (!rec)
294 return log_msg_ret("find", -ENOENT);
295 ret = bloblist_resize_rec(hdr, rec, new_size);
296 if (ret)
297 return log_msg_ret("resize", ret);
298
299 return 0;
300}
301
Simon Glass9f407d42018-11-15 18:43:50 -0700302static u32 bloblist_calc_chksum(struct bloblist_hdr *hdr)
303{
304 struct bloblist_rec *rec;
305 u32 chksum;
306
307 chksum = crc32(0, (unsigned char *)hdr,
308 offsetof(struct bloblist_hdr, chksum));
309 foreach_rec(rec, hdr) {
310 chksum = crc32(chksum, (void *)rec, rec->hdr_size);
311 chksum = crc32(chksum, (void *)rec + rec->hdr_size, rec->size);
312 }
313
314 return chksum;
315}
316
317int bloblist_new(ulong addr, uint size, uint flags)
318{
319 struct bloblist_hdr *hdr;
320
321 if (size < sizeof(*hdr))
322 return log_ret(-ENOSPC);
323 if (addr & (BLOBLIST_ALIGN - 1))
324 return log_ret(-EFAULT);
325 hdr = map_sysmem(addr, size);
326 memset(hdr, '\0', sizeof(*hdr));
327 hdr->version = BLOBLIST_VERSION;
328 hdr->hdr_size = sizeof(*hdr);
329 hdr->flags = flags;
330 hdr->magic = BLOBLIST_MAGIC;
331 hdr->size = size;
332 hdr->alloced = hdr->hdr_size;
333 hdr->chksum = 0;
334 gd->bloblist = hdr;
335
336 return 0;
337}
338
339int bloblist_check(ulong addr, uint size)
340{
341 struct bloblist_hdr *hdr;
342 u32 chksum;
343
344 hdr = map_sysmem(addr, sizeof(*hdr));
345 if (hdr->magic != BLOBLIST_MAGIC)
346 return log_msg_ret("Bad magic", -ENOENT);
347 if (hdr->version != BLOBLIST_VERSION)
348 return log_msg_ret("Bad version", -EPROTONOSUPPORT);
349 if (size && hdr->size != size)
350 return log_msg_ret("Bad size", -EFBIG);
351 chksum = bloblist_calc_chksum(hdr);
352 if (hdr->chksum != chksum) {
Simon Glass1d8bbd72022-01-12 19:26:20 -0700353 log_err("Checksum %x != %x\n", hdr->chksum, chksum);
Simon Glass9f407d42018-11-15 18:43:50 -0700354 return log_msg_ret("Bad checksum", -EIO);
355 }
356 gd->bloblist = hdr;
357
358 return 0;
359}
360
361int bloblist_finish(void)
362{
363 struct bloblist_hdr *hdr = gd->bloblist;
364
365 hdr->chksum = bloblist_calc_chksum(hdr);
Simon Glass99047f52022-01-12 19:26:22 -0700366 log_debug("Finished bloblist size %lx at %lx\n", (ulong)hdr->size,
367 (ulong)map_to_sysmem(hdr));
Simon Glass9f407d42018-11-15 18:43:50 -0700368
369 return 0;
370}
371
Simon Glasse50a24a2022-01-12 19:26:23 -0700372ulong bloblist_get_base(void)
373{
374 return map_to_sysmem(gd->bloblist);
375}
376
377ulong bloblist_get_size(void)
378{
379 struct bloblist_hdr *hdr = gd->bloblist;
380
381 return hdr->size;
382}
383
Simon Glass4aed2272020-09-19 18:49:26 -0600384void bloblist_get_stats(ulong *basep, ulong *sizep, ulong *allocedp)
385{
386 struct bloblist_hdr *hdr = gd->bloblist;
387
388 *basep = map_to_sysmem(gd->bloblist);
389 *sizep = hdr->size;
390 *allocedp = hdr->alloced;
391}
392
393static void show_value(const char *prompt, ulong value)
394{
395 printf("%s:%*s %-5lx ", prompt, 8 - (int)strlen(prompt), "", value);
396 print_size(value, "\n");
397}
398
399void bloblist_show_stats(void)
400{
401 ulong base, size, alloced;
402
403 bloblist_get_stats(&base, &size, &alloced);
404 printf("base: %lx\n", base);
405 show_value("size", size);
406 show_value("alloced", alloced);
407 show_value("free", size - alloced);
408}
409
410void bloblist_show_list(void)
411{
412 struct bloblist_hdr *hdr = gd->bloblist;
413 struct bloblist_rec *rec;
414
Simon Glassf16ec772022-01-12 19:26:19 -0700415 printf("%-8s %8s Tag Name\n", "Address", "Size");
Simon Glass4aed2272020-09-19 18:49:26 -0600416 for (rec = bloblist_first_blob(hdr); rec;
417 rec = bloblist_next_blob(hdr, rec)) {
Simon Glassf16ec772022-01-12 19:26:19 -0700418 printf("%08lx %8x %4x %s\n",
Simon Glass4aed2272020-09-19 18:49:26 -0600419 (ulong)map_to_sysmem((void *)rec + rec->hdr_size),
420 rec->size, rec->tag, bloblist_tag_name(rec->tag));
421 }
422}
423
Simon Glass9fe06462021-01-13 20:29:43 -0700424void bloblist_reloc(void *to, uint to_size, void *from, uint from_size)
425{
426 struct bloblist_hdr *hdr;
427
428 memcpy(to, from, from_size);
429 hdr = to;
430 hdr->size = to_size;
431}
432
Simon Glass9f407d42018-11-15 18:43:50 -0700433int bloblist_init(void)
434{
Simon Glassce3e75d2022-01-22 05:07:27 -0700435 bool fixed = IS_ENABLED(CONFIG_BLOBLIST_FIXED);
Simon Glass9f407d42018-11-15 18:43:50 -0700436 int ret = -ENOENT;
Simon Glass99047f52022-01-12 19:26:22 -0700437 ulong addr, size;
438 bool expected;
Simon Glass9f407d42018-11-15 18:43:50 -0700439
440 /**
Simon Glassce3e75d2022-01-22 05:07:27 -0700441 * We don't expect to find an existing bloblist in the first phase of
442 * U-Boot that runs. Also we have no way to receive the address of an
443 * allocated bloblist from a previous stage, so it must be at a fixed
444 * address.
Simon Glass9f407d42018-11-15 18:43:50 -0700445 */
Simon Glassce3e75d2022-01-22 05:07:27 -0700446 expected = fixed && !u_boot_first_phase();
Simon Glass9fe06462021-01-13 20:29:43 -0700447 if (spl_prev_phase() == PHASE_TPL && !IS_ENABLED(CONFIG_TPL_BLOBLIST))
448 expected = false;
Simon Glassce3e75d2022-01-22 05:07:27 -0700449 if (fixed)
450 addr = IF_ENABLED_INT(CONFIG_BLOBLIST_FIXED,
451 CONFIG_BLOBLIST_ADDR);
Simon Glass99047f52022-01-12 19:26:22 -0700452 size = CONFIG_BLOBLIST_SIZE;
453 if (expected) {
454 ret = bloblist_check(addr, size);
455 if (ret) {
456 log_warning("Expected bloblist at %lx not found (err=%d)\n",
457 addr, ret);
458 } else {
459 /* Get the real size, if it is not what we expected */
460 size = gd->bloblist->size;
461 }
462 }
Simon Glass9f407d42018-11-15 18:43:50 -0700463 if (ret) {
Simon Glass99047f52022-01-12 19:26:22 -0700464 if (CONFIG_IS_ENABLED(BLOBLIST_ALLOC)) {
465 void *ptr = memalign(BLOBLIST_ALIGN, size);
Simon Glassd5b6e912021-11-03 21:09:20 -0600466
467 if (!ptr)
468 return log_msg_ret("alloc", -ENOMEM);
469 addr = map_to_sysmem(ptr);
Simon Glassce3e75d2022-01-22 05:07:27 -0700470 } else if (!fixed) {
471 return log_msg_ret("!fixed", ret);
Simon Glassd5b6e912021-11-03 21:09:20 -0600472 }
Simon Glass99047f52022-01-12 19:26:22 -0700473 log_debug("Creating new bloblist size %lx at %lx\n", size,
474 addr);
475 ret = bloblist_new(addr, size, 0);
Simon Glass9f407d42018-11-15 18:43:50 -0700476 } else {
Simon Glass99047f52022-01-12 19:26:22 -0700477 log_debug("Found existing bloblist size %lx at %lx\n", size,
478 addr);
Simon Glass9f407d42018-11-15 18:43:50 -0700479 }
Simon Glass3d653182023-09-26 08:14:51 -0600480 if (ret)
481 return log_msg_ret("ini", ret);
482 gd->flags |= GD_FLG_BLOBLIST_READY;
Simon Glass9f407d42018-11-15 18:43:50 -0700483
Simon Glass3d653182023-09-26 08:14:51 -0600484 return 0;
485}
486
487int bloblist_maybe_init(void)
488{
489 if (CONFIG_IS_ENABLED(BLOBLIST) && !(gd->flags & GD_FLG_BLOBLIST_READY))
490 return bloblist_init();
491
492 return 0;
Simon Glass9f407d42018-11-15 18:43:50 -0700493}