blob: 37f02167909744aebea08b8da27611cb6f6ad43b [file] [log] [blame]
David Zeuthen8b6973b2016-09-20 12:39:49 -04001/*
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 Zeuthen8b6973b2016-09-20 12:39:49 -040026
27bool 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
55void 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
62void 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 Zeuthenbaf59e22016-11-14 15:39:43 -050083AvbIOResult avb_ab_data_read(AvbABOps* ab_ops, AvbABData* data) {
84 AvbOps* ops = &(ab_ops->ops);
David Zeuthen8b6973b2016-09-20 12:39:49 -040085 AvbABData serialized;
David Zeuthen507752b2016-09-29 16:31:18 -040086 AvbIOResult io_ret;
David Zeuthen8b6973b2016-09-20 12:39:49 -040087 size_t num_bytes_read;
88
David Zeuthen507752b2016-09-29 16:31:18 -040089 io_ret =
David Zeuthen8b6973b2016-09-20 12:39:49 -040090 ops->read_from_partition(ops, "misc", AB_METADATA_MISC_PARTITION_OFFSET,
91 sizeof(AvbABData), &serialized, &num_bytes_read);
David Zeuthen507752b2016-09-29 16:31:18 -040092 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 Zeuthen8b6973b2016-09-20 12:39:49 -040096 avb_error("Error reading A/B metadata.\n");
David Zeuthen507752b2016-09-29 16:31:18 -040097 return AVB_IO_RESULT_ERROR_IO;
David Zeuthen8b6973b2016-09-20 12:39:49 -040098 }
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 Zeuthenbaf59e22016-11-14 15:39:43 -0500105 return avb_ab_data_write(ab_ops, data);
David Zeuthen8b6973b2016-09-20 12:39:49 -0400106 }
107
David Zeuthen507752b2016-09-29 16:31:18 -0400108 return AVB_IO_RESULT_OK;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400109}
110
David Zeuthenbaf59e22016-11-14 15:39:43 -0500111AvbIOResult avb_ab_data_write(AvbABOps* ab_ops, const AvbABData* data) {
112 AvbOps* ops = &(ab_ops->ops);
David Zeuthen8b6973b2016-09-20 12:39:49 -0400113 AvbABData serialized;
David Zeuthen507752b2016-09-29 16:31:18 -0400114 AvbIOResult io_ret;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400115
116 avb_ab_data_update_crc_and_byteswap(data, &serialized);
David Zeuthen507752b2016-09-29 16:31:18 -0400117 io_ret =
David Zeuthen8b6973b2016-09-20 12:39:49 -0400118 ops->write_to_partition(ops, "misc", AB_METADATA_MISC_PARTITION_OFFSET,
119 sizeof(AvbABData), &serialized);
David Zeuthen507752b2016-09-29 16:31:18 -0400120 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 Zeuthen8b6973b2016-09-20 12:39:49 -0400123 avb_error("Error writing A/B metadata.\n");
David Zeuthen507752b2016-09-29 16:31:18 -0400124 return AVB_IO_RESULT_ERROR_IO;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400125 }
David Zeuthen507752b2016-09-29 16:31:18 -0400126 return AVB_IO_RESULT_OK;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400127}
128
129static bool slot_is_bootable(AvbABSlotData* slot) {
130 return slot->priority > 0 &&
131 (slot->successful_boot || (slot->tries_remaining > 0));
132}
133
134static 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 */
144static 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
161static const char* slot_suffixes[2] = {"_a", "_b"};
162
David Zeuthen507752b2016-09-29 16:31:18 -0400163/* Helper function to load metadata - returns AVB_IO_RESULT_OK on
164 * success, error code otherwise.
165 */
David Zeuthenbaf59e22016-11-14 15:39:43 -0500166static AvbIOResult load_metadata(AvbABOps* ab_ops, AvbABData* ab_data,
David Zeuthen507752b2016-09-29 16:31:18 -0400167 AvbABData* ab_data_orig) {
168 AvbIOResult io_ret;
169
David Zeuthenbaf59e22016-11-14 15:39:43 -0500170 io_ret = ab_ops->read_ab_metadata(ab_ops, ab_data);
David Zeuthen507752b2016-09-29 16:31:18 -0400171 if (io_ret != AVB_IO_RESULT_OK) {
David Zeuthen8b6973b2016-09-20 12:39:49 -0400172 avb_error("I/O error while loading A/B metadata.\n");
David Zeuthen507752b2016-09-29 16:31:18 -0400173 return io_ret;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400174 }
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 Zeuthen507752b2016-09-29 16:31:18 -0400183 return AVB_IO_RESULT_OK;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400184}
185
David Zeuthen507752b2016-09-29 16:31:18 -0400186/* Writes A/B metadata to disk only if it has changed - returns
187 * AVB_IO_RESULT_OK on success, error code otherwise.
David Zeuthen8b6973b2016-09-20 12:39:49 -0400188 */
David Zeuthenbaf59e22016-11-14 15:39:43 -0500189static AvbIOResult save_metadata_if_changed(AvbABOps* ab_ops,
190 AvbABData* ab_data,
David Zeuthen507752b2016-09-29 16:31:18 -0400191 AvbABData* ab_data_orig) {
David Zeuthen8b6973b2016-09-20 12:39:49 -0400192 if (avb_safe_memcmp(ab_data, ab_data_orig, sizeof(AvbABData)) != 0) {
193 avb_debug("Writing A/B metadata to disk.\n");
David Zeuthenbaf59e22016-11-14 15:39:43 -0500194 return ab_ops->write_ab_metadata(ab_ops, ab_data);
David Zeuthen8b6973b2016-09-20 12:39:49 -0400195 }
David Zeuthen507752b2016-09-29 16:31:18 -0400196 return AVB_IO_RESULT_OK;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400197}
198
David Zeuthenbaf59e22016-11-14 15:39:43 -0500199AvbABFlowResult avb_ab_flow(AvbABOps* ab_ops,
David Zeuthena8bb9a02016-10-28 14:36:55 -0400200 const char* const* requested_partitions,
David Zeuthen0155e6b2016-11-16 17:58:13 -0500201 bool allow_verification_error,
David Zeuthena8bb9a02016-10-28 14:36:55 -0400202 AvbSlotVerifyData** out_data) {
David Zeuthenbaf59e22016-11-14 15:39:43 -0500203 AvbOps* ops = &(ab_ops->ops);
David Zeuthen8b6973b2016-09-20 12:39:49 -0400204 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 Zeuthen507752b2016-09-29 16:31:18 -0400209 AvbIOResult io_ret;
David Zeuthen0155e6b2016-11-16 17:58:13 -0500210 bool saw_and_allowed_verification_error = false;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400211
David Zeuthenbaf59e22016-11-14 15:39:43 -0500212 io_ret = load_metadata(ab_ops, &ab_data, &ab_data_orig);
David Zeuthen507752b2016-09-29 16:31:18 -0400213 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 Zeuthen8b6973b2016-09-20 12:39:49 -0400217 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 Zeuthen0155e6b2016-11-16 17:58:13 -0500225 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 Zeuthen8b6973b2016-09-20 12:39:49 -0400232 ret = AVB_AB_FLOW_RESULT_ERROR_OOM;
233 goto out;
David Zeuthen0155e6b2016-11-16 17:58:13 -0500234
235 case AVB_SLOT_VERIFY_RESULT_ERROR_IO:
David Zeuthen8b6973b2016-09-20 12:39:49 -0400236 ret = AVB_AB_FLOW_RESULT_ERROR_IO;
237 goto out;
David Zeuthen0155e6b2016-11-16 17:58:13 -0500238
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 Zeuthen8b6973b2016-09-20 12:39:49 -0400266 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 Zeuthen40ee1da2016-11-23 15:14:49 -0500294 * this for every rollback index location.
David Zeuthen8b6973b2016-09-20 12:39:49 -0400295 */
David Zeuthen40ee1da2016-11-23 15:14:49 -0500296 for (n = 0; n < AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS; n++) {
David Zeuthen8b6973b2016-09-20 12:39:49 -0400297 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 Zeuthen507752b2016-09-29 16:31:18 -0400313 io_ret = ops->read_rollback_index(ops, n, &current_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 Zeuthen8b6973b2016-09-20 12:39:49 -0400318 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 Zeuthen507752b2016-09-29 16:31:18 -0400323 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 Zeuthen8b6973b2016-09-20 12:39:49 -0400328 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 Zeuthen0155e6b2016-11-16 17:58:13 -0500340 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 Zeuthen8b6973b2016-09-20 12:39:49 -0400346
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
353out:
David Zeuthenbaf59e22016-11-14 15:39:43 -0500354 io_ret = save_metadata_if_changed(ab_ops, &ab_data, &ab_data_orig);
David Zeuthen507752b2016-09-29 16:31:18 -0400355 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 Zeuthen8b6973b2016-09-20 12:39:49 -0400361 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 Zeuthenbaf59e22016-11-14 15:39:43 -0500384AvbIOResult avb_ab_mark_slot_active(AvbABOps* ab_ops,
385 unsigned int slot_number) {
David Zeuthen8b6973b2016-09-20 12:39:49 -0400386 AvbABData ab_data, ab_data_orig;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400387 unsigned int other_slot_number;
David Zeuthen507752b2016-09-29 16:31:18 -0400388 AvbIOResult ret;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400389
390 avb_assert(slot_number < 2);
391
David Zeuthenbaf59e22016-11-14 15:39:43 -0500392 ret = load_metadata(ab_ops, &ab_data, &ab_data_orig);
David Zeuthen507752b2016-09-29 16:31:18 -0400393 if (ret != AVB_IO_RESULT_OK) {
David Zeuthen8b6973b2016-09-20 12:39:49 -0400394 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 Zeuthen507752b2016-09-29 16:31:18 -0400408 ret = AVB_IO_RESULT_OK;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400409
410out:
David Zeuthen507752b2016-09-29 16:31:18 -0400411 if (ret == AVB_IO_RESULT_OK) {
David Zeuthenbaf59e22016-11-14 15:39:43 -0500412 ret = save_metadata_if_changed(ab_ops, &ab_data, &ab_data_orig);
David Zeuthen8b6973b2016-09-20 12:39:49 -0400413 }
414 return ret;
415}
416
David Zeuthenbaf59e22016-11-14 15:39:43 -0500417AvbIOResult avb_ab_mark_slot_unbootable(AvbABOps* ab_ops,
418 unsigned int slot_number) {
David Zeuthen8b6973b2016-09-20 12:39:49 -0400419 AvbABData ab_data, ab_data_orig;
David Zeuthen507752b2016-09-29 16:31:18 -0400420 AvbIOResult ret;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400421
422 avb_assert(slot_number < 2);
423
David Zeuthenbaf59e22016-11-14 15:39:43 -0500424 ret = load_metadata(ab_ops, &ab_data, &ab_data_orig);
David Zeuthen507752b2016-09-29 16:31:18 -0400425 if (ret != AVB_IO_RESULT_OK) {
David Zeuthen8b6973b2016-09-20 12:39:49 -0400426 goto out;
427 }
428
429 slot_set_unbootable(&ab_data.slots[slot_number]);
David Zeuthen507752b2016-09-29 16:31:18 -0400430
431 ret = AVB_IO_RESULT_OK;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400432
433out:
David Zeuthen507752b2016-09-29 16:31:18 -0400434 if (ret == AVB_IO_RESULT_OK) {
David Zeuthenbaf59e22016-11-14 15:39:43 -0500435 ret = save_metadata_if_changed(ab_ops, &ab_data, &ab_data_orig);
David Zeuthen8b6973b2016-09-20 12:39:49 -0400436 }
437 return ret;
438}
439
David Zeuthenbaf59e22016-11-14 15:39:43 -0500440AvbIOResult avb_ab_mark_slot_successful(AvbABOps* ab_ops,
441 unsigned int slot_number) {
David Zeuthen8b6973b2016-09-20 12:39:49 -0400442 AvbABData ab_data, ab_data_orig;
David Zeuthen507752b2016-09-29 16:31:18 -0400443 AvbIOResult ret;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400444
445 avb_assert(slot_number < 2);
446
David Zeuthenbaf59e22016-11-14 15:39:43 -0500447 ret = load_metadata(ab_ops, &ab_data, &ab_data_orig);
David Zeuthen507752b2016-09-29 16:31:18 -0400448 if (ret != AVB_IO_RESULT_OK) {
David Zeuthen8b6973b2016-09-20 12:39:49 -0400449 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 Zeuthen507752b2016-09-29 16:31:18 -0400454 ret = AVB_IO_RESULT_OK;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400455 goto out;
456 }
457
458 ab_data.slots[slot_number].tries_remaining = 0;
459 ab_data.slots[slot_number].successful_boot = 1;
460
David Zeuthen507752b2016-09-29 16:31:18 -0400461 ret = AVB_IO_RESULT_OK;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400462
463out:
David Zeuthen507752b2016-09-29 16:31:18 -0400464 if (ret == AVB_IO_RESULT_OK) {
David Zeuthenbaf59e22016-11-14 15:39:43 -0500465 ret = save_metadata_if_changed(ab_ops, &ab_data, &ab_data_orig);
David Zeuthen8b6973b2016-09-20 12:39:49 -0400466 }
467 return ret;
468}
David Zeuthen0155e6b2016-11-16 17:58:13 -0500469
470const 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}