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