blob: ac26bd25ae9a911220ee81e1a100a3a3d432d045 [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
20/**
21 * This file provides an interface to the boot loader. Functions defined in
22 * this file should only be called while the boot loader is running.
23 */
24
25#include <assert.h>
26#include <stddef.h>
David Brown52eee562017-07-05 11:25:09 -060027#include <stdbool.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080028#include <inttypes.h>
29#include <stdlib.h>
30#include <string.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080031#include <os/os_malloc.h>
32#include "bootutil/bootutil.h"
33#include "bootutil/image.h"
34#include "bootutil_priv.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050035#include "bootutil/bootutil_log.h"
36
Fabio Utzigba829042018-09-18 08:29:34 -030037#ifdef MCUBOOT_ENC_IMAGES
38#include "bootutil/enc_key.h"
39#endif
40
Fabio Utzigba1fbe62017-07-21 14:01:20 -030041#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030042
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010043MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
44
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040045static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080046
David Vincze2d736ad2019-02-18 11:50:22 +010047#if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT) && !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utziga0e1cce2017-11-23 20:04:01 -020048static int boot_status_fails = 0;
49#define BOOT_STATUS_ASSERT(x) \
50 do { \
Johann Fischered8461b2018-02-15 16:50:31 +010051 if (!(x)) { \
Fabio Utziga0e1cce2017-11-23 20:04:01 -020052 boot_status_fails++; \
53 } \
54 } while (0)
55#else
Fabio Utzig57c40f72017-12-12 21:48:30 -020056#define BOOT_STATUS_ASSERT(x) ASSERT(x)
Fabio Utziga0e1cce2017-11-23 20:04:01 -020057#endif
58
Christopher Collins92ea77f2016-12-12 15:59:26 -080059struct boot_status_table {
David Vincze2d736ad2019-02-18 11:50:22 +010060 uint8_t bst_magic_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -080061 uint8_t bst_magic_scratch;
David Vincze2d736ad2019-02-18 11:50:22 +010062 uint8_t bst_copy_done_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -080063 uint8_t bst_status_source;
64};
65
66/**
67 * This set of tables maps swap state contents to boot status location.
68 * When searching for a match, these tables must be iterated in order.
69 */
70static const struct boot_status_table boot_status_tables[] = {
71 {
David Vincze2d736ad2019-02-18 11:50:22 +010072 /* | primary slot | scratch |
73 * ----------+--------------+--------------|
74 * magic | Good | Any |
75 * copy-done | Set | N/A |
76 * ----------+--------------+--------------'
77 * source: none |
78 * ----------------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -080079 */
David Vincze2d736ad2019-02-18 11:50:22 +010080 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
81 .bst_magic_scratch = BOOT_MAGIC_ANY,
82 .bst_copy_done_primary_slot = BOOT_FLAG_SET,
83 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
Christopher Collins92ea77f2016-12-12 15:59:26 -080084 },
85
86 {
David Vincze2d736ad2019-02-18 11:50:22 +010087 /* | primary slot | scratch |
88 * ----------+--------------+--------------|
89 * magic | Good | Any |
90 * copy-done | Unset | N/A |
91 * ----------+--------------+--------------'
92 * source: primary slot |
93 * ----------------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -080094 */
David Vincze2d736ad2019-02-18 11:50:22 +010095 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
96 .bst_magic_scratch = BOOT_MAGIC_ANY,
97 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
98 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Christopher Collins92ea77f2016-12-12 15:59:26 -080099 },
100
101 {
David Vincze2d736ad2019-02-18 11:50:22 +0100102 /* | primary slot | scratch |
103 * ----------+--------------+--------------|
104 * magic | Any | Good |
105 * copy-done | Any | N/A |
106 * ----------+--------------+--------------'
107 * source: scratch |
108 * ----------------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -0800109 */
David Vincze2d736ad2019-02-18 11:50:22 +0100110 .bst_magic_primary_slot = BOOT_MAGIC_ANY,
111 .bst_magic_scratch = BOOT_MAGIC_GOOD,
112 .bst_copy_done_primary_slot = BOOT_FLAG_ANY,
113 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800114 },
Christopher Collins92ea77f2016-12-12 15:59:26 -0800115 {
David Vincze2d736ad2019-02-18 11:50:22 +0100116 /* | primary slot | scratch |
117 * ----------+--------------+--------------|
118 * magic | Unset | Any |
119 * copy-done | Unset | N/A |
120 * ----------+--------------+--------------|
121 * source: varies |
122 * ----------------------------------------+--------------------------+
Christopher Collins92ea77f2016-12-12 15:59:26 -0800123 * This represents one of two cases: |
124 * o No swaps ever (no status to read, so no harm in checking). |
David Vincze2d736ad2019-02-18 11:50:22 +0100125 * o Mid-revert; status in primary slot. |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800126 * -------------------------------------------------------------------'
127 */
David Vincze2d736ad2019-02-18 11:50:22 +0100128 .bst_magic_primary_slot = BOOT_MAGIC_UNSET,
129 .bst_magic_scratch = BOOT_MAGIC_ANY,
130 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
131 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800132 },
133};
134
135#define BOOT_STATUS_TABLES_COUNT \
136 (sizeof boot_status_tables / sizeof boot_status_tables[0])
137
Marti Bolivarfd20c762017-02-07 16:52:50 -0500138#define BOOT_LOG_SWAP_STATE(area, state) \
139 BOOT_LOG_INF("%s: magic=%s, copy_done=0x%x, image_ok=0x%x", \
140 (area), \
141 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
142 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
143 "bad"), \
144 (state)->copy_done, \
145 (state)->image_ok)
146
Christopher Collins92ea77f2016-12-12 15:59:26 -0800147/**
David Vincze2d736ad2019-02-18 11:50:22 +0100148 * Determines where in flash the most recent boot status is stored. The boot
Christopher Collins92ea77f2016-12-12 15:59:26 -0800149 * status is necessary for completing a swap that was interrupted by a boot
150 * loader reset.
151 *
David Vincze2d736ad2019-02-18 11:50:22 +0100152 * @return A BOOT_STATUS_SOURCE_[...] code indicating where status should
153 * be read from.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800154 */
155static int
156boot_status_source(void)
157{
158 const struct boot_status_table *table;
159 struct boot_swap_state state_scratch;
David Vincze2d736ad2019-02-18 11:50:22 +0100160 struct boot_swap_state state_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800161 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200162 size_t i;
Marti Bolivarfd20c762017-02-07 16:52:50 -0500163 uint8_t source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800164
David Vincze2d736ad2019-02-18 11:50:22 +0100165 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY,
166 &state_primary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800167 assert(rc == 0);
168
Fabio Utzig2473ac02017-05-02 12:45:02 -0300169 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800170 assert(rc == 0);
171
David Vincze2d736ad2019-02-18 11:50:22 +0100172 BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot);
Marti Bolivarfd20c762017-02-07 16:52:50 -0500173 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
174
Christopher Collins92ea77f2016-12-12 15:59:26 -0800175 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300176 table = &boot_status_tables[i];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800177
David Vincze2d736ad2019-02-18 11:50:22 +0100178 if ((table->bst_magic_primary_slot == BOOT_MAGIC_ANY ||
179 table->bst_magic_primary_slot == state_primary_slot.magic) &&
180 (table->bst_magic_scratch == BOOT_MAGIC_ANY ||
181 table->bst_magic_scratch == state_scratch.magic) &&
182 (table->bst_copy_done_primary_slot == BOOT_FLAG_ANY ||
183 table->bst_copy_done_primary_slot == state_primary_slot.copy_done))
184 {
Marti Bolivarfd20c762017-02-07 16:52:50 -0500185 source = table->bst_status_source;
186 BOOT_LOG_INF("Boot source: %s",
187 source == BOOT_STATUS_SOURCE_NONE ? "none" :
188 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
David Vincze2d736ad2019-02-18 11:50:22 +0100189 source == BOOT_STATUS_SOURCE_PRIMARY_SLOT ?
190 "primary slot" : "BUG; can't happen");
Marti Bolivarfd20c762017-02-07 16:52:50 -0500191 return source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800192 }
193 }
194
Marti Bolivarfd20c762017-02-07 16:52:50 -0500195 BOOT_LOG_INF("Boot source: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800196 return BOOT_STATUS_SOURCE_NONE;
197}
198
199/**
200 * Calculates the type of swap that just completed.
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300201 *
202 * This is used when a swap is interrupted by an external event. After
203 * finishing the swap operation determines what the initial request was.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800204 */
205static int
206boot_previous_swap_type(void)
207{
208 int post_swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800209
210 post_swap_type = boot_swap_type();
211
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300212 switch (post_swap_type) {
213 case BOOT_SWAP_TYPE_NONE : return BOOT_SWAP_TYPE_PERM;
214 case BOOT_SWAP_TYPE_REVERT : return BOOT_SWAP_TYPE_TEST;
215 case BOOT_SWAP_TYPE_PANIC : return BOOT_SWAP_TYPE_PANIC;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800216 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300217
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300218 return BOOT_SWAP_TYPE_FAIL;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800219}
220
David Brownf5b33d82017-09-01 10:58:27 -0600221/*
222 * Compute the total size of the given image. Includes the size of
223 * the TLVs.
224 */
Fabio Utzig13d9e352017-10-05 20:32:31 -0300225#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600226static int
227boot_read_image_size(int slot, struct image_header *hdr, uint32_t *size)
228{
229 const struct flash_area *fap;
230 struct image_tlv_info info;
231 int area_id;
232 int rc;
233
234 area_id = flash_area_id_from_image_slot(slot);
235 rc = flash_area_open(area_id, &fap);
236 if (rc != 0) {
237 rc = BOOT_EFLASH;
238 goto done;
239 }
240
241 rc = flash_area_read(fap, hdr->ih_hdr_size + hdr->ih_img_size,
242 &info, sizeof(info));
243 if (rc != 0) {
244 rc = BOOT_EFLASH;
245 goto done;
246 }
247 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
248 rc = BOOT_EBADIMAGE;
249 goto done;
250 }
251 *size = hdr->ih_hdr_size + hdr->ih_img_size + info.it_tlv_tot;
252 rc = 0;
253
254done:
255 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300256 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600257}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300258#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600259
Fabio Utzigc08ed212017-06-20 19:28:36 -0300260static int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800261boot_read_image_header(int slot, struct image_header *out_hdr)
262{
263 const struct flash_area *fap;
264 int area_id;
265 int rc;
266
267 area_id = flash_area_id_from_image_slot(slot);
268 rc = flash_area_open(area_id, &fap);
269 if (rc != 0) {
270 rc = BOOT_EFLASH;
271 goto done;
272 }
273
274 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
275 if (rc != 0) {
276 rc = BOOT_EFLASH;
277 goto done;
278 }
279
280 rc = 0;
281
282done:
283 flash_area_close(fap);
284 return rc;
285}
286
287static int
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200288boot_read_image_headers(bool require_all)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800289{
290 int rc;
291 int i;
292
293 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Marti Bolivarf804f622017-06-12 15:41:48 -0400294 rc = boot_read_image_header(i, boot_img_hdr(&boot_data, i));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800295 if (rc != 0) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200296 /* If `require_all` is set, fail on any single fail, otherwise
297 * if at least the first slot's header was read successfully,
298 * then the boot loader can attempt a boot.
299 *
300 * Failure to read any headers is a fatal error.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800301 */
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200302 if (i > 0 && !require_all) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800303 return 0;
304 } else {
305 return rc;
306 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800307 }
308 }
309
310 return 0;
311}
312
313static uint8_t
314boot_write_sz(void)
315{
316 uint8_t elem_sz;
317 uint8_t align;
318
319 /* Figure out what size to write update status update as. The size depends
320 * on what the minimum write size is for scratch area, active image slot.
321 * We need to use the bigger of those 2 values.
322 */
David Vincze2d736ad2019-02-18 11:50:22 +0100323 elem_sz = flash_area_align(boot_data.imgs[BOOT_PRIMARY_SLOT].area);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200324 align = flash_area_align(boot_data.scratch.area);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800325 if (align > elem_sz) {
326 elem_sz = align;
327 }
328
329 return elem_sz;
330}
331
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200332/*
333 * Slots are compatible when all sectors that store upto to size of the image
334 * round up to sector size, in both slot's are able to fit in the scratch
335 * area, and have sizes that are a multiple of each other (powers of two
336 * presumably!).
337 */
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800338static int
339boot_slots_compatible(void)
340{
David Vincze2d736ad2019-02-18 11:50:22 +0100341 size_t num_sectors_primary;
342 size_t num_sectors_secondary;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200343 size_t sz0, sz1;
David Vincze2d736ad2019-02-18 11:50:22 +0100344 size_t primary_slot_sz, secondary_slot_sz;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200345 size_t scratch_sz;
346 size_t i, j;
347 int8_t smaller;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800348
David Vincze2d736ad2019-02-18 11:50:22 +0100349 num_sectors_primary =
350 boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT);
351 num_sectors_secondary =
352 boot_img_num_sectors(&boot_data, BOOT_SECONDARY_SLOT);
353 if ((num_sectors_primary > BOOT_MAX_IMG_SECTORS) ||
354 (num_sectors_secondary > BOOT_MAX_IMG_SECTORS)) {
Fabio Utziga1fae672018-03-30 10:52:38 -0300355 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800356 return 0;
357 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300358
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200359 scratch_sz = boot_scratch_area_size(&boot_data);
360
361 /*
362 * The following loop scans all sectors in a linear fashion, assuring that
363 * for each possible sector in each slot, it is able to fit in the other
364 * slot's sector or sectors. Slot's should be compatible as long as any
365 * number of a slot's sectors are able to fit into another, which only
366 * excludes cases where sector sizes are not a multiple of each other.
367 */
David Vincze2d736ad2019-02-18 11:50:22 +0100368 i = sz0 = primary_slot_sz = 0;
369 j = sz1 = secondary_slot_sz = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200370 smaller = 0;
David Vincze2d736ad2019-02-18 11:50:22 +0100371 while (i < num_sectors_primary || j < num_sectors_secondary) {
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200372 if (sz0 == sz1) {
David Vincze2d736ad2019-02-18 11:50:22 +0100373 sz0 += boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
374 sz1 += boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT, j);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200375 i++;
376 j++;
377 } else if (sz0 < sz1) {
David Vincze2d736ad2019-02-18 11:50:22 +0100378 sz0 += boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
379 /* Guarantee that multiple sectors of the secondary slot
380 * fit into the primary slot.
381 */
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200382 if (smaller == 2) {
383 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors");
384 return 0;
385 }
386 smaller = 1;
387 i++;
388 } else {
David Vincze2d736ad2019-02-18 11:50:22 +0100389 sz1 += boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT, j);
390 /* Guarantee that multiple sectors of the primary slot
391 * fit into the secondary slot.
392 */
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200393 if (smaller == 1) {
394 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors");
395 return 0;
396 }
397 smaller = 2;
398 j++;
399 }
400 if (sz0 == sz1) {
David Vincze2d736ad2019-02-18 11:50:22 +0100401 primary_slot_sz += sz0;
402 secondary_slot_sz += sz1;
403 /* Scratch has to fit each swap operation to the size of the larger
404 * sector among the primary slot and the secondary slot.
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200405 */
406 if (sz0 > scratch_sz || sz1 > scratch_sz) {
407 BOOT_LOG_WRN("Cannot upgrade: not all sectors fit inside scratch");
408 return 0;
409 }
410 smaller = sz0 = sz1 = 0;
411 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300412 }
413
David Vincze2d736ad2019-02-18 11:50:22 +0100414 if ((i != num_sectors_primary) ||
415 (j != num_sectors_secondary) ||
416 (primary_slot_sz != secondary_slot_sz)) {
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200417 BOOT_LOG_WRN("Cannot upgrade: slots are not compatible");
418 return 0;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800419 }
420
421 return 1;
422}
423
Christopher Collins92ea77f2016-12-12 15:59:26 -0800424/**
425 * Determines the sector layout of both image slots and the scratch area.
426 * This information is necessary for calculating the number of bytes to erase
427 * and copy during an image swap. The information collected during this
428 * function is used to populate the boot_data global.
429 */
430static int
431boot_read_sectors(void)
432{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800433 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800434
David Vincze2d736ad2019-02-18 11:50:22 +0100435 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_PRIMARY);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800436 if (rc != 0) {
437 return BOOT_EFLASH;
438 }
439
David Vincze2d736ad2019-02-18 11:50:22 +0100440 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_SECONDARY);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800441 if (rc != 0) {
442 return BOOT_EFLASH;
443 }
444
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200445 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_SCRATCH);
446 if (rc != 0) {
447 return BOOT_EFLASH;
448 }
449
Marti Bolivare10a7392017-06-14 16:20:07 -0400450 BOOT_WRITE_SZ(&boot_data) = boot_write_sz();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800451
452 return 0;
453}
454
455static uint32_t
456boot_status_internal_off(int idx, int state, int elem_sz)
457{
458 int idx_sz;
459
460 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
461
Fabio Utzig39000012018-07-30 12:40:20 -0300462 return (idx - BOOT_STATUS_IDX_0) * idx_sz +
463 (state - BOOT_STATUS_STATE_0) * elem_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800464}
465
466/**
467 * Reads the status of a partially-completed swap, if any. This is necessary
468 * to recover in case the boot lodaer was reset in the middle of a swap
469 * operation.
470 */
471static int
472boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs)
473{
474 uint32_t off;
475 uint8_t status;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300476 int max_entries;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800477 int found;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200478 int found_idx;
479 int invalid;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800480 int rc;
481 int i;
482
483 off = boot_status_off(fap);
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400484 max_entries = boot_status_entries(fap);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300485
Christopher Collins92ea77f2016-12-12 15:59:26 -0800486 found = 0;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200487 found_idx = 0;
488 invalid = 0;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300489 for (i = 0; i < max_entries; i++) {
Fabio Utzig178be542018-09-19 08:12:56 -0300490 rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(&boot_data),
491 &status, 1);
492 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800493 return BOOT_EFLASH;
494 }
495
Fabio Utzig178be542018-09-19 08:12:56 -0300496 if (rc == 1) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200497 if (found && !found_idx) {
498 found_idx = i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800499 }
500 } else if (!found) {
501 found = 1;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200502 } else if (found_idx) {
503 invalid = 1;
504 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800505 }
506 }
507
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200508 if (invalid) {
509 /* This means there was an error writing status on the last
510 * swap. Tell user and move on to validation!
511 */
512 BOOT_LOG_ERR("Detected inconsistent status!");
513
David Vincze2d736ad2019-02-18 11:50:22 +0100514#if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
515 /* With validation of the primary slot disabled, there is no way
516 * to be sure the swapped primary slot is OK, so abort!
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200517 */
518 assert(0);
519#endif
520 }
521
Christopher Collins92ea77f2016-12-12 15:59:26 -0800522 if (found) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200523 if (!found_idx) {
524 found_idx = i;
525 }
526 found_idx--;
Fabio Utzig39000012018-07-30 12:40:20 -0300527 bs->idx = (found_idx / BOOT_STATUS_STATE_COUNT) + 1;
528 bs->state = (found_idx % BOOT_STATUS_STATE_COUNT) + 1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800529 }
530
531 return 0;
532}
533
534/**
535 * Reads the boot status from the flash. The boot status contains
536 * the current state of an interrupted image copy operation. If the boot
537 * status is not present, or it indicates that previous copy finished,
538 * there is no operation in progress.
539 */
540static int
541boot_read_status(struct boot_status *bs)
542{
543 const struct flash_area *fap;
544 int status_loc;
545 int area_id;
546 int rc;
547
548 memset(bs, 0, sizeof *bs);
Fabio Utzig39000012018-07-30 12:40:20 -0300549 bs->idx = BOOT_STATUS_IDX_0;
550 bs->state = BOOT_STATUS_STATE_0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800551
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700552#ifdef MCUBOOT_OVERWRITE_ONLY
553 /* Overwrite-only doesn't make use of the swap status area. */
554 return 0;
555#endif
556
Christopher Collins92ea77f2016-12-12 15:59:26 -0800557 status_loc = boot_status_source();
558 switch (status_loc) {
559 case BOOT_STATUS_SOURCE_NONE:
560 return 0;
561
562 case BOOT_STATUS_SOURCE_SCRATCH:
563 area_id = FLASH_AREA_IMAGE_SCRATCH;
564 break;
565
David Vincze2d736ad2019-02-18 11:50:22 +0100566 case BOOT_STATUS_SOURCE_PRIMARY_SLOT:
567 area_id = FLASH_AREA_IMAGE_PRIMARY;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800568 break;
569
570 default:
571 assert(0);
572 return BOOT_EBADARGS;
573 }
574
575 rc = flash_area_open(area_id, &fap);
576 if (rc != 0) {
577 return BOOT_EFLASH;
578 }
579
Fabio Utzig46490722017-09-04 15:34:32 -0300580 rc = boot_read_status_bytes(fap, bs);
581
582 flash_area_close(fap);
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700583
Fabio Utzig46490722017-09-04 15:34:32 -0300584 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800585}
586
587/**
588 * Writes the supplied boot status to the flash file system. The boot status
589 * contains the current state of an in-progress image copy operation.
590 *
591 * @param bs The boot status to write.
592 *
593 * @return 0 on success; nonzero on failure.
594 */
595int
596boot_write_status(struct boot_status *bs)
597{
598 const struct flash_area *fap;
599 uint32_t off;
600 int area_id;
601 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300602 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700603 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300604 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800605
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300606 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100607 * the trailer. Since in the last step the primary slot is erased, the
608 * first two status writes go to the scratch which will be copied to
609 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300610 */
611
Fabio Utzig2473ac02017-05-02 12:45:02 -0300612 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800613 /* Write to scratch. */
614 area_id = FLASH_AREA_IMAGE_SCRATCH;
615 } else {
David Vincze2d736ad2019-02-18 11:50:22 +0100616 /* Write to the primary slot. */
617 area_id = FLASH_AREA_IMAGE_PRIMARY;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800618 }
619
620 rc = flash_area_open(area_id, &fap);
621 if (rc != 0) {
622 rc = BOOT_EFLASH;
623 goto done;
624 }
625
626 off = boot_status_off(fap) +
Marti Bolivare10a7392017-06-14 16:20:07 -0400627 boot_status_internal_off(bs->idx, bs->state,
628 BOOT_WRITE_SZ(&boot_data));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200629 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300630 erased_val = flash_area_erased_val(fap);
631 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700632 buf[0] = bs->state;
633
634 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800635 if (rc != 0) {
636 rc = BOOT_EFLASH;
637 goto done;
638 }
639
640 rc = 0;
641
642done:
643 flash_area_close(fap);
644 return rc;
645}
646
647/*
648 * Validate image hash/signature in a slot.
649 */
650static int
Fabio Utzigba829042018-09-18 08:29:34 -0300651boot_image_check(struct image_header *hdr, const struct flash_area *fap,
652 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800653{
David Browndb1d9d32017-01-06 11:07:54 -0700654 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigba829042018-09-18 08:29:34 -0300655 int rc;
656
657#ifndef MCUBOOT_ENC_IMAGES
658 (void)bs;
659 (void)rc;
660#else
David Vincze2d736ad2019-02-18 11:50:22 +0100661 if ((fap->fa_id == FLASH_AREA_IMAGE_SECONDARY) && IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300662 rc = boot_enc_load(hdr, fap, bs->enckey[1]);
663 if (rc < 0) {
664 return BOOT_EBADIMAGE;
665 }
666 if (rc == 0 && boot_enc_set_key(1, bs->enckey[1])) {
667 return BOOT_EBADIMAGE;
668 }
669 }
670#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800671
Christopher Collins92ea77f2016-12-12 15:59:26 -0800672 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
673 NULL, 0, NULL)) {
674 return BOOT_EBADIMAGE;
675 }
676 return 0;
677}
678
679static int
680split_image_check(struct image_header *app_hdr,
681 const struct flash_area *app_fap,
682 struct image_header *loader_hdr,
683 const struct flash_area *loader_fap)
684{
685 static void *tmpbuf;
686 uint8_t loader_hash[32];
687
688 if (!tmpbuf) {
689 tmpbuf = malloc(BOOT_TMPBUF_SZ);
690 if (!tmpbuf) {
691 return BOOT_ENOMEM;
692 }
693 }
694
695 if (bootutil_img_validate(loader_hdr, loader_fap, tmpbuf, BOOT_TMPBUF_SZ,
696 NULL, 0, loader_hash)) {
697 return BOOT_EBADIMAGE;
698 }
699
700 if (bootutil_img_validate(app_hdr, app_fap, tmpbuf, BOOT_TMPBUF_SZ,
701 loader_hash, 32, NULL)) {
702 return BOOT_EBADIMAGE;
703 }
704
705 return 0;
706}
707
Fabio Utzig338a19f2018-12-03 08:37:08 -0200708/*
709 * Check that a memory area consists of a given value.
710 */
711static inline bool
712boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300713{
714 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200715 uint8_t *p = (uint8_t *)data;
716 for (i = 0; i < len; i++) {
717 if (val != p[i]) {
718 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300719 }
720 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200721 return true;
722}
723
724static int
725boot_check_header_erased(int slot)
726{
727 const struct flash_area *fap;
728 struct image_header *hdr;
729 uint8_t erased_val;
730 int rc;
731
732 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
733 if (rc != 0) {
734 return -1;
735 }
736
737 erased_val = flash_area_erased_val(fap);
738 flash_area_close(fap);
739
740 hdr = boot_img_hdr(&boot_data, slot);
741 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
742 return -1;
743 }
744
745 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300746}
747
Christopher Collins92ea77f2016-12-12 15:59:26 -0800748static int
Fabio Utzigba829042018-09-18 08:29:34 -0300749boot_validate_slot(int slot, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800750{
751 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400752 struct image_header *hdr;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800753 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300754
David Brownd930ec62016-12-14 07:59:48 -0700755 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800756 if (rc != 0) {
757 return BOOT_EFLASH;
758 }
759
Fabio Utzig39000012018-07-30 12:40:20 -0300760 hdr = boot_img_hdr(&boot_data, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200761 if (boot_check_header_erased(slot) == 0 || (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100762 /* No bootable image in slot; continue booting from the primary slot. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200763 rc = -1;
764 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300765 }
766
Fabio Utzigba829042018-09-18 08:29:34 -0300767 if ((hdr->ih_magic != IMAGE_MAGIC || boot_image_check(hdr, fap, bs) != 0)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100768 if (slot != BOOT_PRIMARY_SLOT) {
David Brownb38e0442017-02-24 13:57:12 -0700769 flash_area_erase(fap, 0, fap->fa_size);
David Vincze2d736ad2019-02-18 11:50:22 +0100770 /* Image in the secondary slot is invalid. Erase the image and
771 * continue booting from the primary slot.
David Brownb38e0442017-02-24 13:57:12 -0700772 */
773 }
David Vincze2d736ad2019-02-18 11:50:22 +0100774 BOOT_LOG_ERR("Image in the %s slot is not valid!",
775 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
Fabio Utzig338a19f2018-12-03 08:37:08 -0200776 rc = -1;
777 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800778 }
779
David Vincze2d736ad2019-02-18 11:50:22 +0100780 /* Image in the secondary slot is valid. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200781 rc = 0;
782
783out:
784 flash_area_close(fap);
785 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800786}
787
788/**
789 * Determines which swap operation to perform, if any. If it is determined
David Vincze2d736ad2019-02-18 11:50:22 +0100790 * that a swap operation is required, the image in the secondary slot is checked
791 * for validity. If the image in the secondary slot is invalid, it is erased,
792 * and a swap type of "none" is indicated.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800793 *
794 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
795 */
796static int
Fabio Utzigba829042018-09-18 08:29:34 -0300797boot_validated_swap_type(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800798{
799 int swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800800
801 swap_type = boot_swap_type();
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300802 switch (swap_type) {
803 case BOOT_SWAP_TYPE_TEST:
804 case BOOT_SWAP_TYPE_PERM:
805 case BOOT_SWAP_TYPE_REVERT:
David Vincze2d736ad2019-02-18 11:50:22 +0100806 /* Boot loader wants to switch to the secondary slot.
807 * Ensure image is valid.
808 */
809 if (boot_validate_slot(BOOT_SECONDARY_SLOT, bs) != 0) {
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300810 swap_type = BOOT_SWAP_TYPE_FAIL;
811 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800812 }
813
814 return swap_type;
815}
816
817/**
818 * Calculates the number of sectors the scratch area can contain. A "last"
819 * source sector is specified because images are copied backwards in flash
820 * (final index to index number 0).
821 *
822 * @param last_sector_idx The index of the last source sector
823 * (inclusive).
824 * @param out_first_sector_idx The index of the first source sector
825 * (inclusive) gets written here.
826 *
827 * @return The number of bytes comprised by the
828 * [first-sector, last-sector] range.
829 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300830#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800831static uint32_t
832boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
833{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400834 size_t scratch_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800835 uint32_t new_sz;
836 uint32_t sz;
837 int i;
838
839 sz = 0;
840
Marti Bolivard3269fd2017-06-12 16:31:12 -0400841 scratch_sz = boot_scratch_area_size(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800842 for (i = last_sector_idx; i >= 0; i--) {
David Vincze2d736ad2019-02-18 11:50:22 +0100843 new_sz = sz + boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200844 /*
David Vincze2d736ad2019-02-18 11:50:22 +0100845 * The secondary slot is not being checked here, because
846 * `boot_slots_compatible` already provides assurance that the copy size
847 * will be compatible with the primary slot and scratch.
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200848 */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400849 if (new_sz > scratch_sz) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800850 break;
851 }
852 sz = new_sz;
853 }
854
855 /* i currently refers to a sector that doesn't fit or it is -1 because all
856 * sectors have been processed. In both cases, exclude sector i.
857 */
858 *out_first_sector_idx = i + 1;
859 return sz;
860}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300861#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800862
863/**
864 * Erases a region of flash.
865 *
Fabio Utzigba829042018-09-18 08:29:34 -0300866 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800867 * @param off The offset within the flash area to start the
868 * erase.
869 * @param sz The number of bytes to erase.
870 *
871 * @return 0 on success; nonzero on failure.
872 */
Fabio Utzigba829042018-09-18 08:29:34 -0300873static inline int
874boot_erase_sector(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800875{
Fabio Utzigba829042018-09-18 08:29:34 -0300876 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800877}
878
879/**
880 * Copies the contents of one flash region to another. You must erase the
881 * destination region prior to calling this function.
882 *
883 * @param flash_area_id_src The ID of the source flash area.
884 * @param flash_area_id_dst The ID of the destination flash area.
885 * @param off_src The offset within the source flash area to
886 * copy from.
887 * @param off_dst The offset within the destination flash area to
888 * copy to.
889 * @param sz The number of bytes to copy.
890 *
891 * @return 0 on success; nonzero on failure.
892 */
893static int
Fabio Utzigba829042018-09-18 08:29:34 -0300894boot_copy_sector(const struct flash_area *fap_src,
895 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800896 uint32_t off_src, uint32_t off_dst, uint32_t sz)
897{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800898 uint32_t bytes_copied;
899 int chunk_sz;
900 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -0300901#ifdef MCUBOOT_ENC_IMAGES
902 uint32_t off;
903 size_t blk_off;
904 struct image_header *hdr;
905 uint16_t idx;
906 uint32_t blk_sz;
907#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800908
909 static uint8_t buf[1024];
910
Christopher Collins92ea77f2016-12-12 15:59:26 -0800911 bytes_copied = 0;
912 while (bytes_copied < sz) {
913 if (sz - bytes_copied > sizeof buf) {
914 chunk_sz = sizeof buf;
915 } else {
916 chunk_sz = sz - bytes_copied;
917 }
918
919 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
920 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300921 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800922 }
923
Fabio Utzigba829042018-09-18 08:29:34 -0300924#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +0100925 if ((fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY) ||
926 (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY)) {
927 /* assume the secondary slot as src, needs decryption */
928 hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -0300929 off = off_src;
David Vincze2d736ad2019-02-18 11:50:22 +0100930 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY) {
931 /* might need encryption (metadata from the primary slot) */
932 hdr = boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -0300933 off = off_dst;
934 }
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200935 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300936 blk_sz = chunk_sz;
937 idx = 0;
938 if (off + bytes_copied < hdr->ih_hdr_size) {
939 /* do not decrypt header */
940 blk_off = 0;
941 blk_sz = chunk_sz - hdr->ih_hdr_size;
942 idx = hdr->ih_hdr_size;
943 } else {
944 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
945 }
946 if (off + bytes_copied + chunk_sz > hdr->ih_hdr_size + hdr->ih_img_size) {
947 /* do not decrypt TLVs */
948 if (off + bytes_copied >= hdr->ih_hdr_size + hdr->ih_img_size) {
949 blk_sz = 0;
950 } else {
951 blk_sz = (hdr->ih_hdr_size + hdr->ih_img_size) - (off + bytes_copied);
952 }
953 }
954 boot_encrypt(fap_src, (off + bytes_copied + idx) - hdr->ih_hdr_size,
955 blk_sz, blk_off, &buf[idx]);
956 }
957 }
958#endif
959
Christopher Collins92ea77f2016-12-12 15:59:26 -0800960 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
961 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300962 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800963 }
964
965 bytes_copied += chunk_sz;
966 }
967
Fabio Utzigba829042018-09-18 08:29:34 -0300968 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800969}
970
David Brown6b1b3b92017-09-19 08:59:10 -0600971#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -0300972static inline int
Fabio Utzigba829042018-09-18 08:29:34 -0300973boot_status_init(const struct flash_area *fap, const struct boot_status *bs)
Fabio Utzig2473ac02017-05-02 12:45:02 -0300974{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300975 struct boot_swap_state swap_state;
976 int rc;
977
David Vincze2d736ad2019-02-18 11:50:22 +0100978 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY, &swap_state);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300979 assert(rc == 0);
980
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400981 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300982 rc = boot_write_image_ok(fap);
983 assert(rc == 0);
984 }
985
Fabio Utzig46490722017-09-04 15:34:32 -0300986 rc = boot_write_swap_size(fap, bs->swap_size);
987 assert(rc == 0);
988
Fabio Utzigba829042018-09-18 08:29:34 -0300989#ifdef MCUBOOT_ENC_IMAGES
990 rc = boot_write_enc_key(fap, 0, bs->enckey[0]);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300991 assert(rc == 0);
992
Fabio Utzigba829042018-09-18 08:29:34 -0300993 rc = boot_write_enc_key(fap, 1, bs->enckey[1]);
994 assert(rc == 0);
995#endif
996
997 rc = boot_write_magic(fap);
998 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300999
1000 return 0;
1001}
David Brown6b1b3b92017-09-19 08:59:10 -06001002#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001003
Fabio Utzig358c9352017-07-25 22:10:45 -03001004#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -03001005static int
Fabio Utziged0ca432019-01-23 14:50:11 -02001006boot_erase_trailer_sectors(const struct flash_area *fap)
Fabio Utzig2473ac02017-05-02 12:45:02 -03001007{
1008 uint8_t slot;
Fabio Utziged0ca432019-01-23 14:50:11 -02001009 uint32_t sector;
1010 uint32_t trailer_sz;
1011 uint32_t total_sz;
1012 uint32_t off;
1013 uint32_t sz;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001014 int rc;
1015
Fabio Utzigba829042018-09-18 08:29:34 -03001016 switch (fap->fa_id) {
David Vincze2d736ad2019-02-18 11:50:22 +01001017 case FLASH_AREA_IMAGE_PRIMARY:
1018 slot = BOOT_PRIMARY_SLOT;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001019 break;
David Vincze2d736ad2019-02-18 11:50:22 +01001020 case FLASH_AREA_IMAGE_SECONDARY:
1021 slot = BOOT_SECONDARY_SLOT;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001022 break;
1023 default:
1024 return BOOT_EFLASH;
1025 }
1026
Fabio Utziged0ca432019-01-23 14:50:11 -02001027 /* delete starting from last sector and moving to beginning */
1028 sector = boot_img_num_sectors(&boot_data, slot) - 1;
1029 trailer_sz = boot_slots_trailer_sz(BOOT_WRITE_SZ(&boot_data));
1030 total_sz = 0;
1031 do {
1032 sz = boot_img_sector_size(&boot_data, slot, sector);
1033 off = boot_img_sector_off(&boot_data, slot, sector);
1034 rc = boot_erase_sector(fap, off, sz);
1035 assert(rc == 0);
1036
1037 sector--;
1038 total_sz += sz;
1039 } while (total_sz < trailer_sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001040
1041 return rc;
1042}
Fabio Utzig358c9352017-07-25 22:10:45 -03001043#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001044
Christopher Collins92ea77f2016-12-12 15:59:26 -08001045/**
1046 * Swaps the contents of two flash regions within the two image slots.
1047 *
1048 * @param idx The index of the first sector in the range of
1049 * sectors being swapped.
1050 * @param sz The number of bytes to swap.
1051 * @param bs The current boot status. This struct gets
1052 * updated according to the outcome.
1053 *
1054 * @return 0 on success; nonzero on failure.
1055 */
Fabio Utzig3488eef2017-06-12 10:25:43 -03001056#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -08001057static void
Christopher Collins92ea77f2016-12-12 15:59:26 -08001058boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
1059{
David Vincze2d736ad2019-02-18 11:50:22 +01001060 const struct flash_area *fap_primary_slot;
1061 const struct flash_area *fap_secondary_slot;
Fabio Utzigba829042018-09-18 08:29:34 -03001062 const struct flash_area *fap_scratch;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001063 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001064 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001065 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001066 uint32_t scratch_trailer_off;
1067 struct boot_swap_state swap_state;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001068 size_t last_sector;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001069 int rc;
1070
1071 /* Calculate offset from start of image area. */
David Vincze2d736ad2019-02-18 11:50:22 +01001072 img_off = boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT, idx);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001073
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001074 copy_sz = sz;
Marti Bolivare10a7392017-06-14 16:20:07 -04001075 trailer_sz = boot_slots_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Fabio Utzig9678c972017-05-23 11:28:56 -04001076
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001077 /* sz in this function is always sized on a multiple of the sector size.
1078 * The check against the start offset of the last sector
Fabio Utzig9678c972017-05-23 11:28:56 -04001079 * is to determine if we're swapping the last sector. The last sector
1080 * needs special handling because it's where the trailer lives. If we're
1081 * copying it, we need to use scratch to write the trailer temporarily.
1082 *
1083 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
1084 * controls if special handling is needed (swapping last sector).
1085 */
David Vincze2d736ad2019-02-18 11:50:22 +01001086 last_sector = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT) - 1;
1087 if (img_off + sz > boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT,
1088 last_sector)) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001089 copy_sz -= trailer_sz;
1090 }
1091
Fabio Utzig39000012018-07-30 12:40:20 -03001092 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001093
David Vincze2d736ad2019-02-18 11:50:22 +01001094 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001095 assert (rc == 0);
1096
David Vincze2d736ad2019-02-18 11:50:22 +01001097 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001098 assert (rc == 0);
1099
1100 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch);
1101 assert (rc == 0);
1102
Fabio Utzig39000012018-07-30 12:40:20 -03001103 if (bs->state == BOOT_STATUS_STATE_0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001104 rc = boot_erase_sector(fap_scratch, 0, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001105 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001106
David Vincze2d736ad2019-02-18 11:50:22 +01001107 rc = boot_copy_sector(fap_secondary_slot, fap_scratch,
1108 img_off, 0, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001109 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001110
Fabio Utzig39000012018-07-30 12:40:20 -03001111 if (bs->idx == BOOT_STATUS_IDX_0) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001112 if (bs->use_scratch) {
Fabio Utzigba829042018-09-18 08:29:34 -03001113 boot_status_init(fap_scratch, bs);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001114 } else {
1115 /* Prepare the status area... here it is known that the
1116 * last sector is not being used by the image data so it's
1117 * safe to erase.
1118 */
David Vincze2d736ad2019-02-18 11:50:22 +01001119 rc = boot_erase_trailer_sectors(fap_primary_slot);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001120 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001121
David Vincze2d736ad2019-02-18 11:50:22 +01001122 boot_status_init(fap_primary_slot, bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001123 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001124 }
1125
Fabio Utzig39000012018-07-30 12:40:20 -03001126 bs->state = BOOT_STATUS_STATE_1;
Christopher Collins4772ac42017-02-27 20:08:01 -08001127 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001128 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001129 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001130
Fabio Utzig39000012018-07-30 12:40:20 -03001131 if (bs->state == BOOT_STATUS_STATE_1) {
David Vincze2d736ad2019-02-18 11:50:22 +01001132 rc = boot_erase_sector(fap_secondary_slot, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001133 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001134
David Vincze2d736ad2019-02-18 11:50:22 +01001135 rc = boot_copy_sector(fap_primary_slot, fap_secondary_slot,
1136 img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001137 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001138
Fabio Utzig39000012018-07-30 12:40:20 -03001139 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001140 /* If not all sectors of the slot are being swapped,
David Vincze2d736ad2019-02-18 11:50:22 +01001141 * guarantee here that only the primary slot will have the state.
Fabio Utzig2473ac02017-05-02 12:45:02 -03001142 */
David Vincze2d736ad2019-02-18 11:50:22 +01001143 rc = boot_erase_trailer_sectors(fap_secondary_slot);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001144 assert(rc == 0);
1145 }
1146
Fabio Utzig39000012018-07-30 12:40:20 -03001147 bs->state = BOOT_STATUS_STATE_2;
Christopher Collins4772ac42017-02-27 20:08:01 -08001148 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001149 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001150 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001151
Fabio Utzig39000012018-07-30 12:40:20 -03001152 if (bs->state == BOOT_STATUS_STATE_2) {
David Vincze2d736ad2019-02-18 11:50:22 +01001153 rc = boot_erase_sector(fap_primary_slot, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001154 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001155
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001156 /* NOTE: also copy trailer from scratch (has status info) */
David Vincze2d736ad2019-02-18 11:50:22 +01001157 rc = boot_copy_sector(fap_scratch, fap_primary_slot,
1158 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001159 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001160
Fabio Utzig94d998c2017-05-22 11:02:41 -04001161 if (bs->use_scratch) {
Fabio Utzigba829042018-09-18 08:29:34 -03001162 scratch_trailer_off = boot_status_off(fap_scratch);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001163
1164 /* copy current status that is being maintained in scratch */
David Vincze2d736ad2019-02-18 11:50:22 +01001165 rc = boot_copy_sector(fap_scratch, fap_primary_slot,
1166 scratch_trailer_off, img_off + copy_sz,
Marti Bolivare10a7392017-06-14 16:20:07 -04001167 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data));
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001168 BOOT_STATUS_ASSERT(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001169
Fabio Utzig2473ac02017-05-02 12:45:02 -03001170 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1171 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001172 assert(rc == 0);
1173
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001174 if (swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze2d736ad2019-02-18 11:50:22 +01001175 rc = boot_write_image_ok(fap_primary_slot);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001176 assert(rc == 0);
1177 }
1178
David Vincze2d736ad2019-02-18 11:50:22 +01001179 rc = boot_write_swap_size(fap_primary_slot, bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001180 assert(rc == 0);
1181
Fabio Utzigba829042018-09-18 08:29:34 -03001182#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +01001183 rc = boot_write_enc_key(fap_primary_slot, 0, bs->enckey[0]);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001184 assert(rc == 0);
1185
David Vincze2d736ad2019-02-18 11:50:22 +01001186 rc = boot_write_enc_key(fap_primary_slot, 1, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001187 assert(rc == 0);
1188#endif
1189
David Vincze2d736ad2019-02-18 11:50:22 +01001190 rc = boot_write_magic(fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001191 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001192 }
1193
Christopher Collins92ea77f2016-12-12 15:59:26 -08001194 bs->idx++;
Fabio Utzig39000012018-07-30 12:40:20 -03001195 bs->state = BOOT_STATUS_STATE_0;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001196 bs->use_scratch = 0;
Christopher Collins4772ac42017-02-27 20:08:01 -08001197 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001198 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001199 }
Fabio Utzigba829042018-09-18 08:29:34 -03001200
David Vincze2d736ad2019-02-18 11:50:22 +01001201 flash_area_close(fap_primary_slot);
1202 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001203 flash_area_close(fap_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001204}
Fabio Utzig3488eef2017-06-12 10:25:43 -03001205#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001206
1207/**
David Vincze2d736ad2019-02-18 11:50:22 +01001208 * Overwrite primary slot with the image contained in the secondary slot.
1209 * If a prior copy operation was interrupted by a system reset, this function
1210 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001211 *
1212 * @param bs The current boot status. This function reads
1213 * this struct to determine if it is resuming
1214 * an interrupted swap operation. This
1215 * function writes the updated status to this
1216 * function on return.
1217 *
1218 * @return 0 on success; nonzero on failure.
1219 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001220#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001221static int
1222boot_copy_image(struct boot_status *bs)
1223{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001224 size_t sect_count;
1225 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001226 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001227 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001228 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001229 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +01001230 const struct flash_area *fap_primary_slot;
1231 const struct flash_area *fap_secondary_slot;
1232
Fabio Utzig13d9e352017-10-05 20:32:31 -03001233
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001234 (void)bs;
1235
Fabio Utzig13d9e352017-10-05 20:32:31 -03001236#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1237 uint32_t src_size = 0;
David Vincze2d736ad2019-02-18 11:50:22 +01001238 rc = boot_read_image_size(BOOT_SECONDARY_SLOT,
1239 boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT),
1240 &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001241 assert(rc == 0);
1242#endif
David Brown17609d82017-05-05 09:41:34 -06001243
David Vincze2d736ad2019-02-18 11:50:22 +01001244 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1245 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -06001246
David Vincze2d736ad2019-02-18 11:50:22 +01001247 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001248 assert (rc == 0);
1249
David Vincze2d736ad2019-02-18 11:50:22 +01001250 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001251 assert (rc == 0);
1252
David Vincze2d736ad2019-02-18 11:50:22 +01001253 sect_count = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001254 for (sect = 0, size = 0; sect < sect_count; sect++) {
David Vincze2d736ad2019-02-18 11:50:22 +01001255 this_size = boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, sect);
1256 rc = boot_erase_sector(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001257 assert(rc == 0);
1258
1259 size += this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001260
1261#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1262 if (size >= src_size) {
1263 break;
1264 }
1265#endif
David Brown17609d82017-05-05 09:41:34 -06001266 }
1267
Fabio Utzigba829042018-09-18 08:29:34 -03001268#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +01001269 if (IS_ENCRYPTED(boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT))) {
1270 rc = boot_enc_load(boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT),
1271 fap_secondary_slot,
1272 bs->enckey[1]);
1273
Fabio Utzigba829042018-09-18 08:29:34 -03001274 if (rc < 0) {
1275 return BOOT_EBADIMAGE;
1276 }
Fabio Utzige641ea52018-12-03 10:37:53 -02001277 if (rc == 0 && boot_enc_set_key(1, bs->enckey[1])) {
Fabio Utzigba829042018-09-18 08:29:34 -03001278 return BOOT_EBADIMAGE;
1279 }
1280 }
1281#endif
1282
David Vincze2d736ad2019-02-18 11:50:22 +01001283 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1284 size);
1285 rc = boot_copy_sector(fap_secondary_slot, fap_primary_slot, 0, 0, size);
David Brown17609d82017-05-05 09:41:34 -06001286
Fabio Utzig13d9e352017-10-05 20:32:31 -03001287 /*
1288 * Erases header and trailer. The trailer is erased because when a new
1289 * image is written without a trailer as is the case when using newt, the
1290 * trailer that was left might trigger a new upgrade.
1291 */
David Vincze2d736ad2019-02-18 11:50:22 +01001292 rc = boot_erase_sector(fap_secondary_slot,
1293 boot_img_sector_off(&boot_data,
1294 BOOT_SECONDARY_SLOT, 0),
1295 boot_img_sector_size(&boot_data,
1296 BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001297 assert(rc == 0);
David Vincze2d736ad2019-02-18 11:50:22 +01001298 last_sector = boot_img_num_sectors(&boot_data, BOOT_SECONDARY_SLOT) - 1;
1299 rc = boot_erase_sector(fap_secondary_slot,
1300 boot_img_sector_off(&boot_data,
1301 BOOT_SECONDARY_SLOT, last_sector),
1302 boot_img_sector_size(&boot_data,
1303 BOOT_SECONDARY_SLOT, last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001304 assert(rc == 0);
1305
David Vincze2d736ad2019-02-18 11:50:22 +01001306 flash_area_close(fap_primary_slot);
1307 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001308
David Vincze2d736ad2019-02-18 11:50:22 +01001309 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001310
1311 return 0;
1312}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001313#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001314
1315/**
1316 * Swaps the two images in flash. If a prior copy operation was interrupted
1317 * by a system reset, this function completes that operation.
1318 *
1319 * @param bs The current boot status. This function reads
1320 * this struct to determine if it is resuming
1321 * an interrupted swap operation. This
1322 * function writes the updated status to this
1323 * function on return.
1324 *
1325 * @return 0 on success; nonzero on failure.
1326 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001327#if !defined(MCUBOOT_OVERWRITE_ONLY)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001328static int
Fabio Utzig338a19f2018-12-03 08:37:08 -02001329boot_swap_image(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001330{
1331 uint32_t sz;
1332 int first_sector_idx;
1333 int last_sector_idx;
David Vincze2d736ad2019-02-18 11:50:22 +01001334 int last_idx_secondary_slot;
Fabio Utzigcd5774b2017-11-29 10:18:26 -02001335 uint32_t swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001336 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001337#ifdef MCUBOOT_ENC_IMAGES
1338 const struct flash_area *fap;
1339 uint8_t slot;
1340 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001341#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001342 uint32_t size;
1343 uint32_t copy_size;
David Vincze2d736ad2019-02-18 11:50:22 +01001344 uint32_t primary_slot_size;
1345 uint32_t secondary_slot_size;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001346 int rc;
1347
1348 /* FIXME: just do this if asked by user? */
1349
1350 size = copy_size = 0;
1351
Fabio Utzig39000012018-07-30 12:40:20 -03001352 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Fabio Utzig46490722017-09-04 15:34:32 -03001353 /*
1354 * No swap ever happened, so need to find the largest image which
1355 * will be used to determine the amount of sectors to swap.
1356 */
David Vincze2d736ad2019-02-18 11:50:22 +01001357 hdr = boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001358 if (hdr->ih_magic == IMAGE_MAGIC) {
David Vincze2d736ad2019-02-18 11:50:22 +01001359 rc = boot_read_image_size(BOOT_PRIMARY_SLOT, hdr, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001360 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001361 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001362
Fabio Utzigba829042018-09-18 08:29:34 -03001363#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001364 if (IS_ENCRYPTED(hdr)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001365 fap = BOOT_IMG_AREA(&boot_data, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001366 rc = boot_enc_load(hdr, fap, bs->enckey[0]);
1367 assert(rc >= 0);
1368
1369 if (rc == 0) {
1370 rc = boot_enc_set_key(0, bs->enckey[0]);
1371 assert(rc == 0);
1372 } else {
1373 rc = 0;
1374 }
1375 } else {
1376 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1377 }
1378#endif
1379
David Vincze2d736ad2019-02-18 11:50:22 +01001380 hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001381 if (hdr->ih_magic == IMAGE_MAGIC) {
David Vincze2d736ad2019-02-18 11:50:22 +01001382 rc = boot_read_image_size(BOOT_SECONDARY_SLOT, hdr, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001383 assert(rc == 0);
1384 }
1385
Fabio Utzigba829042018-09-18 08:29:34 -03001386#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +01001387 hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001388 if (IS_ENCRYPTED(hdr)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001389 fap = BOOT_IMG_AREA(&boot_data, BOOT_SECONDARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001390 rc = boot_enc_load(hdr, fap, bs->enckey[1]);
1391 assert(rc >= 0);
1392
1393 if (rc == 0) {
1394 rc = boot_enc_set_key(1, bs->enckey[1]);
1395 assert(rc == 0);
1396 } else {
1397 rc = 0;
1398 }
1399 } else {
1400 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1401 }
1402#endif
1403
Fabio Utzig46490722017-09-04 15:34:32 -03001404 if (size > copy_size) {
1405 copy_size = size;
1406 }
1407
1408 bs->swap_size = copy_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001409
Fabio Utzig46490722017-09-04 15:34:32 -03001410 } else {
1411 /*
1412 * If a swap was under way, the swap_size should already be present
1413 * in the trailer...
1414 */
1415 rc = boot_read_swap_size(&bs->swap_size);
1416 assert(rc == 0);
1417
1418 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001419
1420#ifdef MCUBOOT_ENC_IMAGES
1421 for (slot = 0; slot <= 1; slot++) {
1422 rc = boot_read_enc_key(slot, bs->enckey[slot]);
1423 assert(rc == 0);
1424
1425 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001426 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001427 break;
1428 }
1429 }
1430
1431 if (i != BOOT_ENC_KEY_SIZE) {
1432 boot_enc_set_key(slot, bs->enckey[slot]);
1433 }
1434 }
1435#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001436 }
1437
David Vincze2d736ad2019-02-18 11:50:22 +01001438 primary_slot_size = 0;
1439 secondary_slot_size = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001440 last_sector_idx = 0;
David Vincze2d736ad2019-02-18 11:50:22 +01001441 last_idx_secondary_slot = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001442
1443 /*
1444 * Knowing the size of the largest image between both slots, here we
David Vincze2d736ad2019-02-18 11:50:22 +01001445 * find what is the last sector in the primary slot that needs swapping.
1446 * Since we already know that both slots are compatible, the secondary
1447 * slot's last sector is not really required after this check is finished.
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001448 */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001449 while (1) {
David Vincze2d736ad2019-02-18 11:50:22 +01001450 if ((primary_slot_size < copy_size) ||
1451 (primary_slot_size < secondary_slot_size)) {
1452 primary_slot_size += boot_img_sector_size(&boot_data,
1453 BOOT_PRIMARY_SLOT,
1454 last_sector_idx);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001455 }
David Vincze2d736ad2019-02-18 11:50:22 +01001456 if ((secondary_slot_size < copy_size) ||
1457 (secondary_slot_size < primary_slot_size)) {
1458 secondary_slot_size += boot_img_sector_size(&boot_data,
1459 BOOT_SECONDARY_SLOT,
1460 last_idx_secondary_slot);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001461 }
David Vincze2d736ad2019-02-18 11:50:22 +01001462 if (primary_slot_size >= copy_size &&
1463 secondary_slot_size >= copy_size &&
1464 primary_slot_size == secondary_slot_size) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001465 break;
1466 }
1467 last_sector_idx++;
David Vincze2d736ad2019-02-18 11:50:22 +01001468 last_idx_secondary_slot++;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001469 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001470
1471 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001472 while (last_sector_idx >= 0) {
1473 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
Fabio Utzig39000012018-07-30 12:40:20 -03001474 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001475 boot_swap_sectors(first_sector_idx, sz, bs);
1476 }
1477
1478 last_sector_idx = first_sector_idx - 1;
1479 swap_idx++;
1480 }
1481
David Vincze2d736ad2019-02-18 11:50:22 +01001482#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001483 if (boot_status_fails > 0) {
1484 BOOT_LOG_WRN("%d status write fails performing the swap", boot_status_fails);
1485 }
1486#endif
1487
Christopher Collins92ea77f2016-12-12 15:59:26 -08001488 return 0;
1489}
David Brown17609d82017-05-05 09:41:34 -06001490#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001491
1492/**
David Vincze2d736ad2019-02-18 11:50:22 +01001493 * Marks the image in the primary slot as fully copied.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001494 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001495#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001496static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001497boot_set_copy_done(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001498{
1499 const struct flash_area *fap;
1500 int rc;
1501
David Vincze2d736ad2019-02-18 11:50:22 +01001502 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001503 if (rc != 0) {
1504 return BOOT_EFLASH;
1505 }
1506
1507 rc = boot_write_copy_done(fap);
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001508 flash_area_close(fap);
1509 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001510}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001511#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001512
1513/**
David Vincze2d736ad2019-02-18 11:50:22 +01001514 * Marks a reverted image in the primary slot as confirmed. This is necessary to
1515 * ensure the status bytes from the image revert operation don't get processed
1516 * on a subsequent boot.
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001517 *
1518 * NOTE: image_ok is tested before writing because if there's a valid permanent
David Vincze2d736ad2019-02-18 11:50:22 +01001519 * image installed on the primary slot and the new image to be upgrade to has a
1520 * bad sig, image_ok would be overwritten.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001521 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001522#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001523static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001524boot_set_image_ok(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001525{
1526 const struct flash_area *fap;
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001527 struct boot_swap_state state;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001528 int rc;
1529
David Vincze2d736ad2019-02-18 11:50:22 +01001530 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001531 if (rc != 0) {
1532 return BOOT_EFLASH;
1533 }
1534
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001535 rc = boot_read_swap_state(fap, &state);
1536 if (rc != 0) {
1537 rc = BOOT_EFLASH;
1538 goto out;
1539 }
1540
1541 if (state.image_ok == BOOT_FLAG_UNSET) {
1542 rc = boot_write_image_ok(fap);
1543 }
1544
1545out:
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001546 flash_area_close(fap);
1547 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001548}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001549#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001550
1551/**
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001552 * Performs an image swap if one is required.
1553 *
1554 * @param out_swap_type On success, the type of swap performed gets
1555 * written here.
1556 *
1557 * @return 0 on success; nonzero on failure.
1558 */
1559static int
1560boot_swap_if_needed(int *out_swap_type)
1561{
1562 struct boot_status bs;
1563 int swap_type;
1564 int rc;
1565
1566 /* Determine if we rebooted in the middle of an image swap
1567 * operation.
1568 */
1569 rc = boot_read_status(&bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001570 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001571 if (rc != 0) {
1572 return rc;
1573 }
1574
1575 /* If a partial swap was detected, complete it. */
Fabio Utzig39000012018-07-30 12:40:20 -03001576 if (bs.idx != BOOT_STATUS_IDX_0 || bs.state != BOOT_STATUS_STATE_0) {
Fabio Utziga32f1af2019-01-07 06:50:58 -02001577#ifdef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig338a19f2018-12-03 08:37:08 -02001578 /* Should never arrive here, overwrite-only mode has no swap state. */
1579 assert(0);
1580#else
1581 rc = boot_swap_image(&bs);
1582#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001583 assert(rc == 0);
1584
Fabio Utzigb5b2f552017-06-30 10:03:47 -03001585 /* NOTE: here we have finished a swap resume. The initial request
1586 * was either a TEST or PERM swap, which now after the completed
1587 * swap will be determined to be respectively REVERT (was TEST)
1588 * or NONE (was PERM).
1589 */
1590
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001591 /* Extrapolate the type of the partial swap. We need this
1592 * information to know how to mark the swap complete in flash.
1593 */
1594 swap_type = boot_previous_swap_type();
1595 } else {
Fabio Utzigba829042018-09-18 08:29:34 -03001596 swap_type = boot_validated_swap_type(&bs);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001597 switch (swap_type) {
1598 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001599 case BOOT_SWAP_TYPE_PERM:
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001600 case BOOT_SWAP_TYPE_REVERT:
Fabio Utziga32f1af2019-01-07 06:50:58 -02001601#ifdef MCUBOOT_OVERWRITE_ONLY
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001602 rc = boot_copy_image(&bs);
Fabio Utzig338a19f2018-12-03 08:37:08 -02001603#else
1604 rc = boot_swap_image(&bs);
1605#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001606 assert(rc == 0);
1607 break;
Fabio Utzig338a19f2018-12-03 08:37:08 -02001608#ifdef MCUBOOT_BOOTSTRAP
1609 case BOOT_SWAP_TYPE_NONE:
1610 /*
1611 * Header checks are done first because they are inexpensive.
1612 * Since overwrite-only copies starting from offset 0, if
1613 * interrupted, it might leave a valid header magic, so also
David Vincze2d736ad2019-02-18 11:50:22 +01001614 * run validation on the primary slot to be sure it's not OK.
Fabio Utzig338a19f2018-12-03 08:37:08 -02001615 */
David Vincze2d736ad2019-02-18 11:50:22 +01001616 if (boot_check_header_erased(BOOT_PRIMARY_SLOT) == 0 ||
1617 boot_validate_slot(BOOT_PRIMARY_SLOT, &bs) != 0) {
1618 if ((boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT)->ih_magic
1619 == IMAGE_MAGIC ) &&
1620 (boot_validate_slot(BOOT_SECONDARY_SLOT, &bs) == 0)) {
Fabio Utzig338a19f2018-12-03 08:37:08 -02001621 rc = boot_copy_image(&bs);
1622 assert(rc == 0);
1623
1624 /* Returns fail here to trigger a re-read of the headers. */
1625 swap_type = BOOT_SWAP_TYPE_FAIL;
1626 }
1627 }
1628 break;
1629#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001630 }
1631 }
1632
1633 *out_swap_type = swap_type;
1634 return 0;
1635}
1636
1637/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08001638 * Prepares the booting process. This function moves images around in flash as
1639 * appropriate, and tells you what address to boot from.
1640 *
1641 * @param rsp On success, indicates how booting should occur.
1642 *
1643 * @return 0 on success; nonzero on failure.
1644 */
1645int
1646boot_go(struct boot_rsp *rsp)
1647{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001648 int swap_type;
Marti Bolivar84898652017-06-13 17:20:22 -04001649 size_t slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001650 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001651 int fa_id;
David Brown52eee562017-07-05 11:25:09 -06001652 bool reload_headers = false;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001653
1654 /* The array of slot sectors are defined here (as opposed to file scope) so
1655 * that they don't get allocated for non-boot-loader apps. This is
1656 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001657 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001658 */
David Vincze2d736ad2019-02-18 11:50:22 +01001659 static boot_sector_t primary_slot_sectors[BOOT_MAX_IMG_SECTORS];
1660 static boot_sector_t secondary_slot_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001661 static boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
David Vincze2d736ad2019-02-18 11:50:22 +01001662 boot_data.imgs[BOOT_PRIMARY_SLOT].sectors = primary_slot_sectors;
1663 boot_data.imgs[BOOT_SECONDARY_SLOT].sectors = secondary_slot_sectors;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001664 boot_data.scratch.sectors = scratch_sectors;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001665
Fabio Utzigba829042018-09-18 08:29:34 -03001666#ifdef MCUBOOT_ENC_IMAGES
1667 /* FIXME: remove this after RAM is cleared by sim */
1668 boot_enc_zeroize();
1669#endif
1670
Marti Bolivarc0b47912017-06-13 17:18:09 -04001671 /* Open boot_data image areas for the duration of this call. */
1672 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1673 fa_id = flash_area_id_from_image_slot(slot);
1674 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot));
1675 assert(rc == 0);
1676 }
1677 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
1678 &BOOT_SCRATCH_AREA(&boot_data));
1679 assert(rc == 0);
1680
Christopher Collins92ea77f2016-12-12 15:59:26 -08001681 /* Determine the sector layout of the image slots and scratch area. */
1682 rc = boot_read_sectors();
1683 if (rc != 0) {
Fabio Utziga1fae672018-03-30 10:52:38 -03001684 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - too small?",
1685 BOOT_MAX_IMG_SECTORS);
Marti Bolivarc0b47912017-06-13 17:18:09 -04001686 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001687 }
1688
1689 /* Attempt to read an image header from each slot. */
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001690 rc = boot_read_image_headers(false);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001691 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001692 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001693 }
1694
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001695 /* If the image slots aren't compatible, no swap is possible. Just boot
David Vincze2d736ad2019-02-18 11:50:22 +01001696 * into the primary slot.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001697 */
1698 if (boot_slots_compatible()) {
1699 rc = boot_swap_if_needed(&swap_type);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001700 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001701 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001702 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001703 }
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001704
1705 /*
1706 * The following states need image_ok be explicitly set after the
1707 * swap was finished to avoid a new revert.
1708 */
David Vincze2d736ad2019-02-18 11:50:22 +01001709 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1710 swap_type == BOOT_SWAP_TYPE_FAIL) {
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001711#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig695d5642017-07-20 09:47:16 -03001712 rc = boot_set_image_ok();
1713 if (rc != 0) {
1714 swap_type = BOOT_SWAP_TYPE_PANIC;
1715 }
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001716#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001717 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001718 } else {
1719 swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001720 }
1721
1722 switch (swap_type) {
1723 case BOOT_SWAP_TYPE_NONE:
David Vincze2d736ad2019-02-18 11:50:22 +01001724 slot = BOOT_PRIMARY_SLOT;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001725 break;
1726
Fabio Utzig695d5642017-07-20 09:47:16 -03001727 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1728 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001729 case BOOT_SWAP_TYPE_REVERT:
David Vincze2d736ad2019-02-18 11:50:22 +01001730 slot = BOOT_SECONDARY_SLOT;
David Brown52eee562017-07-05 11:25:09 -06001731 reload_headers = true;
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001732#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig695d5642017-07-20 09:47:16 -03001733 rc = boot_set_copy_done();
1734 if (rc != 0) {
1735 swap_type = BOOT_SWAP_TYPE_PANIC;
1736 }
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001737#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001738 break;
1739
1740 case BOOT_SWAP_TYPE_FAIL:
David Vincze2d736ad2019-02-18 11:50:22 +01001741 /* The image in the secondary slot was invalid and is now erased.
1742 * Ensure we don't try to boot into it again on the next reboot.
1743 * Do this by pretending we just reverted back to the primary slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001744 */
David Vincze2d736ad2019-02-18 11:50:22 +01001745 slot = BOOT_PRIMARY_SLOT;
David Brown52eee562017-07-05 11:25:09 -06001746 reload_headers = true;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001747 break;
1748
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001749 default:
Fabio Utzig695d5642017-07-20 09:47:16 -03001750 swap_type = BOOT_SWAP_TYPE_PANIC;
1751 }
1752
1753 if (swap_type == BOOT_SWAP_TYPE_PANIC) {
1754 BOOT_LOG_ERR("panic!");
Fabio Utzigb5b2f552017-06-30 10:03:47 -03001755 assert(0);
Fabio Utzig695d5642017-07-20 09:47:16 -03001756
1757 /* Loop forever... */
1758 while (1) {}
Christopher Collins92ea77f2016-12-12 15:59:26 -08001759 }
1760
David Brown52eee562017-07-05 11:25:09 -06001761 if (reload_headers) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001762 rc = boot_read_image_headers(false);
Fabio Utzigc6a7b0c2017-09-13 19:01:15 -03001763 if (rc != 0) {
1764 goto out;
1765 }
1766 /* Since headers were reloaded, it can be assumed we just performed a
1767 * swap or overwrite. Now the header info that should be used to
David Vincze2d736ad2019-02-18 11:50:22 +01001768 * provide the data for the bootstrap, which previously was at the
1769 * secondary slot, was updated to the primary slot.
Fabio Utzigc6a7b0c2017-09-13 19:01:15 -03001770 */
David Vincze2d736ad2019-02-18 11:50:22 +01001771 slot = BOOT_PRIMARY_SLOT;
David Brown52eee562017-07-05 11:25:09 -06001772 }
1773
David Vincze2d736ad2019-02-18 11:50:22 +01001774#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
1775 rc = boot_validate_slot(BOOT_PRIMARY_SLOT, NULL);
David Brown554c52e2017-06-30 16:01:07 -06001776 if (rc != 0) {
1777 rc = BOOT_EBADIMAGE;
1778 goto out;
1779 }
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001780#else
David Vincze2d736ad2019-02-18 11:50:22 +01001781 /* Even if we're not re-validating the primary slot, we could be booting
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001782 * onto an empty flash chip. At least do a basic sanity check that
1783 * the magic number on the image is OK.
1784 */
David Vincze2d736ad2019-02-18 11:50:22 +01001785 if (boot_data.imgs[BOOT_PRIMARY_SLOT].hdr.ih_magic != IMAGE_MAGIC) {
1786 BOOT_LOG_ERR("bad image magic 0x%lx",
1787 (unsigned long)boot_data.imgs[BOOT_PRIMARY_SLOT].hdr.ih_magic);
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001788 rc = BOOT_EBADIMAGE;
1789 goto out;
1790 }
David Brown554c52e2017-06-30 16:01:07 -06001791#endif
1792
Christopher Collins92ea77f2016-12-12 15:59:26 -08001793 /* Always boot from the primary slot. */
David Vincze2d736ad2019-02-18 11:50:22 +01001794 rsp->br_flash_dev_id = boot_data.imgs[BOOT_PRIMARY_SLOT].area->fa_device_id;
1795 rsp->br_image_off = boot_img_slot_off(&boot_data, BOOT_PRIMARY_SLOT);
Marti Bolivarf804f622017-06-12 15:41:48 -04001796 rsp->br_hdr = boot_img_hdr(&boot_data, slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001797
Marti Bolivarc0b47912017-06-13 17:18:09 -04001798 out:
1799 flash_area_close(BOOT_SCRATCH_AREA(&boot_data));
1800 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1801 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
1802 }
1803 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001804}
1805
1806int
1807split_go(int loader_slot, int split_slot, void **entry)
1808{
Marti Bolivarc50926f2017-06-14 09:35:40 -04001809 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001810 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001811 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001812 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001813 int rc;
1814
Christopher Collins92ea77f2016-12-12 15:59:26 -08001815 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1816 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001817 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001818 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04001819 boot_data.imgs[loader_slot].sectors = sectors + 0;
1820 boot_data.imgs[split_slot].sectors = sectors + BOOT_MAX_IMG_SECTORS;
1821
1822 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1823 rc = flash_area_open(loader_flash_id,
1824 &BOOT_IMG_AREA(&boot_data, split_slot));
1825 assert(rc == 0);
1826 split_flash_id = flash_area_id_from_image_slot(split_slot);
1827 rc = flash_area_open(split_flash_id,
1828 &BOOT_IMG_AREA(&boot_data, split_slot));
1829 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001830
1831 /* Determine the sector layout of the image slots and scratch area. */
1832 rc = boot_read_sectors();
1833 if (rc != 0) {
1834 rc = SPLIT_GO_ERR;
1835 goto done;
1836 }
1837
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001838 rc = boot_read_image_headers(true);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001839 if (rc != 0) {
1840 goto done;
1841 }
1842
Christopher Collins92ea77f2016-12-12 15:59:26 -08001843 /* Don't check the bootable image flag because we could really call a
1844 * bootable or non-bootable image. Just validate that the image check
1845 * passes which is distinct from the normal check.
1846 */
Marti Bolivarf804f622017-06-12 15:41:48 -04001847 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001848 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04001849 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001850 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001851 if (rc != 0) {
1852 rc = SPLIT_GO_NON_MATCHING;
1853 goto done;
1854 }
1855
Marti Bolivarea088872017-06-12 17:10:49 -04001856 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04001857 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001858 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001859 rc = SPLIT_GO_OK;
1860
1861done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04001862 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
1863 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001864 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001865 return rc;
1866}