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