blob: 99cf18684613d6e9df3ab94280cb6e4fdc08e499 [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
David Vinczee2453472019-06-17 12:31:59 +020020/*
21 * Modifications are Copyright (c) 2019 Arm Limited.
22 */
23
Christopher Collins92ea77f2016-12-12 15:59:26 -080024/**
25 * This file provides an interface to the boot loader. Functions defined in
26 * this file should only be called while the boot loader is running.
27 */
28
29#include <assert.h>
30#include <stddef.h>
David Brown52eee562017-07-05 11:25:09 -060031#include <stdbool.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080032#include <inttypes.h>
33#include <stdlib.h>
34#include <string.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080035#include <os/os_malloc.h>
36#include "bootutil/bootutil.h"
37#include "bootutil/image.h"
38#include "bootutil_priv.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050039#include "bootutil/bootutil_log.h"
40
Fabio Utzigba829042018-09-18 08:29:34 -030041#ifdef MCUBOOT_ENC_IMAGES
42#include "bootutil/enc_key.h"
43#endif
44
Fabio Utzigba1fbe62017-07-21 14:01:20 -030045#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030046
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010047MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
48
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040049static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080050
Fabio Utzigabec0732019-07-31 08:40:22 -030051#if (BOOT_IMAGE_NUMBER > 1)
52#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
53#else
54#define IMAGES_ITER(x)
55#endif
56
Fabio Utzig10ee6482019-08-01 12:04:52 -030057/*
58 * This macro allows some control on the allocation of local variables.
59 * When running natively on a target, we don't want to allocated huge
60 * variables on the stack, so make them global instead. For the simulator
61 * we want to run as many threads as there are tests, and it's safer
62 * to just make those variables stack allocated.
63 */
64#if !defined(__BOOTSIM__)
65#define TARGET_STATIC static
66#else
67#define TARGET_STATIC
68#endif
69
David Vincze2d736ad2019-02-18 11:50:22 +010070#if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT) && !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -030071/*
72 * FIXME: this might have to be updated for threaded sim
73 */
Fabio Utziga0e1cce2017-11-23 20:04:01 -020074static int boot_status_fails = 0;
75#define BOOT_STATUS_ASSERT(x) \
76 do { \
Johann Fischered8461b2018-02-15 16:50:31 +010077 if (!(x)) { \
Fabio Utziga0e1cce2017-11-23 20:04:01 -020078 boot_status_fails++; \
79 } \
80 } while (0)
81#else
Fabio Utzig57c40f72017-12-12 21:48:30 -020082#define BOOT_STATUS_ASSERT(x) ASSERT(x)
Fabio Utziga0e1cce2017-11-23 20:04:01 -020083#endif
84
Christopher Collins92ea77f2016-12-12 15:59:26 -080085struct boot_status_table {
David Vincze2d736ad2019-02-18 11:50:22 +010086 uint8_t bst_magic_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -080087 uint8_t bst_magic_scratch;
David Vincze2d736ad2019-02-18 11:50:22 +010088 uint8_t bst_copy_done_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -080089 uint8_t bst_status_source;
90};
91
92/**
93 * This set of tables maps swap state contents to boot status location.
94 * When searching for a match, these tables must be iterated in order.
95 */
96static const struct boot_status_table boot_status_tables[] = {
97 {
David Vincze2d736ad2019-02-18 11:50:22 +010098 /* | primary slot | scratch |
99 * ----------+--------------+--------------|
100 * magic | Good | Any |
101 * copy-done | Set | N/A |
102 * ----------+--------------+--------------'
103 * source: none |
104 * ----------------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -0800105 */
David Vincze2d736ad2019-02-18 11:50:22 +0100106 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
Christopher Collinsa1c12042019-05-23 14:00:28 -0700107 .bst_magic_scratch = BOOT_MAGIC_NOTGOOD,
David Vincze2d736ad2019-02-18 11:50:22 +0100108 .bst_copy_done_primary_slot = BOOT_FLAG_SET,
109 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800110 },
111
112 {
David Vincze2d736ad2019-02-18 11:50:22 +0100113 /* | primary slot | scratch |
114 * ----------+--------------+--------------|
115 * magic | Good | Any |
116 * copy-done | Unset | N/A |
117 * ----------+--------------+--------------'
118 * source: primary slot |
119 * ----------------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -0800120 */
David Vincze2d736ad2019-02-18 11:50:22 +0100121 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
Christopher Collinsa1c12042019-05-23 14:00:28 -0700122 .bst_magic_scratch = BOOT_MAGIC_NOTGOOD,
David Vincze2d736ad2019-02-18 11:50:22 +0100123 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
124 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800125 },
126
127 {
David Vincze2d736ad2019-02-18 11:50:22 +0100128 /* | primary slot | scratch |
129 * ----------+--------------+--------------|
130 * magic | Any | Good |
131 * copy-done | Any | N/A |
132 * ----------+--------------+--------------'
133 * source: scratch |
134 * ----------------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -0800135 */
David Vincze2d736ad2019-02-18 11:50:22 +0100136 .bst_magic_primary_slot = BOOT_MAGIC_ANY,
137 .bst_magic_scratch = BOOT_MAGIC_GOOD,
138 .bst_copy_done_primary_slot = BOOT_FLAG_ANY,
139 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800140 },
Christopher Collins92ea77f2016-12-12 15:59:26 -0800141 {
David Vincze2d736ad2019-02-18 11:50:22 +0100142 /* | primary slot | scratch |
143 * ----------+--------------+--------------|
144 * magic | Unset | Any |
145 * copy-done | Unset | N/A |
146 * ----------+--------------+--------------|
147 * source: varies |
148 * ----------------------------------------+--------------------------+
Christopher Collins92ea77f2016-12-12 15:59:26 -0800149 * This represents one of two cases: |
150 * o No swaps ever (no status to read, so no harm in checking). |
David Vincze2d736ad2019-02-18 11:50:22 +0100151 * o Mid-revert; status in primary slot. |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800152 * -------------------------------------------------------------------'
153 */
David Vincze2d736ad2019-02-18 11:50:22 +0100154 .bst_magic_primary_slot = BOOT_MAGIC_UNSET,
155 .bst_magic_scratch = BOOT_MAGIC_ANY,
156 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
157 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800158 },
159};
160
161#define BOOT_STATUS_TABLES_COUNT \
162 (sizeof boot_status_tables / sizeof boot_status_tables[0])
163
Marti Bolivarfd20c762017-02-07 16:52:50 -0500164#define BOOT_LOG_SWAP_STATE(area, state) \
Christopher Collinsa1c12042019-05-23 14:00:28 -0700165 BOOT_LOG_INF("%s: magic=%s, swap_type=0x%x, copy_done=0x%x, " \
166 "image_ok=0x%x", \
Marti Bolivarfd20c762017-02-07 16:52:50 -0500167 (area), \
168 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
169 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
170 "bad"), \
Christopher Collinsa1c12042019-05-23 14:00:28 -0700171 (state)->swap_type, \
Marti Bolivarfd20c762017-02-07 16:52:50 -0500172 (state)->copy_done, \
173 (state)->image_ok)
174
Christopher Collins92ea77f2016-12-12 15:59:26 -0800175/**
David Vincze2d736ad2019-02-18 11:50:22 +0100176 * Determines where in flash the most recent boot status is stored. The boot
Christopher Collins92ea77f2016-12-12 15:59:26 -0800177 * status is necessary for completing a swap that was interrupted by a boot
178 * loader reset.
179 *
David Vincze2d736ad2019-02-18 11:50:22 +0100180 * @return A BOOT_STATUS_SOURCE_[...] code indicating where status should
181 * be read from.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800182 */
183static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300184boot_status_source(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800185{
186 const struct boot_status_table *table;
187 struct boot_swap_state state_scratch;
David Vincze2d736ad2019-02-18 11:50:22 +0100188 struct boot_swap_state state_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800189 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200190 size_t i;
Marti Bolivarfd20c762017-02-07 16:52:50 -0500191 uint8_t source;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300192 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800193
Fabio Utzigb0f04732019-07-31 09:49:19 -0300194#if (BOOT_IMAGE_NUMBER == 1)
195 (void)state;
196#endif
197
Fabio Utzig10ee6482019-08-01 12:04:52 -0300198 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300199 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
200 &state_primary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800201 assert(rc == 0);
202
Fabio Utzig2473ac02017-05-02 12:45:02 -0300203 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800204 assert(rc == 0);
205
David Vincze2d736ad2019-02-18 11:50:22 +0100206 BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot);
Marti Bolivarfd20c762017-02-07 16:52:50 -0500207 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
208
Christopher Collins92ea77f2016-12-12 15:59:26 -0800209 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300210 table = &boot_status_tables[i];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800211
Christopher Collinsa1c12042019-05-23 14:00:28 -0700212 if (boot_magic_compatible_check(table->bst_magic_primary_slot,
213 state_primary_slot.magic) &&
214 boot_magic_compatible_check(table->bst_magic_scratch,
215 state_scratch.magic) &&
David Vincze2d736ad2019-02-18 11:50:22 +0100216 (table->bst_copy_done_primary_slot == BOOT_FLAG_ANY ||
217 table->bst_copy_done_primary_slot == state_primary_slot.copy_done))
218 {
Marti Bolivarfd20c762017-02-07 16:52:50 -0500219 source = table->bst_status_source;
David Vinczeba3bd602019-06-17 16:01:43 +0200220
221#if (BOOT_IMAGE_NUMBER > 1)
222 /* In case of multi-image boot it can happen that if boot status
223 * info is found on scratch area then it does not belong to the
224 * currently examined image.
225 */
226 if (source == BOOT_STATUS_SOURCE_SCRATCH &&
Fabio Utzig10ee6482019-08-01 12:04:52 -0300227 state_scratch.image_num != BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +0200228 source = BOOT_STATUS_SOURCE_NONE;
229 }
230#endif
231
Marti Bolivarfd20c762017-02-07 16:52:50 -0500232 BOOT_LOG_INF("Boot source: %s",
233 source == BOOT_STATUS_SOURCE_NONE ? "none" :
234 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
David Vincze2d736ad2019-02-18 11:50:22 +0100235 source == BOOT_STATUS_SOURCE_PRIMARY_SLOT ?
236 "primary slot" : "BUG; can't happen");
Marti Bolivarfd20c762017-02-07 16:52:50 -0500237 return source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800238 }
239 }
240
Marti Bolivarfd20c762017-02-07 16:52:50 -0500241 BOOT_LOG_INF("Boot source: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800242 return BOOT_STATUS_SOURCE_NONE;
243}
244
David Brownf5b33d82017-09-01 10:58:27 -0600245/*
246 * Compute the total size of the given image. Includes the size of
247 * the TLVs.
248 */
Fabio Utzig13d9e352017-10-05 20:32:31 -0300249#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600250static int
Fabio Utzigd638b172019-08-09 10:38:05 -0300251boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
David Brownf5b33d82017-09-01 10:58:27 -0600252{
253 const struct flash_area *fap;
254 struct image_tlv_info info;
Fabio Utzigd638b172019-08-09 10:38:05 -0300255 uint32_t tlv_off;
David Brownf5b33d82017-09-01 10:58:27 -0600256 int area_id;
257 int rc;
258
Fabio Utzig10ee6482019-08-01 12:04:52 -0300259#if (BOOT_IMAGE_NUMBER == 1)
260 (void)state;
261#endif
262
263 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
David Brownf5b33d82017-09-01 10:58:27 -0600264 rc = flash_area_open(area_id, &fap);
265 if (rc != 0) {
266 rc = BOOT_EFLASH;
267 goto done;
268 }
269
Fabio Utzigd638b172019-08-09 10:38:05 -0300270 tlv_off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
271 rc = flash_area_read(fap, tlv_off, &info, sizeof(info));
David Brownf5b33d82017-09-01 10:58:27 -0600272 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 }
Fabio Utzigd638b172019-08-09 10:38:05 -0300280 *size = tlv_off + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600281 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 Utzig9d160092019-08-09 07:46:34 -0300581 if (max_entries < 0) {
582 return BOOT_EBADARGS;
583 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300584
Christopher Collins92ea77f2016-12-12 15:59:26 -0800585 found = 0;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200586 found_idx = 0;
587 invalid = 0;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300588 for (i = 0; i < max_entries; i++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300589 rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(state),
Fabio Utzig178be542018-09-19 08:12:56 -0300590 &status, 1);
591 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800592 return BOOT_EFLASH;
593 }
594
Fabio Utzig178be542018-09-19 08:12:56 -0300595 if (rc == 1) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200596 if (found && !found_idx) {
597 found_idx = i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800598 }
599 } else if (!found) {
600 found = 1;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200601 } else if (found_idx) {
602 invalid = 1;
603 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800604 }
605 }
606
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200607 if (invalid) {
608 /* This means there was an error writing status on the last
609 * swap. Tell user and move on to validation!
610 */
611 BOOT_LOG_ERR("Detected inconsistent status!");
612
David Vincze2d736ad2019-02-18 11:50:22 +0100613#if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
614 /* With validation of the primary slot disabled, there is no way
615 * to be sure the swapped primary slot is OK, so abort!
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200616 */
617 assert(0);
618#endif
619 }
620
Christopher Collins92ea77f2016-12-12 15:59:26 -0800621 if (found) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200622 if (!found_idx) {
623 found_idx = i;
624 }
625 found_idx--;
Fabio Utzig39000012018-07-30 12:40:20 -0300626 bs->idx = (found_idx / BOOT_STATUS_STATE_COUNT) + 1;
627 bs->state = (found_idx % BOOT_STATUS_STATE_COUNT) + 1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800628 }
629
630 return 0;
631}
632
633/**
634 * Reads the boot status from the flash. The boot status contains
635 * the current state of an interrupted image copy operation. If the boot
636 * status is not present, or it indicates that previous copy finished,
637 * there is no operation in progress.
638 */
639static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300640boot_read_status(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800641{
642 const struct flash_area *fap;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700643 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200644 uint8_t swap_info;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800645 int status_loc;
646 int area_id;
647 int rc;
648
649 memset(bs, 0, sizeof *bs);
Fabio Utzig39000012018-07-30 12:40:20 -0300650 bs->idx = BOOT_STATUS_IDX_0;
651 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700652 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800653
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700654#ifdef MCUBOOT_OVERWRITE_ONLY
655 /* Overwrite-only doesn't make use of the swap status area. */
656 return 0;
657#endif
658
Fabio Utzig10ee6482019-08-01 12:04:52 -0300659 status_loc = boot_status_source(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800660 switch (status_loc) {
661 case BOOT_STATUS_SOURCE_NONE:
662 return 0;
663
664 case BOOT_STATUS_SOURCE_SCRATCH:
665 area_id = FLASH_AREA_IMAGE_SCRATCH;
666 break;
667
David Vincze2d736ad2019-02-18 11:50:22 +0100668 case BOOT_STATUS_SOURCE_PRIMARY_SLOT:
Fabio Utzig10ee6482019-08-01 12:04:52 -0300669 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800670 break;
671
672 default:
673 assert(0);
674 return BOOT_EBADARGS;
675 }
676
677 rc = flash_area_open(area_id, &fap);
678 if (rc != 0) {
679 return BOOT_EFLASH;
680 }
681
Fabio Utzig10ee6482019-08-01 12:04:52 -0300682 rc = boot_read_status_bytes(fap, state, bs);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700683 if (rc == 0) {
David Vinczee2453472019-06-17 12:31:59 +0200684 off = boot_swap_info_off(fap);
685 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700686 if (rc == 1) {
David Vinczee2453472019-06-17 12:31:59 +0200687 BOOT_SET_SWAP_INFO(swap_info, 0, BOOT_SWAP_TYPE_NONE);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700688 rc = 0;
689 }
David Vinczee2453472019-06-17 12:31:59 +0200690
691 /* Extract the swap type info */
692 bs->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700693 }
Fabio Utzig46490722017-09-04 15:34:32 -0300694
695 flash_area_close(fap);
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700696
Fabio Utzig46490722017-09-04 15:34:32 -0300697 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800698}
699
700/**
701 * Writes the supplied boot status to the flash file system. The boot status
702 * contains the current state of an in-progress image copy operation.
703 *
704 * @param bs The boot status to write.
705 *
706 * @return 0 on success; nonzero on failure.
707 */
708int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300709boot_write_status(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800710{
711 const struct flash_area *fap;
712 uint32_t off;
713 int area_id;
714 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300715 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700716 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300717 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800718
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300719 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100720 * the trailer. Since in the last step the primary slot is erased, the
721 * first two status writes go to the scratch which will be copied to
722 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300723 */
724
Fabio Utzig2473ac02017-05-02 12:45:02 -0300725 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800726 /* Write to scratch. */
727 area_id = FLASH_AREA_IMAGE_SCRATCH;
728 } else {
David Vincze2d736ad2019-02-18 11:50:22 +0100729 /* Write to the primary slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300730 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800731 }
732
733 rc = flash_area_open(area_id, &fap);
734 if (rc != 0) {
735 rc = BOOT_EFLASH;
736 goto done;
737 }
738
739 off = boot_status_off(fap) +
Fabio Utzigd638b172019-08-09 10:38:05 -0300740 boot_status_internal_off(bs->idx, bs->state, BOOT_WRITE_SZ(state));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200741 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300742 erased_val = flash_area_erased_val(fap);
743 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700744 buf[0] = bs->state;
745
746 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800747 if (rc != 0) {
748 rc = BOOT_EFLASH;
749 goto done;
750 }
751
752 rc = 0;
753
754done:
755 flash_area_close(fap);
756 return rc;
757}
758
759/*
760 * Validate image hash/signature in a slot.
761 */
762static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300763boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
764 const struct flash_area *fap, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800765{
Fabio Utzig10ee6482019-08-01 12:04:52 -0300766 TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigb0f04732019-07-31 09:49:19 -0300767 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300768 int rc;
769
Fabio Utzig10ee6482019-08-01 12:04:52 -0300770#if (BOOT_IMAGE_NUMBER == 1)
771 (void)state;
772#endif
773
Fabio Utzigba829042018-09-18 08:29:34 -0300774 (void)bs;
775 (void)rc;
Fabio Utzigbc077932019-08-26 11:16:34 -0300776
777 image_index = BOOT_CURR_IMG(state);
778
779#ifdef MCUBOOT_ENC_IMAGES
780 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300781 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -0300782 if (rc < 0) {
783 return BOOT_EBADIMAGE;
784 }
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300785 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs->enckey[1])) {
Fabio Utzigba829042018-09-18 08:29:34 -0300786 return BOOT_EBADIMAGE;
787 }
788 }
Fabio Utzigbc077932019-08-26 11:16:34 -0300789#endif
790
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300791 if (bootutil_img_validate(BOOT_CURR_ENC(state), image_index, hdr, fap, tmpbuf,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300792 BOOT_TMPBUF_SZ, NULL, 0, NULL)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800793 return BOOT_EBADIMAGE;
794 }
Fabio Utzig10ee6482019-08-01 12:04:52 -0300795
Christopher Collins92ea77f2016-12-12 15:59:26 -0800796 return 0;
797}
798
799static int
800split_image_check(struct image_header *app_hdr,
801 const struct flash_area *app_fap,
802 struct image_header *loader_hdr,
803 const struct flash_area *loader_fap)
804{
805 static void *tmpbuf;
806 uint8_t loader_hash[32];
807
808 if (!tmpbuf) {
809 tmpbuf = malloc(BOOT_TMPBUF_SZ);
810 if (!tmpbuf) {
811 return BOOT_ENOMEM;
812 }
813 }
814
Fabio Utzig10ee6482019-08-01 12:04:52 -0300815 if (bootutil_img_validate(NULL, 0, loader_hdr, loader_fap, tmpbuf,
816 BOOT_TMPBUF_SZ, NULL, 0, loader_hash)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800817 return BOOT_EBADIMAGE;
818 }
819
Fabio Utzig10ee6482019-08-01 12:04:52 -0300820 if (bootutil_img_validate(NULL, 0, app_hdr, app_fap, tmpbuf,
821 BOOT_TMPBUF_SZ, loader_hash, 32, NULL)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800822 return BOOT_EBADIMAGE;
823 }
824
825 return 0;
826}
827
Fabio Utzig338a19f2018-12-03 08:37:08 -0200828/*
829 * Check that a memory area consists of a given value.
830 */
831static inline bool
832boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300833{
834 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200835 uint8_t *p = (uint8_t *)data;
836 for (i = 0; i < len; i++) {
837 if (val != p[i]) {
838 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300839 }
840 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200841 return true;
842}
843
844static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300845boot_check_header_erased(struct boot_loader_state *state, int slot)
Fabio Utzig338a19f2018-12-03 08:37:08 -0200846{
847 const struct flash_area *fap;
848 struct image_header *hdr;
849 uint8_t erased_val;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300850 int area_id;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200851 int rc;
852
Fabio Utzig10ee6482019-08-01 12:04:52 -0300853 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300854 rc = flash_area_open(area_id, &fap);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200855 if (rc != 0) {
856 return -1;
857 }
858
859 erased_val = flash_area_erased_val(fap);
860 flash_area_close(fap);
861
Fabio Utzig10ee6482019-08-01 12:04:52 -0300862 hdr = boot_img_hdr(state, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200863 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
864 return -1;
865 }
866
867 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300868}
869
Christopher Collins92ea77f2016-12-12 15:59:26 -0800870static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300871boot_validate_slot(struct boot_loader_state *state, int slot,
872 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800873{
874 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400875 struct image_header *hdr;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300876 int area_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800877 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300878
Fabio Utzig10ee6482019-08-01 12:04:52 -0300879 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300880 rc = flash_area_open(area_id, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800881 if (rc != 0) {
882 return BOOT_EFLASH;
883 }
884
Fabio Utzig10ee6482019-08-01 12:04:52 -0300885 hdr = boot_img_hdr(state, slot);
886 if (boot_check_header_erased(state, slot) == 0 ||
887 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100888 /* No bootable image in slot; continue booting from the primary slot. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200889 rc = -1;
890 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300891 }
892
Fabio Utzig10ee6482019-08-01 12:04:52 -0300893 if (hdr->ih_magic != IMAGE_MAGIC || boot_image_check(state, hdr, fap, bs)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100894 if (slot != BOOT_PRIMARY_SLOT) {
David Brownb38e0442017-02-24 13:57:12 -0700895 flash_area_erase(fap, 0, fap->fa_size);
David Vincze2d736ad2019-02-18 11:50:22 +0100896 /* Image in the secondary slot is invalid. Erase the image and
897 * continue booting from the primary slot.
David Brownb38e0442017-02-24 13:57:12 -0700898 */
899 }
David Vincze2d736ad2019-02-18 11:50:22 +0100900 BOOT_LOG_ERR("Image in the %s slot is not valid!",
901 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
Fabio Utzig338a19f2018-12-03 08:37:08 -0200902 rc = -1;
903 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800904 }
905
David Vincze2d736ad2019-02-18 11:50:22 +0100906 /* Image in the secondary slot is valid. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200907 rc = 0;
908
909out:
910 flash_area_close(fap);
911 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800912}
913
914/**
915 * Determines which swap operation to perform, if any. If it is determined
David Vincze2d736ad2019-02-18 11:50:22 +0100916 * that a swap operation is required, the image in the secondary slot is checked
917 * for validity. If the image in the secondary slot is invalid, it is erased,
918 * and a swap type of "none" is indicated.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800919 *
920 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
921 */
922static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300923boot_validated_swap_type(struct boot_loader_state *state,
924 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800925{
926 int swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800927
Fabio Utzigb0f04732019-07-31 09:49:19 -0300928#if (BOOT_IMAGE_NUMBER == 1)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800929 swap_type = boot_swap_type();
Fabio Utzigb0f04732019-07-31 09:49:19 -0300930#else
931 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
932#endif
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300933 switch (swap_type) {
934 case BOOT_SWAP_TYPE_TEST:
935 case BOOT_SWAP_TYPE_PERM:
936 case BOOT_SWAP_TYPE_REVERT:
David Vincze2d736ad2019-02-18 11:50:22 +0100937 /* Boot loader wants to switch to the secondary slot.
938 * Ensure image is valid.
939 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300940 if (boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) != 0) {
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300941 swap_type = BOOT_SWAP_TYPE_FAIL;
942 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800943 }
944
945 return swap_type;
946}
947
948/**
949 * Calculates the number of sectors the scratch area can contain. A "last"
950 * source sector is specified because images are copied backwards in flash
951 * (final index to index number 0).
952 *
953 * @param last_sector_idx The index of the last source sector
954 * (inclusive).
955 * @param out_first_sector_idx The index of the first source sector
956 * (inclusive) gets written here.
957 *
958 * @return The number of bytes comprised by the
959 * [first-sector, last-sector] range.
960 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300961#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800962static uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300963boot_copy_sz(struct boot_loader_state *state, int last_sector_idx,
964 int *out_first_sector_idx)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800965{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400966 size_t scratch_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800967 uint32_t new_sz;
968 uint32_t sz;
969 int i;
970
971 sz = 0;
972
Fabio Utzig10ee6482019-08-01 12:04:52 -0300973 scratch_sz = boot_scratch_area_size(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800974 for (i = last_sector_idx; i >= 0; i--) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300975 new_sz = sz + boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200976 /*
David Vincze2d736ad2019-02-18 11:50:22 +0100977 * The secondary slot is not being checked here, because
978 * `boot_slots_compatible` already provides assurance that the copy size
979 * will be compatible with the primary slot and scratch.
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200980 */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400981 if (new_sz > scratch_sz) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800982 break;
983 }
984 sz = new_sz;
985 }
986
987 /* i currently refers to a sector that doesn't fit or it is -1 because all
988 * sectors have been processed. In both cases, exclude sector i.
989 */
990 *out_first_sector_idx = i + 1;
991 return sz;
992}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300993#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800994
995/**
996 * Erases a region of flash.
997 *
Fabio Utzigba829042018-09-18 08:29:34 -0300998 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800999 * @param off The offset within the flash area to start the
1000 * erase.
1001 * @param sz The number of bytes to erase.
1002 *
1003 * @return 0 on success; nonzero on failure.
1004 */
Fabio Utzigba829042018-09-18 08:29:34 -03001005static inline int
1006boot_erase_sector(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001007{
Fabio Utzigba829042018-09-18 08:29:34 -03001008 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001009}
1010
1011/**
1012 * Copies the contents of one flash region to another. You must erase the
1013 * destination region prior to calling this function.
1014 *
1015 * @param flash_area_id_src The ID of the source flash area.
1016 * @param flash_area_id_dst The ID of the destination flash area.
1017 * @param off_src The offset within the source flash area to
1018 * copy from.
1019 * @param off_dst The offset within the destination flash area to
1020 * copy to.
1021 * @param sz The number of bytes to copy.
1022 *
1023 * @return 0 on success; nonzero on failure.
1024 */
1025static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001026boot_copy_sector(struct boot_loader_state *state,
1027 const struct flash_area *fap_src,
Fabio Utzigba829042018-09-18 08:29:34 -03001028 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -08001029 uint32_t off_src, uint32_t off_dst, uint32_t sz)
1030{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001031 uint32_t bytes_copied;
1032 int chunk_sz;
1033 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -03001034#ifdef MCUBOOT_ENC_IMAGES
1035 uint32_t off;
Fabio Utziga87cc7d2019-08-26 11:21:45 -03001036 uint32_t tlv_off;
Fabio Utzigba829042018-09-18 08:29:34 -03001037 size_t blk_off;
1038 struct image_header *hdr;
1039 uint16_t idx;
1040 uint32_t blk_sz;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001041 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -03001042#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001043
Fabio Utzig10ee6482019-08-01 12:04:52 -03001044 TARGET_STATIC uint8_t buf[1024];
1045
1046#if !defined(MCUBOOT_ENC_IMAGES)
1047 (void)state;
1048#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001049
Christopher Collins92ea77f2016-12-12 15:59:26 -08001050 bytes_copied = 0;
1051 while (bytes_copied < sz) {
1052 if (sz - bytes_copied > sizeof buf) {
1053 chunk_sz = sizeof buf;
1054 } else {
1055 chunk_sz = sz - bytes_copied;
1056 }
1057
1058 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
1059 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001060 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001061 }
1062
Fabio Utzigba829042018-09-18 08:29:34 -03001063#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001064 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001065 if (fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) ||
1066 fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001067 /* assume the secondary slot as src, needs decryption */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001068 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001069 off = off_src;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001070 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001071 /* might need encryption (metadata from the primary slot) */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001072 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001073 off = off_dst;
1074 }
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001075 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001076 blk_sz = chunk_sz;
1077 idx = 0;
1078 if (off + bytes_copied < hdr->ih_hdr_size) {
1079 /* do not decrypt header */
1080 blk_off = 0;
1081 blk_sz = chunk_sz - hdr->ih_hdr_size;
1082 idx = hdr->ih_hdr_size;
1083 } else {
1084 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
1085 }
Fabio Utziga87cc7d2019-08-26 11:21:45 -03001086 tlv_off = BOOT_TLV_OFF(hdr);
1087 if (off + bytes_copied + chunk_sz > tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -03001088 /* do not decrypt TLVs */
Fabio Utziga87cc7d2019-08-26 11:21:45 -03001089 if (off + bytes_copied >= tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -03001090 blk_sz = 0;
1091 } else {
Fabio Utziga87cc7d2019-08-26 11:21:45 -03001092 blk_sz = tlv_off - (off + bytes_copied);
Fabio Utzigba829042018-09-18 08:29:34 -03001093 }
1094 }
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001095 boot_encrypt(BOOT_CURR_ENC(state), 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 Utzigd638b172019-08-09 10:38:05 -03001446 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001447 assert(rc == 0);
1448#endif
David Brown17609d82017-05-05 09:41:34 -06001449
David Vincze2d736ad2019-02-18 11:50:22 +01001450 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1451 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -06001452
Fabio Utzigb0f04732019-07-31 09:49:19 -03001453 image_index = BOOT_CURR_IMG(state);
1454
1455 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1456 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001457 assert (rc == 0);
1458
Fabio Utzigb0f04732019-07-31 09:49:19 -03001459 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1460 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001461 assert (rc == 0);
1462
Fabio Utzig10ee6482019-08-01 12:04:52 -03001463 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001464 for (sect = 0, size = 0; sect < sect_count; sect++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001465 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
David Vincze2d736ad2019-02-18 11:50:22 +01001466 rc = boot_erase_sector(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001467 assert(rc == 0);
1468
1469 size += this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001470
1471#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1472 if (size >= src_size) {
1473 break;
1474 }
1475#endif
David Brown17609d82017-05-05 09:41:34 -06001476 }
1477
Fabio Utzigba829042018-09-18 08:29:34 -03001478#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001479 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001480 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001481 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
Fabio Utzigb0f04732019-07-31 09:49:19 -03001482 fap_secondary_slot, bs->enckey[1]);
David Vincze2d736ad2019-02-18 11:50:22 +01001483
Fabio Utzigba829042018-09-18 08:29:34 -03001484 if (rc < 0) {
1485 return BOOT_EBADIMAGE;
1486 }
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001487 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs->enckey[1])) {
Fabio Utzigba829042018-09-18 08:29:34 -03001488 return BOOT_EBADIMAGE;
1489 }
1490 }
1491#endif
1492
David Vincze2d736ad2019-02-18 11:50:22 +01001493 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1494 size);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001495 rc = boot_copy_sector(state, fap_secondary_slot, fap_primary_slot, 0, 0, size);
David Brown17609d82017-05-05 09:41:34 -06001496
Fabio Utzig13d9e352017-10-05 20:32:31 -03001497 /*
1498 * Erases header and trailer. The trailer is erased because when a new
1499 * image is written without a trailer as is the case when using newt, the
1500 * trailer that was left might trigger a new upgrade.
1501 */
Christopher Collins2c88e692019-05-22 15:10:14 -07001502 BOOT_LOG_DBG("erasing secondary header");
David Vincze2d736ad2019-02-18 11:50:22 +01001503 rc = boot_erase_sector(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001504 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
1505 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001506 assert(rc == 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001507 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -07001508 BOOT_LOG_DBG("erasing secondary trailer");
David Vincze2d736ad2019-02-18 11:50:22 +01001509 rc = boot_erase_sector(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001510 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
1511 last_sector),
1512 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
1513 last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001514 assert(rc == 0);
1515
David Vincze2d736ad2019-02-18 11:50:22 +01001516 flash_area_close(fap_primary_slot);
1517 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001518
David Vincze2d736ad2019-02-18 11:50:22 +01001519 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001520
1521 return 0;
1522}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001523#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001524
Christopher Collinsa1c12042019-05-23 14:00:28 -07001525#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -03001526/**
1527 * Swaps the two images in flash. If a prior copy operation was interrupted
1528 * by a system reset, this function completes that operation.
1529 *
1530 * @param bs The current boot status. This function reads
1531 * this struct to determine if it is resuming
1532 * an interrupted swap operation. This
1533 * function writes the updated status to this
1534 * function on return.
1535 *
1536 * @return 0 on success; nonzero on failure.
1537 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001538static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001539boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001540{
1541 uint32_t sz;
1542 int first_sector_idx;
1543 int last_sector_idx;
David Vincze2d736ad2019-02-18 11:50:22 +01001544 int last_idx_secondary_slot;
Fabio Utzigcd5774b2017-11-29 10:18:26 -02001545 uint32_t swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001546 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001547#ifdef MCUBOOT_ENC_IMAGES
1548 const struct flash_area *fap;
1549 uint8_t slot;
1550 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001551#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001552 uint32_t size;
1553 uint32_t copy_size;
David Vincze2d736ad2019-02-18 11:50:22 +01001554 uint32_t primary_slot_size;
1555 uint32_t secondary_slot_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001556 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001557 int rc;
1558
1559 /* FIXME: just do this if asked by user? */
1560
1561 size = copy_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001562 image_index = BOOT_CURR_IMG(state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001563
Fabio Utzig39000012018-07-30 12:40:20 -03001564 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Fabio Utzig46490722017-09-04 15:34:32 -03001565 /*
1566 * No swap ever happened, so need to find the largest image which
1567 * will be used to determine the amount of sectors to swap.
1568 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001569 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001570 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001571 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001572 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001573 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001574
Fabio Utzigba829042018-09-18 08:29:34 -03001575#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001576 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001577 fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001578 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs->enckey[0]);
Fabio Utzigba829042018-09-18 08:29:34 -03001579 assert(rc >= 0);
1580
1581 if (rc == 0) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001582 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 0, bs->enckey[0]);
Fabio Utzigba829042018-09-18 08:29:34 -03001583 assert(rc == 0);
1584 } else {
1585 rc = 0;
1586 }
1587 } else {
1588 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1589 }
1590#endif
1591
Fabio Utzig10ee6482019-08-01 12:04:52 -03001592 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001593 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001594 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001595 assert(rc == 0);
1596 }
1597
Fabio Utzigba829042018-09-18 08:29:34 -03001598#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001599 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001600 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001601 fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001602 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001603 assert(rc >= 0);
1604
1605 if (rc == 0) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001606 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001607 assert(rc == 0);
1608 } else {
1609 rc = 0;
1610 }
1611 } else {
1612 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1613 }
1614#endif
1615
Fabio Utzig46490722017-09-04 15:34:32 -03001616 if (size > copy_size) {
1617 copy_size = size;
1618 }
1619
1620 bs->swap_size = copy_size;
1621 } else {
1622 /*
1623 * If a swap was under way, the swap_size should already be present
1624 * in the trailer...
1625 */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001626 rc = boot_read_swap_size(image_index, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001627 assert(rc == 0);
1628
1629 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001630
1631#ifdef MCUBOOT_ENC_IMAGES
1632 for (slot = 0; slot <= 1; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03001633 rc = boot_read_enc_key(image_index, slot, bs->enckey[slot]);
Fabio Utzigba829042018-09-18 08:29:34 -03001634 assert(rc == 0);
1635
1636 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001637 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001638 break;
1639 }
1640 }
1641
1642 if (i != BOOT_ENC_KEY_SIZE) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001643 boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs->enckey[slot]);
Fabio Utzigba829042018-09-18 08:29:34 -03001644 }
1645 }
1646#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001647 }
1648
David Vincze2d736ad2019-02-18 11:50:22 +01001649 primary_slot_size = 0;
1650 secondary_slot_size = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001651 last_sector_idx = 0;
David Vincze2d736ad2019-02-18 11:50:22 +01001652 last_idx_secondary_slot = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001653
1654 /*
1655 * Knowing the size of the largest image between both slots, here we
David Vincze2d736ad2019-02-18 11:50:22 +01001656 * find what is the last sector in the primary slot that needs swapping.
1657 * Since we already know that both slots are compatible, the secondary
1658 * slot's last sector is not really required after this check is finished.
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001659 */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001660 while (1) {
David Vincze2d736ad2019-02-18 11:50:22 +01001661 if ((primary_slot_size < copy_size) ||
1662 (primary_slot_size < secondary_slot_size)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001663 primary_slot_size += boot_img_sector_size(state,
David Vincze2d736ad2019-02-18 11:50:22 +01001664 BOOT_PRIMARY_SLOT,
1665 last_sector_idx);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001666 }
David Vincze2d736ad2019-02-18 11:50:22 +01001667 if ((secondary_slot_size < copy_size) ||
1668 (secondary_slot_size < primary_slot_size)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001669 secondary_slot_size += boot_img_sector_size(state,
David Vincze2d736ad2019-02-18 11:50:22 +01001670 BOOT_SECONDARY_SLOT,
1671 last_idx_secondary_slot);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001672 }
David Vincze2d736ad2019-02-18 11:50:22 +01001673 if (primary_slot_size >= copy_size &&
1674 secondary_slot_size >= copy_size &&
1675 primary_slot_size == secondary_slot_size) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001676 break;
1677 }
1678 last_sector_idx++;
David Vincze2d736ad2019-02-18 11:50:22 +01001679 last_idx_secondary_slot++;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001680 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001681
1682 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001683 while (last_sector_idx >= 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001684 sz = boot_copy_sz(state, last_sector_idx, &first_sector_idx);
Fabio Utzig39000012018-07-30 12:40:20 -03001685 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001686 boot_swap_sectors(first_sector_idx, sz, state, bs);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001687 }
1688
1689 last_sector_idx = first_sector_idx - 1;
1690 swap_idx++;
1691 }
1692
David Vincze2d736ad2019-02-18 11:50:22 +01001693#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001694 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001695 BOOT_LOG_WRN("%d status write fails performing the swap",
1696 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001697 }
1698#endif
1699
Christopher Collins92ea77f2016-12-12 15:59:26 -08001700 return 0;
1701}
David Brown17609d82017-05-05 09:41:34 -06001702#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001703
1704/**
David Vincze2d736ad2019-02-18 11:50:22 +01001705 * Marks the image in the primary slot as fully copied.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001706 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001707#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001708static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001709boot_set_copy_done(uint8_t image_index)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001710{
1711 const struct flash_area *fap;
1712 int rc;
1713
Fabio Utzigb0f04732019-07-31 09:49:19 -03001714 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1715 &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001716 if (rc != 0) {
1717 return BOOT_EFLASH;
1718 }
1719
1720 rc = boot_write_copy_done(fap);
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001721 flash_area_close(fap);
1722 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001723}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001724#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001725
1726/**
David Vincze2d736ad2019-02-18 11:50:22 +01001727 * Marks a reverted image in the primary slot as confirmed. This is necessary to
1728 * ensure the status bytes from the image revert operation don't get processed
1729 * on a subsequent boot.
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001730 *
1731 * NOTE: image_ok is tested before writing because if there's a valid permanent
David Vincze2d736ad2019-02-18 11:50:22 +01001732 * image installed on the primary slot and the new image to be upgrade to has a
1733 * bad sig, image_ok would be overwritten.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001734 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001735#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001736static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001737boot_set_image_ok(uint8_t image_index)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001738{
1739 const struct flash_area *fap;
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001740 struct boot_swap_state state;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001741 int rc;
1742
Fabio Utzigb0f04732019-07-31 09:49:19 -03001743 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1744 &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001745 if (rc != 0) {
1746 return BOOT_EFLASH;
1747 }
1748
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001749 rc = boot_read_swap_state(fap, &state);
1750 if (rc != 0) {
1751 rc = BOOT_EFLASH;
1752 goto out;
1753 }
1754
1755 if (state.image_ok == BOOT_FLAG_UNSET) {
1756 rc = boot_write_image_ok(fap);
1757 }
1758
1759out:
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001760 flash_area_close(fap);
1761 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001762}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001763#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001764
David Vinczee32483f2019-06-13 10:46:24 +02001765#if (BOOT_IMAGE_NUMBER > 1)
1766/**
1767 * Check the image dependency whether it is satisfied and modify
1768 * the swap type if necessary.
1769 *
1770 * @param dep Image dependency which has to be verified.
1771 *
1772 * @return 0 on success; nonzero on failure.
1773 */
1774static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001775boot_verify_single_dependency(struct boot_loader_state *state,
1776 struct image_dependency *dep)
David Vinczee32483f2019-06-13 10:46:24 +02001777{
1778 struct image_version *dep_version;
1779 size_t dep_slot;
1780 int rc;
1781
1782 /* Determine the source of the image which is the subject of
1783 * the dependency and get it's version. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001784 dep_slot = (state->swap_type[dep->image_id] != BOOT_SWAP_TYPE_NONE) ?
David Vinczee32483f2019-06-13 10:46:24 +02001785 BOOT_SECONDARY_SLOT : BOOT_PRIMARY_SLOT;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001786 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +02001787
1788 rc = boot_is_version_sufficient(&dep->image_min_version, dep_version);
1789 if (rc != 0) {
1790 /* Dependency not satisfied.
1791 * Modify the swap type to decrease the version number of the image
1792 * (which will be located in the primary slot after the boot process),
1793 * consequently the number of unsatisfied dependencies will be
1794 * decreased or remain the same.
1795 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001796 switch (BOOT_SWAP_TYPE(state)) {
David Vinczee32483f2019-06-13 10:46:24 +02001797 case BOOT_SWAP_TYPE_TEST:
1798 case BOOT_SWAP_TYPE_PERM:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001799 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczee32483f2019-06-13 10:46:24 +02001800 break;
1801 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001802 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczee32483f2019-06-13 10:46:24 +02001803 break;
1804 default:
1805 break;
1806 }
1807 }
1808
1809 return rc;
1810}
1811
1812/**
1813 * Read all dependency TLVs of an image from the flash and verify
1814 * one after another to see if they are all satisfied.
1815 *
1816 * @param slot Image slot number.
1817 *
1818 * @return 0 on success; nonzero on failure.
1819 */
1820static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001821boot_verify_all_dependency(struct boot_loader_state *state, uint32_t slot)
David Vinczee32483f2019-06-13 10:46:24 +02001822{
1823 const struct flash_area *fap;
David Vinczee32483f2019-06-13 10:46:24 +02001824 struct image_tlv_info info;
1825 struct image_tlv tlv;
1826 struct image_dependency dep;
1827 uint32_t off;
1828 uint32_t end;
1829 bool dep_tlvs_found = false;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001830 int area_id;
David Vinczee32483f2019-06-13 10:46:24 +02001831 int rc;
1832
Fabio Utzig10ee6482019-08-01 12:04:52 -03001833 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001834 rc = flash_area_open(area_id, &fap);
David Vinczee32483f2019-06-13 10:46:24 +02001835 if (rc != 0) {
1836 rc = BOOT_EFLASH;
1837 goto done;
1838 }
1839
Fabio Utzigd638b172019-08-09 10:38:05 -03001840 off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
David Vinczee32483f2019-06-13 10:46:24 +02001841
1842 /* The TLV area always starts with an image_tlv_info structure. */
1843 rc = flash_area_read(fap, off, &info, sizeof(info));
1844 if (rc != 0) {
1845 rc = BOOT_EFLASH;
1846 goto done;
1847 }
1848
1849 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
1850 rc = BOOT_EBADIMAGE;
1851 goto done;
1852 }
1853 end = off + info.it_tlv_tot;
1854 off += sizeof(info);
1855
1856 /* Traverse through all of the TLVs to find the dependency TLVs. */
1857 for (; off < end; off += sizeof(tlv) + tlv.it_len) {
1858 rc = flash_area_read(fap, off, &tlv, sizeof(tlv));
1859 if (rc != 0) {
1860 rc = BOOT_EFLASH;
1861 goto done;
1862 }
1863
1864 if (tlv.it_type == IMAGE_TLV_DEPENDENCY) {
1865 if (!dep_tlvs_found) {
1866 dep_tlvs_found = true;
1867 }
1868
1869 if (tlv.it_len != sizeof(dep)) {
1870 rc = BOOT_EBADIMAGE;
1871 goto done;
1872 }
1873
1874 rc = flash_area_read(fap, off + sizeof(tlv), &dep, tlv.it_len);
1875 if (rc != 0) {
1876 rc = BOOT_EFLASH;
1877 goto done;
1878 }
1879
1880 /* Verify dependency and modify the swap type if not satisfied. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001881 rc = boot_verify_single_dependency(state, &dep);
David Vinczee32483f2019-06-13 10:46:24 +02001882 if (rc != 0) {
1883 /* Dependency not satisfied. */
1884 goto done;
1885 }
1886
1887 /* Dependency satisfied, no action needed.
1888 * Continue with the next TLV entry.
1889 */
1890 } else if (dep_tlvs_found) {
1891 /* The dependency TLVs are contiguous in the TLV area. If a
1892 * dependency had already been found and the last read TLV
1893 * has a different type then there are no more dependency TLVs.
1894 * The search can be finished.
1895 */
1896 break;
1897 }
1898 }
1899
1900done:
1901 flash_area_close(fap);
1902 return rc;
1903}
1904
1905/**
1906 * Verify whether the image dependencies in the TLV area are
1907 * all satisfied and modify the swap type if necessary.
1908 *
1909 * @return 0 if all dependencies are satisfied,
1910 * nonzero otherwise.
1911 */
1912static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001913boot_verify_single_image_dependency(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001914{
1915 size_t slot;
1916
1917 /* Determine the source of the dependency TLVs. Those dependencies have to
1918 * be checked which belong to the image that will be located in the primary
1919 * slot after the firmware update process.
1920 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001921 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1922 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
David Vinczee32483f2019-06-13 10:46:24 +02001923 slot = BOOT_SECONDARY_SLOT;
1924 } else {
1925 slot = BOOT_PRIMARY_SLOT;
1926 }
1927
Fabio Utzig10ee6482019-08-01 12:04:52 -03001928 return boot_verify_all_dependency(state, slot);
David Vinczee32483f2019-06-13 10:46:24 +02001929}
1930
1931/**
1932 * Iterate over all the images and verify whether the image dependencies in the
1933 * TLV area are all satisfied and update the related swap type if necessary.
1934 */
1935static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001936boot_verify_all_image_dependency(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001937{
David Vinczee32483f2019-06-13 10:46:24 +02001938 int rc;
1939
Fabio Utzig10ee6482019-08-01 12:04:52 -03001940 BOOT_CURR_IMG(state) = 0;
1941 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
1942 rc = boot_verify_single_image_dependency(state);
Fabio Utzigabec0732019-07-31 08:40:22 -03001943 if (rc == 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001944 /* All dependencies've been satisfied, continue with next image. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001945 BOOT_CURR_IMG(state)++;
David Vinczee32483f2019-06-13 10:46:24 +02001946 } else if (rc == BOOT_EBADVERSION) {
1947 /* Dependency check needs to be restarted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001948 BOOT_CURR_IMG(state) = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001949 } else {
1950 /* Other error happened, images are inconsistent */
1951 return;
1952 }
1953 }
1954}
1955#endif /* (BOOT_IMAGE_NUMBER > 1) */
1956
Christopher Collins92ea77f2016-12-12 15:59:26 -08001957/**
David Vinczeba3bd602019-06-17 16:01:43 +02001958 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001959 *
David Vinczeba3bd602019-06-17 16:01:43 +02001960 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001961 *
1962 * @return 0 on success; nonzero on failure.
1963 */
1964static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001965boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001966{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001967 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001968#ifndef MCUBOOT_OVERWRITE_ONLY
1969 uint8_t swap_type;
1970#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001971
David Vinczeba3bd602019-06-17 16:01:43 +02001972 /* At this point there are no aborted swaps. */
1973#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001974 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001975#elif defined(MCUBOOT_BOOTSTRAP)
1976 /* Check if the image update was triggered by a bad image in the
1977 * primary slot (the validity of the image in the secondary slot had
1978 * already been checked).
1979 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001980 if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 ||
1981 boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) {
1982 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001983 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001984 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001985 }
1986#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03001987 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001988#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001989 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001990
1991#ifndef MCUBOOT_OVERWRITE_ONLY
1992 /* The following state needs image_ok be explicitly set after the
1993 * swap was finished to avoid a new revert.
1994 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001995 swap_type = BOOT_SWAP_TYPE(state);
1996 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1997 swap_type == BOOT_SWAP_TYPE_PERM) {
1998 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001999 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002000 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002001 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002002 }
2003
Fabio Utzig10ee6482019-08-01 12:04:52 -03002004 if (swap_type == BOOT_SWAP_TYPE_TEST ||
2005 swap_type == BOOT_SWAP_TYPE_PERM ||
2006 swap_type == BOOT_SWAP_TYPE_REVERT) {
2007 rc = boot_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002008 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002009 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07002010 }
David Vinczeba3bd602019-06-17 16:01:43 +02002011 }
2012#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02002013
David Vinczeba3bd602019-06-17 16:01:43 +02002014 return rc;
2015}
2016
2017/**
2018 * Completes a previously aborted image swap.
2019 *
2020 * @param bs The current boot status.
2021 *
2022 * @return 0 on success; nonzero on failure.
2023 */
2024#if !defined(MCUBOOT_OVERWRITE_ONLY)
2025static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03002026boot_complete_partial_swap(struct boot_loader_state *state,
2027 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02002028{
2029 int rc;
2030
2031 /* Determine the type of swap operation being resumed from the
2032 * `swap-type` trailer field.
2033 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002034 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002035 assert(rc == 0);
2036
Fabio Utzig10ee6482019-08-01 12:04:52 -03002037 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02002038
2039 /* The following states need image_ok be explicitly set after the
2040 * swap was finished to avoid a new revert.
2041 */
2042 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
2043 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002044 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002045 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002046 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002047 }
2048 }
2049
2050 if (bs->swap_type == BOOT_SWAP_TYPE_TEST ||
2051 bs->swap_type == BOOT_SWAP_TYPE_PERM ||
2052 bs->swap_type == BOOT_SWAP_TYPE_REVERT) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002053 rc = boot_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002054 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002055 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002056 }
2057 }
2058
Fabio Utzig10ee6482019-08-01 12:04:52 -03002059 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002060 BOOT_LOG_ERR("panic!");
2061 assert(0);
2062
2063 /* Loop forever... */
2064 while (1) {}
2065 }
2066
2067 return rc;
2068}
2069#endif /* !MCUBOOT_OVERWRITE_ONLY */
2070
2071#if (BOOT_IMAGE_NUMBER > 1)
2072/**
2073 * Review the validity of previously determined swap types of other images.
2074 *
2075 * @param aborted_swap The current image upgrade is a
2076 * partial/aborted swap.
2077 */
2078static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03002079boot_review_image_swap_types(struct boot_loader_state *state,
2080 bool aborted_swap)
David Vinczeba3bd602019-06-17 16:01:43 +02002081{
2082 /* In that case if we rebooted in the middle of an image upgrade process, we
2083 * must review the validity of swap types, that were previously determined
2084 * for other images. The image_ok flag had not been set before the reboot
2085 * for any of the updated images (only the copy_done flag) and thus falsely
2086 * the REVERT swap type has been determined for the previous images that had
2087 * been updated before the reboot.
2088 *
2089 * There are two separate scenarios that we have to deal with:
2090 *
2091 * 1. The reboot has happened during swapping an image:
2092 * The current image upgrade has been determined as a
2093 * partial/aborted swap.
2094 * 2. The reboot has happened between two separate image upgrades:
2095 * In this scenario we must check the swap type of the current image.
2096 * In those cases if it is NONE or REVERT we cannot certainly determine
2097 * the fact of a reboot. In a consistent state images must move in the
2098 * same direction or stay in place, e.g. in practice REVERT and TEST
2099 * swap types cannot be present at the same time. If the swap type of
2100 * the current image is either TEST, PERM or FAIL we must review the
2101 * already determined swap types of other images and set each false
2102 * REVERT swap types to NONE (these images had been successfully
2103 * updated before the system rebooted between two separate image
2104 * upgrades).
2105 */
2106
Fabio Utzig10ee6482019-08-01 12:04:52 -03002107 if (BOOT_CURR_IMG(state) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02002108 /* Nothing to do */
2109 return;
2110 }
2111
2112 if (!aborted_swap) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002113 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
2114 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002115 /* Nothing to do */
2116 return;
2117 }
2118 }
2119
Fabio Utzig10ee6482019-08-01 12:04:52 -03002120 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
2121 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
2122 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002123 }
2124 }
2125}
2126#endif
2127
2128/**
2129 * Prepare image to be updated if required.
2130 *
2131 * Prepare image to be updated if required with completing an image swap
2132 * operation if one was aborted and/or determining the type of the
2133 * swap operation. In case of any error set the swap type to NONE.
2134 *
Fabio Utzig10ee6482019-08-01 12:04:52 -03002135 * @param state TODO
David Vinczeba3bd602019-06-17 16:01:43 +02002136 * @param bs Pointer where the read and possibly updated
2137 * boot status can be written to.
2138 */
2139static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03002140boot_prepare_image_for_update(struct boot_loader_state *state,
2141 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02002142{
2143 int rc;
2144
2145 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002146 rc = boot_read_sectors(state);
David Vinczeba3bd602019-06-17 16:01:43 +02002147 if (rc != 0) {
2148 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
2149 " - too small?", BOOT_MAX_IMG_SECTORS);
2150 /* Unable to determine sector layout, continue with next image
2151 * if there is one.
2152 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002153 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002154 return;
2155 }
2156
2157 /* Attempt to read an image header from each slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002158 rc = boot_read_image_headers(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02002159 if (rc != 0) {
2160 /* Continue with next image if there is one. */
Fabio Utzigb0f04732019-07-31 09:49:19 -03002161 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03002162 BOOT_CURR_IMG(state));
2163 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002164 return;
2165 }
2166
2167 /* If the current image's slots aren't compatible, no swap is possible.
2168 * Just boot into primary slot.
2169 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002170 if (boot_slots_compatible(state)) {
2171 rc = boot_read_status(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002172 if (rc != 0) {
2173 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03002174 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002175 /* Continue with next image if there is one. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002176 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002177 return;
2178 }
2179
2180 /* Determine if we rebooted in the middle of an image swap
2181 * operation. If a partial swap was detected, complete it.
2182 */
2183 if (bs->idx != BOOT_STATUS_IDX_0 || bs->state != BOOT_STATUS_STATE_0) {
2184
2185#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03002186 boot_review_image_swap_types(state, true);
David Vinczeba3bd602019-06-17 16:01:43 +02002187#endif
2188
2189#ifdef MCUBOOT_OVERWRITE_ONLY
2190 /* Should never arrive here, overwrite-only mode has
2191 * no swap state.
2192 */
2193 assert(0);
2194#else
2195 /* Determine the type of swap operation being resumed from the
2196 * `swap-type` trailer field.
2197 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002198 rc = boot_complete_partial_swap(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002199 assert(rc == 0);
2200#endif
2201 /* Attempt to read an image header from each slot. Ensure that
2202 * image headers in slots are aligned with headers in boot_data.
2203 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002204 rc = boot_read_image_headers(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02002205 assert(rc == 0);
2206
2207 /* Swap has finished set to NONE */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002208 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002209 } else {
2210 /* There was no partial swap, determine swap type. */
2211 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002212 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
2213 } else if (boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) != 0) {
2214 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
David Vinczeba3bd602019-06-17 16:01:43 +02002215 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002216 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02002217 }
2218
2219#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03002220 boot_review_image_swap_types(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02002221#endif
2222
2223#ifdef MCUBOOT_BOOTSTRAP
Fabio Utzig10ee6482019-08-01 12:04:52 -03002224 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02002225 /* Header checks are done first because they are
2226 * inexpensive. Since overwrite-only copies starting from
2227 * offset 0, if interrupted, it might leave a valid header
2228 * magic, so also run validation on the primary slot to be
2229 * sure it's not OK.
2230 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002231 if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 ||
2232 boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) {
2233 if (boot_img_hdr(state,
David Vinczeba3bd602019-06-17 16:01:43 +02002234 BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC &&
Fabio Utzig10ee6482019-08-01 12:04:52 -03002235 boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) == 0)
David Vinczeba3bd602019-06-17 16:01:43 +02002236 {
2237 /* Set swap type to REVERT to overwrite the primary
2238 * slot with the image contained in secondary slot
2239 * and to trigger the explicit setting of the
2240 * image_ok flag.
2241 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002242 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02002243 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02002244 }
2245 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02002246#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002247 }
David Vinczeba3bd602019-06-17 16:01:43 +02002248 } else {
2249 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002250 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002251 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002252}
2253
Christopher Collins92ea77f2016-12-12 15:59:26 -08002254int
Fabio Utzig10ee6482019-08-01 12:04:52 -03002255context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08002256{
Marti Bolivar84898652017-06-13 17:20:22 -04002257 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02002258 struct boot_status bs;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002259 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002260 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03002261 int image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002262
2263 /* The array of slot sectors are defined here (as opposed to file scope) so
2264 * that they don't get allocated for non-boot-loader apps. This is
2265 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002266 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08002267 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002268 TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
2269 TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
2270 TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Christopher Collins92ea77f2016-12-12 15:59:26 -08002271
Fabio Utzig10ee6482019-08-01 12:04:52 -03002272 memset(state, 0, sizeof(struct boot_loader_state));
Fabio Utzigba829042018-09-18 08:29:34 -03002273
David Vinczeba3bd602019-06-17 16:01:43 +02002274 /* Iterate over all the images. By the end of the loop the swap type has
2275 * to be determined for each image and all aborted swaps have to be
2276 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002277 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002278 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03002279
David Vinczeba3bd602019-06-17 16:01:43 +02002280#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
2281 /* The keys used for encryption may no longer be valid (could belong to
2282 * another images). Therefore, mark them as invalid to force their reload
2283 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03002284 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03002285 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Brown554c52e2017-06-30 16:01:07 -06002286#endif
2287
Fabio Utzig10ee6482019-08-01 12:04:52 -03002288 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03002289
Fabio Utzig10ee6482019-08-01 12:04:52 -03002290 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002291 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03002292 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002293 secondary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03002294 state->scratch.sectors = scratch_sectors;
David Vinczeba3bd602019-06-17 16:01:43 +02002295
2296 /* Open primary and secondary image areas for the duration
2297 * of this call.
2298 */
2299 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03002300 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03002301 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02002302 assert(rc == 0);
2303 }
2304 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03002305 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002306 assert(rc == 0);
2307
2308 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002309 boot_prepare_image_for_update(state, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002310 }
2311
David Vinczee32483f2019-06-13 10:46:24 +02002312#if (BOOT_IMAGE_NUMBER > 1)
2313 /* Iterate over all the images and verify whether the image dependencies
2314 * are all satisfied and update swap type if necessary.
2315 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002316 boot_verify_all_image_dependency(state);
David Vinczee32483f2019-06-13 10:46:24 +02002317#endif
2318
David Vinczeba3bd602019-06-17 16:01:43 +02002319 /* Iterate over all the images. At this point there are no aborted swaps
2320 * and the swap types are determined for each image. By the end of the loop
2321 * all required update operations will have been finished.
2322 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002323 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002324
2325#if (BOOT_IMAGE_NUMBER > 1)
2326#ifdef MCUBOOT_ENC_IMAGES
2327 /* The keys used for encryption may no longer be valid (could belong to
2328 * another images). Therefore, mark them as invalid to force their reload
2329 * by boot_enc_load().
2330 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03002331 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002332#endif /* MCUBOOT_ENC_IMAGES */
2333
2334 /* Indicate that swap is not aborted */
2335 memset(&bs, 0, sizeof bs);
2336 bs.idx = BOOT_STATUS_IDX_0;
2337 bs.state = BOOT_STATUS_STATE_0;
2338#endif /* (BOOT_IMAGE_NUMBER > 1) */
2339
2340 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002341 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02002342
Fabio Utzig10ee6482019-08-01 12:04:52 -03002343 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002344 case BOOT_SWAP_TYPE_NONE:
2345 break;
2346
2347 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
2348 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
2349 case BOOT_SWAP_TYPE_REVERT:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002350 rc = boot_perform_update(state, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002351 assert(rc == 0);
2352 break;
2353
2354 case BOOT_SWAP_TYPE_FAIL:
2355 /* The image in secondary slot was invalid and is now erased. Ensure
2356 * we don't try to boot into it again on the next reboot. Do this by
2357 * pretending we just reverted back to primary slot.
2358 */
2359#ifndef MCUBOOT_OVERWRITE_ONLY
2360 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002361 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002362 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002363 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002364 }
2365#endif /* !MCUBOOT_OVERWRITE_ONLY */
2366 break;
2367
2368 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002369 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002370 }
2371
Fabio Utzig10ee6482019-08-01 12:04:52 -03002372 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002373 BOOT_LOG_ERR("panic!");
2374 assert(0);
2375
2376 /* Loop forever... */
2377 while (1) {}
2378 }
2379 }
2380
2381 /* Iterate over all the images. At this point all required update operations
2382 * have finished. By the end of the loop each image in the primary slot will
2383 * have been re-validated.
2384 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002385 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2386 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02002387 /* Attempt to read an image header from each slot. Ensure that image
2388 * headers in slots are aligned with headers in boot_data.
2389 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002390 rc = boot_read_image_headers(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02002391 if (rc != 0) {
2392 goto out;
2393 }
2394 /* Since headers were reloaded, it can be assumed we just performed
2395 * a swap or overwrite. Now the header info that should be used to
2396 * provide the data for the bootstrap, which previously was at
2397 * secondary slot, was updated to primary slot.
2398 */
2399 }
2400
2401#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig10ee6482019-08-01 12:04:52 -03002402 rc = boot_validate_slot(state, BOOT_PRIMARY_SLOT, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02002403 if (rc != 0) {
2404 rc = BOOT_EBADIMAGE;
2405 goto out;
2406 }
2407#else
2408 /* Even if we're not re-validating the primary slot, we could be booting
2409 * onto an empty flash chip. At least do a basic sanity check that
2410 * the magic number on the image is OK.
2411 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002412 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002413 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
Fabio Utzig10ee6482019-08-01 12:04:52 -03002414 &boot_img_hdr(state,BOOT_PRIMARY_SLOT)->ih_magic,
2415 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002416 rc = BOOT_EBADIMAGE;
2417 goto out;
2418 }
2419#endif
2420 }
2421
Fabio Utzigb0f04732019-07-31 09:49:19 -03002422#if (BOOT_IMAGE_NUMBER > 1)
David Vinczeba3bd602019-06-17 16:01:43 +02002423 /* Always boot from the primary slot of Image 0. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002424 BOOT_CURR_IMG(state) = 0;
Fabio Utzigb0f04732019-07-31 09:49:19 -03002425#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -03002426 rsp->br_flash_dev_id = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)->fa_device_id;
2427 rsp->br_image_off = boot_img_slot_off(state, BOOT_PRIMARY_SLOT);
2428 rsp->br_hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002429
Marti Bolivarc0b47912017-06-13 17:18:09 -04002430 out:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002431 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2432 flash_area_close(BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002433 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002434 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
David Vinczeba3bd602019-06-17 16:01:43 +02002435 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04002436 }
2437 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002438}
2439
Fabio Utzig10ee6482019-08-01 12:04:52 -03002440/**
2441 * Prepares the booting process. This function moves images around in flash as
2442 * appropriate, and tells you what address to boot from.
2443 *
2444 * @param rsp On success, indicates how booting should occur.
2445 *
2446 * @return 0 on success; nonzero on failure.
2447 */
2448int
2449boot_go(struct boot_rsp *rsp)
2450{
2451 return context_boot_go(&boot_data, rsp);
2452}
2453
Christopher Collins92ea77f2016-12-12 15:59:26 -08002454int
2455split_go(int loader_slot, int split_slot, void **entry)
2456{
Marti Bolivarc50926f2017-06-14 09:35:40 -04002457 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08002458 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002459 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002460 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002461 int rc;
2462
Christopher Collins92ea77f2016-12-12 15:59:26 -08002463 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
2464 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04002465 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002466 }
David Vinczeba3bd602019-06-17 16:01:43 +02002467 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
2468 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002469
2470 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
2471 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07002472 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04002473 assert(rc == 0);
2474 split_flash_id = flash_area_id_from_image_slot(split_slot);
2475 rc = flash_area_open(split_flash_id,
2476 &BOOT_IMG_AREA(&boot_data, split_slot));
2477 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002478
2479 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002480 rc = boot_read_sectors(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002481 if (rc != 0) {
2482 rc = SPLIT_GO_ERR;
2483 goto done;
2484 }
2485
Fabio Utzig10ee6482019-08-01 12:04:52 -03002486 rc = boot_read_image_headers(&boot_data, true);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002487 if (rc != 0) {
2488 goto done;
2489 }
2490
Christopher Collins92ea77f2016-12-12 15:59:26 -08002491 /* Don't check the bootable image flag because we could really call a
2492 * bootable or non-bootable image. Just validate that the image check
2493 * passes which is distinct from the normal check.
2494 */
Marti Bolivarf804f622017-06-12 15:41:48 -04002495 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04002496 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04002497 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04002498 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002499 if (rc != 0) {
2500 rc = SPLIT_GO_NON_MATCHING;
2501 goto done;
2502 }
2503
Marti Bolivarea088872017-06-12 17:10:49 -04002504 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04002505 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08002506 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002507 rc = SPLIT_GO_OK;
2508
2509done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04002510 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
2511 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002512 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002513 return rc;
2514}