blob: 89b415d61783aeec3ce783a1c428022b13021ce3 [file] [log] [blame]
Simon Glass712bd2d2018-11-15 18:43:50 -07001// 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>
Simon Glass5d2199d2021-11-03 21:09:20 -060010#include <malloc.h>
Simon Glass712bd2d2018-11-15 18:43:50 -070011#include <mapmem.h>
12#include <spl.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070014#include <u-boot/crc.h>
Simon Glass712bd2d2018-11-15 18:43:50 -070015
Simon Glassd7985e62020-09-19 18:49:28 -060016/*
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
Simon Glass712bd2d2018-11-15 18:43:50 -070030DECLARE_GLOBAL_DATA_PTR;
31
Simon Glassb936a972020-09-19 18:49:26 -060032static const char *const tag_name[] = {
33 [BLOBLISTT_NONE] = "(none)",
34 [BLOBLISTT_EC_HOSTEVENT] = "EC host event",
Simon Glass90c1a582022-01-12 19:26:17 -070035 [BLOBLISTT_U_BOOT_SPL_HANDOFF] = "SPL hand-off",
Simon Glassb936a972020-09-19 18:49:26 -060036 [BLOBLISTT_VBOOT_CTX] = "Chrome OS vboot context",
37 [BLOBLISTT_VBOOT_HANDOFF] = "Chrome OS vboot hand-off",
Simon Glass0da2c792021-01-13 20:29:44 -070038 [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",
Simon Glassb936a972020-09-19 18:49:26 -060044};
45
46const 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
54static struct bloblist_rec *bloblist_first_blob(struct bloblist_hdr *hdr)
Simon Glass712bd2d2018-11-15 18:43:50 -070055{
56 if (hdr->alloced <= hdr->hdr_size)
57 return NULL;
58 return (struct bloblist_rec *)((void *)hdr + hdr->hdr_size);
59}
60
Simon Glassf20a1132021-07-05 16:32:53 -060061static ulong bloblist_blob_end_ofs(struct bloblist_hdr *hdr,
62 struct bloblist_rec *rec)
Simon Glass712bd2d2018-11-15 18:43:50 -070063{
64 ulong offset;
65
66 offset = (void *)rec - (void *)hdr;
67 offset += rec->hdr_size + ALIGN(rec->size, BLOBLIST_ALIGN);
Simon Glassf20a1132021-07-05 16:32:53 -060068
69 return offset;
70}
71
72static 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
Simon Glass712bd2d2018-11-15 18:43:50 -070077 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
87static 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
Simon Glass06373972020-09-19 18:49:29 -0600103static int bloblist_addrec(uint tag, int size, int align,
104 struct bloblist_rec **recp)
Simon Glass712bd2d2018-11-15 18:43:50 -0700105{
106 struct bloblist_hdr *hdr = gd->bloblist;
107 struct bloblist_rec *rec;
Simon Glassd7985e62020-09-19 18:49:28 -0600108 int data_start, new_alloced;
Simon Glass712bd2d2018-11-15 18:43:50 -0700109
Simon Glass06373972020-09-19 18:49:29 -0600110 if (!align)
111 align = BLOBLIST_ALIGN;
112
Simon Glassd7985e62020-09-19 18:49:28 -0600113 /* Figure out where the new data will start */
Simon Glass06373972020-09-19 18:49:29 -0600114 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);
Simon Glassd7985e62020-09-19 18:49:28 -0600118
119 /* Calculate the new allocated total */
Simon Glass06373972020-09-19 18:49:29 -0600120 new_alloced = data_start + ALIGN(size, align);
121
Simon Glass3e26de52021-07-05 16:32:55 -0600122 if (new_alloced > hdr->size) {
Simon Glass712bd2d2018-11-15 18:43:50 -0700123 log(LOGC_BLOBLIST, LOGL_ERR,
Simon Glass32ec7c62020-01-27 08:49:51 -0700124 "Failed to allocate %x bytes size=%x, need size=%x\n",
Simon Glass712bd2d2018-11-15 18:43:50 -0700125 size, hdr->size, new_alloced);
126 return log_msg_ret("bloblist add", -ENOSPC);
127 }
128 rec = (void *)hdr + hdr->alloced;
Simon Glass712bd2d2018-11-15 18:43:50 -0700129
130 rec->tag = tag;
Simon Glassd7985e62020-09-19 18:49:28 -0600131 rec->hdr_size = data_start - hdr->alloced;
Simon Glass712bd2d2018-11-15 18:43:50 -0700132 rec->size = size;
133 rec->spare = 0;
Simon Glass3e85ea02020-01-27 08:49:52 -0700134
135 /* Zero the record data */
Simon Glassd7985e62020-09-19 18:49:28 -0600136 memset((void *)rec + rec->hdr_size, '\0', rec->size);
137
138 hdr->alloced = new_alloced;
Simon Glass712bd2d2018-11-15 18:43:50 -0700139 *recp = rec;
140
141 return 0;
142}
143
Simon Glass06373972020-09-19 18:49:29 -0600144static int bloblist_ensurerec(uint tag, struct bloblist_rec **recp, int size,
145 int align)
Simon Glass712bd2d2018-11-15 18:43:50 -0700146{
147 struct bloblist_rec *rec;
148
149 rec = bloblist_findrec(tag);
150 if (rec) {
Simon Glass1e0304e2020-01-27 08:49:50 -0700151 if (size && size != rec->size) {
152 *recp = rec;
Simon Glass712bd2d2018-11-15 18:43:50 -0700153 return -ESPIPE;
Simon Glass1e0304e2020-01-27 08:49:50 -0700154 }
Simon Glass712bd2d2018-11-15 18:43:50 -0700155 } else {
156 int ret;
157
Simon Glass06373972020-09-19 18:49:29 -0600158 ret = bloblist_addrec(tag, size, align, &rec);
Simon Glass712bd2d2018-11-15 18:43:50 -0700159 if (ret)
160 return ret;
161 }
162 *recp = rec;
163
164 return 0;
165}
166
167void *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
Simon Glass06373972020-09-19 18:49:29 -0600180void *bloblist_add(uint tag, int size, int align)
Simon Glass712bd2d2018-11-15 18:43:50 -0700181{
182 struct bloblist_rec *rec;
183
Simon Glass06373972020-09-19 18:49:29 -0600184 if (bloblist_addrec(tag, size, align, &rec))
Simon Glass712bd2d2018-11-15 18:43:50 -0700185 return NULL;
186
Simon Glassd7985e62020-09-19 18:49:28 -0600187 return (void *)rec + rec->hdr_size;
Simon Glass712bd2d2018-11-15 18:43:50 -0700188}
189
Simon Glass06373972020-09-19 18:49:29 -0600190int bloblist_ensure_size(uint tag, int size, int align, void **blobp)
Simon Glass712bd2d2018-11-15 18:43:50 -0700191{
192 struct bloblist_rec *rec;
193 int ret;
194
Simon Glass06373972020-09-19 18:49:29 -0600195 ret = bloblist_ensurerec(tag, &rec, size, align);
Simon Glass712bd2d2018-11-15 18:43:50 -0700196 if (ret)
197 return ret;
198 *blobp = (void *)rec + rec->hdr_size;
199
200 return 0;
201}
202
203void *bloblist_ensure(uint tag, int size)
204{
205 struct bloblist_rec *rec;
206
Simon Glass06373972020-09-19 18:49:29 -0600207 if (bloblist_ensurerec(tag, &rec, size, 0))
Simon Glass712bd2d2018-11-15 18:43:50 -0700208 return NULL;
209
210 return (void *)rec + rec->hdr_size;
211}
212
Simon Glass1e0304e2020-01-27 08:49:50 -0700213int bloblist_ensure_size_ret(uint tag, int *sizep, void **blobp)
214{
215 struct bloblist_rec *rec;
216 int ret;
217
Simon Glass06373972020-09-19 18:49:29 -0600218 ret = bloblist_ensurerec(tag, &rec, *sizep, 0);
Simon Glass1e0304e2020-01-27 08:49:50 -0700219 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
Simon Glassf20a1132021-07-05 16:32:53 -0600228static 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
270int 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
Simon Glass712bd2d2018-11-15 18:43:50 -0700286static 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
301int 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
323int 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
346int 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
Simon Glassb936a972020-09-19 18:49:26 -0600355void 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
364static 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
370void 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
381void 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
Simon Glassab7e7462021-01-13 20:29:43 -0700395void 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
Simon Glass712bd2d2018-11-15 18:43:50 -0700404int 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();
Simon Glassab7e7462021-01-13 20:29:43 -0700414 if (spl_prev_phase() == PHASE_TPL && !IS_ENABLED(CONFIG_TPL_BLOBLIST))
415 expected = false;
Simon Glass712bd2d2018-11-15 18:43:50 -0700416 if (expected)
417 ret = bloblist_check(CONFIG_BLOBLIST_ADDR,
418 CONFIG_BLOBLIST_SIZE);
419 if (ret) {
Simon Glass5d2199d2021-11-03 21:09:20 -0600420 ulong addr;
421
Simon Glass712bd2d2018-11-15 18:43:50 -0700422 log(LOGC_BLOBLIST, expected ? LOGL_WARNING : LOGL_DEBUG,
423 "Existing bloblist not found: creating new bloblist\n");
Simon Glass5d2199d2021-11-03 21:09:20 -0600424 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);
Simon Glass712bd2d2018-11-15 18:43:50 -0700435 } else {
436 log(LOGC_BLOBLIST, LOGL_DEBUG, "Found existing bloblist\n");
437 }
438
439 return ret;
440}