xref: /rk3399_rockchip-uboot/include/android_avb/avb_ab_flow.h (revision 5821df21ae36d9ef252d346a5abb76be773c5d69)
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 #if !defined(AVB_INSIDE_LIBAVB_AB_H) && !defined(AVB_COMPILATION)
26 #error \
27     "Never include this file directly, include libavb_ab/libavb_ab.h instead."
28 #endif
29 */
30 
31 #ifndef AVB_AB_FLOW_H_
32 #define AVB_AB_FLOW_H_
33 
34 #include <android_avb/avb_ab_ops.h>
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 /* Magic for the A/B struct when serialized. */
41 #define AVB_AB_MAGIC "\0AB0"
42 #define AVB_AB_MAGIC_LEN 4
43 
44 /* Versioning for the on-disk A/B metadata - keep in sync with avbtool. */
45 #define AVB_AB_MAJOR_VERSION 1
46 #define AVB_AB_MINOR_VERSION 0
47 
48 /* Size of AvbABData struct. */
49 #define AVB_AB_DATA_SIZE 512
50 
51 /* Maximum values for slot data */
52 #define AVB_AB_MAX_PRIORITY 15
53 #define AVB_AB_MAX_TRIES_REMAINING 7
54 
55 /* Struct used for recording per-slot metadata.
56  *
57  * When serialized, data is stored in network byte-order.
58  */
59 typedef struct AvbABSlotData {
60   /* Slot priority. Valid values range from 0 to AVB_AB_MAX_PRIORITY,
61    * both inclusive with 1 being the lowest and AVB_AB_MAX_PRIORITY
62    * being the highest. The special value 0 is used to indicate the
63    * slot is unbootable.
64    */
65   uint8_t priority : 4;
66 
67   /* Number of times left attempting to boot this slot ranging from 0
68    * to AVB_AB_MAX_TRIES_REMAINING.
69    */
70   uint8_t tries_remaining : 3;
71 
72   /* Non-zero if this slot has booted successfully, 0 otherwise. */
73   uint8_t successful_boot : 1;
74     /* 1 if this slot is corrupted from a dm-verity corruption, 0 */
75     /* otherwise. */
76     uint8_t verity_corrupted : 1;
77   /* Reserved for future use. */
78   uint8_t reserved : 7;
79 } AVB_ATTR_PACKED AvbABSlotData;
80 
81 /* Struct used for recording A/B metadata.
82  *
83  * When serialized, data is stored in network byte-order.
84  */
85 typedef struct AvbABData
86 {
87 	/* NUL terminated active slot suffix. */
88 	char slot_suffix[4];
89 	/* Magic number used for identification - see AVB_AB_MAGIC. */
90 	uint8_t magic[AVB_AB_MAGIC_LEN];
91 
92 	/* Version of on-disk struct - see AVB_AB_{MAJOR,MINOR}_VERSION. */
93 	uint8_t version_major;
94 
95 	/* Number of slots being managed. */
96 	uint8_t nb_slot : 3;
97 	/* Number of times left attempting to boot recovery. */
98 	uint8_t recovery_tries_remaining : 3;
99 	/* Padding to ensure |slots| field start eight bytes in. */
100 	uint8_t reserved1[2];
101 
102 	/* Per-slot metadata. */
103 	AvbABSlotData slots[4];
104 
105 	/* Reserved for future use. */
106 	uint8_t reserved2[8];
107 	//uint8_t reserved3[480];
108 
109 	/* CRC32 of all 28 bytes preceding this field. */
110 	uint32_t crc32;
111 	uint8_t version_minor;
112 } AVB_ATTR_PACKED AvbABData;
113 
114 /* Copies |src| to |dest|, byte-swapping fields in the
115  * process. Returns false if the data is invalid (e.g. wrong magic,
116  * wrong CRC32 etc.), true otherwise.
117  */
118 bool avb_ab_data_verify_and_byteswap(const AvbABData* src, AvbABData* dest);
119 
120 /* Copies |src| to |dest|, byte-swapping fields in the process. Also
121  * updates the |crc32| field in |dest|.
122  */
123 void avb_ab_data_update_crc_and_byteswap(const AvbABData* src, AvbABData* dest);
124 
125 /* Initializes |data| such that it has two slots and both slots have
126  * maximum tries remaining. The CRC is not set.
127  */
128 void avb_ab_data_init(AvbABData* data);
129 
130 /* Reads A/B metadata from the 'misc' partition using |ops|. Returned
131  * data is properly byteswapped. Returns AVB_IO_RESULT_OK on
132  * success, error code otherwise.
133  *
134  * If the data read from disk is invalid (e.g. wrong magic or CRC
135  * checksum failure), the metadata will be reset using
136  * avb_ab_data_init() and then written to disk.
137  */
138 AvbIOResult avb_ab_data_read(AvbABOps* ab_ops, AvbABData* data);
139 
140 /* Load A/B metadata, like function avb_ab_data_read*/
141 AvbIOResult load_metadata(AvbABOps* ab_ops,
142                                  AvbABData* ab_data,
143                                  AvbABData* ab_data_orig);
144 
145 /* Writes A/B metadata to the 'misc' partition using |ops|. This will
146  * byteswap and update the CRC as needed. Returns AVB_IO_RESULT_OK on
147  * success, error code otherwise.
148  */
149 AvbIOResult avb_ab_data_write(AvbABOps* ab_ops, const AvbABData* data);
150 
151 /* Return codes used in avb_ab_flow(), see that function for
152  * documentation of each value.
153  */
154 typedef enum {
155   AVB_AB_FLOW_RESULT_OK,
156   AVB_AB_FLOW_RESULT_OK_WITH_VERIFICATION_ERROR,
157   AVB_AB_FLOW_RESULT_ERROR_OOM,
158   AVB_AB_FLOW_RESULT_ERROR_IO,
159   AVB_AB_FLOW_RESULT_ERROR_NO_BOOTABLE_SLOTS,
160   AVB_AB_FLOW_RESULT_ERROR_INVALID_ARGUMENT
161 } AvbABFlowResult;
162 
163 /* Get a textual representation of |result|. */
164 const char* avb_ab_flow_result_to_string(AvbABFlowResult result);
165 
166 /* High-level function to select a slot to boot. The following
167  * algorithm is used:
168  *
169  * 1. A/B metadata is loaded and validated using the
170  * read_ab_metadata() operation. Typically this means it's read from
171  * the 'misc' partition and if it's invalid then it's reset using
172  * avb_ab_data_init() and this reset metadata is returned.
173  *
174  * 2. All bootable slots listed in the A/B metadata are verified using
175  * avb_slot_verify(). If a slot is invalid or if it fails verification
176  * (and AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR is not set, see
177  * below), it will be marked as unbootable in the A/B metadata and the
178  * metadata will be saved to disk before returning.
179  *
180  * 3. If there are no bootable slots, the value
181  * AVB_AB_FLOW_RESULT_ERROR_NO_BOOTABLE_SLOTS is returned.
182  *
183  * 4. For each bootable slot, the Stored Rollback Indexes are updated
184  * such that for each rollback index location, the Stored Rollback
185  * Index is the largest number smaller than or equal to the Rollback
186  * Index of each slot.
187  *
188  * 5. The bootable slot with the highest priority is selected and
189  * returned in |out_data|. If this slot is already marked as
190  * successful, the A/B metadata is not modified. However, if the slot
191  * is not marked as bootable its |tries_remaining| count is
192  * decremented and the A/B metadata is saved to disk before returning.
193  * In either case the value AVB_AB_FLOW_RESULT_OK is returning.
194  *
195  * The partitions to load is given in |requested_partitions| as a
196  * NULL-terminated array of NUL-terminated strings. Typically the
197  * |requested_partitions| array only contains a single item for the
198  * boot partition, 'boot'.
199  *
200  * If the device is unlocked (and _only_ if it's unlocked), the
201  * AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR flag should be set
202  * in the |flags| parameter. This will allow considering slots as
203  * verified even when avb_slot_verify() returns
204  * AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED,
205  * AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION, or
206  * AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX for the slot in
207  * question.
208  *
209  * Note that neither androidboot.slot_suffix nor androidboot.slot are
210  * set in the |cmdline| field in |AvbSlotVerifyData| - you will have
211  * to pass these yourself.
212  *
213  * If a slot was selected and it verified then AVB_AB_FLOW_RESULT_OK
214  * is returned.
215  *
216  * If a slot was selected but it didn't verify then
217  * AVB_AB_FLOW_RESULT_OK_WITH_VERIFICATION_ERROR is returned. This can
218  * only happen when the AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR
219  * flag is set.
220  *
221  * If an I/O operation - such as loading/saving metadata or checking
222  * rollback indexes - fail, the value AVB_AB_FLOW_RESULT_ERROR_IO is
223  * returned.
224  *
225  * If memory allocation fails, AVB_AB_FLOW_RESULT_ERROR_OOM is
226  * returned.
227  *
228  * If invalid arguments are passed,
229  * AVB_AB_FLOW_RESULT_ERROR_INVALID_ARGUMENT is returned. For example
230  * this can happen if using AVB_HASHTREE_ERROR_MODE_LOGGING without
231  * AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR.
232  *
233  * Reasonable behavior for handling AVB_AB_FLOW_RESULT_ERROR_NO_BOOTABLE_SLOTS
234  * is to initiate device repair (which is device-dependent).
235  */
236 AvbABFlowResult avb_ab_flow(AvbABOps* ab_ops,
237                             const char* const* requested_partitions,
238                             AvbSlotVerifyFlags flags,
239                             AvbHashtreeErrorMode hashtree_error_mode,
240                             AvbSlotVerifyData** out_data);
241 
242 AvbABFlowResult avb_ab_slot_select(AvbABOps* ab_ops,char select_slot[]);
243 
244 /* Marks the slot with the given slot number as active. Returns
245  * AVB_IO_RESULT_OK on success, error code otherwise.
246  *
247  * This function is typically used by the OS updater when completing
248  * an update. It can also used by the firmware for implementing the
249  * "set_active" command.
250  */
251 AvbIOResult avb_ab_mark_slot_active(AvbABOps* ab_ops, unsigned int slot_number);
252 
253 /* Marks the slot with the given slot number as unbootable. Returns
254  * AVB_IO_RESULT_OK on success, error code otherwise.
255  *
256  * This function is typically used by the OS updater before writing to
257  * a slot.
258  */
259 AvbIOResult avb_ab_mark_slot_unbootable(AvbABOps* ab_ops,
260                                         unsigned int slot_number);
261 
262 /* Marks the slot with the given slot number as having booted
263  * successfully. Returns AVB_IO_RESULT_OK on success, error code
264  * otherwise.
265  *
266  * Calling this on an unbootable slot is an error - AVB_IO_RESULT_OK
267  * will be returned yet the function will have no side-effects.
268  *
269  * This function is typically used by the OS updater after having
270  * confirmed that the slot works as intended.
271  */
272 AvbIOResult avb_ab_mark_slot_successful(AvbABOps* ab_ops,
273                                         unsigned int slot_number);
274 
275 #ifdef __cplusplus
276 }
277 #endif
278 
279 #endif /* AVB_AB_FLOW_H_ */
280