xref: /aosp_15_r20/external/coreboot/src/commonlib/bsd/include/commonlib/bsd/cbfs_mdata.h (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-or-later */
2 
3 #ifndef _COMMONLIB_BSD_CBFS_MDATA_H_
4 #define _COMMONLIB_BSD_CBFS_MDATA_H_
5 
6 #include <commonlib/bsd/cbfs_serialized.h>
7 #include <stddef.h>
8 #include <stdint.h>
9 
10 /*
11  * Helper structure to allocate space for a blob of metadata on the stack. All functions using
12  * a cbfs_mdata should be getting it via cbfs_walk(), and can rely on the fact that cbfs_walk()
13  * has already fully validated the header (range checks for `len`, `attributes_offset` and
14  * `offset`, and null-termination for `filename`).
15  * NOTE: The fields in any union cbfs_mdata or any of its substructures from cbfs_serialized.h
16  * should always remain in the same byte order as they are stored on flash (= big endian). To
17  * avoid byte-order confusion, fields should always and only be converted to host byte order at
18  * exactly the time they are read from one of these structures into their own separate variable.
19  */
20 union cbfs_mdata {
21 	struct cbfs_file h;
22 	uint8_t raw[CBFS_METADATA_MAX_SIZE];
23 };
24 
25 /* Finds a CBFS attribute in a metadata block. Attribute returned as-is (still big-endian).
26    If |size| is not 0, will check that it matches the length of the attribute (if found)...
27    else caller is responsible for checking the |len| field to avoid reading out-of-bounds. */
28 const void *cbfs_find_attr(const union cbfs_mdata *mdata, uint32_t attr_tag, size_t size_check);
29 
30 /* Returns pointer to CBFS file hash structure in metadata attributes, or NULL if invalid. */
31 const struct vb2_hash *cbfs_file_hash(const union cbfs_mdata *mdata);
32 
33 #endif	/* _COMMONLIB_BSD_CBFS_MDATA_H_ */
34