blob: fc5d09cc28dce0a99b55371eef5dd42b710a7b38 [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
David Vinczee2453472019-06-17 12:31:59 +020020/*
21 * Modifications are Copyright (c) 2019 Arm Limited.
22 */
23
Christopher Collins92ea77f2016-12-12 15:59:26 -080024/**
25 * This file provides an interface to the boot loader. Functions defined in
26 * this file should only be called while the boot loader is running.
27 */
28
29#include <assert.h>
30#include <stddef.h>
David Brown52eee562017-07-05 11:25:09 -060031#include <stdbool.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080032#include <inttypes.h>
33#include <stdlib.h>
34#include <string.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080035#include <os/os_malloc.h>
36#include "bootutil/bootutil.h"
37#include "bootutil/image.h"
38#include "bootutil_priv.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050039#include "bootutil/bootutil_log.h"
40
Fabio Utzigba829042018-09-18 08:29:34 -030041#ifdef MCUBOOT_ENC_IMAGES
42#include "bootutil/enc_key.h"
43#endif
44
Fabio Utzigba1fbe62017-07-21 14:01:20 -030045#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030046
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010047MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
48
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040049static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080050
Fabio Utzigabec0732019-07-31 08:40:22 -030051#if (BOOT_IMAGE_NUMBER > 1)
52#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
53#else
54#define IMAGES_ITER(x)
55#endif
56
Fabio Utzig10ee6482019-08-01 12:04:52 -030057/*
58 * This macro allows some control on the allocation of local variables.
59 * When running natively on a target, we don't want to allocated huge
60 * variables on the stack, so make them global instead. For the simulator
61 * we want to run as many threads as there are tests, and it's safer
62 * to just make those variables stack allocated.
63 */
64#if !defined(__BOOTSIM__)
65#define TARGET_STATIC static
66#else
67#define TARGET_STATIC
68#endif
69
David Vincze2d736ad2019-02-18 11:50:22 +010070#if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT) && !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -030071/*
72 * FIXME: this might have to be updated for threaded sim
73 */
Fabio Utziga0e1cce2017-11-23 20:04:01 -020074static int boot_status_fails = 0;
75#define BOOT_STATUS_ASSERT(x) \
76 do { \
Johann Fischered8461b2018-02-15 16:50:31 +010077 if (!(x)) { \
Fabio Utziga0e1cce2017-11-23 20:04:01 -020078 boot_status_fails++; \
79 } \
80 } while (0)
81#else
Fabio Utzig57c40f72017-12-12 21:48:30 -020082#define BOOT_STATUS_ASSERT(x) ASSERT(x)
Fabio Utziga0e1cce2017-11-23 20:04:01 -020083#endif
84
Christopher Collins92ea77f2016-12-12 15:59:26 -080085struct boot_status_table {
David Vincze2d736ad2019-02-18 11:50:22 +010086 uint8_t bst_magic_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -080087 uint8_t bst_magic_scratch;
David Vincze2d736ad2019-02-18 11:50:22 +010088 uint8_t bst_copy_done_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -080089 uint8_t bst_status_source;
90};
91
92/**
93 * This set of tables maps swap state contents to boot status location.
94 * When searching for a match, these tables must be iterated in order.
95 */
96static const struct boot_status_table boot_status_tables[] = {
97 {
David Vincze2d736ad2019-02-18 11:50:22 +010098 /* | primary slot | scratch |
99 * ----------+--------------+--------------|
100 * magic | Good | Any |
101 * copy-done | Set | N/A |
102 * ----------+--------------+--------------'
103 * source: none |
104 * ----------------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -0800105 */
David Vincze2d736ad2019-02-18 11:50:22 +0100106 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
Christopher Collinsa1c12042019-05-23 14:00:28 -0700107 .bst_magic_scratch = BOOT_MAGIC_NOTGOOD,
David Vincze2d736ad2019-02-18 11:50:22 +0100108 .bst_copy_done_primary_slot = BOOT_FLAG_SET,
109 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800110 },
111
112 {
David Vincze2d736ad2019-02-18 11:50:22 +0100113 /* | primary slot | scratch |
114 * ----------+--------------+--------------|
115 * magic | Good | Any |
116 * copy-done | Unset | N/A |
117 * ----------+--------------+--------------'
118 * source: primary slot |
119 * ----------------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -0800120 */
David Vincze2d736ad2019-02-18 11:50:22 +0100121 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
Christopher Collinsa1c12042019-05-23 14:00:28 -0700122 .bst_magic_scratch = BOOT_MAGIC_NOTGOOD,
David Vincze2d736ad2019-02-18 11:50:22 +0100123 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
124 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800125 },
126
127 {
David Vincze2d736ad2019-02-18 11:50:22 +0100128 /* | primary slot | scratch |
129 * ----------+--------------+--------------|
130 * magic | Any | Good |
131 * copy-done | Any | N/A |
132 * ----------+--------------+--------------'
133 * source: scratch |
134 * ----------------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -0800135 */
David Vincze2d736ad2019-02-18 11:50:22 +0100136 .bst_magic_primary_slot = BOOT_MAGIC_ANY,
137 .bst_magic_scratch = BOOT_MAGIC_GOOD,
138 .bst_copy_done_primary_slot = BOOT_FLAG_ANY,
139 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800140 },
Christopher Collins92ea77f2016-12-12 15:59:26 -0800141 {
David Vincze2d736ad2019-02-18 11:50:22 +0100142 /* | primary slot | scratch |
143 * ----------+--------------+--------------|
144 * magic | Unset | Any |
145 * copy-done | Unset | N/A |
146 * ----------+--------------+--------------|
147 * source: varies |
148 * ----------------------------------------+--------------------------+
Christopher Collins92ea77f2016-12-12 15:59:26 -0800149 * This represents one of two cases: |
150 * o No swaps ever (no status to read, so no harm in checking). |
David Vincze2d736ad2019-02-18 11:50:22 +0100151 * o Mid-revert; status in primary slot. |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800152 * -------------------------------------------------------------------'
153 */
David Vincze2d736ad2019-02-18 11:50:22 +0100154 .bst_magic_primary_slot = BOOT_MAGIC_UNSET,
155 .bst_magic_scratch = BOOT_MAGIC_ANY,
156 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
157 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800158 },
159};
160
161#define BOOT_STATUS_TABLES_COUNT \
162 (sizeof boot_status_tables / sizeof boot_status_tables[0])
163
Marti Bolivarfd20c762017-02-07 16:52:50 -0500164#define BOOT_LOG_SWAP_STATE(area, state) \
Christopher Collinsa1c12042019-05-23 14:00:28 -0700165 BOOT_LOG_INF("%s: magic=%s, swap_type=0x%x, copy_done=0x%x, " \
166 "image_ok=0x%x", \
Marti Bolivarfd20c762017-02-07 16:52:50 -0500167 (area), \
168 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
169 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
170 "bad"), \
Christopher Collinsa1c12042019-05-23 14:00:28 -0700171 (state)->swap_type, \
Marti Bolivarfd20c762017-02-07 16:52:50 -0500172 (state)->copy_done, \
173 (state)->image_ok)
174
Christopher Collins92ea77f2016-12-12 15:59:26 -0800175/**
David Vincze2d736ad2019-02-18 11:50:22 +0100176 * Determines where in flash the most recent boot status is stored. The boot
Christopher Collins92ea77f2016-12-12 15:59:26 -0800177 * status is necessary for completing a swap that was interrupted by a boot
178 * loader reset.
179 *
David Vincze2d736ad2019-02-18 11:50:22 +0100180 * @return A BOOT_STATUS_SOURCE_[...] code indicating where status should
181 * be read from.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800182 */
183static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300184boot_status_source(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800185{
186 const struct boot_status_table *table;
187 struct boot_swap_state state_scratch;
David Vincze2d736ad2019-02-18 11:50:22 +0100188 struct boot_swap_state state_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800189 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200190 size_t i;
Marti Bolivarfd20c762017-02-07 16:52:50 -0500191 uint8_t source;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300192 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800193
Fabio Utzigb0f04732019-07-31 09:49:19 -0300194#if (BOOT_IMAGE_NUMBER == 1)
195 (void)state;
196#endif
197
Fabio Utzig10ee6482019-08-01 12:04:52 -0300198 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300199 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
200 &state_primary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800201 assert(rc == 0);
202
Fabio Utzig2473ac02017-05-02 12:45:02 -0300203 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800204 assert(rc == 0);
205
David Vincze2d736ad2019-02-18 11:50:22 +0100206 BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot);
Marti Bolivarfd20c762017-02-07 16:52:50 -0500207 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
208
Christopher Collins92ea77f2016-12-12 15:59:26 -0800209 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300210 table = &boot_status_tables[i];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800211
Christopher Collinsa1c12042019-05-23 14:00:28 -0700212 if (boot_magic_compatible_check(table->bst_magic_primary_slot,
213 state_primary_slot.magic) &&
214 boot_magic_compatible_check(table->bst_magic_scratch,
215 state_scratch.magic) &&
David Vincze2d736ad2019-02-18 11:50:22 +0100216 (table->bst_copy_done_primary_slot == BOOT_FLAG_ANY ||
217 table->bst_copy_done_primary_slot == state_primary_slot.copy_done))
218 {
Marti Bolivarfd20c762017-02-07 16:52:50 -0500219 source = table->bst_status_source;
David Vinczeba3bd602019-06-17 16:01:43 +0200220
221#if (BOOT_IMAGE_NUMBER > 1)
222 /* In case of multi-image boot it can happen that if boot status
223 * info is found on scratch area then it does not belong to the
224 * currently examined image.
225 */
226 if (source == BOOT_STATUS_SOURCE_SCRATCH &&
Fabio Utzig10ee6482019-08-01 12:04:52 -0300227 state_scratch.image_num != BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +0200228 source = BOOT_STATUS_SOURCE_NONE;
229 }
230#endif
231
Marti Bolivarfd20c762017-02-07 16:52:50 -0500232 BOOT_LOG_INF("Boot source: %s",
233 source == BOOT_STATUS_SOURCE_NONE ? "none" :
234 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
David Vincze2d736ad2019-02-18 11:50:22 +0100235 source == BOOT_STATUS_SOURCE_PRIMARY_SLOT ?
236 "primary slot" : "BUG; can't happen");
Marti Bolivarfd20c762017-02-07 16:52:50 -0500237 return source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800238 }
239 }
240
Marti Bolivarfd20c762017-02-07 16:52:50 -0500241 BOOT_LOG_INF("Boot source: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800242 return BOOT_STATUS_SOURCE_NONE;
243}
244
David Brownf5b33d82017-09-01 10:58:27 -0600245/*
246 * Compute the total size of the given image. Includes the size of
247 * the TLVs.
248 */
Fabio Utzig13d9e352017-10-05 20:32:31 -0300249#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600250static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300251boot_read_image_size(struct boot_loader_state *state, int slot,
252 struct image_header *hdr, uint32_t *size)
David Brownf5b33d82017-09-01 10:58:27 -0600253{
254 const struct flash_area *fap;
255 struct image_tlv_info info;
256 int area_id;
257 int rc;
258
Fabio Utzig10ee6482019-08-01 12:04:52 -0300259#if (BOOT_IMAGE_NUMBER == 1)
260 (void)state;
261#endif
262
263 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
David Brownf5b33d82017-09-01 10:58:27 -0600264 rc = flash_area_open(area_id, &fap);
265 if (rc != 0) {
266 rc = BOOT_EFLASH;
267 goto done;
268 }
269
Fabio Utzigc9621352019-08-08 12:15:51 -0300270 rc = flash_area_read(fap, BOOT_TLV_OFF(hdr), &info, sizeof(info));
David Brownf5b33d82017-09-01 10:58:27 -0600271 if (rc != 0) {
272 rc = BOOT_EFLASH;
273 goto done;
274 }
275 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
276 rc = BOOT_EBADIMAGE;
277 goto done;
278 }
Fabio Utzigc9621352019-08-08 12:15:51 -0300279 *size = BOOT_TLV_OFF(hdr) + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600280 rc = 0;
281
282done:
283 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300284 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600285}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300286#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600287
Fabio Utzigc08ed212017-06-20 19:28:36 -0300288static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300289boot_read_image_header(struct boot_loader_state *state, int slot,
290 struct image_header *out_hdr)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800291{
292 const struct flash_area *fap;
293 int area_id;
294 int rc;
295
Fabio Utzig10ee6482019-08-01 12:04:52 -0300296#if (BOOT_IMAGE_NUMBER == 1)
297 (void)state;
298#endif
299
300 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800301 rc = flash_area_open(area_id, &fap);
302 if (rc != 0) {
303 rc = BOOT_EFLASH;
304 goto done;
305 }
306
307 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
308 if (rc != 0) {
309 rc = BOOT_EFLASH;
310 goto done;
311 }
312
313 rc = 0;
314
315done:
316 flash_area_close(fap);
317 return rc;
318}
319
320static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300321boot_read_image_headers(struct boot_loader_state *state, bool require_all)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800322{
323 int rc;
324 int i;
325
326 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300327 rc = boot_read_image_header(state, i, boot_img_hdr(state, i));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800328 if (rc != 0) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200329 /* If `require_all` is set, fail on any single fail, otherwise
330 * if at least the first slot's header was read successfully,
331 * then the boot loader can attempt a boot.
332 *
333 * Failure to read any headers is a fatal error.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800334 */
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200335 if (i > 0 && !require_all) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800336 return 0;
337 } else {
338 return rc;
339 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800340 }
341 }
342
343 return 0;
344}
345
346static uint8_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300347boot_write_sz(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800348{
349 uint8_t elem_sz;
350 uint8_t align;
351
352 /* Figure out what size to write update status update as. The size depends
353 * on what the minimum write size is for scratch area, active image slot.
354 * We need to use the bigger of those 2 values.
355 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300356 elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
357 align = flash_area_align(BOOT_SCRATCH_AREA(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800358 if (align > elem_sz) {
359 elem_sz = align;
360 }
361
362 return elem_sz;
363}
364
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200365/*
366 * Slots are compatible when all sectors that store upto to size of the image
367 * round up to sector size, in both slot's are able to fit in the scratch
368 * area, and have sizes that are a multiple of each other (powers of two
369 * presumably!).
370 */
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800371static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300372boot_slots_compatible(struct boot_loader_state *state)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800373{
David Vincze2d736ad2019-02-18 11:50:22 +0100374 size_t num_sectors_primary;
375 size_t num_sectors_secondary;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200376 size_t sz0, sz1;
David Vincze2d736ad2019-02-18 11:50:22 +0100377 size_t primary_slot_sz, secondary_slot_sz;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200378 size_t scratch_sz;
379 size_t i, j;
380 int8_t smaller;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800381
Fabio Utzig10ee6482019-08-01 12:04:52 -0300382 num_sectors_primary = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
383 num_sectors_secondary = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT);
David Vincze2d736ad2019-02-18 11:50:22 +0100384 if ((num_sectors_primary > BOOT_MAX_IMG_SECTORS) ||
385 (num_sectors_secondary > BOOT_MAX_IMG_SECTORS)) {
Fabio Utziga1fae672018-03-30 10:52:38 -0300386 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800387 return 0;
388 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300389
Fabio Utzig10ee6482019-08-01 12:04:52 -0300390 scratch_sz = boot_scratch_area_size(state);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200391
392 /*
393 * The following loop scans all sectors in a linear fashion, assuring that
394 * for each possible sector in each slot, it is able to fit in the other
395 * slot's sector or sectors. Slot's should be compatible as long as any
396 * number of a slot's sectors are able to fit into another, which only
397 * excludes cases where sector sizes are not a multiple of each other.
398 */
David Vincze2d736ad2019-02-18 11:50:22 +0100399 i = sz0 = primary_slot_sz = 0;
400 j = sz1 = secondary_slot_sz = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200401 smaller = 0;
David Vincze2d736ad2019-02-18 11:50:22 +0100402 while (i < num_sectors_primary || j < num_sectors_secondary) {
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200403 if (sz0 == sz1) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300404 sz0 += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
405 sz1 += boot_img_sector_size(state, BOOT_SECONDARY_SLOT, j);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200406 i++;
407 j++;
408 } else if (sz0 < sz1) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300409 sz0 += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
David Vincze2d736ad2019-02-18 11:50:22 +0100410 /* Guarantee that multiple sectors of the secondary slot
411 * fit into the primary slot.
412 */
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200413 if (smaller == 2) {
414 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors");
415 return 0;
416 }
417 smaller = 1;
418 i++;
419 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300420 sz1 += boot_img_sector_size(state, BOOT_SECONDARY_SLOT, j);
David Vincze2d736ad2019-02-18 11:50:22 +0100421 /* Guarantee that multiple sectors of the primary slot
422 * fit into the secondary slot.
423 */
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200424 if (smaller == 1) {
425 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors");
426 return 0;
427 }
428 smaller = 2;
429 j++;
430 }
431 if (sz0 == sz1) {
David Vincze2d736ad2019-02-18 11:50:22 +0100432 primary_slot_sz += sz0;
433 secondary_slot_sz += sz1;
434 /* Scratch has to fit each swap operation to the size of the larger
435 * sector among the primary slot and the secondary slot.
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200436 */
437 if (sz0 > scratch_sz || sz1 > scratch_sz) {
438 BOOT_LOG_WRN("Cannot upgrade: not all sectors fit inside scratch");
439 return 0;
440 }
441 smaller = sz0 = sz1 = 0;
442 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300443 }
444
David Vincze2d736ad2019-02-18 11:50:22 +0100445 if ((i != num_sectors_primary) ||
446 (j != num_sectors_secondary) ||
447 (primary_slot_sz != secondary_slot_sz)) {
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200448 BOOT_LOG_WRN("Cannot upgrade: slots are not compatible");
449 return 0;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800450 }
451
452 return 1;
453}
454
Fabio Utzig10ee6482019-08-01 12:04:52 -0300455#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
456static int
457boot_initialize_area(struct boot_loader_state *state, int flash_area)
458{
459 int num_sectors = BOOT_MAX_IMG_SECTORS;
460 int rc;
461
462 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
463 rc = flash_area_to_sectors(flash_area, &num_sectors,
464 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors);
465 BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors = (size_t)num_sectors;
466
467 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
468 rc = flash_area_to_sectors(flash_area, &num_sectors,
469 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors);
470 BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors = (size_t)num_sectors;
471
472 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
473 rc = flash_area_to_sectors(flash_area, &num_sectors,
474 state->scratch.sectors);
475 state->scratch.num_sectors = (size_t)num_sectors;
476 } else {
477 return BOOT_EFLASH;
478 }
479
480 return rc;
481}
482#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
483static int
484boot_initialize_area(struct boot_loader_state *state, int flash_area)
485{
486 uint32_t num_sectors;
487 struct flash_sector *out_sectors;
488 size_t *out_num_sectors;
489 int rc;
490
491 num_sectors = BOOT_MAX_IMG_SECTORS;
492
493 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
494 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
495 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
496 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
497 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
498 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
499 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
500 out_sectors = state->scratch.sectors;
501 out_num_sectors = &state->scratch.num_sectors;
502 } else {
503 return BOOT_EFLASH;
504 }
505
506 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
507 if (rc != 0) {
508 return rc;
509 }
510 *out_num_sectors = num_sectors;
511 return 0;
512}
513#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
514
Christopher Collins92ea77f2016-12-12 15:59:26 -0800515/**
516 * Determines the sector layout of both image slots and the scratch area.
517 * This information is necessary for calculating the number of bytes to erase
518 * and copy during an image swap. The information collected during this
Fabio Utzig10ee6482019-08-01 12:04:52 -0300519 * function is used to populate the state.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800520 */
521static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300522boot_read_sectors(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800523{
Fabio Utzigb0f04732019-07-31 09:49:19 -0300524 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800525 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800526
Fabio Utzig10ee6482019-08-01 12:04:52 -0300527 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300528
529 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800530 if (rc != 0) {
531 return BOOT_EFLASH;
532 }
533
Fabio Utzig10ee6482019-08-01 12:04:52 -0300534 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800535 if (rc != 0) {
536 return BOOT_EFLASH;
537 }
538
Fabio Utzig10ee6482019-08-01 12:04:52 -0300539 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200540 if (rc != 0) {
541 return BOOT_EFLASH;
542 }
543
Fabio Utzig10ee6482019-08-01 12:04:52 -0300544 BOOT_WRITE_SZ(state) = boot_write_sz(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800545
546 return 0;
547}
548
549static uint32_t
550boot_status_internal_off(int idx, int state, int elem_sz)
551{
552 int idx_sz;
553
554 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
555
Fabio Utzig39000012018-07-30 12:40:20 -0300556 return (idx - BOOT_STATUS_IDX_0) * idx_sz +
557 (state - BOOT_STATUS_STATE_0) * elem_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800558}
559
560/**
561 * Reads the status of a partially-completed swap, if any. This is necessary
562 * to recover in case the boot lodaer was reset in the middle of a swap
563 * operation.
564 */
565static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300566boot_read_status_bytes(const struct flash_area *fap,
567 struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800568{
569 uint32_t off;
570 uint8_t status;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300571 int max_entries;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800572 int found;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200573 int found_idx;
574 int invalid;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800575 int rc;
576 int i;
577
578 off = boot_status_off(fap);
Fabio Utzig10ee6482019-08-01 12:04:52 -0300579 max_entries = boot_status_entries(BOOT_CURR_IMG(state), fap);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300580
Christopher Collins92ea77f2016-12-12 15:59:26 -0800581 found = 0;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200582 found_idx = 0;
583 invalid = 0;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300584 for (i = 0; i < max_entries; i++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300585 rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(state),
Fabio Utzig178be542018-09-19 08:12:56 -0300586 &status, 1);
587 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800588 return BOOT_EFLASH;
589 }
590
Fabio Utzig178be542018-09-19 08:12:56 -0300591 if (rc == 1) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200592 if (found && !found_idx) {
593 found_idx = i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800594 }
595 } else if (!found) {
596 found = 1;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200597 } else if (found_idx) {
598 invalid = 1;
599 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800600 }
601 }
602
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200603 if (invalid) {
604 /* This means there was an error writing status on the last
605 * swap. Tell user and move on to validation!
606 */
607 BOOT_LOG_ERR("Detected inconsistent status!");
608
David Vincze2d736ad2019-02-18 11:50:22 +0100609#if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
610 /* With validation of the primary slot disabled, there is no way
611 * to be sure the swapped primary slot is OK, so abort!
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200612 */
613 assert(0);
614#endif
615 }
616
Christopher Collins92ea77f2016-12-12 15:59:26 -0800617 if (found) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200618 if (!found_idx) {
619 found_idx = i;
620 }
621 found_idx--;
Fabio Utzig39000012018-07-30 12:40:20 -0300622 bs->idx = (found_idx / BOOT_STATUS_STATE_COUNT) + 1;
623 bs->state = (found_idx % BOOT_STATUS_STATE_COUNT) + 1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800624 }
625
626 return 0;
627}
628
629/**
630 * Reads the boot status from the flash. The boot status contains
631 * the current state of an interrupted image copy operation. If the boot
632 * status is not present, or it indicates that previous copy finished,
633 * there is no operation in progress.
634 */
635static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300636boot_read_status(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800637{
638 const struct flash_area *fap;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700639 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200640 uint8_t swap_info;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800641 int status_loc;
642 int area_id;
643 int rc;
644
645 memset(bs, 0, sizeof *bs);
Fabio Utzig39000012018-07-30 12:40:20 -0300646 bs->idx = BOOT_STATUS_IDX_0;
647 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700648 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800649
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700650#ifdef MCUBOOT_OVERWRITE_ONLY
651 /* Overwrite-only doesn't make use of the swap status area. */
652 return 0;
653#endif
654
Fabio Utzig10ee6482019-08-01 12:04:52 -0300655 status_loc = boot_status_source(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800656 switch (status_loc) {
657 case BOOT_STATUS_SOURCE_NONE:
658 return 0;
659
660 case BOOT_STATUS_SOURCE_SCRATCH:
661 area_id = FLASH_AREA_IMAGE_SCRATCH;
662 break;
663
David Vincze2d736ad2019-02-18 11:50:22 +0100664 case BOOT_STATUS_SOURCE_PRIMARY_SLOT:
Fabio Utzig10ee6482019-08-01 12:04:52 -0300665 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800666 break;
667
668 default:
669 assert(0);
670 return BOOT_EBADARGS;
671 }
672
673 rc = flash_area_open(area_id, &fap);
674 if (rc != 0) {
675 return BOOT_EFLASH;
676 }
677
Fabio Utzig10ee6482019-08-01 12:04:52 -0300678 rc = boot_read_status_bytes(fap, state, bs);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700679 if (rc == 0) {
David Vinczee2453472019-06-17 12:31:59 +0200680 off = boot_swap_info_off(fap);
681 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700682 if (rc == 1) {
David Vinczee2453472019-06-17 12:31:59 +0200683 BOOT_SET_SWAP_INFO(swap_info, 0, BOOT_SWAP_TYPE_NONE);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700684 rc = 0;
685 }
David Vinczee2453472019-06-17 12:31:59 +0200686
687 /* Extract the swap type info */
688 bs->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700689 }
Fabio Utzig46490722017-09-04 15:34:32 -0300690
691 flash_area_close(fap);
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700692
Fabio Utzig46490722017-09-04 15:34:32 -0300693 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800694}
695
696/**
697 * Writes the supplied boot status to the flash file system. The boot status
698 * contains the current state of an in-progress image copy operation.
699 *
700 * @param bs The boot status to write.
701 *
702 * @return 0 on success; nonzero on failure.
703 */
704int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300705boot_write_status(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800706{
707 const struct flash_area *fap;
708 uint32_t off;
709 int area_id;
710 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300711 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700712 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300713 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800714
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300715 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100716 * the trailer. Since in the last step the primary slot is erased, the
717 * first two status writes go to the scratch which will be copied to
718 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300719 */
720
Fabio Utzig2473ac02017-05-02 12:45:02 -0300721 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800722 /* Write to scratch. */
723 area_id = FLASH_AREA_IMAGE_SCRATCH;
724 } else {
David Vincze2d736ad2019-02-18 11:50:22 +0100725 /* Write to the primary slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300726 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800727 }
728
729 rc = flash_area_open(area_id, &fap);
730 if (rc != 0) {
731 rc = BOOT_EFLASH;
732 goto done;
733 }
734
735 off = boot_status_off(fap) +
Marti Bolivare10a7392017-06-14 16:20:07 -0400736 boot_status_internal_off(bs->idx, bs->state,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300737 BOOT_WRITE_SZ(state));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200738 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300739 erased_val = flash_area_erased_val(fap);
740 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700741 buf[0] = bs->state;
742
743 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800744 if (rc != 0) {
745 rc = BOOT_EFLASH;
746 goto done;
747 }
748
749 rc = 0;
750
751done:
752 flash_area_close(fap);
753 return rc;
754}
755
756/*
757 * Validate image hash/signature in a slot.
758 */
759static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300760boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
761 const struct flash_area *fap, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800762{
Fabio Utzig10ee6482019-08-01 12:04:52 -0300763 TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigb0f04732019-07-31 09:49:19 -0300764 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300765 int rc;
766
Fabio Utzig10ee6482019-08-01 12:04:52 -0300767#if (BOOT_IMAGE_NUMBER == 1)
768 (void)state;
769#endif
770
771 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300772
Fabio Utzigba829042018-09-18 08:29:34 -0300773#ifndef MCUBOOT_ENC_IMAGES
774 (void)bs;
775 (void)rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -0300776 if (bootutil_img_validate(NULL, image_index, hdr, fap, tmpbuf,
777 BOOT_TMPBUF_SZ, NULL, 0, NULL)) {
778 return BOOT_EBADIMAGE;
779 }
Fabio Utzigba829042018-09-18 08:29:34 -0300780#else
Fabio Utzigb0f04732019-07-31 09:49:19 -0300781 if ((fap->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index))
782 && IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300783 rc = boot_enc_load(state->enc, image_index, hdr, fap, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -0300784 if (rc < 0) {
785 return BOOT_EBADIMAGE;
786 }
Fabio Utzig10ee6482019-08-01 12:04:52 -0300787 if (rc == 0 && boot_enc_set_key(state->enc, 1, bs->enckey[1])) {
Fabio Utzigba829042018-09-18 08:29:34 -0300788 return BOOT_EBADIMAGE;
789 }
790 }
Fabio Utzig10ee6482019-08-01 12:04:52 -0300791 if (bootutil_img_validate(state->enc, image_index, hdr, fap, tmpbuf,
792 BOOT_TMPBUF_SZ, NULL, 0, NULL)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800793 return BOOT_EBADIMAGE;
794 }
Fabio Utzig10ee6482019-08-01 12:04:52 -0300795#endif
796
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
Christopher Collins92ea77f2016-12-12 15:59:26 -0800871static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300872boot_validate_slot(struct boot_loader_state *state, int slot,
873 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800874{
875 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400876 struct image_header *hdr;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300877 int area_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800878 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300879
Fabio Utzig10ee6482019-08-01 12:04:52 -0300880 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300881 rc = flash_area_open(area_id, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800882 if (rc != 0) {
883 return BOOT_EFLASH;
884 }
885
Fabio Utzig10ee6482019-08-01 12:04:52 -0300886 hdr = boot_img_hdr(state, slot);
887 if (boot_check_header_erased(state, slot) == 0 ||
888 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100889 /* No bootable image in slot; continue booting from the primary slot. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200890 rc = -1;
891 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300892 }
893
Fabio Utzig10ee6482019-08-01 12:04:52 -0300894 if (hdr->ih_magic != IMAGE_MAGIC || boot_image_check(state, hdr, fap, bs)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100895 if (slot != BOOT_PRIMARY_SLOT) {
David Brownb38e0442017-02-24 13:57:12 -0700896 flash_area_erase(fap, 0, fap->fa_size);
David Vincze2d736ad2019-02-18 11:50:22 +0100897 /* Image in the secondary slot is invalid. Erase the image and
898 * continue booting from the primary slot.
David Brownb38e0442017-02-24 13:57:12 -0700899 */
900 }
David Vincze2d736ad2019-02-18 11:50:22 +0100901 BOOT_LOG_ERR("Image in the %s slot is not valid!",
902 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
Fabio Utzig338a19f2018-12-03 08:37:08 -0200903 rc = -1;
904 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800905 }
906
David Vincze2d736ad2019-02-18 11:50:22 +0100907 /* Image in the secondary slot is valid. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200908 rc = 0;
909
910out:
911 flash_area_close(fap);
912 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800913}
914
915/**
916 * Determines which swap operation to perform, if any. If it is determined
David Vincze2d736ad2019-02-18 11:50:22 +0100917 * that a swap operation is required, the image in the secondary slot is checked
918 * for validity. If the image in the secondary slot is invalid, it is erased,
919 * and a swap type of "none" is indicated.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800920 *
921 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
922 */
923static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300924boot_validated_swap_type(struct boot_loader_state *state,
925 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800926{
927 int swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800928
Fabio Utzigb0f04732019-07-31 09:49:19 -0300929#if (BOOT_IMAGE_NUMBER == 1)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800930 swap_type = boot_swap_type();
Fabio Utzigb0f04732019-07-31 09:49:19 -0300931#else
932 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
933#endif
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300934 switch (swap_type) {
935 case BOOT_SWAP_TYPE_TEST:
936 case BOOT_SWAP_TYPE_PERM:
937 case BOOT_SWAP_TYPE_REVERT:
David Vincze2d736ad2019-02-18 11:50:22 +0100938 /* Boot loader wants to switch to the secondary slot.
939 * Ensure image is valid.
940 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300941 if (boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) != 0) {
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300942 swap_type = BOOT_SWAP_TYPE_FAIL;
943 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800944 }
945
946 return swap_type;
947}
948
949/**
950 * Calculates the number of sectors the scratch area can contain. A "last"
951 * source sector is specified because images are copied backwards in flash
952 * (final index to index number 0).
953 *
954 * @param last_sector_idx The index of the last source sector
955 * (inclusive).
956 * @param out_first_sector_idx The index of the first source sector
957 * (inclusive) gets written here.
958 *
959 * @return The number of bytes comprised by the
960 * [first-sector, last-sector] range.
961 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300962#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800963static uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300964boot_copy_sz(struct boot_loader_state *state, int last_sector_idx,
965 int *out_first_sector_idx)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800966{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400967 size_t scratch_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800968 uint32_t new_sz;
969 uint32_t sz;
970 int i;
971
972 sz = 0;
973
Fabio Utzig10ee6482019-08-01 12:04:52 -0300974 scratch_sz = boot_scratch_area_size(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800975 for (i = last_sector_idx; i >= 0; i--) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300976 new_sz = sz + boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200977 /*
David Vincze2d736ad2019-02-18 11:50:22 +0100978 * The secondary slot is not being checked here, because
979 * `boot_slots_compatible` already provides assurance that the copy size
980 * will be compatible with the primary slot and scratch.
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200981 */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400982 if (new_sz > scratch_sz) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800983 break;
984 }
985 sz = new_sz;
986 }
987
988 /* i currently refers to a sector that doesn't fit or it is -1 because all
989 * sectors have been processed. In both cases, exclude sector i.
990 */
991 *out_first_sector_idx = i + 1;
992 return sz;
993}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300994#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800995
996/**
997 * Erases a region of flash.
998 *
Fabio Utzigba829042018-09-18 08:29:34 -0300999 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001000 * @param off The offset within the flash area to start the
1001 * erase.
1002 * @param sz The number of bytes to erase.
1003 *
1004 * @return 0 on success; nonzero on failure.
1005 */
Fabio Utzigba829042018-09-18 08:29:34 -03001006static inline int
1007boot_erase_sector(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001008{
Fabio Utzigba829042018-09-18 08:29:34 -03001009 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001010}
1011
1012/**
1013 * Copies the contents of one flash region to another. You must erase the
1014 * destination region prior to calling this function.
1015 *
1016 * @param flash_area_id_src The ID of the source flash area.
1017 * @param flash_area_id_dst The ID of the destination flash area.
1018 * @param off_src The offset within the source flash area to
1019 * copy from.
1020 * @param off_dst The offset within the destination flash area to
1021 * copy to.
1022 * @param sz The number of bytes to copy.
1023 *
1024 * @return 0 on success; nonzero on failure.
1025 */
1026static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001027boot_copy_sector(struct boot_loader_state *state,
1028 const struct flash_area *fap_src,
Fabio Utzigba829042018-09-18 08:29:34 -03001029 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -08001030 uint32_t off_src, uint32_t off_dst, uint32_t sz)
1031{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001032 uint32_t bytes_copied;
1033 int chunk_sz;
1034 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -03001035#ifdef MCUBOOT_ENC_IMAGES
1036 uint32_t off;
1037 size_t blk_off;
1038 struct image_header *hdr;
1039 uint16_t idx;
1040 uint32_t blk_sz;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001041 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -03001042#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001043
Fabio Utzig10ee6482019-08-01 12:04:52 -03001044 TARGET_STATIC uint8_t buf[1024];
1045
1046#if !defined(MCUBOOT_ENC_IMAGES)
1047 (void)state;
1048#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001049
Christopher Collins92ea77f2016-12-12 15:59:26 -08001050 bytes_copied = 0;
1051 while (bytes_copied < sz) {
1052 if (sz - bytes_copied > sizeof buf) {
1053 chunk_sz = sizeof buf;
1054 } else {
1055 chunk_sz = sz - bytes_copied;
1056 }
1057
1058 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
1059 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001060 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001061 }
1062
Fabio Utzigba829042018-09-18 08:29:34 -03001063#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001064 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001065 if (fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) ||
1066 fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001067 /* assume the secondary slot as src, needs decryption */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001068 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001069 off = off_src;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001070 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001071 /* might need encryption (metadata from the primary slot) */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001072 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001073 off = off_dst;
1074 }
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001075 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001076 blk_sz = chunk_sz;
1077 idx = 0;
1078 if (off + bytes_copied < hdr->ih_hdr_size) {
1079 /* do not decrypt header */
1080 blk_off = 0;
1081 blk_sz = chunk_sz - hdr->ih_hdr_size;
1082 idx = hdr->ih_hdr_size;
1083 } else {
1084 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
1085 }
Fabio Utzigc9621352019-08-08 12:15:51 -03001086 if (off + bytes_copied + chunk_sz > BOOT_TLV_OFF(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001087 /* do not decrypt TLVs */
Fabio Utzigc9621352019-08-08 12:15:51 -03001088 if (off + bytes_copied >= BOOT_TLV_OFF(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001089 blk_sz = 0;
1090 } else {
Fabio Utzigc9621352019-08-08 12:15:51 -03001091 blk_sz = BOOT_TLV_OFF(hdr) - (off + bytes_copied);
Fabio Utzigba829042018-09-18 08:29:34 -03001092 }
1093 }
Fabio Utzig10ee6482019-08-01 12:04:52 -03001094 boot_encrypt(state->enc, image_index, fap_src,
Fabio Utzigb0f04732019-07-31 09:49:19 -03001095 (off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
1096 blk_off, &buf[idx]);
Fabio Utzigba829042018-09-18 08:29:34 -03001097 }
1098 }
1099#endif
1100
Christopher Collins92ea77f2016-12-12 15:59:26 -08001101 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
1102 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001103 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001104 }
1105
1106 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -03001107
1108 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -08001109 }
1110
Fabio Utzigba829042018-09-18 08:29:34 -03001111 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001112}
1113
David Brown6b1b3b92017-09-19 08:59:10 -06001114#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -03001115static inline int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001116boot_status_init(const struct boot_loader_state *state,
1117 const struct flash_area *fap,
1118 const struct boot_status *bs)
Fabio Utzig2473ac02017-05-02 12:45:02 -03001119{
Fabio Utzig2473ac02017-05-02 12:45:02 -03001120 struct boot_swap_state swap_state;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001121 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001122 int rc;
1123
Fabio Utzig10ee6482019-08-01 12:04:52 -03001124#if (BOOT_IMAGE_NUMBER == 1)
1125 (void)state;
1126#endif
1127
1128 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001129
Christopher Collins2c88e692019-05-22 15:10:14 -07001130 BOOT_LOG_DBG("initializing status; fa_id=%d", fap->fa_id);
1131
Fabio Utzigb0f04732019-07-31 09:49:19 -03001132 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
1133 &swap_state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001134 assert(rc == 0);
1135
Christopher Collinsa1c12042019-05-23 14:00:28 -07001136 if (bs->swap_type != BOOT_SWAP_TYPE_NONE) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03001137 rc = boot_write_swap_info(fap, bs->swap_type, image_index);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001138 assert(rc == 0);
1139 }
1140
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001141 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001142 rc = boot_write_image_ok(fap);
1143 assert(rc == 0);
1144 }
1145
Fabio Utzig46490722017-09-04 15:34:32 -03001146 rc = boot_write_swap_size(fap, bs->swap_size);
1147 assert(rc == 0);
1148
Fabio Utzigba829042018-09-18 08:29:34 -03001149#ifdef MCUBOOT_ENC_IMAGES
1150 rc = boot_write_enc_key(fap, 0, bs->enckey[0]);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001151 assert(rc == 0);
1152
Fabio Utzigba829042018-09-18 08:29:34 -03001153 rc = boot_write_enc_key(fap, 1, bs->enckey[1]);
1154 assert(rc == 0);
1155#endif
1156
1157 rc = boot_write_magic(fap);
1158 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001159
1160 return 0;
1161}
Christopher Collinsa1c12042019-05-23 14:00:28 -07001162
David Brown6b1b3b92017-09-19 08:59:10 -06001163#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001164
Fabio Utzig358c9352017-07-25 22:10:45 -03001165#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -03001166static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001167boot_erase_trailer_sectors(const struct boot_loader_state *state,
1168 const struct flash_area *fap)
Fabio Utzig2473ac02017-05-02 12:45:02 -03001169{
1170 uint8_t slot;
Fabio Utziged0ca432019-01-23 14:50:11 -02001171 uint32_t sector;
1172 uint32_t trailer_sz;
1173 uint32_t total_sz;
1174 uint32_t off;
1175 uint32_t sz;
David Vinczeb75c12a2019-03-22 14:58:33 +01001176 int fa_id_primary;
1177 int fa_id_secondary;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001178 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001179 int rc;
1180
Christopher Collins2c88e692019-05-22 15:10:14 -07001181 BOOT_LOG_DBG("erasing trailer; fa_id=%d", fap->fa_id);
1182
Fabio Utzig10ee6482019-08-01 12:04:52 -03001183 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001184 fa_id_primary = flash_area_id_from_multi_image_slot(image_index,
1185 BOOT_PRIMARY_SLOT);
1186 fa_id_secondary = flash_area_id_from_multi_image_slot(image_index,
1187 BOOT_SECONDARY_SLOT);
David Vinczeb75c12a2019-03-22 14:58:33 +01001188
1189 if (fap->fa_id == fa_id_primary) {
David Vincze2d736ad2019-02-18 11:50:22 +01001190 slot = BOOT_PRIMARY_SLOT;
David Vinczeb75c12a2019-03-22 14:58:33 +01001191 } else if (fap->fa_id == fa_id_secondary) {
David Vincze2d736ad2019-02-18 11:50:22 +01001192 slot = BOOT_SECONDARY_SLOT;
David Vinczeb75c12a2019-03-22 14:58:33 +01001193 } else {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001194 return BOOT_EFLASH;
1195 }
1196
Fabio Utziged0ca432019-01-23 14:50:11 -02001197 /* delete starting from last sector and moving to beginning */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001198 sector = boot_img_num_sectors(state, slot) - 1;
1199 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
Fabio Utziged0ca432019-01-23 14:50:11 -02001200 total_sz = 0;
1201 do {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001202 sz = boot_img_sector_size(state, slot, sector);
1203 off = boot_img_sector_off(state, slot, sector);
Fabio Utziged0ca432019-01-23 14:50:11 -02001204 rc = boot_erase_sector(fap, off, sz);
1205 assert(rc == 0);
1206
1207 sector--;
1208 total_sz += sz;
1209 } while (total_sz < trailer_sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001210
1211 return rc;
1212}
Fabio Utzig358c9352017-07-25 22:10:45 -03001213#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001214
Christopher Collins92ea77f2016-12-12 15:59:26 -08001215/**
1216 * Swaps the contents of two flash regions within the two image slots.
1217 *
1218 * @param idx The index of the first sector in the range of
1219 * sectors being swapped.
1220 * @param sz The number of bytes to swap.
1221 * @param bs The current boot status. This struct gets
1222 * updated according to the outcome.
1223 *
1224 * @return 0 on success; nonzero on failure.
1225 */
Fabio Utzig3488eef2017-06-12 10:25:43 -03001226#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -08001227static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001228boot_swap_sectors(int idx, uint32_t sz, struct boot_loader_state *state,
1229 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001230{
David Vincze2d736ad2019-02-18 11:50:22 +01001231 const struct flash_area *fap_primary_slot;
1232 const struct flash_area *fap_secondary_slot;
Fabio Utzigba829042018-09-18 08:29:34 -03001233 const struct flash_area *fap_scratch;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001234 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001235 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001236 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001237 uint32_t scratch_trailer_off;
1238 struct boot_swap_state swap_state;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001239 size_t last_sector;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001240 bool erase_scratch;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001241 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001242 int rc;
1243
1244 /* Calculate offset from start of image area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001245 img_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001246
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001247 copy_sz = sz;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001248 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
Fabio Utzig9678c972017-05-23 11:28:56 -04001249
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001250 /* sz in this function is always sized on a multiple of the sector size.
1251 * The check against the start offset of the last sector
Fabio Utzig9678c972017-05-23 11:28:56 -04001252 * is to determine if we're swapping the last sector. The last sector
1253 * needs special handling because it's where the trailer lives. If we're
1254 * copying it, we need to use scratch to write the trailer temporarily.
1255 *
1256 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
1257 * controls if special handling is needed (swapping last sector).
1258 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001259 last_sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
David Vinczeba3bd602019-06-17 16:01:43 +02001260 if ((img_off + sz) >
Fabio Utzig10ee6482019-08-01 12:04:52 -03001261 boot_img_sector_off(state, BOOT_PRIMARY_SLOT, last_sector)) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001262 copy_sz -= trailer_sz;
1263 }
1264
Fabio Utzig39000012018-07-30 12:40:20 -03001265 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001266
Fabio Utzig10ee6482019-08-01 12:04:52 -03001267 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001268
1269 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1270 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001271 assert (rc == 0);
1272
Fabio Utzigb0f04732019-07-31 09:49:19 -03001273 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1274 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001275 assert (rc == 0);
1276
1277 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch);
1278 assert (rc == 0);
1279
Fabio Utzig39000012018-07-30 12:40:20 -03001280 if (bs->state == BOOT_STATUS_STATE_0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001281 BOOT_LOG_DBG("erasing scratch area");
Christopher Collinsa1c12042019-05-23 14:00:28 -07001282 rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size);
Christopher Collins4772ac42017-02-27 20:08:01 -08001283 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001284
Fabio Utzig39000012018-07-30 12:40:20 -03001285 if (bs->idx == BOOT_STATUS_IDX_0) {
Christopher Collinsa1c12042019-05-23 14:00:28 -07001286 /* Write a trailer to the scratch area, even if we don't need the
1287 * scratch area for status. We need a temporary place to store the
1288 * `swap-type` while we erase the primary trailer.
1289 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001290 rc = boot_status_init(state, fap_scratch, bs);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001291 assert(rc == 0);
1292
1293 if (!bs->use_scratch) {
1294 /* Prepare the primary status area... here it is known that the
1295 * last sector is not being used by the image data so it's safe
1296 * to erase.
Fabio Utzig2473ac02017-05-02 12:45:02 -03001297 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001298 rc = boot_erase_trailer_sectors(state, fap_primary_slot);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001299 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001300
Fabio Utzig10ee6482019-08-01 12:04:52 -03001301 rc = boot_status_init(state, fap_primary_slot, bs);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001302 assert(rc == 0);
1303
1304 /* Erase the temporary trailer from the scratch area. */
1305 rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size);
1306 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001307 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001308 }
1309
Fabio Utzig10ee6482019-08-01 12:04:52 -03001310 rc = boot_copy_sector(state, fap_secondary_slot, fap_scratch,
Christopher Collinsa1c12042019-05-23 14:00:28 -07001311 img_off, 0, copy_sz);
1312 assert(rc == 0);
1313
Fabio Utzig39000012018-07-30 12:40:20 -03001314 bs->state = BOOT_STATUS_STATE_1;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001315 rc = boot_write_status(state, bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001316 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001317 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001318
Fabio Utzig39000012018-07-30 12:40:20 -03001319 if (bs->state == BOOT_STATUS_STATE_1) {
David Vincze2d736ad2019-02-18 11:50:22 +01001320 rc = boot_erase_sector(fap_secondary_slot, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001321 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001322
Fabio Utzig10ee6482019-08-01 12:04:52 -03001323 rc = boot_copy_sector(state, fap_primary_slot, fap_secondary_slot,
David Vincze2d736ad2019-02-18 11:50:22 +01001324 img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001325 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001326
Fabio Utzig39000012018-07-30 12:40:20 -03001327 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001328 /* If not all sectors of the slot are being swapped,
David Vincze2d736ad2019-02-18 11:50:22 +01001329 * guarantee here that only the primary slot will have the state.
Fabio Utzig2473ac02017-05-02 12:45:02 -03001330 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001331 rc = boot_erase_trailer_sectors(state, fap_secondary_slot);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001332 assert(rc == 0);
1333 }
1334
Fabio Utzig39000012018-07-30 12:40:20 -03001335 bs->state = BOOT_STATUS_STATE_2;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001336 rc = boot_write_status(state, bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001337 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001338 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001339
Fabio Utzig39000012018-07-30 12:40:20 -03001340 if (bs->state == BOOT_STATUS_STATE_2) {
David Vincze2d736ad2019-02-18 11:50:22 +01001341 rc = boot_erase_sector(fap_primary_slot, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001342 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001343
Christopher Collinsa1c12042019-05-23 14:00:28 -07001344 /* NOTE: If this is the final sector, we exclude the image trailer from
1345 * this copy (copy_sz was truncated earlier).
1346 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001347 rc = boot_copy_sector(state, fap_scratch, fap_primary_slot,
David Vincze2d736ad2019-02-18 11:50:22 +01001348 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001349 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001350
Fabio Utzig94d998c2017-05-22 11:02:41 -04001351 if (bs->use_scratch) {
Fabio Utzigba829042018-09-18 08:29:34 -03001352 scratch_trailer_off = boot_status_off(fap_scratch);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001353
1354 /* copy current status that is being maintained in scratch */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001355 rc = boot_copy_sector(state, fap_scratch, fap_primary_slot,
David Vincze2d736ad2019-02-18 11:50:22 +01001356 scratch_trailer_off, img_off + copy_sz,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001357 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(state));
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001358 BOOT_STATUS_ASSERT(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001359
Fabio Utzig2473ac02017-05-02 12:45:02 -03001360 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1361 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001362 assert(rc == 0);
1363
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001364 if (swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze2d736ad2019-02-18 11:50:22 +01001365 rc = boot_write_image_ok(fap_primary_slot);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001366 assert(rc == 0);
1367 }
1368
Christopher Collinsa1c12042019-05-23 14:00:28 -07001369 if (swap_state.swap_type != BOOT_SWAP_TYPE_NONE) {
David Vinczee2453472019-06-17 12:31:59 +02001370 rc = boot_write_swap_info(fap_primary_slot,
Fabio Utzigb0f04732019-07-31 09:49:19 -03001371 swap_state.swap_type, image_index);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001372 assert(rc == 0);
1373 }
1374
David Vincze2d736ad2019-02-18 11:50:22 +01001375 rc = boot_write_swap_size(fap_primary_slot, bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001376 assert(rc == 0);
1377
Fabio Utzigba829042018-09-18 08:29:34 -03001378#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +01001379 rc = boot_write_enc_key(fap_primary_slot, 0, bs->enckey[0]);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001380 assert(rc == 0);
1381
David Vincze2d736ad2019-02-18 11:50:22 +01001382 rc = boot_write_enc_key(fap_primary_slot, 1, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001383 assert(rc == 0);
1384#endif
David Vincze2d736ad2019-02-18 11:50:22 +01001385 rc = boot_write_magic(fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001386 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001387 }
1388
Christopher Collinsa1c12042019-05-23 14:00:28 -07001389 /* If we wrote a trailer to the scratch area, erase it after we persist
1390 * a trailer to the primary slot. We do this to prevent mcuboot from
1391 * reading a stale status from the scratch area in case of immediate
1392 * reset.
1393 */
1394 erase_scratch = bs->use_scratch;
1395 bs->use_scratch = 0;
1396
Christopher Collins92ea77f2016-12-12 15:59:26 -08001397 bs->idx++;
Fabio Utzig39000012018-07-30 12:40:20 -03001398 bs->state = BOOT_STATUS_STATE_0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001399 rc = boot_write_status(state, bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001400 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001401
1402 if (erase_scratch) {
1403 rc = boot_erase_sector(fap_scratch, 0, sz);
1404 assert(rc == 0);
1405 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001406 }
Fabio Utzigba829042018-09-18 08:29:34 -03001407
David Vincze2d736ad2019-02-18 11:50:22 +01001408 flash_area_close(fap_primary_slot);
1409 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001410 flash_area_close(fap_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001411}
Fabio Utzig3488eef2017-06-12 10:25:43 -03001412#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001413
1414/**
David Vincze2d736ad2019-02-18 11:50:22 +01001415 * Overwrite primary slot with the image contained in the secondary slot.
1416 * If a prior copy operation was interrupted by a system reset, this function
1417 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001418 *
1419 * @param bs The current boot status. This function reads
1420 * this struct to determine if it is resuming
1421 * an interrupted swap operation. This
1422 * function writes the updated status to this
1423 * function on return.
1424 *
1425 * @return 0 on success; nonzero on failure.
1426 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001427#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001428static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001429boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
David Brown17609d82017-05-05 09:41:34 -06001430{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001431 size_t sect_count;
1432 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001433 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001434 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001435 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001436 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +01001437 const struct flash_area *fap_primary_slot;
1438 const struct flash_area *fap_secondary_slot;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001439 uint8_t image_index;
David Vincze2d736ad2019-02-18 11:50:22 +01001440
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001441 (void)bs;
1442
Fabio Utzig13d9e352017-10-05 20:32:31 -03001443#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1444 uint32_t src_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001445 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT,
1446 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
David Vincze2d736ad2019-02-18 11:50:22 +01001447 &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001448 assert(rc == 0);
1449#endif
David Brown17609d82017-05-05 09:41:34 -06001450
David Vincze2d736ad2019-02-18 11:50:22 +01001451 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1452 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -06001453
Fabio Utzigb0f04732019-07-31 09:49:19 -03001454 image_index = BOOT_CURR_IMG(state);
1455
1456 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1457 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001458 assert (rc == 0);
1459
Fabio Utzigb0f04732019-07-31 09:49:19 -03001460 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1461 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001462 assert (rc == 0);
1463
Fabio Utzig10ee6482019-08-01 12:04:52 -03001464 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001465 for (sect = 0, size = 0; sect < sect_count; sect++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001466 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
David Vincze2d736ad2019-02-18 11:50:22 +01001467 rc = boot_erase_sector(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001468 assert(rc == 0);
1469
1470 size += this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001471
1472#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1473 if (size >= src_size) {
1474 break;
1475 }
1476#endif
David Brown17609d82017-05-05 09:41:34 -06001477 }
1478
Fabio Utzigba829042018-09-18 08:29:34 -03001479#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001480 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
1481 rc = boot_enc_load(state->enc, image_index,
1482 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
Fabio Utzigb0f04732019-07-31 09:49:19 -03001483 fap_secondary_slot, bs->enckey[1]);
David Vincze2d736ad2019-02-18 11:50:22 +01001484
Fabio Utzigba829042018-09-18 08:29:34 -03001485 if (rc < 0) {
1486 return BOOT_EBADIMAGE;
1487 }
Fabio Utzig10ee6482019-08-01 12:04:52 -03001488 if (rc == 0 && boot_enc_set_key(state->enc, 1, bs->enckey[1])) {
Fabio Utzigba829042018-09-18 08:29:34 -03001489 return BOOT_EBADIMAGE;
1490 }
1491 }
1492#endif
1493
David Vincze2d736ad2019-02-18 11:50:22 +01001494 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1495 size);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001496 rc = boot_copy_sector(state, fap_secondary_slot, fap_primary_slot, 0, 0, size);
David Brown17609d82017-05-05 09:41:34 -06001497
Fabio Utzig13d9e352017-10-05 20:32:31 -03001498 /*
1499 * Erases header and trailer. The trailer is erased because when a new
1500 * image is written without a trailer as is the case when using newt, the
1501 * trailer that was left might trigger a new upgrade.
1502 */
Christopher Collins2c88e692019-05-22 15:10:14 -07001503 BOOT_LOG_DBG("erasing secondary header");
David Vincze2d736ad2019-02-18 11:50:22 +01001504 rc = boot_erase_sector(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001505 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
1506 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001507 assert(rc == 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001508 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -07001509 BOOT_LOG_DBG("erasing secondary trailer");
David Vincze2d736ad2019-02-18 11:50:22 +01001510 rc = boot_erase_sector(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001511 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
1512 last_sector),
1513 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
1514 last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001515 assert(rc == 0);
1516
David Vincze2d736ad2019-02-18 11:50:22 +01001517 flash_area_close(fap_primary_slot);
1518 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001519
David Vincze2d736ad2019-02-18 11:50:22 +01001520 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001521
1522 return 0;
1523}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001524#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001525
Christopher Collinsa1c12042019-05-23 14:00:28 -07001526#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -03001527/**
1528 * Swaps the two images in flash. If a prior copy operation was interrupted
1529 * by a system reset, this function completes that operation.
1530 *
1531 * @param bs The current boot status. This function reads
1532 * this struct to determine if it is resuming
1533 * an interrupted swap operation. This
1534 * function writes the updated status to this
1535 * function on return.
1536 *
1537 * @return 0 on success; nonzero on failure.
1538 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001539static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001540boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001541{
1542 uint32_t sz;
1543 int first_sector_idx;
1544 int last_sector_idx;
David Vincze2d736ad2019-02-18 11:50:22 +01001545 int last_idx_secondary_slot;
Fabio Utzigcd5774b2017-11-29 10:18:26 -02001546 uint32_t swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001547 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001548#ifdef MCUBOOT_ENC_IMAGES
1549 const struct flash_area *fap;
1550 uint8_t slot;
1551 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001552#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001553 uint32_t size;
1554 uint32_t copy_size;
David Vincze2d736ad2019-02-18 11:50:22 +01001555 uint32_t primary_slot_size;
1556 uint32_t secondary_slot_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001557 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001558 int rc;
1559
1560 /* FIXME: just do this if asked by user? */
1561
1562 size = copy_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001563 image_index = BOOT_CURR_IMG(state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001564
Fabio Utzig39000012018-07-30 12:40:20 -03001565 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Fabio Utzig46490722017-09-04 15:34:32 -03001566 /*
1567 * No swap ever happened, so need to find the largest image which
1568 * will be used to determine the amount of sectors to swap.
1569 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001570 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001571 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001572 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, hdr, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001573 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001574 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001575
Fabio Utzigba829042018-09-18 08:29:34 -03001576#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001577 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001578 fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
1579 rc = boot_enc_load(state->enc, image_index, hdr, fap, bs->enckey[0]);
Fabio Utzigba829042018-09-18 08:29:34 -03001580 assert(rc >= 0);
1581
1582 if (rc == 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001583 rc = boot_enc_set_key(state->enc, 0, bs->enckey[0]);
Fabio Utzigba829042018-09-18 08:29:34 -03001584 assert(rc == 0);
1585 } else {
1586 rc = 0;
1587 }
1588 } else {
1589 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1590 }
1591#endif
1592
Fabio Utzig10ee6482019-08-01 12:04:52 -03001593 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001594 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001595 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, hdr, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001596 assert(rc == 0);
1597 }
1598
Fabio Utzigba829042018-09-18 08:29:34 -03001599#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001600 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001601 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001602 fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
1603 rc = boot_enc_load(state->enc, image_index, hdr, fap, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001604 assert(rc >= 0);
1605
1606 if (rc == 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001607 rc = boot_enc_set_key(state->enc, 1, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001608 assert(rc == 0);
1609 } else {
1610 rc = 0;
1611 }
1612 } else {
1613 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1614 }
1615#endif
1616
Fabio Utzig46490722017-09-04 15:34:32 -03001617 if (size > copy_size) {
1618 copy_size = size;
1619 }
1620
1621 bs->swap_size = copy_size;
1622 } else {
1623 /*
1624 * If a swap was under way, the swap_size should already be present
1625 * in the trailer...
1626 */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001627 rc = boot_read_swap_size(image_index, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001628 assert(rc == 0);
1629
1630 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001631
1632#ifdef MCUBOOT_ENC_IMAGES
1633 for (slot = 0; slot <= 1; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03001634 rc = boot_read_enc_key(image_index, slot, bs->enckey[slot]);
Fabio Utzigba829042018-09-18 08:29:34 -03001635 assert(rc == 0);
1636
1637 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001638 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001639 break;
1640 }
1641 }
1642
1643 if (i != BOOT_ENC_KEY_SIZE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001644 boot_enc_set_key(state->enc, slot, bs->enckey[slot]);
Fabio Utzigba829042018-09-18 08:29:34 -03001645 }
1646 }
1647#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001648 }
1649
David Vincze2d736ad2019-02-18 11:50:22 +01001650 primary_slot_size = 0;
1651 secondary_slot_size = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001652 last_sector_idx = 0;
David Vincze2d736ad2019-02-18 11:50:22 +01001653 last_idx_secondary_slot = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001654
1655 /*
1656 * Knowing the size of the largest image between both slots, here we
David Vincze2d736ad2019-02-18 11:50:22 +01001657 * find what is the last sector in the primary slot that needs swapping.
1658 * Since we already know that both slots are compatible, the secondary
1659 * slot's last sector is not really required after this check is finished.
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001660 */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001661 while (1) {
David Vincze2d736ad2019-02-18 11:50:22 +01001662 if ((primary_slot_size < copy_size) ||
1663 (primary_slot_size < secondary_slot_size)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001664 primary_slot_size += boot_img_sector_size(state,
David Vincze2d736ad2019-02-18 11:50:22 +01001665 BOOT_PRIMARY_SLOT,
1666 last_sector_idx);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001667 }
David Vincze2d736ad2019-02-18 11:50:22 +01001668 if ((secondary_slot_size < copy_size) ||
1669 (secondary_slot_size < primary_slot_size)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001670 secondary_slot_size += boot_img_sector_size(state,
David Vincze2d736ad2019-02-18 11:50:22 +01001671 BOOT_SECONDARY_SLOT,
1672 last_idx_secondary_slot);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001673 }
David Vincze2d736ad2019-02-18 11:50:22 +01001674 if (primary_slot_size >= copy_size &&
1675 secondary_slot_size >= copy_size &&
1676 primary_slot_size == secondary_slot_size) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001677 break;
1678 }
1679 last_sector_idx++;
David Vincze2d736ad2019-02-18 11:50:22 +01001680 last_idx_secondary_slot++;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001681 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001682
1683 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001684 while (last_sector_idx >= 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001685 sz = boot_copy_sz(state, last_sector_idx, &first_sector_idx);
Fabio Utzig39000012018-07-30 12:40:20 -03001686 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001687 boot_swap_sectors(first_sector_idx, sz, state, bs);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001688 }
1689
1690 last_sector_idx = first_sector_idx - 1;
1691 swap_idx++;
1692 }
1693
David Vincze2d736ad2019-02-18 11:50:22 +01001694#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001695 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001696 BOOT_LOG_WRN("%d status write fails performing the swap",
1697 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001698 }
1699#endif
1700
Christopher Collins92ea77f2016-12-12 15:59:26 -08001701 return 0;
1702}
David Brown17609d82017-05-05 09:41:34 -06001703#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001704
1705/**
David Vincze2d736ad2019-02-18 11:50:22 +01001706 * Marks the image in the primary slot as fully copied.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001707 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001708#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001709static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001710boot_set_copy_done(uint8_t image_index)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001711{
1712 const struct flash_area *fap;
1713 int rc;
1714
Fabio Utzigb0f04732019-07-31 09:49:19 -03001715 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1716 &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001717 if (rc != 0) {
1718 return BOOT_EFLASH;
1719 }
1720
1721 rc = boot_write_copy_done(fap);
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001722 flash_area_close(fap);
1723 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001724}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001725#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001726
1727/**
David Vincze2d736ad2019-02-18 11:50:22 +01001728 * Marks a reverted image in the primary slot as confirmed. This is necessary to
1729 * ensure the status bytes from the image revert operation don't get processed
1730 * on a subsequent boot.
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001731 *
1732 * NOTE: image_ok is tested before writing because if there's a valid permanent
David Vincze2d736ad2019-02-18 11:50:22 +01001733 * image installed on the primary slot and the new image to be upgrade to has a
1734 * bad sig, image_ok would be overwritten.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001735 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001736#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001737static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001738boot_set_image_ok(uint8_t image_index)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001739{
1740 const struct flash_area *fap;
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001741 struct boot_swap_state state;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001742 int rc;
1743
Fabio Utzigb0f04732019-07-31 09:49:19 -03001744 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1745 &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001746 if (rc != 0) {
1747 return BOOT_EFLASH;
1748 }
1749
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001750 rc = boot_read_swap_state(fap, &state);
1751 if (rc != 0) {
1752 rc = BOOT_EFLASH;
1753 goto out;
1754 }
1755
1756 if (state.image_ok == BOOT_FLAG_UNSET) {
1757 rc = boot_write_image_ok(fap);
1758 }
1759
1760out:
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001761 flash_area_close(fap);
1762 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001763}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001764#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001765
David Vinczee32483f2019-06-13 10:46:24 +02001766#if (BOOT_IMAGE_NUMBER > 1)
1767/**
1768 * Check the image dependency whether it is satisfied and modify
1769 * the swap type if necessary.
1770 *
1771 * @param dep Image dependency which has to be verified.
1772 *
1773 * @return 0 on success; nonzero on failure.
1774 */
1775static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001776boot_verify_single_dependency(struct boot_loader_state *state,
1777 struct image_dependency *dep)
David Vinczee32483f2019-06-13 10:46:24 +02001778{
1779 struct image_version *dep_version;
1780 size_t dep_slot;
1781 int rc;
1782
1783 /* Determine the source of the image which is the subject of
1784 * the dependency and get it's version. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001785 dep_slot = (state->swap_type[dep->image_id] != BOOT_SWAP_TYPE_NONE) ?
David Vinczee32483f2019-06-13 10:46:24 +02001786 BOOT_SECONDARY_SLOT : BOOT_PRIMARY_SLOT;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001787 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +02001788
1789 rc = boot_is_version_sufficient(&dep->image_min_version, dep_version);
1790 if (rc != 0) {
1791 /* Dependency not satisfied.
1792 * Modify the swap type to decrease the version number of the image
1793 * (which will be located in the primary slot after the boot process),
1794 * consequently the number of unsatisfied dependencies will be
1795 * decreased or remain the same.
1796 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001797 switch (BOOT_SWAP_TYPE(state)) {
David Vinczee32483f2019-06-13 10:46:24 +02001798 case BOOT_SWAP_TYPE_TEST:
1799 case BOOT_SWAP_TYPE_PERM:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001800 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczee32483f2019-06-13 10:46:24 +02001801 break;
1802 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001803 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczee32483f2019-06-13 10:46:24 +02001804 break;
1805 default:
1806 break;
1807 }
1808 }
1809
1810 return rc;
1811}
1812
1813/**
1814 * Read all dependency TLVs of an image from the flash and verify
1815 * one after another to see if they are all satisfied.
1816 *
1817 * @param slot Image slot number.
1818 *
1819 * @return 0 on success; nonzero on failure.
1820 */
1821static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001822boot_verify_all_dependency(struct boot_loader_state *state, uint32_t slot)
David Vinczee32483f2019-06-13 10:46:24 +02001823{
1824 const struct flash_area *fap;
1825 struct image_header *hdr;
1826 struct image_tlv_info info;
1827 struct image_tlv tlv;
1828 struct image_dependency dep;
1829 uint32_t off;
1830 uint32_t end;
1831 bool dep_tlvs_found = false;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001832 int area_id;
David Vinczee32483f2019-06-13 10:46:24 +02001833 int rc;
1834
Fabio Utzig10ee6482019-08-01 12:04:52 -03001835 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001836 rc = flash_area_open(area_id, &fap);
David Vinczee32483f2019-06-13 10:46:24 +02001837 if (rc != 0) {
1838 rc = BOOT_EFLASH;
1839 goto done;
1840 }
1841
Fabio Utzig10ee6482019-08-01 12:04:52 -03001842 hdr = boot_img_hdr(state, slot);
Fabio Utzigc9621352019-08-08 12:15:51 -03001843 off = BOOT_TLV_OFF(hdr);
David Vinczee32483f2019-06-13 10:46:24 +02001844
1845 /* The TLV area always starts with an image_tlv_info structure. */
1846 rc = flash_area_read(fap, off, &info, sizeof(info));
1847 if (rc != 0) {
1848 rc = BOOT_EFLASH;
1849 goto done;
1850 }
1851
1852 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
1853 rc = BOOT_EBADIMAGE;
1854 goto done;
1855 }
1856 end = off + info.it_tlv_tot;
1857 off += sizeof(info);
1858
1859 /* Traverse through all of the TLVs to find the dependency TLVs. */
1860 for (; off < end; off += sizeof(tlv) + tlv.it_len) {
1861 rc = flash_area_read(fap, off, &tlv, sizeof(tlv));
1862 if (rc != 0) {
1863 rc = BOOT_EFLASH;
1864 goto done;
1865 }
1866
1867 if (tlv.it_type == IMAGE_TLV_DEPENDENCY) {
1868 if (!dep_tlvs_found) {
1869 dep_tlvs_found = true;
1870 }
1871
1872 if (tlv.it_len != sizeof(dep)) {
1873 rc = BOOT_EBADIMAGE;
1874 goto done;
1875 }
1876
1877 rc = flash_area_read(fap, off + sizeof(tlv), &dep, tlv.it_len);
1878 if (rc != 0) {
1879 rc = BOOT_EFLASH;
1880 goto done;
1881 }
1882
1883 /* Verify dependency and modify the swap type if not satisfied. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001884 rc = boot_verify_single_dependency(state, &dep);
David Vinczee32483f2019-06-13 10:46:24 +02001885 if (rc != 0) {
1886 /* Dependency not satisfied. */
1887 goto done;
1888 }
1889
1890 /* Dependency satisfied, no action needed.
1891 * Continue with the next TLV entry.
1892 */
1893 } else if (dep_tlvs_found) {
1894 /* The dependency TLVs are contiguous in the TLV area. If a
1895 * dependency had already been found and the last read TLV
1896 * has a different type then there are no more dependency TLVs.
1897 * The search can be finished.
1898 */
1899 break;
1900 }
1901 }
1902
1903done:
1904 flash_area_close(fap);
1905 return rc;
1906}
1907
1908/**
1909 * Verify whether the image dependencies in the TLV area are
1910 * all satisfied and modify the swap type if necessary.
1911 *
1912 * @return 0 if all dependencies are satisfied,
1913 * nonzero otherwise.
1914 */
1915static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001916boot_verify_single_image_dependency(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001917{
1918 size_t slot;
1919
1920 /* Determine the source of the dependency TLVs. Those dependencies have to
1921 * be checked which belong to the image that will be located in the primary
1922 * slot after the firmware update process.
1923 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001924 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1925 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
David Vinczee32483f2019-06-13 10:46:24 +02001926 slot = BOOT_SECONDARY_SLOT;
1927 } else {
1928 slot = BOOT_PRIMARY_SLOT;
1929 }
1930
Fabio Utzig10ee6482019-08-01 12:04:52 -03001931 return boot_verify_all_dependency(state, slot);
David Vinczee32483f2019-06-13 10:46:24 +02001932}
1933
1934/**
1935 * Iterate over all the images and verify whether the image dependencies in the
1936 * TLV area are all satisfied and update the related swap type if necessary.
1937 */
1938static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001939boot_verify_all_image_dependency(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001940{
David Vinczee32483f2019-06-13 10:46:24 +02001941 int rc;
1942
Fabio Utzig10ee6482019-08-01 12:04:52 -03001943 BOOT_CURR_IMG(state) = 0;
1944 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
1945 rc = boot_verify_single_image_dependency(state);
Fabio Utzigabec0732019-07-31 08:40:22 -03001946 if (rc == 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001947 /* All dependencies've been satisfied, continue with next image. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001948 BOOT_CURR_IMG(state)++;
David Vinczee32483f2019-06-13 10:46:24 +02001949 } else if (rc == BOOT_EBADVERSION) {
1950 /* Dependency check needs to be restarted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001951 BOOT_CURR_IMG(state) = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001952 } else {
1953 /* Other error happened, images are inconsistent */
1954 return;
1955 }
1956 }
1957}
1958#endif /* (BOOT_IMAGE_NUMBER > 1) */
1959
Christopher Collins92ea77f2016-12-12 15:59:26 -08001960/**
David Vinczeba3bd602019-06-17 16:01:43 +02001961 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001962 *
David Vinczeba3bd602019-06-17 16:01:43 +02001963 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001964 *
1965 * @return 0 on success; nonzero on failure.
1966 */
1967static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001968boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001969{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001970 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001971#ifndef MCUBOOT_OVERWRITE_ONLY
1972 uint8_t swap_type;
1973#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001974
David Vinczeba3bd602019-06-17 16:01:43 +02001975 /* At this point there are no aborted swaps. */
1976#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001977 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001978#elif defined(MCUBOOT_BOOTSTRAP)
1979 /* Check if the image update was triggered by a bad image in the
1980 * primary slot (the validity of the image in the secondary slot had
1981 * already been checked).
1982 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001983 if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 ||
1984 boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) {
1985 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001986 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001987 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001988 }
1989#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03001990 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001991#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001992 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001993
1994#ifndef MCUBOOT_OVERWRITE_ONLY
1995 /* The following state needs image_ok be explicitly set after the
1996 * swap was finished to avoid a new revert.
1997 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001998 swap_type = BOOT_SWAP_TYPE(state);
1999 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
2000 swap_type == BOOT_SWAP_TYPE_PERM) {
2001 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002002 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002003 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002004 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002005 }
2006
Fabio Utzig10ee6482019-08-01 12:04:52 -03002007 if (swap_type == BOOT_SWAP_TYPE_TEST ||
2008 swap_type == BOOT_SWAP_TYPE_PERM ||
2009 swap_type == BOOT_SWAP_TYPE_REVERT) {
2010 rc = boot_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002011 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002012 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07002013 }
David Vinczeba3bd602019-06-17 16:01:43 +02002014 }
2015#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02002016
David Vinczeba3bd602019-06-17 16:01:43 +02002017 return rc;
2018}
2019
2020/**
2021 * Completes a previously aborted image swap.
2022 *
2023 * @param bs The current boot status.
2024 *
2025 * @return 0 on success; nonzero on failure.
2026 */
2027#if !defined(MCUBOOT_OVERWRITE_ONLY)
2028static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03002029boot_complete_partial_swap(struct boot_loader_state *state,
2030 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02002031{
2032 int rc;
2033
2034 /* Determine the type of swap operation being resumed from the
2035 * `swap-type` trailer field.
2036 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002037 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002038 assert(rc == 0);
2039
Fabio Utzig10ee6482019-08-01 12:04:52 -03002040 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02002041
2042 /* The following states need image_ok be explicitly set after the
2043 * swap was finished to avoid a new revert.
2044 */
2045 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
2046 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002047 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002048 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002049 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002050 }
2051 }
2052
2053 if (bs->swap_type == BOOT_SWAP_TYPE_TEST ||
2054 bs->swap_type == BOOT_SWAP_TYPE_PERM ||
2055 bs->swap_type == BOOT_SWAP_TYPE_REVERT) {
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 Utzig10ee6482019-08-01 12:04:52 -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,
David Vinczeba3bd602019-06-17 16:01:43 +02002237 BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC &&
Fabio Utzig10ee6482019-08-01 12:04:52 -03002238 boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) == 0)
David Vinczeba3bd602019-06-17 16:01:43 +02002239 {
2240 /* Set swap type to REVERT to overwrite the primary
2241 * slot with the image contained in secondary slot
2242 * and to trigger the explicit setting of the
2243 * image_ok flag.
2244 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002245 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02002246 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02002247 }
2248 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02002249#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002250 }
David Vinczeba3bd602019-06-17 16:01:43 +02002251 } else {
2252 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002253 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002254 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002255}
2256
Christopher Collins92ea77f2016-12-12 15:59:26 -08002257int
Fabio Utzig10ee6482019-08-01 12:04:52 -03002258context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08002259{
Marti Bolivar84898652017-06-13 17:20:22 -04002260 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02002261 struct boot_status bs;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002262 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002263 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03002264 int image_index;
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 Utzigba829042018-09-18 08:29:34 -03002276
David Vinczeba3bd602019-06-17 16:01:43 +02002277 /* Iterate over all the images. By the end of the loop the swap type has
2278 * to be determined for each image and all aborted swaps have to be
2279 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002280 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002281 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03002282
David Vinczeba3bd602019-06-17 16:01:43 +02002283#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
2284 /* The keys used for encryption may no longer be valid (could belong to
2285 * another images). Therefore, mark them as invalid to force their reload
2286 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03002287 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002288 boot_enc_mark_keys_invalid(state->enc);
David Brown554c52e2017-06-30 16:01:07 -06002289#endif
2290
Fabio Utzig10ee6482019-08-01 12:04:52 -03002291 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03002292
Fabio Utzig10ee6482019-08-01 12:04:52 -03002293 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002294 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03002295 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002296 secondary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03002297 state->scratch.sectors = scratch_sectors;
David Vinczeba3bd602019-06-17 16:01:43 +02002298
2299 /* Open primary and secondary image areas for the duration
2300 * of this call.
2301 */
2302 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03002303 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03002304 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02002305 assert(rc == 0);
2306 }
2307 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03002308 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002309 assert(rc == 0);
2310
2311 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002312 boot_prepare_image_for_update(state, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002313 }
2314
David Vinczee32483f2019-06-13 10:46:24 +02002315#if (BOOT_IMAGE_NUMBER > 1)
2316 /* Iterate over all the images and verify whether the image dependencies
2317 * are all satisfied and update swap type if necessary.
2318 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002319 boot_verify_all_image_dependency(state);
David Vinczee32483f2019-06-13 10:46:24 +02002320#endif
2321
David Vinczeba3bd602019-06-17 16:01:43 +02002322 /* Iterate over all the images. At this point there are no aborted swaps
2323 * and the swap types are determined for each image. By the end of the loop
2324 * all required update operations will have been finished.
2325 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002326 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002327
2328#if (BOOT_IMAGE_NUMBER > 1)
2329#ifdef MCUBOOT_ENC_IMAGES
2330 /* The keys used for encryption may no longer be valid (could belong to
2331 * another images). Therefore, mark them as invalid to force their reload
2332 * by boot_enc_load().
2333 */
2334 boot_enc_mark_keys_invalid();
2335#endif /* MCUBOOT_ENC_IMAGES */
2336
2337 /* Indicate that swap is not aborted */
2338 memset(&bs, 0, sizeof bs);
2339 bs.idx = BOOT_STATUS_IDX_0;
2340 bs.state = BOOT_STATUS_STATE_0;
2341#endif /* (BOOT_IMAGE_NUMBER > 1) */
2342
2343 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002344 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02002345
Fabio Utzig10ee6482019-08-01 12:04:52 -03002346 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002347 case BOOT_SWAP_TYPE_NONE:
2348 break;
2349
2350 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
2351 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
2352 case BOOT_SWAP_TYPE_REVERT:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002353 rc = boot_perform_update(state, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002354 assert(rc == 0);
2355 break;
2356
2357 case BOOT_SWAP_TYPE_FAIL:
2358 /* The image in secondary slot was invalid and is now erased. Ensure
2359 * we don't try to boot into it again on the next reboot. Do this by
2360 * pretending we just reverted back to primary slot.
2361 */
2362#ifndef MCUBOOT_OVERWRITE_ONLY
2363 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002364 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002365 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002366 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002367 }
2368#endif /* !MCUBOOT_OVERWRITE_ONLY */
2369 break;
2370
2371 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002372 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002373 }
2374
Fabio Utzig10ee6482019-08-01 12:04:52 -03002375 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002376 BOOT_LOG_ERR("panic!");
2377 assert(0);
2378
2379 /* Loop forever... */
2380 while (1) {}
2381 }
2382 }
2383
2384 /* Iterate over all the images. At this point all required update operations
2385 * have finished. By the end of the loop each image in the primary slot will
2386 * have been re-validated.
2387 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002388 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2389 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02002390 /* Attempt to read an image header from each slot. Ensure that image
2391 * headers in slots are aligned with headers in boot_data.
2392 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002393 rc = boot_read_image_headers(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02002394 if (rc != 0) {
2395 goto out;
2396 }
2397 /* Since headers were reloaded, it can be assumed we just performed
2398 * a swap or overwrite. Now the header info that should be used to
2399 * provide the data for the bootstrap, which previously was at
2400 * secondary slot, was updated to primary slot.
2401 */
2402 }
2403
2404#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig10ee6482019-08-01 12:04:52 -03002405 rc = boot_validate_slot(state, BOOT_PRIMARY_SLOT, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02002406 if (rc != 0) {
2407 rc = BOOT_EBADIMAGE;
2408 goto out;
2409 }
2410#else
2411 /* Even if we're not re-validating the primary slot, we could be booting
2412 * onto an empty flash chip. At least do a basic sanity check that
2413 * the magic number on the image is OK.
2414 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002415 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002416 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
Fabio Utzig10ee6482019-08-01 12:04:52 -03002417 &boot_img_hdr(state,BOOT_PRIMARY_SLOT)->ih_magic,
2418 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002419 rc = BOOT_EBADIMAGE;
2420 goto out;
2421 }
2422#endif
2423 }
2424
Fabio Utzigb0f04732019-07-31 09:49:19 -03002425#if (BOOT_IMAGE_NUMBER > 1)
David Vinczeba3bd602019-06-17 16:01:43 +02002426 /* Always boot from the primary slot of Image 0. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002427 BOOT_CURR_IMG(state) = 0;
Fabio Utzigb0f04732019-07-31 09:49:19 -03002428#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -03002429 rsp->br_flash_dev_id = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)->fa_device_id;
2430 rsp->br_image_off = boot_img_slot_off(state, BOOT_PRIMARY_SLOT);
2431 rsp->br_hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002432
Marti Bolivarc0b47912017-06-13 17:18:09 -04002433 out:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002434 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2435 flash_area_close(BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002436 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002437 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
David Vinczeba3bd602019-06-17 16:01:43 +02002438 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04002439 }
2440 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002441}
2442
Fabio Utzig10ee6482019-08-01 12:04:52 -03002443/**
2444 * Prepares the booting process. This function moves images around in flash as
2445 * appropriate, and tells you what address to boot from.
2446 *
2447 * @param rsp On success, indicates how booting should occur.
2448 *
2449 * @return 0 on success; nonzero on failure.
2450 */
2451int
2452boot_go(struct boot_rsp *rsp)
2453{
2454 return context_boot_go(&boot_data, rsp);
2455}
2456
Christopher Collins92ea77f2016-12-12 15:59:26 -08002457int
2458split_go(int loader_slot, int split_slot, void **entry)
2459{
Marti Bolivarc50926f2017-06-14 09:35:40 -04002460 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08002461 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002462 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002463 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002464 int rc;
2465
Christopher Collins92ea77f2016-12-12 15:59:26 -08002466 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
2467 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04002468 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002469 }
David Vinczeba3bd602019-06-17 16:01:43 +02002470 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
2471 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002472
2473 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
2474 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07002475 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04002476 assert(rc == 0);
2477 split_flash_id = flash_area_id_from_image_slot(split_slot);
2478 rc = flash_area_open(split_flash_id,
2479 &BOOT_IMG_AREA(&boot_data, split_slot));
2480 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002481
2482 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002483 rc = boot_read_sectors(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002484 if (rc != 0) {
2485 rc = SPLIT_GO_ERR;
2486 goto done;
2487 }
2488
Fabio Utzig10ee6482019-08-01 12:04:52 -03002489 rc = boot_read_image_headers(&boot_data, true);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002490 if (rc != 0) {
2491 goto done;
2492 }
2493
Christopher Collins92ea77f2016-12-12 15:59:26 -08002494 /* Don't check the bootable image flag because we could really call a
2495 * bootable or non-bootable image. Just validate that the image check
2496 * passes which is distinct from the normal check.
2497 */
Marti Bolivarf804f622017-06-12 15:41:48 -04002498 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04002499 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04002500 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04002501 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002502 if (rc != 0) {
2503 rc = SPLIT_GO_NON_MATCHING;
2504 goto done;
2505 }
2506
Marti Bolivarea088872017-06-12 17:10:49 -04002507 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04002508 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08002509 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002510 rc = SPLIT_GO_OK;
2511
2512done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04002513 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
2514 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002515 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002516 return rc;
2517}