blob: 3ee633564aa92bc6d92aa5a07f40b8eb8bd57d41 [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
David Vinczee2453472019-06-17 12:31:59 +020020/*
21 * Modifications are Copyright (c) 2019 Arm Limited.
22 */
23
Christopher Collins92ea77f2016-12-12 15:59:26 -080024/**
25 * This file provides an interface to the boot loader. Functions defined in
26 * this file should only be called while the boot loader is running.
27 */
28
29#include <assert.h>
30#include <stddef.h>
David Brown52eee562017-07-05 11:25:09 -060031#include <stdbool.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080032#include <inttypes.h>
33#include <stdlib.h>
34#include <string.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080035#include <os/os_malloc.h>
36#include "bootutil/bootutil.h"
37#include "bootutil/image.h"
38#include "bootutil_priv.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050039#include "bootutil/bootutil_log.h"
40
Fabio Utzigba829042018-09-18 08:29:34 -030041#ifdef MCUBOOT_ENC_IMAGES
42#include "bootutil/enc_key.h"
43#endif
44
Fabio Utzigba1fbe62017-07-21 14:01:20 -030045#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030046
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010047MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
48
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040049static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080050
Fabio Utzigabec0732019-07-31 08:40:22 -030051#if (BOOT_IMAGE_NUMBER > 1)
52#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
53#else
54#define IMAGES_ITER(x)
55#endif
56
Fabio Utzig10ee6482019-08-01 12:04:52 -030057/*
58 * This macro allows some control on the allocation of local variables.
59 * When running natively on a target, we don't want to allocated huge
60 * variables on the stack, so make them global instead. For the simulator
61 * we want to run as many threads as there are tests, and it's safer
62 * to just make those variables stack allocated.
63 */
64#if !defined(__BOOTSIM__)
65#define TARGET_STATIC static
66#else
67#define TARGET_STATIC
68#endif
69
David Vincze2d736ad2019-02-18 11:50:22 +010070#if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT) && !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -030071/*
72 * FIXME: this might have to be updated for threaded sim
73 */
Fabio Utziga0e1cce2017-11-23 20:04:01 -020074static int boot_status_fails = 0;
75#define BOOT_STATUS_ASSERT(x) \
76 do { \
Johann Fischered8461b2018-02-15 16:50:31 +010077 if (!(x)) { \
Fabio Utziga0e1cce2017-11-23 20:04:01 -020078 boot_status_fails++; \
79 } \
80 } while (0)
81#else
Fabio Utzig57c40f72017-12-12 21:48:30 -020082#define BOOT_STATUS_ASSERT(x) ASSERT(x)
Fabio Utziga0e1cce2017-11-23 20:04:01 -020083#endif
84
Christopher Collins92ea77f2016-12-12 15:59:26 -080085struct boot_status_table {
David Vincze2d736ad2019-02-18 11:50:22 +010086 uint8_t bst_magic_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -080087 uint8_t bst_magic_scratch;
David Vincze2d736ad2019-02-18 11:50:22 +010088 uint8_t bst_copy_done_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -080089 uint8_t bst_status_source;
90};
91
92/**
93 * This set of tables maps swap state contents to boot status location.
94 * When searching for a match, these tables must be iterated in order.
95 */
96static const struct boot_status_table boot_status_tables[] = {
97 {
David Vincze2d736ad2019-02-18 11:50:22 +010098 /* | primary slot | scratch |
99 * ----------+--------------+--------------|
100 * magic | Good | Any |
101 * copy-done | Set | N/A |
102 * ----------+--------------+--------------'
103 * source: none |
104 * ----------------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -0800105 */
David Vincze2d736ad2019-02-18 11:50:22 +0100106 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
Christopher Collinsa1c12042019-05-23 14:00:28 -0700107 .bst_magic_scratch = BOOT_MAGIC_NOTGOOD,
David Vincze2d736ad2019-02-18 11:50:22 +0100108 .bst_copy_done_primary_slot = BOOT_FLAG_SET,
109 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800110 },
111
112 {
David Vincze2d736ad2019-02-18 11:50:22 +0100113 /* | primary slot | scratch |
114 * ----------+--------------+--------------|
115 * magic | Good | Any |
116 * copy-done | Unset | N/A |
117 * ----------+--------------+--------------'
118 * source: primary slot |
119 * ----------------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -0800120 */
David Vincze2d736ad2019-02-18 11:50:22 +0100121 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
Christopher Collinsa1c12042019-05-23 14:00:28 -0700122 .bst_magic_scratch = BOOT_MAGIC_NOTGOOD,
David Vincze2d736ad2019-02-18 11:50:22 +0100123 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
124 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800125 },
126
127 {
David Vincze2d736ad2019-02-18 11:50:22 +0100128 /* | primary slot | scratch |
129 * ----------+--------------+--------------|
130 * magic | Any | Good |
131 * copy-done | Any | N/A |
132 * ----------+--------------+--------------'
133 * source: scratch |
134 * ----------------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -0800135 */
David Vincze2d736ad2019-02-18 11:50:22 +0100136 .bst_magic_primary_slot = BOOT_MAGIC_ANY,
137 .bst_magic_scratch = BOOT_MAGIC_GOOD,
138 .bst_copy_done_primary_slot = BOOT_FLAG_ANY,
139 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800140 },
Christopher Collins92ea77f2016-12-12 15:59:26 -0800141 {
David Vincze2d736ad2019-02-18 11:50:22 +0100142 /* | primary slot | scratch |
143 * ----------+--------------+--------------|
144 * magic | Unset | Any |
145 * copy-done | Unset | N/A |
146 * ----------+--------------+--------------|
147 * source: varies |
148 * ----------------------------------------+--------------------------+
Christopher Collins92ea77f2016-12-12 15:59:26 -0800149 * This represents one of two cases: |
150 * o No swaps ever (no status to read, so no harm in checking). |
David Vincze2d736ad2019-02-18 11:50:22 +0100151 * o Mid-revert; status in primary slot. |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800152 * -------------------------------------------------------------------'
153 */
David Vincze2d736ad2019-02-18 11:50:22 +0100154 .bst_magic_primary_slot = BOOT_MAGIC_UNSET,
155 .bst_magic_scratch = BOOT_MAGIC_ANY,
156 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
157 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800158 },
159};
160
161#define BOOT_STATUS_TABLES_COUNT \
162 (sizeof boot_status_tables / sizeof boot_status_tables[0])
163
Marti Bolivarfd20c762017-02-07 16:52:50 -0500164#define BOOT_LOG_SWAP_STATE(area, state) \
Christopher Collinsa1c12042019-05-23 14:00:28 -0700165 BOOT_LOG_INF("%s: magic=%s, swap_type=0x%x, copy_done=0x%x, " \
166 "image_ok=0x%x", \
Marti Bolivarfd20c762017-02-07 16:52:50 -0500167 (area), \
168 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
169 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
170 "bad"), \
Christopher Collinsa1c12042019-05-23 14:00:28 -0700171 (state)->swap_type, \
Marti Bolivarfd20c762017-02-07 16:52:50 -0500172 (state)->copy_done, \
173 (state)->image_ok)
174
Christopher Collins92ea77f2016-12-12 15:59:26 -0800175/**
David Vincze2d736ad2019-02-18 11:50:22 +0100176 * Determines where in flash the most recent boot status is stored. The boot
Christopher Collins92ea77f2016-12-12 15:59:26 -0800177 * status is necessary for completing a swap that was interrupted by a boot
178 * loader reset.
179 *
David Vincze2d736ad2019-02-18 11:50:22 +0100180 * @return A BOOT_STATUS_SOURCE_[...] code indicating where status should
181 * be read from.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800182 */
183static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300184boot_status_source(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800185{
186 const struct boot_status_table *table;
187 struct boot_swap_state state_scratch;
David Vincze2d736ad2019-02-18 11:50:22 +0100188 struct boot_swap_state state_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800189 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200190 size_t i;
Marti Bolivarfd20c762017-02-07 16:52:50 -0500191 uint8_t source;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300192 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800193
Fabio Utzigb0f04732019-07-31 09:49:19 -0300194#if (BOOT_IMAGE_NUMBER == 1)
195 (void)state;
196#endif
197
Fabio Utzig10ee6482019-08-01 12:04:52 -0300198 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300199 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
200 &state_primary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800201 assert(rc == 0);
202
Fabio Utzig2473ac02017-05-02 12:45:02 -0300203 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800204 assert(rc == 0);
205
David Vincze2d736ad2019-02-18 11:50:22 +0100206 BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot);
Marti Bolivarfd20c762017-02-07 16:52:50 -0500207 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
208
Christopher Collins92ea77f2016-12-12 15:59:26 -0800209 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300210 table = &boot_status_tables[i];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800211
Christopher Collinsa1c12042019-05-23 14:00:28 -0700212 if (boot_magic_compatible_check(table->bst_magic_primary_slot,
213 state_primary_slot.magic) &&
214 boot_magic_compatible_check(table->bst_magic_scratch,
215 state_scratch.magic) &&
David Vincze2d736ad2019-02-18 11:50:22 +0100216 (table->bst_copy_done_primary_slot == BOOT_FLAG_ANY ||
217 table->bst_copy_done_primary_slot == state_primary_slot.copy_done))
218 {
Marti Bolivarfd20c762017-02-07 16:52:50 -0500219 source = table->bst_status_source;
David Vinczeba3bd602019-06-17 16:01:43 +0200220
221#if (BOOT_IMAGE_NUMBER > 1)
222 /* In case of multi-image boot it can happen that if boot status
223 * info is found on scratch area then it does not belong to the
224 * currently examined image.
225 */
226 if (source == BOOT_STATUS_SOURCE_SCRATCH &&
Fabio Utzig10ee6482019-08-01 12:04:52 -0300227 state_scratch.image_num != BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +0200228 source = BOOT_STATUS_SOURCE_NONE;
229 }
230#endif
231
Marti Bolivarfd20c762017-02-07 16:52:50 -0500232 BOOT_LOG_INF("Boot source: %s",
233 source == BOOT_STATUS_SOURCE_NONE ? "none" :
234 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
David Vincze2d736ad2019-02-18 11:50:22 +0100235 source == BOOT_STATUS_SOURCE_PRIMARY_SLOT ?
236 "primary slot" : "BUG; can't happen");
Marti Bolivarfd20c762017-02-07 16:52:50 -0500237 return source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800238 }
239 }
240
Marti Bolivarfd20c762017-02-07 16:52:50 -0500241 BOOT_LOG_INF("Boot source: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800242 return BOOT_STATUS_SOURCE_NONE;
243}
244
David Brownf5b33d82017-09-01 10:58:27 -0600245/*
246 * Compute the total size of the given image. Includes the size of
247 * the TLVs.
248 */
Fabio Utzig13d9e352017-10-05 20:32:31 -0300249#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600250static int
Fabio Utzigd638b172019-08-09 10:38:05 -0300251boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
David Brownf5b33d82017-09-01 10:58:27 -0600252{
253 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300254 struct image_tlv_info info;
Fabio Utzig233af7d2019-08-26 12:06:16 -0300255 uint32_t off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300256 uint32_t protect_tlv_size;
David Brownf5b33d82017-09-01 10:58:27 -0600257 int area_id;
258 int rc;
259
Fabio Utzig10ee6482019-08-01 12:04:52 -0300260#if (BOOT_IMAGE_NUMBER == 1)
261 (void)state;
262#endif
263
264 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
David Brownf5b33d82017-09-01 10:58:27 -0600265 rc = flash_area_open(area_id, &fap);
266 if (rc != 0) {
267 rc = BOOT_EFLASH;
268 goto done;
269 }
270
Fabio Utzig61fd8882019-09-14 20:00:20 -0300271 off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
272
273 if (flash_area_read(fap, off, &info, sizeof(info))) {
274 rc = BOOT_EFLASH;
David Brownf5b33d82017-09-01 10:58:27 -0600275 goto done;
276 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300277
Fabio Utzige52c08e2019-09-11 19:32:00 -0300278 protect_tlv_size = boot_img_hdr(state, slot)->ih_protect_tlv_size;
279 if (info.it_magic == IMAGE_TLV_PROT_INFO_MAGIC) {
280 if (protect_tlv_size != info.it_tlv_tot) {
281 rc = BOOT_EBADIMAGE;
282 goto done;
283 }
284
285 if (flash_area_read(fap, off + info.it_tlv_tot, &info, sizeof(info))) {
286 rc = BOOT_EFLASH;
287 goto done;
288 }
289 } else if (protect_tlv_size != 0) {
290 rc = BOOT_EBADIMAGE;
291 goto done;
292 }
293
Fabio Utzig61fd8882019-09-14 20:00:20 -0300294 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
295 rc = BOOT_EBADIMAGE;
296 goto done;
297 }
298
Fabio Utzige52c08e2019-09-11 19:32:00 -0300299 *size = off + protect_tlv_size + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600300 rc = 0;
301
302done:
303 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300304 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600305}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300306#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600307
Fabio Utzigc08ed212017-06-20 19:28:36 -0300308static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300309boot_read_image_header(struct boot_loader_state *state, int slot,
310 struct image_header *out_hdr)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800311{
312 const struct flash_area *fap;
313 int area_id;
314 int rc;
315
Fabio Utzig10ee6482019-08-01 12:04:52 -0300316#if (BOOT_IMAGE_NUMBER == 1)
317 (void)state;
318#endif
319
320 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800321 rc = flash_area_open(area_id, &fap);
322 if (rc != 0) {
323 rc = BOOT_EFLASH;
324 goto done;
325 }
326
327 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
328 if (rc != 0) {
329 rc = BOOT_EFLASH;
330 goto done;
331 }
332
333 rc = 0;
334
335done:
336 flash_area_close(fap);
337 return rc;
338}
339
340static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300341boot_read_image_headers(struct boot_loader_state *state, bool require_all)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800342{
343 int rc;
344 int i;
345
346 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300347 rc = boot_read_image_header(state, i, boot_img_hdr(state, i));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800348 if (rc != 0) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200349 /* If `require_all` is set, fail on any single fail, otherwise
350 * if at least the first slot's header was read successfully,
351 * then the boot loader can attempt a boot.
352 *
353 * Failure to read any headers is a fatal error.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800354 */
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200355 if (i > 0 && !require_all) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800356 return 0;
357 } else {
358 return rc;
359 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800360 }
361 }
362
363 return 0;
364}
365
366static uint8_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300367boot_write_sz(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800368{
369 uint8_t elem_sz;
370 uint8_t align;
371
372 /* Figure out what size to write update status update as. The size depends
373 * on what the minimum write size is for scratch area, active image slot.
374 * We need to use the bigger of those 2 values.
375 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300376 elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
377 align = flash_area_align(BOOT_SCRATCH_AREA(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800378 if (align > elem_sz) {
379 elem_sz = align;
380 }
381
382 return elem_sz;
383}
384
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200385/*
386 * Slots are compatible when all sectors that store upto to size of the image
387 * round up to sector size, in both slot's are able to fit in the scratch
388 * area, and have sizes that are a multiple of each other (powers of two
389 * presumably!).
390 */
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800391static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300392boot_slots_compatible(struct boot_loader_state *state)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800393{
David Vincze2d736ad2019-02-18 11:50:22 +0100394 size_t num_sectors_primary;
395 size_t num_sectors_secondary;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200396 size_t sz0, sz1;
David Vincze2d736ad2019-02-18 11:50:22 +0100397 size_t primary_slot_sz, secondary_slot_sz;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200398 size_t scratch_sz;
399 size_t i, j;
400 int8_t smaller;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800401
Fabio Utzig10ee6482019-08-01 12:04:52 -0300402 num_sectors_primary = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
403 num_sectors_secondary = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT);
David Vincze2d736ad2019-02-18 11:50:22 +0100404 if ((num_sectors_primary > BOOT_MAX_IMG_SECTORS) ||
405 (num_sectors_secondary > BOOT_MAX_IMG_SECTORS)) {
Fabio Utziga1fae672018-03-30 10:52:38 -0300406 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800407 return 0;
408 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300409
Fabio Utzig10ee6482019-08-01 12:04:52 -0300410 scratch_sz = boot_scratch_area_size(state);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200411
412 /*
413 * The following loop scans all sectors in a linear fashion, assuring that
414 * for each possible sector in each slot, it is able to fit in the other
415 * slot's sector or sectors. Slot's should be compatible as long as any
416 * number of a slot's sectors are able to fit into another, which only
417 * excludes cases where sector sizes are not a multiple of each other.
418 */
David Vincze2d736ad2019-02-18 11:50:22 +0100419 i = sz0 = primary_slot_sz = 0;
420 j = sz1 = secondary_slot_sz = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200421 smaller = 0;
David Vincze2d736ad2019-02-18 11:50:22 +0100422 while (i < num_sectors_primary || j < num_sectors_secondary) {
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200423 if (sz0 == sz1) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300424 sz0 += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
425 sz1 += boot_img_sector_size(state, BOOT_SECONDARY_SLOT, j);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200426 i++;
427 j++;
428 } else if (sz0 < sz1) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300429 sz0 += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
David Vincze2d736ad2019-02-18 11:50:22 +0100430 /* Guarantee that multiple sectors of the secondary slot
431 * fit into the primary slot.
432 */
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200433 if (smaller == 2) {
434 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors");
435 return 0;
436 }
437 smaller = 1;
438 i++;
439 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300440 sz1 += boot_img_sector_size(state, BOOT_SECONDARY_SLOT, j);
David Vincze2d736ad2019-02-18 11:50:22 +0100441 /* Guarantee that multiple sectors of the primary slot
442 * fit into the secondary slot.
443 */
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200444 if (smaller == 1) {
445 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors");
446 return 0;
447 }
448 smaller = 2;
449 j++;
450 }
451 if (sz0 == sz1) {
David Vincze2d736ad2019-02-18 11:50:22 +0100452 primary_slot_sz += sz0;
453 secondary_slot_sz += sz1;
454 /* Scratch has to fit each swap operation to the size of the larger
455 * sector among the primary slot and the secondary slot.
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200456 */
457 if (sz0 > scratch_sz || sz1 > scratch_sz) {
458 BOOT_LOG_WRN("Cannot upgrade: not all sectors fit inside scratch");
459 return 0;
460 }
461 smaller = sz0 = sz1 = 0;
462 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300463 }
464
David Vincze2d736ad2019-02-18 11:50:22 +0100465 if ((i != num_sectors_primary) ||
466 (j != num_sectors_secondary) ||
467 (primary_slot_sz != secondary_slot_sz)) {
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200468 BOOT_LOG_WRN("Cannot upgrade: slots are not compatible");
469 return 0;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800470 }
471
472 return 1;
473}
474
Fabio Utzig10ee6482019-08-01 12:04:52 -0300475#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
476static int
477boot_initialize_area(struct boot_loader_state *state, int flash_area)
478{
479 int num_sectors = BOOT_MAX_IMG_SECTORS;
480 int rc;
481
482 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
483 rc = flash_area_to_sectors(flash_area, &num_sectors,
484 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors);
485 BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors = (size_t)num_sectors;
486
487 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
488 rc = flash_area_to_sectors(flash_area, &num_sectors,
489 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors);
490 BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors = (size_t)num_sectors;
491
492 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
493 rc = flash_area_to_sectors(flash_area, &num_sectors,
494 state->scratch.sectors);
495 state->scratch.num_sectors = (size_t)num_sectors;
496 } else {
497 return BOOT_EFLASH;
498 }
499
500 return rc;
501}
502#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
503static int
504boot_initialize_area(struct boot_loader_state *state, int flash_area)
505{
506 uint32_t num_sectors;
507 struct flash_sector *out_sectors;
508 size_t *out_num_sectors;
509 int rc;
510
511 num_sectors = BOOT_MAX_IMG_SECTORS;
512
513 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
514 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
515 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
516 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
517 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
518 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
519 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
520 out_sectors = state->scratch.sectors;
521 out_num_sectors = &state->scratch.num_sectors;
522 } else {
523 return BOOT_EFLASH;
524 }
525
526 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
527 if (rc != 0) {
528 return rc;
529 }
530 *out_num_sectors = num_sectors;
531 return 0;
532}
533#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
534
Christopher Collins92ea77f2016-12-12 15:59:26 -0800535/**
536 * Determines the sector layout of both image slots and the scratch area.
537 * This information is necessary for calculating the number of bytes to erase
538 * and copy during an image swap. The information collected during this
Fabio Utzig10ee6482019-08-01 12:04:52 -0300539 * function is used to populate the state.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800540 */
541static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300542boot_read_sectors(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800543{
Fabio Utzigb0f04732019-07-31 09:49:19 -0300544 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800545 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800546
Fabio Utzig10ee6482019-08-01 12:04:52 -0300547 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300548
549 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800550 if (rc != 0) {
551 return BOOT_EFLASH;
552 }
553
Fabio Utzig10ee6482019-08-01 12:04:52 -0300554 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800555 if (rc != 0) {
556 return BOOT_EFLASH;
557 }
558
Fabio Utzig10ee6482019-08-01 12:04:52 -0300559 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200560 if (rc != 0) {
561 return BOOT_EFLASH;
562 }
563
Fabio Utzig10ee6482019-08-01 12:04:52 -0300564 BOOT_WRITE_SZ(state) = boot_write_sz(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800565
566 return 0;
567}
568
569static uint32_t
570boot_status_internal_off(int idx, int state, int elem_sz)
571{
572 int idx_sz;
573
574 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
575
Fabio Utzig39000012018-07-30 12:40:20 -0300576 return (idx - BOOT_STATUS_IDX_0) * idx_sz +
577 (state - BOOT_STATUS_STATE_0) * elem_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800578}
579
580/**
581 * Reads the status of a partially-completed swap, if any. This is necessary
582 * to recover in case the boot lodaer was reset in the middle of a swap
583 * operation.
584 */
585static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300586boot_read_status_bytes(const struct flash_area *fap,
587 struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800588{
589 uint32_t off;
590 uint8_t status;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300591 int max_entries;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800592 int found;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200593 int found_idx;
594 int invalid;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800595 int rc;
596 int i;
597
598 off = boot_status_off(fap);
Fabio Utzig10ee6482019-08-01 12:04:52 -0300599 max_entries = boot_status_entries(BOOT_CURR_IMG(state), fap);
Fabio Utzig9d160092019-08-09 07:46:34 -0300600 if (max_entries < 0) {
601 return BOOT_EBADARGS;
602 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300603
Christopher Collins92ea77f2016-12-12 15:59:26 -0800604 found = 0;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200605 found_idx = 0;
606 invalid = 0;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300607 for (i = 0; i < max_entries; i++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300608 rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(state),
Fabio Utzig178be542018-09-19 08:12:56 -0300609 &status, 1);
610 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800611 return BOOT_EFLASH;
612 }
613
Fabio Utzig178be542018-09-19 08:12:56 -0300614 if (rc == 1) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200615 if (found && !found_idx) {
616 found_idx = i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800617 }
618 } else if (!found) {
619 found = 1;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200620 } else if (found_idx) {
621 invalid = 1;
622 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800623 }
624 }
625
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200626 if (invalid) {
627 /* This means there was an error writing status on the last
628 * swap. Tell user and move on to validation!
629 */
630 BOOT_LOG_ERR("Detected inconsistent status!");
631
David Vincze2d736ad2019-02-18 11:50:22 +0100632#if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
633 /* With validation of the primary slot disabled, there is no way
634 * to be sure the swapped primary slot is OK, so abort!
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200635 */
636 assert(0);
637#endif
638 }
639
Christopher Collins92ea77f2016-12-12 15:59:26 -0800640 if (found) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200641 if (!found_idx) {
642 found_idx = i;
643 }
Fabio Utzig39000012018-07-30 12:40:20 -0300644 bs->idx = (found_idx / BOOT_STATUS_STATE_COUNT) + 1;
645 bs->state = (found_idx % BOOT_STATUS_STATE_COUNT) + 1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800646 }
647
648 return 0;
649}
650
651/**
652 * Reads the boot status from the flash. The boot status contains
653 * the current state of an interrupted image copy operation. If the boot
654 * status is not present, or it indicates that previous copy finished,
655 * there is no operation in progress.
656 */
657static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300658boot_read_status(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800659{
660 const struct flash_area *fap;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700661 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200662 uint8_t swap_info;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800663 int status_loc;
664 int area_id;
665 int rc;
666
667 memset(bs, 0, sizeof *bs);
Fabio Utzig39000012018-07-30 12:40:20 -0300668 bs->idx = BOOT_STATUS_IDX_0;
669 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700670 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800671
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700672#ifdef MCUBOOT_OVERWRITE_ONLY
673 /* Overwrite-only doesn't make use of the swap status area. */
674 return 0;
675#endif
676
Fabio Utzig10ee6482019-08-01 12:04:52 -0300677 status_loc = boot_status_source(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800678 switch (status_loc) {
679 case BOOT_STATUS_SOURCE_NONE:
680 return 0;
681
682 case BOOT_STATUS_SOURCE_SCRATCH:
683 area_id = FLASH_AREA_IMAGE_SCRATCH;
684 break;
685
David Vincze2d736ad2019-02-18 11:50:22 +0100686 case BOOT_STATUS_SOURCE_PRIMARY_SLOT:
Fabio Utzig10ee6482019-08-01 12:04:52 -0300687 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800688 break;
689
690 default:
691 assert(0);
692 return BOOT_EBADARGS;
693 }
694
695 rc = flash_area_open(area_id, &fap);
696 if (rc != 0) {
697 return BOOT_EFLASH;
698 }
699
Fabio Utzig10ee6482019-08-01 12:04:52 -0300700 rc = boot_read_status_bytes(fap, state, bs);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700701 if (rc == 0) {
David Vinczee2453472019-06-17 12:31:59 +0200702 off = boot_swap_info_off(fap);
703 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700704 if (rc == 1) {
David Vinczee2453472019-06-17 12:31:59 +0200705 BOOT_SET_SWAP_INFO(swap_info, 0, BOOT_SWAP_TYPE_NONE);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700706 rc = 0;
707 }
David Vinczee2453472019-06-17 12:31:59 +0200708
709 /* Extract the swap type info */
710 bs->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700711 }
Fabio Utzig46490722017-09-04 15:34:32 -0300712
713 flash_area_close(fap);
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700714
Fabio Utzig46490722017-09-04 15:34:32 -0300715 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800716}
717
718/**
719 * Writes the supplied boot status to the flash file system. The boot status
720 * contains the current state of an in-progress image copy operation.
721 *
722 * @param bs The boot status to write.
723 *
724 * @return 0 on success; nonzero on failure.
725 */
726int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300727boot_write_status(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800728{
729 const struct flash_area *fap;
730 uint32_t off;
731 int area_id;
732 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300733 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700734 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300735 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800736
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300737 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100738 * the trailer. Since in the last step the primary slot is erased, the
739 * first two status writes go to the scratch which will be copied to
740 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300741 */
742
Fabio Utzig2473ac02017-05-02 12:45:02 -0300743 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800744 /* Write to scratch. */
745 area_id = FLASH_AREA_IMAGE_SCRATCH;
746 } else {
David Vincze2d736ad2019-02-18 11:50:22 +0100747 /* Write to the primary slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300748 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800749 }
750
751 rc = flash_area_open(area_id, &fap);
752 if (rc != 0) {
753 rc = BOOT_EFLASH;
754 goto done;
755 }
756
757 off = boot_status_off(fap) +
Fabio Utzigd638b172019-08-09 10:38:05 -0300758 boot_status_internal_off(bs->idx, bs->state, BOOT_WRITE_SZ(state));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200759 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300760 erased_val = flash_area_erased_val(fap);
761 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700762 buf[0] = bs->state;
763
764 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800765 if (rc != 0) {
766 rc = BOOT_EFLASH;
767 goto done;
768 }
769
770 rc = 0;
771
772done:
773 flash_area_close(fap);
774 return rc;
775}
776
777/*
778 * Validate image hash/signature in a slot.
779 */
780static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300781boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
782 const struct flash_area *fap, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800783{
Fabio Utzig10ee6482019-08-01 12:04:52 -0300784 TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigb0f04732019-07-31 09:49:19 -0300785 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300786 int rc;
787
Fabio Utzig10ee6482019-08-01 12:04:52 -0300788#if (BOOT_IMAGE_NUMBER == 1)
789 (void)state;
790#endif
791
Fabio Utzigba829042018-09-18 08:29:34 -0300792 (void)bs;
793 (void)rc;
Fabio Utzigbc077932019-08-26 11:16:34 -0300794
795 image_index = BOOT_CURR_IMG(state);
796
797#ifdef MCUBOOT_ENC_IMAGES
798 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300799 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -0300800 if (rc < 0) {
801 return BOOT_EBADIMAGE;
802 }
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300803 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs->enckey[1])) {
Fabio Utzigba829042018-09-18 08:29:34 -0300804 return BOOT_EBADIMAGE;
805 }
806 }
Fabio Utzigbc077932019-08-26 11:16:34 -0300807#endif
808
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300809 if (bootutil_img_validate(BOOT_CURR_ENC(state), image_index, hdr, fap, tmpbuf,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300810 BOOT_TMPBUF_SZ, NULL, 0, NULL)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800811 return BOOT_EBADIMAGE;
812 }
Fabio Utzig10ee6482019-08-01 12:04:52 -0300813
Christopher Collins92ea77f2016-12-12 15:59:26 -0800814 return 0;
815}
816
817static int
818split_image_check(struct image_header *app_hdr,
819 const struct flash_area *app_fap,
820 struct image_header *loader_hdr,
821 const struct flash_area *loader_fap)
822{
823 static void *tmpbuf;
824 uint8_t loader_hash[32];
825
826 if (!tmpbuf) {
827 tmpbuf = malloc(BOOT_TMPBUF_SZ);
828 if (!tmpbuf) {
829 return BOOT_ENOMEM;
830 }
831 }
832
Fabio Utzig10ee6482019-08-01 12:04:52 -0300833 if (bootutil_img_validate(NULL, 0, loader_hdr, loader_fap, tmpbuf,
834 BOOT_TMPBUF_SZ, NULL, 0, loader_hash)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800835 return BOOT_EBADIMAGE;
836 }
837
Fabio Utzig10ee6482019-08-01 12:04:52 -0300838 if (bootutil_img_validate(NULL, 0, app_hdr, app_fap, tmpbuf,
839 BOOT_TMPBUF_SZ, loader_hash, 32, NULL)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800840 return BOOT_EBADIMAGE;
841 }
842
843 return 0;
844}
845
Fabio Utzig338a19f2018-12-03 08:37:08 -0200846/*
David Brown9bf95af2019-10-10 15:36:36 -0600847 * Check that this is a valid header. Valid means that the magic is
848 * correct, and that the sizes/offsets are "sane". Sane means that
849 * there is no overflow on the arithmetic, and that the result fits
850 * within the flash area we are in.
851 */
852static bool
853boot_is_header_valid(const struct image_header *hdr, const struct flash_area *fap)
854{
855 uint32_t size;
856
857 if (hdr->ih_magic != IMAGE_MAGIC) {
858 return false;
859 }
860
861 if (!boot_u32_safe_add(&size, hdr->ih_img_size, hdr->ih_hdr_size)) {
862 return false;
863 }
864
865 if (size >= fap->fa_size) {
866 return false;
867 }
868
869 return true;
870}
871
872/*
Fabio Utzig338a19f2018-12-03 08:37:08 -0200873 * Check that a memory area consists of a given value.
874 */
875static inline bool
876boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300877{
878 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200879 uint8_t *p = (uint8_t *)data;
880 for (i = 0; i < len; i++) {
881 if (val != p[i]) {
882 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300883 }
884 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200885 return true;
886}
887
888static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300889boot_check_header_erased(struct boot_loader_state *state, int slot)
Fabio Utzig338a19f2018-12-03 08:37:08 -0200890{
891 const struct flash_area *fap;
892 struct image_header *hdr;
893 uint8_t erased_val;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300894 int area_id;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200895 int rc;
896
Fabio Utzig10ee6482019-08-01 12:04:52 -0300897 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300898 rc = flash_area_open(area_id, &fap);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200899 if (rc != 0) {
900 return -1;
901 }
902
903 erased_val = flash_area_erased_val(fap);
904 flash_area_close(fap);
905
Fabio Utzig10ee6482019-08-01 12:04:52 -0300906 hdr = boot_img_hdr(state, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200907 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
908 return -1;
909 }
910
911 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300912}
913
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300914/*
915 * Check that there is a valid image in a slot
916 *
917 * @returns
918 * 0 if image was succesfully validated
919 * 1 if no bootloable image was found
920 * -1 on any errors
921 */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800922static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300923boot_validate_slot(struct boot_loader_state *state, int slot,
924 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800925{
926 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400927 struct image_header *hdr;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300928 int area_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800929 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300930
Fabio Utzig10ee6482019-08-01 12:04:52 -0300931 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300932 rc = flash_area_open(area_id, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800933 if (rc != 0) {
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300934 return -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800935 }
936
Fabio Utzig10ee6482019-08-01 12:04:52 -0300937 hdr = boot_img_hdr(state, slot);
938 if (boot_check_header_erased(state, slot) == 0 ||
939 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100940 /* No bootable image in slot; continue booting from the primary slot. */
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300941 rc = 1;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200942 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300943 }
944
David Brown9bf95af2019-10-10 15:36:36 -0600945 if (!boot_is_header_valid(hdr, fap) || boot_image_check(state, hdr, fap, bs)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100946 if (slot != BOOT_PRIMARY_SLOT) {
David Brownb38e0442017-02-24 13:57:12 -0700947 flash_area_erase(fap, 0, fap->fa_size);
David Vincze2d736ad2019-02-18 11:50:22 +0100948 /* Image in the secondary slot is invalid. Erase the image and
949 * continue booting from the primary slot.
David Brownb38e0442017-02-24 13:57:12 -0700950 */
951 }
David Vincze2d736ad2019-02-18 11:50:22 +0100952 BOOT_LOG_ERR("Image in the %s slot is not valid!",
953 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
Fabio Utzig338a19f2018-12-03 08:37:08 -0200954 rc = -1;
955 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800956 }
957
David Vincze2d736ad2019-02-18 11:50:22 +0100958 /* Image in the secondary slot is valid. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200959 rc = 0;
960
961out:
962 flash_area_close(fap);
963 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800964}
965
966/**
967 * Determines which swap operation to perform, if any. If it is determined
David Vincze2d736ad2019-02-18 11:50:22 +0100968 * that a swap operation is required, the image in the secondary slot is checked
969 * for validity. If the image in the secondary slot is invalid, it is erased,
970 * and a swap type of "none" is indicated.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800971 *
972 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
973 */
974static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300975boot_validated_swap_type(struct boot_loader_state *state,
976 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800977{
978 int swap_type;
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300979 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800980
Fabio Utzigb0f04732019-07-31 09:49:19 -0300981 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
Fabio Utzige575b0b2019-09-11 12:34:23 -0300982 if (BOOT_IS_UPGRADE(swap_type)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100983 /* Boot loader wants to switch to the secondary slot.
984 * Ensure image is valid.
985 */
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300986 rc = boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs);
987 if (rc == 1) {
988 swap_type = BOOT_SWAP_TYPE_NONE;
989 } else if (rc != 0) {
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300990 swap_type = BOOT_SWAP_TYPE_FAIL;
991 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800992 }
993
994 return swap_type;
995}
996
997/**
998 * Calculates the number of sectors the scratch area can contain. A "last"
999 * source sector is specified because images are copied backwards in flash
1000 * (final index to index number 0).
1001 *
1002 * @param last_sector_idx The index of the last source sector
1003 * (inclusive).
1004 * @param out_first_sector_idx The index of the first source sector
1005 * (inclusive) gets written here.
1006 *
1007 * @return The number of bytes comprised by the
1008 * [first-sector, last-sector] range.
1009 */
Fabio Utzig3488eef2017-06-12 10:25:43 -03001010#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001011static uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -03001012boot_copy_sz(struct boot_loader_state *state, int last_sector_idx,
1013 int *out_first_sector_idx)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001014{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001015 size_t scratch_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001016 uint32_t new_sz;
1017 uint32_t sz;
1018 int i;
1019
1020 sz = 0;
1021
Fabio Utzig10ee6482019-08-01 12:04:52 -03001022 scratch_sz = boot_scratch_area_size(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001023 for (i = last_sector_idx; i >= 0; i--) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001024 new_sz = sz + boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001025 /*
David Vincze2d736ad2019-02-18 11:50:22 +01001026 * The secondary slot is not being checked here, because
1027 * `boot_slots_compatible` already provides assurance that the copy size
1028 * will be compatible with the primary slot and scratch.
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001029 */
Marti Bolivard3269fd2017-06-12 16:31:12 -04001030 if (new_sz > scratch_sz) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001031 break;
1032 }
1033 sz = new_sz;
1034 }
1035
1036 /* i currently refers to a sector that doesn't fit or it is -1 because all
1037 * sectors have been processed. In both cases, exclude sector i.
1038 */
1039 *out_first_sector_idx = i + 1;
1040 return sz;
1041}
Fabio Utzig3488eef2017-06-12 10:25:43 -03001042#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001043
1044/**
1045 * Erases a region of flash.
1046 *
Fabio Utzigba829042018-09-18 08:29:34 -03001047 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001048 * @param off The offset within the flash area to start the
1049 * erase.
1050 * @param sz The number of bytes to erase.
1051 *
1052 * @return 0 on success; nonzero on failure.
1053 */
Fabio Utzigba829042018-09-18 08:29:34 -03001054static inline int
Fabio Utzigc28005b2019-09-10 12:18:29 -03001055boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001056{
Fabio Utzigba829042018-09-18 08:29:34 -03001057 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001058}
1059
1060/**
1061 * Copies the contents of one flash region to another. You must erase the
1062 * destination region prior to calling this function.
1063 *
1064 * @param flash_area_id_src The ID of the source flash area.
1065 * @param flash_area_id_dst The ID of the destination flash area.
1066 * @param off_src The offset within the source flash area to
1067 * copy from.
1068 * @param off_dst The offset within the destination flash area to
1069 * copy to.
1070 * @param sz The number of bytes to copy.
1071 *
1072 * @return 0 on success; nonzero on failure.
1073 */
1074static int
Fabio Utzigc28005b2019-09-10 12:18:29 -03001075boot_copy_region(struct boot_loader_state *state,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001076 const struct flash_area *fap_src,
Fabio Utzigba829042018-09-18 08:29:34 -03001077 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -08001078 uint32_t off_src, uint32_t off_dst, uint32_t sz)
1079{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001080 uint32_t bytes_copied;
1081 int chunk_sz;
1082 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -03001083#ifdef MCUBOOT_ENC_IMAGES
1084 uint32_t off;
Fabio Utziga87cc7d2019-08-26 11:21:45 -03001085 uint32_t tlv_off;
Fabio Utzigba829042018-09-18 08:29:34 -03001086 size_t blk_off;
1087 struct image_header *hdr;
1088 uint16_t idx;
1089 uint32_t blk_sz;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001090 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -03001091#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001092
Fabio Utzig10ee6482019-08-01 12:04:52 -03001093 TARGET_STATIC uint8_t buf[1024];
1094
1095#if !defined(MCUBOOT_ENC_IMAGES)
1096 (void)state;
1097#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001098
Christopher Collins92ea77f2016-12-12 15:59:26 -08001099 bytes_copied = 0;
1100 while (bytes_copied < sz) {
1101 if (sz - bytes_copied > sizeof buf) {
1102 chunk_sz = sizeof buf;
1103 } else {
1104 chunk_sz = sz - bytes_copied;
1105 }
1106
1107 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
1108 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001109 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001110 }
1111
Fabio Utzigba829042018-09-18 08:29:34 -03001112#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001113 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001114 if (fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) ||
1115 fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001116 /* assume the secondary slot as src, needs decryption */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001117 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001118 off = off_src;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001119 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001120 /* might need encryption (metadata from the primary slot) */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001121 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001122 off = off_dst;
1123 }
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001124 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001125 blk_sz = chunk_sz;
1126 idx = 0;
1127 if (off + bytes_copied < hdr->ih_hdr_size) {
1128 /* do not decrypt header */
1129 blk_off = 0;
1130 blk_sz = chunk_sz - hdr->ih_hdr_size;
1131 idx = hdr->ih_hdr_size;
1132 } else {
1133 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
1134 }
Fabio Utziga87cc7d2019-08-26 11:21:45 -03001135 tlv_off = BOOT_TLV_OFF(hdr);
1136 if (off + bytes_copied + chunk_sz > tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -03001137 /* do not decrypt TLVs */
Fabio Utziga87cc7d2019-08-26 11:21:45 -03001138 if (off + bytes_copied >= tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -03001139 blk_sz = 0;
1140 } else {
Fabio Utziga87cc7d2019-08-26 11:21:45 -03001141 blk_sz = tlv_off - (off + bytes_copied);
Fabio Utzigba829042018-09-18 08:29:34 -03001142 }
1143 }
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001144 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
Fabio Utzigb0f04732019-07-31 09:49:19 -03001145 (off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
1146 blk_off, &buf[idx]);
Fabio Utzigba829042018-09-18 08:29:34 -03001147 }
1148 }
1149#endif
1150
Christopher Collins92ea77f2016-12-12 15:59:26 -08001151 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
1152 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001153 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001154 }
1155
1156 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -03001157
1158 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -08001159 }
1160
Fabio Utzigba829042018-09-18 08:29:34 -03001161 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001162}
1163
David Brown6b1b3b92017-09-19 08:59:10 -06001164#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -03001165static inline int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001166boot_status_init(const struct boot_loader_state *state,
1167 const struct flash_area *fap,
1168 const struct boot_status *bs)
Fabio Utzig2473ac02017-05-02 12:45:02 -03001169{
Fabio Utzig2473ac02017-05-02 12:45:02 -03001170 struct boot_swap_state swap_state;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001171 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001172 int rc;
1173
Fabio Utzig10ee6482019-08-01 12:04:52 -03001174#if (BOOT_IMAGE_NUMBER == 1)
1175 (void)state;
1176#endif
1177
1178 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001179
Christopher Collins2c88e692019-05-22 15:10:14 -07001180 BOOT_LOG_DBG("initializing status; fa_id=%d", fap->fa_id);
1181
Fabio Utzigb0f04732019-07-31 09:49:19 -03001182 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
1183 &swap_state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001184 assert(rc == 0);
1185
Christopher Collinsa1c12042019-05-23 14:00:28 -07001186 if (bs->swap_type != BOOT_SWAP_TYPE_NONE) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03001187 rc = boot_write_swap_info(fap, bs->swap_type, image_index);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001188 assert(rc == 0);
1189 }
1190
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001191 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001192 rc = boot_write_image_ok(fap);
1193 assert(rc == 0);
1194 }
1195
Fabio Utzig46490722017-09-04 15:34:32 -03001196 rc = boot_write_swap_size(fap, bs->swap_size);
1197 assert(rc == 0);
1198
Fabio Utzigba829042018-09-18 08:29:34 -03001199#ifdef MCUBOOT_ENC_IMAGES
1200 rc = boot_write_enc_key(fap, 0, bs->enckey[0]);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001201 assert(rc == 0);
1202
Fabio Utzigba829042018-09-18 08:29:34 -03001203 rc = boot_write_enc_key(fap, 1, bs->enckey[1]);
1204 assert(rc == 0);
1205#endif
1206
1207 rc = boot_write_magic(fap);
1208 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001209
1210 return 0;
1211}
Christopher Collinsa1c12042019-05-23 14:00:28 -07001212
David Brown6b1b3b92017-09-19 08:59:10 -06001213#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001214
Fabio Utzig358c9352017-07-25 22:10:45 -03001215#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -03001216static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001217boot_erase_trailer_sectors(const struct boot_loader_state *state,
1218 const struct flash_area *fap)
Fabio Utzig2473ac02017-05-02 12:45:02 -03001219{
1220 uint8_t slot;
Fabio Utziged0ca432019-01-23 14:50:11 -02001221 uint32_t sector;
1222 uint32_t trailer_sz;
1223 uint32_t total_sz;
1224 uint32_t off;
1225 uint32_t sz;
David Vinczeb75c12a2019-03-22 14:58:33 +01001226 int fa_id_primary;
1227 int fa_id_secondary;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001228 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001229 int rc;
1230
Christopher Collins2c88e692019-05-22 15:10:14 -07001231 BOOT_LOG_DBG("erasing trailer; fa_id=%d", fap->fa_id);
1232
Fabio Utzig10ee6482019-08-01 12:04:52 -03001233 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001234 fa_id_primary = flash_area_id_from_multi_image_slot(image_index,
1235 BOOT_PRIMARY_SLOT);
1236 fa_id_secondary = flash_area_id_from_multi_image_slot(image_index,
1237 BOOT_SECONDARY_SLOT);
David Vinczeb75c12a2019-03-22 14:58:33 +01001238
1239 if (fap->fa_id == fa_id_primary) {
David Vincze2d736ad2019-02-18 11:50:22 +01001240 slot = BOOT_PRIMARY_SLOT;
David Vinczeb75c12a2019-03-22 14:58:33 +01001241 } else if (fap->fa_id == fa_id_secondary) {
David Vincze2d736ad2019-02-18 11:50:22 +01001242 slot = BOOT_SECONDARY_SLOT;
David Vinczeb75c12a2019-03-22 14:58:33 +01001243 } else {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001244 return BOOT_EFLASH;
1245 }
1246
Fabio Utziged0ca432019-01-23 14:50:11 -02001247 /* delete starting from last sector and moving to beginning */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001248 sector = boot_img_num_sectors(state, slot) - 1;
1249 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
Fabio Utziged0ca432019-01-23 14:50:11 -02001250 total_sz = 0;
1251 do {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001252 sz = boot_img_sector_size(state, slot, sector);
1253 off = boot_img_sector_off(state, slot, sector);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001254 rc = boot_erase_region(fap, off, sz);
Fabio Utziged0ca432019-01-23 14:50:11 -02001255 assert(rc == 0);
1256
1257 sector--;
1258 total_sz += sz;
1259 } while (total_sz < trailer_sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001260
1261 return rc;
1262}
Fabio Utzig358c9352017-07-25 22:10:45 -03001263#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001264
Christopher Collins92ea77f2016-12-12 15:59:26 -08001265/**
1266 * Swaps the contents of two flash regions within the two image slots.
1267 *
1268 * @param idx The index of the first sector in the range of
1269 * sectors being swapped.
1270 * @param sz The number of bytes to swap.
1271 * @param bs The current boot status. This struct gets
1272 * updated according to the outcome.
1273 *
1274 * @return 0 on success; nonzero on failure.
1275 */
Fabio Utzig3488eef2017-06-12 10:25:43 -03001276#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -08001277static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001278boot_swap_sectors(int idx, uint32_t sz, struct boot_loader_state *state,
1279 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001280{
David Vincze2d736ad2019-02-18 11:50:22 +01001281 const struct flash_area *fap_primary_slot;
1282 const struct flash_area *fap_secondary_slot;
Fabio Utzigba829042018-09-18 08:29:34 -03001283 const struct flash_area *fap_scratch;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001284 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001285 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001286 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001287 uint32_t scratch_trailer_off;
1288 struct boot_swap_state swap_state;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001289 size_t last_sector;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001290 bool erase_scratch;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001291 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001292 int rc;
1293
1294 /* Calculate offset from start of image area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001295 img_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001296
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001297 copy_sz = sz;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001298 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
Fabio Utzig9678c972017-05-23 11:28:56 -04001299
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001300 /* sz in this function is always sized on a multiple of the sector size.
1301 * The check against the start offset of the last sector
Fabio Utzig9678c972017-05-23 11:28:56 -04001302 * is to determine if we're swapping the last sector. The last sector
1303 * needs special handling because it's where the trailer lives. If we're
1304 * copying it, we need to use scratch to write the trailer temporarily.
1305 *
1306 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
1307 * controls if special handling is needed (swapping last sector).
1308 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001309 last_sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
David Vinczeba3bd602019-06-17 16:01:43 +02001310 if ((img_off + sz) >
Fabio Utzig10ee6482019-08-01 12:04:52 -03001311 boot_img_sector_off(state, BOOT_PRIMARY_SLOT, last_sector)) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001312 copy_sz -= trailer_sz;
1313 }
1314
Fabio Utzig39000012018-07-30 12:40:20 -03001315 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001316
Fabio Utzig10ee6482019-08-01 12:04:52 -03001317 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001318
1319 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1320 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001321 assert (rc == 0);
1322
Fabio Utzigb0f04732019-07-31 09:49:19 -03001323 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1324 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001325 assert (rc == 0);
1326
1327 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch);
1328 assert (rc == 0);
1329
Fabio Utzig39000012018-07-30 12:40:20 -03001330 if (bs->state == BOOT_STATUS_STATE_0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001331 BOOT_LOG_DBG("erasing scratch area");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001332 rc = boot_erase_region(fap_scratch, 0, fap_scratch->fa_size);
Christopher Collins4772ac42017-02-27 20:08:01 -08001333 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001334
Fabio Utzig39000012018-07-30 12:40:20 -03001335 if (bs->idx == BOOT_STATUS_IDX_0) {
Christopher Collinsa1c12042019-05-23 14:00:28 -07001336 /* Write a trailer to the scratch area, even if we don't need the
1337 * scratch area for status. We need a temporary place to store the
1338 * `swap-type` while we erase the primary trailer.
1339 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001340 rc = boot_status_init(state, fap_scratch, bs);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001341 assert(rc == 0);
1342
1343 if (!bs->use_scratch) {
1344 /* Prepare the primary status area... here it is known that the
1345 * last sector is not being used by the image data so it's safe
1346 * to erase.
Fabio Utzig2473ac02017-05-02 12:45:02 -03001347 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001348 rc = boot_erase_trailer_sectors(state, fap_primary_slot);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001349 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001350
Fabio Utzig10ee6482019-08-01 12:04:52 -03001351 rc = boot_status_init(state, fap_primary_slot, bs);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001352 assert(rc == 0);
1353
1354 /* Erase the temporary trailer from the scratch area. */
Fabio Utzigc28005b2019-09-10 12:18:29 -03001355 rc = boot_erase_region(fap_scratch, 0, fap_scratch->fa_size);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001356 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001357 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001358 }
1359
Fabio Utzigc28005b2019-09-10 12:18:29 -03001360 rc = boot_copy_region(state, fap_secondary_slot, fap_scratch,
Christopher Collinsa1c12042019-05-23 14:00:28 -07001361 img_off, 0, copy_sz);
1362 assert(rc == 0);
1363
Fabio Utzig10ee6482019-08-01 12:04:52 -03001364 rc = boot_write_status(state, bs);
Fabio Utzig4d7396d2019-09-05 18:38:05 -03001365 bs->state = BOOT_STATUS_STATE_1;
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001366 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001367 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001368
Fabio Utzig39000012018-07-30 12:40:20 -03001369 if (bs->state == BOOT_STATUS_STATE_1) {
Fabio Utzigc28005b2019-09-10 12:18:29 -03001370 rc = boot_erase_region(fap_secondary_slot, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001371 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001372
Fabio Utzigc28005b2019-09-10 12:18:29 -03001373 rc = boot_copy_region(state, fap_primary_slot, fap_secondary_slot,
David Vincze2d736ad2019-02-18 11:50:22 +01001374 img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001375 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001376
Fabio Utzig39000012018-07-30 12:40:20 -03001377 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001378 /* If not all sectors of the slot are being swapped,
David Vincze2d736ad2019-02-18 11:50:22 +01001379 * guarantee here that only the primary slot will have the state.
Fabio Utzig2473ac02017-05-02 12:45:02 -03001380 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001381 rc = boot_erase_trailer_sectors(state, fap_secondary_slot);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001382 assert(rc == 0);
1383 }
1384
Fabio Utzig10ee6482019-08-01 12:04:52 -03001385 rc = boot_write_status(state, bs);
Fabio Utzig4d7396d2019-09-05 18:38:05 -03001386 bs->state = BOOT_STATUS_STATE_2;
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001387 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001388 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001389
Fabio Utzig39000012018-07-30 12:40:20 -03001390 if (bs->state == BOOT_STATUS_STATE_2) {
Fabio Utzigc28005b2019-09-10 12:18:29 -03001391 rc = boot_erase_region(fap_primary_slot, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001392 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001393
Christopher Collinsa1c12042019-05-23 14:00:28 -07001394 /* NOTE: If this is the final sector, we exclude the image trailer from
1395 * this copy (copy_sz was truncated earlier).
1396 */
Fabio Utzigc28005b2019-09-10 12:18:29 -03001397 rc = boot_copy_region(state, fap_scratch, fap_primary_slot,
David Vincze2d736ad2019-02-18 11:50:22 +01001398 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001399 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001400
Fabio Utzig94d998c2017-05-22 11:02:41 -04001401 if (bs->use_scratch) {
Fabio Utzigba829042018-09-18 08:29:34 -03001402 scratch_trailer_off = boot_status_off(fap_scratch);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001403
1404 /* copy current status that is being maintained in scratch */
Fabio Utzigc28005b2019-09-10 12:18:29 -03001405 rc = boot_copy_region(state, fap_scratch, fap_primary_slot,
David Vincze2d736ad2019-02-18 11:50:22 +01001406 scratch_trailer_off, img_off + copy_sz,
Fabio Utzig4d7396d2019-09-05 18:38:05 -03001407 (BOOT_STATUS_STATE_COUNT - 1) * BOOT_WRITE_SZ(state));
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001408 BOOT_STATUS_ASSERT(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001409
Fabio Utzig2473ac02017-05-02 12:45:02 -03001410 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1411 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001412 assert(rc == 0);
1413
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001414 if (swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze2d736ad2019-02-18 11:50:22 +01001415 rc = boot_write_image_ok(fap_primary_slot);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001416 assert(rc == 0);
1417 }
1418
Christopher Collinsa1c12042019-05-23 14:00:28 -07001419 if (swap_state.swap_type != BOOT_SWAP_TYPE_NONE) {
David Vinczee2453472019-06-17 12:31:59 +02001420 rc = boot_write_swap_info(fap_primary_slot,
Fabio Utzigb0f04732019-07-31 09:49:19 -03001421 swap_state.swap_type, image_index);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001422 assert(rc == 0);
1423 }
1424
David Vincze2d736ad2019-02-18 11:50:22 +01001425 rc = boot_write_swap_size(fap_primary_slot, bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001426 assert(rc == 0);
1427
Fabio Utzigba829042018-09-18 08:29:34 -03001428#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +01001429 rc = boot_write_enc_key(fap_primary_slot, 0, bs->enckey[0]);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001430 assert(rc == 0);
1431
David Vincze2d736ad2019-02-18 11:50:22 +01001432 rc = boot_write_enc_key(fap_primary_slot, 1, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001433 assert(rc == 0);
1434#endif
David Vincze2d736ad2019-02-18 11:50:22 +01001435 rc = boot_write_magic(fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001436 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001437 }
1438
Christopher Collinsa1c12042019-05-23 14:00:28 -07001439 /* If we wrote a trailer to the scratch area, erase it after we persist
1440 * a trailer to the primary slot. We do this to prevent mcuboot from
1441 * reading a stale status from the scratch area in case of immediate
1442 * reset.
1443 */
1444 erase_scratch = bs->use_scratch;
1445 bs->use_scratch = 0;
1446
Fabio Utzig4d7396d2019-09-05 18:38:05 -03001447 rc = boot_write_status(state, bs);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001448 bs->idx++;
Fabio Utzig39000012018-07-30 12:40:20 -03001449 bs->state = BOOT_STATUS_STATE_0;
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001450 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001451
1452 if (erase_scratch) {
Fabio Utzigc28005b2019-09-10 12:18:29 -03001453 rc = boot_erase_region(fap_scratch, 0, sz);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001454 assert(rc == 0);
1455 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001456 }
Fabio Utzigba829042018-09-18 08:29:34 -03001457
David Vincze2d736ad2019-02-18 11:50:22 +01001458 flash_area_close(fap_primary_slot);
1459 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001460 flash_area_close(fap_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001461}
Fabio Utzig3488eef2017-06-12 10:25:43 -03001462#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001463
1464/**
David Vincze2d736ad2019-02-18 11:50:22 +01001465 * Overwrite primary slot with the image contained in the secondary slot.
1466 * If a prior copy operation was interrupted by a system reset, this function
1467 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001468 *
1469 * @param bs The current boot status. This function reads
1470 * this struct to determine if it is resuming
1471 * an interrupted swap operation. This
1472 * function writes the updated status to this
1473 * function on return.
1474 *
1475 * @return 0 on success; nonzero on failure.
1476 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001477#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001478static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001479boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
David Brown17609d82017-05-05 09:41:34 -06001480{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001481 size_t sect_count;
1482 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001483 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001484 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001485 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001486 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +01001487 const struct flash_area *fap_primary_slot;
1488 const struct flash_area *fap_secondary_slot;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001489 uint8_t image_index;
David Vincze2d736ad2019-02-18 11:50:22 +01001490
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001491 (void)bs;
1492
Fabio Utzig13d9e352017-10-05 20:32:31 -03001493#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1494 uint32_t src_size = 0;
Fabio Utzigd638b172019-08-09 10:38:05 -03001495 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001496 assert(rc == 0);
1497#endif
David Brown17609d82017-05-05 09:41:34 -06001498
David Vincze2d736ad2019-02-18 11:50:22 +01001499 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1500 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -06001501
Fabio Utzigb0f04732019-07-31 09:49:19 -03001502 image_index = BOOT_CURR_IMG(state);
1503
1504 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1505 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001506 assert (rc == 0);
1507
Fabio Utzigb0f04732019-07-31 09:49:19 -03001508 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1509 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001510 assert (rc == 0);
1511
Fabio Utzig10ee6482019-08-01 12:04:52 -03001512 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001513 for (sect = 0, size = 0; sect < sect_count; sect++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001514 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001515 rc = boot_erase_region(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001516 assert(rc == 0);
1517
1518 size += this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001519
1520#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1521 if (size >= src_size) {
1522 break;
1523 }
1524#endif
David Brown17609d82017-05-05 09:41:34 -06001525 }
1526
Fabio Utzigba829042018-09-18 08:29:34 -03001527#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001528 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001529 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001530 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
Fabio Utzigb0f04732019-07-31 09:49:19 -03001531 fap_secondary_slot, bs->enckey[1]);
David Vincze2d736ad2019-02-18 11:50:22 +01001532
Fabio Utzigba829042018-09-18 08:29:34 -03001533 if (rc < 0) {
1534 return BOOT_EBADIMAGE;
1535 }
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001536 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs->enckey[1])) {
Fabio Utzigba829042018-09-18 08:29:34 -03001537 return BOOT_EBADIMAGE;
1538 }
1539 }
1540#endif
1541
David Vincze2d736ad2019-02-18 11:50:22 +01001542 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1543 size);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001544 rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot, 0, 0, size);
David Brown17609d82017-05-05 09:41:34 -06001545
Fabio Utzig13d9e352017-10-05 20:32:31 -03001546 /*
1547 * Erases header and trailer. The trailer is erased because when a new
1548 * image is written without a trailer as is the case when using newt, the
1549 * trailer that was left might trigger a new upgrade.
1550 */
Christopher Collins2c88e692019-05-22 15:10:14 -07001551 BOOT_LOG_DBG("erasing secondary header");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001552 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001553 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
1554 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001555 assert(rc == 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001556 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -07001557 BOOT_LOG_DBG("erasing secondary trailer");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001558 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001559 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
1560 last_sector),
1561 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
1562 last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001563 assert(rc == 0);
1564
David Vincze2d736ad2019-02-18 11:50:22 +01001565 flash_area_close(fap_primary_slot);
1566 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001567
David Vincze2d736ad2019-02-18 11:50:22 +01001568 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001569
1570 return 0;
1571}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001572#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001573
Christopher Collinsa1c12042019-05-23 14:00:28 -07001574#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -03001575/**
1576 * Swaps the two images in flash. If a prior copy operation was interrupted
1577 * by a system reset, this function completes that operation.
1578 *
1579 * @param bs The current boot status. This function reads
1580 * this struct to determine if it is resuming
1581 * an interrupted swap operation. This
1582 * function writes the updated status to this
1583 * function on return.
1584 *
1585 * @return 0 on success; nonzero on failure.
1586 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001587static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001588boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001589{
1590 uint32_t sz;
1591 int first_sector_idx;
1592 int last_sector_idx;
David Vincze2d736ad2019-02-18 11:50:22 +01001593 int last_idx_secondary_slot;
Fabio Utzigcd5774b2017-11-29 10:18:26 -02001594 uint32_t swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001595 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001596#ifdef MCUBOOT_ENC_IMAGES
1597 const struct flash_area *fap;
1598 uint8_t slot;
1599 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001600#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001601 uint32_t size;
1602 uint32_t copy_size;
David Vincze2d736ad2019-02-18 11:50:22 +01001603 uint32_t primary_slot_size;
1604 uint32_t secondary_slot_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001605 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001606 int rc;
1607
1608 /* FIXME: just do this if asked by user? */
1609
1610 size = copy_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001611 image_index = BOOT_CURR_IMG(state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001612
Fabio Utzig39000012018-07-30 12:40:20 -03001613 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Fabio Utzig46490722017-09-04 15:34:32 -03001614 /*
1615 * No swap ever happened, so need to find the largest image which
1616 * will be used to determine the amount of sectors to swap.
1617 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001618 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001619 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001620 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001621 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001622 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001623
Fabio Utzigba829042018-09-18 08:29:34 -03001624#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001625 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001626 fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001627 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs->enckey[0]);
Fabio Utzigba829042018-09-18 08:29:34 -03001628 assert(rc >= 0);
1629
1630 if (rc == 0) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001631 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 0, bs->enckey[0]);
Fabio Utzigba829042018-09-18 08:29:34 -03001632 assert(rc == 0);
1633 } else {
1634 rc = 0;
1635 }
1636 } else {
1637 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1638 }
1639#endif
1640
Fabio Utzig10ee6482019-08-01 12:04:52 -03001641 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001642 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001643 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001644 assert(rc == 0);
1645 }
1646
Fabio Utzigba829042018-09-18 08:29:34 -03001647#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001648 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001649 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001650 fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001651 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001652 assert(rc >= 0);
1653
1654 if (rc == 0) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001655 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001656 assert(rc == 0);
1657 } else {
1658 rc = 0;
1659 }
1660 } else {
1661 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1662 }
1663#endif
1664
Fabio Utzig46490722017-09-04 15:34:32 -03001665 if (size > copy_size) {
1666 copy_size = size;
1667 }
1668
1669 bs->swap_size = copy_size;
1670 } else {
1671 /*
1672 * If a swap was under way, the swap_size should already be present
1673 * in the trailer...
1674 */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001675 rc = boot_read_swap_size(image_index, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001676 assert(rc == 0);
1677
1678 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001679
1680#ifdef MCUBOOT_ENC_IMAGES
1681 for (slot = 0; slot <= 1; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03001682 rc = boot_read_enc_key(image_index, slot, bs->enckey[slot]);
Fabio Utzigba829042018-09-18 08:29:34 -03001683 assert(rc == 0);
1684
1685 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001686 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001687 break;
1688 }
1689 }
1690
1691 if (i != BOOT_ENC_KEY_SIZE) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001692 boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs->enckey[slot]);
Fabio Utzigba829042018-09-18 08:29:34 -03001693 }
1694 }
1695#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001696 }
1697
David Vincze2d736ad2019-02-18 11:50:22 +01001698 primary_slot_size = 0;
1699 secondary_slot_size = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001700 last_sector_idx = 0;
David Vincze2d736ad2019-02-18 11:50:22 +01001701 last_idx_secondary_slot = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001702
1703 /*
1704 * Knowing the size of the largest image between both slots, here we
David Vincze2d736ad2019-02-18 11:50:22 +01001705 * find what is the last sector in the primary slot that needs swapping.
1706 * Since we already know that both slots are compatible, the secondary
1707 * slot's last sector is not really required after this check is finished.
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001708 */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001709 while (1) {
David Vincze2d736ad2019-02-18 11:50:22 +01001710 if ((primary_slot_size < copy_size) ||
1711 (primary_slot_size < secondary_slot_size)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001712 primary_slot_size += boot_img_sector_size(state,
David Vincze2d736ad2019-02-18 11:50:22 +01001713 BOOT_PRIMARY_SLOT,
1714 last_sector_idx);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001715 }
David Vincze2d736ad2019-02-18 11:50:22 +01001716 if ((secondary_slot_size < copy_size) ||
1717 (secondary_slot_size < primary_slot_size)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001718 secondary_slot_size += boot_img_sector_size(state,
David Vincze2d736ad2019-02-18 11:50:22 +01001719 BOOT_SECONDARY_SLOT,
1720 last_idx_secondary_slot);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001721 }
David Vincze2d736ad2019-02-18 11:50:22 +01001722 if (primary_slot_size >= copy_size &&
1723 secondary_slot_size >= copy_size &&
1724 primary_slot_size == secondary_slot_size) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001725 break;
1726 }
1727 last_sector_idx++;
David Vincze2d736ad2019-02-18 11:50:22 +01001728 last_idx_secondary_slot++;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001729 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001730
1731 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001732 while (last_sector_idx >= 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001733 sz = boot_copy_sz(state, last_sector_idx, &first_sector_idx);
Fabio Utzig39000012018-07-30 12:40:20 -03001734 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001735 boot_swap_sectors(first_sector_idx, sz, state, bs);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001736 }
1737
1738 last_sector_idx = first_sector_idx - 1;
1739 swap_idx++;
1740 }
1741
David Vincze2d736ad2019-02-18 11:50:22 +01001742#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001743 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001744 BOOT_LOG_WRN("%d status write fails performing the swap",
1745 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001746 }
1747#endif
1748
Christopher Collins92ea77f2016-12-12 15:59:26 -08001749 return 0;
1750}
David Brown17609d82017-05-05 09:41:34 -06001751#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001752
1753/**
David Vincze2d736ad2019-02-18 11:50:22 +01001754 * Marks the image in the primary slot as fully copied.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001755 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001756#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001757static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001758boot_set_copy_done(uint8_t image_index)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001759{
1760 const struct flash_area *fap;
1761 int rc;
1762
Fabio Utzigb0f04732019-07-31 09:49:19 -03001763 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1764 &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001765 if (rc != 0) {
1766 return BOOT_EFLASH;
1767 }
1768
1769 rc = boot_write_copy_done(fap);
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001770 flash_area_close(fap);
1771 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001772}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001773#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001774
1775/**
David Vincze2d736ad2019-02-18 11:50:22 +01001776 * Marks a reverted image in the primary slot as confirmed. This is necessary to
1777 * ensure the status bytes from the image revert operation don't get processed
1778 * on a subsequent boot.
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001779 *
1780 * NOTE: image_ok is tested before writing because if there's a valid permanent
David Vincze2d736ad2019-02-18 11:50:22 +01001781 * image installed on the primary slot and the new image to be upgrade to has a
1782 * bad sig, image_ok would be overwritten.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001783 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001784#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001785static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001786boot_set_image_ok(uint8_t image_index)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001787{
1788 const struct flash_area *fap;
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001789 struct boot_swap_state state;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001790 int rc;
1791
Fabio Utzigb0f04732019-07-31 09:49:19 -03001792 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1793 &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001794 if (rc != 0) {
1795 return BOOT_EFLASH;
1796 }
1797
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001798 rc = boot_read_swap_state(fap, &state);
1799 if (rc != 0) {
1800 rc = BOOT_EFLASH;
1801 goto out;
1802 }
1803
1804 if (state.image_ok == BOOT_FLAG_UNSET) {
1805 rc = boot_write_image_ok(fap);
1806 }
1807
1808out:
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001809 flash_area_close(fap);
1810 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001811}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001812#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001813
David Vinczee32483f2019-06-13 10:46:24 +02001814#if (BOOT_IMAGE_NUMBER > 1)
1815/**
Fabio Utzig019a81a2019-08-27 10:33:39 -03001816 * Check if the version of the image is not older than required.
1817 *
1818 * @param req Required minimal image version.
1819 * @param ver Version of the image to be checked.
1820 *
1821 * @return 0 if the version is sufficient, nonzero otherwise.
1822 */
1823static int
1824boot_is_version_sufficient(struct image_version *req,
1825 struct image_version *ver)
1826{
1827 if (ver->iv_major > req->iv_major) {
1828 return 0;
1829 }
1830 if (ver->iv_major < req->iv_major) {
1831 return BOOT_EBADVERSION;
1832 }
1833 /* The major version numbers are equal. */
1834 if (ver->iv_minor > req->iv_minor) {
1835 return 0;
1836 }
1837 if (ver->iv_minor < req->iv_minor) {
1838 return BOOT_EBADVERSION;
1839 }
1840 /* The minor version numbers are equal. */
1841 if (ver->iv_revision < req->iv_revision) {
1842 return BOOT_EBADVERSION;
1843 }
1844
1845 return 0;
1846}
1847
1848/**
David Vinczee32483f2019-06-13 10:46:24 +02001849 * Check the image dependency whether it is satisfied and modify
1850 * the swap type if necessary.
1851 *
1852 * @param dep Image dependency which has to be verified.
1853 *
1854 * @return 0 on success; nonzero on failure.
1855 */
1856static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001857boot_verify_slot_dependency(struct boot_loader_state *state,
1858 struct image_dependency *dep)
David Vinczee32483f2019-06-13 10:46:24 +02001859{
1860 struct image_version *dep_version;
1861 size_t dep_slot;
1862 int rc;
David Browne6ab34c2019-09-03 12:24:21 -06001863 uint8_t swap_type;
David Vinczee32483f2019-06-13 10:46:24 +02001864
1865 /* Determine the source of the image which is the subject of
1866 * the dependency and get it's version. */
David Browne6ab34c2019-09-03 12:24:21 -06001867 swap_type = state->swap_type[dep->image_id];
Fabio Utzigb1adb1e2019-09-11 11:42:53 -03001868 dep_slot = (swap_type != BOOT_SWAP_TYPE_NONE) ?
David Vinczee32483f2019-06-13 10:46:24 +02001869 BOOT_SECONDARY_SLOT : BOOT_PRIMARY_SLOT;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001870 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +02001871
1872 rc = boot_is_version_sufficient(&dep->image_min_version, dep_version);
1873 if (rc != 0) {
1874 /* Dependency not satisfied.
1875 * Modify the swap type to decrease the version number of the image
1876 * (which will be located in the primary slot after the boot process),
1877 * consequently the number of unsatisfied dependencies will be
1878 * decreased or remain the same.
1879 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001880 switch (BOOT_SWAP_TYPE(state)) {
David Vinczee32483f2019-06-13 10:46:24 +02001881 case BOOT_SWAP_TYPE_TEST:
1882 case BOOT_SWAP_TYPE_PERM:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001883 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczee32483f2019-06-13 10:46:24 +02001884 break;
1885 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001886 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczee32483f2019-06-13 10:46:24 +02001887 break;
1888 default:
1889 break;
1890 }
1891 }
1892
1893 return rc;
1894}
1895
1896/**
1897 * Read all dependency TLVs of an image from the flash and verify
1898 * one after another to see if they are all satisfied.
1899 *
1900 * @param slot Image slot number.
1901 *
1902 * @return 0 on success; nonzero on failure.
1903 */
1904static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001905boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
David Vinczee32483f2019-06-13 10:46:24 +02001906{
1907 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001908 struct image_tlv_iter it;
David Vinczee32483f2019-06-13 10:46:24 +02001909 struct image_dependency dep;
1910 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001911 uint16_t len;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001912 int area_id;
David Vinczee32483f2019-06-13 10:46:24 +02001913 int rc;
1914
Fabio Utzig10ee6482019-08-01 12:04:52 -03001915 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001916 rc = flash_area_open(area_id, &fap);
David Vinczee32483f2019-06-13 10:46:24 +02001917 if (rc != 0) {
1918 rc = BOOT_EFLASH;
1919 goto done;
1920 }
1921
Fabio Utzig61fd8882019-09-14 20:00:20 -03001922 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
1923 IMAGE_TLV_DEPENDENCY, true);
David Vinczee32483f2019-06-13 10:46:24 +02001924 if (rc != 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001925 goto done;
1926 }
1927
Fabio Utzig61fd8882019-09-14 20:00:20 -03001928 while (true) {
1929 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
1930 if (rc < 0) {
1931 return -1;
1932 } else if (rc > 0) {
1933 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001934 break;
1935 }
Fabio Utzig61fd8882019-09-14 20:00:20 -03001936
1937 if (len != sizeof(dep)) {
1938 rc = BOOT_EBADIMAGE;
1939 goto done;
1940 }
1941
1942 rc = flash_area_read(fap, off, &dep, len);
1943 if (rc != 0) {
1944 rc = BOOT_EFLASH;
1945 goto done;
1946 }
1947
1948 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
1949 rc = BOOT_EBADARGS;
1950 goto done;
1951 }
1952
1953 /* Verify dependency and modify the swap type if not satisfied. */
1954 rc = boot_verify_slot_dependency(state, &dep);
1955 if (rc != 0) {
1956 /* Dependency not satisfied. */
1957 goto done;
1958 }
David Vinczee32483f2019-06-13 10:46:24 +02001959 }
1960
1961done:
1962 flash_area_close(fap);
1963 return rc;
1964}
1965
1966/**
David Vinczee32483f2019-06-13 10:46:24 +02001967 * Iterate over all the images and verify whether the image dependencies in the
1968 * TLV area are all satisfied and update the related swap type if necessary.
1969 */
Fabio Utzig298913b2019-08-28 11:22:45 -03001970static int
1971boot_verify_dependencies(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001972{
David Vinczee32483f2019-06-13 10:46:24 +02001973 int rc;
Fabio Utzig298913b2019-08-28 11:22:45 -03001974 uint8_t slot;
David Vinczee32483f2019-06-13 10:46:24 +02001975
Fabio Utzig10ee6482019-08-01 12:04:52 -03001976 BOOT_CURR_IMG(state) = 0;
1977 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001978 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1979 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
1980 slot = BOOT_SECONDARY_SLOT;
1981 } else {
1982 slot = BOOT_PRIMARY_SLOT;
1983 }
1984
1985 rc = boot_verify_slot_dependencies(state, slot);
Fabio Utzigabec0732019-07-31 08:40:22 -03001986 if (rc == 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001987 /* All dependencies've been satisfied, continue with next image. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001988 BOOT_CURR_IMG(state)++;
David Vinczee32483f2019-06-13 10:46:24 +02001989 } else if (rc == BOOT_EBADVERSION) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001990 /* Cannot upgrade due to non-met dependencies, so disable all
1991 * image upgrades.
1992 */
1993 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
1994 BOOT_CURR_IMG(state) = idx;
1995 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1996 }
1997 break;
David Vinczee32483f2019-06-13 10:46:24 +02001998 } else {
1999 /* Other error happened, images are inconsistent */
Fabio Utzig298913b2019-08-28 11:22:45 -03002000 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02002001 }
2002 }
Fabio Utzig298913b2019-08-28 11:22:45 -03002003 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02002004}
2005#endif /* (BOOT_IMAGE_NUMBER > 1) */
2006
Christopher Collins92ea77f2016-12-12 15:59:26 -08002007/**
David Vinczeba3bd602019-06-17 16:01:43 +02002008 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002009 *
David Vinczeba3bd602019-06-17 16:01:43 +02002010 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002011 *
2012 * @return 0 on success; nonzero on failure.
2013 */
2014static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03002015boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002016{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002017 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03002018#ifndef MCUBOOT_OVERWRITE_ONLY
2019 uint8_t swap_type;
2020#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002021
David Vinczeba3bd602019-06-17 16:01:43 +02002022 /* At this point there are no aborted swaps. */
2023#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03002024 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002025#elif defined(MCUBOOT_BOOTSTRAP)
2026 /* Check if the image update was triggered by a bad image in the
2027 * primary slot (the validity of the image in the secondary slot had
2028 * already been checked).
2029 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002030 if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 ||
2031 boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) {
2032 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002033 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002034 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002035 }
2036#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03002037 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002038#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03002039 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02002040
2041#ifndef MCUBOOT_OVERWRITE_ONLY
2042 /* The following state needs image_ok be explicitly set after the
2043 * swap was finished to avoid a new revert.
2044 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002045 swap_type = BOOT_SWAP_TYPE(state);
2046 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
2047 swap_type == BOOT_SWAP_TYPE_PERM) {
2048 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002049 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002050 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002051 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002052 }
2053
Fabio Utzige575b0b2019-09-11 12:34:23 -03002054 if (BOOT_IS_UPGRADE(swap_type)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002055 rc = boot_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002056 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002057 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07002058 }
David Vinczeba3bd602019-06-17 16:01:43 +02002059 }
2060#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02002061
David Vinczeba3bd602019-06-17 16:01:43 +02002062 return rc;
2063}
2064
2065/**
2066 * Completes a previously aborted image swap.
2067 *
2068 * @param bs The current boot status.
2069 *
2070 * @return 0 on success; nonzero on failure.
2071 */
2072#if !defined(MCUBOOT_OVERWRITE_ONLY)
2073static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03002074boot_complete_partial_swap(struct boot_loader_state *state,
2075 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02002076{
2077 int rc;
2078
2079 /* Determine the type of swap operation being resumed from the
2080 * `swap-type` trailer field.
2081 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002082 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002083 assert(rc == 0);
2084
Fabio Utzig10ee6482019-08-01 12:04:52 -03002085 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02002086
2087 /* The following states need image_ok be explicitly set after the
2088 * swap was finished to avoid a new revert.
2089 */
2090 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
2091 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002092 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002093 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002094 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002095 }
2096 }
2097
Fabio Utzige575b0b2019-09-11 12:34:23 -03002098 if (BOOT_IS_UPGRADE(bs->swap_type)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002099 rc = boot_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002100 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002101 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002102 }
2103 }
2104
Fabio Utzig10ee6482019-08-01 12:04:52 -03002105 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002106 BOOT_LOG_ERR("panic!");
2107 assert(0);
2108
2109 /* Loop forever... */
2110 while (1) {}
2111 }
2112
2113 return rc;
2114}
2115#endif /* !MCUBOOT_OVERWRITE_ONLY */
2116
2117#if (BOOT_IMAGE_NUMBER > 1)
2118/**
2119 * Review the validity of previously determined swap types of other images.
2120 *
2121 * @param aborted_swap The current image upgrade is a
2122 * partial/aborted swap.
2123 */
2124static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03002125boot_review_image_swap_types(struct boot_loader_state *state,
2126 bool aborted_swap)
David Vinczeba3bd602019-06-17 16:01:43 +02002127{
2128 /* In that case if we rebooted in the middle of an image upgrade process, we
2129 * must review the validity of swap types, that were previously determined
2130 * for other images. The image_ok flag had not been set before the reboot
2131 * for any of the updated images (only the copy_done flag) and thus falsely
2132 * the REVERT swap type has been determined for the previous images that had
2133 * been updated before the reboot.
2134 *
2135 * There are two separate scenarios that we have to deal with:
2136 *
2137 * 1. The reboot has happened during swapping an image:
2138 * The current image upgrade has been determined as a
2139 * partial/aborted swap.
2140 * 2. The reboot has happened between two separate image upgrades:
2141 * In this scenario we must check the swap type of the current image.
2142 * In those cases if it is NONE or REVERT we cannot certainly determine
2143 * the fact of a reboot. In a consistent state images must move in the
2144 * same direction or stay in place, e.g. in practice REVERT and TEST
2145 * swap types cannot be present at the same time. If the swap type of
2146 * the current image is either TEST, PERM or FAIL we must review the
2147 * already determined swap types of other images and set each false
2148 * REVERT swap types to NONE (these images had been successfully
2149 * updated before the system rebooted between two separate image
2150 * upgrades).
2151 */
2152
Fabio Utzig10ee6482019-08-01 12:04:52 -03002153 if (BOOT_CURR_IMG(state) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02002154 /* Nothing to do */
2155 return;
2156 }
2157
2158 if (!aborted_swap) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002159 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
2160 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002161 /* Nothing to do */
2162 return;
2163 }
2164 }
2165
Fabio Utzig10ee6482019-08-01 12:04:52 -03002166 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
2167 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
2168 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002169 }
2170 }
2171}
2172#endif
2173
2174/**
2175 * Prepare image to be updated if required.
2176 *
2177 * Prepare image to be updated if required with completing an image swap
2178 * operation if one was aborted and/or determining the type of the
2179 * swap operation. In case of any error set the swap type to NONE.
2180 *
Fabio Utzig10ee6482019-08-01 12:04:52 -03002181 * @param state TODO
David Vinczeba3bd602019-06-17 16:01:43 +02002182 * @param bs Pointer where the read and possibly updated
2183 * boot status can be written to.
2184 */
2185static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03002186boot_prepare_image_for_update(struct boot_loader_state *state,
2187 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02002188{
2189 int rc;
2190
2191 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002192 rc = boot_read_sectors(state);
David Vinczeba3bd602019-06-17 16:01:43 +02002193 if (rc != 0) {
2194 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
2195 " - too small?", BOOT_MAX_IMG_SECTORS);
2196 /* Unable to determine sector layout, continue with next image
2197 * if there is one.
2198 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002199 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002200 return;
2201 }
2202
2203 /* Attempt to read an image header from each slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002204 rc = boot_read_image_headers(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02002205 if (rc != 0) {
2206 /* Continue with next image if there is one. */
Fabio Utzigb0f04732019-07-31 09:49:19 -03002207 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03002208 BOOT_CURR_IMG(state));
2209 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002210 return;
2211 }
2212
2213 /* If the current image's slots aren't compatible, no swap is possible.
2214 * Just boot into primary slot.
2215 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002216 if (boot_slots_compatible(state)) {
2217 rc = boot_read_status(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002218 if (rc != 0) {
2219 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03002220 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002221 /* Continue with next image if there is one. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002222 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002223 return;
2224 }
2225
2226 /* Determine if we rebooted in the middle of an image swap
2227 * operation. If a partial swap was detected, complete it.
2228 */
2229 if (bs->idx != BOOT_STATUS_IDX_0 || bs->state != BOOT_STATUS_STATE_0) {
2230
2231#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03002232 boot_review_image_swap_types(state, true);
David Vinczeba3bd602019-06-17 16:01:43 +02002233#endif
2234
2235#ifdef MCUBOOT_OVERWRITE_ONLY
2236 /* Should never arrive here, overwrite-only mode has
2237 * no swap state.
2238 */
2239 assert(0);
2240#else
2241 /* Determine the type of swap operation being resumed from the
2242 * `swap-type` trailer field.
2243 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002244 rc = boot_complete_partial_swap(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002245 assert(rc == 0);
2246#endif
2247 /* Attempt to read an image header from each slot. Ensure that
2248 * image headers in slots are aligned with headers in boot_data.
2249 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002250 rc = boot_read_image_headers(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02002251 assert(rc == 0);
2252
2253 /* Swap has finished set to NONE */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002254 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002255 } else {
2256 /* There was no partial swap, determine swap type. */
2257 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002258 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
2259 } else if (boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) != 0) {
2260 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
David Vinczeba3bd602019-06-17 16:01:43 +02002261 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002262 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02002263 }
2264
2265#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03002266 boot_review_image_swap_types(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02002267#endif
2268
2269#ifdef MCUBOOT_BOOTSTRAP
Fabio Utzig10ee6482019-08-01 12:04:52 -03002270 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02002271 /* Header checks are done first because they are
2272 * inexpensive. Since overwrite-only copies starting from
2273 * offset 0, if interrupted, it might leave a valid header
2274 * magic, so also run validation on the primary slot to be
2275 * sure it's not OK.
2276 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03002277 if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 ||
2278 boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) {
2279 if (boot_img_hdr(state,
2280 BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC &&
2281 boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02002282 /* Set swap type to REVERT to overwrite the primary
2283 * slot with the image contained in secondary slot
2284 * and to trigger the explicit setting of the
2285 * image_ok flag.
2286 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03002287 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02002288 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02002289 }
2290 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02002291#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002292 }
David Vinczeba3bd602019-06-17 16:01:43 +02002293 } else {
2294 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002295 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002296 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002297}
2298
Christopher Collins92ea77f2016-12-12 15:59:26 -08002299int
Fabio Utzig10ee6482019-08-01 12:04:52 -03002300context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08002301{
Marti Bolivar84898652017-06-13 17:20:22 -04002302 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02002303 struct boot_status bs;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002304 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002305 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03002306 int image_index;
Fabio Utzig298913b2019-08-28 11:22:45 -03002307 bool has_upgrade;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002308
2309 /* The array of slot sectors are defined here (as opposed to file scope) so
2310 * that they don't get allocated for non-boot-loader apps. This is
2311 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002312 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08002313 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002314 TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
2315 TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
2316 TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Christopher Collins92ea77f2016-12-12 15:59:26 -08002317
Fabio Utzig10ee6482019-08-01 12:04:52 -03002318 memset(state, 0, sizeof(struct boot_loader_state));
Fabio Utzig298913b2019-08-28 11:22:45 -03002319 has_upgrade = false;
2320
2321#if (BOOT_IMAGE_NUMBER == 1)
2322 (void)has_upgrade;
2323#endif
Fabio Utzigba829042018-09-18 08:29:34 -03002324
David Vinczeba3bd602019-06-17 16:01:43 +02002325 /* Iterate over all the images. By the end of the loop the swap type has
2326 * to be determined for each image and all aborted swaps have to be
2327 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002328 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002329 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03002330
David Vinczeba3bd602019-06-17 16:01:43 +02002331#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
2332 /* The keys used for encryption may no longer be valid (could belong to
2333 * another images). Therefore, mark them as invalid to force their reload
2334 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03002335 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03002336 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Brown554c52e2017-06-30 16:01:07 -06002337#endif
2338
Fabio Utzig10ee6482019-08-01 12:04:52 -03002339 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03002340
Fabio Utzig10ee6482019-08-01 12:04:52 -03002341 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002342 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03002343 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002344 secondary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03002345 state->scratch.sectors = scratch_sectors;
David Vinczeba3bd602019-06-17 16:01:43 +02002346
2347 /* Open primary and secondary image areas for the duration
2348 * of this call.
2349 */
2350 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03002351 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03002352 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02002353 assert(rc == 0);
2354 }
2355 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03002356 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002357 assert(rc == 0);
2358
2359 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002360 boot_prepare_image_for_update(state, &bs);
Fabio Utzig298913b2019-08-28 11:22:45 -03002361
Fabio Utzige575b0b2019-09-11 12:34:23 -03002362 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
Fabio Utzig298913b2019-08-28 11:22:45 -03002363 has_upgrade = true;
2364 }
David Vinczeba3bd602019-06-17 16:01:43 +02002365 }
2366
David Vinczee32483f2019-06-13 10:46:24 +02002367#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig298913b2019-08-28 11:22:45 -03002368 if (has_upgrade) {
2369 /* Iterate over all the images and verify whether the image dependencies
2370 * are all satisfied and update swap type if necessary.
2371 */
2372 rc = boot_verify_dependencies(state);
2373 if (rc == BOOT_EBADVERSION) {
2374 /*
2375 * It was impossible to upgrade because the expected dependency version
2376 * was not available. Here we already changed the swap_type so that
2377 * instead of asserting the bootloader, we continue and no upgrade is
2378 * performed.
2379 */
2380 rc = 0;
2381 }
2382 }
David Vinczee32483f2019-06-13 10:46:24 +02002383#endif
2384
David Vinczeba3bd602019-06-17 16:01:43 +02002385 /* Iterate over all the images. At this point there are no aborted swaps
2386 * and the swap types are determined for each image. By the end of the loop
2387 * all required update operations will have been finished.
2388 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002389 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002390
2391#if (BOOT_IMAGE_NUMBER > 1)
2392#ifdef MCUBOOT_ENC_IMAGES
2393 /* The keys used for encryption may no longer be valid (could belong to
2394 * another images). Therefore, mark them as invalid to force their reload
2395 * by boot_enc_load().
2396 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03002397 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002398#endif /* MCUBOOT_ENC_IMAGES */
2399
2400 /* Indicate that swap is not aborted */
2401 memset(&bs, 0, sizeof bs);
2402 bs.idx = BOOT_STATUS_IDX_0;
2403 bs.state = BOOT_STATUS_STATE_0;
2404#endif /* (BOOT_IMAGE_NUMBER > 1) */
2405
2406 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002407 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02002408
Fabio Utzig10ee6482019-08-01 12:04:52 -03002409 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002410 case BOOT_SWAP_TYPE_NONE:
2411 break;
2412
2413 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
2414 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
2415 case BOOT_SWAP_TYPE_REVERT:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002416 rc = boot_perform_update(state, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002417 assert(rc == 0);
2418 break;
2419
2420 case BOOT_SWAP_TYPE_FAIL:
2421 /* The image in secondary slot was invalid and is now erased. Ensure
2422 * we don't try to boot into it again on the next reboot. Do this by
2423 * pretending we just reverted back to primary slot.
2424 */
2425#ifndef MCUBOOT_OVERWRITE_ONLY
2426 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002427 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002428 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002429 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002430 }
2431#endif /* !MCUBOOT_OVERWRITE_ONLY */
2432 break;
2433
2434 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002435 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002436 }
2437
Fabio Utzig10ee6482019-08-01 12:04:52 -03002438 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002439 BOOT_LOG_ERR("panic!");
2440 assert(0);
2441
2442 /* Loop forever... */
2443 while (1) {}
2444 }
2445 }
2446
2447 /* Iterate over all the images. At this point all required update operations
2448 * have finished. By the end of the loop each image in the primary slot will
2449 * have been re-validated.
2450 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002451 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2452 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02002453 /* Attempt to read an image header from each slot. Ensure that image
2454 * headers in slots are aligned with headers in boot_data.
2455 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002456 rc = boot_read_image_headers(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02002457 if (rc != 0) {
2458 goto out;
2459 }
2460 /* Since headers were reloaded, it can be assumed we just performed
2461 * a swap or overwrite. Now the header info that should be used to
2462 * provide the data for the bootstrap, which previously was at
2463 * secondary slot, was updated to primary slot.
2464 */
2465 }
2466
2467#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig10ee6482019-08-01 12:04:52 -03002468 rc = boot_validate_slot(state, BOOT_PRIMARY_SLOT, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02002469 if (rc != 0) {
2470 rc = BOOT_EBADIMAGE;
2471 goto out;
2472 }
2473#else
2474 /* Even if we're not re-validating the primary slot, we could be booting
2475 * onto an empty flash chip. At least do a basic sanity check that
2476 * the magic number on the image is OK.
2477 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002478 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002479 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
Fabio Utzig10ee6482019-08-01 12:04:52 -03002480 &boot_img_hdr(state,BOOT_PRIMARY_SLOT)->ih_magic,
2481 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002482 rc = BOOT_EBADIMAGE;
2483 goto out;
2484 }
2485#endif
2486 }
2487
Fabio Utzigb0f04732019-07-31 09:49:19 -03002488#if (BOOT_IMAGE_NUMBER > 1)
David Vinczeba3bd602019-06-17 16:01:43 +02002489 /* Always boot from the primary slot of Image 0. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002490 BOOT_CURR_IMG(state) = 0;
Fabio Utzigb0f04732019-07-31 09:49:19 -03002491#endif
Fabio Utzig298913b2019-08-28 11:22:45 -03002492
Fabio Utzig10ee6482019-08-01 12:04:52 -03002493 rsp->br_flash_dev_id = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)->fa_device_id;
2494 rsp->br_image_off = boot_img_slot_off(state, BOOT_PRIMARY_SLOT);
2495 rsp->br_hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002496
Fabio Utzig298913b2019-08-28 11:22:45 -03002497out:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002498 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2499 flash_area_close(BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002500 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002501 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
David Vinczeba3bd602019-06-17 16:01:43 +02002502 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04002503 }
2504 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002505}
2506
Fabio Utzig10ee6482019-08-01 12:04:52 -03002507/**
2508 * Prepares the booting process. This function moves images around in flash as
2509 * appropriate, and tells you what address to boot from.
2510 *
2511 * @param rsp On success, indicates how booting should occur.
2512 *
2513 * @return 0 on success; nonzero on failure.
2514 */
2515int
2516boot_go(struct boot_rsp *rsp)
2517{
2518 return context_boot_go(&boot_data, rsp);
2519}
2520
Christopher Collins92ea77f2016-12-12 15:59:26 -08002521int
2522split_go(int loader_slot, int split_slot, void **entry)
2523{
Marti Bolivarc50926f2017-06-14 09:35:40 -04002524 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08002525 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002526 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002527 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002528 int rc;
2529
Christopher Collins92ea77f2016-12-12 15:59:26 -08002530 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
2531 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04002532 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002533 }
David Vinczeba3bd602019-06-17 16:01:43 +02002534 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
2535 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002536
2537 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
2538 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07002539 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04002540 assert(rc == 0);
2541 split_flash_id = flash_area_id_from_image_slot(split_slot);
2542 rc = flash_area_open(split_flash_id,
2543 &BOOT_IMG_AREA(&boot_data, split_slot));
2544 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002545
2546 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002547 rc = boot_read_sectors(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002548 if (rc != 0) {
2549 rc = SPLIT_GO_ERR;
2550 goto done;
2551 }
2552
Fabio Utzig10ee6482019-08-01 12:04:52 -03002553 rc = boot_read_image_headers(&boot_data, true);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002554 if (rc != 0) {
2555 goto done;
2556 }
2557
Christopher Collins92ea77f2016-12-12 15:59:26 -08002558 /* Don't check the bootable image flag because we could really call a
2559 * bootable or non-bootable image. Just validate that the image check
2560 * passes which is distinct from the normal check.
2561 */
Marti Bolivarf804f622017-06-12 15:41:48 -04002562 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04002563 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04002564 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04002565 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002566 if (rc != 0) {
2567 rc = SPLIT_GO_NON_MATCHING;
2568 goto done;
2569 }
2570
Marti Bolivarea088872017-06-12 17:10:49 -04002571 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04002572 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08002573 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002574 rc = SPLIT_GO_OK;
2575
2576done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04002577 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
2578 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002579 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002580 return rc;
2581}