blob: 1240e23a9f0e7412ef9ae06a8fc88796114ae590 [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
Christopher Collins2c88e692019-05-22 15:10:14 -0700978 BOOT_LOG_DBG("initializing status; fa_id=%d", fap->fa_id);
979
David Vincze2d736ad2019-02-18 11:50:22 +0100980 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY, &swap_state);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300981 assert(rc == 0);
982
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400983 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300984 rc = boot_write_image_ok(fap);
985 assert(rc == 0);
986 }
987
Fabio Utzig46490722017-09-04 15:34:32 -0300988 rc = boot_write_swap_size(fap, bs->swap_size);
989 assert(rc == 0);
990
Fabio Utzigba829042018-09-18 08:29:34 -0300991#ifdef MCUBOOT_ENC_IMAGES
992 rc = boot_write_enc_key(fap, 0, bs->enckey[0]);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300993 assert(rc == 0);
994
Fabio Utzigba829042018-09-18 08:29:34 -0300995 rc = boot_write_enc_key(fap, 1, bs->enckey[1]);
996 assert(rc == 0);
997#endif
998
999 rc = boot_write_magic(fap);
1000 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001001
1002 return 0;
1003}
David Brown6b1b3b92017-09-19 08:59:10 -06001004#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001005
Fabio Utzig358c9352017-07-25 22:10:45 -03001006#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -03001007static int
Fabio Utziged0ca432019-01-23 14:50:11 -02001008boot_erase_trailer_sectors(const struct flash_area *fap)
Fabio Utzig2473ac02017-05-02 12:45:02 -03001009{
1010 uint8_t slot;
Fabio Utziged0ca432019-01-23 14:50:11 -02001011 uint32_t sector;
1012 uint32_t trailer_sz;
1013 uint32_t total_sz;
1014 uint32_t off;
1015 uint32_t sz;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001016 int rc;
1017
Christopher Collins2c88e692019-05-22 15:10:14 -07001018 BOOT_LOG_DBG("erasing trailer; fa_id=%d", fap->fa_id);
1019
Fabio Utzigba829042018-09-18 08:29:34 -03001020 switch (fap->fa_id) {
David Vincze2d736ad2019-02-18 11:50:22 +01001021 case FLASH_AREA_IMAGE_PRIMARY:
1022 slot = BOOT_PRIMARY_SLOT;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001023 break;
David Vincze2d736ad2019-02-18 11:50:22 +01001024 case FLASH_AREA_IMAGE_SECONDARY:
1025 slot = BOOT_SECONDARY_SLOT;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001026 break;
1027 default:
1028 return BOOT_EFLASH;
1029 }
1030
Fabio Utziged0ca432019-01-23 14:50:11 -02001031 /* delete starting from last sector and moving to beginning */
1032 sector = boot_img_num_sectors(&boot_data, slot) - 1;
Christopher Collins2adef702019-05-22 14:37:31 -07001033 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Fabio Utziged0ca432019-01-23 14:50:11 -02001034 total_sz = 0;
1035 do {
1036 sz = boot_img_sector_size(&boot_data, slot, sector);
1037 off = boot_img_sector_off(&boot_data, slot, sector);
1038 rc = boot_erase_sector(fap, off, sz);
1039 assert(rc == 0);
1040
1041 sector--;
1042 total_sz += sz;
1043 } while (total_sz < trailer_sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001044
1045 return rc;
1046}
Fabio Utzig358c9352017-07-25 22:10:45 -03001047#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001048
Christopher Collins92ea77f2016-12-12 15:59:26 -08001049/**
1050 * Swaps the contents of two flash regions within the two image slots.
1051 *
1052 * @param idx The index of the first sector in the range of
1053 * sectors being swapped.
1054 * @param sz The number of bytes to swap.
1055 * @param bs The current boot status. This struct gets
1056 * updated according to the outcome.
1057 *
1058 * @return 0 on success; nonzero on failure.
1059 */
Fabio Utzig3488eef2017-06-12 10:25:43 -03001060#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -08001061static void
Christopher Collins92ea77f2016-12-12 15:59:26 -08001062boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
1063{
David Vincze2d736ad2019-02-18 11:50:22 +01001064 const struct flash_area *fap_primary_slot;
1065 const struct flash_area *fap_secondary_slot;
Fabio Utzigba829042018-09-18 08:29:34 -03001066 const struct flash_area *fap_scratch;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001067 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001068 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001069 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001070 uint32_t scratch_trailer_off;
1071 struct boot_swap_state swap_state;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001072 size_t last_sector;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001073 int rc;
1074
1075 /* Calculate offset from start of image area. */
David Vincze2d736ad2019-02-18 11:50:22 +01001076 img_off = boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT, idx);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001077
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001078 copy_sz = sz;
Christopher Collins2adef702019-05-22 14:37:31 -07001079 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Fabio Utzig9678c972017-05-23 11:28:56 -04001080
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001081 /* sz in this function is always sized on a multiple of the sector size.
1082 * The check against the start offset of the last sector
Fabio Utzig9678c972017-05-23 11:28:56 -04001083 * is to determine if we're swapping the last sector. The last sector
1084 * needs special handling because it's where the trailer lives. If we're
1085 * copying it, we need to use scratch to write the trailer temporarily.
1086 *
1087 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
1088 * controls if special handling is needed (swapping last sector).
1089 */
David Vincze2d736ad2019-02-18 11:50:22 +01001090 last_sector = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT) - 1;
1091 if (img_off + sz > boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT,
1092 last_sector)) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001093 copy_sz -= trailer_sz;
1094 }
1095
Fabio Utzig39000012018-07-30 12:40:20 -03001096 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001097
David Vincze2d736ad2019-02-18 11:50:22 +01001098 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001099 assert (rc == 0);
1100
David Vincze2d736ad2019-02-18 11:50:22 +01001101 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001102 assert (rc == 0);
1103
1104 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch);
1105 assert (rc == 0);
1106
Fabio Utzig39000012018-07-30 12:40:20 -03001107 if (bs->state == BOOT_STATUS_STATE_0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001108 BOOT_LOG_DBG("erasing scratch area");
Fabio Utzigba829042018-09-18 08:29:34 -03001109 rc = boot_erase_sector(fap_scratch, 0, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001110 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001111
David Vincze2d736ad2019-02-18 11:50:22 +01001112 rc = boot_copy_sector(fap_secondary_slot, fap_scratch,
1113 img_off, 0, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001114 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001115
Fabio Utzig39000012018-07-30 12:40:20 -03001116 if (bs->idx == BOOT_STATUS_IDX_0) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001117 if (bs->use_scratch) {
Fabio Utzigba829042018-09-18 08:29:34 -03001118 boot_status_init(fap_scratch, bs);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001119 } else {
1120 /* Prepare the status area... here it is known that the
1121 * last sector is not being used by the image data so it's
1122 * safe to erase.
1123 */
David Vincze2d736ad2019-02-18 11:50:22 +01001124 rc = boot_erase_trailer_sectors(fap_primary_slot);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001125 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001126
David Vincze2d736ad2019-02-18 11:50:22 +01001127 boot_status_init(fap_primary_slot, bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001128 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001129 }
1130
Fabio Utzig39000012018-07-30 12:40:20 -03001131 bs->state = BOOT_STATUS_STATE_1;
Christopher Collins4772ac42017-02-27 20:08:01 -08001132 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001133 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001134 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001135
Fabio Utzig39000012018-07-30 12:40:20 -03001136 if (bs->state == BOOT_STATUS_STATE_1) {
David Vincze2d736ad2019-02-18 11:50:22 +01001137 rc = boot_erase_sector(fap_secondary_slot, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001138 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001139
David Vincze2d736ad2019-02-18 11:50:22 +01001140 rc = boot_copy_sector(fap_primary_slot, fap_secondary_slot,
1141 img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001142 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001143
Fabio Utzig39000012018-07-30 12:40:20 -03001144 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001145 /* If not all sectors of the slot are being swapped,
David Vincze2d736ad2019-02-18 11:50:22 +01001146 * guarantee here that only the primary slot will have the state.
Fabio Utzig2473ac02017-05-02 12:45:02 -03001147 */
David Vincze2d736ad2019-02-18 11:50:22 +01001148 rc = boot_erase_trailer_sectors(fap_secondary_slot);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001149 assert(rc == 0);
1150 }
1151
Fabio Utzig39000012018-07-30 12:40:20 -03001152 bs->state = BOOT_STATUS_STATE_2;
Christopher Collins4772ac42017-02-27 20:08:01 -08001153 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001154 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001155 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001156
Fabio Utzig39000012018-07-30 12:40:20 -03001157 if (bs->state == BOOT_STATUS_STATE_2) {
David Vincze2d736ad2019-02-18 11:50:22 +01001158 rc = boot_erase_sector(fap_primary_slot, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001159 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001160
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001161 /* NOTE: also copy trailer from scratch (has status info) */
David Vincze2d736ad2019-02-18 11:50:22 +01001162 rc = boot_copy_sector(fap_scratch, fap_primary_slot,
1163 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001164 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001165
Fabio Utzig94d998c2017-05-22 11:02:41 -04001166 if (bs->use_scratch) {
Fabio Utzigba829042018-09-18 08:29:34 -03001167 scratch_trailer_off = boot_status_off(fap_scratch);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001168
1169 /* copy current status that is being maintained in scratch */
David Vincze2d736ad2019-02-18 11:50:22 +01001170 rc = boot_copy_sector(fap_scratch, fap_primary_slot,
1171 scratch_trailer_off, img_off + copy_sz,
Marti Bolivare10a7392017-06-14 16:20:07 -04001172 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data));
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001173 BOOT_STATUS_ASSERT(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001174
Fabio Utzig2473ac02017-05-02 12:45:02 -03001175 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1176 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001177 assert(rc == 0);
1178
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001179 if (swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze2d736ad2019-02-18 11:50:22 +01001180 rc = boot_write_image_ok(fap_primary_slot);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001181 assert(rc == 0);
1182 }
1183
David Vincze2d736ad2019-02-18 11:50:22 +01001184 rc = boot_write_swap_size(fap_primary_slot, bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001185 assert(rc == 0);
1186
Fabio Utzigba829042018-09-18 08:29:34 -03001187#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +01001188 rc = boot_write_enc_key(fap_primary_slot, 0, bs->enckey[0]);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001189 assert(rc == 0);
1190
David Vincze2d736ad2019-02-18 11:50:22 +01001191 rc = boot_write_enc_key(fap_primary_slot, 1, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001192 assert(rc == 0);
1193#endif
1194
David Vincze2d736ad2019-02-18 11:50:22 +01001195 rc = boot_write_magic(fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001196 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001197 }
1198
Christopher Collins92ea77f2016-12-12 15:59:26 -08001199 bs->idx++;
Fabio Utzig39000012018-07-30 12:40:20 -03001200 bs->state = BOOT_STATUS_STATE_0;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001201 bs->use_scratch = 0;
Christopher Collins4772ac42017-02-27 20:08:01 -08001202 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001203 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001204 }
Fabio Utzigba829042018-09-18 08:29:34 -03001205
David Vincze2d736ad2019-02-18 11:50:22 +01001206 flash_area_close(fap_primary_slot);
1207 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001208 flash_area_close(fap_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001209}
Fabio Utzig3488eef2017-06-12 10:25:43 -03001210#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001211
1212/**
David Vincze2d736ad2019-02-18 11:50:22 +01001213 * Overwrite primary slot with the image contained in the secondary slot.
1214 * If a prior copy operation was interrupted by a system reset, this function
1215 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001216 *
1217 * @param bs The current boot status. This function reads
1218 * this struct to determine if it is resuming
1219 * an interrupted swap operation. This
1220 * function writes the updated status to this
1221 * function on return.
1222 *
1223 * @return 0 on success; nonzero on failure.
1224 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001225#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001226static int
1227boot_copy_image(struct boot_status *bs)
1228{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001229 size_t sect_count;
1230 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001231 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001232 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001233 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001234 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +01001235 const struct flash_area *fap_primary_slot;
1236 const struct flash_area *fap_secondary_slot;
1237
Fabio Utzig13d9e352017-10-05 20:32:31 -03001238
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001239 (void)bs;
1240
Fabio Utzig13d9e352017-10-05 20:32:31 -03001241#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1242 uint32_t src_size = 0;
David Vincze2d736ad2019-02-18 11:50:22 +01001243 rc = boot_read_image_size(BOOT_SECONDARY_SLOT,
1244 boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT),
1245 &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001246 assert(rc == 0);
1247#endif
David Brown17609d82017-05-05 09:41:34 -06001248
David Vincze2d736ad2019-02-18 11:50:22 +01001249 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1250 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -06001251
David Vincze2d736ad2019-02-18 11:50:22 +01001252 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001253 assert (rc == 0);
1254
David Vincze2d736ad2019-02-18 11:50:22 +01001255 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001256 assert (rc == 0);
1257
David Vincze2d736ad2019-02-18 11:50:22 +01001258 sect_count = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001259 for (sect = 0, size = 0; sect < sect_count; sect++) {
David Vincze2d736ad2019-02-18 11:50:22 +01001260 this_size = boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, sect);
1261 rc = boot_erase_sector(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001262 assert(rc == 0);
1263
1264 size += this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001265
1266#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1267 if (size >= src_size) {
1268 break;
1269 }
1270#endif
David Brown17609d82017-05-05 09:41:34 -06001271 }
1272
Fabio Utzigba829042018-09-18 08:29:34 -03001273#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +01001274 if (IS_ENCRYPTED(boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT))) {
1275 rc = boot_enc_load(boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT),
1276 fap_secondary_slot,
1277 bs->enckey[1]);
1278
Fabio Utzigba829042018-09-18 08:29:34 -03001279 if (rc < 0) {
1280 return BOOT_EBADIMAGE;
1281 }
Fabio Utzige641ea52018-12-03 10:37:53 -02001282 if (rc == 0 && boot_enc_set_key(1, bs->enckey[1])) {
Fabio Utzigba829042018-09-18 08:29:34 -03001283 return BOOT_EBADIMAGE;
1284 }
1285 }
1286#endif
1287
David Vincze2d736ad2019-02-18 11:50:22 +01001288 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1289 size);
1290 rc = boot_copy_sector(fap_secondary_slot, fap_primary_slot, 0, 0, size);
David Brown17609d82017-05-05 09:41:34 -06001291
Fabio Utzig13d9e352017-10-05 20:32:31 -03001292 /*
1293 * Erases header and trailer. The trailer is erased because when a new
1294 * image is written without a trailer as is the case when using newt, the
1295 * trailer that was left might trigger a new upgrade.
1296 */
Christopher Collins2c88e692019-05-22 15:10:14 -07001297 BOOT_LOG_DBG("erasing secondary header");
David Vincze2d736ad2019-02-18 11:50:22 +01001298 rc = boot_erase_sector(fap_secondary_slot,
1299 boot_img_sector_off(&boot_data,
1300 BOOT_SECONDARY_SLOT, 0),
1301 boot_img_sector_size(&boot_data,
1302 BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001303 assert(rc == 0);
David Vincze2d736ad2019-02-18 11:50:22 +01001304 last_sector = boot_img_num_sectors(&boot_data, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -07001305 BOOT_LOG_DBG("erasing secondary trailer");
David Vincze2d736ad2019-02-18 11:50:22 +01001306 rc = boot_erase_sector(fap_secondary_slot,
1307 boot_img_sector_off(&boot_data,
1308 BOOT_SECONDARY_SLOT, last_sector),
1309 boot_img_sector_size(&boot_data,
1310 BOOT_SECONDARY_SLOT, last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001311 assert(rc == 0);
1312
David Vincze2d736ad2019-02-18 11:50:22 +01001313 flash_area_close(fap_primary_slot);
1314 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001315
David Vincze2d736ad2019-02-18 11:50:22 +01001316 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001317
1318 return 0;
1319}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001320#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001321
1322/**
1323 * Swaps the two images in flash. If a prior copy operation was interrupted
1324 * by a system reset, this function completes that operation.
1325 *
1326 * @param bs The current boot status. This function reads
1327 * this struct to determine if it is resuming
1328 * an interrupted swap operation. This
1329 * function writes the updated status to this
1330 * function on return.
1331 *
1332 * @return 0 on success; nonzero on failure.
1333 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001334#if !defined(MCUBOOT_OVERWRITE_ONLY)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001335static int
Fabio Utzig338a19f2018-12-03 08:37:08 -02001336boot_swap_image(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001337{
1338 uint32_t sz;
1339 int first_sector_idx;
1340 int last_sector_idx;
David Vincze2d736ad2019-02-18 11:50:22 +01001341 int last_idx_secondary_slot;
Fabio Utzigcd5774b2017-11-29 10:18:26 -02001342 uint32_t swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001343 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001344#ifdef MCUBOOT_ENC_IMAGES
1345 const struct flash_area *fap;
1346 uint8_t slot;
1347 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001348#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001349 uint32_t size;
1350 uint32_t copy_size;
David Vincze2d736ad2019-02-18 11:50:22 +01001351 uint32_t primary_slot_size;
1352 uint32_t secondary_slot_size;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001353 int rc;
1354
1355 /* FIXME: just do this if asked by user? */
1356
1357 size = copy_size = 0;
1358
Fabio Utzig39000012018-07-30 12:40:20 -03001359 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Fabio Utzig46490722017-09-04 15:34:32 -03001360 /*
1361 * No swap ever happened, so need to find the largest image which
1362 * will be used to determine the amount of sectors to swap.
1363 */
David Vincze2d736ad2019-02-18 11:50:22 +01001364 hdr = boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001365 if (hdr->ih_magic == IMAGE_MAGIC) {
David Vincze2d736ad2019-02-18 11:50:22 +01001366 rc = boot_read_image_size(BOOT_PRIMARY_SLOT, hdr, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001367 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001368 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001369
Fabio Utzigba829042018-09-18 08:29:34 -03001370#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001371 if (IS_ENCRYPTED(hdr)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001372 fap = BOOT_IMG_AREA(&boot_data, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001373 rc = boot_enc_load(hdr, fap, bs->enckey[0]);
1374 assert(rc >= 0);
1375
1376 if (rc == 0) {
1377 rc = boot_enc_set_key(0, bs->enckey[0]);
1378 assert(rc == 0);
1379 } else {
1380 rc = 0;
1381 }
1382 } else {
1383 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1384 }
1385#endif
1386
David Vincze2d736ad2019-02-18 11:50:22 +01001387 hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001388 if (hdr->ih_magic == IMAGE_MAGIC) {
David Vincze2d736ad2019-02-18 11:50:22 +01001389 rc = boot_read_image_size(BOOT_SECONDARY_SLOT, hdr, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001390 assert(rc == 0);
1391 }
1392
Fabio Utzigba829042018-09-18 08:29:34 -03001393#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +01001394 hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001395 if (IS_ENCRYPTED(hdr)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001396 fap = BOOT_IMG_AREA(&boot_data, BOOT_SECONDARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001397 rc = boot_enc_load(hdr, fap, bs->enckey[1]);
1398 assert(rc >= 0);
1399
1400 if (rc == 0) {
1401 rc = boot_enc_set_key(1, bs->enckey[1]);
1402 assert(rc == 0);
1403 } else {
1404 rc = 0;
1405 }
1406 } else {
1407 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1408 }
1409#endif
1410
Fabio Utzig46490722017-09-04 15:34:32 -03001411 if (size > copy_size) {
1412 copy_size = size;
1413 }
1414
1415 bs->swap_size = copy_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001416
Fabio Utzig46490722017-09-04 15:34:32 -03001417 } else {
1418 /*
1419 * If a swap was under way, the swap_size should already be present
1420 * in the trailer...
1421 */
1422 rc = boot_read_swap_size(&bs->swap_size);
1423 assert(rc == 0);
1424
1425 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001426
1427#ifdef MCUBOOT_ENC_IMAGES
1428 for (slot = 0; slot <= 1; slot++) {
1429 rc = boot_read_enc_key(slot, bs->enckey[slot]);
1430 assert(rc == 0);
1431
1432 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001433 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001434 break;
1435 }
1436 }
1437
1438 if (i != BOOT_ENC_KEY_SIZE) {
1439 boot_enc_set_key(slot, bs->enckey[slot]);
1440 }
1441 }
1442#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001443 }
1444
David Vincze2d736ad2019-02-18 11:50:22 +01001445 primary_slot_size = 0;
1446 secondary_slot_size = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001447 last_sector_idx = 0;
David Vincze2d736ad2019-02-18 11:50:22 +01001448 last_idx_secondary_slot = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001449
1450 /*
1451 * Knowing the size of the largest image between both slots, here we
David Vincze2d736ad2019-02-18 11:50:22 +01001452 * find what is the last sector in the primary slot that needs swapping.
1453 * Since we already know that both slots are compatible, the secondary
1454 * slot's last sector is not really required after this check is finished.
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001455 */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001456 while (1) {
David Vincze2d736ad2019-02-18 11:50:22 +01001457 if ((primary_slot_size < copy_size) ||
1458 (primary_slot_size < secondary_slot_size)) {
1459 primary_slot_size += boot_img_sector_size(&boot_data,
1460 BOOT_PRIMARY_SLOT,
1461 last_sector_idx);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001462 }
David Vincze2d736ad2019-02-18 11:50:22 +01001463 if ((secondary_slot_size < copy_size) ||
1464 (secondary_slot_size < primary_slot_size)) {
1465 secondary_slot_size += boot_img_sector_size(&boot_data,
1466 BOOT_SECONDARY_SLOT,
1467 last_idx_secondary_slot);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001468 }
David Vincze2d736ad2019-02-18 11:50:22 +01001469 if (primary_slot_size >= copy_size &&
1470 secondary_slot_size >= copy_size &&
1471 primary_slot_size == secondary_slot_size) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001472 break;
1473 }
1474 last_sector_idx++;
David Vincze2d736ad2019-02-18 11:50:22 +01001475 last_idx_secondary_slot++;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001476 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001477
1478 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001479 while (last_sector_idx >= 0) {
1480 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
Fabio Utzig39000012018-07-30 12:40:20 -03001481 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001482 boot_swap_sectors(first_sector_idx, sz, bs);
1483 }
1484
1485 last_sector_idx = first_sector_idx - 1;
1486 swap_idx++;
1487 }
1488
David Vincze2d736ad2019-02-18 11:50:22 +01001489#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001490 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001491 BOOT_LOG_WRN("%d status write fails performing the swap",
1492 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001493 }
1494#endif
1495
Christopher Collins92ea77f2016-12-12 15:59:26 -08001496 return 0;
1497}
David Brown17609d82017-05-05 09:41:34 -06001498#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001499
1500/**
David Vincze2d736ad2019-02-18 11:50:22 +01001501 * Marks the image in the primary slot as fully copied.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001502 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001503#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001504static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001505boot_set_copy_done(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001506{
1507 const struct flash_area *fap;
1508 int rc;
1509
David Vincze2d736ad2019-02-18 11:50:22 +01001510 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001511 if (rc != 0) {
1512 return BOOT_EFLASH;
1513 }
1514
1515 rc = boot_write_copy_done(fap);
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001516 flash_area_close(fap);
1517 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001518}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001519#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001520
1521/**
David Vincze2d736ad2019-02-18 11:50:22 +01001522 * Marks a reverted image in the primary slot as confirmed. This is necessary to
1523 * ensure the status bytes from the image revert operation don't get processed
1524 * on a subsequent boot.
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001525 *
1526 * NOTE: image_ok is tested before writing because if there's a valid permanent
David Vincze2d736ad2019-02-18 11:50:22 +01001527 * image installed on the primary slot and the new image to be upgrade to has a
1528 * bad sig, image_ok would be overwritten.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001529 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001530#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001531static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001532boot_set_image_ok(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001533{
1534 const struct flash_area *fap;
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001535 struct boot_swap_state state;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001536 int rc;
1537
David Vincze2d736ad2019-02-18 11:50:22 +01001538 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001539 if (rc != 0) {
1540 return BOOT_EFLASH;
1541 }
1542
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001543 rc = boot_read_swap_state(fap, &state);
1544 if (rc != 0) {
1545 rc = BOOT_EFLASH;
1546 goto out;
1547 }
1548
1549 if (state.image_ok == BOOT_FLAG_UNSET) {
1550 rc = boot_write_image_ok(fap);
1551 }
1552
1553out:
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001554 flash_area_close(fap);
1555 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001556}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001557#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001558
1559/**
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001560 * Performs an image swap if one is required.
1561 *
1562 * @param out_swap_type On success, the type of swap performed gets
1563 * written here.
1564 *
1565 * @return 0 on success; nonzero on failure.
1566 */
1567static int
1568boot_swap_if_needed(int *out_swap_type)
1569{
1570 struct boot_status bs;
1571 int swap_type;
1572 int rc;
1573
1574 /* Determine if we rebooted in the middle of an image swap
1575 * operation.
1576 */
1577 rc = boot_read_status(&bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001578 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001579 if (rc != 0) {
1580 return rc;
1581 }
1582
1583 /* If a partial swap was detected, complete it. */
Fabio Utzig39000012018-07-30 12:40:20 -03001584 if (bs.idx != BOOT_STATUS_IDX_0 || bs.state != BOOT_STATUS_STATE_0) {
Fabio Utziga32f1af2019-01-07 06:50:58 -02001585#ifdef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig338a19f2018-12-03 08:37:08 -02001586 /* Should never arrive here, overwrite-only mode has no swap state. */
1587 assert(0);
1588#else
1589 rc = boot_swap_image(&bs);
1590#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001591 assert(rc == 0);
1592
Fabio Utzigb5b2f552017-06-30 10:03:47 -03001593 /* NOTE: here we have finished a swap resume. The initial request
1594 * was either a TEST or PERM swap, which now after the completed
1595 * swap will be determined to be respectively REVERT (was TEST)
1596 * or NONE (was PERM).
1597 */
1598
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001599 /* Extrapolate the type of the partial swap. We need this
1600 * information to know how to mark the swap complete in flash.
1601 */
1602 swap_type = boot_previous_swap_type();
1603 } else {
Fabio Utzigba829042018-09-18 08:29:34 -03001604 swap_type = boot_validated_swap_type(&bs);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001605 switch (swap_type) {
1606 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001607 case BOOT_SWAP_TYPE_PERM:
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001608 case BOOT_SWAP_TYPE_REVERT:
Fabio Utziga32f1af2019-01-07 06:50:58 -02001609#ifdef MCUBOOT_OVERWRITE_ONLY
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001610 rc = boot_copy_image(&bs);
Fabio Utzig338a19f2018-12-03 08:37:08 -02001611#else
1612 rc = boot_swap_image(&bs);
1613#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001614 assert(rc == 0);
1615 break;
Fabio Utzig338a19f2018-12-03 08:37:08 -02001616#ifdef MCUBOOT_BOOTSTRAP
1617 case BOOT_SWAP_TYPE_NONE:
1618 /*
1619 * Header checks are done first because they are inexpensive.
1620 * Since overwrite-only copies starting from offset 0, if
1621 * interrupted, it might leave a valid header magic, so also
David Vincze2d736ad2019-02-18 11:50:22 +01001622 * run validation on the primary slot to be sure it's not OK.
Fabio Utzig338a19f2018-12-03 08:37:08 -02001623 */
David Vincze2d736ad2019-02-18 11:50:22 +01001624 if (boot_check_header_erased(BOOT_PRIMARY_SLOT) == 0 ||
1625 boot_validate_slot(BOOT_PRIMARY_SLOT, &bs) != 0) {
1626 if ((boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT)->ih_magic
1627 == IMAGE_MAGIC ) &&
1628 (boot_validate_slot(BOOT_SECONDARY_SLOT, &bs) == 0)) {
Fabio Utzig338a19f2018-12-03 08:37:08 -02001629 rc = boot_copy_image(&bs);
1630 assert(rc == 0);
1631
1632 /* Returns fail here to trigger a re-read of the headers. */
1633 swap_type = BOOT_SWAP_TYPE_FAIL;
1634 }
1635 }
1636 break;
1637#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001638 }
1639 }
1640
1641 *out_swap_type = swap_type;
1642 return 0;
1643}
1644
1645/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08001646 * Prepares the booting process. This function moves images around in flash as
1647 * appropriate, and tells you what address to boot from.
1648 *
1649 * @param rsp On success, indicates how booting should occur.
1650 *
1651 * @return 0 on success; nonzero on failure.
1652 */
1653int
1654boot_go(struct boot_rsp *rsp)
1655{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001656 int swap_type;
Marti Bolivar84898652017-06-13 17:20:22 -04001657 size_t slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001658 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001659 int fa_id;
David Brown52eee562017-07-05 11:25:09 -06001660 bool reload_headers = false;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001661
1662 /* The array of slot sectors are defined here (as opposed to file scope) so
1663 * that they don't get allocated for non-boot-loader apps. This is
1664 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001665 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001666 */
David Vincze2d736ad2019-02-18 11:50:22 +01001667 static boot_sector_t primary_slot_sectors[BOOT_MAX_IMG_SECTORS];
1668 static boot_sector_t secondary_slot_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001669 static boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
David Vincze2d736ad2019-02-18 11:50:22 +01001670 boot_data.imgs[BOOT_PRIMARY_SLOT].sectors = primary_slot_sectors;
1671 boot_data.imgs[BOOT_SECONDARY_SLOT].sectors = secondary_slot_sectors;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001672 boot_data.scratch.sectors = scratch_sectors;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001673
Fabio Utzigba829042018-09-18 08:29:34 -03001674#ifdef MCUBOOT_ENC_IMAGES
1675 /* FIXME: remove this after RAM is cleared by sim */
1676 boot_enc_zeroize();
1677#endif
1678
Marti Bolivarc0b47912017-06-13 17:18:09 -04001679 /* Open boot_data image areas for the duration of this call. */
1680 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1681 fa_id = flash_area_id_from_image_slot(slot);
1682 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot));
1683 assert(rc == 0);
1684 }
1685 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
1686 &BOOT_SCRATCH_AREA(&boot_data));
1687 assert(rc == 0);
1688
Christopher Collins92ea77f2016-12-12 15:59:26 -08001689 /* Determine the sector layout of the image slots and scratch area. */
1690 rc = boot_read_sectors();
1691 if (rc != 0) {
Fabio Utziga1fae672018-03-30 10:52:38 -03001692 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - too small?",
1693 BOOT_MAX_IMG_SECTORS);
Marti Bolivarc0b47912017-06-13 17:18:09 -04001694 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001695 }
1696
1697 /* Attempt to read an image header from each slot. */
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001698 rc = boot_read_image_headers(false);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001699 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001700 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001701 }
1702
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001703 /* If the image slots aren't compatible, no swap is possible. Just boot
David Vincze2d736ad2019-02-18 11:50:22 +01001704 * into the primary slot.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001705 */
1706 if (boot_slots_compatible()) {
1707 rc = boot_swap_if_needed(&swap_type);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001708 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001709 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001710 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001711 }
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001712
1713 /*
1714 * The following states need image_ok be explicitly set after the
1715 * swap was finished to avoid a new revert.
1716 */
David Vincze2d736ad2019-02-18 11:50:22 +01001717 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1718 swap_type == BOOT_SWAP_TYPE_FAIL) {
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001719#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig695d5642017-07-20 09:47:16 -03001720 rc = boot_set_image_ok();
1721 if (rc != 0) {
1722 swap_type = BOOT_SWAP_TYPE_PANIC;
1723 }
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001724#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001725 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001726 } else {
1727 swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001728 }
1729
1730 switch (swap_type) {
1731 case BOOT_SWAP_TYPE_NONE:
David Vincze2d736ad2019-02-18 11:50:22 +01001732 slot = BOOT_PRIMARY_SLOT;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001733 break;
1734
Fabio Utzig695d5642017-07-20 09:47:16 -03001735 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1736 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001737 case BOOT_SWAP_TYPE_REVERT:
David Vincze2d736ad2019-02-18 11:50:22 +01001738 slot = BOOT_SECONDARY_SLOT;
David Brown52eee562017-07-05 11:25:09 -06001739 reload_headers = true;
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001740#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig695d5642017-07-20 09:47:16 -03001741 rc = boot_set_copy_done();
1742 if (rc != 0) {
1743 swap_type = BOOT_SWAP_TYPE_PANIC;
1744 }
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001745#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001746 break;
1747
1748 case BOOT_SWAP_TYPE_FAIL:
David Vincze2d736ad2019-02-18 11:50:22 +01001749 /* The image in the secondary slot was invalid and is now erased.
1750 * Ensure we don't try to boot into it again on the next reboot.
1751 * Do this by pretending we just reverted back to the primary slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001752 */
David Vincze2d736ad2019-02-18 11:50:22 +01001753 slot = BOOT_PRIMARY_SLOT;
David Brown52eee562017-07-05 11:25:09 -06001754 reload_headers = true;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001755 break;
1756
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001757 default:
Fabio Utzig695d5642017-07-20 09:47:16 -03001758 swap_type = BOOT_SWAP_TYPE_PANIC;
1759 }
1760
1761 if (swap_type == BOOT_SWAP_TYPE_PANIC) {
1762 BOOT_LOG_ERR("panic!");
Fabio Utzigb5b2f552017-06-30 10:03:47 -03001763 assert(0);
Fabio Utzig695d5642017-07-20 09:47:16 -03001764
1765 /* Loop forever... */
1766 while (1) {}
Christopher Collins92ea77f2016-12-12 15:59:26 -08001767 }
1768
David Brown52eee562017-07-05 11:25:09 -06001769 if (reload_headers) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001770 rc = boot_read_image_headers(false);
Fabio Utzigc6a7b0c2017-09-13 19:01:15 -03001771 if (rc != 0) {
1772 goto out;
1773 }
1774 /* Since headers were reloaded, it can be assumed we just performed a
1775 * swap or overwrite. Now the header info that should be used to
David Vincze2d736ad2019-02-18 11:50:22 +01001776 * provide the data for the bootstrap, which previously was at the
1777 * secondary slot, was updated to the primary slot.
Fabio Utzigc6a7b0c2017-09-13 19:01:15 -03001778 */
David Vincze2d736ad2019-02-18 11:50:22 +01001779 slot = BOOT_PRIMARY_SLOT;
David Brown52eee562017-07-05 11:25:09 -06001780 }
1781
David Vincze2d736ad2019-02-18 11:50:22 +01001782#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
1783 rc = boot_validate_slot(BOOT_PRIMARY_SLOT, NULL);
David Brown554c52e2017-06-30 16:01:07 -06001784 if (rc != 0) {
1785 rc = BOOT_EBADIMAGE;
1786 goto out;
1787 }
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001788#else
David Vincze2d736ad2019-02-18 11:50:22 +01001789 /* Even if we're not re-validating the primary slot, we could be booting
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001790 * onto an empty flash chip. At least do a basic sanity check that
1791 * the magic number on the image is OK.
1792 */
David Vincze2d736ad2019-02-18 11:50:22 +01001793 if (boot_data.imgs[BOOT_PRIMARY_SLOT].hdr.ih_magic != IMAGE_MAGIC) {
1794 BOOT_LOG_ERR("bad image magic 0x%lx",
1795 (unsigned long)boot_data.imgs[BOOT_PRIMARY_SLOT].hdr.ih_magic);
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001796 rc = BOOT_EBADIMAGE;
1797 goto out;
1798 }
David Brown554c52e2017-06-30 16:01:07 -06001799#endif
1800
Christopher Collins92ea77f2016-12-12 15:59:26 -08001801 /* Always boot from the primary slot. */
David Vincze2d736ad2019-02-18 11:50:22 +01001802 rsp->br_flash_dev_id = boot_data.imgs[BOOT_PRIMARY_SLOT].area->fa_device_id;
1803 rsp->br_image_off = boot_img_slot_off(&boot_data, BOOT_PRIMARY_SLOT);
Marti Bolivarf804f622017-06-12 15:41:48 -04001804 rsp->br_hdr = boot_img_hdr(&boot_data, slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001805
Marti Bolivarc0b47912017-06-13 17:18:09 -04001806 out:
1807 flash_area_close(BOOT_SCRATCH_AREA(&boot_data));
1808 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1809 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
1810 }
1811 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001812}
1813
1814int
1815split_go(int loader_slot, int split_slot, void **entry)
1816{
Marti Bolivarc50926f2017-06-14 09:35:40 -04001817 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001818 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001819 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001820 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001821 int rc;
1822
Christopher Collins92ea77f2016-12-12 15:59:26 -08001823 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1824 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001825 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001826 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04001827 boot_data.imgs[loader_slot].sectors = sectors + 0;
1828 boot_data.imgs[split_slot].sectors = sectors + BOOT_MAX_IMG_SECTORS;
1829
1830 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1831 rc = flash_area_open(loader_flash_id,
1832 &BOOT_IMG_AREA(&boot_data, split_slot));
1833 assert(rc == 0);
1834 split_flash_id = flash_area_id_from_image_slot(split_slot);
1835 rc = flash_area_open(split_flash_id,
1836 &BOOT_IMG_AREA(&boot_data, split_slot));
1837 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001838
1839 /* Determine the sector layout of the image slots and scratch area. */
1840 rc = boot_read_sectors();
1841 if (rc != 0) {
1842 rc = SPLIT_GO_ERR;
1843 goto done;
1844 }
1845
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001846 rc = boot_read_image_headers(true);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001847 if (rc != 0) {
1848 goto done;
1849 }
1850
Christopher Collins92ea77f2016-12-12 15:59:26 -08001851 /* Don't check the bootable image flag because we could really call a
1852 * bootable or non-bootable image. Just validate that the image check
1853 * passes which is distinct from the normal check.
1854 */
Marti Bolivarf804f622017-06-12 15:41:48 -04001855 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001856 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04001857 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001858 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001859 if (rc != 0) {
1860 rc = SPLIT_GO_NON_MATCHING;
1861 goto done;
1862 }
1863
Marti Bolivarea088872017-06-12 17:10:49 -04001864 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04001865 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001866 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001867 rc = SPLIT_GO_OK;
1868
1869done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04001870 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
1871 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001872 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001873 return rc;
1874}