David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 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 | |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 25 | #if !defined(AVB_INSIDE_LIBAVB_AB_H) && !defined(AVB_COMPILATION) |
| 26 | #error "Never include this file directly, include libavb/libavb_ab.h instead." |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 27 | #endif |
| 28 | |
| 29 | #ifndef AVB_AB_FLOW_H_ |
| 30 | #define AVB_AB_FLOW_H_ |
| 31 | |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 32 | #include "avb_ab_ops.h" |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 33 | |
| 34 | #ifdef __cplusplus |
| 35 | extern "C" { |
| 36 | #endif |
| 37 | |
| 38 | /* Magic for the A/B struct when serialized. */ |
| 39 | #define AVB_AB_MAGIC "\0AB0" |
| 40 | #define AVB_AB_MAGIC_LEN 4 |
| 41 | |
| 42 | /* Versioning for the on-disk A/B metadata - keep in sync with avbtool. */ |
| 43 | #define AVB_AB_MAJOR_VERSION 1 |
| 44 | #define AVB_AB_MINOR_VERSION 0 |
| 45 | |
| 46 | /* Size of AvbABData struct. */ |
| 47 | #define AVB_AB_DATA_SIZE 32 |
| 48 | |
| 49 | /* Maximum values for slot data */ |
| 50 | #define AVB_AB_MAX_PRIORITY 15 |
| 51 | #define AVB_AB_MAX_TRIES_REMAINING 7 |
| 52 | |
| 53 | /* Struct used for recording per-slot metadata. |
| 54 | * |
| 55 | * When serialized, data is stored in network byte-order. |
| 56 | */ |
| 57 | typedef struct AvbABSlotData { |
| 58 | /* Slot priority. Valid values range from 0 to AVB_AB_MAX_PRIORITY, |
| 59 | * both inclusive with 1 being the lowest and AVB_AB_MAX_PRIORITY |
| 60 | * being the highest. The special value 0 is used to indicate the |
| 61 | * slot is unbootable. |
| 62 | */ |
| 63 | uint8_t priority; |
| 64 | |
| 65 | /* Number of times left attempting to boot this slot ranging from 0 |
| 66 | * to AVB_AB_MAX_TRIES_REMAINING. |
| 67 | */ |
| 68 | uint8_t tries_remaining; |
| 69 | |
| 70 | /* Non-zero if this slot has booted successfully, 0 otherwise. */ |
| 71 | uint8_t successful_boot; |
| 72 | |
| 73 | /* Reserved for future use. */ |
| 74 | uint8_t reserved[1]; |
| 75 | } AVB_ATTR_PACKED AvbABSlotData; |
| 76 | |
| 77 | /* Struct used for recording A/B metadata. |
| 78 | * |
| 79 | * When serialized, data is stored in network byte-order. |
| 80 | */ |
| 81 | typedef struct AvbABData { |
| 82 | /* Magic number used for identification - see AVB_AB_MAGIC. */ |
| 83 | uint8_t magic[AVB_AB_MAGIC_LEN]; |
| 84 | |
| 85 | /* Version of on-disk struct - see AVB_AB_{MAJOR,MINOR}_VERSION. */ |
| 86 | uint8_t version_major; |
| 87 | uint8_t version_minor; |
| 88 | |
| 89 | /* Padding to ensure |slots| field start eight bytes in. */ |
| 90 | uint8_t reserved1[2]; |
| 91 | |
| 92 | /* Per-slot metadata. */ |
| 93 | AvbABSlotData slots[2]; |
| 94 | |
| 95 | /* Reserved for future use. */ |
| 96 | uint8_t reserved2[12]; |
| 97 | |
| 98 | /* CRC32 of all 28 bytes preceding this field. */ |
| 99 | uint32_t crc32; |
| 100 | } AVB_ATTR_PACKED AvbABData; |
| 101 | |
| 102 | /* Copies |src| to |dest|, byte-swapping fields in the |
| 103 | * process. Returns false if the data is invalid (e.g. wrong magic, |
| 104 | * wrong CRC32 etc.), true otherwise. |
| 105 | */ |
| 106 | bool avb_ab_data_verify_and_byteswap(const AvbABData* src, AvbABData* dest); |
| 107 | |
| 108 | /* Copies |src| to |dest|, byte-swapping fields in the process. Also |
| 109 | * updates the |crc32| field in |dest|. |
| 110 | */ |
| 111 | void avb_ab_data_update_crc_and_byteswap(const AvbABData* src, AvbABData* dest); |
| 112 | |
| 113 | /* Initializes |data| such that it has two slots and both slots have |
| 114 | * maximum tries remaining. The CRC is not set. |
| 115 | */ |
| 116 | void avb_ab_data_init(AvbABData* data); |
| 117 | |
| 118 | /* Reads A/B metadata from the 'misc' partition using |ops|. Returned |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 119 | * data is properly byteswapped. Returns AVB_IO_RESULT_OK on |
| 120 | * success, error code otherwise. |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 121 | * |
| 122 | * If the data read from disk is invalid (e.g. wrong magic or CRC |
| 123 | * checksum failure), the metadata will be reset using |
| 124 | * avb_ab_data_init() and then written to disk. |
| 125 | */ |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 126 | AvbIOResult avb_ab_data_read(AvbABOps* ab_ops, AvbABData* data); |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 127 | |
| 128 | /* Writes A/B metadata to the 'misc' partition using |ops|. This will |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 129 | * byteswap and update the CRC as needed. Returns AVB_IO_RESULT_OK on |
| 130 | * success, error code otherwise. |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 131 | */ |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 132 | AvbIOResult avb_ab_data_write(AvbABOps* ab_ops, const AvbABData* data); |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 133 | |
| 134 | /* Return codes used in avb_ab_flow(), see that function for |
| 135 | * documentation of each value. |
| 136 | */ |
| 137 | typedef enum { |
| 138 | AVB_AB_FLOW_RESULT_OK, |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 139 | AVB_AB_FLOW_RESULT_OK_WITH_VERIFICATION_ERROR, |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 140 | AVB_AB_FLOW_RESULT_ERROR_OOM, |
| 141 | AVB_AB_FLOW_RESULT_ERROR_IO, |
| 142 | AVB_AB_FLOW_RESULT_ERROR_NO_BOOTABLE_SLOTS |
| 143 | } AvbABFlowResult; |
| 144 | |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 145 | /* Get a textual representation of |result|. */ |
| 146 | const char* avb_ab_flow_result_to_string(AvbABFlowResult result); |
| 147 | |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 148 | /* High-level function to select a slot to boot. The following |
| 149 | * algorithm is used: |
| 150 | * |
David Zeuthen | 4cc9652 | 2016-10-28 15:22:42 -0400 | [diff] [blame] | 151 | * 1. A/B metadata is loaded and validated using the |
| 152 | * read_ab_metadata() operation. Typically this means it's read from |
| 153 | * the 'misc' partition and if it's invalid then it's reset using |
| 154 | * avb_ab_data_init() and this reset metadata is returned. |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 155 | * |
| 156 | * 2. All bootable slots listed in the A/B metadata are verified using |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 157 | * avb_slot_verify(). If a slot is invalid or if it fails verification |
| 158 | * (and |allow_verification_error| is false, see below), it will be |
| 159 | * marked as unbootable in the A/B metadata and the metadata will be |
| 160 | * saved to disk before returning. |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 161 | * |
| 162 | * 3. If there are no bootable slots, the value |
| 163 | * AVB_AB_FLOW_RESULT_ERROR_NO_BOOTABLE_SLOTS is returned. |
| 164 | * |
| 165 | * 4. For each bootable slot, the Stored Rollback Indexes are updated |
David Zeuthen | 40ee1da | 2016-11-23 15:14:49 -0500 | [diff] [blame^] | 166 | * such that for each rollback index location, the Stored Rollback |
| 167 | * Index is the largest number smaller than or equal to the Rollback |
| 168 | * Index of each slot. |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 169 | * |
| 170 | * 5. The bootable slot with the highest priority is selected and |
| 171 | * returned in |out_data|. If this slot is already marked as |
| 172 | * successful, the A/B metadata is not modified. However, if the slot |
| 173 | * is not marked as bootable its |tries_remaining| count is |
| 174 | * decremented and the A/B metadata is saved to disk before returning. |
| 175 | * In either case the value AVB_AB_FLOW_RESULT_OK is returning. |
| 176 | * |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 177 | * The partitions to load is given in |requested_partitions| as a |
| 178 | * NULL-terminated array of NUL-terminated strings. Typically the |
| 179 | * |requested_partitions| array only contains a single item for the |
| 180 | * boot partition, 'boot'. |
| 181 | * |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 182 | * If the device is unlocked (and _only_ if it's unlocked), true |
| 183 | * should be passed in the |allow_verification_error| parameter. This |
| 184 | * will allow considering slots as verified even when |
| 185 | * avb_slot_verify() returns |
| 186 | * AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED, |
| 187 | * AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION, or |
| 188 | * AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX for the slot in |
| 189 | * question. |
| 190 | * |
| 191 | * If a slot was selected and it verified then AVB_AB_FLOW_RESULT_OK |
| 192 | * is returned. |
| 193 | * |
| 194 | * If a slot was selected but it didn't verify then |
| 195 | * AVB_AB_FLOW_RESULT_OK_WITH_VERIFICATION_ERROR is returned. This can |
| 196 | * only happen when |allow_verification_error| is true. |
| 197 | * |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 198 | * If an I/O operation - such as loading/saving metadata or checking |
| 199 | * rollback indexes - fail, the value AVB_AB_FLOW_RESULT_ERROR_IO is |
| 200 | * returned. |
| 201 | * |
| 202 | * If memory allocation fails, AVB_AB_FLOW_RESULT_ERROR_OOM is |
| 203 | * returned. |
| 204 | * |
| 205 | * Reasonable behavior for handling AVB_AB_FLOW_RESULT_ERROR_NO_BOOTABLE_SLOTS |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 206 | * is to initiate device repair (which is device-dependent). |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 207 | */ |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 208 | AvbABFlowResult avb_ab_flow(AvbABOps* ab_ops, |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 209 | const char* const* requested_partitions, |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 210 | bool allow_verification_error, |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 211 | AvbSlotVerifyData** out_data); |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 212 | |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 213 | /* Marks the slot with the given slot number as active. Returns |
| 214 | * AVB_IO_RESULT_OK on success, error code otherwise. |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 215 | * |
| 216 | * This function is typically used by the OS updater when completing |
| 217 | * an update. It can also used by the firmware for implementing the |
| 218 | * "set_active" command. |
| 219 | */ |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 220 | AvbIOResult avb_ab_mark_slot_active(AvbABOps* ab_ops, unsigned int slot_number); |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 221 | |
| 222 | /* Marks the slot with the given slot number as unbootable. Returns |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 223 | * AVB_IO_RESULT_OK on success, error code otherwise. |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 224 | * |
| 225 | * This function is typically used by the OS updater before writing to |
| 226 | * a slot. |
| 227 | */ |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 228 | AvbIOResult avb_ab_mark_slot_unbootable(AvbABOps* ab_ops, |
| 229 | unsigned int slot_number); |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 230 | |
| 231 | /* Marks the slot with the given slot number as having booted |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 232 | * successfully. Returns AVB_IO_RESULT_OK on success, error code |
| 233 | * otherwise. |
| 234 | * |
| 235 | * Calling this on an unbootable slot is an error - AVB_IO_RESULT_OK |
| 236 | * will be returned yet the function will have no side-effects. |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 237 | * |
| 238 | * This function is typically used by the OS updater after having |
| 239 | * confirmed that the slot works as intended. |
| 240 | */ |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 241 | AvbIOResult avb_ab_mark_slot_successful(AvbABOps* ab_ops, |
| 242 | unsigned int slot_number); |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 243 | |
| 244 | #ifdef __cplusplus |
| 245 | } |
| 246 | #endif |
| 247 | |
| 248 | #endif /* AVB_AB_FLOW_H_ */ |