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