xref: /OK3568_Linux_fs/u-boot/include/android_avb/avb_descriptor.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Permission is hereby granted, free of charge, to any person
5  * obtaining a copy of this software and associated documentation
6  * files (the "Software"), to deal in the Software without
7  * restriction, including without limitation the rights to use, copy,
8  * modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
25 /*
26 #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION)
27 #error "Never include this file directly, include libavb.h instead."
28 #endif
29 */
30 
31 #ifndef AVB_DESCRIPTOR_H_
32 #define AVB_DESCRIPTOR_H_
33 
34 #include <android_avb/avb_sysdeps.h>
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 /* Well-known descriptor tags.
41  *
42  * AVB_DESCRIPTOR_TAG_PROPERTY: see |AvbPropertyDescriptor| struct.
43  * AVB_DESCRIPTOR_TAG_HASHTREE: see |AvbHashtreeDescriptor| struct.
44  * AVB_DESCRIPTOR_TAG_HASH: see |AvbHashDescriptor| struct.
45  * AVB_DESCRIPTOR_TAG_KERNEL_CMDLINE: see |AvbKernelCmdlineDescriptor| struct.
46  * AVB_DESCRIPTOR_TAG_CHAIN_PARTITION: see |AvbChainPartitionDescriptor| struct.
47  */
48 typedef enum {
49   AVB_DESCRIPTOR_TAG_PROPERTY,
50   AVB_DESCRIPTOR_TAG_HASHTREE,
51   AVB_DESCRIPTOR_TAG_HASH,
52   AVB_DESCRIPTOR_TAG_KERNEL_CMDLINE,
53   AVB_DESCRIPTOR_TAG_CHAIN_PARTITION,
54 } AvbDescriptorTag;
55 
56 /* The header for a serialized descriptor.
57  *
58  * A descriptor always have two fields, a |tag| (denoting its type,
59  * see the |AvbDescriptorTag| enumeration) and the size of the bytes
60  * following, |num_bytes_following|.
61  *
62  * For padding, |num_bytes_following| is always a multiple of 8.
63  */
64 typedef struct AvbDescriptor {
65   uint64_t tag;
66   uint64_t num_bytes_following;
67 } AVB_ATTR_PACKED AvbDescriptor;
68 
69 /* Copies |src| to |dest| and validates, byte-swapping fields in the
70  * process if needed. Returns true if valid, false if invalid.
71  *
72  * Data following the struct is not validated nor copied.
73  */
74 bool avb_descriptor_validate_and_byteswap(
75     const AvbDescriptor* src, AvbDescriptor* dest) AVB_ATTR_WARN_UNUSED_RESULT;
76 
77 /* Signature for callback function used in avb_descriptor_foreach().
78  * The passed in descriptor is given by |descriptor| and the
79  * |user_data| passed to avb_descriptor_foreach() function is in
80  * |user_data|. Return true to continue iterating, false to stop
81  * iterating.
82  *
83  * Note that |descriptor| points into the image passed to
84  * avb_descriptor_foreach() - all fields need to be byteswapped!
85  */
86 typedef bool AvbDescriptorForeachFunc(const AvbDescriptor* descriptor,
87                                       void* user_data);
88 
89 /* Convenience function to iterate over all descriptors in an vbmeta
90  * image.
91  *
92  * The function given by |foreach_func| will be called for each
93  * descriptor. The given function should return true to continue
94  * iterating, false to stop.
95  *
96  * The |user_data| parameter will be passed to |foreach_func|.
97  *
98  * Returns false if the iteration was short-circuited, that is if
99  * an invocation of |foreach_func| returned false.
100  *
101  * Before using this function, you MUST verify |image_data| with
102  * avb_vbmeta_image_verify() and reject it unless it's signed by a known
103  * good public key. Additionally, |image_data| must be word-aligned.
104  */
105 bool avb_descriptor_foreach(const uint8_t* image_data,
106                             size_t image_size,
107                             AvbDescriptorForeachFunc foreach_func,
108                             void* user_data);
109 
110 /* Gets all descriptors in a vbmeta image.
111  *
112  * The return value is a NULL-pointer terminated array of
113  * AvbDescriptor pointers. Free with avb_free() when you are done with
114  * it. If |out_num_descriptors| is non-NULL, the number of descriptors
115  * will be returned there.
116  *
117  * Note that each AvbDescriptor pointer in the array points into
118  * |image_data| - all fields need to be byteswapped!
119  *
120  * Before using this function, you MUST verify |image_data| with
121  * avb_vbmeta_image_verify() and reject it unless it's signed by a known
122  * good public key. Additionally, |image_data| must be word-aligned.
123  */
124 const AvbDescriptor** avb_descriptor_get_all(const uint8_t* image_data,
125                                              size_t image_size,
126                                              size_t* out_num_descriptors)
127     AVB_ATTR_WARN_UNUSED_RESULT;
128 
129 #ifdef __cplusplus
130 }
131 #endif
132 
133 #endif /* AVB_DESCRIPTOR_H_ */
134