blob: 045be527cd48939bf80e7faf0c3f0c7fb8b82408 [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
David Vinczee2453472019-06-17 12:31:59 +020020/*
21 * Modifications are Copyright (c) 2019 Arm Limited.
22 */
23
Christopher Collins92ea77f2016-12-12 15:59:26 -080024/**
25 * This file provides an interface to the boot loader. Functions defined in
26 * this file should only be called while the boot loader is running.
27 */
28
29#include <assert.h>
30#include <stddef.h>
David Brown52eee562017-07-05 11:25:09 -060031#include <stdbool.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080032#include <inttypes.h>
33#include <stdlib.h>
34#include <string.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080035#include <os/os_malloc.h>
36#include "bootutil/bootutil.h"
37#include "bootutil/image.h"
38#include "bootutil_priv.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050039#include "bootutil/bootutil_log.h"
40
Fabio Utzigba829042018-09-18 08:29:34 -030041#ifdef MCUBOOT_ENC_IMAGES
42#include "bootutil/enc_key.h"
43#endif
44
Fabio Utzigba1fbe62017-07-21 14:01:20 -030045#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030046
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010047MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
48
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040049static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080050
Fabio Utzigabec0732019-07-31 08:40:22 -030051#if (BOOT_IMAGE_NUMBER > 1)
52#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
53#else
54#define IMAGES_ITER(x)
55#endif
56
Fabio Utzig10ee6482019-08-01 12:04:52 -030057/*
58 * This macro allows some control on the allocation of local variables.
59 * When running natively on a target, we don't want to allocated huge
60 * variables on the stack, so make them global instead. For the simulator
61 * we want to run as many threads as there are tests, and it's safer
62 * to just make those variables stack allocated.
63 */
64#if !defined(__BOOTSIM__)
65#define TARGET_STATIC static
66#else
67#define TARGET_STATIC
68#endif
69
David Vincze2d736ad2019-02-18 11:50:22 +010070#if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT) && !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -030071/*
72 * FIXME: this might have to be updated for threaded sim
73 */
Fabio Utziga0e1cce2017-11-23 20:04:01 -020074static int boot_status_fails = 0;
75#define BOOT_STATUS_ASSERT(x) \
76 do { \
Johann Fischered8461b2018-02-15 16:50:31 +010077 if (!(x)) { \
Fabio Utziga0e1cce2017-11-23 20:04:01 -020078 boot_status_fails++; \
79 } \
80 } while (0)
81#else
Fabio Utzig57c40f72017-12-12 21:48:30 -020082#define BOOT_STATUS_ASSERT(x) ASSERT(x)
Fabio Utziga0e1cce2017-11-23 20:04:01 -020083#endif
84
Christopher Collins92ea77f2016-12-12 15:59:26 -080085struct boot_status_table {
David Vincze2d736ad2019-02-18 11:50:22 +010086 uint8_t bst_magic_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -080087 uint8_t bst_magic_scratch;
David Vincze2d736ad2019-02-18 11:50:22 +010088 uint8_t bst_copy_done_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -080089 uint8_t bst_status_source;
90};
91
92/**
93 * This set of tables maps swap state contents to boot status location.
94 * When searching for a match, these tables must be iterated in order.
95 */
96static const struct boot_status_table boot_status_tables[] = {
97 {
David Vincze2d736ad2019-02-18 11:50:22 +010098 /* | primary slot | scratch |
99 * ----------+--------------+--------------|
100 * magic | Good | Any |
101 * copy-done | Set | N/A |
102 * ----------+--------------+--------------'
103 * source: none |
104 * ----------------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -0800105 */
David Vincze2d736ad2019-02-18 11:50:22 +0100106 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
Christopher Collinsa1c12042019-05-23 14:00:28 -0700107 .bst_magic_scratch = BOOT_MAGIC_NOTGOOD,
David Vincze2d736ad2019-02-18 11:50:22 +0100108 .bst_copy_done_primary_slot = BOOT_FLAG_SET,
109 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800110 },
111
112 {
David Vincze2d736ad2019-02-18 11:50:22 +0100113 /* | primary slot | scratch |
114 * ----------+--------------+--------------|
115 * magic | Good | Any |
116 * copy-done | Unset | N/A |
117 * ----------+--------------+--------------'
118 * source: primary slot |
119 * ----------------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -0800120 */
David Vincze2d736ad2019-02-18 11:50:22 +0100121 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
Christopher Collinsa1c12042019-05-23 14:00:28 -0700122 .bst_magic_scratch = BOOT_MAGIC_NOTGOOD,
David Vincze2d736ad2019-02-18 11:50:22 +0100123 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
124 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800125 },
126
127 {
David Vincze2d736ad2019-02-18 11:50:22 +0100128 /* | primary slot | scratch |
129 * ----------+--------------+--------------|
130 * magic | Any | Good |
131 * copy-done | Any | N/A |
132 * ----------+--------------+--------------'
133 * source: scratch |
134 * ----------------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -0800135 */
David Vincze2d736ad2019-02-18 11:50:22 +0100136 .bst_magic_primary_slot = BOOT_MAGIC_ANY,
137 .bst_magic_scratch = BOOT_MAGIC_GOOD,
138 .bst_copy_done_primary_slot = BOOT_FLAG_ANY,
139 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800140 },
Christopher Collins92ea77f2016-12-12 15:59:26 -0800141 {
David Vincze2d736ad2019-02-18 11:50:22 +0100142 /* | primary slot | scratch |
143 * ----------+--------------+--------------|
144 * magic | Unset | Any |
145 * copy-done | Unset | N/A |
146 * ----------+--------------+--------------|
147 * source: varies |
148 * ----------------------------------------+--------------------------+
Christopher Collins92ea77f2016-12-12 15:59:26 -0800149 * This represents one of two cases: |
150 * o No swaps ever (no status to read, so no harm in checking). |
David Vincze2d736ad2019-02-18 11:50:22 +0100151 * o Mid-revert; status in primary slot. |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800152 * -------------------------------------------------------------------'
153 */
David Vincze2d736ad2019-02-18 11:50:22 +0100154 .bst_magic_primary_slot = BOOT_MAGIC_UNSET,
155 .bst_magic_scratch = BOOT_MAGIC_ANY,
156 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
157 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800158 },
159};
160
161#define BOOT_STATUS_TABLES_COUNT \
162 (sizeof boot_status_tables / sizeof boot_status_tables[0])
163
Marti Bolivarfd20c762017-02-07 16:52:50 -0500164#define BOOT_LOG_SWAP_STATE(area, state) \
Christopher Collinsa1c12042019-05-23 14:00:28 -0700165 BOOT_LOG_INF("%s: magic=%s, swap_type=0x%x, copy_done=0x%x, " \
166 "image_ok=0x%x", \
Marti Bolivarfd20c762017-02-07 16:52:50 -0500167 (area), \
168 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
169 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
170 "bad"), \
Christopher Collinsa1c12042019-05-23 14:00:28 -0700171 (state)->swap_type, \
Marti Bolivarfd20c762017-02-07 16:52:50 -0500172 (state)->copy_done, \
173 (state)->image_ok)
174
Christopher Collins92ea77f2016-12-12 15:59:26 -0800175/**
David Vincze2d736ad2019-02-18 11:50:22 +0100176 * Determines where in flash the most recent boot status is stored. The boot
Christopher Collins92ea77f2016-12-12 15:59:26 -0800177 * status is necessary for completing a swap that was interrupted by a boot
178 * loader reset.
179 *
David Vincze2d736ad2019-02-18 11:50:22 +0100180 * @return A BOOT_STATUS_SOURCE_[...] code indicating where status should
181 * be read from.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800182 */
183static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300184boot_status_source(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800185{
186 const struct boot_status_table *table;
187 struct boot_swap_state state_scratch;
David Vincze2d736ad2019-02-18 11:50:22 +0100188 struct boot_swap_state state_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800189 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200190 size_t i;
Marti Bolivarfd20c762017-02-07 16:52:50 -0500191 uint8_t source;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300192 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800193
Fabio Utzigb0f04732019-07-31 09:49:19 -0300194#if (BOOT_IMAGE_NUMBER == 1)
195 (void)state;
196#endif
197
Fabio Utzig10ee6482019-08-01 12:04:52 -0300198 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300199 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
200 &state_primary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800201 assert(rc == 0);
202
Fabio Utzig2473ac02017-05-02 12:45:02 -0300203 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800204 assert(rc == 0);
205
David Vincze2d736ad2019-02-18 11:50:22 +0100206 BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot);
Marti Bolivarfd20c762017-02-07 16:52:50 -0500207 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
208
Christopher Collins92ea77f2016-12-12 15:59:26 -0800209 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300210 table = &boot_status_tables[i];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800211
Christopher Collinsa1c12042019-05-23 14:00:28 -0700212 if (boot_magic_compatible_check(table->bst_magic_primary_slot,
213 state_primary_slot.magic) &&
214 boot_magic_compatible_check(table->bst_magic_scratch,
215 state_scratch.magic) &&
David Vincze2d736ad2019-02-18 11:50:22 +0100216 (table->bst_copy_done_primary_slot == BOOT_FLAG_ANY ||
217 table->bst_copy_done_primary_slot == state_primary_slot.copy_done))
218 {
Marti Bolivarfd20c762017-02-07 16:52:50 -0500219 source = table->bst_status_source;
David Vinczeba3bd602019-06-17 16:01:43 +0200220
221#if (BOOT_IMAGE_NUMBER > 1)
222 /* In case of multi-image boot it can happen that if boot status
223 * info is found on scratch area then it does not belong to the
224 * currently examined image.
225 */
226 if (source == BOOT_STATUS_SOURCE_SCRATCH &&
Fabio Utzig10ee6482019-08-01 12:04:52 -0300227 state_scratch.image_num != BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +0200228 source = BOOT_STATUS_SOURCE_NONE;
229 }
230#endif
231
Marti Bolivarfd20c762017-02-07 16:52:50 -0500232 BOOT_LOG_INF("Boot source: %s",
233 source == BOOT_STATUS_SOURCE_NONE ? "none" :
234 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
David Vincze2d736ad2019-02-18 11:50:22 +0100235 source == BOOT_STATUS_SOURCE_PRIMARY_SLOT ?
236 "primary slot" : "BUG; can't happen");
Marti Bolivarfd20c762017-02-07 16:52:50 -0500237 return source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800238 }
239 }
240
Marti Bolivarfd20c762017-02-07 16:52:50 -0500241 BOOT_LOG_INF("Boot source: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800242 return BOOT_STATUS_SOURCE_NONE;
243}
244
David Brownf5b33d82017-09-01 10:58:27 -0600245/*
246 * Compute the total size of the given image. Includes the size of
247 * the TLVs.
248 */
Fabio Utzig13d9e352017-10-05 20:32:31 -0300249#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600250static int
Fabio Utzigd638b172019-08-09 10:38:05 -0300251boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
David Brownf5b33d82017-09-01 10:58:27 -0600252{
253 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300254 struct image_tlv_info info;
Fabio Utzig233af7d2019-08-26 12:06:16 -0300255 uint32_t off;
David Brownf5b33d82017-09-01 10:58:27 -0600256 int area_id;
257 int rc;
258
Fabio Utzig10ee6482019-08-01 12:04:52 -0300259#if (BOOT_IMAGE_NUMBER == 1)
260 (void)state;
261#endif
262
263 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
David Brownf5b33d82017-09-01 10:58:27 -0600264 rc = flash_area_open(area_id, &fap);
265 if (rc != 0) {
266 rc = BOOT_EFLASH;
267 goto done;
268 }
269
Fabio Utzig61fd8882019-09-14 20:00:20 -0300270 off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
271
272 if (flash_area_read(fap, off, &info, sizeof(info))) {
273 rc = BOOT_EFLASH;
David Brownf5b33d82017-09-01 10:58:27 -0600274 goto done;
275 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300276
277 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
278 rc = BOOT_EBADIMAGE;
279 goto done;
280 }
281
282 *size = off + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600283 rc = 0;
284
285done:
286 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300287 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600288}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300289#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600290
Fabio Utzigc08ed212017-06-20 19:28:36 -0300291static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300292boot_read_image_header(struct boot_loader_state *state, int slot,
293 struct image_header *out_hdr)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800294{
295 const struct flash_area *fap;
296 int area_id;
297 int rc;
298
Fabio Utzig10ee6482019-08-01 12:04:52 -0300299#if (BOOT_IMAGE_NUMBER == 1)
300 (void)state;
301#endif
302
303 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800304 rc = flash_area_open(area_id, &fap);
305 if (rc != 0) {
306 rc = BOOT_EFLASH;
307 goto done;
308 }
309
310 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
311 if (rc != 0) {
312 rc = BOOT_EFLASH;
313 goto done;
314 }
315
316 rc = 0;
317
318done:
319 flash_area_close(fap);
320 return rc;
321}
322
323static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300324boot_read_image_headers(struct boot_loader_state *state, bool require_all)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800325{
326 int rc;
327 int i;
328
329 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300330 rc = boot_read_image_header(state, i, boot_img_hdr(state, i));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800331 if (rc != 0) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200332 /* If `require_all` is set, fail on any single fail, otherwise
333 * if at least the first slot's header was read successfully,
334 * then the boot loader can attempt a boot.
335 *
336 * Failure to read any headers is a fatal error.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800337 */
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200338 if (i > 0 && !require_all) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800339 return 0;
340 } else {
341 return rc;
342 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800343 }
344 }
345
346 return 0;
347}
348
349static uint8_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300350boot_write_sz(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800351{
352 uint8_t elem_sz;
353 uint8_t align;
354
355 /* Figure out what size to write update status update as. The size depends
356 * on what the minimum write size is for scratch area, active image slot.
357 * We need to use the bigger of those 2 values.
358 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300359 elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
360 align = flash_area_align(BOOT_SCRATCH_AREA(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800361 if (align > elem_sz) {
362 elem_sz = align;
363 }
364
365 return elem_sz;
366}
367
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200368/*
369 * Slots are compatible when all sectors that store upto to size of the image
370 * round up to sector size, in both slot's are able to fit in the scratch
371 * area, and have sizes that are a multiple of each other (powers of two
372 * presumably!).
373 */
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800374static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300375boot_slots_compatible(struct boot_loader_state *state)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800376{
David Vincze2d736ad2019-02-18 11:50:22 +0100377 size_t num_sectors_primary;
378 size_t num_sectors_secondary;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200379 size_t sz0, sz1;
David Vincze2d736ad2019-02-18 11:50:22 +0100380 size_t primary_slot_sz, secondary_slot_sz;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200381 size_t scratch_sz;
382 size_t i, j;
383 int8_t smaller;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800384
Fabio Utzig10ee6482019-08-01 12:04:52 -0300385 num_sectors_primary = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
386 num_sectors_secondary = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT);
David Vincze2d736ad2019-02-18 11:50:22 +0100387 if ((num_sectors_primary > BOOT_MAX_IMG_SECTORS) ||
388 (num_sectors_secondary > BOOT_MAX_IMG_SECTORS)) {
Fabio Utziga1fae672018-03-30 10:52:38 -0300389 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800390 return 0;
391 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300392
Fabio Utzig10ee6482019-08-01 12:04:52 -0300393 scratch_sz = boot_scratch_area_size(state);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200394
395 /*
396 * The following loop scans all sectors in a linear fashion, assuring that
397 * for each possible sector in each slot, it is able to fit in the other
398 * slot's sector or sectors. Slot's should be compatible as long as any
399 * number of a slot's sectors are able to fit into another, which only
400 * excludes cases where sector sizes are not a multiple of each other.
401 */
David Vincze2d736ad2019-02-18 11:50:22 +0100402 i = sz0 = primary_slot_sz = 0;
403 j = sz1 = secondary_slot_sz = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200404 smaller = 0;
David Vincze2d736ad2019-02-18 11:50:22 +0100405 while (i < num_sectors_primary || j < num_sectors_secondary) {
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200406 if (sz0 == sz1) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300407 sz0 += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
408 sz1 += boot_img_sector_size(state, BOOT_SECONDARY_SLOT, j);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200409 i++;
410 j++;
411 } else if (sz0 < sz1) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300412 sz0 += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
David Vincze2d736ad2019-02-18 11:50:22 +0100413 /* Guarantee that multiple sectors of the secondary slot
414 * fit into the primary slot.
415 */
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200416 if (smaller == 2) {
417 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors");
418 return 0;
419 }
420 smaller = 1;
421 i++;
422 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300423 sz1 += boot_img_sector_size(state, BOOT_SECONDARY_SLOT, j);
David Vincze2d736ad2019-02-18 11:50:22 +0100424 /* Guarantee that multiple sectors of the primary slot
425 * fit into the secondary slot.
426 */
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200427 if (smaller == 1) {
428 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors");
429 return 0;
430 }
431 smaller = 2;
432 j++;
433 }
434 if (sz0 == sz1) {
David Vincze2d736ad2019-02-18 11:50:22 +0100435 primary_slot_sz += sz0;
436 secondary_slot_sz += sz1;
437 /* Scratch has to fit each swap operation to the size of the larger
438 * sector among the primary slot and the secondary slot.
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200439 */
440 if (sz0 > scratch_sz || sz1 > scratch_sz) {
441 BOOT_LOG_WRN("Cannot upgrade: not all sectors fit inside scratch");
442 return 0;
443 }
444 smaller = sz0 = sz1 = 0;
445 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300446 }
447
David Vincze2d736ad2019-02-18 11:50:22 +0100448 if ((i != num_sectors_primary) ||
449 (j != num_sectors_secondary) ||
450 (primary_slot_sz != secondary_slot_sz)) {
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200451 BOOT_LOG_WRN("Cannot upgrade: slots are not compatible");
452 return 0;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800453 }
454
455 return 1;
456}
457
Fabio Utzig10ee6482019-08-01 12:04:52 -0300458#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
459static int
460boot_initialize_area(struct boot_loader_state *state, int flash_area)
461{
462 int num_sectors = BOOT_MAX_IMG_SECTORS;
463 int rc;
464
465 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
466 rc = flash_area_to_sectors(flash_area, &num_sectors,
467 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors);
468 BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors = (size_t)num_sectors;
469
470 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
471 rc = flash_area_to_sectors(flash_area, &num_sectors,
472 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors);
473 BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors = (size_t)num_sectors;
474
475 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
476 rc = flash_area_to_sectors(flash_area, &num_sectors,
477 state->scratch.sectors);
478 state->scratch.num_sectors = (size_t)num_sectors;
479 } else {
480 return BOOT_EFLASH;
481 }
482
483 return rc;
484}
485#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
486static int
487boot_initialize_area(struct boot_loader_state *state, int flash_area)
488{
489 uint32_t num_sectors;
490 struct flash_sector *out_sectors;
491 size_t *out_num_sectors;
492 int rc;
493
494 num_sectors = BOOT_MAX_IMG_SECTORS;
495
496 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
497 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
498 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
499 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
500 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
501 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
502 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
503 out_sectors = state->scratch.sectors;
504 out_num_sectors = &state->scratch.num_sectors;
505 } else {
506 return BOOT_EFLASH;
507 }
508
509 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
510 if (rc != 0) {
511 return rc;
512 }
513 *out_num_sectors = num_sectors;
514 return 0;
515}
516#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
517
Christopher Collins92ea77f2016-12-12 15:59:26 -0800518/**
519 * Determines the sector layout of both image slots and the scratch area.
520 * This information is necessary for calculating the number of bytes to erase
521 * and copy during an image swap. The information collected during this
Fabio Utzig10ee6482019-08-01 12:04:52 -0300522 * function is used to populate the state.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800523 */
524static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300525boot_read_sectors(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800526{
Fabio Utzigb0f04732019-07-31 09:49:19 -0300527 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800528 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800529
Fabio Utzig10ee6482019-08-01 12:04:52 -0300530 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300531
532 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800533 if (rc != 0) {
534 return BOOT_EFLASH;
535 }
536
Fabio Utzig10ee6482019-08-01 12:04:52 -0300537 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800538 if (rc != 0) {
539 return BOOT_EFLASH;
540 }
541
Fabio Utzig10ee6482019-08-01 12:04:52 -0300542 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200543 if (rc != 0) {
544 return BOOT_EFLASH;
545 }
546
Fabio Utzig10ee6482019-08-01 12:04:52 -0300547 BOOT_WRITE_SZ(state) = boot_write_sz(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800548
549 return 0;
550}
551
552static uint32_t
553boot_status_internal_off(int idx, int state, int elem_sz)
554{
555 int idx_sz;
556
557 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
558
Fabio Utzig39000012018-07-30 12:40:20 -0300559 return (idx - BOOT_STATUS_IDX_0) * idx_sz +
560 (state - BOOT_STATUS_STATE_0) * elem_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800561}
562
563/**
564 * Reads the status of a partially-completed swap, if any. This is necessary
565 * to recover in case the boot lodaer was reset in the middle of a swap
566 * operation.
567 */
568static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300569boot_read_status_bytes(const struct flash_area *fap,
570 struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800571{
572 uint32_t off;
573 uint8_t status;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300574 int max_entries;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800575 int found;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200576 int found_idx;
577 int invalid;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800578 int rc;
579 int i;
580
581 off = boot_status_off(fap);
Fabio Utzig10ee6482019-08-01 12:04:52 -0300582 max_entries = boot_status_entries(BOOT_CURR_IMG(state), fap);
Fabio Utzig9d160092019-08-09 07:46:34 -0300583 if (max_entries < 0) {
584 return BOOT_EBADARGS;
585 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300586
Christopher Collins92ea77f2016-12-12 15:59:26 -0800587 found = 0;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200588 found_idx = 0;
589 invalid = 0;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300590 for (i = 0; i < max_entries; i++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300591 rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(state),
Fabio Utzig178be542018-09-19 08:12:56 -0300592 &status, 1);
593 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800594 return BOOT_EFLASH;
595 }
596
Fabio Utzig178be542018-09-19 08:12:56 -0300597 if (rc == 1) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200598 if (found && !found_idx) {
599 found_idx = i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800600 }
601 } else if (!found) {
602 found = 1;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200603 } else if (found_idx) {
604 invalid = 1;
605 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800606 }
607 }
608
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200609 if (invalid) {
610 /* This means there was an error writing status on the last
611 * swap. Tell user and move on to validation!
612 */
613 BOOT_LOG_ERR("Detected inconsistent status!");
614
David Vincze2d736ad2019-02-18 11:50:22 +0100615#if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
616 /* With validation of the primary slot disabled, there is no way
617 * to be sure the swapped primary slot is OK, so abort!
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200618 */
619 assert(0);
620#endif
621 }
622
Christopher Collins92ea77f2016-12-12 15:59:26 -0800623 if (found) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200624 if (!found_idx) {
625 found_idx = i;
626 }
Fabio Utzig39000012018-07-30 12:40:20 -0300627 bs->idx = (found_idx / BOOT_STATUS_STATE_COUNT) + 1;
628 bs->state = (found_idx % BOOT_STATUS_STATE_COUNT) + 1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800629 }
630
631 return 0;
632}
633
634/**
635 * Reads the boot status from the flash. The boot status contains
636 * the current state of an interrupted image copy operation. If the boot
637 * status is not present, or it indicates that previous copy finished,
638 * there is no operation in progress.
639 */
640static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300641boot_read_status(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800642{
643 const struct flash_area *fap;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700644 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200645 uint8_t swap_info;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800646 int status_loc;
647 int area_id;
648 int rc;
649
650 memset(bs, 0, sizeof *bs);
Fabio Utzig39000012018-07-30 12:40:20 -0300651 bs->idx = BOOT_STATUS_IDX_0;
652 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700653 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800654
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700655#ifdef MCUBOOT_OVERWRITE_ONLY
656 /* Overwrite-only doesn't make use of the swap status area. */
657 return 0;
658#endif
659
Fabio Utzig10ee6482019-08-01 12:04:52 -0300660 status_loc = boot_status_source(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800661 switch (status_loc) {
662 case BOOT_STATUS_SOURCE_NONE:
663 return 0;
664
665 case BOOT_STATUS_SOURCE_SCRATCH:
666 area_id = FLASH_AREA_IMAGE_SCRATCH;
667 break;
668
David Vincze2d736ad2019-02-18 11:50:22 +0100669 case BOOT_STATUS_SOURCE_PRIMARY_SLOT:
Fabio Utzig10ee6482019-08-01 12:04:52 -0300670 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800671 break;
672
673 default:
674 assert(0);
675 return BOOT_EBADARGS;
676 }
677
678 rc = flash_area_open(area_id, &fap);
679 if (rc != 0) {
680 return BOOT_EFLASH;
681 }
682
Fabio Utzig10ee6482019-08-01 12:04:52 -0300683 rc = boot_read_status_bytes(fap, state, bs);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700684 if (rc == 0) {
David Vinczee2453472019-06-17 12:31:59 +0200685 off = boot_swap_info_off(fap);
686 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700687 if (rc == 1) {
David Vinczee2453472019-06-17 12:31:59 +0200688 BOOT_SET_SWAP_INFO(swap_info, 0, BOOT_SWAP_TYPE_NONE);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700689 rc = 0;
690 }
David Vinczee2453472019-06-17 12:31:59 +0200691
692 /* Extract the swap type info */
693 bs->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700694 }
Fabio Utzig46490722017-09-04 15:34:32 -0300695
696 flash_area_close(fap);
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700697
Fabio Utzig46490722017-09-04 15:34:32 -0300698 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800699}
700
701/**
702 * Writes the supplied boot status to the flash file system. The boot status
703 * contains the current state of an in-progress image copy operation.
704 *
705 * @param bs The boot status to write.
706 *
707 * @return 0 on success; nonzero on failure.
708 */
709int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300710boot_write_status(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800711{
712 const struct flash_area *fap;
713 uint32_t off;
714 int area_id;
715 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300716 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700717 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300718 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800719
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300720 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100721 * the trailer. Since in the last step the primary slot is erased, the
722 * first two status writes go to the scratch which will be copied to
723 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300724 */
725
Fabio Utzig2473ac02017-05-02 12:45:02 -0300726 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800727 /* Write to scratch. */
728 area_id = FLASH_AREA_IMAGE_SCRATCH;
729 } else {
David Vincze2d736ad2019-02-18 11:50:22 +0100730 /* Write to the primary slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300731 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800732 }
733
734 rc = flash_area_open(area_id, &fap);
735 if (rc != 0) {
736 rc = BOOT_EFLASH;
737 goto done;
738 }
739
740 off = boot_status_off(fap) +
Fabio Utzigd638b172019-08-09 10:38:05 -0300741 boot_status_internal_off(bs->idx, bs->state, BOOT_WRITE_SZ(state));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200742 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300743 erased_val = flash_area_erased_val(fap);
744 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700745 buf[0] = bs->state;
746
747 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800748 if (rc != 0) {
749 rc = BOOT_EFLASH;
750 goto done;
751 }
752
753 rc = 0;
754
755done:
756 flash_area_close(fap);
757 return rc;
758}
759
760/*
761 * Validate image hash/signature in a slot.
762 */
763static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300764boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
765 const struct flash_area *fap, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800766{
Fabio Utzig10ee6482019-08-01 12:04:52 -0300767 TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigb0f04732019-07-31 09:49:19 -0300768 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300769 int rc;
770
Fabio Utzig10ee6482019-08-01 12:04:52 -0300771#if (BOOT_IMAGE_NUMBER == 1)
772 (void)state;
773#endif
774
Fabio Utzigba829042018-09-18 08:29:34 -0300775 (void)bs;
776 (void)rc;
Fabio Utzigbc077932019-08-26 11:16:34 -0300777
778 image_index = BOOT_CURR_IMG(state);
779
780#ifdef MCUBOOT_ENC_IMAGES
781 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300782 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -0300783 if (rc < 0) {
784 return BOOT_EBADIMAGE;
785 }
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300786 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs->enckey[1])) {
Fabio Utzigba829042018-09-18 08:29:34 -0300787 return BOOT_EBADIMAGE;
788 }
789 }
Fabio Utzigbc077932019-08-26 11:16:34 -0300790#endif
791
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300792 if (bootutil_img_validate(BOOT_CURR_ENC(state), image_index, hdr, fap, tmpbuf,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300793 BOOT_TMPBUF_SZ, NULL, 0, NULL)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800794 return BOOT_EBADIMAGE;
795 }
Fabio Utzig10ee6482019-08-01 12:04:52 -0300796
Christopher Collins92ea77f2016-12-12 15:59:26 -0800797 return 0;
798}
799
800static int
801split_image_check(struct image_header *app_hdr,
802 const struct flash_area *app_fap,
803 struct image_header *loader_hdr,
804 const struct flash_area *loader_fap)
805{
806 static void *tmpbuf;
807 uint8_t loader_hash[32];
808
809 if (!tmpbuf) {
810 tmpbuf = malloc(BOOT_TMPBUF_SZ);
811 if (!tmpbuf) {
812 return BOOT_ENOMEM;
813 }
814 }
815
Fabio Utzig10ee6482019-08-01 12:04:52 -0300816 if (bootutil_img_validate(NULL, 0, loader_hdr, loader_fap, tmpbuf,
817 BOOT_TMPBUF_SZ, NULL, 0, loader_hash)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800818 return BOOT_EBADIMAGE;
819 }
820
Fabio Utzig10ee6482019-08-01 12:04:52 -0300821 if (bootutil_img_validate(NULL, 0, app_hdr, app_fap, tmpbuf,
822 BOOT_TMPBUF_SZ, loader_hash, 32, NULL)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800823 return BOOT_EBADIMAGE;
824 }
825
826 return 0;
827}
828
Fabio Utzig338a19f2018-12-03 08:37:08 -0200829/*
830 * Check that a memory area consists of a given value.
831 */
832static inline bool
833boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300834{
835 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200836 uint8_t *p = (uint8_t *)data;
837 for (i = 0; i < len; i++) {
838 if (val != p[i]) {
839 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300840 }
841 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200842 return true;
843}
844
845static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300846boot_check_header_erased(struct boot_loader_state *state, int slot)
Fabio Utzig338a19f2018-12-03 08:37:08 -0200847{
848 const struct flash_area *fap;
849 struct image_header *hdr;
850 uint8_t erased_val;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300851 int area_id;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200852 int rc;
853
Fabio Utzig10ee6482019-08-01 12:04:52 -0300854 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300855 rc = flash_area_open(area_id, &fap);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200856 if (rc != 0) {
857 return -1;
858 }
859
860 erased_val = flash_area_erased_val(fap);
861 flash_area_close(fap);
862
Fabio Utzig10ee6482019-08-01 12:04:52 -0300863 hdr = boot_img_hdr(state, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200864 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
865 return -1;
866 }
867
868 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300869}
870
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300871/*
872 * Check that there is a valid image in a slot
873 *
874 * @returns
875 * 0 if image was succesfully validated
876 * 1 if no bootloable image was found
877 * -1 on any errors
878 */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800879static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300880boot_validate_slot(struct boot_loader_state *state, int slot,
881 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800882{
883 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400884 struct image_header *hdr;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300885 int area_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800886 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300887
Fabio Utzig10ee6482019-08-01 12:04:52 -0300888 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300889 rc = flash_area_open(area_id, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800890 if (rc != 0) {
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300891 return -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800892 }
893
Fabio Utzig10ee6482019-08-01 12:04:52 -0300894 hdr = boot_img_hdr(state, slot);
895 if (boot_check_header_erased(state, slot) == 0 ||
896 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100897 /* No bootable image in slot; continue booting from the primary slot. */
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300898 rc = 1;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200899 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300900 }
901
Fabio Utzig10ee6482019-08-01 12:04:52 -0300902 if (hdr->ih_magic != IMAGE_MAGIC || boot_image_check(state, hdr, fap, bs)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100903 if (slot != BOOT_PRIMARY_SLOT) {
David Brownb38e0442017-02-24 13:57:12 -0700904 flash_area_erase(fap, 0, fap->fa_size);
David Vincze2d736ad2019-02-18 11:50:22 +0100905 /* Image in the secondary slot is invalid. Erase the image and
906 * continue booting from the primary slot.
David Brownb38e0442017-02-24 13:57:12 -0700907 */
908 }
David Vincze2d736ad2019-02-18 11:50:22 +0100909 BOOT_LOG_ERR("Image in the %s slot is not valid!",
910 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
Fabio Utzig338a19f2018-12-03 08:37:08 -0200911 rc = -1;
912 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800913 }
914
David Vincze2d736ad2019-02-18 11:50:22 +0100915 /* Image in the secondary slot is valid. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200916 rc = 0;
917
918out:
919 flash_area_close(fap);
920 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800921}
922
923/**
924 * Determines which swap operation to perform, if any. If it is determined
David Vincze2d736ad2019-02-18 11:50:22 +0100925 * that a swap operation is required, the image in the secondary slot is checked
926 * for validity. If the image in the secondary slot is invalid, it is erased,
927 * and a swap type of "none" is indicated.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800928 *
929 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
930 */
931static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300932boot_validated_swap_type(struct boot_loader_state *state,
933 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800934{
935 int swap_type;
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300936 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800937
Fabio Utzigb0f04732019-07-31 09:49:19 -0300938 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
Fabio Utzige575b0b2019-09-11 12:34:23 -0300939 if (BOOT_IS_UPGRADE(swap_type)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100940 /* Boot loader wants to switch to the secondary slot.
941 * Ensure image is valid.
942 */
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300943 rc = boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs);
944 if (rc == 1) {
945 swap_type = BOOT_SWAP_TYPE_NONE;
946 } else if (rc != 0) {
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300947 swap_type = BOOT_SWAP_TYPE_FAIL;
948 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800949 }
950
951 return swap_type;
952}
953
954/**
955 * Calculates the number of sectors the scratch area can contain. A "last"
956 * source sector is specified because images are copied backwards in flash
957 * (final index to index number 0).
958 *
959 * @param last_sector_idx The index of the last source sector
960 * (inclusive).
961 * @param out_first_sector_idx The index of the first source sector
962 * (inclusive) gets written here.
963 *
964 * @return The number of bytes comprised by the
965 * [first-sector, last-sector] range.
966 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300967#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800968static uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300969boot_copy_sz(struct boot_loader_state *state, int last_sector_idx,
970 int *out_first_sector_idx)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800971{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400972 size_t scratch_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800973 uint32_t new_sz;
974 uint32_t sz;
975 int i;
976
977 sz = 0;
978
Fabio Utzig10ee6482019-08-01 12:04:52 -0300979 scratch_sz = boot_scratch_area_size(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800980 for (i = last_sector_idx; i >= 0; i--) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300981 new_sz = sz + boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200982 /*
David Vincze2d736ad2019-02-18 11:50:22 +0100983 * The secondary slot is not being checked here, because
984 * `boot_slots_compatible` already provides assurance that the copy size
985 * will be compatible with the primary slot and scratch.
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200986 */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400987 if (new_sz > scratch_sz) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800988 break;
989 }
990 sz = new_sz;
991 }
992
993 /* i currently refers to a sector that doesn't fit or it is -1 because all
994 * sectors have been processed. In both cases, exclude sector i.
995 */
996 *out_first_sector_idx = i + 1;
997 return sz;
998}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300999#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001000
1001/**
1002 * Erases a region of flash.
1003 *
Fabio Utzigba829042018-09-18 08:29:34 -03001004 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001005 * @param off The offset within the flash area to start the
1006 * erase.
1007 * @param sz The number of bytes to erase.
1008 *
1009 * @return 0 on success; nonzero on failure.
1010 */
Fabio Utzigba829042018-09-18 08:29:34 -03001011static inline int
Fabio Utzigc28005b2019-09-10 12:18:29 -03001012boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001013{
Fabio Utzigba829042018-09-18 08:29:34 -03001014 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001015}
1016
1017/**
1018 * Copies the contents of one flash region to another. You must erase the
1019 * destination region prior to calling this function.
1020 *
1021 * @param flash_area_id_src The ID of the source flash area.
1022 * @param flash_area_id_dst The ID of the destination flash area.
1023 * @param off_src The offset within the source flash area to
1024 * copy from.
1025 * @param off_dst The offset within the destination flash area to
1026 * copy to.
1027 * @param sz The number of bytes to copy.
1028 *
1029 * @return 0 on success; nonzero on failure.
1030 */
1031static int
Fabio Utzigc28005b2019-09-10 12:18:29 -03001032boot_copy_region(struct boot_loader_state *state,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001033 const struct flash_area *fap_src,
Fabio Utzigba829042018-09-18 08:29:34 -03001034 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -08001035 uint32_t off_src, uint32_t off_dst, uint32_t sz)
1036{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001037 uint32_t bytes_copied;
1038 int chunk_sz;
1039 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -03001040#ifdef MCUBOOT_ENC_IMAGES
1041 uint32_t off;
Fabio Utziga87cc7d2019-08-26 11:21:45 -03001042 uint32_t tlv_off;
Fabio Utzigba829042018-09-18 08:29:34 -03001043 size_t blk_off;
1044 struct image_header *hdr;
1045 uint16_t idx;
1046 uint32_t blk_sz;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001047 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -03001048#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001049
Fabio Utzig10ee6482019-08-01 12:04:52 -03001050 TARGET_STATIC uint8_t buf[1024];
1051
1052#if !defined(MCUBOOT_ENC_IMAGES)
1053 (void)state;
1054#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001055
Christopher Collins92ea77f2016-12-12 15:59:26 -08001056 bytes_copied = 0;
1057 while (bytes_copied < sz) {
1058 if (sz - bytes_copied > sizeof buf) {
1059 chunk_sz = sizeof buf;
1060 } else {
1061 chunk_sz = sz - bytes_copied;
1062 }
1063
1064 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
1065 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001066 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001067 }
1068
Fabio Utzigba829042018-09-18 08:29:34 -03001069#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001070 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001071 if (fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) ||
1072 fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001073 /* assume the secondary slot as src, needs decryption */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001074 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001075 off = off_src;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001076 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001077 /* might need encryption (metadata from the primary slot) */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001078 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001079 off = off_dst;
1080 }
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001081 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001082 blk_sz = chunk_sz;
1083 idx = 0;
1084 if (off + bytes_copied < hdr->ih_hdr_size) {
1085 /* do not decrypt header */
1086 blk_off = 0;
1087 blk_sz = chunk_sz - hdr->ih_hdr_size;
1088 idx = hdr->ih_hdr_size;
1089 } else {
1090 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
1091 }
Fabio Utziga87cc7d2019-08-26 11:21:45 -03001092 tlv_off = BOOT_TLV_OFF(hdr);
1093 if (off + bytes_copied + chunk_sz > tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -03001094 /* do not decrypt TLVs */
Fabio Utziga87cc7d2019-08-26 11:21:45 -03001095 if (off + bytes_copied >= tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -03001096 blk_sz = 0;
1097 } else {
Fabio Utziga87cc7d2019-08-26 11:21:45 -03001098 blk_sz = tlv_off - (off + bytes_copied);
Fabio Utzigba829042018-09-18 08:29:34 -03001099 }
1100 }
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001101 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
Fabio Utzigb0f04732019-07-31 09:49:19 -03001102 (off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
1103 blk_off, &buf[idx]);
Fabio Utzigba829042018-09-18 08:29:34 -03001104 }
1105 }
1106#endif
1107
Christopher Collins92ea77f2016-12-12 15:59:26 -08001108 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
1109 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001110 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001111 }
1112
1113 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -03001114
1115 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -08001116 }
1117
Fabio Utzigba829042018-09-18 08:29:34 -03001118 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001119}
1120
David Brown6b1b3b92017-09-19 08:59:10 -06001121#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -03001122static inline int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001123boot_status_init(const struct boot_loader_state *state,
1124 const struct flash_area *fap,
1125 const struct boot_status *bs)
Fabio Utzig2473ac02017-05-02 12:45:02 -03001126{
Fabio Utzig2473ac02017-05-02 12:45:02 -03001127 struct boot_swap_state swap_state;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001128 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001129 int rc;
1130
Fabio Utzig10ee6482019-08-01 12:04:52 -03001131#if (BOOT_IMAGE_NUMBER == 1)
1132 (void)state;
1133#endif
1134
1135 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001136
Christopher Collins2c88e692019-05-22 15:10:14 -07001137 BOOT_LOG_DBG("initializing status; fa_id=%d", fap->fa_id);
1138
Fabio Utzigb0f04732019-07-31 09:49:19 -03001139 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
1140 &swap_state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001141 assert(rc == 0);
1142
Christopher Collinsa1c12042019-05-23 14:00:28 -07001143 if (bs->swap_type != BOOT_SWAP_TYPE_NONE) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03001144 rc = boot_write_swap_info(fap, bs->swap_type, image_index);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001145 assert(rc == 0);
1146 }
1147
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001148 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001149 rc = boot_write_image_ok(fap);
1150 assert(rc == 0);
1151 }
1152
Fabio Utzig46490722017-09-04 15:34:32 -03001153 rc = boot_write_swap_size(fap, bs->swap_size);
1154 assert(rc == 0);
1155
Fabio Utzigba829042018-09-18 08:29:34 -03001156#ifdef MCUBOOT_ENC_IMAGES
1157 rc = boot_write_enc_key(fap, 0, bs->enckey[0]);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001158 assert(rc == 0);
1159
Fabio Utzigba829042018-09-18 08:29:34 -03001160 rc = boot_write_enc_key(fap, 1, bs->enckey[1]);
1161 assert(rc == 0);
1162#endif
1163
1164 rc = boot_write_magic(fap);
1165 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001166
1167 return 0;
1168}
Christopher Collinsa1c12042019-05-23 14:00:28 -07001169
David Brown6b1b3b92017-09-19 08:59:10 -06001170#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001171
Fabio Utzig358c9352017-07-25 22:10:45 -03001172#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -03001173static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001174boot_erase_trailer_sectors(const struct boot_loader_state *state,
1175 const struct flash_area *fap)
Fabio Utzig2473ac02017-05-02 12:45:02 -03001176{
1177 uint8_t slot;
Fabio Utziged0ca432019-01-23 14:50:11 -02001178 uint32_t sector;
1179 uint32_t trailer_sz;
1180 uint32_t total_sz;
1181 uint32_t off;
1182 uint32_t sz;
David Vinczeb75c12a2019-03-22 14:58:33 +01001183 int fa_id_primary;
1184 int fa_id_secondary;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001185 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001186 int rc;
1187
Christopher Collins2c88e692019-05-22 15:10:14 -07001188 BOOT_LOG_DBG("erasing trailer; fa_id=%d", fap->fa_id);
1189
Fabio Utzig10ee6482019-08-01 12:04:52 -03001190 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001191 fa_id_primary = flash_area_id_from_multi_image_slot(image_index,
1192 BOOT_PRIMARY_SLOT);
1193 fa_id_secondary = flash_area_id_from_multi_image_slot(image_index,
1194 BOOT_SECONDARY_SLOT);
David Vinczeb75c12a2019-03-22 14:58:33 +01001195
1196 if (fap->fa_id == fa_id_primary) {
David Vincze2d736ad2019-02-18 11:50:22 +01001197 slot = BOOT_PRIMARY_SLOT;
David Vinczeb75c12a2019-03-22 14:58:33 +01001198 } else if (fap->fa_id == fa_id_secondary) {
David Vincze2d736ad2019-02-18 11:50:22 +01001199 slot = BOOT_SECONDARY_SLOT;
David Vinczeb75c12a2019-03-22 14:58:33 +01001200 } else {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001201 return BOOT_EFLASH;
1202 }
1203
Fabio Utziged0ca432019-01-23 14:50:11 -02001204 /* delete starting from last sector and moving to beginning */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001205 sector = boot_img_num_sectors(state, slot) - 1;
1206 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
Fabio Utziged0ca432019-01-23 14:50:11 -02001207 total_sz = 0;
1208 do {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001209 sz = boot_img_sector_size(state, slot, sector);
1210 off = boot_img_sector_off(state, slot, sector);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001211 rc = boot_erase_region(fap, off, sz);
Fabio Utziged0ca432019-01-23 14:50:11 -02001212 assert(rc == 0);
1213
1214 sector--;
1215 total_sz += sz;
1216 } while (total_sz < trailer_sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001217
1218 return rc;
1219}
Fabio Utzig358c9352017-07-25 22:10:45 -03001220#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001221
Christopher Collins92ea77f2016-12-12 15:59:26 -08001222/**
1223 * Swaps the contents of two flash regions within the two image slots.
1224 *
1225 * @param idx The index of the first sector in the range of
1226 * sectors being swapped.
1227 * @param sz The number of bytes to swap.
1228 * @param bs The current boot status. This struct gets
1229 * updated according to the outcome.
1230 *
1231 * @return 0 on success; nonzero on failure.
1232 */
Fabio Utzig3488eef2017-06-12 10:25:43 -03001233#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -08001234static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001235boot_swap_sectors(int idx, uint32_t sz, struct boot_loader_state *state,
1236 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001237{
David Vincze2d736ad2019-02-18 11:50:22 +01001238 const struct flash_area *fap_primary_slot;
1239 const struct flash_area *fap_secondary_slot;
Fabio Utzigba829042018-09-18 08:29:34 -03001240 const struct flash_area *fap_scratch;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001241 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001242 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001243 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001244 uint32_t scratch_trailer_off;
1245 struct boot_swap_state swap_state;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001246 size_t last_sector;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001247 bool erase_scratch;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001248 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001249 int rc;
1250
1251 /* Calculate offset from start of image area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001252 img_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001253
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001254 copy_sz = sz;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001255 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
Fabio Utzig9678c972017-05-23 11:28:56 -04001256
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001257 /* sz in this function is always sized on a multiple of the sector size.
1258 * The check against the start offset of the last sector
Fabio Utzig9678c972017-05-23 11:28:56 -04001259 * is to determine if we're swapping the last sector. The last sector
1260 * needs special handling because it's where the trailer lives. If we're
1261 * copying it, we need to use scratch to write the trailer temporarily.
1262 *
1263 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
1264 * controls if special handling is needed (swapping last sector).
1265 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001266 last_sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
David Vinczeba3bd602019-06-17 16:01:43 +02001267 if ((img_off + sz) >
Fabio Utzig10ee6482019-08-01 12:04:52 -03001268 boot_img_sector_off(state, BOOT_PRIMARY_SLOT, last_sector)) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001269 copy_sz -= trailer_sz;
1270 }
1271
Fabio Utzig39000012018-07-30 12:40:20 -03001272 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001273
Fabio Utzig10ee6482019-08-01 12:04:52 -03001274 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001275
1276 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1277 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001278 assert (rc == 0);
1279
Fabio Utzigb0f04732019-07-31 09:49:19 -03001280 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1281 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001282 assert (rc == 0);
1283
1284 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch);
1285 assert (rc == 0);
1286
Fabio Utzig39000012018-07-30 12:40:20 -03001287 if (bs->state == BOOT_STATUS_STATE_0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001288 BOOT_LOG_DBG("erasing scratch area");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001289 rc = boot_erase_region(fap_scratch, 0, fap_scratch->fa_size);
Christopher Collins4772ac42017-02-27 20:08:01 -08001290 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001291
Fabio Utzig39000012018-07-30 12:40:20 -03001292 if (bs->idx == BOOT_STATUS_IDX_0) {
Christopher Collinsa1c12042019-05-23 14:00:28 -07001293 /* Write a trailer to the scratch area, even if we don't need the
1294 * scratch area for status. We need a temporary place to store the
1295 * `swap-type` while we erase the primary trailer.
1296 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001297 rc = boot_status_init(state, fap_scratch, bs);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001298 assert(rc == 0);
1299
1300 if (!bs->use_scratch) {
1301 /* Prepare the primary status area... here it is known that the
1302 * last sector is not being used by the image data so it's safe
1303 * to erase.
Fabio Utzig2473ac02017-05-02 12:45:02 -03001304 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001305 rc = boot_erase_trailer_sectors(state, fap_primary_slot);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001306 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001307
Fabio Utzig10ee6482019-08-01 12:04:52 -03001308 rc = boot_status_init(state, fap_primary_slot, bs);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001309 assert(rc == 0);
1310
1311 /* Erase the temporary trailer from the scratch area. */
Fabio Utzigc28005b2019-09-10 12:18:29 -03001312 rc = boot_erase_region(fap_scratch, 0, fap_scratch->fa_size);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001313 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001314 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001315 }
1316
Fabio Utzigc28005b2019-09-10 12:18:29 -03001317 rc = boot_copy_region(state, fap_secondary_slot, fap_scratch,
Christopher Collinsa1c12042019-05-23 14:00:28 -07001318 img_off, 0, copy_sz);
1319 assert(rc == 0);
1320
Fabio Utzig10ee6482019-08-01 12:04:52 -03001321 rc = boot_write_status(state, bs);
Fabio Utzig4d7396d2019-09-05 18:38:05 -03001322 bs->state = BOOT_STATUS_STATE_1;
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001323 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001324 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001325
Fabio Utzig39000012018-07-30 12:40:20 -03001326 if (bs->state == BOOT_STATUS_STATE_1) {
Fabio Utzigc28005b2019-09-10 12:18:29 -03001327 rc = boot_erase_region(fap_secondary_slot, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001328 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001329
Fabio Utzigc28005b2019-09-10 12:18:29 -03001330 rc = boot_copy_region(state, fap_primary_slot, fap_secondary_slot,
David Vincze2d736ad2019-02-18 11:50:22 +01001331 img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001332 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001333
Fabio Utzig39000012018-07-30 12:40:20 -03001334 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001335 /* If not all sectors of the slot are being swapped,
David Vincze2d736ad2019-02-18 11:50:22 +01001336 * guarantee here that only the primary slot will have the state.
Fabio Utzig2473ac02017-05-02 12:45:02 -03001337 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001338 rc = boot_erase_trailer_sectors(state, fap_secondary_slot);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001339 assert(rc == 0);
1340 }
1341
Fabio Utzig10ee6482019-08-01 12:04:52 -03001342 rc = boot_write_status(state, bs);
Fabio Utzig4d7396d2019-09-05 18:38:05 -03001343 bs->state = BOOT_STATUS_STATE_2;
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001344 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001345 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001346
Fabio Utzig39000012018-07-30 12:40:20 -03001347 if (bs->state == BOOT_STATUS_STATE_2) {
Fabio Utzigc28005b2019-09-10 12:18:29 -03001348 rc = boot_erase_region(fap_primary_slot, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001349 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001350
Christopher Collinsa1c12042019-05-23 14:00:28 -07001351 /* NOTE: If this is the final sector, we exclude the image trailer from
1352 * this copy (copy_sz was truncated earlier).
1353 */
Fabio Utzigc28005b2019-09-10 12:18:29 -03001354 rc = boot_copy_region(state, fap_scratch, fap_primary_slot,
David Vincze2d736ad2019-02-18 11:50:22 +01001355 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001356 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001357
Fabio Utzig94d998c2017-05-22 11:02:41 -04001358 if (bs->use_scratch) {
Fabio Utzigba829042018-09-18 08:29:34 -03001359 scratch_trailer_off = boot_status_off(fap_scratch);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001360
1361 /* copy current status that is being maintained in scratch */
Fabio Utzigc28005b2019-09-10 12:18:29 -03001362 rc = boot_copy_region(state, fap_scratch, fap_primary_slot,
David Vincze2d736ad2019-02-18 11:50:22 +01001363 scratch_trailer_off, img_off + copy_sz,
Fabio Utzig4d7396d2019-09-05 18:38:05 -03001364 (BOOT_STATUS_STATE_COUNT - 1) * BOOT_WRITE_SZ(state));
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001365 BOOT_STATUS_ASSERT(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001366
Fabio Utzig2473ac02017-05-02 12:45:02 -03001367 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1368 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001369 assert(rc == 0);
1370
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001371 if (swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze2d736ad2019-02-18 11:50:22 +01001372 rc = boot_write_image_ok(fap_primary_slot);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001373 assert(rc == 0);
1374 }
1375
Christopher Collinsa1c12042019-05-23 14:00:28 -07001376 if (swap_state.swap_type != BOOT_SWAP_TYPE_NONE) {
David Vinczee2453472019-06-17 12:31:59 +02001377 rc = boot_write_swap_info(fap_primary_slot,
Fabio Utzigb0f04732019-07-31 09:49:19 -03001378 swap_state.swap_type, image_index);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001379 assert(rc == 0);
1380 }
1381
David Vincze2d736ad2019-02-18 11:50:22 +01001382 rc = boot_write_swap_size(fap_primary_slot, bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001383 assert(rc == 0);
1384
Fabio Utzigba829042018-09-18 08:29:34 -03001385#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +01001386 rc = boot_write_enc_key(fap_primary_slot, 0, bs->enckey[0]);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001387 assert(rc == 0);
1388
David Vincze2d736ad2019-02-18 11:50:22 +01001389 rc = boot_write_enc_key(fap_primary_slot, 1, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001390 assert(rc == 0);
1391#endif
David Vincze2d736ad2019-02-18 11:50:22 +01001392 rc = boot_write_magic(fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001393 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001394 }
1395
Christopher Collinsa1c12042019-05-23 14:00:28 -07001396 /* If we wrote a trailer to the scratch area, erase it after we persist
1397 * a trailer to the primary slot. We do this to prevent mcuboot from
1398 * reading a stale status from the scratch area in case of immediate
1399 * reset.
1400 */
1401 erase_scratch = bs->use_scratch;
1402 bs->use_scratch = 0;
1403
Fabio Utzig4d7396d2019-09-05 18:38:05 -03001404 rc = boot_write_status(state, bs);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001405 bs->idx++;
Fabio Utzig39000012018-07-30 12:40:20 -03001406 bs->state = BOOT_STATUS_STATE_0;
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001407 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001408
1409 if (erase_scratch) {
Fabio Utzigc28005b2019-09-10 12:18:29 -03001410 rc = boot_erase_region(fap_scratch, 0, sz);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001411 assert(rc == 0);
1412 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001413 }
Fabio Utzigba829042018-09-18 08:29:34 -03001414
David Vincze2d736ad2019-02-18 11:50:22 +01001415 flash_area_close(fap_primary_slot);
1416 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001417 flash_area_close(fap_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001418}
Fabio Utzig3488eef2017-06-12 10:25:43 -03001419#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001420
1421/**
David Vincze2d736ad2019-02-18 11:50:22 +01001422 * Overwrite primary slot with the image contained in the secondary slot.
1423 * If a prior copy operation was interrupted by a system reset, this function
1424 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001425 *
1426 * @param bs The current boot status. This function reads
1427 * this struct to determine if it is resuming
1428 * an interrupted swap operation. This
1429 * function writes the updated status to this
1430 * function on return.
1431 *
1432 * @return 0 on success; nonzero on failure.
1433 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001434#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001435static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001436boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
David Brown17609d82017-05-05 09:41:34 -06001437{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001438 size_t sect_count;
1439 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001440 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001441 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001442 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001443 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +01001444 const struct flash_area *fap_primary_slot;
1445 const struct flash_area *fap_secondary_slot;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001446 uint8_t image_index;
David Vincze2d736ad2019-02-18 11:50:22 +01001447
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001448 (void)bs;
1449
Fabio Utzig13d9e352017-10-05 20:32:31 -03001450#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1451 uint32_t src_size = 0;
Fabio Utzigd638b172019-08-09 10:38:05 -03001452 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001453 assert(rc == 0);
1454#endif
David Brown17609d82017-05-05 09:41:34 -06001455
David Vincze2d736ad2019-02-18 11:50:22 +01001456 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1457 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -06001458
Fabio Utzigb0f04732019-07-31 09:49:19 -03001459 image_index = BOOT_CURR_IMG(state);
1460
1461 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1462 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001463 assert (rc == 0);
1464
Fabio Utzigb0f04732019-07-31 09:49:19 -03001465 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1466 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001467 assert (rc == 0);
1468
Fabio Utzig10ee6482019-08-01 12:04:52 -03001469 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001470 for (sect = 0, size = 0; sect < sect_count; sect++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001471 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001472 rc = boot_erase_region(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001473 assert(rc == 0);
1474
1475 size += this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001476
1477#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1478 if (size >= src_size) {
1479 break;
1480 }
1481#endif
David Brown17609d82017-05-05 09:41:34 -06001482 }
1483
Fabio Utzigba829042018-09-18 08:29:34 -03001484#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001485 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001486 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001487 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
Fabio Utzigb0f04732019-07-31 09:49:19 -03001488 fap_secondary_slot, bs->enckey[1]);
David Vincze2d736ad2019-02-18 11:50:22 +01001489
Fabio Utzigba829042018-09-18 08:29:34 -03001490 if (rc < 0) {
1491 return BOOT_EBADIMAGE;
1492 }
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001493 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs->enckey[1])) {
Fabio Utzigba829042018-09-18 08:29:34 -03001494 return BOOT_EBADIMAGE;
1495 }
1496 }
1497#endif
1498
David Vincze2d736ad2019-02-18 11:50:22 +01001499 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1500 size);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001501 rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot, 0, 0, size);
David Brown17609d82017-05-05 09:41:34 -06001502
Fabio Utzig13d9e352017-10-05 20:32:31 -03001503 /*
1504 * Erases header and trailer. The trailer is erased because when a new
1505 * image is written without a trailer as is the case when using newt, the
1506 * trailer that was left might trigger a new upgrade.
1507 */
Christopher Collins2c88e692019-05-22 15:10:14 -07001508 BOOT_LOG_DBG("erasing secondary header");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001509 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001510 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
1511 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001512 assert(rc == 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001513 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -07001514 BOOT_LOG_DBG("erasing secondary trailer");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001515 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001516 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
1517 last_sector),
1518 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
1519 last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001520 assert(rc == 0);
1521
David Vincze2d736ad2019-02-18 11:50:22 +01001522 flash_area_close(fap_primary_slot);
1523 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001524
David Vincze2d736ad2019-02-18 11:50:22 +01001525 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001526
1527 return 0;
1528}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001529#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001530
Christopher Collinsa1c12042019-05-23 14:00:28 -07001531#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -03001532/**
1533 * Swaps the two images in flash. If a prior copy operation was interrupted
1534 * by a system reset, this function completes that operation.
1535 *
1536 * @param bs The current boot status. This function reads
1537 * this struct to determine if it is resuming
1538 * an interrupted swap operation. This
1539 * function writes the updated status to this
1540 * function on return.
1541 *
1542 * @return 0 on success; nonzero on failure.
1543 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001544static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001545boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001546{
1547 uint32_t sz;
1548 int first_sector_idx;
1549 int last_sector_idx;
David Vincze2d736ad2019-02-18 11:50:22 +01001550 int last_idx_secondary_slot;
Fabio Utzigcd5774b2017-11-29 10:18:26 -02001551 uint32_t swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001552 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001553#ifdef MCUBOOT_ENC_IMAGES
1554 const struct flash_area *fap;
1555 uint8_t slot;
1556 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001557#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001558 uint32_t size;
1559 uint32_t copy_size;
David Vincze2d736ad2019-02-18 11:50:22 +01001560 uint32_t primary_slot_size;
1561 uint32_t secondary_slot_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001562 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001563 int rc;
1564
1565 /* FIXME: just do this if asked by user? */
1566
1567 size = copy_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001568 image_index = BOOT_CURR_IMG(state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001569
Fabio Utzig39000012018-07-30 12:40:20 -03001570 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Fabio Utzig46490722017-09-04 15:34:32 -03001571 /*
1572 * No swap ever happened, so need to find the largest image which
1573 * will be used to determine the amount of sectors to swap.
1574 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001575 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001576 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001577 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001578 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001579 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001580
Fabio Utzigba829042018-09-18 08:29:34 -03001581#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001582 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001583 fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001584 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs->enckey[0]);
Fabio Utzigba829042018-09-18 08:29:34 -03001585 assert(rc >= 0);
1586
1587 if (rc == 0) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001588 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 0, bs->enckey[0]);
Fabio Utzigba829042018-09-18 08:29:34 -03001589 assert(rc == 0);
1590 } else {
1591 rc = 0;
1592 }
1593 } else {
1594 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1595 }
1596#endif
1597
Fabio Utzig10ee6482019-08-01 12:04:52 -03001598 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001599 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001600 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001601 assert(rc == 0);
1602 }
1603
Fabio Utzigba829042018-09-18 08:29:34 -03001604#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001605 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001606 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001607 fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001608 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001609 assert(rc >= 0);
1610
1611 if (rc == 0) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001612 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001613 assert(rc == 0);
1614 } else {
1615 rc = 0;
1616 }
1617 } else {
1618 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1619 }
1620#endif
1621
Fabio Utzig46490722017-09-04 15:34:32 -03001622 if (size > copy_size) {
1623 copy_size = size;
1624 }
1625
1626 bs->swap_size = copy_size;
1627 } else {
1628 /*
1629 * If a swap was under way, the swap_size should already be present
1630 * in the trailer...
1631 */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001632 rc = boot_read_swap_size(image_index, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001633 assert(rc == 0);
1634
1635 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001636
1637#ifdef MCUBOOT_ENC_IMAGES
1638 for (slot = 0; slot <= 1; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03001639 rc = boot_read_enc_key(image_index, slot, bs->enckey[slot]);
Fabio Utzigba829042018-09-18 08:29:34 -03001640 assert(rc == 0);
1641
1642 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001643 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001644 break;
1645 }
1646 }
1647
1648 if (i != BOOT_ENC_KEY_SIZE) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001649 boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs->enckey[slot]);
Fabio Utzigba829042018-09-18 08:29:34 -03001650 }
1651 }
1652#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001653 }
1654
David Vincze2d736ad2019-02-18 11:50:22 +01001655 primary_slot_size = 0;
1656 secondary_slot_size = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001657 last_sector_idx = 0;
David Vincze2d736ad2019-02-18 11:50:22 +01001658 last_idx_secondary_slot = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001659
1660 /*
1661 * Knowing the size of the largest image between both slots, here we
David Vincze2d736ad2019-02-18 11:50:22 +01001662 * find what is the last sector in the primary slot that needs swapping.
1663 * Since we already know that both slots are compatible, the secondary
1664 * slot's last sector is not really required after this check is finished.
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001665 */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001666 while (1) {
David Vincze2d736ad2019-02-18 11:50:22 +01001667 if ((primary_slot_size < copy_size) ||
1668 (primary_slot_size < secondary_slot_size)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001669 primary_slot_size += boot_img_sector_size(state,
David Vincze2d736ad2019-02-18 11:50:22 +01001670 BOOT_PRIMARY_SLOT,
1671 last_sector_idx);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001672 }
David Vincze2d736ad2019-02-18 11:50:22 +01001673 if ((secondary_slot_size < copy_size) ||
1674 (secondary_slot_size < primary_slot_size)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001675 secondary_slot_size += boot_img_sector_size(state,
David Vincze2d736ad2019-02-18 11:50:22 +01001676 BOOT_SECONDARY_SLOT,
1677 last_idx_secondary_slot);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001678 }
David Vincze2d736ad2019-02-18 11:50:22 +01001679 if (primary_slot_size >= copy_size &&
1680 secondary_slot_size >= copy_size &&
1681 primary_slot_size == secondary_slot_size) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001682 break;
1683 }
1684 last_sector_idx++;
David Vincze2d736ad2019-02-18 11:50:22 +01001685 last_idx_secondary_slot++;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001686 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001687
1688 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001689 while (last_sector_idx >= 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001690 sz = boot_copy_sz(state, last_sector_idx, &first_sector_idx);
Fabio Utzig39000012018-07-30 12:40:20 -03001691 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001692 boot_swap_sectors(first_sector_idx, sz, state, bs);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001693 }
1694
1695 last_sector_idx = first_sector_idx - 1;
1696 swap_idx++;
1697 }
1698
David Vincze2d736ad2019-02-18 11:50:22 +01001699#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001700 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001701 BOOT_LOG_WRN("%d status write fails performing the swap",
1702 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001703 }
1704#endif
1705
Christopher Collins92ea77f2016-12-12 15:59:26 -08001706 return 0;
1707}
David Brown17609d82017-05-05 09:41:34 -06001708#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001709
1710/**
David Vincze2d736ad2019-02-18 11:50:22 +01001711 * Marks the image in the primary slot as fully copied.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001712 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001713#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001714static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001715boot_set_copy_done(uint8_t image_index)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001716{
1717 const struct flash_area *fap;
1718 int rc;
1719
Fabio Utzigb0f04732019-07-31 09:49:19 -03001720 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1721 &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001722 if (rc != 0) {
1723 return BOOT_EFLASH;
1724 }
1725
1726 rc = boot_write_copy_done(fap);
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001727 flash_area_close(fap);
1728 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001729}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001730#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001731
1732/**
David Vincze2d736ad2019-02-18 11:50:22 +01001733 * Marks a reverted image in the primary slot as confirmed. This is necessary to
1734 * ensure the status bytes from the image revert operation don't get processed
1735 * on a subsequent boot.
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001736 *
1737 * NOTE: image_ok is tested before writing because if there's a valid permanent
David Vincze2d736ad2019-02-18 11:50:22 +01001738 * image installed on the primary slot and the new image to be upgrade to has a
1739 * bad sig, image_ok would be overwritten.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001740 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001741#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001742static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001743boot_set_image_ok(uint8_t image_index)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001744{
1745 const struct flash_area *fap;
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001746 struct boot_swap_state state;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001747 int rc;
1748
Fabio Utzigb0f04732019-07-31 09:49:19 -03001749 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1750 &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001751 if (rc != 0) {
1752 return BOOT_EFLASH;
1753 }
1754
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001755 rc = boot_read_swap_state(fap, &state);
1756 if (rc != 0) {
1757 rc = BOOT_EFLASH;
1758 goto out;
1759 }
1760
1761 if (state.image_ok == BOOT_FLAG_UNSET) {
1762 rc = boot_write_image_ok(fap);
1763 }
1764
1765out:
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001766 flash_area_close(fap);
1767 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001768}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001769#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001770
David Vinczee32483f2019-06-13 10:46:24 +02001771#if (BOOT_IMAGE_NUMBER > 1)
1772/**
Fabio Utzig019a81a2019-08-27 10:33:39 -03001773 * Check if the version of the image is not older than required.
1774 *
1775 * @param req Required minimal image version.
1776 * @param ver Version of the image to be checked.
1777 *
1778 * @return 0 if the version is sufficient, nonzero otherwise.
1779 */
1780static int
1781boot_is_version_sufficient(struct image_version *req,
1782 struct image_version *ver)
1783{
1784 if (ver->iv_major > req->iv_major) {
1785 return 0;
1786 }
1787 if (ver->iv_major < req->iv_major) {
1788 return BOOT_EBADVERSION;
1789 }
1790 /* The major version numbers are equal. */
1791 if (ver->iv_minor > req->iv_minor) {
1792 return 0;
1793 }
1794 if (ver->iv_minor < req->iv_minor) {
1795 return BOOT_EBADVERSION;
1796 }
1797 /* The minor version numbers are equal. */
1798 if (ver->iv_revision < req->iv_revision) {
1799 return BOOT_EBADVERSION;
1800 }
1801
1802 return 0;
1803}
1804
1805/**
David Vinczee32483f2019-06-13 10:46:24 +02001806 * Check the image dependency whether it is satisfied and modify
1807 * the swap type if necessary.
1808 *
1809 * @param dep Image dependency which has to be verified.
1810 *
1811 * @return 0 on success; nonzero on failure.
1812 */
1813static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001814boot_verify_slot_dependency(struct boot_loader_state *state,
1815 struct image_dependency *dep)
David Vinczee32483f2019-06-13 10:46:24 +02001816{
1817 struct image_version *dep_version;
1818 size_t dep_slot;
1819 int rc;
David Browne6ab34c2019-09-03 12:24:21 -06001820 uint8_t swap_type;
David Vinczee32483f2019-06-13 10:46:24 +02001821
1822 /* Determine the source of the image which is the subject of
1823 * the dependency and get it's version. */
David Browne6ab34c2019-09-03 12:24:21 -06001824 swap_type = state->swap_type[dep->image_id];
Fabio Utzigb1adb1e2019-09-11 11:42:53 -03001825 dep_slot = (swap_type != BOOT_SWAP_TYPE_NONE) ?
David Vinczee32483f2019-06-13 10:46:24 +02001826 BOOT_SECONDARY_SLOT : BOOT_PRIMARY_SLOT;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001827 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +02001828
1829 rc = boot_is_version_sufficient(&dep->image_min_version, dep_version);
1830 if (rc != 0) {
1831 /* Dependency not satisfied.
1832 * Modify the swap type to decrease the version number of the image
1833 * (which will be located in the primary slot after the boot process),
1834 * consequently the number of unsatisfied dependencies will be
1835 * decreased or remain the same.
1836 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001837 switch (BOOT_SWAP_TYPE(state)) {
David Vinczee32483f2019-06-13 10:46:24 +02001838 case BOOT_SWAP_TYPE_TEST:
1839 case BOOT_SWAP_TYPE_PERM:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001840 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczee32483f2019-06-13 10:46:24 +02001841 break;
1842 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001843 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczee32483f2019-06-13 10:46:24 +02001844 break;
1845 default:
1846 break;
1847 }
1848 }
1849
1850 return rc;
1851}
1852
1853/**
1854 * Read all dependency TLVs of an image from the flash and verify
1855 * one after another to see if they are all satisfied.
1856 *
1857 * @param slot Image slot number.
1858 *
1859 * @return 0 on success; nonzero on failure.
1860 */
1861static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001862boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
David Vinczee32483f2019-06-13 10:46:24 +02001863{
1864 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001865 struct image_tlv_iter it;
David Vinczee32483f2019-06-13 10:46:24 +02001866 struct image_dependency dep;
1867 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001868 uint16_t len;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001869 int area_id;
David Vinczee32483f2019-06-13 10:46:24 +02001870 int rc;
1871
Fabio Utzig10ee6482019-08-01 12:04:52 -03001872 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001873 rc = flash_area_open(area_id, &fap);
David Vinczee32483f2019-06-13 10:46:24 +02001874 if (rc != 0) {
1875 rc = BOOT_EFLASH;
1876 goto done;
1877 }
1878
Fabio Utzig61fd8882019-09-14 20:00:20 -03001879 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
1880 IMAGE_TLV_DEPENDENCY, true);
David Vinczee32483f2019-06-13 10:46:24 +02001881 if (rc != 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001882 goto done;
1883 }
1884
Fabio Utzig61fd8882019-09-14 20:00:20 -03001885 while (true) {
1886 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
1887 if (rc < 0) {
1888 return -1;
1889 } else if (rc > 0) {
1890 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001891 break;
1892 }
Fabio Utzig61fd8882019-09-14 20:00:20 -03001893
1894 if (len != sizeof(dep)) {
1895 rc = BOOT_EBADIMAGE;
1896 goto done;
1897 }
1898
1899 rc = flash_area_read(fap, off, &dep, len);
1900 if (rc != 0) {
1901 rc = BOOT_EFLASH;
1902 goto done;
1903 }
1904
1905 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
1906 rc = BOOT_EBADARGS;
1907 goto done;
1908 }
1909
1910 /* Verify dependency and modify the swap type if not satisfied. */
1911 rc = boot_verify_slot_dependency(state, &dep);
1912 if (rc != 0) {
1913 /* Dependency not satisfied. */
1914 goto done;
1915 }
David Vinczee32483f2019-06-13 10:46:24 +02001916 }
1917
1918done:
1919 flash_area_close(fap);
1920 return rc;
1921}
1922
1923/**
David Vinczee32483f2019-06-13 10:46:24 +02001924 * Iterate over all the images and verify whether the image dependencies in the
1925 * TLV area are all satisfied and update the related swap type if necessary.
1926 */
Fabio Utzig298913b2019-08-28 11:22:45 -03001927static int
1928boot_verify_dependencies(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001929{
David Vinczee32483f2019-06-13 10:46:24 +02001930 int rc;
Fabio Utzig298913b2019-08-28 11:22:45 -03001931 uint8_t slot;
David Vinczee32483f2019-06-13 10:46:24 +02001932
Fabio Utzig10ee6482019-08-01 12:04:52 -03001933 BOOT_CURR_IMG(state) = 0;
1934 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001935 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1936 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
1937 slot = BOOT_SECONDARY_SLOT;
1938 } else {
1939 slot = BOOT_PRIMARY_SLOT;
1940 }
1941
1942 rc = boot_verify_slot_dependencies(state, slot);
Fabio Utzigabec0732019-07-31 08:40:22 -03001943 if (rc == 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001944 /* All dependencies've been satisfied, continue with next image. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001945 BOOT_CURR_IMG(state)++;
David Vinczee32483f2019-06-13 10:46:24 +02001946 } else if (rc == BOOT_EBADVERSION) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001947 /* Cannot upgrade due to non-met dependencies, so disable all
1948 * image upgrades.
1949 */
1950 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
1951 BOOT_CURR_IMG(state) = idx;
1952 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1953 }
1954 break;
David Vinczee32483f2019-06-13 10:46:24 +02001955 } else {
1956 /* Other error happened, images are inconsistent */
Fabio Utzig298913b2019-08-28 11:22:45 -03001957 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02001958 }
1959 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001960 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02001961}
1962#endif /* (BOOT_IMAGE_NUMBER > 1) */
1963
Christopher Collins92ea77f2016-12-12 15:59:26 -08001964/**
David Vinczeba3bd602019-06-17 16:01:43 +02001965 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001966 *
David Vinczeba3bd602019-06-17 16:01:43 +02001967 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001968 *
1969 * @return 0 on success; nonzero on failure.
1970 */
1971static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001972boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001973{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001974 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001975#ifndef MCUBOOT_OVERWRITE_ONLY
1976 uint8_t swap_type;
1977#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001978
David Vinczeba3bd602019-06-17 16:01:43 +02001979 /* At this point there are no aborted swaps. */
1980#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001981 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001982#elif defined(MCUBOOT_BOOTSTRAP)
1983 /* Check if the image update was triggered by a bad image in the
1984 * primary slot (the validity of the image in the secondary slot had
1985 * already been checked).
1986 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001987 if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 ||
1988 boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) {
1989 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001990 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001991 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001992 }
1993#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03001994 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001995#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001996 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001997
1998#ifndef MCUBOOT_OVERWRITE_ONLY
1999 /* The following state needs image_ok be explicitly set after the
2000 * swap was finished to avoid a new revert.
2001 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002002 swap_type = BOOT_SWAP_TYPE(state);
2003 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
2004 swap_type == BOOT_SWAP_TYPE_PERM) {
2005 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002006 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002007 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002008 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002009 }
2010
Fabio Utzige575b0b2019-09-11 12:34:23 -03002011 if (BOOT_IS_UPGRADE(swap_type)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002012 rc = boot_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002013 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002014 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07002015 }
David Vinczeba3bd602019-06-17 16:01:43 +02002016 }
2017#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02002018
David Vinczeba3bd602019-06-17 16:01:43 +02002019 return rc;
2020}
2021
2022/**
2023 * Completes a previously aborted image swap.
2024 *
2025 * @param bs The current boot status.
2026 *
2027 * @return 0 on success; nonzero on failure.
2028 */
2029#if !defined(MCUBOOT_OVERWRITE_ONLY)
2030static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03002031boot_complete_partial_swap(struct boot_loader_state *state,
2032 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02002033{
2034 int rc;
2035
2036 /* Determine the type of swap operation being resumed from the
2037 * `swap-type` trailer field.
2038 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002039 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002040 assert(rc == 0);
2041
Fabio Utzig10ee6482019-08-01 12:04:52 -03002042 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02002043
2044 /* The following states need image_ok be explicitly set after the
2045 * swap was finished to avoid a new revert.
2046 */
2047 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
2048 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002049 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002050 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002051 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002052 }
2053 }
2054
Fabio Utzige575b0b2019-09-11 12:34:23 -03002055 if (BOOT_IS_UPGRADE(bs->swap_type)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002056 rc = boot_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002057 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002058 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002059 }
2060 }
2061
Fabio Utzig10ee6482019-08-01 12:04:52 -03002062 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002063 BOOT_LOG_ERR("panic!");
2064 assert(0);
2065
2066 /* Loop forever... */
2067 while (1) {}
2068 }
2069
2070 return rc;
2071}
2072#endif /* !MCUBOOT_OVERWRITE_ONLY */
2073
2074#if (BOOT_IMAGE_NUMBER > 1)
2075/**
2076 * Review the validity of previously determined swap types of other images.
2077 *
2078 * @param aborted_swap The current image upgrade is a
2079 * partial/aborted swap.
2080 */
2081static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03002082boot_review_image_swap_types(struct boot_loader_state *state,
2083 bool aborted_swap)
David Vinczeba3bd602019-06-17 16:01:43 +02002084{
2085 /* In that case if we rebooted in the middle of an image upgrade process, we
2086 * must review the validity of swap types, that were previously determined
2087 * for other images. The image_ok flag had not been set before the reboot
2088 * for any of the updated images (only the copy_done flag) and thus falsely
2089 * the REVERT swap type has been determined for the previous images that had
2090 * been updated before the reboot.
2091 *
2092 * There are two separate scenarios that we have to deal with:
2093 *
2094 * 1. The reboot has happened during swapping an image:
2095 * The current image upgrade has been determined as a
2096 * partial/aborted swap.
2097 * 2. The reboot has happened between two separate image upgrades:
2098 * In this scenario we must check the swap type of the current image.
2099 * In those cases if it is NONE or REVERT we cannot certainly determine
2100 * the fact of a reboot. In a consistent state images must move in the
2101 * same direction or stay in place, e.g. in practice REVERT and TEST
2102 * swap types cannot be present at the same time. If the swap type of
2103 * the current image is either TEST, PERM or FAIL we must review the
2104 * already determined swap types of other images and set each false
2105 * REVERT swap types to NONE (these images had been successfully
2106 * updated before the system rebooted between two separate image
2107 * upgrades).
2108 */
2109
Fabio Utzig10ee6482019-08-01 12:04:52 -03002110 if (BOOT_CURR_IMG(state) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02002111 /* Nothing to do */
2112 return;
2113 }
2114
2115 if (!aborted_swap) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002116 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
2117 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002118 /* Nothing to do */
2119 return;
2120 }
2121 }
2122
Fabio Utzig10ee6482019-08-01 12:04:52 -03002123 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
2124 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
2125 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002126 }
2127 }
2128}
2129#endif
2130
2131/**
2132 * Prepare image to be updated if required.
2133 *
2134 * Prepare image to be updated if required with completing an image swap
2135 * operation if one was aborted and/or determining the type of the
2136 * swap operation. In case of any error set the swap type to NONE.
2137 *
Fabio Utzig10ee6482019-08-01 12:04:52 -03002138 * @param state TODO
David Vinczeba3bd602019-06-17 16:01:43 +02002139 * @param bs Pointer where the read and possibly updated
2140 * boot status can be written to.
2141 */
2142static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03002143boot_prepare_image_for_update(struct boot_loader_state *state,
2144 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02002145{
2146 int rc;
2147
2148 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002149 rc = boot_read_sectors(state);
David Vinczeba3bd602019-06-17 16:01:43 +02002150 if (rc != 0) {
2151 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
2152 " - too small?", BOOT_MAX_IMG_SECTORS);
2153 /* Unable to determine sector layout, continue with next image
2154 * if there is one.
2155 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002156 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002157 return;
2158 }
2159
2160 /* Attempt to read an image header from each slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002161 rc = boot_read_image_headers(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02002162 if (rc != 0) {
2163 /* Continue with next image if there is one. */
Fabio Utzigb0f04732019-07-31 09:49:19 -03002164 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03002165 BOOT_CURR_IMG(state));
2166 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002167 return;
2168 }
2169
2170 /* If the current image's slots aren't compatible, no swap is possible.
2171 * Just boot into primary slot.
2172 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002173 if (boot_slots_compatible(state)) {
2174 rc = boot_read_status(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002175 if (rc != 0) {
2176 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03002177 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002178 /* Continue with next image if there is one. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002179 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002180 return;
2181 }
2182
2183 /* Determine if we rebooted in the middle of an image swap
2184 * operation. If a partial swap was detected, complete it.
2185 */
2186 if (bs->idx != BOOT_STATUS_IDX_0 || bs->state != BOOT_STATUS_STATE_0) {
2187
2188#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03002189 boot_review_image_swap_types(state, true);
David Vinczeba3bd602019-06-17 16:01:43 +02002190#endif
2191
2192#ifdef MCUBOOT_OVERWRITE_ONLY
2193 /* Should never arrive here, overwrite-only mode has
2194 * no swap state.
2195 */
2196 assert(0);
2197#else
2198 /* Determine the type of swap operation being resumed from the
2199 * `swap-type` trailer field.
2200 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002201 rc = boot_complete_partial_swap(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002202 assert(rc == 0);
2203#endif
2204 /* Attempt to read an image header from each slot. Ensure that
2205 * image headers in slots are aligned with headers in boot_data.
2206 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002207 rc = boot_read_image_headers(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02002208 assert(rc == 0);
2209
2210 /* Swap has finished set to NONE */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002211 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002212 } else {
2213 /* There was no partial swap, determine swap type. */
2214 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002215 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
2216 } else if (boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) != 0) {
2217 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
David Vinczeba3bd602019-06-17 16:01:43 +02002218 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002219 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02002220 }
2221
2222#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03002223 boot_review_image_swap_types(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02002224#endif
2225
2226#ifdef MCUBOOT_BOOTSTRAP
Fabio Utzig10ee6482019-08-01 12:04:52 -03002227 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02002228 /* Header checks are done first because they are
2229 * inexpensive. Since overwrite-only copies starting from
2230 * offset 0, if interrupted, it might leave a valid header
2231 * magic, so also run validation on the primary slot to be
2232 * sure it's not OK.
2233 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03002234 if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 ||
2235 boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) {
2236 if (boot_img_hdr(state,
2237 BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC &&
2238 boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02002239 /* Set swap type to REVERT to overwrite the primary
2240 * slot with the image contained in secondary slot
2241 * and to trigger the explicit setting of the
2242 * image_ok flag.
2243 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03002244 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02002245 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02002246 }
2247 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02002248#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002249 }
David Vinczeba3bd602019-06-17 16:01:43 +02002250 } else {
2251 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002252 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002253 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002254}
2255
Christopher Collins92ea77f2016-12-12 15:59:26 -08002256int
Fabio Utzig10ee6482019-08-01 12:04:52 -03002257context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08002258{
Marti Bolivar84898652017-06-13 17:20:22 -04002259 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02002260 struct boot_status bs;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002261 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002262 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03002263 int image_index;
Fabio Utzig298913b2019-08-28 11:22:45 -03002264 bool has_upgrade;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002265
2266 /* The array of slot sectors are defined here (as opposed to file scope) so
2267 * that they don't get allocated for non-boot-loader apps. This is
2268 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002269 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08002270 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002271 TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
2272 TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
2273 TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Christopher Collins92ea77f2016-12-12 15:59:26 -08002274
Fabio Utzig10ee6482019-08-01 12:04:52 -03002275 memset(state, 0, sizeof(struct boot_loader_state));
Fabio Utzig298913b2019-08-28 11:22:45 -03002276 has_upgrade = false;
2277
2278#if (BOOT_IMAGE_NUMBER == 1)
2279 (void)has_upgrade;
2280#endif
Fabio Utzigba829042018-09-18 08:29:34 -03002281
David Vinczeba3bd602019-06-17 16:01:43 +02002282 /* Iterate over all the images. By the end of the loop the swap type has
2283 * to be determined for each image and all aborted swaps have to be
2284 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002285 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002286 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03002287
David Vinczeba3bd602019-06-17 16:01:43 +02002288#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
2289 /* The keys used for encryption may no longer be valid (could belong to
2290 * another images). Therefore, mark them as invalid to force their reload
2291 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03002292 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03002293 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Brown554c52e2017-06-30 16:01:07 -06002294#endif
2295
Fabio Utzig10ee6482019-08-01 12:04:52 -03002296 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03002297
Fabio Utzig10ee6482019-08-01 12:04:52 -03002298 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002299 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03002300 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002301 secondary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03002302 state->scratch.sectors = scratch_sectors;
David Vinczeba3bd602019-06-17 16:01:43 +02002303
2304 /* Open primary and secondary image areas for the duration
2305 * of this call.
2306 */
2307 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03002308 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03002309 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02002310 assert(rc == 0);
2311 }
2312 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03002313 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002314 assert(rc == 0);
2315
2316 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002317 boot_prepare_image_for_update(state, &bs);
Fabio Utzig298913b2019-08-28 11:22:45 -03002318
Fabio Utzige575b0b2019-09-11 12:34:23 -03002319 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
Fabio Utzig298913b2019-08-28 11:22:45 -03002320 has_upgrade = true;
2321 }
David Vinczeba3bd602019-06-17 16:01:43 +02002322 }
2323
David Vinczee32483f2019-06-13 10:46:24 +02002324#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig298913b2019-08-28 11:22:45 -03002325 if (has_upgrade) {
2326 /* Iterate over all the images and verify whether the image dependencies
2327 * are all satisfied and update swap type if necessary.
2328 */
2329 rc = boot_verify_dependencies(state);
2330 if (rc == BOOT_EBADVERSION) {
2331 /*
2332 * It was impossible to upgrade because the expected dependency version
2333 * was not available. Here we already changed the swap_type so that
2334 * instead of asserting the bootloader, we continue and no upgrade is
2335 * performed.
2336 */
2337 rc = 0;
2338 }
2339 }
David Vinczee32483f2019-06-13 10:46:24 +02002340#endif
2341
David Vinczeba3bd602019-06-17 16:01:43 +02002342 /* Iterate over all the images. At this point there are no aborted swaps
2343 * and the swap types are determined for each image. By the end of the loop
2344 * all required update operations will have been finished.
2345 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002346 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002347
2348#if (BOOT_IMAGE_NUMBER > 1)
2349#ifdef MCUBOOT_ENC_IMAGES
2350 /* The keys used for encryption may no longer be valid (could belong to
2351 * another images). Therefore, mark them as invalid to force their reload
2352 * by boot_enc_load().
2353 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03002354 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002355#endif /* MCUBOOT_ENC_IMAGES */
2356
2357 /* Indicate that swap is not aborted */
2358 memset(&bs, 0, sizeof bs);
2359 bs.idx = BOOT_STATUS_IDX_0;
2360 bs.state = BOOT_STATUS_STATE_0;
2361#endif /* (BOOT_IMAGE_NUMBER > 1) */
2362
2363 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002364 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02002365
Fabio Utzig10ee6482019-08-01 12:04:52 -03002366 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002367 case BOOT_SWAP_TYPE_NONE:
2368 break;
2369
2370 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
2371 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
2372 case BOOT_SWAP_TYPE_REVERT:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002373 rc = boot_perform_update(state, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002374 assert(rc == 0);
2375 break;
2376
2377 case BOOT_SWAP_TYPE_FAIL:
2378 /* The image in secondary slot was invalid and is now erased. Ensure
2379 * we don't try to boot into it again on the next reboot. Do this by
2380 * pretending we just reverted back to primary slot.
2381 */
2382#ifndef MCUBOOT_OVERWRITE_ONLY
2383 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002384 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002385 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002386 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002387 }
2388#endif /* !MCUBOOT_OVERWRITE_ONLY */
2389 break;
2390
2391 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002392 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002393 }
2394
Fabio Utzig10ee6482019-08-01 12:04:52 -03002395 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002396 BOOT_LOG_ERR("panic!");
2397 assert(0);
2398
2399 /* Loop forever... */
2400 while (1) {}
2401 }
2402 }
2403
2404 /* Iterate over all the images. At this point all required update operations
2405 * have finished. By the end of the loop each image in the primary slot will
2406 * have been re-validated.
2407 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002408 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2409 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02002410 /* Attempt to read an image header from each slot. Ensure that image
2411 * headers in slots are aligned with headers in boot_data.
2412 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002413 rc = boot_read_image_headers(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02002414 if (rc != 0) {
2415 goto out;
2416 }
2417 /* Since headers were reloaded, it can be assumed we just performed
2418 * a swap or overwrite. Now the header info that should be used to
2419 * provide the data for the bootstrap, which previously was at
2420 * secondary slot, was updated to primary slot.
2421 */
2422 }
2423
2424#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig10ee6482019-08-01 12:04:52 -03002425 rc = boot_validate_slot(state, BOOT_PRIMARY_SLOT, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02002426 if (rc != 0) {
2427 rc = BOOT_EBADIMAGE;
2428 goto out;
2429 }
2430#else
2431 /* Even if we're not re-validating the primary slot, we could be booting
2432 * onto an empty flash chip. At least do a basic sanity check that
2433 * the magic number on the image is OK.
2434 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002435 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002436 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
Fabio Utzig10ee6482019-08-01 12:04:52 -03002437 &boot_img_hdr(state,BOOT_PRIMARY_SLOT)->ih_magic,
2438 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002439 rc = BOOT_EBADIMAGE;
2440 goto out;
2441 }
2442#endif
2443 }
2444
Fabio Utzigb0f04732019-07-31 09:49:19 -03002445#if (BOOT_IMAGE_NUMBER > 1)
David Vinczeba3bd602019-06-17 16:01:43 +02002446 /* Always boot from the primary slot of Image 0. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002447 BOOT_CURR_IMG(state) = 0;
Fabio Utzigb0f04732019-07-31 09:49:19 -03002448#endif
Fabio Utzig298913b2019-08-28 11:22:45 -03002449
Fabio Utzig10ee6482019-08-01 12:04:52 -03002450 rsp->br_flash_dev_id = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)->fa_device_id;
2451 rsp->br_image_off = boot_img_slot_off(state, BOOT_PRIMARY_SLOT);
2452 rsp->br_hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002453
Fabio Utzig298913b2019-08-28 11:22:45 -03002454out:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002455 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2456 flash_area_close(BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002457 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002458 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
David Vinczeba3bd602019-06-17 16:01:43 +02002459 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04002460 }
2461 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002462}
2463
Fabio Utzig10ee6482019-08-01 12:04:52 -03002464/**
2465 * Prepares the booting process. This function moves images around in flash as
2466 * appropriate, and tells you what address to boot from.
2467 *
2468 * @param rsp On success, indicates how booting should occur.
2469 *
2470 * @return 0 on success; nonzero on failure.
2471 */
2472int
2473boot_go(struct boot_rsp *rsp)
2474{
2475 return context_boot_go(&boot_data, rsp);
2476}
2477
Christopher Collins92ea77f2016-12-12 15:59:26 -08002478int
2479split_go(int loader_slot, int split_slot, void **entry)
2480{
Marti Bolivarc50926f2017-06-14 09:35:40 -04002481 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08002482 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002483 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002484 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002485 int rc;
2486
Christopher Collins92ea77f2016-12-12 15:59:26 -08002487 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
2488 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04002489 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002490 }
David Vinczeba3bd602019-06-17 16:01:43 +02002491 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
2492 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002493
2494 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
2495 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07002496 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04002497 assert(rc == 0);
2498 split_flash_id = flash_area_id_from_image_slot(split_slot);
2499 rc = flash_area_open(split_flash_id,
2500 &BOOT_IMG_AREA(&boot_data, split_slot));
2501 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002502
2503 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002504 rc = boot_read_sectors(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002505 if (rc != 0) {
2506 rc = SPLIT_GO_ERR;
2507 goto done;
2508 }
2509
Fabio Utzig10ee6482019-08-01 12:04:52 -03002510 rc = boot_read_image_headers(&boot_data, true);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002511 if (rc != 0) {
2512 goto done;
2513 }
2514
Christopher Collins92ea77f2016-12-12 15:59:26 -08002515 /* Don't check the bootable image flag because we could really call a
2516 * bootable or non-bootable image. Just validate that the image check
2517 * passes which is distinct from the normal check.
2518 */
Marti Bolivarf804f622017-06-12 15:41:48 -04002519 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04002520 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04002521 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04002522 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002523 if (rc != 0) {
2524 rc = SPLIT_GO_NON_MATCHING;
2525 goto done;
2526 }
2527
Marti Bolivarea088872017-06-12 17:10:49 -04002528 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04002529 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08002530 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002531 rc = SPLIT_GO_OK;
2532
2533done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04002534 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
2535 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002536 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002537 return rc;
2538}