blob: a843d732e8c36a2adce19e00813f260e3552b670 [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;
David Vinczeb75c12a2019-03-22 14:58:33 +010050uint8_t current_image = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -080051
David Vincze2d736ad2019-02-18 11:50:22 +010052#if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT) && !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utziga0e1cce2017-11-23 20:04:01 -020053static int boot_status_fails = 0;
54#define BOOT_STATUS_ASSERT(x) \
55 do { \
Johann Fischered8461b2018-02-15 16:50:31 +010056 if (!(x)) { \
Fabio Utziga0e1cce2017-11-23 20:04:01 -020057 boot_status_fails++; \
58 } \
59 } while (0)
60#else
Fabio Utzig57c40f72017-12-12 21:48:30 -020061#define BOOT_STATUS_ASSERT(x) ASSERT(x)
Fabio Utziga0e1cce2017-11-23 20:04:01 -020062#endif
63
Christopher Collins92ea77f2016-12-12 15:59:26 -080064struct boot_status_table {
David Vincze2d736ad2019-02-18 11:50:22 +010065 uint8_t bst_magic_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -080066 uint8_t bst_magic_scratch;
David Vincze2d736ad2019-02-18 11:50:22 +010067 uint8_t bst_copy_done_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -080068 uint8_t bst_status_source;
69};
70
71/**
72 * This set of tables maps swap state contents to boot status location.
73 * When searching for a match, these tables must be iterated in order.
74 */
75static const struct boot_status_table boot_status_tables[] = {
76 {
David Vincze2d736ad2019-02-18 11:50:22 +010077 /* | primary slot | scratch |
78 * ----------+--------------+--------------|
79 * magic | Good | Any |
80 * copy-done | Set | N/A |
81 * ----------+--------------+--------------'
82 * source: none |
83 * ----------------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -080084 */
David Vincze2d736ad2019-02-18 11:50:22 +010085 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
Christopher Collinsa1c12042019-05-23 14:00:28 -070086 .bst_magic_scratch = BOOT_MAGIC_NOTGOOD,
David Vincze2d736ad2019-02-18 11:50:22 +010087 .bst_copy_done_primary_slot = BOOT_FLAG_SET,
88 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
Christopher Collins92ea77f2016-12-12 15:59:26 -080089 },
90
91 {
David Vincze2d736ad2019-02-18 11:50:22 +010092 /* | primary slot | scratch |
93 * ----------+--------------+--------------|
94 * magic | Good | Any |
95 * copy-done | Unset | N/A |
96 * ----------+--------------+--------------'
97 * source: primary slot |
98 * ----------------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -080099 */
David Vincze2d736ad2019-02-18 11:50:22 +0100100 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
Christopher Collinsa1c12042019-05-23 14:00:28 -0700101 .bst_magic_scratch = BOOT_MAGIC_NOTGOOD,
David Vincze2d736ad2019-02-18 11:50:22 +0100102 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
103 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800104 },
105
106 {
David Vincze2d736ad2019-02-18 11:50:22 +0100107 /* | primary slot | scratch |
108 * ----------+--------------+--------------|
109 * magic | Any | Good |
110 * copy-done | Any | N/A |
111 * ----------+--------------+--------------'
112 * source: scratch |
113 * ----------------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -0800114 */
David Vincze2d736ad2019-02-18 11:50:22 +0100115 .bst_magic_primary_slot = BOOT_MAGIC_ANY,
116 .bst_magic_scratch = BOOT_MAGIC_GOOD,
117 .bst_copy_done_primary_slot = BOOT_FLAG_ANY,
118 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800119 },
Christopher Collins92ea77f2016-12-12 15:59:26 -0800120 {
David Vincze2d736ad2019-02-18 11:50:22 +0100121 /* | primary slot | scratch |
122 * ----------+--------------+--------------|
123 * magic | Unset | Any |
124 * copy-done | Unset | N/A |
125 * ----------+--------------+--------------|
126 * source: varies |
127 * ----------------------------------------+--------------------------+
Christopher Collins92ea77f2016-12-12 15:59:26 -0800128 * This represents one of two cases: |
129 * o No swaps ever (no status to read, so no harm in checking). |
David Vincze2d736ad2019-02-18 11:50:22 +0100130 * o Mid-revert; status in primary slot. |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800131 * -------------------------------------------------------------------'
132 */
David Vincze2d736ad2019-02-18 11:50:22 +0100133 .bst_magic_primary_slot = BOOT_MAGIC_UNSET,
134 .bst_magic_scratch = BOOT_MAGIC_ANY,
135 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
136 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800137 },
138};
139
140#define BOOT_STATUS_TABLES_COUNT \
141 (sizeof boot_status_tables / sizeof boot_status_tables[0])
142
Marti Bolivarfd20c762017-02-07 16:52:50 -0500143#define BOOT_LOG_SWAP_STATE(area, state) \
Christopher Collinsa1c12042019-05-23 14:00:28 -0700144 BOOT_LOG_INF("%s: magic=%s, swap_type=0x%x, copy_done=0x%x, " \
145 "image_ok=0x%x", \
Marti Bolivarfd20c762017-02-07 16:52:50 -0500146 (area), \
147 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
148 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
149 "bad"), \
Christopher Collinsa1c12042019-05-23 14:00:28 -0700150 (state)->swap_type, \
Marti Bolivarfd20c762017-02-07 16:52:50 -0500151 (state)->copy_done, \
152 (state)->image_ok)
153
Christopher Collins92ea77f2016-12-12 15:59:26 -0800154/**
David Vincze2d736ad2019-02-18 11:50:22 +0100155 * Determines where in flash the most recent boot status is stored. The boot
Christopher Collins92ea77f2016-12-12 15:59:26 -0800156 * status is necessary for completing a swap that was interrupted by a boot
157 * loader reset.
158 *
David Vincze2d736ad2019-02-18 11:50:22 +0100159 * @return A BOOT_STATUS_SOURCE_[...] code indicating where status should
160 * be read from.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800161 */
162static int
163boot_status_source(void)
164{
165 const struct boot_status_table *table;
166 struct boot_swap_state state_scratch;
David Vincze2d736ad2019-02-18 11:50:22 +0100167 struct boot_swap_state state_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800168 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200169 size_t i;
Marti Bolivarfd20c762017-02-07 16:52:50 -0500170 uint8_t source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800171
David Vincze2d736ad2019-02-18 11:50:22 +0100172 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY,
173 &state_primary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800174 assert(rc == 0);
175
Fabio Utzig2473ac02017-05-02 12:45:02 -0300176 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800177 assert(rc == 0);
178
David Vincze2d736ad2019-02-18 11:50:22 +0100179 BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot);
Marti Bolivarfd20c762017-02-07 16:52:50 -0500180 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
181
Christopher Collins92ea77f2016-12-12 15:59:26 -0800182 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300183 table = &boot_status_tables[i];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800184
Christopher Collinsa1c12042019-05-23 14:00:28 -0700185 if (boot_magic_compatible_check(table->bst_magic_primary_slot,
186 state_primary_slot.magic) &&
187 boot_magic_compatible_check(table->bst_magic_scratch,
188 state_scratch.magic) &&
David Vincze2d736ad2019-02-18 11:50:22 +0100189 (table->bst_copy_done_primary_slot == BOOT_FLAG_ANY ||
190 table->bst_copy_done_primary_slot == state_primary_slot.copy_done))
191 {
Marti Bolivarfd20c762017-02-07 16:52:50 -0500192 source = table->bst_status_source;
193 BOOT_LOG_INF("Boot source: %s",
194 source == BOOT_STATUS_SOURCE_NONE ? "none" :
195 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
David Vincze2d736ad2019-02-18 11:50:22 +0100196 source == BOOT_STATUS_SOURCE_PRIMARY_SLOT ?
197 "primary slot" : "BUG; can't happen");
Marti Bolivarfd20c762017-02-07 16:52:50 -0500198 return source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800199 }
200 }
201
Marti Bolivarfd20c762017-02-07 16:52:50 -0500202 BOOT_LOG_INF("Boot source: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800203 return BOOT_STATUS_SOURCE_NONE;
204}
205
David Brownf5b33d82017-09-01 10:58:27 -0600206/*
207 * Compute the total size of the given image. Includes the size of
208 * the TLVs.
209 */
Fabio Utzig13d9e352017-10-05 20:32:31 -0300210#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600211static int
212boot_read_image_size(int slot, struct image_header *hdr, uint32_t *size)
213{
214 const struct flash_area *fap;
215 struct image_tlv_info info;
216 int area_id;
217 int rc;
218
219 area_id = flash_area_id_from_image_slot(slot);
220 rc = flash_area_open(area_id, &fap);
221 if (rc != 0) {
222 rc = BOOT_EFLASH;
223 goto done;
224 }
225
226 rc = flash_area_read(fap, hdr->ih_hdr_size + hdr->ih_img_size,
227 &info, sizeof(info));
228 if (rc != 0) {
229 rc = BOOT_EFLASH;
230 goto done;
231 }
232 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
233 rc = BOOT_EBADIMAGE;
234 goto done;
235 }
236 *size = hdr->ih_hdr_size + hdr->ih_img_size + info.it_tlv_tot;
237 rc = 0;
238
239done:
240 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300241 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600242}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300243#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600244
Fabio Utzigc08ed212017-06-20 19:28:36 -0300245static int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800246boot_read_image_header(int slot, struct image_header *out_hdr)
247{
248 const struct flash_area *fap;
249 int area_id;
250 int rc;
251
252 area_id = flash_area_id_from_image_slot(slot);
253 rc = flash_area_open(area_id, &fap);
254 if (rc != 0) {
255 rc = BOOT_EFLASH;
256 goto done;
257 }
258
259 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
260 if (rc != 0) {
261 rc = BOOT_EFLASH;
262 goto done;
263 }
264
265 rc = 0;
266
267done:
268 flash_area_close(fap);
269 return rc;
270}
271
272static int
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200273boot_read_image_headers(bool require_all)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800274{
275 int rc;
276 int i;
277
278 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Marti Bolivarf804f622017-06-12 15:41:48 -0400279 rc = boot_read_image_header(i, boot_img_hdr(&boot_data, i));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800280 if (rc != 0) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200281 /* If `require_all` is set, fail on any single fail, otherwise
282 * if at least the first slot's header was read successfully,
283 * then the boot loader can attempt a boot.
284 *
285 * Failure to read any headers is a fatal error.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800286 */
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200287 if (i > 0 && !require_all) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800288 return 0;
289 } else {
290 return rc;
291 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800292 }
293 }
294
295 return 0;
296}
297
298static uint8_t
299boot_write_sz(void)
300{
301 uint8_t elem_sz;
302 uint8_t align;
303
304 /* Figure out what size to write update status update as. The size depends
305 * on what the minimum write size is for scratch area, active image slot.
306 * We need to use the bigger of those 2 values.
307 */
David Vincze2d736ad2019-02-18 11:50:22 +0100308 elem_sz = flash_area_align(boot_data.imgs[BOOT_PRIMARY_SLOT].area);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200309 align = flash_area_align(boot_data.scratch.area);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800310 if (align > elem_sz) {
311 elem_sz = align;
312 }
313
314 return elem_sz;
315}
316
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200317/*
318 * Slots are compatible when all sectors that store upto to size of the image
319 * round up to sector size, in both slot's are able to fit in the scratch
320 * area, and have sizes that are a multiple of each other (powers of two
321 * presumably!).
322 */
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800323static int
324boot_slots_compatible(void)
325{
David Vincze2d736ad2019-02-18 11:50:22 +0100326 size_t num_sectors_primary;
327 size_t num_sectors_secondary;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200328 size_t sz0, sz1;
David Vincze2d736ad2019-02-18 11:50:22 +0100329 size_t primary_slot_sz, secondary_slot_sz;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200330 size_t scratch_sz;
331 size_t i, j;
332 int8_t smaller;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800333
David Vincze2d736ad2019-02-18 11:50:22 +0100334 num_sectors_primary =
335 boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT);
336 num_sectors_secondary =
337 boot_img_num_sectors(&boot_data, BOOT_SECONDARY_SLOT);
338 if ((num_sectors_primary > BOOT_MAX_IMG_SECTORS) ||
339 (num_sectors_secondary > BOOT_MAX_IMG_SECTORS)) {
Fabio Utziga1fae672018-03-30 10:52:38 -0300340 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800341 return 0;
342 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300343
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200344 scratch_sz = boot_scratch_area_size(&boot_data);
345
346 /*
347 * The following loop scans all sectors in a linear fashion, assuring that
348 * for each possible sector in each slot, it is able to fit in the other
349 * slot's sector or sectors. Slot's should be compatible as long as any
350 * number of a slot's sectors are able to fit into another, which only
351 * excludes cases where sector sizes are not a multiple of each other.
352 */
David Vincze2d736ad2019-02-18 11:50:22 +0100353 i = sz0 = primary_slot_sz = 0;
354 j = sz1 = secondary_slot_sz = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200355 smaller = 0;
David Vincze2d736ad2019-02-18 11:50:22 +0100356 while (i < num_sectors_primary || j < num_sectors_secondary) {
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200357 if (sz0 == sz1) {
David Vincze2d736ad2019-02-18 11:50:22 +0100358 sz0 += boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
359 sz1 += boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT, j);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200360 i++;
361 j++;
362 } else if (sz0 < sz1) {
David Vincze2d736ad2019-02-18 11:50:22 +0100363 sz0 += boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
364 /* Guarantee that multiple sectors of the secondary slot
365 * fit into the primary slot.
366 */
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200367 if (smaller == 2) {
368 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors");
369 return 0;
370 }
371 smaller = 1;
372 i++;
373 } else {
David Vincze2d736ad2019-02-18 11:50:22 +0100374 sz1 += boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT, j);
375 /* Guarantee that multiple sectors of the primary slot
376 * fit into the secondary slot.
377 */
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200378 if (smaller == 1) {
379 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors");
380 return 0;
381 }
382 smaller = 2;
383 j++;
384 }
385 if (sz0 == sz1) {
David Vincze2d736ad2019-02-18 11:50:22 +0100386 primary_slot_sz += sz0;
387 secondary_slot_sz += sz1;
388 /* Scratch has to fit each swap operation to the size of the larger
389 * sector among the primary slot and the secondary slot.
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200390 */
391 if (sz0 > scratch_sz || sz1 > scratch_sz) {
392 BOOT_LOG_WRN("Cannot upgrade: not all sectors fit inside scratch");
393 return 0;
394 }
395 smaller = sz0 = sz1 = 0;
396 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300397 }
398
David Vincze2d736ad2019-02-18 11:50:22 +0100399 if ((i != num_sectors_primary) ||
400 (j != num_sectors_secondary) ||
401 (primary_slot_sz != secondary_slot_sz)) {
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200402 BOOT_LOG_WRN("Cannot upgrade: slots are not compatible");
403 return 0;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800404 }
405
406 return 1;
407}
408
Christopher Collins92ea77f2016-12-12 15:59:26 -0800409/**
410 * Determines the sector layout of both image slots and the scratch area.
411 * This information is necessary for calculating the number of bytes to erase
412 * and copy during an image swap. The information collected during this
413 * function is used to populate the boot_data global.
414 */
415static int
416boot_read_sectors(void)
417{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800418 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800419
David Vincze2d736ad2019-02-18 11:50:22 +0100420 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_PRIMARY);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800421 if (rc != 0) {
422 return BOOT_EFLASH;
423 }
424
David Vincze2d736ad2019-02-18 11:50:22 +0100425 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_SECONDARY);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800426 if (rc != 0) {
427 return BOOT_EFLASH;
428 }
429
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200430 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_SCRATCH);
431 if (rc != 0) {
432 return BOOT_EFLASH;
433 }
434
Marti Bolivare10a7392017-06-14 16:20:07 -0400435 BOOT_WRITE_SZ(&boot_data) = boot_write_sz();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800436
437 return 0;
438}
439
440static uint32_t
441boot_status_internal_off(int idx, int state, int elem_sz)
442{
443 int idx_sz;
444
445 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
446
Fabio Utzig39000012018-07-30 12:40:20 -0300447 return (idx - BOOT_STATUS_IDX_0) * idx_sz +
448 (state - BOOT_STATUS_STATE_0) * elem_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800449}
450
451/**
452 * Reads the status of a partially-completed swap, if any. This is necessary
453 * to recover in case the boot lodaer was reset in the middle of a swap
454 * operation.
455 */
456static int
457boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs)
458{
459 uint32_t off;
460 uint8_t status;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300461 int max_entries;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800462 int found;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200463 int found_idx;
464 int invalid;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800465 int rc;
466 int i;
467
468 off = boot_status_off(fap);
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400469 max_entries = boot_status_entries(fap);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300470
Christopher Collins92ea77f2016-12-12 15:59:26 -0800471 found = 0;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200472 found_idx = 0;
473 invalid = 0;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300474 for (i = 0; i < max_entries; i++) {
Fabio Utzig178be542018-09-19 08:12:56 -0300475 rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(&boot_data),
476 &status, 1);
477 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800478 return BOOT_EFLASH;
479 }
480
Fabio Utzig178be542018-09-19 08:12:56 -0300481 if (rc == 1) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200482 if (found && !found_idx) {
483 found_idx = i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800484 }
485 } else if (!found) {
486 found = 1;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200487 } else if (found_idx) {
488 invalid = 1;
489 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800490 }
491 }
492
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200493 if (invalid) {
494 /* This means there was an error writing status on the last
495 * swap. Tell user and move on to validation!
496 */
497 BOOT_LOG_ERR("Detected inconsistent status!");
498
David Vincze2d736ad2019-02-18 11:50:22 +0100499#if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
500 /* With validation of the primary slot disabled, there is no way
501 * to be sure the swapped primary slot is OK, so abort!
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200502 */
503 assert(0);
504#endif
505 }
506
Christopher Collins92ea77f2016-12-12 15:59:26 -0800507 if (found) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200508 if (!found_idx) {
509 found_idx = i;
510 }
511 found_idx--;
Fabio Utzig39000012018-07-30 12:40:20 -0300512 bs->idx = (found_idx / BOOT_STATUS_STATE_COUNT) + 1;
513 bs->state = (found_idx % BOOT_STATUS_STATE_COUNT) + 1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800514 }
515
516 return 0;
517}
518
519/**
520 * Reads the boot status from the flash. The boot status contains
521 * the current state of an interrupted image copy operation. If the boot
522 * status is not present, or it indicates that previous copy finished,
523 * there is no operation in progress.
524 */
525static int
526boot_read_status(struct boot_status *bs)
527{
528 const struct flash_area *fap;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700529 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200530 uint8_t swap_info;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800531 int status_loc;
532 int area_id;
533 int rc;
534
535 memset(bs, 0, sizeof *bs);
Fabio Utzig39000012018-07-30 12:40:20 -0300536 bs->idx = BOOT_STATUS_IDX_0;
537 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700538 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800539
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700540#ifdef MCUBOOT_OVERWRITE_ONLY
541 /* Overwrite-only doesn't make use of the swap status area. */
542 return 0;
543#endif
544
Christopher Collins92ea77f2016-12-12 15:59:26 -0800545 status_loc = boot_status_source();
546 switch (status_loc) {
547 case BOOT_STATUS_SOURCE_NONE:
548 return 0;
549
550 case BOOT_STATUS_SOURCE_SCRATCH:
551 area_id = FLASH_AREA_IMAGE_SCRATCH;
552 break;
553
David Vincze2d736ad2019-02-18 11:50:22 +0100554 case BOOT_STATUS_SOURCE_PRIMARY_SLOT:
555 area_id = FLASH_AREA_IMAGE_PRIMARY;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800556 break;
557
558 default:
559 assert(0);
560 return BOOT_EBADARGS;
561 }
562
563 rc = flash_area_open(area_id, &fap);
564 if (rc != 0) {
565 return BOOT_EFLASH;
566 }
567
Fabio Utzig46490722017-09-04 15:34:32 -0300568 rc = boot_read_status_bytes(fap, bs);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700569 if (rc == 0) {
David Vinczee2453472019-06-17 12:31:59 +0200570 off = boot_swap_info_off(fap);
571 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700572 if (rc == 1) {
David Vinczee2453472019-06-17 12:31:59 +0200573 BOOT_SET_SWAP_INFO(swap_info, 0, BOOT_SWAP_TYPE_NONE);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700574 rc = 0;
575 }
David Vinczee2453472019-06-17 12:31:59 +0200576
577 /* Extract the swap type info */
578 bs->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700579 }
Fabio Utzig46490722017-09-04 15:34:32 -0300580
581 flash_area_close(fap);
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700582
Fabio Utzig46490722017-09-04 15:34:32 -0300583 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800584}
585
586/**
587 * Writes the supplied boot status to the flash file system. The boot status
588 * contains the current state of an in-progress image copy operation.
589 *
590 * @param bs The boot status to write.
591 *
592 * @return 0 on success; nonzero on failure.
593 */
594int
595boot_write_status(struct boot_status *bs)
596{
597 const struct flash_area *fap;
598 uint32_t off;
599 int area_id;
600 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300601 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700602 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300603 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800604
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300605 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100606 * the trailer. Since in the last step the primary slot is erased, the
607 * first two status writes go to the scratch which will be copied to
608 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300609 */
610
Fabio Utzig2473ac02017-05-02 12:45:02 -0300611 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800612 /* Write to scratch. */
613 area_id = FLASH_AREA_IMAGE_SCRATCH;
614 } else {
David Vincze2d736ad2019-02-18 11:50:22 +0100615 /* Write to the primary slot. */
616 area_id = FLASH_AREA_IMAGE_PRIMARY;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800617 }
618
619 rc = flash_area_open(area_id, &fap);
620 if (rc != 0) {
621 rc = BOOT_EFLASH;
622 goto done;
623 }
624
625 off = boot_status_off(fap) +
Marti Bolivare10a7392017-06-14 16:20:07 -0400626 boot_status_internal_off(bs->idx, bs->state,
627 BOOT_WRITE_SZ(&boot_data));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200628 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300629 erased_val = flash_area_erased_val(fap);
630 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700631 buf[0] = bs->state;
632
633 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800634 if (rc != 0) {
635 rc = BOOT_EFLASH;
636 goto done;
637 }
638
639 rc = 0;
640
641done:
642 flash_area_close(fap);
643 return rc;
644}
645
646/*
647 * Validate image hash/signature in a slot.
648 */
649static int
Fabio Utzigba829042018-09-18 08:29:34 -0300650boot_image_check(struct image_header *hdr, const struct flash_area *fap,
651 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800652{
David Browndb1d9d32017-01-06 11:07:54 -0700653 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigba829042018-09-18 08:29:34 -0300654 int rc;
655
656#ifndef MCUBOOT_ENC_IMAGES
657 (void)bs;
658 (void)rc;
659#else
David Vincze2d736ad2019-02-18 11:50:22 +0100660 if ((fap->fa_id == FLASH_AREA_IMAGE_SECONDARY) && IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300661 rc = boot_enc_load(hdr, fap, bs->enckey[1]);
662 if (rc < 0) {
663 return BOOT_EBADIMAGE;
664 }
665 if (rc == 0 && boot_enc_set_key(1, bs->enckey[1])) {
666 return BOOT_EBADIMAGE;
667 }
668 }
669#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800670
Christopher Collins92ea77f2016-12-12 15:59:26 -0800671 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
672 NULL, 0, NULL)) {
673 return BOOT_EBADIMAGE;
674 }
675 return 0;
676}
677
678static int
679split_image_check(struct image_header *app_hdr,
680 const struct flash_area *app_fap,
681 struct image_header *loader_hdr,
682 const struct flash_area *loader_fap)
683{
684 static void *tmpbuf;
685 uint8_t loader_hash[32];
686
687 if (!tmpbuf) {
688 tmpbuf = malloc(BOOT_TMPBUF_SZ);
689 if (!tmpbuf) {
690 return BOOT_ENOMEM;
691 }
692 }
693
694 if (bootutil_img_validate(loader_hdr, loader_fap, tmpbuf, BOOT_TMPBUF_SZ,
695 NULL, 0, loader_hash)) {
696 return BOOT_EBADIMAGE;
697 }
698
699 if (bootutil_img_validate(app_hdr, app_fap, tmpbuf, BOOT_TMPBUF_SZ,
700 loader_hash, 32, NULL)) {
701 return BOOT_EBADIMAGE;
702 }
703
704 return 0;
705}
706
Fabio Utzig338a19f2018-12-03 08:37:08 -0200707/*
708 * Check that a memory area consists of a given value.
709 */
710static inline bool
711boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300712{
713 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200714 uint8_t *p = (uint8_t *)data;
715 for (i = 0; i < len; i++) {
716 if (val != p[i]) {
717 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300718 }
719 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200720 return true;
721}
722
723static int
724boot_check_header_erased(int slot)
725{
726 const struct flash_area *fap;
727 struct image_header *hdr;
728 uint8_t erased_val;
729 int rc;
730
731 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
732 if (rc != 0) {
733 return -1;
734 }
735
736 erased_val = flash_area_erased_val(fap);
737 flash_area_close(fap);
738
739 hdr = boot_img_hdr(&boot_data, slot);
740 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
741 return -1;
742 }
743
744 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300745}
746
Christopher Collins92ea77f2016-12-12 15:59:26 -0800747static int
Fabio Utzigba829042018-09-18 08:29:34 -0300748boot_validate_slot(int slot, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800749{
750 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400751 struct image_header *hdr;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800752 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300753
David Brownd930ec62016-12-14 07:59:48 -0700754 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800755 if (rc != 0) {
756 return BOOT_EFLASH;
757 }
758
Fabio Utzig39000012018-07-30 12:40:20 -0300759 hdr = boot_img_hdr(&boot_data, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200760 if (boot_check_header_erased(slot) == 0 || (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100761 /* No bootable image in slot; continue booting from the primary slot. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200762 rc = -1;
763 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300764 }
765
Fabio Utzigba829042018-09-18 08:29:34 -0300766 if ((hdr->ih_magic != IMAGE_MAGIC || boot_image_check(hdr, fap, bs) != 0)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100767 if (slot != BOOT_PRIMARY_SLOT) {
David Brownb38e0442017-02-24 13:57:12 -0700768 flash_area_erase(fap, 0, fap->fa_size);
David Vincze2d736ad2019-02-18 11:50:22 +0100769 /* Image in the secondary slot is invalid. Erase the image and
770 * continue booting from the primary slot.
David Brownb38e0442017-02-24 13:57:12 -0700771 */
772 }
David Vincze2d736ad2019-02-18 11:50:22 +0100773 BOOT_LOG_ERR("Image in the %s slot is not valid!",
774 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
Fabio Utzig338a19f2018-12-03 08:37:08 -0200775 rc = -1;
776 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800777 }
778
David Vincze2d736ad2019-02-18 11:50:22 +0100779 /* Image in the secondary slot is valid. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200780 rc = 0;
781
782out:
783 flash_area_close(fap);
784 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800785}
786
787/**
788 * Determines which swap operation to perform, if any. If it is determined
David Vincze2d736ad2019-02-18 11:50:22 +0100789 * that a swap operation is required, the image in the secondary slot is checked
790 * for validity. If the image in the secondary slot is invalid, it is erased,
791 * and a swap type of "none" is indicated.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800792 *
793 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
794 */
795static int
Fabio Utzigba829042018-09-18 08:29:34 -0300796boot_validated_swap_type(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800797{
798 int swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800799
800 swap_type = boot_swap_type();
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300801 switch (swap_type) {
802 case BOOT_SWAP_TYPE_TEST:
803 case BOOT_SWAP_TYPE_PERM:
804 case BOOT_SWAP_TYPE_REVERT:
David Vincze2d736ad2019-02-18 11:50:22 +0100805 /* Boot loader wants to switch to the secondary slot.
806 * Ensure image is valid.
807 */
808 if (boot_validate_slot(BOOT_SECONDARY_SLOT, bs) != 0) {
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300809 swap_type = BOOT_SWAP_TYPE_FAIL;
810 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800811 }
812
813 return swap_type;
814}
815
816/**
817 * Calculates the number of sectors the scratch area can contain. A "last"
818 * source sector is specified because images are copied backwards in flash
819 * (final index to index number 0).
820 *
821 * @param last_sector_idx The index of the last source sector
822 * (inclusive).
823 * @param out_first_sector_idx The index of the first source sector
824 * (inclusive) gets written here.
825 *
826 * @return The number of bytes comprised by the
827 * [first-sector, last-sector] range.
828 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300829#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800830static uint32_t
831boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
832{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400833 size_t scratch_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800834 uint32_t new_sz;
835 uint32_t sz;
836 int i;
837
838 sz = 0;
839
Marti Bolivard3269fd2017-06-12 16:31:12 -0400840 scratch_sz = boot_scratch_area_size(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800841 for (i = last_sector_idx; i >= 0; i--) {
David Vincze2d736ad2019-02-18 11:50:22 +0100842 new_sz = sz + boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200843 /*
David Vincze2d736ad2019-02-18 11:50:22 +0100844 * The secondary slot is not being checked here, because
845 * `boot_slots_compatible` already provides assurance that the copy size
846 * will be compatible with the primary slot and scratch.
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200847 */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400848 if (new_sz > scratch_sz) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800849 break;
850 }
851 sz = new_sz;
852 }
853
854 /* i currently refers to a sector that doesn't fit or it is -1 because all
855 * sectors have been processed. In both cases, exclude sector i.
856 */
857 *out_first_sector_idx = i + 1;
858 return sz;
859}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300860#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800861
862/**
863 * Erases a region of flash.
864 *
Fabio Utzigba829042018-09-18 08:29:34 -0300865 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800866 * @param off The offset within the flash area to start the
867 * erase.
868 * @param sz The number of bytes to erase.
869 *
870 * @return 0 on success; nonzero on failure.
871 */
Fabio Utzigba829042018-09-18 08:29:34 -0300872static inline int
873boot_erase_sector(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800874{
Fabio Utzigba829042018-09-18 08:29:34 -0300875 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800876}
877
878/**
879 * Copies the contents of one flash region to another. You must erase the
880 * destination region prior to calling this function.
881 *
882 * @param flash_area_id_src The ID of the source flash area.
883 * @param flash_area_id_dst The ID of the destination flash area.
884 * @param off_src The offset within the source flash area to
885 * copy from.
886 * @param off_dst The offset within the destination flash area to
887 * copy to.
888 * @param sz The number of bytes to copy.
889 *
890 * @return 0 on success; nonzero on failure.
891 */
892static int
Fabio Utzigba829042018-09-18 08:29:34 -0300893boot_copy_sector(const struct flash_area *fap_src,
894 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800895 uint32_t off_src, uint32_t off_dst, uint32_t sz)
896{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800897 uint32_t bytes_copied;
898 int chunk_sz;
899 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -0300900#ifdef MCUBOOT_ENC_IMAGES
901 uint32_t off;
902 size_t blk_off;
903 struct image_header *hdr;
904 uint16_t idx;
905 uint32_t blk_sz;
906#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800907
908 static uint8_t buf[1024];
909
Christopher Collins92ea77f2016-12-12 15:59:26 -0800910 bytes_copied = 0;
911 while (bytes_copied < sz) {
912 if (sz - bytes_copied > sizeof buf) {
913 chunk_sz = sizeof buf;
914 } else {
915 chunk_sz = sz - bytes_copied;
916 }
917
918 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
919 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300920 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800921 }
922
Fabio Utzigba829042018-09-18 08:29:34 -0300923#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +0100924 if ((fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY) ||
925 (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY)) {
926 /* assume the secondary slot as src, needs decryption */
927 hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -0300928 off = off_src;
David Vincze2d736ad2019-02-18 11:50:22 +0100929 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY) {
930 /* might need encryption (metadata from the primary slot) */
931 hdr = boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -0300932 off = off_dst;
933 }
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200934 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300935 blk_sz = chunk_sz;
936 idx = 0;
937 if (off + bytes_copied < hdr->ih_hdr_size) {
938 /* do not decrypt header */
939 blk_off = 0;
940 blk_sz = chunk_sz - hdr->ih_hdr_size;
941 idx = hdr->ih_hdr_size;
942 } else {
943 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
944 }
945 if (off + bytes_copied + chunk_sz > hdr->ih_hdr_size + hdr->ih_img_size) {
946 /* do not decrypt TLVs */
947 if (off + bytes_copied >= hdr->ih_hdr_size + hdr->ih_img_size) {
948 blk_sz = 0;
949 } else {
950 blk_sz = (hdr->ih_hdr_size + hdr->ih_img_size) - (off + bytes_copied);
951 }
952 }
953 boot_encrypt(fap_src, (off + bytes_copied + idx) - hdr->ih_hdr_size,
954 blk_sz, blk_off, &buf[idx]);
955 }
956 }
957#endif
958
Christopher Collins92ea77f2016-12-12 15:59:26 -0800959 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
960 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300961 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800962 }
963
964 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -0300965
966 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800967 }
968
Fabio Utzigba829042018-09-18 08:29:34 -0300969 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800970}
971
David Brown6b1b3b92017-09-19 08:59:10 -0600972#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -0300973static inline int
Fabio Utzigba829042018-09-18 08:29:34 -0300974boot_status_init(const struct flash_area *fap, const struct boot_status *bs)
Fabio Utzig2473ac02017-05-02 12:45:02 -0300975{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300976 struct boot_swap_state swap_state;
977 int rc;
978
Christopher Collins2c88e692019-05-22 15:10:14 -0700979 BOOT_LOG_DBG("initializing status; fa_id=%d", fap->fa_id);
980
David Vincze2d736ad2019-02-18 11:50:22 +0100981 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY, &swap_state);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300982 assert(rc == 0);
983
Christopher Collinsa1c12042019-05-23 14:00:28 -0700984 if (bs->swap_type != BOOT_SWAP_TYPE_NONE) {
David Vinczee2453472019-06-17 12:31:59 +0200985 rc = boot_write_swap_info(fap,
986 bs->swap_type,
987 0);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700988 assert(rc == 0);
989 }
990
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400991 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300992 rc = boot_write_image_ok(fap);
993 assert(rc == 0);
994 }
995
Fabio Utzig46490722017-09-04 15:34:32 -0300996 rc = boot_write_swap_size(fap, bs->swap_size);
997 assert(rc == 0);
998
Fabio Utzigba829042018-09-18 08:29:34 -0300999#ifdef MCUBOOT_ENC_IMAGES
1000 rc = boot_write_enc_key(fap, 0, bs->enckey[0]);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001001 assert(rc == 0);
1002
Fabio Utzigba829042018-09-18 08:29:34 -03001003 rc = boot_write_enc_key(fap, 1, bs->enckey[1]);
1004 assert(rc == 0);
1005#endif
1006
1007 rc = boot_write_magic(fap);
1008 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001009
1010 return 0;
1011}
Christopher Collinsa1c12042019-05-23 14:00:28 -07001012
David Brown6b1b3b92017-09-19 08:59:10 -06001013#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001014
Fabio Utzig358c9352017-07-25 22:10:45 -03001015#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -03001016static int
Fabio Utziged0ca432019-01-23 14:50:11 -02001017boot_erase_trailer_sectors(const struct flash_area *fap)
Fabio Utzig2473ac02017-05-02 12:45:02 -03001018{
1019 uint8_t slot;
Fabio Utziged0ca432019-01-23 14:50:11 -02001020 uint32_t sector;
1021 uint32_t trailer_sz;
1022 uint32_t total_sz;
1023 uint32_t off;
1024 uint32_t sz;
David Vinczeb75c12a2019-03-22 14:58:33 +01001025 int fa_id_primary;
1026 int fa_id_secondary;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001027 int rc;
1028
Christopher Collins2c88e692019-05-22 15:10:14 -07001029 BOOT_LOG_DBG("erasing trailer; fa_id=%d", fap->fa_id);
1030
David Vinczeb75c12a2019-03-22 14:58:33 +01001031 fa_id_primary = flash_area_id_from_image_slot(BOOT_PRIMARY_SLOT);
1032 fa_id_secondary = flash_area_id_from_image_slot(BOOT_SECONDARY_SLOT);
1033
1034 if (fap->fa_id == fa_id_primary) {
David Vincze2d736ad2019-02-18 11:50:22 +01001035 slot = BOOT_PRIMARY_SLOT;
David Vinczeb75c12a2019-03-22 14:58:33 +01001036 } else if (fap->fa_id == fa_id_secondary) {
David Vincze2d736ad2019-02-18 11:50:22 +01001037 slot = BOOT_SECONDARY_SLOT;
David Vinczeb75c12a2019-03-22 14:58:33 +01001038 } else {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001039 return BOOT_EFLASH;
1040 }
1041
Fabio Utziged0ca432019-01-23 14:50:11 -02001042 /* delete starting from last sector and moving to beginning */
1043 sector = boot_img_num_sectors(&boot_data, slot) - 1;
Christopher Collins2adef702019-05-22 14:37:31 -07001044 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Fabio Utziged0ca432019-01-23 14:50:11 -02001045 total_sz = 0;
1046 do {
1047 sz = boot_img_sector_size(&boot_data, slot, sector);
1048 off = boot_img_sector_off(&boot_data, slot, sector);
1049 rc = boot_erase_sector(fap, off, sz);
1050 assert(rc == 0);
1051
1052 sector--;
1053 total_sz += sz;
1054 } while (total_sz < trailer_sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001055
1056 return rc;
1057}
Fabio Utzig358c9352017-07-25 22:10:45 -03001058#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001059
Christopher Collins92ea77f2016-12-12 15:59:26 -08001060/**
1061 * Swaps the contents of two flash regions within the two image slots.
1062 *
1063 * @param idx The index of the first sector in the range of
1064 * sectors being swapped.
1065 * @param sz The number of bytes to swap.
1066 * @param bs The current boot status. This struct gets
1067 * updated according to the outcome.
1068 *
1069 * @return 0 on success; nonzero on failure.
1070 */
Fabio Utzig3488eef2017-06-12 10:25:43 -03001071#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -08001072static void
Christopher Collins92ea77f2016-12-12 15:59:26 -08001073boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
1074{
David Vincze2d736ad2019-02-18 11:50:22 +01001075 const struct flash_area *fap_primary_slot;
1076 const struct flash_area *fap_secondary_slot;
Fabio Utzigba829042018-09-18 08:29:34 -03001077 const struct flash_area *fap_scratch;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001078 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001079 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001080 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001081 uint32_t scratch_trailer_off;
1082 struct boot_swap_state swap_state;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001083 size_t last_sector;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001084 bool erase_scratch;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001085 int rc;
1086
1087 /* Calculate offset from start of image area. */
David Vincze2d736ad2019-02-18 11:50:22 +01001088 img_off = boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT, idx);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001089
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001090 copy_sz = sz;
Christopher Collins2adef702019-05-22 14:37:31 -07001091 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Fabio Utzig9678c972017-05-23 11:28:56 -04001092
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001093 /* sz in this function is always sized on a multiple of the sector size.
1094 * The check against the start offset of the last sector
Fabio Utzig9678c972017-05-23 11:28:56 -04001095 * is to determine if we're swapping the last sector. The last sector
1096 * needs special handling because it's where the trailer lives. If we're
1097 * copying it, we need to use scratch to write the trailer temporarily.
1098 *
1099 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
1100 * controls if special handling is needed (swapping last sector).
1101 */
David Vincze2d736ad2019-02-18 11:50:22 +01001102 last_sector = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT) - 1;
1103 if (img_off + sz > boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT,
1104 last_sector)) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001105 copy_sz -= trailer_sz;
1106 }
1107
Fabio Utzig39000012018-07-30 12:40:20 -03001108 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001109
David Vincze2d736ad2019-02-18 11:50:22 +01001110 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001111 assert (rc == 0);
1112
David Vincze2d736ad2019-02-18 11:50:22 +01001113 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001114 assert (rc == 0);
1115
1116 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch);
1117 assert (rc == 0);
1118
Fabio Utzig39000012018-07-30 12:40:20 -03001119 if (bs->state == BOOT_STATUS_STATE_0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001120 BOOT_LOG_DBG("erasing scratch area");
Christopher Collinsa1c12042019-05-23 14:00:28 -07001121 rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size);
Christopher Collins4772ac42017-02-27 20:08:01 -08001122 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001123
Fabio Utzig39000012018-07-30 12:40:20 -03001124 if (bs->idx == BOOT_STATUS_IDX_0) {
Christopher Collinsa1c12042019-05-23 14:00:28 -07001125 /* Write a trailer to the scratch area, even if we don't need the
1126 * scratch area for status. We need a temporary place to store the
1127 * `swap-type` while we erase the primary trailer.
1128 */
1129 rc = boot_status_init(fap_scratch, bs);
1130 assert(rc == 0);
1131
1132 if (!bs->use_scratch) {
1133 /* Prepare the primary status area... here it is known that the
1134 * last sector is not being used by the image data so it's safe
1135 * to erase.
Fabio Utzig2473ac02017-05-02 12:45:02 -03001136 */
David Vincze2d736ad2019-02-18 11:50:22 +01001137 rc = boot_erase_trailer_sectors(fap_primary_slot);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001138 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001139
Christopher Collinsa1c12042019-05-23 14:00:28 -07001140 rc = boot_status_init(fap_primary_slot, bs);
1141 assert(rc == 0);
1142
1143 /* Erase the temporary trailer from the scratch area. */
1144 rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size);
1145 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001146 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001147 }
1148
Christopher Collinsa1c12042019-05-23 14:00:28 -07001149 rc = boot_copy_sector(fap_secondary_slot, fap_scratch,
1150 img_off, 0, copy_sz);
1151 assert(rc == 0);
1152
Fabio Utzig39000012018-07-30 12:40:20 -03001153 bs->state = BOOT_STATUS_STATE_1;
Christopher Collins4772ac42017-02-27 20:08:01 -08001154 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001155 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001156 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001157
Fabio Utzig39000012018-07-30 12:40:20 -03001158 if (bs->state == BOOT_STATUS_STATE_1) {
David Vincze2d736ad2019-02-18 11:50:22 +01001159 rc = boot_erase_sector(fap_secondary_slot, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001160 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001161
David Vincze2d736ad2019-02-18 11:50:22 +01001162 rc = boot_copy_sector(fap_primary_slot, fap_secondary_slot,
1163 img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001164 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001165
Fabio Utzig39000012018-07-30 12:40:20 -03001166 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001167 /* If not all sectors of the slot are being swapped,
David Vincze2d736ad2019-02-18 11:50:22 +01001168 * guarantee here that only the primary slot will have the state.
Fabio Utzig2473ac02017-05-02 12:45:02 -03001169 */
David Vincze2d736ad2019-02-18 11:50:22 +01001170 rc = boot_erase_trailer_sectors(fap_secondary_slot);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001171 assert(rc == 0);
1172 }
1173
Fabio Utzig39000012018-07-30 12:40:20 -03001174 bs->state = BOOT_STATUS_STATE_2;
Christopher Collins4772ac42017-02-27 20:08:01 -08001175 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001176 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001177 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001178
Fabio Utzig39000012018-07-30 12:40:20 -03001179 if (bs->state == BOOT_STATUS_STATE_2) {
David Vincze2d736ad2019-02-18 11:50:22 +01001180 rc = boot_erase_sector(fap_primary_slot, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001181 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001182
Christopher Collinsa1c12042019-05-23 14:00:28 -07001183 /* NOTE: If this is the final sector, we exclude the image trailer from
1184 * this copy (copy_sz was truncated earlier).
1185 */
David Vincze2d736ad2019-02-18 11:50:22 +01001186 rc = boot_copy_sector(fap_scratch, fap_primary_slot,
1187 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001188 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001189
Fabio Utzig94d998c2017-05-22 11:02:41 -04001190 if (bs->use_scratch) {
Fabio Utzigba829042018-09-18 08:29:34 -03001191 scratch_trailer_off = boot_status_off(fap_scratch);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001192
1193 /* copy current status that is being maintained in scratch */
David Vincze2d736ad2019-02-18 11:50:22 +01001194 rc = boot_copy_sector(fap_scratch, fap_primary_slot,
1195 scratch_trailer_off, img_off + copy_sz,
Marti Bolivare10a7392017-06-14 16:20:07 -04001196 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data));
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001197 BOOT_STATUS_ASSERT(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001198
Fabio Utzig2473ac02017-05-02 12:45:02 -03001199 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1200 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001201 assert(rc == 0);
1202
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001203 if (swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze2d736ad2019-02-18 11:50:22 +01001204 rc = boot_write_image_ok(fap_primary_slot);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001205 assert(rc == 0);
1206 }
1207
Christopher Collinsa1c12042019-05-23 14:00:28 -07001208 if (swap_state.swap_type != BOOT_SWAP_TYPE_NONE) {
David Vinczee2453472019-06-17 12:31:59 +02001209 rc = boot_write_swap_info(fap_primary_slot,
1210 swap_state.swap_type,
1211 0);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001212 assert(rc == 0);
1213 }
1214
David Vincze2d736ad2019-02-18 11:50:22 +01001215 rc = boot_write_swap_size(fap_primary_slot, bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001216 assert(rc == 0);
1217
Fabio Utzigba829042018-09-18 08:29:34 -03001218#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +01001219 rc = boot_write_enc_key(fap_primary_slot, 0, bs->enckey[0]);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001220 assert(rc == 0);
1221
David Vincze2d736ad2019-02-18 11:50:22 +01001222 rc = boot_write_enc_key(fap_primary_slot, 1, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001223 assert(rc == 0);
1224#endif
David Vincze2d736ad2019-02-18 11:50:22 +01001225 rc = boot_write_magic(fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001226 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001227 }
1228
Christopher Collinsa1c12042019-05-23 14:00:28 -07001229 /* If we wrote a trailer to the scratch area, erase it after we persist
1230 * a trailer to the primary slot. We do this to prevent mcuboot from
1231 * reading a stale status from the scratch area in case of immediate
1232 * reset.
1233 */
1234 erase_scratch = bs->use_scratch;
1235 bs->use_scratch = 0;
1236
Christopher Collins92ea77f2016-12-12 15:59:26 -08001237 bs->idx++;
Fabio Utzig39000012018-07-30 12:40:20 -03001238 bs->state = BOOT_STATUS_STATE_0;
Christopher Collins4772ac42017-02-27 20:08:01 -08001239 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001240 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001241
1242 if (erase_scratch) {
1243 rc = boot_erase_sector(fap_scratch, 0, sz);
1244 assert(rc == 0);
1245 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001246 }
Fabio Utzigba829042018-09-18 08:29:34 -03001247
David Vincze2d736ad2019-02-18 11:50:22 +01001248 flash_area_close(fap_primary_slot);
1249 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001250 flash_area_close(fap_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001251}
Fabio Utzig3488eef2017-06-12 10:25:43 -03001252#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001253
1254/**
David Vincze2d736ad2019-02-18 11:50:22 +01001255 * Overwrite primary slot with the image contained in the secondary slot.
1256 * If a prior copy operation was interrupted by a system reset, this function
1257 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001258 *
1259 * @param bs The current boot status. This function reads
1260 * this struct to determine if it is resuming
1261 * an interrupted swap operation. This
1262 * function writes the updated status to this
1263 * function on return.
1264 *
1265 * @return 0 on success; nonzero on failure.
1266 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001267#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001268static int
1269boot_copy_image(struct boot_status *bs)
1270{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001271 size_t sect_count;
1272 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001273 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001274 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001275 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001276 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +01001277 const struct flash_area *fap_primary_slot;
1278 const struct flash_area *fap_secondary_slot;
1279
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001280 (void)bs;
1281
Fabio Utzig13d9e352017-10-05 20:32:31 -03001282#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1283 uint32_t src_size = 0;
David Vincze2d736ad2019-02-18 11:50:22 +01001284 rc = boot_read_image_size(BOOT_SECONDARY_SLOT,
1285 boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT),
1286 &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001287 assert(rc == 0);
1288#endif
David Brown17609d82017-05-05 09:41:34 -06001289
David Vincze2d736ad2019-02-18 11:50:22 +01001290 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1291 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -06001292
David Vincze2d736ad2019-02-18 11:50:22 +01001293 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001294 assert (rc == 0);
1295
David Vincze2d736ad2019-02-18 11:50:22 +01001296 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001297 assert (rc == 0);
1298
David Vincze2d736ad2019-02-18 11:50:22 +01001299 sect_count = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001300 for (sect = 0, size = 0; sect < sect_count; sect++) {
David Vincze2d736ad2019-02-18 11:50:22 +01001301 this_size = boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, sect);
1302 rc = boot_erase_sector(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001303 assert(rc == 0);
1304
1305 size += this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001306
1307#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1308 if (size >= src_size) {
1309 break;
1310 }
1311#endif
David Brown17609d82017-05-05 09:41:34 -06001312 }
1313
Fabio Utzigba829042018-09-18 08:29:34 -03001314#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +01001315 if (IS_ENCRYPTED(boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT))) {
1316 rc = boot_enc_load(boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT),
1317 fap_secondary_slot,
1318 bs->enckey[1]);
1319
Fabio Utzigba829042018-09-18 08:29:34 -03001320 if (rc < 0) {
1321 return BOOT_EBADIMAGE;
1322 }
Fabio Utzige641ea52018-12-03 10:37:53 -02001323 if (rc == 0 && boot_enc_set_key(1, bs->enckey[1])) {
Fabio Utzigba829042018-09-18 08:29:34 -03001324 return BOOT_EBADIMAGE;
1325 }
1326 }
1327#endif
1328
David Vincze2d736ad2019-02-18 11:50:22 +01001329 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1330 size);
1331 rc = boot_copy_sector(fap_secondary_slot, fap_primary_slot, 0, 0, size);
David Brown17609d82017-05-05 09:41:34 -06001332
Fabio Utzig13d9e352017-10-05 20:32:31 -03001333 /*
1334 * Erases header and trailer. The trailer is erased because when a new
1335 * image is written without a trailer as is the case when using newt, the
1336 * trailer that was left might trigger a new upgrade.
1337 */
Christopher Collins2c88e692019-05-22 15:10:14 -07001338 BOOT_LOG_DBG("erasing secondary header");
David Vincze2d736ad2019-02-18 11:50:22 +01001339 rc = boot_erase_sector(fap_secondary_slot,
1340 boot_img_sector_off(&boot_data,
1341 BOOT_SECONDARY_SLOT, 0),
1342 boot_img_sector_size(&boot_data,
1343 BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001344 assert(rc == 0);
David Vincze2d736ad2019-02-18 11:50:22 +01001345 last_sector = boot_img_num_sectors(&boot_data, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -07001346 BOOT_LOG_DBG("erasing secondary trailer");
David Vincze2d736ad2019-02-18 11:50:22 +01001347 rc = boot_erase_sector(fap_secondary_slot,
1348 boot_img_sector_off(&boot_data,
1349 BOOT_SECONDARY_SLOT, last_sector),
1350 boot_img_sector_size(&boot_data,
1351 BOOT_SECONDARY_SLOT, last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001352 assert(rc == 0);
1353
David Vincze2d736ad2019-02-18 11:50:22 +01001354 flash_area_close(fap_primary_slot);
1355 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001356
David Vincze2d736ad2019-02-18 11:50:22 +01001357 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001358
1359 return 0;
1360}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001361#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001362
Christopher Collinsa1c12042019-05-23 14:00:28 -07001363#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -03001364/**
1365 * Swaps the two images in flash. If a prior copy operation was interrupted
1366 * by a system reset, this function completes that operation.
1367 *
1368 * @param bs The current boot status. This function reads
1369 * this struct to determine if it is resuming
1370 * an interrupted swap operation. This
1371 * function writes the updated status to this
1372 * function on return.
1373 *
1374 * @return 0 on success; nonzero on failure.
1375 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001376static int
Fabio Utzig338a19f2018-12-03 08:37:08 -02001377boot_swap_image(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001378{
1379 uint32_t sz;
1380 int first_sector_idx;
1381 int last_sector_idx;
David Vincze2d736ad2019-02-18 11:50:22 +01001382 int last_idx_secondary_slot;
Fabio Utzigcd5774b2017-11-29 10:18:26 -02001383 uint32_t swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001384 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001385#ifdef MCUBOOT_ENC_IMAGES
1386 const struct flash_area *fap;
1387 uint8_t slot;
1388 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001389#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001390 uint32_t size;
1391 uint32_t copy_size;
David Vincze2d736ad2019-02-18 11:50:22 +01001392 uint32_t primary_slot_size;
1393 uint32_t secondary_slot_size;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001394 int rc;
1395
1396 /* FIXME: just do this if asked by user? */
1397
1398 size = copy_size = 0;
1399
Fabio Utzig39000012018-07-30 12:40:20 -03001400 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Fabio Utzig46490722017-09-04 15:34:32 -03001401 /*
1402 * No swap ever happened, so need to find the largest image which
1403 * will be used to determine the amount of sectors to swap.
1404 */
David Vincze2d736ad2019-02-18 11:50:22 +01001405 hdr = boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001406 if (hdr->ih_magic == IMAGE_MAGIC) {
David Vincze2d736ad2019-02-18 11:50:22 +01001407 rc = boot_read_image_size(BOOT_PRIMARY_SLOT, hdr, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001408 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001409 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001410
Fabio Utzigba829042018-09-18 08:29:34 -03001411#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001412 if (IS_ENCRYPTED(hdr)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001413 fap = BOOT_IMG_AREA(&boot_data, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001414 rc = boot_enc_load(hdr, fap, bs->enckey[0]);
1415 assert(rc >= 0);
1416
1417 if (rc == 0) {
1418 rc = boot_enc_set_key(0, bs->enckey[0]);
1419 assert(rc == 0);
1420 } else {
1421 rc = 0;
1422 }
1423 } else {
1424 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1425 }
1426#endif
1427
David Vincze2d736ad2019-02-18 11:50:22 +01001428 hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001429 if (hdr->ih_magic == IMAGE_MAGIC) {
David Vincze2d736ad2019-02-18 11:50:22 +01001430 rc = boot_read_image_size(BOOT_SECONDARY_SLOT, hdr, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001431 assert(rc == 0);
1432 }
1433
Fabio Utzigba829042018-09-18 08:29:34 -03001434#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +01001435 hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001436 if (IS_ENCRYPTED(hdr)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001437 fap = BOOT_IMG_AREA(&boot_data, BOOT_SECONDARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001438 rc = boot_enc_load(hdr, fap, bs->enckey[1]);
1439 assert(rc >= 0);
1440
1441 if (rc == 0) {
1442 rc = boot_enc_set_key(1, bs->enckey[1]);
1443 assert(rc == 0);
1444 } else {
1445 rc = 0;
1446 }
1447 } else {
1448 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1449 }
1450#endif
1451
Fabio Utzig46490722017-09-04 15:34:32 -03001452 if (size > copy_size) {
1453 copy_size = size;
1454 }
1455
1456 bs->swap_size = copy_size;
1457 } else {
1458 /*
1459 * If a swap was under way, the swap_size should already be present
1460 * in the trailer...
1461 */
1462 rc = boot_read_swap_size(&bs->swap_size);
1463 assert(rc == 0);
1464
1465 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001466
1467#ifdef MCUBOOT_ENC_IMAGES
1468 for (slot = 0; slot <= 1; slot++) {
1469 rc = boot_read_enc_key(slot, bs->enckey[slot]);
1470 assert(rc == 0);
1471
1472 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001473 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001474 break;
1475 }
1476 }
1477
1478 if (i != BOOT_ENC_KEY_SIZE) {
1479 boot_enc_set_key(slot, bs->enckey[slot]);
1480 }
1481 }
1482#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001483 }
1484
David Vincze2d736ad2019-02-18 11:50:22 +01001485 primary_slot_size = 0;
1486 secondary_slot_size = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001487 last_sector_idx = 0;
David Vincze2d736ad2019-02-18 11:50:22 +01001488 last_idx_secondary_slot = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001489
1490 /*
1491 * Knowing the size of the largest image between both slots, here we
David Vincze2d736ad2019-02-18 11:50:22 +01001492 * find what is the last sector in the primary slot that needs swapping.
1493 * Since we already know that both slots are compatible, the secondary
1494 * slot's last sector is not really required after this check is finished.
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001495 */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001496 while (1) {
David Vincze2d736ad2019-02-18 11:50:22 +01001497 if ((primary_slot_size < copy_size) ||
1498 (primary_slot_size < secondary_slot_size)) {
1499 primary_slot_size += boot_img_sector_size(&boot_data,
1500 BOOT_PRIMARY_SLOT,
1501 last_sector_idx);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001502 }
David Vincze2d736ad2019-02-18 11:50:22 +01001503 if ((secondary_slot_size < copy_size) ||
1504 (secondary_slot_size < primary_slot_size)) {
1505 secondary_slot_size += boot_img_sector_size(&boot_data,
1506 BOOT_SECONDARY_SLOT,
1507 last_idx_secondary_slot);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001508 }
David Vincze2d736ad2019-02-18 11:50:22 +01001509 if (primary_slot_size >= copy_size &&
1510 secondary_slot_size >= copy_size &&
1511 primary_slot_size == secondary_slot_size) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001512 break;
1513 }
1514 last_sector_idx++;
David Vincze2d736ad2019-02-18 11:50:22 +01001515 last_idx_secondary_slot++;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001516 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001517
1518 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001519 while (last_sector_idx >= 0) {
1520 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
Fabio Utzig39000012018-07-30 12:40:20 -03001521 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001522 boot_swap_sectors(first_sector_idx, sz, bs);
1523 }
1524
1525 last_sector_idx = first_sector_idx - 1;
1526 swap_idx++;
1527 }
1528
David Vincze2d736ad2019-02-18 11:50:22 +01001529#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001530 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001531 BOOT_LOG_WRN("%d status write fails performing the swap",
1532 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001533 }
1534#endif
1535
Christopher Collins92ea77f2016-12-12 15:59:26 -08001536 return 0;
1537}
David Brown17609d82017-05-05 09:41:34 -06001538#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001539
1540/**
David Vincze2d736ad2019-02-18 11:50:22 +01001541 * Marks the image in the primary slot as fully copied.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001542 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001543#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001544static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001545boot_set_copy_done(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001546{
1547 const struct flash_area *fap;
1548 int rc;
1549
David Vincze2d736ad2019-02-18 11:50:22 +01001550 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001551 if (rc != 0) {
1552 return BOOT_EFLASH;
1553 }
1554
1555 rc = boot_write_copy_done(fap);
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001556 flash_area_close(fap);
1557 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001558}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001559#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001560
1561/**
David Vincze2d736ad2019-02-18 11:50:22 +01001562 * Marks a reverted image in the primary slot as confirmed. This is necessary to
1563 * ensure the status bytes from the image revert operation don't get processed
1564 * on a subsequent boot.
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001565 *
1566 * NOTE: image_ok is tested before writing because if there's a valid permanent
David Vincze2d736ad2019-02-18 11:50:22 +01001567 * image installed on the primary slot and the new image to be upgrade to has a
1568 * bad sig, image_ok would be overwritten.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001569 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001570#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001571static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001572boot_set_image_ok(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001573{
1574 const struct flash_area *fap;
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001575 struct boot_swap_state state;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001576 int rc;
1577
David Vincze2d736ad2019-02-18 11:50:22 +01001578 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001579 if (rc != 0) {
1580 return BOOT_EFLASH;
1581 }
1582
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001583 rc = boot_read_swap_state(fap, &state);
1584 if (rc != 0) {
1585 rc = BOOT_EFLASH;
1586 goto out;
1587 }
1588
1589 if (state.image_ok == BOOT_FLAG_UNSET) {
1590 rc = boot_write_image_ok(fap);
1591 }
1592
1593out:
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001594 flash_area_close(fap);
1595 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001596}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001597#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001598
1599/**
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001600 * Performs an image swap if one is required.
1601 *
1602 * @param out_swap_type On success, the type of swap performed gets
1603 * written here.
1604 *
1605 * @return 0 on success; nonzero on failure.
1606 */
1607static int
1608boot_swap_if_needed(int *out_swap_type)
1609{
1610 struct boot_status bs;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001611 int rc;
1612
Christopher Collinsa1c12042019-05-23 14:00:28 -07001613 /* Determine if we rebooted in the middle of an image swap operation. */
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001614 rc = boot_read_status(&bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001615 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001616 if (rc != 0) {
1617 return rc;
1618 }
1619
1620 /* If a partial swap was detected, complete it. */
Fabio Utzig39000012018-07-30 12:40:20 -03001621 if (bs.idx != BOOT_STATUS_IDX_0 || bs.state != BOOT_STATUS_STATE_0) {
Fabio Utziga32f1af2019-01-07 06:50:58 -02001622#ifdef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig338a19f2018-12-03 08:37:08 -02001623 /* Should never arrive here, overwrite-only mode has no swap state. */
1624 assert(0);
1625#else
Christopher Collinsa1c12042019-05-23 14:00:28 -07001626 /* Determine the type of swap operation being resumed from the
1627 * `swap-type` trailer field.
1628 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001629 rc = boot_swap_image(&bs);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001630 assert(rc == 0);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001631#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001632
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001633 } else {
Christopher Collinsa1c12042019-05-23 14:00:28 -07001634 if (bs.swap_type == BOOT_SWAP_TYPE_NONE) {
1635 bs.swap_type = boot_validated_swap_type(&bs);
1636 } else if (boot_validate_slot(BOOT_SECONDARY_SLOT, &bs) != 0) {
1637 bs.swap_type = BOOT_SWAP_TYPE_FAIL;
1638 }
1639 switch (bs.swap_type) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001640 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001641 case BOOT_SWAP_TYPE_PERM:
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001642 case BOOT_SWAP_TYPE_REVERT:
Fabio Utziga32f1af2019-01-07 06:50:58 -02001643#ifdef MCUBOOT_OVERWRITE_ONLY
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001644 rc = boot_copy_image(&bs);
Fabio Utzig338a19f2018-12-03 08:37:08 -02001645#else
1646 rc = boot_swap_image(&bs);
1647#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001648 assert(rc == 0);
1649 break;
Fabio Utzig338a19f2018-12-03 08:37:08 -02001650#ifdef MCUBOOT_BOOTSTRAP
1651 case BOOT_SWAP_TYPE_NONE:
1652 /*
1653 * Header checks are done first because they are inexpensive.
1654 * Since overwrite-only copies starting from offset 0, if
1655 * interrupted, it might leave a valid header magic, so also
David Vincze2d736ad2019-02-18 11:50:22 +01001656 * run validation on the primary slot to be sure it's not OK.
Fabio Utzig338a19f2018-12-03 08:37:08 -02001657 */
David Vincze2d736ad2019-02-18 11:50:22 +01001658 if (boot_check_header_erased(BOOT_PRIMARY_SLOT) == 0 ||
1659 boot_validate_slot(BOOT_PRIMARY_SLOT, &bs) != 0) {
1660 if ((boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT)->ih_magic
1661 == IMAGE_MAGIC ) &&
1662 (boot_validate_slot(BOOT_SECONDARY_SLOT, &bs) == 0)) {
Fabio Utzig338a19f2018-12-03 08:37:08 -02001663 rc = boot_copy_image(&bs);
1664 assert(rc == 0);
1665
1666 /* Returns fail here to trigger a re-read of the headers. */
Christopher Collinsa1c12042019-05-23 14:00:28 -07001667 bs.swap_type = BOOT_SWAP_TYPE_FAIL;
Fabio Utzig338a19f2018-12-03 08:37:08 -02001668 }
1669 }
1670 break;
1671#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001672 }
1673 }
1674
Christopher Collinsa1c12042019-05-23 14:00:28 -07001675 *out_swap_type = bs.swap_type;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001676 return 0;
1677}
1678
1679/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08001680 * Prepares the booting process. This function moves images around in flash as
1681 * appropriate, and tells you what address to boot from.
1682 *
1683 * @param rsp On success, indicates how booting should occur.
1684 *
1685 * @return 0 on success; nonzero on failure.
1686 */
1687int
1688boot_go(struct boot_rsp *rsp)
1689{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001690 int swap_type;
Marti Bolivar84898652017-06-13 17:20:22 -04001691 size_t slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001692 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001693 int fa_id;
David Brown52eee562017-07-05 11:25:09 -06001694 bool reload_headers = false;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001695
1696 /* The array of slot sectors are defined here (as opposed to file scope) so
1697 * that they don't get allocated for non-boot-loader apps. This is
1698 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001699 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001700 */
David Vincze2d736ad2019-02-18 11:50:22 +01001701 static boot_sector_t primary_slot_sectors[BOOT_MAX_IMG_SECTORS];
1702 static boot_sector_t secondary_slot_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001703 static boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
David Vincze2d736ad2019-02-18 11:50:22 +01001704 boot_data.imgs[BOOT_PRIMARY_SLOT].sectors = primary_slot_sectors;
1705 boot_data.imgs[BOOT_SECONDARY_SLOT].sectors = secondary_slot_sectors;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001706 boot_data.scratch.sectors = scratch_sectors;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001707
Fabio Utzigba829042018-09-18 08:29:34 -03001708#ifdef MCUBOOT_ENC_IMAGES
1709 /* FIXME: remove this after RAM is cleared by sim */
1710 boot_enc_zeroize();
1711#endif
1712
Marti Bolivarc0b47912017-06-13 17:18:09 -04001713 /* Open boot_data image areas for the duration of this call. */
1714 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1715 fa_id = flash_area_id_from_image_slot(slot);
1716 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot));
1717 assert(rc == 0);
1718 }
1719 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
1720 &BOOT_SCRATCH_AREA(&boot_data));
1721 assert(rc == 0);
1722
Christopher Collins92ea77f2016-12-12 15:59:26 -08001723 /* Determine the sector layout of the image slots and scratch area. */
1724 rc = boot_read_sectors();
1725 if (rc != 0) {
Fabio Utziga1fae672018-03-30 10:52:38 -03001726 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - too small?",
1727 BOOT_MAX_IMG_SECTORS);
Marti Bolivarc0b47912017-06-13 17:18:09 -04001728 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001729 }
1730
1731 /* Attempt to read an image header from each slot. */
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001732 rc = boot_read_image_headers(false);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001733 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001734 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001735 }
1736
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001737 /* If the image slots aren't compatible, no swap is possible. Just boot
David Vincze2d736ad2019-02-18 11:50:22 +01001738 * into the primary slot.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001739 */
1740 if (boot_slots_compatible()) {
1741 rc = boot_swap_if_needed(&swap_type);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001742 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001743 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001744 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001745 }
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001746
1747 /*
1748 * The following states need image_ok be explicitly set after the
1749 * swap was finished to avoid a new revert.
1750 */
David Vincze2d736ad2019-02-18 11:50:22 +01001751 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
Christopher Collinsa1c12042019-05-23 14:00:28 -07001752 swap_type == BOOT_SWAP_TYPE_FAIL ||
1753 swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001754#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig695d5642017-07-20 09:47:16 -03001755 rc = boot_set_image_ok();
1756 if (rc != 0) {
1757 swap_type = BOOT_SWAP_TYPE_PANIC;
1758 }
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001759#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001760 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001761 } else {
1762 swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001763 }
1764
1765 switch (swap_type) {
1766 case BOOT_SWAP_TYPE_NONE:
David Vincze2d736ad2019-02-18 11:50:22 +01001767 slot = BOOT_PRIMARY_SLOT;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001768 break;
1769
Fabio Utzig695d5642017-07-20 09:47:16 -03001770 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1771 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001772 case BOOT_SWAP_TYPE_REVERT:
David Vincze2d736ad2019-02-18 11:50:22 +01001773 slot = BOOT_SECONDARY_SLOT;
David Brown52eee562017-07-05 11:25:09 -06001774 reload_headers = true;
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001775#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig695d5642017-07-20 09:47:16 -03001776 rc = boot_set_copy_done();
1777 if (rc != 0) {
1778 swap_type = BOOT_SWAP_TYPE_PANIC;
1779 }
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001780#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001781 break;
1782
1783 case BOOT_SWAP_TYPE_FAIL:
David Vincze2d736ad2019-02-18 11:50:22 +01001784 /* The image in the secondary slot was invalid and is now erased.
1785 * Ensure we don't try to boot into it again on the next reboot.
1786 * Do this by pretending we just reverted back to the primary slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001787 */
David Vincze2d736ad2019-02-18 11:50:22 +01001788 slot = BOOT_PRIMARY_SLOT;
David Brown52eee562017-07-05 11:25:09 -06001789 reload_headers = true;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001790 break;
1791
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001792 default:
Fabio Utzig695d5642017-07-20 09:47:16 -03001793 swap_type = BOOT_SWAP_TYPE_PANIC;
1794 }
1795
1796 if (swap_type == BOOT_SWAP_TYPE_PANIC) {
1797 BOOT_LOG_ERR("panic!");
Fabio Utzigb5b2f552017-06-30 10:03:47 -03001798 assert(0);
Fabio Utzig695d5642017-07-20 09:47:16 -03001799
1800 /* Loop forever... */
1801 while (1) {}
Christopher Collins92ea77f2016-12-12 15:59:26 -08001802 }
1803
David Brown52eee562017-07-05 11:25:09 -06001804 if (reload_headers) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001805 rc = boot_read_image_headers(false);
Fabio Utzigc6a7b0c2017-09-13 19:01:15 -03001806 if (rc != 0) {
1807 goto out;
1808 }
1809 /* Since headers were reloaded, it can be assumed we just performed a
1810 * swap or overwrite. Now the header info that should be used to
David Vincze2d736ad2019-02-18 11:50:22 +01001811 * provide the data for the bootstrap, which previously was at the
1812 * secondary slot, was updated to the primary slot.
Fabio Utzigc6a7b0c2017-09-13 19:01:15 -03001813 */
David Vincze2d736ad2019-02-18 11:50:22 +01001814 slot = BOOT_PRIMARY_SLOT;
David Brown52eee562017-07-05 11:25:09 -06001815 }
1816
David Vincze2d736ad2019-02-18 11:50:22 +01001817#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
1818 rc = boot_validate_slot(BOOT_PRIMARY_SLOT, NULL);
David Brown554c52e2017-06-30 16:01:07 -06001819 if (rc != 0) {
1820 rc = BOOT_EBADIMAGE;
1821 goto out;
1822 }
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001823#else
David Vincze2d736ad2019-02-18 11:50:22 +01001824 /* Even if we're not re-validating the primary slot, we could be booting
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001825 * onto an empty flash chip. At least do a basic sanity check that
1826 * the magic number on the image is OK.
1827 */
David Vincze2d736ad2019-02-18 11:50:22 +01001828 if (boot_data.imgs[BOOT_PRIMARY_SLOT].hdr.ih_magic != IMAGE_MAGIC) {
1829 BOOT_LOG_ERR("bad image magic 0x%lx",
1830 (unsigned long)boot_data.imgs[BOOT_PRIMARY_SLOT].hdr.ih_magic);
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001831 rc = BOOT_EBADIMAGE;
1832 goto out;
1833 }
David Brown554c52e2017-06-30 16:01:07 -06001834#endif
1835
Christopher Collins92ea77f2016-12-12 15:59:26 -08001836 /* Always boot from the primary slot. */
David Vincze2d736ad2019-02-18 11:50:22 +01001837 rsp->br_flash_dev_id = boot_data.imgs[BOOT_PRIMARY_SLOT].area->fa_device_id;
1838 rsp->br_image_off = boot_img_slot_off(&boot_data, BOOT_PRIMARY_SLOT);
Marti Bolivarf804f622017-06-12 15:41:48 -04001839 rsp->br_hdr = boot_img_hdr(&boot_data, slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001840
Marti Bolivarc0b47912017-06-13 17:18:09 -04001841 out:
1842 flash_area_close(BOOT_SCRATCH_AREA(&boot_data));
1843 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1844 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
1845 }
1846 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001847}
1848
1849int
1850split_go(int loader_slot, int split_slot, void **entry)
1851{
Marti Bolivarc50926f2017-06-14 09:35:40 -04001852 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001853 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001854 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001855 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001856 int rc;
1857
Christopher Collins92ea77f2016-12-12 15:59:26 -08001858 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1859 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001860 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001861 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04001862 boot_data.imgs[loader_slot].sectors = sectors + 0;
1863 boot_data.imgs[split_slot].sectors = sectors + BOOT_MAX_IMG_SECTORS;
1864
1865 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1866 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07001867 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04001868 assert(rc == 0);
1869 split_flash_id = flash_area_id_from_image_slot(split_slot);
1870 rc = flash_area_open(split_flash_id,
1871 &BOOT_IMG_AREA(&boot_data, split_slot));
1872 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001873
1874 /* Determine the sector layout of the image slots and scratch area. */
1875 rc = boot_read_sectors();
1876 if (rc != 0) {
1877 rc = SPLIT_GO_ERR;
1878 goto done;
1879 }
1880
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001881 rc = boot_read_image_headers(true);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001882 if (rc != 0) {
1883 goto done;
1884 }
1885
Christopher Collins92ea77f2016-12-12 15:59:26 -08001886 /* Don't check the bootable image flag because we could really call a
1887 * bootable or non-bootable image. Just validate that the image check
1888 * passes which is distinct from the normal check.
1889 */
Marti Bolivarf804f622017-06-12 15:41:48 -04001890 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001891 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04001892 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001893 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001894 if (rc != 0) {
1895 rc = SPLIT_GO_NON_MATCHING;
1896 goto done;
1897 }
1898
Marti Bolivarea088872017-06-12 17:10:49 -04001899 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04001900 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001901 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001902 rc = SPLIT_GO_OK;
1903
1904done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04001905 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
1906 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001907 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001908 return rc;
1909}