blob: c2fd07575fb0b9dbcb31ecd638e40cf5c4491ecf [file] [log] [blame]
Simon Glassbbd7bda2022-01-12 19:26:25 -07001// SPDX-License-Identifier: GPL-2.0+ BSD-3-Clause
Simon Glass712bd2d2018-11-15 18:43:50 -07002/*
3 * Copyright 2018 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
5 */
6
Simon Glass963bf322022-01-12 19:26:20 -07007#define LOG_CATEGORY LOGC_BLOBLIST
8
Simon Glass712bd2d2018-11-15 18:43:50 -07009#include <common.h>
10#include <bloblist.h>
Simon Glass1ab16922022-07-31 12:28:48 -060011#include <display_options.h>
Simon Glass712bd2d2018-11-15 18:43:50 -070012#include <log.h>
Simon Glass5d2199d2021-11-03 21:09:20 -060013#include <malloc.h>
Simon Glass712bd2d2018-11-15 18:43:50 -070014#include <mapmem.h>
15#include <spl.h>
Simon Glass8968c052023-12-27 13:07:05 -080016#include <tables_csum.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060017#include <asm/global_data.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070018#include <u-boot/crc.h>
Simon Glass712bd2d2018-11-15 18:43:50 -070019
Simon Glassd7985e62020-09-19 18:49:28 -060020/*
21 * A bloblist is a single contiguous chunk of memory with a header
22 * (struct bloblist_hdr) and a number of blobs in it.
23 *
24 * Each blob starts on a BLOBLIST_ALIGN boundary relative to the start of the
25 * bloblist and consists of a struct bloblist_rec, some padding to the required
26 * alignment for the blog and then the actual data. The padding ensures that the
27 * start address of the data in each blob is aligned as required. Note that
28 * each blob's *data* is aligned to BLOBLIST_ALIGN regardless of the alignment
29 * of the bloblist itself or the blob header.
Simon Glassd7985e62020-09-19 18:49:28 -060030 */
31
Simon Glass712bd2d2018-11-15 18:43:50 -070032DECLARE_GLOBAL_DATA_PTR;
33
Simon Glassfd20df42022-01-12 19:26:19 -070034static struct tag_name {
35 enum bloblist_tag_t tag;
36 const char *name;
37} tag_name[] = {
Simon Glass871e5d02023-12-27 13:06:59 -080038 { BLOBLISTT_VOID, "(void)" },
Simon Glassfd20df42022-01-12 19:26:19 -070039
40 /* BLOBLISTT_AREA_FIRMWARE_TOP */
Simon Glass871e5d02023-12-27 13:06:59 -080041 { BLOBLISTT_CONTROL_FDT, "Control FDT" },
42 { BLOBLISTT_HOB_BLOCK, "HOB block" },
43 { BLOBLISTT_HOB_LIST, "HOB list" },
44 { BLOBLISTT_ACPI_TABLES, "ACPI tables for x86" },
45 { BLOBLISTT_TPM_EVLOG, "TPM event log defined by TCG EFI" },
46 { BLOBLISTT_TPM_CRB_BASE, "TPM Command Response Buffer address" },
Simon Glassfd20df42022-01-12 19:26:19 -070047
48 /* BLOBLISTT_AREA_FIRMWARE */
Simon Glassfd20df42022-01-12 19:26:19 -070049 { BLOBLISTT_TPM2_TCG_LOG, "TPM v2 log space" },
50 { BLOBLISTT_TCPA_LOG, "TPM log space" },
Simon Glass871e5d02023-12-27 13:06:59 -080051 { BLOBLISTT_ACPI_GNVS, "ACPI GNVS" },
52
53 /* BLOBLISTT_AREA_TF */
54 { BLOBLISTT_OPTEE_PAGABLE_PART, "OP-TEE pagable part" },
55
56 /* BLOBLISTT_AREA_OTHER */
57 { BLOBLISTT_INTEL_VBT, "Intel Video-BIOS table" },
Simon Glassfd20df42022-01-12 19:26:19 -070058 { BLOBLISTT_SMBIOS_TABLES, "SMBIOS tables for x86" },
59 { BLOBLISTT_VBOOT_CTX, "Chrome OS vboot context" },
60
61 /* BLOBLISTT_PROJECT_AREA */
62 { BLOBLISTT_U_BOOT_SPL_HANDOFF, "SPL hand-off" },
Simon Glassd2c642c2023-09-26 08:14:52 -060063 { BLOBLISTT_VBE, "VBE" },
Simon Glass358077b2023-07-15 21:38:59 -060064 { BLOBLISTT_U_BOOT_VIDEO, "SPL video handoff" },
Simon Glassfd20df42022-01-12 19:26:19 -070065
66 /* BLOBLISTT_VENDOR_AREA */
Simon Glassb936a972020-09-19 18:49:26 -060067};
68
69const char *bloblist_tag_name(enum bloblist_tag_t tag)
70{
Simon Glassfd20df42022-01-12 19:26:19 -070071 int i;
72
73 for (i = 0; i < ARRAY_SIZE(tag_name); i++) {
74 if (tag_name[i].tag == tag)
75 return tag_name[i].name;
76 }
Simon Glassb936a972020-09-19 18:49:26 -060077
Simon Glassfd20df42022-01-12 19:26:19 -070078 return "invalid";
Simon Glassb936a972020-09-19 18:49:26 -060079}
80
81static struct bloblist_rec *bloblist_first_blob(struct bloblist_hdr *hdr)
Simon Glass712bd2d2018-11-15 18:43:50 -070082{
Simon Glass39d5ad32023-12-27 13:07:08 -080083 if (hdr->used_size <= hdr->hdr_size)
Simon Glass712bd2d2018-11-15 18:43:50 -070084 return NULL;
85 return (struct bloblist_rec *)((void *)hdr + hdr->hdr_size);
86}
87
Simon Glass01b4fdd2023-12-27 13:07:03 -080088static inline uint rec_hdr_size(struct bloblist_rec *rec)
89{
Simon Glass703a5da2023-12-27 13:07:07 -080090 return (rec->tag_and_hdr_size & BLOBLISTR_HDR_SIZE_MASK) >>
91 BLOBLISTR_HDR_SIZE_SHIFT;
Simon Glass01b4fdd2023-12-27 13:07:03 -080092}
93
94static inline uint rec_tag(struct bloblist_rec *rec)
95{
Simon Glass703a5da2023-12-27 13:07:07 -080096 return (rec->tag_and_hdr_size & BLOBLISTR_TAG_MASK) >>
97 BLOBLISTR_TAG_SHIFT;
Simon Glass01b4fdd2023-12-27 13:07:03 -080098}
99
Simon Glassf20a1132021-07-05 16:32:53 -0600100static ulong bloblist_blob_end_ofs(struct bloblist_hdr *hdr,
101 struct bloblist_rec *rec)
Simon Glass712bd2d2018-11-15 18:43:50 -0700102{
103 ulong offset;
104
105 offset = (void *)rec - (void *)hdr;
Simon Glass703a5da2023-12-27 13:07:07 -0800106 /*
107 * The data section of next TE should start from an address aligned
108 * to 1 << hdr->align_log2.
109 */
110 offset += rec_hdr_size(rec) + rec->size;
111 offset = round_up(offset + rec_hdr_size(rec), 1 << hdr->align_log2);
112 offset -= rec_hdr_size(rec);
Simon Glassf20a1132021-07-05 16:32:53 -0600113
114 return offset;
115}
116
117static struct bloblist_rec *bloblist_next_blob(struct bloblist_hdr *hdr,
118 struct bloblist_rec *rec)
119{
120 ulong offset = bloblist_blob_end_ofs(hdr, rec);
121
Simon Glass39d5ad32023-12-27 13:07:08 -0800122 if (offset >= hdr->used_size)
Simon Glass712bd2d2018-11-15 18:43:50 -0700123 return NULL;
124 return (struct bloblist_rec *)((void *)hdr + offset);
125}
126
127#define foreach_rec(_rec, _hdr) \
128 for (_rec = bloblist_first_blob(_hdr); \
129 _rec; \
130 _rec = bloblist_next_blob(_hdr, _rec))
131
132static struct bloblist_rec *bloblist_findrec(uint tag)
133{
134 struct bloblist_hdr *hdr = gd->bloblist;
135 struct bloblist_rec *rec;
136
137 if (!hdr)
138 return NULL;
139
140 foreach_rec(rec, hdr) {
Simon Glass01b4fdd2023-12-27 13:07:03 -0800141 if (rec_tag(rec) == tag)
Simon Glass712bd2d2018-11-15 18:43:50 -0700142 return rec;
143 }
144
145 return NULL;
146}
147
Simon Glassc2aa4ef2023-12-27 13:07:00 -0800148static int bloblist_addrec(uint tag, int size, int align_log2,
Simon Glass06373972020-09-19 18:49:29 -0600149 struct bloblist_rec **recp)
Simon Glass712bd2d2018-11-15 18:43:50 -0700150{
151 struct bloblist_hdr *hdr = gd->bloblist;
152 struct bloblist_rec *rec;
Simon Glassb560b242023-12-27 13:07:06 -0800153 int data_start, aligned_start, new_alloced;
Simon Glass712bd2d2018-11-15 18:43:50 -0700154
Simon Glassc2aa4ef2023-12-27 13:07:00 -0800155 if (!align_log2)
Simon Glass703a5da2023-12-27 13:07:07 -0800156 align_log2 = BLOBLIST_BLOB_ALIGN_LOG2;
Simon Glass06373972020-09-19 18:49:29 -0600157
Simon Glassd7985e62020-09-19 18:49:28 -0600158 /* Figure out where the new data will start */
Simon Glass39d5ad32023-12-27 13:07:08 -0800159 data_start = map_to_sysmem(hdr) + hdr->used_size + sizeof(*rec);
Simon Glass06373972020-09-19 18:49:29 -0600160
Simon Glass39d5ad32023-12-27 13:07:08 -0800161 /* Align the address and then calculate the offset from used size */
Simon Glassb560b242023-12-27 13:07:06 -0800162 aligned_start = ALIGN(data_start, 1U << align_log2) - data_start;
163
164 /* If we need to create a dummy record, create it */
165 if (aligned_start) {
166 int void_size = aligned_start - sizeof(*rec);
167 struct bloblist_rec *vrec;
168 int ret;
169
170 ret = bloblist_addrec(BLOBLISTT_VOID, void_size, 0, &vrec);
171 if (ret)
172 return log_msg_ret("void", ret);
173
174 /* start the record after that */
Simon Glass39d5ad32023-12-27 13:07:08 -0800175 data_start = map_to_sysmem(hdr) + hdr->used_size + sizeof(*vrec);
Simon Glassb560b242023-12-27 13:07:06 -0800176 }
Simon Glassd7985e62020-09-19 18:49:28 -0600177
178 /* Calculate the new allocated total */
Simon Glassb560b242023-12-27 13:07:06 -0800179 new_alloced = data_start - map_to_sysmem(hdr) +
180 ALIGN(size, 1U << align_log2);
Simon Glass06373972020-09-19 18:49:29 -0600181
Simon Glass39d5ad32023-12-27 13:07:08 -0800182 if (new_alloced > hdr->total_size) {
183 log_err("Failed to allocate %x bytes\n", size);
184 log_err("Used size=%x, total size=%x\n",
185 hdr->used_size, hdr->total_size);
Simon Glass712bd2d2018-11-15 18:43:50 -0700186 return log_msg_ret("bloblist add", -ENOSPC);
187 }
Simon Glass39d5ad32023-12-27 13:07:08 -0800188 rec = (void *)hdr + hdr->used_size;
Simon Glass712bd2d2018-11-15 18:43:50 -0700189
Simon Glass703a5da2023-12-27 13:07:07 -0800190 rec->tag_and_hdr_size = tag | sizeof(*rec) << BLOBLISTR_HDR_SIZE_SHIFT;
Simon Glass712bd2d2018-11-15 18:43:50 -0700191 rec->size = size;
Simon Glass3e85ea02020-01-27 08:49:52 -0700192
193 /* Zero the record data */
Simon Glass01b4fdd2023-12-27 13:07:03 -0800194 memset((void *)rec + rec_hdr_size(rec), '\0', rec->size);
Simon Glassd7985e62020-09-19 18:49:28 -0600195
Simon Glass39d5ad32023-12-27 13:07:08 -0800196 hdr->used_size = new_alloced;
Simon Glass712bd2d2018-11-15 18:43:50 -0700197 *recp = rec;
198
199 return 0;
200}
201
Simon Glass06373972020-09-19 18:49:29 -0600202static int bloblist_ensurerec(uint tag, struct bloblist_rec **recp, int size,
Simon Glassc2aa4ef2023-12-27 13:07:00 -0800203 int align_log2)
Simon Glass712bd2d2018-11-15 18:43:50 -0700204{
205 struct bloblist_rec *rec;
206
207 rec = bloblist_findrec(tag);
208 if (rec) {
Simon Glass1e0304e2020-01-27 08:49:50 -0700209 if (size && size != rec->size) {
210 *recp = rec;
Simon Glass712bd2d2018-11-15 18:43:50 -0700211 return -ESPIPE;
Simon Glass1e0304e2020-01-27 08:49:50 -0700212 }
Simon Glass712bd2d2018-11-15 18:43:50 -0700213 } else {
214 int ret;
215
Simon Glassc2aa4ef2023-12-27 13:07:00 -0800216 ret = bloblist_addrec(tag, size, align_log2, &rec);
Simon Glass712bd2d2018-11-15 18:43:50 -0700217 if (ret)
218 return ret;
219 }
220 *recp = rec;
221
222 return 0;
223}
224
225void *bloblist_find(uint tag, int size)
226{
227 struct bloblist_rec *rec;
228
229 rec = bloblist_findrec(tag);
230 if (!rec)
231 return NULL;
232 if (size && size != rec->size)
233 return NULL;
234
Simon Glass01b4fdd2023-12-27 13:07:03 -0800235 return (void *)rec + rec_hdr_size(rec);
Simon Glass712bd2d2018-11-15 18:43:50 -0700236}
237
Simon Glassc2aa4ef2023-12-27 13:07:00 -0800238void *bloblist_add(uint tag, int size, int align_log2)
Simon Glass712bd2d2018-11-15 18:43:50 -0700239{
240 struct bloblist_rec *rec;
241
Simon Glassc2aa4ef2023-12-27 13:07:00 -0800242 if (bloblist_addrec(tag, size, align_log2, &rec))
Simon Glass712bd2d2018-11-15 18:43:50 -0700243 return NULL;
244
Simon Glass01b4fdd2023-12-27 13:07:03 -0800245 return (void *)rec + rec_hdr_size(rec);
Simon Glass712bd2d2018-11-15 18:43:50 -0700246}
247
Simon Glassc2aa4ef2023-12-27 13:07:00 -0800248int bloblist_ensure_size(uint tag, int size, int align_log2, void **blobp)
Simon Glass712bd2d2018-11-15 18:43:50 -0700249{
250 struct bloblist_rec *rec;
251 int ret;
252
Simon Glassc2aa4ef2023-12-27 13:07:00 -0800253 ret = bloblist_ensurerec(tag, &rec, size, align_log2);
Simon Glass712bd2d2018-11-15 18:43:50 -0700254 if (ret)
255 return ret;
Simon Glass01b4fdd2023-12-27 13:07:03 -0800256 *blobp = (void *)rec + rec_hdr_size(rec);
Simon Glass712bd2d2018-11-15 18:43:50 -0700257
258 return 0;
259}
260
261void *bloblist_ensure(uint tag, int size)
262{
263 struct bloblist_rec *rec;
264
Simon Glass06373972020-09-19 18:49:29 -0600265 if (bloblist_ensurerec(tag, &rec, size, 0))
Simon Glass712bd2d2018-11-15 18:43:50 -0700266 return NULL;
267
Simon Glass01b4fdd2023-12-27 13:07:03 -0800268 return (void *)rec + rec_hdr_size(rec);
Simon Glass712bd2d2018-11-15 18:43:50 -0700269}
270
Simon Glass1e0304e2020-01-27 08:49:50 -0700271int bloblist_ensure_size_ret(uint tag, int *sizep, void **blobp)
272{
273 struct bloblist_rec *rec;
274 int ret;
275
Simon Glass06373972020-09-19 18:49:29 -0600276 ret = bloblist_ensurerec(tag, &rec, *sizep, 0);
Simon Glass1e0304e2020-01-27 08:49:50 -0700277 if (ret == -ESPIPE)
278 *sizep = rec->size;
279 else if (ret)
280 return ret;
Simon Glass01b4fdd2023-12-27 13:07:03 -0800281 *blobp = (void *)rec + rec_hdr_size(rec);
Simon Glass1e0304e2020-01-27 08:49:50 -0700282
283 return 0;
284}
285
Simon Glassf20a1132021-07-05 16:32:53 -0600286static int bloblist_resize_rec(struct bloblist_hdr *hdr,
287 struct bloblist_rec *rec,
288 int new_size)
289{
290 int expand_by; /* Number of bytes to expand by (-ve to contract) */
Simon Glass39d5ad32023-12-27 13:07:08 -0800291 int new_alloced;
Simon Glassf20a1132021-07-05 16:32:53 -0600292 ulong next_ofs; /* Offset of the record after @rec */
293
Simon Glass703a5da2023-12-27 13:07:07 -0800294 expand_by = ALIGN(new_size - rec->size, BLOBLIST_BLOB_ALIGN);
Simon Glass39d5ad32023-12-27 13:07:08 -0800295 new_alloced = ALIGN(hdr->used_size + expand_by, BLOBLIST_BLOB_ALIGN);
Simon Glassf20a1132021-07-05 16:32:53 -0600296 if (new_size < 0) {
Simon Glass963bf322022-01-12 19:26:20 -0700297 log_debug("Attempt to shrink blob size below 0 (%x)\n",
298 new_size);
Simon Glassf20a1132021-07-05 16:32:53 -0600299 return log_msg_ret("size", -EINVAL);
300 }
Simon Glass39d5ad32023-12-27 13:07:08 -0800301 if (new_alloced > hdr->total_size) {
302 log_err("Failed to allocate %x bytes\n", new_size);
303 log_err("Used size=%x, total size=%x\n",
304 hdr->used_size, hdr->total_size);
Simon Glassf20a1132021-07-05 16:32:53 -0600305 return log_msg_ret("alloc", -ENOSPC);
306 }
307
308 /* Move the following blobs up or down, if this is not the last */
309 next_ofs = bloblist_blob_end_ofs(hdr, rec);
Simon Glass39d5ad32023-12-27 13:07:08 -0800310 if (next_ofs != hdr->used_size) {
Simon Glassf20a1132021-07-05 16:32:53 -0600311 memmove((void *)hdr + next_ofs + expand_by,
312 (void *)hdr + next_ofs, new_alloced - next_ofs);
313 }
Simon Glass39d5ad32023-12-27 13:07:08 -0800314 hdr->used_size = new_alloced;
Simon Glassf20a1132021-07-05 16:32:53 -0600315
316 /* Zero the new part of the blob */
317 if (expand_by > 0) {
Simon Glass01b4fdd2023-12-27 13:07:03 -0800318 memset((void *)rec + rec_hdr_size(rec) + rec->size, '\0',
Simon Glassf20a1132021-07-05 16:32:53 -0600319 new_size - rec->size);
320 }
321
322 /* Update the size of this blob */
323 rec->size = new_size;
324
325 return 0;
326}
327
328int bloblist_resize(uint tag, int new_size)
329{
330 struct bloblist_hdr *hdr = gd->bloblist;
331 struct bloblist_rec *rec;
332 int ret;
333
334 rec = bloblist_findrec(tag);
335 if (!rec)
336 return log_msg_ret("find", -ENOENT);
337 ret = bloblist_resize_rec(hdr, rec, new_size);
338 if (ret)
339 return log_msg_ret("resize", ret);
340
341 return 0;
342}
343
Simon Glass712bd2d2018-11-15 18:43:50 -0700344static u32 bloblist_calc_chksum(struct bloblist_hdr *hdr)
345{
Simon Glass8968c052023-12-27 13:07:05 -0800346 u8 chksum;
Simon Glass712bd2d2018-11-15 18:43:50 -0700347
Simon Glass39d5ad32023-12-27 13:07:08 -0800348 chksum = table_compute_checksum(hdr, hdr->used_size);
Simon Glass8968c052023-12-27 13:07:05 -0800349 chksum += hdr->chksum;
Simon Glass712bd2d2018-11-15 18:43:50 -0700350
351 return chksum;
352}
353
Simon Glassaee5a9c2023-12-27 13:07:09 -0800354int bloblist_new(ulong addr, uint size, uint flags, uint align_log2)
Simon Glass712bd2d2018-11-15 18:43:50 -0700355{
356 struct bloblist_hdr *hdr;
357
358 if (size < sizeof(*hdr))
359 return log_ret(-ENOSPC);
360 if (addr & (BLOBLIST_ALIGN - 1))
361 return log_ret(-EFAULT);
362 hdr = map_sysmem(addr, size);
363 memset(hdr, '\0', sizeof(*hdr));
364 hdr->version = BLOBLIST_VERSION;
365 hdr->hdr_size = sizeof(*hdr);
366 hdr->flags = flags;
367 hdr->magic = BLOBLIST_MAGIC;
Simon Glass39d5ad32023-12-27 13:07:08 -0800368 hdr->used_size = hdr->hdr_size;
369 hdr->total_size = size;
Simon Glassaee5a9c2023-12-27 13:07:09 -0800370 hdr->align_log2 = align_log2 ? align_log2 : BLOBLIST_BLOB_ALIGN_LOG2;
Simon Glass712bd2d2018-11-15 18:43:50 -0700371 hdr->chksum = 0;
372 gd->bloblist = hdr;
373
374 return 0;
375}
376
377int bloblist_check(ulong addr, uint size)
378{
379 struct bloblist_hdr *hdr;
380 u32 chksum;
381
382 hdr = map_sysmem(addr, sizeof(*hdr));
383 if (hdr->magic != BLOBLIST_MAGIC)
384 return log_msg_ret("Bad magic", -ENOENT);
385 if (hdr->version != BLOBLIST_VERSION)
386 return log_msg_ret("Bad version", -EPROTONOSUPPORT);
Raymond Maobc7f0ab2024-02-03 08:36:21 -0800387 if (!hdr->total_size || (size && hdr->total_size > size))
Simon Glass39d5ad32023-12-27 13:07:08 -0800388 return log_msg_ret("Bad total size", -EFBIG);
389 if (hdr->used_size > hdr->total_size)
390 return log_msg_ret("Bad used size", -ENOENT);
391 if (hdr->hdr_size != sizeof(struct bloblist_hdr))
392 return log_msg_ret("Bad header size", -ENOENT);
393
Simon Glass712bd2d2018-11-15 18:43:50 -0700394 chksum = bloblist_calc_chksum(hdr);
395 if (hdr->chksum != chksum) {
Simon Glass963bf322022-01-12 19:26:20 -0700396 log_err("Checksum %x != %x\n", hdr->chksum, chksum);
Simon Glass712bd2d2018-11-15 18:43:50 -0700397 return log_msg_ret("Bad checksum", -EIO);
398 }
399 gd->bloblist = hdr;
400
401 return 0;
402}
403
404int bloblist_finish(void)
405{
406 struct bloblist_hdr *hdr = gd->bloblist;
407
408 hdr->chksum = bloblist_calc_chksum(hdr);
Simon Glass39d5ad32023-12-27 13:07:08 -0800409 log_debug("Finished bloblist size %lx at %lx\n", (ulong)hdr->used_size,
Simon Glassc6fcb942022-01-12 19:26:22 -0700410 (ulong)map_to_sysmem(hdr));
Simon Glass712bd2d2018-11-15 18:43:50 -0700411
412 return 0;
413}
414
Simon Glass01fd56a2022-01-12 19:26:23 -0700415ulong bloblist_get_base(void)
416{
417 return map_to_sysmem(gd->bloblist);
418}
419
420ulong bloblist_get_size(void)
421{
422 struct bloblist_hdr *hdr = gd->bloblist;
423
Simon Glass39d5ad32023-12-27 13:07:08 -0800424 return hdr->used_size;
425}
426
427ulong bloblist_get_total_size(void)
428{
429 struct bloblist_hdr *hdr = gd->bloblist;
430
431 return hdr->total_size;
Simon Glass01fd56a2022-01-12 19:26:23 -0700432}
433
Simon Glass39d5ad32023-12-27 13:07:08 -0800434void bloblist_get_stats(ulong *basep, ulong *tsizep, ulong *usizep)
Simon Glassb936a972020-09-19 18:49:26 -0600435{
436 struct bloblist_hdr *hdr = gd->bloblist;
437
438 *basep = map_to_sysmem(gd->bloblist);
Simon Glass39d5ad32023-12-27 13:07:08 -0800439 *tsizep = hdr->total_size;
440 *usizep = hdr->used_size;
Simon Glassb936a972020-09-19 18:49:26 -0600441}
442
443static void show_value(const char *prompt, ulong value)
444{
Simon Glass39d5ad32023-12-27 13:07:08 -0800445 printf("%s:%*s %-5lx ", prompt, 10 - (int)strlen(prompt), "", value);
Simon Glassb936a972020-09-19 18:49:26 -0600446 print_size(value, "\n");
447}
448
449void bloblist_show_stats(void)
450{
Simon Glass39d5ad32023-12-27 13:07:08 -0800451 ulong base, tsize, usize;
Simon Glassb936a972020-09-19 18:49:26 -0600452
Simon Glass39d5ad32023-12-27 13:07:08 -0800453 bloblist_get_stats(&base, &tsize, &usize);
454 printf("base: %lx\n", base);
455 show_value("total size", tsize);
456 show_value("used size", usize);
457 show_value("free", tsize - usize);
Simon Glassb936a972020-09-19 18:49:26 -0600458}
459
460void bloblist_show_list(void)
461{
462 struct bloblist_hdr *hdr = gd->bloblist;
463 struct bloblist_rec *rec;
464
Simon Glassfd20df42022-01-12 19:26:19 -0700465 printf("%-8s %8s Tag Name\n", "Address", "Size");
Simon Glassb936a972020-09-19 18:49:26 -0600466 for (rec = bloblist_first_blob(hdr); rec;
467 rec = bloblist_next_blob(hdr, rec)) {
Simon Glassfd20df42022-01-12 19:26:19 -0700468 printf("%08lx %8x %4x %s\n",
Simon Glass01b4fdd2023-12-27 13:07:03 -0800469 (ulong)map_to_sysmem((void *)rec + rec_hdr_size(rec)),
470 rec->size, rec_tag(rec),
471 bloblist_tag_name(rec_tag(rec)));
Simon Glassb936a972020-09-19 18:49:26 -0600472 }
473}
474
Raymond Mao1a99d2c2024-02-03 08:36:22 -0800475int bloblist_reloc(void *to, uint to_size)
Simon Glassab7e7462021-01-13 20:29:43 -0700476{
477 struct bloblist_hdr *hdr;
478
Raymond Mao1a99d2c2024-02-03 08:36:22 -0800479 if (to_size < gd->bloblist->total_size)
480 return -ENOSPC;
481
482 memcpy(to, gd->bloblist, gd->bloblist->total_size);
Simon Glassab7e7462021-01-13 20:29:43 -0700483 hdr = to;
Simon Glass39d5ad32023-12-27 13:07:08 -0800484 hdr->total_size = to_size;
Raymond Mao1a99d2c2024-02-03 08:36:22 -0800485 gd->bloblist = to;
486
487 return 0;
Simon Glassab7e7462021-01-13 20:29:43 -0700488}
489
Simon Glass712bd2d2018-11-15 18:43:50 -0700490int bloblist_init(void)
491{
Simon Glassef70ef72022-01-22 05:07:27 -0700492 bool fixed = IS_ENABLED(CONFIG_BLOBLIST_FIXED);
Simon Glass712bd2d2018-11-15 18:43:50 -0700493 int ret = -ENOENT;
Simon Glassc6fcb942022-01-12 19:26:22 -0700494 ulong addr, size;
495 bool expected;
Simon Glass712bd2d2018-11-15 18:43:50 -0700496
497 /**
Simon Glassef70ef72022-01-22 05:07:27 -0700498 * We don't expect to find an existing bloblist in the first phase of
499 * U-Boot that runs. Also we have no way to receive the address of an
500 * allocated bloblist from a previous stage, so it must be at a fixed
501 * address.
Simon Glass712bd2d2018-11-15 18:43:50 -0700502 */
Simon Glassef70ef72022-01-22 05:07:27 -0700503 expected = fixed && !u_boot_first_phase();
Simon Glassab7e7462021-01-13 20:29:43 -0700504 if (spl_prev_phase() == PHASE_TPL && !IS_ENABLED(CONFIG_TPL_BLOBLIST))
505 expected = false;
Simon Glassef70ef72022-01-22 05:07:27 -0700506 if (fixed)
507 addr = IF_ENABLED_INT(CONFIG_BLOBLIST_FIXED,
508 CONFIG_BLOBLIST_ADDR);
Simon Glassc6fcb942022-01-12 19:26:22 -0700509 size = CONFIG_BLOBLIST_SIZE;
510 if (expected) {
511 ret = bloblist_check(addr, size);
512 if (ret) {
513 log_warning("Expected bloblist at %lx not found (err=%d)\n",
514 addr, ret);
515 } else {
516 /* Get the real size, if it is not what we expected */
Simon Glass39d5ad32023-12-27 13:07:08 -0800517 size = gd->bloblist->total_size;
Simon Glassc6fcb942022-01-12 19:26:22 -0700518 }
519 }
Simon Glass712bd2d2018-11-15 18:43:50 -0700520 if (ret) {
Simon Glassc6fcb942022-01-12 19:26:22 -0700521 if (CONFIG_IS_ENABLED(BLOBLIST_ALLOC)) {
522 void *ptr = memalign(BLOBLIST_ALIGN, size);
Simon Glass5d2199d2021-11-03 21:09:20 -0600523
524 if (!ptr)
525 return log_msg_ret("alloc", -ENOMEM);
526 addr = map_to_sysmem(ptr);
Simon Glassef70ef72022-01-22 05:07:27 -0700527 } else if (!fixed) {
528 return log_msg_ret("!fixed", ret);
Simon Glass5d2199d2021-11-03 21:09:20 -0600529 }
Simon Glassc6fcb942022-01-12 19:26:22 -0700530 log_debug("Creating new bloblist size %lx at %lx\n", size,
531 addr);
Simon Glassaee5a9c2023-12-27 13:07:09 -0800532 ret = bloblist_new(addr, size, 0, 0);
Simon Glass712bd2d2018-11-15 18:43:50 -0700533 } else {
Simon Glassc6fcb942022-01-12 19:26:22 -0700534 log_debug("Found existing bloblist size %lx at %lx\n", size,
535 addr);
Simon Glass712bd2d2018-11-15 18:43:50 -0700536 }
Simon Glassa28f39c2023-09-26 08:14:51 -0600537 if (ret)
538 return log_msg_ret("ini", ret);
539 gd->flags |= GD_FLG_BLOBLIST_READY;
540
541 return 0;
542}
543
544int bloblist_maybe_init(void)
545{
546 if (CONFIG_IS_ENABLED(BLOBLIST) && !(gd->flags & GD_FLG_BLOBLIST_READY))
547 return bloblist_init();
Simon Glass712bd2d2018-11-15 18:43:50 -0700548
Simon Glassa28f39c2023-09-26 08:14:51 -0600549 return 0;
Simon Glass712bd2d2018-11-15 18:43:50 -0700550}
Raymond Maofe016b62024-02-03 08:36:20 -0800551
552int bloblist_check_reg_conv(ulong rfdt, ulong rzero, ulong rsig)
553{
554 if (rzero || rsig != (BLOBLIST_MAGIC | BLOBLIST_REGCONV_VER) ||
555 rfdt != (ulong)bloblist_find(BLOBLISTT_CONTROL_FDT, 0)) {
556 gd->bloblist = NULL; /* Reset the gd bloblist pointer */
557 return -EIO;
558 }
559
560 return 0;
561}