blob: 466ea8c8007a4a6f5cc9f40d30af195cfb9b4f88 [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/*
Fabio Utzig233af7d2019-08-26 12:06:16 -0300246 * Locate the TLVs in an image.
247 *
248 * @param hdr The image_header struct of the image being checked
249 * @param fap flash_area struct of the slot storing the image being checked
250 * @param off Address of the first TLV (after TLV info)
251 * @param end Address where TLV area ends
252 *
253 * Returns 0 on success.
254 */
255int
256boot_find_tlv_offs(const struct image_header *hdr, const struct flash_area *fap,
257 uint32_t *off, uint32_t *end)
258{
259 struct image_tlv_info info;
260 uint32_t off_;
261
262 off_ = BOOT_TLV_OFF(hdr);
263
264 if (flash_area_read(fap, off_, &info, sizeof(info))) {
265 return BOOT_EFLASH;
266 }
267
268 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
269 return BOOT_EBADIMAGE;
270 }
271
272 *end = off_ + info.it_tlv_tot;
273 *off = off_ + sizeof(info);
274 return 0;
275}
276
277/*
David Brownf5b33d82017-09-01 10:58:27 -0600278 * Compute the total size of the given image. Includes the size of
279 * the TLVs.
280 */
Fabio Utzig13d9e352017-10-05 20:32:31 -0300281#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600282static int
Fabio Utzigd638b172019-08-09 10:38:05 -0300283boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
David Brownf5b33d82017-09-01 10:58:27 -0600284{
285 const struct flash_area *fap;
Fabio Utzig233af7d2019-08-26 12:06:16 -0300286 uint32_t off;
David Brownf5b33d82017-09-01 10:58:27 -0600287 int area_id;
288 int rc;
289
Fabio Utzig10ee6482019-08-01 12:04:52 -0300290#if (BOOT_IMAGE_NUMBER == 1)
291 (void)state;
292#endif
293
294 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
David Brownf5b33d82017-09-01 10:58:27 -0600295 rc = flash_area_open(area_id, &fap);
296 if (rc != 0) {
297 rc = BOOT_EFLASH;
298 goto done;
299 }
300
Fabio Utzig233af7d2019-08-26 12:06:16 -0300301 rc = boot_find_tlv_offs(boot_img_hdr(state, slot), fap, &off, size);
David Brownf5b33d82017-09-01 10:58:27 -0600302 if (rc != 0) {
David Brownf5b33d82017-09-01 10:58:27 -0600303 goto done;
304 }
David Brownf5b33d82017-09-01 10:58:27 -0600305 rc = 0;
306
307done:
308 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300309 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600310}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300311#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600312
Fabio Utzigc08ed212017-06-20 19:28:36 -0300313static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300314boot_read_image_header(struct boot_loader_state *state, int slot,
315 struct image_header *out_hdr)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800316{
317 const struct flash_area *fap;
318 int area_id;
319 int rc;
320
Fabio Utzig10ee6482019-08-01 12:04:52 -0300321#if (BOOT_IMAGE_NUMBER == 1)
322 (void)state;
323#endif
324
325 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800326 rc = flash_area_open(area_id, &fap);
327 if (rc != 0) {
328 rc = BOOT_EFLASH;
329 goto done;
330 }
331
332 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
333 if (rc != 0) {
334 rc = BOOT_EFLASH;
335 goto done;
336 }
337
338 rc = 0;
339
340done:
341 flash_area_close(fap);
342 return rc;
343}
344
345static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300346boot_read_image_headers(struct boot_loader_state *state, bool require_all)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800347{
348 int rc;
349 int i;
350
351 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300352 rc = boot_read_image_header(state, i, boot_img_hdr(state, i));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800353 if (rc != 0) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200354 /* If `require_all` is set, fail on any single fail, otherwise
355 * if at least the first slot's header was read successfully,
356 * then the boot loader can attempt a boot.
357 *
358 * Failure to read any headers is a fatal error.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800359 */
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200360 if (i > 0 && !require_all) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800361 return 0;
362 } else {
363 return rc;
364 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800365 }
366 }
367
368 return 0;
369}
370
371static uint8_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300372boot_write_sz(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800373{
374 uint8_t elem_sz;
375 uint8_t align;
376
377 /* Figure out what size to write update status update as. The size depends
378 * on what the minimum write size is for scratch area, active image slot.
379 * We need to use the bigger of those 2 values.
380 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300381 elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
382 align = flash_area_align(BOOT_SCRATCH_AREA(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800383 if (align > elem_sz) {
384 elem_sz = align;
385 }
386
387 return elem_sz;
388}
389
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200390/*
391 * Slots are compatible when all sectors that store upto to size of the image
392 * round up to sector size, in both slot's are able to fit in the scratch
393 * area, and have sizes that are a multiple of each other (powers of two
394 * presumably!).
395 */
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800396static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300397boot_slots_compatible(struct boot_loader_state *state)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800398{
David Vincze2d736ad2019-02-18 11:50:22 +0100399 size_t num_sectors_primary;
400 size_t num_sectors_secondary;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200401 size_t sz0, sz1;
David Vincze2d736ad2019-02-18 11:50:22 +0100402 size_t primary_slot_sz, secondary_slot_sz;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200403 size_t scratch_sz;
404 size_t i, j;
405 int8_t smaller;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800406
Fabio Utzig10ee6482019-08-01 12:04:52 -0300407 num_sectors_primary = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
408 num_sectors_secondary = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT);
David Vincze2d736ad2019-02-18 11:50:22 +0100409 if ((num_sectors_primary > BOOT_MAX_IMG_SECTORS) ||
410 (num_sectors_secondary > BOOT_MAX_IMG_SECTORS)) {
Fabio Utziga1fae672018-03-30 10:52:38 -0300411 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800412 return 0;
413 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300414
Fabio Utzig10ee6482019-08-01 12:04:52 -0300415 scratch_sz = boot_scratch_area_size(state);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200416
417 /*
418 * The following loop scans all sectors in a linear fashion, assuring that
419 * for each possible sector in each slot, it is able to fit in the other
420 * slot's sector or sectors. Slot's should be compatible as long as any
421 * number of a slot's sectors are able to fit into another, which only
422 * excludes cases where sector sizes are not a multiple of each other.
423 */
David Vincze2d736ad2019-02-18 11:50:22 +0100424 i = sz0 = primary_slot_sz = 0;
425 j = sz1 = secondary_slot_sz = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200426 smaller = 0;
David Vincze2d736ad2019-02-18 11:50:22 +0100427 while (i < num_sectors_primary || j < num_sectors_secondary) {
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200428 if (sz0 == sz1) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300429 sz0 += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
430 sz1 += boot_img_sector_size(state, BOOT_SECONDARY_SLOT, j);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200431 i++;
432 j++;
433 } else if (sz0 < sz1) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300434 sz0 += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
David Vincze2d736ad2019-02-18 11:50:22 +0100435 /* Guarantee that multiple sectors of the secondary slot
436 * fit into the primary slot.
437 */
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200438 if (smaller == 2) {
439 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors");
440 return 0;
441 }
442 smaller = 1;
443 i++;
444 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300445 sz1 += boot_img_sector_size(state, BOOT_SECONDARY_SLOT, j);
David Vincze2d736ad2019-02-18 11:50:22 +0100446 /* Guarantee that multiple sectors of the primary slot
447 * fit into the secondary slot.
448 */
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200449 if (smaller == 1) {
450 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors");
451 return 0;
452 }
453 smaller = 2;
454 j++;
455 }
456 if (sz0 == sz1) {
David Vincze2d736ad2019-02-18 11:50:22 +0100457 primary_slot_sz += sz0;
458 secondary_slot_sz += sz1;
459 /* Scratch has to fit each swap operation to the size of the larger
460 * sector among the primary slot and the secondary slot.
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200461 */
462 if (sz0 > scratch_sz || sz1 > scratch_sz) {
463 BOOT_LOG_WRN("Cannot upgrade: not all sectors fit inside scratch");
464 return 0;
465 }
466 smaller = sz0 = sz1 = 0;
467 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300468 }
469
David Vincze2d736ad2019-02-18 11:50:22 +0100470 if ((i != num_sectors_primary) ||
471 (j != num_sectors_secondary) ||
472 (primary_slot_sz != secondary_slot_sz)) {
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200473 BOOT_LOG_WRN("Cannot upgrade: slots are not compatible");
474 return 0;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800475 }
476
477 return 1;
478}
479
Fabio Utzig10ee6482019-08-01 12:04:52 -0300480#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
481static int
482boot_initialize_area(struct boot_loader_state *state, int flash_area)
483{
484 int num_sectors = BOOT_MAX_IMG_SECTORS;
485 int rc;
486
487 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
488 rc = flash_area_to_sectors(flash_area, &num_sectors,
489 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors);
490 BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors = (size_t)num_sectors;
491
492 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
493 rc = flash_area_to_sectors(flash_area, &num_sectors,
494 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors);
495 BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors = (size_t)num_sectors;
496
497 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
498 rc = flash_area_to_sectors(flash_area, &num_sectors,
499 state->scratch.sectors);
500 state->scratch.num_sectors = (size_t)num_sectors;
501 } else {
502 return BOOT_EFLASH;
503 }
504
505 return rc;
506}
507#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
508static int
509boot_initialize_area(struct boot_loader_state *state, int flash_area)
510{
511 uint32_t num_sectors;
512 struct flash_sector *out_sectors;
513 size_t *out_num_sectors;
514 int rc;
515
516 num_sectors = BOOT_MAX_IMG_SECTORS;
517
518 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
519 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
520 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
521 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
522 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
523 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
524 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
525 out_sectors = state->scratch.sectors;
526 out_num_sectors = &state->scratch.num_sectors;
527 } else {
528 return BOOT_EFLASH;
529 }
530
531 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
532 if (rc != 0) {
533 return rc;
534 }
535 *out_num_sectors = num_sectors;
536 return 0;
537}
538#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
539
Christopher Collins92ea77f2016-12-12 15:59:26 -0800540/**
541 * Determines the sector layout of both image slots and the scratch area.
542 * This information is necessary for calculating the number of bytes to erase
543 * and copy during an image swap. The information collected during this
Fabio Utzig10ee6482019-08-01 12:04:52 -0300544 * function is used to populate the state.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800545 */
546static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300547boot_read_sectors(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800548{
Fabio Utzigb0f04732019-07-31 09:49:19 -0300549 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800550 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800551
Fabio Utzig10ee6482019-08-01 12:04:52 -0300552 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300553
554 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(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_SECONDARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800560 if (rc != 0) {
561 return BOOT_EFLASH;
562 }
563
Fabio Utzig10ee6482019-08-01 12:04:52 -0300564 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200565 if (rc != 0) {
566 return BOOT_EFLASH;
567 }
568
Fabio Utzig10ee6482019-08-01 12:04:52 -0300569 BOOT_WRITE_SZ(state) = boot_write_sz(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800570
571 return 0;
572}
573
574static uint32_t
575boot_status_internal_off(int idx, int state, int elem_sz)
576{
577 int idx_sz;
578
579 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
580
Fabio Utzig39000012018-07-30 12:40:20 -0300581 return (idx - BOOT_STATUS_IDX_0) * idx_sz +
582 (state - BOOT_STATUS_STATE_0) * elem_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800583}
584
585/**
586 * Reads the status of a partially-completed swap, if any. This is necessary
587 * to recover in case the boot lodaer was reset in the middle of a swap
588 * operation.
589 */
590static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300591boot_read_status_bytes(const struct flash_area *fap,
592 struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800593{
594 uint32_t off;
595 uint8_t status;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300596 int max_entries;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800597 int found;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200598 int found_idx;
599 int invalid;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800600 int rc;
601 int i;
602
603 off = boot_status_off(fap);
Fabio Utzig10ee6482019-08-01 12:04:52 -0300604 max_entries = boot_status_entries(BOOT_CURR_IMG(state), fap);
Fabio Utzig9d160092019-08-09 07:46:34 -0300605 if (max_entries < 0) {
606 return BOOT_EBADARGS;
607 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300608
Christopher Collins92ea77f2016-12-12 15:59:26 -0800609 found = 0;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200610 found_idx = 0;
611 invalid = 0;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300612 for (i = 0; i < max_entries; i++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300613 rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(state),
Fabio Utzig178be542018-09-19 08:12:56 -0300614 &status, 1);
615 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800616 return BOOT_EFLASH;
617 }
618
Fabio Utzig178be542018-09-19 08:12:56 -0300619 if (rc == 1) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200620 if (found && !found_idx) {
621 found_idx = i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800622 }
623 } else if (!found) {
624 found = 1;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200625 } else if (found_idx) {
626 invalid = 1;
627 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800628 }
629 }
630
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200631 if (invalid) {
632 /* This means there was an error writing status on the last
633 * swap. Tell user and move on to validation!
634 */
635 BOOT_LOG_ERR("Detected inconsistent status!");
636
David Vincze2d736ad2019-02-18 11:50:22 +0100637#if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
638 /* With validation of the primary slot disabled, there is no way
639 * to be sure the swapped primary slot is OK, so abort!
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200640 */
641 assert(0);
642#endif
643 }
644
Christopher Collins92ea77f2016-12-12 15:59:26 -0800645 if (found) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200646 if (!found_idx) {
647 found_idx = i;
648 }
649 found_idx--;
Fabio Utzig39000012018-07-30 12:40:20 -0300650 bs->idx = (found_idx / BOOT_STATUS_STATE_COUNT) + 1;
651 bs->state = (found_idx % BOOT_STATUS_STATE_COUNT) + 1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800652 }
653
654 return 0;
655}
656
657/**
658 * Reads the boot status from the flash. The boot status contains
659 * the current state of an interrupted image copy operation. If the boot
660 * status is not present, or it indicates that previous copy finished,
661 * there is no operation in progress.
662 */
663static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300664boot_read_status(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800665{
666 const struct flash_area *fap;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700667 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200668 uint8_t swap_info;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800669 int status_loc;
670 int area_id;
671 int rc;
672
673 memset(bs, 0, sizeof *bs);
Fabio Utzig39000012018-07-30 12:40:20 -0300674 bs->idx = BOOT_STATUS_IDX_0;
675 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700676 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800677
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700678#ifdef MCUBOOT_OVERWRITE_ONLY
679 /* Overwrite-only doesn't make use of the swap status area. */
680 return 0;
681#endif
682
Fabio Utzig10ee6482019-08-01 12:04:52 -0300683 status_loc = boot_status_source(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800684 switch (status_loc) {
685 case BOOT_STATUS_SOURCE_NONE:
686 return 0;
687
688 case BOOT_STATUS_SOURCE_SCRATCH:
689 area_id = FLASH_AREA_IMAGE_SCRATCH;
690 break;
691
David Vincze2d736ad2019-02-18 11:50:22 +0100692 case BOOT_STATUS_SOURCE_PRIMARY_SLOT:
Fabio Utzig10ee6482019-08-01 12:04:52 -0300693 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800694 break;
695
696 default:
697 assert(0);
698 return BOOT_EBADARGS;
699 }
700
701 rc = flash_area_open(area_id, &fap);
702 if (rc != 0) {
703 return BOOT_EFLASH;
704 }
705
Fabio Utzig10ee6482019-08-01 12:04:52 -0300706 rc = boot_read_status_bytes(fap, state, bs);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700707 if (rc == 0) {
David Vinczee2453472019-06-17 12:31:59 +0200708 off = boot_swap_info_off(fap);
709 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700710 if (rc == 1) {
David Vinczee2453472019-06-17 12:31:59 +0200711 BOOT_SET_SWAP_INFO(swap_info, 0, BOOT_SWAP_TYPE_NONE);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700712 rc = 0;
713 }
David Vinczee2453472019-06-17 12:31:59 +0200714
715 /* Extract the swap type info */
716 bs->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700717 }
Fabio Utzig46490722017-09-04 15:34:32 -0300718
719 flash_area_close(fap);
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700720
Fabio Utzig46490722017-09-04 15:34:32 -0300721 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800722}
723
724/**
725 * Writes the supplied boot status to the flash file system. The boot status
726 * contains the current state of an in-progress image copy operation.
727 *
728 * @param bs The boot status to write.
729 *
730 * @return 0 on success; nonzero on failure.
731 */
732int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300733boot_write_status(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800734{
735 const struct flash_area *fap;
736 uint32_t off;
737 int area_id;
738 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300739 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700740 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300741 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800742
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300743 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100744 * the trailer. Since in the last step the primary slot is erased, the
745 * first two status writes go to the scratch which will be copied to
746 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300747 */
748
Fabio Utzig2473ac02017-05-02 12:45:02 -0300749 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800750 /* Write to scratch. */
751 area_id = FLASH_AREA_IMAGE_SCRATCH;
752 } else {
David Vincze2d736ad2019-02-18 11:50:22 +0100753 /* Write to the primary slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300754 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800755 }
756
757 rc = flash_area_open(area_id, &fap);
758 if (rc != 0) {
759 rc = BOOT_EFLASH;
760 goto done;
761 }
762
763 off = boot_status_off(fap) +
Fabio Utzigd638b172019-08-09 10:38:05 -0300764 boot_status_internal_off(bs->idx, bs->state, BOOT_WRITE_SZ(state));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200765 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300766 erased_val = flash_area_erased_val(fap);
767 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700768 buf[0] = bs->state;
769
770 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800771 if (rc != 0) {
772 rc = BOOT_EFLASH;
773 goto done;
774 }
775
776 rc = 0;
777
778done:
779 flash_area_close(fap);
780 return rc;
781}
782
783/*
784 * Validate image hash/signature in a slot.
785 */
786static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300787boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
788 const struct flash_area *fap, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800789{
Fabio Utzig10ee6482019-08-01 12:04:52 -0300790 TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigb0f04732019-07-31 09:49:19 -0300791 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300792 int rc;
793
Fabio Utzig10ee6482019-08-01 12:04:52 -0300794#if (BOOT_IMAGE_NUMBER == 1)
795 (void)state;
796#endif
797
Fabio Utzigba829042018-09-18 08:29:34 -0300798 (void)bs;
799 (void)rc;
Fabio Utzigbc077932019-08-26 11:16:34 -0300800
801 image_index = BOOT_CURR_IMG(state);
802
803#ifdef MCUBOOT_ENC_IMAGES
804 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300805 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -0300806 if (rc < 0) {
807 return BOOT_EBADIMAGE;
808 }
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300809 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs->enckey[1])) {
Fabio Utzigba829042018-09-18 08:29:34 -0300810 return BOOT_EBADIMAGE;
811 }
812 }
Fabio Utzigbc077932019-08-26 11:16:34 -0300813#endif
814
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300815 if (bootutil_img_validate(BOOT_CURR_ENC(state), image_index, hdr, fap, tmpbuf,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300816 BOOT_TMPBUF_SZ, NULL, 0, NULL)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800817 return BOOT_EBADIMAGE;
818 }
Fabio Utzig10ee6482019-08-01 12:04:52 -0300819
Christopher Collins92ea77f2016-12-12 15:59:26 -0800820 return 0;
821}
822
823static int
824split_image_check(struct image_header *app_hdr,
825 const struct flash_area *app_fap,
826 struct image_header *loader_hdr,
827 const struct flash_area *loader_fap)
828{
829 static void *tmpbuf;
830 uint8_t loader_hash[32];
831
832 if (!tmpbuf) {
833 tmpbuf = malloc(BOOT_TMPBUF_SZ);
834 if (!tmpbuf) {
835 return BOOT_ENOMEM;
836 }
837 }
838
Fabio Utzig10ee6482019-08-01 12:04:52 -0300839 if (bootutil_img_validate(NULL, 0, loader_hdr, loader_fap, tmpbuf,
840 BOOT_TMPBUF_SZ, NULL, 0, loader_hash)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800841 return BOOT_EBADIMAGE;
842 }
843
Fabio Utzig10ee6482019-08-01 12:04:52 -0300844 if (bootutil_img_validate(NULL, 0, app_hdr, app_fap, tmpbuf,
845 BOOT_TMPBUF_SZ, loader_hash, 32, NULL)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800846 return BOOT_EBADIMAGE;
847 }
848
849 return 0;
850}
851
Fabio Utzig338a19f2018-12-03 08:37:08 -0200852/*
853 * Check that a memory area consists of a given value.
854 */
855static inline bool
856boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300857{
858 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200859 uint8_t *p = (uint8_t *)data;
860 for (i = 0; i < len; i++) {
861 if (val != p[i]) {
862 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300863 }
864 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200865 return true;
866}
867
868static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300869boot_check_header_erased(struct boot_loader_state *state, int slot)
Fabio Utzig338a19f2018-12-03 08:37:08 -0200870{
871 const struct flash_area *fap;
872 struct image_header *hdr;
873 uint8_t erased_val;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300874 int area_id;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200875 int rc;
876
Fabio Utzig10ee6482019-08-01 12:04:52 -0300877 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300878 rc = flash_area_open(area_id, &fap);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200879 if (rc != 0) {
880 return -1;
881 }
882
883 erased_val = flash_area_erased_val(fap);
884 flash_area_close(fap);
885
Fabio Utzig10ee6482019-08-01 12:04:52 -0300886 hdr = boot_img_hdr(state, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200887 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
888 return -1;
889 }
890
891 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300892}
893
Christopher Collins92ea77f2016-12-12 15:59:26 -0800894static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300895boot_validate_slot(struct boot_loader_state *state, int slot,
896 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800897{
898 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400899 struct image_header *hdr;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300900 int area_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800901 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300902
Fabio Utzig10ee6482019-08-01 12:04:52 -0300903 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300904 rc = flash_area_open(area_id, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800905 if (rc != 0) {
906 return BOOT_EFLASH;
907 }
908
Fabio Utzig10ee6482019-08-01 12:04:52 -0300909 hdr = boot_img_hdr(state, slot);
910 if (boot_check_header_erased(state, slot) == 0 ||
911 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100912 /* No bootable image in slot; continue booting from the primary slot. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200913 rc = -1;
914 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300915 }
916
Fabio Utzig10ee6482019-08-01 12:04:52 -0300917 if (hdr->ih_magic != IMAGE_MAGIC || boot_image_check(state, hdr, fap, bs)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100918 if (slot != BOOT_PRIMARY_SLOT) {
David Brownb38e0442017-02-24 13:57:12 -0700919 flash_area_erase(fap, 0, fap->fa_size);
David Vincze2d736ad2019-02-18 11:50:22 +0100920 /* Image in the secondary slot is invalid. Erase the image and
921 * continue booting from the primary slot.
David Brownb38e0442017-02-24 13:57:12 -0700922 */
923 }
David Vincze2d736ad2019-02-18 11:50:22 +0100924 BOOT_LOG_ERR("Image in the %s slot is not valid!",
925 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
Fabio Utzig338a19f2018-12-03 08:37:08 -0200926 rc = -1;
927 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800928 }
929
David Vincze2d736ad2019-02-18 11:50:22 +0100930 /* Image in the secondary slot is valid. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200931 rc = 0;
932
933out:
934 flash_area_close(fap);
935 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800936}
937
938/**
939 * Determines which swap operation to perform, if any. If it is determined
David Vincze2d736ad2019-02-18 11:50:22 +0100940 * that a swap operation is required, the image in the secondary slot is checked
941 * for validity. If the image in the secondary slot is invalid, it is erased,
942 * and a swap type of "none" is indicated.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800943 *
944 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
945 */
946static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300947boot_validated_swap_type(struct boot_loader_state *state,
948 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800949{
950 int swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800951
Fabio Utzigb0f04732019-07-31 09:49:19 -0300952 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300953 switch (swap_type) {
954 case BOOT_SWAP_TYPE_TEST:
955 case BOOT_SWAP_TYPE_PERM:
956 case BOOT_SWAP_TYPE_REVERT:
David Vincze2d736ad2019-02-18 11:50:22 +0100957 /* Boot loader wants to switch to the secondary slot.
958 * Ensure image is valid.
959 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300960 if (boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) != 0) {
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300961 swap_type = BOOT_SWAP_TYPE_FAIL;
962 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800963 }
964
965 return swap_type;
966}
967
968/**
969 * Calculates the number of sectors the scratch area can contain. A "last"
970 * source sector is specified because images are copied backwards in flash
971 * (final index to index number 0).
972 *
973 * @param last_sector_idx The index of the last source sector
974 * (inclusive).
975 * @param out_first_sector_idx The index of the first source sector
976 * (inclusive) gets written here.
977 *
978 * @return The number of bytes comprised by the
979 * [first-sector, last-sector] range.
980 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300981#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800982static uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300983boot_copy_sz(struct boot_loader_state *state, int last_sector_idx,
984 int *out_first_sector_idx)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800985{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400986 size_t scratch_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800987 uint32_t new_sz;
988 uint32_t sz;
989 int i;
990
991 sz = 0;
992
Fabio Utzig10ee6482019-08-01 12:04:52 -0300993 scratch_sz = boot_scratch_area_size(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800994 for (i = last_sector_idx; i >= 0; i--) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300995 new_sz = sz + boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200996 /*
David Vincze2d736ad2019-02-18 11:50:22 +0100997 * The secondary slot is not being checked here, because
998 * `boot_slots_compatible` already provides assurance that the copy size
999 * will be compatible with the primary slot and scratch.
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001000 */
Marti Bolivard3269fd2017-06-12 16:31:12 -04001001 if (new_sz > scratch_sz) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001002 break;
1003 }
1004 sz = new_sz;
1005 }
1006
1007 /* i currently refers to a sector that doesn't fit or it is -1 because all
1008 * sectors have been processed. In both cases, exclude sector i.
1009 */
1010 *out_first_sector_idx = i + 1;
1011 return sz;
1012}
Fabio Utzig3488eef2017-06-12 10:25:43 -03001013#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001014
1015/**
1016 * Erases a region of flash.
1017 *
Fabio Utzigba829042018-09-18 08:29:34 -03001018 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001019 * @param off The offset within the flash area to start the
1020 * erase.
1021 * @param sz The number of bytes to erase.
1022 *
1023 * @return 0 on success; nonzero on failure.
1024 */
Fabio Utzigba829042018-09-18 08:29:34 -03001025static inline int
1026boot_erase_sector(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001027{
Fabio Utzigba829042018-09-18 08:29:34 -03001028 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001029}
1030
1031/**
1032 * Copies the contents of one flash region to another. You must erase the
1033 * destination region prior to calling this function.
1034 *
1035 * @param flash_area_id_src The ID of the source flash area.
1036 * @param flash_area_id_dst The ID of the destination flash area.
1037 * @param off_src The offset within the source flash area to
1038 * copy from.
1039 * @param off_dst The offset within the destination flash area to
1040 * copy to.
1041 * @param sz The number of bytes to copy.
1042 *
1043 * @return 0 on success; nonzero on failure.
1044 */
1045static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001046boot_copy_sector(struct boot_loader_state *state,
1047 const struct flash_area *fap_src,
Fabio Utzigba829042018-09-18 08:29:34 -03001048 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -08001049 uint32_t off_src, uint32_t off_dst, uint32_t sz)
1050{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001051 uint32_t bytes_copied;
1052 int chunk_sz;
1053 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -03001054#ifdef MCUBOOT_ENC_IMAGES
1055 uint32_t off;
Fabio Utziga87cc7d2019-08-26 11:21:45 -03001056 uint32_t tlv_off;
Fabio Utzigba829042018-09-18 08:29:34 -03001057 size_t blk_off;
1058 struct image_header *hdr;
1059 uint16_t idx;
1060 uint32_t blk_sz;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001061 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -03001062#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001063
Fabio Utzig10ee6482019-08-01 12:04:52 -03001064 TARGET_STATIC uint8_t buf[1024];
1065
1066#if !defined(MCUBOOT_ENC_IMAGES)
1067 (void)state;
1068#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001069
Christopher Collins92ea77f2016-12-12 15:59:26 -08001070 bytes_copied = 0;
1071 while (bytes_copied < sz) {
1072 if (sz - bytes_copied > sizeof buf) {
1073 chunk_sz = sizeof buf;
1074 } else {
1075 chunk_sz = sz - bytes_copied;
1076 }
1077
1078 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
1079 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001080 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001081 }
1082
Fabio Utzigba829042018-09-18 08:29:34 -03001083#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001084 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001085 if (fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) ||
1086 fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001087 /* assume the secondary slot as src, needs decryption */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001088 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001089 off = off_src;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001090 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001091 /* might need encryption (metadata from the primary slot) */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001092 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001093 off = off_dst;
1094 }
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001095 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001096 blk_sz = chunk_sz;
1097 idx = 0;
1098 if (off + bytes_copied < hdr->ih_hdr_size) {
1099 /* do not decrypt header */
1100 blk_off = 0;
1101 blk_sz = chunk_sz - hdr->ih_hdr_size;
1102 idx = hdr->ih_hdr_size;
1103 } else {
1104 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
1105 }
Fabio Utziga87cc7d2019-08-26 11:21:45 -03001106 tlv_off = BOOT_TLV_OFF(hdr);
1107 if (off + bytes_copied + chunk_sz > tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -03001108 /* do not decrypt TLVs */
Fabio Utziga87cc7d2019-08-26 11:21:45 -03001109 if (off + bytes_copied >= tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -03001110 blk_sz = 0;
1111 } else {
Fabio Utziga87cc7d2019-08-26 11:21:45 -03001112 blk_sz = tlv_off - (off + bytes_copied);
Fabio Utzigba829042018-09-18 08:29:34 -03001113 }
1114 }
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001115 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
Fabio Utzigb0f04732019-07-31 09:49:19 -03001116 (off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
1117 blk_off, &buf[idx]);
Fabio Utzigba829042018-09-18 08:29:34 -03001118 }
1119 }
1120#endif
1121
Christopher Collins92ea77f2016-12-12 15:59:26 -08001122 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
1123 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001124 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001125 }
1126
1127 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -03001128
1129 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -08001130 }
1131
Fabio Utzigba829042018-09-18 08:29:34 -03001132 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001133}
1134
David Brown6b1b3b92017-09-19 08:59:10 -06001135#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -03001136static inline int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001137boot_status_init(const struct boot_loader_state *state,
1138 const struct flash_area *fap,
1139 const struct boot_status *bs)
Fabio Utzig2473ac02017-05-02 12:45:02 -03001140{
Fabio Utzig2473ac02017-05-02 12:45:02 -03001141 struct boot_swap_state swap_state;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001142 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001143 int rc;
1144
Fabio Utzig10ee6482019-08-01 12:04:52 -03001145#if (BOOT_IMAGE_NUMBER == 1)
1146 (void)state;
1147#endif
1148
1149 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001150
Christopher Collins2c88e692019-05-22 15:10:14 -07001151 BOOT_LOG_DBG("initializing status; fa_id=%d", fap->fa_id);
1152
Fabio Utzigb0f04732019-07-31 09:49:19 -03001153 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
1154 &swap_state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001155 assert(rc == 0);
1156
Christopher Collinsa1c12042019-05-23 14:00:28 -07001157 if (bs->swap_type != BOOT_SWAP_TYPE_NONE) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03001158 rc = boot_write_swap_info(fap, bs->swap_type, image_index);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001159 assert(rc == 0);
1160 }
1161
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001162 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001163 rc = boot_write_image_ok(fap);
1164 assert(rc == 0);
1165 }
1166
Fabio Utzig46490722017-09-04 15:34:32 -03001167 rc = boot_write_swap_size(fap, bs->swap_size);
1168 assert(rc == 0);
1169
Fabio Utzigba829042018-09-18 08:29:34 -03001170#ifdef MCUBOOT_ENC_IMAGES
1171 rc = boot_write_enc_key(fap, 0, bs->enckey[0]);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001172 assert(rc == 0);
1173
Fabio Utzigba829042018-09-18 08:29:34 -03001174 rc = boot_write_enc_key(fap, 1, bs->enckey[1]);
1175 assert(rc == 0);
1176#endif
1177
1178 rc = boot_write_magic(fap);
1179 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001180
1181 return 0;
1182}
Christopher Collinsa1c12042019-05-23 14:00:28 -07001183
David Brown6b1b3b92017-09-19 08:59:10 -06001184#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001185
Fabio Utzig358c9352017-07-25 22:10:45 -03001186#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -03001187static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001188boot_erase_trailer_sectors(const struct boot_loader_state *state,
1189 const struct flash_area *fap)
Fabio Utzig2473ac02017-05-02 12:45:02 -03001190{
1191 uint8_t slot;
Fabio Utziged0ca432019-01-23 14:50:11 -02001192 uint32_t sector;
1193 uint32_t trailer_sz;
1194 uint32_t total_sz;
1195 uint32_t off;
1196 uint32_t sz;
David Vinczeb75c12a2019-03-22 14:58:33 +01001197 int fa_id_primary;
1198 int fa_id_secondary;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001199 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001200 int rc;
1201
Christopher Collins2c88e692019-05-22 15:10:14 -07001202 BOOT_LOG_DBG("erasing trailer; fa_id=%d", fap->fa_id);
1203
Fabio Utzig10ee6482019-08-01 12:04:52 -03001204 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001205 fa_id_primary = flash_area_id_from_multi_image_slot(image_index,
1206 BOOT_PRIMARY_SLOT);
1207 fa_id_secondary = flash_area_id_from_multi_image_slot(image_index,
1208 BOOT_SECONDARY_SLOT);
David Vinczeb75c12a2019-03-22 14:58:33 +01001209
1210 if (fap->fa_id == fa_id_primary) {
David Vincze2d736ad2019-02-18 11:50:22 +01001211 slot = BOOT_PRIMARY_SLOT;
David Vinczeb75c12a2019-03-22 14:58:33 +01001212 } else if (fap->fa_id == fa_id_secondary) {
David Vincze2d736ad2019-02-18 11:50:22 +01001213 slot = BOOT_SECONDARY_SLOT;
David Vinczeb75c12a2019-03-22 14:58:33 +01001214 } else {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001215 return BOOT_EFLASH;
1216 }
1217
Fabio Utziged0ca432019-01-23 14:50:11 -02001218 /* delete starting from last sector and moving to beginning */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001219 sector = boot_img_num_sectors(state, slot) - 1;
1220 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
Fabio Utziged0ca432019-01-23 14:50:11 -02001221 total_sz = 0;
1222 do {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001223 sz = boot_img_sector_size(state, slot, sector);
1224 off = boot_img_sector_off(state, slot, sector);
Fabio Utziged0ca432019-01-23 14:50:11 -02001225 rc = boot_erase_sector(fap, off, sz);
1226 assert(rc == 0);
1227
1228 sector--;
1229 total_sz += sz;
1230 } while (total_sz < trailer_sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001231
1232 return rc;
1233}
Fabio Utzig358c9352017-07-25 22:10:45 -03001234#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001235
Christopher Collins92ea77f2016-12-12 15:59:26 -08001236/**
1237 * Swaps the contents of two flash regions within the two image slots.
1238 *
1239 * @param idx The index of the first sector in the range of
1240 * sectors being swapped.
1241 * @param sz The number of bytes to swap.
1242 * @param bs The current boot status. This struct gets
1243 * updated according to the outcome.
1244 *
1245 * @return 0 on success; nonzero on failure.
1246 */
Fabio Utzig3488eef2017-06-12 10:25:43 -03001247#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -08001248static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001249boot_swap_sectors(int idx, uint32_t sz, struct boot_loader_state *state,
1250 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001251{
David Vincze2d736ad2019-02-18 11:50:22 +01001252 const struct flash_area *fap_primary_slot;
1253 const struct flash_area *fap_secondary_slot;
Fabio Utzigba829042018-09-18 08:29:34 -03001254 const struct flash_area *fap_scratch;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001255 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001256 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001257 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001258 uint32_t scratch_trailer_off;
1259 struct boot_swap_state swap_state;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001260 size_t last_sector;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001261 bool erase_scratch;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001262 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001263 int rc;
1264
1265 /* Calculate offset from start of image area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001266 img_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001267
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001268 copy_sz = sz;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001269 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
Fabio Utzig9678c972017-05-23 11:28:56 -04001270
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001271 /* sz in this function is always sized on a multiple of the sector size.
1272 * The check against the start offset of the last sector
Fabio Utzig9678c972017-05-23 11:28:56 -04001273 * is to determine if we're swapping the last sector. The last sector
1274 * needs special handling because it's where the trailer lives. If we're
1275 * copying it, we need to use scratch to write the trailer temporarily.
1276 *
1277 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
1278 * controls if special handling is needed (swapping last sector).
1279 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001280 last_sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
David Vinczeba3bd602019-06-17 16:01:43 +02001281 if ((img_off + sz) >
Fabio Utzig10ee6482019-08-01 12:04:52 -03001282 boot_img_sector_off(state, BOOT_PRIMARY_SLOT, last_sector)) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001283 copy_sz -= trailer_sz;
1284 }
1285
Fabio Utzig39000012018-07-30 12:40:20 -03001286 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001287
Fabio Utzig10ee6482019-08-01 12:04:52 -03001288 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001289
1290 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1291 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001292 assert (rc == 0);
1293
Fabio Utzigb0f04732019-07-31 09:49:19 -03001294 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1295 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001296 assert (rc == 0);
1297
1298 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch);
1299 assert (rc == 0);
1300
Fabio Utzig39000012018-07-30 12:40:20 -03001301 if (bs->state == BOOT_STATUS_STATE_0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001302 BOOT_LOG_DBG("erasing scratch area");
Christopher Collinsa1c12042019-05-23 14:00:28 -07001303 rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size);
Christopher Collins4772ac42017-02-27 20:08:01 -08001304 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001305
Fabio Utzig39000012018-07-30 12:40:20 -03001306 if (bs->idx == BOOT_STATUS_IDX_0) {
Christopher Collinsa1c12042019-05-23 14:00:28 -07001307 /* Write a trailer to the scratch area, even if we don't need the
1308 * scratch area for status. We need a temporary place to store the
1309 * `swap-type` while we erase the primary trailer.
1310 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001311 rc = boot_status_init(state, fap_scratch, bs);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001312 assert(rc == 0);
1313
1314 if (!bs->use_scratch) {
1315 /* Prepare the primary status area... here it is known that the
1316 * last sector is not being used by the image data so it's safe
1317 * to erase.
Fabio Utzig2473ac02017-05-02 12:45:02 -03001318 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001319 rc = boot_erase_trailer_sectors(state, fap_primary_slot);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001320 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001321
Fabio Utzig10ee6482019-08-01 12:04:52 -03001322 rc = boot_status_init(state, fap_primary_slot, bs);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001323 assert(rc == 0);
1324
1325 /* Erase the temporary trailer from the scratch area. */
1326 rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size);
1327 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001328 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001329 }
1330
Fabio Utzig10ee6482019-08-01 12:04:52 -03001331 rc = boot_copy_sector(state, fap_secondary_slot, fap_scratch,
Christopher Collinsa1c12042019-05-23 14:00:28 -07001332 img_off, 0, copy_sz);
1333 assert(rc == 0);
1334
Fabio Utzig39000012018-07-30 12:40:20 -03001335 bs->state = BOOT_STATUS_STATE_1;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001336 rc = boot_write_status(state, bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001337 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001338 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001339
Fabio Utzig39000012018-07-30 12:40:20 -03001340 if (bs->state == BOOT_STATUS_STATE_1) {
David Vincze2d736ad2019-02-18 11:50:22 +01001341 rc = boot_erase_sector(fap_secondary_slot, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001342 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001343
Fabio Utzig10ee6482019-08-01 12:04:52 -03001344 rc = boot_copy_sector(state, fap_primary_slot, fap_secondary_slot,
David Vincze2d736ad2019-02-18 11:50:22 +01001345 img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001346 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001347
Fabio Utzig39000012018-07-30 12:40:20 -03001348 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001349 /* If not all sectors of the slot are being swapped,
David Vincze2d736ad2019-02-18 11:50:22 +01001350 * guarantee here that only the primary slot will have the state.
Fabio Utzig2473ac02017-05-02 12:45:02 -03001351 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001352 rc = boot_erase_trailer_sectors(state, fap_secondary_slot);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001353 assert(rc == 0);
1354 }
1355
Fabio Utzig39000012018-07-30 12:40:20 -03001356 bs->state = BOOT_STATUS_STATE_2;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001357 rc = boot_write_status(state, bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001358 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001359 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001360
Fabio Utzig39000012018-07-30 12:40:20 -03001361 if (bs->state == BOOT_STATUS_STATE_2) {
David Vincze2d736ad2019-02-18 11:50:22 +01001362 rc = boot_erase_sector(fap_primary_slot, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001363 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001364
Christopher Collinsa1c12042019-05-23 14:00:28 -07001365 /* NOTE: If this is the final sector, we exclude the image trailer from
1366 * this copy (copy_sz was truncated earlier).
1367 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001368 rc = boot_copy_sector(state, fap_scratch, fap_primary_slot,
David Vincze2d736ad2019-02-18 11:50:22 +01001369 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001370 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001371
Fabio Utzig94d998c2017-05-22 11:02:41 -04001372 if (bs->use_scratch) {
Fabio Utzigba829042018-09-18 08:29:34 -03001373 scratch_trailer_off = boot_status_off(fap_scratch);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001374
1375 /* copy current status that is being maintained in scratch */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001376 rc = boot_copy_sector(state, fap_scratch, fap_primary_slot,
David Vincze2d736ad2019-02-18 11:50:22 +01001377 scratch_trailer_off, img_off + copy_sz,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001378 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(state));
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001379 BOOT_STATUS_ASSERT(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001380
Fabio Utzig2473ac02017-05-02 12:45:02 -03001381 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1382 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001383 assert(rc == 0);
1384
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001385 if (swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze2d736ad2019-02-18 11:50:22 +01001386 rc = boot_write_image_ok(fap_primary_slot);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001387 assert(rc == 0);
1388 }
1389
Christopher Collinsa1c12042019-05-23 14:00:28 -07001390 if (swap_state.swap_type != BOOT_SWAP_TYPE_NONE) {
David Vinczee2453472019-06-17 12:31:59 +02001391 rc = boot_write_swap_info(fap_primary_slot,
Fabio Utzigb0f04732019-07-31 09:49:19 -03001392 swap_state.swap_type, image_index);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001393 assert(rc == 0);
1394 }
1395
David Vincze2d736ad2019-02-18 11:50:22 +01001396 rc = boot_write_swap_size(fap_primary_slot, bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001397 assert(rc == 0);
1398
Fabio Utzigba829042018-09-18 08:29:34 -03001399#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +01001400 rc = boot_write_enc_key(fap_primary_slot, 0, bs->enckey[0]);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001401 assert(rc == 0);
1402
David Vincze2d736ad2019-02-18 11:50:22 +01001403 rc = boot_write_enc_key(fap_primary_slot, 1, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001404 assert(rc == 0);
1405#endif
David Vincze2d736ad2019-02-18 11:50:22 +01001406 rc = boot_write_magic(fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001407 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001408 }
1409
Christopher Collinsa1c12042019-05-23 14:00:28 -07001410 /* If we wrote a trailer to the scratch area, erase it after we persist
1411 * a trailer to the primary slot. We do this to prevent mcuboot from
1412 * reading a stale status from the scratch area in case of immediate
1413 * reset.
1414 */
1415 erase_scratch = bs->use_scratch;
1416 bs->use_scratch = 0;
1417
Christopher Collins92ea77f2016-12-12 15:59:26 -08001418 bs->idx++;
Fabio Utzig39000012018-07-30 12:40:20 -03001419 bs->state = BOOT_STATUS_STATE_0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001420 rc = boot_write_status(state, bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001421 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001422
1423 if (erase_scratch) {
1424 rc = boot_erase_sector(fap_scratch, 0, sz);
1425 assert(rc == 0);
1426 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001427 }
Fabio Utzigba829042018-09-18 08:29:34 -03001428
David Vincze2d736ad2019-02-18 11:50:22 +01001429 flash_area_close(fap_primary_slot);
1430 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001431 flash_area_close(fap_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001432}
Fabio Utzig3488eef2017-06-12 10:25:43 -03001433#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001434
1435/**
David Vincze2d736ad2019-02-18 11:50:22 +01001436 * Overwrite primary slot with the image contained in the secondary slot.
1437 * If a prior copy operation was interrupted by a system reset, this function
1438 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001439 *
1440 * @param bs The current boot status. This function reads
1441 * this struct to determine if it is resuming
1442 * an interrupted swap operation. This
1443 * function writes the updated status to this
1444 * function on return.
1445 *
1446 * @return 0 on success; nonzero on failure.
1447 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001448#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001449static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001450boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
David Brown17609d82017-05-05 09:41:34 -06001451{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001452 size_t sect_count;
1453 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001454 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001455 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001456 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001457 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +01001458 const struct flash_area *fap_primary_slot;
1459 const struct flash_area *fap_secondary_slot;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001460 uint8_t image_index;
David Vincze2d736ad2019-02-18 11:50:22 +01001461
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001462 (void)bs;
1463
Fabio Utzig13d9e352017-10-05 20:32:31 -03001464#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1465 uint32_t src_size = 0;
Fabio Utzigd638b172019-08-09 10:38:05 -03001466 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001467 assert(rc == 0);
1468#endif
David Brown17609d82017-05-05 09:41:34 -06001469
David Vincze2d736ad2019-02-18 11:50:22 +01001470 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1471 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -06001472
Fabio Utzigb0f04732019-07-31 09:49:19 -03001473 image_index = BOOT_CURR_IMG(state);
1474
1475 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1476 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001477 assert (rc == 0);
1478
Fabio Utzigb0f04732019-07-31 09:49:19 -03001479 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1480 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001481 assert (rc == 0);
1482
Fabio Utzig10ee6482019-08-01 12:04:52 -03001483 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001484 for (sect = 0, size = 0; sect < sect_count; sect++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001485 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
David Vincze2d736ad2019-02-18 11:50:22 +01001486 rc = boot_erase_sector(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001487 assert(rc == 0);
1488
1489 size += this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001490
1491#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1492 if (size >= src_size) {
1493 break;
1494 }
1495#endif
David Brown17609d82017-05-05 09:41:34 -06001496 }
1497
Fabio Utzigba829042018-09-18 08:29:34 -03001498#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001499 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001500 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001501 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
Fabio Utzigb0f04732019-07-31 09:49:19 -03001502 fap_secondary_slot, bs->enckey[1]);
David Vincze2d736ad2019-02-18 11:50:22 +01001503
Fabio Utzigba829042018-09-18 08:29:34 -03001504 if (rc < 0) {
1505 return BOOT_EBADIMAGE;
1506 }
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001507 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs->enckey[1])) {
Fabio Utzigba829042018-09-18 08:29:34 -03001508 return BOOT_EBADIMAGE;
1509 }
1510 }
1511#endif
1512
David Vincze2d736ad2019-02-18 11:50:22 +01001513 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1514 size);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001515 rc = boot_copy_sector(state, fap_secondary_slot, fap_primary_slot, 0, 0, size);
David Brown17609d82017-05-05 09:41:34 -06001516
Fabio Utzig13d9e352017-10-05 20:32:31 -03001517 /*
1518 * Erases header and trailer. The trailer is erased because when a new
1519 * image is written without a trailer as is the case when using newt, the
1520 * trailer that was left might trigger a new upgrade.
1521 */
Christopher Collins2c88e692019-05-22 15:10:14 -07001522 BOOT_LOG_DBG("erasing secondary header");
David Vincze2d736ad2019-02-18 11:50:22 +01001523 rc = boot_erase_sector(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001524 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
1525 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001526 assert(rc == 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001527 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -07001528 BOOT_LOG_DBG("erasing secondary trailer");
David Vincze2d736ad2019-02-18 11:50:22 +01001529 rc = boot_erase_sector(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001530 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
1531 last_sector),
1532 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
1533 last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001534 assert(rc == 0);
1535
David Vincze2d736ad2019-02-18 11:50:22 +01001536 flash_area_close(fap_primary_slot);
1537 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001538
David Vincze2d736ad2019-02-18 11:50:22 +01001539 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001540
1541 return 0;
1542}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001543#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001544
Christopher Collinsa1c12042019-05-23 14:00:28 -07001545#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -03001546/**
1547 * Swaps the two images in flash. If a prior copy operation was interrupted
1548 * by a system reset, this function completes that operation.
1549 *
1550 * @param bs The current boot status. This function reads
1551 * this struct to determine if it is resuming
1552 * an interrupted swap operation. This
1553 * function writes the updated status to this
1554 * function on return.
1555 *
1556 * @return 0 on success; nonzero on failure.
1557 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001558static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001559boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001560{
1561 uint32_t sz;
1562 int first_sector_idx;
1563 int last_sector_idx;
David Vincze2d736ad2019-02-18 11:50:22 +01001564 int last_idx_secondary_slot;
Fabio Utzigcd5774b2017-11-29 10:18:26 -02001565 uint32_t swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001566 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001567#ifdef MCUBOOT_ENC_IMAGES
1568 const struct flash_area *fap;
1569 uint8_t slot;
1570 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001571#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001572 uint32_t size;
1573 uint32_t copy_size;
David Vincze2d736ad2019-02-18 11:50:22 +01001574 uint32_t primary_slot_size;
1575 uint32_t secondary_slot_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001576 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001577 int rc;
1578
1579 /* FIXME: just do this if asked by user? */
1580
1581 size = copy_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001582 image_index = BOOT_CURR_IMG(state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001583
Fabio Utzig39000012018-07-30 12:40:20 -03001584 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Fabio Utzig46490722017-09-04 15:34:32 -03001585 /*
1586 * No swap ever happened, so need to find the largest image which
1587 * will be used to determine the amount of sectors to swap.
1588 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001589 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001590 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001591 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001592 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001593 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001594
Fabio Utzigba829042018-09-18 08:29:34 -03001595#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001596 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001597 fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001598 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs->enckey[0]);
Fabio Utzigba829042018-09-18 08:29:34 -03001599 assert(rc >= 0);
1600
1601 if (rc == 0) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001602 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 0, bs->enckey[0]);
Fabio Utzigba829042018-09-18 08:29:34 -03001603 assert(rc == 0);
1604 } else {
1605 rc = 0;
1606 }
1607 } else {
1608 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1609 }
1610#endif
1611
Fabio Utzig10ee6482019-08-01 12:04:52 -03001612 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001613 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001614 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001615 assert(rc == 0);
1616 }
1617
Fabio Utzigba829042018-09-18 08:29:34 -03001618#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001619 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001620 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001621 fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001622 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001623 assert(rc >= 0);
1624
1625 if (rc == 0) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001626 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001627 assert(rc == 0);
1628 } else {
1629 rc = 0;
1630 }
1631 } else {
1632 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1633 }
1634#endif
1635
Fabio Utzig46490722017-09-04 15:34:32 -03001636 if (size > copy_size) {
1637 copy_size = size;
1638 }
1639
1640 bs->swap_size = copy_size;
1641 } else {
1642 /*
1643 * If a swap was under way, the swap_size should already be present
1644 * in the trailer...
1645 */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001646 rc = boot_read_swap_size(image_index, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001647 assert(rc == 0);
1648
1649 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001650
1651#ifdef MCUBOOT_ENC_IMAGES
1652 for (slot = 0; slot <= 1; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03001653 rc = boot_read_enc_key(image_index, slot, bs->enckey[slot]);
Fabio Utzigba829042018-09-18 08:29:34 -03001654 assert(rc == 0);
1655
1656 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001657 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001658 break;
1659 }
1660 }
1661
1662 if (i != BOOT_ENC_KEY_SIZE) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001663 boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs->enckey[slot]);
Fabio Utzigba829042018-09-18 08:29:34 -03001664 }
1665 }
1666#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001667 }
1668
David Vincze2d736ad2019-02-18 11:50:22 +01001669 primary_slot_size = 0;
1670 secondary_slot_size = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001671 last_sector_idx = 0;
David Vincze2d736ad2019-02-18 11:50:22 +01001672 last_idx_secondary_slot = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001673
1674 /*
1675 * Knowing the size of the largest image between both slots, here we
David Vincze2d736ad2019-02-18 11:50:22 +01001676 * find what is the last sector in the primary slot that needs swapping.
1677 * Since we already know that both slots are compatible, the secondary
1678 * slot's last sector is not really required after this check is finished.
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001679 */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001680 while (1) {
David Vincze2d736ad2019-02-18 11:50:22 +01001681 if ((primary_slot_size < copy_size) ||
1682 (primary_slot_size < secondary_slot_size)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001683 primary_slot_size += boot_img_sector_size(state,
David Vincze2d736ad2019-02-18 11:50:22 +01001684 BOOT_PRIMARY_SLOT,
1685 last_sector_idx);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001686 }
David Vincze2d736ad2019-02-18 11:50:22 +01001687 if ((secondary_slot_size < copy_size) ||
1688 (secondary_slot_size < primary_slot_size)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001689 secondary_slot_size += boot_img_sector_size(state,
David Vincze2d736ad2019-02-18 11:50:22 +01001690 BOOT_SECONDARY_SLOT,
1691 last_idx_secondary_slot);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001692 }
David Vincze2d736ad2019-02-18 11:50:22 +01001693 if (primary_slot_size >= copy_size &&
1694 secondary_slot_size >= copy_size &&
1695 primary_slot_size == secondary_slot_size) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001696 break;
1697 }
1698 last_sector_idx++;
David Vincze2d736ad2019-02-18 11:50:22 +01001699 last_idx_secondary_slot++;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001700 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001701
1702 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001703 while (last_sector_idx >= 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001704 sz = boot_copy_sz(state, last_sector_idx, &first_sector_idx);
Fabio Utzig39000012018-07-30 12:40:20 -03001705 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001706 boot_swap_sectors(first_sector_idx, sz, state, bs);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001707 }
1708
1709 last_sector_idx = first_sector_idx - 1;
1710 swap_idx++;
1711 }
1712
David Vincze2d736ad2019-02-18 11:50:22 +01001713#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001714 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001715 BOOT_LOG_WRN("%d status write fails performing the swap",
1716 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001717 }
1718#endif
1719
Christopher Collins92ea77f2016-12-12 15:59:26 -08001720 return 0;
1721}
David Brown17609d82017-05-05 09:41:34 -06001722#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001723
1724/**
David Vincze2d736ad2019-02-18 11:50:22 +01001725 * Marks the image in the primary slot as fully copied.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001726 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001727#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001728static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001729boot_set_copy_done(uint8_t image_index)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001730{
1731 const struct flash_area *fap;
1732 int rc;
1733
Fabio Utzigb0f04732019-07-31 09:49:19 -03001734 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1735 &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001736 if (rc != 0) {
1737 return BOOT_EFLASH;
1738 }
1739
1740 rc = boot_write_copy_done(fap);
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001741 flash_area_close(fap);
1742 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001743}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001744#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001745
1746/**
David Vincze2d736ad2019-02-18 11:50:22 +01001747 * Marks a reverted image in the primary slot as confirmed. This is necessary to
1748 * ensure the status bytes from the image revert operation don't get processed
1749 * on a subsequent boot.
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001750 *
1751 * NOTE: image_ok is tested before writing because if there's a valid permanent
David Vincze2d736ad2019-02-18 11:50:22 +01001752 * image installed on the primary slot and the new image to be upgrade to has a
1753 * bad sig, image_ok would be overwritten.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001754 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001755#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001756static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001757boot_set_image_ok(uint8_t image_index)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001758{
1759 const struct flash_area *fap;
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001760 struct boot_swap_state state;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001761 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
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001769 rc = boot_read_swap_state(fap, &state);
1770 if (rc != 0) {
1771 rc = BOOT_EFLASH;
1772 goto out;
1773 }
1774
1775 if (state.image_ok == BOOT_FLAG_UNSET) {
1776 rc = boot_write_image_ok(fap);
1777 }
1778
1779out:
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001780 flash_area_close(fap);
1781 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001782}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001783#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001784
David Vinczee32483f2019-06-13 10:46:24 +02001785#if (BOOT_IMAGE_NUMBER > 1)
1786/**
Fabio Utzig019a81a2019-08-27 10:33:39 -03001787 * Check if the version of the image is not older than required.
1788 *
1789 * @param req Required minimal image version.
1790 * @param ver Version of the image to be checked.
1791 *
1792 * @return 0 if the version is sufficient, nonzero otherwise.
1793 */
1794static int
1795boot_is_version_sufficient(struct image_version *req,
1796 struct image_version *ver)
1797{
1798 if (ver->iv_major > req->iv_major) {
1799 return 0;
1800 }
1801 if (ver->iv_major < req->iv_major) {
1802 return BOOT_EBADVERSION;
1803 }
1804 /* The major version numbers are equal. */
1805 if (ver->iv_minor > req->iv_minor) {
1806 return 0;
1807 }
1808 if (ver->iv_minor < req->iv_minor) {
1809 return BOOT_EBADVERSION;
1810 }
1811 /* The minor version numbers are equal. */
1812 if (ver->iv_revision < req->iv_revision) {
1813 return BOOT_EBADVERSION;
1814 }
1815
1816 return 0;
1817}
1818
1819/**
David Vinczee32483f2019-06-13 10:46:24 +02001820 * Check the image dependency whether it is satisfied and modify
1821 * the swap type if necessary.
1822 *
1823 * @param dep Image dependency which has to be verified.
1824 *
1825 * @return 0 on success; nonzero on failure.
1826 */
1827static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001828boot_verify_slot_dependency(struct boot_loader_state *state,
1829 struct image_dependency *dep)
David Vinczee32483f2019-06-13 10:46:24 +02001830{
1831 struct image_version *dep_version;
1832 size_t dep_slot;
1833 int rc;
David Browne6ab34c2019-09-03 12:24:21 -06001834 uint8_t swap_type;
David Vinczee32483f2019-06-13 10:46:24 +02001835
1836 /* Determine the source of the image which is the subject of
1837 * the dependency and get it's version. */
David Browne6ab34c2019-09-03 12:24:21 -06001838 swap_type = state->swap_type[dep->image_id];
1839 dep_slot = (swap_type != BOOT_SWAP_TYPE_NONE && swap_type != BOOT_SWAP_TYPE_FAIL) ?
David Vinczee32483f2019-06-13 10:46:24 +02001840 BOOT_SECONDARY_SLOT : BOOT_PRIMARY_SLOT;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001841 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +02001842
1843 rc = boot_is_version_sufficient(&dep->image_min_version, dep_version);
1844 if (rc != 0) {
1845 /* Dependency not satisfied.
1846 * Modify the swap type to decrease the version number of the image
1847 * (which will be located in the primary slot after the boot process),
1848 * consequently the number of unsatisfied dependencies will be
1849 * decreased or remain the same.
1850 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001851 switch (BOOT_SWAP_TYPE(state)) {
David Vinczee32483f2019-06-13 10:46:24 +02001852 case BOOT_SWAP_TYPE_TEST:
1853 case BOOT_SWAP_TYPE_PERM:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001854 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczee32483f2019-06-13 10:46:24 +02001855 break;
1856 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001857 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczee32483f2019-06-13 10:46:24 +02001858 break;
1859 default:
1860 break;
1861 }
1862 }
1863
1864 return rc;
1865}
1866
1867/**
1868 * Read all dependency TLVs of an image from the flash and verify
1869 * one after another to see if they are all satisfied.
1870 *
1871 * @param slot Image slot number.
1872 *
1873 * @return 0 on success; nonzero on failure.
1874 */
1875static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001876boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
David Vinczee32483f2019-06-13 10:46:24 +02001877{
1878 const struct flash_area *fap;
David Vinczee32483f2019-06-13 10:46:24 +02001879 struct image_tlv tlv;
1880 struct image_dependency dep;
1881 uint32_t off;
1882 uint32_t end;
1883 bool dep_tlvs_found = false;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001884 int area_id;
David Vinczee32483f2019-06-13 10:46:24 +02001885 int rc;
1886
Fabio Utzig10ee6482019-08-01 12:04:52 -03001887 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001888 rc = flash_area_open(area_id, &fap);
David Vinczee32483f2019-06-13 10:46:24 +02001889 if (rc != 0) {
1890 rc = BOOT_EFLASH;
1891 goto done;
1892 }
1893
Fabio Utzig233af7d2019-08-26 12:06:16 -03001894 rc = boot_find_tlv_offs(boot_img_hdr(state, slot), fap, &off, &end);
David Vinczee32483f2019-06-13 10:46:24 +02001895 if (rc != 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001896 goto done;
1897 }
1898
David Vinczee32483f2019-06-13 10:46:24 +02001899 /* Traverse through all of the TLVs to find the dependency TLVs. */
1900 for (; off < end; off += sizeof(tlv) + tlv.it_len) {
1901 rc = flash_area_read(fap, off, &tlv, sizeof(tlv));
1902 if (rc != 0) {
1903 rc = BOOT_EFLASH;
1904 goto done;
1905 }
1906
1907 if (tlv.it_type == IMAGE_TLV_DEPENDENCY) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001908 dep_tlvs_found = true;
David Vinczee32483f2019-06-13 10:46:24 +02001909
1910 if (tlv.it_len != sizeof(dep)) {
1911 rc = BOOT_EBADIMAGE;
1912 goto done;
1913 }
1914
1915 rc = flash_area_read(fap, off + sizeof(tlv), &dep, tlv.it_len);
1916 if (rc != 0) {
1917 rc = BOOT_EFLASH;
1918 goto done;
1919 }
1920
Fabio Utzig298913b2019-08-28 11:22:45 -03001921 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
1922 rc = BOOT_EBADARGS;
1923 goto done;
1924 }
1925
David Vinczee32483f2019-06-13 10:46:24 +02001926 /* Verify dependency and modify the swap type if not satisfied. */
Fabio Utzig298913b2019-08-28 11:22:45 -03001927 rc = boot_verify_slot_dependency(state, &dep);
David Vinczee32483f2019-06-13 10:46:24 +02001928 if (rc != 0) {
1929 /* Dependency not satisfied. */
1930 goto done;
1931 }
1932
1933 /* Dependency satisfied, no action needed.
1934 * Continue with the next TLV entry.
1935 */
1936 } else if (dep_tlvs_found) {
1937 /* The dependency TLVs are contiguous in the TLV area. If a
1938 * dependency had already been found and the last read TLV
1939 * has a different type then there are no more dependency TLVs.
1940 * The search can be finished.
1941 */
1942 break;
1943 }
1944 }
1945
1946done:
1947 flash_area_close(fap);
1948 return rc;
1949}
1950
1951/**
David Vinczee32483f2019-06-13 10:46:24 +02001952 * Iterate over all the images and verify whether the image dependencies in the
1953 * TLV area are all satisfied and update the related swap type if necessary.
1954 */
Fabio Utzig298913b2019-08-28 11:22:45 -03001955static int
1956boot_verify_dependencies(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001957{
David Vinczee32483f2019-06-13 10:46:24 +02001958 int rc;
Fabio Utzig298913b2019-08-28 11:22:45 -03001959 uint8_t slot;
David Vinczee32483f2019-06-13 10:46:24 +02001960
Fabio Utzig10ee6482019-08-01 12:04:52 -03001961 BOOT_CURR_IMG(state) = 0;
1962 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001963 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1964 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
1965 slot = BOOT_SECONDARY_SLOT;
1966 } else {
1967 slot = BOOT_PRIMARY_SLOT;
1968 }
1969
1970 rc = boot_verify_slot_dependencies(state, slot);
Fabio Utzigabec0732019-07-31 08:40:22 -03001971 if (rc == 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001972 /* All dependencies've been satisfied, continue with next image. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001973 BOOT_CURR_IMG(state)++;
David Vinczee32483f2019-06-13 10:46:24 +02001974 } else if (rc == BOOT_EBADVERSION) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001975 /* Cannot upgrade due to non-met dependencies, so disable all
1976 * image upgrades.
1977 */
1978 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
1979 BOOT_CURR_IMG(state) = idx;
1980 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1981 }
1982 break;
David Vinczee32483f2019-06-13 10:46:24 +02001983 } else {
1984 /* Other error happened, images are inconsistent */
Fabio Utzig298913b2019-08-28 11:22:45 -03001985 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02001986 }
1987 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001988 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02001989}
1990#endif /* (BOOT_IMAGE_NUMBER > 1) */
1991
Christopher Collins92ea77f2016-12-12 15:59:26 -08001992/**
David Vinczeba3bd602019-06-17 16:01:43 +02001993 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001994 *
David Vinczeba3bd602019-06-17 16:01:43 +02001995 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001996 *
1997 * @return 0 on success; nonzero on failure.
1998 */
1999static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03002000boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002001{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002002 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03002003#ifndef MCUBOOT_OVERWRITE_ONLY
2004 uint8_t swap_type;
2005#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002006
David Vinczeba3bd602019-06-17 16:01:43 +02002007 /* At this point there are no aborted swaps. */
2008#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03002009 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002010#elif defined(MCUBOOT_BOOTSTRAP)
2011 /* Check if the image update was triggered by a bad image in the
2012 * primary slot (the validity of the image in the secondary slot had
2013 * already been checked).
2014 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002015 if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 ||
2016 boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) {
2017 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002018 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002019 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002020 }
2021#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03002022 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002023#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03002024 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02002025
2026#ifndef MCUBOOT_OVERWRITE_ONLY
2027 /* The following state needs image_ok be explicitly set after the
2028 * swap was finished to avoid a new revert.
2029 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002030 swap_type = BOOT_SWAP_TYPE(state);
2031 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
2032 swap_type == BOOT_SWAP_TYPE_PERM) {
2033 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002034 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002035 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002036 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002037 }
2038
Fabio Utzig10ee6482019-08-01 12:04:52 -03002039 if (swap_type == BOOT_SWAP_TYPE_TEST ||
2040 swap_type == BOOT_SWAP_TYPE_PERM ||
2041 swap_type == BOOT_SWAP_TYPE_REVERT) {
2042 rc = boot_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002043 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002044 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07002045 }
David Vinczeba3bd602019-06-17 16:01:43 +02002046 }
2047#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02002048
David Vinczeba3bd602019-06-17 16:01:43 +02002049 return rc;
2050}
2051
2052/**
2053 * Completes a previously aborted image swap.
2054 *
2055 * @param bs The current boot status.
2056 *
2057 * @return 0 on success; nonzero on failure.
2058 */
2059#if !defined(MCUBOOT_OVERWRITE_ONLY)
2060static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03002061boot_complete_partial_swap(struct boot_loader_state *state,
2062 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02002063{
2064 int rc;
2065
2066 /* Determine the type of swap operation being resumed from the
2067 * `swap-type` trailer field.
2068 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002069 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002070 assert(rc == 0);
2071
Fabio Utzig10ee6482019-08-01 12:04:52 -03002072 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02002073
2074 /* The following states need image_ok be explicitly set after the
2075 * swap was finished to avoid a new revert.
2076 */
2077 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
2078 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002079 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002080 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002081 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002082 }
2083 }
2084
2085 if (bs->swap_type == BOOT_SWAP_TYPE_TEST ||
2086 bs->swap_type == BOOT_SWAP_TYPE_PERM ||
2087 bs->swap_type == BOOT_SWAP_TYPE_REVERT) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002088 rc = boot_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002089 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002090 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002091 }
2092 }
2093
Fabio Utzig10ee6482019-08-01 12:04:52 -03002094 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002095 BOOT_LOG_ERR("panic!");
2096 assert(0);
2097
2098 /* Loop forever... */
2099 while (1) {}
2100 }
2101
2102 return rc;
2103}
2104#endif /* !MCUBOOT_OVERWRITE_ONLY */
2105
2106#if (BOOT_IMAGE_NUMBER > 1)
2107/**
2108 * Review the validity of previously determined swap types of other images.
2109 *
2110 * @param aborted_swap The current image upgrade is a
2111 * partial/aborted swap.
2112 */
2113static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03002114boot_review_image_swap_types(struct boot_loader_state *state,
2115 bool aborted_swap)
David Vinczeba3bd602019-06-17 16:01:43 +02002116{
2117 /* In that case if we rebooted in the middle of an image upgrade process, we
2118 * must review the validity of swap types, that were previously determined
2119 * for other images. The image_ok flag had not been set before the reboot
2120 * for any of the updated images (only the copy_done flag) and thus falsely
2121 * the REVERT swap type has been determined for the previous images that had
2122 * been updated before the reboot.
2123 *
2124 * There are two separate scenarios that we have to deal with:
2125 *
2126 * 1. The reboot has happened during swapping an image:
2127 * The current image upgrade has been determined as a
2128 * partial/aborted swap.
2129 * 2. The reboot has happened between two separate image upgrades:
2130 * In this scenario we must check the swap type of the current image.
2131 * In those cases if it is NONE or REVERT we cannot certainly determine
2132 * the fact of a reboot. In a consistent state images must move in the
2133 * same direction or stay in place, e.g. in practice REVERT and TEST
2134 * swap types cannot be present at the same time. If the swap type of
2135 * the current image is either TEST, PERM or FAIL we must review the
2136 * already determined swap types of other images and set each false
2137 * REVERT swap types to NONE (these images had been successfully
2138 * updated before the system rebooted between two separate image
2139 * upgrades).
2140 */
2141
Fabio Utzig10ee6482019-08-01 12:04:52 -03002142 if (BOOT_CURR_IMG(state) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02002143 /* Nothing to do */
2144 return;
2145 }
2146
2147 if (!aborted_swap) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002148 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
2149 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002150 /* Nothing to do */
2151 return;
2152 }
2153 }
2154
Fabio Utzig10ee6482019-08-01 12:04:52 -03002155 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
2156 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
2157 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002158 }
2159 }
2160}
2161#endif
2162
2163/**
2164 * Prepare image to be updated if required.
2165 *
2166 * Prepare image to be updated if required with completing an image swap
2167 * operation if one was aborted and/or determining the type of the
2168 * swap operation. In case of any error set the swap type to NONE.
2169 *
Fabio Utzig10ee6482019-08-01 12:04:52 -03002170 * @param state TODO
David Vinczeba3bd602019-06-17 16:01:43 +02002171 * @param bs Pointer where the read and possibly updated
2172 * boot status can be written to.
2173 */
2174static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03002175boot_prepare_image_for_update(struct boot_loader_state *state,
2176 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02002177{
2178 int rc;
2179
2180 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002181 rc = boot_read_sectors(state);
David Vinczeba3bd602019-06-17 16:01:43 +02002182 if (rc != 0) {
2183 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
2184 " - too small?", BOOT_MAX_IMG_SECTORS);
2185 /* Unable to determine sector layout, continue with next image
2186 * if there is one.
2187 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002188 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002189 return;
2190 }
2191
2192 /* Attempt to read an image header from each slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002193 rc = boot_read_image_headers(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02002194 if (rc != 0) {
2195 /* Continue with next image if there is one. */
Fabio Utzigb0f04732019-07-31 09:49:19 -03002196 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03002197 BOOT_CURR_IMG(state));
2198 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002199 return;
2200 }
2201
2202 /* If the current image's slots aren't compatible, no swap is possible.
2203 * Just boot into primary slot.
2204 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002205 if (boot_slots_compatible(state)) {
2206 rc = boot_read_status(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002207 if (rc != 0) {
2208 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03002209 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002210 /* Continue with next image if there is one. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002211 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002212 return;
2213 }
2214
2215 /* Determine if we rebooted in the middle of an image swap
2216 * operation. If a partial swap was detected, complete it.
2217 */
2218 if (bs->idx != BOOT_STATUS_IDX_0 || bs->state != BOOT_STATUS_STATE_0) {
2219
2220#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03002221 boot_review_image_swap_types(state, true);
David Vinczeba3bd602019-06-17 16:01:43 +02002222#endif
2223
2224#ifdef MCUBOOT_OVERWRITE_ONLY
2225 /* Should never arrive here, overwrite-only mode has
2226 * no swap state.
2227 */
2228 assert(0);
2229#else
2230 /* Determine the type of swap operation being resumed from the
2231 * `swap-type` trailer field.
2232 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002233 rc = boot_complete_partial_swap(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002234 assert(rc == 0);
2235#endif
2236 /* Attempt to read an image header from each slot. Ensure that
2237 * image headers in slots are aligned with headers in boot_data.
2238 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002239 rc = boot_read_image_headers(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02002240 assert(rc == 0);
2241
2242 /* Swap has finished set to NONE */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002243 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002244 } else {
2245 /* There was no partial swap, determine swap type. */
2246 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002247 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
2248 } else if (boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) != 0) {
2249 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
David Vinczeba3bd602019-06-17 16:01:43 +02002250 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002251 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02002252 }
2253
2254#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03002255 boot_review_image_swap_types(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02002256#endif
2257
2258#ifdef MCUBOOT_BOOTSTRAP
Fabio Utzig10ee6482019-08-01 12:04:52 -03002259 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02002260 /* Header checks are done first because they are
2261 * inexpensive. Since overwrite-only copies starting from
2262 * offset 0, if interrupted, it might leave a valid header
2263 * magic, so also run validation on the primary slot to be
2264 * sure it's not OK.
2265 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002266 if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 ||
2267 boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) {
2268 if (boot_img_hdr(state,
David Vinczeba3bd602019-06-17 16:01:43 +02002269 BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC &&
Fabio Utzig10ee6482019-08-01 12:04:52 -03002270 boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) == 0)
David Vinczeba3bd602019-06-17 16:01:43 +02002271 {
2272 /* Set swap type to REVERT to overwrite the primary
2273 * slot with the image contained in secondary slot
2274 * and to trigger the explicit setting of the
2275 * image_ok flag.
2276 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002277 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02002278 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02002279 }
2280 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02002281#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002282 }
David Vinczeba3bd602019-06-17 16:01:43 +02002283 } else {
2284 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002285 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002286 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002287}
2288
Christopher Collins92ea77f2016-12-12 15:59:26 -08002289int
Fabio Utzig10ee6482019-08-01 12:04:52 -03002290context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08002291{
Marti Bolivar84898652017-06-13 17:20:22 -04002292 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02002293 struct boot_status bs;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002294 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002295 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03002296 int image_index;
Fabio Utzig298913b2019-08-28 11:22:45 -03002297 bool has_upgrade;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002298
2299 /* The array of slot sectors are defined here (as opposed to file scope) so
2300 * that they don't get allocated for non-boot-loader apps. This is
2301 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002302 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08002303 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002304 TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
2305 TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
2306 TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Christopher Collins92ea77f2016-12-12 15:59:26 -08002307
Fabio Utzig10ee6482019-08-01 12:04:52 -03002308 memset(state, 0, sizeof(struct boot_loader_state));
Fabio Utzig298913b2019-08-28 11:22:45 -03002309 has_upgrade = false;
2310
2311#if (BOOT_IMAGE_NUMBER == 1)
2312 (void)has_upgrade;
2313#endif
Fabio Utzigba829042018-09-18 08:29:34 -03002314
David Vinczeba3bd602019-06-17 16:01:43 +02002315 /* Iterate over all the images. By the end of the loop the swap type has
2316 * to be determined for each image and all aborted swaps have to be
2317 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002318 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002319 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03002320
David Vinczeba3bd602019-06-17 16:01:43 +02002321#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
2322 /* The keys used for encryption may no longer be valid (could belong to
2323 * another images). Therefore, mark them as invalid to force their reload
2324 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03002325 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03002326 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Brown554c52e2017-06-30 16:01:07 -06002327#endif
2328
Fabio Utzig10ee6482019-08-01 12:04:52 -03002329 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03002330
Fabio Utzig10ee6482019-08-01 12:04:52 -03002331 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002332 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03002333 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002334 secondary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03002335 state->scratch.sectors = scratch_sectors;
David Vinczeba3bd602019-06-17 16:01:43 +02002336
2337 /* Open primary and secondary image areas for the duration
2338 * of this call.
2339 */
2340 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03002341 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03002342 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02002343 assert(rc == 0);
2344 }
2345 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03002346 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002347 assert(rc == 0);
2348
2349 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002350 boot_prepare_image_for_update(state, &bs);
Fabio Utzig298913b2019-08-28 11:22:45 -03002351
2352 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
2353 has_upgrade = true;
2354 }
David Vinczeba3bd602019-06-17 16:01:43 +02002355 }
2356
David Vinczee32483f2019-06-13 10:46:24 +02002357#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig298913b2019-08-28 11:22:45 -03002358 if (has_upgrade) {
2359 /* Iterate over all the images and verify whether the image dependencies
2360 * are all satisfied and update swap type if necessary.
2361 */
2362 rc = boot_verify_dependencies(state);
2363 if (rc == BOOT_EBADVERSION) {
2364 /*
2365 * It was impossible to upgrade because the expected dependency version
2366 * was not available. Here we already changed the swap_type so that
2367 * instead of asserting the bootloader, we continue and no upgrade is
2368 * performed.
2369 */
2370 rc = 0;
2371 }
2372 }
David Vinczee32483f2019-06-13 10:46:24 +02002373#endif
2374
David Vinczeba3bd602019-06-17 16:01:43 +02002375 /* Iterate over all the images. At this point there are no aborted swaps
2376 * and the swap types are determined for each image. By the end of the loop
2377 * all required update operations will have been finished.
2378 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002379 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002380
2381#if (BOOT_IMAGE_NUMBER > 1)
2382#ifdef MCUBOOT_ENC_IMAGES
2383 /* The keys used for encryption may no longer be valid (could belong to
2384 * another images). Therefore, mark them as invalid to force their reload
2385 * by boot_enc_load().
2386 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03002387 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002388#endif /* MCUBOOT_ENC_IMAGES */
2389
2390 /* Indicate that swap is not aborted */
2391 memset(&bs, 0, sizeof bs);
2392 bs.idx = BOOT_STATUS_IDX_0;
2393 bs.state = BOOT_STATUS_STATE_0;
2394#endif /* (BOOT_IMAGE_NUMBER > 1) */
2395
2396 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002397 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02002398
Fabio Utzig10ee6482019-08-01 12:04:52 -03002399 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002400 case BOOT_SWAP_TYPE_NONE:
2401 break;
2402
2403 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
2404 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
2405 case BOOT_SWAP_TYPE_REVERT:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002406 rc = boot_perform_update(state, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002407 assert(rc == 0);
2408 break;
2409
2410 case BOOT_SWAP_TYPE_FAIL:
2411 /* The image in secondary slot was invalid and is now erased. Ensure
2412 * we don't try to boot into it again on the next reboot. Do this by
2413 * pretending we just reverted back to primary slot.
2414 */
2415#ifndef MCUBOOT_OVERWRITE_ONLY
2416 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002417 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002418 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002419 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002420 }
2421#endif /* !MCUBOOT_OVERWRITE_ONLY */
2422 break;
2423
2424 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002425 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002426 }
2427
Fabio Utzig10ee6482019-08-01 12:04:52 -03002428 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002429 BOOT_LOG_ERR("panic!");
2430 assert(0);
2431
2432 /* Loop forever... */
2433 while (1) {}
2434 }
2435 }
2436
2437 /* Iterate over all the images. At this point all required update operations
2438 * have finished. By the end of the loop each image in the primary slot will
2439 * have been re-validated.
2440 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002441 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2442 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02002443 /* Attempt to read an image header from each slot. Ensure that image
2444 * headers in slots are aligned with headers in boot_data.
2445 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002446 rc = boot_read_image_headers(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02002447 if (rc != 0) {
2448 goto out;
2449 }
2450 /* Since headers were reloaded, it can be assumed we just performed
2451 * a swap or overwrite. Now the header info that should be used to
2452 * provide the data for the bootstrap, which previously was at
2453 * secondary slot, was updated to primary slot.
2454 */
2455 }
2456
2457#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig10ee6482019-08-01 12:04:52 -03002458 rc = boot_validate_slot(state, BOOT_PRIMARY_SLOT, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02002459 if (rc != 0) {
2460 rc = BOOT_EBADIMAGE;
2461 goto out;
2462 }
2463#else
2464 /* Even if we're not re-validating the primary slot, we could be booting
2465 * onto an empty flash chip. At least do a basic sanity check that
2466 * the magic number on the image is OK.
2467 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002468 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002469 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
Fabio Utzig10ee6482019-08-01 12:04:52 -03002470 &boot_img_hdr(state,BOOT_PRIMARY_SLOT)->ih_magic,
2471 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002472 rc = BOOT_EBADIMAGE;
2473 goto out;
2474 }
2475#endif
2476 }
2477
Fabio Utzigb0f04732019-07-31 09:49:19 -03002478#if (BOOT_IMAGE_NUMBER > 1)
David Vinczeba3bd602019-06-17 16:01:43 +02002479 /* Always boot from the primary slot of Image 0. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002480 BOOT_CURR_IMG(state) = 0;
Fabio Utzigb0f04732019-07-31 09:49:19 -03002481#endif
Fabio Utzig298913b2019-08-28 11:22:45 -03002482
Fabio Utzig10ee6482019-08-01 12:04:52 -03002483 rsp->br_flash_dev_id = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)->fa_device_id;
2484 rsp->br_image_off = boot_img_slot_off(state, BOOT_PRIMARY_SLOT);
2485 rsp->br_hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002486
Fabio Utzig298913b2019-08-28 11:22:45 -03002487out:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002488 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2489 flash_area_close(BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002490 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002491 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
David Vinczeba3bd602019-06-17 16:01:43 +02002492 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04002493 }
2494 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002495}
2496
Fabio Utzig10ee6482019-08-01 12:04:52 -03002497/**
2498 * Prepares the booting process. This function moves images around in flash as
2499 * appropriate, and tells you what address to boot from.
2500 *
2501 * @param rsp On success, indicates how booting should occur.
2502 *
2503 * @return 0 on success; nonzero on failure.
2504 */
2505int
2506boot_go(struct boot_rsp *rsp)
2507{
2508 return context_boot_go(&boot_data, rsp);
2509}
2510
Christopher Collins92ea77f2016-12-12 15:59:26 -08002511int
2512split_go(int loader_slot, int split_slot, void **entry)
2513{
Marti Bolivarc50926f2017-06-14 09:35:40 -04002514 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08002515 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002516 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002517 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002518 int rc;
2519
Christopher Collins92ea77f2016-12-12 15:59:26 -08002520 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
2521 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04002522 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002523 }
David Vinczeba3bd602019-06-17 16:01:43 +02002524 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
2525 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002526
2527 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
2528 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07002529 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04002530 assert(rc == 0);
2531 split_flash_id = flash_area_id_from_image_slot(split_slot);
2532 rc = flash_area_open(split_flash_id,
2533 &BOOT_IMG_AREA(&boot_data, split_slot));
2534 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002535
2536 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002537 rc = boot_read_sectors(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002538 if (rc != 0) {
2539 rc = SPLIT_GO_ERR;
2540 goto done;
2541 }
2542
Fabio Utzig10ee6482019-08-01 12:04:52 -03002543 rc = boot_read_image_headers(&boot_data, true);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002544 if (rc != 0) {
2545 goto done;
2546 }
2547
Christopher Collins92ea77f2016-12-12 15:59:26 -08002548 /* Don't check the bootable image flag because we could really call a
2549 * bootable or non-bootable image. Just validate that the image check
2550 * passes which is distinct from the normal check.
2551 */
Marti Bolivarf804f622017-06-12 15:41:48 -04002552 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04002553 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04002554 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04002555 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002556 if (rc != 0) {
2557 rc = SPLIT_GO_NON_MATCHING;
2558 goto done;
2559 }
2560
Marti Bolivarea088872017-06-12 17:10:49 -04002561 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04002562 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08002563 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002564 rc = SPLIT_GO_OK;
2565
2566done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04002567 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
2568 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002569 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002570 return rc;
2571}