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 | |
| 25 | #include "avb_ab_flow.h" |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 26 | |
| 27 | bool avb_ab_data_verify_and_byteswap(const AvbABData* src, AvbABData* dest) { |
| 28 | /* Ensure magic is correct. */ |
| 29 | if (avb_safe_memcmp(src->magic, AVB_AB_MAGIC, AVB_AB_MAGIC_LEN) != 0) { |
| 30 | avb_error("Magic is incorrect.\n"); |
| 31 | return false; |
| 32 | } |
| 33 | |
| 34 | avb_memcpy(dest, src, sizeof(AvbABData)); |
| 35 | dest->crc32 = avb_be32toh(dest->crc32); |
| 36 | |
| 37 | /* Ensure we don't attempt to access any fields if the major version |
| 38 | * is not supported. |
| 39 | */ |
| 40 | if (dest->version_major > AVB_AB_MAJOR_VERSION) { |
| 41 | avb_error("No support for given major version.\n"); |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | /* Bail if CRC32 doesn't match. */ |
| 46 | if (dest->crc32 != |
| 47 | avb_crc32((const uint8_t*)dest, sizeof(AvbABData) - sizeof(uint32_t))) { |
| 48 | avb_error("CRC32 does not match.\n"); |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | void avb_ab_data_update_crc_and_byteswap(const AvbABData* src, |
| 56 | AvbABData* dest) { |
| 57 | avb_memcpy(dest, src, sizeof(AvbABData)); |
| 58 | dest->crc32 = avb_htobe32( |
| 59 | avb_crc32((const uint8_t*)dest, sizeof(AvbABData) - sizeof(uint32_t))); |
| 60 | } |
| 61 | |
| 62 | void avb_ab_data_init(AvbABData* data) { |
| 63 | avb_memset(data, '\0', sizeof(AvbABData)); |
| 64 | avb_memcpy(data->magic, AVB_AB_MAGIC, AVB_AB_MAGIC_LEN); |
| 65 | data->version_major = AVB_AB_MAJOR_VERSION; |
| 66 | data->version_minor = AVB_AB_MINOR_VERSION; |
| 67 | data->slots[0].priority = AVB_AB_MAX_PRIORITY; |
| 68 | data->slots[0].tries_remaining = AVB_AB_MAX_TRIES_REMAINING; |
| 69 | data->slots[0].successful_boot = 0; |
| 70 | data->slots[1].priority = AVB_AB_MAX_PRIORITY - 1; |
| 71 | data->slots[1].tries_remaining = AVB_AB_MAX_TRIES_REMAINING; |
| 72 | data->slots[1].successful_boot = 0; |
| 73 | } |
| 74 | |
| 75 | /* The AvbABData struct is stored 2048 bytes into the 'misc' partition |
| 76 | * following the 'struct bootloader_message' field. The struct is |
| 77 | * compatible with the guidelines in bootable/recovery/bootloader.h - |
| 78 | * e.g. it is stored in the |slot_suffix| field, starts with a |
| 79 | * NUL-byte, and is 32 bytes long. |
| 80 | */ |
| 81 | #define AB_METADATA_MISC_PARTITION_OFFSET 2048 |
| 82 | |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 83 | AvbIOResult avb_ab_data_read(AvbABOps* ab_ops, AvbABData* data) { |
| 84 | AvbOps* ops = &(ab_ops->ops); |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 85 | AvbABData serialized; |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 86 | AvbIOResult io_ret; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 87 | size_t num_bytes_read; |
| 88 | |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 89 | io_ret = |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 90 | ops->read_from_partition(ops, "misc", AB_METADATA_MISC_PARTITION_OFFSET, |
| 91 | sizeof(AvbABData), &serialized, &num_bytes_read); |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 92 | if (io_ret == AVB_IO_RESULT_ERROR_OOM) { |
| 93 | return AVB_IO_RESULT_ERROR_OOM; |
| 94 | } else if (io_ret != AVB_IO_RESULT_OK || |
| 95 | num_bytes_read != sizeof(AvbABData)) { |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 96 | avb_error("Error reading A/B metadata.\n"); |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 97 | return AVB_IO_RESULT_ERROR_IO; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | if (!avb_ab_data_verify_and_byteswap(&serialized, data)) { |
| 101 | avb_error( |
| 102 | "Error validating A/B metadata from disk. " |
| 103 | "Resetting and writing new A/B metadata to disk.\n"); |
| 104 | avb_ab_data_init(data); |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 105 | return avb_ab_data_write(ab_ops, data); |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 106 | } |
| 107 | |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 108 | return AVB_IO_RESULT_OK; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 109 | } |
| 110 | |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 111 | AvbIOResult avb_ab_data_write(AvbABOps* ab_ops, const AvbABData* data) { |
| 112 | AvbOps* ops = &(ab_ops->ops); |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 113 | AvbABData serialized; |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 114 | AvbIOResult io_ret; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 115 | |
| 116 | avb_ab_data_update_crc_and_byteswap(data, &serialized); |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 117 | io_ret = |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 118 | ops->write_to_partition(ops, "misc", AB_METADATA_MISC_PARTITION_OFFSET, |
| 119 | sizeof(AvbABData), &serialized); |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 120 | if (io_ret == AVB_IO_RESULT_ERROR_OOM) { |
| 121 | return AVB_IO_RESULT_ERROR_OOM; |
| 122 | } else if (io_ret != AVB_IO_RESULT_OK) { |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 123 | avb_error("Error writing A/B metadata.\n"); |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 124 | return AVB_IO_RESULT_ERROR_IO; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 125 | } |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 126 | return AVB_IO_RESULT_OK; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | static bool slot_is_bootable(AvbABSlotData* slot) { |
| 130 | return slot->priority > 0 && |
| 131 | (slot->successful_boot || (slot->tries_remaining > 0)); |
| 132 | } |
| 133 | |
| 134 | static void slot_set_unbootable(AvbABSlotData* slot) { |
| 135 | slot->priority = 0; |
| 136 | slot->tries_remaining = 0; |
| 137 | slot->successful_boot = 0; |
| 138 | } |
| 139 | |
| 140 | /* Ensure all unbootable and/or illegal states are marked as the |
| 141 | * canonical 'unbootable' state, e.g. priority=0, tries_remaining=0, |
| 142 | * and successful_boot=0. |
| 143 | */ |
| 144 | static void slot_normalize(AvbABSlotData* slot) { |
| 145 | if (slot->priority > 0) { |
| 146 | if (slot->tries_remaining == 0 && !slot->successful_boot) { |
| 147 | /* We've exhausted all tries -> unbootable. */ |
| 148 | slot_set_unbootable(slot); |
| 149 | } |
| 150 | if (slot->tries_remaining > 0 && slot->successful_boot) { |
| 151 | /* Illegal state - avb_ab_mark_slot_successful() will clear |
| 152 | * tries_remaining when setting successful_boot. |
| 153 | */ |
| 154 | slot_set_unbootable(slot); |
| 155 | } |
| 156 | } else { |
| 157 | slot_set_unbootable(slot); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | static const char* slot_suffixes[2] = {"_a", "_b"}; |
| 162 | |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 163 | /* Helper function to load metadata - returns AVB_IO_RESULT_OK on |
| 164 | * success, error code otherwise. |
| 165 | */ |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 166 | static AvbIOResult load_metadata(AvbABOps* ab_ops, AvbABData* ab_data, |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 167 | AvbABData* ab_data_orig) { |
| 168 | AvbIOResult io_ret; |
| 169 | |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 170 | io_ret = ab_ops->read_ab_metadata(ab_ops, ab_data); |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 171 | if (io_ret != AVB_IO_RESULT_OK) { |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 172 | avb_error("I/O error while loading A/B metadata.\n"); |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 173 | return io_ret; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 174 | } |
| 175 | *ab_data_orig = *ab_data; |
| 176 | |
| 177 | /* Ensure data is normalized, e.g. illegal states will be marked as |
| 178 | * unbootable and all unbootable states are represented with |
| 179 | * (priority=0, tries_remaining=0, successful_boot=0). |
| 180 | */ |
| 181 | slot_normalize(&ab_data->slots[0]); |
| 182 | slot_normalize(&ab_data->slots[1]); |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 183 | return AVB_IO_RESULT_OK; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 184 | } |
| 185 | |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 186 | /* Writes A/B metadata to disk only if it has changed - returns |
| 187 | * AVB_IO_RESULT_OK on success, error code otherwise. |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 188 | */ |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 189 | static AvbIOResult save_metadata_if_changed(AvbABOps* ab_ops, |
| 190 | AvbABData* ab_data, |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 191 | AvbABData* ab_data_orig) { |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 192 | if (avb_safe_memcmp(ab_data, ab_data_orig, sizeof(AvbABData)) != 0) { |
| 193 | avb_debug("Writing A/B metadata to disk.\n"); |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 194 | return ab_ops->write_ab_metadata(ab_ops, ab_data); |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 195 | } |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 196 | return AVB_IO_RESULT_OK; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 197 | } |
| 198 | |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 199 | AvbABFlowResult avb_ab_flow(AvbABOps* ab_ops, |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 200 | const char* const* requested_partitions, |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 201 | bool allow_verification_error, |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 202 | AvbSlotVerifyData** out_data) { |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 203 | AvbOps* ops = &(ab_ops->ops); |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 204 | AvbSlotVerifyData* slot_data[2] = {NULL, NULL}; |
| 205 | AvbSlotVerifyData* data = NULL; |
| 206 | AvbABFlowResult ret; |
| 207 | AvbABData ab_data, ab_data_orig; |
| 208 | size_t slot_index_to_boot, n; |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 209 | AvbIOResult io_ret; |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 210 | bool saw_and_allowed_verification_error = false; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 211 | |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 212 | io_ret = load_metadata(ab_ops, &ab_data, &ab_data_orig); |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 213 | if (io_ret == AVB_IO_RESULT_ERROR_OOM) { |
| 214 | ret = AVB_AB_FLOW_RESULT_ERROR_OOM; |
| 215 | goto out; |
| 216 | } else if (io_ret != AVB_IO_RESULT_OK) { |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 217 | ret = AVB_AB_FLOW_RESULT_ERROR_IO; |
| 218 | goto out; |
| 219 | } |
| 220 | |
| 221 | /* Validate all bootable slots. */ |
| 222 | for (n = 0; n < 2; n++) { |
| 223 | if (slot_is_bootable(&ab_data.slots[n])) { |
| 224 | AvbSlotVerifyResult verify_result; |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 225 | bool set_slot_unbootable = false; |
| 226 | |
| 227 | verify_result = |
| 228 | avb_slot_verify(ops, requested_partitions, slot_suffixes[n], |
| 229 | allow_verification_error, &slot_data[n]); |
| 230 | switch (verify_result) { |
| 231 | case AVB_SLOT_VERIFY_RESULT_ERROR_OOM: |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 232 | ret = AVB_AB_FLOW_RESULT_ERROR_OOM; |
| 233 | goto out; |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 234 | |
| 235 | case AVB_SLOT_VERIFY_RESULT_ERROR_IO: |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 236 | ret = AVB_AB_FLOW_RESULT_ERROR_IO; |
| 237 | goto out; |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 238 | |
| 239 | case AVB_SLOT_VERIFY_RESULT_OK: |
| 240 | break; |
| 241 | |
| 242 | case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA: |
| 243 | /* Even with |allow_verification_error| this is game over. */ |
| 244 | set_slot_unbootable = true; |
| 245 | break; |
| 246 | |
| 247 | /* explicit fallthrough. */ |
| 248 | case AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION: |
| 249 | case AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX: |
| 250 | case AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED: |
| 251 | if (allow_verification_error) { |
| 252 | /* Do nothing since we allow this. */ |
| 253 | avb_debugv("Allowing slot ", slot_suffixes[n], |
| 254 | " which verified " |
| 255 | "with result ", |
| 256 | avb_slot_verify_result_to_string(verify_result), |
| 257 | " because |allow_verification_error| is true.\n", NULL); |
| 258 | saw_and_allowed_verification_error = true; |
| 259 | } else { |
| 260 | set_slot_unbootable = true; |
| 261 | } |
| 262 | break; |
| 263 | } |
| 264 | |
| 265 | if (set_slot_unbootable) { |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 266 | avb_errorv("Error verifying slot ", slot_suffixes[n], " with result ", |
| 267 | avb_slot_verify_result_to_string(verify_result), |
| 268 | " - setting unbootable.\n", NULL); |
| 269 | slot_set_unbootable(&ab_data.slots[n]); |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | if (slot_is_bootable(&ab_data.slots[0]) && |
| 275 | slot_is_bootable(&ab_data.slots[1])) { |
| 276 | if (ab_data.slots[1].priority > ab_data.slots[0].priority) { |
| 277 | slot_index_to_boot = 1; |
| 278 | } else { |
| 279 | slot_index_to_boot = 0; |
| 280 | } |
| 281 | } else if (slot_is_bootable(&ab_data.slots[0])) { |
| 282 | slot_index_to_boot = 0; |
| 283 | } else if (slot_is_bootable(&ab_data.slots[1])) { |
| 284 | slot_index_to_boot = 1; |
| 285 | } else { |
| 286 | /* No bootable slots! */ |
| 287 | avb_error("No bootable slots found.\n"); |
| 288 | ret = AVB_AB_FLOW_RESULT_ERROR_NO_BOOTABLE_SLOTS; |
| 289 | goto out; |
| 290 | } |
| 291 | |
| 292 | /* Update stored rollback index such that the stored rollback index |
| 293 | * is the largest value supporting all currently bootable slots. Do |
David Zeuthen | 40ee1da | 2016-11-23 15:14:49 -0500 | [diff] [blame^] | 294 | * this for every rollback index location. |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 295 | */ |
David Zeuthen | 40ee1da | 2016-11-23 15:14:49 -0500 | [diff] [blame^] | 296 | for (n = 0; n < AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS; n++) { |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 297 | uint64_t rollback_index_value = 0; |
| 298 | |
| 299 | if (slot_data[0] != NULL && slot_data[1] != NULL) { |
| 300 | uint64_t a_rollback_index = slot_data[0]->rollback_indexes[n]; |
| 301 | uint64_t b_rollback_index = slot_data[1]->rollback_indexes[n]; |
| 302 | rollback_index_value = |
| 303 | (a_rollback_index < b_rollback_index ? a_rollback_index |
| 304 | : b_rollback_index); |
| 305 | } else if (slot_data[0] != NULL) { |
| 306 | rollback_index_value = slot_data[0]->rollback_indexes[n]; |
| 307 | } else if (slot_data[1] != NULL) { |
| 308 | rollback_index_value = slot_data[1]->rollback_indexes[n]; |
| 309 | } |
| 310 | |
| 311 | if (rollback_index_value != 0) { |
| 312 | uint64_t current_rollback_index_value; |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 313 | io_ret = ops->read_rollback_index(ops, n, ¤t_rollback_index_value); |
| 314 | if (io_ret == AVB_IO_RESULT_ERROR_OOM) { |
| 315 | ret = AVB_AB_FLOW_RESULT_ERROR_OOM; |
| 316 | goto out; |
| 317 | } else if (io_ret != AVB_IO_RESULT_OK) { |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 318 | avb_error("Error getting rollback index for slot.\n"); |
| 319 | ret = AVB_AB_FLOW_RESULT_ERROR_IO; |
| 320 | goto out; |
| 321 | } |
| 322 | if (current_rollback_index_value != rollback_index_value) { |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 323 | io_ret = ops->write_rollback_index(ops, n, rollback_index_value); |
| 324 | if (io_ret == AVB_IO_RESULT_ERROR_OOM) { |
| 325 | ret = AVB_AB_FLOW_RESULT_ERROR_OOM; |
| 326 | goto out; |
| 327 | } else if (io_ret != AVB_IO_RESULT_OK) { |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 328 | avb_error("Error setting stored rollback index.\n"); |
| 329 | ret = AVB_AB_FLOW_RESULT_ERROR_IO; |
| 330 | goto out; |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | /* Finally, select this slot. */ |
| 337 | avb_assert(slot_data[slot_index_to_boot] != NULL); |
| 338 | data = slot_data[slot_index_to_boot]; |
| 339 | slot_data[slot_index_to_boot] = NULL; |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 340 | if (saw_and_allowed_verification_error) { |
| 341 | avb_assert(allow_verification_error); |
| 342 | ret = AVB_AB_FLOW_RESULT_OK_WITH_VERIFICATION_ERROR; |
| 343 | } else { |
| 344 | ret = AVB_AB_FLOW_RESULT_OK; |
| 345 | } |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 346 | |
| 347 | /* ... and decrement tries remaining, if applicable. */ |
| 348 | if (!ab_data.slots[slot_index_to_boot].successful_boot && |
| 349 | ab_data.slots[slot_index_to_boot].tries_remaining > 0) { |
| 350 | ab_data.slots[slot_index_to_boot].tries_remaining -= 1; |
| 351 | } |
| 352 | |
| 353 | out: |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 354 | io_ret = save_metadata_if_changed(ab_ops, &ab_data, &ab_data_orig); |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 355 | if (io_ret != AVB_IO_RESULT_OK) { |
| 356 | if (io_ret == AVB_IO_RESULT_ERROR_OOM) { |
| 357 | ret = AVB_AB_FLOW_RESULT_ERROR_OOM; |
| 358 | } else { |
| 359 | ret = AVB_AB_FLOW_RESULT_ERROR_IO; |
| 360 | } |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 361 | if (data != NULL) { |
| 362 | avb_slot_verify_data_free(data); |
| 363 | data = NULL; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | for (n = 0; n < 2; n++) { |
| 368 | if (slot_data[n] != NULL) { |
| 369 | avb_slot_verify_data_free(slot_data[n]); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | if (out_data != NULL) { |
| 374 | *out_data = data; |
| 375 | } else { |
| 376 | if (data != NULL) { |
| 377 | avb_slot_verify_data_free(data); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | return ret; |
| 382 | } |
| 383 | |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 384 | AvbIOResult avb_ab_mark_slot_active(AvbABOps* ab_ops, |
| 385 | unsigned int slot_number) { |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 386 | AvbABData ab_data, ab_data_orig; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 387 | unsigned int other_slot_number; |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 388 | AvbIOResult ret; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 389 | |
| 390 | avb_assert(slot_number < 2); |
| 391 | |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 392 | ret = load_metadata(ab_ops, &ab_data, &ab_data_orig); |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 393 | if (ret != AVB_IO_RESULT_OK) { |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 394 | goto out; |
| 395 | } |
| 396 | |
| 397 | /* Make requested slot top priority, unsuccessful, and with max tries. */ |
| 398 | ab_data.slots[slot_number].priority = AVB_AB_MAX_PRIORITY; |
| 399 | ab_data.slots[slot_number].tries_remaining = AVB_AB_MAX_TRIES_REMAINING; |
| 400 | ab_data.slots[slot_number].successful_boot = 0; |
| 401 | |
| 402 | /* Ensure other slot doesn't have as high a priority. */ |
| 403 | other_slot_number = 1 - slot_number; |
| 404 | if (ab_data.slots[other_slot_number].priority == AVB_AB_MAX_PRIORITY) { |
| 405 | ab_data.slots[other_slot_number].priority = AVB_AB_MAX_PRIORITY - 1; |
| 406 | } |
| 407 | |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 408 | ret = AVB_IO_RESULT_OK; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 409 | |
| 410 | out: |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 411 | if (ret == AVB_IO_RESULT_OK) { |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 412 | ret = save_metadata_if_changed(ab_ops, &ab_data, &ab_data_orig); |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 413 | } |
| 414 | return ret; |
| 415 | } |
| 416 | |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 417 | AvbIOResult avb_ab_mark_slot_unbootable(AvbABOps* ab_ops, |
| 418 | unsigned int slot_number) { |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 419 | AvbABData ab_data, ab_data_orig; |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 420 | AvbIOResult ret; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 421 | |
| 422 | avb_assert(slot_number < 2); |
| 423 | |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 424 | ret = load_metadata(ab_ops, &ab_data, &ab_data_orig); |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 425 | if (ret != AVB_IO_RESULT_OK) { |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 426 | goto out; |
| 427 | } |
| 428 | |
| 429 | slot_set_unbootable(&ab_data.slots[slot_number]); |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 430 | |
| 431 | ret = AVB_IO_RESULT_OK; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 432 | |
| 433 | out: |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 434 | if (ret == AVB_IO_RESULT_OK) { |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 435 | ret = save_metadata_if_changed(ab_ops, &ab_data, &ab_data_orig); |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 436 | } |
| 437 | return ret; |
| 438 | } |
| 439 | |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 440 | AvbIOResult avb_ab_mark_slot_successful(AvbABOps* ab_ops, |
| 441 | unsigned int slot_number) { |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 442 | AvbABData ab_data, ab_data_orig; |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 443 | AvbIOResult ret; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 444 | |
| 445 | avb_assert(slot_number < 2); |
| 446 | |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 447 | ret = load_metadata(ab_ops, &ab_data, &ab_data_orig); |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 448 | if (ret != AVB_IO_RESULT_OK) { |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 449 | goto out; |
| 450 | } |
| 451 | |
| 452 | if (!slot_is_bootable(&ab_data.slots[slot_number])) { |
| 453 | avb_error("Cannot mark unbootable slot as successful.\n"); |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 454 | ret = AVB_IO_RESULT_OK; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 455 | goto out; |
| 456 | } |
| 457 | |
| 458 | ab_data.slots[slot_number].tries_remaining = 0; |
| 459 | ab_data.slots[slot_number].successful_boot = 1; |
| 460 | |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 461 | ret = AVB_IO_RESULT_OK; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 462 | |
| 463 | out: |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 464 | if (ret == AVB_IO_RESULT_OK) { |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 465 | ret = save_metadata_if_changed(ab_ops, &ab_data, &ab_data_orig); |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 466 | } |
| 467 | return ret; |
| 468 | } |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 469 | |
| 470 | const char* avb_ab_flow_result_to_string(AvbABFlowResult result) { |
| 471 | const char* ret = NULL; |
| 472 | |
| 473 | switch (result) { |
| 474 | case AVB_AB_FLOW_RESULT_OK: |
| 475 | ret = "OK"; |
| 476 | break; |
| 477 | |
| 478 | case AVB_AB_FLOW_RESULT_OK_WITH_VERIFICATION_ERROR: |
| 479 | ret = "OK_WITH_VERIFICATION_ERROR"; |
| 480 | break; |
| 481 | |
| 482 | case AVB_AB_FLOW_RESULT_ERROR_OOM: |
| 483 | ret = "ERROR_OOM"; |
| 484 | break; |
| 485 | |
| 486 | case AVB_AB_FLOW_RESULT_ERROR_IO: |
| 487 | ret = "ERROR_IO"; |
| 488 | break; |
| 489 | |
| 490 | case AVB_AB_FLOW_RESULT_ERROR_NO_BOOTABLE_SLOTS: |
| 491 | ret = "ERROR_NO_BOOTABLE_SLOTS"; |
| 492 | break; |
| 493 | /* Do not add a 'default:' case here because of -Wswitch. */ |
| 494 | } |
| 495 | |
| 496 | if (ret == NULL) { |
| 497 | avb_error("Unknown AvbABFlowResult value.\n"); |
| 498 | ret = "(unknown)"; |
| 499 | } |
| 500 | |
| 501 | return ret; |
| 502 | } |