blob: fcbf08263289138b465ce8aae12f32071c1d8854 [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
David Vinczee2453472019-06-17 12:31:59 +020020/*
21 * Modifications are Copyright (c) 2019 Arm Limited.
22 */
23
Christopher Collins92ea77f2016-12-12 15:59:26 -080024/**
25 * This file provides an interface to the boot loader. Functions defined in
26 * this file should only be called while the boot loader is running.
27 */
28
29#include <assert.h>
30#include <stddef.h>
David Brown52eee562017-07-05 11:25:09 -060031#include <stdbool.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080032#include <inttypes.h>
33#include <stdlib.h>
34#include <string.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080035#include <os/os_malloc.h>
36#include "bootutil/bootutil.h"
37#include "bootutil/image.h"
38#include "bootutil_priv.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050039#include "bootutil/bootutil_log.h"
40
Fabio Utzigba829042018-09-18 08:29:34 -030041#ifdef MCUBOOT_ENC_IMAGES
42#include "bootutil/enc_key.h"
43#endif
44
Fabio Utzigba1fbe62017-07-21 14:01:20 -030045#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030046
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010047MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
48
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040049static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080050
David Vincze2d736ad2019-02-18 11:50:22 +010051#if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT) && !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utziga0e1cce2017-11-23 20:04:01 -020052static int boot_status_fails = 0;
53#define BOOT_STATUS_ASSERT(x) \
54 do { \
Johann Fischered8461b2018-02-15 16:50:31 +010055 if (!(x)) { \
Fabio Utziga0e1cce2017-11-23 20:04:01 -020056 boot_status_fails++; \
57 } \
58 } while (0)
59#else
Fabio Utzig57c40f72017-12-12 21:48:30 -020060#define BOOT_STATUS_ASSERT(x) ASSERT(x)
Fabio Utziga0e1cce2017-11-23 20:04:01 -020061#endif
62
Christopher Collins92ea77f2016-12-12 15:59:26 -080063struct boot_status_table {
David Vincze2d736ad2019-02-18 11:50:22 +010064 uint8_t bst_magic_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -080065 uint8_t bst_magic_scratch;
David Vincze2d736ad2019-02-18 11:50:22 +010066 uint8_t bst_copy_done_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -080067 uint8_t bst_status_source;
68};
69
70/**
71 * This set of tables maps swap state contents to boot status location.
72 * When searching for a match, these tables must be iterated in order.
73 */
74static const struct boot_status_table boot_status_tables[] = {
75 {
David Vincze2d736ad2019-02-18 11:50:22 +010076 /* | primary slot | scratch |
77 * ----------+--------------+--------------|
78 * magic | Good | Any |
79 * copy-done | Set | N/A |
80 * ----------+--------------+--------------'
81 * source: none |
82 * ----------------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -080083 */
David Vincze2d736ad2019-02-18 11:50:22 +010084 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
Christopher Collinsa1c12042019-05-23 14:00:28 -070085 .bst_magic_scratch = BOOT_MAGIC_NOTGOOD,
David Vincze2d736ad2019-02-18 11:50:22 +010086 .bst_copy_done_primary_slot = BOOT_FLAG_SET,
87 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
Christopher Collins92ea77f2016-12-12 15:59:26 -080088 },
89
90 {
David Vincze2d736ad2019-02-18 11:50:22 +010091 /* | primary slot | scratch |
92 * ----------+--------------+--------------|
93 * magic | Good | Any |
94 * copy-done | Unset | N/A |
95 * ----------+--------------+--------------'
96 * source: primary slot |
97 * ----------------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -080098 */
David Vincze2d736ad2019-02-18 11:50:22 +010099 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
Christopher Collinsa1c12042019-05-23 14:00:28 -0700100 .bst_magic_scratch = BOOT_MAGIC_NOTGOOD,
David Vincze2d736ad2019-02-18 11:50:22 +0100101 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
102 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800103 },
104
105 {
David Vincze2d736ad2019-02-18 11:50:22 +0100106 /* | primary slot | scratch |
107 * ----------+--------------+--------------|
108 * magic | Any | Good |
109 * copy-done | Any | N/A |
110 * ----------+--------------+--------------'
111 * source: scratch |
112 * ----------------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -0800113 */
David Vincze2d736ad2019-02-18 11:50:22 +0100114 .bst_magic_primary_slot = BOOT_MAGIC_ANY,
115 .bst_magic_scratch = BOOT_MAGIC_GOOD,
116 .bst_copy_done_primary_slot = BOOT_FLAG_ANY,
117 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800118 },
Christopher Collins92ea77f2016-12-12 15:59:26 -0800119 {
David Vincze2d736ad2019-02-18 11:50:22 +0100120 /* | primary slot | scratch |
121 * ----------+--------------+--------------|
122 * magic | Unset | Any |
123 * copy-done | Unset | N/A |
124 * ----------+--------------+--------------|
125 * source: varies |
126 * ----------------------------------------+--------------------------+
Christopher Collins92ea77f2016-12-12 15:59:26 -0800127 * This represents one of two cases: |
128 * o No swaps ever (no status to read, so no harm in checking). |
David Vincze2d736ad2019-02-18 11:50:22 +0100129 * o Mid-revert; status in primary slot. |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800130 * -------------------------------------------------------------------'
131 */
David Vincze2d736ad2019-02-18 11:50:22 +0100132 .bst_magic_primary_slot = BOOT_MAGIC_UNSET,
133 .bst_magic_scratch = BOOT_MAGIC_ANY,
134 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
135 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800136 },
137};
138
139#define BOOT_STATUS_TABLES_COUNT \
140 (sizeof boot_status_tables / sizeof boot_status_tables[0])
141
Marti Bolivarfd20c762017-02-07 16:52:50 -0500142#define BOOT_LOG_SWAP_STATE(area, state) \
Christopher Collinsa1c12042019-05-23 14:00:28 -0700143 BOOT_LOG_INF("%s: magic=%s, swap_type=0x%x, copy_done=0x%x, " \
144 "image_ok=0x%x", \
Marti Bolivarfd20c762017-02-07 16:52:50 -0500145 (area), \
146 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
147 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
148 "bad"), \
Christopher Collinsa1c12042019-05-23 14:00:28 -0700149 (state)->swap_type, \
Marti Bolivarfd20c762017-02-07 16:52:50 -0500150 (state)->copy_done, \
151 (state)->image_ok)
152
Christopher Collins92ea77f2016-12-12 15:59:26 -0800153/**
David Vincze2d736ad2019-02-18 11:50:22 +0100154 * Determines where in flash the most recent boot status is stored. The boot
Christopher Collins92ea77f2016-12-12 15:59:26 -0800155 * status is necessary for completing a swap that was interrupted by a boot
156 * loader reset.
157 *
David Vincze2d736ad2019-02-18 11:50:22 +0100158 * @return A BOOT_STATUS_SOURCE_[...] code indicating where status should
159 * be read from.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800160 */
161static int
162boot_status_source(void)
163{
164 const struct boot_status_table *table;
165 struct boot_swap_state state_scratch;
David Vincze2d736ad2019-02-18 11:50:22 +0100166 struct boot_swap_state state_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800167 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200168 size_t i;
Marti Bolivarfd20c762017-02-07 16:52:50 -0500169 uint8_t source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800170
David Vincze2d736ad2019-02-18 11:50:22 +0100171 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY,
172 &state_primary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800173 assert(rc == 0);
174
Fabio Utzig2473ac02017-05-02 12:45:02 -0300175 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800176 assert(rc == 0);
177
David Vincze2d736ad2019-02-18 11:50:22 +0100178 BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot);
Marti Bolivarfd20c762017-02-07 16:52:50 -0500179 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
180
Christopher Collins92ea77f2016-12-12 15:59:26 -0800181 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300182 table = &boot_status_tables[i];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800183
Christopher Collinsa1c12042019-05-23 14:00:28 -0700184 if (boot_magic_compatible_check(table->bst_magic_primary_slot,
185 state_primary_slot.magic) &&
186 boot_magic_compatible_check(table->bst_magic_scratch,
187 state_scratch.magic) &&
David Vincze2d736ad2019-02-18 11:50:22 +0100188 (table->bst_copy_done_primary_slot == BOOT_FLAG_ANY ||
189 table->bst_copy_done_primary_slot == state_primary_slot.copy_done))
190 {
Marti Bolivarfd20c762017-02-07 16:52:50 -0500191 source = table->bst_status_source;
192 BOOT_LOG_INF("Boot source: %s",
193 source == BOOT_STATUS_SOURCE_NONE ? "none" :
194 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
David Vincze2d736ad2019-02-18 11:50:22 +0100195 source == BOOT_STATUS_SOURCE_PRIMARY_SLOT ?
196 "primary slot" : "BUG; can't happen");
Marti Bolivarfd20c762017-02-07 16:52:50 -0500197 return source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800198 }
199 }
200
Marti Bolivarfd20c762017-02-07 16:52:50 -0500201 BOOT_LOG_INF("Boot source: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800202 return BOOT_STATUS_SOURCE_NONE;
203}
204
David Brownf5b33d82017-09-01 10:58:27 -0600205/*
206 * Compute the total size of the given image. Includes the size of
207 * the TLVs.
208 */
Fabio Utzig13d9e352017-10-05 20:32:31 -0300209#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600210static int
211boot_read_image_size(int slot, struct image_header *hdr, uint32_t *size)
212{
213 const struct flash_area *fap;
214 struct image_tlv_info info;
215 int area_id;
216 int rc;
217
218 area_id = flash_area_id_from_image_slot(slot);
219 rc = flash_area_open(area_id, &fap);
220 if (rc != 0) {
221 rc = BOOT_EFLASH;
222 goto done;
223 }
224
225 rc = flash_area_read(fap, hdr->ih_hdr_size + hdr->ih_img_size,
226 &info, sizeof(info));
227 if (rc != 0) {
228 rc = BOOT_EFLASH;
229 goto done;
230 }
231 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
232 rc = BOOT_EBADIMAGE;
233 goto done;
234 }
235 *size = hdr->ih_hdr_size + hdr->ih_img_size + info.it_tlv_tot;
236 rc = 0;
237
238done:
239 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300240 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600241}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300242#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600243
Fabio Utzigc08ed212017-06-20 19:28:36 -0300244static int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800245boot_read_image_header(int slot, struct image_header *out_hdr)
246{
247 const struct flash_area *fap;
248 int area_id;
249 int rc;
250
251 area_id = flash_area_id_from_image_slot(slot);
252 rc = flash_area_open(area_id, &fap);
253 if (rc != 0) {
254 rc = BOOT_EFLASH;
255 goto done;
256 }
257
258 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
259 if (rc != 0) {
260 rc = BOOT_EFLASH;
261 goto done;
262 }
263
264 rc = 0;
265
266done:
267 flash_area_close(fap);
268 return rc;
269}
270
271static int
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200272boot_read_image_headers(bool require_all)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800273{
274 int rc;
275 int i;
276
277 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Marti Bolivarf804f622017-06-12 15:41:48 -0400278 rc = boot_read_image_header(i, boot_img_hdr(&boot_data, i));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800279 if (rc != 0) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200280 /* If `require_all` is set, fail on any single fail, otherwise
281 * if at least the first slot's header was read successfully,
282 * then the boot loader can attempt a boot.
283 *
284 * Failure to read any headers is a fatal error.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800285 */
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200286 if (i > 0 && !require_all) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800287 return 0;
288 } else {
289 return rc;
290 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800291 }
292 }
293
294 return 0;
295}
296
297static uint8_t
298boot_write_sz(void)
299{
300 uint8_t elem_sz;
301 uint8_t align;
302
303 /* Figure out what size to write update status update as. The size depends
304 * on what the minimum write size is for scratch area, active image slot.
305 * We need to use the bigger of those 2 values.
306 */
David Vincze2d736ad2019-02-18 11:50:22 +0100307 elem_sz = flash_area_align(boot_data.imgs[BOOT_PRIMARY_SLOT].area);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200308 align = flash_area_align(boot_data.scratch.area);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800309 if (align > elem_sz) {
310 elem_sz = align;
311 }
312
313 return elem_sz;
314}
315
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200316/*
317 * Slots are compatible when all sectors that store upto to size of the image
318 * round up to sector size, in both slot's are able to fit in the scratch
319 * area, and have sizes that are a multiple of each other (powers of two
320 * presumably!).
321 */
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800322static int
323boot_slots_compatible(void)
324{
David Vincze2d736ad2019-02-18 11:50:22 +0100325 size_t num_sectors_primary;
326 size_t num_sectors_secondary;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200327 size_t sz0, sz1;
David Vincze2d736ad2019-02-18 11:50:22 +0100328 size_t primary_slot_sz, secondary_slot_sz;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200329 size_t scratch_sz;
330 size_t i, j;
331 int8_t smaller;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800332
David Vincze2d736ad2019-02-18 11:50:22 +0100333 num_sectors_primary =
334 boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT);
335 num_sectors_secondary =
336 boot_img_num_sectors(&boot_data, BOOT_SECONDARY_SLOT);
337 if ((num_sectors_primary > BOOT_MAX_IMG_SECTORS) ||
338 (num_sectors_secondary > BOOT_MAX_IMG_SECTORS)) {
Fabio Utziga1fae672018-03-30 10:52:38 -0300339 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800340 return 0;
341 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300342
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200343 scratch_sz = boot_scratch_area_size(&boot_data);
344
345 /*
346 * The following loop scans all sectors in a linear fashion, assuring that
347 * for each possible sector in each slot, it is able to fit in the other
348 * slot's sector or sectors. Slot's should be compatible as long as any
349 * number of a slot's sectors are able to fit into another, which only
350 * excludes cases where sector sizes are not a multiple of each other.
351 */
David Vincze2d736ad2019-02-18 11:50:22 +0100352 i = sz0 = primary_slot_sz = 0;
353 j = sz1 = secondary_slot_sz = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200354 smaller = 0;
David Vincze2d736ad2019-02-18 11:50:22 +0100355 while (i < num_sectors_primary || j < num_sectors_secondary) {
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200356 if (sz0 == sz1) {
David Vincze2d736ad2019-02-18 11:50:22 +0100357 sz0 += boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
358 sz1 += boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT, j);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200359 i++;
360 j++;
361 } else if (sz0 < sz1) {
David Vincze2d736ad2019-02-18 11:50:22 +0100362 sz0 += boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
363 /* Guarantee that multiple sectors of the secondary slot
364 * fit into the primary slot.
365 */
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200366 if (smaller == 2) {
367 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors");
368 return 0;
369 }
370 smaller = 1;
371 i++;
372 } else {
David Vincze2d736ad2019-02-18 11:50:22 +0100373 sz1 += boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT, j);
374 /* Guarantee that multiple sectors of the primary slot
375 * fit into the secondary slot.
376 */
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200377 if (smaller == 1) {
378 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors");
379 return 0;
380 }
381 smaller = 2;
382 j++;
383 }
384 if (sz0 == sz1) {
David Vincze2d736ad2019-02-18 11:50:22 +0100385 primary_slot_sz += sz0;
386 secondary_slot_sz += sz1;
387 /* Scratch has to fit each swap operation to the size of the larger
388 * sector among the primary slot and the secondary slot.
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200389 */
390 if (sz0 > scratch_sz || sz1 > scratch_sz) {
391 BOOT_LOG_WRN("Cannot upgrade: not all sectors fit inside scratch");
392 return 0;
393 }
394 smaller = sz0 = sz1 = 0;
395 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300396 }
397
David Vincze2d736ad2019-02-18 11:50:22 +0100398 if ((i != num_sectors_primary) ||
399 (j != num_sectors_secondary) ||
400 (primary_slot_sz != secondary_slot_sz)) {
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200401 BOOT_LOG_WRN("Cannot upgrade: slots are not compatible");
402 return 0;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800403 }
404
405 return 1;
406}
407
Christopher Collins92ea77f2016-12-12 15:59:26 -0800408/**
409 * Determines the sector layout of both image slots and the scratch area.
410 * This information is necessary for calculating the number of bytes to erase
411 * and copy during an image swap. The information collected during this
412 * function is used to populate the boot_data global.
413 */
414static int
415boot_read_sectors(void)
416{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800417 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800418
David Vincze2d736ad2019-02-18 11:50:22 +0100419 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_PRIMARY);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800420 if (rc != 0) {
421 return BOOT_EFLASH;
422 }
423
David Vincze2d736ad2019-02-18 11:50:22 +0100424 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_SECONDARY);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800425 if (rc != 0) {
426 return BOOT_EFLASH;
427 }
428
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200429 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_SCRATCH);
430 if (rc != 0) {
431 return BOOT_EFLASH;
432 }
433
Marti Bolivare10a7392017-06-14 16:20:07 -0400434 BOOT_WRITE_SZ(&boot_data) = boot_write_sz();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800435
436 return 0;
437}
438
439static uint32_t
440boot_status_internal_off(int idx, int state, int elem_sz)
441{
442 int idx_sz;
443
444 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
445
Fabio Utzig39000012018-07-30 12:40:20 -0300446 return (idx - BOOT_STATUS_IDX_0) * idx_sz +
447 (state - BOOT_STATUS_STATE_0) * elem_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800448}
449
450/**
451 * Reads the status of a partially-completed swap, if any. This is necessary
452 * to recover in case the boot lodaer was reset in the middle of a swap
453 * operation.
454 */
455static int
456boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs)
457{
458 uint32_t off;
459 uint8_t status;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300460 int max_entries;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800461 int found;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200462 int found_idx;
463 int invalid;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800464 int rc;
465 int i;
466
467 off = boot_status_off(fap);
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400468 max_entries = boot_status_entries(fap);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300469
Christopher Collins92ea77f2016-12-12 15:59:26 -0800470 found = 0;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200471 found_idx = 0;
472 invalid = 0;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300473 for (i = 0; i < max_entries; i++) {
Fabio Utzig178be542018-09-19 08:12:56 -0300474 rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(&boot_data),
475 &status, 1);
476 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800477 return BOOT_EFLASH;
478 }
479
Fabio Utzig178be542018-09-19 08:12:56 -0300480 if (rc == 1) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200481 if (found && !found_idx) {
482 found_idx = i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800483 }
484 } else if (!found) {
485 found = 1;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200486 } else if (found_idx) {
487 invalid = 1;
488 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800489 }
490 }
491
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200492 if (invalid) {
493 /* This means there was an error writing status on the last
494 * swap. Tell user and move on to validation!
495 */
496 BOOT_LOG_ERR("Detected inconsistent status!");
497
David Vincze2d736ad2019-02-18 11:50:22 +0100498#if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
499 /* With validation of the primary slot disabled, there is no way
500 * to be sure the swapped primary slot is OK, so abort!
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200501 */
502 assert(0);
503#endif
504 }
505
Christopher Collins92ea77f2016-12-12 15:59:26 -0800506 if (found) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200507 if (!found_idx) {
508 found_idx = i;
509 }
510 found_idx--;
Fabio Utzig39000012018-07-30 12:40:20 -0300511 bs->idx = (found_idx / BOOT_STATUS_STATE_COUNT) + 1;
512 bs->state = (found_idx % BOOT_STATUS_STATE_COUNT) + 1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800513 }
514
515 return 0;
516}
517
518/**
519 * Reads the boot status from the flash. The boot status contains
520 * the current state of an interrupted image copy operation. If the boot
521 * status is not present, or it indicates that previous copy finished,
522 * there is no operation in progress.
523 */
524static int
525boot_read_status(struct boot_status *bs)
526{
527 const struct flash_area *fap;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700528 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200529 uint8_t swap_info;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800530 int status_loc;
531 int area_id;
532 int rc;
533
534 memset(bs, 0, sizeof *bs);
Fabio Utzig39000012018-07-30 12:40:20 -0300535 bs->idx = BOOT_STATUS_IDX_0;
536 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700537 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800538
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700539#ifdef MCUBOOT_OVERWRITE_ONLY
540 /* Overwrite-only doesn't make use of the swap status area. */
541 return 0;
542#endif
543
Christopher Collins92ea77f2016-12-12 15:59:26 -0800544 status_loc = boot_status_source();
545 switch (status_loc) {
546 case BOOT_STATUS_SOURCE_NONE:
547 return 0;
548
549 case BOOT_STATUS_SOURCE_SCRATCH:
550 area_id = FLASH_AREA_IMAGE_SCRATCH;
551 break;
552
David Vincze2d736ad2019-02-18 11:50:22 +0100553 case BOOT_STATUS_SOURCE_PRIMARY_SLOT:
554 area_id = FLASH_AREA_IMAGE_PRIMARY;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800555 break;
556
557 default:
558 assert(0);
559 return BOOT_EBADARGS;
560 }
561
562 rc = flash_area_open(area_id, &fap);
563 if (rc != 0) {
564 return BOOT_EFLASH;
565 }
566
Fabio Utzig46490722017-09-04 15:34:32 -0300567 rc = boot_read_status_bytes(fap, bs);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700568 if (rc == 0) {
David Vinczee2453472019-06-17 12:31:59 +0200569 off = boot_swap_info_off(fap);
570 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700571 if (rc == 1) {
David Vinczee2453472019-06-17 12:31:59 +0200572 BOOT_SET_SWAP_INFO(swap_info, 0, BOOT_SWAP_TYPE_NONE);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700573 rc = 0;
574 }
David Vinczee2453472019-06-17 12:31:59 +0200575
576 /* Extract the swap type info */
577 bs->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700578 }
Fabio Utzig46490722017-09-04 15:34:32 -0300579
580 flash_area_close(fap);
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700581
Fabio Utzig46490722017-09-04 15:34:32 -0300582 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800583}
584
585/**
586 * Writes the supplied boot status to the flash file system. The boot status
587 * contains the current state of an in-progress image copy operation.
588 *
589 * @param bs The boot status to write.
590 *
591 * @return 0 on success; nonzero on failure.
592 */
593int
594boot_write_status(struct boot_status *bs)
595{
596 const struct flash_area *fap;
597 uint32_t off;
598 int area_id;
599 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300600 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700601 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300602 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800603
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300604 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100605 * the trailer. Since in the last step the primary slot is erased, the
606 * first two status writes go to the scratch which will be copied to
607 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300608 */
609
Fabio Utzig2473ac02017-05-02 12:45:02 -0300610 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800611 /* Write to scratch. */
612 area_id = FLASH_AREA_IMAGE_SCRATCH;
613 } else {
David Vincze2d736ad2019-02-18 11:50:22 +0100614 /* Write to the primary slot. */
615 area_id = FLASH_AREA_IMAGE_PRIMARY;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800616 }
617
618 rc = flash_area_open(area_id, &fap);
619 if (rc != 0) {
620 rc = BOOT_EFLASH;
621 goto done;
622 }
623
624 off = boot_status_off(fap) +
Marti Bolivare10a7392017-06-14 16:20:07 -0400625 boot_status_internal_off(bs->idx, bs->state,
626 BOOT_WRITE_SZ(&boot_data));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200627 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300628 erased_val = flash_area_erased_val(fap);
629 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700630 buf[0] = bs->state;
631
632 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800633 if (rc != 0) {
634 rc = BOOT_EFLASH;
635 goto done;
636 }
637
638 rc = 0;
639
640done:
641 flash_area_close(fap);
642 return rc;
643}
644
645/*
646 * Validate image hash/signature in a slot.
647 */
648static int
Fabio Utzigba829042018-09-18 08:29:34 -0300649boot_image_check(struct image_header *hdr, const struct flash_area *fap,
650 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800651{
David Browndb1d9d32017-01-06 11:07:54 -0700652 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigba829042018-09-18 08:29:34 -0300653 int rc;
654
655#ifndef MCUBOOT_ENC_IMAGES
656 (void)bs;
657 (void)rc;
658#else
David Vincze2d736ad2019-02-18 11:50:22 +0100659 if ((fap->fa_id == FLASH_AREA_IMAGE_SECONDARY) && IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300660 rc = boot_enc_load(hdr, fap, bs->enckey[1]);
661 if (rc < 0) {
662 return BOOT_EBADIMAGE;
663 }
664 if (rc == 0 && boot_enc_set_key(1, bs->enckey[1])) {
665 return BOOT_EBADIMAGE;
666 }
667 }
668#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800669
Christopher Collins92ea77f2016-12-12 15:59:26 -0800670 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
671 NULL, 0, NULL)) {
672 return BOOT_EBADIMAGE;
673 }
674 return 0;
675}
676
677static int
678split_image_check(struct image_header *app_hdr,
679 const struct flash_area *app_fap,
680 struct image_header *loader_hdr,
681 const struct flash_area *loader_fap)
682{
683 static void *tmpbuf;
684 uint8_t loader_hash[32];
685
686 if (!tmpbuf) {
687 tmpbuf = malloc(BOOT_TMPBUF_SZ);
688 if (!tmpbuf) {
689 return BOOT_ENOMEM;
690 }
691 }
692
693 if (bootutil_img_validate(loader_hdr, loader_fap, tmpbuf, BOOT_TMPBUF_SZ,
694 NULL, 0, loader_hash)) {
695 return BOOT_EBADIMAGE;
696 }
697
698 if (bootutil_img_validate(app_hdr, app_fap, tmpbuf, BOOT_TMPBUF_SZ,
699 loader_hash, 32, NULL)) {
700 return BOOT_EBADIMAGE;
701 }
702
703 return 0;
704}
705
Fabio Utzig338a19f2018-12-03 08:37:08 -0200706/*
707 * Check that a memory area consists of a given value.
708 */
709static inline bool
710boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300711{
712 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200713 uint8_t *p = (uint8_t *)data;
714 for (i = 0; i < len; i++) {
715 if (val != p[i]) {
716 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300717 }
718 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200719 return true;
720}
721
722static int
723boot_check_header_erased(int slot)
724{
725 const struct flash_area *fap;
726 struct image_header *hdr;
727 uint8_t erased_val;
728 int rc;
729
730 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
731 if (rc != 0) {
732 return -1;
733 }
734
735 erased_val = flash_area_erased_val(fap);
736 flash_area_close(fap);
737
738 hdr = boot_img_hdr(&boot_data, slot);
739 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
740 return -1;
741 }
742
743 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300744}
745
Christopher Collins92ea77f2016-12-12 15:59:26 -0800746static int
Fabio Utzigba829042018-09-18 08:29:34 -0300747boot_validate_slot(int slot, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800748{
749 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400750 struct image_header *hdr;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800751 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300752
David Brownd930ec62016-12-14 07:59:48 -0700753 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800754 if (rc != 0) {
755 return BOOT_EFLASH;
756 }
757
Fabio Utzig39000012018-07-30 12:40:20 -0300758 hdr = boot_img_hdr(&boot_data, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200759 if (boot_check_header_erased(slot) == 0 || (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100760 /* No bootable image in slot; continue booting from the primary slot. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200761 rc = -1;
762 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300763 }
764
Fabio Utzigba829042018-09-18 08:29:34 -0300765 if ((hdr->ih_magic != IMAGE_MAGIC || boot_image_check(hdr, fap, bs) != 0)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100766 if (slot != BOOT_PRIMARY_SLOT) {
David Brownb38e0442017-02-24 13:57:12 -0700767 flash_area_erase(fap, 0, fap->fa_size);
David Vincze2d736ad2019-02-18 11:50:22 +0100768 /* Image in the secondary slot is invalid. Erase the image and
769 * continue booting from the primary slot.
David Brownb38e0442017-02-24 13:57:12 -0700770 */
771 }
David Vincze2d736ad2019-02-18 11:50:22 +0100772 BOOT_LOG_ERR("Image in the %s slot is not valid!",
773 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
Fabio Utzig338a19f2018-12-03 08:37:08 -0200774 rc = -1;
775 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800776 }
777
David Vincze2d736ad2019-02-18 11:50:22 +0100778 /* Image in the secondary slot is valid. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200779 rc = 0;
780
781out:
782 flash_area_close(fap);
783 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800784}
785
786/**
787 * Determines which swap operation to perform, if any. If it is determined
David Vincze2d736ad2019-02-18 11:50:22 +0100788 * that a swap operation is required, the image in the secondary slot is checked
789 * for validity. If the image in the secondary slot is invalid, it is erased,
790 * and a swap type of "none" is indicated.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800791 *
792 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
793 */
794static int
Fabio Utzigba829042018-09-18 08:29:34 -0300795boot_validated_swap_type(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800796{
797 int swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800798
799 swap_type = boot_swap_type();
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300800 switch (swap_type) {
801 case BOOT_SWAP_TYPE_TEST:
802 case BOOT_SWAP_TYPE_PERM:
803 case BOOT_SWAP_TYPE_REVERT:
David Vincze2d736ad2019-02-18 11:50:22 +0100804 /* Boot loader wants to switch to the secondary slot.
805 * Ensure image is valid.
806 */
807 if (boot_validate_slot(BOOT_SECONDARY_SLOT, bs) != 0) {
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300808 swap_type = BOOT_SWAP_TYPE_FAIL;
809 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800810 }
811
812 return swap_type;
813}
814
815/**
816 * Calculates the number of sectors the scratch area can contain. A "last"
817 * source sector is specified because images are copied backwards in flash
818 * (final index to index number 0).
819 *
820 * @param last_sector_idx The index of the last source sector
821 * (inclusive).
822 * @param out_first_sector_idx The index of the first source sector
823 * (inclusive) gets written here.
824 *
825 * @return The number of bytes comprised by the
826 * [first-sector, last-sector] range.
827 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300828#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800829static uint32_t
830boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
831{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400832 size_t scratch_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800833 uint32_t new_sz;
834 uint32_t sz;
835 int i;
836
837 sz = 0;
838
Marti Bolivard3269fd2017-06-12 16:31:12 -0400839 scratch_sz = boot_scratch_area_size(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800840 for (i = last_sector_idx; i >= 0; i--) {
David Vincze2d736ad2019-02-18 11:50:22 +0100841 new_sz = sz + boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200842 /*
David Vincze2d736ad2019-02-18 11:50:22 +0100843 * The secondary slot is not being checked here, because
844 * `boot_slots_compatible` already provides assurance that the copy size
845 * will be compatible with the primary slot and scratch.
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200846 */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400847 if (new_sz > scratch_sz) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800848 break;
849 }
850 sz = new_sz;
851 }
852
853 /* i currently refers to a sector that doesn't fit or it is -1 because all
854 * sectors have been processed. In both cases, exclude sector i.
855 */
856 *out_first_sector_idx = i + 1;
857 return sz;
858}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300859#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800860
861/**
862 * Erases a region of flash.
863 *
Fabio Utzigba829042018-09-18 08:29:34 -0300864 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800865 * @param off The offset within the flash area to start the
866 * erase.
867 * @param sz The number of bytes to erase.
868 *
869 * @return 0 on success; nonzero on failure.
870 */
Fabio Utzigba829042018-09-18 08:29:34 -0300871static inline int
872boot_erase_sector(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800873{
Fabio Utzigba829042018-09-18 08:29:34 -0300874 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800875}
876
877/**
878 * Copies the contents of one flash region to another. You must erase the
879 * destination region prior to calling this function.
880 *
881 * @param flash_area_id_src The ID of the source flash area.
882 * @param flash_area_id_dst The ID of the destination flash area.
883 * @param off_src The offset within the source flash area to
884 * copy from.
885 * @param off_dst The offset within the destination flash area to
886 * copy to.
887 * @param sz The number of bytes to copy.
888 *
889 * @return 0 on success; nonzero on failure.
890 */
891static int
Fabio Utzigba829042018-09-18 08:29:34 -0300892boot_copy_sector(const struct flash_area *fap_src,
893 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800894 uint32_t off_src, uint32_t off_dst, uint32_t sz)
895{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800896 uint32_t bytes_copied;
897 int chunk_sz;
898 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -0300899#ifdef MCUBOOT_ENC_IMAGES
900 uint32_t off;
901 size_t blk_off;
902 struct image_header *hdr;
903 uint16_t idx;
904 uint32_t blk_sz;
905#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800906
907 static uint8_t buf[1024];
908
Christopher Collins92ea77f2016-12-12 15:59:26 -0800909 bytes_copied = 0;
910 while (bytes_copied < sz) {
911 if (sz - bytes_copied > sizeof buf) {
912 chunk_sz = sizeof buf;
913 } else {
914 chunk_sz = sz - bytes_copied;
915 }
916
917 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
918 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300919 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800920 }
921
Fabio Utzigba829042018-09-18 08:29:34 -0300922#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +0100923 if ((fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY) ||
924 (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY)) {
925 /* assume the secondary slot as src, needs decryption */
926 hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -0300927 off = off_src;
David Vincze2d736ad2019-02-18 11:50:22 +0100928 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY) {
929 /* might need encryption (metadata from the primary slot) */
930 hdr = boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -0300931 off = off_dst;
932 }
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200933 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300934 blk_sz = chunk_sz;
935 idx = 0;
936 if (off + bytes_copied < hdr->ih_hdr_size) {
937 /* do not decrypt header */
938 blk_off = 0;
939 blk_sz = chunk_sz - hdr->ih_hdr_size;
940 idx = hdr->ih_hdr_size;
941 } else {
942 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
943 }
944 if (off + bytes_copied + chunk_sz > hdr->ih_hdr_size + hdr->ih_img_size) {
945 /* do not decrypt TLVs */
946 if (off + bytes_copied >= hdr->ih_hdr_size + hdr->ih_img_size) {
947 blk_sz = 0;
948 } else {
949 blk_sz = (hdr->ih_hdr_size + hdr->ih_img_size) - (off + bytes_copied);
950 }
951 }
952 boot_encrypt(fap_src, (off + bytes_copied + idx) - hdr->ih_hdr_size,
953 blk_sz, blk_off, &buf[idx]);
954 }
955 }
956#endif
957
Christopher Collins92ea77f2016-12-12 15:59:26 -0800958 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
959 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300960 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800961 }
962
963 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -0300964
965 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800966 }
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
Christopher Collinsa1c12042019-05-23 14:00:28 -0700983 if (bs->swap_type != BOOT_SWAP_TYPE_NONE) {
David Vinczee2453472019-06-17 12:31:59 +0200984 rc = boot_write_swap_info(fap,
985 bs->swap_type,
986 0);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700987 assert(rc == 0);
988 }
989
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400990 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300991 rc = boot_write_image_ok(fap);
992 assert(rc == 0);
993 }
994
Fabio Utzig46490722017-09-04 15:34:32 -0300995 rc = boot_write_swap_size(fap, bs->swap_size);
996 assert(rc == 0);
997
Fabio Utzigba829042018-09-18 08:29:34 -0300998#ifdef MCUBOOT_ENC_IMAGES
999 rc = boot_write_enc_key(fap, 0, bs->enckey[0]);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001000 assert(rc == 0);
1001
Fabio Utzigba829042018-09-18 08:29:34 -03001002 rc = boot_write_enc_key(fap, 1, bs->enckey[1]);
1003 assert(rc == 0);
1004#endif
1005
1006 rc = boot_write_magic(fap);
1007 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001008
1009 return 0;
1010}
Christopher Collinsa1c12042019-05-23 14:00:28 -07001011
David Brown6b1b3b92017-09-19 08:59:10 -06001012#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001013
Fabio Utzig358c9352017-07-25 22:10:45 -03001014#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -03001015static int
Fabio Utziged0ca432019-01-23 14:50:11 -02001016boot_erase_trailer_sectors(const struct flash_area *fap)
Fabio Utzig2473ac02017-05-02 12:45:02 -03001017{
1018 uint8_t slot;
Fabio Utziged0ca432019-01-23 14:50:11 -02001019 uint32_t sector;
1020 uint32_t trailer_sz;
1021 uint32_t total_sz;
1022 uint32_t off;
1023 uint32_t sz;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001024 int rc;
1025
Christopher Collins2c88e692019-05-22 15:10:14 -07001026 BOOT_LOG_DBG("erasing trailer; fa_id=%d", fap->fa_id);
1027
Fabio Utzigba829042018-09-18 08:29:34 -03001028 switch (fap->fa_id) {
David Vincze2d736ad2019-02-18 11:50:22 +01001029 case FLASH_AREA_IMAGE_PRIMARY:
1030 slot = BOOT_PRIMARY_SLOT;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001031 break;
David Vincze2d736ad2019-02-18 11:50:22 +01001032 case FLASH_AREA_IMAGE_SECONDARY:
1033 slot = BOOT_SECONDARY_SLOT;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001034 break;
1035 default:
1036 return BOOT_EFLASH;
1037 }
1038
Fabio Utziged0ca432019-01-23 14:50:11 -02001039 /* delete starting from last sector and moving to beginning */
1040 sector = boot_img_num_sectors(&boot_data, slot) - 1;
Christopher Collins2adef702019-05-22 14:37:31 -07001041 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Fabio Utziged0ca432019-01-23 14:50:11 -02001042 total_sz = 0;
1043 do {
1044 sz = boot_img_sector_size(&boot_data, slot, sector);
1045 off = boot_img_sector_off(&boot_data, slot, sector);
1046 rc = boot_erase_sector(fap, off, sz);
1047 assert(rc == 0);
1048
1049 sector--;
1050 total_sz += sz;
1051 } while (total_sz < trailer_sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001052
1053 return rc;
1054}
Fabio Utzig358c9352017-07-25 22:10:45 -03001055#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001056
Christopher Collins92ea77f2016-12-12 15:59:26 -08001057/**
1058 * Swaps the contents of two flash regions within the two image slots.
1059 *
1060 * @param idx The index of the first sector in the range of
1061 * sectors being swapped.
1062 * @param sz The number of bytes to swap.
1063 * @param bs The current boot status. This struct gets
1064 * updated according to the outcome.
1065 *
1066 * @return 0 on success; nonzero on failure.
1067 */
Fabio Utzig3488eef2017-06-12 10:25:43 -03001068#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -08001069static void
Christopher Collins92ea77f2016-12-12 15:59:26 -08001070boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
1071{
David Vincze2d736ad2019-02-18 11:50:22 +01001072 const struct flash_area *fap_primary_slot;
1073 const struct flash_area *fap_secondary_slot;
Fabio Utzigba829042018-09-18 08:29:34 -03001074 const struct flash_area *fap_scratch;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001075 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001076 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001077 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001078 uint32_t scratch_trailer_off;
1079 struct boot_swap_state swap_state;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001080 size_t last_sector;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001081 bool erase_scratch;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001082 int rc;
1083
1084 /* Calculate offset from start of image area. */
David Vincze2d736ad2019-02-18 11:50:22 +01001085 img_off = boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT, idx);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001086
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001087 copy_sz = sz;
Christopher Collins2adef702019-05-22 14:37:31 -07001088 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Fabio Utzig9678c972017-05-23 11:28:56 -04001089
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001090 /* sz in this function is always sized on a multiple of the sector size.
1091 * The check against the start offset of the last sector
Fabio Utzig9678c972017-05-23 11:28:56 -04001092 * is to determine if we're swapping the last sector. The last sector
1093 * needs special handling because it's where the trailer lives. If we're
1094 * copying it, we need to use scratch to write the trailer temporarily.
1095 *
1096 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
1097 * controls if special handling is needed (swapping last sector).
1098 */
David Vincze2d736ad2019-02-18 11:50:22 +01001099 last_sector = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT) - 1;
1100 if (img_off + sz > boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT,
1101 last_sector)) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001102 copy_sz -= trailer_sz;
1103 }
1104
Fabio Utzig39000012018-07-30 12:40:20 -03001105 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001106
David Vincze2d736ad2019-02-18 11:50:22 +01001107 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001108 assert (rc == 0);
1109
David Vincze2d736ad2019-02-18 11:50:22 +01001110 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001111 assert (rc == 0);
1112
1113 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch);
1114 assert (rc == 0);
1115
Fabio Utzig39000012018-07-30 12:40:20 -03001116 if (bs->state == BOOT_STATUS_STATE_0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001117 BOOT_LOG_DBG("erasing scratch area");
Christopher Collinsa1c12042019-05-23 14:00:28 -07001118 rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size);
Christopher Collins4772ac42017-02-27 20:08:01 -08001119 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001120
Fabio Utzig39000012018-07-30 12:40:20 -03001121 if (bs->idx == BOOT_STATUS_IDX_0) {
Christopher Collinsa1c12042019-05-23 14:00:28 -07001122 /* Write a trailer to the scratch area, even if we don't need the
1123 * scratch area for status. We need a temporary place to store the
1124 * `swap-type` while we erase the primary trailer.
1125 */
1126 rc = boot_status_init(fap_scratch, bs);
1127 assert(rc == 0);
1128
1129 if (!bs->use_scratch) {
1130 /* Prepare the primary status area... here it is known that the
1131 * last sector is not being used by the image data so it's safe
1132 * to erase.
Fabio Utzig2473ac02017-05-02 12:45:02 -03001133 */
David Vincze2d736ad2019-02-18 11:50:22 +01001134 rc = boot_erase_trailer_sectors(fap_primary_slot);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001135 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001136
Christopher Collinsa1c12042019-05-23 14:00:28 -07001137 rc = boot_status_init(fap_primary_slot, bs);
1138 assert(rc == 0);
1139
1140 /* Erase the temporary trailer from the scratch area. */
1141 rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size);
1142 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001143 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001144 }
1145
Christopher Collinsa1c12042019-05-23 14:00:28 -07001146 rc = boot_copy_sector(fap_secondary_slot, fap_scratch,
1147 img_off, 0, copy_sz);
1148 assert(rc == 0);
1149
Fabio Utzig39000012018-07-30 12:40:20 -03001150 bs->state = BOOT_STATUS_STATE_1;
Christopher Collins4772ac42017-02-27 20:08:01 -08001151 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001152 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001153 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001154
Fabio Utzig39000012018-07-30 12:40:20 -03001155 if (bs->state == BOOT_STATUS_STATE_1) {
David Vincze2d736ad2019-02-18 11:50:22 +01001156 rc = boot_erase_sector(fap_secondary_slot, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001157 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001158
David Vincze2d736ad2019-02-18 11:50:22 +01001159 rc = boot_copy_sector(fap_primary_slot, fap_secondary_slot,
1160 img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001161 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001162
Fabio Utzig39000012018-07-30 12:40:20 -03001163 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001164 /* If not all sectors of the slot are being swapped,
David Vincze2d736ad2019-02-18 11:50:22 +01001165 * guarantee here that only the primary slot will have the state.
Fabio Utzig2473ac02017-05-02 12:45:02 -03001166 */
David Vincze2d736ad2019-02-18 11:50:22 +01001167 rc = boot_erase_trailer_sectors(fap_secondary_slot);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001168 assert(rc == 0);
1169 }
1170
Fabio Utzig39000012018-07-30 12:40:20 -03001171 bs->state = BOOT_STATUS_STATE_2;
Christopher Collins4772ac42017-02-27 20:08:01 -08001172 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001173 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001174 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001175
Fabio Utzig39000012018-07-30 12:40:20 -03001176 if (bs->state == BOOT_STATUS_STATE_2) {
David Vincze2d736ad2019-02-18 11:50:22 +01001177 rc = boot_erase_sector(fap_primary_slot, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001178 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001179
Christopher Collinsa1c12042019-05-23 14:00:28 -07001180 /* NOTE: If this is the final sector, we exclude the image trailer from
1181 * this copy (copy_sz was truncated earlier).
1182 */
David Vincze2d736ad2019-02-18 11:50:22 +01001183 rc = boot_copy_sector(fap_scratch, fap_primary_slot,
1184 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001185 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001186
Fabio Utzig94d998c2017-05-22 11:02:41 -04001187 if (bs->use_scratch) {
Fabio Utzigba829042018-09-18 08:29:34 -03001188 scratch_trailer_off = boot_status_off(fap_scratch);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001189
1190 /* copy current status that is being maintained in scratch */
David Vincze2d736ad2019-02-18 11:50:22 +01001191 rc = boot_copy_sector(fap_scratch, fap_primary_slot,
1192 scratch_trailer_off, img_off + copy_sz,
Marti Bolivare10a7392017-06-14 16:20:07 -04001193 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data));
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001194 BOOT_STATUS_ASSERT(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001195
Fabio Utzig2473ac02017-05-02 12:45:02 -03001196 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1197 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001198 assert(rc == 0);
1199
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001200 if (swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze2d736ad2019-02-18 11:50:22 +01001201 rc = boot_write_image_ok(fap_primary_slot);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001202 assert(rc == 0);
1203 }
1204
Christopher Collinsa1c12042019-05-23 14:00:28 -07001205 if (swap_state.swap_type != BOOT_SWAP_TYPE_NONE) {
David Vinczee2453472019-06-17 12:31:59 +02001206 rc = boot_write_swap_info(fap_primary_slot,
1207 swap_state.swap_type,
1208 0);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001209 assert(rc == 0);
1210 }
1211
David Vincze2d736ad2019-02-18 11:50:22 +01001212 rc = boot_write_swap_size(fap_primary_slot, bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001213 assert(rc == 0);
1214
Fabio Utzigba829042018-09-18 08:29:34 -03001215#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +01001216 rc = boot_write_enc_key(fap_primary_slot, 0, bs->enckey[0]);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001217 assert(rc == 0);
1218
David Vincze2d736ad2019-02-18 11:50:22 +01001219 rc = boot_write_enc_key(fap_primary_slot, 1, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001220 assert(rc == 0);
1221#endif
David Vincze2d736ad2019-02-18 11:50:22 +01001222 rc = boot_write_magic(fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001223 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001224 }
1225
Christopher Collinsa1c12042019-05-23 14:00:28 -07001226 /* If we wrote a trailer to the scratch area, erase it after we persist
1227 * a trailer to the primary slot. We do this to prevent mcuboot from
1228 * reading a stale status from the scratch area in case of immediate
1229 * reset.
1230 */
1231 erase_scratch = bs->use_scratch;
1232 bs->use_scratch = 0;
1233
Christopher Collins92ea77f2016-12-12 15:59:26 -08001234 bs->idx++;
Fabio Utzig39000012018-07-30 12:40:20 -03001235 bs->state = BOOT_STATUS_STATE_0;
Christopher Collins4772ac42017-02-27 20:08:01 -08001236 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001237 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001238
1239 if (erase_scratch) {
1240 rc = boot_erase_sector(fap_scratch, 0, sz);
1241 assert(rc == 0);
1242 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001243 }
Fabio Utzigba829042018-09-18 08:29:34 -03001244
David Vincze2d736ad2019-02-18 11:50:22 +01001245 flash_area_close(fap_primary_slot);
1246 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001247 flash_area_close(fap_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001248}
Fabio Utzig3488eef2017-06-12 10:25:43 -03001249#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001250
1251/**
David Vincze2d736ad2019-02-18 11:50:22 +01001252 * Overwrite primary slot with the image contained in the secondary slot.
1253 * If a prior copy operation was interrupted by a system reset, this function
1254 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001255 *
1256 * @param bs The current boot status. This function reads
1257 * this struct to determine if it is resuming
1258 * an interrupted swap operation. This
1259 * function writes the updated status to this
1260 * function on return.
1261 *
1262 * @return 0 on success; nonzero on failure.
1263 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001264#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001265static int
1266boot_copy_image(struct boot_status *bs)
1267{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001268 size_t sect_count;
1269 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001270 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001271 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001272 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001273 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +01001274 const struct flash_area *fap_primary_slot;
1275 const struct flash_area *fap_secondary_slot;
1276
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001277 (void)bs;
1278
Fabio Utzig13d9e352017-10-05 20:32:31 -03001279#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1280 uint32_t src_size = 0;
David Vincze2d736ad2019-02-18 11:50:22 +01001281 rc = boot_read_image_size(BOOT_SECONDARY_SLOT,
1282 boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT),
1283 &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001284 assert(rc == 0);
1285#endif
David Brown17609d82017-05-05 09:41:34 -06001286
David Vincze2d736ad2019-02-18 11:50:22 +01001287 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1288 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -06001289
David Vincze2d736ad2019-02-18 11:50:22 +01001290 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001291 assert (rc == 0);
1292
David Vincze2d736ad2019-02-18 11:50:22 +01001293 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001294 assert (rc == 0);
1295
David Vincze2d736ad2019-02-18 11:50:22 +01001296 sect_count = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001297 for (sect = 0, size = 0; sect < sect_count; sect++) {
David Vincze2d736ad2019-02-18 11:50:22 +01001298 this_size = boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, sect);
1299 rc = boot_erase_sector(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001300 assert(rc == 0);
1301
1302 size += this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001303
1304#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1305 if (size >= src_size) {
1306 break;
1307 }
1308#endif
David Brown17609d82017-05-05 09:41:34 -06001309 }
1310
Fabio Utzigba829042018-09-18 08:29:34 -03001311#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +01001312 if (IS_ENCRYPTED(boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT))) {
1313 rc = boot_enc_load(boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT),
1314 fap_secondary_slot,
1315 bs->enckey[1]);
1316
Fabio Utzigba829042018-09-18 08:29:34 -03001317 if (rc < 0) {
1318 return BOOT_EBADIMAGE;
1319 }
Fabio Utzige641ea52018-12-03 10:37:53 -02001320 if (rc == 0 && boot_enc_set_key(1, bs->enckey[1])) {
Fabio Utzigba829042018-09-18 08:29:34 -03001321 return BOOT_EBADIMAGE;
1322 }
1323 }
1324#endif
1325
David Vincze2d736ad2019-02-18 11:50:22 +01001326 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1327 size);
1328 rc = boot_copy_sector(fap_secondary_slot, fap_primary_slot, 0, 0, size);
David Brown17609d82017-05-05 09:41:34 -06001329
Fabio Utzig13d9e352017-10-05 20:32:31 -03001330 /*
1331 * Erases header and trailer. The trailer is erased because when a new
1332 * image is written without a trailer as is the case when using newt, the
1333 * trailer that was left might trigger a new upgrade.
1334 */
Christopher Collins2c88e692019-05-22 15:10:14 -07001335 BOOT_LOG_DBG("erasing secondary header");
David Vincze2d736ad2019-02-18 11:50:22 +01001336 rc = boot_erase_sector(fap_secondary_slot,
1337 boot_img_sector_off(&boot_data,
1338 BOOT_SECONDARY_SLOT, 0),
1339 boot_img_sector_size(&boot_data,
1340 BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001341 assert(rc == 0);
David Vincze2d736ad2019-02-18 11:50:22 +01001342 last_sector = boot_img_num_sectors(&boot_data, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -07001343 BOOT_LOG_DBG("erasing secondary trailer");
David Vincze2d736ad2019-02-18 11:50:22 +01001344 rc = boot_erase_sector(fap_secondary_slot,
1345 boot_img_sector_off(&boot_data,
1346 BOOT_SECONDARY_SLOT, last_sector),
1347 boot_img_sector_size(&boot_data,
1348 BOOT_SECONDARY_SLOT, last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001349 assert(rc == 0);
1350
David Vincze2d736ad2019-02-18 11:50:22 +01001351 flash_area_close(fap_primary_slot);
1352 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001353
David Vincze2d736ad2019-02-18 11:50:22 +01001354 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001355
1356 return 0;
1357}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001358#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001359
Christopher Collinsa1c12042019-05-23 14:00:28 -07001360#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -03001361/**
1362 * Swaps the two images in flash. If a prior copy operation was interrupted
1363 * by a system reset, this function completes that operation.
1364 *
1365 * @param bs The current boot status. This function reads
1366 * this struct to determine if it is resuming
1367 * an interrupted swap operation. This
1368 * function writes the updated status to this
1369 * function on return.
1370 *
1371 * @return 0 on success; nonzero on failure.
1372 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001373static int
Fabio Utzig338a19f2018-12-03 08:37:08 -02001374boot_swap_image(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001375{
1376 uint32_t sz;
1377 int first_sector_idx;
1378 int last_sector_idx;
David Vincze2d736ad2019-02-18 11:50:22 +01001379 int last_idx_secondary_slot;
Fabio Utzigcd5774b2017-11-29 10:18:26 -02001380 uint32_t swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001381 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001382#ifdef MCUBOOT_ENC_IMAGES
1383 const struct flash_area *fap;
1384 uint8_t slot;
1385 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001386#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001387 uint32_t size;
1388 uint32_t copy_size;
David Vincze2d736ad2019-02-18 11:50:22 +01001389 uint32_t primary_slot_size;
1390 uint32_t secondary_slot_size;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001391 int rc;
1392
1393 /* FIXME: just do this if asked by user? */
1394
1395 size = copy_size = 0;
1396
Fabio Utzig39000012018-07-30 12:40:20 -03001397 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Fabio Utzig46490722017-09-04 15:34:32 -03001398 /*
1399 * No swap ever happened, so need to find the largest image which
1400 * will be used to determine the amount of sectors to swap.
1401 */
David Vincze2d736ad2019-02-18 11:50:22 +01001402 hdr = boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001403 if (hdr->ih_magic == IMAGE_MAGIC) {
David Vincze2d736ad2019-02-18 11:50:22 +01001404 rc = boot_read_image_size(BOOT_PRIMARY_SLOT, hdr, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001405 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001406 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001407
Fabio Utzigba829042018-09-18 08:29:34 -03001408#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001409 if (IS_ENCRYPTED(hdr)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001410 fap = BOOT_IMG_AREA(&boot_data, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001411 rc = boot_enc_load(hdr, fap, bs->enckey[0]);
1412 assert(rc >= 0);
1413
1414 if (rc == 0) {
1415 rc = boot_enc_set_key(0, bs->enckey[0]);
1416 assert(rc == 0);
1417 } else {
1418 rc = 0;
1419 }
1420 } else {
1421 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1422 }
1423#endif
1424
David Vincze2d736ad2019-02-18 11:50:22 +01001425 hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001426 if (hdr->ih_magic == IMAGE_MAGIC) {
David Vincze2d736ad2019-02-18 11:50:22 +01001427 rc = boot_read_image_size(BOOT_SECONDARY_SLOT, hdr, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001428 assert(rc == 0);
1429 }
1430
Fabio Utzigba829042018-09-18 08:29:34 -03001431#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +01001432 hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001433 if (IS_ENCRYPTED(hdr)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001434 fap = BOOT_IMG_AREA(&boot_data, BOOT_SECONDARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001435 rc = boot_enc_load(hdr, fap, bs->enckey[1]);
1436 assert(rc >= 0);
1437
1438 if (rc == 0) {
1439 rc = boot_enc_set_key(1, bs->enckey[1]);
1440 assert(rc == 0);
1441 } else {
1442 rc = 0;
1443 }
1444 } else {
1445 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1446 }
1447#endif
1448
Fabio Utzig46490722017-09-04 15:34:32 -03001449 if (size > copy_size) {
1450 copy_size = size;
1451 }
1452
1453 bs->swap_size = copy_size;
1454 } else {
1455 /*
1456 * If a swap was under way, the swap_size should already be present
1457 * in the trailer...
1458 */
1459 rc = boot_read_swap_size(&bs->swap_size);
1460 assert(rc == 0);
1461
1462 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001463
1464#ifdef MCUBOOT_ENC_IMAGES
1465 for (slot = 0; slot <= 1; slot++) {
1466 rc = boot_read_enc_key(slot, bs->enckey[slot]);
1467 assert(rc == 0);
1468
1469 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001470 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001471 break;
1472 }
1473 }
1474
1475 if (i != BOOT_ENC_KEY_SIZE) {
1476 boot_enc_set_key(slot, bs->enckey[slot]);
1477 }
1478 }
1479#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001480 }
1481
David Vincze2d736ad2019-02-18 11:50:22 +01001482 primary_slot_size = 0;
1483 secondary_slot_size = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001484 last_sector_idx = 0;
David Vincze2d736ad2019-02-18 11:50:22 +01001485 last_idx_secondary_slot = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001486
1487 /*
1488 * Knowing the size of the largest image between both slots, here we
David Vincze2d736ad2019-02-18 11:50:22 +01001489 * find what is the last sector in the primary slot that needs swapping.
1490 * Since we already know that both slots are compatible, the secondary
1491 * slot's last sector is not really required after this check is finished.
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001492 */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001493 while (1) {
David Vincze2d736ad2019-02-18 11:50:22 +01001494 if ((primary_slot_size < copy_size) ||
1495 (primary_slot_size < secondary_slot_size)) {
1496 primary_slot_size += boot_img_sector_size(&boot_data,
1497 BOOT_PRIMARY_SLOT,
1498 last_sector_idx);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001499 }
David Vincze2d736ad2019-02-18 11:50:22 +01001500 if ((secondary_slot_size < copy_size) ||
1501 (secondary_slot_size < primary_slot_size)) {
1502 secondary_slot_size += boot_img_sector_size(&boot_data,
1503 BOOT_SECONDARY_SLOT,
1504 last_idx_secondary_slot);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001505 }
David Vincze2d736ad2019-02-18 11:50:22 +01001506 if (primary_slot_size >= copy_size &&
1507 secondary_slot_size >= copy_size &&
1508 primary_slot_size == secondary_slot_size) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001509 break;
1510 }
1511 last_sector_idx++;
David Vincze2d736ad2019-02-18 11:50:22 +01001512 last_idx_secondary_slot++;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001513 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001514
1515 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001516 while (last_sector_idx >= 0) {
1517 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
Fabio Utzig39000012018-07-30 12:40:20 -03001518 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001519 boot_swap_sectors(first_sector_idx, sz, bs);
1520 }
1521
1522 last_sector_idx = first_sector_idx - 1;
1523 swap_idx++;
1524 }
1525
David Vincze2d736ad2019-02-18 11:50:22 +01001526#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001527 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001528 BOOT_LOG_WRN("%d status write fails performing the swap",
1529 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001530 }
1531#endif
1532
Christopher Collins92ea77f2016-12-12 15:59:26 -08001533 return 0;
1534}
David Brown17609d82017-05-05 09:41:34 -06001535#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001536
1537/**
David Vincze2d736ad2019-02-18 11:50:22 +01001538 * Marks the image in the primary slot as fully copied.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001539 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001540#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001541static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001542boot_set_copy_done(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001543{
1544 const struct flash_area *fap;
1545 int rc;
1546
David Vincze2d736ad2019-02-18 11:50:22 +01001547 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001548 if (rc != 0) {
1549 return BOOT_EFLASH;
1550 }
1551
1552 rc = boot_write_copy_done(fap);
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001553 flash_area_close(fap);
1554 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001555}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001556#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001557
1558/**
David Vincze2d736ad2019-02-18 11:50:22 +01001559 * Marks a reverted image in the primary slot as confirmed. This is necessary to
1560 * ensure the status bytes from the image revert operation don't get processed
1561 * on a subsequent boot.
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001562 *
1563 * NOTE: image_ok is tested before writing because if there's a valid permanent
David Vincze2d736ad2019-02-18 11:50:22 +01001564 * image installed on the primary slot and the new image to be upgrade to has a
1565 * bad sig, image_ok would be overwritten.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001566 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001567#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001568static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001569boot_set_image_ok(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001570{
1571 const struct flash_area *fap;
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001572 struct boot_swap_state state;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001573 int rc;
1574
David Vincze2d736ad2019-02-18 11:50:22 +01001575 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001576 if (rc != 0) {
1577 return BOOT_EFLASH;
1578 }
1579
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001580 rc = boot_read_swap_state(fap, &state);
1581 if (rc != 0) {
1582 rc = BOOT_EFLASH;
1583 goto out;
1584 }
1585
1586 if (state.image_ok == BOOT_FLAG_UNSET) {
1587 rc = boot_write_image_ok(fap);
1588 }
1589
1590out:
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001591 flash_area_close(fap);
1592 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001593}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001594#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001595
1596/**
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001597 * Performs an image swap if one is required.
1598 *
1599 * @param out_swap_type On success, the type of swap performed gets
1600 * written here.
1601 *
1602 * @return 0 on success; nonzero on failure.
1603 */
1604static int
1605boot_swap_if_needed(int *out_swap_type)
1606{
1607 struct boot_status bs;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001608 int rc;
1609
Christopher Collinsa1c12042019-05-23 14:00:28 -07001610 /* Determine if we rebooted in the middle of an image swap operation. */
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001611 rc = boot_read_status(&bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001612 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001613 if (rc != 0) {
1614 return rc;
1615 }
1616
1617 /* If a partial swap was detected, complete it. */
Fabio Utzig39000012018-07-30 12:40:20 -03001618 if (bs.idx != BOOT_STATUS_IDX_0 || bs.state != BOOT_STATUS_STATE_0) {
Fabio Utziga32f1af2019-01-07 06:50:58 -02001619#ifdef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig338a19f2018-12-03 08:37:08 -02001620 /* Should never arrive here, overwrite-only mode has no swap state. */
1621 assert(0);
1622#else
Christopher Collinsa1c12042019-05-23 14:00:28 -07001623 /* Determine the type of swap operation being resumed from the
1624 * `swap-type` trailer field.
1625 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001626 rc = boot_swap_image(&bs);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001627 assert(rc == 0);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001628#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001629
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001630 } else {
Christopher Collinsa1c12042019-05-23 14:00:28 -07001631 if (bs.swap_type == BOOT_SWAP_TYPE_NONE) {
1632 bs.swap_type = boot_validated_swap_type(&bs);
1633 } else if (boot_validate_slot(BOOT_SECONDARY_SLOT, &bs) != 0) {
1634 bs.swap_type = BOOT_SWAP_TYPE_FAIL;
1635 }
1636 switch (bs.swap_type) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001637 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001638 case BOOT_SWAP_TYPE_PERM:
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001639 case BOOT_SWAP_TYPE_REVERT:
Fabio Utziga32f1af2019-01-07 06:50:58 -02001640#ifdef MCUBOOT_OVERWRITE_ONLY
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001641 rc = boot_copy_image(&bs);
Fabio Utzig338a19f2018-12-03 08:37:08 -02001642#else
1643 rc = boot_swap_image(&bs);
1644#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001645 assert(rc == 0);
1646 break;
Fabio Utzig338a19f2018-12-03 08:37:08 -02001647#ifdef MCUBOOT_BOOTSTRAP
1648 case BOOT_SWAP_TYPE_NONE:
1649 /*
1650 * Header checks are done first because they are inexpensive.
1651 * Since overwrite-only copies starting from offset 0, if
1652 * interrupted, it might leave a valid header magic, so also
David Vincze2d736ad2019-02-18 11:50:22 +01001653 * run validation on the primary slot to be sure it's not OK.
Fabio Utzig338a19f2018-12-03 08:37:08 -02001654 */
David Vincze2d736ad2019-02-18 11:50:22 +01001655 if (boot_check_header_erased(BOOT_PRIMARY_SLOT) == 0 ||
1656 boot_validate_slot(BOOT_PRIMARY_SLOT, &bs) != 0) {
1657 if ((boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT)->ih_magic
1658 == IMAGE_MAGIC ) &&
1659 (boot_validate_slot(BOOT_SECONDARY_SLOT, &bs) == 0)) {
Fabio Utzig338a19f2018-12-03 08:37:08 -02001660 rc = boot_copy_image(&bs);
1661 assert(rc == 0);
1662
1663 /* Returns fail here to trigger a re-read of the headers. */
Christopher Collinsa1c12042019-05-23 14:00:28 -07001664 bs.swap_type = BOOT_SWAP_TYPE_FAIL;
Fabio Utzig338a19f2018-12-03 08:37:08 -02001665 }
1666 }
1667 break;
1668#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001669 }
1670 }
1671
Christopher Collinsa1c12042019-05-23 14:00:28 -07001672 *out_swap_type = bs.swap_type;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001673 return 0;
1674}
1675
1676/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08001677 * Prepares the booting process. This function moves images around in flash as
1678 * appropriate, and tells you what address to boot from.
1679 *
1680 * @param rsp On success, indicates how booting should occur.
1681 *
1682 * @return 0 on success; nonzero on failure.
1683 */
1684int
1685boot_go(struct boot_rsp *rsp)
1686{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001687 int swap_type;
Marti Bolivar84898652017-06-13 17:20:22 -04001688 size_t slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001689 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001690 int fa_id;
David Brown52eee562017-07-05 11:25:09 -06001691 bool reload_headers = false;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001692
1693 /* The array of slot sectors are defined here (as opposed to file scope) so
1694 * that they don't get allocated for non-boot-loader apps. This is
1695 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001696 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001697 */
David Vincze2d736ad2019-02-18 11:50:22 +01001698 static boot_sector_t primary_slot_sectors[BOOT_MAX_IMG_SECTORS];
1699 static boot_sector_t secondary_slot_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001700 static boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
David Vincze2d736ad2019-02-18 11:50:22 +01001701 boot_data.imgs[BOOT_PRIMARY_SLOT].sectors = primary_slot_sectors;
1702 boot_data.imgs[BOOT_SECONDARY_SLOT].sectors = secondary_slot_sectors;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001703 boot_data.scratch.sectors = scratch_sectors;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001704
Fabio Utzigba829042018-09-18 08:29:34 -03001705#ifdef MCUBOOT_ENC_IMAGES
1706 /* FIXME: remove this after RAM is cleared by sim */
1707 boot_enc_zeroize();
1708#endif
1709
Marti Bolivarc0b47912017-06-13 17:18:09 -04001710 /* Open boot_data image areas for the duration of this call. */
1711 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1712 fa_id = flash_area_id_from_image_slot(slot);
1713 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot));
1714 assert(rc == 0);
1715 }
1716 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
1717 &BOOT_SCRATCH_AREA(&boot_data));
1718 assert(rc == 0);
1719
Christopher Collins92ea77f2016-12-12 15:59:26 -08001720 /* Determine the sector layout of the image slots and scratch area. */
1721 rc = boot_read_sectors();
1722 if (rc != 0) {
Fabio Utziga1fae672018-03-30 10:52:38 -03001723 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - too small?",
1724 BOOT_MAX_IMG_SECTORS);
Marti Bolivarc0b47912017-06-13 17:18:09 -04001725 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001726 }
1727
1728 /* Attempt to read an image header from each slot. */
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001729 rc = boot_read_image_headers(false);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001730 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001731 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001732 }
1733
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001734 /* If the image slots aren't compatible, no swap is possible. Just boot
David Vincze2d736ad2019-02-18 11:50:22 +01001735 * into the primary slot.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001736 */
1737 if (boot_slots_compatible()) {
1738 rc = boot_swap_if_needed(&swap_type);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001739 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001740 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001741 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001742 }
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001743
1744 /*
1745 * The following states need image_ok be explicitly set after the
1746 * swap was finished to avoid a new revert.
1747 */
David Vincze2d736ad2019-02-18 11:50:22 +01001748 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
Christopher Collinsa1c12042019-05-23 14:00:28 -07001749 swap_type == BOOT_SWAP_TYPE_FAIL ||
1750 swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001751#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig695d5642017-07-20 09:47:16 -03001752 rc = boot_set_image_ok();
1753 if (rc != 0) {
1754 swap_type = BOOT_SWAP_TYPE_PANIC;
1755 }
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001756#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001757 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001758 } else {
1759 swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001760 }
1761
1762 switch (swap_type) {
1763 case BOOT_SWAP_TYPE_NONE:
David Vincze2d736ad2019-02-18 11:50:22 +01001764 slot = BOOT_PRIMARY_SLOT;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001765 break;
1766
Fabio Utzig695d5642017-07-20 09:47:16 -03001767 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1768 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001769 case BOOT_SWAP_TYPE_REVERT:
David Vincze2d736ad2019-02-18 11:50:22 +01001770 slot = BOOT_SECONDARY_SLOT;
David Brown52eee562017-07-05 11:25:09 -06001771 reload_headers = true;
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001772#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig695d5642017-07-20 09:47:16 -03001773 rc = boot_set_copy_done();
1774 if (rc != 0) {
1775 swap_type = BOOT_SWAP_TYPE_PANIC;
1776 }
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001777#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001778 break;
1779
1780 case BOOT_SWAP_TYPE_FAIL:
David Vincze2d736ad2019-02-18 11:50:22 +01001781 /* The image in the secondary slot was invalid and is now erased.
1782 * Ensure we don't try to boot into it again on the next reboot.
1783 * Do this by pretending we just reverted back to the primary slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001784 */
David Vincze2d736ad2019-02-18 11:50:22 +01001785 slot = BOOT_PRIMARY_SLOT;
David Brown52eee562017-07-05 11:25:09 -06001786 reload_headers = true;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001787 break;
1788
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001789 default:
Fabio Utzig695d5642017-07-20 09:47:16 -03001790 swap_type = BOOT_SWAP_TYPE_PANIC;
1791 }
1792
1793 if (swap_type == BOOT_SWAP_TYPE_PANIC) {
1794 BOOT_LOG_ERR("panic!");
Fabio Utzigb5b2f552017-06-30 10:03:47 -03001795 assert(0);
Fabio Utzig695d5642017-07-20 09:47:16 -03001796
1797 /* Loop forever... */
1798 while (1) {}
Christopher Collins92ea77f2016-12-12 15:59:26 -08001799 }
1800
David Brown52eee562017-07-05 11:25:09 -06001801 if (reload_headers) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001802 rc = boot_read_image_headers(false);
Fabio Utzigc6a7b0c2017-09-13 19:01:15 -03001803 if (rc != 0) {
1804 goto out;
1805 }
1806 /* Since headers were reloaded, it can be assumed we just performed a
1807 * swap or overwrite. Now the header info that should be used to
David Vincze2d736ad2019-02-18 11:50:22 +01001808 * provide the data for the bootstrap, which previously was at the
1809 * secondary slot, was updated to the primary slot.
Fabio Utzigc6a7b0c2017-09-13 19:01:15 -03001810 */
David Vincze2d736ad2019-02-18 11:50:22 +01001811 slot = BOOT_PRIMARY_SLOT;
David Brown52eee562017-07-05 11:25:09 -06001812 }
1813
David Vincze2d736ad2019-02-18 11:50:22 +01001814#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
1815 rc = boot_validate_slot(BOOT_PRIMARY_SLOT, NULL);
David Brown554c52e2017-06-30 16:01:07 -06001816 if (rc != 0) {
1817 rc = BOOT_EBADIMAGE;
1818 goto out;
1819 }
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001820#else
David Vincze2d736ad2019-02-18 11:50:22 +01001821 /* Even if we're not re-validating the primary slot, we could be booting
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001822 * onto an empty flash chip. At least do a basic sanity check that
1823 * the magic number on the image is OK.
1824 */
David Vincze2d736ad2019-02-18 11:50:22 +01001825 if (boot_data.imgs[BOOT_PRIMARY_SLOT].hdr.ih_magic != IMAGE_MAGIC) {
1826 BOOT_LOG_ERR("bad image magic 0x%lx",
1827 (unsigned long)boot_data.imgs[BOOT_PRIMARY_SLOT].hdr.ih_magic);
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001828 rc = BOOT_EBADIMAGE;
1829 goto out;
1830 }
David Brown554c52e2017-06-30 16:01:07 -06001831#endif
1832
Christopher Collins92ea77f2016-12-12 15:59:26 -08001833 /* Always boot from the primary slot. */
David Vincze2d736ad2019-02-18 11:50:22 +01001834 rsp->br_flash_dev_id = boot_data.imgs[BOOT_PRIMARY_SLOT].area->fa_device_id;
1835 rsp->br_image_off = boot_img_slot_off(&boot_data, BOOT_PRIMARY_SLOT);
Marti Bolivarf804f622017-06-12 15:41:48 -04001836 rsp->br_hdr = boot_img_hdr(&boot_data, slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001837
Marti Bolivarc0b47912017-06-13 17:18:09 -04001838 out:
1839 flash_area_close(BOOT_SCRATCH_AREA(&boot_data));
1840 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1841 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
1842 }
1843 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001844}
1845
1846int
1847split_go(int loader_slot, int split_slot, void **entry)
1848{
Marti Bolivarc50926f2017-06-14 09:35:40 -04001849 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001850 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001851 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001852 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001853 int rc;
1854
Christopher Collins92ea77f2016-12-12 15:59:26 -08001855 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1856 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001857 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001858 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04001859 boot_data.imgs[loader_slot].sectors = sectors + 0;
1860 boot_data.imgs[split_slot].sectors = sectors + BOOT_MAX_IMG_SECTORS;
1861
1862 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1863 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07001864 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04001865 assert(rc == 0);
1866 split_flash_id = flash_area_id_from_image_slot(split_slot);
1867 rc = flash_area_open(split_flash_id,
1868 &BOOT_IMG_AREA(&boot_data, split_slot));
1869 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001870
1871 /* Determine the sector layout of the image slots and scratch area. */
1872 rc = boot_read_sectors();
1873 if (rc != 0) {
1874 rc = SPLIT_GO_ERR;
1875 goto done;
1876 }
1877
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001878 rc = boot_read_image_headers(true);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001879 if (rc != 0) {
1880 goto done;
1881 }
1882
Christopher Collins92ea77f2016-12-12 15:59:26 -08001883 /* Don't check the bootable image flag because we could really call a
1884 * bootable or non-bootable image. Just validate that the image check
1885 * passes which is distinct from the normal check.
1886 */
Marti Bolivarf804f622017-06-12 15:41:48 -04001887 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001888 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04001889 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001890 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001891 if (rc != 0) {
1892 rc = SPLIT_GO_NON_MATCHING;
1893 goto done;
1894 }
1895
Marti Bolivarea088872017-06-12 17:10:49 -04001896 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04001897 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001898 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001899 rc = SPLIT_GO_OK;
1900
1901done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04001902 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
1903 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001904 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001905 return rc;
1906}