blob: 29ea5a768a61ee1a0423cf08030c2f0d8982aa1c [file] [log] [blame]
Simon Glass9f407d42018-11-15 18:43:50 -07001/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * This provides a standard way of passing information between boot phases
4 * (TPL -> SPL -> U-Boot proper.)
5 *
6 * A list of blobs of data, tagged with their owner. The list resides in memory
7 * and can be updated by SPL, U-Boot, etc.
8 *
9 * Copyright 2018 Google, Inc
10 * Written by Simon Glass <sjg@chromium.org>
11 */
12
13#ifndef __BLOBLIST_H
14#define __BLOBLIST_H
15
Simon Glassff3bd492022-01-12 19:26:16 -070016#include <mapmem.h>
17
Simon Glass9f407d42018-11-15 18:43:50 -070018enum {
19 BLOBLIST_VERSION = 0,
20 BLOBLIST_MAGIC = 0xb00757a3,
21 BLOBLIST_ALIGN = 16,
22};
23
Simon Glass4aed2272020-09-19 18:49:26 -060024/* Supported tags - add new ones to tag_name in bloblist.c */
Simon Glass9f407d42018-11-15 18:43:50 -070025enum bloblist_tag_t {
26 BLOBLISTT_NONE = 0,
27
28 /* Vendor-specific tags are permitted here */
29 BLOBLISTT_EC_HOSTEVENT, /* Chromium OS EC host-event mask */
30 BLOBLISTT_SPL_HANDOFF, /* Hand-off info from SPL */
31 BLOBLISTT_VBOOT_CTX, /* Chromium OS verified boot context */
32 BLOBLISTT_VBOOT_HANDOFF, /* Chromium OS internal handoff info */
Simon Glass99e555a2020-09-22 12:44:55 -060033 /*
34 * Advanced Configuration and Power Interface Global Non-Volatile
35 * Sleeping table. This forms part of the ACPI tables passed to Linux.
36 */
37 BLOBLISTT_ACPI_GNVS,
Simon Glassc9cc37d2020-09-22 12:45:03 -060038 BLOBLISTT_INTEL_VBT, /* Intel Video-BIOS table */
Simon Glass9179c352020-09-22 12:45:32 -060039 BLOBLISTT_TPM2_TCG_LOG, /* TPM v2 log space */
Simon Glass77bb1c62020-09-22 12:45:33 -060040 BLOBLISTT_TCPA_LOG, /* TPM log space */
Simon Glassd2cb7a22020-11-04 09:57:25 -070041 BLOBLISTT_ACPI_TABLES, /* ACPI tables for x86 */
42 BLOBLISTT_SMBIOS_TABLES, /* SMBIOS tables for x86 */
Simon Glass4aed2272020-09-19 18:49:26 -060043
44 BLOBLISTT_COUNT
Simon Glass9f407d42018-11-15 18:43:50 -070045};
46
47/**
48 * struct bloblist_hdr - header for the bloblist
49 *
50 * This is stored at the start of the bloblist which is always on a 16-byte
51 * boundary. Records follow this header. The bloblist normally stays in the
52 * same place in memory as SPL and U-Boot execute, but it can be safely moved
53 * around.
54 *
55 * None of the bloblist structures contain pointers but it is possible to put
56 * pointers inside a bloblist record if desired. This is not encouraged,
57 * since it can make part of the bloblist inaccessible if the pointer is
58 * no-longer valid. It is better to just store all the data inside a bloblist
59 * record.
60 *
61 * Each bloblist record is aligned to a 16-byte boundary and follows immediately
62 * from the last.
63 *
Simon Glassff3bd492022-01-12 19:26:16 -070064 * @magic: BLOBLIST_MAGIC
Simon Glass9f407d42018-11-15 18:43:50 -070065 * @version: BLOBLIST_VERSION
66 * @hdr_size: Size of this header, normally sizeof(struct bloblist_hdr). The
67 * first bloblist_rec starts at this offset from the start of the header
68 * @flags: Space for BLOBLISTF_... flags (none yet)
Simon Glassfaff5542021-07-05 16:32:54 -060069 * @size: Total size of the bloblist (non-zero if valid) including this header.
Simon Glass9f407d42018-11-15 18:43:50 -070070 * The bloblist extends for this many bytes from the start of this header.
Simon Glassfaff5542021-07-05 16:32:54 -060071 * When adding new records, the bloblist can grow up to this size.
72 * @alloced: Total size allocated so far for this bloblist. This starts out as
Simon Glass9f407d42018-11-15 18:43:50 -070073 * sizeof(bloblist_hdr) since we need at least that much space to store a
74 * valid bloblist
Simon Glasse9b6b2c2020-09-19 18:49:30 -060075 * @spare: Spare space (for future use)
Simon Glass9f407d42018-11-15 18:43:50 -070076 * @chksum: CRC32 for the entire bloblist allocated area. Since any of the
77 * blobs can be altered after being created, this checksum is only valid
78 * when the bloblist is finalised before jumping to the next stage of boot.
79 * Note: @chksum is last to make it easier to exclude it from the checksum
80 * calculation.
81 */
82struct bloblist_hdr {
Simon Glassff3bd492022-01-12 19:26:16 -070083 u32 magic;
Simon Glass9f407d42018-11-15 18:43:50 -070084 u32 version;
85 u32 hdr_size;
86 u32 flags;
Simon Glass9f407d42018-11-15 18:43:50 -070087
88 u32 size;
89 u32 alloced;
90 u32 spare;
91 u32 chksum;
92};
93
94/**
95 * struct bloblist_rec - record for the bloblist
96 *
97 * NOTE: Only exported for testing purposes. Do not use this struct.
98 *
99 * The bloblist contains a number of records each consisting of this record
100 * structure followed by the data contained. Each records is 16-byte aligned.
101 *
102 * @tag: Tag indicating what the record contains
103 * @hdr_size: Size of this header, normally sizeof(struct bloblist_rec). The
104 * record's data starts at this offset from the start of the record
105 * @size: Size of record in bytes, excluding the header size. This does not
106 * need to be aligned (e.g. 3 is OK).
107 * @spare: Spare space for other things
108 */
109struct bloblist_rec {
110 u32 tag;
111 u32 hdr_size;
112 u32 size;
113 u32 spare;
114};
115
116/**
Simon Glassff3bd492022-01-12 19:26:16 -0700117 * bloblist_check_magic() - return a bloblist if the magic matches
118 *
119 * @addr: Address to check
120 * @return pointer to bloblist, if the magic matches, else NULL
121 */
122static inline void *bloblist_check_magic(ulong addr)
123{
124 u32 *ptr;
125
126 if (!addr)
127 return NULL;
128 ptr = map_sysmem(addr, 0);
129 if (*ptr != BLOBLIST_MAGIC)
130 return NULL;
131
132 return ptr;
133}
134
135/**
Simon Glass9f407d42018-11-15 18:43:50 -0700136 * bloblist_find() - Find a blob
137 *
138 * Searches the bloblist and returns the blob with the matching tag
139 *
140 * @tag: Tag to search for (enum bloblist_tag_t)
Simon Glasse9b6b2c2020-09-19 18:49:30 -0600141 * @size: Expected size of the blob, or 0 for any size
Simon Glass9f407d42018-11-15 18:43:50 -0700142 * @return pointer to blob if found, or NULL if not found, or a blob was found
143 * but it is the wrong size
144 */
145void *bloblist_find(uint tag, int size);
146
147/**
148 * bloblist_add() - Add a new blob
149 *
150 * Add a new blob to the bloblist
151 *
152 * This should only be called if you konw there is no existing blob for a
153 * particular tag. It is typically safe to call in the first phase of U-Boot
154 * (e.g. TPL or SPL). After that, bloblist_ensure() should be used instead.
155 *
156 * @tag: Tag to add (enum bloblist_tag_t)
157 * @size: Size of the blob
Simon Glass4c1497e2020-09-19 18:49:29 -0600158 * @align: Alignment of the blob (in bytes), 0 for default
Simon Glass9f407d42018-11-15 18:43:50 -0700159 * @return pointer to the newly added block, or NULL if there is not enough
160 * space for the blob
161 */
Simon Glass4c1497e2020-09-19 18:49:29 -0600162void *bloblist_add(uint tag, int size, int align);
Simon Glass9f407d42018-11-15 18:43:50 -0700163
164/**
165 * bloblist_ensure_size() - Find or add a blob
166 *
167 * Find an existing blob, or add a new one if not found
168 *
169 * @tag: Tag to add (enum bloblist_tag_t)
170 * @size: Size of the blob
171 * @blobp: Returns a pointer to blob on success
Simon Glass4c1497e2020-09-19 18:49:29 -0600172 * @align: Alignment of the blob (in bytes), 0 for default
Simon Glass9f407d42018-11-15 18:43:50 -0700173 * @return 0 if OK, -ENOSPC if it is missing and could not be added due to lack
174 * of space, or -ESPIPE it exists but has the wrong size
175 */
Simon Glass4c1497e2020-09-19 18:49:29 -0600176int bloblist_ensure_size(uint tag, int size, int align, void **blobp);
Simon Glass9f407d42018-11-15 18:43:50 -0700177
178/**
179 * bloblist_ensure() - Find or add a blob
180 *
181 * Find an existing blob, or add a new one if not found
182 *
183 * @tag: Tag to add (enum bloblist_tag_t)
184 * @size: Size of the blob
185 * @return pointer to blob, or NULL if it is missing and could not be added due
186 * to lack of space, or it exists but has the wrong size
187 */
188void *bloblist_ensure(uint tag, int size);
189
190/**
Simon Glass5b044542020-01-27 08:49:50 -0700191 * bloblist_ensure_size_ret() - Find or add a blob
192 *
193 * Find an existing blob, or add a new one if not found
194 *
195 * @tag: Tag to add (enum bloblist_tag_t)
196 * @sizep: Size of the blob to create; returns size of actual blob
197 * @blobp: Returns a pointer to blob on success
198 * @return 0 if OK, -ENOSPC if it is missing and could not be added due to lack
199 * of space
200 */
201int bloblist_ensure_size_ret(uint tag, int *sizep, void **blobp);
202
203/**
Simon Glass1fe59372021-07-05 16:32:53 -0600204 * bloblist_resize() - resize a blob
205 *
206 * Any blobs above this one are relocated up or down. The resized blob remains
207 * in the same place.
208 *
209 * @tag: Tag to add (enum bloblist_tag_t)
210 * @new_size: New size of the blob (>0 to expand, <0 to contract)
211 * @return 0 if OK, -ENOSPC if the bloblist does not have enough space, -ENOENT
212 * if the tag is not found
213 */
214int bloblist_resize(uint tag, int new_size);
215
216/**
Simon Glass9f407d42018-11-15 18:43:50 -0700217 * bloblist_new() - Create a new, empty bloblist of a given size
218 *
219 * @addr: Address of bloblist
220 * @size: Initial size for bloblist
221 * @flags: Flags to use for bloblist
222 * @return 0 if OK, -EFAULT if addr is not aligned correctly, -ENOSPC is the
223 * area is not large enough
224 */
225int bloblist_new(ulong addr, uint size, uint flags);
226
227/**
228 * bloblist_check() - Check if a bloblist exists
229 *
230 * @addr: Address of bloblist
231 * @size: Expected size of blobsize, or 0 to detect the size
232 * @return 0 if OK, -ENOENT if the magic number doesn't match (indicating that
233 * there problem is no bloblist at the given address), -EPROTONOSUPPORT
234 * if the version does not match, -EIO if the checksum does not match,
Simon Glass02247c12020-01-27 08:49:51 -0700235 * -EFBIG if the expected size does not match the detected size, -ENOSPC
236 * if the size is not large enough to hold the headers
Simon Glass9f407d42018-11-15 18:43:50 -0700237 */
238int bloblist_check(ulong addr, uint size);
239
240/**
241 * bloblist_finish() - Set up the bloblist for the next U-Boot part
242 *
243 * This sets the correct checksum for the bloblist. This ensures that the
244 * bloblist will be detected correctly by the next phase of U-Boot.
245 *
246 * @return 0
247 */
248int bloblist_finish(void);
249
250/**
Simon Glass4aed2272020-09-19 18:49:26 -0600251 * bloblist_get_stats() - Get information about the bloblist
252 *
253 * This returns useful information about the bloblist
Simon Glassfaff5542021-07-05 16:32:54 -0600254 *
255 * @basep: Returns base address of bloblist
256 * @sizep: Returns the number of bytes used in the bloblist
257 * @allocedp: Returns the total space allocated to the bloblist
Simon Glass4aed2272020-09-19 18:49:26 -0600258 */
259void bloblist_get_stats(ulong *basep, ulong *sizep, ulong *allocedp);
260
261/**
262 * bloblist_show_stats() - Show information about the bloblist
263 *
264 * This shows useful information about the bloblist on the console
265 */
266void bloblist_show_stats(void);
267
268/**
269 * bloblist_show_list() - Show a list of blobs in the bloblist
270 *
271 * This shows a list of blobs, showing their address, size and tag.
272 */
273void bloblist_show_list(void);
274
275/**
276 * bloblist_tag_name() - Get the name for a tag
277 *
278 * @tag: Tag to check
279 * @return name of tag, or "invalid" if an invalid tag is provided
280 */
281const char *bloblist_tag_name(enum bloblist_tag_t tag);
282
283/**
Simon Glass9fe06462021-01-13 20:29:43 -0700284 * bloblist_reloc() - Relocate the bloblist and optionally resize it
285 *
286 * @to: Pointer to new bloblist location (must not overlap old location)
287 * @to:size: New size for bloblist (must be larger than from_size)
288 * @from: Pointer to bloblist to relocate
289 * @from_size: Size of bloblist to relocate
290 */
291void bloblist_reloc(void *to, uint to_size, void *from, uint from_size);
292
293/**
Simon Glass9f407d42018-11-15 18:43:50 -0700294 * bloblist_init() - Init the bloblist system with a single bloblist
295 *
296 * This uses CONFIG_BLOBLIST_ADDR and CONFIG_BLOBLIST_SIZE to set up a bloblist
297 * for use by U-Boot.
298 */
299int bloblist_init(void);
300
301#endif /* __BLOBLIST_H */