blob: b5fea12b69763472eab498a7e7176c808bb599fe [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
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>
11#include <log.h>
Simon Glassd5b6e912021-11-03 21:09:20 -060012#include <malloc.h>
Simon Glass9f407d42018-11-15 18:43:50 -070013#include <mapmem.h>
14#include <spl.h>
Simon Glass401d1c42020-10-30 21:38:53 -060015#include <asm/global_data.h>
Simon Glass3db71102019-11-14 12:57:16 -070016#include <u-boot/crc.h>
Simon Glass9f407d42018-11-15 18:43:50 -070017
Simon Glass751b7c72020-09-19 18:49:28 -060018/*
19 * A bloblist is a single contiguous chunk of memory with a header
20 * (struct bloblist_hdr) and a number of blobs in it.
21 *
22 * Each blob starts on a BLOBLIST_ALIGN boundary relative to the start of the
23 * bloblist and consists of a struct bloblist_rec, some padding to the required
24 * alignment for the blog and then the actual data. The padding ensures that the
25 * start address of the data in each blob is aligned as required. Note that
26 * each blob's *data* is aligned to BLOBLIST_ALIGN regardless of the alignment
27 * of the bloblist itself or the blob header.
28 *
29 * So far, only BLOBLIST_ALIGN alignment is supported.
30 */
31
Simon Glass9f407d42018-11-15 18:43:50 -070032DECLARE_GLOBAL_DATA_PTR;
33
Simon Glassf16ec772022-01-12 19:26:19 -070034static struct tag_name {
35 enum bloblist_tag_t tag;
36 const char *name;
37} tag_name[] = {
38 { BLOBLISTT_NONE, "(none)" },
39
40 /* BLOBLISTT_AREA_FIRMWARE_TOP */
41
42 /* BLOBLISTT_AREA_FIRMWARE */
43 { BLOBLISTT_ACPI_GNVS, "ACPI GNVS" },
44 { BLOBLISTT_INTEL_VBT, "Intel Video-BIOS table" },
45 { BLOBLISTT_TPM2_TCG_LOG, "TPM v2 log space" },
46 { BLOBLISTT_TCPA_LOG, "TPM log space" },
47 { BLOBLISTT_ACPI_TABLES, "ACPI tables for x86" },
48 { BLOBLISTT_SMBIOS_TABLES, "SMBIOS tables for x86" },
49 { BLOBLISTT_VBOOT_CTX, "Chrome OS vboot context" },
50
51 /* BLOBLISTT_PROJECT_AREA */
52 { BLOBLISTT_U_BOOT_SPL_HANDOFF, "SPL hand-off" },
53
54 /* BLOBLISTT_VENDOR_AREA */
Simon Glass4aed2272020-09-19 18:49:26 -060055};
56
57const char *bloblist_tag_name(enum bloblist_tag_t tag)
58{
Simon Glassf16ec772022-01-12 19:26:19 -070059 int i;
Simon Glass4aed2272020-09-19 18:49:26 -060060
Simon Glassf16ec772022-01-12 19:26:19 -070061 for (i = 0; i < ARRAY_SIZE(tag_name); i++) {
62 if (tag_name[i].tag == tag)
63 return tag_name[i].name;
64 }
65
66 return "invalid";
Simon Glass4aed2272020-09-19 18:49:26 -060067}
68
69static struct bloblist_rec *bloblist_first_blob(struct bloblist_hdr *hdr)
Simon Glass9f407d42018-11-15 18:43:50 -070070{
71 if (hdr->alloced <= hdr->hdr_size)
72 return NULL;
73 return (struct bloblist_rec *)((void *)hdr + hdr->hdr_size);
74}
75
Simon Glass1fe59372021-07-05 16:32:53 -060076static ulong bloblist_blob_end_ofs(struct bloblist_hdr *hdr,
77 struct bloblist_rec *rec)
Simon Glass9f407d42018-11-15 18:43:50 -070078{
79 ulong offset;
80
81 offset = (void *)rec - (void *)hdr;
82 offset += rec->hdr_size + ALIGN(rec->size, BLOBLIST_ALIGN);
Simon Glass1fe59372021-07-05 16:32:53 -060083
84 return offset;
85}
86
87static struct bloblist_rec *bloblist_next_blob(struct bloblist_hdr *hdr,
88 struct bloblist_rec *rec)
89{
90 ulong offset = bloblist_blob_end_ofs(hdr, rec);
91
Simon Glass9f407d42018-11-15 18:43:50 -070092 if (offset >= hdr->alloced)
93 return NULL;
94 return (struct bloblist_rec *)((void *)hdr + offset);
95}
96
97#define foreach_rec(_rec, _hdr) \
98 for (_rec = bloblist_first_blob(_hdr); \
99 _rec; \
100 _rec = bloblist_next_blob(_hdr, _rec))
101
102static struct bloblist_rec *bloblist_findrec(uint tag)
103{
104 struct bloblist_hdr *hdr = gd->bloblist;
105 struct bloblist_rec *rec;
106
107 if (!hdr)
108 return NULL;
109
110 foreach_rec(rec, hdr) {
111 if (rec->tag == tag)
112 return rec;
113 }
114
115 return NULL;
116}
117
Simon Glass4c1497e2020-09-19 18:49:29 -0600118static int bloblist_addrec(uint tag, int size, int align,
119 struct bloblist_rec **recp)
Simon Glass9f407d42018-11-15 18:43:50 -0700120{
121 struct bloblist_hdr *hdr = gd->bloblist;
122 struct bloblist_rec *rec;
Simon Glass751b7c72020-09-19 18:49:28 -0600123 int data_start, new_alloced;
Simon Glass9f407d42018-11-15 18:43:50 -0700124
Simon Glass4c1497e2020-09-19 18:49:29 -0600125 if (!align)
126 align = BLOBLIST_ALIGN;
127
Simon Glass751b7c72020-09-19 18:49:28 -0600128 /* Figure out where the new data will start */
Simon Glass4c1497e2020-09-19 18:49:29 -0600129 data_start = map_to_sysmem(hdr) + hdr->alloced + sizeof(*rec);
130
131 /* Align the address and then calculate the offset from ->alloced */
132 data_start = ALIGN(data_start, align) - map_to_sysmem(hdr);
Simon Glass751b7c72020-09-19 18:49:28 -0600133
134 /* Calculate the new allocated total */
Simon Glass4c1497e2020-09-19 18:49:29 -0600135 new_alloced = data_start + ALIGN(size, align);
136
Simon Glass1f618d52021-07-05 16:32:55 -0600137 if (new_alloced > hdr->size) {
Simon Glass1d8bbd72022-01-12 19:26:20 -0700138 log_err("Failed to allocate %x bytes size=%x, need size=%x\n",
139 size, hdr->size, new_alloced);
Simon Glass9f407d42018-11-15 18:43:50 -0700140 return log_msg_ret("bloblist add", -ENOSPC);
141 }
142 rec = (void *)hdr + hdr->alloced;
Simon Glass9f407d42018-11-15 18:43:50 -0700143
144 rec->tag = tag;
Simon Glass751b7c72020-09-19 18:49:28 -0600145 rec->hdr_size = data_start - hdr->alloced;
Simon Glass9f407d42018-11-15 18:43:50 -0700146 rec->size = size;
147 rec->spare = 0;
Simon Glassb83994d2020-01-27 08:49:52 -0700148
149 /* Zero the record data */
Simon Glass751b7c72020-09-19 18:49:28 -0600150 memset((void *)rec + rec->hdr_size, '\0', rec->size);
151
152 hdr->alloced = new_alloced;
Simon Glass9f407d42018-11-15 18:43:50 -0700153 *recp = rec;
154
155 return 0;
156}
157
Simon Glass4c1497e2020-09-19 18:49:29 -0600158static int bloblist_ensurerec(uint tag, struct bloblist_rec **recp, int size,
159 int align)
Simon Glass9f407d42018-11-15 18:43:50 -0700160{
161 struct bloblist_rec *rec;
162
163 rec = bloblist_findrec(tag);
164 if (rec) {
Simon Glass5b044542020-01-27 08:49:50 -0700165 if (size && size != rec->size) {
166 *recp = rec;
Simon Glass9f407d42018-11-15 18:43:50 -0700167 return -ESPIPE;
Simon Glass5b044542020-01-27 08:49:50 -0700168 }
Simon Glass9f407d42018-11-15 18:43:50 -0700169 } else {
170 int ret;
171
Simon Glass4c1497e2020-09-19 18:49:29 -0600172 ret = bloblist_addrec(tag, size, align, &rec);
Simon Glass9f407d42018-11-15 18:43:50 -0700173 if (ret)
174 return ret;
175 }
176 *recp = rec;
177
178 return 0;
179}
180
181void *bloblist_find(uint tag, int size)
182{
183 struct bloblist_rec *rec;
184
185 rec = bloblist_findrec(tag);
186 if (!rec)
187 return NULL;
188 if (size && size != rec->size)
189 return NULL;
190
191 return (void *)rec + rec->hdr_size;
192}
193
Simon Glass4c1497e2020-09-19 18:49:29 -0600194void *bloblist_add(uint tag, int size, int align)
Simon Glass9f407d42018-11-15 18:43:50 -0700195{
196 struct bloblist_rec *rec;
197
Simon Glass4c1497e2020-09-19 18:49:29 -0600198 if (bloblist_addrec(tag, size, align, &rec))
Simon Glass9f407d42018-11-15 18:43:50 -0700199 return NULL;
200
Simon Glass751b7c72020-09-19 18:49:28 -0600201 return (void *)rec + rec->hdr_size;
Simon Glass9f407d42018-11-15 18:43:50 -0700202}
203
Simon Glass4c1497e2020-09-19 18:49:29 -0600204int bloblist_ensure_size(uint tag, int size, int align, void **blobp)
Simon Glass9f407d42018-11-15 18:43:50 -0700205{
206 struct bloblist_rec *rec;
207 int ret;
208
Simon Glass4c1497e2020-09-19 18:49:29 -0600209 ret = bloblist_ensurerec(tag, &rec, size, align);
Simon Glass9f407d42018-11-15 18:43:50 -0700210 if (ret)
211 return ret;
212 *blobp = (void *)rec + rec->hdr_size;
213
214 return 0;
215}
216
217void *bloblist_ensure(uint tag, int size)
218{
219 struct bloblist_rec *rec;
220
Simon Glass4c1497e2020-09-19 18:49:29 -0600221 if (bloblist_ensurerec(tag, &rec, size, 0))
Simon Glass9f407d42018-11-15 18:43:50 -0700222 return NULL;
223
224 return (void *)rec + rec->hdr_size;
225}
226
Simon Glass5b044542020-01-27 08:49:50 -0700227int bloblist_ensure_size_ret(uint tag, int *sizep, void **blobp)
228{
229 struct bloblist_rec *rec;
230 int ret;
231
Simon Glass4c1497e2020-09-19 18:49:29 -0600232 ret = bloblist_ensurerec(tag, &rec, *sizep, 0);
Simon Glass5b044542020-01-27 08:49:50 -0700233 if (ret == -ESPIPE)
234 *sizep = rec->size;
235 else if (ret)
236 return ret;
237 *blobp = (void *)rec + rec->hdr_size;
238
239 return 0;
240}
241
Simon Glass1fe59372021-07-05 16:32:53 -0600242static int bloblist_resize_rec(struct bloblist_hdr *hdr,
243 struct bloblist_rec *rec,
244 int new_size)
245{
246 int expand_by; /* Number of bytes to expand by (-ve to contract) */
247 int new_alloced; /* New value for @hdr->alloced */
248 ulong next_ofs; /* Offset of the record after @rec */
249
250 expand_by = ALIGN(new_size - rec->size, BLOBLIST_ALIGN);
251 new_alloced = ALIGN(hdr->alloced + expand_by, BLOBLIST_ALIGN);
252 if (new_size < 0) {
Simon Glass1d8bbd72022-01-12 19:26:20 -0700253 log_debug("Attempt to shrink blob size below 0 (%x)\n",
254 new_size);
Simon Glass1fe59372021-07-05 16:32:53 -0600255 return log_msg_ret("size", -EINVAL);
256 }
257 if (new_alloced > hdr->size) {
Simon Glass1d8bbd72022-01-12 19:26:20 -0700258 log_err("Failed to allocate %x bytes size=%x, need size=%x\n",
259 new_size, hdr->size, new_alloced);
Simon Glass1fe59372021-07-05 16:32:53 -0600260 return log_msg_ret("alloc", -ENOSPC);
261 }
262
263 /* Move the following blobs up or down, if this is not the last */
264 next_ofs = bloblist_blob_end_ofs(hdr, rec);
265 if (next_ofs != hdr->alloced) {
266 memmove((void *)hdr + next_ofs + expand_by,
267 (void *)hdr + next_ofs, new_alloced - next_ofs);
268 }
269 hdr->alloced = new_alloced;
270
271 /* Zero the new part of the blob */
272 if (expand_by > 0) {
273 memset((void *)rec + rec->hdr_size + rec->size, '\0',
274 new_size - rec->size);
275 }
276
277 /* Update the size of this blob */
278 rec->size = new_size;
279
280 return 0;
281}
282
283int bloblist_resize(uint tag, int new_size)
284{
285 struct bloblist_hdr *hdr = gd->bloblist;
286 struct bloblist_rec *rec;
287 int ret;
288
289 rec = bloblist_findrec(tag);
290 if (!rec)
291 return log_msg_ret("find", -ENOENT);
292 ret = bloblist_resize_rec(hdr, rec, new_size);
293 if (ret)
294 return log_msg_ret("resize", ret);
295
296 return 0;
297}
298
Simon Glass9f407d42018-11-15 18:43:50 -0700299static u32 bloblist_calc_chksum(struct bloblist_hdr *hdr)
300{
301 struct bloblist_rec *rec;
302 u32 chksum;
303
304 chksum = crc32(0, (unsigned char *)hdr,
305 offsetof(struct bloblist_hdr, chksum));
306 foreach_rec(rec, hdr) {
307 chksum = crc32(chksum, (void *)rec, rec->hdr_size);
308 chksum = crc32(chksum, (void *)rec + rec->hdr_size, rec->size);
309 }
310
311 return chksum;
312}
313
314int bloblist_new(ulong addr, uint size, uint flags)
315{
316 struct bloblist_hdr *hdr;
317
318 if (size < sizeof(*hdr))
319 return log_ret(-ENOSPC);
320 if (addr & (BLOBLIST_ALIGN - 1))
321 return log_ret(-EFAULT);
322 hdr = map_sysmem(addr, size);
323 memset(hdr, '\0', sizeof(*hdr));
324 hdr->version = BLOBLIST_VERSION;
325 hdr->hdr_size = sizeof(*hdr);
326 hdr->flags = flags;
327 hdr->magic = BLOBLIST_MAGIC;
328 hdr->size = size;
329 hdr->alloced = hdr->hdr_size;
330 hdr->chksum = 0;
331 gd->bloblist = hdr;
332
333 return 0;
334}
335
336int bloblist_check(ulong addr, uint size)
337{
338 struct bloblist_hdr *hdr;
339 u32 chksum;
340
341 hdr = map_sysmem(addr, sizeof(*hdr));
342 if (hdr->magic != BLOBLIST_MAGIC)
343 return log_msg_ret("Bad magic", -ENOENT);
344 if (hdr->version != BLOBLIST_VERSION)
345 return log_msg_ret("Bad version", -EPROTONOSUPPORT);
346 if (size && hdr->size != size)
347 return log_msg_ret("Bad size", -EFBIG);
348 chksum = bloblist_calc_chksum(hdr);
349 if (hdr->chksum != chksum) {
Simon Glass1d8bbd72022-01-12 19:26:20 -0700350 log_err("Checksum %x != %x\n", hdr->chksum, chksum);
Simon Glass9f407d42018-11-15 18:43:50 -0700351 return log_msg_ret("Bad checksum", -EIO);
352 }
353 gd->bloblist = hdr;
354
355 return 0;
356}
357
358int bloblist_finish(void)
359{
360 struct bloblist_hdr *hdr = gd->bloblist;
361
362 hdr->chksum = bloblist_calc_chksum(hdr);
363
364 return 0;
365}
366
Simon Glass4aed2272020-09-19 18:49:26 -0600367void bloblist_get_stats(ulong *basep, ulong *sizep, ulong *allocedp)
368{
369 struct bloblist_hdr *hdr = gd->bloblist;
370
371 *basep = map_to_sysmem(gd->bloblist);
372 *sizep = hdr->size;
373 *allocedp = hdr->alloced;
374}
375
376static void show_value(const char *prompt, ulong value)
377{
378 printf("%s:%*s %-5lx ", prompt, 8 - (int)strlen(prompt), "", value);
379 print_size(value, "\n");
380}
381
382void bloblist_show_stats(void)
383{
384 ulong base, size, alloced;
385
386 bloblist_get_stats(&base, &size, &alloced);
387 printf("base: %lx\n", base);
388 show_value("size", size);
389 show_value("alloced", alloced);
390 show_value("free", size - alloced);
391}
392
393void bloblist_show_list(void)
394{
395 struct bloblist_hdr *hdr = gd->bloblist;
396 struct bloblist_rec *rec;
397
Simon Glassf16ec772022-01-12 19:26:19 -0700398 printf("%-8s %8s Tag Name\n", "Address", "Size");
Simon Glass4aed2272020-09-19 18:49:26 -0600399 for (rec = bloblist_first_blob(hdr); rec;
400 rec = bloblist_next_blob(hdr, rec)) {
Simon Glassf16ec772022-01-12 19:26:19 -0700401 printf("%08lx %8x %4x %s\n",
Simon Glass4aed2272020-09-19 18:49:26 -0600402 (ulong)map_to_sysmem((void *)rec + rec->hdr_size),
403 rec->size, rec->tag, bloblist_tag_name(rec->tag));
404 }
405}
406
Simon Glass9fe06462021-01-13 20:29:43 -0700407void bloblist_reloc(void *to, uint to_size, void *from, uint from_size)
408{
409 struct bloblist_hdr *hdr;
410
411 memcpy(to, from, from_size);
412 hdr = to;
413 hdr->size = to_size;
414}
415
Simon Glass9f407d42018-11-15 18:43:50 -0700416int bloblist_init(void)
417{
418 bool expected;
419 int ret = -ENOENT;
420
421 /**
422 * Wed expect to find an existing bloblist in the first phase of U-Boot
423 * that runs
424 */
425 expected = !u_boot_first_phase();
Simon Glass9fe06462021-01-13 20:29:43 -0700426 if (spl_prev_phase() == PHASE_TPL && !IS_ENABLED(CONFIG_TPL_BLOBLIST))
427 expected = false;
Simon Glass9f407d42018-11-15 18:43:50 -0700428 if (expected)
429 ret = bloblist_check(CONFIG_BLOBLIST_ADDR,
430 CONFIG_BLOBLIST_SIZE);
431 if (ret) {
Simon Glassd5b6e912021-11-03 21:09:20 -0600432 ulong addr;
433
Simon Glass9f407d42018-11-15 18:43:50 -0700434 log(LOGC_BLOBLIST, expected ? LOGL_WARNING : LOGL_DEBUG,
435 "Existing bloblist not found: creating new bloblist\n");
Simon Glassd5b6e912021-11-03 21:09:20 -0600436 if (IS_ENABLED(CONFIG_BLOBLIST_ALLOC)) {
437 void *ptr = memalign(BLOBLIST_ALIGN,
438 CONFIG_BLOBLIST_SIZE);
439
440 if (!ptr)
441 return log_msg_ret("alloc", -ENOMEM);
442 addr = map_to_sysmem(ptr);
443 } else {
444 addr = CONFIG_BLOBLIST_ADDR;
445 }
446 ret = bloblist_new(addr, CONFIG_BLOBLIST_SIZE, 0);
Simon Glass9f407d42018-11-15 18:43:50 -0700447 } else {
Simon Glass1d8bbd72022-01-12 19:26:20 -0700448 log_debug("Found existing bloblist\n");
Simon Glass9f407d42018-11-15 18:43:50 -0700449 }
450
451 return ret;
452}