]> Git Repo - J-u-boot.git/blob - common/bloblist.c
Merge branch 'master' of https://gitlab.denx.de/u-boot/custodians/u-boot-samsung
[J-u-boot.git] / common / bloblist.c
1 // SPDX-License-Identifier: GPL-2.0+ BSD-3-Clause
2 /*
3  * Copyright 2018 Google, Inc
4  * Written by Simon Glass <[email protected]>
5  */
6
7 #define LOG_CATEGORY    LOGC_BLOBLIST
8
9 #include <common.h>
10 #include <bloblist.h>
11 #include <display_options.h>
12 #include <log.h>
13 #include <malloc.h>
14 #include <mapmem.h>
15 #include <spl.h>
16 #include <tables_csum.h>
17 #include <asm/global_data.h>
18 #include <u-boot/crc.h>
19
20 /*
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.
30  */
31
32 DECLARE_GLOBAL_DATA_PTR;
33
34 static struct tag_name {
35         enum bloblist_tag_t tag;
36         const char *name;
37 } tag_name[] = {
38         { BLOBLISTT_VOID, "(void)" },
39
40         /* BLOBLISTT_AREA_FIRMWARE_TOP */
41         { 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" },
47
48         /* BLOBLISTT_AREA_FIRMWARE */
49         { BLOBLISTT_TPM2_TCG_LOG, "TPM v2 log space" },
50         { BLOBLISTT_TCPA_LOG, "TPM log space" },
51         { 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" },
58         { 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" },
63         { BLOBLISTT_VBE, "VBE" },
64         { BLOBLISTT_U_BOOT_VIDEO, "SPL video handoff" },
65
66         /* BLOBLISTT_VENDOR_AREA */
67 };
68
69 const char *bloblist_tag_name(enum bloblist_tag_t tag)
70 {
71         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         }
77
78         return "invalid";
79 }
80
81 static struct bloblist_rec *bloblist_first_blob(struct bloblist_hdr *hdr)
82 {
83         if (hdr->used_size <= hdr->hdr_size)
84                 return NULL;
85         return (struct bloblist_rec *)((void *)hdr + hdr->hdr_size);
86 }
87
88 static inline uint rec_hdr_size(struct bloblist_rec *rec)
89 {
90         return (rec->tag_and_hdr_size & BLOBLISTR_HDR_SIZE_MASK) >>
91                 BLOBLISTR_HDR_SIZE_SHIFT;
92 }
93
94 static inline uint rec_tag(struct bloblist_rec *rec)
95 {
96         return (rec->tag_and_hdr_size & BLOBLISTR_TAG_MASK) >>
97                 BLOBLISTR_TAG_SHIFT;
98 }
99
100 static ulong bloblist_blob_end_ofs(struct bloblist_hdr *hdr,
101                                    struct bloblist_rec *rec)
102 {
103         ulong offset;
104
105         offset = (void *)rec - (void *)hdr;
106         /*
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);
113
114         return offset;
115 }
116
117 static 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
122         if (offset >= hdr->used_size)
123                 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
132 static 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) {
141                 if (rec_tag(rec) == tag)
142                         return rec;
143         }
144
145         return NULL;
146 }
147
148 static int bloblist_addrec(uint tag, int size, int align_log2,
149                            struct bloblist_rec **recp)
150 {
151         struct bloblist_hdr *hdr = gd->bloblist;
152         struct bloblist_rec *rec;
153         int data_start, aligned_start, new_alloced;
154
155         if (!align_log2)
156                 align_log2 = BLOBLIST_BLOB_ALIGN_LOG2;
157
158         /* Figure out where the new data will start */
159         data_start = map_to_sysmem(hdr) + hdr->used_size + sizeof(*rec);
160
161         /* Align the address and then calculate the offset from used size */
162         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 */
175                 data_start = map_to_sysmem(hdr) + hdr->used_size + sizeof(*vrec);
176         }
177
178         /* Calculate the new allocated total */
179         new_alloced = data_start - map_to_sysmem(hdr) +
180                 ALIGN(size, 1U << align_log2);
181
182         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);
186                 return log_msg_ret("bloblist add", -ENOSPC);
187         }
188         rec = (void *)hdr + hdr->used_size;
189
190         rec->tag_and_hdr_size = tag | sizeof(*rec) << BLOBLISTR_HDR_SIZE_SHIFT;
191         rec->size = size;
192
193         /* Zero the record data */
194         memset((void *)rec + rec_hdr_size(rec), '\0', rec->size);
195
196         hdr->used_size = new_alloced;
197         *recp = rec;
198
199         return 0;
200 }
201
202 static int bloblist_ensurerec(uint tag, struct bloblist_rec **recp, int size,
203                               int align_log2)
204 {
205         struct bloblist_rec *rec;
206
207         rec = bloblist_findrec(tag);
208         if (rec) {
209                 if (size && size != rec->size) {
210                         *recp = rec;
211                         return -ESPIPE;
212                 }
213         } else {
214                 int ret;
215
216                 ret = bloblist_addrec(tag, size, align_log2, &rec);
217                 if (ret)
218                         return ret;
219         }
220         *recp = rec;
221
222         return 0;
223 }
224
225 void *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
235         return (void *)rec + rec_hdr_size(rec);
236 }
237
238 void *bloblist_add(uint tag, int size, int align_log2)
239 {
240         struct bloblist_rec *rec;
241
242         if (bloblist_addrec(tag, size, align_log2, &rec))
243                 return NULL;
244
245         return (void *)rec + rec_hdr_size(rec);
246 }
247
248 int bloblist_ensure_size(uint tag, int size, int align_log2, void **blobp)
249 {
250         struct bloblist_rec *rec;
251         int ret;
252
253         ret = bloblist_ensurerec(tag, &rec, size, align_log2);
254         if (ret)
255                 return ret;
256         *blobp = (void *)rec + rec_hdr_size(rec);
257
258         return 0;
259 }
260
261 void *bloblist_ensure(uint tag, int size)
262 {
263         struct bloblist_rec *rec;
264
265         if (bloblist_ensurerec(tag, &rec, size, 0))
266                 return NULL;
267
268         return (void *)rec + rec_hdr_size(rec);
269 }
270
271 int bloblist_ensure_size_ret(uint tag, int *sizep, void **blobp)
272 {
273         struct bloblist_rec *rec;
274         int ret;
275
276         ret = bloblist_ensurerec(tag, &rec, *sizep, 0);
277         if (ret == -ESPIPE)
278                 *sizep = rec->size;
279         else if (ret)
280                 return ret;
281         *blobp = (void *)rec + rec_hdr_size(rec);
282
283         return 0;
284 }
285
286 static 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) */
291         int new_alloced;
292         ulong next_ofs; /* Offset of the record after @rec */
293
294         expand_by = ALIGN(new_size - rec->size, BLOBLIST_BLOB_ALIGN);
295         new_alloced = ALIGN(hdr->used_size + expand_by, BLOBLIST_BLOB_ALIGN);
296         if (new_size < 0) {
297                 log_debug("Attempt to shrink blob size below 0 (%x)\n",
298                           new_size);
299                 return log_msg_ret("size", -EINVAL);
300         }
301         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);
305                 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);
310         if (next_ofs != hdr->used_size) {
311                 memmove((void *)hdr + next_ofs + expand_by,
312                         (void *)hdr + next_ofs, new_alloced - next_ofs);
313         }
314         hdr->used_size = new_alloced;
315
316         /* Zero the new part of the blob */
317         if (expand_by > 0) {
318                 memset((void *)rec + rec_hdr_size(rec) + rec->size, '\0',
319                        new_size - rec->size);
320         }
321
322         /* Update the size of this blob */
323         rec->size = new_size;
324
325         return 0;
326 }
327
328 int 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
344 static u32 bloblist_calc_chksum(struct bloblist_hdr *hdr)
345 {
346         u8 chksum;
347
348         chksum = table_compute_checksum(hdr, hdr->used_size);
349         chksum += hdr->chksum;
350
351         return chksum;
352 }
353
354 int bloblist_new(ulong addr, uint size, uint flags, uint align_log2)
355 {
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;
368         hdr->used_size = hdr->hdr_size;
369         hdr->total_size = size;
370         hdr->align_log2 = align_log2 ? align_log2 : BLOBLIST_BLOB_ALIGN_LOG2;
371         hdr->chksum = 0;
372         gd->bloblist = hdr;
373
374         return 0;
375 }
376
377 int 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);
387         if (!hdr->total_size || (size && hdr->total_size != size))
388                 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
394         chksum = bloblist_calc_chksum(hdr);
395         if (hdr->chksum != chksum) {
396                 log_err("Checksum %x != %x\n", hdr->chksum, chksum);
397                 return log_msg_ret("Bad checksum", -EIO);
398         }
399         gd->bloblist = hdr;
400
401         return 0;
402 }
403
404 int bloblist_finish(void)
405 {
406         struct bloblist_hdr *hdr = gd->bloblist;
407
408         hdr->chksum = bloblist_calc_chksum(hdr);
409         log_debug("Finished bloblist size %lx at %lx\n", (ulong)hdr->used_size,
410                   (ulong)map_to_sysmem(hdr));
411
412         return 0;
413 }
414
415 ulong bloblist_get_base(void)
416 {
417         return map_to_sysmem(gd->bloblist);
418 }
419
420 ulong bloblist_get_size(void)
421 {
422         struct bloblist_hdr *hdr = gd->bloblist;
423
424         return hdr->used_size;
425 }
426
427 ulong bloblist_get_total_size(void)
428 {
429         struct bloblist_hdr *hdr = gd->bloblist;
430
431         return hdr->total_size;
432 }
433
434 void bloblist_get_stats(ulong *basep, ulong *tsizep, ulong *usizep)
435 {
436         struct bloblist_hdr *hdr = gd->bloblist;
437
438         *basep = map_to_sysmem(gd->bloblist);
439         *tsizep = hdr->total_size;
440         *usizep = hdr->used_size;
441 }
442
443 static void show_value(const char *prompt, ulong value)
444 {
445         printf("%s:%*s %-5lx  ", prompt, 10 - (int)strlen(prompt), "", value);
446         print_size(value, "\n");
447 }
448
449 void bloblist_show_stats(void)
450 {
451         ulong base, tsize, usize;
452
453         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);
458 }
459
460 void bloblist_show_list(void)
461 {
462         struct bloblist_hdr *hdr = gd->bloblist;
463         struct bloblist_rec *rec;
464
465         printf("%-8s  %8s   Tag Name\n", "Address", "Size");
466         for (rec = bloblist_first_blob(hdr); rec;
467              rec = bloblist_next_blob(hdr, rec)) {
468                 printf("%08lx  %8x  %4x %s\n",
469                        (ulong)map_to_sysmem((void *)rec + rec_hdr_size(rec)),
470                        rec->size, rec_tag(rec),
471                        bloblist_tag_name(rec_tag(rec)));
472         }
473 }
474
475 void bloblist_reloc(void *to, uint to_size, void *from, uint from_size)
476 {
477         struct bloblist_hdr *hdr;
478
479         memcpy(to, from, from_size);
480         hdr = to;
481         hdr->total_size = to_size;
482 }
483
484 int bloblist_init(void)
485 {
486         bool fixed = IS_ENABLED(CONFIG_BLOBLIST_FIXED);
487         int ret = -ENOENT;
488         ulong addr, size;
489         bool expected;
490
491         /**
492          * We don't expect to find an existing bloblist in the first phase of
493          * U-Boot that runs. Also we have no way to receive the address of an
494          * allocated bloblist from a previous stage, so it must be at a fixed
495          * address.
496          */
497         expected = fixed && !u_boot_first_phase();
498         if (spl_prev_phase() == PHASE_TPL && !IS_ENABLED(CONFIG_TPL_BLOBLIST))
499                 expected = false;
500         if (fixed)
501                 addr = IF_ENABLED_INT(CONFIG_BLOBLIST_FIXED,
502                                       CONFIG_BLOBLIST_ADDR);
503         size = CONFIG_BLOBLIST_SIZE;
504         if (expected) {
505                 ret = bloblist_check(addr, size);
506                 if (ret) {
507                         log_warning("Expected bloblist at %lx not found (err=%d)\n",
508                                     addr, ret);
509                 } else {
510                         /* Get the real size, if it is not what we expected */
511                         size = gd->bloblist->total_size;
512                 }
513         }
514         if (ret) {
515                 if (CONFIG_IS_ENABLED(BLOBLIST_ALLOC)) {
516                         void *ptr = memalign(BLOBLIST_ALIGN, size);
517
518                         if (!ptr)
519                                 return log_msg_ret("alloc", -ENOMEM);
520                         addr = map_to_sysmem(ptr);
521                 } else if (!fixed) {
522                         return log_msg_ret("!fixed", ret);
523                 }
524                 log_debug("Creating new bloblist size %lx at %lx\n", size,
525                           addr);
526                 ret = bloblist_new(addr, size, 0, 0);
527         } else {
528                 log_debug("Found existing bloblist size %lx at %lx\n", size,
529                           addr);
530         }
531         if (ret)
532                 return log_msg_ret("ini", ret);
533         gd->flags |= GD_FLG_BLOBLIST_READY;
534
535         return 0;
536 }
537
538 int bloblist_maybe_init(void)
539 {
540         if (CONFIG_IS_ENABLED(BLOBLIST) && !(gd->flags & GD_FLG_BLOBLIST_READY))
541                 return bloblist_init();
542
543         return 0;
544 }
This page took 0.054376 seconds and 4 git commands to generate.