blob: 43a69553eb877ab138395fc978bd9680e12ccda7 [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
David Vinczee2453472019-06-17 12:31:59 +020020/*
21 * Modifications are Copyright (c) 2019 Arm Limited.
22 */
23
Christopher Collins92ea77f2016-12-12 15:59:26 -080024/**
25 * This file provides an interface to the boot loader. Functions defined in
26 * this file should only be called while the boot loader is running.
27 */
28
29#include <assert.h>
30#include <stddef.h>
David Brown52eee562017-07-05 11:25:09 -060031#include <stdbool.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080032#include <inttypes.h>
33#include <stdlib.h>
34#include <string.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080035#include <os/os_malloc.h>
36#include "bootutil/bootutil.h"
37#include "bootutil/image.h"
38#include "bootutil_priv.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050039#include "bootutil/bootutil_log.h"
40
Fabio Utzigba829042018-09-18 08:29:34 -030041#ifdef MCUBOOT_ENC_IMAGES
42#include "bootutil/enc_key.h"
43#endif
44
Fabio Utzigba1fbe62017-07-21 14:01:20 -030045#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030046
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010047MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
48
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040049static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080050
Fabio Utzigabec0732019-07-31 08:40:22 -030051#if (BOOT_IMAGE_NUMBER > 1)
52#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
53#else
54#define IMAGES_ITER(x)
55#endif
56
Fabio Utzig10ee6482019-08-01 12:04:52 -030057/*
58 * This macro allows some control on the allocation of local variables.
59 * When running natively on a target, we don't want to allocated huge
60 * variables on the stack, so make them global instead. For the simulator
61 * we want to run as many threads as there are tests, and it's safer
62 * to just make those variables stack allocated.
63 */
64#if !defined(__BOOTSIM__)
65#define TARGET_STATIC static
66#else
67#define TARGET_STATIC
68#endif
69
David Vincze2d736ad2019-02-18 11:50:22 +010070#if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT) && !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -030071/*
72 * FIXME: this might have to be updated for threaded sim
73 */
Fabio Utziga0e1cce2017-11-23 20:04:01 -020074static int boot_status_fails = 0;
75#define BOOT_STATUS_ASSERT(x) \
76 do { \
Johann Fischered8461b2018-02-15 16:50:31 +010077 if (!(x)) { \
Fabio Utziga0e1cce2017-11-23 20:04:01 -020078 boot_status_fails++; \
79 } \
80 } while (0)
81#else
Fabio Utzig57c40f72017-12-12 21:48:30 -020082#define BOOT_STATUS_ASSERT(x) ASSERT(x)
Fabio Utziga0e1cce2017-11-23 20:04:01 -020083#endif
84
Christopher Collins92ea77f2016-12-12 15:59:26 -080085struct boot_status_table {
David Vincze2d736ad2019-02-18 11:50:22 +010086 uint8_t bst_magic_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -080087 uint8_t bst_magic_scratch;
David Vincze2d736ad2019-02-18 11:50:22 +010088 uint8_t bst_copy_done_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -080089 uint8_t bst_status_source;
90};
91
92/**
93 * This set of tables maps swap state contents to boot status location.
94 * When searching for a match, these tables must be iterated in order.
95 */
96static const struct boot_status_table boot_status_tables[] = {
97 {
David Vincze2d736ad2019-02-18 11:50:22 +010098 /* | primary slot | scratch |
99 * ----------+--------------+--------------|
100 * magic | Good | Any |
101 * copy-done | Set | N/A |
102 * ----------+--------------+--------------'
103 * source: none |
104 * ----------------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -0800105 */
David Vincze2d736ad2019-02-18 11:50:22 +0100106 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
Christopher Collinsa1c12042019-05-23 14:00:28 -0700107 .bst_magic_scratch = BOOT_MAGIC_NOTGOOD,
David Vincze2d736ad2019-02-18 11:50:22 +0100108 .bst_copy_done_primary_slot = BOOT_FLAG_SET,
109 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800110 },
111
112 {
David Vincze2d736ad2019-02-18 11:50:22 +0100113 /* | primary slot | scratch |
114 * ----------+--------------+--------------|
115 * magic | Good | Any |
116 * copy-done | Unset | N/A |
117 * ----------+--------------+--------------'
118 * source: primary slot |
119 * ----------------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -0800120 */
David Vincze2d736ad2019-02-18 11:50:22 +0100121 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
Christopher Collinsa1c12042019-05-23 14:00:28 -0700122 .bst_magic_scratch = BOOT_MAGIC_NOTGOOD,
David Vincze2d736ad2019-02-18 11:50:22 +0100123 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
124 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800125 },
126
127 {
David Vincze2d736ad2019-02-18 11:50:22 +0100128 /* | primary slot | scratch |
129 * ----------+--------------+--------------|
130 * magic | Any | Good |
131 * copy-done | Any | N/A |
132 * ----------+--------------+--------------'
133 * source: scratch |
134 * ----------------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -0800135 */
David Vincze2d736ad2019-02-18 11:50:22 +0100136 .bst_magic_primary_slot = BOOT_MAGIC_ANY,
137 .bst_magic_scratch = BOOT_MAGIC_GOOD,
138 .bst_copy_done_primary_slot = BOOT_FLAG_ANY,
139 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800140 },
Christopher Collins92ea77f2016-12-12 15:59:26 -0800141 {
David Vincze2d736ad2019-02-18 11:50:22 +0100142 /* | primary slot | scratch |
143 * ----------+--------------+--------------|
144 * magic | Unset | Any |
145 * copy-done | Unset | N/A |
146 * ----------+--------------+--------------|
147 * source: varies |
148 * ----------------------------------------+--------------------------+
Christopher Collins92ea77f2016-12-12 15:59:26 -0800149 * This represents one of two cases: |
150 * o No swaps ever (no status to read, so no harm in checking). |
David Vincze2d736ad2019-02-18 11:50:22 +0100151 * o Mid-revert; status in primary slot. |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800152 * -------------------------------------------------------------------'
153 */
David Vincze2d736ad2019-02-18 11:50:22 +0100154 .bst_magic_primary_slot = BOOT_MAGIC_UNSET,
155 .bst_magic_scratch = BOOT_MAGIC_ANY,
156 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
157 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800158 },
159};
160
161#define BOOT_STATUS_TABLES_COUNT \
162 (sizeof boot_status_tables / sizeof boot_status_tables[0])
163
Marti Bolivarfd20c762017-02-07 16:52:50 -0500164#define BOOT_LOG_SWAP_STATE(area, state) \
Christopher Collinsa1c12042019-05-23 14:00:28 -0700165 BOOT_LOG_INF("%s: magic=%s, swap_type=0x%x, copy_done=0x%x, " \
166 "image_ok=0x%x", \
Marti Bolivarfd20c762017-02-07 16:52:50 -0500167 (area), \
168 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
169 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
170 "bad"), \
Christopher Collinsa1c12042019-05-23 14:00:28 -0700171 (state)->swap_type, \
Marti Bolivarfd20c762017-02-07 16:52:50 -0500172 (state)->copy_done, \
173 (state)->image_ok)
174
Christopher Collins92ea77f2016-12-12 15:59:26 -0800175/**
David Vincze2d736ad2019-02-18 11:50:22 +0100176 * Determines where in flash the most recent boot status is stored. The boot
Christopher Collins92ea77f2016-12-12 15:59:26 -0800177 * status is necessary for completing a swap that was interrupted by a boot
178 * loader reset.
179 *
David Vincze2d736ad2019-02-18 11:50:22 +0100180 * @return A BOOT_STATUS_SOURCE_[...] code indicating where status should
181 * be read from.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800182 */
183static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300184boot_status_source(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800185{
186 const struct boot_status_table *table;
187 struct boot_swap_state state_scratch;
David Vincze2d736ad2019-02-18 11:50:22 +0100188 struct boot_swap_state state_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800189 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200190 size_t i;
Marti Bolivarfd20c762017-02-07 16:52:50 -0500191 uint8_t source;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300192 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800193
Fabio Utzigb0f04732019-07-31 09:49:19 -0300194#if (BOOT_IMAGE_NUMBER == 1)
195 (void)state;
196#endif
197
Fabio Utzig10ee6482019-08-01 12:04:52 -0300198 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300199 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
200 &state_primary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800201 assert(rc == 0);
202
Fabio Utzig2473ac02017-05-02 12:45:02 -0300203 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800204 assert(rc == 0);
205
David Vincze2d736ad2019-02-18 11:50:22 +0100206 BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot);
Marti Bolivarfd20c762017-02-07 16:52:50 -0500207 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
208
Christopher Collins92ea77f2016-12-12 15:59:26 -0800209 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300210 table = &boot_status_tables[i];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800211
Christopher Collinsa1c12042019-05-23 14:00:28 -0700212 if (boot_magic_compatible_check(table->bst_magic_primary_slot,
213 state_primary_slot.magic) &&
214 boot_magic_compatible_check(table->bst_magic_scratch,
215 state_scratch.magic) &&
David Vincze2d736ad2019-02-18 11:50:22 +0100216 (table->bst_copy_done_primary_slot == BOOT_FLAG_ANY ||
217 table->bst_copy_done_primary_slot == state_primary_slot.copy_done))
218 {
Marti Bolivarfd20c762017-02-07 16:52:50 -0500219 source = table->bst_status_source;
David Vinczeba3bd602019-06-17 16:01:43 +0200220
221#if (BOOT_IMAGE_NUMBER > 1)
222 /* In case of multi-image boot it can happen that if boot status
223 * info is found on scratch area then it does not belong to the
224 * currently examined image.
225 */
226 if (source == BOOT_STATUS_SOURCE_SCRATCH &&
Fabio Utzig10ee6482019-08-01 12:04:52 -0300227 state_scratch.image_num != BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +0200228 source = BOOT_STATUS_SOURCE_NONE;
229 }
230#endif
231
Marti Bolivarfd20c762017-02-07 16:52:50 -0500232 BOOT_LOG_INF("Boot source: %s",
233 source == BOOT_STATUS_SOURCE_NONE ? "none" :
234 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
David Vincze2d736ad2019-02-18 11:50:22 +0100235 source == BOOT_STATUS_SOURCE_PRIMARY_SLOT ?
236 "primary slot" : "BUG; can't happen");
Marti Bolivarfd20c762017-02-07 16:52:50 -0500237 return source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800238 }
239 }
240
Marti Bolivarfd20c762017-02-07 16:52:50 -0500241 BOOT_LOG_INF("Boot source: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800242 return BOOT_STATUS_SOURCE_NONE;
243}
244
David Brownf5b33d82017-09-01 10:58:27 -0600245/*
246 * Compute the total size of the given image. Includes the size of
247 * the TLVs.
248 */
Fabio Utzig13d9e352017-10-05 20:32:31 -0300249#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600250static int
Fabio Utzigd638b172019-08-09 10:38:05 -0300251boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
David Brownf5b33d82017-09-01 10:58:27 -0600252{
253 const struct flash_area *fap;
254 struct image_tlv_info info;
Fabio Utzigd638b172019-08-09 10:38:05 -0300255 uint32_t tlv_off;
David Brownf5b33d82017-09-01 10:58:27 -0600256 int area_id;
257 int rc;
258
Fabio Utzig10ee6482019-08-01 12:04:52 -0300259#if (BOOT_IMAGE_NUMBER == 1)
260 (void)state;
261#endif
262
263 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
David Brownf5b33d82017-09-01 10:58:27 -0600264 rc = flash_area_open(area_id, &fap);
265 if (rc != 0) {
266 rc = BOOT_EFLASH;
267 goto done;
268 }
269
Fabio Utzigd638b172019-08-09 10:38:05 -0300270 tlv_off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
271 rc = flash_area_read(fap, tlv_off, &info, sizeof(info));
David Brownf5b33d82017-09-01 10:58:27 -0600272 if (rc != 0) {
273 rc = BOOT_EFLASH;
274 goto done;
275 }
276 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
277 rc = BOOT_EBADIMAGE;
278 goto done;
279 }
Fabio Utzigd638b172019-08-09 10:38:05 -0300280 *size = tlv_off + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600281 rc = 0;
282
283done:
284 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300285 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600286}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300287#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600288
Fabio Utzigc08ed212017-06-20 19:28:36 -0300289static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300290boot_read_image_header(struct boot_loader_state *state, int slot,
291 struct image_header *out_hdr)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800292{
293 const struct flash_area *fap;
294 int area_id;
295 int rc;
296
Fabio Utzig10ee6482019-08-01 12:04:52 -0300297#if (BOOT_IMAGE_NUMBER == 1)
298 (void)state;
299#endif
300
301 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800302 rc = flash_area_open(area_id, &fap);
303 if (rc != 0) {
304 rc = BOOT_EFLASH;
305 goto done;
306 }
307
308 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
309 if (rc != 0) {
310 rc = BOOT_EFLASH;
311 goto done;
312 }
313
314 rc = 0;
315
316done:
317 flash_area_close(fap);
318 return rc;
319}
320
321static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300322boot_read_image_headers(struct boot_loader_state *state, bool require_all)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800323{
324 int rc;
325 int i;
326
327 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300328 rc = boot_read_image_header(state, i, boot_img_hdr(state, i));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800329 if (rc != 0) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200330 /* If `require_all` is set, fail on any single fail, otherwise
331 * if at least the first slot's header was read successfully,
332 * then the boot loader can attempt a boot.
333 *
334 * Failure to read any headers is a fatal error.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800335 */
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200336 if (i > 0 && !require_all) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800337 return 0;
338 } else {
339 return rc;
340 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800341 }
342 }
343
344 return 0;
345}
346
347static uint8_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300348boot_write_sz(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800349{
350 uint8_t elem_sz;
351 uint8_t align;
352
353 /* Figure out what size to write update status update as. The size depends
354 * on what the minimum write size is for scratch area, active image slot.
355 * We need to use the bigger of those 2 values.
356 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300357 elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
358 align = flash_area_align(BOOT_SCRATCH_AREA(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800359 if (align > elem_sz) {
360 elem_sz = align;
361 }
362
363 return elem_sz;
364}
365
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200366/*
367 * Slots are compatible when all sectors that store upto to size of the image
368 * round up to sector size, in both slot's are able to fit in the scratch
369 * area, and have sizes that are a multiple of each other (powers of two
370 * presumably!).
371 */
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800372static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300373boot_slots_compatible(struct boot_loader_state *state)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800374{
David Vincze2d736ad2019-02-18 11:50:22 +0100375 size_t num_sectors_primary;
376 size_t num_sectors_secondary;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200377 size_t sz0, sz1;
David Vincze2d736ad2019-02-18 11:50:22 +0100378 size_t primary_slot_sz, secondary_slot_sz;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200379 size_t scratch_sz;
380 size_t i, j;
381 int8_t smaller;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800382
Fabio Utzig10ee6482019-08-01 12:04:52 -0300383 num_sectors_primary = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
384 num_sectors_secondary = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT);
David Vincze2d736ad2019-02-18 11:50:22 +0100385 if ((num_sectors_primary > BOOT_MAX_IMG_SECTORS) ||
386 (num_sectors_secondary > BOOT_MAX_IMG_SECTORS)) {
Fabio Utziga1fae672018-03-30 10:52:38 -0300387 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800388 return 0;
389 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300390
Fabio Utzig10ee6482019-08-01 12:04:52 -0300391 scratch_sz = boot_scratch_area_size(state);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200392
393 /*
394 * The following loop scans all sectors in a linear fashion, assuring that
395 * for each possible sector in each slot, it is able to fit in the other
396 * slot's sector or sectors. Slot's should be compatible as long as any
397 * number of a slot's sectors are able to fit into another, which only
398 * excludes cases where sector sizes are not a multiple of each other.
399 */
David Vincze2d736ad2019-02-18 11:50:22 +0100400 i = sz0 = primary_slot_sz = 0;
401 j = sz1 = secondary_slot_sz = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200402 smaller = 0;
David Vincze2d736ad2019-02-18 11:50:22 +0100403 while (i < num_sectors_primary || j < num_sectors_secondary) {
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200404 if (sz0 == sz1) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300405 sz0 += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
406 sz1 += boot_img_sector_size(state, BOOT_SECONDARY_SLOT, j);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200407 i++;
408 j++;
409 } else if (sz0 < sz1) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300410 sz0 += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
David Vincze2d736ad2019-02-18 11:50:22 +0100411 /* Guarantee that multiple sectors of the secondary slot
412 * fit into the primary slot.
413 */
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200414 if (smaller == 2) {
415 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors");
416 return 0;
417 }
418 smaller = 1;
419 i++;
420 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300421 sz1 += boot_img_sector_size(state, BOOT_SECONDARY_SLOT, j);
David Vincze2d736ad2019-02-18 11:50:22 +0100422 /* Guarantee that multiple sectors of the primary slot
423 * fit into the secondary slot.
424 */
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200425 if (smaller == 1) {
426 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors");
427 return 0;
428 }
429 smaller = 2;
430 j++;
431 }
432 if (sz0 == sz1) {
David Vincze2d736ad2019-02-18 11:50:22 +0100433 primary_slot_sz += sz0;
434 secondary_slot_sz += sz1;
435 /* Scratch has to fit each swap operation to the size of the larger
436 * sector among the primary slot and the secondary slot.
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200437 */
438 if (sz0 > scratch_sz || sz1 > scratch_sz) {
439 BOOT_LOG_WRN("Cannot upgrade: not all sectors fit inside scratch");
440 return 0;
441 }
442 smaller = sz0 = sz1 = 0;
443 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300444 }
445
David Vincze2d736ad2019-02-18 11:50:22 +0100446 if ((i != num_sectors_primary) ||
447 (j != num_sectors_secondary) ||
448 (primary_slot_sz != secondary_slot_sz)) {
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200449 BOOT_LOG_WRN("Cannot upgrade: slots are not compatible");
450 return 0;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800451 }
452
453 return 1;
454}
455
Fabio Utzig10ee6482019-08-01 12:04:52 -0300456#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
457static int
458boot_initialize_area(struct boot_loader_state *state, int flash_area)
459{
460 int num_sectors = BOOT_MAX_IMG_SECTORS;
461 int rc;
462
463 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
464 rc = flash_area_to_sectors(flash_area, &num_sectors,
465 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors);
466 BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors = (size_t)num_sectors;
467
468 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
469 rc = flash_area_to_sectors(flash_area, &num_sectors,
470 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors);
471 BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors = (size_t)num_sectors;
472
473 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
474 rc = flash_area_to_sectors(flash_area, &num_sectors,
475 state->scratch.sectors);
476 state->scratch.num_sectors = (size_t)num_sectors;
477 } else {
478 return BOOT_EFLASH;
479 }
480
481 return rc;
482}
483#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
484static int
485boot_initialize_area(struct boot_loader_state *state, int flash_area)
486{
487 uint32_t num_sectors;
488 struct flash_sector *out_sectors;
489 size_t *out_num_sectors;
490 int rc;
491
492 num_sectors = BOOT_MAX_IMG_SECTORS;
493
494 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
495 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
496 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
497 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
498 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
499 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
500 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
501 out_sectors = state->scratch.sectors;
502 out_num_sectors = &state->scratch.num_sectors;
503 } else {
504 return BOOT_EFLASH;
505 }
506
507 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
508 if (rc != 0) {
509 return rc;
510 }
511 *out_num_sectors = num_sectors;
512 return 0;
513}
514#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
515
Christopher Collins92ea77f2016-12-12 15:59:26 -0800516/**
517 * Determines the sector layout of both image slots and the scratch area.
518 * This information is necessary for calculating the number of bytes to erase
519 * and copy during an image swap. The information collected during this
Fabio Utzig10ee6482019-08-01 12:04:52 -0300520 * function is used to populate the state.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800521 */
522static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300523boot_read_sectors(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800524{
Fabio Utzigb0f04732019-07-31 09:49:19 -0300525 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800526 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800527
Fabio Utzig10ee6482019-08-01 12:04:52 -0300528 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300529
530 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800531 if (rc != 0) {
532 return BOOT_EFLASH;
533 }
534
Fabio Utzig10ee6482019-08-01 12:04:52 -0300535 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800536 if (rc != 0) {
537 return BOOT_EFLASH;
538 }
539
Fabio Utzig10ee6482019-08-01 12:04:52 -0300540 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200541 if (rc != 0) {
542 return BOOT_EFLASH;
543 }
544
Fabio Utzig10ee6482019-08-01 12:04:52 -0300545 BOOT_WRITE_SZ(state) = boot_write_sz(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800546
547 return 0;
548}
549
550static uint32_t
551boot_status_internal_off(int idx, int state, int elem_sz)
552{
553 int idx_sz;
554
555 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
556
Fabio Utzig39000012018-07-30 12:40:20 -0300557 return (idx - BOOT_STATUS_IDX_0) * idx_sz +
558 (state - BOOT_STATUS_STATE_0) * elem_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800559}
560
561/**
562 * Reads the status of a partially-completed swap, if any. This is necessary
563 * to recover in case the boot lodaer was reset in the middle of a swap
564 * operation.
565 */
566static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300567boot_read_status_bytes(const struct flash_area *fap,
568 struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800569{
570 uint32_t off;
571 uint8_t status;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300572 int max_entries;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800573 int found;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200574 int found_idx;
575 int invalid;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800576 int rc;
577 int i;
578
579 off = boot_status_off(fap);
Fabio Utzig10ee6482019-08-01 12:04:52 -0300580 max_entries = boot_status_entries(BOOT_CURR_IMG(state), fap);
Fabio Utzig9d160092019-08-09 07:46:34 -0300581 if (max_entries < 0) {
582 return BOOT_EBADARGS;
583 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300584
Christopher Collins92ea77f2016-12-12 15:59:26 -0800585 found = 0;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200586 found_idx = 0;
587 invalid = 0;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300588 for (i = 0; i < max_entries; i++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300589 rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(state),
Fabio Utzig178be542018-09-19 08:12:56 -0300590 &status, 1);
591 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800592 return BOOT_EFLASH;
593 }
594
Fabio Utzig178be542018-09-19 08:12:56 -0300595 if (rc == 1) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200596 if (found && !found_idx) {
597 found_idx = i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800598 }
599 } else if (!found) {
600 found = 1;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200601 } else if (found_idx) {
602 invalid = 1;
603 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800604 }
605 }
606
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200607 if (invalid) {
608 /* This means there was an error writing status on the last
609 * swap. Tell user and move on to validation!
610 */
611 BOOT_LOG_ERR("Detected inconsistent status!");
612
David Vincze2d736ad2019-02-18 11:50:22 +0100613#if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
614 /* With validation of the primary slot disabled, there is no way
615 * to be sure the swapped primary slot is OK, so abort!
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200616 */
617 assert(0);
618#endif
619 }
620
Christopher Collins92ea77f2016-12-12 15:59:26 -0800621 if (found) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200622 if (!found_idx) {
623 found_idx = i;
624 }
625 found_idx--;
Fabio Utzig39000012018-07-30 12:40:20 -0300626 bs->idx = (found_idx / BOOT_STATUS_STATE_COUNT) + 1;
627 bs->state = (found_idx % BOOT_STATUS_STATE_COUNT) + 1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800628 }
629
630 return 0;
631}
632
633/**
634 * Reads the boot status from the flash. The boot status contains
635 * the current state of an interrupted image copy operation. If the boot
636 * status is not present, or it indicates that previous copy finished,
637 * there is no operation in progress.
638 */
639static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300640boot_read_status(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800641{
642 const struct flash_area *fap;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700643 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200644 uint8_t swap_info;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800645 int status_loc;
646 int area_id;
647 int rc;
648
649 memset(bs, 0, sizeof *bs);
Fabio Utzig39000012018-07-30 12:40:20 -0300650 bs->idx = BOOT_STATUS_IDX_0;
651 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700652 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800653
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700654#ifdef MCUBOOT_OVERWRITE_ONLY
655 /* Overwrite-only doesn't make use of the swap status area. */
656 return 0;
657#endif
658
Fabio Utzig10ee6482019-08-01 12:04:52 -0300659 status_loc = boot_status_source(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800660 switch (status_loc) {
661 case BOOT_STATUS_SOURCE_NONE:
662 return 0;
663
664 case BOOT_STATUS_SOURCE_SCRATCH:
665 area_id = FLASH_AREA_IMAGE_SCRATCH;
666 break;
667
David Vincze2d736ad2019-02-18 11:50:22 +0100668 case BOOT_STATUS_SOURCE_PRIMARY_SLOT:
Fabio Utzig10ee6482019-08-01 12:04:52 -0300669 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800670 break;
671
672 default:
673 assert(0);
674 return BOOT_EBADARGS;
675 }
676
677 rc = flash_area_open(area_id, &fap);
678 if (rc != 0) {
679 return BOOT_EFLASH;
680 }
681
Fabio Utzig10ee6482019-08-01 12:04:52 -0300682 rc = boot_read_status_bytes(fap, state, bs);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700683 if (rc == 0) {
David Vinczee2453472019-06-17 12:31:59 +0200684 off = boot_swap_info_off(fap);
685 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700686 if (rc == 1) {
David Vinczee2453472019-06-17 12:31:59 +0200687 BOOT_SET_SWAP_INFO(swap_info, 0, BOOT_SWAP_TYPE_NONE);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700688 rc = 0;
689 }
David Vinczee2453472019-06-17 12:31:59 +0200690
691 /* Extract the swap type info */
692 bs->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700693 }
Fabio Utzig46490722017-09-04 15:34:32 -0300694
695 flash_area_close(fap);
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700696
Fabio Utzig46490722017-09-04 15:34:32 -0300697 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800698}
699
700/**
701 * Writes the supplied boot status to the flash file system. The boot status
702 * contains the current state of an in-progress image copy operation.
703 *
704 * @param bs The boot status to write.
705 *
706 * @return 0 on success; nonzero on failure.
707 */
708int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300709boot_write_status(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800710{
711 const struct flash_area *fap;
712 uint32_t off;
713 int area_id;
714 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300715 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700716 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300717 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800718
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300719 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100720 * the trailer. Since in the last step the primary slot is erased, the
721 * first two status writes go to the scratch which will be copied to
722 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300723 */
724
Fabio Utzig2473ac02017-05-02 12:45:02 -0300725 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800726 /* Write to scratch. */
727 area_id = FLASH_AREA_IMAGE_SCRATCH;
728 } else {
David Vincze2d736ad2019-02-18 11:50:22 +0100729 /* Write to the primary slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300730 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800731 }
732
733 rc = flash_area_open(area_id, &fap);
734 if (rc != 0) {
735 rc = BOOT_EFLASH;
736 goto done;
737 }
738
739 off = boot_status_off(fap) +
Fabio Utzigd638b172019-08-09 10:38:05 -0300740 boot_status_internal_off(bs->idx, bs->state, BOOT_WRITE_SZ(state));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200741 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300742 erased_val = flash_area_erased_val(fap);
743 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700744 buf[0] = bs->state;
745
746 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800747 if (rc != 0) {
748 rc = BOOT_EFLASH;
749 goto done;
750 }
751
752 rc = 0;
753
754done:
755 flash_area_close(fap);
756 return rc;
757}
758
759/*
760 * Validate image hash/signature in a slot.
761 */
762static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300763boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
764 const struct flash_area *fap, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800765{
Fabio Utzig10ee6482019-08-01 12:04:52 -0300766 TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigb0f04732019-07-31 09:49:19 -0300767 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300768 int rc;
769
Fabio Utzig10ee6482019-08-01 12:04:52 -0300770#if (BOOT_IMAGE_NUMBER == 1)
771 (void)state;
772#endif
773
Fabio Utzigba829042018-09-18 08:29:34 -0300774 (void)bs;
775 (void)rc;
Fabio Utzigbc077932019-08-26 11:16:34 -0300776
777 image_index = BOOT_CURR_IMG(state);
778
779#ifdef MCUBOOT_ENC_IMAGES
780 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300781 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -0300782 if (rc < 0) {
783 return BOOT_EBADIMAGE;
784 }
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300785 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs->enckey[1])) {
Fabio Utzigba829042018-09-18 08:29:34 -0300786 return BOOT_EBADIMAGE;
787 }
788 }
Fabio Utzigbc077932019-08-26 11:16:34 -0300789#endif
790
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300791 if (bootutil_img_validate(BOOT_CURR_ENC(state), image_index, hdr, fap, tmpbuf,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300792 BOOT_TMPBUF_SZ, NULL, 0, NULL)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800793 return BOOT_EBADIMAGE;
794 }
Fabio Utzig10ee6482019-08-01 12:04:52 -0300795
Christopher Collins92ea77f2016-12-12 15:59:26 -0800796 return 0;
797}
798
799static int
800split_image_check(struct image_header *app_hdr,
801 const struct flash_area *app_fap,
802 struct image_header *loader_hdr,
803 const struct flash_area *loader_fap)
804{
805 static void *tmpbuf;
806 uint8_t loader_hash[32];
807
808 if (!tmpbuf) {
809 tmpbuf = malloc(BOOT_TMPBUF_SZ);
810 if (!tmpbuf) {
811 return BOOT_ENOMEM;
812 }
813 }
814
Fabio Utzig10ee6482019-08-01 12:04:52 -0300815 if (bootutil_img_validate(NULL, 0, loader_hdr, loader_fap, tmpbuf,
816 BOOT_TMPBUF_SZ, NULL, 0, loader_hash)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800817 return BOOT_EBADIMAGE;
818 }
819
Fabio Utzig10ee6482019-08-01 12:04:52 -0300820 if (bootutil_img_validate(NULL, 0, app_hdr, app_fap, tmpbuf,
821 BOOT_TMPBUF_SZ, loader_hash, 32, NULL)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800822 return BOOT_EBADIMAGE;
823 }
824
825 return 0;
826}
827
Fabio Utzig338a19f2018-12-03 08:37:08 -0200828/*
829 * Check that a memory area consists of a given value.
830 */
831static inline bool
832boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300833{
834 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200835 uint8_t *p = (uint8_t *)data;
836 for (i = 0; i < len; i++) {
837 if (val != p[i]) {
838 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300839 }
840 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200841 return true;
842}
843
844static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300845boot_check_header_erased(struct boot_loader_state *state, int slot)
Fabio Utzig338a19f2018-12-03 08:37:08 -0200846{
847 const struct flash_area *fap;
848 struct image_header *hdr;
849 uint8_t erased_val;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300850 int area_id;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200851 int rc;
852
Fabio Utzig10ee6482019-08-01 12:04:52 -0300853 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300854 rc = flash_area_open(area_id, &fap);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200855 if (rc != 0) {
856 return -1;
857 }
858
859 erased_val = flash_area_erased_val(fap);
860 flash_area_close(fap);
861
Fabio Utzig10ee6482019-08-01 12:04:52 -0300862 hdr = boot_img_hdr(state, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200863 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
864 return -1;
865 }
866
867 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300868}
869
Christopher Collins92ea77f2016-12-12 15:59:26 -0800870static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300871boot_validate_slot(struct boot_loader_state *state, int slot,
872 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800873{
874 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400875 struct image_header *hdr;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300876 int area_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800877 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300878
Fabio Utzig10ee6482019-08-01 12:04:52 -0300879 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300880 rc = flash_area_open(area_id, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800881 if (rc != 0) {
882 return BOOT_EFLASH;
883 }
884
Fabio Utzig10ee6482019-08-01 12:04:52 -0300885 hdr = boot_img_hdr(state, slot);
886 if (boot_check_header_erased(state, slot) == 0 ||
887 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100888 /* No bootable image in slot; continue booting from the primary slot. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200889 rc = -1;
890 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300891 }
892
Fabio Utzig10ee6482019-08-01 12:04:52 -0300893 if (hdr->ih_magic != IMAGE_MAGIC || boot_image_check(state, hdr, fap, bs)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100894 if (slot != BOOT_PRIMARY_SLOT) {
David Brownb38e0442017-02-24 13:57:12 -0700895 flash_area_erase(fap, 0, fap->fa_size);
David Vincze2d736ad2019-02-18 11:50:22 +0100896 /* Image in the secondary slot is invalid. Erase the image and
897 * continue booting from the primary slot.
David Brownb38e0442017-02-24 13:57:12 -0700898 */
899 }
David Vincze2d736ad2019-02-18 11:50:22 +0100900 BOOT_LOG_ERR("Image in the %s slot is not valid!",
901 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
Fabio Utzig338a19f2018-12-03 08:37:08 -0200902 rc = -1;
903 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800904 }
905
David Vincze2d736ad2019-02-18 11:50:22 +0100906 /* Image in the secondary slot is valid. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200907 rc = 0;
908
909out:
910 flash_area_close(fap);
911 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800912}
913
914/**
915 * Determines which swap operation to perform, if any. If it is determined
David Vincze2d736ad2019-02-18 11:50:22 +0100916 * that a swap operation is required, the image in the secondary slot is checked
917 * for validity. If the image in the secondary slot is invalid, it is erased,
918 * and a swap type of "none" is indicated.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800919 *
920 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
921 */
922static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300923boot_validated_swap_type(struct boot_loader_state *state,
924 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800925{
926 int swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800927
Fabio Utzigb0f04732019-07-31 09:49:19 -0300928#if (BOOT_IMAGE_NUMBER == 1)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800929 swap_type = boot_swap_type();
Fabio Utzigb0f04732019-07-31 09:49:19 -0300930#else
931 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
932#endif
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300933 switch (swap_type) {
934 case BOOT_SWAP_TYPE_TEST:
935 case BOOT_SWAP_TYPE_PERM:
936 case BOOT_SWAP_TYPE_REVERT:
David Vincze2d736ad2019-02-18 11:50:22 +0100937 /* Boot loader wants to switch to the secondary slot.
938 * Ensure image is valid.
939 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300940 if (boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) != 0) {
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300941 swap_type = BOOT_SWAP_TYPE_FAIL;
942 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800943 }
944
945 return swap_type;
946}
947
948/**
949 * Calculates the number of sectors the scratch area can contain. A "last"
950 * source sector is specified because images are copied backwards in flash
951 * (final index to index number 0).
952 *
953 * @param last_sector_idx The index of the last source sector
954 * (inclusive).
955 * @param out_first_sector_idx The index of the first source sector
956 * (inclusive) gets written here.
957 *
958 * @return The number of bytes comprised by the
959 * [first-sector, last-sector] range.
960 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300961#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800962static uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300963boot_copy_sz(struct boot_loader_state *state, int last_sector_idx,
964 int *out_first_sector_idx)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800965{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400966 size_t scratch_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800967 uint32_t new_sz;
968 uint32_t sz;
969 int i;
970
971 sz = 0;
972
Fabio Utzig10ee6482019-08-01 12:04:52 -0300973 scratch_sz = boot_scratch_area_size(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800974 for (i = last_sector_idx; i >= 0; i--) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300975 new_sz = sz + boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200976 /*
David Vincze2d736ad2019-02-18 11:50:22 +0100977 * The secondary slot is not being checked here, because
978 * `boot_slots_compatible` already provides assurance that the copy size
979 * will be compatible with the primary slot and scratch.
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200980 */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400981 if (new_sz > scratch_sz) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800982 break;
983 }
984 sz = new_sz;
985 }
986
987 /* i currently refers to a sector that doesn't fit or it is -1 because all
988 * sectors have been processed. In both cases, exclude sector i.
989 */
990 *out_first_sector_idx = i + 1;
991 return sz;
992}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300993#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800994
995/**
996 * Erases a region of flash.
997 *
Fabio Utzigba829042018-09-18 08:29:34 -0300998 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800999 * @param off The offset within the flash area to start the
1000 * erase.
1001 * @param sz The number of bytes to erase.
1002 *
1003 * @return 0 on success; nonzero on failure.
1004 */
Fabio Utzigba829042018-09-18 08:29:34 -03001005static inline int
1006boot_erase_sector(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001007{
Fabio Utzigba829042018-09-18 08:29:34 -03001008 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001009}
1010
1011/**
1012 * Copies the contents of one flash region to another. You must erase the
1013 * destination region prior to calling this function.
1014 *
1015 * @param flash_area_id_src The ID of the source flash area.
1016 * @param flash_area_id_dst The ID of the destination flash area.
1017 * @param off_src The offset within the source flash area to
1018 * copy from.
1019 * @param off_dst The offset within the destination flash area to
1020 * copy to.
1021 * @param sz The number of bytes to copy.
1022 *
1023 * @return 0 on success; nonzero on failure.
1024 */
1025static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001026boot_copy_sector(struct boot_loader_state *state,
1027 const struct flash_area *fap_src,
Fabio Utzigba829042018-09-18 08:29:34 -03001028 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -08001029 uint32_t off_src, uint32_t off_dst, uint32_t sz)
1030{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001031 uint32_t bytes_copied;
1032 int chunk_sz;
1033 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -03001034#ifdef MCUBOOT_ENC_IMAGES
1035 uint32_t off;
1036 size_t blk_off;
1037 struct image_header *hdr;
1038 uint16_t idx;
1039 uint32_t blk_sz;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001040 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -03001041#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001042
Fabio Utzig10ee6482019-08-01 12:04:52 -03001043 TARGET_STATIC uint8_t buf[1024];
1044
1045#if !defined(MCUBOOT_ENC_IMAGES)
1046 (void)state;
1047#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001048
Christopher Collins92ea77f2016-12-12 15:59:26 -08001049 bytes_copied = 0;
1050 while (bytes_copied < sz) {
1051 if (sz - bytes_copied > sizeof buf) {
1052 chunk_sz = sizeof buf;
1053 } else {
1054 chunk_sz = sz - bytes_copied;
1055 }
1056
1057 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
1058 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001059 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001060 }
1061
Fabio Utzigba829042018-09-18 08:29:34 -03001062#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001063 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001064 if (fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) ||
1065 fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001066 /* assume the secondary slot as src, needs decryption */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001067 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001068 off = off_src;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001069 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001070 /* might need encryption (metadata from the primary slot) */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001071 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001072 off = off_dst;
1073 }
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001074 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001075 blk_sz = chunk_sz;
1076 idx = 0;
1077 if (off + bytes_copied < hdr->ih_hdr_size) {
1078 /* do not decrypt header */
1079 blk_off = 0;
1080 blk_sz = chunk_sz - hdr->ih_hdr_size;
1081 idx = hdr->ih_hdr_size;
1082 } else {
1083 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
1084 }
Fabio Utzigc9621352019-08-08 12:15:51 -03001085 if (off + bytes_copied + chunk_sz > BOOT_TLV_OFF(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001086 /* do not decrypt TLVs */
Fabio Utzigc9621352019-08-08 12:15:51 -03001087 if (off + bytes_copied >= BOOT_TLV_OFF(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001088 blk_sz = 0;
1089 } else {
Fabio Utzigc9621352019-08-08 12:15:51 -03001090 blk_sz = BOOT_TLV_OFF(hdr) - (off + bytes_copied);
Fabio Utzigba829042018-09-18 08:29:34 -03001091 }
1092 }
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001093 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
Fabio Utzigb0f04732019-07-31 09:49:19 -03001094 (off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
1095 blk_off, &buf[idx]);
Fabio Utzigba829042018-09-18 08:29:34 -03001096 }
1097 }
1098#endif
1099
Christopher Collins92ea77f2016-12-12 15:59:26 -08001100 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
1101 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001102 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001103 }
1104
1105 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -03001106
1107 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -08001108 }
1109
Fabio Utzigba829042018-09-18 08:29:34 -03001110 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001111}
1112
David Brown6b1b3b92017-09-19 08:59:10 -06001113#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -03001114static inline int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001115boot_status_init(const struct boot_loader_state *state,
1116 const struct flash_area *fap,
1117 const struct boot_status *bs)
Fabio Utzig2473ac02017-05-02 12:45:02 -03001118{
Fabio Utzig2473ac02017-05-02 12:45:02 -03001119 struct boot_swap_state swap_state;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001120 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001121 int rc;
1122
Fabio Utzig10ee6482019-08-01 12:04:52 -03001123#if (BOOT_IMAGE_NUMBER == 1)
1124 (void)state;
1125#endif
1126
1127 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001128
Christopher Collins2c88e692019-05-22 15:10:14 -07001129 BOOT_LOG_DBG("initializing status; fa_id=%d", fap->fa_id);
1130
Fabio Utzigb0f04732019-07-31 09:49:19 -03001131 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
1132 &swap_state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001133 assert(rc == 0);
1134
Christopher Collinsa1c12042019-05-23 14:00:28 -07001135 if (bs->swap_type != BOOT_SWAP_TYPE_NONE) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03001136 rc = boot_write_swap_info(fap, bs->swap_type, image_index);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001137 assert(rc == 0);
1138 }
1139
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001140 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001141 rc = boot_write_image_ok(fap);
1142 assert(rc == 0);
1143 }
1144
Fabio Utzig46490722017-09-04 15:34:32 -03001145 rc = boot_write_swap_size(fap, bs->swap_size);
1146 assert(rc == 0);
1147
Fabio Utzigba829042018-09-18 08:29:34 -03001148#ifdef MCUBOOT_ENC_IMAGES
1149 rc = boot_write_enc_key(fap, 0, bs->enckey[0]);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001150 assert(rc == 0);
1151
Fabio Utzigba829042018-09-18 08:29:34 -03001152 rc = boot_write_enc_key(fap, 1, bs->enckey[1]);
1153 assert(rc == 0);
1154#endif
1155
1156 rc = boot_write_magic(fap);
1157 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001158
1159 return 0;
1160}
Christopher Collinsa1c12042019-05-23 14:00:28 -07001161
David Brown6b1b3b92017-09-19 08:59:10 -06001162#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001163
Fabio Utzig358c9352017-07-25 22:10:45 -03001164#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -03001165static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001166boot_erase_trailer_sectors(const struct boot_loader_state *state,
1167 const struct flash_area *fap)
Fabio Utzig2473ac02017-05-02 12:45:02 -03001168{
1169 uint8_t slot;
Fabio Utziged0ca432019-01-23 14:50:11 -02001170 uint32_t sector;
1171 uint32_t trailer_sz;
1172 uint32_t total_sz;
1173 uint32_t off;
1174 uint32_t sz;
David Vinczeb75c12a2019-03-22 14:58:33 +01001175 int fa_id_primary;
1176 int fa_id_secondary;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001177 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001178 int rc;
1179
Christopher Collins2c88e692019-05-22 15:10:14 -07001180 BOOT_LOG_DBG("erasing trailer; fa_id=%d", fap->fa_id);
1181
Fabio Utzig10ee6482019-08-01 12:04:52 -03001182 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001183 fa_id_primary = flash_area_id_from_multi_image_slot(image_index,
1184 BOOT_PRIMARY_SLOT);
1185 fa_id_secondary = flash_area_id_from_multi_image_slot(image_index,
1186 BOOT_SECONDARY_SLOT);
David Vinczeb75c12a2019-03-22 14:58:33 +01001187
1188 if (fap->fa_id == fa_id_primary) {
David Vincze2d736ad2019-02-18 11:50:22 +01001189 slot = BOOT_PRIMARY_SLOT;
David Vinczeb75c12a2019-03-22 14:58:33 +01001190 } else if (fap->fa_id == fa_id_secondary) {
David Vincze2d736ad2019-02-18 11:50:22 +01001191 slot = BOOT_SECONDARY_SLOT;
David Vinczeb75c12a2019-03-22 14:58:33 +01001192 } else {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001193 return BOOT_EFLASH;
1194 }
1195
Fabio Utziged0ca432019-01-23 14:50:11 -02001196 /* delete starting from last sector and moving to beginning */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001197 sector = boot_img_num_sectors(state, slot) - 1;
1198 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
Fabio Utziged0ca432019-01-23 14:50:11 -02001199 total_sz = 0;
1200 do {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001201 sz = boot_img_sector_size(state, slot, sector);
1202 off = boot_img_sector_off(state, slot, sector);
Fabio Utziged0ca432019-01-23 14:50:11 -02001203 rc = boot_erase_sector(fap, off, sz);
1204 assert(rc == 0);
1205
1206 sector--;
1207 total_sz += sz;
1208 } while (total_sz < trailer_sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001209
1210 return rc;
1211}
Fabio Utzig358c9352017-07-25 22:10:45 -03001212#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001213
Christopher Collins92ea77f2016-12-12 15:59:26 -08001214/**
1215 * Swaps the contents of two flash regions within the two image slots.
1216 *
1217 * @param idx The index of the first sector in the range of
1218 * sectors being swapped.
1219 * @param sz The number of bytes to swap.
1220 * @param bs The current boot status. This struct gets
1221 * updated according to the outcome.
1222 *
1223 * @return 0 on success; nonzero on failure.
1224 */
Fabio Utzig3488eef2017-06-12 10:25:43 -03001225#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -08001226static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001227boot_swap_sectors(int idx, uint32_t sz, struct boot_loader_state *state,
1228 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001229{
David Vincze2d736ad2019-02-18 11:50:22 +01001230 const struct flash_area *fap_primary_slot;
1231 const struct flash_area *fap_secondary_slot;
Fabio Utzigba829042018-09-18 08:29:34 -03001232 const struct flash_area *fap_scratch;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001233 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001234 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001235 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001236 uint32_t scratch_trailer_off;
1237 struct boot_swap_state swap_state;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001238 size_t last_sector;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001239 bool erase_scratch;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001240 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001241 int rc;
1242
1243 /* Calculate offset from start of image area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001244 img_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001245
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001246 copy_sz = sz;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001247 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
Fabio Utzig9678c972017-05-23 11:28:56 -04001248
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001249 /* sz in this function is always sized on a multiple of the sector size.
1250 * The check against the start offset of the last sector
Fabio Utzig9678c972017-05-23 11:28:56 -04001251 * is to determine if we're swapping the last sector. The last sector
1252 * needs special handling because it's where the trailer lives. If we're
1253 * copying it, we need to use scratch to write the trailer temporarily.
1254 *
1255 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
1256 * controls if special handling is needed (swapping last sector).
1257 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001258 last_sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
David Vinczeba3bd602019-06-17 16:01:43 +02001259 if ((img_off + sz) >
Fabio Utzig10ee6482019-08-01 12:04:52 -03001260 boot_img_sector_off(state, BOOT_PRIMARY_SLOT, last_sector)) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001261 copy_sz -= trailer_sz;
1262 }
1263
Fabio Utzig39000012018-07-30 12:40:20 -03001264 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001265
Fabio Utzig10ee6482019-08-01 12:04:52 -03001266 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001267
1268 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1269 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001270 assert (rc == 0);
1271
Fabio Utzigb0f04732019-07-31 09:49:19 -03001272 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1273 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001274 assert (rc == 0);
1275
1276 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch);
1277 assert (rc == 0);
1278
Fabio Utzig39000012018-07-30 12:40:20 -03001279 if (bs->state == BOOT_STATUS_STATE_0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001280 BOOT_LOG_DBG("erasing scratch area");
Christopher Collinsa1c12042019-05-23 14:00:28 -07001281 rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size);
Christopher Collins4772ac42017-02-27 20:08:01 -08001282 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001283
Fabio Utzig39000012018-07-30 12:40:20 -03001284 if (bs->idx == BOOT_STATUS_IDX_0) {
Christopher Collinsa1c12042019-05-23 14:00:28 -07001285 /* Write a trailer to the scratch area, even if we don't need the
1286 * scratch area for status. We need a temporary place to store the
1287 * `swap-type` while we erase the primary trailer.
1288 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001289 rc = boot_status_init(state, fap_scratch, bs);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001290 assert(rc == 0);
1291
1292 if (!bs->use_scratch) {
1293 /* Prepare the primary status area... here it is known that the
1294 * last sector is not being used by the image data so it's safe
1295 * to erase.
Fabio Utzig2473ac02017-05-02 12:45:02 -03001296 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001297 rc = boot_erase_trailer_sectors(state, fap_primary_slot);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001298 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001299
Fabio Utzig10ee6482019-08-01 12:04:52 -03001300 rc = boot_status_init(state, fap_primary_slot, bs);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001301 assert(rc == 0);
1302
1303 /* Erase the temporary trailer from the scratch area. */
1304 rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size);
1305 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001306 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001307 }
1308
Fabio Utzig10ee6482019-08-01 12:04:52 -03001309 rc = boot_copy_sector(state, fap_secondary_slot, fap_scratch,
Christopher Collinsa1c12042019-05-23 14:00:28 -07001310 img_off, 0, copy_sz);
1311 assert(rc == 0);
1312
Fabio Utzig39000012018-07-30 12:40:20 -03001313 bs->state = BOOT_STATUS_STATE_1;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001314 rc = boot_write_status(state, bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001315 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001316 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001317
Fabio Utzig39000012018-07-30 12:40:20 -03001318 if (bs->state == BOOT_STATUS_STATE_1) {
David Vincze2d736ad2019-02-18 11:50:22 +01001319 rc = boot_erase_sector(fap_secondary_slot, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001320 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001321
Fabio Utzig10ee6482019-08-01 12:04:52 -03001322 rc = boot_copy_sector(state, fap_primary_slot, fap_secondary_slot,
David Vincze2d736ad2019-02-18 11:50:22 +01001323 img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001324 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001325
Fabio Utzig39000012018-07-30 12:40:20 -03001326 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001327 /* If not all sectors of the slot are being swapped,
David Vincze2d736ad2019-02-18 11:50:22 +01001328 * guarantee here that only the primary slot will have the state.
Fabio Utzig2473ac02017-05-02 12:45:02 -03001329 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001330 rc = boot_erase_trailer_sectors(state, fap_secondary_slot);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001331 assert(rc == 0);
1332 }
1333
Fabio Utzig39000012018-07-30 12:40:20 -03001334 bs->state = BOOT_STATUS_STATE_2;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001335 rc = boot_write_status(state, bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001336 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001337 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001338
Fabio Utzig39000012018-07-30 12:40:20 -03001339 if (bs->state == BOOT_STATUS_STATE_2) {
David Vincze2d736ad2019-02-18 11:50:22 +01001340 rc = boot_erase_sector(fap_primary_slot, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001341 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001342
Christopher Collinsa1c12042019-05-23 14:00:28 -07001343 /* NOTE: If this is the final sector, we exclude the image trailer from
1344 * this copy (copy_sz was truncated earlier).
1345 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001346 rc = boot_copy_sector(state, fap_scratch, fap_primary_slot,
David Vincze2d736ad2019-02-18 11:50:22 +01001347 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001348 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001349
Fabio Utzig94d998c2017-05-22 11:02:41 -04001350 if (bs->use_scratch) {
Fabio Utzigba829042018-09-18 08:29:34 -03001351 scratch_trailer_off = boot_status_off(fap_scratch);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001352
1353 /* copy current status that is being maintained in scratch */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001354 rc = boot_copy_sector(state, fap_scratch, fap_primary_slot,
David Vincze2d736ad2019-02-18 11:50:22 +01001355 scratch_trailer_off, img_off + copy_sz,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001356 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(state));
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001357 BOOT_STATUS_ASSERT(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001358
Fabio Utzig2473ac02017-05-02 12:45:02 -03001359 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1360 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001361 assert(rc == 0);
1362
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001363 if (swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze2d736ad2019-02-18 11:50:22 +01001364 rc = boot_write_image_ok(fap_primary_slot);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001365 assert(rc == 0);
1366 }
1367
Christopher Collinsa1c12042019-05-23 14:00:28 -07001368 if (swap_state.swap_type != BOOT_SWAP_TYPE_NONE) {
David Vinczee2453472019-06-17 12:31:59 +02001369 rc = boot_write_swap_info(fap_primary_slot,
Fabio Utzigb0f04732019-07-31 09:49:19 -03001370 swap_state.swap_type, image_index);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001371 assert(rc == 0);
1372 }
1373
David Vincze2d736ad2019-02-18 11:50:22 +01001374 rc = boot_write_swap_size(fap_primary_slot, bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001375 assert(rc == 0);
1376
Fabio Utzigba829042018-09-18 08:29:34 -03001377#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +01001378 rc = boot_write_enc_key(fap_primary_slot, 0, bs->enckey[0]);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001379 assert(rc == 0);
1380
David Vincze2d736ad2019-02-18 11:50:22 +01001381 rc = boot_write_enc_key(fap_primary_slot, 1, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001382 assert(rc == 0);
1383#endif
David Vincze2d736ad2019-02-18 11:50:22 +01001384 rc = boot_write_magic(fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001385 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001386 }
1387
Christopher Collinsa1c12042019-05-23 14:00:28 -07001388 /* If we wrote a trailer to the scratch area, erase it after we persist
1389 * a trailer to the primary slot. We do this to prevent mcuboot from
1390 * reading a stale status from the scratch area in case of immediate
1391 * reset.
1392 */
1393 erase_scratch = bs->use_scratch;
1394 bs->use_scratch = 0;
1395
Christopher Collins92ea77f2016-12-12 15:59:26 -08001396 bs->idx++;
Fabio Utzig39000012018-07-30 12:40:20 -03001397 bs->state = BOOT_STATUS_STATE_0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001398 rc = boot_write_status(state, bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001399 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001400
1401 if (erase_scratch) {
1402 rc = boot_erase_sector(fap_scratch, 0, sz);
1403 assert(rc == 0);
1404 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001405 }
Fabio Utzigba829042018-09-18 08:29:34 -03001406
David Vincze2d736ad2019-02-18 11:50:22 +01001407 flash_area_close(fap_primary_slot);
1408 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001409 flash_area_close(fap_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001410}
Fabio Utzig3488eef2017-06-12 10:25:43 -03001411#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001412
1413/**
David Vincze2d736ad2019-02-18 11:50:22 +01001414 * Overwrite primary slot with the image contained in the secondary slot.
1415 * If a prior copy operation was interrupted by a system reset, this function
1416 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001417 *
1418 * @param bs The current boot status. This function reads
1419 * this struct to determine if it is resuming
1420 * an interrupted swap operation. This
1421 * function writes the updated status to this
1422 * function on return.
1423 *
1424 * @return 0 on success; nonzero on failure.
1425 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001426#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001427static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001428boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
David Brown17609d82017-05-05 09:41:34 -06001429{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001430 size_t sect_count;
1431 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001432 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001433 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001434 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001435 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +01001436 const struct flash_area *fap_primary_slot;
1437 const struct flash_area *fap_secondary_slot;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001438 uint8_t image_index;
David Vincze2d736ad2019-02-18 11:50:22 +01001439
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001440 (void)bs;
1441
Fabio Utzig13d9e352017-10-05 20:32:31 -03001442#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1443 uint32_t src_size = 0;
Fabio Utzigd638b172019-08-09 10:38:05 -03001444 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001445 assert(rc == 0);
1446#endif
David Brown17609d82017-05-05 09:41:34 -06001447
David Vincze2d736ad2019-02-18 11:50:22 +01001448 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1449 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -06001450
Fabio Utzigb0f04732019-07-31 09:49:19 -03001451 image_index = BOOT_CURR_IMG(state);
1452
1453 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1454 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001455 assert (rc == 0);
1456
Fabio Utzigb0f04732019-07-31 09:49:19 -03001457 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1458 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001459 assert (rc == 0);
1460
Fabio Utzig10ee6482019-08-01 12:04:52 -03001461 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001462 for (sect = 0, size = 0; sect < sect_count; sect++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001463 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
David Vincze2d736ad2019-02-18 11:50:22 +01001464 rc = boot_erase_sector(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001465 assert(rc == 0);
1466
1467 size += this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001468
1469#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1470 if (size >= src_size) {
1471 break;
1472 }
1473#endif
David Brown17609d82017-05-05 09:41:34 -06001474 }
1475
Fabio Utzigba829042018-09-18 08:29:34 -03001476#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001477 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001478 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001479 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
Fabio Utzigb0f04732019-07-31 09:49:19 -03001480 fap_secondary_slot, bs->enckey[1]);
David Vincze2d736ad2019-02-18 11:50:22 +01001481
Fabio Utzigba829042018-09-18 08:29:34 -03001482 if (rc < 0) {
1483 return BOOT_EBADIMAGE;
1484 }
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001485 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs->enckey[1])) {
Fabio Utzigba829042018-09-18 08:29:34 -03001486 return BOOT_EBADIMAGE;
1487 }
1488 }
1489#endif
1490
David Vincze2d736ad2019-02-18 11:50:22 +01001491 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1492 size);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001493 rc = boot_copy_sector(state, fap_secondary_slot, fap_primary_slot, 0, 0, size);
David Brown17609d82017-05-05 09:41:34 -06001494
Fabio Utzig13d9e352017-10-05 20:32:31 -03001495 /*
1496 * Erases header and trailer. The trailer is erased because when a new
1497 * image is written without a trailer as is the case when using newt, the
1498 * trailer that was left might trigger a new upgrade.
1499 */
Christopher Collins2c88e692019-05-22 15:10:14 -07001500 BOOT_LOG_DBG("erasing secondary header");
David Vincze2d736ad2019-02-18 11:50:22 +01001501 rc = boot_erase_sector(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001502 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
1503 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001504 assert(rc == 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001505 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -07001506 BOOT_LOG_DBG("erasing secondary trailer");
David Vincze2d736ad2019-02-18 11:50:22 +01001507 rc = boot_erase_sector(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001508 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
1509 last_sector),
1510 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
1511 last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001512 assert(rc == 0);
1513
David Vincze2d736ad2019-02-18 11:50:22 +01001514 flash_area_close(fap_primary_slot);
1515 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001516
David Vincze2d736ad2019-02-18 11:50:22 +01001517 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001518
1519 return 0;
1520}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001521#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001522
Christopher Collinsa1c12042019-05-23 14:00:28 -07001523#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -03001524/**
1525 * Swaps the two images in flash. If a prior copy operation was interrupted
1526 * by a system reset, this function completes that operation.
1527 *
1528 * @param bs The current boot status. This function reads
1529 * this struct to determine if it is resuming
1530 * an interrupted swap operation. This
1531 * function writes the updated status to this
1532 * function on return.
1533 *
1534 * @return 0 on success; nonzero on failure.
1535 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001536static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001537boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001538{
1539 uint32_t sz;
1540 int first_sector_idx;
1541 int last_sector_idx;
David Vincze2d736ad2019-02-18 11:50:22 +01001542 int last_idx_secondary_slot;
Fabio Utzigcd5774b2017-11-29 10:18:26 -02001543 uint32_t swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001544 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001545#ifdef MCUBOOT_ENC_IMAGES
1546 const struct flash_area *fap;
1547 uint8_t slot;
1548 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001549#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001550 uint32_t size;
1551 uint32_t copy_size;
David Vincze2d736ad2019-02-18 11:50:22 +01001552 uint32_t primary_slot_size;
1553 uint32_t secondary_slot_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001554 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001555 int rc;
1556
1557 /* FIXME: just do this if asked by user? */
1558
1559 size = copy_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001560 image_index = BOOT_CURR_IMG(state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001561
Fabio Utzig39000012018-07-30 12:40:20 -03001562 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Fabio Utzig46490722017-09-04 15:34:32 -03001563 /*
1564 * No swap ever happened, so need to find the largest image which
1565 * will be used to determine the amount of sectors to swap.
1566 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001567 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001568 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001569 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001570 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001571 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001572
Fabio Utzigba829042018-09-18 08:29:34 -03001573#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001574 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001575 fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001576 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs->enckey[0]);
Fabio Utzigba829042018-09-18 08:29:34 -03001577 assert(rc >= 0);
1578
1579 if (rc == 0) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001580 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 0, bs->enckey[0]);
Fabio Utzigba829042018-09-18 08:29:34 -03001581 assert(rc == 0);
1582 } else {
1583 rc = 0;
1584 }
1585 } else {
1586 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1587 }
1588#endif
1589
Fabio Utzig10ee6482019-08-01 12:04:52 -03001590 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001591 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001592 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001593 assert(rc == 0);
1594 }
1595
Fabio Utzigba829042018-09-18 08:29:34 -03001596#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001597 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001598 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001599 fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001600 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001601 assert(rc >= 0);
1602
1603 if (rc == 0) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001604 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001605 assert(rc == 0);
1606 } else {
1607 rc = 0;
1608 }
1609 } else {
1610 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1611 }
1612#endif
1613
Fabio Utzig46490722017-09-04 15:34:32 -03001614 if (size > copy_size) {
1615 copy_size = size;
1616 }
1617
1618 bs->swap_size = copy_size;
1619 } else {
1620 /*
1621 * If a swap was under way, the swap_size should already be present
1622 * in the trailer...
1623 */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001624 rc = boot_read_swap_size(image_index, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001625 assert(rc == 0);
1626
1627 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001628
1629#ifdef MCUBOOT_ENC_IMAGES
1630 for (slot = 0; slot <= 1; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03001631 rc = boot_read_enc_key(image_index, slot, bs->enckey[slot]);
Fabio Utzigba829042018-09-18 08:29:34 -03001632 assert(rc == 0);
1633
1634 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001635 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001636 break;
1637 }
1638 }
1639
1640 if (i != BOOT_ENC_KEY_SIZE) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001641 boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs->enckey[slot]);
Fabio Utzigba829042018-09-18 08:29:34 -03001642 }
1643 }
1644#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001645 }
1646
David Vincze2d736ad2019-02-18 11:50:22 +01001647 primary_slot_size = 0;
1648 secondary_slot_size = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001649 last_sector_idx = 0;
David Vincze2d736ad2019-02-18 11:50:22 +01001650 last_idx_secondary_slot = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001651
1652 /*
1653 * Knowing the size of the largest image between both slots, here we
David Vincze2d736ad2019-02-18 11:50:22 +01001654 * find what is the last sector in the primary slot that needs swapping.
1655 * Since we already know that both slots are compatible, the secondary
1656 * slot's last sector is not really required after this check is finished.
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001657 */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001658 while (1) {
David Vincze2d736ad2019-02-18 11:50:22 +01001659 if ((primary_slot_size < copy_size) ||
1660 (primary_slot_size < secondary_slot_size)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001661 primary_slot_size += boot_img_sector_size(state,
David Vincze2d736ad2019-02-18 11:50:22 +01001662 BOOT_PRIMARY_SLOT,
1663 last_sector_idx);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001664 }
David Vincze2d736ad2019-02-18 11:50:22 +01001665 if ((secondary_slot_size < copy_size) ||
1666 (secondary_slot_size < primary_slot_size)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001667 secondary_slot_size += boot_img_sector_size(state,
David Vincze2d736ad2019-02-18 11:50:22 +01001668 BOOT_SECONDARY_SLOT,
1669 last_idx_secondary_slot);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001670 }
David Vincze2d736ad2019-02-18 11:50:22 +01001671 if (primary_slot_size >= copy_size &&
1672 secondary_slot_size >= copy_size &&
1673 primary_slot_size == secondary_slot_size) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001674 break;
1675 }
1676 last_sector_idx++;
David Vincze2d736ad2019-02-18 11:50:22 +01001677 last_idx_secondary_slot++;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001678 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001679
1680 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001681 while (last_sector_idx >= 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001682 sz = boot_copy_sz(state, last_sector_idx, &first_sector_idx);
Fabio Utzig39000012018-07-30 12:40:20 -03001683 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001684 boot_swap_sectors(first_sector_idx, sz, state, bs);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001685 }
1686
1687 last_sector_idx = first_sector_idx - 1;
1688 swap_idx++;
1689 }
1690
David Vincze2d736ad2019-02-18 11:50:22 +01001691#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001692 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001693 BOOT_LOG_WRN("%d status write fails performing the swap",
1694 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001695 }
1696#endif
1697
Christopher Collins92ea77f2016-12-12 15:59:26 -08001698 return 0;
1699}
David Brown17609d82017-05-05 09:41:34 -06001700#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001701
1702/**
David Vincze2d736ad2019-02-18 11:50:22 +01001703 * Marks the image in the primary slot as fully copied.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001704 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001705#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001706static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001707boot_set_copy_done(uint8_t image_index)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001708{
1709 const struct flash_area *fap;
1710 int rc;
1711
Fabio Utzigb0f04732019-07-31 09:49:19 -03001712 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1713 &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001714 if (rc != 0) {
1715 return BOOT_EFLASH;
1716 }
1717
1718 rc = boot_write_copy_done(fap);
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001719 flash_area_close(fap);
1720 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001721}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001722#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001723
1724/**
David Vincze2d736ad2019-02-18 11:50:22 +01001725 * Marks a reverted image in the primary slot as confirmed. This is necessary to
1726 * ensure the status bytes from the image revert operation don't get processed
1727 * on a subsequent boot.
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001728 *
1729 * NOTE: image_ok is tested before writing because if there's a valid permanent
David Vincze2d736ad2019-02-18 11:50:22 +01001730 * image installed on the primary slot and the new image to be upgrade to has a
1731 * bad sig, image_ok would be overwritten.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001732 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001733#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001734static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001735boot_set_image_ok(uint8_t image_index)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001736{
1737 const struct flash_area *fap;
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001738 struct boot_swap_state state;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001739 int rc;
1740
Fabio Utzigb0f04732019-07-31 09:49:19 -03001741 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1742 &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001743 if (rc != 0) {
1744 return BOOT_EFLASH;
1745 }
1746
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001747 rc = boot_read_swap_state(fap, &state);
1748 if (rc != 0) {
1749 rc = BOOT_EFLASH;
1750 goto out;
1751 }
1752
1753 if (state.image_ok == BOOT_FLAG_UNSET) {
1754 rc = boot_write_image_ok(fap);
1755 }
1756
1757out:
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001758 flash_area_close(fap);
1759 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001760}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001761#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001762
David Vinczee32483f2019-06-13 10:46:24 +02001763#if (BOOT_IMAGE_NUMBER > 1)
1764/**
1765 * Check the image dependency whether it is satisfied and modify
1766 * the swap type if necessary.
1767 *
1768 * @param dep Image dependency which has to be verified.
1769 *
1770 * @return 0 on success; nonzero on failure.
1771 */
1772static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001773boot_verify_single_dependency(struct boot_loader_state *state,
1774 struct image_dependency *dep)
David Vinczee32483f2019-06-13 10:46:24 +02001775{
1776 struct image_version *dep_version;
1777 size_t dep_slot;
1778 int rc;
1779
1780 /* Determine the source of the image which is the subject of
1781 * the dependency and get it's version. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001782 dep_slot = (state->swap_type[dep->image_id] != BOOT_SWAP_TYPE_NONE) ?
David Vinczee32483f2019-06-13 10:46:24 +02001783 BOOT_SECONDARY_SLOT : BOOT_PRIMARY_SLOT;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001784 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +02001785
1786 rc = boot_is_version_sufficient(&dep->image_min_version, dep_version);
1787 if (rc != 0) {
1788 /* Dependency not satisfied.
1789 * Modify the swap type to decrease the version number of the image
1790 * (which will be located in the primary slot after the boot process),
1791 * consequently the number of unsatisfied dependencies will be
1792 * decreased or remain the same.
1793 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001794 switch (BOOT_SWAP_TYPE(state)) {
David Vinczee32483f2019-06-13 10:46:24 +02001795 case BOOT_SWAP_TYPE_TEST:
1796 case BOOT_SWAP_TYPE_PERM:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001797 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczee32483f2019-06-13 10:46:24 +02001798 break;
1799 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001800 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczee32483f2019-06-13 10:46:24 +02001801 break;
1802 default:
1803 break;
1804 }
1805 }
1806
1807 return rc;
1808}
1809
1810/**
1811 * Read all dependency TLVs of an image from the flash and verify
1812 * one after another to see if they are all satisfied.
1813 *
1814 * @param slot Image slot number.
1815 *
1816 * @return 0 on success; nonzero on failure.
1817 */
1818static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001819boot_verify_all_dependency(struct boot_loader_state *state, uint32_t slot)
David Vinczee32483f2019-06-13 10:46:24 +02001820{
1821 const struct flash_area *fap;
David Vinczee32483f2019-06-13 10:46:24 +02001822 struct image_tlv_info info;
1823 struct image_tlv tlv;
1824 struct image_dependency dep;
1825 uint32_t off;
1826 uint32_t end;
1827 bool dep_tlvs_found = false;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001828 int area_id;
David Vinczee32483f2019-06-13 10:46:24 +02001829 int rc;
1830
Fabio Utzig10ee6482019-08-01 12:04:52 -03001831 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001832 rc = flash_area_open(area_id, &fap);
David Vinczee32483f2019-06-13 10:46:24 +02001833 if (rc != 0) {
1834 rc = BOOT_EFLASH;
1835 goto done;
1836 }
1837
Fabio Utzigd638b172019-08-09 10:38:05 -03001838 off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
David Vinczee32483f2019-06-13 10:46:24 +02001839
1840 /* The TLV area always starts with an image_tlv_info structure. */
1841 rc = flash_area_read(fap, off, &info, sizeof(info));
1842 if (rc != 0) {
1843 rc = BOOT_EFLASH;
1844 goto done;
1845 }
1846
1847 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
1848 rc = BOOT_EBADIMAGE;
1849 goto done;
1850 }
1851 end = off + info.it_tlv_tot;
1852 off += sizeof(info);
1853
1854 /* Traverse through all of the TLVs to find the dependency TLVs. */
1855 for (; off < end; off += sizeof(tlv) + tlv.it_len) {
1856 rc = flash_area_read(fap, off, &tlv, sizeof(tlv));
1857 if (rc != 0) {
1858 rc = BOOT_EFLASH;
1859 goto done;
1860 }
1861
1862 if (tlv.it_type == IMAGE_TLV_DEPENDENCY) {
1863 if (!dep_tlvs_found) {
1864 dep_tlvs_found = true;
1865 }
1866
1867 if (tlv.it_len != sizeof(dep)) {
1868 rc = BOOT_EBADIMAGE;
1869 goto done;
1870 }
1871
1872 rc = flash_area_read(fap, off + sizeof(tlv), &dep, tlv.it_len);
1873 if (rc != 0) {
1874 rc = BOOT_EFLASH;
1875 goto done;
1876 }
1877
1878 /* Verify dependency and modify the swap type if not satisfied. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001879 rc = boot_verify_single_dependency(state, &dep);
David Vinczee32483f2019-06-13 10:46:24 +02001880 if (rc != 0) {
1881 /* Dependency not satisfied. */
1882 goto done;
1883 }
1884
1885 /* Dependency satisfied, no action needed.
1886 * Continue with the next TLV entry.
1887 */
1888 } else if (dep_tlvs_found) {
1889 /* The dependency TLVs are contiguous in the TLV area. If a
1890 * dependency had already been found and the last read TLV
1891 * has a different type then there are no more dependency TLVs.
1892 * The search can be finished.
1893 */
1894 break;
1895 }
1896 }
1897
1898done:
1899 flash_area_close(fap);
1900 return rc;
1901}
1902
1903/**
1904 * Verify whether the image dependencies in the TLV area are
1905 * all satisfied and modify the swap type if necessary.
1906 *
1907 * @return 0 if all dependencies are satisfied,
1908 * nonzero otherwise.
1909 */
1910static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001911boot_verify_single_image_dependency(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001912{
1913 size_t slot;
1914
1915 /* Determine the source of the dependency TLVs. Those dependencies have to
1916 * be checked which belong to the image that will be located in the primary
1917 * slot after the firmware update process.
1918 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001919 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1920 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
David Vinczee32483f2019-06-13 10:46:24 +02001921 slot = BOOT_SECONDARY_SLOT;
1922 } else {
1923 slot = BOOT_PRIMARY_SLOT;
1924 }
1925
Fabio Utzig10ee6482019-08-01 12:04:52 -03001926 return boot_verify_all_dependency(state, slot);
David Vinczee32483f2019-06-13 10:46:24 +02001927}
1928
1929/**
1930 * Iterate over all the images and verify whether the image dependencies in the
1931 * TLV area are all satisfied and update the related swap type if necessary.
1932 */
1933static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001934boot_verify_all_image_dependency(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001935{
David Vinczee32483f2019-06-13 10:46:24 +02001936 int rc;
1937
Fabio Utzig10ee6482019-08-01 12:04:52 -03001938 BOOT_CURR_IMG(state) = 0;
1939 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
1940 rc = boot_verify_single_image_dependency(state);
Fabio Utzigabec0732019-07-31 08:40:22 -03001941 if (rc == 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001942 /* All dependencies've been satisfied, continue with next image. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001943 BOOT_CURR_IMG(state)++;
David Vinczee32483f2019-06-13 10:46:24 +02001944 } else if (rc == BOOT_EBADVERSION) {
1945 /* Dependency check needs to be restarted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001946 BOOT_CURR_IMG(state) = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001947 } else {
1948 /* Other error happened, images are inconsistent */
1949 return;
1950 }
1951 }
1952}
1953#endif /* (BOOT_IMAGE_NUMBER > 1) */
1954
Christopher Collins92ea77f2016-12-12 15:59:26 -08001955/**
David Vinczeba3bd602019-06-17 16:01:43 +02001956 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001957 *
David Vinczeba3bd602019-06-17 16:01:43 +02001958 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001959 *
1960 * @return 0 on success; nonzero on failure.
1961 */
1962static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001963boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001964{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001965 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001966#ifndef MCUBOOT_OVERWRITE_ONLY
1967 uint8_t swap_type;
1968#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001969
David Vinczeba3bd602019-06-17 16:01:43 +02001970 /* At this point there are no aborted swaps. */
1971#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001972 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001973#elif defined(MCUBOOT_BOOTSTRAP)
1974 /* Check if the image update was triggered by a bad image in the
1975 * primary slot (the validity of the image in the secondary slot had
1976 * already been checked).
1977 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001978 if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 ||
1979 boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) {
1980 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001981 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001982 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001983 }
1984#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03001985 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001986#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001987 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001988
1989#ifndef MCUBOOT_OVERWRITE_ONLY
1990 /* The following state needs image_ok be explicitly set after the
1991 * swap was finished to avoid a new revert.
1992 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001993 swap_type = BOOT_SWAP_TYPE(state);
1994 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1995 swap_type == BOOT_SWAP_TYPE_PERM) {
1996 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001997 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001998 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001999 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002000 }
2001
Fabio Utzig10ee6482019-08-01 12:04:52 -03002002 if (swap_type == BOOT_SWAP_TYPE_TEST ||
2003 swap_type == BOOT_SWAP_TYPE_PERM ||
2004 swap_type == BOOT_SWAP_TYPE_REVERT) {
2005 rc = boot_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002006 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002007 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07002008 }
David Vinczeba3bd602019-06-17 16:01:43 +02002009 }
2010#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02002011
David Vinczeba3bd602019-06-17 16:01:43 +02002012 return rc;
2013}
2014
2015/**
2016 * Completes a previously aborted image swap.
2017 *
2018 * @param bs The current boot status.
2019 *
2020 * @return 0 on success; nonzero on failure.
2021 */
2022#if !defined(MCUBOOT_OVERWRITE_ONLY)
2023static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03002024boot_complete_partial_swap(struct boot_loader_state *state,
2025 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02002026{
2027 int rc;
2028
2029 /* Determine the type of swap operation being resumed from the
2030 * `swap-type` trailer field.
2031 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002032 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002033 assert(rc == 0);
2034
Fabio Utzig10ee6482019-08-01 12:04:52 -03002035 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02002036
2037 /* The following states need image_ok be explicitly set after the
2038 * swap was finished to avoid a new revert.
2039 */
2040 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
2041 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002042 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002043 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002044 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002045 }
2046 }
2047
2048 if (bs->swap_type == BOOT_SWAP_TYPE_TEST ||
2049 bs->swap_type == BOOT_SWAP_TYPE_PERM ||
2050 bs->swap_type == BOOT_SWAP_TYPE_REVERT) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002051 rc = boot_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002052 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002053 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002054 }
2055 }
2056
Fabio Utzig10ee6482019-08-01 12:04:52 -03002057 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002058 BOOT_LOG_ERR("panic!");
2059 assert(0);
2060
2061 /* Loop forever... */
2062 while (1) {}
2063 }
2064
2065 return rc;
2066}
2067#endif /* !MCUBOOT_OVERWRITE_ONLY */
2068
2069#if (BOOT_IMAGE_NUMBER > 1)
2070/**
2071 * Review the validity of previously determined swap types of other images.
2072 *
2073 * @param aborted_swap The current image upgrade is a
2074 * partial/aborted swap.
2075 */
2076static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03002077boot_review_image_swap_types(struct boot_loader_state *state,
2078 bool aborted_swap)
David Vinczeba3bd602019-06-17 16:01:43 +02002079{
2080 /* In that case if we rebooted in the middle of an image upgrade process, we
2081 * must review the validity of swap types, that were previously determined
2082 * for other images. The image_ok flag had not been set before the reboot
2083 * for any of the updated images (only the copy_done flag) and thus falsely
2084 * the REVERT swap type has been determined for the previous images that had
2085 * been updated before the reboot.
2086 *
2087 * There are two separate scenarios that we have to deal with:
2088 *
2089 * 1. The reboot has happened during swapping an image:
2090 * The current image upgrade has been determined as a
2091 * partial/aborted swap.
2092 * 2. The reboot has happened between two separate image upgrades:
2093 * In this scenario we must check the swap type of the current image.
2094 * In those cases if it is NONE or REVERT we cannot certainly determine
2095 * the fact of a reboot. In a consistent state images must move in the
2096 * same direction or stay in place, e.g. in practice REVERT and TEST
2097 * swap types cannot be present at the same time. If the swap type of
2098 * the current image is either TEST, PERM or FAIL we must review the
2099 * already determined swap types of other images and set each false
2100 * REVERT swap types to NONE (these images had been successfully
2101 * updated before the system rebooted between two separate image
2102 * upgrades).
2103 */
2104
Fabio Utzig10ee6482019-08-01 12:04:52 -03002105 if (BOOT_CURR_IMG(state) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02002106 /* Nothing to do */
2107 return;
2108 }
2109
2110 if (!aborted_swap) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002111 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
2112 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002113 /* Nothing to do */
2114 return;
2115 }
2116 }
2117
Fabio Utzig10ee6482019-08-01 12:04:52 -03002118 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
2119 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
2120 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002121 }
2122 }
2123}
2124#endif
2125
2126/**
2127 * Prepare image to be updated if required.
2128 *
2129 * Prepare image to be updated if required with completing an image swap
2130 * operation if one was aborted and/or determining the type of the
2131 * swap operation. In case of any error set the swap type to NONE.
2132 *
Fabio Utzig10ee6482019-08-01 12:04:52 -03002133 * @param state TODO
David Vinczeba3bd602019-06-17 16:01:43 +02002134 * @param bs Pointer where the read and possibly updated
2135 * boot status can be written to.
2136 */
2137static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03002138boot_prepare_image_for_update(struct boot_loader_state *state,
2139 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02002140{
2141 int rc;
2142
2143 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002144 rc = boot_read_sectors(state);
David Vinczeba3bd602019-06-17 16:01:43 +02002145 if (rc != 0) {
2146 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
2147 " - too small?", BOOT_MAX_IMG_SECTORS);
2148 /* Unable to determine sector layout, continue with next image
2149 * if there is one.
2150 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002151 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002152 return;
2153 }
2154
2155 /* Attempt to read an image header from each slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002156 rc = boot_read_image_headers(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02002157 if (rc != 0) {
2158 /* Continue with next image if there is one. */
Fabio Utzigb0f04732019-07-31 09:49:19 -03002159 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03002160 BOOT_CURR_IMG(state));
2161 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002162 return;
2163 }
2164
2165 /* If the current image's slots aren't compatible, no swap is possible.
2166 * Just boot into primary slot.
2167 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002168 if (boot_slots_compatible(state)) {
2169 rc = boot_read_status(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002170 if (rc != 0) {
2171 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03002172 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002173 /* Continue with next image if there is one. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002174 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002175 return;
2176 }
2177
2178 /* Determine if we rebooted in the middle of an image swap
2179 * operation. If a partial swap was detected, complete it.
2180 */
2181 if (bs->idx != BOOT_STATUS_IDX_0 || bs->state != BOOT_STATUS_STATE_0) {
2182
2183#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03002184 boot_review_image_swap_types(state, true);
David Vinczeba3bd602019-06-17 16:01:43 +02002185#endif
2186
2187#ifdef MCUBOOT_OVERWRITE_ONLY
2188 /* Should never arrive here, overwrite-only mode has
2189 * no swap state.
2190 */
2191 assert(0);
2192#else
2193 /* Determine the type of swap operation being resumed from the
2194 * `swap-type` trailer field.
2195 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002196 rc = boot_complete_partial_swap(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002197 assert(rc == 0);
2198#endif
2199 /* Attempt to read an image header from each slot. Ensure that
2200 * image headers in slots are aligned with headers in boot_data.
2201 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002202 rc = boot_read_image_headers(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02002203 assert(rc == 0);
2204
2205 /* Swap has finished set to NONE */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002206 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002207 } else {
2208 /* There was no partial swap, determine swap type. */
2209 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002210 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
2211 } else if (boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) != 0) {
2212 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
David Vinczeba3bd602019-06-17 16:01:43 +02002213 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002214 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02002215 }
2216
2217#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03002218 boot_review_image_swap_types(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02002219#endif
2220
2221#ifdef MCUBOOT_BOOTSTRAP
Fabio Utzig10ee6482019-08-01 12:04:52 -03002222 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02002223 /* Header checks are done first because they are
2224 * inexpensive. Since overwrite-only copies starting from
2225 * offset 0, if interrupted, it might leave a valid header
2226 * magic, so also run validation on the primary slot to be
2227 * sure it's not OK.
2228 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002229 if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 ||
2230 boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) {
2231 if (boot_img_hdr(state,
David Vinczeba3bd602019-06-17 16:01:43 +02002232 BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC &&
Fabio Utzig10ee6482019-08-01 12:04:52 -03002233 boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) == 0)
David Vinczeba3bd602019-06-17 16:01:43 +02002234 {
2235 /* Set swap type to REVERT to overwrite the primary
2236 * slot with the image contained in secondary slot
2237 * and to trigger the explicit setting of the
2238 * image_ok flag.
2239 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002240 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02002241 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02002242 }
2243 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02002244#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002245 }
David Vinczeba3bd602019-06-17 16:01:43 +02002246 } else {
2247 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002248 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002249 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002250}
2251
Christopher Collins92ea77f2016-12-12 15:59:26 -08002252int
Fabio Utzig10ee6482019-08-01 12:04:52 -03002253context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08002254{
Marti Bolivar84898652017-06-13 17:20:22 -04002255 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02002256 struct boot_status bs;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002257 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002258 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03002259 int image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002260
2261 /* The array of slot sectors are defined here (as opposed to file scope) so
2262 * that they don't get allocated for non-boot-loader apps. This is
2263 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002264 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08002265 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002266 TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
2267 TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
2268 TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Christopher Collins92ea77f2016-12-12 15:59:26 -08002269
Fabio Utzig10ee6482019-08-01 12:04:52 -03002270 memset(state, 0, sizeof(struct boot_loader_state));
Fabio Utzigba829042018-09-18 08:29:34 -03002271
David Vinczeba3bd602019-06-17 16:01:43 +02002272 /* Iterate over all the images. By the end of the loop the swap type has
2273 * to be determined for each image and all aborted swaps have to be
2274 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002275 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002276 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03002277
David Vinczeba3bd602019-06-17 16:01:43 +02002278#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
2279 /* The keys used for encryption may no longer be valid (could belong to
2280 * another images). Therefore, mark them as invalid to force their reload
2281 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03002282 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03002283 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Brown554c52e2017-06-30 16:01:07 -06002284#endif
2285
Fabio Utzig10ee6482019-08-01 12:04:52 -03002286 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03002287
Fabio Utzig10ee6482019-08-01 12:04:52 -03002288 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002289 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03002290 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002291 secondary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03002292 state->scratch.sectors = scratch_sectors;
David Vinczeba3bd602019-06-17 16:01:43 +02002293
2294 /* Open primary and secondary image areas for the duration
2295 * of this call.
2296 */
2297 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03002298 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03002299 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02002300 assert(rc == 0);
2301 }
2302 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03002303 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002304 assert(rc == 0);
2305
2306 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002307 boot_prepare_image_for_update(state, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002308 }
2309
David Vinczee32483f2019-06-13 10:46:24 +02002310#if (BOOT_IMAGE_NUMBER > 1)
2311 /* Iterate over all the images and verify whether the image dependencies
2312 * are all satisfied and update swap type if necessary.
2313 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002314 boot_verify_all_image_dependency(state);
David Vinczee32483f2019-06-13 10:46:24 +02002315#endif
2316
David Vinczeba3bd602019-06-17 16:01:43 +02002317 /* Iterate over all the images. At this point there are no aborted swaps
2318 * and the swap types are determined for each image. By the end of the loop
2319 * all required update operations will have been finished.
2320 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002321 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002322
2323#if (BOOT_IMAGE_NUMBER > 1)
2324#ifdef MCUBOOT_ENC_IMAGES
2325 /* The keys used for encryption may no longer be valid (could belong to
2326 * another images). Therefore, mark them as invalid to force their reload
2327 * by boot_enc_load().
2328 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03002329 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002330#endif /* MCUBOOT_ENC_IMAGES */
2331
2332 /* Indicate that swap is not aborted */
2333 memset(&bs, 0, sizeof bs);
2334 bs.idx = BOOT_STATUS_IDX_0;
2335 bs.state = BOOT_STATUS_STATE_0;
2336#endif /* (BOOT_IMAGE_NUMBER > 1) */
2337
2338 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002339 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02002340
Fabio Utzig10ee6482019-08-01 12:04:52 -03002341 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002342 case BOOT_SWAP_TYPE_NONE:
2343 break;
2344
2345 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
2346 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
2347 case BOOT_SWAP_TYPE_REVERT:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002348 rc = boot_perform_update(state, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002349 assert(rc == 0);
2350 break;
2351
2352 case BOOT_SWAP_TYPE_FAIL:
2353 /* The image in secondary slot was invalid and is now erased. Ensure
2354 * we don't try to boot into it again on the next reboot. Do this by
2355 * pretending we just reverted back to primary slot.
2356 */
2357#ifndef MCUBOOT_OVERWRITE_ONLY
2358 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002359 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002360 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002361 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002362 }
2363#endif /* !MCUBOOT_OVERWRITE_ONLY */
2364 break;
2365
2366 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002367 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002368 }
2369
Fabio Utzig10ee6482019-08-01 12:04:52 -03002370 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002371 BOOT_LOG_ERR("panic!");
2372 assert(0);
2373
2374 /* Loop forever... */
2375 while (1) {}
2376 }
2377 }
2378
2379 /* Iterate over all the images. At this point all required update operations
2380 * have finished. By the end of the loop each image in the primary slot will
2381 * have been re-validated.
2382 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002383 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2384 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02002385 /* Attempt to read an image header from each slot. Ensure that image
2386 * headers in slots are aligned with headers in boot_data.
2387 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002388 rc = boot_read_image_headers(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02002389 if (rc != 0) {
2390 goto out;
2391 }
2392 /* Since headers were reloaded, it can be assumed we just performed
2393 * a swap or overwrite. Now the header info that should be used to
2394 * provide the data for the bootstrap, which previously was at
2395 * secondary slot, was updated to primary slot.
2396 */
2397 }
2398
2399#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig10ee6482019-08-01 12:04:52 -03002400 rc = boot_validate_slot(state, BOOT_PRIMARY_SLOT, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02002401 if (rc != 0) {
2402 rc = BOOT_EBADIMAGE;
2403 goto out;
2404 }
2405#else
2406 /* Even if we're not re-validating the primary slot, we could be booting
2407 * onto an empty flash chip. At least do a basic sanity check that
2408 * the magic number on the image is OK.
2409 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002410 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002411 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
Fabio Utzig10ee6482019-08-01 12:04:52 -03002412 &boot_img_hdr(state,BOOT_PRIMARY_SLOT)->ih_magic,
2413 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002414 rc = BOOT_EBADIMAGE;
2415 goto out;
2416 }
2417#endif
2418 }
2419
Fabio Utzigb0f04732019-07-31 09:49:19 -03002420#if (BOOT_IMAGE_NUMBER > 1)
David Vinczeba3bd602019-06-17 16:01:43 +02002421 /* Always boot from the primary slot of Image 0. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002422 BOOT_CURR_IMG(state) = 0;
Fabio Utzigb0f04732019-07-31 09:49:19 -03002423#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -03002424 rsp->br_flash_dev_id = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)->fa_device_id;
2425 rsp->br_image_off = boot_img_slot_off(state, BOOT_PRIMARY_SLOT);
2426 rsp->br_hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002427
Marti Bolivarc0b47912017-06-13 17:18:09 -04002428 out:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002429 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2430 flash_area_close(BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002431 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002432 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
David Vinczeba3bd602019-06-17 16:01:43 +02002433 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04002434 }
2435 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002436}
2437
Fabio Utzig10ee6482019-08-01 12:04:52 -03002438/**
2439 * Prepares the booting process. This function moves images around in flash as
2440 * appropriate, and tells you what address to boot from.
2441 *
2442 * @param rsp On success, indicates how booting should occur.
2443 *
2444 * @return 0 on success; nonzero on failure.
2445 */
2446int
2447boot_go(struct boot_rsp *rsp)
2448{
2449 return context_boot_go(&boot_data, rsp);
2450}
2451
Christopher Collins92ea77f2016-12-12 15:59:26 -08002452int
2453split_go(int loader_slot, int split_slot, void **entry)
2454{
Marti Bolivarc50926f2017-06-14 09:35:40 -04002455 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08002456 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002457 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002458 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002459 int rc;
2460
Christopher Collins92ea77f2016-12-12 15:59:26 -08002461 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
2462 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04002463 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002464 }
David Vinczeba3bd602019-06-17 16:01:43 +02002465 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
2466 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002467
2468 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
2469 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07002470 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04002471 assert(rc == 0);
2472 split_flash_id = flash_area_id_from_image_slot(split_slot);
2473 rc = flash_area_open(split_flash_id,
2474 &BOOT_IMG_AREA(&boot_data, split_slot));
2475 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002476
2477 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002478 rc = boot_read_sectors(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002479 if (rc != 0) {
2480 rc = SPLIT_GO_ERR;
2481 goto done;
2482 }
2483
Fabio Utzig10ee6482019-08-01 12:04:52 -03002484 rc = boot_read_image_headers(&boot_data, true);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002485 if (rc != 0) {
2486 goto done;
2487 }
2488
Christopher Collins92ea77f2016-12-12 15:59:26 -08002489 /* Don't check the bootable image flag because we could really call a
2490 * bootable or non-bootable image. Just validate that the image check
2491 * passes which is distinct from the normal check.
2492 */
Marti Bolivarf804f622017-06-12 15:41:48 -04002493 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04002494 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04002495 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04002496 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002497 if (rc != 0) {
2498 rc = SPLIT_GO_NON_MATCHING;
2499 goto done;
2500 }
2501
Marti Bolivarea088872017-06-12 17:10:49 -04002502 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04002503 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08002504 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002505 rc = SPLIT_GO_OK;
2506
2507done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04002508 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
2509 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002510 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002511 return rc;
2512}