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