blob: e01d00e39c8e7bf6bf266cc76be140804461d1ec [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 Utzig10ee6482019-08-01 12:04:52 -0300251boot_read_image_size(struct boot_loader_state *state, int slot,
252 struct image_header *hdr, uint32_t *size)
David Brownf5b33d82017-09-01 10:58:27 -0600253{
254 const struct flash_area *fap;
255 struct image_tlv_info info;
256 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 Utzigc9621352019-08-08 12:15:51 -0300270 rc = flash_area_read(fap, BOOT_TLV_OFF(hdr), &info, sizeof(info));
David Brownf5b33d82017-09-01 10:58:27 -0600271 if (rc != 0) {
272 rc = BOOT_EFLASH;
273 goto done;
274 }
275 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
276 rc = BOOT_EBADIMAGE;
277 goto done;
278 }
Fabio Utzigc9621352019-08-08 12:15:51 -0300279 *size = BOOT_TLV_OFF(hdr) + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600280 rc = 0;
281
282done:
283 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300284 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600285}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300286#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600287
Fabio Utzigc08ed212017-06-20 19:28:36 -0300288static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300289boot_read_image_header(struct boot_loader_state *state, int slot,
290 struct image_header *out_hdr)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800291{
292 const struct flash_area *fap;
293 int area_id;
294 int rc;
295
Fabio Utzig10ee6482019-08-01 12:04:52 -0300296#if (BOOT_IMAGE_NUMBER == 1)
297 (void)state;
298#endif
299
300 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800301 rc = flash_area_open(area_id, &fap);
302 if (rc != 0) {
303 rc = BOOT_EFLASH;
304 goto done;
305 }
306
307 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
308 if (rc != 0) {
309 rc = BOOT_EFLASH;
310 goto done;
311 }
312
313 rc = 0;
314
315done:
316 flash_area_close(fap);
317 return rc;
318}
319
320static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300321boot_read_image_headers(struct boot_loader_state *state, bool require_all)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800322{
323 int rc;
324 int i;
325
326 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300327 rc = boot_read_image_header(state, i, boot_img_hdr(state, i));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800328 if (rc != 0) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200329 /* If `require_all` is set, fail on any single fail, otherwise
330 * if at least the first slot's header was read successfully,
331 * then the boot loader can attempt a boot.
332 *
333 * Failure to read any headers is a fatal error.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800334 */
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200335 if (i > 0 && !require_all) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800336 return 0;
337 } else {
338 return rc;
339 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800340 }
341 }
342
343 return 0;
344}
345
346static uint8_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300347boot_write_sz(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800348{
349 uint8_t elem_sz;
350 uint8_t align;
351
352 /* Figure out what size to write update status update as. The size depends
353 * on what the minimum write size is for scratch area, active image slot.
354 * We need to use the bigger of those 2 values.
355 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300356 elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
357 align = flash_area_align(BOOT_SCRATCH_AREA(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800358 if (align > elem_sz) {
359 elem_sz = align;
360 }
361
362 return elem_sz;
363}
364
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200365/*
366 * Slots are compatible when all sectors that store upto to size of the image
367 * round up to sector size, in both slot's are able to fit in the scratch
368 * area, and have sizes that are a multiple of each other (powers of two
369 * presumably!).
370 */
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800371static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300372boot_slots_compatible(struct boot_loader_state *state)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800373{
David Vincze2d736ad2019-02-18 11:50:22 +0100374 size_t num_sectors_primary;
375 size_t num_sectors_secondary;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200376 size_t sz0, sz1;
David Vincze2d736ad2019-02-18 11:50:22 +0100377 size_t primary_slot_sz, secondary_slot_sz;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200378 size_t scratch_sz;
379 size_t i, j;
380 int8_t smaller;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800381
Fabio Utzig10ee6482019-08-01 12:04:52 -0300382 num_sectors_primary = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
383 num_sectors_secondary = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT);
David Vincze2d736ad2019-02-18 11:50:22 +0100384 if ((num_sectors_primary > BOOT_MAX_IMG_SECTORS) ||
385 (num_sectors_secondary > BOOT_MAX_IMG_SECTORS)) {
Fabio Utziga1fae672018-03-30 10:52:38 -0300386 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800387 return 0;
388 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300389
Fabio Utzig10ee6482019-08-01 12:04:52 -0300390 scratch_sz = boot_scratch_area_size(state);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200391
392 /*
393 * The following loop scans all sectors in a linear fashion, assuring that
394 * for each possible sector in each slot, it is able to fit in the other
395 * slot's sector or sectors. Slot's should be compatible as long as any
396 * number of a slot's sectors are able to fit into another, which only
397 * excludes cases where sector sizes are not a multiple of each other.
398 */
David Vincze2d736ad2019-02-18 11:50:22 +0100399 i = sz0 = primary_slot_sz = 0;
400 j = sz1 = secondary_slot_sz = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200401 smaller = 0;
David Vincze2d736ad2019-02-18 11:50:22 +0100402 while (i < num_sectors_primary || j < num_sectors_secondary) {
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200403 if (sz0 == sz1) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300404 sz0 += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
405 sz1 += boot_img_sector_size(state, BOOT_SECONDARY_SLOT, j);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200406 i++;
407 j++;
408 } else if (sz0 < sz1) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300409 sz0 += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
David Vincze2d736ad2019-02-18 11:50:22 +0100410 /* Guarantee that multiple sectors of the secondary slot
411 * fit into the primary slot.
412 */
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200413 if (smaller == 2) {
414 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors");
415 return 0;
416 }
417 smaller = 1;
418 i++;
419 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300420 sz1 += boot_img_sector_size(state, BOOT_SECONDARY_SLOT, j);
David Vincze2d736ad2019-02-18 11:50:22 +0100421 /* Guarantee that multiple sectors of the primary slot
422 * fit into the secondary slot.
423 */
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200424 if (smaller == 1) {
425 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors");
426 return 0;
427 }
428 smaller = 2;
429 j++;
430 }
431 if (sz0 == sz1) {
David Vincze2d736ad2019-02-18 11:50:22 +0100432 primary_slot_sz += sz0;
433 secondary_slot_sz += sz1;
434 /* Scratch has to fit each swap operation to the size of the larger
435 * sector among the primary slot and the secondary slot.
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200436 */
437 if (sz0 > scratch_sz || sz1 > scratch_sz) {
438 BOOT_LOG_WRN("Cannot upgrade: not all sectors fit inside scratch");
439 return 0;
440 }
441 smaller = sz0 = sz1 = 0;
442 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300443 }
444
David Vincze2d736ad2019-02-18 11:50:22 +0100445 if ((i != num_sectors_primary) ||
446 (j != num_sectors_secondary) ||
447 (primary_slot_sz != secondary_slot_sz)) {
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200448 BOOT_LOG_WRN("Cannot upgrade: slots are not compatible");
449 return 0;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800450 }
451
452 return 1;
453}
454
Fabio Utzig10ee6482019-08-01 12:04:52 -0300455#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
456static int
457boot_initialize_area(struct boot_loader_state *state, int flash_area)
458{
459 int num_sectors = BOOT_MAX_IMG_SECTORS;
460 int rc;
461
462 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
463 rc = flash_area_to_sectors(flash_area, &num_sectors,
464 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors);
465 BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors = (size_t)num_sectors;
466
467 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
468 rc = flash_area_to_sectors(flash_area, &num_sectors,
469 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors);
470 BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors = (size_t)num_sectors;
471
472 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
473 rc = flash_area_to_sectors(flash_area, &num_sectors,
474 state->scratch.sectors);
475 state->scratch.num_sectors = (size_t)num_sectors;
476 } else {
477 return BOOT_EFLASH;
478 }
479
480 return rc;
481}
482#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
483static int
484boot_initialize_area(struct boot_loader_state *state, int flash_area)
485{
486 uint32_t num_sectors;
487 struct flash_sector *out_sectors;
488 size_t *out_num_sectors;
489 int rc;
490
491 num_sectors = BOOT_MAX_IMG_SECTORS;
492
493 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
494 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
495 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
496 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
497 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
498 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
499 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
500 out_sectors = state->scratch.sectors;
501 out_num_sectors = &state->scratch.num_sectors;
502 } else {
503 return BOOT_EFLASH;
504 }
505
506 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
507 if (rc != 0) {
508 return rc;
509 }
510 *out_num_sectors = num_sectors;
511 return 0;
512}
513#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
514
Christopher Collins92ea77f2016-12-12 15:59:26 -0800515/**
516 * Determines the sector layout of both image slots and the scratch area.
517 * This information is necessary for calculating the number of bytes to erase
518 * and copy during an image swap. The information collected during this
Fabio Utzig10ee6482019-08-01 12:04:52 -0300519 * function is used to populate the state.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800520 */
521static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300522boot_read_sectors(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800523{
Fabio Utzigb0f04732019-07-31 09:49:19 -0300524 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800525 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800526
Fabio Utzig10ee6482019-08-01 12:04:52 -0300527 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300528
529 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800530 if (rc != 0) {
531 return BOOT_EFLASH;
532 }
533
Fabio Utzig10ee6482019-08-01 12:04:52 -0300534 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800535 if (rc != 0) {
536 return BOOT_EFLASH;
537 }
538
Fabio Utzig10ee6482019-08-01 12:04:52 -0300539 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200540 if (rc != 0) {
541 return BOOT_EFLASH;
542 }
543
Fabio Utzig10ee6482019-08-01 12:04:52 -0300544 BOOT_WRITE_SZ(state) = boot_write_sz(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800545
546 return 0;
547}
548
549static uint32_t
550boot_status_internal_off(int idx, int state, int elem_sz)
551{
552 int idx_sz;
553
554 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
555
Fabio Utzig39000012018-07-30 12:40:20 -0300556 return (idx - BOOT_STATUS_IDX_0) * idx_sz +
557 (state - BOOT_STATUS_STATE_0) * elem_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800558}
559
560/**
561 * Reads the status of a partially-completed swap, if any. This is necessary
562 * to recover in case the boot lodaer was reset in the middle of a swap
563 * operation.
564 */
565static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300566boot_read_status_bytes(const struct flash_area *fap,
567 struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800568{
569 uint32_t off;
570 uint8_t status;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300571 int max_entries;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800572 int found;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200573 int found_idx;
574 int invalid;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800575 int rc;
576 int i;
577
578 off = boot_status_off(fap);
Fabio Utzig10ee6482019-08-01 12:04:52 -0300579 max_entries = boot_status_entries(BOOT_CURR_IMG(state), fap);
Fabio Utzig9d160092019-08-09 07:46:34 -0300580 if (max_entries < 0) {
581 return BOOT_EBADARGS;
582 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300583
Christopher Collins92ea77f2016-12-12 15:59:26 -0800584 found = 0;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200585 found_idx = 0;
586 invalid = 0;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300587 for (i = 0; i < max_entries; i++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300588 rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(state),
Fabio Utzig178be542018-09-19 08:12:56 -0300589 &status, 1);
590 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800591 return BOOT_EFLASH;
592 }
593
Fabio Utzig178be542018-09-19 08:12:56 -0300594 if (rc == 1) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200595 if (found && !found_idx) {
596 found_idx = i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800597 }
598 } else if (!found) {
599 found = 1;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200600 } else if (found_idx) {
601 invalid = 1;
602 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800603 }
604 }
605
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200606 if (invalid) {
607 /* This means there was an error writing status on the last
608 * swap. Tell user and move on to validation!
609 */
610 BOOT_LOG_ERR("Detected inconsistent status!");
611
David Vincze2d736ad2019-02-18 11:50:22 +0100612#if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
613 /* With validation of the primary slot disabled, there is no way
614 * to be sure the swapped primary slot is OK, so abort!
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200615 */
616 assert(0);
617#endif
618 }
619
Christopher Collins92ea77f2016-12-12 15:59:26 -0800620 if (found) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200621 if (!found_idx) {
622 found_idx = i;
623 }
624 found_idx--;
Fabio Utzig39000012018-07-30 12:40:20 -0300625 bs->idx = (found_idx / BOOT_STATUS_STATE_COUNT) + 1;
626 bs->state = (found_idx % BOOT_STATUS_STATE_COUNT) + 1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800627 }
628
629 return 0;
630}
631
632/**
633 * Reads the boot status from the flash. The boot status contains
634 * the current state of an interrupted image copy operation. If the boot
635 * status is not present, or it indicates that previous copy finished,
636 * there is no operation in progress.
637 */
638static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300639boot_read_status(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800640{
641 const struct flash_area *fap;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700642 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200643 uint8_t swap_info;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800644 int status_loc;
645 int area_id;
646 int rc;
647
648 memset(bs, 0, sizeof *bs);
Fabio Utzig39000012018-07-30 12:40:20 -0300649 bs->idx = BOOT_STATUS_IDX_0;
650 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700651 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800652
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700653#ifdef MCUBOOT_OVERWRITE_ONLY
654 /* Overwrite-only doesn't make use of the swap status area. */
655 return 0;
656#endif
657
Fabio Utzig10ee6482019-08-01 12:04:52 -0300658 status_loc = boot_status_source(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800659 switch (status_loc) {
660 case BOOT_STATUS_SOURCE_NONE:
661 return 0;
662
663 case BOOT_STATUS_SOURCE_SCRATCH:
664 area_id = FLASH_AREA_IMAGE_SCRATCH;
665 break;
666
David Vincze2d736ad2019-02-18 11:50:22 +0100667 case BOOT_STATUS_SOURCE_PRIMARY_SLOT:
Fabio Utzig10ee6482019-08-01 12:04:52 -0300668 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800669 break;
670
671 default:
672 assert(0);
673 return BOOT_EBADARGS;
674 }
675
676 rc = flash_area_open(area_id, &fap);
677 if (rc != 0) {
678 return BOOT_EFLASH;
679 }
680
Fabio Utzig10ee6482019-08-01 12:04:52 -0300681 rc = boot_read_status_bytes(fap, state, bs);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700682 if (rc == 0) {
David Vinczee2453472019-06-17 12:31:59 +0200683 off = boot_swap_info_off(fap);
684 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700685 if (rc == 1) {
David Vinczee2453472019-06-17 12:31:59 +0200686 BOOT_SET_SWAP_INFO(swap_info, 0, BOOT_SWAP_TYPE_NONE);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700687 rc = 0;
688 }
David Vinczee2453472019-06-17 12:31:59 +0200689
690 /* Extract the swap type info */
691 bs->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700692 }
Fabio Utzig46490722017-09-04 15:34:32 -0300693
694 flash_area_close(fap);
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700695
Fabio Utzig46490722017-09-04 15:34:32 -0300696 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800697}
698
699/**
700 * Writes the supplied boot status to the flash file system. The boot status
701 * contains the current state of an in-progress image copy operation.
702 *
703 * @param bs The boot status to write.
704 *
705 * @return 0 on success; nonzero on failure.
706 */
707int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300708boot_write_status(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800709{
710 const struct flash_area *fap;
711 uint32_t off;
712 int area_id;
713 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300714 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700715 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300716 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800717
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300718 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100719 * the trailer. Since in the last step the primary slot is erased, the
720 * first two status writes go to the scratch which will be copied to
721 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300722 */
723
Fabio Utzig2473ac02017-05-02 12:45:02 -0300724 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800725 /* Write to scratch. */
726 area_id = FLASH_AREA_IMAGE_SCRATCH;
727 } else {
David Vincze2d736ad2019-02-18 11:50:22 +0100728 /* Write to the primary slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300729 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800730 }
731
732 rc = flash_area_open(area_id, &fap);
733 if (rc != 0) {
734 rc = BOOT_EFLASH;
735 goto done;
736 }
737
738 off = boot_status_off(fap) +
Marti Bolivare10a7392017-06-14 16:20:07 -0400739 boot_status_internal_off(bs->idx, bs->state,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300740 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
774 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300775
Fabio Utzigba829042018-09-18 08:29:34 -0300776#ifndef MCUBOOT_ENC_IMAGES
777 (void)bs;
778 (void)rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -0300779 if (bootutil_img_validate(NULL, image_index, hdr, fap, tmpbuf,
780 BOOT_TMPBUF_SZ, NULL, 0, NULL)) {
781 return BOOT_EBADIMAGE;
782 }
Fabio Utzigba829042018-09-18 08:29:34 -0300783#else
Fabio Utzigb0f04732019-07-31 09:49:19 -0300784 if ((fap->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index))
785 && IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300786 rc = boot_enc_load(state->enc, image_index, hdr, fap, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -0300787 if (rc < 0) {
788 return BOOT_EBADIMAGE;
789 }
Fabio Utzig10ee6482019-08-01 12:04:52 -0300790 if (rc == 0 && boot_enc_set_key(state->enc, 1, bs->enckey[1])) {
Fabio Utzigba829042018-09-18 08:29:34 -0300791 return BOOT_EBADIMAGE;
792 }
793 }
Fabio Utzig10ee6482019-08-01 12:04:52 -0300794 if (bootutil_img_validate(state->enc, image_index, hdr, fap, tmpbuf,
795 BOOT_TMPBUF_SZ, NULL, 0, NULL)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800796 return BOOT_EBADIMAGE;
797 }
Fabio Utzig10ee6482019-08-01 12:04:52 -0300798#endif
799
Christopher Collins92ea77f2016-12-12 15:59:26 -0800800 return 0;
801}
802
803static int
804split_image_check(struct image_header *app_hdr,
805 const struct flash_area *app_fap,
806 struct image_header *loader_hdr,
807 const struct flash_area *loader_fap)
808{
809 static void *tmpbuf;
810 uint8_t loader_hash[32];
811
812 if (!tmpbuf) {
813 tmpbuf = malloc(BOOT_TMPBUF_SZ);
814 if (!tmpbuf) {
815 return BOOT_ENOMEM;
816 }
817 }
818
Fabio Utzig10ee6482019-08-01 12:04:52 -0300819 if (bootutil_img_validate(NULL, 0, loader_hdr, loader_fap, tmpbuf,
820 BOOT_TMPBUF_SZ, NULL, 0, loader_hash)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800821 return BOOT_EBADIMAGE;
822 }
823
Fabio Utzig10ee6482019-08-01 12:04:52 -0300824 if (bootutil_img_validate(NULL, 0, app_hdr, app_fap, tmpbuf,
825 BOOT_TMPBUF_SZ, loader_hash, 32, NULL)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800826 return BOOT_EBADIMAGE;
827 }
828
829 return 0;
830}
831
Fabio Utzig338a19f2018-12-03 08:37:08 -0200832/*
833 * Check that a memory area consists of a given value.
834 */
835static inline bool
836boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300837{
838 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200839 uint8_t *p = (uint8_t *)data;
840 for (i = 0; i < len; i++) {
841 if (val != p[i]) {
842 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300843 }
844 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200845 return true;
846}
847
848static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300849boot_check_header_erased(struct boot_loader_state *state, int slot)
Fabio Utzig338a19f2018-12-03 08:37:08 -0200850{
851 const struct flash_area *fap;
852 struct image_header *hdr;
853 uint8_t erased_val;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300854 int area_id;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200855 int rc;
856
Fabio Utzig10ee6482019-08-01 12:04:52 -0300857 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300858 rc = flash_area_open(area_id, &fap);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200859 if (rc != 0) {
860 return -1;
861 }
862
863 erased_val = flash_area_erased_val(fap);
864 flash_area_close(fap);
865
Fabio Utzig10ee6482019-08-01 12:04:52 -0300866 hdr = boot_img_hdr(state, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200867 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
868 return -1;
869 }
870
871 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300872}
873
Christopher Collins92ea77f2016-12-12 15:59:26 -0800874static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300875boot_validate_slot(struct boot_loader_state *state, int slot,
876 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800877{
878 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400879 struct image_header *hdr;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300880 int area_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800881 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300882
Fabio Utzig10ee6482019-08-01 12:04:52 -0300883 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300884 rc = flash_area_open(area_id, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800885 if (rc != 0) {
886 return BOOT_EFLASH;
887 }
888
Fabio Utzig10ee6482019-08-01 12:04:52 -0300889 hdr = boot_img_hdr(state, slot);
890 if (boot_check_header_erased(state, slot) == 0 ||
891 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100892 /* No bootable image in slot; continue booting from the primary slot. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200893 rc = -1;
894 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300895 }
896
Fabio Utzig10ee6482019-08-01 12:04:52 -0300897 if (hdr->ih_magic != IMAGE_MAGIC || boot_image_check(state, hdr, fap, bs)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100898 if (slot != BOOT_PRIMARY_SLOT) {
David Brownb38e0442017-02-24 13:57:12 -0700899 flash_area_erase(fap, 0, fap->fa_size);
David Vincze2d736ad2019-02-18 11:50:22 +0100900 /* Image in the secondary slot is invalid. Erase the image and
901 * continue booting from the primary slot.
David Brownb38e0442017-02-24 13:57:12 -0700902 */
903 }
David Vincze2d736ad2019-02-18 11:50:22 +0100904 BOOT_LOG_ERR("Image in the %s slot is not valid!",
905 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
Fabio Utzig338a19f2018-12-03 08:37:08 -0200906 rc = -1;
907 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800908 }
909
David Vincze2d736ad2019-02-18 11:50:22 +0100910 /* Image in the secondary slot is valid. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200911 rc = 0;
912
913out:
914 flash_area_close(fap);
915 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800916}
917
918/**
919 * Determines which swap operation to perform, if any. If it is determined
David Vincze2d736ad2019-02-18 11:50:22 +0100920 * that a swap operation is required, the image in the secondary slot is checked
921 * for validity. If the image in the secondary slot is invalid, it is erased,
922 * and a swap type of "none" is indicated.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800923 *
924 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
925 */
926static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300927boot_validated_swap_type(struct boot_loader_state *state,
928 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800929{
930 int swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800931
Fabio Utzigb0f04732019-07-31 09:49:19 -0300932#if (BOOT_IMAGE_NUMBER == 1)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800933 swap_type = boot_swap_type();
Fabio Utzigb0f04732019-07-31 09:49:19 -0300934#else
935 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
936#endif
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300937 switch (swap_type) {
938 case BOOT_SWAP_TYPE_TEST:
939 case BOOT_SWAP_TYPE_PERM:
940 case BOOT_SWAP_TYPE_REVERT:
David Vincze2d736ad2019-02-18 11:50:22 +0100941 /* Boot loader wants to switch to the secondary slot.
942 * Ensure image is valid.
943 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300944 if (boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) != 0) {
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300945 swap_type = BOOT_SWAP_TYPE_FAIL;
946 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800947 }
948
949 return swap_type;
950}
951
952/**
953 * Calculates the number of sectors the scratch area can contain. A "last"
954 * source sector is specified because images are copied backwards in flash
955 * (final index to index number 0).
956 *
957 * @param last_sector_idx The index of the last source sector
958 * (inclusive).
959 * @param out_first_sector_idx The index of the first source sector
960 * (inclusive) gets written here.
961 *
962 * @return The number of bytes comprised by the
963 * [first-sector, last-sector] range.
964 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300965#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800966static uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300967boot_copy_sz(struct boot_loader_state *state, int last_sector_idx,
968 int *out_first_sector_idx)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800969{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400970 size_t scratch_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800971 uint32_t new_sz;
972 uint32_t sz;
973 int i;
974
975 sz = 0;
976
Fabio Utzig10ee6482019-08-01 12:04:52 -0300977 scratch_sz = boot_scratch_area_size(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800978 for (i = last_sector_idx; i >= 0; i--) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300979 new_sz = sz + boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200980 /*
David Vincze2d736ad2019-02-18 11:50:22 +0100981 * The secondary slot is not being checked here, because
982 * `boot_slots_compatible` already provides assurance that the copy size
983 * will be compatible with the primary slot and scratch.
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200984 */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400985 if (new_sz > scratch_sz) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800986 break;
987 }
988 sz = new_sz;
989 }
990
991 /* i currently refers to a sector that doesn't fit or it is -1 because all
992 * sectors have been processed. In both cases, exclude sector i.
993 */
994 *out_first_sector_idx = i + 1;
995 return sz;
996}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300997#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800998
999/**
1000 * Erases a region of flash.
1001 *
Fabio Utzigba829042018-09-18 08:29:34 -03001002 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001003 * @param off The offset within the flash area to start the
1004 * erase.
1005 * @param sz The number of bytes to erase.
1006 *
1007 * @return 0 on success; nonzero on failure.
1008 */
Fabio Utzigba829042018-09-18 08:29:34 -03001009static inline int
1010boot_erase_sector(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001011{
Fabio Utzigba829042018-09-18 08:29:34 -03001012 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001013}
1014
1015/**
1016 * Copies the contents of one flash region to another. You must erase the
1017 * destination region prior to calling this function.
1018 *
1019 * @param flash_area_id_src The ID of the source flash area.
1020 * @param flash_area_id_dst The ID of the destination flash area.
1021 * @param off_src The offset within the source flash area to
1022 * copy from.
1023 * @param off_dst The offset within the destination flash area to
1024 * copy to.
1025 * @param sz The number of bytes to copy.
1026 *
1027 * @return 0 on success; nonzero on failure.
1028 */
1029static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001030boot_copy_sector(struct boot_loader_state *state,
1031 const struct flash_area *fap_src,
Fabio Utzigba829042018-09-18 08:29:34 -03001032 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -08001033 uint32_t off_src, uint32_t off_dst, uint32_t sz)
1034{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001035 uint32_t bytes_copied;
1036 int chunk_sz;
1037 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -03001038#ifdef MCUBOOT_ENC_IMAGES
1039 uint32_t off;
1040 size_t blk_off;
1041 struct image_header *hdr;
1042 uint16_t idx;
1043 uint32_t blk_sz;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001044 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -03001045#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001046
Fabio Utzig10ee6482019-08-01 12:04:52 -03001047 TARGET_STATIC uint8_t buf[1024];
1048
1049#if !defined(MCUBOOT_ENC_IMAGES)
1050 (void)state;
1051#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001052
Christopher Collins92ea77f2016-12-12 15:59:26 -08001053 bytes_copied = 0;
1054 while (bytes_copied < sz) {
1055 if (sz - bytes_copied > sizeof buf) {
1056 chunk_sz = sizeof buf;
1057 } else {
1058 chunk_sz = sz - bytes_copied;
1059 }
1060
1061 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
1062 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001063 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001064 }
1065
Fabio Utzigba829042018-09-18 08:29:34 -03001066#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001067 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001068 if (fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) ||
1069 fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001070 /* assume the secondary slot as src, needs decryption */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001071 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001072 off = off_src;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001073 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001074 /* might need encryption (metadata from the primary slot) */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001075 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001076 off = off_dst;
1077 }
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001078 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001079 blk_sz = chunk_sz;
1080 idx = 0;
1081 if (off + bytes_copied < hdr->ih_hdr_size) {
1082 /* do not decrypt header */
1083 blk_off = 0;
1084 blk_sz = chunk_sz - hdr->ih_hdr_size;
1085 idx = hdr->ih_hdr_size;
1086 } else {
1087 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
1088 }
Fabio Utzigc9621352019-08-08 12:15:51 -03001089 if (off + bytes_copied + chunk_sz > BOOT_TLV_OFF(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001090 /* do not decrypt TLVs */
Fabio Utzigc9621352019-08-08 12:15:51 -03001091 if (off + bytes_copied >= BOOT_TLV_OFF(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001092 blk_sz = 0;
1093 } else {
Fabio Utzigc9621352019-08-08 12:15:51 -03001094 blk_sz = BOOT_TLV_OFF(hdr) - (off + bytes_copied);
Fabio Utzigba829042018-09-18 08:29:34 -03001095 }
1096 }
Fabio Utzig10ee6482019-08-01 12:04:52 -03001097 boot_encrypt(state->enc, image_index, fap_src,
Fabio Utzigb0f04732019-07-31 09:49:19 -03001098 (off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
1099 blk_off, &buf[idx]);
Fabio Utzigba829042018-09-18 08:29:34 -03001100 }
1101 }
1102#endif
1103
Christopher Collins92ea77f2016-12-12 15:59:26 -08001104 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
1105 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001106 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001107 }
1108
1109 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -03001110
1111 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -08001112 }
1113
Fabio Utzigba829042018-09-18 08:29:34 -03001114 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001115}
1116
David Brown6b1b3b92017-09-19 08:59:10 -06001117#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -03001118static inline int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001119boot_status_init(const struct boot_loader_state *state,
1120 const struct flash_area *fap,
1121 const struct boot_status *bs)
Fabio Utzig2473ac02017-05-02 12:45:02 -03001122{
Fabio Utzig2473ac02017-05-02 12:45:02 -03001123 struct boot_swap_state swap_state;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001124 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001125 int rc;
1126
Fabio Utzig10ee6482019-08-01 12:04:52 -03001127#if (BOOT_IMAGE_NUMBER == 1)
1128 (void)state;
1129#endif
1130
1131 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001132
Christopher Collins2c88e692019-05-22 15:10:14 -07001133 BOOT_LOG_DBG("initializing status; fa_id=%d", fap->fa_id);
1134
Fabio Utzigb0f04732019-07-31 09:49:19 -03001135 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
1136 &swap_state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001137 assert(rc == 0);
1138
Christopher Collinsa1c12042019-05-23 14:00:28 -07001139 if (bs->swap_type != BOOT_SWAP_TYPE_NONE) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03001140 rc = boot_write_swap_info(fap, bs->swap_type, image_index);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001141 assert(rc == 0);
1142 }
1143
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001144 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001145 rc = boot_write_image_ok(fap);
1146 assert(rc == 0);
1147 }
1148
Fabio Utzig46490722017-09-04 15:34:32 -03001149 rc = boot_write_swap_size(fap, bs->swap_size);
1150 assert(rc == 0);
1151
Fabio Utzigba829042018-09-18 08:29:34 -03001152#ifdef MCUBOOT_ENC_IMAGES
1153 rc = boot_write_enc_key(fap, 0, bs->enckey[0]);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001154 assert(rc == 0);
1155
Fabio Utzigba829042018-09-18 08:29:34 -03001156 rc = boot_write_enc_key(fap, 1, bs->enckey[1]);
1157 assert(rc == 0);
1158#endif
1159
1160 rc = boot_write_magic(fap);
1161 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001162
1163 return 0;
1164}
Christopher Collinsa1c12042019-05-23 14:00:28 -07001165
David Brown6b1b3b92017-09-19 08:59:10 -06001166#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001167
Fabio Utzig358c9352017-07-25 22:10:45 -03001168#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -03001169static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001170boot_erase_trailer_sectors(const struct boot_loader_state *state,
1171 const struct flash_area *fap)
Fabio Utzig2473ac02017-05-02 12:45:02 -03001172{
1173 uint8_t slot;
Fabio Utziged0ca432019-01-23 14:50:11 -02001174 uint32_t sector;
1175 uint32_t trailer_sz;
1176 uint32_t total_sz;
1177 uint32_t off;
1178 uint32_t sz;
David Vinczeb75c12a2019-03-22 14:58:33 +01001179 int fa_id_primary;
1180 int fa_id_secondary;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001181 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001182 int rc;
1183
Christopher Collins2c88e692019-05-22 15:10:14 -07001184 BOOT_LOG_DBG("erasing trailer; fa_id=%d", fap->fa_id);
1185
Fabio Utzig10ee6482019-08-01 12:04:52 -03001186 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001187 fa_id_primary = flash_area_id_from_multi_image_slot(image_index,
1188 BOOT_PRIMARY_SLOT);
1189 fa_id_secondary = flash_area_id_from_multi_image_slot(image_index,
1190 BOOT_SECONDARY_SLOT);
David Vinczeb75c12a2019-03-22 14:58:33 +01001191
1192 if (fap->fa_id == fa_id_primary) {
David Vincze2d736ad2019-02-18 11:50:22 +01001193 slot = BOOT_PRIMARY_SLOT;
David Vinczeb75c12a2019-03-22 14:58:33 +01001194 } else if (fap->fa_id == fa_id_secondary) {
David Vincze2d736ad2019-02-18 11:50:22 +01001195 slot = BOOT_SECONDARY_SLOT;
David Vinczeb75c12a2019-03-22 14:58:33 +01001196 } else {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001197 return BOOT_EFLASH;
1198 }
1199
Fabio Utziged0ca432019-01-23 14:50:11 -02001200 /* delete starting from last sector and moving to beginning */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001201 sector = boot_img_num_sectors(state, slot) - 1;
1202 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
Fabio Utziged0ca432019-01-23 14:50:11 -02001203 total_sz = 0;
1204 do {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001205 sz = boot_img_sector_size(state, slot, sector);
1206 off = boot_img_sector_off(state, slot, sector);
Fabio Utziged0ca432019-01-23 14:50:11 -02001207 rc = boot_erase_sector(fap, off, sz);
1208 assert(rc == 0);
1209
1210 sector--;
1211 total_sz += sz;
1212 } while (total_sz < trailer_sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001213
1214 return rc;
1215}
Fabio Utzig358c9352017-07-25 22:10:45 -03001216#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001217
Christopher Collins92ea77f2016-12-12 15:59:26 -08001218/**
1219 * Swaps the contents of two flash regions within the two image slots.
1220 *
1221 * @param idx The index of the first sector in the range of
1222 * sectors being swapped.
1223 * @param sz The number of bytes to swap.
1224 * @param bs The current boot status. This struct gets
1225 * updated according to the outcome.
1226 *
1227 * @return 0 on success; nonzero on failure.
1228 */
Fabio Utzig3488eef2017-06-12 10:25:43 -03001229#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -08001230static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001231boot_swap_sectors(int idx, uint32_t sz, struct boot_loader_state *state,
1232 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001233{
David Vincze2d736ad2019-02-18 11:50:22 +01001234 const struct flash_area *fap_primary_slot;
1235 const struct flash_area *fap_secondary_slot;
Fabio Utzigba829042018-09-18 08:29:34 -03001236 const struct flash_area *fap_scratch;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001237 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001238 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001239 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001240 uint32_t scratch_trailer_off;
1241 struct boot_swap_state swap_state;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001242 size_t last_sector;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001243 bool erase_scratch;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001244 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001245 int rc;
1246
1247 /* Calculate offset from start of image area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001248 img_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001249
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001250 copy_sz = sz;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001251 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
Fabio Utzig9678c972017-05-23 11:28:56 -04001252
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001253 /* sz in this function is always sized on a multiple of the sector size.
1254 * The check against the start offset of the last sector
Fabio Utzig9678c972017-05-23 11:28:56 -04001255 * is to determine if we're swapping the last sector. The last sector
1256 * needs special handling because it's where the trailer lives. If we're
1257 * copying it, we need to use scratch to write the trailer temporarily.
1258 *
1259 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
1260 * controls if special handling is needed (swapping last sector).
1261 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001262 last_sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
David Vinczeba3bd602019-06-17 16:01:43 +02001263 if ((img_off + sz) >
Fabio Utzig10ee6482019-08-01 12:04:52 -03001264 boot_img_sector_off(state, BOOT_PRIMARY_SLOT, last_sector)) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001265 copy_sz -= trailer_sz;
1266 }
1267
Fabio Utzig39000012018-07-30 12:40:20 -03001268 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001269
Fabio Utzig10ee6482019-08-01 12:04:52 -03001270 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001271
1272 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1273 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001274 assert (rc == 0);
1275
Fabio Utzigb0f04732019-07-31 09:49:19 -03001276 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1277 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001278 assert (rc == 0);
1279
1280 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch);
1281 assert (rc == 0);
1282
Fabio Utzig39000012018-07-30 12:40:20 -03001283 if (bs->state == BOOT_STATUS_STATE_0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001284 BOOT_LOG_DBG("erasing scratch area");
Christopher Collinsa1c12042019-05-23 14:00:28 -07001285 rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size);
Christopher Collins4772ac42017-02-27 20:08:01 -08001286 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001287
Fabio Utzig39000012018-07-30 12:40:20 -03001288 if (bs->idx == BOOT_STATUS_IDX_0) {
Christopher Collinsa1c12042019-05-23 14:00:28 -07001289 /* Write a trailer to the scratch area, even if we don't need the
1290 * scratch area for status. We need a temporary place to store the
1291 * `swap-type` while we erase the primary trailer.
1292 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001293 rc = boot_status_init(state, fap_scratch, bs);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001294 assert(rc == 0);
1295
1296 if (!bs->use_scratch) {
1297 /* Prepare the primary status area... here it is known that the
1298 * last sector is not being used by the image data so it's safe
1299 * to erase.
Fabio Utzig2473ac02017-05-02 12:45:02 -03001300 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001301 rc = boot_erase_trailer_sectors(state, fap_primary_slot);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001302 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001303
Fabio Utzig10ee6482019-08-01 12:04:52 -03001304 rc = boot_status_init(state, fap_primary_slot, bs);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001305 assert(rc == 0);
1306
1307 /* Erase the temporary trailer from the scratch area. */
1308 rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size);
1309 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001310 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001311 }
1312
Fabio Utzig10ee6482019-08-01 12:04:52 -03001313 rc = boot_copy_sector(state, fap_secondary_slot, fap_scratch,
Christopher Collinsa1c12042019-05-23 14:00:28 -07001314 img_off, 0, copy_sz);
1315 assert(rc == 0);
1316
Fabio Utzig39000012018-07-30 12:40:20 -03001317 bs->state = BOOT_STATUS_STATE_1;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001318 rc = boot_write_status(state, bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001319 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001320 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001321
Fabio Utzig39000012018-07-30 12:40:20 -03001322 if (bs->state == BOOT_STATUS_STATE_1) {
David Vincze2d736ad2019-02-18 11:50:22 +01001323 rc = boot_erase_sector(fap_secondary_slot, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001324 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001325
Fabio Utzig10ee6482019-08-01 12:04:52 -03001326 rc = boot_copy_sector(state, fap_primary_slot, fap_secondary_slot,
David Vincze2d736ad2019-02-18 11:50:22 +01001327 img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001328 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001329
Fabio Utzig39000012018-07-30 12:40:20 -03001330 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001331 /* If not all sectors of the slot are being swapped,
David Vincze2d736ad2019-02-18 11:50:22 +01001332 * guarantee here that only the primary slot will have the state.
Fabio Utzig2473ac02017-05-02 12:45:02 -03001333 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001334 rc = boot_erase_trailer_sectors(state, fap_secondary_slot);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001335 assert(rc == 0);
1336 }
1337
Fabio Utzig39000012018-07-30 12:40:20 -03001338 bs->state = BOOT_STATUS_STATE_2;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001339 rc = boot_write_status(state, bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001340 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001341 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001342
Fabio Utzig39000012018-07-30 12:40:20 -03001343 if (bs->state == BOOT_STATUS_STATE_2) {
David Vincze2d736ad2019-02-18 11:50:22 +01001344 rc = boot_erase_sector(fap_primary_slot, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001345 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001346
Christopher Collinsa1c12042019-05-23 14:00:28 -07001347 /* NOTE: If this is the final sector, we exclude the image trailer from
1348 * this copy (copy_sz was truncated earlier).
1349 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001350 rc = boot_copy_sector(state, fap_scratch, fap_primary_slot,
David Vincze2d736ad2019-02-18 11:50:22 +01001351 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001352 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001353
Fabio Utzig94d998c2017-05-22 11:02:41 -04001354 if (bs->use_scratch) {
Fabio Utzigba829042018-09-18 08:29:34 -03001355 scratch_trailer_off = boot_status_off(fap_scratch);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001356
1357 /* copy current status that is being maintained in scratch */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001358 rc = boot_copy_sector(state, fap_scratch, fap_primary_slot,
David Vincze2d736ad2019-02-18 11:50:22 +01001359 scratch_trailer_off, img_off + copy_sz,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001360 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(state));
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001361 BOOT_STATUS_ASSERT(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001362
Fabio Utzig2473ac02017-05-02 12:45:02 -03001363 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1364 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001365 assert(rc == 0);
1366
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001367 if (swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze2d736ad2019-02-18 11:50:22 +01001368 rc = boot_write_image_ok(fap_primary_slot);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001369 assert(rc == 0);
1370 }
1371
Christopher Collinsa1c12042019-05-23 14:00:28 -07001372 if (swap_state.swap_type != BOOT_SWAP_TYPE_NONE) {
David Vinczee2453472019-06-17 12:31:59 +02001373 rc = boot_write_swap_info(fap_primary_slot,
Fabio Utzigb0f04732019-07-31 09:49:19 -03001374 swap_state.swap_type, image_index);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001375 assert(rc == 0);
1376 }
1377
David Vincze2d736ad2019-02-18 11:50:22 +01001378 rc = boot_write_swap_size(fap_primary_slot, bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001379 assert(rc == 0);
1380
Fabio Utzigba829042018-09-18 08:29:34 -03001381#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +01001382 rc = boot_write_enc_key(fap_primary_slot, 0, bs->enckey[0]);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001383 assert(rc == 0);
1384
David Vincze2d736ad2019-02-18 11:50:22 +01001385 rc = boot_write_enc_key(fap_primary_slot, 1, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001386 assert(rc == 0);
1387#endif
David Vincze2d736ad2019-02-18 11:50:22 +01001388 rc = boot_write_magic(fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001389 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001390 }
1391
Christopher Collinsa1c12042019-05-23 14:00:28 -07001392 /* If we wrote a trailer to the scratch area, erase it after we persist
1393 * a trailer to the primary slot. We do this to prevent mcuboot from
1394 * reading a stale status from the scratch area in case of immediate
1395 * reset.
1396 */
1397 erase_scratch = bs->use_scratch;
1398 bs->use_scratch = 0;
1399
Christopher Collins92ea77f2016-12-12 15:59:26 -08001400 bs->idx++;
Fabio Utzig39000012018-07-30 12:40:20 -03001401 bs->state = BOOT_STATUS_STATE_0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001402 rc = boot_write_status(state, bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001403 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001404
1405 if (erase_scratch) {
1406 rc = boot_erase_sector(fap_scratch, 0, sz);
1407 assert(rc == 0);
1408 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001409 }
Fabio Utzigba829042018-09-18 08:29:34 -03001410
David Vincze2d736ad2019-02-18 11:50:22 +01001411 flash_area_close(fap_primary_slot);
1412 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001413 flash_area_close(fap_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001414}
Fabio Utzig3488eef2017-06-12 10:25:43 -03001415#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001416
1417/**
David Vincze2d736ad2019-02-18 11:50:22 +01001418 * Overwrite primary slot with the image contained in the secondary slot.
1419 * If a prior copy operation was interrupted by a system reset, this function
1420 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001421 *
1422 * @param bs The current boot status. This function reads
1423 * this struct to determine if it is resuming
1424 * an interrupted swap operation. This
1425 * function writes the updated status to this
1426 * function on return.
1427 *
1428 * @return 0 on success; nonzero on failure.
1429 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001430#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001431static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001432boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
David Brown17609d82017-05-05 09:41:34 -06001433{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001434 size_t sect_count;
1435 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001436 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001437 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001438 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001439 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +01001440 const struct flash_area *fap_primary_slot;
1441 const struct flash_area *fap_secondary_slot;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001442 uint8_t image_index;
David Vincze2d736ad2019-02-18 11:50:22 +01001443
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001444 (void)bs;
1445
Fabio Utzig13d9e352017-10-05 20:32:31 -03001446#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1447 uint32_t src_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001448 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT,
1449 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
David Vincze2d736ad2019-02-18 11:50:22 +01001450 &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001451 assert(rc == 0);
1452#endif
David Brown17609d82017-05-05 09:41:34 -06001453
David Vincze2d736ad2019-02-18 11:50:22 +01001454 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1455 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -06001456
Fabio Utzigb0f04732019-07-31 09:49:19 -03001457 image_index = BOOT_CURR_IMG(state);
1458
1459 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1460 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001461 assert (rc == 0);
1462
Fabio Utzigb0f04732019-07-31 09:49:19 -03001463 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1464 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001465 assert (rc == 0);
1466
Fabio Utzig10ee6482019-08-01 12:04:52 -03001467 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001468 for (sect = 0, size = 0; sect < sect_count; sect++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001469 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
David Vincze2d736ad2019-02-18 11:50:22 +01001470 rc = boot_erase_sector(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001471 assert(rc == 0);
1472
1473 size += this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001474
1475#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1476 if (size >= src_size) {
1477 break;
1478 }
1479#endif
David Brown17609d82017-05-05 09:41:34 -06001480 }
1481
Fabio Utzigba829042018-09-18 08:29:34 -03001482#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001483 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
1484 rc = boot_enc_load(state->enc, image_index,
1485 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
Fabio Utzigb0f04732019-07-31 09:49:19 -03001486 fap_secondary_slot, bs->enckey[1]);
David Vincze2d736ad2019-02-18 11:50:22 +01001487
Fabio Utzigba829042018-09-18 08:29:34 -03001488 if (rc < 0) {
1489 return BOOT_EBADIMAGE;
1490 }
Fabio Utzig10ee6482019-08-01 12:04:52 -03001491 if (rc == 0 && boot_enc_set_key(state->enc, 1, bs->enckey[1])) {
Fabio Utzigba829042018-09-18 08:29:34 -03001492 return BOOT_EBADIMAGE;
1493 }
1494 }
1495#endif
1496
David Vincze2d736ad2019-02-18 11:50:22 +01001497 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1498 size);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001499 rc = boot_copy_sector(state, fap_secondary_slot, fap_primary_slot, 0, 0, size);
David Brown17609d82017-05-05 09:41:34 -06001500
Fabio Utzig13d9e352017-10-05 20:32:31 -03001501 /*
1502 * Erases header and trailer. The trailer is erased because when a new
1503 * image is written without a trailer as is the case when using newt, the
1504 * trailer that was left might trigger a new upgrade.
1505 */
Christopher Collins2c88e692019-05-22 15:10:14 -07001506 BOOT_LOG_DBG("erasing secondary header");
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, 0),
1509 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001510 assert(rc == 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001511 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -07001512 BOOT_LOG_DBG("erasing secondary trailer");
David Vincze2d736ad2019-02-18 11:50:22 +01001513 rc = boot_erase_sector(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001514 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
1515 last_sector),
1516 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
1517 last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001518 assert(rc == 0);
1519
David Vincze2d736ad2019-02-18 11:50:22 +01001520 flash_area_close(fap_primary_slot);
1521 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001522
David Vincze2d736ad2019-02-18 11:50:22 +01001523 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001524
1525 return 0;
1526}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001527#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001528
Christopher Collinsa1c12042019-05-23 14:00:28 -07001529#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -03001530/**
1531 * Swaps the two images in flash. If a prior copy operation was interrupted
1532 * by a system reset, this function completes that operation.
1533 *
1534 * @param bs The current boot status. This function reads
1535 * this struct to determine if it is resuming
1536 * an interrupted swap operation. This
1537 * function writes the updated status to this
1538 * function on return.
1539 *
1540 * @return 0 on success; nonzero on failure.
1541 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001542static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001543boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001544{
1545 uint32_t sz;
1546 int first_sector_idx;
1547 int last_sector_idx;
David Vincze2d736ad2019-02-18 11:50:22 +01001548 int last_idx_secondary_slot;
Fabio Utzigcd5774b2017-11-29 10:18:26 -02001549 uint32_t swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001550 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001551#ifdef MCUBOOT_ENC_IMAGES
1552 const struct flash_area *fap;
1553 uint8_t slot;
1554 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001555#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001556 uint32_t size;
1557 uint32_t copy_size;
David Vincze2d736ad2019-02-18 11:50:22 +01001558 uint32_t primary_slot_size;
1559 uint32_t secondary_slot_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001560 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001561 int rc;
1562
1563 /* FIXME: just do this if asked by user? */
1564
1565 size = copy_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001566 image_index = BOOT_CURR_IMG(state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001567
Fabio Utzig39000012018-07-30 12:40:20 -03001568 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Fabio Utzig46490722017-09-04 15:34:32 -03001569 /*
1570 * No swap ever happened, so need to find the largest image which
1571 * will be used to determine the amount of sectors to swap.
1572 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001573 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001574 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001575 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, hdr, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001576 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001577 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001578
Fabio Utzigba829042018-09-18 08:29:34 -03001579#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001580 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001581 fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
1582 rc = boot_enc_load(state->enc, image_index, hdr, fap, bs->enckey[0]);
Fabio Utzigba829042018-09-18 08:29:34 -03001583 assert(rc >= 0);
1584
1585 if (rc == 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001586 rc = boot_enc_set_key(state->enc, 0, bs->enckey[0]);
Fabio Utzigba829042018-09-18 08:29:34 -03001587 assert(rc == 0);
1588 } else {
1589 rc = 0;
1590 }
1591 } else {
1592 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1593 }
1594#endif
1595
Fabio Utzig10ee6482019-08-01 12:04:52 -03001596 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001597 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001598 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, hdr, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001599 assert(rc == 0);
1600 }
1601
Fabio Utzigba829042018-09-18 08:29:34 -03001602#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001603 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001604 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001605 fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
1606 rc = boot_enc_load(state->enc, image_index, hdr, fap, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001607 assert(rc >= 0);
1608
1609 if (rc == 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001610 rc = boot_enc_set_key(state->enc, 1, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001611 assert(rc == 0);
1612 } else {
1613 rc = 0;
1614 }
1615 } else {
1616 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1617 }
1618#endif
1619
Fabio Utzig46490722017-09-04 15:34:32 -03001620 if (size > copy_size) {
1621 copy_size = size;
1622 }
1623
1624 bs->swap_size = copy_size;
1625 } else {
1626 /*
1627 * If a swap was under way, the swap_size should already be present
1628 * in the trailer...
1629 */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001630 rc = boot_read_swap_size(image_index, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001631 assert(rc == 0);
1632
1633 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001634
1635#ifdef MCUBOOT_ENC_IMAGES
1636 for (slot = 0; slot <= 1; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03001637 rc = boot_read_enc_key(image_index, slot, bs->enckey[slot]);
Fabio Utzigba829042018-09-18 08:29:34 -03001638 assert(rc == 0);
1639
1640 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001641 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001642 break;
1643 }
1644 }
1645
1646 if (i != BOOT_ENC_KEY_SIZE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001647 boot_enc_set_key(state->enc, slot, bs->enckey[slot]);
Fabio Utzigba829042018-09-18 08:29:34 -03001648 }
1649 }
1650#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001651 }
1652
David Vincze2d736ad2019-02-18 11:50:22 +01001653 primary_slot_size = 0;
1654 secondary_slot_size = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001655 last_sector_idx = 0;
David Vincze2d736ad2019-02-18 11:50:22 +01001656 last_idx_secondary_slot = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001657
1658 /*
1659 * Knowing the size of the largest image between both slots, here we
David Vincze2d736ad2019-02-18 11:50:22 +01001660 * find what is the last sector in the primary slot that needs swapping.
1661 * Since we already know that both slots are compatible, the secondary
1662 * slot's last sector is not really required after this check is finished.
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001663 */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001664 while (1) {
David Vincze2d736ad2019-02-18 11:50:22 +01001665 if ((primary_slot_size < copy_size) ||
1666 (primary_slot_size < secondary_slot_size)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001667 primary_slot_size += boot_img_sector_size(state,
David Vincze2d736ad2019-02-18 11:50:22 +01001668 BOOT_PRIMARY_SLOT,
1669 last_sector_idx);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001670 }
David Vincze2d736ad2019-02-18 11:50:22 +01001671 if ((secondary_slot_size < copy_size) ||
1672 (secondary_slot_size < primary_slot_size)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001673 secondary_slot_size += boot_img_sector_size(state,
David Vincze2d736ad2019-02-18 11:50:22 +01001674 BOOT_SECONDARY_SLOT,
1675 last_idx_secondary_slot);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001676 }
David Vincze2d736ad2019-02-18 11:50:22 +01001677 if (primary_slot_size >= copy_size &&
1678 secondary_slot_size >= copy_size &&
1679 primary_slot_size == secondary_slot_size) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001680 break;
1681 }
1682 last_sector_idx++;
David Vincze2d736ad2019-02-18 11:50:22 +01001683 last_idx_secondary_slot++;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001684 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001685
1686 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001687 while (last_sector_idx >= 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001688 sz = boot_copy_sz(state, last_sector_idx, &first_sector_idx);
Fabio Utzig39000012018-07-30 12:40:20 -03001689 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001690 boot_swap_sectors(first_sector_idx, sz, state, bs);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001691 }
1692
1693 last_sector_idx = first_sector_idx - 1;
1694 swap_idx++;
1695 }
1696
David Vincze2d736ad2019-02-18 11:50:22 +01001697#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001698 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001699 BOOT_LOG_WRN("%d status write fails performing the swap",
1700 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001701 }
1702#endif
1703
Christopher Collins92ea77f2016-12-12 15:59:26 -08001704 return 0;
1705}
David Brown17609d82017-05-05 09:41:34 -06001706#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001707
1708/**
David Vincze2d736ad2019-02-18 11:50:22 +01001709 * Marks the image in the primary slot as fully copied.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001710 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001711#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001712static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001713boot_set_copy_done(uint8_t image_index)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001714{
1715 const struct flash_area *fap;
1716 int rc;
1717
Fabio Utzigb0f04732019-07-31 09:49:19 -03001718 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1719 &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001720 if (rc != 0) {
1721 return BOOT_EFLASH;
1722 }
1723
1724 rc = boot_write_copy_done(fap);
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001725 flash_area_close(fap);
1726 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001727}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001728#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001729
1730/**
David Vincze2d736ad2019-02-18 11:50:22 +01001731 * Marks a reverted image in the primary slot as confirmed. This is necessary to
1732 * ensure the status bytes from the image revert operation don't get processed
1733 * on a subsequent boot.
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001734 *
1735 * NOTE: image_ok is tested before writing because if there's a valid permanent
David Vincze2d736ad2019-02-18 11:50:22 +01001736 * image installed on the primary slot and the new image to be upgrade to has a
1737 * bad sig, image_ok would be overwritten.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001738 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001739#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001740static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001741boot_set_image_ok(uint8_t image_index)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001742{
1743 const struct flash_area *fap;
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001744 struct boot_swap_state state;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001745 int rc;
1746
Fabio Utzigb0f04732019-07-31 09:49:19 -03001747 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1748 &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001749 if (rc != 0) {
1750 return BOOT_EFLASH;
1751 }
1752
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001753 rc = boot_read_swap_state(fap, &state);
1754 if (rc != 0) {
1755 rc = BOOT_EFLASH;
1756 goto out;
1757 }
1758
1759 if (state.image_ok == BOOT_FLAG_UNSET) {
1760 rc = boot_write_image_ok(fap);
1761 }
1762
1763out:
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001764 flash_area_close(fap);
1765 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001766}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001767#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001768
David Vinczee32483f2019-06-13 10:46:24 +02001769#if (BOOT_IMAGE_NUMBER > 1)
1770/**
1771 * Check the image dependency whether it is satisfied and modify
1772 * the swap type if necessary.
1773 *
1774 * @param dep Image dependency which has to be verified.
1775 *
1776 * @return 0 on success; nonzero on failure.
1777 */
1778static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001779boot_verify_single_dependency(struct boot_loader_state *state,
1780 struct image_dependency *dep)
David Vinczee32483f2019-06-13 10:46:24 +02001781{
1782 struct image_version *dep_version;
1783 size_t dep_slot;
1784 int rc;
1785
1786 /* Determine the source of the image which is the subject of
1787 * the dependency and get it's version. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001788 dep_slot = (state->swap_type[dep->image_id] != BOOT_SWAP_TYPE_NONE) ?
David Vinczee32483f2019-06-13 10:46:24 +02001789 BOOT_SECONDARY_SLOT : BOOT_PRIMARY_SLOT;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001790 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +02001791
1792 rc = boot_is_version_sufficient(&dep->image_min_version, dep_version);
1793 if (rc != 0) {
1794 /* Dependency not satisfied.
1795 * Modify the swap type to decrease the version number of the image
1796 * (which will be located in the primary slot after the boot process),
1797 * consequently the number of unsatisfied dependencies will be
1798 * decreased or remain the same.
1799 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001800 switch (BOOT_SWAP_TYPE(state)) {
David Vinczee32483f2019-06-13 10:46:24 +02001801 case BOOT_SWAP_TYPE_TEST:
1802 case BOOT_SWAP_TYPE_PERM:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001803 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczee32483f2019-06-13 10:46:24 +02001804 break;
1805 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001806 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczee32483f2019-06-13 10:46:24 +02001807 break;
1808 default:
1809 break;
1810 }
1811 }
1812
1813 return rc;
1814}
1815
1816/**
1817 * Read all dependency TLVs of an image from the flash and verify
1818 * one after another to see if they are all satisfied.
1819 *
1820 * @param slot Image slot number.
1821 *
1822 * @return 0 on success; nonzero on failure.
1823 */
1824static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001825boot_verify_all_dependency(struct boot_loader_state *state, uint32_t slot)
David Vinczee32483f2019-06-13 10:46:24 +02001826{
1827 const struct flash_area *fap;
1828 struct image_header *hdr;
1829 struct image_tlv_info info;
1830 struct image_tlv tlv;
1831 struct image_dependency dep;
1832 uint32_t off;
1833 uint32_t end;
1834 bool dep_tlvs_found = false;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001835 int area_id;
David Vinczee32483f2019-06-13 10:46:24 +02001836 int rc;
1837
Fabio Utzig10ee6482019-08-01 12:04:52 -03001838 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001839 rc = flash_area_open(area_id, &fap);
David Vinczee32483f2019-06-13 10:46:24 +02001840 if (rc != 0) {
1841 rc = BOOT_EFLASH;
1842 goto done;
1843 }
1844
Fabio Utzig10ee6482019-08-01 12:04:52 -03001845 hdr = boot_img_hdr(state, slot);
Fabio Utzigc9621352019-08-08 12:15:51 -03001846 off = BOOT_TLV_OFF(hdr);
David Vinczee32483f2019-06-13 10:46:24 +02001847
1848 /* The TLV area always starts with an image_tlv_info structure. */
1849 rc = flash_area_read(fap, off, &info, sizeof(info));
1850 if (rc != 0) {
1851 rc = BOOT_EFLASH;
1852 goto done;
1853 }
1854
1855 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
1856 rc = BOOT_EBADIMAGE;
1857 goto done;
1858 }
1859 end = off + info.it_tlv_tot;
1860 off += sizeof(info);
1861
1862 /* Traverse through all of the TLVs to find the dependency TLVs. */
1863 for (; off < end; off += sizeof(tlv) + tlv.it_len) {
1864 rc = flash_area_read(fap, off, &tlv, sizeof(tlv));
1865 if (rc != 0) {
1866 rc = BOOT_EFLASH;
1867 goto done;
1868 }
1869
1870 if (tlv.it_type == IMAGE_TLV_DEPENDENCY) {
1871 if (!dep_tlvs_found) {
1872 dep_tlvs_found = true;
1873 }
1874
1875 if (tlv.it_len != sizeof(dep)) {
1876 rc = BOOT_EBADIMAGE;
1877 goto done;
1878 }
1879
1880 rc = flash_area_read(fap, off + sizeof(tlv), &dep, tlv.it_len);
1881 if (rc != 0) {
1882 rc = BOOT_EFLASH;
1883 goto done;
1884 }
1885
1886 /* Verify dependency and modify the swap type if not satisfied. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001887 rc = boot_verify_single_dependency(state, &dep);
David Vinczee32483f2019-06-13 10:46:24 +02001888 if (rc != 0) {
1889 /* Dependency not satisfied. */
1890 goto done;
1891 }
1892
1893 /* Dependency satisfied, no action needed.
1894 * Continue with the next TLV entry.
1895 */
1896 } else if (dep_tlvs_found) {
1897 /* The dependency TLVs are contiguous in the TLV area. If a
1898 * dependency had already been found and the last read TLV
1899 * has a different type then there are no more dependency TLVs.
1900 * The search can be finished.
1901 */
1902 break;
1903 }
1904 }
1905
1906done:
1907 flash_area_close(fap);
1908 return rc;
1909}
1910
1911/**
1912 * Verify whether the image dependencies in the TLV area are
1913 * all satisfied and modify the swap type if necessary.
1914 *
1915 * @return 0 if all dependencies are satisfied,
1916 * nonzero otherwise.
1917 */
1918static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001919boot_verify_single_image_dependency(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001920{
1921 size_t slot;
1922
1923 /* Determine the source of the dependency TLVs. Those dependencies have to
1924 * be checked which belong to the image that will be located in the primary
1925 * slot after the firmware update process.
1926 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001927 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1928 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
David Vinczee32483f2019-06-13 10:46:24 +02001929 slot = BOOT_SECONDARY_SLOT;
1930 } else {
1931 slot = BOOT_PRIMARY_SLOT;
1932 }
1933
Fabio Utzig10ee6482019-08-01 12:04:52 -03001934 return boot_verify_all_dependency(state, slot);
David Vinczee32483f2019-06-13 10:46:24 +02001935}
1936
1937/**
1938 * Iterate over all the images and verify whether the image dependencies in the
1939 * TLV area are all satisfied and update the related swap type if necessary.
1940 */
1941static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001942boot_verify_all_image_dependency(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001943{
David Vinczee32483f2019-06-13 10:46:24 +02001944 int rc;
1945
Fabio Utzig10ee6482019-08-01 12:04:52 -03001946 BOOT_CURR_IMG(state) = 0;
1947 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
1948 rc = boot_verify_single_image_dependency(state);
Fabio Utzigabec0732019-07-31 08:40:22 -03001949 if (rc == 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001950 /* All dependencies've been satisfied, continue with next image. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001951 BOOT_CURR_IMG(state)++;
David Vinczee32483f2019-06-13 10:46:24 +02001952 } else if (rc == BOOT_EBADVERSION) {
1953 /* Dependency check needs to be restarted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001954 BOOT_CURR_IMG(state) = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001955 } else {
1956 /* Other error happened, images are inconsistent */
1957 return;
1958 }
1959 }
1960}
1961#endif /* (BOOT_IMAGE_NUMBER > 1) */
1962
Christopher Collins92ea77f2016-12-12 15:59:26 -08001963/**
David Vinczeba3bd602019-06-17 16:01:43 +02001964 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001965 *
David Vinczeba3bd602019-06-17 16:01:43 +02001966 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001967 *
1968 * @return 0 on success; nonzero on failure.
1969 */
1970static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001971boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001972{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001973 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001974#ifndef MCUBOOT_OVERWRITE_ONLY
1975 uint8_t swap_type;
1976#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001977
David Vinczeba3bd602019-06-17 16:01:43 +02001978 /* At this point there are no aborted swaps. */
1979#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001980 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001981#elif defined(MCUBOOT_BOOTSTRAP)
1982 /* Check if the image update was triggered by a bad image in the
1983 * primary slot (the validity of the image in the secondary slot had
1984 * already been checked).
1985 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001986 if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 ||
1987 boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) {
1988 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001989 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001990 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001991 }
1992#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03001993 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001994#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001995 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001996
1997#ifndef MCUBOOT_OVERWRITE_ONLY
1998 /* The following state needs image_ok be explicitly set after the
1999 * swap was finished to avoid a new revert.
2000 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002001 swap_type = BOOT_SWAP_TYPE(state);
2002 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
2003 swap_type == BOOT_SWAP_TYPE_PERM) {
2004 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002005 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002006 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002007 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002008 }
2009
Fabio Utzig10ee6482019-08-01 12:04:52 -03002010 if (swap_type == BOOT_SWAP_TYPE_TEST ||
2011 swap_type == BOOT_SWAP_TYPE_PERM ||
2012 swap_type == BOOT_SWAP_TYPE_REVERT) {
2013 rc = boot_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002014 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002015 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07002016 }
David Vinczeba3bd602019-06-17 16:01:43 +02002017 }
2018#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02002019
David Vinczeba3bd602019-06-17 16:01:43 +02002020 return rc;
2021}
2022
2023/**
2024 * Completes a previously aborted image swap.
2025 *
2026 * @param bs The current boot status.
2027 *
2028 * @return 0 on success; nonzero on failure.
2029 */
2030#if !defined(MCUBOOT_OVERWRITE_ONLY)
2031static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03002032boot_complete_partial_swap(struct boot_loader_state *state,
2033 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02002034{
2035 int rc;
2036
2037 /* Determine the type of swap operation being resumed from the
2038 * `swap-type` trailer field.
2039 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002040 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002041 assert(rc == 0);
2042
Fabio Utzig10ee6482019-08-01 12:04:52 -03002043 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02002044
2045 /* The following states need image_ok be explicitly set after the
2046 * swap was finished to avoid a new revert.
2047 */
2048 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
2049 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002050 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002051 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002052 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002053 }
2054 }
2055
2056 if (bs->swap_type == BOOT_SWAP_TYPE_TEST ||
2057 bs->swap_type == BOOT_SWAP_TYPE_PERM ||
2058 bs->swap_type == BOOT_SWAP_TYPE_REVERT) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002059 rc = boot_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002060 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002061 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002062 }
2063 }
2064
Fabio Utzig10ee6482019-08-01 12:04:52 -03002065 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002066 BOOT_LOG_ERR("panic!");
2067 assert(0);
2068
2069 /* Loop forever... */
2070 while (1) {}
2071 }
2072
2073 return rc;
2074}
2075#endif /* !MCUBOOT_OVERWRITE_ONLY */
2076
2077#if (BOOT_IMAGE_NUMBER > 1)
2078/**
2079 * Review the validity of previously determined swap types of other images.
2080 *
2081 * @param aborted_swap The current image upgrade is a
2082 * partial/aborted swap.
2083 */
2084static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03002085boot_review_image_swap_types(struct boot_loader_state *state,
2086 bool aborted_swap)
David Vinczeba3bd602019-06-17 16:01:43 +02002087{
2088 /* In that case if we rebooted in the middle of an image upgrade process, we
2089 * must review the validity of swap types, that were previously determined
2090 * for other images. The image_ok flag had not been set before the reboot
2091 * for any of the updated images (only the copy_done flag) and thus falsely
2092 * the REVERT swap type has been determined for the previous images that had
2093 * been updated before the reboot.
2094 *
2095 * There are two separate scenarios that we have to deal with:
2096 *
2097 * 1. The reboot has happened during swapping an image:
2098 * The current image upgrade has been determined as a
2099 * partial/aborted swap.
2100 * 2. The reboot has happened between two separate image upgrades:
2101 * In this scenario we must check the swap type of the current image.
2102 * In those cases if it is NONE or REVERT we cannot certainly determine
2103 * the fact of a reboot. In a consistent state images must move in the
2104 * same direction or stay in place, e.g. in practice REVERT and TEST
2105 * swap types cannot be present at the same time. If the swap type of
2106 * the current image is either TEST, PERM or FAIL we must review the
2107 * already determined swap types of other images and set each false
2108 * REVERT swap types to NONE (these images had been successfully
2109 * updated before the system rebooted between two separate image
2110 * upgrades).
2111 */
2112
Fabio Utzig10ee6482019-08-01 12:04:52 -03002113 if (BOOT_CURR_IMG(state) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02002114 /* Nothing to do */
2115 return;
2116 }
2117
2118 if (!aborted_swap) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002119 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
2120 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002121 /* Nothing to do */
2122 return;
2123 }
2124 }
2125
Fabio Utzig10ee6482019-08-01 12:04:52 -03002126 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
2127 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
2128 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002129 }
2130 }
2131}
2132#endif
2133
2134/**
2135 * Prepare image to be updated if required.
2136 *
2137 * Prepare image to be updated if required with completing an image swap
2138 * operation if one was aborted and/or determining the type of the
2139 * swap operation. In case of any error set the swap type to NONE.
2140 *
Fabio Utzig10ee6482019-08-01 12:04:52 -03002141 * @param state TODO
David Vinczeba3bd602019-06-17 16:01:43 +02002142 * @param bs Pointer where the read and possibly updated
2143 * boot status can be written to.
2144 */
2145static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03002146boot_prepare_image_for_update(struct boot_loader_state *state,
2147 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02002148{
2149 int rc;
2150
2151 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002152 rc = boot_read_sectors(state);
David Vinczeba3bd602019-06-17 16:01:43 +02002153 if (rc != 0) {
2154 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
2155 " - too small?", BOOT_MAX_IMG_SECTORS);
2156 /* Unable to determine sector layout, continue with next image
2157 * if there is one.
2158 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002159 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002160 return;
2161 }
2162
2163 /* Attempt to read an image header from each slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002164 rc = boot_read_image_headers(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02002165 if (rc != 0) {
2166 /* Continue with next image if there is one. */
Fabio Utzigb0f04732019-07-31 09:49:19 -03002167 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03002168 BOOT_CURR_IMG(state));
2169 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002170 return;
2171 }
2172
2173 /* If the current image's slots aren't compatible, no swap is possible.
2174 * Just boot into primary slot.
2175 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002176 if (boot_slots_compatible(state)) {
2177 rc = boot_read_status(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002178 if (rc != 0) {
2179 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03002180 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002181 /* Continue with next image if there is one. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002182 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002183 return;
2184 }
2185
2186 /* Determine if we rebooted in the middle of an image swap
2187 * operation. If a partial swap was detected, complete it.
2188 */
2189 if (bs->idx != BOOT_STATUS_IDX_0 || bs->state != BOOT_STATUS_STATE_0) {
2190
2191#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03002192 boot_review_image_swap_types(state, true);
David Vinczeba3bd602019-06-17 16:01:43 +02002193#endif
2194
2195#ifdef MCUBOOT_OVERWRITE_ONLY
2196 /* Should never arrive here, overwrite-only mode has
2197 * no swap state.
2198 */
2199 assert(0);
2200#else
2201 /* Determine the type of swap operation being resumed from the
2202 * `swap-type` trailer field.
2203 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002204 rc = boot_complete_partial_swap(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002205 assert(rc == 0);
2206#endif
2207 /* Attempt to read an image header from each slot. Ensure that
2208 * image headers in slots are aligned with headers in boot_data.
2209 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002210 rc = boot_read_image_headers(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02002211 assert(rc == 0);
2212
2213 /* Swap has finished set to NONE */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002214 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002215 } else {
2216 /* There was no partial swap, determine swap type. */
2217 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002218 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
2219 } else if (boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) != 0) {
2220 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
David Vinczeba3bd602019-06-17 16:01:43 +02002221 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002222 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02002223 }
2224
2225#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03002226 boot_review_image_swap_types(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02002227#endif
2228
2229#ifdef MCUBOOT_BOOTSTRAP
Fabio Utzig10ee6482019-08-01 12:04:52 -03002230 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02002231 /* Header checks are done first because they are
2232 * inexpensive. Since overwrite-only copies starting from
2233 * offset 0, if interrupted, it might leave a valid header
2234 * magic, so also run validation on the primary slot to be
2235 * sure it's not OK.
2236 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002237 if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 ||
2238 boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) {
2239 if (boot_img_hdr(state,
David Vinczeba3bd602019-06-17 16:01:43 +02002240 BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC &&
Fabio Utzig10ee6482019-08-01 12:04:52 -03002241 boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) == 0)
David Vinczeba3bd602019-06-17 16:01:43 +02002242 {
2243 /* Set swap type to REVERT to overwrite the primary
2244 * slot with the image contained in secondary slot
2245 * and to trigger the explicit setting of the
2246 * image_ok flag.
2247 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002248 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02002249 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02002250 }
2251 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02002252#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002253 }
David Vinczeba3bd602019-06-17 16:01:43 +02002254 } else {
2255 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002256 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002257 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002258}
2259
Christopher Collins92ea77f2016-12-12 15:59:26 -08002260int
Fabio Utzig10ee6482019-08-01 12:04:52 -03002261context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08002262{
Marti Bolivar84898652017-06-13 17:20:22 -04002263 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02002264 struct boot_status bs;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002265 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002266 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03002267 int image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002268
2269 /* The array of slot sectors are defined here (as opposed to file scope) so
2270 * that they don't get allocated for non-boot-loader apps. This is
2271 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002272 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08002273 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002274 TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
2275 TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
2276 TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Christopher Collins92ea77f2016-12-12 15:59:26 -08002277
Fabio Utzig10ee6482019-08-01 12:04:52 -03002278 memset(state, 0, sizeof(struct boot_loader_state));
Fabio Utzigba829042018-09-18 08:29:34 -03002279
David Vinczeba3bd602019-06-17 16:01:43 +02002280 /* Iterate over all the images. By the end of the loop the swap type has
2281 * to be determined for each image and all aborted swaps have to be
2282 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002283 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002284 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03002285
David Vinczeba3bd602019-06-17 16:01:43 +02002286#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
2287 /* The keys used for encryption may no longer be valid (could belong to
2288 * another images). Therefore, mark them as invalid to force their reload
2289 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03002290 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002291 boot_enc_mark_keys_invalid(state->enc);
David Brown554c52e2017-06-30 16:01:07 -06002292#endif
2293
Fabio Utzig10ee6482019-08-01 12:04:52 -03002294 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03002295
Fabio Utzig10ee6482019-08-01 12:04:52 -03002296 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002297 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03002298 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002299 secondary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03002300 state->scratch.sectors = scratch_sectors;
David Vinczeba3bd602019-06-17 16:01:43 +02002301
2302 /* Open primary and secondary image areas for the duration
2303 * of this call.
2304 */
2305 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03002306 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03002307 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02002308 assert(rc == 0);
2309 }
2310 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03002311 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002312 assert(rc == 0);
2313
2314 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002315 boot_prepare_image_for_update(state, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002316 }
2317
David Vinczee32483f2019-06-13 10:46:24 +02002318#if (BOOT_IMAGE_NUMBER > 1)
2319 /* Iterate over all the images and verify whether the image dependencies
2320 * are all satisfied and update swap type if necessary.
2321 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002322 boot_verify_all_image_dependency(state);
David Vinczee32483f2019-06-13 10:46:24 +02002323#endif
2324
David Vinczeba3bd602019-06-17 16:01:43 +02002325 /* Iterate over all the images. At this point there are no aborted swaps
2326 * and the swap types are determined for each image. By the end of the loop
2327 * all required update operations will have been finished.
2328 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002329 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002330
2331#if (BOOT_IMAGE_NUMBER > 1)
2332#ifdef MCUBOOT_ENC_IMAGES
2333 /* The keys used for encryption may no longer be valid (could belong to
2334 * another images). Therefore, mark them as invalid to force their reload
2335 * by boot_enc_load().
2336 */
2337 boot_enc_mark_keys_invalid();
2338#endif /* MCUBOOT_ENC_IMAGES */
2339
2340 /* Indicate that swap is not aborted */
2341 memset(&bs, 0, sizeof bs);
2342 bs.idx = BOOT_STATUS_IDX_0;
2343 bs.state = BOOT_STATUS_STATE_0;
2344#endif /* (BOOT_IMAGE_NUMBER > 1) */
2345
2346 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002347 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02002348
Fabio Utzig10ee6482019-08-01 12:04:52 -03002349 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002350 case BOOT_SWAP_TYPE_NONE:
2351 break;
2352
2353 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
2354 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
2355 case BOOT_SWAP_TYPE_REVERT:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002356 rc = boot_perform_update(state, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002357 assert(rc == 0);
2358 break;
2359
2360 case BOOT_SWAP_TYPE_FAIL:
2361 /* The image in secondary slot was invalid and is now erased. Ensure
2362 * we don't try to boot into it again on the next reboot. Do this by
2363 * pretending we just reverted back to primary slot.
2364 */
2365#ifndef MCUBOOT_OVERWRITE_ONLY
2366 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002367 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002368 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002369 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002370 }
2371#endif /* !MCUBOOT_OVERWRITE_ONLY */
2372 break;
2373
2374 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002375 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002376 }
2377
Fabio Utzig10ee6482019-08-01 12:04:52 -03002378 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002379 BOOT_LOG_ERR("panic!");
2380 assert(0);
2381
2382 /* Loop forever... */
2383 while (1) {}
2384 }
2385 }
2386
2387 /* Iterate over all the images. At this point all required update operations
2388 * have finished. By the end of the loop each image in the primary slot will
2389 * have been re-validated.
2390 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002391 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2392 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02002393 /* Attempt to read an image header from each slot. Ensure that image
2394 * headers in slots are aligned with headers in boot_data.
2395 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002396 rc = boot_read_image_headers(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02002397 if (rc != 0) {
2398 goto out;
2399 }
2400 /* Since headers were reloaded, it can be assumed we just performed
2401 * a swap or overwrite. Now the header info that should be used to
2402 * provide the data for the bootstrap, which previously was at
2403 * secondary slot, was updated to primary slot.
2404 */
2405 }
2406
2407#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig10ee6482019-08-01 12:04:52 -03002408 rc = boot_validate_slot(state, BOOT_PRIMARY_SLOT, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02002409 if (rc != 0) {
2410 rc = BOOT_EBADIMAGE;
2411 goto out;
2412 }
2413#else
2414 /* Even if we're not re-validating the primary slot, we could be booting
2415 * onto an empty flash chip. At least do a basic sanity check that
2416 * the magic number on the image is OK.
2417 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002418 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002419 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
Fabio Utzig10ee6482019-08-01 12:04:52 -03002420 &boot_img_hdr(state,BOOT_PRIMARY_SLOT)->ih_magic,
2421 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002422 rc = BOOT_EBADIMAGE;
2423 goto out;
2424 }
2425#endif
2426 }
2427
Fabio Utzigb0f04732019-07-31 09:49:19 -03002428#if (BOOT_IMAGE_NUMBER > 1)
David Vinczeba3bd602019-06-17 16:01:43 +02002429 /* Always boot from the primary slot of Image 0. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002430 BOOT_CURR_IMG(state) = 0;
Fabio Utzigb0f04732019-07-31 09:49:19 -03002431#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -03002432 rsp->br_flash_dev_id = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)->fa_device_id;
2433 rsp->br_image_off = boot_img_slot_off(state, BOOT_PRIMARY_SLOT);
2434 rsp->br_hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002435
Marti Bolivarc0b47912017-06-13 17:18:09 -04002436 out:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002437 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2438 flash_area_close(BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002439 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002440 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
David Vinczeba3bd602019-06-17 16:01:43 +02002441 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04002442 }
2443 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002444}
2445
Fabio Utzig10ee6482019-08-01 12:04:52 -03002446/**
2447 * Prepares the booting process. This function moves images around in flash as
2448 * appropriate, and tells you what address to boot from.
2449 *
2450 * @param rsp On success, indicates how booting should occur.
2451 *
2452 * @return 0 on success; nonzero on failure.
2453 */
2454int
2455boot_go(struct boot_rsp *rsp)
2456{
2457 return context_boot_go(&boot_data, rsp);
2458}
2459
Christopher Collins92ea77f2016-12-12 15:59:26 -08002460int
2461split_go(int loader_slot, int split_slot, void **entry)
2462{
Marti Bolivarc50926f2017-06-14 09:35:40 -04002463 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08002464 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002465 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002466 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002467 int rc;
2468
Christopher Collins92ea77f2016-12-12 15:59:26 -08002469 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
2470 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04002471 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002472 }
David Vinczeba3bd602019-06-17 16:01:43 +02002473 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
2474 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002475
2476 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
2477 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07002478 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04002479 assert(rc == 0);
2480 split_flash_id = flash_area_id_from_image_slot(split_slot);
2481 rc = flash_area_open(split_flash_id,
2482 &BOOT_IMG_AREA(&boot_data, split_slot));
2483 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002484
2485 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002486 rc = boot_read_sectors(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002487 if (rc != 0) {
2488 rc = SPLIT_GO_ERR;
2489 goto done;
2490 }
2491
Fabio Utzig10ee6482019-08-01 12:04:52 -03002492 rc = boot_read_image_headers(&boot_data, true);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002493 if (rc != 0) {
2494 goto done;
2495 }
2496
Christopher Collins92ea77f2016-12-12 15:59:26 -08002497 /* Don't check the bootable image flag because we could really call a
2498 * bootable or non-bootable image. Just validate that the image check
2499 * passes which is distinct from the normal check.
2500 */
Marti Bolivarf804f622017-06-12 15:41:48 -04002501 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04002502 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04002503 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04002504 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002505 if (rc != 0) {
2506 rc = SPLIT_GO_NON_MATCHING;
2507 goto done;
2508 }
2509
Marti Bolivarea088872017-06-12 17:10:49 -04002510 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04002511 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08002512 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002513 rc = SPLIT_GO_OK;
2514
2515done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04002516 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
2517 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002518 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002519 return rc;
2520}