blob: ad06d7a1795deb9b1799abfd9348fcfa4706f776 [file] [log] [blame]
Simon Glass6c9e3d12022-01-12 19:26:25 -07001// SPDX-License-Identifier: GPL-2.0+ BSD-3-Clause
Simon Glass9f407d42018-11-15 18:43:50 -07002/*
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>
Simon Glass4e4bf942022-07-31 12:28:48 -060011#include <display_options.h>
Simon Glass9f407d42018-11-15 18:43:50 -070012#include <log.h>
Simon Glassd5b6e912021-11-03 21:09:20 -060013#include <malloc.h>
Simon Glass9f407d42018-11-15 18:43:50 -070014#include <mapmem.h>
15#include <spl.h>
Simon Glass997dac62023-12-27 13:07:05 -080016#include <tables_csum.h>
Simon Glass401d1c42020-10-30 21:38:53 -060017#include <asm/global_data.h>
Simon Glass3db71102019-11-14 12:57:16 -070018#include <u-boot/crc.h>
Simon Glass9f407d42018-11-15 18:43:50 -070019
Simon Glass751b7c72020-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 Glass751b7c72020-09-19 18:49:28 -060030 */
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[] = {
Simon Glasse748e4b2023-12-27 13:06:59 -080038 { BLOBLISTT_VOID, "(void)" },
Simon Glassf16ec772022-01-12 19:26:19 -070039
40 /* BLOBLISTT_AREA_FIRMWARE_TOP */
Simon Glasse748e4b2023-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 Glassf16ec772022-01-12 19:26:19 -070047
48 /* BLOBLISTT_AREA_FIRMWARE */
Simon Glassf16ec772022-01-12 19:26:19 -070049 { BLOBLISTT_TPM2_TCG_LOG, "TPM v2 log space" },
50 { BLOBLISTT_TCPA_LOG, "TPM log space" },
Simon Glasse748e4b2023-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 Glassf16ec772022-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 Glass14d9f632023-09-26 08:14:52 -060063 { BLOBLISTT_VBE, "VBE" },
Simon Glass03fe79c2023-07-15 21:38:59 -060064 { BLOBLISTT_U_BOOT_VIDEO, "SPL video handoff" },
Simon Glassf16ec772022-01-12 19:26:19 -070065
66 /* BLOBLISTT_VENDOR_AREA */
Simon Glass4aed2272020-09-19 18:49:26 -060067};
68
69const char *bloblist_tag_name(enum bloblist_tag_t tag)
70{
Simon Glassf16ec772022-01-12 19:26:19 -070071 int i;
Simon Glass4aed2272020-09-19 18:49:26 -060072
Simon Glassf16ec772022-01-12 19:26:19 -070073 for (i = 0; i < ARRAY_SIZE(tag_name); i++) {
74 if (tag_name[i].tag == tag)
75 return tag_name[i].name;
76 }
77
78 return "invalid";
Simon Glass4aed2272020-09-19 18:49:26 -060079}
80
81static struct bloblist_rec *bloblist_first_blob(struct bloblist_hdr *hdr)
Simon Glass9f407d42018-11-15 18:43:50 -070082{
Simon Glassb86b2d92023-12-27 13:07:08 -080083 if (hdr->used_size <= hdr->hdr_size)
Simon Glass9f407d42018-11-15 18:43:50 -070084 return NULL;
85 return (struct bloblist_rec *)((void *)hdr + hdr->hdr_size);
86}
87
Simon Glass1f06ed42023-12-27 13:07:03 -080088static inline uint rec_hdr_size(struct bloblist_rec *rec)
89{
Simon Glassb6e83822023-12-27 13:07:07 -080090 return (rec->tag_and_hdr_size & BLOBLISTR_HDR_SIZE_MASK) >>
91 BLOBLISTR_HDR_SIZE_SHIFT;
Simon Glass1f06ed42023-12-27 13:07:03 -080092}
93
94static inline uint rec_tag(struct bloblist_rec *rec)
95{
Simon Glassb6e83822023-12-27 13:07:07 -080096 return (rec->tag_and_hdr_size & BLOBLISTR_TAG_MASK) >>
97 BLOBLISTR_TAG_SHIFT;
Simon Glass1f06ed42023-12-27 13:07:03 -080098}
99
Simon Glass1fe59372021-07-05 16:32:53 -0600100static ulong bloblist_blob_end_ofs(struct bloblist_hdr *hdr,
101 struct bloblist_rec *rec)
Simon Glass9f407d42018-11-15 18:43:50 -0700102{
103 ulong offset;
104
105 offset = (void *)rec - (void *)hdr;
Simon Glassb6e83822023-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 Glass1fe59372021-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 Glassb86b2d92023-12-27 13:07:08 -0800122 if (offset >= hdr->used_size)
Simon Glass9f407d42018-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 Glass1f06ed42023-12-27 13:07:03 -0800141 if (rec_tag(rec) == tag)
Simon Glass9f407d42018-11-15 18:43:50 -0700142 return rec;
143 }
144
145 return NULL;
146}
147
Simon Glass1a2e02f2023-12-27 13:07:00 -0800148static int bloblist_addrec(uint tag, int size, int align_log2,
Simon Glass4c1497e2020-09-19 18:49:29 -0600149 struct bloblist_rec **recp)
Simon Glass9f407d42018-11-15 18:43:50 -0700150{
151 struct bloblist_hdr *hdr = gd->bloblist;
152 struct bloblist_rec *rec;
Simon Glassf9ef9fb2023-12-27 13:07:06 -0800153 int data_start, aligned_start, new_alloced;
Simon Glass9f407d42018-11-15 18:43:50 -0700154
Simon Glass1a2e02f2023-12-27 13:07:00 -0800155 if (!align_log2)
Simon Glassb6e83822023-12-27 13:07:07 -0800156 align_log2 = BLOBLIST_BLOB_ALIGN_LOG2;
Simon Glass4c1497e2020-09-19 18:49:29 -0600157
Simon Glass751b7c72020-09-19 18:49:28 -0600158 /* Figure out where the new data will start */
Simon Glassb86b2d92023-12-27 13:07:08 -0800159 data_start = map_to_sysmem(hdr) + hdr->used_size + sizeof(*rec);
Simon Glass4c1497e2020-09-19 18:49:29 -0600160
Simon Glassb86b2d92023-12-27 13:07:08 -0800161 /* Align the address and then calculate the offset from used size */
Simon Glassf9ef9fb2023-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 Glassb86b2d92023-12-27 13:07:08 -0800175 data_start = map_to_sysmem(hdr) + hdr->used_size + sizeof(*vrec);
Simon Glassf9ef9fb2023-12-27 13:07:06 -0800176 }
Simon Glass751b7c72020-09-19 18:49:28 -0600177
178 /* Calculate the new allocated total */
Simon Glassf9ef9fb2023-12-27 13:07:06 -0800179 new_alloced = data_start - map_to_sysmem(hdr) +
180 ALIGN(size, 1U << align_log2);
Simon Glass4c1497e2020-09-19 18:49:29 -0600181
Simon Glassb86b2d92023-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 Glass9f407d42018-11-15 18:43:50 -0700186 return log_msg_ret("bloblist add", -ENOSPC);
187 }
Simon Glassb86b2d92023-12-27 13:07:08 -0800188 rec = (void *)hdr + hdr->used_size;
Simon Glass9f407d42018-11-15 18:43:50 -0700189
Simon Glassb6e83822023-12-27 13:07:07 -0800190 rec->tag_and_hdr_size = tag | sizeof(*rec) << BLOBLISTR_HDR_SIZE_SHIFT;
Simon Glass9f407d42018-11-15 18:43:50 -0700191 rec->size = size;
Simon Glassb83994d2020-01-27 08:49:52 -0700192
193 /* Zero the record data */
Simon Glass1f06ed42023-12-27 13:07:03 -0800194 memset((void *)rec + rec_hdr_size(rec), '\0', rec->size);
Simon Glass751b7c72020-09-19 18:49:28 -0600195
Simon Glassb86b2d92023-12-27 13:07:08 -0800196 hdr->used_size = new_alloced;
Simon Glass9f407d42018-11-15 18:43:50 -0700197 *recp = rec;
198
199 return 0;
200}
201
Simon Glass4c1497e2020-09-19 18:49:29 -0600202static int bloblist_ensurerec(uint tag, struct bloblist_rec **recp, int size,
Simon Glass1a2e02f2023-12-27 13:07:00 -0800203 int align_log2)
Simon Glass9f407d42018-11-15 18:43:50 -0700204{
205 struct bloblist_rec *rec;
206
207 rec = bloblist_findrec(tag);
208 if (rec) {
Simon Glass5b044542020-01-27 08:49:50 -0700209 if (size && size != rec->size) {
210 *recp = rec;
Simon Glass9f407d42018-11-15 18:43:50 -0700211 return -ESPIPE;
Simon Glass5b044542020-01-27 08:49:50 -0700212 }
Simon Glass9f407d42018-11-15 18:43:50 -0700213 } else {
214 int ret;
215
Simon Glass1a2e02f2023-12-27 13:07:00 -0800216 ret = bloblist_addrec(tag, size, align_log2, &rec);
Simon Glass9f407d42018-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 Glass1f06ed42023-12-27 13:07:03 -0800235 return (void *)rec + rec_hdr_size(rec);
Simon Glass9f407d42018-11-15 18:43:50 -0700236}
237
Simon Glass1a2e02f2023-12-27 13:07:00 -0800238void *bloblist_add(uint tag, int size, int align_log2)
Simon Glass9f407d42018-11-15 18:43:50 -0700239{
240 struct bloblist_rec *rec;
241
Simon Glass1a2e02f2023-12-27 13:07:00 -0800242 if (bloblist_addrec(tag, size, align_log2, &rec))
Simon Glass9f407d42018-11-15 18:43:50 -0700243 return NULL;
244
Simon Glass1f06ed42023-12-27 13:07:03 -0800245 return (void *)rec + rec_hdr_size(rec);
Simon Glass9f407d42018-11-15 18:43:50 -0700246}
247
Simon Glass1a2e02f2023-12-27 13:07:00 -0800248int bloblist_ensure_size(uint tag, int size, int align_log2, void **blobp)
Simon Glass9f407d42018-11-15 18:43:50 -0700249{
250 struct bloblist_rec *rec;
251 int ret;
252
Simon Glass1a2e02f2023-12-27 13:07:00 -0800253 ret = bloblist_ensurerec(tag, &rec, size, align_log2);
Simon Glass9f407d42018-11-15 18:43:50 -0700254 if (ret)
255 return ret;
Simon Glass1f06ed42023-12-27 13:07:03 -0800256 *blobp = (void *)rec + rec_hdr_size(rec);
Simon Glass9f407d42018-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 Glass4c1497e2020-09-19 18:49:29 -0600265 if (bloblist_ensurerec(tag, &rec, size, 0))
Simon Glass9f407d42018-11-15 18:43:50 -0700266 return NULL;
267
Simon Glass1f06ed42023-12-27 13:07:03 -0800268 return (void *)rec + rec_hdr_size(rec);
Simon Glass9f407d42018-11-15 18:43:50 -0700269}
270
Simon Glass5b044542020-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 Glass4c1497e2020-09-19 18:49:29 -0600276 ret = bloblist_ensurerec(tag, &rec, *sizep, 0);
Simon Glass5b044542020-01-27 08:49:50 -0700277 if (ret == -ESPIPE)
278 *sizep = rec->size;
279 else if (ret)
280 return ret;
Simon Glass1f06ed42023-12-27 13:07:03 -0800281 *blobp = (void *)rec + rec_hdr_size(rec);
Simon Glass5b044542020-01-27 08:49:50 -0700282
283 return 0;
284}
285
Simon Glass1fe59372021-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 Glassb86b2d92023-12-27 13:07:08 -0800291 int new_alloced;
Simon Glass1fe59372021-07-05 16:32:53 -0600292 ulong next_ofs; /* Offset of the record after @rec */
293
Simon Glassb6e83822023-12-27 13:07:07 -0800294 expand_by = ALIGN(new_size - rec->size, BLOBLIST_BLOB_ALIGN);
Simon Glassb86b2d92023-12-27 13:07:08 -0800295 new_alloced = ALIGN(hdr->used_size + expand_by, BLOBLIST_BLOB_ALIGN);
Simon Glass1fe59372021-07-05 16:32:53 -0600296 if (new_size < 0) {
Simon Glass1d8bbd72022-01-12 19:26:20 -0700297 log_debug("Attempt to shrink blob size below 0 (%x)\n",
298 new_size);
Simon Glass1fe59372021-07-05 16:32:53 -0600299 return log_msg_ret("size", -EINVAL);
300 }
Simon Glassb86b2d92023-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 Glass1fe59372021-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 Glassb86b2d92023-12-27 13:07:08 -0800310 if (next_ofs != hdr->used_size) {
Simon Glass1fe59372021-07-05 16:32:53 -0600311 memmove((void *)hdr + next_ofs + expand_by,
312 (void *)hdr + next_ofs, new_alloced - next_ofs);
313 }
Simon Glassb86b2d92023-12-27 13:07:08 -0800314 hdr->used_size = new_alloced;
Simon Glass1fe59372021-07-05 16:32:53 -0600315
316 /* Zero the new part of the blob */
317 if (expand_by > 0) {
Simon Glass1f06ed42023-12-27 13:07:03 -0800318 memset((void *)rec + rec_hdr_size(rec) + rec->size, '\0',
Simon Glass1fe59372021-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 Glass9f407d42018-11-15 18:43:50 -0700344static u32 bloblist_calc_chksum(struct bloblist_hdr *hdr)
345{
Simon Glass997dac62023-12-27 13:07:05 -0800346 u8 chksum;
Simon Glass9f407d42018-11-15 18:43:50 -0700347
Simon Glassb86b2d92023-12-27 13:07:08 -0800348 chksum = table_compute_checksum(hdr, hdr->used_size);
Simon Glass997dac62023-12-27 13:07:05 -0800349 chksum += hdr->chksum;
Simon Glass9f407d42018-11-15 18:43:50 -0700350
351 return chksum;
352}
353
Simon Glass7d790a82023-12-27 13:07:09 -0800354int bloblist_new(ulong addr, uint size, uint flags, uint align_log2)
Simon Glass9f407d42018-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 Glassb86b2d92023-12-27 13:07:08 -0800368 hdr->used_size = hdr->hdr_size;
369 hdr->total_size = size;
Simon Glass7d790a82023-12-27 13:07:09 -0800370 hdr->align_log2 = align_log2 ? align_log2 : BLOBLIST_BLOB_ALIGN_LOG2;
Simon Glass9f407d42018-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 Mao67254212024-02-03 08:36:21 -0800387 if (!hdr->total_size || (size && hdr->total_size > size))
Simon Glassb86b2d92023-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 Glass9f407d42018-11-15 18:43:50 -0700394 chksum = bloblist_calc_chksum(hdr);
395 if (hdr->chksum != chksum) {
Simon Glass1d8bbd72022-01-12 19:26:20 -0700396 log_err("Checksum %x != %x\n", hdr->chksum, chksum);
Simon Glass9f407d42018-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 Glassb86b2d92023-12-27 13:07:08 -0800409 log_debug("Finished bloblist size %lx at %lx\n", (ulong)hdr->used_size,
Simon Glass99047f52022-01-12 19:26:22 -0700410 (ulong)map_to_sysmem(hdr));
Simon Glass9f407d42018-11-15 18:43:50 -0700411
412 return 0;
413}
414
Simon Glasse50a24a2022-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 Glassb86b2d92023-12-27 13:07:08 -0800424 return hdr->used_size;
Simon Glasse50a24a2022-01-12 19:26:23 -0700425}
426
Simon Glassb86b2d92023-12-27 13:07:08 -0800427ulong bloblist_get_total_size(void)
428{
429 struct bloblist_hdr *hdr = gd->bloblist;
430
431 return hdr->total_size;
432}
433
434void bloblist_get_stats(ulong *basep, ulong *tsizep, ulong *usizep)
Simon Glass4aed2272020-09-19 18:49:26 -0600435{
436 struct bloblist_hdr *hdr = gd->bloblist;
437
438 *basep = map_to_sysmem(gd->bloblist);
Simon Glassb86b2d92023-12-27 13:07:08 -0800439 *tsizep = hdr->total_size;
440 *usizep = hdr->used_size;
Simon Glass4aed2272020-09-19 18:49:26 -0600441}
442
443static void show_value(const char *prompt, ulong value)
444{
Simon Glassb86b2d92023-12-27 13:07:08 -0800445 printf("%s:%*s %-5lx ", prompt, 10 - (int)strlen(prompt), "", value);
Simon Glass4aed2272020-09-19 18:49:26 -0600446 print_size(value, "\n");
447}
448
449void bloblist_show_stats(void)
450{
Simon Glassb86b2d92023-12-27 13:07:08 -0800451 ulong base, tsize, usize;
Simon Glass4aed2272020-09-19 18:49:26 -0600452
Simon Glassb86b2d92023-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 Glass4aed2272020-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 Glassf16ec772022-01-12 19:26:19 -0700465 printf("%-8s %8s Tag Name\n", "Address", "Size");
Simon Glass4aed2272020-09-19 18:49:26 -0600466 for (rec = bloblist_first_blob(hdr); rec;
467 rec = bloblist_next_blob(hdr, rec)) {
Simon Glassf16ec772022-01-12 19:26:19 -0700468 printf("%08lx %8x %4x %s\n",
Simon Glass1f06ed42023-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 Glass4aed2272020-09-19 18:49:26 -0600472 }
473}
474
Raymond Mao1ef43f32024-02-03 08:36:22 -0800475int bloblist_reloc(void *to, uint to_size)
Simon Glass9fe06462021-01-13 20:29:43 -0700476{
477 struct bloblist_hdr *hdr;
478
Raymond Mao1ef43f32024-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 Glass9fe06462021-01-13 20:29:43 -0700483 hdr = to;
Simon Glassb86b2d92023-12-27 13:07:08 -0800484 hdr->total_size = to_size;
Raymond Mao1ef43f32024-02-03 08:36:22 -0800485 gd->bloblist = to;
486
487 return 0;
Simon Glass9fe06462021-01-13 20:29:43 -0700488}
489
Raymond Mao66131312024-02-03 08:36:26 -0800490/*
491 * Weak default function for getting bloblist from boot args.
492 */
493int __weak xferlist_from_boot_arg(ulong __always_unused addr,
494 ulong __always_unused size)
495{
496 return -ENOENT;
497}
498
Simon Glass9f407d42018-11-15 18:43:50 -0700499int bloblist_init(void)
500{
Simon Glassce3e75d2022-01-22 05:07:27 -0700501 bool fixed = IS_ENABLED(CONFIG_BLOBLIST_FIXED);
Simon Glass9f407d42018-11-15 18:43:50 -0700502 int ret = -ENOENT;
Simon Glass99047f52022-01-12 19:26:22 -0700503 ulong addr, size;
Raymond Mao66131312024-02-03 08:36:26 -0800504 /*
505 * If U-Boot is not in the first phase, an existing bloblist must be
506 * at a fixed address.
507 */
508 bool from_addr = fixed && !u_boot_first_phase();
509 /*
510 * If U-Boot is in the first phase that an arch custom routine should
511 * install the bloblist passed from previous loader to this fixed
Simon Glassce3e75d2022-01-22 05:07:27 -0700512 * address.
Simon Glass9f407d42018-11-15 18:43:50 -0700513 */
Raymond Mao66131312024-02-03 08:36:26 -0800514 bool from_boot_arg = fixed && u_boot_first_phase();
515
Simon Glass9fe06462021-01-13 20:29:43 -0700516 if (spl_prev_phase() == PHASE_TPL && !IS_ENABLED(CONFIG_TPL_BLOBLIST))
Raymond Mao66131312024-02-03 08:36:26 -0800517 from_addr = false;
Simon Glassce3e75d2022-01-22 05:07:27 -0700518 if (fixed)
519 addr = IF_ENABLED_INT(CONFIG_BLOBLIST_FIXED,
520 CONFIG_BLOBLIST_ADDR);
Simon Glass99047f52022-01-12 19:26:22 -0700521 size = CONFIG_BLOBLIST_SIZE;
Raymond Mao66131312024-02-03 08:36:26 -0800522
523 if (from_boot_arg)
524 ret = xferlist_from_boot_arg(addr, size);
525 else if (from_addr)
Simon Glass99047f52022-01-12 19:26:22 -0700526 ret = bloblist_check(addr, size);
Raymond Mao66131312024-02-03 08:36:26 -0800527
528 if (ret)
529 log_warning("Bloblist at %lx not found (err=%d)\n",
530 addr, ret);
531 else
532 /* Get the real size */
533 size = gd->bloblist->total_size;
534
Simon Glass9f407d42018-11-15 18:43:50 -0700535 if (ret) {
Raymond Mao66131312024-02-03 08:36:26 -0800536 /*
537 * If we don't have a bloblist from a fixed address, or the one
538 * in the fixed address is not valid. we must allocate the
539 * memory for it now.
540 */
Simon Glass99047f52022-01-12 19:26:22 -0700541 if (CONFIG_IS_ENABLED(BLOBLIST_ALLOC)) {
542 void *ptr = memalign(BLOBLIST_ALIGN, size);
Simon Glassd5b6e912021-11-03 21:09:20 -0600543
544 if (!ptr)
545 return log_msg_ret("alloc", -ENOMEM);
546 addr = map_to_sysmem(ptr);
Simon Glassce3e75d2022-01-22 05:07:27 -0700547 } else if (!fixed) {
Raymond Mao66131312024-02-03 08:36:26 -0800548 return log_msg_ret("BLOBLIST_FIXED is not enabled",
549 ret);
Simon Glassd5b6e912021-11-03 21:09:20 -0600550 }
Simon Glass99047f52022-01-12 19:26:22 -0700551 log_debug("Creating new bloblist size %lx at %lx\n", size,
552 addr);
Simon Glass7d790a82023-12-27 13:07:09 -0800553 ret = bloblist_new(addr, size, 0, 0);
Simon Glass9f407d42018-11-15 18:43:50 -0700554 } else {
Simon Glass99047f52022-01-12 19:26:22 -0700555 log_debug("Found existing bloblist size %lx at %lx\n", size,
556 addr);
Simon Glass9f407d42018-11-15 18:43:50 -0700557 }
Simon Glass3d653182023-09-26 08:14:51 -0600558 if (ret)
559 return log_msg_ret("ini", ret);
560 gd->flags |= GD_FLG_BLOBLIST_READY;
Simon Glass9f407d42018-11-15 18:43:50 -0700561
Raymond Mao66131312024-02-03 08:36:26 -0800562#ifdef DEBUG
563 bloblist_show_stats();
564 bloblist_show_list();
565#endif
566
Simon Glass3d653182023-09-26 08:14:51 -0600567 return 0;
568}
569
570int bloblist_maybe_init(void)
571{
572 if (CONFIG_IS_ENABLED(BLOBLIST) && !(gd->flags & GD_FLG_BLOBLIST_READY))
573 return bloblist_init();
574
575 return 0;
Simon Glass9f407d42018-11-15 18:43:50 -0700576}
Raymond Mao1c4751f2024-02-03 08:36:20 -0800577
578int bloblist_check_reg_conv(ulong rfdt, ulong rzero, ulong rsig)
579{
580 if (rzero || rsig != (BLOBLIST_MAGIC | BLOBLIST_REGCONV_VER) ||
581 rfdt != (ulong)bloblist_find(BLOBLISTT_CONTROL_FDT, 0)) {
582 gd->bloblist = NULL; /* Reset the gd bloblist pointer */
583 return -EIO;
584 }
585
586 return 0;
587}