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