blob: 7eff709ec8d68fbe4f4d09bf8ffc87ff973da2f7 [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 * This provides a standard way of passing information between boot phases
4 * (TPL -> SPL -> U-Boot proper.)
5 *
Simon Glass68ff6d32022-03-13 16:22:48 -06006 * It consists of a list of blobs of data, tagged with their owner / contents.
7 * The list resides in memory and can be updated by SPL, U-Boot, etc.
8 *
9 * Design goals for bloblist:
10 *
11 * 1. Small and efficient structure. This avoids UUIDs or 16-byte name fields,
12 * since a 32-bit tag provides enough space for all the tags we will even need.
13 * If UUIDs are desired, they can be added inside a particular blob.
14 *
15 * 2. Avoids use of pointers, so the structure can be relocated in memory. The
16 * data in each blob is inline, rather than using pointers.
17 *
18 * 3. Bloblist is designed to start small in TPL or SPL, when only a few things
19 * are needed, like the memory size or whether console output should be enabled.
20 * Then it can grow in U-Boot proper, e.g. to include space for ACPI tables.
21 *
22 * 4. The bloblist structure is simple enough that it can be implemented in a
23 * small amount of C code. The API does not require use of strings or UUIDs,
24 * which would add to code size. For Thumb-2 the code size needed in SPL is
25 * approximately 940 bytes (e.g. for chromebook_bob).
26 *
27 * 5. Bloblist uses 16-byte alignment internally and is designed to start on a
28 * 16-byte boundary. Its headers are multiples of 16 bytes. This makes it easier
29 * to deal with data structures which need this level of alignment, such as ACPI
30 * tables. For use in SPL and TPL the alignment can be relaxed, since it can be
31 * relocated to an aligned address in U-Boot proper.
32 *
33 * 6. Bloblist is designed to be passed to Linux as reserved memory. While linux
34 * doesn't understand the bloblist header, it can be passed the indivdual blobs.
35 * For example, ACPI tables can reside in a blob and the address of those is
36 * passed to Linux, without Linux ever being away of the existence of a
37 * bloblist. Having all the blobs contiguous in memory simplifies the
38 * reserved-memory space.
39 *
40 * 7. Bloblist tags are defined in the enum below. There is an area for
41 * project-specific stuff (e.g. U-Boot, TF-A) and vendor-specific stuff, e.g.
42 * something used only on a particular SoC. There is also a private area for
43 * temporary, local use.
44 *
45 * 8. Bloblist includes a simple checksum, so that each boot phase can update
46 * this and allow the next phase to check that all is well. While the bloblist
47 * is small, this is quite cheap to calculate. When it grows (e.g. in U-Boot\
48 * proper), the CPU is likely running faster, so it is not prohibitive. Having
49 * said that, U-Boot is often the last phase that uses bloblist, so calculating
50 * the checksum there may not be necessary.
51 *
52 * 9. It would be possible to extend bloblist to support a non-contiguous
53 * structure, e.g. by creating a blob type that points to the next bloblist.
54 * This does not seem necessary for now. It adds complexity and code. We can
55 * always just copy it.
56 *
57 * 10. Bloblist is designed for simple structures, those that can be defined by
58 * a single C struct. More complex structures should be passed in a device tree.
59 * There are some exceptions, chiefly the various binary structures that Intel
60 * is fond of creating. But device tree provides a dictionary-type format which
61 * is fairly efficient (for use in U-Boot proper and Linux at least), along with
62 * a schema and a good set of tools. New formats should be designed around
63 * device tree rather than creating new binary formats, unless they are needed
64 * early in boot (where libfdt's 3KB of overhead is too large) and are trival
65 * enough to be described by a C struct.
Simon Glass9f407d42018-11-15 18:43:50 -070066 *
67 * Copyright 2018 Google, Inc
68 * Written by Simon Glass <sjg@chromium.org>
69 */
70
71#ifndef __BLOBLIST_H
72#define __BLOBLIST_H
73
Simon Glassff3bd492022-01-12 19:26:16 -070074#include <mapmem.h>
75
Simon Glass9f407d42018-11-15 18:43:50 -070076enum {
Simon Glass0b9f77f2023-12-27 13:07:02 -080077 BLOBLIST_VERSION = 1,
Simon Glass5a53e562023-12-27 13:07:01 -080078 BLOBLIST_MAGIC = 0x4a0fb10b,
Simon Glass1a2e02f2023-12-27 13:07:00 -080079
80 BLOBLIST_ALIGN_LOG2 = 3,
81 BLOBLIST_ALIGN = 1 << BLOBLIST_ALIGN_LOG2,
Simon Glass9f407d42018-11-15 18:43:50 -070082};
83
Simon Glass4aed2272020-09-19 18:49:26 -060084/* Supported tags - add new ones to tag_name in bloblist.c */
Simon Glass9f407d42018-11-15 18:43:50 -070085enum bloblist_tag_t {
Simon Glasse748e4b2023-12-27 13:06:59 -080086 BLOBLISTT_VOID = 0,
Simon Glass9f407d42018-11-15 18:43:50 -070087
Simon Glassf16ec772022-01-12 19:26:19 -070088 /*
89 * Standard area to allocate blobs used across firmware components, for
90 * things that are very commonly used, particularly in multiple
91 * projects.
92 */
93 BLOBLISTT_AREA_FIRMWARE_TOP = 0x1,
Simon Glasse748e4b2023-12-27 13:06:59 -080094 /*
95 * Devicetree for use by firmware. On some platforms this is passed to
96 * the OS also
97 */
98 BLOBLISTT_CONTROL_FDT = 1,
99 BLOBLISTT_HOB_BLOCK = 2,
100 BLOBLISTT_HOB_LIST = 3,
101 BLOBLISTT_ACPI_TABLES = 4,
102 BLOBLISTT_TPM_EVLOG = 5,
103 BLOBLISTT_TPM_CRB_BASE = 6,
Simon Glassf16ec772022-01-12 19:26:19 -0700104
105 /* Standard area to allocate blobs used across firmware components */
Simon Glasse748e4b2023-12-27 13:06:59 -0800106 BLOBLISTT_AREA_FIRMWARE = 0x10,
107 BLOBLISTT_TPM2_TCG_LOG = 0x10, /* TPM v2 log space */
108 BLOBLISTT_TCPA_LOG = 0x11, /* TPM log space */
Simon Glass99e555a2020-09-22 12:44:55 -0600109 /*
110 * Advanced Configuration and Power Interface Global Non-Volatile
111 * Sleeping table. This forms part of the ACPI tables passed to Linux.
112 */
Simon Glasse748e4b2023-12-27 13:06:59 -0800113 BLOBLISTT_ACPI_GNVS = 0x12,
Simon Glass4aed2272020-09-19 18:49:26 -0600114
Simon Glasse748e4b2023-12-27 13:06:59 -0800115 /* Standard area to allocate blobs used for Trusted Firmware */
116 BLOBLISTT_AREA_TF = 0x100,
117 BLOBLISTT_OPTEE_PAGABLE_PART = 0x100,
Simon Glassf16ec772022-01-12 19:26:19 -0700118
Simon Glasse748e4b2023-12-27 13:06:59 -0800119 /* Other standard area to allocate blobs */
120 BLOBLISTT_AREA_OTHER = 0x200,
121 BLOBLISTT_INTEL_VBT = 0x200, /* Intel Video-BIOS table */
122 BLOBLISTT_SMBIOS_TABLES = 0x201, /* SMBIOS tables for x86 */
123 BLOBLISTT_VBOOT_CTX = 0x202, /* Chromium OS verified boot context */
Simon Glassf16ec772022-01-12 19:26:19 -0700124
125 /*
126 * Tags from here are on reserved for private use within a single
127 * firmware binary (i.e. a single executable or phase of a project).
128 * These tags can be passed between binaries within a local
129 * implementation, but cannot be used in upstream code. Allocate a
130 * tag in one of the areas above if you want that.
131 *
Simon Glasse748e4b2023-12-27 13:06:59 -0800132 * Project-specific tags are permitted here. Projects can be open source
133 * or not, but the format of the data must be fuily documented in an
134 * open source project, including all fields, bits, etc. Naming should
135 * be: BLOBLISTT_<project>_<purpose_here>
136 *
137 * Vendor-specific tags are also permitted. Projects can be open source
138 * or not, but the format of the data must be fuily documented in an
139 * open source project, including all fields, bits, etc. Naming should
140 * be BLOBLISTT_<vendor>_<purpose_here>
Simon Glassf16ec772022-01-12 19:26:19 -0700141 */
Simon Glasse748e4b2023-12-27 13:06:59 -0800142 BLOBLISTT_PRIVATE_AREA = 0xfff000,
143 BLOBLISTT_U_BOOT_SPL_HANDOFF = 0xfff000, /* Hand-off info from SPL */
144 BLOBLISTT_VBE = 0xfff001, /* VBE per-phase state */
145 BLOBLISTT_U_BOOT_VIDEO = 0xfff002, /* Video info from SPL */
Simon Glass9f407d42018-11-15 18:43:50 -0700146};
147
148/**
149 * struct bloblist_hdr - header for the bloblist
150 *
151 * This is stored at the start of the bloblist which is always on a 16-byte
152 * boundary. Records follow this header. The bloblist normally stays in the
153 * same place in memory as SPL and U-Boot execute, but it can be safely moved
154 * around.
155 *
Simon Glass20a14932022-01-12 19:26:24 -0700156 * None of the bloblist headers themselves contain pointers but it is possible
157 * to put pointers inside a bloblist record if desired. This is not encouraged,
Simon Glass9f407d42018-11-15 18:43:50 -0700158 * since it can make part of the bloblist inaccessible if the pointer is
159 * no-longer valid. It is better to just store all the data inside a bloblist
160 * record.
161 *
162 * Each bloblist record is aligned to a 16-byte boundary and follows immediately
163 * from the last.
164 *
Simon Glassff3bd492022-01-12 19:26:16 -0700165 * @magic: BLOBLIST_MAGIC
Simon Glass9f407d42018-11-15 18:43:50 -0700166 * @version: BLOBLIST_VERSION
167 * @hdr_size: Size of this header, normally sizeof(struct bloblist_hdr). The
168 * first bloblist_rec starts at this offset from the start of the header
Simon Glass20a14932022-01-12 19:26:24 -0700169 * @flags: Space for BLOBLISTF... flags (none yet)
Simon Glassfaff5542021-07-05 16:32:54 -0600170 * @size: Total size of the bloblist (non-zero if valid) including this header.
Simon Glass9f407d42018-11-15 18:43:50 -0700171 * The bloblist extends for this many bytes from the start of this header.
Simon Glassfaff5542021-07-05 16:32:54 -0600172 * When adding new records, the bloblist can grow up to this size.
173 * @alloced: Total size allocated so far for this bloblist. This starts out as
Simon Glass9f407d42018-11-15 18:43:50 -0700174 * sizeof(bloblist_hdr) since we need at least that much space to store a
175 * valid bloblist
Simon Glasse9b6b2c2020-09-19 18:49:30 -0600176 * @spare: Spare space (for future use)
Simon Glass9f407d42018-11-15 18:43:50 -0700177 * @chksum: CRC32 for the entire bloblist allocated area. Since any of the
178 * blobs can be altered after being created, this checksum is only valid
179 * when the bloblist is finalised before jumping to the next stage of boot.
Simon Glass20a14932022-01-12 19:26:24 -0700180 * Note that chksum is last to make it easier to exclude it from the
181 * checksum calculation.
Simon Glass9f407d42018-11-15 18:43:50 -0700182 */
183struct bloblist_hdr {
Simon Glassff3bd492022-01-12 19:26:16 -0700184 u32 magic;
Simon Glass9f407d42018-11-15 18:43:50 -0700185 u32 version;
186 u32 hdr_size;
187 u32 flags;
Simon Glass9f407d42018-11-15 18:43:50 -0700188
189 u32 size;
190 u32 alloced;
191 u32 spare;
192 u32 chksum;
193};
194
195/**
196 * struct bloblist_rec - record for the bloblist
197 *
Simon Glass9f407d42018-11-15 18:43:50 -0700198 * The bloblist contains a number of records each consisting of this record
199 * structure followed by the data contained. Each records is 16-byte aligned.
200 *
Simon Glass20a14932022-01-12 19:26:24 -0700201 * NOTE: Only exported for testing purposes. Do not use this struct.
202 *
Simon Glass9f407d42018-11-15 18:43:50 -0700203 * @tag: Tag indicating what the record contains
204 * @hdr_size: Size of this header, normally sizeof(struct bloblist_rec). The
205 * record's data starts at this offset from the start of the record
206 * @size: Size of record in bytes, excluding the header size. This does not
207 * need to be aligned (e.g. 3 is OK).
208 * @spare: Spare space for other things
209 */
210struct bloblist_rec {
211 u32 tag;
212 u32 hdr_size;
213 u32 size;
214 u32 spare;
215};
216
217/**
Simon Glassff3bd492022-01-12 19:26:16 -0700218 * bloblist_check_magic() - return a bloblist if the magic matches
219 *
220 * @addr: Address to check
Simon Glass20a14932022-01-12 19:26:24 -0700221 * Return: pointer to bloblist, if the magic matches, else NULL
Simon Glassff3bd492022-01-12 19:26:16 -0700222 */
223static inline void *bloblist_check_magic(ulong addr)
224{
225 u32 *ptr;
226
227 if (!addr)
228 return NULL;
229 ptr = map_sysmem(addr, 0);
230 if (*ptr != BLOBLIST_MAGIC)
231 return NULL;
232
233 return ptr;
234}
235
236/**
Simon Glass9f407d42018-11-15 18:43:50 -0700237 * bloblist_find() - Find a blob
238 *
239 * Searches the bloblist and returns the blob with the matching tag
240 *
241 * @tag: Tag to search for (enum bloblist_tag_t)
Simon Glasse9b6b2c2020-09-19 18:49:30 -0600242 * @size: Expected size of the blob, or 0 for any size
Simon Glass20a14932022-01-12 19:26:24 -0700243 * Return: pointer to blob if found, or NULL if not found, or a blob was found
244 * but it is the wrong size
Simon Glass9f407d42018-11-15 18:43:50 -0700245 */
246void *bloblist_find(uint tag, int size);
247
248/**
249 * bloblist_add() - Add a new blob
250 *
251 * Add a new blob to the bloblist
252 *
253 * This should only be called if you konw there is no existing blob for a
254 * particular tag. It is typically safe to call in the first phase of U-Boot
255 * (e.g. TPL or SPL). After that, bloblist_ensure() should be used instead.
256 *
257 * @tag: Tag to add (enum bloblist_tag_t)
258 * @size: Size of the blob
Simon Glass1a2e02f2023-12-27 13:07:00 -0800259 * @align_log2: Alignment of the blob (in bytes log2), 0 for default
Simon Glass20a14932022-01-12 19:26:24 -0700260 * Return: pointer to the newly added block, or NULL if there is not enough
261 * space for the blob
Simon Glass9f407d42018-11-15 18:43:50 -0700262 */
Simon Glass1a2e02f2023-12-27 13:07:00 -0800263void *bloblist_add(uint tag, int size, int align_log2);
Simon Glass9f407d42018-11-15 18:43:50 -0700264
265/**
266 * bloblist_ensure_size() - Find or add a blob
267 *
268 * Find an existing blob, or add a new one if not found
269 *
270 * @tag: Tag to add (enum bloblist_tag_t)
271 * @size: Size of the blob
272 * @blobp: Returns a pointer to blob on success
Simon Glass1a2e02f2023-12-27 13:07:00 -0800273 * @align_log2: Alignment of the blob (in bytes log2), 0 for default
Simon Glass20a14932022-01-12 19:26:24 -0700274 * Return: 0 if OK, -ENOSPC if it is missing and could not be added due to lack
275 * of space, or -ESPIPE it exists but has the wrong size
Simon Glass9f407d42018-11-15 18:43:50 -0700276 */
Simon Glass1a2e02f2023-12-27 13:07:00 -0800277int bloblist_ensure_size(uint tag, int size, int align_log2, void **blobp);
Simon Glass9f407d42018-11-15 18:43:50 -0700278
279/**
280 * bloblist_ensure() - Find or add a blob
281 *
282 * Find an existing blob, or add a new one if not found
283 *
284 * @tag: Tag to add (enum bloblist_tag_t)
285 * @size: Size of the blob
Simon Glass20a14932022-01-12 19:26:24 -0700286 * Return: pointer to blob, or NULL if it is missing and could not be added due
287 * to lack of space, or it exists but has the wrong size
Simon Glass9f407d42018-11-15 18:43:50 -0700288 */
289void *bloblist_ensure(uint tag, int size);
290
291/**
Simon Glass5b044542020-01-27 08:49:50 -0700292 * bloblist_ensure_size_ret() - Find or add a blob
293 *
294 * Find an existing blob, or add a new one if not found
295 *
296 * @tag: Tag to add (enum bloblist_tag_t)
297 * @sizep: Size of the blob to create; returns size of actual blob
298 * @blobp: Returns a pointer to blob on success
Simon Glass20a14932022-01-12 19:26:24 -0700299 * Return: 0 if OK, -ENOSPC if it is missing and could not be added due to lack
300 * of space
Simon Glass5b044542020-01-27 08:49:50 -0700301 */
302int bloblist_ensure_size_ret(uint tag, int *sizep, void **blobp);
303
304/**
Simon Glass1fe59372021-07-05 16:32:53 -0600305 * bloblist_resize() - resize a blob
306 *
307 * Any blobs above this one are relocated up or down. The resized blob remains
308 * in the same place.
309 *
310 * @tag: Tag to add (enum bloblist_tag_t)
311 * @new_size: New size of the blob (>0 to expand, <0 to contract)
Simon Glass20a14932022-01-12 19:26:24 -0700312 * Return: 0 if OK, -ENOSPC if the bloblist does not have enough space, -ENOENT
313 * if the tag is not found
Simon Glass1fe59372021-07-05 16:32:53 -0600314 */
315int bloblist_resize(uint tag, int new_size);
316
317/**
Simon Glass9f407d42018-11-15 18:43:50 -0700318 * bloblist_new() - Create a new, empty bloblist of a given size
319 *
320 * @addr: Address of bloblist
321 * @size: Initial size for bloblist
322 * @flags: Flags to use for bloblist
Simon Glass20a14932022-01-12 19:26:24 -0700323 * Return: 0 if OK, -EFAULT if addr is not aligned correctly, -ENOSPC is the
324 * area is not large enough
Simon Glass9f407d42018-11-15 18:43:50 -0700325 */
326int bloblist_new(ulong addr, uint size, uint flags);
327
328/**
329 * bloblist_check() - Check if a bloblist exists
330 *
331 * @addr: Address of bloblist
332 * @size: Expected size of blobsize, or 0 to detect the size
Simon Glass20a14932022-01-12 19:26:24 -0700333 * Return: 0 if OK, -ENOENT if the magic number doesn't match (indicating that
334 * there problem is no bloblist at the given address), -EPROTONOSUPPORT
335 * if the version does not match, -EIO if the checksum does not match,
336 * -EFBIG if the expected size does not match the detected size, -ENOSPC
337 * if the size is not large enough to hold the headers
Simon Glass9f407d42018-11-15 18:43:50 -0700338 */
339int bloblist_check(ulong addr, uint size);
340
341/**
342 * bloblist_finish() - Set up the bloblist for the next U-Boot part
343 *
344 * This sets the correct checksum for the bloblist. This ensures that the
345 * bloblist will be detected correctly by the next phase of U-Boot.
346 *
Simon Glass20a14932022-01-12 19:26:24 -0700347 * Return: 0
Simon Glass9f407d42018-11-15 18:43:50 -0700348 */
349int bloblist_finish(void);
350
351/**
Simon Glass4aed2272020-09-19 18:49:26 -0600352 * bloblist_get_stats() - Get information about the bloblist
353 *
354 * This returns useful information about the bloblist
Simon Glassfaff5542021-07-05 16:32:54 -0600355 *
356 * @basep: Returns base address of bloblist
357 * @sizep: Returns the number of bytes used in the bloblist
358 * @allocedp: Returns the total space allocated to the bloblist
Simon Glass4aed2272020-09-19 18:49:26 -0600359 */
360void bloblist_get_stats(ulong *basep, ulong *sizep, ulong *allocedp);
361
362/**
Simon Glasse50a24a2022-01-12 19:26:23 -0700363 * bloblist_get_base() - Get the base address of the bloblist
364 *
Simon Glass20a14932022-01-12 19:26:24 -0700365 * Return: base address of bloblist
Simon Glasse50a24a2022-01-12 19:26:23 -0700366 */
367ulong bloblist_get_base(void);
368
369/**
370 * bloblist_get_size() - Get the size of the bloblist
371 *
Simon Glass20a14932022-01-12 19:26:24 -0700372 * Return: the size in bytes
Simon Glasse50a24a2022-01-12 19:26:23 -0700373 */
374ulong bloblist_get_size(void);
375
376/**
Simon Glass4aed2272020-09-19 18:49:26 -0600377 * bloblist_show_stats() - Show information about the bloblist
378 *
379 * This shows useful information about the bloblist on the console
380 */
381void bloblist_show_stats(void);
382
383/**
384 * bloblist_show_list() - Show a list of blobs in the bloblist
385 *
386 * This shows a list of blobs, showing their address, size and tag.
387 */
388void bloblist_show_list(void);
389
390/**
391 * bloblist_tag_name() - Get the name for a tag
392 *
393 * @tag: Tag to check
Simon Glass20a14932022-01-12 19:26:24 -0700394 * Return: name of tag, or "invalid" if an invalid tag is provided
Simon Glass4aed2272020-09-19 18:49:26 -0600395 */
396const char *bloblist_tag_name(enum bloblist_tag_t tag);
397
398/**
Simon Glass9fe06462021-01-13 20:29:43 -0700399 * bloblist_reloc() - Relocate the bloblist and optionally resize it
400 *
401 * @to: Pointer to new bloblist location (must not overlap old location)
Simon Glass20a14932022-01-12 19:26:24 -0700402 * @to_size: New size for bloblist (must be larger than from_size)
Simon Glass9fe06462021-01-13 20:29:43 -0700403 * @from: Pointer to bloblist to relocate
404 * @from_size: Size of bloblist to relocate
405 */
406void bloblist_reloc(void *to, uint to_size, void *from, uint from_size);
407
408/**
Simon Glass9f407d42018-11-15 18:43:50 -0700409 * bloblist_init() - Init the bloblist system with a single bloblist
410 *
Simon Glass20a14932022-01-12 19:26:24 -0700411 * This locates and sets up the blocklist for use.
412 *
413 * If CONFIG_BLOBLIST_FIXED is selected, it uses CONFIG_BLOBLIST_ADDR and
414 * CONFIG_BLOBLIST_SIZE to set up a bloblist for use by U-Boot.
415 *
416 * If CONFIG_BLOBLIST_ALLOC is selected, it allocates memory for a bloblist of
417 * size CONFIG_BLOBLIST_SIZE.
418 *
419 * If CONFIG_BLOBLIST_PASSAGE is selected, it uses the bloblist in the incoming
420 * standard passage. The size is detected automatically so CONFIG_BLOBLIST_SIZE
421 * can be 0.
422 *
Simon Glass3d653182023-09-26 08:14:51 -0600423 * Sets GD_FLG_BLOBLIST_READY in global_data flags on success
424 *
Simon Glass20a14932022-01-12 19:26:24 -0700425 * Return: 0 if OK, -ve on error
Simon Glass9f407d42018-11-15 18:43:50 -0700426 */
427int bloblist_init(void);
428
Simon Glass3d653182023-09-26 08:14:51 -0600429#if CONFIG_IS_ENABLED(BLOBLIST)
430/**
431 * bloblist_maybe_init() - Init the bloblist system if not already done
432 *
433 * Calls bloblist_init() if the GD_FLG_BLOBLIST_READY flag is not et
434 *
435 * Return: 0 if OK, -ve on error
436 */
437int bloblist_maybe_init(void);
438#else
439static inline int bloblist_maybe_init(void)
440{
441 return 0;
442}
443#endif /* BLOBLIST */
444
Simon Glass9f407d42018-11-15 18:43:50 -0700445#endif /* __BLOBLIST_H */