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