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