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