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