xref: /rk3399_rockchip-uboot/include/android_avb/avb_slot_verify.h (revision aee63dc84c1f5be59ea35ceb209a4ea937bdeb41)
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_SLOT_VERIFY_H_
32 #define AVB_SLOT_VERIFY_H_
33 
34 #include <android_avb/avb_ops.h>
35 #include <android_avb/avb_vbmeta_image.h>
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 /* Return codes used in avb_slot_verify(), see that function for
42  * documentation for each field.
43  *
44  * Use avb_slot_verify_result_to_string() to get a textual
45  * representation usable for error/debug output.
46  */
47 typedef enum {
48   AVB_SLOT_VERIFY_RESULT_OK,
49   AVB_SLOT_VERIFY_RESULT_ERROR_OOM,
50   AVB_SLOT_VERIFY_RESULT_ERROR_IO,
51   AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION,
52   AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX,
53   AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED,
54   AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA,
55   AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION,
56   AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT
57 } AvbSlotVerifyResult;
58 
59 /* Various error handling modes for when verification fails using a
60  * hashtree at runtime inside the HLOS.
61  *
62  * AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE means that the OS
63  * will invalidate the current slot and restart.
64  *
65  * AVB_HASHTREE_ERROR_MODE_RESTART means that the OS will restart.
66  *
67  * AVB_HASHTREE_ERROR_MODE_EIO means that an EIO error will be
68  * returned to applications.
69  *
70  * AVB_HASHTREE_ERROR_MODE_LOGGING means that errors will be logged
71  * and corrupt data may be returned to applications. This mode should
72  * be used ONLY for diagnostics and debugging. It cannot be used
73  * unless AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR is also
74  * used.
75  */
76 typedef enum {
77   AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE,
78   AVB_HASHTREE_ERROR_MODE_RESTART,
79   AVB_HASHTREE_ERROR_MODE_EIO,
80   AVB_HASHTREE_ERROR_MODE_LOGGING
81 } AvbHashtreeErrorMode;
82 
83 /* Flags that influence how avb_slot_verify() works.
84  *
85  * If AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR is NOT set then
86  * avb_slot_verify() will bail out as soon as an error is encountered
87  * and |out_data| is set only if AVB_SLOT_VERIFY_RESULT_OK is
88  * returned.
89  *
90  * Otherwise if AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR is set
91  * avb_slot_verify() will continue verification efforts and |out_data|
92  * is also set if AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED,
93  * AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION, or
94  * AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX is returned. It is
95  * undefined which error is returned if more than one distinct error
96  * is encountered. It is guaranteed that AVB_SLOT_VERIFY_RESULT_OK is
97  * returned if, and only if, there are no errors. This mode is needed
98  * to boot valid but unverified slots when the device is unlocked.
99  *
100  * Also, if AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR is set the
101  * contents loaded from |requested_partition| will be the contents of
102  * the entire partition instead of just the size specified in the hash
103  * descriptor.
104  */
105 typedef enum {
106   AVB_SLOT_VERIFY_FLAGS_NONE = 0,
107   AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR = (1 << 0)
108 } AvbSlotVerifyFlags;
109 
110 /* Get a textual representation of |result|. */
111 const char* avb_slot_verify_result_to_string(AvbSlotVerifyResult result);
112 
113 /* Maximum number of rollback index locations supported. */
114 #define AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS 32
115 
116 /* AvbPartitionData contains data loaded from partitions when using
117  * avb_slot_verify(). The |partition_name| field contains the name of
118  * the partition (without A/B suffix), |data| points to the loaded
119  * data which is |data_size| bytes long.
120  *
121  * Note that this is strictly less than the partition size - it's only
122  * the image stored there, not the entire partition nor any of the
123  * metadata.
124  */
125 typedef struct {
126   char* partition_name;
127   uint8_t* data;
128   size_t data_size;
129 } AvbPartitionData;
130 
131 /* AvbVBMetaData contains a vbmeta struct loaded from a partition when
132  * using avb_slot_verify(). The |partition_name| field contains the
133  * name of the partition (without A/B suffix), |vbmeta_data| points to
134  * the loaded data which is |vbmeta_size| bytes long.
135  *
136  * The |verify_result| field contains the result of
137  * avb_vbmeta_image_verify() on the data. This is guaranteed to be
138  * AVB_VBMETA_VERIFY_RESULT_OK for all vbmeta images if
139  * avb_slot_verify() returns AVB_SLOT_VERIFY_RESULT_OK.
140  *
141  * You can use avb_descriptor_get_all(), avb_descriptor_foreach(), and
142  * avb_vbmeta_image_header_to_host_byte_order() with this data.
143  */
144 typedef struct {
145   char* partition_name;
146   uint8_t* vbmeta_data;
147   size_t vbmeta_size;
148   AvbVBMetaVerifyResult verify_result;
149 } AvbVBMetaData;
150 
151 /* AvbSlotVerifyData contains data needed to boot a particular slot
152  * and is returned by avb_slot_verify() if partitions in a slot are
153  * successfully verified.
154  *
155  * All data pointed to by this struct - including data in each item in
156  * the |partitions| array - will be freed when the
157  * avb_slot_verify_data_free() function is called.
158  *
159  * The |ab_suffix| field is the copy of the of |ab_suffix| field
160  * passed to avb_slot_verify(). It is the A/B suffix of the slot. This
161  * value includes the leading underscore - typical values are "" (if
162  * no slots are in use), "_a" (for the first slot), and "_b" (for the
163  * second slot).
164  *
165  * The VBMeta images that were checked are available in the
166  * |vbmeta_images| field. The field |num_vbmeta_images| contains the
167  * number of elements in this array. The first element -
168  * vbmeta_images[0] - is guaranteed to be from the partition with the
169  * top-level vbmeta struct. This is usually the "vbmeta" partition in
170  * the requested slot but if there is no "vbmeta" partition it can
171  * also be the "boot" partition.
172  *
173  * The partitions loaded and verified from from the slot are
174  * accessible in the |loaded_partitions| array. The field
175  * |num_loaded_partitions| contains the number of elements in this
176  * array. The order of partitions in this array may not necessarily be
177  * the same order as in the passed-in |requested_partitions| array.
178  *
179  * Rollback indexes for the verified slot are stored in the
180  * |rollback_indexes| field. Note that avb_slot_verify() will NEVER
181  * modify stored_rollback_index[n] locations e.g. it will never use
182  * the write_rollback_index() AvbOps operation. Instead it is the job
183  * of the caller of avb_slot_verify() to do this based on e.g. A/B
184  * policy and other factors. See libavb_ab/avb_ab_flow.c for an
185  * example of how to do this.
186  *
187  * The |cmdline| field is a NUL-terminated string in UTF-8 resulting
188  * from concatenating all |AvbKernelCmdlineDescriptor| and then
189  * performing proper substitution of the variables
190  * $(ANDROID_SYSTEM_PARTUUID), $(ANDROID_BOOT_PARTUUID), and
191  * $(ANDROID_VBMETA_PARTUUID) using the
192  * get_unique_guid_for_partition() operation in |AvbOps|. Additionally
193  * $(ANDROID_VERITY_MODE) will be replaced with the proper dm-verity
194  * option depending on the value of |hashtree_error_mode|.
195  *
196  * Additionally, the |cmdline| field will have the following kernel
197  * command-line options set:
198  *
199  *   androidboot.veritymode: This is set to 'disabled' if the
200  *   AVB_VBMETA_IMAGE_FLAGS_HASHTREE_DISABLED flag is set in top-level
201  *   vbmeta struct. Otherwise it is set to 'enforcing' if the
202  *   passed-in hashtree error mode is AVB_HASHTREE_ERROR_MODE_RESTART
203  *   or AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE, 'eio' if it's
204  *   set to AVB_HASHTREE_ERROR_MODE_EIO, and 'logging' if it's set to
205  *   AVB_HASHTREE_ERROR_MODE_LOGGING.
206  *
207  *   androidboot.vbmeta.invalidate_on_error: This is set to 'yes' only
208  *   if hashtree validation isn't disabled and the passed-in hashtree
209  *   error mode is AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE.
210  *
211  *   androidboot.vbmeta.device_state: set to "locked" or "unlocked"
212  *   depending on the result of the result of AvbOps's
213  *   read_is_unlocked() function.
214  *
215  *   androidboot.vbmeta.{hash_alg, size, digest}: Will be set to
216  *   the digest of all images in |vbmeta_images|.
217  *
218  *   androidboot.vbmeta.device: This is set to the value
219  *   PARTUUID=$(ANDROID_VBMETA_PARTUUID) before substitution so it
220  *   will end up pointing to the vbmeta partition for the verified
221  *   slot. If there is no vbmeta partition it will point to the boot
222  *   partition of the verified slot.
223  *
224  *   androidboot.vbmeta.avb_version: This is set to the decimal value
225  *   of AVB_VERSION_MAJOR followed by a dot followed by the decimal
226  *   value of AVB_VERSION_MINOR, for example "1.0" or "1.4". This
227  *   version number represents the vbmeta file format version
228  *   supported by libavb copy used in the boot loader. This is not
229  *   necessarily the same version number of the on-disk metadata for
230  *   the slot that was verified.
231  *
232  * Note that neither androidboot.slot_suffix nor androidboot.slot are
233  * set in the |cmdline| field in |AvbSlotVerifyData| - you will have
234  * to pass these yourself.
235  *
236  * Also note that androidboot.veritymode is set by libavb and since
237  * AVB only supports 'enforcing' and 'disabled' values, the boot
238  * loader is relieved of managing any state related to dm-verity or
239  * setting this cmdline parameter.
240  *
241  * This struct may grow in the future without it being considered an
242  * ABI break.
243  */
244 typedef struct {
245   char* ab_suffix;
246   AvbVBMetaData* vbmeta_images;
247   size_t num_vbmeta_images;
248   AvbPartitionData* loaded_partitions;
249   size_t num_loaded_partitions;
250   char* cmdline;
251   uint64_t rollback_indexes[AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS];
252 } AvbSlotVerifyData;
253 
254 /* Frees a |AvbSlotVerifyData| including all data it points to. */
255 void avb_slot_verify_data_free(AvbSlotVerifyData* data);
256 
257 /* Performs a full verification of the slot identified by |ab_suffix|
258  * and load and verify the contents of the partitions whose name is in
259  * the NULL-terminated string array |requested_partitions| (each
260  * partition must use hash verification). If not using A/B, pass an
261  * empty string (e.g. "", not NULL) for |ab_suffix|. This parameter
262  * must include the leading underscore, for example "_a" should be
263  * used to refer to the first slot.
264  *
265  * Typically the |requested_partitions| array only contains a single
266  * item for the boot partition, 'boot'.
267  *
268  * Verification includes loading and verifying data from the 'vbmeta',
269  * the requested hash partitions, and possibly other partitions (with
270  * |ab_suffix| appended), inspecting rollback indexes, and checking if
271  * the public key used to sign the data is acceptable. The functions
272  * in |ops| will be used to do this.
273  *
274  * If |out_data| is not NULL, it will be set to a newly allocated
275  * |AvbSlotVerifyData| struct containing all the data needed to
276  * actually boot the slot. This data structure should be freed with
277  * avb_slot_verify_data_free() when you are done with it. See below
278  * for when this is returned.
279  *
280  * The |flags| parameter is used to influence the semantics of
281  * avb_slot_verify() - for example the
282  * AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR flag can be used to
283  * ignore verification errors which is something needed in the
284  * UNLOCKED state. See the AvbSlotVerifyFlags enumeration for details.
285  *
286  * The |hashtree_error_mode| parameter should be set to the desired
287  * error handling mode when hashtree validation fails inside the
288  * HLOS. This value isn't used by libavb per se - it is forwarded to
289  * the HLOS through the androidboot.veritymode and
290  * androidboot.vbmeta.invalidate_on_error cmdline parameters. See the
291  * AvbHashtreeErrorMode enumeration for details.
292  *
293  * Also note that |out_data| is never set if
294  * AVB_SLOT_VERIFY_RESULT_ERROR_OOM, AVB_SLOT_VERIFY_RESULT_ERROR_IO,
295  * or AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA is returned.
296  *
297  * AVB_SLOT_VERIFY_RESULT_OK is returned if everything is verified
298  * correctly and all public keys are accepted.
299  *
300  * AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED is returned if
301  * everything is verified correctly out but one or more public keys
302  * are not accepted. This includes the case where integrity data is
303  * not signed.
304  *
305  * AVB_SLOT_VERIFY_RESULT_ERROR_OOM is returned if unable to
306  * allocate memory.
307  *
308  * AVB_SLOT_VERIFY_RESULT_ERROR_IO is returned if an I/O error
309  * occurred while trying to load data or get a rollback index.
310  *
311  * AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION is returned if the data
312  * did not verify, e.g. the digest didn't match or signature checks
313  * failed.
314  *
315  * AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX is returned if a
316  * rollback index was less than its stored value.
317  *
318  * AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA is returned if some
319  * of the metadata is invalid or inconsistent.
320  *
321  * AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION is returned if
322  * some of the metadata requires a newer version of libavb than what
323  * is in use.
324  *
325  * AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT is returned if the
326  * caller passed invalid parameters, for example trying to use
327  * AVB_HASHTREE_ERROR_MODE_LOGGING without
328  * AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR.
329  */
330 AvbSlotVerifyResult avb_slot_verify(AvbOps* ops,
331                                     const char* const* requested_partitions,
332                                     const char* ab_suffix,
333                                     AvbSlotVerifyFlags flags,
334                                     AvbHashtreeErrorMode hashtree_error_mode,
335                                     AvbSlotVerifyData** out_data);
336 
337 #ifdef __cplusplus
338 }
339 #endif
340 
341 #endif /* AVB_SLOT_VERIFY_H_ */
342