blob: 4c94fa7395a73f8dbea73faecfc4ed35235437f2 [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;
David Vinczeba3bd602019-06-17 16:01:43 +0200193
194#if (BOOT_IMAGE_NUMBER > 1)
195 /* In case of multi-image boot it can happen that if boot status
196 * info is found on scratch area then it does not belong to the
197 * currently examined image.
198 */
199 if (source == BOOT_STATUS_SOURCE_SCRATCH &&
200 state_scratch.image_num != current_image) {
201 source = BOOT_STATUS_SOURCE_NONE;
202 }
203#endif
204
Marti Bolivarfd20c762017-02-07 16:52:50 -0500205 BOOT_LOG_INF("Boot source: %s",
206 source == BOOT_STATUS_SOURCE_NONE ? "none" :
207 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
David Vincze2d736ad2019-02-18 11:50:22 +0100208 source == BOOT_STATUS_SOURCE_PRIMARY_SLOT ?
209 "primary slot" : "BUG; can't happen");
Marti Bolivarfd20c762017-02-07 16:52:50 -0500210 return source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800211 }
212 }
213
Marti Bolivarfd20c762017-02-07 16:52:50 -0500214 BOOT_LOG_INF("Boot source: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800215 return BOOT_STATUS_SOURCE_NONE;
216}
217
David Brownf5b33d82017-09-01 10:58:27 -0600218/*
219 * Compute the total size of the given image. Includes the size of
220 * the TLVs.
221 */
Fabio Utzig13d9e352017-10-05 20:32:31 -0300222#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600223static int
224boot_read_image_size(int slot, struct image_header *hdr, uint32_t *size)
225{
226 const struct flash_area *fap;
227 struct image_tlv_info info;
228 int area_id;
229 int rc;
230
231 area_id = flash_area_id_from_image_slot(slot);
232 rc = flash_area_open(area_id, &fap);
233 if (rc != 0) {
234 rc = BOOT_EFLASH;
235 goto done;
236 }
237
238 rc = flash_area_read(fap, hdr->ih_hdr_size + hdr->ih_img_size,
239 &info, sizeof(info));
240 if (rc != 0) {
241 rc = BOOT_EFLASH;
242 goto done;
243 }
244 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
245 rc = BOOT_EBADIMAGE;
246 goto done;
247 }
248 *size = hdr->ih_hdr_size + hdr->ih_img_size + info.it_tlv_tot;
249 rc = 0;
250
251done:
252 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300253 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600254}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300255#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600256
Fabio Utzigc08ed212017-06-20 19:28:36 -0300257static int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800258boot_read_image_header(int slot, struct image_header *out_hdr)
259{
260 const struct flash_area *fap;
261 int area_id;
262 int rc;
263
264 area_id = flash_area_id_from_image_slot(slot);
265 rc = flash_area_open(area_id, &fap);
266 if (rc != 0) {
267 rc = BOOT_EFLASH;
268 goto done;
269 }
270
271 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
272 if (rc != 0) {
273 rc = BOOT_EFLASH;
274 goto done;
275 }
276
277 rc = 0;
278
279done:
280 flash_area_close(fap);
281 return rc;
282}
283
284static int
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200285boot_read_image_headers(bool require_all)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800286{
287 int rc;
288 int i;
289
290 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Marti Bolivarf804f622017-06-12 15:41:48 -0400291 rc = boot_read_image_header(i, boot_img_hdr(&boot_data, i));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800292 if (rc != 0) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200293 /* If `require_all` is set, fail on any single fail, otherwise
294 * if at least the first slot's header was read successfully,
295 * then the boot loader can attempt a boot.
296 *
297 * Failure to read any headers is a fatal error.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800298 */
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200299 if (i > 0 && !require_all) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800300 return 0;
301 } else {
302 return rc;
303 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800304 }
305 }
306
307 return 0;
308}
309
310static uint8_t
311boot_write_sz(void)
312{
313 uint8_t elem_sz;
314 uint8_t align;
315
316 /* Figure out what size to write update status update as. The size depends
317 * on what the minimum write size is for scratch area, active image slot.
318 * We need to use the bigger of those 2 values.
319 */
David Vinczeba3bd602019-06-17 16:01:43 +0200320 elem_sz = flash_area_align(BOOT_IMG_AREA(&boot_data, BOOT_PRIMARY_SLOT));
321 align = flash_area_align(BOOT_SCRATCH_AREA(&boot_data));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800322 if (align > elem_sz) {
323 elem_sz = align;
324 }
325
326 return elem_sz;
327}
328
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200329/*
330 * Slots are compatible when all sectors that store upto to size of the image
331 * round up to sector size, in both slot's are able to fit in the scratch
332 * area, and have sizes that are a multiple of each other (powers of two
333 * presumably!).
334 */
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800335static int
336boot_slots_compatible(void)
337{
David Vincze2d736ad2019-02-18 11:50:22 +0100338 size_t num_sectors_primary;
339 size_t num_sectors_secondary;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200340 size_t sz0, sz1;
David Vincze2d736ad2019-02-18 11:50:22 +0100341 size_t primary_slot_sz, secondary_slot_sz;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200342 size_t scratch_sz;
343 size_t i, j;
344 int8_t smaller;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800345
David Vincze2d736ad2019-02-18 11:50:22 +0100346 num_sectors_primary =
347 boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT);
348 num_sectors_secondary =
349 boot_img_num_sectors(&boot_data, BOOT_SECONDARY_SLOT);
350 if ((num_sectors_primary > BOOT_MAX_IMG_SECTORS) ||
351 (num_sectors_secondary > BOOT_MAX_IMG_SECTORS)) {
Fabio Utziga1fae672018-03-30 10:52:38 -0300352 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800353 return 0;
354 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300355
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200356 scratch_sz = boot_scratch_area_size(&boot_data);
357
358 /*
359 * The following loop scans all sectors in a linear fashion, assuring that
360 * for each possible sector in each slot, it is able to fit in the other
361 * slot's sector or sectors. Slot's should be compatible as long as any
362 * number of a slot's sectors are able to fit into another, which only
363 * excludes cases where sector sizes are not a multiple of each other.
364 */
David Vincze2d736ad2019-02-18 11:50:22 +0100365 i = sz0 = primary_slot_sz = 0;
366 j = sz1 = secondary_slot_sz = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200367 smaller = 0;
David Vincze2d736ad2019-02-18 11:50:22 +0100368 while (i < num_sectors_primary || j < num_sectors_secondary) {
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200369 if (sz0 == sz1) {
David Vincze2d736ad2019-02-18 11:50:22 +0100370 sz0 += boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
371 sz1 += boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT, j);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200372 i++;
373 j++;
374 } else if (sz0 < sz1) {
David Vincze2d736ad2019-02-18 11:50:22 +0100375 sz0 += boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
376 /* Guarantee that multiple sectors of the secondary slot
377 * fit into the primary slot.
378 */
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200379 if (smaller == 2) {
380 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors");
381 return 0;
382 }
383 smaller = 1;
384 i++;
385 } else {
David Vincze2d736ad2019-02-18 11:50:22 +0100386 sz1 += boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT, j);
387 /* Guarantee that multiple sectors of the primary slot
388 * fit into the secondary slot.
389 */
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200390 if (smaller == 1) {
391 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors");
392 return 0;
393 }
394 smaller = 2;
395 j++;
396 }
397 if (sz0 == sz1) {
David Vincze2d736ad2019-02-18 11:50:22 +0100398 primary_slot_sz += sz0;
399 secondary_slot_sz += sz1;
400 /* Scratch has to fit each swap operation to the size of the larger
401 * sector among the primary slot and the secondary slot.
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200402 */
403 if (sz0 > scratch_sz || sz1 > scratch_sz) {
404 BOOT_LOG_WRN("Cannot upgrade: not all sectors fit inside scratch");
405 return 0;
406 }
407 smaller = sz0 = sz1 = 0;
408 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300409 }
410
David Vincze2d736ad2019-02-18 11:50:22 +0100411 if ((i != num_sectors_primary) ||
412 (j != num_sectors_secondary) ||
413 (primary_slot_sz != secondary_slot_sz)) {
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200414 BOOT_LOG_WRN("Cannot upgrade: slots are not compatible");
415 return 0;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800416 }
417
418 return 1;
419}
420
Christopher Collins92ea77f2016-12-12 15:59:26 -0800421/**
422 * Determines the sector layout of both image slots and the scratch area.
423 * This information is necessary for calculating the number of bytes to erase
424 * and copy during an image swap. The information collected during this
425 * function is used to populate the boot_data global.
426 */
427static int
428boot_read_sectors(void)
429{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800430 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800431
David Vincze2d736ad2019-02-18 11:50:22 +0100432 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_PRIMARY);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800433 if (rc != 0) {
434 return BOOT_EFLASH;
435 }
436
David Vincze2d736ad2019-02-18 11:50:22 +0100437 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_SECONDARY);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800438 if (rc != 0) {
439 return BOOT_EFLASH;
440 }
441
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200442 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_SCRATCH);
443 if (rc != 0) {
444 return BOOT_EFLASH;
445 }
446
Marti Bolivare10a7392017-06-14 16:20:07 -0400447 BOOT_WRITE_SZ(&boot_data) = boot_write_sz();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800448
449 return 0;
450}
451
452static uint32_t
453boot_status_internal_off(int idx, int state, int elem_sz)
454{
455 int idx_sz;
456
457 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
458
Fabio Utzig39000012018-07-30 12:40:20 -0300459 return (idx - BOOT_STATUS_IDX_0) * idx_sz +
460 (state - BOOT_STATUS_STATE_0) * elem_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800461}
462
463/**
464 * Reads the status of a partially-completed swap, if any. This is necessary
465 * to recover in case the boot lodaer was reset in the middle of a swap
466 * operation.
467 */
468static int
469boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs)
470{
471 uint32_t off;
472 uint8_t status;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300473 int max_entries;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800474 int found;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200475 int found_idx;
476 int invalid;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800477 int rc;
478 int i;
479
480 off = boot_status_off(fap);
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400481 max_entries = boot_status_entries(fap);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300482
Christopher Collins92ea77f2016-12-12 15:59:26 -0800483 found = 0;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200484 found_idx = 0;
485 invalid = 0;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300486 for (i = 0; i < max_entries; i++) {
Fabio Utzig178be542018-09-19 08:12:56 -0300487 rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(&boot_data),
488 &status, 1);
489 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800490 return BOOT_EFLASH;
491 }
492
Fabio Utzig178be542018-09-19 08:12:56 -0300493 if (rc == 1) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200494 if (found && !found_idx) {
495 found_idx = i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800496 }
497 } else if (!found) {
498 found = 1;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200499 } else if (found_idx) {
500 invalid = 1;
501 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800502 }
503 }
504
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200505 if (invalid) {
506 /* This means there was an error writing status on the last
507 * swap. Tell user and move on to validation!
508 */
509 BOOT_LOG_ERR("Detected inconsistent status!");
510
David Vincze2d736ad2019-02-18 11:50:22 +0100511#if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
512 /* With validation of the primary slot disabled, there is no way
513 * to be sure the swapped primary slot is OK, so abort!
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200514 */
515 assert(0);
516#endif
517 }
518
Christopher Collins92ea77f2016-12-12 15:59:26 -0800519 if (found) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200520 if (!found_idx) {
521 found_idx = i;
522 }
523 found_idx--;
Fabio Utzig39000012018-07-30 12:40:20 -0300524 bs->idx = (found_idx / BOOT_STATUS_STATE_COUNT) + 1;
525 bs->state = (found_idx % BOOT_STATUS_STATE_COUNT) + 1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800526 }
527
528 return 0;
529}
530
531/**
532 * Reads the boot status from the flash. The boot status contains
533 * the current state of an interrupted image copy operation. If the boot
534 * status is not present, or it indicates that previous copy finished,
535 * there is no operation in progress.
536 */
537static int
538boot_read_status(struct boot_status *bs)
539{
540 const struct flash_area *fap;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700541 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200542 uint8_t swap_info;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800543 int status_loc;
544 int area_id;
545 int rc;
546
547 memset(bs, 0, sizeof *bs);
Fabio Utzig39000012018-07-30 12:40:20 -0300548 bs->idx = BOOT_STATUS_IDX_0;
549 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700550 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800551
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700552#ifdef MCUBOOT_OVERWRITE_ONLY
553 /* Overwrite-only doesn't make use of the swap status area. */
554 return 0;
555#endif
556
Christopher Collins92ea77f2016-12-12 15:59:26 -0800557 status_loc = boot_status_source();
558 switch (status_loc) {
559 case BOOT_STATUS_SOURCE_NONE:
560 return 0;
561
562 case BOOT_STATUS_SOURCE_SCRATCH:
563 area_id = FLASH_AREA_IMAGE_SCRATCH;
564 break;
565
David Vincze2d736ad2019-02-18 11:50:22 +0100566 case BOOT_STATUS_SOURCE_PRIMARY_SLOT:
567 area_id = FLASH_AREA_IMAGE_PRIMARY;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800568 break;
569
570 default:
571 assert(0);
572 return BOOT_EBADARGS;
573 }
574
575 rc = flash_area_open(area_id, &fap);
576 if (rc != 0) {
577 return BOOT_EFLASH;
578 }
579
Fabio Utzig46490722017-09-04 15:34:32 -0300580 rc = boot_read_status_bytes(fap, bs);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700581 if (rc == 0) {
David Vinczee2453472019-06-17 12:31:59 +0200582 off = boot_swap_info_off(fap);
583 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700584 if (rc == 1) {
David Vinczee2453472019-06-17 12:31:59 +0200585 BOOT_SET_SWAP_INFO(swap_info, 0, BOOT_SWAP_TYPE_NONE);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700586 rc = 0;
587 }
David Vinczee2453472019-06-17 12:31:59 +0200588
589 /* Extract the swap type info */
590 bs->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700591 }
Fabio Utzig46490722017-09-04 15:34:32 -0300592
593 flash_area_close(fap);
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700594
Fabio Utzig46490722017-09-04 15:34:32 -0300595 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800596}
597
598/**
599 * Writes the supplied boot status to the flash file system. The boot status
600 * contains the current state of an in-progress image copy operation.
601 *
602 * @param bs The boot status to write.
603 *
604 * @return 0 on success; nonzero on failure.
605 */
606int
607boot_write_status(struct boot_status *bs)
608{
609 const struct flash_area *fap;
610 uint32_t off;
611 int area_id;
612 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300613 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700614 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300615 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800616
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300617 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100618 * the trailer. Since in the last step the primary slot is erased, the
619 * first two status writes go to the scratch which will be copied to
620 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300621 */
622
Fabio Utzig2473ac02017-05-02 12:45:02 -0300623 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800624 /* Write to scratch. */
625 area_id = FLASH_AREA_IMAGE_SCRATCH;
626 } else {
David Vincze2d736ad2019-02-18 11:50:22 +0100627 /* Write to the primary slot. */
628 area_id = FLASH_AREA_IMAGE_PRIMARY;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800629 }
630
631 rc = flash_area_open(area_id, &fap);
632 if (rc != 0) {
633 rc = BOOT_EFLASH;
634 goto done;
635 }
636
637 off = boot_status_off(fap) +
Marti Bolivare10a7392017-06-14 16:20:07 -0400638 boot_status_internal_off(bs->idx, bs->state,
639 BOOT_WRITE_SZ(&boot_data));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200640 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300641 erased_val = flash_area_erased_val(fap);
642 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700643 buf[0] = bs->state;
644
645 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800646 if (rc != 0) {
647 rc = BOOT_EFLASH;
648 goto done;
649 }
650
651 rc = 0;
652
653done:
654 flash_area_close(fap);
655 return rc;
656}
657
658/*
659 * Validate image hash/signature in a slot.
660 */
661static int
Fabio Utzigba829042018-09-18 08:29:34 -0300662boot_image_check(struct image_header *hdr, const struct flash_area *fap,
663 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800664{
David Browndb1d9d32017-01-06 11:07:54 -0700665 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigba829042018-09-18 08:29:34 -0300666 int rc;
667
668#ifndef MCUBOOT_ENC_IMAGES
669 (void)bs;
670 (void)rc;
671#else
David Vincze2d736ad2019-02-18 11:50:22 +0100672 if ((fap->fa_id == FLASH_AREA_IMAGE_SECONDARY) && IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300673 rc = boot_enc_load(hdr, fap, bs->enckey[1]);
674 if (rc < 0) {
675 return BOOT_EBADIMAGE;
676 }
677 if (rc == 0 && boot_enc_set_key(1, bs->enckey[1])) {
678 return BOOT_EBADIMAGE;
679 }
680 }
681#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800682
Christopher Collins92ea77f2016-12-12 15:59:26 -0800683 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
684 NULL, 0, NULL)) {
685 return BOOT_EBADIMAGE;
686 }
687 return 0;
688}
689
690static int
691split_image_check(struct image_header *app_hdr,
692 const struct flash_area *app_fap,
693 struct image_header *loader_hdr,
694 const struct flash_area *loader_fap)
695{
696 static void *tmpbuf;
697 uint8_t loader_hash[32];
698
699 if (!tmpbuf) {
700 tmpbuf = malloc(BOOT_TMPBUF_SZ);
701 if (!tmpbuf) {
702 return BOOT_ENOMEM;
703 }
704 }
705
706 if (bootutil_img_validate(loader_hdr, loader_fap, tmpbuf, BOOT_TMPBUF_SZ,
707 NULL, 0, loader_hash)) {
708 return BOOT_EBADIMAGE;
709 }
710
711 if (bootutil_img_validate(app_hdr, app_fap, tmpbuf, BOOT_TMPBUF_SZ,
712 loader_hash, 32, NULL)) {
713 return BOOT_EBADIMAGE;
714 }
715
716 return 0;
717}
718
Fabio Utzig338a19f2018-12-03 08:37:08 -0200719/*
720 * Check that a memory area consists of a given value.
721 */
722static inline bool
723boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300724{
725 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200726 uint8_t *p = (uint8_t *)data;
727 for (i = 0; i < len; i++) {
728 if (val != p[i]) {
729 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300730 }
731 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200732 return true;
733}
734
735static int
736boot_check_header_erased(int slot)
737{
738 const struct flash_area *fap;
739 struct image_header *hdr;
740 uint8_t erased_val;
741 int rc;
742
743 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
744 if (rc != 0) {
745 return -1;
746 }
747
748 erased_val = flash_area_erased_val(fap);
749 flash_area_close(fap);
750
751 hdr = boot_img_hdr(&boot_data, slot);
752 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
753 return -1;
754 }
755
756 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300757}
758
Christopher Collins92ea77f2016-12-12 15:59:26 -0800759static int
Fabio Utzigba829042018-09-18 08:29:34 -0300760boot_validate_slot(int slot, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800761{
762 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400763 struct image_header *hdr;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800764 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300765
David Brownd930ec62016-12-14 07:59:48 -0700766 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800767 if (rc != 0) {
768 return BOOT_EFLASH;
769 }
770
Fabio Utzig39000012018-07-30 12:40:20 -0300771 hdr = boot_img_hdr(&boot_data, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200772 if (boot_check_header_erased(slot) == 0 || (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100773 /* No bootable image in slot; continue booting from the primary slot. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200774 rc = -1;
775 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300776 }
777
Fabio Utzigba829042018-09-18 08:29:34 -0300778 if ((hdr->ih_magic != IMAGE_MAGIC || boot_image_check(hdr, fap, bs) != 0)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100779 if (slot != BOOT_PRIMARY_SLOT) {
David Brownb38e0442017-02-24 13:57:12 -0700780 flash_area_erase(fap, 0, fap->fa_size);
David Vincze2d736ad2019-02-18 11:50:22 +0100781 /* Image in the secondary slot is invalid. Erase the image and
782 * continue booting from the primary slot.
David Brownb38e0442017-02-24 13:57:12 -0700783 */
784 }
David Vincze2d736ad2019-02-18 11:50:22 +0100785 BOOT_LOG_ERR("Image in the %s slot is not valid!",
786 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
Fabio Utzig338a19f2018-12-03 08:37:08 -0200787 rc = -1;
788 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800789 }
790
David Vincze2d736ad2019-02-18 11:50:22 +0100791 /* Image in the secondary slot is valid. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200792 rc = 0;
793
794out:
795 flash_area_close(fap);
796 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800797}
798
799/**
800 * Determines which swap operation to perform, if any. If it is determined
David Vincze2d736ad2019-02-18 11:50:22 +0100801 * that a swap operation is required, the image in the secondary slot is checked
802 * for validity. If the image in the secondary slot is invalid, it is erased,
803 * and a swap type of "none" is indicated.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800804 *
805 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
806 */
807static int
Fabio Utzigba829042018-09-18 08:29:34 -0300808boot_validated_swap_type(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800809{
810 int swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800811
812 swap_type = boot_swap_type();
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300813 switch (swap_type) {
814 case BOOT_SWAP_TYPE_TEST:
815 case BOOT_SWAP_TYPE_PERM:
816 case BOOT_SWAP_TYPE_REVERT:
David Vincze2d736ad2019-02-18 11:50:22 +0100817 /* Boot loader wants to switch to the secondary slot.
818 * Ensure image is valid.
819 */
820 if (boot_validate_slot(BOOT_SECONDARY_SLOT, bs) != 0) {
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300821 swap_type = BOOT_SWAP_TYPE_FAIL;
822 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800823 }
824
825 return swap_type;
826}
827
828/**
829 * Calculates the number of sectors the scratch area can contain. A "last"
830 * source sector is specified because images are copied backwards in flash
831 * (final index to index number 0).
832 *
833 * @param last_sector_idx The index of the last source sector
834 * (inclusive).
835 * @param out_first_sector_idx The index of the first source sector
836 * (inclusive) gets written here.
837 *
838 * @return The number of bytes comprised by the
839 * [first-sector, last-sector] range.
840 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300841#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800842static uint32_t
843boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
844{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400845 size_t scratch_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800846 uint32_t new_sz;
847 uint32_t sz;
848 int i;
849
850 sz = 0;
851
Marti Bolivard3269fd2017-06-12 16:31:12 -0400852 scratch_sz = boot_scratch_area_size(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800853 for (i = last_sector_idx; i >= 0; i--) {
David Vincze2d736ad2019-02-18 11:50:22 +0100854 new_sz = sz + boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200855 /*
David Vincze2d736ad2019-02-18 11:50:22 +0100856 * The secondary slot is not being checked here, because
857 * `boot_slots_compatible` already provides assurance that the copy size
858 * will be compatible with the primary slot and scratch.
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200859 */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400860 if (new_sz > scratch_sz) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800861 break;
862 }
863 sz = new_sz;
864 }
865
866 /* i currently refers to a sector that doesn't fit or it is -1 because all
867 * sectors have been processed. In both cases, exclude sector i.
868 */
869 *out_first_sector_idx = i + 1;
870 return sz;
871}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300872#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800873
874/**
875 * Erases a region of flash.
876 *
Fabio Utzigba829042018-09-18 08:29:34 -0300877 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800878 * @param off The offset within the flash area to start the
879 * erase.
880 * @param sz The number of bytes to erase.
881 *
882 * @return 0 on success; nonzero on failure.
883 */
Fabio Utzigba829042018-09-18 08:29:34 -0300884static inline int
885boot_erase_sector(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800886{
Fabio Utzigba829042018-09-18 08:29:34 -0300887 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800888}
889
890/**
891 * Copies the contents of one flash region to another. You must erase the
892 * destination region prior to calling this function.
893 *
894 * @param flash_area_id_src The ID of the source flash area.
895 * @param flash_area_id_dst The ID of the destination flash area.
896 * @param off_src The offset within the source flash area to
897 * copy from.
898 * @param off_dst The offset within the destination flash area to
899 * copy to.
900 * @param sz The number of bytes to copy.
901 *
902 * @return 0 on success; nonzero on failure.
903 */
904static int
Fabio Utzigba829042018-09-18 08:29:34 -0300905boot_copy_sector(const struct flash_area *fap_src,
906 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800907 uint32_t off_src, uint32_t off_dst, uint32_t sz)
908{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800909 uint32_t bytes_copied;
910 int chunk_sz;
911 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -0300912#ifdef MCUBOOT_ENC_IMAGES
913 uint32_t off;
914 size_t blk_off;
915 struct image_header *hdr;
916 uint16_t idx;
917 uint32_t blk_sz;
918#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800919
920 static uint8_t buf[1024];
921
Christopher Collins92ea77f2016-12-12 15:59:26 -0800922 bytes_copied = 0;
923 while (bytes_copied < sz) {
924 if (sz - bytes_copied > sizeof buf) {
925 chunk_sz = sizeof buf;
926 } else {
927 chunk_sz = sz - bytes_copied;
928 }
929
930 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
931 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300932 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800933 }
934
Fabio Utzigba829042018-09-18 08:29:34 -0300935#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +0100936 if ((fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY) ||
937 (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY)) {
938 /* assume the secondary slot as src, needs decryption */
939 hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -0300940 off = off_src;
David Vincze2d736ad2019-02-18 11:50:22 +0100941 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY) {
942 /* might need encryption (metadata from the primary slot) */
943 hdr = boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -0300944 off = off_dst;
945 }
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200946 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300947 blk_sz = chunk_sz;
948 idx = 0;
949 if (off + bytes_copied < hdr->ih_hdr_size) {
950 /* do not decrypt header */
951 blk_off = 0;
952 blk_sz = chunk_sz - hdr->ih_hdr_size;
953 idx = hdr->ih_hdr_size;
954 } else {
955 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
956 }
957 if (off + bytes_copied + chunk_sz > hdr->ih_hdr_size + hdr->ih_img_size) {
958 /* do not decrypt TLVs */
959 if (off + bytes_copied >= hdr->ih_hdr_size + hdr->ih_img_size) {
960 blk_sz = 0;
961 } else {
962 blk_sz = (hdr->ih_hdr_size + hdr->ih_img_size) - (off + bytes_copied);
963 }
964 }
965 boot_encrypt(fap_src, (off + bytes_copied + idx) - hdr->ih_hdr_size,
966 blk_sz, blk_off, &buf[idx]);
967 }
968 }
969#endif
970
Christopher Collins92ea77f2016-12-12 15:59:26 -0800971 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
972 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300973 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800974 }
975
976 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -0300977
978 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800979 }
980
Fabio Utzigba829042018-09-18 08:29:34 -0300981 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800982}
983
David Brown6b1b3b92017-09-19 08:59:10 -0600984#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -0300985static inline int
Fabio Utzigba829042018-09-18 08:29:34 -0300986boot_status_init(const struct flash_area *fap, const struct boot_status *bs)
Fabio Utzig2473ac02017-05-02 12:45:02 -0300987{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300988 struct boot_swap_state swap_state;
989 int rc;
990
Christopher Collins2c88e692019-05-22 15:10:14 -0700991 BOOT_LOG_DBG("initializing status; fa_id=%d", fap->fa_id);
992
David Vincze2d736ad2019-02-18 11:50:22 +0100993 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY, &swap_state);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300994 assert(rc == 0);
995
Christopher Collinsa1c12042019-05-23 14:00:28 -0700996 if (bs->swap_type != BOOT_SWAP_TYPE_NONE) {
David Vinczee2453472019-06-17 12:31:59 +0200997 rc = boot_write_swap_info(fap,
998 bs->swap_type,
David Vinczeba3bd602019-06-17 16:01:43 +0200999 current_image);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001000 assert(rc == 0);
1001 }
1002
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001003 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001004 rc = boot_write_image_ok(fap);
1005 assert(rc == 0);
1006 }
1007
Fabio Utzig46490722017-09-04 15:34:32 -03001008 rc = boot_write_swap_size(fap, bs->swap_size);
1009 assert(rc == 0);
1010
Fabio Utzigba829042018-09-18 08:29:34 -03001011#ifdef MCUBOOT_ENC_IMAGES
1012 rc = boot_write_enc_key(fap, 0, bs->enckey[0]);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001013 assert(rc == 0);
1014
Fabio Utzigba829042018-09-18 08:29:34 -03001015 rc = boot_write_enc_key(fap, 1, bs->enckey[1]);
1016 assert(rc == 0);
1017#endif
1018
1019 rc = boot_write_magic(fap);
1020 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001021
1022 return 0;
1023}
Christopher Collinsa1c12042019-05-23 14:00:28 -07001024
David Brown6b1b3b92017-09-19 08:59:10 -06001025#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001026
Fabio Utzig358c9352017-07-25 22:10:45 -03001027#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -03001028static int
Fabio Utziged0ca432019-01-23 14:50:11 -02001029boot_erase_trailer_sectors(const struct flash_area *fap)
Fabio Utzig2473ac02017-05-02 12:45:02 -03001030{
1031 uint8_t slot;
Fabio Utziged0ca432019-01-23 14:50:11 -02001032 uint32_t sector;
1033 uint32_t trailer_sz;
1034 uint32_t total_sz;
1035 uint32_t off;
1036 uint32_t sz;
David Vinczeb75c12a2019-03-22 14:58:33 +01001037 int fa_id_primary;
1038 int fa_id_secondary;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001039 int rc;
1040
Christopher Collins2c88e692019-05-22 15:10:14 -07001041 BOOT_LOG_DBG("erasing trailer; fa_id=%d", fap->fa_id);
1042
David Vinczeb75c12a2019-03-22 14:58:33 +01001043 fa_id_primary = flash_area_id_from_image_slot(BOOT_PRIMARY_SLOT);
1044 fa_id_secondary = flash_area_id_from_image_slot(BOOT_SECONDARY_SLOT);
1045
1046 if (fap->fa_id == fa_id_primary) {
David Vincze2d736ad2019-02-18 11:50:22 +01001047 slot = BOOT_PRIMARY_SLOT;
David Vinczeb75c12a2019-03-22 14:58:33 +01001048 } else if (fap->fa_id == fa_id_secondary) {
David Vincze2d736ad2019-02-18 11:50:22 +01001049 slot = BOOT_SECONDARY_SLOT;
David Vinczeb75c12a2019-03-22 14:58:33 +01001050 } else {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001051 return BOOT_EFLASH;
1052 }
1053
Fabio Utziged0ca432019-01-23 14:50:11 -02001054 /* delete starting from last sector and moving to beginning */
1055 sector = boot_img_num_sectors(&boot_data, slot) - 1;
Christopher Collins2adef702019-05-22 14:37:31 -07001056 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Fabio Utziged0ca432019-01-23 14:50:11 -02001057 total_sz = 0;
1058 do {
1059 sz = boot_img_sector_size(&boot_data, slot, sector);
1060 off = boot_img_sector_off(&boot_data, slot, sector);
1061 rc = boot_erase_sector(fap, off, sz);
1062 assert(rc == 0);
1063
1064 sector--;
1065 total_sz += sz;
1066 } while (total_sz < trailer_sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001067
1068 return rc;
1069}
Fabio Utzig358c9352017-07-25 22:10:45 -03001070#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001071
Christopher Collins92ea77f2016-12-12 15:59:26 -08001072/**
1073 * Swaps the contents of two flash regions within the two image slots.
1074 *
1075 * @param idx The index of the first sector in the range of
1076 * sectors being swapped.
1077 * @param sz The number of bytes to swap.
1078 * @param bs The current boot status. This struct gets
1079 * updated according to the outcome.
1080 *
1081 * @return 0 on success; nonzero on failure.
1082 */
Fabio Utzig3488eef2017-06-12 10:25:43 -03001083#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -08001084static void
Christopher Collins92ea77f2016-12-12 15:59:26 -08001085boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
1086{
David Vincze2d736ad2019-02-18 11:50:22 +01001087 const struct flash_area *fap_primary_slot;
1088 const struct flash_area *fap_secondary_slot;
Fabio Utzigba829042018-09-18 08:29:34 -03001089 const struct flash_area *fap_scratch;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001090 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001091 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001092 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001093 uint32_t scratch_trailer_off;
1094 struct boot_swap_state swap_state;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001095 size_t last_sector;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001096 bool erase_scratch;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001097 int rc;
1098
1099 /* Calculate offset from start of image area. */
David Vincze2d736ad2019-02-18 11:50:22 +01001100 img_off = boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT, idx);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001101
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001102 copy_sz = sz;
Christopher Collins2adef702019-05-22 14:37:31 -07001103 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Fabio Utzig9678c972017-05-23 11:28:56 -04001104
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001105 /* sz in this function is always sized on a multiple of the sector size.
1106 * The check against the start offset of the last sector
Fabio Utzig9678c972017-05-23 11:28:56 -04001107 * is to determine if we're swapping the last sector. The last sector
1108 * needs special handling because it's where the trailer lives. If we're
1109 * copying it, we need to use scratch to write the trailer temporarily.
1110 *
1111 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
1112 * controls if special handling is needed (swapping last sector).
1113 */
David Vincze2d736ad2019-02-18 11:50:22 +01001114 last_sector = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT) - 1;
David Vinczeba3bd602019-06-17 16:01:43 +02001115 if ((img_off + sz) >
1116 boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT, last_sector)) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001117 copy_sz -= trailer_sz;
1118 }
1119
Fabio Utzig39000012018-07-30 12:40:20 -03001120 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001121
David Vincze2d736ad2019-02-18 11:50:22 +01001122 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001123 assert (rc == 0);
1124
David Vincze2d736ad2019-02-18 11:50:22 +01001125 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001126 assert (rc == 0);
1127
1128 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch);
1129 assert (rc == 0);
1130
Fabio Utzig39000012018-07-30 12:40:20 -03001131 if (bs->state == BOOT_STATUS_STATE_0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001132 BOOT_LOG_DBG("erasing scratch area");
Christopher Collinsa1c12042019-05-23 14:00:28 -07001133 rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size);
Christopher Collins4772ac42017-02-27 20:08:01 -08001134 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001135
Fabio Utzig39000012018-07-30 12:40:20 -03001136 if (bs->idx == BOOT_STATUS_IDX_0) {
Christopher Collinsa1c12042019-05-23 14:00:28 -07001137 /* Write a trailer to the scratch area, even if we don't need the
1138 * scratch area for status. We need a temporary place to store the
1139 * `swap-type` while we erase the primary trailer.
1140 */
1141 rc = boot_status_init(fap_scratch, bs);
1142 assert(rc == 0);
1143
1144 if (!bs->use_scratch) {
1145 /* Prepare the primary status area... here it is known that the
1146 * last sector is not being used by the image data so it's safe
1147 * to erase.
Fabio Utzig2473ac02017-05-02 12:45:02 -03001148 */
David Vincze2d736ad2019-02-18 11:50:22 +01001149 rc = boot_erase_trailer_sectors(fap_primary_slot);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001150 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001151
Christopher Collinsa1c12042019-05-23 14:00:28 -07001152 rc = boot_status_init(fap_primary_slot, bs);
1153 assert(rc == 0);
1154
1155 /* Erase the temporary trailer from the scratch area. */
1156 rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size);
1157 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001158 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001159 }
1160
Christopher Collinsa1c12042019-05-23 14:00:28 -07001161 rc = boot_copy_sector(fap_secondary_slot, fap_scratch,
1162 img_off, 0, copy_sz);
1163 assert(rc == 0);
1164
Fabio Utzig39000012018-07-30 12:40:20 -03001165 bs->state = BOOT_STATUS_STATE_1;
Christopher Collins4772ac42017-02-27 20:08:01 -08001166 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001167 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001168 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001169
Fabio Utzig39000012018-07-30 12:40:20 -03001170 if (bs->state == BOOT_STATUS_STATE_1) {
David Vincze2d736ad2019-02-18 11:50:22 +01001171 rc = boot_erase_sector(fap_secondary_slot, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001172 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001173
David Vincze2d736ad2019-02-18 11:50:22 +01001174 rc = boot_copy_sector(fap_primary_slot, fap_secondary_slot,
1175 img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001176 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001177
Fabio Utzig39000012018-07-30 12:40:20 -03001178 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001179 /* If not all sectors of the slot are being swapped,
David Vincze2d736ad2019-02-18 11:50:22 +01001180 * guarantee here that only the primary slot will have the state.
Fabio Utzig2473ac02017-05-02 12:45:02 -03001181 */
David Vincze2d736ad2019-02-18 11:50:22 +01001182 rc = boot_erase_trailer_sectors(fap_secondary_slot);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001183 assert(rc == 0);
1184 }
1185
Fabio Utzig39000012018-07-30 12:40:20 -03001186 bs->state = BOOT_STATUS_STATE_2;
Christopher Collins4772ac42017-02-27 20:08:01 -08001187 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001188 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001189 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001190
Fabio Utzig39000012018-07-30 12:40:20 -03001191 if (bs->state == BOOT_STATUS_STATE_2) {
David Vincze2d736ad2019-02-18 11:50:22 +01001192 rc = boot_erase_sector(fap_primary_slot, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001193 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001194
Christopher Collinsa1c12042019-05-23 14:00:28 -07001195 /* NOTE: If this is the final sector, we exclude the image trailer from
1196 * this copy (copy_sz was truncated earlier).
1197 */
David Vincze2d736ad2019-02-18 11:50:22 +01001198 rc = boot_copy_sector(fap_scratch, fap_primary_slot,
1199 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001200 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001201
Fabio Utzig94d998c2017-05-22 11:02:41 -04001202 if (bs->use_scratch) {
Fabio Utzigba829042018-09-18 08:29:34 -03001203 scratch_trailer_off = boot_status_off(fap_scratch);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001204
1205 /* copy current status that is being maintained in scratch */
David Vincze2d736ad2019-02-18 11:50:22 +01001206 rc = boot_copy_sector(fap_scratch, fap_primary_slot,
1207 scratch_trailer_off, img_off + copy_sz,
Marti Bolivare10a7392017-06-14 16:20:07 -04001208 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data));
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001209 BOOT_STATUS_ASSERT(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001210
Fabio Utzig2473ac02017-05-02 12:45:02 -03001211 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1212 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001213 assert(rc == 0);
1214
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001215 if (swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze2d736ad2019-02-18 11:50:22 +01001216 rc = boot_write_image_ok(fap_primary_slot);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001217 assert(rc == 0);
1218 }
1219
Christopher Collinsa1c12042019-05-23 14:00:28 -07001220 if (swap_state.swap_type != BOOT_SWAP_TYPE_NONE) {
David Vinczee2453472019-06-17 12:31:59 +02001221 rc = boot_write_swap_info(fap_primary_slot,
1222 swap_state.swap_type,
David Vinczeba3bd602019-06-17 16:01:43 +02001223 current_image);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001224 assert(rc == 0);
1225 }
1226
David Vincze2d736ad2019-02-18 11:50:22 +01001227 rc = boot_write_swap_size(fap_primary_slot, bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001228 assert(rc == 0);
1229
Fabio Utzigba829042018-09-18 08:29:34 -03001230#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +01001231 rc = boot_write_enc_key(fap_primary_slot, 0, bs->enckey[0]);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001232 assert(rc == 0);
1233
David Vincze2d736ad2019-02-18 11:50:22 +01001234 rc = boot_write_enc_key(fap_primary_slot, 1, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001235 assert(rc == 0);
1236#endif
David Vincze2d736ad2019-02-18 11:50:22 +01001237 rc = boot_write_magic(fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001238 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001239 }
1240
Christopher Collinsa1c12042019-05-23 14:00:28 -07001241 /* If we wrote a trailer to the scratch area, erase it after we persist
1242 * a trailer to the primary slot. We do this to prevent mcuboot from
1243 * reading a stale status from the scratch area in case of immediate
1244 * reset.
1245 */
1246 erase_scratch = bs->use_scratch;
1247 bs->use_scratch = 0;
1248
Christopher Collins92ea77f2016-12-12 15:59:26 -08001249 bs->idx++;
Fabio Utzig39000012018-07-30 12:40:20 -03001250 bs->state = BOOT_STATUS_STATE_0;
Christopher Collins4772ac42017-02-27 20:08:01 -08001251 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001252 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001253
1254 if (erase_scratch) {
1255 rc = boot_erase_sector(fap_scratch, 0, sz);
1256 assert(rc == 0);
1257 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001258 }
Fabio Utzigba829042018-09-18 08:29:34 -03001259
David Vincze2d736ad2019-02-18 11:50:22 +01001260 flash_area_close(fap_primary_slot);
1261 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001262 flash_area_close(fap_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001263}
Fabio Utzig3488eef2017-06-12 10:25:43 -03001264#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001265
1266/**
David Vincze2d736ad2019-02-18 11:50:22 +01001267 * Overwrite primary slot with the image contained in the secondary slot.
1268 * If a prior copy operation was interrupted by a system reset, this function
1269 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001270 *
1271 * @param bs The current boot status. This function reads
1272 * this struct to determine if it is resuming
1273 * an interrupted swap operation. This
1274 * function writes the updated status to this
1275 * function on return.
1276 *
1277 * @return 0 on success; nonzero on failure.
1278 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001279#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001280static int
1281boot_copy_image(struct boot_status *bs)
1282{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001283 size_t sect_count;
1284 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001285 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001286 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001287 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001288 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +01001289 const struct flash_area *fap_primary_slot;
1290 const struct flash_area *fap_secondary_slot;
1291
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001292 (void)bs;
1293
Fabio Utzig13d9e352017-10-05 20:32:31 -03001294#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1295 uint32_t src_size = 0;
David Vincze2d736ad2019-02-18 11:50:22 +01001296 rc = boot_read_image_size(BOOT_SECONDARY_SLOT,
1297 boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT),
1298 &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001299 assert(rc == 0);
1300#endif
David Brown17609d82017-05-05 09:41:34 -06001301
David Vincze2d736ad2019-02-18 11:50:22 +01001302 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1303 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -06001304
David Vincze2d736ad2019-02-18 11:50:22 +01001305 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001306 assert (rc == 0);
1307
David Vincze2d736ad2019-02-18 11:50:22 +01001308 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001309 assert (rc == 0);
1310
David Vincze2d736ad2019-02-18 11:50:22 +01001311 sect_count = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001312 for (sect = 0, size = 0; sect < sect_count; sect++) {
David Vincze2d736ad2019-02-18 11:50:22 +01001313 this_size = boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, sect);
1314 rc = boot_erase_sector(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001315 assert(rc == 0);
1316
1317 size += this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001318
1319#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1320 if (size >= src_size) {
1321 break;
1322 }
1323#endif
David Brown17609d82017-05-05 09:41:34 -06001324 }
1325
Fabio Utzigba829042018-09-18 08:29:34 -03001326#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +01001327 if (IS_ENCRYPTED(boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT))) {
1328 rc = boot_enc_load(boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT),
1329 fap_secondary_slot,
1330 bs->enckey[1]);
1331
Fabio Utzigba829042018-09-18 08:29:34 -03001332 if (rc < 0) {
1333 return BOOT_EBADIMAGE;
1334 }
Fabio Utzige641ea52018-12-03 10:37:53 -02001335 if (rc == 0 && boot_enc_set_key(1, bs->enckey[1])) {
Fabio Utzigba829042018-09-18 08:29:34 -03001336 return BOOT_EBADIMAGE;
1337 }
1338 }
1339#endif
1340
David Vincze2d736ad2019-02-18 11:50:22 +01001341 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1342 size);
1343 rc = boot_copy_sector(fap_secondary_slot, fap_primary_slot, 0, 0, size);
David Brown17609d82017-05-05 09:41:34 -06001344
Fabio Utzig13d9e352017-10-05 20:32:31 -03001345 /*
1346 * Erases header and trailer. The trailer is erased because when a new
1347 * image is written without a trailer as is the case when using newt, the
1348 * trailer that was left might trigger a new upgrade.
1349 */
Christopher Collins2c88e692019-05-22 15:10:14 -07001350 BOOT_LOG_DBG("erasing secondary header");
David Vincze2d736ad2019-02-18 11:50:22 +01001351 rc = boot_erase_sector(fap_secondary_slot,
1352 boot_img_sector_off(&boot_data,
1353 BOOT_SECONDARY_SLOT, 0),
1354 boot_img_sector_size(&boot_data,
1355 BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001356 assert(rc == 0);
David Vincze2d736ad2019-02-18 11:50:22 +01001357 last_sector = boot_img_num_sectors(&boot_data, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -07001358 BOOT_LOG_DBG("erasing secondary trailer");
David Vincze2d736ad2019-02-18 11:50:22 +01001359 rc = boot_erase_sector(fap_secondary_slot,
1360 boot_img_sector_off(&boot_data,
1361 BOOT_SECONDARY_SLOT, last_sector),
1362 boot_img_sector_size(&boot_data,
1363 BOOT_SECONDARY_SLOT, last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001364 assert(rc == 0);
1365
David Vincze2d736ad2019-02-18 11:50:22 +01001366 flash_area_close(fap_primary_slot);
1367 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001368
David Vincze2d736ad2019-02-18 11:50:22 +01001369 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001370
1371 return 0;
1372}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001373#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001374
Christopher Collinsa1c12042019-05-23 14:00:28 -07001375#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -03001376/**
1377 * Swaps the two images in flash. If a prior copy operation was interrupted
1378 * by a system reset, this function completes that operation.
1379 *
1380 * @param bs The current boot status. This function reads
1381 * this struct to determine if it is resuming
1382 * an interrupted swap operation. This
1383 * function writes the updated status to this
1384 * function on return.
1385 *
1386 * @return 0 on success; nonzero on failure.
1387 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001388static int
Fabio Utzig338a19f2018-12-03 08:37:08 -02001389boot_swap_image(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001390{
1391 uint32_t sz;
1392 int first_sector_idx;
1393 int last_sector_idx;
David Vincze2d736ad2019-02-18 11:50:22 +01001394 int last_idx_secondary_slot;
Fabio Utzigcd5774b2017-11-29 10:18:26 -02001395 uint32_t swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001396 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001397#ifdef MCUBOOT_ENC_IMAGES
1398 const struct flash_area *fap;
1399 uint8_t slot;
1400 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001401#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001402 uint32_t size;
1403 uint32_t copy_size;
David Vincze2d736ad2019-02-18 11:50:22 +01001404 uint32_t primary_slot_size;
1405 uint32_t secondary_slot_size;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001406 int rc;
1407
1408 /* FIXME: just do this if asked by user? */
1409
1410 size = copy_size = 0;
1411
Fabio Utzig39000012018-07-30 12:40:20 -03001412 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Fabio Utzig46490722017-09-04 15:34:32 -03001413 /*
1414 * No swap ever happened, so need to find the largest image which
1415 * will be used to determine the amount of sectors to swap.
1416 */
David Vincze2d736ad2019-02-18 11:50:22 +01001417 hdr = boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001418 if (hdr->ih_magic == IMAGE_MAGIC) {
David Vincze2d736ad2019-02-18 11:50:22 +01001419 rc = boot_read_image_size(BOOT_PRIMARY_SLOT, hdr, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001420 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001421 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001422
Fabio Utzigba829042018-09-18 08:29:34 -03001423#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001424 if (IS_ENCRYPTED(hdr)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001425 fap = BOOT_IMG_AREA(&boot_data, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001426 rc = boot_enc_load(hdr, fap, bs->enckey[0]);
1427 assert(rc >= 0);
1428
1429 if (rc == 0) {
1430 rc = boot_enc_set_key(0, bs->enckey[0]);
1431 assert(rc == 0);
1432 } else {
1433 rc = 0;
1434 }
1435 } else {
1436 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1437 }
1438#endif
1439
David Vincze2d736ad2019-02-18 11:50:22 +01001440 hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001441 if (hdr->ih_magic == IMAGE_MAGIC) {
David Vincze2d736ad2019-02-18 11:50:22 +01001442 rc = boot_read_image_size(BOOT_SECONDARY_SLOT, hdr, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001443 assert(rc == 0);
1444 }
1445
Fabio Utzigba829042018-09-18 08:29:34 -03001446#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +01001447 hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001448 if (IS_ENCRYPTED(hdr)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001449 fap = BOOT_IMG_AREA(&boot_data, BOOT_SECONDARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001450 rc = boot_enc_load(hdr, fap, bs->enckey[1]);
1451 assert(rc >= 0);
1452
1453 if (rc == 0) {
1454 rc = boot_enc_set_key(1, bs->enckey[1]);
1455 assert(rc == 0);
1456 } else {
1457 rc = 0;
1458 }
1459 } else {
1460 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1461 }
1462#endif
1463
Fabio Utzig46490722017-09-04 15:34:32 -03001464 if (size > copy_size) {
1465 copy_size = size;
1466 }
1467
1468 bs->swap_size = copy_size;
1469 } else {
1470 /*
1471 * If a swap was under way, the swap_size should already be present
1472 * in the trailer...
1473 */
1474 rc = boot_read_swap_size(&bs->swap_size);
1475 assert(rc == 0);
1476
1477 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001478
1479#ifdef MCUBOOT_ENC_IMAGES
1480 for (slot = 0; slot <= 1; slot++) {
1481 rc = boot_read_enc_key(slot, bs->enckey[slot]);
1482 assert(rc == 0);
1483
1484 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001485 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001486 break;
1487 }
1488 }
1489
1490 if (i != BOOT_ENC_KEY_SIZE) {
1491 boot_enc_set_key(slot, bs->enckey[slot]);
1492 }
1493 }
1494#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001495 }
1496
David Vincze2d736ad2019-02-18 11:50:22 +01001497 primary_slot_size = 0;
1498 secondary_slot_size = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001499 last_sector_idx = 0;
David Vincze2d736ad2019-02-18 11:50:22 +01001500 last_idx_secondary_slot = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001501
1502 /*
1503 * Knowing the size of the largest image between both slots, here we
David Vincze2d736ad2019-02-18 11:50:22 +01001504 * find what is the last sector in the primary slot that needs swapping.
1505 * Since we already know that both slots are compatible, the secondary
1506 * slot's last sector is not really required after this check is finished.
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001507 */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001508 while (1) {
David Vincze2d736ad2019-02-18 11:50:22 +01001509 if ((primary_slot_size < copy_size) ||
1510 (primary_slot_size < secondary_slot_size)) {
1511 primary_slot_size += boot_img_sector_size(&boot_data,
1512 BOOT_PRIMARY_SLOT,
1513 last_sector_idx);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001514 }
David Vincze2d736ad2019-02-18 11:50:22 +01001515 if ((secondary_slot_size < copy_size) ||
1516 (secondary_slot_size < primary_slot_size)) {
1517 secondary_slot_size += boot_img_sector_size(&boot_data,
1518 BOOT_SECONDARY_SLOT,
1519 last_idx_secondary_slot);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001520 }
David Vincze2d736ad2019-02-18 11:50:22 +01001521 if (primary_slot_size >= copy_size &&
1522 secondary_slot_size >= copy_size &&
1523 primary_slot_size == secondary_slot_size) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001524 break;
1525 }
1526 last_sector_idx++;
David Vincze2d736ad2019-02-18 11:50:22 +01001527 last_idx_secondary_slot++;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001528 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001529
1530 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001531 while (last_sector_idx >= 0) {
1532 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
Fabio Utzig39000012018-07-30 12:40:20 -03001533 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001534 boot_swap_sectors(first_sector_idx, sz, bs);
1535 }
1536
1537 last_sector_idx = first_sector_idx - 1;
1538 swap_idx++;
1539 }
1540
David Vincze2d736ad2019-02-18 11:50:22 +01001541#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001542 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001543 BOOT_LOG_WRN("%d status write fails performing the swap",
1544 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001545 }
1546#endif
1547
Christopher Collins92ea77f2016-12-12 15:59:26 -08001548 return 0;
1549}
David Brown17609d82017-05-05 09:41:34 -06001550#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001551
1552/**
David Vincze2d736ad2019-02-18 11:50:22 +01001553 * Marks the image in the primary slot as fully copied.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001554 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001555#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001556static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001557boot_set_copy_done(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001558{
1559 const struct flash_area *fap;
1560 int rc;
1561
David Vincze2d736ad2019-02-18 11:50:22 +01001562 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001563 if (rc != 0) {
1564 return BOOT_EFLASH;
1565 }
1566
1567 rc = boot_write_copy_done(fap);
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001568 flash_area_close(fap);
1569 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001570}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001571#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001572
1573/**
David Vincze2d736ad2019-02-18 11:50:22 +01001574 * Marks a reverted image in the primary slot as confirmed. This is necessary to
1575 * ensure the status bytes from the image revert operation don't get processed
1576 * on a subsequent boot.
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001577 *
1578 * NOTE: image_ok is tested before writing because if there's a valid permanent
David Vincze2d736ad2019-02-18 11:50:22 +01001579 * image installed on the primary slot and the new image to be upgrade to has a
1580 * bad sig, image_ok would be overwritten.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001581 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001582#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001583static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001584boot_set_image_ok(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001585{
1586 const struct flash_area *fap;
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001587 struct boot_swap_state state;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001588 int rc;
1589
David Vincze2d736ad2019-02-18 11:50:22 +01001590 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001591 if (rc != 0) {
1592 return BOOT_EFLASH;
1593 }
1594
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001595 rc = boot_read_swap_state(fap, &state);
1596 if (rc != 0) {
1597 rc = BOOT_EFLASH;
1598 goto out;
1599 }
1600
1601 if (state.image_ok == BOOT_FLAG_UNSET) {
1602 rc = boot_write_image_ok(fap);
1603 }
1604
1605out:
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001606 flash_area_close(fap);
1607 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001608}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001609#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001610
1611/**
David Vinczeba3bd602019-06-17 16:01:43 +02001612 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001613 *
David Vinczeba3bd602019-06-17 16:01:43 +02001614 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001615 *
1616 * @return 0 on success; nonzero on failure.
1617 */
1618static int
David Vinczeba3bd602019-06-17 16:01:43 +02001619boot_perform_update(struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001620{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001621 int rc;
1622
David Vinczeba3bd602019-06-17 16:01:43 +02001623 /* At this point there are no aborted swaps. */
1624#if defined(MCUBOOT_OVERWRITE_ONLY)
1625 rc = boot_copy_image(bs);
1626#elif defined(MCUBOOT_BOOTSTRAP)
1627 /* Check if the image update was triggered by a bad image in the
1628 * primary slot (the validity of the image in the secondary slot had
1629 * already been checked).
1630 */
1631 if (boot_check_header_erased(BOOT_PRIMARY_SLOT) == 0 ||
1632 boot_validate_slot(BOOT_PRIMARY_SLOT, bs) != 0) {
1633 rc = boot_copy_image(bs);
1634 } else {
1635 rc = boot_swap_image(bs);
1636 }
1637#else
1638 rc = boot_swap_image(bs);
1639#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001640 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001641
1642#ifndef MCUBOOT_OVERWRITE_ONLY
1643 /* The following state needs image_ok be explicitly set after the
1644 * swap was finished to avoid a new revert.
1645 */
1646 if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_REVERT ||
1647 BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_PERM) {
1648 rc = boot_set_image_ok();
1649 if (rc != 0) {
1650 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
1651 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001652 }
1653
David Vinczeba3bd602019-06-17 16:01:43 +02001654 if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_TEST ||
1655 BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_PERM ||
1656 BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_REVERT) {
1657 rc = boot_set_copy_done();
1658 if (rc != 0) {
1659 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001660 }
David Vinczeba3bd602019-06-17 16:01:43 +02001661 }
1662#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001663
David Vinczeba3bd602019-06-17 16:01:43 +02001664 return rc;
1665}
1666
1667/**
1668 * Completes a previously aborted image swap.
1669 *
1670 * @param bs The current boot status.
1671 *
1672 * @return 0 on success; nonzero on failure.
1673 */
1674#if !defined(MCUBOOT_OVERWRITE_ONLY)
1675static int
1676boot_complete_partial_swap(struct boot_status *bs)
1677{
1678 int rc;
1679
1680 /* Determine the type of swap operation being resumed from the
1681 * `swap-type` trailer field.
1682 */
1683 rc = boot_swap_image(bs);
1684 assert(rc == 0);
1685
1686 BOOT_SWAP_TYPE(&boot_data) = bs->swap_type;
1687
1688 /* The following states need image_ok be explicitly set after the
1689 * swap was finished to avoid a new revert.
1690 */
1691 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1692 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
1693 rc = boot_set_image_ok();
1694 if (rc != 0) {
1695 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
1696 }
1697 }
1698
1699 if (bs->swap_type == BOOT_SWAP_TYPE_TEST ||
1700 bs->swap_type == BOOT_SWAP_TYPE_PERM ||
1701 bs->swap_type == BOOT_SWAP_TYPE_REVERT) {
1702 rc = boot_set_copy_done();
1703 if (rc != 0) {
1704 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
1705 }
1706 }
1707
1708 if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_PANIC) {
1709 BOOT_LOG_ERR("panic!");
1710 assert(0);
1711
1712 /* Loop forever... */
1713 while (1) {}
1714 }
1715
1716 return rc;
1717}
1718#endif /* !MCUBOOT_OVERWRITE_ONLY */
1719
1720#if (BOOT_IMAGE_NUMBER > 1)
1721/**
1722 * Review the validity of previously determined swap types of other images.
1723 *
1724 * @param aborted_swap The current image upgrade is a
1725 * partial/aborted swap.
1726 */
1727static void
1728boot_review_image_swap_types(bool aborted_swap)
1729{
1730 /* In that case if we rebooted in the middle of an image upgrade process, we
1731 * must review the validity of swap types, that were previously determined
1732 * for other images. The image_ok flag had not been set before the reboot
1733 * for any of the updated images (only the copy_done flag) and thus falsely
1734 * the REVERT swap type has been determined for the previous images that had
1735 * been updated before the reboot.
1736 *
1737 * There are two separate scenarios that we have to deal with:
1738 *
1739 * 1. The reboot has happened during swapping an image:
1740 * The current image upgrade has been determined as a
1741 * partial/aborted swap.
1742 * 2. The reboot has happened between two separate image upgrades:
1743 * In this scenario we must check the swap type of the current image.
1744 * In those cases if it is NONE or REVERT we cannot certainly determine
1745 * the fact of a reboot. In a consistent state images must move in the
1746 * same direction or stay in place, e.g. in practice REVERT and TEST
1747 * swap types cannot be present at the same time. If the swap type of
1748 * the current image is either TEST, PERM or FAIL we must review the
1749 * already determined swap types of other images and set each false
1750 * REVERT swap types to NONE (these images had been successfully
1751 * updated before the system rebooted between two separate image
1752 * upgrades).
1753 */
1754
1755 if (current_image == 0) {
1756 /* Nothing to do */
1757 return;
1758 }
1759
1760 if (!aborted_swap) {
1761 if ((BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_NONE) ||
1762 (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_REVERT)) {
1763 /* Nothing to do */
1764 return;
1765 }
1766 }
1767
1768 for (uint8_t i = 0; i < current_image; i++) {
1769 if (boot_data.swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
1770 boot_data.swap_type[i] = BOOT_SWAP_TYPE_NONE;
1771 }
1772 }
1773}
1774#endif
1775
1776/**
1777 * Prepare image to be updated if required.
1778 *
1779 * Prepare image to be updated if required with completing an image swap
1780 * operation if one was aborted and/or determining the type of the
1781 * swap operation. In case of any error set the swap type to NONE.
1782 *
1783 * @param bs Pointer where the read and possibly updated
1784 * boot status can be written to.
1785 */
1786static void
1787boot_prepare_image_for_update(struct boot_status *bs)
1788{
1789 int rc;
1790
1791 /* Determine the sector layout of the image slots and scratch area. */
1792 rc = boot_read_sectors();
1793 if (rc != 0) {
1794 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
1795 " - too small?", BOOT_MAX_IMG_SECTORS);
1796 /* Unable to determine sector layout, continue with next image
1797 * if there is one.
1798 */
1799 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE;
1800 return;
1801 }
1802
1803 /* Attempt to read an image header from each slot. */
1804 rc = boot_read_image_headers(false);
1805 if (rc != 0) {
1806 /* Continue with next image if there is one. */
1807 BOOT_LOG_WRN("Failed reading image headers; Image=%u", current_image);
1808 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE;
1809 return;
1810 }
1811
1812 /* If the current image's slots aren't compatible, no swap is possible.
1813 * Just boot into primary slot.
1814 */
1815 if (boot_slots_compatible()) {
1816
1817 rc = boot_read_status(bs);
1818 if (rc != 0) {
1819 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
1820 current_image);
1821 /* Continue with next image if there is one. */
1822 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE;
1823 return;
1824 }
1825
1826 /* Determine if we rebooted in the middle of an image swap
1827 * operation. If a partial swap was detected, complete it.
1828 */
1829 if (bs->idx != BOOT_STATUS_IDX_0 || bs->state != BOOT_STATUS_STATE_0) {
1830
1831#if (BOOT_IMAGE_NUMBER > 1)
1832 boot_review_image_swap_types(true);
1833#endif
1834
1835#ifdef MCUBOOT_OVERWRITE_ONLY
1836 /* Should never arrive here, overwrite-only mode has
1837 * no swap state.
1838 */
1839 assert(0);
1840#else
1841 /* Determine the type of swap operation being resumed from the
1842 * `swap-type` trailer field.
1843 */
1844 rc = boot_complete_partial_swap(bs);
1845 assert(rc == 0);
1846#endif
1847 /* Attempt to read an image header from each slot. Ensure that
1848 * image headers in slots are aligned with headers in boot_data.
1849 */
1850 rc = boot_read_image_headers(false);
1851 assert(rc == 0);
1852
1853 /* Swap has finished set to NONE */
1854 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE;
1855 } else {
1856 /* There was no partial swap, determine swap type. */
1857 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
1858 BOOT_SWAP_TYPE(&boot_data) = boot_validated_swap_type(bs);
1859 } else if (boot_validate_slot(BOOT_SECONDARY_SLOT, bs) != 0) {
1860 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_FAIL;
1861 } else {
1862 BOOT_SWAP_TYPE(&boot_data) = bs->swap_type;
1863 }
1864
1865#if (BOOT_IMAGE_NUMBER > 1)
1866 boot_review_image_swap_types(false);
1867#endif
1868
1869#ifdef MCUBOOT_BOOTSTRAP
1870 if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_NONE) {
1871 /* Header checks are done first because they are
1872 * inexpensive. Since overwrite-only copies starting from
1873 * offset 0, if interrupted, it might leave a valid header
1874 * magic, so also run validation on the primary slot to be
1875 * sure it's not OK.
1876 */
1877 if (boot_check_header_erased(BOOT_PRIMARY_SLOT) == 0 ||
1878 boot_validate_slot(BOOT_PRIMARY_SLOT, bs) != 0) {
1879 if (boot_img_hdr(&boot_data,
1880 BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC &&
1881 boot_validate_slot(BOOT_SECONDARY_SLOT, bs) == 0)
1882 {
1883 /* Set swap type to REVERT to overwrite the primary
1884 * slot with the image contained in secondary slot
1885 * and to trigger the explicit setting of the
1886 * image_ok flag.
1887 */
1888 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_REVERT;
1889 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001890 }
1891 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001892#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001893 }
David Vinczeba3bd602019-06-17 16:01:43 +02001894 } else {
1895 /* In that case if slots are not compatible. */
1896 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001897 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001898}
1899
1900/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08001901 * Prepares the booting process. This function moves images around in flash as
1902 * appropriate, and tells you what address to boot from.
1903 *
1904 * @param rsp On success, indicates how booting should occur.
1905 *
1906 * @return 0 on success; nonzero on failure.
1907 */
1908int
1909boot_go(struct boot_rsp *rsp)
1910{
Marti Bolivar84898652017-06-13 17:20:22 -04001911 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02001912 struct boot_status bs;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001913 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001914 int fa_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001915
1916 /* The array of slot sectors are defined here (as opposed to file scope) so
1917 * that they don't get allocated for non-boot-loader apps. This is
1918 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001919 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001920 */
David Vinczeba3bd602019-06-17 16:01:43 +02001921 static boot_sector_t
1922 primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
1923 static boot_sector_t
1924 secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001925 static boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Christopher Collins92ea77f2016-12-12 15:59:26 -08001926
Fabio Utzigba829042018-09-18 08:29:34 -03001927#ifdef MCUBOOT_ENC_IMAGES
1928 /* FIXME: remove this after RAM is cleared by sim */
1929 boot_enc_zeroize();
1930#endif
1931
David Vinczeba3bd602019-06-17 16:01:43 +02001932 /* Iterate over all the images. By the end of the loop the swap type has
1933 * to be determined for each image and all aborted swaps have to be
1934 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001935 */
David Vinczeba3bd602019-06-17 16:01:43 +02001936 for (current_image = 0; current_image < BOOT_IMAGE_NUMBER; ++current_image)
1937 {
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001938
David Vinczeba3bd602019-06-17 16:01:43 +02001939#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
1940 /* The keys used for encryption may no longer be valid (could belong to
1941 * another images). Therefore, mark them as invalid to force their reload
1942 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001943 */
David Vinczeba3bd602019-06-17 16:01:43 +02001944 boot_enc_mark_keys_invalid();
David Brown554c52e2017-06-30 16:01:07 -06001945#endif
1946
David Vinczeba3bd602019-06-17 16:01:43 +02001947 BOOT_IMG(&boot_data, BOOT_PRIMARY_SLOT).sectors =
1948 primary_slot_sectors[current_image];
1949 BOOT_IMG(&boot_data, BOOT_SECONDARY_SLOT).sectors =
1950 secondary_slot_sectors[current_image];
1951 boot_data.scratch.sectors = scratch_sectors;
1952
1953 /* Open primary and secondary image areas for the duration
1954 * of this call.
1955 */
1956 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1957 fa_id = flash_area_id_from_image_slot(slot);
1958 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot));
1959 assert(rc == 0);
1960 }
1961 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
1962 &BOOT_SCRATCH_AREA(&boot_data));
1963 assert(rc == 0);
1964
1965 /* Determine swap type and complete swap if it has been aborted. */
1966 boot_prepare_image_for_update(&bs);
1967 }
1968
1969 /* Iterate over all the images. At this point there are no aborted swaps
1970 * and the swap types are determined for each image. By the end of the loop
1971 * all required update operations will have been finished.
1972 */
1973 for (current_image = 0; current_image < BOOT_IMAGE_NUMBER; ++current_image)
1974 {
1975
1976#if (BOOT_IMAGE_NUMBER > 1)
1977#ifdef MCUBOOT_ENC_IMAGES
1978 /* The keys used for encryption may no longer be valid (could belong to
1979 * another images). Therefore, mark them as invalid to force their reload
1980 * by boot_enc_load().
1981 */
1982 boot_enc_mark_keys_invalid();
1983#endif /* MCUBOOT_ENC_IMAGES */
1984
1985 /* Indicate that swap is not aborted */
1986 memset(&bs, 0, sizeof bs);
1987 bs.idx = BOOT_STATUS_IDX_0;
1988 bs.state = BOOT_STATUS_STATE_0;
1989#endif /* (BOOT_IMAGE_NUMBER > 1) */
1990
1991 /* Set the previously determined swap type */
1992 bs.swap_type = BOOT_SWAP_TYPE(&boot_data);
1993
1994 switch (BOOT_SWAP_TYPE(&boot_data)) {
1995 case BOOT_SWAP_TYPE_NONE:
1996 break;
1997
1998 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1999 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
2000 case BOOT_SWAP_TYPE_REVERT:
2001 rc = boot_perform_update(&bs);
2002 assert(rc == 0);
2003 break;
2004
2005 case BOOT_SWAP_TYPE_FAIL:
2006 /* The image in secondary slot was invalid and is now erased. Ensure
2007 * we don't try to boot into it again on the next reboot. Do this by
2008 * pretending we just reverted back to primary slot.
2009 */
2010#ifndef MCUBOOT_OVERWRITE_ONLY
2011 /* image_ok needs to be explicitly set to avoid a new revert. */
2012 rc = boot_set_image_ok();
2013 if (rc != 0) {
2014 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
2015 }
2016#endif /* !MCUBOOT_OVERWRITE_ONLY */
2017 break;
2018
2019 default:
2020 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
2021 }
2022
2023 if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_PANIC) {
2024 BOOT_LOG_ERR("panic!");
2025 assert(0);
2026
2027 /* Loop forever... */
2028 while (1) {}
2029 }
2030 }
2031
2032 /* Iterate over all the images. At this point all required update operations
2033 * have finished. By the end of the loop each image in the primary slot will
2034 * have been re-validated.
2035 */
2036 for (current_image = 0; current_image < BOOT_IMAGE_NUMBER; ++current_image)
2037 {
2038 if (BOOT_SWAP_TYPE(&boot_data) != BOOT_SWAP_TYPE_NONE) {
2039 /* Attempt to read an image header from each slot. Ensure that image
2040 * headers in slots are aligned with headers in boot_data.
2041 */
2042 rc = boot_read_image_headers(false);
2043 if (rc != 0) {
2044 goto out;
2045 }
2046 /* Since headers were reloaded, it can be assumed we just performed
2047 * a swap or overwrite. Now the header info that should be used to
2048 * provide the data for the bootstrap, which previously was at
2049 * secondary slot, was updated to primary slot.
2050 */
2051 }
2052
2053#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
2054 rc = boot_validate_slot(BOOT_PRIMARY_SLOT, NULL);
2055 if (rc != 0) {
2056 rc = BOOT_EBADIMAGE;
2057 goto out;
2058 }
2059#else
2060 /* Even if we're not re-validating the primary slot, we could be booting
2061 * onto an empty flash chip. At least do a basic sanity check that
2062 * the magic number on the image is OK.
2063 */
2064 if (BOOT_IMG(&boot_data, BOOT_PRIMARY_SLOT).hdr.ih_magic !=
2065 IMAGE_MAGIC) {
2066 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
2067 &boot_img_hdr(&boot_data,BOOT_PRIMARY_SLOT)->ih_magic,
2068 current_image);
2069 rc = BOOT_EBADIMAGE;
2070 goto out;
2071 }
2072#endif
2073 }
2074
2075 /* Always boot from the primary slot of Image 0. */
2076 current_image = 0;
2077 rsp->br_flash_dev_id =
2078 BOOT_IMG_AREA(&boot_data, BOOT_PRIMARY_SLOT)->fa_device_id;
2079 rsp->br_image_off =
2080 boot_img_slot_off(&boot_data, BOOT_PRIMARY_SLOT);
2081 rsp->br_hdr =
2082 boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002083
Marti Bolivarc0b47912017-06-13 17:18:09 -04002084 out:
David Vinczeba3bd602019-06-17 16:01:43 +02002085 for (current_image = 0; current_image < BOOT_IMAGE_NUMBER; ++current_image)
2086 {
2087 flash_area_close(BOOT_SCRATCH_AREA(&boot_data));
2088 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2089 flash_area_close(BOOT_IMG_AREA(&boot_data,
2090 BOOT_NUM_SLOTS - 1 - slot));
2091 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04002092 }
2093 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002094}
2095
2096int
2097split_go(int loader_slot, int split_slot, void **entry)
2098{
Marti Bolivarc50926f2017-06-14 09:35:40 -04002099 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08002100 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002101 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002102 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002103 int rc;
2104
Christopher Collins92ea77f2016-12-12 15:59:26 -08002105 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
2106 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04002107 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002108 }
David Vinczeba3bd602019-06-17 16:01:43 +02002109 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
2110 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002111
2112 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
2113 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07002114 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04002115 assert(rc == 0);
2116 split_flash_id = flash_area_id_from_image_slot(split_slot);
2117 rc = flash_area_open(split_flash_id,
2118 &BOOT_IMG_AREA(&boot_data, split_slot));
2119 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002120
2121 /* Determine the sector layout of the image slots and scratch area. */
2122 rc = boot_read_sectors();
2123 if (rc != 0) {
2124 rc = SPLIT_GO_ERR;
2125 goto done;
2126 }
2127
Fabio Utzig9c25fa72017-12-12 14:57:20 -02002128 rc = boot_read_image_headers(true);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002129 if (rc != 0) {
2130 goto done;
2131 }
2132
Christopher Collins92ea77f2016-12-12 15:59:26 -08002133 /* Don't check the bootable image flag because we could really call a
2134 * bootable or non-bootable image. Just validate that the image check
2135 * passes which is distinct from the normal check.
2136 */
Marti Bolivarf804f622017-06-12 15:41:48 -04002137 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04002138 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04002139 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04002140 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002141 if (rc != 0) {
2142 rc = SPLIT_GO_NON_MATCHING;
2143 goto done;
2144 }
2145
Marti Bolivarea088872017-06-12 17:10:49 -04002146 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04002147 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08002148 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002149 rc = SPLIT_GO_OK;
2150
2151done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04002152 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
2153 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002154 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002155 return rc;
2156}