blob: ab48a3cdb98d9217ebfcac7622b25af15c6f9839 [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 <bloblist.h>
Simon Glass1ab16922022-07-31 12:28:48 -060010#include <display_options.h>
Simon Glass712bd2d2018-11-15 18:43:50 -070011#include <log.h>
Simon Glass5d2199d2021-11-03 21:09:20 -060012#include <malloc.h>
Simon Glass712bd2d2018-11-15 18:43:50 -070013#include <mapmem.h>
14#include <spl.h>
Simon Glass8968c052023-12-27 13:07:05 -080015#include <tables_csum.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060016#include <asm/global_data.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070017#include <u-boot/crc.h>
Simon Glass712bd2d2018-11-15 18:43:50 -070018
Simon Glassd7985e62020-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.
Simon Glassd7985e62020-09-19 18:49:28 -060029 */
30
Simon Glass712bd2d2018-11-15 18:43:50 -070031DECLARE_GLOBAL_DATA_PTR;
32
Simon Glassfd20df42022-01-12 19:26:19 -070033static struct tag_name {
34 enum bloblist_tag_t tag;
35 const char *name;
36} tag_name[] = {
Simon Glass871e5d02023-12-27 13:06:59 -080037 { BLOBLISTT_VOID, "(void)" },
Simon Glassfd20df42022-01-12 19:26:19 -070038
39 /* BLOBLISTT_AREA_FIRMWARE_TOP */
Simon Glass871e5d02023-12-27 13:06:59 -080040 { BLOBLISTT_CONTROL_FDT, "Control FDT" },
41 { BLOBLISTT_HOB_BLOCK, "HOB block" },
42 { BLOBLISTT_HOB_LIST, "HOB list" },
43 { BLOBLISTT_ACPI_TABLES, "ACPI tables for x86" },
44 { BLOBLISTT_TPM_EVLOG, "TPM event log defined by TCG EFI" },
45 { BLOBLISTT_TPM_CRB_BASE, "TPM Command Response Buffer address" },
Simon Glassfd20df42022-01-12 19:26:19 -070046
47 /* BLOBLISTT_AREA_FIRMWARE */
Simon Glassfd20df42022-01-12 19:26:19 -070048 { BLOBLISTT_TPM2_TCG_LOG, "TPM v2 log space" },
49 { BLOBLISTT_TCPA_LOG, "TPM log space" },
Simon Glass871e5d02023-12-27 13:06:59 -080050 { BLOBLISTT_ACPI_GNVS, "ACPI GNVS" },
51
52 /* BLOBLISTT_AREA_TF */
53 { BLOBLISTT_OPTEE_PAGABLE_PART, "OP-TEE pagable part" },
54
55 /* BLOBLISTT_AREA_OTHER */
56 { BLOBLISTT_INTEL_VBT, "Intel Video-BIOS table" },
Simon Glassfd20df42022-01-12 19:26:19 -070057 { BLOBLISTT_SMBIOS_TABLES, "SMBIOS tables for x86" },
58 { BLOBLISTT_VBOOT_CTX, "Chrome OS vboot context" },
59
60 /* BLOBLISTT_PROJECT_AREA */
61 { BLOBLISTT_U_BOOT_SPL_HANDOFF, "SPL hand-off" },
Simon Glassd2c642c2023-09-26 08:14:52 -060062 { BLOBLISTT_VBE, "VBE" },
Simon Glass358077b2023-07-15 21:38:59 -060063 { BLOBLISTT_U_BOOT_VIDEO, "SPL video handoff" },
Simon Glassfd20df42022-01-12 19:26:19 -070064
65 /* BLOBLISTT_VENDOR_AREA */
Simon Glassb936a972020-09-19 18:49:26 -060066};
67
68const char *bloblist_tag_name(enum bloblist_tag_t tag)
69{
Simon Glassfd20df42022-01-12 19:26:19 -070070 int i;
71
72 for (i = 0; i < ARRAY_SIZE(tag_name); i++) {
73 if (tag_name[i].tag == tag)
74 return tag_name[i].name;
75 }
Simon Glassb936a972020-09-19 18:49:26 -060076
Simon Glassfd20df42022-01-12 19:26:19 -070077 return "invalid";
Simon Glassb936a972020-09-19 18:49:26 -060078}
79
80static struct bloblist_rec *bloblist_first_blob(struct bloblist_hdr *hdr)
Simon Glass712bd2d2018-11-15 18:43:50 -070081{
Simon Glass39d5ad32023-12-27 13:07:08 -080082 if (hdr->used_size <= hdr->hdr_size)
Simon Glass712bd2d2018-11-15 18:43:50 -070083 return NULL;
84 return (struct bloblist_rec *)((void *)hdr + hdr->hdr_size);
85}
86
Simon Glass01b4fdd2023-12-27 13:07:03 -080087static inline uint rec_hdr_size(struct bloblist_rec *rec)
88{
Simon Glass703a5da2023-12-27 13:07:07 -080089 return (rec->tag_and_hdr_size & BLOBLISTR_HDR_SIZE_MASK) >>
90 BLOBLISTR_HDR_SIZE_SHIFT;
Simon Glass01b4fdd2023-12-27 13:07:03 -080091}
92
93static inline uint rec_tag(struct bloblist_rec *rec)
94{
Simon Glass703a5da2023-12-27 13:07:07 -080095 return (rec->tag_and_hdr_size & BLOBLISTR_TAG_MASK) >>
96 BLOBLISTR_TAG_SHIFT;
Simon Glass01b4fdd2023-12-27 13:07:03 -080097}
98
Simon Glassf20a1132021-07-05 16:32:53 -060099static ulong bloblist_blob_end_ofs(struct bloblist_hdr *hdr,
100 struct bloblist_rec *rec)
Simon Glass712bd2d2018-11-15 18:43:50 -0700101{
102 ulong offset;
103
104 offset = (void *)rec - (void *)hdr;
Simon Glass703a5da2023-12-27 13:07:07 -0800105 /*
106 * The data section of next TE should start from an address aligned
107 * to 1 << hdr->align_log2.
108 */
109 offset += rec_hdr_size(rec) + rec->size;
110 offset = round_up(offset + rec_hdr_size(rec), 1 << hdr->align_log2);
111 offset -= rec_hdr_size(rec);
Simon Glassf20a1132021-07-05 16:32:53 -0600112
113 return offset;
114}
115
116static struct bloblist_rec *bloblist_next_blob(struct bloblist_hdr *hdr,
117 struct bloblist_rec *rec)
118{
119 ulong offset = bloblist_blob_end_ofs(hdr, rec);
120
Simon Glass39d5ad32023-12-27 13:07:08 -0800121 if (offset >= hdr->used_size)
Simon Glass712bd2d2018-11-15 18:43:50 -0700122 return NULL;
123 return (struct bloblist_rec *)((void *)hdr + offset);
124}
125
126#define foreach_rec(_rec, _hdr) \
127 for (_rec = bloblist_first_blob(_hdr); \
128 _rec; \
129 _rec = bloblist_next_blob(_hdr, _rec))
130
131static struct bloblist_rec *bloblist_findrec(uint tag)
132{
133 struct bloblist_hdr *hdr = gd->bloblist;
134 struct bloblist_rec *rec;
135
136 if (!hdr)
137 return NULL;
138
139 foreach_rec(rec, hdr) {
Simon Glass01b4fdd2023-12-27 13:07:03 -0800140 if (rec_tag(rec) == tag)
Simon Glass712bd2d2018-11-15 18:43:50 -0700141 return rec;
142 }
143
144 return NULL;
145}
146
Simon Glassc2aa4ef2023-12-27 13:07:00 -0800147static int bloblist_addrec(uint tag, int size, int align_log2,
Simon Glass06373972020-09-19 18:49:29 -0600148 struct bloblist_rec **recp)
Simon Glass712bd2d2018-11-15 18:43:50 -0700149{
150 struct bloblist_hdr *hdr = gd->bloblist;
151 struct bloblist_rec *rec;
Simon Glassb560b242023-12-27 13:07:06 -0800152 int data_start, aligned_start, new_alloced;
Simon Glass712bd2d2018-11-15 18:43:50 -0700153
Simon Glassc2aa4ef2023-12-27 13:07:00 -0800154 if (!align_log2)
Simon Glass703a5da2023-12-27 13:07:07 -0800155 align_log2 = BLOBLIST_BLOB_ALIGN_LOG2;
Simon Glass06373972020-09-19 18:49:29 -0600156
Simon Glassd7985e62020-09-19 18:49:28 -0600157 /* Figure out where the new data will start */
Simon Glass39d5ad32023-12-27 13:07:08 -0800158 data_start = map_to_sysmem(hdr) + hdr->used_size + sizeof(*rec);
Simon Glass06373972020-09-19 18:49:29 -0600159
Simon Glass39d5ad32023-12-27 13:07:08 -0800160 /* Align the address and then calculate the offset from used size */
Simon Glassb560b242023-12-27 13:07:06 -0800161 aligned_start = ALIGN(data_start, 1U << align_log2) - data_start;
162
163 /* If we need to create a dummy record, create it */
164 if (aligned_start) {
165 int void_size = aligned_start - sizeof(*rec);
166 struct bloblist_rec *vrec;
167 int ret;
168
169 ret = bloblist_addrec(BLOBLISTT_VOID, void_size, 0, &vrec);
170 if (ret)
171 return log_msg_ret("void", ret);
172
173 /* start the record after that */
Simon Glass39d5ad32023-12-27 13:07:08 -0800174 data_start = map_to_sysmem(hdr) + hdr->used_size + sizeof(*vrec);
Simon Glassb560b242023-12-27 13:07:06 -0800175 }
Simon Glassd7985e62020-09-19 18:49:28 -0600176
177 /* Calculate the new allocated total */
Simon Glassb560b242023-12-27 13:07:06 -0800178 new_alloced = data_start - map_to_sysmem(hdr) +
179 ALIGN(size, 1U << align_log2);
Simon Glass06373972020-09-19 18:49:29 -0600180
Simon Glass39d5ad32023-12-27 13:07:08 -0800181 if (new_alloced > hdr->total_size) {
182 log_err("Failed to allocate %x bytes\n", size);
183 log_err("Used size=%x, total size=%x\n",
184 hdr->used_size, hdr->total_size);
Simon Glass712bd2d2018-11-15 18:43:50 -0700185 return log_msg_ret("bloblist add", -ENOSPC);
186 }
Simon Glass39d5ad32023-12-27 13:07:08 -0800187 rec = (void *)hdr + hdr->used_size;
Simon Glass712bd2d2018-11-15 18:43:50 -0700188
Simon Glass703a5da2023-12-27 13:07:07 -0800189 rec->tag_and_hdr_size = tag | sizeof(*rec) << BLOBLISTR_HDR_SIZE_SHIFT;
Simon Glass712bd2d2018-11-15 18:43:50 -0700190 rec->size = size;
Simon Glass3e85ea02020-01-27 08:49:52 -0700191
192 /* Zero the record data */
Simon Glass01b4fdd2023-12-27 13:07:03 -0800193 memset((void *)rec + rec_hdr_size(rec), '\0', rec->size);
Simon Glassd7985e62020-09-19 18:49:28 -0600194
Simon Glass39d5ad32023-12-27 13:07:08 -0800195 hdr->used_size = new_alloced;
Simon Glass712bd2d2018-11-15 18:43:50 -0700196 *recp = rec;
197
198 return 0;
199}
200
Simon Glass06373972020-09-19 18:49:29 -0600201static int bloblist_ensurerec(uint tag, struct bloblist_rec **recp, int size,
Simon Glassc2aa4ef2023-12-27 13:07:00 -0800202 int align_log2)
Simon Glass712bd2d2018-11-15 18:43:50 -0700203{
204 struct bloblist_rec *rec;
205
206 rec = bloblist_findrec(tag);
207 if (rec) {
Simon Glass1e0304e2020-01-27 08:49:50 -0700208 if (size && size != rec->size) {
209 *recp = rec;
Simon Glass712bd2d2018-11-15 18:43:50 -0700210 return -ESPIPE;
Simon Glass1e0304e2020-01-27 08:49:50 -0700211 }
Simon Glass712bd2d2018-11-15 18:43:50 -0700212 } else {
213 int ret;
214
Simon Glassc2aa4ef2023-12-27 13:07:00 -0800215 ret = bloblist_addrec(tag, size, align_log2, &rec);
Simon Glass712bd2d2018-11-15 18:43:50 -0700216 if (ret)
217 return ret;
218 }
219 *recp = rec;
220
221 return 0;
222}
223
224void *bloblist_find(uint tag, int size)
225{
Raymond Maoc6881f12025-01-27 06:49:34 -0800226 void *blob = NULL;
227 int blob_size;
228
229 blob = bloblist_get_blob(tag, &blob_size);
230
231 if (size && size != blob_size)
232 return NULL;
233
234 return blob;
235}
236
237void *bloblist_get_blob(uint tag, int *sizep)
238{
Simon Glass712bd2d2018-11-15 18:43:50 -0700239 struct bloblist_rec *rec;
240
241 rec = bloblist_findrec(tag);
242 if (!rec)
243 return NULL;
Raymond Maoc6881f12025-01-27 06:49:34 -0800244
245 *sizep = rec->size;
Simon Glass712bd2d2018-11-15 18:43:50 -0700246
Simon Glass01b4fdd2023-12-27 13:07:03 -0800247 return (void *)rec + rec_hdr_size(rec);
Simon Glass712bd2d2018-11-15 18:43:50 -0700248}
249
Simon Glassc2aa4ef2023-12-27 13:07:00 -0800250void *bloblist_add(uint tag, int size, int align_log2)
Simon Glass712bd2d2018-11-15 18:43:50 -0700251{
252 struct bloblist_rec *rec;
253
Simon Glassc2aa4ef2023-12-27 13:07:00 -0800254 if (bloblist_addrec(tag, size, align_log2, &rec))
Simon Glass712bd2d2018-11-15 18:43:50 -0700255 return NULL;
256
Simon Glass01b4fdd2023-12-27 13:07:03 -0800257 return (void *)rec + rec_hdr_size(rec);
Simon Glass712bd2d2018-11-15 18:43:50 -0700258}
259
Simon Glassc2aa4ef2023-12-27 13:07:00 -0800260int bloblist_ensure_size(uint tag, int size, int align_log2, void **blobp)
Simon Glass712bd2d2018-11-15 18:43:50 -0700261{
262 struct bloblist_rec *rec;
263 int ret;
264
Simon Glassc2aa4ef2023-12-27 13:07:00 -0800265 ret = bloblist_ensurerec(tag, &rec, size, align_log2);
Simon Glass712bd2d2018-11-15 18:43:50 -0700266 if (ret)
267 return ret;
Simon Glass01b4fdd2023-12-27 13:07:03 -0800268 *blobp = (void *)rec + rec_hdr_size(rec);
Simon Glass712bd2d2018-11-15 18:43:50 -0700269
270 return 0;
271}
272
273void *bloblist_ensure(uint tag, int size)
274{
275 struct bloblist_rec *rec;
276
Simon Glass06373972020-09-19 18:49:29 -0600277 if (bloblist_ensurerec(tag, &rec, size, 0))
Simon Glass712bd2d2018-11-15 18:43:50 -0700278 return NULL;
279
Simon Glass01b4fdd2023-12-27 13:07:03 -0800280 return (void *)rec + rec_hdr_size(rec);
Simon Glass712bd2d2018-11-15 18:43:50 -0700281}
282
Simon Glass1e0304e2020-01-27 08:49:50 -0700283int bloblist_ensure_size_ret(uint tag, int *sizep, void **blobp)
284{
285 struct bloblist_rec *rec;
286 int ret;
287
Simon Glass06373972020-09-19 18:49:29 -0600288 ret = bloblist_ensurerec(tag, &rec, *sizep, 0);
Simon Glass1e0304e2020-01-27 08:49:50 -0700289 if (ret == -ESPIPE)
290 *sizep = rec->size;
291 else if (ret)
292 return ret;
Simon Glass01b4fdd2023-12-27 13:07:03 -0800293 *blobp = (void *)rec + rec_hdr_size(rec);
Simon Glass1e0304e2020-01-27 08:49:50 -0700294
295 return 0;
296}
297
Simon Glassf20a1132021-07-05 16:32:53 -0600298static int bloblist_resize_rec(struct bloblist_hdr *hdr,
299 struct bloblist_rec *rec,
300 int new_size)
301{
302 int expand_by; /* Number of bytes to expand by (-ve to contract) */
Simon Glass39d5ad32023-12-27 13:07:08 -0800303 int new_alloced;
Simon Glassf20a1132021-07-05 16:32:53 -0600304 ulong next_ofs; /* Offset of the record after @rec */
305
Simon Glass703a5da2023-12-27 13:07:07 -0800306 expand_by = ALIGN(new_size - rec->size, BLOBLIST_BLOB_ALIGN);
Simon Glass39d5ad32023-12-27 13:07:08 -0800307 new_alloced = ALIGN(hdr->used_size + expand_by, BLOBLIST_BLOB_ALIGN);
Simon Glassf20a1132021-07-05 16:32:53 -0600308 if (new_size < 0) {
Simon Glass963bf322022-01-12 19:26:20 -0700309 log_debug("Attempt to shrink blob size below 0 (%x)\n",
310 new_size);
Simon Glassf20a1132021-07-05 16:32:53 -0600311 return log_msg_ret("size", -EINVAL);
312 }
Simon Glass39d5ad32023-12-27 13:07:08 -0800313 if (new_alloced > hdr->total_size) {
314 log_err("Failed to allocate %x bytes\n", new_size);
315 log_err("Used size=%x, total size=%x\n",
316 hdr->used_size, hdr->total_size);
Simon Glassf20a1132021-07-05 16:32:53 -0600317 return log_msg_ret("alloc", -ENOSPC);
318 }
319
320 /* Move the following blobs up or down, if this is not the last */
321 next_ofs = bloblist_blob_end_ofs(hdr, rec);
Simon Glass39d5ad32023-12-27 13:07:08 -0800322 if (next_ofs != hdr->used_size) {
Simon Glassf20a1132021-07-05 16:32:53 -0600323 memmove((void *)hdr + next_ofs + expand_by,
324 (void *)hdr + next_ofs, new_alloced - next_ofs);
325 }
Simon Glass39d5ad32023-12-27 13:07:08 -0800326 hdr->used_size = new_alloced;
Simon Glassf20a1132021-07-05 16:32:53 -0600327
328 /* Zero the new part of the blob */
329 if (expand_by > 0) {
Simon Glass01b4fdd2023-12-27 13:07:03 -0800330 memset((void *)rec + rec_hdr_size(rec) + rec->size, '\0',
Simon Glassf20a1132021-07-05 16:32:53 -0600331 new_size - rec->size);
332 }
333
334 /* Update the size of this blob */
335 rec->size = new_size;
336
337 return 0;
338}
339
340int bloblist_resize(uint tag, int new_size)
341{
342 struct bloblist_hdr *hdr = gd->bloblist;
343 struct bloblist_rec *rec;
344 int ret;
345
346 rec = bloblist_findrec(tag);
347 if (!rec)
348 return log_msg_ret("find", -ENOENT);
349 ret = bloblist_resize_rec(hdr, rec, new_size);
350 if (ret)
351 return log_msg_ret("resize", ret);
352
353 return 0;
354}
355
Simon Glass712bd2d2018-11-15 18:43:50 -0700356static u32 bloblist_calc_chksum(struct bloblist_hdr *hdr)
357{
Simon Glass8968c052023-12-27 13:07:05 -0800358 u8 chksum;
Simon Glass712bd2d2018-11-15 18:43:50 -0700359
Simon Glass39d5ad32023-12-27 13:07:08 -0800360 chksum = table_compute_checksum(hdr, hdr->used_size);
Simon Glass8968c052023-12-27 13:07:05 -0800361 chksum += hdr->chksum;
Simon Glass712bd2d2018-11-15 18:43:50 -0700362
363 return chksum;
364}
365
Simon Glassaee5a9c2023-12-27 13:07:09 -0800366int bloblist_new(ulong addr, uint size, uint flags, uint align_log2)
Simon Glass712bd2d2018-11-15 18:43:50 -0700367{
368 struct bloblist_hdr *hdr;
369
370 if (size < sizeof(*hdr))
371 return log_ret(-ENOSPC);
372 if (addr & (BLOBLIST_ALIGN - 1))
373 return log_ret(-EFAULT);
374 hdr = map_sysmem(addr, size);
375 memset(hdr, '\0', sizeof(*hdr));
376 hdr->version = BLOBLIST_VERSION;
377 hdr->hdr_size = sizeof(*hdr);
378 hdr->flags = flags;
379 hdr->magic = BLOBLIST_MAGIC;
Simon Glass39d5ad32023-12-27 13:07:08 -0800380 hdr->used_size = hdr->hdr_size;
381 hdr->total_size = size;
Simon Glassaee5a9c2023-12-27 13:07:09 -0800382 hdr->align_log2 = align_log2 ? align_log2 : BLOBLIST_BLOB_ALIGN_LOG2;
Simon Glass712bd2d2018-11-15 18:43:50 -0700383 hdr->chksum = 0;
384 gd->bloblist = hdr;
385
386 return 0;
387}
388
389int bloblist_check(ulong addr, uint size)
390{
391 struct bloblist_hdr *hdr;
392 u32 chksum;
393
394 hdr = map_sysmem(addr, sizeof(*hdr));
395 if (hdr->magic != BLOBLIST_MAGIC)
396 return log_msg_ret("Bad magic", -ENOENT);
397 if (hdr->version != BLOBLIST_VERSION)
398 return log_msg_ret("Bad version", -EPROTONOSUPPORT);
Raymond Maobc7f0ab2024-02-03 08:36:21 -0800399 if (!hdr->total_size || (size && hdr->total_size > size))
Simon Glass39d5ad32023-12-27 13:07:08 -0800400 return log_msg_ret("Bad total size", -EFBIG);
401 if (hdr->used_size > hdr->total_size)
402 return log_msg_ret("Bad used size", -ENOENT);
403 if (hdr->hdr_size != sizeof(struct bloblist_hdr))
404 return log_msg_ret("Bad header size", -ENOENT);
405
Simon Glass712bd2d2018-11-15 18:43:50 -0700406 chksum = bloblist_calc_chksum(hdr);
407 if (hdr->chksum != chksum) {
Simon Glass963bf322022-01-12 19:26:20 -0700408 log_err("Checksum %x != %x\n", hdr->chksum, chksum);
Simon Glass712bd2d2018-11-15 18:43:50 -0700409 return log_msg_ret("Bad checksum", -EIO);
410 }
411 gd->bloblist = hdr;
412
413 return 0;
414}
415
416int bloblist_finish(void)
417{
418 struct bloblist_hdr *hdr = gd->bloblist;
419
420 hdr->chksum = bloblist_calc_chksum(hdr);
Simon Glass39d5ad32023-12-27 13:07:08 -0800421 log_debug("Finished bloblist size %lx at %lx\n", (ulong)hdr->used_size,
Simon Glassc6fcb942022-01-12 19:26:22 -0700422 (ulong)map_to_sysmem(hdr));
Simon Glass712bd2d2018-11-15 18:43:50 -0700423
424 return 0;
425}
426
Simon Glass01fd56a2022-01-12 19:26:23 -0700427ulong bloblist_get_base(void)
428{
429 return map_to_sysmem(gd->bloblist);
430}
431
432ulong bloblist_get_size(void)
433{
434 struct bloblist_hdr *hdr = gd->bloblist;
435
Simon Glass39d5ad32023-12-27 13:07:08 -0800436 return hdr->used_size;
437}
438
439ulong bloblist_get_total_size(void)
440{
441 struct bloblist_hdr *hdr = gd->bloblist;
442
443 return hdr->total_size;
Simon Glass01fd56a2022-01-12 19:26:23 -0700444}
445
Simon Glass39d5ad32023-12-27 13:07:08 -0800446void bloblist_get_stats(ulong *basep, ulong *tsizep, ulong *usizep)
Simon Glassb936a972020-09-19 18:49:26 -0600447{
448 struct bloblist_hdr *hdr = gd->bloblist;
449
450 *basep = map_to_sysmem(gd->bloblist);
Simon Glass39d5ad32023-12-27 13:07:08 -0800451 *tsizep = hdr->total_size;
452 *usizep = hdr->used_size;
Simon Glassb936a972020-09-19 18:49:26 -0600453}
454
455static void show_value(const char *prompt, ulong value)
456{
Simon Glass39d5ad32023-12-27 13:07:08 -0800457 printf("%s:%*s %-5lx ", prompt, 10 - (int)strlen(prompt), "", value);
Simon Glassb936a972020-09-19 18:49:26 -0600458 print_size(value, "\n");
459}
460
461void bloblist_show_stats(void)
462{
Simon Glass39d5ad32023-12-27 13:07:08 -0800463 ulong base, tsize, usize;
Simon Glassb936a972020-09-19 18:49:26 -0600464
Simon Glass39d5ad32023-12-27 13:07:08 -0800465 bloblist_get_stats(&base, &tsize, &usize);
466 printf("base: %lx\n", base);
467 show_value("total size", tsize);
468 show_value("used size", usize);
469 show_value("free", tsize - usize);
Simon Glassb936a972020-09-19 18:49:26 -0600470}
471
472void bloblist_show_list(void)
473{
474 struct bloblist_hdr *hdr = gd->bloblist;
475 struct bloblist_rec *rec;
476
Simon Glassfd20df42022-01-12 19:26:19 -0700477 printf("%-8s %8s Tag Name\n", "Address", "Size");
Simon Glassb936a972020-09-19 18:49:26 -0600478 for (rec = bloblist_first_blob(hdr); rec;
479 rec = bloblist_next_blob(hdr, rec)) {
Simon Glassfd20df42022-01-12 19:26:19 -0700480 printf("%08lx %8x %4x %s\n",
Simon Glass01b4fdd2023-12-27 13:07:03 -0800481 (ulong)map_to_sysmem((void *)rec + rec_hdr_size(rec)),
482 rec->size, rec_tag(rec),
483 bloblist_tag_name(rec_tag(rec)));
Simon Glassb936a972020-09-19 18:49:26 -0600484 }
485}
486
Raymond Mao1a99d2c2024-02-03 08:36:22 -0800487int bloblist_reloc(void *to, uint to_size)
Simon Glassab7e7462021-01-13 20:29:43 -0700488{
489 struct bloblist_hdr *hdr;
490
Raymond Mao1a99d2c2024-02-03 08:36:22 -0800491 if (to_size < gd->bloblist->total_size)
492 return -ENOSPC;
493
494 memcpy(to, gd->bloblist, gd->bloblist->total_size);
Simon Glassab7e7462021-01-13 20:29:43 -0700495 hdr = to;
Simon Glass39d5ad32023-12-27 13:07:08 -0800496 hdr->total_size = to_size;
Raymond Mao1a99d2c2024-02-03 08:36:22 -0800497 gd->bloblist = to;
498
499 return 0;
Simon Glassab7e7462021-01-13 20:29:43 -0700500}
501
Raymond Mao72c99cc2024-02-03 08:36:26 -0800502/*
503 * Weak default function for getting bloblist from boot args.
504 */
505int __weak xferlist_from_boot_arg(ulong __always_unused addr,
506 ulong __always_unused size)
507{
508 return -ENOENT;
509}
510
Simon Glass712bd2d2018-11-15 18:43:50 -0700511int bloblist_init(void)
512{
Simon Glassef70ef72022-01-22 05:07:27 -0700513 bool fixed = IS_ENABLED(CONFIG_BLOBLIST_FIXED);
Simon Glass712bd2d2018-11-15 18:43:50 -0700514 int ret = -ENOENT;
Patrick Rudolphdba3e132024-10-23 15:20:18 +0200515 ulong addr = 0, size;
Raymond Mao72c99cc2024-02-03 08:36:26 -0800516 /*
517 * If U-Boot is not in the first phase, an existing bloblist must be
518 * at a fixed address.
519 */
Simon Glass3235f1e2024-09-29 19:49:34 -0600520 bool from_addr = fixed && !xpl_is_first_phase();
Raymond Mao72c99cc2024-02-03 08:36:26 -0800521 /*
522 * If U-Boot is in the first phase that an arch custom routine should
523 * install the bloblist passed from previous loader to this fixed
Simon Glassef70ef72022-01-22 05:07:27 -0700524 * address.
Simon Glass712bd2d2018-11-15 18:43:50 -0700525 */
Simon Glass3235f1e2024-09-29 19:49:34 -0600526 bool from_boot_arg = fixed && xpl_is_first_phase();
Raymond Mao72c99cc2024-02-03 08:36:26 -0800527
Simon Glass6b7f9c02024-09-29 19:49:39 -0600528 if (xpl_prev_phase() == PHASE_TPL && !IS_ENABLED(CONFIG_TPL_BLOBLIST))
Raymond Mao72c99cc2024-02-03 08:36:26 -0800529 from_addr = false;
Simon Glassef70ef72022-01-22 05:07:27 -0700530 if (fixed)
531 addr = IF_ENABLED_INT(CONFIG_BLOBLIST_FIXED,
532 CONFIG_BLOBLIST_ADDR);
Simon Glassc6fcb942022-01-12 19:26:22 -0700533 size = CONFIG_BLOBLIST_SIZE;
Raymond Mao72c99cc2024-02-03 08:36:26 -0800534
535 if (from_boot_arg)
536 ret = xferlist_from_boot_arg(addr, size);
537 else if (from_addr)
Simon Glassc6fcb942022-01-12 19:26:22 -0700538 ret = bloblist_check(addr, size);
Raymond Mao72c99cc2024-02-03 08:36:26 -0800539
540 if (ret)
541 log_warning("Bloblist at %lx not found (err=%d)\n",
542 addr, ret);
543 else
544 /* Get the real size */
545 size = gd->bloblist->total_size;
546
Simon Glass712bd2d2018-11-15 18:43:50 -0700547 if (ret) {
Raymond Mao72c99cc2024-02-03 08:36:26 -0800548 /*
549 * If we don't have a bloblist from a fixed address, or the one
550 * in the fixed address is not valid. we must allocate the
551 * memory for it now.
552 */
Simon Glassc6fcb942022-01-12 19:26:22 -0700553 if (CONFIG_IS_ENABLED(BLOBLIST_ALLOC)) {
554 void *ptr = memalign(BLOBLIST_ALIGN, size);
Simon Glass5d2199d2021-11-03 21:09:20 -0600555
556 if (!ptr)
557 return log_msg_ret("alloc", -ENOMEM);
558 addr = map_to_sysmem(ptr);
Simon Glassef70ef72022-01-22 05:07:27 -0700559 } else if (!fixed) {
Raymond Mao72c99cc2024-02-03 08:36:26 -0800560 return log_msg_ret("BLOBLIST_FIXED is not enabled",
561 ret);
Simon Glass5d2199d2021-11-03 21:09:20 -0600562 }
Simon Glassc6fcb942022-01-12 19:26:22 -0700563 log_debug("Creating new bloblist size %lx at %lx\n", size,
564 addr);
Simon Glassaee5a9c2023-12-27 13:07:09 -0800565 ret = bloblist_new(addr, size, 0, 0);
Simon Glass712bd2d2018-11-15 18:43:50 -0700566 } else {
Simon Glassc6fcb942022-01-12 19:26:22 -0700567 log_debug("Found existing bloblist size %lx at %lx\n", size,
568 addr);
Simon Glass712bd2d2018-11-15 18:43:50 -0700569 }
Simon Glassa28f39c2023-09-26 08:14:51 -0600570 if (ret)
571 return log_msg_ret("ini", ret);
572 gd->flags |= GD_FLG_BLOBLIST_READY;
573
Raymond Mao72c99cc2024-02-03 08:36:26 -0800574#ifdef DEBUG
575 bloblist_show_stats();
576 bloblist_show_list();
577#endif
578
Simon Glassa28f39c2023-09-26 08:14:51 -0600579 return 0;
580}
581
582int bloblist_maybe_init(void)
583{
584 if (CONFIG_IS_ENABLED(BLOBLIST) && !(gd->flags & GD_FLG_BLOBLIST_READY))
585 return bloblist_init();
Simon Glass712bd2d2018-11-15 18:43:50 -0700586
Simon Glassa28f39c2023-09-26 08:14:51 -0600587 return 0;
Simon Glass712bd2d2018-11-15 18:43:50 -0700588}
Raymond Maofe016b62024-02-03 08:36:20 -0800589
590int bloblist_check_reg_conv(ulong rfdt, ulong rzero, ulong rsig)
591{
Andrew Goodbody5b5322c2024-12-16 18:07:35 +0000592 u64 version = BLOBLIST_REGCONV_VER;
Levi Yun5bfbd072024-07-10 14:53:20 +0100593 ulong sigval;
594
Andrew Goodbody5b5322c2024-12-16 18:07:35 +0000595 if ((IS_ENABLED(CONFIG_64BIT) && !IS_ENABLED(CONFIG_SPL_BUILD)) ||
596 (IS_ENABLED(CONFIG_SPL_64BIT) && IS_ENABLED(CONFIG_SPL_BUILD))) {
597 sigval = ((BLOBLIST_MAGIC & ((1ULL << BLOBLIST_REGCONV_SHIFT_64) - 1)) |
598 ((version & BLOBLIST_REGCONV_MASK) << BLOBLIST_REGCONV_SHIFT_64));
599 } else {
600 sigval = ((BLOBLIST_MAGIC & ((1UL << BLOBLIST_REGCONV_SHIFT_32) - 1)) |
Levi Yun5bfbd072024-07-10 14:53:20 +0100601 ((version & BLOBLIST_REGCONV_MASK) << BLOBLIST_REGCONV_SHIFT_32));
Andrew Goodbody5b5322c2024-12-16 18:07:35 +0000602 }
Levi Yun5bfbd072024-07-10 14:53:20 +0100603
604 if (rzero || rsig != sigval ||
Raymond Maofe016b62024-02-03 08:36:20 -0800605 rfdt != (ulong)bloblist_find(BLOBLISTT_CONTROL_FDT, 0)) {
606 gd->bloblist = NULL; /* Reset the gd bloblist pointer */
607 return -EIO;
608 }
609
610 return 0;
611}