xref: /openbmc/u-boot/lib/libavb/avb_property_descriptor.h (revision 8e51c0f254063f4da2e91c2cf47fad37285be73d)
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * SPDX-License-Identifier:	MIT
5  */
6 
7 #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION)
8 #error "Never include this file directly, include libavb.h instead."
9 #endif
10 
11 #ifndef AVB_PROPERTY_DESCRIPTOR_H_
12 #define AVB_PROPERTY_DESCRIPTOR_H_
13 
14 #include "avb_descriptor.h"
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 /* A descriptor for properties (free-form key/value pairs).
21  *
22  * Following this struct are |key_num_bytes| bytes of key data,
23  * followed by a NUL byte, then |value_num_bytes| bytes of value data,
24  * followed by a NUL byte and then enough padding to make the combined
25  * size a multiple of 8.
26  */
27 typedef struct AvbPropertyDescriptor {
28   AvbDescriptor parent_descriptor;
29   uint64_t key_num_bytes;
30   uint64_t value_num_bytes;
31 } AVB_ATTR_PACKED AvbPropertyDescriptor;
32 
33 /* Copies |src| to |dest| and validates, byte-swapping fields in the
34  * process if needed. Returns true if valid, false if invalid.
35  *
36  * Data following the struct is not validated nor copied.
37  */
38 bool avb_property_descriptor_validate_and_byteswap(
39     const AvbPropertyDescriptor* src,
40     AvbPropertyDescriptor* dest) AVB_ATTR_WARN_UNUSED_RESULT;
41 
42 /* Convenience function for looking up the value for a property with
43  * name |key| in a vbmeta image. If |key_size| is 0, |key| must be
44  * NUL-terminated.
45  *
46  * The |image_data| parameter must be a pointer to a vbmeta image of
47  * size |image_size|.
48  *
49  * This function returns a pointer to the value inside the passed-in
50  * image or NULL if not found. Note that the value is always
51  * guaranteed to be followed by a NUL byte.
52  *
53  * If the value was found and |out_value_size| is not NULL, the size
54  * of the value is returned there.
55  *
56  * This function is O(n) in number of descriptors so if you need to
57  * look up a lot of values, you may want to build a more efficient
58  * lookup-table by manually walking all descriptors using
59  * avb_descriptor_foreach().
60  *
61  * Before using this function, you MUST verify |image_data| with
62  * avb_vbmeta_image_verify() and reject it unless it's signed by a
63  * known good public key.
64  */
65 const char* avb_property_lookup(const uint8_t* image_data,
66                                 size_t image_size,
67                                 const char* key,
68                                 size_t key_size,
69                                 size_t* out_value_size)
70     AVB_ATTR_WARN_UNUSED_RESULT;
71 
72 /* Like avb_property_lookup() but parses the intial portions of the
73  * value as an unsigned 64-bit integer. Both decimal and hexadecimal
74  * representations (e.g. "0x2a") are supported. Returns false on
75  * failure and true on success. On success, the parsed value is
76  * returned in |out_value|.
77  */
78 bool avb_property_lookup_uint64(const uint8_t* image_data,
79                                 size_t image_size,
80                                 const char* key,
81                                 size_t key_size,
82                                 uint64_t* out_value)
83     AVB_ATTR_WARN_UNUSED_RESULT;
84 
85 #ifdef __cplusplus
86 }
87 #endif
88 
89 #endif /* AVB_PROPERTY_DESCRIPTOR_H_ */
90