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 reserved2[12]; 100 101 /* CRC32 of all 28 bytes preceding this field. */ 102 uint32_t crc32; 103 } AVB_ATTR_PACKED AvbABData; 104 105 /* Copies |src| to |dest|, byte-swapping fields in the 106 * process. Returns false if the data is invalid (e.g. wrong magic, 107 * wrong CRC32 etc.), true otherwise. 108 */ 109 bool avb_ab_data_verify_and_byteswap(const AvbABData* src, AvbABData* dest); 110 111 /* Copies |src| to |dest|, byte-swapping fields in the process. Also 112 * updates the |crc32| field in |dest|. 113 */ 114 void avb_ab_data_update_crc_and_byteswap(const AvbABData* src, AvbABData* dest); 115 116 /* Initializes |data| such that it has two slots and both slots have 117 * maximum tries remaining. The CRC is not set. 118 */ 119 void avb_ab_data_init(AvbABData* data); 120 121 /* Reads A/B metadata from the 'misc' partition using |ops|. Returned 122 * data is properly byteswapped. Returns AVB_IO_RESULT_OK on 123 * success, error code otherwise. 124 * 125 * If the data read from disk is invalid (e.g. wrong magic or CRC 126 * checksum failure), the metadata will be reset using 127 * avb_ab_data_init() and then written to disk. 128 */ 129 AvbIOResult avb_ab_data_read(AvbABOps* ab_ops, AvbABData* data); 130 131 /* Writes A/B metadata to the 'misc' partition using |ops|. This will 132 * byteswap and update the CRC as needed. Returns AVB_IO_RESULT_OK on 133 * success, error code otherwise. 134 */ 135 AvbIOResult avb_ab_data_write(AvbABOps* ab_ops, const AvbABData* data); 136 137 /* Return codes used in avb_ab_flow(), see that function for 138 * documentation of each value. 139 */ 140 typedef enum { 141 AVB_AB_FLOW_RESULT_OK, 142 AVB_AB_FLOW_RESULT_OK_WITH_VERIFICATION_ERROR, 143 AVB_AB_FLOW_RESULT_ERROR_OOM, 144 AVB_AB_FLOW_RESULT_ERROR_IO, 145 AVB_AB_FLOW_RESULT_ERROR_NO_BOOTABLE_SLOTS, 146 AVB_AB_FLOW_RESULT_ERROR_INVALID_ARGUMENT 147 } AvbABFlowResult; 148 149 /* Get a textual representation of |result|. */ 150 const char* avb_ab_flow_result_to_string(AvbABFlowResult result); 151 152 /* High-level function to select a slot to boot. The following 153 * algorithm is used: 154 * 155 * 1. A/B metadata is loaded and validated using the 156 * read_ab_metadata() operation. Typically this means it's read from 157 * the 'misc' partition and if it's invalid then it's reset using 158 * avb_ab_data_init() and this reset metadata is returned. 159 * 160 * 2. All bootable slots listed in the A/B metadata are verified using 161 * avb_slot_verify(). If a slot is invalid or if it fails verification 162 * (and AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR is not set, see 163 * below), it will be marked as unbootable in the A/B metadata and the 164 * metadata will be saved to disk before returning. 165 * 166 * 3. If there are no bootable slots, the value 167 * AVB_AB_FLOW_RESULT_ERROR_NO_BOOTABLE_SLOTS is returned. 168 * 169 * 4. For each bootable slot, the Stored Rollback Indexes are updated 170 * such that for each rollback index location, the Stored Rollback 171 * Index is the largest number smaller than or equal to the Rollback 172 * Index of each slot. 173 * 174 * 5. The bootable slot with the highest priority is selected and 175 * returned in |out_data|. If this slot is already marked as 176 * successful, the A/B metadata is not modified. However, if the slot 177 * is not marked as bootable its |tries_remaining| count is 178 * decremented and the A/B metadata is saved to disk before returning. 179 * In either case the value AVB_AB_FLOW_RESULT_OK is returning. 180 * 181 * The partitions to load is given in |requested_partitions| as a 182 * NULL-terminated array of NUL-terminated strings. Typically the 183 * |requested_partitions| array only contains a single item for the 184 * boot partition, 'boot'. 185 * 186 * If the device is unlocked (and _only_ if it's unlocked), the 187 * AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR flag should be set 188 * in the |flags| parameter. This will allow considering slots as 189 * verified even when avb_slot_verify() returns 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 for the slot in 193 * question. 194 * 195 * Note that neither androidboot.slot_suffix nor androidboot.slot are 196 * set in the |cmdline| field in |AvbSlotVerifyData| - you will have 197 * to pass these yourself. 198 * 199 * If a slot was selected and it verified then AVB_AB_FLOW_RESULT_OK 200 * is returned. 201 * 202 * If a slot was selected but it didn't verify then 203 * AVB_AB_FLOW_RESULT_OK_WITH_VERIFICATION_ERROR is returned. This can 204 * only happen when the AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR 205 * flag is set. 206 * 207 * If an I/O operation - such as loading/saving metadata or checking 208 * rollback indexes - fail, the value AVB_AB_FLOW_RESULT_ERROR_IO is 209 * returned. 210 * 211 * If memory allocation fails, AVB_AB_FLOW_RESULT_ERROR_OOM is 212 * returned. 213 * 214 * If invalid arguments are passed, 215 * AVB_AB_FLOW_RESULT_ERROR_INVALID_ARGUMENT is returned. For example 216 * this can happen if using AVB_HASHTREE_ERROR_MODE_LOGGING without 217 * AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR. 218 * 219 * Reasonable behavior for handling AVB_AB_FLOW_RESULT_ERROR_NO_BOOTABLE_SLOTS 220 * is to initiate device repair (which is device-dependent). 221 */ 222 AvbABFlowResult avb_ab_flow(AvbABOps* ab_ops, 223 const char* const* requested_partitions, 224 AvbSlotVerifyFlags flags, 225 AvbHashtreeErrorMode hashtree_error_mode, 226 AvbSlotVerifyData** out_data); 227 228 /* Marks the slot with the given slot number as active. Returns 229 * AVB_IO_RESULT_OK on success, error code otherwise. 230 * 231 * This function is typically used by the OS updater when completing 232 * an update. It can also used by the firmware for implementing the 233 * "set_active" command. 234 */ 235 AvbIOResult avb_ab_mark_slot_active(AvbABOps* ab_ops, unsigned int slot_number); 236 237 /* Marks the slot with the given slot number as unbootable. Returns 238 * AVB_IO_RESULT_OK on success, error code otherwise. 239 * 240 * This function is typically used by the OS updater before writing to 241 * a slot. 242 */ 243 AvbIOResult avb_ab_mark_slot_unbootable(AvbABOps* ab_ops, 244 unsigned int slot_number); 245 246 /* Marks the slot with the given slot number as having booted 247 * successfully. Returns AVB_IO_RESULT_OK on success, error code 248 * otherwise. 249 * 250 * Calling this on an unbootable slot is an error - AVB_IO_RESULT_OK 251 * will be returned yet the function will have no side-effects. 252 * 253 * This function is typically used by the OS updater after having 254 * confirmed that the slot works as intended. 255 */ 256 AvbIOResult avb_ab_mark_slot_successful(AvbABOps* ab_ops, 257 unsigned int slot_number); 258 259 /* 260 * Load metadata. 261 */ 262 AvbIOResult load_metadata(AvbABOps* ab_ops, 263 AvbABData* ab_data, 264 AvbABData* ab_data_orig); 265 266 /* Writes A/B metadata to disk only if it has changed - returns 267 * AVB_IO_RESULT_OK on success, error code otherwise. 268 */ 269 AvbIOResult save_metadata_if_changed(AvbABOps* ab_ops, 270 AvbABData* ab_data, 271 AvbABData* ab_data_orig); 272 273 #ifdef __cplusplus 274 } 275 #endif 276 277 #endif /* AVB_AB_FLOW_H_ */ 278