blob: 80bebd7a34a3b2a4b1d1e407691b15da06ebcd98 [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
270 rc = flash_area_read(fap, hdr->ih_hdr_size + hdr->ih_img_size,
271 &info, sizeof(info));
272 if (rc != 0) {
273 rc = BOOT_EFLASH;
274 goto done;
275 }
276 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
277 rc = BOOT_EBADIMAGE;
278 goto done;
279 }
280 *size = hdr->ih_hdr_size + hdr->ih_img_size + info.it_tlv_tot;
281 rc = 0;
282
283done:
284 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300285 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600286}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300287#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600288
Fabio Utzigc08ed212017-06-20 19:28:36 -0300289static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300290boot_read_image_header(struct boot_loader_state *state, int slot,
291 struct image_header *out_hdr)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800292{
293 const struct flash_area *fap;
294 int area_id;
295 int rc;
296
Fabio Utzig10ee6482019-08-01 12:04:52 -0300297#if (BOOT_IMAGE_NUMBER == 1)
298 (void)state;
299#endif
300
301 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800302 rc = flash_area_open(area_id, &fap);
303 if (rc != 0) {
304 rc = BOOT_EFLASH;
305 goto done;
306 }
307
308 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
309 if (rc != 0) {
310 rc = BOOT_EFLASH;
311 goto done;
312 }
313
314 rc = 0;
315
316done:
317 flash_area_close(fap);
318 return rc;
319}
320
321static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300322boot_read_image_headers(struct boot_loader_state *state, bool require_all)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800323{
324 int rc;
325 int i;
326
327 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300328 rc = boot_read_image_header(state, i, boot_img_hdr(state, i));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800329 if (rc != 0) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200330 /* If `require_all` is set, fail on any single fail, otherwise
331 * if at least the first slot's header was read successfully,
332 * then the boot loader can attempt a boot.
333 *
334 * Failure to read any headers is a fatal error.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800335 */
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200336 if (i > 0 && !require_all) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800337 return 0;
338 } else {
339 return rc;
340 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800341 }
342 }
343
344 return 0;
345}
346
347static uint8_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300348boot_write_sz(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800349{
350 uint8_t elem_sz;
351 uint8_t align;
352
353 /* Figure out what size to write update status update as. The size depends
354 * on what the minimum write size is for scratch area, active image slot.
355 * We need to use the bigger of those 2 values.
356 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300357 elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
358 align = flash_area_align(BOOT_SCRATCH_AREA(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800359 if (align > elem_sz) {
360 elem_sz = align;
361 }
362
363 return elem_sz;
364}
365
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200366/*
367 * Slots are compatible when all sectors that store upto to size of the image
368 * round up to sector size, in both slot's are able to fit in the scratch
369 * area, and have sizes that are a multiple of each other (powers of two
370 * presumably!).
371 */
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800372static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300373boot_slots_compatible(struct boot_loader_state *state)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800374{
David Vincze2d736ad2019-02-18 11:50:22 +0100375 size_t num_sectors_primary;
376 size_t num_sectors_secondary;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200377 size_t sz0, sz1;
David Vincze2d736ad2019-02-18 11:50:22 +0100378 size_t primary_slot_sz, secondary_slot_sz;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200379 size_t scratch_sz;
380 size_t i, j;
381 int8_t smaller;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800382
Fabio Utzig10ee6482019-08-01 12:04:52 -0300383 num_sectors_primary = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
384 num_sectors_secondary = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT);
David Vincze2d736ad2019-02-18 11:50:22 +0100385 if ((num_sectors_primary > BOOT_MAX_IMG_SECTORS) ||
386 (num_sectors_secondary > BOOT_MAX_IMG_SECTORS)) {
Fabio Utziga1fae672018-03-30 10:52:38 -0300387 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800388 return 0;
389 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300390
Fabio Utzig10ee6482019-08-01 12:04:52 -0300391 scratch_sz = boot_scratch_area_size(state);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200392
393 /*
394 * The following loop scans all sectors in a linear fashion, assuring that
395 * for each possible sector in each slot, it is able to fit in the other
396 * slot's sector or sectors. Slot's should be compatible as long as any
397 * number of a slot's sectors are able to fit into another, which only
398 * excludes cases where sector sizes are not a multiple of each other.
399 */
David Vincze2d736ad2019-02-18 11:50:22 +0100400 i = sz0 = primary_slot_sz = 0;
401 j = sz1 = secondary_slot_sz = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200402 smaller = 0;
David Vincze2d736ad2019-02-18 11:50:22 +0100403 while (i < num_sectors_primary || j < num_sectors_secondary) {
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200404 if (sz0 == sz1) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300405 sz0 += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
406 sz1 += boot_img_sector_size(state, BOOT_SECONDARY_SLOT, j);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200407 i++;
408 j++;
409 } else if (sz0 < sz1) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300410 sz0 += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
David Vincze2d736ad2019-02-18 11:50:22 +0100411 /* Guarantee that multiple sectors of the secondary slot
412 * fit into the primary slot.
413 */
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200414 if (smaller == 2) {
415 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors");
416 return 0;
417 }
418 smaller = 1;
419 i++;
420 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300421 sz1 += boot_img_sector_size(state, BOOT_SECONDARY_SLOT, j);
David Vincze2d736ad2019-02-18 11:50:22 +0100422 /* Guarantee that multiple sectors of the primary slot
423 * fit into the secondary slot.
424 */
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200425 if (smaller == 1) {
426 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors");
427 return 0;
428 }
429 smaller = 2;
430 j++;
431 }
432 if (sz0 == sz1) {
David Vincze2d736ad2019-02-18 11:50:22 +0100433 primary_slot_sz += sz0;
434 secondary_slot_sz += sz1;
435 /* Scratch has to fit each swap operation to the size of the larger
436 * sector among the primary slot and the secondary slot.
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200437 */
438 if (sz0 > scratch_sz || sz1 > scratch_sz) {
439 BOOT_LOG_WRN("Cannot upgrade: not all sectors fit inside scratch");
440 return 0;
441 }
442 smaller = sz0 = sz1 = 0;
443 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300444 }
445
David Vincze2d736ad2019-02-18 11:50:22 +0100446 if ((i != num_sectors_primary) ||
447 (j != num_sectors_secondary) ||
448 (primary_slot_sz != secondary_slot_sz)) {
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200449 BOOT_LOG_WRN("Cannot upgrade: slots are not compatible");
450 return 0;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800451 }
452
453 return 1;
454}
455
Fabio Utzig10ee6482019-08-01 12:04:52 -0300456#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
457static int
458boot_initialize_area(struct boot_loader_state *state, int flash_area)
459{
460 int num_sectors = BOOT_MAX_IMG_SECTORS;
461 int rc;
462
463 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
464 rc = flash_area_to_sectors(flash_area, &num_sectors,
465 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors);
466 BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors = (size_t)num_sectors;
467
468 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
469 rc = flash_area_to_sectors(flash_area, &num_sectors,
470 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors);
471 BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors = (size_t)num_sectors;
472
473 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
474 rc = flash_area_to_sectors(flash_area, &num_sectors,
475 state->scratch.sectors);
476 state->scratch.num_sectors = (size_t)num_sectors;
477 } else {
478 return BOOT_EFLASH;
479 }
480
481 return rc;
482}
483#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
484static int
485boot_initialize_area(struct boot_loader_state *state, int flash_area)
486{
487 uint32_t num_sectors;
488 struct flash_sector *out_sectors;
489 size_t *out_num_sectors;
490 int rc;
491
492 num_sectors = BOOT_MAX_IMG_SECTORS;
493
494 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
495 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
496 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
497 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
498 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
499 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
500 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
501 out_sectors = state->scratch.sectors;
502 out_num_sectors = &state->scratch.num_sectors;
503 } else {
504 return BOOT_EFLASH;
505 }
506
507 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
508 if (rc != 0) {
509 return rc;
510 }
511 *out_num_sectors = num_sectors;
512 return 0;
513}
514#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
515
Christopher Collins92ea77f2016-12-12 15:59:26 -0800516/**
517 * Determines the sector layout of both image slots and the scratch area.
518 * This information is necessary for calculating the number of bytes to erase
519 * and copy during an image swap. The information collected during this
Fabio Utzig10ee6482019-08-01 12:04:52 -0300520 * function is used to populate the state.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800521 */
522static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300523boot_read_sectors(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800524{
Fabio Utzigb0f04732019-07-31 09:49:19 -0300525 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800526 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800527
Fabio Utzig10ee6482019-08-01 12:04:52 -0300528 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300529
530 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800531 if (rc != 0) {
532 return BOOT_EFLASH;
533 }
534
Fabio Utzig10ee6482019-08-01 12:04:52 -0300535 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800536 if (rc != 0) {
537 return BOOT_EFLASH;
538 }
539
Fabio Utzig10ee6482019-08-01 12:04:52 -0300540 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200541 if (rc != 0) {
542 return BOOT_EFLASH;
543 }
544
Fabio Utzig10ee6482019-08-01 12:04:52 -0300545 BOOT_WRITE_SZ(state) = boot_write_sz(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800546
547 return 0;
548}
549
550static uint32_t
551boot_status_internal_off(int idx, int state, int elem_sz)
552{
553 int idx_sz;
554
555 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
556
Fabio Utzig39000012018-07-30 12:40:20 -0300557 return (idx - BOOT_STATUS_IDX_0) * idx_sz +
558 (state - BOOT_STATUS_STATE_0) * elem_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800559}
560
561/**
562 * Reads the status of a partially-completed swap, if any. This is necessary
563 * to recover in case the boot lodaer was reset in the middle of a swap
564 * operation.
565 */
566static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300567boot_read_status_bytes(const struct flash_area *fap,
568 struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800569{
570 uint32_t off;
571 uint8_t status;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300572 int max_entries;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800573 int found;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200574 int found_idx;
575 int invalid;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800576 int rc;
577 int i;
578
579 off = boot_status_off(fap);
Fabio Utzig10ee6482019-08-01 12:04:52 -0300580 max_entries = boot_status_entries(BOOT_CURR_IMG(state), fap);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300581
Christopher Collins92ea77f2016-12-12 15:59:26 -0800582 found = 0;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200583 found_idx = 0;
584 invalid = 0;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300585 for (i = 0; i < max_entries; i++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300586 rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(state),
Fabio Utzig178be542018-09-19 08:12:56 -0300587 &status, 1);
588 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800589 return BOOT_EFLASH;
590 }
591
Fabio Utzig178be542018-09-19 08:12:56 -0300592 if (rc == 1) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200593 if (found && !found_idx) {
594 found_idx = i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800595 }
596 } else if (!found) {
597 found = 1;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200598 } else if (found_idx) {
599 invalid = 1;
600 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800601 }
602 }
603
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200604 if (invalid) {
605 /* This means there was an error writing status on the last
606 * swap. Tell user and move on to validation!
607 */
608 BOOT_LOG_ERR("Detected inconsistent status!");
609
David Vincze2d736ad2019-02-18 11:50:22 +0100610#if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
611 /* With validation of the primary slot disabled, there is no way
612 * to be sure the swapped primary slot is OK, so abort!
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200613 */
614 assert(0);
615#endif
616 }
617
Christopher Collins92ea77f2016-12-12 15:59:26 -0800618 if (found) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200619 if (!found_idx) {
620 found_idx = i;
621 }
622 found_idx--;
Fabio Utzig39000012018-07-30 12:40:20 -0300623 bs->idx = (found_idx / BOOT_STATUS_STATE_COUNT) + 1;
624 bs->state = (found_idx % BOOT_STATUS_STATE_COUNT) + 1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800625 }
626
627 return 0;
628}
629
630/**
631 * Reads the boot status from the flash. The boot status contains
632 * the current state of an interrupted image copy operation. If the boot
633 * status is not present, or it indicates that previous copy finished,
634 * there is no operation in progress.
635 */
636static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300637boot_read_status(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800638{
639 const struct flash_area *fap;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700640 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200641 uint8_t swap_info;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800642 int status_loc;
643 int area_id;
644 int rc;
645
646 memset(bs, 0, sizeof *bs);
Fabio Utzig39000012018-07-30 12:40:20 -0300647 bs->idx = BOOT_STATUS_IDX_0;
648 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700649 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800650
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700651#ifdef MCUBOOT_OVERWRITE_ONLY
652 /* Overwrite-only doesn't make use of the swap status area. */
653 return 0;
654#endif
655
Fabio Utzig10ee6482019-08-01 12:04:52 -0300656 status_loc = boot_status_source(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800657 switch (status_loc) {
658 case BOOT_STATUS_SOURCE_NONE:
659 return 0;
660
661 case BOOT_STATUS_SOURCE_SCRATCH:
662 area_id = FLASH_AREA_IMAGE_SCRATCH;
663 break;
664
David Vincze2d736ad2019-02-18 11:50:22 +0100665 case BOOT_STATUS_SOURCE_PRIMARY_SLOT:
Fabio Utzig10ee6482019-08-01 12:04:52 -0300666 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800667 break;
668
669 default:
670 assert(0);
671 return BOOT_EBADARGS;
672 }
673
674 rc = flash_area_open(area_id, &fap);
675 if (rc != 0) {
676 return BOOT_EFLASH;
677 }
678
Fabio Utzig10ee6482019-08-01 12:04:52 -0300679 rc = boot_read_status_bytes(fap, state, bs);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700680 if (rc == 0) {
David Vinczee2453472019-06-17 12:31:59 +0200681 off = boot_swap_info_off(fap);
682 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700683 if (rc == 1) {
David Vinczee2453472019-06-17 12:31:59 +0200684 BOOT_SET_SWAP_INFO(swap_info, 0, BOOT_SWAP_TYPE_NONE);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700685 rc = 0;
686 }
David Vinczee2453472019-06-17 12:31:59 +0200687
688 /* Extract the swap type info */
689 bs->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700690 }
Fabio Utzig46490722017-09-04 15:34:32 -0300691
692 flash_area_close(fap);
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700693
Fabio Utzig46490722017-09-04 15:34:32 -0300694 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800695}
696
697/**
698 * Writes the supplied boot status to the flash file system. The boot status
699 * contains the current state of an in-progress image copy operation.
700 *
701 * @param bs The boot status to write.
702 *
703 * @return 0 on success; nonzero on failure.
704 */
705int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300706boot_write_status(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800707{
708 const struct flash_area *fap;
709 uint32_t off;
710 int area_id;
711 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300712 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700713 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300714 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800715
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300716 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100717 * the trailer. Since in the last step the primary slot is erased, the
718 * first two status writes go to the scratch which will be copied to
719 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300720 */
721
Fabio Utzig2473ac02017-05-02 12:45:02 -0300722 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800723 /* Write to scratch. */
724 area_id = FLASH_AREA_IMAGE_SCRATCH;
725 } else {
David Vincze2d736ad2019-02-18 11:50:22 +0100726 /* Write to the primary slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300727 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800728 }
729
730 rc = flash_area_open(area_id, &fap);
731 if (rc != 0) {
732 rc = BOOT_EFLASH;
733 goto done;
734 }
735
736 off = boot_status_off(fap) +
Marti Bolivare10a7392017-06-14 16:20:07 -0400737 boot_status_internal_off(bs->idx, bs->state,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300738 BOOT_WRITE_SZ(state));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200739 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300740 erased_val = flash_area_erased_val(fap);
741 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700742 buf[0] = bs->state;
743
744 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800745 if (rc != 0) {
746 rc = BOOT_EFLASH;
747 goto done;
748 }
749
750 rc = 0;
751
752done:
753 flash_area_close(fap);
754 return rc;
755}
756
757/*
758 * Validate image hash/signature in a slot.
759 */
760static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300761boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
762 const struct flash_area *fap, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800763{
Fabio Utzig10ee6482019-08-01 12:04:52 -0300764 TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigb0f04732019-07-31 09:49:19 -0300765 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300766 int rc;
767
Fabio Utzig10ee6482019-08-01 12:04:52 -0300768#if (BOOT_IMAGE_NUMBER == 1)
769 (void)state;
770#endif
771
772 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300773
Fabio Utzigba829042018-09-18 08:29:34 -0300774#ifndef MCUBOOT_ENC_IMAGES
775 (void)bs;
776 (void)rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -0300777 if (bootutil_img_validate(NULL, image_index, hdr, fap, tmpbuf,
778 BOOT_TMPBUF_SZ, NULL, 0, NULL)) {
779 return BOOT_EBADIMAGE;
780 }
Fabio Utzigba829042018-09-18 08:29:34 -0300781#else
Fabio Utzigb0f04732019-07-31 09:49:19 -0300782 if ((fap->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index))
783 && IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300784 rc = boot_enc_load(state->enc, image_index, hdr, fap, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -0300785 if (rc < 0) {
786 return BOOT_EBADIMAGE;
787 }
Fabio Utzig10ee6482019-08-01 12:04:52 -0300788 if (rc == 0 && boot_enc_set_key(state->enc, 1, bs->enckey[1])) {
Fabio Utzigba829042018-09-18 08:29:34 -0300789 return BOOT_EBADIMAGE;
790 }
791 }
Fabio Utzig10ee6482019-08-01 12:04:52 -0300792 if (bootutil_img_validate(state->enc, image_index, hdr, fap, tmpbuf,
793 BOOT_TMPBUF_SZ, NULL, 0, NULL)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800794 return BOOT_EBADIMAGE;
795 }
Fabio Utzig10ee6482019-08-01 12:04:52 -0300796#endif
797
Christopher Collins92ea77f2016-12-12 15:59:26 -0800798 return 0;
799}
800
801static int
802split_image_check(struct image_header *app_hdr,
803 const struct flash_area *app_fap,
804 struct image_header *loader_hdr,
805 const struct flash_area *loader_fap)
806{
807 static void *tmpbuf;
808 uint8_t loader_hash[32];
809
810 if (!tmpbuf) {
811 tmpbuf = malloc(BOOT_TMPBUF_SZ);
812 if (!tmpbuf) {
813 return BOOT_ENOMEM;
814 }
815 }
816
Fabio Utzig10ee6482019-08-01 12:04:52 -0300817 if (bootutil_img_validate(NULL, 0, loader_hdr, loader_fap, tmpbuf,
818 BOOT_TMPBUF_SZ, NULL, 0, loader_hash)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800819 return BOOT_EBADIMAGE;
820 }
821
Fabio Utzig10ee6482019-08-01 12:04:52 -0300822 if (bootutil_img_validate(NULL, 0, app_hdr, app_fap, tmpbuf,
823 BOOT_TMPBUF_SZ, loader_hash, 32, NULL)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800824 return BOOT_EBADIMAGE;
825 }
826
827 return 0;
828}
829
Fabio Utzig338a19f2018-12-03 08:37:08 -0200830/*
831 * Check that a memory area consists of a given value.
832 */
833static inline bool
834boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300835{
836 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200837 uint8_t *p = (uint8_t *)data;
838 for (i = 0; i < len; i++) {
839 if (val != p[i]) {
840 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300841 }
842 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200843 return true;
844}
845
846static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300847boot_check_header_erased(struct boot_loader_state *state, int slot)
Fabio Utzig338a19f2018-12-03 08:37:08 -0200848{
849 const struct flash_area *fap;
850 struct image_header *hdr;
851 uint8_t erased_val;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300852 int area_id;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200853 int rc;
854
Fabio Utzig10ee6482019-08-01 12:04:52 -0300855 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300856 rc = flash_area_open(area_id, &fap);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200857 if (rc != 0) {
858 return -1;
859 }
860
861 erased_val = flash_area_erased_val(fap);
862 flash_area_close(fap);
863
Fabio Utzig10ee6482019-08-01 12:04:52 -0300864 hdr = boot_img_hdr(state, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200865 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
866 return -1;
867 }
868
869 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300870}
871
Christopher Collins92ea77f2016-12-12 15:59:26 -0800872static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300873boot_validate_slot(struct boot_loader_state *state, int slot,
874 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800875{
876 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400877 struct image_header *hdr;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300878 int area_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800879 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300880
Fabio Utzig10ee6482019-08-01 12:04:52 -0300881 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300882 rc = flash_area_open(area_id, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800883 if (rc != 0) {
884 return BOOT_EFLASH;
885 }
886
Fabio Utzig10ee6482019-08-01 12:04:52 -0300887 hdr = boot_img_hdr(state, slot);
888 if (boot_check_header_erased(state, slot) == 0 ||
889 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100890 /* No bootable image in slot; continue booting from the primary slot. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200891 rc = -1;
892 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300893 }
894
Fabio Utzig10ee6482019-08-01 12:04:52 -0300895 if (hdr->ih_magic != IMAGE_MAGIC || boot_image_check(state, hdr, fap, bs)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100896 if (slot != BOOT_PRIMARY_SLOT) {
David Brownb38e0442017-02-24 13:57:12 -0700897 flash_area_erase(fap, 0, fap->fa_size);
David Vincze2d736ad2019-02-18 11:50:22 +0100898 /* Image in the secondary slot is invalid. Erase the image and
899 * continue booting from the primary slot.
David Brownb38e0442017-02-24 13:57:12 -0700900 */
901 }
David Vincze2d736ad2019-02-18 11:50:22 +0100902 BOOT_LOG_ERR("Image in the %s slot is not valid!",
903 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
Fabio Utzig338a19f2018-12-03 08:37:08 -0200904 rc = -1;
905 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800906 }
907
David Vincze2d736ad2019-02-18 11:50:22 +0100908 /* Image in the secondary slot is valid. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200909 rc = 0;
910
911out:
912 flash_area_close(fap);
913 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800914}
915
916/**
917 * Determines which swap operation to perform, if any. If it is determined
David Vincze2d736ad2019-02-18 11:50:22 +0100918 * that a swap operation is required, the image in the secondary slot is checked
919 * for validity. If the image in the secondary slot is invalid, it is erased,
920 * and a swap type of "none" is indicated.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800921 *
922 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
923 */
924static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300925boot_validated_swap_type(struct boot_loader_state *state,
926 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800927{
928 int swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800929
Fabio Utzigb0f04732019-07-31 09:49:19 -0300930#if (BOOT_IMAGE_NUMBER == 1)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800931 swap_type = boot_swap_type();
Fabio Utzigb0f04732019-07-31 09:49:19 -0300932#else
933 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
934#endif
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300935 switch (swap_type) {
936 case BOOT_SWAP_TYPE_TEST:
937 case BOOT_SWAP_TYPE_PERM:
938 case BOOT_SWAP_TYPE_REVERT:
David Vincze2d736ad2019-02-18 11:50:22 +0100939 /* Boot loader wants to switch to the secondary slot.
940 * Ensure image is valid.
941 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300942 if (boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) != 0) {
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300943 swap_type = BOOT_SWAP_TYPE_FAIL;
944 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800945 }
946
947 return swap_type;
948}
949
950/**
951 * Calculates the number of sectors the scratch area can contain. A "last"
952 * source sector is specified because images are copied backwards in flash
953 * (final index to index number 0).
954 *
955 * @param last_sector_idx The index of the last source sector
956 * (inclusive).
957 * @param out_first_sector_idx The index of the first source sector
958 * (inclusive) gets written here.
959 *
960 * @return The number of bytes comprised by the
961 * [first-sector, last-sector] range.
962 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300963#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800964static uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300965boot_copy_sz(struct boot_loader_state *state, int last_sector_idx,
966 int *out_first_sector_idx)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800967{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400968 size_t scratch_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800969 uint32_t new_sz;
970 uint32_t sz;
971 int i;
972
973 sz = 0;
974
Fabio Utzig10ee6482019-08-01 12:04:52 -0300975 scratch_sz = boot_scratch_area_size(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800976 for (i = last_sector_idx; i >= 0; i--) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300977 new_sz = sz + boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200978 /*
David Vincze2d736ad2019-02-18 11:50:22 +0100979 * The secondary slot is not being checked here, because
980 * `boot_slots_compatible` already provides assurance that the copy size
981 * will be compatible with the primary slot and scratch.
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200982 */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400983 if (new_sz > scratch_sz) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800984 break;
985 }
986 sz = new_sz;
987 }
988
989 /* i currently refers to a sector that doesn't fit or it is -1 because all
990 * sectors have been processed. In both cases, exclude sector i.
991 */
992 *out_first_sector_idx = i + 1;
993 return sz;
994}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300995#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800996
997/**
998 * Erases a region of flash.
999 *
Fabio Utzigba829042018-09-18 08:29:34 -03001000 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001001 * @param off The offset within the flash area to start the
1002 * erase.
1003 * @param sz The number of bytes to erase.
1004 *
1005 * @return 0 on success; nonzero on failure.
1006 */
Fabio Utzigba829042018-09-18 08:29:34 -03001007static inline int
1008boot_erase_sector(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001009{
Fabio Utzigba829042018-09-18 08:29:34 -03001010 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001011}
1012
1013/**
1014 * Copies the contents of one flash region to another. You must erase the
1015 * destination region prior to calling this function.
1016 *
1017 * @param flash_area_id_src The ID of the source flash area.
1018 * @param flash_area_id_dst The ID of the destination flash area.
1019 * @param off_src The offset within the source flash area to
1020 * copy from.
1021 * @param off_dst The offset within the destination flash area to
1022 * copy to.
1023 * @param sz The number of bytes to copy.
1024 *
1025 * @return 0 on success; nonzero on failure.
1026 */
1027static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001028boot_copy_sector(struct boot_loader_state *state,
1029 const struct flash_area *fap_src,
Fabio Utzigba829042018-09-18 08:29:34 -03001030 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -08001031 uint32_t off_src, uint32_t off_dst, uint32_t sz)
1032{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001033 uint32_t bytes_copied;
1034 int chunk_sz;
1035 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -03001036#ifdef MCUBOOT_ENC_IMAGES
1037 uint32_t off;
1038 size_t blk_off;
1039 struct image_header *hdr;
1040 uint16_t idx;
1041 uint32_t blk_sz;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001042 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -03001043#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001044
Fabio Utzig10ee6482019-08-01 12:04:52 -03001045 TARGET_STATIC uint8_t buf[1024];
1046
1047#if !defined(MCUBOOT_ENC_IMAGES)
1048 (void)state;
1049#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001050
Christopher Collins92ea77f2016-12-12 15:59:26 -08001051 bytes_copied = 0;
1052 while (bytes_copied < sz) {
1053 if (sz - bytes_copied > sizeof buf) {
1054 chunk_sz = sizeof buf;
1055 } else {
1056 chunk_sz = sz - bytes_copied;
1057 }
1058
1059 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
1060 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001061 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001062 }
1063
Fabio Utzigba829042018-09-18 08:29:34 -03001064#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001065 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001066 if (fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) ||
1067 fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001068 /* assume the secondary slot as src, needs decryption */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001069 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001070 off = off_src;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001071 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001072 /* might need encryption (metadata from the primary slot) */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001073 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001074 off = off_dst;
1075 }
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001076 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001077 blk_sz = chunk_sz;
1078 idx = 0;
1079 if (off + bytes_copied < hdr->ih_hdr_size) {
1080 /* do not decrypt header */
1081 blk_off = 0;
1082 blk_sz = chunk_sz - hdr->ih_hdr_size;
1083 idx = hdr->ih_hdr_size;
1084 } else {
1085 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
1086 }
1087 if (off + bytes_copied + chunk_sz > hdr->ih_hdr_size + hdr->ih_img_size) {
1088 /* do not decrypt TLVs */
1089 if (off + bytes_copied >= hdr->ih_hdr_size + hdr->ih_img_size) {
1090 blk_sz = 0;
1091 } else {
1092 blk_sz = (hdr->ih_hdr_size + hdr->ih_img_size) - (off + bytes_copied);
1093 }
1094 }
Fabio Utzig10ee6482019-08-01 12:04:52 -03001095 boot_encrypt(state->enc, image_index, fap_src,
Fabio Utzigb0f04732019-07-31 09:49:19 -03001096 (off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
1097 blk_off, &buf[idx]);
Fabio Utzigba829042018-09-18 08:29:34 -03001098 }
1099 }
1100#endif
1101
Christopher Collins92ea77f2016-12-12 15:59:26 -08001102 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
1103 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001104 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001105 }
1106
1107 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -03001108
1109 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -08001110 }
1111
Fabio Utzigba829042018-09-18 08:29:34 -03001112 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001113}
1114
David Brown6b1b3b92017-09-19 08:59:10 -06001115#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -03001116static inline int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001117boot_status_init(const struct boot_loader_state *state,
1118 const struct flash_area *fap,
1119 const struct boot_status *bs)
Fabio Utzig2473ac02017-05-02 12:45:02 -03001120{
Fabio Utzig2473ac02017-05-02 12:45:02 -03001121 struct boot_swap_state swap_state;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001122 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001123 int rc;
1124
Fabio Utzig10ee6482019-08-01 12:04:52 -03001125#if (BOOT_IMAGE_NUMBER == 1)
1126 (void)state;
1127#endif
1128
1129 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001130
Christopher Collins2c88e692019-05-22 15:10:14 -07001131 BOOT_LOG_DBG("initializing status; fa_id=%d", fap->fa_id);
1132
Fabio Utzigb0f04732019-07-31 09:49:19 -03001133 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
1134 &swap_state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001135 assert(rc == 0);
1136
Christopher Collinsa1c12042019-05-23 14:00:28 -07001137 if (bs->swap_type != BOOT_SWAP_TYPE_NONE) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03001138 rc = boot_write_swap_info(fap, bs->swap_type, image_index);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001139 assert(rc == 0);
1140 }
1141
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001142 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001143 rc = boot_write_image_ok(fap);
1144 assert(rc == 0);
1145 }
1146
Fabio Utzig46490722017-09-04 15:34:32 -03001147 rc = boot_write_swap_size(fap, bs->swap_size);
1148 assert(rc == 0);
1149
Fabio Utzigba829042018-09-18 08:29:34 -03001150#ifdef MCUBOOT_ENC_IMAGES
1151 rc = boot_write_enc_key(fap, 0, bs->enckey[0]);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001152 assert(rc == 0);
1153
Fabio Utzigba829042018-09-18 08:29:34 -03001154 rc = boot_write_enc_key(fap, 1, bs->enckey[1]);
1155 assert(rc == 0);
1156#endif
1157
1158 rc = boot_write_magic(fap);
1159 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001160
1161 return 0;
1162}
Christopher Collinsa1c12042019-05-23 14:00:28 -07001163
David Brown6b1b3b92017-09-19 08:59:10 -06001164#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001165
Fabio Utzig358c9352017-07-25 22:10:45 -03001166#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -03001167static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001168boot_erase_trailer_sectors(const struct boot_loader_state *state,
1169 const struct flash_area *fap)
Fabio Utzig2473ac02017-05-02 12:45:02 -03001170{
1171 uint8_t slot;
Fabio Utziged0ca432019-01-23 14:50:11 -02001172 uint32_t sector;
1173 uint32_t trailer_sz;
1174 uint32_t total_sz;
1175 uint32_t off;
1176 uint32_t sz;
David Vinczeb75c12a2019-03-22 14:58:33 +01001177 int fa_id_primary;
1178 int fa_id_secondary;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001179 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001180 int rc;
1181
Christopher Collins2c88e692019-05-22 15:10:14 -07001182 BOOT_LOG_DBG("erasing trailer; fa_id=%d", fap->fa_id);
1183
Fabio Utzig10ee6482019-08-01 12:04:52 -03001184 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001185 fa_id_primary = flash_area_id_from_multi_image_slot(image_index,
1186 BOOT_PRIMARY_SLOT);
1187 fa_id_secondary = flash_area_id_from_multi_image_slot(image_index,
1188 BOOT_SECONDARY_SLOT);
David Vinczeb75c12a2019-03-22 14:58:33 +01001189
1190 if (fap->fa_id == fa_id_primary) {
David Vincze2d736ad2019-02-18 11:50:22 +01001191 slot = BOOT_PRIMARY_SLOT;
David Vinczeb75c12a2019-03-22 14:58:33 +01001192 } else if (fap->fa_id == fa_id_secondary) {
David Vincze2d736ad2019-02-18 11:50:22 +01001193 slot = BOOT_SECONDARY_SLOT;
David Vinczeb75c12a2019-03-22 14:58:33 +01001194 } else {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001195 return BOOT_EFLASH;
1196 }
1197
Fabio Utziged0ca432019-01-23 14:50:11 -02001198 /* delete starting from last sector and moving to beginning */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001199 sector = boot_img_num_sectors(state, slot) - 1;
1200 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
Fabio Utziged0ca432019-01-23 14:50:11 -02001201 total_sz = 0;
1202 do {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001203 sz = boot_img_sector_size(state, slot, sector);
1204 off = boot_img_sector_off(state, slot, sector);
Fabio Utziged0ca432019-01-23 14:50:11 -02001205 rc = boot_erase_sector(fap, off, sz);
1206 assert(rc == 0);
1207
1208 sector--;
1209 total_sz += sz;
1210 } while (total_sz < trailer_sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001211
1212 return rc;
1213}
Fabio Utzig358c9352017-07-25 22:10:45 -03001214#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001215
Christopher Collins92ea77f2016-12-12 15:59:26 -08001216/**
1217 * Swaps the contents of two flash regions within the two image slots.
1218 *
1219 * @param idx The index of the first sector in the range of
1220 * sectors being swapped.
1221 * @param sz The number of bytes to swap.
1222 * @param bs The current boot status. This struct gets
1223 * updated according to the outcome.
1224 *
1225 * @return 0 on success; nonzero on failure.
1226 */
Fabio Utzig3488eef2017-06-12 10:25:43 -03001227#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -08001228static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001229boot_swap_sectors(int idx, uint32_t sz, struct boot_loader_state *state,
1230 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001231{
David Vincze2d736ad2019-02-18 11:50:22 +01001232 const struct flash_area *fap_primary_slot;
1233 const struct flash_area *fap_secondary_slot;
Fabio Utzigba829042018-09-18 08:29:34 -03001234 const struct flash_area *fap_scratch;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001235 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001236 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001237 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001238 uint32_t scratch_trailer_off;
1239 struct boot_swap_state swap_state;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001240 size_t last_sector;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001241 bool erase_scratch;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001242 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001243 int rc;
1244
1245 /* Calculate offset from start of image area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001246 img_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001247
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001248 copy_sz = sz;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001249 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
Fabio Utzig9678c972017-05-23 11:28:56 -04001250
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001251 /* sz in this function is always sized on a multiple of the sector size.
1252 * The check against the start offset of the last sector
Fabio Utzig9678c972017-05-23 11:28:56 -04001253 * is to determine if we're swapping the last sector. The last sector
1254 * needs special handling because it's where the trailer lives. If we're
1255 * copying it, we need to use scratch to write the trailer temporarily.
1256 *
1257 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
1258 * controls if special handling is needed (swapping last sector).
1259 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001260 last_sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
David Vinczeba3bd602019-06-17 16:01:43 +02001261 if ((img_off + sz) >
Fabio Utzig10ee6482019-08-01 12:04:52 -03001262 boot_img_sector_off(state, BOOT_PRIMARY_SLOT, last_sector)) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001263 copy_sz -= trailer_sz;
1264 }
1265
Fabio Utzig39000012018-07-30 12:40:20 -03001266 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001267
Fabio Utzig10ee6482019-08-01 12:04:52 -03001268 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001269
1270 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1271 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001272 assert (rc == 0);
1273
Fabio Utzigb0f04732019-07-31 09:49:19 -03001274 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1275 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001276 assert (rc == 0);
1277
1278 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch);
1279 assert (rc == 0);
1280
Fabio Utzig39000012018-07-30 12:40:20 -03001281 if (bs->state == BOOT_STATUS_STATE_0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001282 BOOT_LOG_DBG("erasing scratch area");
Christopher Collinsa1c12042019-05-23 14:00:28 -07001283 rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size);
Christopher Collins4772ac42017-02-27 20:08:01 -08001284 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001285
Fabio Utzig39000012018-07-30 12:40:20 -03001286 if (bs->idx == BOOT_STATUS_IDX_0) {
Christopher Collinsa1c12042019-05-23 14:00:28 -07001287 /* Write a trailer to the scratch area, even if we don't need the
1288 * scratch area for status. We need a temporary place to store the
1289 * `swap-type` while we erase the primary trailer.
1290 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001291 rc = boot_status_init(state, fap_scratch, bs);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001292 assert(rc == 0);
1293
1294 if (!bs->use_scratch) {
1295 /* Prepare the primary status area... here it is known that the
1296 * last sector is not being used by the image data so it's safe
1297 * to erase.
Fabio Utzig2473ac02017-05-02 12:45:02 -03001298 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001299 rc = boot_erase_trailer_sectors(state, fap_primary_slot);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001300 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001301
Fabio Utzig10ee6482019-08-01 12:04:52 -03001302 rc = boot_status_init(state, fap_primary_slot, bs);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001303 assert(rc == 0);
1304
1305 /* Erase the temporary trailer from the scratch area. */
1306 rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size);
1307 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001308 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001309 }
1310
Fabio Utzig10ee6482019-08-01 12:04:52 -03001311 rc = boot_copy_sector(state, fap_secondary_slot, fap_scratch,
Christopher Collinsa1c12042019-05-23 14:00:28 -07001312 img_off, 0, copy_sz);
1313 assert(rc == 0);
1314
Fabio Utzig39000012018-07-30 12:40:20 -03001315 bs->state = BOOT_STATUS_STATE_1;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001316 rc = boot_write_status(state, bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001317 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001318 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001319
Fabio Utzig39000012018-07-30 12:40:20 -03001320 if (bs->state == BOOT_STATUS_STATE_1) {
David Vincze2d736ad2019-02-18 11:50:22 +01001321 rc = boot_erase_sector(fap_secondary_slot, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001322 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001323
Fabio Utzig10ee6482019-08-01 12:04:52 -03001324 rc = boot_copy_sector(state, fap_primary_slot, fap_secondary_slot,
David Vincze2d736ad2019-02-18 11:50:22 +01001325 img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001326 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001327
Fabio Utzig39000012018-07-30 12:40:20 -03001328 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001329 /* If not all sectors of the slot are being swapped,
David Vincze2d736ad2019-02-18 11:50:22 +01001330 * guarantee here that only the primary slot will have the state.
Fabio Utzig2473ac02017-05-02 12:45:02 -03001331 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001332 rc = boot_erase_trailer_sectors(state, fap_secondary_slot);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001333 assert(rc == 0);
1334 }
1335
Fabio Utzig39000012018-07-30 12:40:20 -03001336 bs->state = BOOT_STATUS_STATE_2;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001337 rc = boot_write_status(state, bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001338 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001339 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001340
Fabio Utzig39000012018-07-30 12:40:20 -03001341 if (bs->state == BOOT_STATUS_STATE_2) {
David Vincze2d736ad2019-02-18 11:50:22 +01001342 rc = boot_erase_sector(fap_primary_slot, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001343 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001344
Christopher Collinsa1c12042019-05-23 14:00:28 -07001345 /* NOTE: If this is the final sector, we exclude the image trailer from
1346 * this copy (copy_sz was truncated earlier).
1347 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001348 rc = boot_copy_sector(state, fap_scratch, fap_primary_slot,
David Vincze2d736ad2019-02-18 11:50:22 +01001349 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001350 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001351
Fabio Utzig94d998c2017-05-22 11:02:41 -04001352 if (bs->use_scratch) {
Fabio Utzigba829042018-09-18 08:29:34 -03001353 scratch_trailer_off = boot_status_off(fap_scratch);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001354
1355 /* copy current status that is being maintained in scratch */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001356 rc = boot_copy_sector(state, fap_scratch, fap_primary_slot,
David Vincze2d736ad2019-02-18 11:50:22 +01001357 scratch_trailer_off, img_off + copy_sz,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001358 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(state));
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001359 BOOT_STATUS_ASSERT(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001360
Fabio Utzig2473ac02017-05-02 12:45:02 -03001361 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1362 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001363 assert(rc == 0);
1364
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001365 if (swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze2d736ad2019-02-18 11:50:22 +01001366 rc = boot_write_image_ok(fap_primary_slot);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001367 assert(rc == 0);
1368 }
1369
Christopher Collinsa1c12042019-05-23 14:00:28 -07001370 if (swap_state.swap_type != BOOT_SWAP_TYPE_NONE) {
David Vinczee2453472019-06-17 12:31:59 +02001371 rc = boot_write_swap_info(fap_primary_slot,
Fabio Utzigb0f04732019-07-31 09:49:19 -03001372 swap_state.swap_type, image_index);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001373 assert(rc == 0);
1374 }
1375
David Vincze2d736ad2019-02-18 11:50:22 +01001376 rc = boot_write_swap_size(fap_primary_slot, bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001377 assert(rc == 0);
1378
Fabio Utzigba829042018-09-18 08:29:34 -03001379#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +01001380 rc = boot_write_enc_key(fap_primary_slot, 0, bs->enckey[0]);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001381 assert(rc == 0);
1382
David Vincze2d736ad2019-02-18 11:50:22 +01001383 rc = boot_write_enc_key(fap_primary_slot, 1, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001384 assert(rc == 0);
1385#endif
David Vincze2d736ad2019-02-18 11:50:22 +01001386 rc = boot_write_magic(fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001387 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001388 }
1389
Christopher Collinsa1c12042019-05-23 14:00:28 -07001390 /* If we wrote a trailer to the scratch area, erase it after we persist
1391 * a trailer to the primary slot. We do this to prevent mcuboot from
1392 * reading a stale status from the scratch area in case of immediate
1393 * reset.
1394 */
1395 erase_scratch = bs->use_scratch;
1396 bs->use_scratch = 0;
1397
Christopher Collins92ea77f2016-12-12 15:59:26 -08001398 bs->idx++;
Fabio Utzig39000012018-07-30 12:40:20 -03001399 bs->state = BOOT_STATUS_STATE_0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001400 rc = boot_write_status(state, bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001401 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001402
1403 if (erase_scratch) {
1404 rc = boot_erase_sector(fap_scratch, 0, sz);
1405 assert(rc == 0);
1406 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001407 }
Fabio Utzigba829042018-09-18 08:29:34 -03001408
David Vincze2d736ad2019-02-18 11:50:22 +01001409 flash_area_close(fap_primary_slot);
1410 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001411 flash_area_close(fap_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001412}
Fabio Utzig3488eef2017-06-12 10:25:43 -03001413#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001414
1415/**
David Vincze2d736ad2019-02-18 11:50:22 +01001416 * Overwrite primary slot with the image contained in the secondary slot.
1417 * If a prior copy operation was interrupted by a system reset, this function
1418 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001419 *
1420 * @param bs The current boot status. This function reads
1421 * this struct to determine if it is resuming
1422 * an interrupted swap operation. This
1423 * function writes the updated status to this
1424 * function on return.
1425 *
1426 * @return 0 on success; nonzero on failure.
1427 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001428#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001429static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001430boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
David Brown17609d82017-05-05 09:41:34 -06001431{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001432 size_t sect_count;
1433 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001434 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001435 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001436 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001437 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +01001438 const struct flash_area *fap_primary_slot;
1439 const struct flash_area *fap_secondary_slot;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001440 uint8_t image_index;
David Vincze2d736ad2019-02-18 11:50:22 +01001441
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001442 (void)bs;
1443
Fabio Utzig13d9e352017-10-05 20:32:31 -03001444#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1445 uint32_t src_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001446 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT,
1447 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
David Vincze2d736ad2019-02-18 11:50:22 +01001448 &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001449 assert(rc == 0);
1450#endif
David Brown17609d82017-05-05 09:41:34 -06001451
David Vincze2d736ad2019-02-18 11:50:22 +01001452 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1453 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -06001454
Fabio Utzigb0f04732019-07-31 09:49:19 -03001455 image_index = BOOT_CURR_IMG(state);
1456
1457 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1458 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001459 assert (rc == 0);
1460
Fabio Utzigb0f04732019-07-31 09:49:19 -03001461 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1462 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001463 assert (rc == 0);
1464
Fabio Utzig10ee6482019-08-01 12:04:52 -03001465 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001466 for (sect = 0, size = 0; sect < sect_count; sect++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001467 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
David Vincze2d736ad2019-02-18 11:50:22 +01001468 rc = boot_erase_sector(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001469 assert(rc == 0);
1470
1471 size += this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001472
1473#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1474 if (size >= src_size) {
1475 break;
1476 }
1477#endif
David Brown17609d82017-05-05 09:41:34 -06001478 }
1479
Fabio Utzigba829042018-09-18 08:29:34 -03001480#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001481 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
1482 rc = boot_enc_load(state->enc, image_index,
1483 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
Fabio Utzigb0f04732019-07-31 09:49:19 -03001484 fap_secondary_slot, bs->enckey[1]);
David Vincze2d736ad2019-02-18 11:50:22 +01001485
Fabio Utzigba829042018-09-18 08:29:34 -03001486 if (rc < 0) {
1487 return BOOT_EBADIMAGE;
1488 }
Fabio Utzig10ee6482019-08-01 12:04:52 -03001489 if (rc == 0 && boot_enc_set_key(state->enc, 1, bs->enckey[1])) {
Fabio Utzigba829042018-09-18 08:29:34 -03001490 return BOOT_EBADIMAGE;
1491 }
1492 }
1493#endif
1494
David Vincze2d736ad2019-02-18 11:50:22 +01001495 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1496 size);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001497 rc = boot_copy_sector(state, fap_secondary_slot, fap_primary_slot, 0, 0, size);
David Brown17609d82017-05-05 09:41:34 -06001498
Fabio Utzig13d9e352017-10-05 20:32:31 -03001499 /*
1500 * Erases header and trailer. The trailer is erased because when a new
1501 * image is written without a trailer as is the case when using newt, the
1502 * trailer that was left might trigger a new upgrade.
1503 */
Christopher Collins2c88e692019-05-22 15:10:14 -07001504 BOOT_LOG_DBG("erasing secondary header");
David Vincze2d736ad2019-02-18 11:50:22 +01001505 rc = boot_erase_sector(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001506 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
1507 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001508 assert(rc == 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001509 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -07001510 BOOT_LOG_DBG("erasing secondary trailer");
David Vincze2d736ad2019-02-18 11:50:22 +01001511 rc = boot_erase_sector(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001512 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
1513 last_sector),
1514 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
1515 last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001516 assert(rc == 0);
1517
David Vincze2d736ad2019-02-18 11:50:22 +01001518 flash_area_close(fap_primary_slot);
1519 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001520
David Vincze2d736ad2019-02-18 11:50:22 +01001521 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001522
1523 return 0;
1524}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001525#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001526
Christopher Collinsa1c12042019-05-23 14:00:28 -07001527#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -03001528/**
1529 * Swaps the two images in flash. If a prior copy operation was interrupted
1530 * by a system reset, this function completes that operation.
1531 *
1532 * @param bs The current boot status. This function reads
1533 * this struct to determine if it is resuming
1534 * an interrupted swap operation. This
1535 * function writes the updated status to this
1536 * function on return.
1537 *
1538 * @return 0 on success; nonzero on failure.
1539 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001540static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001541boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001542{
1543 uint32_t sz;
1544 int first_sector_idx;
1545 int last_sector_idx;
David Vincze2d736ad2019-02-18 11:50:22 +01001546 int last_idx_secondary_slot;
Fabio Utzigcd5774b2017-11-29 10:18:26 -02001547 uint32_t swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001548 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001549#ifdef MCUBOOT_ENC_IMAGES
1550 const struct flash_area *fap;
1551 uint8_t slot;
1552 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001553#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001554 uint32_t size;
1555 uint32_t copy_size;
David Vincze2d736ad2019-02-18 11:50:22 +01001556 uint32_t primary_slot_size;
1557 uint32_t secondary_slot_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001558 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001559 int rc;
1560
1561 /* FIXME: just do this if asked by user? */
1562
1563 size = copy_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001564 image_index = BOOT_CURR_IMG(state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001565
Fabio Utzig39000012018-07-30 12:40:20 -03001566 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Fabio Utzig46490722017-09-04 15:34:32 -03001567 /*
1568 * No swap ever happened, so need to find the largest image which
1569 * will be used to determine the amount of sectors to swap.
1570 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001571 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001572 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001573 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, hdr, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001574 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001575 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001576
Fabio Utzigba829042018-09-18 08:29:34 -03001577#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001578 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001579 fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
1580 rc = boot_enc_load(state->enc, image_index, hdr, fap, bs->enckey[0]);
Fabio Utzigba829042018-09-18 08:29:34 -03001581 assert(rc >= 0);
1582
1583 if (rc == 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001584 rc = boot_enc_set_key(state->enc, 0, bs->enckey[0]);
Fabio Utzigba829042018-09-18 08:29:34 -03001585 assert(rc == 0);
1586 } else {
1587 rc = 0;
1588 }
1589 } else {
1590 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1591 }
1592#endif
1593
Fabio Utzig10ee6482019-08-01 12:04:52 -03001594 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001595 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001596 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, hdr, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001597 assert(rc == 0);
1598 }
1599
Fabio Utzigba829042018-09-18 08:29:34 -03001600#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001601 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001602 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001603 fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
1604 rc = boot_enc_load(state->enc, image_index, hdr, fap, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001605 assert(rc >= 0);
1606
1607 if (rc == 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001608 rc = boot_enc_set_key(state->enc, 1, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001609 assert(rc == 0);
1610 } else {
1611 rc = 0;
1612 }
1613 } else {
1614 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1615 }
1616#endif
1617
Fabio Utzig46490722017-09-04 15:34:32 -03001618 if (size > copy_size) {
1619 copy_size = size;
1620 }
1621
1622 bs->swap_size = copy_size;
1623 } else {
1624 /*
1625 * If a swap was under way, the swap_size should already be present
1626 * in the trailer...
1627 */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001628 rc = boot_read_swap_size(image_index, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001629 assert(rc == 0);
1630
1631 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001632
1633#ifdef MCUBOOT_ENC_IMAGES
1634 for (slot = 0; slot <= 1; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03001635 rc = boot_read_enc_key(image_index, slot, bs->enckey[slot]);
Fabio Utzigba829042018-09-18 08:29:34 -03001636 assert(rc == 0);
1637
1638 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001639 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001640 break;
1641 }
1642 }
1643
1644 if (i != BOOT_ENC_KEY_SIZE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001645 boot_enc_set_key(state->enc, slot, bs->enckey[slot]);
Fabio Utzigba829042018-09-18 08:29:34 -03001646 }
1647 }
1648#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001649 }
1650
David Vincze2d736ad2019-02-18 11:50:22 +01001651 primary_slot_size = 0;
1652 secondary_slot_size = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001653 last_sector_idx = 0;
David Vincze2d736ad2019-02-18 11:50:22 +01001654 last_idx_secondary_slot = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001655
1656 /*
1657 * Knowing the size of the largest image between both slots, here we
David Vincze2d736ad2019-02-18 11:50:22 +01001658 * find what is the last sector in the primary slot that needs swapping.
1659 * Since we already know that both slots are compatible, the secondary
1660 * slot's last sector is not really required after this check is finished.
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001661 */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001662 while (1) {
David Vincze2d736ad2019-02-18 11:50:22 +01001663 if ((primary_slot_size < copy_size) ||
1664 (primary_slot_size < secondary_slot_size)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001665 primary_slot_size += boot_img_sector_size(state,
David Vincze2d736ad2019-02-18 11:50:22 +01001666 BOOT_PRIMARY_SLOT,
1667 last_sector_idx);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001668 }
David Vincze2d736ad2019-02-18 11:50:22 +01001669 if ((secondary_slot_size < copy_size) ||
1670 (secondary_slot_size < primary_slot_size)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001671 secondary_slot_size += boot_img_sector_size(state,
David Vincze2d736ad2019-02-18 11:50:22 +01001672 BOOT_SECONDARY_SLOT,
1673 last_idx_secondary_slot);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001674 }
David Vincze2d736ad2019-02-18 11:50:22 +01001675 if (primary_slot_size >= copy_size &&
1676 secondary_slot_size >= copy_size &&
1677 primary_slot_size == secondary_slot_size) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001678 break;
1679 }
1680 last_sector_idx++;
David Vincze2d736ad2019-02-18 11:50:22 +01001681 last_idx_secondary_slot++;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001682 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001683
1684 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001685 while (last_sector_idx >= 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001686 sz = boot_copy_sz(state, last_sector_idx, &first_sector_idx);
Fabio Utzig39000012018-07-30 12:40:20 -03001687 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001688 boot_swap_sectors(first_sector_idx, sz, state, bs);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001689 }
1690
1691 last_sector_idx = first_sector_idx - 1;
1692 swap_idx++;
1693 }
1694
David Vincze2d736ad2019-02-18 11:50:22 +01001695#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001696 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001697 BOOT_LOG_WRN("%d status write fails performing the swap",
1698 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001699 }
1700#endif
1701
Christopher Collins92ea77f2016-12-12 15:59:26 -08001702 return 0;
1703}
David Brown17609d82017-05-05 09:41:34 -06001704#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001705
1706/**
David Vincze2d736ad2019-02-18 11:50:22 +01001707 * Marks the image in the primary slot as fully copied.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001708 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001709#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001710static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001711boot_set_copy_done(uint8_t image_index)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001712{
1713 const struct flash_area *fap;
1714 int rc;
1715
Fabio Utzigb0f04732019-07-31 09:49:19 -03001716 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1717 &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001718 if (rc != 0) {
1719 return BOOT_EFLASH;
1720 }
1721
1722 rc = boot_write_copy_done(fap);
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001723 flash_area_close(fap);
1724 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001725}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001726#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001727
1728/**
David Vincze2d736ad2019-02-18 11:50:22 +01001729 * Marks a reverted image in the primary slot as confirmed. This is necessary to
1730 * ensure the status bytes from the image revert operation don't get processed
1731 * on a subsequent boot.
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001732 *
1733 * NOTE: image_ok is tested before writing because if there's a valid permanent
David Vincze2d736ad2019-02-18 11:50:22 +01001734 * image installed on the primary slot and the new image to be upgrade to has a
1735 * bad sig, image_ok would be overwritten.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001736 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001737#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001738static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001739boot_set_image_ok(uint8_t image_index)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001740{
1741 const struct flash_area *fap;
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001742 struct boot_swap_state state;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001743 int rc;
1744
Fabio Utzigb0f04732019-07-31 09:49:19 -03001745 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1746 &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001747 if (rc != 0) {
1748 return BOOT_EFLASH;
1749 }
1750
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001751 rc = boot_read_swap_state(fap, &state);
1752 if (rc != 0) {
1753 rc = BOOT_EFLASH;
1754 goto out;
1755 }
1756
1757 if (state.image_ok == BOOT_FLAG_UNSET) {
1758 rc = boot_write_image_ok(fap);
1759 }
1760
1761out:
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001762 flash_area_close(fap);
1763 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001764}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001765#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001766
David Vinczee32483f2019-06-13 10:46:24 +02001767#if (BOOT_IMAGE_NUMBER > 1)
1768/**
1769 * Check the image dependency whether it is satisfied and modify
1770 * the swap type if necessary.
1771 *
1772 * @param dep Image dependency which has to be verified.
1773 *
1774 * @return 0 on success; nonzero on failure.
1775 */
1776static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001777boot_verify_single_dependency(struct boot_loader_state *state,
1778 struct image_dependency *dep)
David Vinczee32483f2019-06-13 10:46:24 +02001779{
1780 struct image_version *dep_version;
1781 size_t dep_slot;
1782 int rc;
1783
1784 /* Determine the source of the image which is the subject of
1785 * the dependency and get it's version. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001786 dep_slot = (state->swap_type[dep->image_id] != BOOT_SWAP_TYPE_NONE) ?
David Vinczee32483f2019-06-13 10:46:24 +02001787 BOOT_SECONDARY_SLOT : BOOT_PRIMARY_SLOT;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001788 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +02001789
1790 rc = boot_is_version_sufficient(&dep->image_min_version, dep_version);
1791 if (rc != 0) {
1792 /* Dependency not satisfied.
1793 * Modify the swap type to decrease the version number of the image
1794 * (which will be located in the primary slot after the boot process),
1795 * consequently the number of unsatisfied dependencies will be
1796 * decreased or remain the same.
1797 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001798 switch (BOOT_SWAP_TYPE(state)) {
David Vinczee32483f2019-06-13 10:46:24 +02001799 case BOOT_SWAP_TYPE_TEST:
1800 case BOOT_SWAP_TYPE_PERM:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001801 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczee32483f2019-06-13 10:46:24 +02001802 break;
1803 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001804 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczee32483f2019-06-13 10:46:24 +02001805 break;
1806 default:
1807 break;
1808 }
1809 }
1810
1811 return rc;
1812}
1813
1814/**
1815 * Read all dependency TLVs of an image from the flash and verify
1816 * one after another to see if they are all satisfied.
1817 *
1818 * @param slot Image slot number.
1819 *
1820 * @return 0 on success; nonzero on failure.
1821 */
1822static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001823boot_verify_all_dependency(struct boot_loader_state *state, uint32_t slot)
David Vinczee32483f2019-06-13 10:46:24 +02001824{
1825 const struct flash_area *fap;
1826 struct image_header *hdr;
1827 struct image_tlv_info info;
1828 struct image_tlv tlv;
1829 struct image_dependency dep;
1830 uint32_t off;
1831 uint32_t end;
1832 bool dep_tlvs_found = false;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001833 int area_id;
David Vinczee32483f2019-06-13 10:46:24 +02001834 int rc;
1835
Fabio Utzig10ee6482019-08-01 12:04:52 -03001836 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001837 rc = flash_area_open(area_id, &fap);
David Vinczee32483f2019-06-13 10:46:24 +02001838 if (rc != 0) {
1839 rc = BOOT_EFLASH;
1840 goto done;
1841 }
1842
Fabio Utzig10ee6482019-08-01 12:04:52 -03001843 hdr = boot_img_hdr(state, slot);
David Vinczee32483f2019-06-13 10:46:24 +02001844 /* The TLVs come after the image. */
1845 off = hdr->ih_hdr_size + hdr->ih_img_size;
1846
1847 /* The TLV area always starts with an image_tlv_info structure. */
1848 rc = flash_area_read(fap, off, &info, sizeof(info));
1849 if (rc != 0) {
1850 rc = BOOT_EFLASH;
1851 goto done;
1852 }
1853
1854 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
1855 rc = BOOT_EBADIMAGE;
1856 goto done;
1857 }
1858 end = off + info.it_tlv_tot;
1859 off += sizeof(info);
1860
1861 /* Traverse through all of the TLVs to find the dependency TLVs. */
1862 for (; off < end; off += sizeof(tlv) + tlv.it_len) {
1863 rc = flash_area_read(fap, off, &tlv, sizeof(tlv));
1864 if (rc != 0) {
1865 rc = BOOT_EFLASH;
1866 goto done;
1867 }
1868
1869 if (tlv.it_type == IMAGE_TLV_DEPENDENCY) {
1870 if (!dep_tlvs_found) {
1871 dep_tlvs_found = true;
1872 }
1873
1874 if (tlv.it_len != sizeof(dep)) {
1875 rc = BOOT_EBADIMAGE;
1876 goto done;
1877 }
1878
1879 rc = flash_area_read(fap, off + sizeof(tlv), &dep, tlv.it_len);
1880 if (rc != 0) {
1881 rc = BOOT_EFLASH;
1882 goto done;
1883 }
1884
1885 /* Verify dependency and modify the swap type if not satisfied. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001886 rc = boot_verify_single_dependency(state, &dep);
David Vinczee32483f2019-06-13 10:46:24 +02001887 if (rc != 0) {
1888 /* Dependency not satisfied. */
1889 goto done;
1890 }
1891
1892 /* Dependency satisfied, no action needed.
1893 * Continue with the next TLV entry.
1894 */
1895 } else if (dep_tlvs_found) {
1896 /* The dependency TLVs are contiguous in the TLV area. If a
1897 * dependency had already been found and the last read TLV
1898 * has a different type then there are no more dependency TLVs.
1899 * The search can be finished.
1900 */
1901 break;
1902 }
1903 }
1904
1905done:
1906 flash_area_close(fap);
1907 return rc;
1908}
1909
1910/**
1911 * Verify whether the image dependencies in the TLV area are
1912 * all satisfied and modify the swap type if necessary.
1913 *
1914 * @return 0 if all dependencies are satisfied,
1915 * nonzero otherwise.
1916 */
1917static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001918boot_verify_single_image_dependency(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001919{
1920 size_t slot;
1921
1922 /* Determine the source of the dependency TLVs. Those dependencies have to
1923 * be checked which belong to the image that will be located in the primary
1924 * slot after the firmware update process.
1925 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001926 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1927 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
David Vinczee32483f2019-06-13 10:46:24 +02001928 slot = BOOT_SECONDARY_SLOT;
1929 } else {
1930 slot = BOOT_PRIMARY_SLOT;
1931 }
1932
Fabio Utzig10ee6482019-08-01 12:04:52 -03001933 return boot_verify_all_dependency(state, slot);
David Vinczee32483f2019-06-13 10:46:24 +02001934}
1935
1936/**
1937 * Iterate over all the images and verify whether the image dependencies in the
1938 * TLV area are all satisfied and update the related swap type if necessary.
1939 */
1940static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001941boot_verify_all_image_dependency(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001942{
David Vinczee32483f2019-06-13 10:46:24 +02001943 int rc;
1944
Fabio Utzig10ee6482019-08-01 12:04:52 -03001945 BOOT_CURR_IMG(state) = 0;
1946 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
1947 rc = boot_verify_single_image_dependency(state);
Fabio Utzigabec0732019-07-31 08:40:22 -03001948 if (rc == 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001949 /* All dependencies've been satisfied, continue with next image. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001950 BOOT_CURR_IMG(state)++;
David Vinczee32483f2019-06-13 10:46:24 +02001951 } else if (rc == BOOT_EBADVERSION) {
1952 /* Dependency check needs to be restarted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001953 BOOT_CURR_IMG(state) = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001954 } else {
1955 /* Other error happened, images are inconsistent */
1956 return;
1957 }
1958 }
1959}
1960#endif /* (BOOT_IMAGE_NUMBER > 1) */
1961
Christopher Collins92ea77f2016-12-12 15:59:26 -08001962/**
David Vinczeba3bd602019-06-17 16:01:43 +02001963 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001964 *
David Vinczeba3bd602019-06-17 16:01:43 +02001965 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001966 *
1967 * @return 0 on success; nonzero on failure.
1968 */
1969static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001970boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001971{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001972 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001973#ifndef MCUBOOT_OVERWRITE_ONLY
1974 uint8_t swap_type;
1975#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001976
David Vinczeba3bd602019-06-17 16:01:43 +02001977 /* At this point there are no aborted swaps. */
1978#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001979 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001980#elif defined(MCUBOOT_BOOTSTRAP)
1981 /* Check if the image update was triggered by a bad image in the
1982 * primary slot (the validity of the image in the secondary slot had
1983 * already been checked).
1984 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001985 if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 ||
1986 boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) {
1987 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001988 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001989 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001990 }
1991#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03001992 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001993#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001994 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001995
1996#ifndef MCUBOOT_OVERWRITE_ONLY
1997 /* The following state needs image_ok be explicitly set after the
1998 * swap was finished to avoid a new revert.
1999 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002000 swap_type = BOOT_SWAP_TYPE(state);
2001 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
2002 swap_type == BOOT_SWAP_TYPE_PERM) {
2003 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002004 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002005 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002006 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002007 }
2008
Fabio Utzig10ee6482019-08-01 12:04:52 -03002009 if (swap_type == BOOT_SWAP_TYPE_TEST ||
2010 swap_type == BOOT_SWAP_TYPE_PERM ||
2011 swap_type == BOOT_SWAP_TYPE_REVERT) {
2012 rc = boot_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002013 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002014 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07002015 }
David Vinczeba3bd602019-06-17 16:01:43 +02002016 }
2017#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02002018
David Vinczeba3bd602019-06-17 16:01:43 +02002019 return rc;
2020}
2021
2022/**
2023 * Completes a previously aborted image swap.
2024 *
2025 * @param bs The current boot status.
2026 *
2027 * @return 0 on success; nonzero on failure.
2028 */
2029#if !defined(MCUBOOT_OVERWRITE_ONLY)
2030static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03002031boot_complete_partial_swap(struct boot_loader_state *state,
2032 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02002033{
2034 int rc;
2035
2036 /* Determine the type of swap operation being resumed from the
2037 * `swap-type` trailer field.
2038 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002039 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002040 assert(rc == 0);
2041
Fabio Utzig10ee6482019-08-01 12:04:52 -03002042 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02002043
2044 /* The following states need image_ok be explicitly set after the
2045 * swap was finished to avoid a new revert.
2046 */
2047 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
2048 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002049 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002050 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002051 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002052 }
2053 }
2054
2055 if (bs->swap_type == BOOT_SWAP_TYPE_TEST ||
2056 bs->swap_type == BOOT_SWAP_TYPE_PERM ||
2057 bs->swap_type == BOOT_SWAP_TYPE_REVERT) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002058 rc = boot_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002059 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002060 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002061 }
2062 }
2063
Fabio Utzig10ee6482019-08-01 12:04:52 -03002064 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002065 BOOT_LOG_ERR("panic!");
2066 assert(0);
2067
2068 /* Loop forever... */
2069 while (1) {}
2070 }
2071
2072 return rc;
2073}
2074#endif /* !MCUBOOT_OVERWRITE_ONLY */
2075
2076#if (BOOT_IMAGE_NUMBER > 1)
2077/**
2078 * Review the validity of previously determined swap types of other images.
2079 *
2080 * @param aborted_swap The current image upgrade is a
2081 * partial/aborted swap.
2082 */
2083static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03002084boot_review_image_swap_types(struct boot_loader_state *state,
2085 bool aborted_swap)
David Vinczeba3bd602019-06-17 16:01:43 +02002086{
2087 /* In that case if we rebooted in the middle of an image upgrade process, we
2088 * must review the validity of swap types, that were previously determined
2089 * for other images. The image_ok flag had not been set before the reboot
2090 * for any of the updated images (only the copy_done flag) and thus falsely
2091 * the REVERT swap type has been determined for the previous images that had
2092 * been updated before the reboot.
2093 *
2094 * There are two separate scenarios that we have to deal with:
2095 *
2096 * 1. The reboot has happened during swapping an image:
2097 * The current image upgrade has been determined as a
2098 * partial/aborted swap.
2099 * 2. The reboot has happened between two separate image upgrades:
2100 * In this scenario we must check the swap type of the current image.
2101 * In those cases if it is NONE or REVERT we cannot certainly determine
2102 * the fact of a reboot. In a consistent state images must move in the
2103 * same direction or stay in place, e.g. in practice REVERT and TEST
2104 * swap types cannot be present at the same time. If the swap type of
2105 * the current image is either TEST, PERM or FAIL we must review the
2106 * already determined swap types of other images and set each false
2107 * REVERT swap types to NONE (these images had been successfully
2108 * updated before the system rebooted between two separate image
2109 * upgrades).
2110 */
2111
Fabio Utzig10ee6482019-08-01 12:04:52 -03002112 if (BOOT_CURR_IMG(state) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02002113 /* Nothing to do */
2114 return;
2115 }
2116
2117 if (!aborted_swap) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002118 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
2119 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002120 /* Nothing to do */
2121 return;
2122 }
2123 }
2124
Fabio Utzig10ee6482019-08-01 12:04:52 -03002125 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
2126 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
2127 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002128 }
2129 }
2130}
2131#endif
2132
2133/**
2134 * Prepare image to be updated if required.
2135 *
2136 * Prepare image to be updated if required with completing an image swap
2137 * operation if one was aborted and/or determining the type of the
2138 * swap operation. In case of any error set the swap type to NONE.
2139 *
Fabio Utzig10ee6482019-08-01 12:04:52 -03002140 * @param state TODO
David Vinczeba3bd602019-06-17 16:01:43 +02002141 * @param bs Pointer where the read and possibly updated
2142 * boot status can be written to.
2143 */
2144static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03002145boot_prepare_image_for_update(struct boot_loader_state *state,
2146 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02002147{
2148 int rc;
2149
2150 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002151 rc = boot_read_sectors(state);
David Vinczeba3bd602019-06-17 16:01:43 +02002152 if (rc != 0) {
2153 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
2154 " - too small?", BOOT_MAX_IMG_SECTORS);
2155 /* Unable to determine sector layout, continue with next image
2156 * if there is one.
2157 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002158 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002159 return;
2160 }
2161
2162 /* Attempt to read an image header from each slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002163 rc = boot_read_image_headers(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02002164 if (rc != 0) {
2165 /* Continue with next image if there is one. */
Fabio Utzigb0f04732019-07-31 09:49:19 -03002166 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03002167 BOOT_CURR_IMG(state));
2168 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002169 return;
2170 }
2171
2172 /* If the current image's slots aren't compatible, no swap is possible.
2173 * Just boot into primary slot.
2174 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002175 if (boot_slots_compatible(state)) {
2176 rc = boot_read_status(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002177 if (rc != 0) {
2178 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03002179 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002180 /* Continue with next image if there is one. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002181 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002182 return;
2183 }
2184
2185 /* Determine if we rebooted in the middle of an image swap
2186 * operation. If a partial swap was detected, complete it.
2187 */
2188 if (bs->idx != BOOT_STATUS_IDX_0 || bs->state != BOOT_STATUS_STATE_0) {
2189
2190#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03002191 boot_review_image_swap_types(state, true);
David Vinczeba3bd602019-06-17 16:01:43 +02002192#endif
2193
2194#ifdef MCUBOOT_OVERWRITE_ONLY
2195 /* Should never arrive here, overwrite-only mode has
2196 * no swap state.
2197 */
2198 assert(0);
2199#else
2200 /* Determine the type of swap operation being resumed from the
2201 * `swap-type` trailer field.
2202 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002203 rc = boot_complete_partial_swap(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002204 assert(rc == 0);
2205#endif
2206 /* Attempt to read an image header from each slot. Ensure that
2207 * image headers in slots are aligned with headers in boot_data.
2208 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002209 rc = boot_read_image_headers(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02002210 assert(rc == 0);
2211
2212 /* Swap has finished set to NONE */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002213 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002214 } else {
2215 /* There was no partial swap, determine swap type. */
2216 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002217 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
2218 } else if (boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) != 0) {
2219 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
David Vinczeba3bd602019-06-17 16:01:43 +02002220 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002221 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02002222 }
2223
2224#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03002225 boot_review_image_swap_types(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02002226#endif
2227
2228#ifdef MCUBOOT_BOOTSTRAP
Fabio Utzig10ee6482019-08-01 12:04:52 -03002229 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02002230 /* Header checks are done first because they are
2231 * inexpensive. Since overwrite-only copies starting from
2232 * offset 0, if interrupted, it might leave a valid header
2233 * magic, so also run validation on the primary slot to be
2234 * sure it's not OK.
2235 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002236 if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 ||
2237 boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) {
2238 if (boot_img_hdr(state,
David Vinczeba3bd602019-06-17 16:01:43 +02002239 BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC &&
Fabio Utzig10ee6482019-08-01 12:04:52 -03002240 boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) == 0)
David Vinczeba3bd602019-06-17 16:01:43 +02002241 {
2242 /* Set swap type to REVERT to overwrite the primary
2243 * slot with the image contained in secondary slot
2244 * and to trigger the explicit setting of the
2245 * image_ok flag.
2246 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002247 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02002248 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02002249 }
2250 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02002251#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002252 }
David Vinczeba3bd602019-06-17 16:01:43 +02002253 } else {
2254 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002255 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002256 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002257}
2258
Christopher Collins92ea77f2016-12-12 15:59:26 -08002259int
Fabio Utzig10ee6482019-08-01 12:04:52 -03002260context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08002261{
Marti Bolivar84898652017-06-13 17:20:22 -04002262 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02002263 struct boot_status bs;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002264 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002265 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03002266 int image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002267
2268 /* The array of slot sectors are defined here (as opposed to file scope) so
2269 * that they don't get allocated for non-boot-loader apps. This is
2270 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002271 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08002272 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002273 TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
2274 TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
2275 TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Christopher Collins92ea77f2016-12-12 15:59:26 -08002276
Fabio Utzig10ee6482019-08-01 12:04:52 -03002277 memset(state, 0, sizeof(struct boot_loader_state));
Fabio Utzigba829042018-09-18 08:29:34 -03002278
David Vinczeba3bd602019-06-17 16:01:43 +02002279 /* Iterate over all the images. By the end of the loop the swap type has
2280 * to be determined for each image and all aborted swaps have to be
2281 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002282 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002283 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03002284
David Vinczeba3bd602019-06-17 16:01:43 +02002285#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
2286 /* The keys used for encryption may no longer be valid (could belong to
2287 * another images). Therefore, mark them as invalid to force their reload
2288 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03002289 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002290 boot_enc_mark_keys_invalid(state->enc);
David Brown554c52e2017-06-30 16:01:07 -06002291#endif
2292
Fabio Utzig10ee6482019-08-01 12:04:52 -03002293 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03002294
Fabio Utzig10ee6482019-08-01 12:04:52 -03002295 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002296 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03002297 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002298 secondary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03002299 state->scratch.sectors = scratch_sectors;
David Vinczeba3bd602019-06-17 16:01:43 +02002300
2301 /* Open primary and secondary image areas for the duration
2302 * of this call.
2303 */
2304 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03002305 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03002306 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02002307 assert(rc == 0);
2308 }
2309 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03002310 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002311 assert(rc == 0);
2312
2313 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002314 boot_prepare_image_for_update(state, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002315 }
2316
David Vinczee32483f2019-06-13 10:46:24 +02002317#if (BOOT_IMAGE_NUMBER > 1)
2318 /* Iterate over all the images and verify whether the image dependencies
2319 * are all satisfied and update swap type if necessary.
2320 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002321 boot_verify_all_image_dependency(state);
David Vinczee32483f2019-06-13 10:46:24 +02002322#endif
2323
David Vinczeba3bd602019-06-17 16:01:43 +02002324 /* Iterate over all the images. At this point there are no aborted swaps
2325 * and the swap types are determined for each image. By the end of the loop
2326 * all required update operations will have been finished.
2327 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002328 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002329
2330#if (BOOT_IMAGE_NUMBER > 1)
2331#ifdef MCUBOOT_ENC_IMAGES
2332 /* The keys used for encryption may no longer be valid (could belong to
2333 * another images). Therefore, mark them as invalid to force their reload
2334 * by boot_enc_load().
2335 */
2336 boot_enc_mark_keys_invalid();
2337#endif /* MCUBOOT_ENC_IMAGES */
2338
2339 /* Indicate that swap is not aborted */
2340 memset(&bs, 0, sizeof bs);
2341 bs.idx = BOOT_STATUS_IDX_0;
2342 bs.state = BOOT_STATUS_STATE_0;
2343#endif /* (BOOT_IMAGE_NUMBER > 1) */
2344
2345 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002346 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02002347
Fabio Utzig10ee6482019-08-01 12:04:52 -03002348 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002349 case BOOT_SWAP_TYPE_NONE:
2350 break;
2351
2352 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
2353 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
2354 case BOOT_SWAP_TYPE_REVERT:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002355 rc = boot_perform_update(state, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002356 assert(rc == 0);
2357 break;
2358
2359 case BOOT_SWAP_TYPE_FAIL:
2360 /* The image in secondary slot was invalid and is now erased. Ensure
2361 * we don't try to boot into it again on the next reboot. Do this by
2362 * pretending we just reverted back to primary slot.
2363 */
2364#ifndef MCUBOOT_OVERWRITE_ONLY
2365 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002366 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002367 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002368 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002369 }
2370#endif /* !MCUBOOT_OVERWRITE_ONLY */
2371 break;
2372
2373 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002374 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002375 }
2376
Fabio Utzig10ee6482019-08-01 12:04:52 -03002377 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002378 BOOT_LOG_ERR("panic!");
2379 assert(0);
2380
2381 /* Loop forever... */
2382 while (1) {}
2383 }
2384 }
2385
2386 /* Iterate over all the images. At this point all required update operations
2387 * have finished. By the end of the loop each image in the primary slot will
2388 * have been re-validated.
2389 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002390 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2391 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02002392 /* Attempt to read an image header from each slot. Ensure that image
2393 * headers in slots are aligned with headers in boot_data.
2394 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002395 rc = boot_read_image_headers(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02002396 if (rc != 0) {
2397 goto out;
2398 }
2399 /* Since headers were reloaded, it can be assumed we just performed
2400 * a swap or overwrite. Now the header info that should be used to
2401 * provide the data for the bootstrap, which previously was at
2402 * secondary slot, was updated to primary slot.
2403 */
2404 }
2405
2406#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig10ee6482019-08-01 12:04:52 -03002407 rc = boot_validate_slot(state, BOOT_PRIMARY_SLOT, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02002408 if (rc != 0) {
2409 rc = BOOT_EBADIMAGE;
2410 goto out;
2411 }
2412#else
2413 /* Even if we're not re-validating the primary slot, we could be booting
2414 * onto an empty flash chip. At least do a basic sanity check that
2415 * the magic number on the image is OK.
2416 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002417 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002418 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
Fabio Utzig10ee6482019-08-01 12:04:52 -03002419 &boot_img_hdr(state,BOOT_PRIMARY_SLOT)->ih_magic,
2420 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002421 rc = BOOT_EBADIMAGE;
2422 goto out;
2423 }
2424#endif
2425 }
2426
Fabio Utzigb0f04732019-07-31 09:49:19 -03002427#if (BOOT_IMAGE_NUMBER > 1)
David Vinczeba3bd602019-06-17 16:01:43 +02002428 /* Always boot from the primary slot of Image 0. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002429 BOOT_CURR_IMG(state) = 0;
Fabio Utzigb0f04732019-07-31 09:49:19 -03002430#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -03002431 rsp->br_flash_dev_id = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)->fa_device_id;
2432 rsp->br_image_off = boot_img_slot_off(state, BOOT_PRIMARY_SLOT);
2433 rsp->br_hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002434
Marti Bolivarc0b47912017-06-13 17:18:09 -04002435 out:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002436 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2437 flash_area_close(BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002438 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002439 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
David Vinczeba3bd602019-06-17 16:01:43 +02002440 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04002441 }
2442 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002443}
2444
Fabio Utzig10ee6482019-08-01 12:04:52 -03002445/**
2446 * Prepares the booting process. This function moves images around in flash as
2447 * appropriate, and tells you what address to boot from.
2448 *
2449 * @param rsp On success, indicates how booting should occur.
2450 *
2451 * @return 0 on success; nonzero on failure.
2452 */
2453int
2454boot_go(struct boot_rsp *rsp)
2455{
2456 return context_boot_go(&boot_data, rsp);
2457}
2458
Christopher Collins92ea77f2016-12-12 15:59:26 -08002459int
2460split_go(int loader_slot, int split_slot, void **entry)
2461{
Marti Bolivarc50926f2017-06-14 09:35:40 -04002462 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08002463 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002464 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002465 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002466 int rc;
2467
Christopher Collins92ea77f2016-12-12 15:59:26 -08002468 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
2469 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04002470 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002471 }
David Vinczeba3bd602019-06-17 16:01:43 +02002472 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
2473 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002474
2475 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
2476 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07002477 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04002478 assert(rc == 0);
2479 split_flash_id = flash_area_id_from_image_slot(split_slot);
2480 rc = flash_area_open(split_flash_id,
2481 &BOOT_IMG_AREA(&boot_data, split_slot));
2482 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002483
2484 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002485 rc = boot_read_sectors(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002486 if (rc != 0) {
2487 rc = SPLIT_GO_ERR;
2488 goto done;
2489 }
2490
Fabio Utzig10ee6482019-08-01 12:04:52 -03002491 rc = boot_read_image_headers(&boot_data, true);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002492 if (rc != 0) {
2493 goto done;
2494 }
2495
Christopher Collins92ea77f2016-12-12 15:59:26 -08002496 /* Don't check the bootable image flag because we could really call a
2497 * bootable or non-bootable image. Just validate that the image check
2498 * passes which is distinct from the normal check.
2499 */
Marti Bolivarf804f622017-06-12 15:41:48 -04002500 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04002501 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04002502 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04002503 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002504 if (rc != 0) {
2505 rc = SPLIT_GO_NON_MATCHING;
2506 goto done;
2507 }
2508
Marti Bolivarea088872017-06-12 17:10:49 -04002509 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04002510 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08002511 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002512 rc = SPLIT_GO_OK;
2513
2514done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04002515 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
2516 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002517 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002518 return rc;
2519}