xref: /aosp_15_r20/external/avb/libavb/avb_property_descriptor.h (revision d289c2ba6de359471b23d594623b906876bc48a0)
1*d289c2baSAndroid Build Coastguard Worker /*
2*d289c2baSAndroid Build Coastguard Worker  * Copyright (C) 2016 The Android Open Source Project
3*d289c2baSAndroid Build Coastguard Worker  *
4*d289c2baSAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person
5*d289c2baSAndroid Build Coastguard Worker  * obtaining a copy of this software and associated documentation
6*d289c2baSAndroid Build Coastguard Worker  * files (the "Software"), to deal in the Software without
7*d289c2baSAndroid Build Coastguard Worker  * restriction, including without limitation the rights to use, copy,
8*d289c2baSAndroid Build Coastguard Worker  * modify, merge, publish, distribute, sublicense, and/or sell copies
9*d289c2baSAndroid Build Coastguard Worker  * of the Software, and to permit persons to whom the Software is
10*d289c2baSAndroid Build Coastguard Worker  * furnished to do so, subject to the following conditions:
11*d289c2baSAndroid Build Coastguard Worker  *
12*d289c2baSAndroid Build Coastguard Worker  * The above copyright notice and this permission notice shall be
13*d289c2baSAndroid Build Coastguard Worker  * included in all copies or substantial portions of the Software.
14*d289c2baSAndroid Build Coastguard Worker  *
15*d289c2baSAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*d289c2baSAndroid Build Coastguard Worker  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*d289c2baSAndroid Build Coastguard Worker  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18*d289c2baSAndroid Build Coastguard Worker  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19*d289c2baSAndroid Build Coastguard Worker  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20*d289c2baSAndroid Build Coastguard Worker  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21*d289c2baSAndroid Build Coastguard Worker  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*d289c2baSAndroid Build Coastguard Worker  * SOFTWARE.
23*d289c2baSAndroid Build Coastguard Worker  */
24*d289c2baSAndroid Build Coastguard Worker 
25*d289c2baSAndroid Build Coastguard Worker #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION)
26*d289c2baSAndroid Build Coastguard Worker #error "Never include this file directly, include libavb.h instead."
27*d289c2baSAndroid Build Coastguard Worker #endif
28*d289c2baSAndroid Build Coastguard Worker 
29*d289c2baSAndroid Build Coastguard Worker #ifndef AVB_PROPERTY_DESCRIPTOR_H_
30*d289c2baSAndroid Build Coastguard Worker #define AVB_PROPERTY_DESCRIPTOR_H_
31*d289c2baSAndroid Build Coastguard Worker 
32*d289c2baSAndroid Build Coastguard Worker #include "avb_descriptor.h"
33*d289c2baSAndroid Build Coastguard Worker 
34*d289c2baSAndroid Build Coastguard Worker #ifdef __cplusplus
35*d289c2baSAndroid Build Coastguard Worker extern "C" {
36*d289c2baSAndroid Build Coastguard Worker #endif
37*d289c2baSAndroid Build Coastguard Worker 
38*d289c2baSAndroid Build Coastguard Worker /* A descriptor for properties (free-form key/value pairs).
39*d289c2baSAndroid Build Coastguard Worker  *
40*d289c2baSAndroid Build Coastguard Worker  * Following this struct are |key_num_bytes| bytes of key data encoded
41*d289c2baSAndroid Build Coastguard Worker  * as UTF-8, followed by a NUL byte, then |value_num_bytes| bytes of
42*d289c2baSAndroid Build Coastguard Worker  * value data, followed by a NUL byte and then enough padding to make
43*d289c2baSAndroid Build Coastguard Worker  * the combined size a multiple of 8.
44*d289c2baSAndroid Build Coastguard Worker  */
45*d289c2baSAndroid Build Coastguard Worker typedef struct AvbPropertyDescriptor {
46*d289c2baSAndroid Build Coastguard Worker   AvbDescriptor parent_descriptor;
47*d289c2baSAndroid Build Coastguard Worker   uint64_t key_num_bytes;
48*d289c2baSAndroid Build Coastguard Worker   uint64_t value_num_bytes;
49*d289c2baSAndroid Build Coastguard Worker } AVB_ATTR_PACKED AvbPropertyDescriptor;
50*d289c2baSAndroid Build Coastguard Worker 
51*d289c2baSAndroid Build Coastguard Worker /* Copies |src| to |dest| and validates, byte-swapping fields in the
52*d289c2baSAndroid Build Coastguard Worker  * process if needed. Returns true if valid, false if invalid.
53*d289c2baSAndroid Build Coastguard Worker  *
54*d289c2baSAndroid Build Coastguard Worker  * Data following the struct is not validated nor copied.
55*d289c2baSAndroid Build Coastguard Worker  */
56*d289c2baSAndroid Build Coastguard Worker bool avb_property_descriptor_validate_and_byteswap(
57*d289c2baSAndroid Build Coastguard Worker     const AvbPropertyDescriptor* src,
58*d289c2baSAndroid Build Coastguard Worker     AvbPropertyDescriptor* dest) AVB_ATTR_WARN_UNUSED_RESULT;
59*d289c2baSAndroid Build Coastguard Worker 
60*d289c2baSAndroid Build Coastguard Worker /* Convenience function for looking up the value for a property with
61*d289c2baSAndroid Build Coastguard Worker  * name |key| in a vbmeta image. If |key_size| is 0, |key| must be
62*d289c2baSAndroid Build Coastguard Worker  * NUL-terminated.
63*d289c2baSAndroid Build Coastguard Worker  *
64*d289c2baSAndroid Build Coastguard Worker  * The |image_data| parameter must be a pointer to a vbmeta image of
65*d289c2baSAndroid Build Coastguard Worker  * size |image_size|.
66*d289c2baSAndroid Build Coastguard Worker  *
67*d289c2baSAndroid Build Coastguard Worker  * This function returns a pointer to the value inside the passed-in
68*d289c2baSAndroid Build Coastguard Worker  * image or NULL if not found. Note that the value is always
69*d289c2baSAndroid Build Coastguard Worker  * guaranteed to be followed by a NUL byte.
70*d289c2baSAndroid Build Coastguard Worker  *
71*d289c2baSAndroid Build Coastguard Worker  * If the value was found and |out_value_size| is not NULL, the size
72*d289c2baSAndroid Build Coastguard Worker  * of the value is returned there.
73*d289c2baSAndroid Build Coastguard Worker  *
74*d289c2baSAndroid Build Coastguard Worker  * This function is O(n) in number of descriptors so if you need to
75*d289c2baSAndroid Build Coastguard Worker  * look up a lot of values, you may want to build a more efficient
76*d289c2baSAndroid Build Coastguard Worker  * lookup-table by manually walking all descriptors using
77*d289c2baSAndroid Build Coastguard Worker  * avb_descriptor_foreach().
78*d289c2baSAndroid Build Coastguard Worker  *
79*d289c2baSAndroid Build Coastguard Worker  * Before using this function, you MUST verify |image_data| with
80*d289c2baSAndroid Build Coastguard Worker  * avb_vbmeta_image_verify() and reject it unless it's signed by a
81*d289c2baSAndroid Build Coastguard Worker  * known good public key.
82*d289c2baSAndroid Build Coastguard Worker  */
83*d289c2baSAndroid Build Coastguard Worker const char* avb_property_lookup(const uint8_t* image_data,
84*d289c2baSAndroid Build Coastguard Worker                                 size_t image_size,
85*d289c2baSAndroid Build Coastguard Worker                                 const char* key,
86*d289c2baSAndroid Build Coastguard Worker                                 size_t key_size,
87*d289c2baSAndroid Build Coastguard Worker                                 size_t* out_value_size)
88*d289c2baSAndroid Build Coastguard Worker     AVB_ATTR_WARN_UNUSED_RESULT;
89*d289c2baSAndroid Build Coastguard Worker 
90*d289c2baSAndroid Build Coastguard Worker /* Like avb_property_lookup() but parses the intial portions of the
91*d289c2baSAndroid Build Coastguard Worker  * value as an unsigned 64-bit integer. Both decimal and hexadecimal
92*d289c2baSAndroid Build Coastguard Worker  * representations (e.g. "0x2a") are supported. Returns false on
93*d289c2baSAndroid Build Coastguard Worker  * failure and true on success. On success, the parsed value is
94*d289c2baSAndroid Build Coastguard Worker  * returned in |out_value|.
95*d289c2baSAndroid Build Coastguard Worker  */
96*d289c2baSAndroid Build Coastguard Worker bool avb_property_lookup_uint64(const uint8_t* image_data,
97*d289c2baSAndroid Build Coastguard Worker                                 size_t image_size,
98*d289c2baSAndroid Build Coastguard Worker                                 const char* key,
99*d289c2baSAndroid Build Coastguard Worker                                 size_t key_size,
100*d289c2baSAndroid Build Coastguard Worker                                 uint64_t* out_value)
101*d289c2baSAndroid Build Coastguard Worker     AVB_ATTR_WARN_UNUSED_RESULT;
102*d289c2baSAndroid Build Coastguard Worker 
103*d289c2baSAndroid Build Coastguard Worker #ifdef __cplusplus
104*d289c2baSAndroid Build Coastguard Worker }
105*d289c2baSAndroid Build Coastguard Worker #endif
106*d289c2baSAndroid Build Coastguard Worker 
107*d289c2baSAndroid Build Coastguard Worker #endif /* AVB_PROPERTY_DESCRIPTOR_H_ */
108