blob: 905bcf6dc069c844cc6b954a8e5b50a49ebdf22a [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
David Brownaac71112020-02-03 16:13:42 -07002 * SPDX-License-Identifier: Apache-2.0
3 *
4 * Copyright (c) 2017-2019 Linaro LTD
5 * Copyright (c) 2016-2019 JUUL Labs
Raef Colese8fe6cf2020-05-26 13:07:40 +01006 * Copyright (c) 2019-2020 Arm Limited
David Brownaac71112020-02-03 16:13:42 -07007 *
8 * Original license:
9 *
Christopher Collins92ea77f2016-12-12 15:59:26 -080010 * Licensed to the Apache Software Foundation (ASF) under one
11 * or more contributor license agreements. See the NOTICE file
12 * distributed with this work for additional information
13 * regarding copyright ownership. The ASF licenses this file
14 * to you under the Apache License, Version 2.0 (the
15 * "License"); you may not use this file except in compliance
16 * with the License. You may obtain a copy of the License at
17 *
18 * http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing,
21 * software distributed under the License is distributed on an
22 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
23 * KIND, either express or implied. See the License for the
24 * specific language governing permissions and limitations
25 * under the License.
26 */
27
Christopher Collins92ea77f2016-12-12 15:59:26 -080028#include <string.h>
29#include <inttypes.h>
Fabio Utziga0bc9b52017-06-28 09:19:55 -030030#include <stddef.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080031
Christopher Collins92ea77f2016-12-12 15:59:26 -080032#include "sysflash/sysflash.h"
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020033#include "flash_map_backend/flash_map_backend.h"
34
Christopher Collins92ea77f2016-12-12 15:59:26 -080035#include "bootutil/image.h"
36#include "bootutil/bootutil.h"
37#include "bootutil_priv.h"
Fabio Utzig7ebb7c22017-04-26 10:59:31 -030038#include "bootutil/bootutil_log.h"
Raef Colese8fe6cf2020-05-26 13:07:40 +010039#include "bootutil/fault_injection_hardening.h"
Fabio Utzigba829042018-09-18 08:29:34 -030040#ifdef MCUBOOT_ENC_IMAGES
41#include "bootutil/enc_key.h"
42#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -030043
Bohdan Kovalchuk7460ae52020-07-21 17:03:57 +030044#ifdef MCUBOOT_SWAP_USING_STATUS
45#include "swap_status.h"
46#endif
47
48#include "mcuboot_config/mcuboot_config.h"
49
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010050MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
51
Fabio Utzig10ee6482019-08-01 12:04:52 -030052/* Currently only used by imgmgr */
Christopher Collins92ea77f2016-12-12 15:59:26 -080053int boot_current_slot;
54
Fabio Utzig24a273d2017-04-20 08:21:31 -030055const uint32_t boot_img_magic[] = {
Christopher Collins92ea77f2016-12-12 15:59:26 -080056 0xf395c277,
57 0x7fefd260,
58 0x0f505235,
59 0x8079b62c,
60};
61
Hovland, Sigvart1d96f362018-09-25 13:23:42 +020062#define BOOT_MAGIC_ARR_SZ \
63 (sizeof boot_img_magic / sizeof boot_img_magic[0])
64
Christopher Collins92ea77f2016-12-12 15:59:26 -080065struct boot_swap_table {
David Vincze2d736ad2019-02-18 11:50:22 +010066 uint8_t magic_primary_slot;
67 uint8_t magic_secondary_slot;
68 uint8_t image_ok_primary_slot;
69 uint8_t image_ok_secondary_slot;
70 uint8_t copy_done_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -080071
Fabio Utzigb5b2f552017-06-30 10:03:47 -030072 uint8_t swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -080073};
74
75/**
76 * This set of tables maps image trailer contents to swap operation type.
77 * When searching for a match, these tables must be iterated sequentially.
Fabio Utzigb5b2f552017-06-30 10:03:47 -030078 *
David Vincze2d736ad2019-02-18 11:50:22 +010079 * NOTE: the table order is very important. The settings in the secondary
80 * slot always are priority to the primary slot and should be located
81 * earlier in the table.
Fabio Utzigb5b2f552017-06-30 10:03:47 -030082 *
83 * The table lists only states where there is action needs to be taken by
84 * the bootloader, as in starting/finishing a swap operation.
Christopher Collins92ea77f2016-12-12 15:59:26 -080085 */
86static const struct boot_swap_table boot_swap_tables[] = {
87 {
David Vincze2d736ad2019-02-18 11:50:22 +010088 .magic_primary_slot = BOOT_MAGIC_ANY,
89 .magic_secondary_slot = BOOT_MAGIC_GOOD,
90 .image_ok_primary_slot = BOOT_FLAG_ANY,
91 .image_ok_secondary_slot = BOOT_FLAG_UNSET,
92 .copy_done_primary_slot = BOOT_FLAG_ANY,
93 .swap_type = BOOT_SWAP_TYPE_TEST,
Christopher Collins92ea77f2016-12-12 15:59:26 -080094 },
Christopher Collins92ea77f2016-12-12 15:59:26 -080095 {
David Vincze2d736ad2019-02-18 11:50:22 +010096 .magic_primary_slot = BOOT_MAGIC_ANY,
97 .magic_secondary_slot = BOOT_MAGIC_GOOD,
98 .image_ok_primary_slot = BOOT_FLAG_ANY,
99 .image_ok_secondary_slot = BOOT_FLAG_SET,
100 .copy_done_primary_slot = BOOT_FLAG_ANY,
101 .swap_type = BOOT_SWAP_TYPE_PERM,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800102 },
Christopher Collins92ea77f2016-12-12 15:59:26 -0800103 {
David Vincze2d736ad2019-02-18 11:50:22 +0100104 .magic_primary_slot = BOOT_MAGIC_GOOD,
105 .magic_secondary_slot = BOOT_MAGIC_UNSET,
106 .image_ok_primary_slot = BOOT_FLAG_UNSET,
107 .image_ok_secondary_slot = BOOT_FLAG_ANY,
108 .copy_done_primary_slot = BOOT_FLAG_SET,
109 .swap_type = BOOT_SWAP_TYPE_REVERT,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800110 },
111};
112
113#define BOOT_SWAP_TABLES_COUNT \
114 (sizeof boot_swap_tables / sizeof boot_swap_tables[0])
115
Raef Colese8fe6cf2020-05-26 13:07:40 +0100116/**
117 * @brief Determine if the data at two memory addresses is equal
118 *
119 * @param s1 The first memory region to compare.
120 * @param s2 The second memory region to compare.
121 * @param n The amount of bytes to compare.
122 *
123 * @note This function does not comply with the specification of memcmp,
124 * so should not be considered a drop-in replacement. It has no
125 * constant time execution. The point is to make sure that all the
126 * bytes are compared and detect if loop was abused and some cycles
127 * was skipped due to fault injection.
128 *
129 * @return FIH_SUCCESS if memory regions are equal, otherwise FIH_FAILURE
130 */
131#ifdef MCUBOOT_FIH_PROFILE_OFF
132inline
133fih_int boot_fih_memequal(const void *s1, const void *s2, size_t n)
134{
135 return memcmp(s1, s2, n);
136}
137#else
138fih_int boot_fih_memequal(const void *s1, const void *s2, size_t n)
139{
140 size_t i;
141 uint8_t *s1_p = (uint8_t*) s1;
142 uint8_t *s2_p = (uint8_t*) s2;
143 fih_int ret = FIH_FAILURE;
144
145 for (i = 0; i < n; i++) {
146 if (s1_p[i] != s2_p[i]) {
147 goto out;
148 }
149 }
150 if (i == n) {
151 ret = FIH_SUCCESS;
152 }
153
154out:
155 FIH_RET(ret);
156}
157#endif
158
Bohdan Kovalchuk7460ae52020-07-21 17:03:57 +0300159#ifndef MCUBOOT_SWAP_USING_STATUS
160
Fabio Utzig39000012018-07-30 12:40:20 -0300161static int
Fabio Utzig178be542018-09-19 08:12:56 -0300162boot_magic_decode(const uint32_t *magic)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800163{
Fabio Utzig24a273d2017-04-20 08:21:31 -0300164 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800165 return BOOT_MAGIC_GOOD;
166 }
Fabio Utzig178be542018-09-19 08:12:56 -0300167 return BOOT_MAGIC_BAD;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800168}
169
Fabio Utzig39000012018-07-30 12:40:20 -0300170static int
Fabio Utzig178be542018-09-19 08:12:56 -0300171boot_flag_decode(uint8_t flag)
Fabio Utzig39000012018-07-30 12:40:20 -0300172{
Fabio Utzig39000012018-07-30 12:40:20 -0300173 if (flag != BOOT_FLAG_SET) {
174 return BOOT_FLAG_BAD;
175 }
176 return BOOT_FLAG_SET;
177}
Bohdan Kovalchuk7460ae52020-07-21 17:03:57 +0300178#endif /* not defined MCUBOOT_SWAP_USING_STATUS */
Christopher Collinsa1c12042019-05-23 14:00:28 -0700179/**
180 * Determines if a status source table is satisfied by the specified magic
181 * code.
182 *
183 * @param tbl_val A magic field from a status source table.
184 * @param val The magic value in a trailer, encoded as a
185 * BOOT_MAGIC_[...].
186 *
187 * @return 1 if the two values are compatible;
188 * 0 otherwise.
189 */
190int
191boot_magic_compatible_check(uint8_t tbl_val, uint8_t val)
192{
193 switch (tbl_val) {
194 case BOOT_MAGIC_ANY:
195 return 1;
196
197 case BOOT_MAGIC_NOTGOOD:
198 return val != BOOT_MAGIC_GOOD;
199
200 default:
201 return tbl_val == val;
202 }
203}
204
Christopher Collins92ea77f2016-12-12 15:59:26 -0800205uint32_t
Fabio Utzig3fbbdac2019-12-19 15:18:23 -0300206boot_status_sz(uint32_t min_write_sz)
207{
208 return /* state for all sectors */
209 BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz;
210}
211
212uint32_t
David Brownab449182019-11-15 09:32:52 -0700213boot_trailer_sz(uint32_t min_write_sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800214{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300215 return /* state for all sectors */
Fabio Utzig3fbbdac2019-12-19 15:18:23 -0300216 boot_status_sz(min_write_sz) +
Fabio Utzigba829042018-09-18 08:29:34 -0300217#ifdef MCUBOOT_ENC_IMAGES
218 /* encryption keys */
Fabio Utzig4741c452019-12-19 15:32:41 -0300219# if MCUBOOT_SWAP_SAVE_ENCTLV
220 BOOT_ENC_TLV_ALIGN_SIZE * 2 +
221# else
Fabio Utzigba829042018-09-18 08:29:34 -0300222 BOOT_ENC_KEY_SIZE * 2 +
Fabio Utzig4741c452019-12-19 15:32:41 -0300223# endif
Fabio Utzigba829042018-09-18 08:29:34 -0300224#endif
Christopher Collins2adef702019-05-22 14:37:31 -0700225 /* swap_type + copy_done + image_ok + swap_size */
226 BOOT_MAX_ALIGN * 4 +
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300227 BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800228}
229
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400230int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300231boot_status_entries(int image_index, const struct flash_area *fap)
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400232{
Fabio Utzig12d59162019-11-28 10:01:59 -0300233#if MCUBOOT_SWAP_USING_SCRATCH
David Vinczeb75c12a2019-03-22 14:58:33 +0100234 if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400235 return BOOT_STATUS_STATE_COUNT;
Fabio Utzig12d59162019-11-28 10:01:59 -0300236 } else
237#endif
238 if (fap->fa_id == FLASH_AREA_IMAGE_PRIMARY(image_index) ||
Fabio Utzigb0f04732019-07-31 09:49:19 -0300239 fap->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vinczeb75c12a2019-03-22 14:58:33 +0100240 return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES;
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400241 }
Fabio Utzig9d160092019-08-09 07:46:34 -0300242 return -1;
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400243}
244
Bohdan Kovalchuk7460ae52020-07-21 17:03:57 +0300245#ifndef MCUBOOT_SWAP_USING_STATUS
Christopher Collins92ea77f2016-12-12 15:59:26 -0800246uint32_t
247boot_status_off(const struct flash_area *fap)
248{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300249 uint32_t off_from_end;
250 uint8_t elem_sz;
251
252 elem_sz = flash_area_align(fap);
253
Christopher Collins2adef702019-05-22 14:37:31 -0700254 off_from_end = boot_trailer_sz(elem_sz);
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300255
256 assert(off_from_end <= fap->fa_size);
257 return fap->fa_size - off_from_end;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800258}
Bohdan Kovalchuk7460ae52020-07-21 17:03:57 +0300259#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800260
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300261static inline uint32_t
262boot_magic_off(const struct flash_area *fap)
263{
264 return fap->fa_size - BOOT_MAGIC_SZ;
265}
266
Bohdan Kovalchuk7460ae52020-07-21 17:03:57 +0300267#ifndef MCUBOOT_SWAP_USING_STATUS
268
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300269static inline uint32_t
270boot_image_ok_off(const struct flash_area *fap)
271{
272 return boot_magic_off(fap) - BOOT_MAX_ALIGN;
273}
274
275static inline uint32_t
276boot_copy_done_off(const struct flash_area *fap)
277{
278 return boot_image_ok_off(fap) - BOOT_MAX_ALIGN;
279}
280
Christopher Collinsa1c12042019-05-23 14:00:28 -0700281uint32_t
David Vinczee2453472019-06-17 12:31:59 +0200282boot_swap_info_off(const struct flash_area *fap)
Christopher Collinsa1c12042019-05-23 14:00:28 -0700283{
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300284 return boot_copy_done_off(fap) - BOOT_MAX_ALIGN;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700285}
286
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300287static inline uint32_t
Fabio Utzig46490722017-09-04 15:34:32 -0300288boot_swap_size_off(const struct flash_area *fap)
289{
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300290 return boot_swap_info_off(fap) - BOOT_MAX_ALIGN;
Fabio Utzig46490722017-09-04 15:34:32 -0300291}
Bohdan Kovalchuk7460ae52020-07-21 17:03:57 +0300292#endif
Fabio Utzig46490722017-09-04 15:34:32 -0300293
Fabio Utzigba829042018-09-18 08:29:34 -0300294#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300295static inline uint32_t
Fabio Utzigba829042018-09-18 08:29:34 -0300296boot_enc_key_off(const struct flash_area *fap, uint8_t slot)
297{
Fabio Utzig4741c452019-12-19 15:32:41 -0300298#if MCUBOOT_SWAP_SAVE_ENCTLV
299 return boot_swap_size_off(fap) - ((slot + 1) *
300 ((((BOOT_ENC_TLV_SIZE - 1) / BOOT_MAX_ALIGN) + 1) * BOOT_MAX_ALIGN));
301#else
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300302 return boot_swap_size_off(fap) - ((slot + 1) * BOOT_ENC_KEY_SIZE);
Fabio Utzig4741c452019-12-19 15:32:41 -0300303#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300304}
305#endif
306
Fabio Utzig4b2e55f2020-09-24 11:49:20 -0300307bool bootutil_buffer_is_erased(const struct flash_area *area,
308 const void *buffer, size_t len)
309{
310 size_t i;
311 uint8_t *u8b;
312 uint8_t erased_val;
313
314 if (buffer == NULL || len == 0) {
315 return false;
316 }
317
318 erased_val = flash_area_erased_val(area);
319 for (i = 0, u8b = (uint8_t *)buffer; i < len; i++) {
320 if (u8b[i] != erased_val) {
321 return false;
322 }
323 }
324
325 return true;
326}
327
Bohdan Kovalchuk7460ae52020-07-21 17:03:57 +0300328#ifndef MCUBOOT_SWAP_USING_STATUS
Christopher Collins92ea77f2016-12-12 15:59:26 -0800329int
330boot_read_swap_state(const struct flash_area *fap,
331 struct boot_swap_state *state)
332{
Hovland, Sigvart1d96f362018-09-25 13:23:42 +0200333 uint32_t magic[BOOT_MAGIC_ARR_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800334 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200335 uint8_t swap_info;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800336 int rc;
337
338 off = boot_magic_off(fap);
Fabio Utzig4b2e55f2020-09-24 11:49:20 -0300339 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
Fabio Utzig178be542018-09-19 08:12:56 -0300340 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800341 return BOOT_EFLASH;
342 }
Fabio Utzig4b2e55f2020-09-24 11:49:20 -0300343 if (bootutil_buffer_is_erased(fap, magic, BOOT_MAGIC_SZ)) {
Fabio Utzig178be542018-09-19 08:12:56 -0300344 state->magic = BOOT_MAGIC_UNSET;
345 } else {
346 state->magic = boot_magic_decode(magic);
347 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800348
David Vinczee2453472019-06-17 12:31:59 +0200349 off = boot_swap_info_off(fap);
Fabio Utzig4b2e55f2020-09-24 11:49:20 -0300350 rc = flash_area_read(fap, off, &swap_info, sizeof swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700351 if (rc < 0) {
352 return BOOT_EFLASH;
353 }
David Vinczee2453472019-06-17 12:31:59 +0200354
355 /* Extract the swap type and image number */
356 state->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
357 state->image_num = BOOT_GET_IMAGE_NUM(swap_info);
358
Fabio Utzig4b2e55f2020-09-24 11:49:20 -0300359 if (bootutil_buffer_is_erased(fap, &swap_info, sizeof swap_info) ||
360 state->swap_type > BOOT_SWAP_TYPE_REVERT) {
Christopher Collinsa1c12042019-05-23 14:00:28 -0700361 state->swap_type = BOOT_SWAP_TYPE_NONE;
David Vinczee2453472019-06-17 12:31:59 +0200362 state->image_num = 0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700363 }
364
Christopher Collins2adef702019-05-22 14:37:31 -0700365 off = boot_copy_done_off(fap);
Fabio Utzig4b2e55f2020-09-24 11:49:20 -0300366 rc = flash_area_read(fap, off, &state->copy_done, sizeof state->copy_done);
Christopher Collins2adef702019-05-22 14:37:31 -0700367 if (rc < 0) {
368 return BOOT_EFLASH;
369 }
Fabio Utzig4b2e55f2020-09-24 11:49:20 -0300370 if (bootutil_buffer_is_erased(fap, &state->copy_done,
371 sizeof state->copy_done)) {
Christopher Collins2adef702019-05-22 14:37:31 -0700372 state->copy_done = BOOT_FLAG_UNSET;
373 } else {
374 state->copy_done = boot_flag_decode(state->copy_done);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800375 }
376
377 off = boot_image_ok_off(fap);
Fabio Utzig4b2e55f2020-09-24 11:49:20 -0300378 rc = flash_area_read(fap, off, &state->image_ok, sizeof state->image_ok);
Fabio Utzig178be542018-09-19 08:12:56 -0300379 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800380 return BOOT_EFLASH;
381 }
Fabio Utzig4b2e55f2020-09-24 11:49:20 -0300382 if (bootutil_buffer_is_erased(fap, &state->image_ok,
383 sizeof state->image_ok)) {
Fabio Utzig178be542018-09-19 08:12:56 -0300384 state->image_ok = BOOT_FLAG_UNSET;
385 } else {
386 state->image_ok = boot_flag_decode(state->image_ok);
387 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800388
389 return 0;
390}
Bohdan Kovalchuk7460ae52020-07-21 17:03:57 +0300391#endif /* not MCUBOOT_SWAP_USING_STATUS */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800392
393/**
394 * Reads the image trailer from the scratch area.
395 */
396int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300397boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800398{
399 const struct flash_area *fap;
400 int rc;
401
Fabio Utzigb0f04732019-07-31 09:49:19 -0300402 rc = flash_area_open(flash_area_id, &fap);
403 if (rc != 0) {
404 return BOOT_EFLASH;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300405 }
406
407 rc = boot_read_swap_state(fap, state);
Fabio Utzigacfba2e2017-05-22 11:06:29 -0400408 flash_area_close(fap);
409 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800410}
411
Bohdan Kovalchuk7460ae52020-07-21 17:03:57 +0300412#ifndef MCUBOOT_SWAP_USING_STATUS
413
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300414/**
415 * This functions tries to locate the status area after an aborted swap,
416 * by looking for the magic in the possible locations.
417 *
Sam Bristowd0ca0ff2019-10-30 20:51:35 +1300418 * If the magic is successfully found, a flash_area * is returned and it
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300419 * is the responsibility of the called to close it.
420 *
421 * @returns 0 on success, -1 on errors
422 */
423static int
424boot_find_status(int image_index, const struct flash_area **fap)
425{
426 uint32_t magic[BOOT_MAGIC_ARR_SZ];
427 uint32_t off;
428 uint8_t areas[2] = {
Fabio Utzig12d59162019-11-28 10:01:59 -0300429#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300430 FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig12d59162019-11-28 10:01:59 -0300431#endif
Fabio Utzig3c446072019-11-22 12:48:26 -0300432 FLASH_AREA_IMAGE_PRIMARY(image_index),
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300433 };
434 unsigned int i;
435 int rc;
436
437 /*
438 * In the middle a swap, tries to locate the area that is currently
439 * storing a valid magic, first on the primary slot, then on scratch.
440 * Both "slots" can end up being temporary storage for a swap and it
441 * is assumed that if magic is valid then other metadata is too,
442 * because magic is always written in the last step.
443 */
444
445 for (i = 0; i < sizeof(areas) / sizeof(areas[0]); i++) {
446 rc = flash_area_open(areas[i], fap);
447 if (rc != 0) {
448 return rc;
449 }
450
451 off = boot_magic_off(*fap);
452 rc = flash_area_read(*fap, off, magic, BOOT_MAGIC_SZ);
453 if (rc != 0) {
454 flash_area_close(*fap);
455 return rc;
456 }
457
458 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
459 return 0;
460 }
461
462 flash_area_close(*fap);
463 }
464
465 /* If we got here, no magic was found */
466 return -1;
467}
468
Christopher Collins92ea77f2016-12-12 15:59:26 -0800469int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300470boot_read_swap_size(int image_index, uint32_t *swap_size)
Fabio Utzig46490722017-09-04 15:34:32 -0300471{
Fabio Utzig46490722017-09-04 15:34:32 -0300472 uint32_t off;
473 const struct flash_area *fap;
474 int rc;
475
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300476 rc = boot_find_status(image_index, &fap);
477 if (rc == 0) {
478 off = boot_swap_size_off(fap);
479 rc = flash_area_read(fap, off, swap_size, sizeof *swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -0300480 flash_area_close(fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300481 }
482
Fabio Utzig46490722017-09-04 15:34:32 -0300483 return rc;
484}
485
Bohdan Kovalchuk7460ae52020-07-21 17:03:57 +0300486#endif /* not MCUBOOT_SWAP_USING_STATUS */
487
Fabio Utzigba829042018-09-18 08:29:34 -0300488#ifdef MCUBOOT_ENC_IMAGES
489int
Fabio Utzig4741c452019-12-19 15:32:41 -0300490boot_read_enc_key(int image_index, uint8_t slot, struct boot_status *bs)
Fabio Utzigba829042018-09-18 08:29:34 -0300491{
Fabio Utzigba829042018-09-18 08:29:34 -0300492 uint32_t off;
493 const struct flash_area *fap;
Fabio Utzig4741c452019-12-19 15:32:41 -0300494#if MCUBOOT_SWAP_SAVE_ENCTLV
495 int i;
496#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300497 int rc;
498
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300499 rc = boot_find_status(image_index, &fap);
500 if (rc == 0) {
501 off = boot_enc_key_off(fap, slot);
Fabio Utzig4741c452019-12-19 15:32:41 -0300502#if MCUBOOT_SWAP_SAVE_ENCTLV
503 rc = flash_area_read(fap, off, bs->enctlv[slot], BOOT_ENC_TLV_ALIGN_SIZE);
504 if (rc == 0) {
505 for (i = 0; i < BOOT_ENC_TLV_ALIGN_SIZE; i++) {
506 if (bs->enctlv[slot][i] != 0xff) {
507 break;
508 }
509 }
510 /* Only try to decrypt non-erased TLV metadata */
511 if (i != BOOT_ENC_TLV_ALIGN_SIZE) {
512 rc = boot_enc_decrypt(bs->enctlv[slot], bs->enckey[slot]);
513 }
514 }
515#else
516 rc = flash_area_read(fap, off, bs->enckey[slot], BOOT_ENC_KEY_SIZE);
517#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300518 flash_area_close(fap);
Fabio Utzigba829042018-09-18 08:29:34 -0300519 }
520
Fabio Utzigba829042018-09-18 08:29:34 -0300521 return rc;
522}
523#endif
Fabio Utzig46490722017-09-04 15:34:32 -0300524
Bohdan Kovalchuk7460ae52020-07-21 17:03:57 +0300525#ifndef MCUBOOT_SWAP_USING_STATUS
Fabio Utzig46490722017-09-04 15:34:32 -0300526int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800527boot_write_magic(const struct flash_area *fap)
528{
529 uint32_t off;
530 int rc;
531
532 off = boot_magic_off(fap);
533
Ben McCrea4c0ee952019-08-28 09:13:24 -0700534 BOOT_LOG_DBG("writing magic; fa_id=%d off=0x%lx (0x%lx)",
Marti Bolivar99ec3832019-09-12 21:21:26 -0600535 fap->fa_id, (unsigned long)off,
536 (unsigned long)(fap->fa_off + off));
Fabio Utzig24a273d2017-04-20 08:21:31 -0300537 rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800538 if (rc != 0) {
539 return BOOT_EFLASH;
540 }
541
542 return 0;
543}
544
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300545/**
546 * Write trailer data; status bytes, swap_size, etc
547 *
548 * @returns 0 on success, != 0 on error.
549 */
Fabio Utzig2473ac02017-05-02 12:45:02 -0300550static int
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300551boot_write_trailer(const struct flash_area *fap, uint32_t off,
552 const uint8_t *inbuf, uint8_t inlen)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800553{
Fabio Utzig644b8d42017-04-20 07:56:05 -0300554 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700555 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300556 uint8_t erased_val;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700557 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800558
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200559 align = flash_area_align(fap);
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300560 if (inlen > BOOT_MAX_ALIGN || align > BOOT_MAX_ALIGN) {
561 return -1;
562 }
Fabio Utzig39000012018-07-30 12:40:20 -0300563 erased_val = flash_area_erased_val(fap);
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300564 if (align < inlen) {
565 align = inlen;
566 }
567 memcpy(buf, inbuf, inlen);
568 memset(&buf[inlen], erased_val, align - inlen);
David Brown9d725462017-01-23 15:50:58 -0700569
570 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800571 if (rc != 0) {
572 return BOOT_EFLASH;
573 }
574
575 return 0;
576}
577
Bohdan Kovalchuk7460ae52020-07-21 17:03:57 +0300578#endif /* MCUBOOT_SWAP_USING_STATUS */
579
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300580static int
581boot_write_trailer_flag(const struct flash_area *fap, uint32_t off,
582 uint8_t flag_val)
583{
584 const uint8_t buf[1] = { flag_val };
585 return boot_write_trailer(fap, off, buf, 1);
586}
587
Christopher Collins92ea77f2016-12-12 15:59:26 -0800588int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300589boot_write_copy_done(const struct flash_area *fap)
590{
Christopher Collinsa1c12042019-05-23 14:00:28 -0700591 uint32_t off;
592
593 off = boot_copy_done_off(fap);
Ben McCrea4c0ee952019-08-28 09:13:24 -0700594 BOOT_LOG_DBG("writing copy_done; fa_id=%d off=0x%lx (0x%lx)",
Marti Bolivar99ec3832019-09-12 21:21:26 -0600595 fap->fa_id, (unsigned long)off,
596 (unsigned long)(fap->fa_off + off));
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300597 return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300598}
599
600int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800601boot_write_image_ok(const struct flash_area *fap)
602{
Christopher Collinsa1c12042019-05-23 14:00:28 -0700603 uint32_t off;
604
605 off = boot_image_ok_off(fap);
Ben McCrea4c0ee952019-08-28 09:13:24 -0700606 BOOT_LOG_DBG("writing image_ok; fa_id=%d off=0x%lx (0x%lx)",
Marti Bolivar99ec3832019-09-12 21:21:26 -0600607 fap->fa_id, (unsigned long)off,
608 (unsigned long)(fap->fa_off + off));
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300609 return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700610}
611
612/**
613 * Writes the specified value to the `swap-type` field of an image trailer.
614 * This value is persisted so that the boot loader knows what swap operation to
615 * resume in case of an unexpected reset.
616 */
617int
David Vinczee2453472019-06-17 12:31:59 +0200618boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
619 uint8_t image_num)
Christopher Collinsa1c12042019-05-23 14:00:28 -0700620{
621 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200622 uint8_t swap_info;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700623
David Vinczee2453472019-06-17 12:31:59 +0200624 BOOT_SET_SWAP_INFO(swap_info, image_num, swap_type);
625 off = boot_swap_info_off(fap);
Ben McCrea4c0ee952019-08-28 09:13:24 -0700626 BOOT_LOG_DBG("writing swap_info; fa_id=%d off=0x%lx (0x%lx), swap_type=0x%x"
David Vinczee2453472019-06-17 12:31:59 +0200627 " image_num=0x%x",
Marti Bolivar99ec3832019-09-12 21:21:26 -0600628 fap->fa_id, (unsigned long)off,
629 (unsigned long)(fap->fa_off + off), swap_type, image_num);
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300630 return boot_write_trailer(fap, off, (const uint8_t *) &swap_info, 1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800631}
632
633int
Fabio Utzig46490722017-09-04 15:34:32 -0300634boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size)
635{
636 uint32_t off;
Fabio Utzig46490722017-09-04 15:34:32 -0300637
638 off = boot_swap_size_off(fap);
Ben McCrea4c0ee952019-08-28 09:13:24 -0700639 BOOT_LOG_DBG("writing swap_size; fa_id=%d off=0x%lx (0x%lx)",
Marti Bolivar99ec3832019-09-12 21:21:26 -0600640 fap->fa_id, (unsigned long)off,
641 (unsigned long)fap->fa_off + off);
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300642 return boot_write_trailer(fap, off, (const uint8_t *) &swap_size, 4);
Fabio Utzig46490722017-09-04 15:34:32 -0300643}
644
Fabio Utzigba829042018-09-18 08:29:34 -0300645#ifdef MCUBOOT_ENC_IMAGES
646int
Fabio Utzig4741c452019-12-19 15:32:41 -0300647boot_write_enc_key(const struct flash_area *fap, uint8_t slot,
648 const struct boot_status *bs)
Fabio Utzigba829042018-09-18 08:29:34 -0300649{
650 uint32_t off;
651 int rc;
652
653 off = boot_enc_key_off(fap, slot);
Fabio Utzig5e6ea222019-12-10 09:49:59 -0300654 BOOT_LOG_DBG("writing enc_key; fa_id=%d off=0x%lx (0x%lx)",
655 fap->fa_id, (unsigned long)off,
656 (unsigned long)fap->fa_off + off);
Fabio Utzig4741c452019-12-19 15:32:41 -0300657#if MCUBOOT_SWAP_SAVE_ENCTLV
658 rc = flash_area_write(fap, off, bs->enctlv[slot], BOOT_ENC_TLV_ALIGN_SIZE);
659#else
660 rc = flash_area_write(fap, off, bs->enckey[slot], BOOT_ENC_KEY_SIZE);
661#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300662 if (rc != 0) {
663 return BOOT_EFLASH;
664 }
665
666 return 0;
667}
668#endif
669
Bohdan Kovalchuk7460ae52020-07-21 17:03:57 +0300670#define BOOT_LOG_SWAP_STATE(area, state) \
671 BOOT_LOG_INF("%s: magic=%s, swap_type=0x%x, copy_done=0x%x, " \
672 "image_ok=0x%x", \
673 (area), \
674 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
675 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
676 "bad"), \
677 (state)->swap_type, \
678 (state)->copy_done, \
679 (state)->image_ok)
680
Fabio Utzig46490722017-09-04 15:34:32 -0300681int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300682boot_swap_type_multi(int image_index)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800683{
684 const struct boot_swap_table *table;
David Vincze2d736ad2019-02-18 11:50:22 +0100685 struct boot_swap_state primary_slot;
686 struct boot_swap_state secondary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800687 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200688 size_t i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800689
Fabio Utzigb0f04732019-07-31 09:49:19 -0300690 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
691 &primary_slot);
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300692 if (rc) {
693 return BOOT_SWAP_TYPE_PANIC;
694 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800695
Fabio Utzigb0f04732019-07-31 09:49:19 -0300696 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
David Vincze2d736ad2019-02-18 11:50:22 +0100697 &secondary_slot);
Bohdan Kovalchuk7460ae52020-07-21 17:03:57 +0300698
699 BOOT_LOG_SWAP_STATE("boot_swap_type_multi: Primary image", &primary_slot);
700 BOOT_LOG_SWAP_STATE("boot_swap_type_multi: Secondary image", &secondary_slot);
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300701 if (rc) {
702 return BOOT_SWAP_TYPE_PANIC;
703 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800704
705 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
706 table = boot_swap_tables + i;
707
Christopher Collinsa1c12042019-05-23 14:00:28 -0700708 if (boot_magic_compatible_check(table->magic_primary_slot,
709 primary_slot.magic) &&
710 boot_magic_compatible_check(table->magic_secondary_slot,
711 secondary_slot.magic) &&
David Vincze2d736ad2019-02-18 11:50:22 +0100712 (table->image_ok_primary_slot == BOOT_FLAG_ANY ||
713 table->image_ok_primary_slot == primary_slot.image_ok) &&
714 (table->image_ok_secondary_slot == BOOT_FLAG_ANY ||
715 table->image_ok_secondary_slot == secondary_slot.image_ok) &&
716 (table->copy_done_primary_slot == BOOT_FLAG_ANY ||
717 table->copy_done_primary_slot == primary_slot.copy_done)) {
Fabio Utzig34e393e2017-05-22 11:07:07 -0400718 BOOT_LOG_INF("Swap type: %s",
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300719 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
720 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
721 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
722 "BUG; can't happen");
Fabio Utzigf0dbd422019-08-09 10:22:05 -0300723 if (table->swap_type != BOOT_SWAP_TYPE_TEST &&
724 table->swap_type != BOOT_SWAP_TYPE_PERM &&
725 table->swap_type != BOOT_SWAP_TYPE_REVERT) {
726 return BOOT_SWAP_TYPE_PANIC;
727 }
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300728 return table->swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800729 }
730 }
731
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300732 BOOT_LOG_INF("Swap type: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800733 return BOOT_SWAP_TYPE_NONE;
734}
735
Fabio Utzig75e9a592019-08-27 09:10:36 -0300736/*
737 * This function is not used by the bootloader itself, but its required API
738 * by external tooling like mcumgr.
739 */
Fabio Utzigb0f04732019-07-31 09:49:19 -0300740int
741boot_swap_type(void)
742{
743 return boot_swap_type_multi(0);
744}
745
Christopher Collins92ea77f2016-12-12 15:59:26 -0800746/**
David Vincze2d736ad2019-02-18 11:50:22 +0100747 * Marks the image in the secondary slot as pending. On the next reboot,
748 * the system will perform a one-time boot of the the secondary slot image.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800749 *
Christopher Collins7835c1e2016-12-21 10:10:51 -0800750 * @param permanent Whether the image should be used permanently or
751 * only tested once:
752 * 0=run image once, then confirm or revert.
753 * 1=run image forever.
754 *
Christopher Collins92ea77f2016-12-12 15:59:26 -0800755 * @return 0 on success; nonzero on failure.
756 */
757int
Christopher Collins7835c1e2016-12-21 10:10:51 -0800758boot_set_pending(int permanent)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800759{
760 const struct flash_area *fap;
David Vincze2d736ad2019-02-18 11:50:22 +0100761 struct boot_swap_state state_secondary_slot;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700762 uint8_t swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800763 int rc;
764
Fabio Utzigb0f04732019-07-31 09:49:19 -0300765 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(0),
David Vincze2d736ad2019-02-18 11:50:22 +0100766 &state_secondary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800767 if (rc != 0) {
768 return rc;
769 }
770
David Vincze2d736ad2019-02-18 11:50:22 +0100771 switch (state_secondary_slot.magic) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800772 case BOOT_MAGIC_GOOD:
773 /* Swap already scheduled. */
774 return 0;
775
776 case BOOT_MAGIC_UNSET:
Fabio Utzigb0f04732019-07-31 09:49:19 -0300777 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(0), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800778 if (rc != 0) {
779 rc = BOOT_EFLASH;
780 } else {
781 rc = boot_write_magic(fap);
782 }
783
Christopher Collins7835c1e2016-12-21 10:10:51 -0800784 if (rc == 0 && permanent) {
785 rc = boot_write_image_ok(fap);
786 }
787
Christopher Collinsa1c12042019-05-23 14:00:28 -0700788 if (rc == 0) {
789 if (permanent) {
790 swap_type = BOOT_SWAP_TYPE_PERM;
791 } else {
792 swap_type = BOOT_SWAP_TYPE_TEST;
793 }
David Vinczee2453472019-06-17 12:31:59 +0200794 rc = boot_write_swap_info(fap, swap_type, 0);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700795 }
796
Christopher Collins92ea77f2016-12-12 15:59:26 -0800797 flash_area_close(fap);
798 return rc;
799
Christopher Collinsae01f152019-01-30 09:56:37 -0800800 case BOOT_MAGIC_BAD:
801 /* The image slot is corrupt. There is no way to recover, so erase the
802 * slot to allow future upgrades.
803 */
Fabio Utzigb0f04732019-07-31 09:49:19 -0300804 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(0), &fap);
Christopher Collinsae01f152019-01-30 09:56:37 -0800805 if (rc != 0) {
806 return BOOT_EFLASH;
807 }
808
809 flash_area_erase(fap, 0, fap->fa_size);
810 flash_area_close(fap);
811 return BOOT_EBADIMAGE;
812
Christopher Collins92ea77f2016-12-12 15:59:26 -0800813 default:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800814 assert(0);
Christopher Collinsae01f152019-01-30 09:56:37 -0800815 return BOOT_EBADIMAGE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800816 }
817}
818
819/**
David Vincze2d736ad2019-02-18 11:50:22 +0100820 * Marks the image in the primary slot as confirmed. The system will continue
821 * booting into the image in the primary slot until told to boot from a
822 * different slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800823 *
824 * @return 0 on success; nonzero on failure.
825 */
826int
827boot_set_confirmed(void)
828{
829 const struct flash_area *fap;
David Vincze2d736ad2019-02-18 11:50:22 +0100830 struct boot_swap_state state_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800831 int rc;
832
Fabio Utzigb0f04732019-07-31 09:49:19 -0300833 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(0),
David Vincze2d736ad2019-02-18 11:50:22 +0100834 &state_primary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800835 if (rc != 0) {
836 return rc;
837 }
838
David Vincze2d736ad2019-02-18 11:50:22 +0100839 switch (state_primary_slot.magic) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800840 case BOOT_MAGIC_GOOD:
841 /* Confirm needed; proceed. */
842 break;
843
844 case BOOT_MAGIC_UNSET:
845 /* Already confirmed. */
846 return 0;
847
848 case BOOT_MAGIC_BAD:
849 /* Unexpected state. */
850 return BOOT_EBADVECT;
851 }
852
Fabio Utzigb0f04732019-07-31 09:49:19 -0300853 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(0), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800854 if (rc) {
855 rc = BOOT_EFLASH;
856 goto done;
857 }
858
David Vincze2d736ad2019-02-18 11:50:22 +0100859 if (state_primary_slot.copy_done == BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300860 /* Swap never completed. This is unexpected. */
861 rc = BOOT_EBADVECT;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800862 goto done;
863 }
864
David Vincze2d736ad2019-02-18 11:50:22 +0100865 if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300866 /* Already confirmed. */
867 goto done;
868 }
869
870 rc = boot_write_image_ok(fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800871
872done:
873 flash_area_close(fap);
874 return rc;
875}