blob: 73dbbc01c081cef4ad1289dc05e5f4fdeecd16ab [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{
83 if (hdr->alloced <= hdr->hdr_size)
84 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{
90 return rec->hdr_size;
91}
92
93static inline uint rec_tag(struct bloblist_rec *rec)
94{
95 return rec->tag;
96}
97
Simon Glass1fe59372021-07-05 16:32:53 -060098static ulong bloblist_blob_end_ofs(struct bloblist_hdr *hdr,
99 struct bloblist_rec *rec)
Simon Glass9f407d42018-11-15 18:43:50 -0700100{
101 ulong offset;
102
103 offset = (void *)rec - (void *)hdr;
Simon Glass1f06ed42023-12-27 13:07:03 -0800104 offset += rec_hdr_size(rec) + ALIGN(rec->size, BLOBLIST_ALIGN);
Simon Glass1fe59372021-07-05 16:32:53 -0600105
106 return offset;
107}
108
109static struct bloblist_rec *bloblist_next_blob(struct bloblist_hdr *hdr,
110 struct bloblist_rec *rec)
111{
112 ulong offset = bloblist_blob_end_ofs(hdr, rec);
113
Simon Glass9f407d42018-11-15 18:43:50 -0700114 if (offset >= hdr->alloced)
115 return NULL;
116 return (struct bloblist_rec *)((void *)hdr + offset);
117}
118
119#define foreach_rec(_rec, _hdr) \
120 for (_rec = bloblist_first_blob(_hdr); \
121 _rec; \
122 _rec = bloblist_next_blob(_hdr, _rec))
123
124static struct bloblist_rec *bloblist_findrec(uint tag)
125{
126 struct bloblist_hdr *hdr = gd->bloblist;
127 struct bloblist_rec *rec;
128
129 if (!hdr)
130 return NULL;
131
132 foreach_rec(rec, hdr) {
Simon Glass1f06ed42023-12-27 13:07:03 -0800133 if (rec_tag(rec) == tag)
Simon Glass9f407d42018-11-15 18:43:50 -0700134 return rec;
135 }
136
137 return NULL;
138}
139
Simon Glass1a2e02f2023-12-27 13:07:00 -0800140static int bloblist_addrec(uint tag, int size, int align_log2,
Simon Glass4c1497e2020-09-19 18:49:29 -0600141 struct bloblist_rec **recp)
Simon Glass9f407d42018-11-15 18:43:50 -0700142{
143 struct bloblist_hdr *hdr = gd->bloblist;
144 struct bloblist_rec *rec;
Simon Glassf9ef9fb2023-12-27 13:07:06 -0800145 int data_start, aligned_start, new_alloced;
Simon Glass9f407d42018-11-15 18:43:50 -0700146
Simon Glass1a2e02f2023-12-27 13:07:00 -0800147 if (!align_log2)
148 align_log2 = BLOBLIST_ALIGN_LOG2;
Simon Glass4c1497e2020-09-19 18:49:29 -0600149
Simon Glass751b7c72020-09-19 18:49:28 -0600150 /* Figure out where the new data will start */
Simon Glass4c1497e2020-09-19 18:49:29 -0600151 data_start = map_to_sysmem(hdr) + hdr->alloced + sizeof(*rec);
152
153 /* Align the address and then calculate the offset from ->alloced */
Simon Glassf9ef9fb2023-12-27 13:07:06 -0800154 aligned_start = ALIGN(data_start, 1U << align_log2) - data_start;
155
156 /* If we need to create a dummy record, create it */
157 if (aligned_start) {
158 int void_size = aligned_start - sizeof(*rec);
159 struct bloblist_rec *vrec;
160 int ret;
161
162 ret = bloblist_addrec(BLOBLISTT_VOID, void_size, 0, &vrec);
163 if (ret)
164 return log_msg_ret("void", ret);
165
166 /* start the record after that */
167 data_start = map_to_sysmem(hdr) + hdr->alloced + sizeof(*vrec);
168 }
Simon Glass751b7c72020-09-19 18:49:28 -0600169
170 /* Calculate the new allocated total */
Simon Glassf9ef9fb2023-12-27 13:07:06 -0800171 new_alloced = data_start - map_to_sysmem(hdr) +
172 ALIGN(size, 1U << align_log2);
Simon Glass4c1497e2020-09-19 18:49:29 -0600173
Simon Glass1f618d52021-07-05 16:32:55 -0600174 if (new_alloced > hdr->size) {
Simon Glass1d8bbd72022-01-12 19:26:20 -0700175 log_err("Failed to allocate %x bytes size=%x, need size=%x\n",
176 size, hdr->size, new_alloced);
Simon Glass9f407d42018-11-15 18:43:50 -0700177 return log_msg_ret("bloblist add", -ENOSPC);
178 }
179 rec = (void *)hdr + hdr->alloced;
Simon Glass9f407d42018-11-15 18:43:50 -0700180
181 rec->tag = tag;
Simon Glassf9ef9fb2023-12-27 13:07:06 -0800182 rec->hdr_size = sizeof(struct bloblist_rec);
Simon Glass9f407d42018-11-15 18:43:50 -0700183 rec->size = size;
Simon Glassb83994d2020-01-27 08:49:52 -0700184
185 /* Zero the record data */
Simon Glass1f06ed42023-12-27 13:07:03 -0800186 memset((void *)rec + rec_hdr_size(rec), '\0', rec->size);
Simon Glass751b7c72020-09-19 18:49:28 -0600187
188 hdr->alloced = new_alloced;
Simon Glass9f407d42018-11-15 18:43:50 -0700189 *recp = rec;
190
191 return 0;
192}
193
Simon Glass4c1497e2020-09-19 18:49:29 -0600194static int bloblist_ensurerec(uint tag, struct bloblist_rec **recp, int size,
Simon Glass1a2e02f2023-12-27 13:07:00 -0800195 int align_log2)
Simon Glass9f407d42018-11-15 18:43:50 -0700196{
197 struct bloblist_rec *rec;
198
199 rec = bloblist_findrec(tag);
200 if (rec) {
Simon Glass5b044542020-01-27 08:49:50 -0700201 if (size && size != rec->size) {
202 *recp = rec;
Simon Glass9f407d42018-11-15 18:43:50 -0700203 return -ESPIPE;
Simon Glass5b044542020-01-27 08:49:50 -0700204 }
Simon Glass9f407d42018-11-15 18:43:50 -0700205 } else {
206 int ret;
207
Simon Glass1a2e02f2023-12-27 13:07:00 -0800208 ret = bloblist_addrec(tag, size, align_log2, &rec);
Simon Glass9f407d42018-11-15 18:43:50 -0700209 if (ret)
210 return ret;
211 }
212 *recp = rec;
213
214 return 0;
215}
216
217void *bloblist_find(uint tag, int size)
218{
219 struct bloblist_rec *rec;
220
221 rec = bloblist_findrec(tag);
222 if (!rec)
223 return NULL;
224 if (size && size != rec->size)
225 return NULL;
226
Simon Glass1f06ed42023-12-27 13:07:03 -0800227 return (void *)rec + rec_hdr_size(rec);
Simon Glass9f407d42018-11-15 18:43:50 -0700228}
229
Simon Glass1a2e02f2023-12-27 13:07:00 -0800230void *bloblist_add(uint tag, int size, int align_log2)
Simon Glass9f407d42018-11-15 18:43:50 -0700231{
232 struct bloblist_rec *rec;
233
Simon Glass1a2e02f2023-12-27 13:07:00 -0800234 if (bloblist_addrec(tag, size, align_log2, &rec))
Simon Glass9f407d42018-11-15 18:43:50 -0700235 return NULL;
236
Simon Glass1f06ed42023-12-27 13:07:03 -0800237 return (void *)rec + rec_hdr_size(rec);
Simon Glass9f407d42018-11-15 18:43:50 -0700238}
239
Simon Glass1a2e02f2023-12-27 13:07:00 -0800240int bloblist_ensure_size(uint tag, int size, int align_log2, void **blobp)
Simon Glass9f407d42018-11-15 18:43:50 -0700241{
242 struct bloblist_rec *rec;
243 int ret;
244
Simon Glass1a2e02f2023-12-27 13:07:00 -0800245 ret = bloblist_ensurerec(tag, &rec, size, align_log2);
Simon Glass9f407d42018-11-15 18:43:50 -0700246 if (ret)
247 return ret;
Simon Glass1f06ed42023-12-27 13:07:03 -0800248 *blobp = (void *)rec + rec_hdr_size(rec);
Simon Glass9f407d42018-11-15 18:43:50 -0700249
250 return 0;
251}
252
253void *bloblist_ensure(uint tag, int size)
254{
255 struct bloblist_rec *rec;
256
Simon Glass4c1497e2020-09-19 18:49:29 -0600257 if (bloblist_ensurerec(tag, &rec, size, 0))
Simon Glass9f407d42018-11-15 18:43:50 -0700258 return NULL;
259
Simon Glass1f06ed42023-12-27 13:07:03 -0800260 return (void *)rec + rec_hdr_size(rec);
Simon Glass9f407d42018-11-15 18:43:50 -0700261}
262
Simon Glass5b044542020-01-27 08:49:50 -0700263int bloblist_ensure_size_ret(uint tag, int *sizep, void **blobp)
264{
265 struct bloblist_rec *rec;
266 int ret;
267
Simon Glass4c1497e2020-09-19 18:49:29 -0600268 ret = bloblist_ensurerec(tag, &rec, *sizep, 0);
Simon Glass5b044542020-01-27 08:49:50 -0700269 if (ret == -ESPIPE)
270 *sizep = rec->size;
271 else if (ret)
272 return ret;
Simon Glass1f06ed42023-12-27 13:07:03 -0800273 *blobp = (void *)rec + rec_hdr_size(rec);
Simon Glass5b044542020-01-27 08:49:50 -0700274
275 return 0;
276}
277
Simon Glass1fe59372021-07-05 16:32:53 -0600278static int bloblist_resize_rec(struct bloblist_hdr *hdr,
279 struct bloblist_rec *rec,
280 int new_size)
281{
282 int expand_by; /* Number of bytes to expand by (-ve to contract) */
283 int new_alloced; /* New value for @hdr->alloced */
284 ulong next_ofs; /* Offset of the record after @rec */
285
286 expand_by = ALIGN(new_size - rec->size, BLOBLIST_ALIGN);
287 new_alloced = ALIGN(hdr->alloced + expand_by, BLOBLIST_ALIGN);
288 if (new_size < 0) {
Simon Glass1d8bbd72022-01-12 19:26:20 -0700289 log_debug("Attempt to shrink blob size below 0 (%x)\n",
290 new_size);
Simon Glass1fe59372021-07-05 16:32:53 -0600291 return log_msg_ret("size", -EINVAL);
292 }
293 if (new_alloced > hdr->size) {
Simon Glass1d8bbd72022-01-12 19:26:20 -0700294 log_err("Failed to allocate %x bytes size=%x, need size=%x\n",
295 new_size, hdr->size, new_alloced);
Simon Glass1fe59372021-07-05 16:32:53 -0600296 return log_msg_ret("alloc", -ENOSPC);
297 }
298
299 /* Move the following blobs up or down, if this is not the last */
300 next_ofs = bloblist_blob_end_ofs(hdr, rec);
301 if (next_ofs != hdr->alloced) {
302 memmove((void *)hdr + next_ofs + expand_by,
303 (void *)hdr + next_ofs, new_alloced - next_ofs);
304 }
305 hdr->alloced = new_alloced;
306
307 /* Zero the new part of the blob */
308 if (expand_by > 0) {
Simon Glass1f06ed42023-12-27 13:07:03 -0800309 memset((void *)rec + rec_hdr_size(rec) + rec->size, '\0',
Simon Glass1fe59372021-07-05 16:32:53 -0600310 new_size - rec->size);
311 }
312
313 /* Update the size of this blob */
314 rec->size = new_size;
315
316 return 0;
317}
318
319int bloblist_resize(uint tag, int new_size)
320{
321 struct bloblist_hdr *hdr = gd->bloblist;
322 struct bloblist_rec *rec;
323 int ret;
324
325 rec = bloblist_findrec(tag);
326 if (!rec)
327 return log_msg_ret("find", -ENOENT);
328 ret = bloblist_resize_rec(hdr, rec, new_size);
329 if (ret)
330 return log_msg_ret("resize", ret);
331
332 return 0;
333}
334
Simon Glass9f407d42018-11-15 18:43:50 -0700335static u32 bloblist_calc_chksum(struct bloblist_hdr *hdr)
336{
Simon Glass997dac62023-12-27 13:07:05 -0800337 u8 chksum;
Simon Glass9f407d42018-11-15 18:43:50 -0700338
Simon Glass997dac62023-12-27 13:07:05 -0800339 chksum = table_compute_checksum(hdr, hdr->alloced);
340 chksum += hdr->chksum;
Simon Glass9f407d42018-11-15 18:43:50 -0700341
342 return chksum;
343}
344
345int bloblist_new(ulong addr, uint size, uint flags)
346{
347 struct bloblist_hdr *hdr;
348
349 if (size < sizeof(*hdr))
350 return log_ret(-ENOSPC);
351 if (addr & (BLOBLIST_ALIGN - 1))
352 return log_ret(-EFAULT);
353 hdr = map_sysmem(addr, size);
354 memset(hdr, '\0', sizeof(*hdr));
355 hdr->version = BLOBLIST_VERSION;
356 hdr->hdr_size = sizeof(*hdr);
357 hdr->flags = flags;
358 hdr->magic = BLOBLIST_MAGIC;
359 hdr->size = size;
360 hdr->alloced = hdr->hdr_size;
361 hdr->chksum = 0;
362 gd->bloblist = hdr;
363
364 return 0;
365}
366
367int bloblist_check(ulong addr, uint size)
368{
369 struct bloblist_hdr *hdr;
370 u32 chksum;
371
372 hdr = map_sysmem(addr, sizeof(*hdr));
373 if (hdr->magic != BLOBLIST_MAGIC)
374 return log_msg_ret("Bad magic", -ENOENT);
375 if (hdr->version != BLOBLIST_VERSION)
376 return log_msg_ret("Bad version", -EPROTONOSUPPORT);
377 if (size && hdr->size != size)
378 return log_msg_ret("Bad size", -EFBIG);
379 chksum = bloblist_calc_chksum(hdr);
380 if (hdr->chksum != chksum) {
Simon Glass1d8bbd72022-01-12 19:26:20 -0700381 log_err("Checksum %x != %x\n", hdr->chksum, chksum);
Simon Glass9f407d42018-11-15 18:43:50 -0700382 return log_msg_ret("Bad checksum", -EIO);
383 }
384 gd->bloblist = hdr;
385
386 return 0;
387}
388
389int bloblist_finish(void)
390{
391 struct bloblist_hdr *hdr = gd->bloblist;
392
393 hdr->chksum = bloblist_calc_chksum(hdr);
Simon Glass99047f52022-01-12 19:26:22 -0700394 log_debug("Finished bloblist size %lx at %lx\n", (ulong)hdr->size,
395 (ulong)map_to_sysmem(hdr));
Simon Glass9f407d42018-11-15 18:43:50 -0700396
397 return 0;
398}
399
Simon Glasse50a24a2022-01-12 19:26:23 -0700400ulong bloblist_get_base(void)
401{
402 return map_to_sysmem(gd->bloblist);
403}
404
405ulong bloblist_get_size(void)
406{
407 struct bloblist_hdr *hdr = gd->bloblist;
408
409 return hdr->size;
410}
411
Simon Glass4aed2272020-09-19 18:49:26 -0600412void bloblist_get_stats(ulong *basep, ulong *sizep, ulong *allocedp)
413{
414 struct bloblist_hdr *hdr = gd->bloblist;
415
416 *basep = map_to_sysmem(gd->bloblist);
417 *sizep = hdr->size;
418 *allocedp = hdr->alloced;
419}
420
421static void show_value(const char *prompt, ulong value)
422{
423 printf("%s:%*s %-5lx ", prompt, 8 - (int)strlen(prompt), "", value);
424 print_size(value, "\n");
425}
426
427void bloblist_show_stats(void)
428{
429 ulong base, size, alloced;
430
431 bloblist_get_stats(&base, &size, &alloced);
432 printf("base: %lx\n", base);
433 show_value("size", size);
434 show_value("alloced", alloced);
435 show_value("free", size - alloced);
436}
437
438void bloblist_show_list(void)
439{
440 struct bloblist_hdr *hdr = gd->bloblist;
441 struct bloblist_rec *rec;
442
Simon Glassf16ec772022-01-12 19:26:19 -0700443 printf("%-8s %8s Tag Name\n", "Address", "Size");
Simon Glass4aed2272020-09-19 18:49:26 -0600444 for (rec = bloblist_first_blob(hdr); rec;
445 rec = bloblist_next_blob(hdr, rec)) {
Simon Glassf16ec772022-01-12 19:26:19 -0700446 printf("%08lx %8x %4x %s\n",
Simon Glass1f06ed42023-12-27 13:07:03 -0800447 (ulong)map_to_sysmem((void *)rec + rec_hdr_size(rec)),
448 rec->size, rec_tag(rec),
449 bloblist_tag_name(rec_tag(rec)));
Simon Glass4aed2272020-09-19 18:49:26 -0600450 }
451}
452
Simon Glass9fe06462021-01-13 20:29:43 -0700453void bloblist_reloc(void *to, uint to_size, void *from, uint from_size)
454{
455 struct bloblist_hdr *hdr;
456
457 memcpy(to, from, from_size);
458 hdr = to;
459 hdr->size = to_size;
460}
461
Simon Glass9f407d42018-11-15 18:43:50 -0700462int bloblist_init(void)
463{
Simon Glassce3e75d2022-01-22 05:07:27 -0700464 bool fixed = IS_ENABLED(CONFIG_BLOBLIST_FIXED);
Simon Glass9f407d42018-11-15 18:43:50 -0700465 int ret = -ENOENT;
Simon Glass99047f52022-01-12 19:26:22 -0700466 ulong addr, size;
467 bool expected;
Simon Glass9f407d42018-11-15 18:43:50 -0700468
469 /**
Simon Glassce3e75d2022-01-22 05:07:27 -0700470 * We don't expect to find an existing bloblist in the first phase of
471 * U-Boot that runs. Also we have no way to receive the address of an
472 * allocated bloblist from a previous stage, so it must be at a fixed
473 * address.
Simon Glass9f407d42018-11-15 18:43:50 -0700474 */
Simon Glassce3e75d2022-01-22 05:07:27 -0700475 expected = fixed && !u_boot_first_phase();
Simon Glass9fe06462021-01-13 20:29:43 -0700476 if (spl_prev_phase() == PHASE_TPL && !IS_ENABLED(CONFIG_TPL_BLOBLIST))
477 expected = false;
Simon Glassce3e75d2022-01-22 05:07:27 -0700478 if (fixed)
479 addr = IF_ENABLED_INT(CONFIG_BLOBLIST_FIXED,
480 CONFIG_BLOBLIST_ADDR);
Simon Glass99047f52022-01-12 19:26:22 -0700481 size = CONFIG_BLOBLIST_SIZE;
482 if (expected) {
483 ret = bloblist_check(addr, size);
484 if (ret) {
485 log_warning("Expected bloblist at %lx not found (err=%d)\n",
486 addr, ret);
487 } else {
488 /* Get the real size, if it is not what we expected */
489 size = gd->bloblist->size;
490 }
491 }
Simon Glass9f407d42018-11-15 18:43:50 -0700492 if (ret) {
Simon Glass99047f52022-01-12 19:26:22 -0700493 if (CONFIG_IS_ENABLED(BLOBLIST_ALLOC)) {
494 void *ptr = memalign(BLOBLIST_ALIGN, size);
Simon Glassd5b6e912021-11-03 21:09:20 -0600495
496 if (!ptr)
497 return log_msg_ret("alloc", -ENOMEM);
498 addr = map_to_sysmem(ptr);
Simon Glassce3e75d2022-01-22 05:07:27 -0700499 } else if (!fixed) {
500 return log_msg_ret("!fixed", ret);
Simon Glassd5b6e912021-11-03 21:09:20 -0600501 }
Simon Glass99047f52022-01-12 19:26:22 -0700502 log_debug("Creating new bloblist size %lx at %lx\n", size,
503 addr);
504 ret = bloblist_new(addr, size, 0);
Simon Glass9f407d42018-11-15 18:43:50 -0700505 } else {
Simon Glass99047f52022-01-12 19:26:22 -0700506 log_debug("Found existing bloblist size %lx at %lx\n", size,
507 addr);
Simon Glass9f407d42018-11-15 18:43:50 -0700508 }
Simon Glass3d653182023-09-26 08:14:51 -0600509 if (ret)
510 return log_msg_ret("ini", ret);
511 gd->flags |= GD_FLG_BLOBLIST_READY;
Simon Glass9f407d42018-11-15 18:43:50 -0700512
Simon Glass3d653182023-09-26 08:14:51 -0600513 return 0;
514}
515
516int bloblist_maybe_init(void)
517{
518 if (CONFIG_IS_ENABLED(BLOBLIST) && !(gd->flags & GD_FLG_BLOBLIST_READY))
519 return bloblist_init();
520
521 return 0;
Simon Glass9f407d42018-11-15 18:43:50 -0700522}