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