blob: 1290fff850413e603747ac9e84141b014864f28e [file] [log] [blame]
Simon Glass9f407d42018-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>
10#include <mapmem.h>
11#include <spl.h>
Simon Glass401d1c42020-10-30 21:38:53 -060012#include <asm/global_data.h>
Simon Glass3db71102019-11-14 12:57:16 -070013#include <u-boot/crc.h>
Simon Glass9f407d42018-11-15 18:43:50 -070014
Simon Glass751b7c72020-09-19 18:49:28 -060015/*
16 * A bloblist is a single contiguous chunk of memory with a header
17 * (struct bloblist_hdr) and a number of blobs in it.
18 *
19 * Each blob starts on a BLOBLIST_ALIGN boundary relative to the start of the
20 * bloblist and consists of a struct bloblist_rec, some padding to the required
21 * alignment for the blog and then the actual data. The padding ensures that the
22 * start address of the data in each blob is aligned as required. Note that
23 * each blob's *data* is aligned to BLOBLIST_ALIGN regardless of the alignment
24 * of the bloblist itself or the blob header.
25 *
26 * So far, only BLOBLIST_ALIGN alignment is supported.
27 */
28
Simon Glass9f407d42018-11-15 18:43:50 -070029DECLARE_GLOBAL_DATA_PTR;
30
Simon Glass4aed2272020-09-19 18:49:26 -060031static const char *const tag_name[] = {
32 [BLOBLISTT_NONE] = "(none)",
33 [BLOBLISTT_EC_HOSTEVENT] = "EC host event",
34 [BLOBLISTT_SPL_HANDOFF] = "SPL hand-off",
35 [BLOBLISTT_VBOOT_CTX] = "Chrome OS vboot context",
36 [BLOBLISTT_VBOOT_HANDOFF] = "Chrome OS vboot hand-off",
Simon Glass02d7a532021-01-13 20:29:44 -070037 [BLOBLISTT_ACPI_GNVS] = "ACPI GNVS",
38 [BLOBLISTT_INTEL_VBT] = "Intel Video-BIOS table",
39 [BLOBLISTT_TPM2_TCG_LOG] = "TPM v2 log space",
40 [BLOBLISTT_TCPA_LOG] = "TPM log space",
41 [BLOBLISTT_ACPI_TABLES] = "ACPI tables for x86",
42 [BLOBLISTT_SMBIOS_TABLES] = "SMBIOS tables for x86",
Simon Glass4aed2272020-09-19 18:49:26 -060043};
44
45const char *bloblist_tag_name(enum bloblist_tag_t tag)
46{
47 if (tag < 0 || tag >= BLOBLISTT_COUNT)
48 return "invalid";
49
50 return tag_name[tag];
51}
52
53static struct bloblist_rec *bloblist_first_blob(struct bloblist_hdr *hdr)
Simon Glass9f407d42018-11-15 18:43:50 -070054{
55 if (hdr->alloced <= hdr->hdr_size)
56 return NULL;
57 return (struct bloblist_rec *)((void *)hdr + hdr->hdr_size);
58}
59
Simon Glass1fe59372021-07-05 16:32:53 -060060static ulong bloblist_blob_end_ofs(struct bloblist_hdr *hdr,
61 struct bloblist_rec *rec)
Simon Glass9f407d42018-11-15 18:43:50 -070062{
63 ulong offset;
64
65 offset = (void *)rec - (void *)hdr;
66 offset += rec->hdr_size + ALIGN(rec->size, BLOBLIST_ALIGN);
Simon Glass1fe59372021-07-05 16:32:53 -060067
68 return offset;
69}
70
71static struct bloblist_rec *bloblist_next_blob(struct bloblist_hdr *hdr,
72 struct bloblist_rec *rec)
73{
74 ulong offset = bloblist_blob_end_ofs(hdr, rec);
75
Simon Glass9f407d42018-11-15 18:43:50 -070076 if (offset >= hdr->alloced)
77 return NULL;
78 return (struct bloblist_rec *)((void *)hdr + offset);
79}
80
81#define foreach_rec(_rec, _hdr) \
82 for (_rec = bloblist_first_blob(_hdr); \
83 _rec; \
84 _rec = bloblist_next_blob(_hdr, _rec))
85
86static struct bloblist_rec *bloblist_findrec(uint tag)
87{
88 struct bloblist_hdr *hdr = gd->bloblist;
89 struct bloblist_rec *rec;
90
91 if (!hdr)
92 return NULL;
93
94 foreach_rec(rec, hdr) {
95 if (rec->tag == tag)
96 return rec;
97 }
98
99 return NULL;
100}
101
Simon Glass4c1497e2020-09-19 18:49:29 -0600102static int bloblist_addrec(uint tag, int size, int align,
103 struct bloblist_rec **recp)
Simon Glass9f407d42018-11-15 18:43:50 -0700104{
105 struct bloblist_hdr *hdr = gd->bloblist;
106 struct bloblist_rec *rec;
Simon Glass751b7c72020-09-19 18:49:28 -0600107 int data_start, new_alloced;
Simon Glass9f407d42018-11-15 18:43:50 -0700108
Simon Glass4c1497e2020-09-19 18:49:29 -0600109 if (!align)
110 align = BLOBLIST_ALIGN;
111
Simon Glass751b7c72020-09-19 18:49:28 -0600112 /* Figure out where the new data will start */
Simon Glass4c1497e2020-09-19 18:49:29 -0600113 data_start = map_to_sysmem(hdr) + hdr->alloced + sizeof(*rec);
114
115 /* Align the address and then calculate the offset from ->alloced */
116 data_start = ALIGN(data_start, align) - map_to_sysmem(hdr);
Simon Glass751b7c72020-09-19 18:49:28 -0600117
118 /* Calculate the new allocated total */
Simon Glass4c1497e2020-09-19 18:49:29 -0600119 new_alloced = data_start + ALIGN(size, align);
120
Simon Glass1f618d52021-07-05 16:32:55 -0600121 if (new_alloced > hdr->size) {
Simon Glass9f407d42018-11-15 18:43:50 -0700122 log(LOGC_BLOBLIST, LOGL_ERR,
Simon Glass02247c12020-01-27 08:49:51 -0700123 "Failed to allocate %x bytes size=%x, need size=%x\n",
Simon Glass9f407d42018-11-15 18:43:50 -0700124 size, hdr->size, new_alloced);
125 return log_msg_ret("bloblist add", -ENOSPC);
126 }
127 rec = (void *)hdr + hdr->alloced;
Simon Glass9f407d42018-11-15 18:43:50 -0700128
129 rec->tag = tag;
Simon Glass751b7c72020-09-19 18:49:28 -0600130 rec->hdr_size = data_start - hdr->alloced;
Simon Glass9f407d42018-11-15 18:43:50 -0700131 rec->size = size;
132 rec->spare = 0;
Simon Glassb83994d2020-01-27 08:49:52 -0700133
134 /* Zero the record data */
Simon Glass751b7c72020-09-19 18:49:28 -0600135 memset((void *)rec + rec->hdr_size, '\0', rec->size);
136
137 hdr->alloced = new_alloced;
Simon Glass9f407d42018-11-15 18:43:50 -0700138 *recp = rec;
139
140 return 0;
141}
142
Simon Glass4c1497e2020-09-19 18:49:29 -0600143static int bloblist_ensurerec(uint tag, struct bloblist_rec **recp, int size,
144 int align)
Simon Glass9f407d42018-11-15 18:43:50 -0700145{
146 struct bloblist_rec *rec;
147
148 rec = bloblist_findrec(tag);
149 if (rec) {
Simon Glass5b044542020-01-27 08:49:50 -0700150 if (size && size != rec->size) {
151 *recp = rec;
Simon Glass9f407d42018-11-15 18:43:50 -0700152 return -ESPIPE;
Simon Glass5b044542020-01-27 08:49:50 -0700153 }
Simon Glass9f407d42018-11-15 18:43:50 -0700154 } else {
155 int ret;
156
Simon Glass4c1497e2020-09-19 18:49:29 -0600157 ret = bloblist_addrec(tag, size, align, &rec);
Simon Glass9f407d42018-11-15 18:43:50 -0700158 if (ret)
159 return ret;
160 }
161 *recp = rec;
162
163 return 0;
164}
165
166void *bloblist_find(uint tag, int size)
167{
168 struct bloblist_rec *rec;
169
170 rec = bloblist_findrec(tag);
171 if (!rec)
172 return NULL;
173 if (size && size != rec->size)
174 return NULL;
175
176 return (void *)rec + rec->hdr_size;
177}
178
Simon Glass4c1497e2020-09-19 18:49:29 -0600179void *bloblist_add(uint tag, int size, int align)
Simon Glass9f407d42018-11-15 18:43:50 -0700180{
181 struct bloblist_rec *rec;
182
Simon Glass4c1497e2020-09-19 18:49:29 -0600183 if (bloblist_addrec(tag, size, align, &rec))
Simon Glass9f407d42018-11-15 18:43:50 -0700184 return NULL;
185
Simon Glass751b7c72020-09-19 18:49:28 -0600186 return (void *)rec + rec->hdr_size;
Simon Glass9f407d42018-11-15 18:43:50 -0700187}
188
Simon Glass4c1497e2020-09-19 18:49:29 -0600189int bloblist_ensure_size(uint tag, int size, int align, void **blobp)
Simon Glass9f407d42018-11-15 18:43:50 -0700190{
191 struct bloblist_rec *rec;
192 int ret;
193
Simon Glass4c1497e2020-09-19 18:49:29 -0600194 ret = bloblist_ensurerec(tag, &rec, size, align);
Simon Glass9f407d42018-11-15 18:43:50 -0700195 if (ret)
196 return ret;
197 *blobp = (void *)rec + rec->hdr_size;
198
199 return 0;
200}
201
202void *bloblist_ensure(uint tag, int size)
203{
204 struct bloblist_rec *rec;
205
Simon Glass4c1497e2020-09-19 18:49:29 -0600206 if (bloblist_ensurerec(tag, &rec, size, 0))
Simon Glass9f407d42018-11-15 18:43:50 -0700207 return NULL;
208
209 return (void *)rec + rec->hdr_size;
210}
211
Simon Glass5b044542020-01-27 08:49:50 -0700212int bloblist_ensure_size_ret(uint tag, int *sizep, void **blobp)
213{
214 struct bloblist_rec *rec;
215 int ret;
216
Simon Glass4c1497e2020-09-19 18:49:29 -0600217 ret = bloblist_ensurerec(tag, &rec, *sizep, 0);
Simon Glass5b044542020-01-27 08:49:50 -0700218 if (ret == -ESPIPE)
219 *sizep = rec->size;
220 else if (ret)
221 return ret;
222 *blobp = (void *)rec + rec->hdr_size;
223
224 return 0;
225}
226
Simon Glass1fe59372021-07-05 16:32:53 -0600227static int bloblist_resize_rec(struct bloblist_hdr *hdr,
228 struct bloblist_rec *rec,
229 int new_size)
230{
231 int expand_by; /* Number of bytes to expand by (-ve to contract) */
232 int new_alloced; /* New value for @hdr->alloced */
233 ulong next_ofs; /* Offset of the record after @rec */
234
235 expand_by = ALIGN(new_size - rec->size, BLOBLIST_ALIGN);
236 new_alloced = ALIGN(hdr->alloced + expand_by, BLOBLIST_ALIGN);
237 if (new_size < 0) {
238 log(LOGC_BLOBLIST, LOGL_DEBUG,
239 "Attempt to shrink blob size below 0 (%x)\n", new_size);
240 return log_msg_ret("size", -EINVAL);
241 }
242 if (new_alloced > hdr->size) {
243 log(LOGC_BLOBLIST, LOGL_ERR,
244 "Failed to allocate %x bytes size=%x, need size=%x\n",
245 new_size, hdr->size, new_alloced);
246 return log_msg_ret("alloc", -ENOSPC);
247 }
248
249 /* Move the following blobs up or down, if this is not the last */
250 next_ofs = bloblist_blob_end_ofs(hdr, rec);
251 if (next_ofs != hdr->alloced) {
252 memmove((void *)hdr + next_ofs + expand_by,
253 (void *)hdr + next_ofs, new_alloced - next_ofs);
254 }
255 hdr->alloced = new_alloced;
256
257 /* Zero the new part of the blob */
258 if (expand_by > 0) {
259 memset((void *)rec + rec->hdr_size + rec->size, '\0',
260 new_size - rec->size);
261 }
262
263 /* Update the size of this blob */
264 rec->size = new_size;
265
266 return 0;
267}
268
269int bloblist_resize(uint tag, int new_size)
270{
271 struct bloblist_hdr *hdr = gd->bloblist;
272 struct bloblist_rec *rec;
273 int ret;
274
275 rec = bloblist_findrec(tag);
276 if (!rec)
277 return log_msg_ret("find", -ENOENT);
278 ret = bloblist_resize_rec(hdr, rec, new_size);
279 if (ret)
280 return log_msg_ret("resize", ret);
281
282 return 0;
283}
284
Simon Glass9f407d42018-11-15 18:43:50 -0700285static u32 bloblist_calc_chksum(struct bloblist_hdr *hdr)
286{
287 struct bloblist_rec *rec;
288 u32 chksum;
289
290 chksum = crc32(0, (unsigned char *)hdr,
291 offsetof(struct bloblist_hdr, chksum));
292 foreach_rec(rec, hdr) {
293 chksum = crc32(chksum, (void *)rec, rec->hdr_size);
294 chksum = crc32(chksum, (void *)rec + rec->hdr_size, rec->size);
295 }
296
297 return chksum;
298}
299
300int bloblist_new(ulong addr, uint size, uint flags)
301{
302 struct bloblist_hdr *hdr;
303
304 if (size < sizeof(*hdr))
305 return log_ret(-ENOSPC);
306 if (addr & (BLOBLIST_ALIGN - 1))
307 return log_ret(-EFAULT);
308 hdr = map_sysmem(addr, size);
309 memset(hdr, '\0', sizeof(*hdr));
310 hdr->version = BLOBLIST_VERSION;
311 hdr->hdr_size = sizeof(*hdr);
312 hdr->flags = flags;
313 hdr->magic = BLOBLIST_MAGIC;
314 hdr->size = size;
315 hdr->alloced = hdr->hdr_size;
316 hdr->chksum = 0;
317 gd->bloblist = hdr;
318
319 return 0;
320}
321
322int bloblist_check(ulong addr, uint size)
323{
324 struct bloblist_hdr *hdr;
325 u32 chksum;
326
327 hdr = map_sysmem(addr, sizeof(*hdr));
328 if (hdr->magic != BLOBLIST_MAGIC)
329 return log_msg_ret("Bad magic", -ENOENT);
330 if (hdr->version != BLOBLIST_VERSION)
331 return log_msg_ret("Bad version", -EPROTONOSUPPORT);
332 if (size && hdr->size != size)
333 return log_msg_ret("Bad size", -EFBIG);
334 chksum = bloblist_calc_chksum(hdr);
335 if (hdr->chksum != chksum) {
336 log(LOGC_BLOBLIST, LOGL_ERR, "Checksum %x != %x\n", hdr->chksum,
337 chksum);
338 return log_msg_ret("Bad checksum", -EIO);
339 }
340 gd->bloblist = hdr;
341
342 return 0;
343}
344
345int bloblist_finish(void)
346{
347 struct bloblist_hdr *hdr = gd->bloblist;
348
349 hdr->chksum = bloblist_calc_chksum(hdr);
350
351 return 0;
352}
353
Simon Glass4aed2272020-09-19 18:49:26 -0600354void bloblist_get_stats(ulong *basep, ulong *sizep, ulong *allocedp)
355{
356 struct bloblist_hdr *hdr = gd->bloblist;
357
358 *basep = map_to_sysmem(gd->bloblist);
359 *sizep = hdr->size;
360 *allocedp = hdr->alloced;
361}
362
363static void show_value(const char *prompt, ulong value)
364{
365 printf("%s:%*s %-5lx ", prompt, 8 - (int)strlen(prompt), "", value);
366 print_size(value, "\n");
367}
368
369void bloblist_show_stats(void)
370{
371 ulong base, size, alloced;
372
373 bloblist_get_stats(&base, &size, &alloced);
374 printf("base: %lx\n", base);
375 show_value("size", size);
376 show_value("alloced", alloced);
377 show_value("free", size - alloced);
378}
379
380void bloblist_show_list(void)
381{
382 struct bloblist_hdr *hdr = gd->bloblist;
383 struct bloblist_rec *rec;
384
385 printf("%-8s %8s Tag Name\n", "Address", "Size");
386 for (rec = bloblist_first_blob(hdr); rec;
387 rec = bloblist_next_blob(hdr, rec)) {
388 printf("%08lx %8x %3d %s\n",
389 (ulong)map_to_sysmem((void *)rec + rec->hdr_size),
390 rec->size, rec->tag, bloblist_tag_name(rec->tag));
391 }
392}
393
Simon Glass9fe06462021-01-13 20:29:43 -0700394void bloblist_reloc(void *to, uint to_size, void *from, uint from_size)
395{
396 struct bloblist_hdr *hdr;
397
398 memcpy(to, from, from_size);
399 hdr = to;
400 hdr->size = to_size;
401}
402
Simon Glass9f407d42018-11-15 18:43:50 -0700403int bloblist_init(void)
404{
405 bool expected;
406 int ret = -ENOENT;
407
408 /**
409 * Wed expect to find an existing bloblist in the first phase of U-Boot
410 * that runs
411 */
412 expected = !u_boot_first_phase();
Simon Glass9fe06462021-01-13 20:29:43 -0700413 if (spl_prev_phase() == PHASE_TPL && !IS_ENABLED(CONFIG_TPL_BLOBLIST))
414 expected = false;
Simon Glass9f407d42018-11-15 18:43:50 -0700415 if (expected)
416 ret = bloblist_check(CONFIG_BLOBLIST_ADDR,
417 CONFIG_BLOBLIST_SIZE);
418 if (ret) {
419 log(LOGC_BLOBLIST, expected ? LOGL_WARNING : LOGL_DEBUG,
420 "Existing bloblist not found: creating new bloblist\n");
421 ret = bloblist_new(CONFIG_BLOBLIST_ADDR, CONFIG_BLOBLIST_SIZE,
422 0);
423 } else {
424 log(LOGC_BLOBLIST, LOGL_DEBUG, "Found existing bloblist\n");
425 }
426
427 return ret;
428}