blob: fe53460b67c9ed02a65d3b5164da5f411a344f6e [file] [log] [blame]
David Zeuthen21e95262016-07-27 17:58:40 -04001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
David Zeuthenc612e2e2016-09-16 16:44:08 -04004 * 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:
David Zeuthen21e95262016-07-27 17:58:40 -040011 *
David Zeuthenc612e2e2016-09-16 16:44:08 -040012 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
David Zeuthen21e95262016-07-27 17:58:40 -040014 *
David Zeuthenc612e2e2016-09-16 16:44:08 -040015 * 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.
David Zeuthen21e95262016-07-27 17:58:40 -040023 */
24
25#if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION)
26#error "Never include this file directly, include libavb.h instead."
27#endif
28
29#ifndef AVB_SLOT_VERIFY_H_
30#define AVB_SLOT_VERIFY_H_
31
32#include "avb_ops.h"
33#include "avb_vbmeta_image.h"
34
35#ifdef __cplusplus
36extern "C" {
37#endif
38
39/* Return codes used in avb_slot_verify(), see that function for
40 * documentation for each field.
David Zeuthen8b6973b2016-09-20 12:39:49 -040041 *
42 * Use avb_slot_verify_result_to_string() to get a textual
43 * representation usable for error/debug output.
David Zeuthen21e95262016-07-27 17:58:40 -040044 */
45typedef enum {
46 AVB_SLOT_VERIFY_RESULT_OK,
47 AVB_SLOT_VERIFY_RESULT_ERROR_OOM,
48 AVB_SLOT_VERIFY_RESULT_ERROR_IO,
49 AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION,
50 AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX,
51 AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED,
52 AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA
53} AvbSlotVerifyResult;
54
David Zeuthen8b6973b2016-09-20 12:39:49 -040055/* Get a textual representation of |result|. */
56const char* avb_slot_verify_result_to_string(AvbSlotVerifyResult result);
57
David Zeuthenbaf59e22016-11-14 15:39:43 -050058/* Maximum number of rollback index locations supported. */
David Zeuthen40ee1da2016-11-23 15:14:49 -050059#define AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS 32
David Zeuthen21e95262016-07-27 17:58:40 -040060
David Zeuthena8bb9a02016-10-28 14:36:55 -040061/* AvbPartitionData contains data loaded from partitions when using
62 * avb_slot_verify(). The |partition_name| field contains the name of
David Zeuthen8681a332016-11-23 13:44:06 -050063 * the partition (without A/B suffix), |data| points to the loaded
64 * data which is |data_size| bytes long.
David Zeuthena8bb9a02016-10-28 14:36:55 -040065 *
66 * Note that this is strictly less than the partition size - it's only
67 * the image stored there, not the entire partition nor any of the
68 * metadata.
69 */
70typedef struct {
71 char* partition_name;
72 uint8_t* data;
73 size_t data_size;
74} AvbPartitionData;
75
David Zeuthen8681a332016-11-23 13:44:06 -050076/* AvbVBMetaData contains a vbmeta struct loaded from a partition when
77 * using avb_slot_verify(). The |partition_name| field contains the
78 * name of the partition (without A/B suffix), |vbmeta_data| points to
79 * the loaded data which is |vbmeta_size| bytes long.
80 *
81 * The |verify_result| field contains the result of
82 * avb_vbmeta_image_verify() on the data. This is guaranteed to be
83 * AVB_VBMETA_VERIFY_RESULT_OK for all vbmeta images if
84 * avb_slot_verify() returns AVB_SLOT_VERIFY_RESULT_OK.
85 *
86 * You can use avb_descriptor_get_all(), avb_descriptor_foreach(), and
87 * avb_vbmeta_image_header_to_host_byte_order() with this data.
88 */
89typedef struct {
90 char* partition_name;
91 uint8_t* vbmeta_data;
92 size_t vbmeta_size;
93 AvbVBMetaVerifyResult verify_result;
94} AvbVBMetaData;
95
David Zeuthen21e95262016-07-27 17:58:40 -040096/* AvbSlotVerifyData contains data needed to boot a particular slot
97 * and is returned by avb_slot_verify() if partitions in a slot are
98 * successfully verified.
99 *
David Zeuthena8bb9a02016-10-28 14:36:55 -0400100 * All data pointed to by this struct - including data in each item in
101 * the |partitions| array - will be freed when the
David Zeuthen21e95262016-07-27 17:58:40 -0400102 * avb_slot_verify_data_free() function is called.
103 *
David Zeuthen8b6973b2016-09-20 12:39:49 -0400104 * The |ab_suffix| field is the copy of the of |ab_suffix| field
105 * passed to avb_slot_verify(). It is the A/B suffix of the slot.
106 *
David Zeuthen8681a332016-11-23 13:44:06 -0500107 * The VBMeta images that were checked are available in the
108 * |vbmeta_images| field. The field |num_vbmeta_images| contains the
109 * number of elements in this array. The first element -
110 * vbmeta_images[0] - is guaranteed to be from the "vbmeta" partition
111 * in the requested slot.
112 *
David Zeuthena8bb9a02016-10-28 14:36:55 -0400113 * The partitions loaded and verified from from the slot are
114 * accessible in the |loaded_partitions| array. The field
115 * |num_loaded_partitions| contains the number of elements in this
116 * array. The order of partitions in this array may not necessarily be
117 * the same order as in the passed-in |requested_partitions| array.
David Zeuthen21e95262016-07-27 17:58:40 -0400118 *
David Zeuthenbaf59e22016-11-14 15:39:43 -0500119 * Rollback indexes for the verified slot are stored in the
120 * |rollback_indexes| field. Note that avb_slot_verify() will NEVER
121 * modify stored_rollback_index[n] locations e.g. it will never use
122 * the write_rollback_index() AvbOps operation. Instead it is the job
123 * of the caller of avb_slot_verify() to do this based on e.g. A/B
124 * policy and other factors. See libavb_ab/avb_ab_flow.c for an
125 * example of how to do this.
David Zeuthen21e95262016-07-27 17:58:40 -0400126 *
127 * The |cmdline| field is a NUL-terminated string in UTF-8 resulting
128 * from concatenating all |AvbKernelCmdlineDescriptor| and then
129 * performing proper substitution of the variables
130 * $(ANDROID_SYSTEM_PARTUUID) and $(ANDROID_BOOT_PARTUUID) using the
131 * get_unique_guid_for_partition() operation in |AvbOps|.
132 *
133 * Additionally, the |cmdline| field will have the following kernel
134 * command-line options set:
135 *
David Zeuthen8681a332016-11-23 13:44:06 -0500136 * androidboot.vbmeta.device_state: set to "locked" or "unlocked"
David Zeuthen21e95262016-07-27 17:58:40 -0400137 * depending on the result of the result of AvbOps's
138 * read_is_unlocked() function.
139 *
140 * androidboot.slot_suffix: If |ab_suffix| as passed into
141 * avb_slot_verify() is non-empty, this variable will be set to its
142 * value.
143 *
144 * androidboot.vbmeta.{hash_alg, size, digest}: Will be set to
David Zeuthen8681a332016-11-23 13:44:06 -0500145 * the digest of all images in |vbmeta_images|.
David Zeuthena8bb9a02016-10-28 14:36:55 -0400146 *
147 * This struct may grow in the future without it being considered an
148 * ABI break.
David Zeuthen21e95262016-07-27 17:58:40 -0400149 */
150typedef struct {
David Zeuthen8b6973b2016-09-20 12:39:49 -0400151 char* ab_suffix;
David Zeuthen8681a332016-11-23 13:44:06 -0500152 AvbVBMetaData* vbmeta_images;
153 size_t num_vbmeta_images;
David Zeuthena8bb9a02016-10-28 14:36:55 -0400154 AvbPartitionData* loaded_partitions;
155 size_t num_loaded_partitions;
David Zeuthen21e95262016-07-27 17:58:40 -0400156 char* cmdline;
David Zeuthen40ee1da2016-11-23 15:14:49 -0500157 uint64_t rollback_indexes[AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS];
David Zeuthen21e95262016-07-27 17:58:40 -0400158} AvbSlotVerifyData;
159
160/* Frees a |AvbSlotVerifyData| including all data it points to. */
161void avb_slot_verify_data_free(AvbSlotVerifyData* data);
162
David Zeuthena8bb9a02016-10-28 14:36:55 -0400163/* Performs a full verification of the slot identified by |ab_suffix|
164 * and load the contents of the partitions whose name is in the
165 * NULL-terminated string array |requested_partitions| (each partition
166 * must use hash verification). If not using A/B, pass an empty string
167 * (e.g. "", not NULL) for |ab_suffix|.
David Zeuthen21e95262016-07-27 17:58:40 -0400168 *
David Zeuthena8bb9a02016-10-28 14:36:55 -0400169 * Typically the |requested_partitions| array only contains a single
170 * item for the boot partition, 'boot'.
171 *
172 * Verification includes loading data from the 'vbmeta', all hash
173 * partitions, and possibly other partitions (with |ab_suffix|
174 * appended), inspecting rollback indexes, and checking if the public
175 * key used to sign the data is acceptable. The functions in |ops|
176 * will be used to do this.
David Zeuthen21e95262016-07-27 17:58:40 -0400177 *
178 * If |out_data| is not NULL, it will be set to a newly allocated
179 * |AvbSlotVerifyData| struct containing all the data needed to
180 * actually boot the slot. This data structure should be freed with
David Zeuthen0155e6b2016-11-16 17:58:13 -0500181 * avb_slot_verify_data_free() when you are done with it. See below
182 * for when this is returned.
183 *
184 * If |allow_verification_error| is false this function will bail out
185 * as soon as an error is encountered and |out_data| is set only if
186 * AVB_SLOT_VERIFY_RESULT_OK is returned.
187 *
188 * Otherwise if |allow_verification_error| is true the function will
189 * continue verification efforts and |out_data| is also set if
190 * AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED,
191 * AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION, or
192 * AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX is returned. It is
193 * undefined which error is returned if more than one distinct error
194 * is encountered. It is guaranteed that AVB_SLOT_VERIFY_RESULT_OK is
195 * returned if, and only if, there are no errors. This mode is needed
196 * to boot valid but unverified slots when the device is unlocked.
197 *
198 * Also note that |out_data| is never set if
199 * AVB_SLOT_VERIFY_RESULT_ERROR_OOM, AVB_SLOT_VERIFY_RESULT_ERROR_IO,
200 * or AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA is returned.
David Zeuthen21e95262016-07-27 17:58:40 -0400201 *
202 * AVB_SLOT_VERIFY_RESULT_OK is returned if everything is verified
203 * correctly and all public keys are accepted.
204 *
205 * AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED is returned if
206 * everything is verified correctly out but one or more public keys
207 * are not accepted. This includes the case where integrity data is
208 * not signed.
209 *
210 * AVB_SLOT_VERIFY_RESULT_ERROR_OOM is returned if unable to
211 * allocate memory.
212 *
213 * AVB_SLOT_VERIFY_RESULT_ERROR_IO is returned if an I/O error
214 * occurred while trying to load data or get a rollback index.
215 *
216 * AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION is returned if the data
217 * did not verify, e.g. the digest didn't match or signature checks
218 * failed.
219 *
220 * AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX is returned if a
221 * rollback index was less than its stored value.
222 *
223 * AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA is returned if some
224 * of the metadata is invalid or inconsistent.
225 */
David Zeuthena8bb9a02016-10-28 14:36:55 -0400226AvbSlotVerifyResult avb_slot_verify(AvbOps* ops,
227 const char* const* requested_partitions,
228 const char* ab_suffix,
David Zeuthen0155e6b2016-11-16 17:58:13 -0500229 bool allow_verification_error,
David Zeuthen21e95262016-07-27 17:58:40 -0400230 AvbSlotVerifyData** out_data);
231
232#ifdef __cplusplus
233}
234#endif
235
236#endif /* AVB_SLOT_VERIFY_H_ */