blob: c6ec34f5bc3bd6e04fc4fe0ec6628faa85502a24 [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20/**
21 * This file provides an interface to the boot loader. Functions defined in
22 * this file should only be called while the boot loader is running.
23 */
24
25#include <assert.h>
26#include <stddef.h>
David Brown52eee562017-07-05 11:25:09 -060027#include <stdbool.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080028#include <inttypes.h>
29#include <stdlib.h>
30#include <string.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080031#include <os/os_malloc.h>
32#include "bootutil/bootutil.h"
33#include "bootutil/image.h"
34#include "bootutil_priv.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050035#include "bootutil/bootutil_log.h"
36
Fabio Utzigba829042018-09-18 08:29:34 -030037#ifdef MCUBOOT_ENC_IMAGES
38#include "bootutil/enc_key.h"
39#endif
40
Fabio Utzigba1fbe62017-07-21 14:01:20 -030041#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030042
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010043MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
44
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040045static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080046
David Vincze2d736ad2019-02-18 11:50:22 +010047#if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT) && !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utziga0e1cce2017-11-23 20:04:01 -020048static int boot_status_fails = 0;
49#define BOOT_STATUS_ASSERT(x) \
50 do { \
Johann Fischered8461b2018-02-15 16:50:31 +010051 if (!(x)) { \
Fabio Utziga0e1cce2017-11-23 20:04:01 -020052 boot_status_fails++; \
53 } \
54 } while (0)
55#else
Fabio Utzig57c40f72017-12-12 21:48:30 -020056#define BOOT_STATUS_ASSERT(x) ASSERT(x)
Fabio Utziga0e1cce2017-11-23 20:04:01 -020057#endif
58
Christopher Collins92ea77f2016-12-12 15:59:26 -080059struct boot_status_table {
David Vincze2d736ad2019-02-18 11:50:22 +010060 uint8_t bst_magic_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -080061 uint8_t bst_magic_scratch;
David Vincze2d736ad2019-02-18 11:50:22 +010062 uint8_t bst_copy_done_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -080063 uint8_t bst_status_source;
64};
65
66/**
67 * This set of tables maps swap state contents to boot status location.
68 * When searching for a match, these tables must be iterated in order.
69 */
70static const struct boot_status_table boot_status_tables[] = {
71 {
David Vincze2d736ad2019-02-18 11:50:22 +010072 /* | primary slot | scratch |
73 * ----------+--------------+--------------|
74 * magic | Good | Any |
75 * copy-done | Set | N/A |
76 * ----------+--------------+--------------'
77 * source: none |
78 * ----------------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -080079 */
David Vincze2d736ad2019-02-18 11:50:22 +010080 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
Christopher Collinsa1c12042019-05-23 14:00:28 -070081 .bst_magic_scratch = BOOT_MAGIC_NOTGOOD,
David Vincze2d736ad2019-02-18 11:50:22 +010082 .bst_copy_done_primary_slot = BOOT_FLAG_SET,
83 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
Christopher Collins92ea77f2016-12-12 15:59:26 -080084 },
85
86 {
David Vincze2d736ad2019-02-18 11:50:22 +010087 /* | primary slot | scratch |
88 * ----------+--------------+--------------|
89 * magic | Good | Any |
90 * copy-done | Unset | N/A |
91 * ----------+--------------+--------------'
92 * source: primary slot |
93 * ----------------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -080094 */
David Vincze2d736ad2019-02-18 11:50:22 +010095 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
Christopher Collinsa1c12042019-05-23 14:00:28 -070096 .bst_magic_scratch = BOOT_MAGIC_NOTGOOD,
David Vincze2d736ad2019-02-18 11:50:22 +010097 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
98 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Christopher Collins92ea77f2016-12-12 15:59:26 -080099 },
100
101 {
David Vincze2d736ad2019-02-18 11:50:22 +0100102 /* | primary slot | scratch |
103 * ----------+--------------+--------------|
104 * magic | Any | Good |
105 * copy-done | Any | N/A |
106 * ----------+--------------+--------------'
107 * source: scratch |
108 * ----------------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -0800109 */
David Vincze2d736ad2019-02-18 11:50:22 +0100110 .bst_magic_primary_slot = BOOT_MAGIC_ANY,
111 .bst_magic_scratch = BOOT_MAGIC_GOOD,
112 .bst_copy_done_primary_slot = BOOT_FLAG_ANY,
113 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800114 },
Christopher Collins92ea77f2016-12-12 15:59:26 -0800115 {
David Vincze2d736ad2019-02-18 11:50:22 +0100116 /* | primary slot | scratch |
117 * ----------+--------------+--------------|
118 * magic | Unset | Any |
119 * copy-done | Unset | N/A |
120 * ----------+--------------+--------------|
121 * source: varies |
122 * ----------------------------------------+--------------------------+
Christopher Collins92ea77f2016-12-12 15:59:26 -0800123 * This represents one of two cases: |
124 * o No swaps ever (no status to read, so no harm in checking). |
David Vincze2d736ad2019-02-18 11:50:22 +0100125 * o Mid-revert; status in primary slot. |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800126 * -------------------------------------------------------------------'
127 */
David Vincze2d736ad2019-02-18 11:50:22 +0100128 .bst_magic_primary_slot = BOOT_MAGIC_UNSET,
129 .bst_magic_scratch = BOOT_MAGIC_ANY,
130 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
131 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800132 },
133};
134
135#define BOOT_STATUS_TABLES_COUNT \
136 (sizeof boot_status_tables / sizeof boot_status_tables[0])
137
Marti Bolivarfd20c762017-02-07 16:52:50 -0500138#define BOOT_LOG_SWAP_STATE(area, state) \
Christopher Collinsa1c12042019-05-23 14:00:28 -0700139 BOOT_LOG_INF("%s: magic=%s, swap_type=0x%x, copy_done=0x%x, " \
140 "image_ok=0x%x", \
Marti Bolivarfd20c762017-02-07 16:52:50 -0500141 (area), \
142 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
143 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
144 "bad"), \
Christopher Collinsa1c12042019-05-23 14:00:28 -0700145 (state)->swap_type, \
Marti Bolivarfd20c762017-02-07 16:52:50 -0500146 (state)->copy_done, \
147 (state)->image_ok)
148
Christopher Collins92ea77f2016-12-12 15:59:26 -0800149/**
David Vincze2d736ad2019-02-18 11:50:22 +0100150 * Determines where in flash the most recent boot status is stored. The boot
Christopher Collins92ea77f2016-12-12 15:59:26 -0800151 * status is necessary for completing a swap that was interrupted by a boot
152 * loader reset.
153 *
David Vincze2d736ad2019-02-18 11:50:22 +0100154 * @return A BOOT_STATUS_SOURCE_[...] code indicating where status should
155 * be read from.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800156 */
157static int
158boot_status_source(void)
159{
160 const struct boot_status_table *table;
161 struct boot_swap_state state_scratch;
David Vincze2d736ad2019-02-18 11:50:22 +0100162 struct boot_swap_state state_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800163 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200164 size_t i;
Marti Bolivarfd20c762017-02-07 16:52:50 -0500165 uint8_t source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800166
David Vincze2d736ad2019-02-18 11:50:22 +0100167 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY,
168 &state_primary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800169 assert(rc == 0);
170
Fabio Utzig2473ac02017-05-02 12:45:02 -0300171 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800172 assert(rc == 0);
173
David Vincze2d736ad2019-02-18 11:50:22 +0100174 BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot);
Marti Bolivarfd20c762017-02-07 16:52:50 -0500175 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
176
Christopher Collins92ea77f2016-12-12 15:59:26 -0800177 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300178 table = &boot_status_tables[i];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800179
Christopher Collinsa1c12042019-05-23 14:00:28 -0700180 if (boot_magic_compatible_check(table->bst_magic_primary_slot,
181 state_primary_slot.magic) &&
182 boot_magic_compatible_check(table->bst_magic_scratch,
183 state_scratch.magic) &&
David Vincze2d736ad2019-02-18 11:50:22 +0100184 (table->bst_copy_done_primary_slot == BOOT_FLAG_ANY ||
185 table->bst_copy_done_primary_slot == state_primary_slot.copy_done))
186 {
Marti Bolivarfd20c762017-02-07 16:52:50 -0500187 source = table->bst_status_source;
188 BOOT_LOG_INF("Boot source: %s",
189 source == BOOT_STATUS_SOURCE_NONE ? "none" :
190 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
David Vincze2d736ad2019-02-18 11:50:22 +0100191 source == BOOT_STATUS_SOURCE_PRIMARY_SLOT ?
192 "primary slot" : "BUG; can't happen");
Marti Bolivarfd20c762017-02-07 16:52:50 -0500193 return source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800194 }
195 }
196
Marti Bolivarfd20c762017-02-07 16:52:50 -0500197 BOOT_LOG_INF("Boot source: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800198 return BOOT_STATUS_SOURCE_NONE;
199}
200
David Brownf5b33d82017-09-01 10:58:27 -0600201/*
202 * Compute the total size of the given image. Includes the size of
203 * the TLVs.
204 */
Fabio Utzig13d9e352017-10-05 20:32:31 -0300205#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600206static int
207boot_read_image_size(int slot, struct image_header *hdr, uint32_t *size)
208{
209 const struct flash_area *fap;
210 struct image_tlv_info info;
211 int area_id;
212 int rc;
213
214 area_id = flash_area_id_from_image_slot(slot);
215 rc = flash_area_open(area_id, &fap);
216 if (rc != 0) {
217 rc = BOOT_EFLASH;
218 goto done;
219 }
220
221 rc = flash_area_read(fap, hdr->ih_hdr_size + hdr->ih_img_size,
222 &info, sizeof(info));
223 if (rc != 0) {
224 rc = BOOT_EFLASH;
225 goto done;
226 }
227 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
228 rc = BOOT_EBADIMAGE;
229 goto done;
230 }
231 *size = hdr->ih_hdr_size + hdr->ih_img_size + info.it_tlv_tot;
232 rc = 0;
233
234done:
235 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300236 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600237}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300238#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600239
Fabio Utzigc08ed212017-06-20 19:28:36 -0300240static int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800241boot_read_image_header(int slot, struct image_header *out_hdr)
242{
243 const struct flash_area *fap;
244 int area_id;
245 int rc;
246
247 area_id = flash_area_id_from_image_slot(slot);
248 rc = flash_area_open(area_id, &fap);
249 if (rc != 0) {
250 rc = BOOT_EFLASH;
251 goto done;
252 }
253
254 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
255 if (rc != 0) {
256 rc = BOOT_EFLASH;
257 goto done;
258 }
259
260 rc = 0;
261
262done:
263 flash_area_close(fap);
264 return rc;
265}
266
267static int
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200268boot_read_image_headers(bool require_all)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800269{
270 int rc;
271 int i;
272
273 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Marti Bolivarf804f622017-06-12 15:41:48 -0400274 rc = boot_read_image_header(i, boot_img_hdr(&boot_data, i));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800275 if (rc != 0) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200276 /* If `require_all` is set, fail on any single fail, otherwise
277 * if at least the first slot's header was read successfully,
278 * then the boot loader can attempt a boot.
279 *
280 * Failure to read any headers is a fatal error.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800281 */
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200282 if (i > 0 && !require_all) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800283 return 0;
284 } else {
285 return rc;
286 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800287 }
288 }
289
290 return 0;
291}
292
293static uint8_t
294boot_write_sz(void)
295{
296 uint8_t elem_sz;
297 uint8_t align;
298
299 /* Figure out what size to write update status update as. The size depends
300 * on what the minimum write size is for scratch area, active image slot.
301 * We need to use the bigger of those 2 values.
302 */
David Vincze2d736ad2019-02-18 11:50:22 +0100303 elem_sz = flash_area_align(boot_data.imgs[BOOT_PRIMARY_SLOT].area);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200304 align = flash_area_align(boot_data.scratch.area);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800305 if (align > elem_sz) {
306 elem_sz = align;
307 }
308
309 return elem_sz;
310}
311
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200312/*
313 * Slots are compatible when all sectors that store upto to size of the image
314 * round up to sector size, in both slot's are able to fit in the scratch
315 * area, and have sizes that are a multiple of each other (powers of two
316 * presumably!).
317 */
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800318static int
319boot_slots_compatible(void)
320{
David Vincze2d736ad2019-02-18 11:50:22 +0100321 size_t num_sectors_primary;
322 size_t num_sectors_secondary;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200323 size_t sz0, sz1;
David Vincze2d736ad2019-02-18 11:50:22 +0100324 size_t primary_slot_sz, secondary_slot_sz;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200325 size_t scratch_sz;
326 size_t i, j;
327 int8_t smaller;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800328
David Vincze2d736ad2019-02-18 11:50:22 +0100329 num_sectors_primary =
330 boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT);
331 num_sectors_secondary =
332 boot_img_num_sectors(&boot_data, BOOT_SECONDARY_SLOT);
333 if ((num_sectors_primary > BOOT_MAX_IMG_SECTORS) ||
334 (num_sectors_secondary > BOOT_MAX_IMG_SECTORS)) {
Fabio Utziga1fae672018-03-30 10:52:38 -0300335 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800336 return 0;
337 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300338
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200339 scratch_sz = boot_scratch_area_size(&boot_data);
340
341 /*
342 * The following loop scans all sectors in a linear fashion, assuring that
343 * for each possible sector in each slot, it is able to fit in the other
344 * slot's sector or sectors. Slot's should be compatible as long as any
345 * number of a slot's sectors are able to fit into another, which only
346 * excludes cases where sector sizes are not a multiple of each other.
347 */
David Vincze2d736ad2019-02-18 11:50:22 +0100348 i = sz0 = primary_slot_sz = 0;
349 j = sz1 = secondary_slot_sz = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200350 smaller = 0;
David Vincze2d736ad2019-02-18 11:50:22 +0100351 while (i < num_sectors_primary || j < num_sectors_secondary) {
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200352 if (sz0 == sz1) {
David Vincze2d736ad2019-02-18 11:50:22 +0100353 sz0 += boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
354 sz1 += boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT, j);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200355 i++;
356 j++;
357 } else if (sz0 < sz1) {
David Vincze2d736ad2019-02-18 11:50:22 +0100358 sz0 += boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
359 /* Guarantee that multiple sectors of the secondary slot
360 * fit into the primary slot.
361 */
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200362 if (smaller == 2) {
363 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors");
364 return 0;
365 }
366 smaller = 1;
367 i++;
368 } else {
David Vincze2d736ad2019-02-18 11:50:22 +0100369 sz1 += boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT, j);
370 /* Guarantee that multiple sectors of the primary slot
371 * fit into the secondary slot.
372 */
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200373 if (smaller == 1) {
374 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors");
375 return 0;
376 }
377 smaller = 2;
378 j++;
379 }
380 if (sz0 == sz1) {
David Vincze2d736ad2019-02-18 11:50:22 +0100381 primary_slot_sz += sz0;
382 secondary_slot_sz += sz1;
383 /* Scratch has to fit each swap operation to the size of the larger
384 * sector among the primary slot and the secondary slot.
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200385 */
386 if (sz0 > scratch_sz || sz1 > scratch_sz) {
387 BOOT_LOG_WRN("Cannot upgrade: not all sectors fit inside scratch");
388 return 0;
389 }
390 smaller = sz0 = sz1 = 0;
391 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300392 }
393
David Vincze2d736ad2019-02-18 11:50:22 +0100394 if ((i != num_sectors_primary) ||
395 (j != num_sectors_secondary) ||
396 (primary_slot_sz != secondary_slot_sz)) {
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200397 BOOT_LOG_WRN("Cannot upgrade: slots are not compatible");
398 return 0;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800399 }
400
401 return 1;
402}
403
Christopher Collins92ea77f2016-12-12 15:59:26 -0800404/**
405 * Determines the sector layout of both image slots and the scratch area.
406 * This information is necessary for calculating the number of bytes to erase
407 * and copy during an image swap. The information collected during this
408 * function is used to populate the boot_data global.
409 */
410static int
411boot_read_sectors(void)
412{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800413 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800414
David Vincze2d736ad2019-02-18 11:50:22 +0100415 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_PRIMARY);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800416 if (rc != 0) {
417 return BOOT_EFLASH;
418 }
419
David Vincze2d736ad2019-02-18 11:50:22 +0100420 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_SECONDARY);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800421 if (rc != 0) {
422 return BOOT_EFLASH;
423 }
424
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200425 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_SCRATCH);
426 if (rc != 0) {
427 return BOOT_EFLASH;
428 }
429
Marti Bolivare10a7392017-06-14 16:20:07 -0400430 BOOT_WRITE_SZ(&boot_data) = boot_write_sz();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800431
432 return 0;
433}
434
435static uint32_t
436boot_status_internal_off(int idx, int state, int elem_sz)
437{
438 int idx_sz;
439
440 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
441
Fabio Utzig39000012018-07-30 12:40:20 -0300442 return (idx - BOOT_STATUS_IDX_0) * idx_sz +
443 (state - BOOT_STATUS_STATE_0) * elem_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800444}
445
446/**
447 * Reads the status of a partially-completed swap, if any. This is necessary
448 * to recover in case the boot lodaer was reset in the middle of a swap
449 * operation.
450 */
451static int
452boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs)
453{
454 uint32_t off;
455 uint8_t status;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300456 int max_entries;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800457 int found;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200458 int found_idx;
459 int invalid;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800460 int rc;
461 int i;
462
463 off = boot_status_off(fap);
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400464 max_entries = boot_status_entries(fap);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300465
Christopher Collins92ea77f2016-12-12 15:59:26 -0800466 found = 0;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200467 found_idx = 0;
468 invalid = 0;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300469 for (i = 0; i < max_entries; i++) {
Fabio Utzig178be542018-09-19 08:12:56 -0300470 rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(&boot_data),
471 &status, 1);
472 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800473 return BOOT_EFLASH;
474 }
475
Fabio Utzig178be542018-09-19 08:12:56 -0300476 if (rc == 1) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200477 if (found && !found_idx) {
478 found_idx = i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800479 }
480 } else if (!found) {
481 found = 1;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200482 } else if (found_idx) {
483 invalid = 1;
484 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800485 }
486 }
487
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200488 if (invalid) {
489 /* This means there was an error writing status on the last
490 * swap. Tell user and move on to validation!
491 */
492 BOOT_LOG_ERR("Detected inconsistent status!");
493
David Vincze2d736ad2019-02-18 11:50:22 +0100494#if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
495 /* With validation of the primary slot disabled, there is no way
496 * to be sure the swapped primary slot is OK, so abort!
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200497 */
498 assert(0);
499#endif
500 }
501
Christopher Collins92ea77f2016-12-12 15:59:26 -0800502 if (found) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200503 if (!found_idx) {
504 found_idx = i;
505 }
506 found_idx--;
Fabio Utzig39000012018-07-30 12:40:20 -0300507 bs->idx = (found_idx / BOOT_STATUS_STATE_COUNT) + 1;
508 bs->state = (found_idx % BOOT_STATUS_STATE_COUNT) + 1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800509 }
510
511 return 0;
512}
513
514/**
515 * Reads the boot status from the flash. The boot status contains
516 * the current state of an interrupted image copy operation. If the boot
517 * status is not present, or it indicates that previous copy finished,
518 * there is no operation in progress.
519 */
520static int
521boot_read_status(struct boot_status *bs)
522{
523 const struct flash_area *fap;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700524 uint32_t off;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800525 int status_loc;
526 int area_id;
527 int rc;
528
529 memset(bs, 0, sizeof *bs);
Fabio Utzig39000012018-07-30 12:40:20 -0300530 bs->idx = BOOT_STATUS_IDX_0;
531 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700532 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800533
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700534#ifdef MCUBOOT_OVERWRITE_ONLY
535 /* Overwrite-only doesn't make use of the swap status area. */
536 return 0;
537#endif
538
Christopher Collins92ea77f2016-12-12 15:59:26 -0800539 status_loc = boot_status_source();
540 switch (status_loc) {
541 case BOOT_STATUS_SOURCE_NONE:
542 return 0;
543
544 case BOOT_STATUS_SOURCE_SCRATCH:
545 area_id = FLASH_AREA_IMAGE_SCRATCH;
546 break;
547
David Vincze2d736ad2019-02-18 11:50:22 +0100548 case BOOT_STATUS_SOURCE_PRIMARY_SLOT:
549 area_id = FLASH_AREA_IMAGE_PRIMARY;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800550 break;
551
552 default:
553 assert(0);
554 return BOOT_EBADARGS;
555 }
556
557 rc = flash_area_open(area_id, &fap);
558 if (rc != 0) {
559 return BOOT_EFLASH;
560 }
561
Fabio Utzig46490722017-09-04 15:34:32 -0300562 rc = boot_read_status_bytes(fap, bs);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700563 if (rc == 0) {
564 off = boot_swap_type_off(fap);
565 rc = flash_area_read_is_empty(fap, off, &bs->swap_type,
566 sizeof bs->swap_type);
567 if (rc == 1) {
568 bs->swap_type = BOOT_SWAP_TYPE_NONE;
569 rc = 0;
570 }
571 }
Fabio Utzig46490722017-09-04 15:34:32 -0300572
573 flash_area_close(fap);
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700574
Fabio Utzig46490722017-09-04 15:34:32 -0300575 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800576}
577
578/**
579 * Writes the supplied boot status to the flash file system. The boot status
580 * contains the current state of an in-progress image copy operation.
581 *
582 * @param bs The boot status to write.
583 *
584 * @return 0 on success; nonzero on failure.
585 */
586int
587boot_write_status(struct boot_status *bs)
588{
589 const struct flash_area *fap;
590 uint32_t off;
591 int area_id;
592 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300593 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700594 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300595 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800596
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300597 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100598 * the trailer. Since in the last step the primary slot is erased, the
599 * first two status writes go to the scratch which will be copied to
600 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300601 */
602
Fabio Utzig2473ac02017-05-02 12:45:02 -0300603 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800604 /* Write to scratch. */
605 area_id = FLASH_AREA_IMAGE_SCRATCH;
606 } else {
David Vincze2d736ad2019-02-18 11:50:22 +0100607 /* Write to the primary slot. */
608 area_id = FLASH_AREA_IMAGE_PRIMARY;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800609 }
610
611 rc = flash_area_open(area_id, &fap);
612 if (rc != 0) {
613 rc = BOOT_EFLASH;
614 goto done;
615 }
616
617 off = boot_status_off(fap) +
Marti Bolivare10a7392017-06-14 16:20:07 -0400618 boot_status_internal_off(bs->idx, bs->state,
619 BOOT_WRITE_SZ(&boot_data));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200620 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300621 erased_val = flash_area_erased_val(fap);
622 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700623 buf[0] = bs->state;
624
625 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800626 if (rc != 0) {
627 rc = BOOT_EFLASH;
628 goto done;
629 }
630
631 rc = 0;
632
633done:
634 flash_area_close(fap);
635 return rc;
636}
637
638/*
639 * Validate image hash/signature in a slot.
640 */
641static int
Fabio Utzigba829042018-09-18 08:29:34 -0300642boot_image_check(struct image_header *hdr, const struct flash_area *fap,
643 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800644{
David Browndb1d9d32017-01-06 11:07:54 -0700645 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigba829042018-09-18 08:29:34 -0300646 int rc;
647
648#ifndef MCUBOOT_ENC_IMAGES
649 (void)bs;
650 (void)rc;
651#else
David Vincze2d736ad2019-02-18 11:50:22 +0100652 if ((fap->fa_id == FLASH_AREA_IMAGE_SECONDARY) && IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300653 rc = boot_enc_load(hdr, fap, bs->enckey[1]);
654 if (rc < 0) {
655 return BOOT_EBADIMAGE;
656 }
657 if (rc == 0 && boot_enc_set_key(1, bs->enckey[1])) {
658 return BOOT_EBADIMAGE;
659 }
660 }
661#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800662
Christopher Collins92ea77f2016-12-12 15:59:26 -0800663 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
664 NULL, 0, NULL)) {
665 return BOOT_EBADIMAGE;
666 }
667 return 0;
668}
669
670static int
671split_image_check(struct image_header *app_hdr,
672 const struct flash_area *app_fap,
673 struct image_header *loader_hdr,
674 const struct flash_area *loader_fap)
675{
676 static void *tmpbuf;
677 uint8_t loader_hash[32];
678
679 if (!tmpbuf) {
680 tmpbuf = malloc(BOOT_TMPBUF_SZ);
681 if (!tmpbuf) {
682 return BOOT_ENOMEM;
683 }
684 }
685
686 if (bootutil_img_validate(loader_hdr, loader_fap, tmpbuf, BOOT_TMPBUF_SZ,
687 NULL, 0, loader_hash)) {
688 return BOOT_EBADIMAGE;
689 }
690
691 if (bootutil_img_validate(app_hdr, app_fap, tmpbuf, BOOT_TMPBUF_SZ,
692 loader_hash, 32, NULL)) {
693 return BOOT_EBADIMAGE;
694 }
695
696 return 0;
697}
698
Fabio Utzig338a19f2018-12-03 08:37:08 -0200699/*
700 * Check that a memory area consists of a given value.
701 */
702static inline bool
703boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300704{
705 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200706 uint8_t *p = (uint8_t *)data;
707 for (i = 0; i < len; i++) {
708 if (val != p[i]) {
709 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300710 }
711 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200712 return true;
713}
714
715static int
716boot_check_header_erased(int slot)
717{
718 const struct flash_area *fap;
719 struct image_header *hdr;
720 uint8_t erased_val;
721 int rc;
722
723 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
724 if (rc != 0) {
725 return -1;
726 }
727
728 erased_val = flash_area_erased_val(fap);
729 flash_area_close(fap);
730
731 hdr = boot_img_hdr(&boot_data, slot);
732 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
733 return -1;
734 }
735
736 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300737}
738
Christopher Collins92ea77f2016-12-12 15:59:26 -0800739static int
Fabio Utzigba829042018-09-18 08:29:34 -0300740boot_validate_slot(int slot, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800741{
742 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400743 struct image_header *hdr;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800744 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300745
David Brownd930ec62016-12-14 07:59:48 -0700746 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800747 if (rc != 0) {
748 return BOOT_EFLASH;
749 }
750
Fabio Utzig39000012018-07-30 12:40:20 -0300751 hdr = boot_img_hdr(&boot_data, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200752 if (boot_check_header_erased(slot) == 0 || (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100753 /* No bootable image in slot; continue booting from the primary slot. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200754 rc = -1;
755 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300756 }
757
Fabio Utzigba829042018-09-18 08:29:34 -0300758 if ((hdr->ih_magic != IMAGE_MAGIC || boot_image_check(hdr, fap, bs) != 0)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100759 if (slot != BOOT_PRIMARY_SLOT) {
David Brownb38e0442017-02-24 13:57:12 -0700760 flash_area_erase(fap, 0, fap->fa_size);
David Vincze2d736ad2019-02-18 11:50:22 +0100761 /* Image in the secondary slot is invalid. Erase the image and
762 * continue booting from the primary slot.
David Brownb38e0442017-02-24 13:57:12 -0700763 */
764 }
David Vincze2d736ad2019-02-18 11:50:22 +0100765 BOOT_LOG_ERR("Image in the %s slot is not valid!",
766 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
Fabio Utzig338a19f2018-12-03 08:37:08 -0200767 rc = -1;
768 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800769 }
770
David Vincze2d736ad2019-02-18 11:50:22 +0100771 /* Image in the secondary slot is valid. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200772 rc = 0;
773
774out:
775 flash_area_close(fap);
776 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800777}
778
779/**
780 * Determines which swap operation to perform, if any. If it is determined
David Vincze2d736ad2019-02-18 11:50:22 +0100781 * that a swap operation is required, the image in the secondary slot is checked
782 * for validity. If the image in the secondary slot is invalid, it is erased,
783 * and a swap type of "none" is indicated.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800784 *
785 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
786 */
787static int
Fabio Utzigba829042018-09-18 08:29:34 -0300788boot_validated_swap_type(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800789{
790 int swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800791
792 swap_type = boot_swap_type();
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300793 switch (swap_type) {
794 case BOOT_SWAP_TYPE_TEST:
795 case BOOT_SWAP_TYPE_PERM:
796 case BOOT_SWAP_TYPE_REVERT:
David Vincze2d736ad2019-02-18 11:50:22 +0100797 /* Boot loader wants to switch to the secondary slot.
798 * Ensure image is valid.
799 */
800 if (boot_validate_slot(BOOT_SECONDARY_SLOT, bs) != 0) {
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300801 swap_type = BOOT_SWAP_TYPE_FAIL;
802 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800803 }
804
805 return swap_type;
806}
807
808/**
809 * Calculates the number of sectors the scratch area can contain. A "last"
810 * source sector is specified because images are copied backwards in flash
811 * (final index to index number 0).
812 *
813 * @param last_sector_idx The index of the last source sector
814 * (inclusive).
815 * @param out_first_sector_idx The index of the first source sector
816 * (inclusive) gets written here.
817 *
818 * @return The number of bytes comprised by the
819 * [first-sector, last-sector] range.
820 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300821#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800822static uint32_t
823boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
824{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400825 size_t scratch_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800826 uint32_t new_sz;
827 uint32_t sz;
828 int i;
829
830 sz = 0;
831
Marti Bolivard3269fd2017-06-12 16:31:12 -0400832 scratch_sz = boot_scratch_area_size(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800833 for (i = last_sector_idx; i >= 0; i--) {
David Vincze2d736ad2019-02-18 11:50:22 +0100834 new_sz = sz + boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200835 /*
David Vincze2d736ad2019-02-18 11:50:22 +0100836 * The secondary slot is not being checked here, because
837 * `boot_slots_compatible` already provides assurance that the copy size
838 * will be compatible with the primary slot and scratch.
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200839 */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400840 if (new_sz > scratch_sz) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800841 break;
842 }
843 sz = new_sz;
844 }
845
846 /* i currently refers to a sector that doesn't fit or it is -1 because all
847 * sectors have been processed. In both cases, exclude sector i.
848 */
849 *out_first_sector_idx = i + 1;
850 return sz;
851}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300852#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800853
854/**
855 * Erases a region of flash.
856 *
Fabio Utzigba829042018-09-18 08:29:34 -0300857 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800858 * @param off The offset within the flash area to start the
859 * erase.
860 * @param sz The number of bytes to erase.
861 *
862 * @return 0 on success; nonzero on failure.
863 */
Fabio Utzigba829042018-09-18 08:29:34 -0300864static inline int
865boot_erase_sector(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800866{
Fabio Utzigba829042018-09-18 08:29:34 -0300867 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800868}
869
870/**
871 * Copies the contents of one flash region to another. You must erase the
872 * destination region prior to calling this function.
873 *
874 * @param flash_area_id_src The ID of the source flash area.
875 * @param flash_area_id_dst The ID of the destination flash area.
876 * @param off_src The offset within the source flash area to
877 * copy from.
878 * @param off_dst The offset within the destination flash area to
879 * copy to.
880 * @param sz The number of bytes to copy.
881 *
882 * @return 0 on success; nonzero on failure.
883 */
884static int
Fabio Utzigba829042018-09-18 08:29:34 -0300885boot_copy_sector(const struct flash_area *fap_src,
886 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800887 uint32_t off_src, uint32_t off_dst, uint32_t sz)
888{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800889 uint32_t bytes_copied;
890 int chunk_sz;
891 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -0300892#ifdef MCUBOOT_ENC_IMAGES
893 uint32_t off;
894 size_t blk_off;
895 struct image_header *hdr;
896 uint16_t idx;
897 uint32_t blk_sz;
898#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800899
900 static uint8_t buf[1024];
901
Christopher Collins92ea77f2016-12-12 15:59:26 -0800902 bytes_copied = 0;
903 while (bytes_copied < sz) {
904 if (sz - bytes_copied > sizeof buf) {
905 chunk_sz = sizeof buf;
906 } else {
907 chunk_sz = sz - bytes_copied;
908 }
909
910 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
911 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300912 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800913 }
914
Fabio Utzigba829042018-09-18 08:29:34 -0300915#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +0100916 if ((fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY) ||
917 (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY)) {
918 /* assume the secondary slot as src, needs decryption */
919 hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -0300920 off = off_src;
David Vincze2d736ad2019-02-18 11:50:22 +0100921 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY) {
922 /* might need encryption (metadata from the primary slot) */
923 hdr = boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -0300924 off = off_dst;
925 }
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200926 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300927 blk_sz = chunk_sz;
928 idx = 0;
929 if (off + bytes_copied < hdr->ih_hdr_size) {
930 /* do not decrypt header */
931 blk_off = 0;
932 blk_sz = chunk_sz - hdr->ih_hdr_size;
933 idx = hdr->ih_hdr_size;
934 } else {
935 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
936 }
937 if (off + bytes_copied + chunk_sz > hdr->ih_hdr_size + hdr->ih_img_size) {
938 /* do not decrypt TLVs */
939 if (off + bytes_copied >= hdr->ih_hdr_size + hdr->ih_img_size) {
940 blk_sz = 0;
941 } else {
942 blk_sz = (hdr->ih_hdr_size + hdr->ih_img_size) - (off + bytes_copied);
943 }
944 }
945 boot_encrypt(fap_src, (off + bytes_copied + idx) - hdr->ih_hdr_size,
946 blk_sz, blk_off, &buf[idx]);
947 }
948 }
949#endif
950
Christopher Collins92ea77f2016-12-12 15:59:26 -0800951 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
952 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300953 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800954 }
955
956 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -0300957
958 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800959 }
960
Fabio Utzigba829042018-09-18 08:29:34 -0300961 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800962}
963
David Brown6b1b3b92017-09-19 08:59:10 -0600964#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -0300965static inline int
Fabio Utzigba829042018-09-18 08:29:34 -0300966boot_status_init(const struct flash_area *fap, const struct boot_status *bs)
Fabio Utzig2473ac02017-05-02 12:45:02 -0300967{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300968 struct boot_swap_state swap_state;
969 int rc;
970
Christopher Collins2c88e692019-05-22 15:10:14 -0700971 BOOT_LOG_DBG("initializing status; fa_id=%d", fap->fa_id);
972
David Vincze2d736ad2019-02-18 11:50:22 +0100973 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY, &swap_state);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300974 assert(rc == 0);
975
Christopher Collinsa1c12042019-05-23 14:00:28 -0700976 if (bs->swap_type != BOOT_SWAP_TYPE_NONE) {
977 rc = boot_write_swap_type(fap, bs->swap_type);
978 assert(rc == 0);
979 }
980
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400981 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300982 rc = boot_write_image_ok(fap);
983 assert(rc == 0);
984 }
985
Fabio Utzig46490722017-09-04 15:34:32 -0300986 rc = boot_write_swap_size(fap, bs->swap_size);
987 assert(rc == 0);
988
Fabio Utzigba829042018-09-18 08:29:34 -0300989#ifdef MCUBOOT_ENC_IMAGES
990 rc = boot_write_enc_key(fap, 0, bs->enckey[0]);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300991 assert(rc == 0);
992
Fabio Utzigba829042018-09-18 08:29:34 -0300993 rc = boot_write_enc_key(fap, 1, bs->enckey[1]);
994 assert(rc == 0);
995#endif
996
997 rc = boot_write_magic(fap);
998 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300999
1000 return 0;
1001}
Christopher Collinsa1c12042019-05-23 14:00:28 -07001002
David Brown6b1b3b92017-09-19 08:59:10 -06001003#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001004
Fabio Utzig358c9352017-07-25 22:10:45 -03001005#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -03001006static int
Fabio Utziged0ca432019-01-23 14:50:11 -02001007boot_erase_trailer_sectors(const struct flash_area *fap)
Fabio Utzig2473ac02017-05-02 12:45:02 -03001008{
1009 uint8_t slot;
Fabio Utziged0ca432019-01-23 14:50:11 -02001010 uint32_t sector;
1011 uint32_t trailer_sz;
1012 uint32_t total_sz;
1013 uint32_t off;
1014 uint32_t sz;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001015 int rc;
1016
Christopher Collins2c88e692019-05-22 15:10:14 -07001017 BOOT_LOG_DBG("erasing trailer; fa_id=%d", fap->fa_id);
1018
Fabio Utzigba829042018-09-18 08:29:34 -03001019 switch (fap->fa_id) {
David Vincze2d736ad2019-02-18 11:50:22 +01001020 case FLASH_AREA_IMAGE_PRIMARY:
1021 slot = BOOT_PRIMARY_SLOT;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001022 break;
David Vincze2d736ad2019-02-18 11:50:22 +01001023 case FLASH_AREA_IMAGE_SECONDARY:
1024 slot = BOOT_SECONDARY_SLOT;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001025 break;
1026 default:
1027 return BOOT_EFLASH;
1028 }
1029
Fabio Utziged0ca432019-01-23 14:50:11 -02001030 /* delete starting from last sector and moving to beginning */
1031 sector = boot_img_num_sectors(&boot_data, slot) - 1;
Christopher Collins2adef702019-05-22 14:37:31 -07001032 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Fabio Utziged0ca432019-01-23 14:50:11 -02001033 total_sz = 0;
1034 do {
1035 sz = boot_img_sector_size(&boot_data, slot, sector);
1036 off = boot_img_sector_off(&boot_data, slot, sector);
1037 rc = boot_erase_sector(fap, off, sz);
1038 assert(rc == 0);
1039
1040 sector--;
1041 total_sz += sz;
1042 } while (total_sz < trailer_sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001043
1044 return rc;
1045}
Fabio Utzig358c9352017-07-25 22:10:45 -03001046#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001047
Christopher Collins92ea77f2016-12-12 15:59:26 -08001048/**
1049 * Swaps the contents of two flash regions within the two image slots.
1050 *
1051 * @param idx The index of the first sector in the range of
1052 * sectors being swapped.
1053 * @param sz The number of bytes to swap.
1054 * @param bs The current boot status. This struct gets
1055 * updated according to the outcome.
1056 *
1057 * @return 0 on success; nonzero on failure.
1058 */
Fabio Utzig3488eef2017-06-12 10:25:43 -03001059#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -08001060static void
Christopher Collins92ea77f2016-12-12 15:59:26 -08001061boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
1062{
David Vincze2d736ad2019-02-18 11:50:22 +01001063 const struct flash_area *fap_primary_slot;
1064 const struct flash_area *fap_secondary_slot;
Fabio Utzigba829042018-09-18 08:29:34 -03001065 const struct flash_area *fap_scratch;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001066 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001067 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001068 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001069 uint32_t scratch_trailer_off;
1070 struct boot_swap_state swap_state;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001071 size_t last_sector;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001072 bool erase_scratch;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001073 int rc;
1074
1075 /* Calculate offset from start of image area. */
David Vincze2d736ad2019-02-18 11:50:22 +01001076 img_off = boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT, idx);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001077
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001078 copy_sz = sz;
Christopher Collins2adef702019-05-22 14:37:31 -07001079 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Fabio Utzig9678c972017-05-23 11:28:56 -04001080
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001081 /* sz in this function is always sized on a multiple of the sector size.
1082 * The check against the start offset of the last sector
Fabio Utzig9678c972017-05-23 11:28:56 -04001083 * is to determine if we're swapping the last sector. The last sector
1084 * needs special handling because it's where the trailer lives. If we're
1085 * copying it, we need to use scratch to write the trailer temporarily.
1086 *
1087 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
1088 * controls if special handling is needed (swapping last sector).
1089 */
David Vincze2d736ad2019-02-18 11:50:22 +01001090 last_sector = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT) - 1;
1091 if (img_off + sz > boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT,
1092 last_sector)) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001093 copy_sz -= trailer_sz;
1094 }
1095
Fabio Utzig39000012018-07-30 12:40:20 -03001096 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001097
David Vincze2d736ad2019-02-18 11:50:22 +01001098 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001099 assert (rc == 0);
1100
David Vincze2d736ad2019-02-18 11:50:22 +01001101 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001102 assert (rc == 0);
1103
1104 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch);
1105 assert (rc == 0);
1106
Fabio Utzig39000012018-07-30 12:40:20 -03001107 if (bs->state == BOOT_STATUS_STATE_0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001108 BOOT_LOG_DBG("erasing scratch area");
Christopher Collinsa1c12042019-05-23 14:00:28 -07001109 rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size);
Christopher Collins4772ac42017-02-27 20:08:01 -08001110 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001111
Fabio Utzig39000012018-07-30 12:40:20 -03001112 if (bs->idx == BOOT_STATUS_IDX_0) {
Christopher Collinsa1c12042019-05-23 14:00:28 -07001113 /* Write a trailer to the scratch area, even if we don't need the
1114 * scratch area for status. We need a temporary place to store the
1115 * `swap-type` while we erase the primary trailer.
1116 */
1117 rc = boot_status_init(fap_scratch, bs);
1118 assert(rc == 0);
1119
1120 if (!bs->use_scratch) {
1121 /* Prepare the primary status area... here it is known that the
1122 * last sector is not being used by the image data so it's safe
1123 * to erase.
Fabio Utzig2473ac02017-05-02 12:45:02 -03001124 */
David Vincze2d736ad2019-02-18 11:50:22 +01001125 rc = boot_erase_trailer_sectors(fap_primary_slot);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001126 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001127
Christopher Collinsa1c12042019-05-23 14:00:28 -07001128 rc = boot_status_init(fap_primary_slot, bs);
1129 assert(rc == 0);
1130
1131 /* Erase the temporary trailer from the scratch area. */
1132 rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size);
1133 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001134 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001135 }
1136
Christopher Collinsa1c12042019-05-23 14:00:28 -07001137 rc = boot_copy_sector(fap_secondary_slot, fap_scratch,
1138 img_off, 0, copy_sz);
1139 assert(rc == 0);
1140
Fabio Utzig39000012018-07-30 12:40:20 -03001141 bs->state = BOOT_STATUS_STATE_1;
Christopher Collins4772ac42017-02-27 20:08:01 -08001142 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001143 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001144 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001145
Fabio Utzig39000012018-07-30 12:40:20 -03001146 if (bs->state == BOOT_STATUS_STATE_1) {
David Vincze2d736ad2019-02-18 11:50:22 +01001147 rc = boot_erase_sector(fap_secondary_slot, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001148 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001149
David Vincze2d736ad2019-02-18 11:50:22 +01001150 rc = boot_copy_sector(fap_primary_slot, fap_secondary_slot,
1151 img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001152 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001153
Fabio Utzig39000012018-07-30 12:40:20 -03001154 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001155 /* If not all sectors of the slot are being swapped,
David Vincze2d736ad2019-02-18 11:50:22 +01001156 * guarantee here that only the primary slot will have the state.
Fabio Utzig2473ac02017-05-02 12:45:02 -03001157 */
David Vincze2d736ad2019-02-18 11:50:22 +01001158 rc = boot_erase_trailer_sectors(fap_secondary_slot);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001159 assert(rc == 0);
1160 }
1161
Fabio Utzig39000012018-07-30 12:40:20 -03001162 bs->state = BOOT_STATUS_STATE_2;
Christopher Collins4772ac42017-02-27 20:08:01 -08001163 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001164 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001165 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001166
Fabio Utzig39000012018-07-30 12:40:20 -03001167 if (bs->state == BOOT_STATUS_STATE_2) {
David Vincze2d736ad2019-02-18 11:50:22 +01001168 rc = boot_erase_sector(fap_primary_slot, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001169 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001170
Christopher Collinsa1c12042019-05-23 14:00:28 -07001171 /* NOTE: If this is the final sector, we exclude the image trailer from
1172 * this copy (copy_sz was truncated earlier).
1173 */
David Vincze2d736ad2019-02-18 11:50:22 +01001174 rc = boot_copy_sector(fap_scratch, fap_primary_slot,
1175 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001176 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001177
Fabio Utzig94d998c2017-05-22 11:02:41 -04001178 if (bs->use_scratch) {
Fabio Utzigba829042018-09-18 08:29:34 -03001179 scratch_trailer_off = boot_status_off(fap_scratch);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001180
1181 /* copy current status that is being maintained in scratch */
David Vincze2d736ad2019-02-18 11:50:22 +01001182 rc = boot_copy_sector(fap_scratch, fap_primary_slot,
1183 scratch_trailer_off, img_off + copy_sz,
Marti Bolivare10a7392017-06-14 16:20:07 -04001184 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data));
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001185 BOOT_STATUS_ASSERT(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001186
Fabio Utzig2473ac02017-05-02 12:45:02 -03001187 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1188 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001189 assert(rc == 0);
1190
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001191 if (swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze2d736ad2019-02-18 11:50:22 +01001192 rc = boot_write_image_ok(fap_primary_slot);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001193 assert(rc == 0);
1194 }
1195
Christopher Collinsa1c12042019-05-23 14:00:28 -07001196 if (swap_state.swap_type != BOOT_SWAP_TYPE_NONE) {
1197 rc = boot_write_swap_type(fap_primary_slot,
1198 swap_state.swap_type);
1199 assert(rc == 0);
1200 }
1201
David Vincze2d736ad2019-02-18 11:50:22 +01001202 rc = boot_write_swap_size(fap_primary_slot, bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001203 assert(rc == 0);
1204
Fabio Utzigba829042018-09-18 08:29:34 -03001205#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +01001206 rc = boot_write_enc_key(fap_primary_slot, 0, bs->enckey[0]);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001207 assert(rc == 0);
1208
David Vincze2d736ad2019-02-18 11:50:22 +01001209 rc = boot_write_enc_key(fap_primary_slot, 1, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001210 assert(rc == 0);
1211#endif
David Vincze2d736ad2019-02-18 11:50:22 +01001212 rc = boot_write_magic(fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001213 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001214 }
1215
Christopher Collinsa1c12042019-05-23 14:00:28 -07001216 /* If we wrote a trailer to the scratch area, erase it after we persist
1217 * a trailer to the primary slot. We do this to prevent mcuboot from
1218 * reading a stale status from the scratch area in case of immediate
1219 * reset.
1220 */
1221 erase_scratch = bs->use_scratch;
1222 bs->use_scratch = 0;
1223
Christopher Collins92ea77f2016-12-12 15:59:26 -08001224 bs->idx++;
Fabio Utzig39000012018-07-30 12:40:20 -03001225 bs->state = BOOT_STATUS_STATE_0;
Christopher Collins4772ac42017-02-27 20:08:01 -08001226 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001227 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001228
1229 if (erase_scratch) {
1230 rc = boot_erase_sector(fap_scratch, 0, sz);
1231 assert(rc == 0);
1232 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001233 }
Fabio Utzigba829042018-09-18 08:29:34 -03001234
David Vincze2d736ad2019-02-18 11:50:22 +01001235 flash_area_close(fap_primary_slot);
1236 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001237 flash_area_close(fap_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001238}
Fabio Utzig3488eef2017-06-12 10:25:43 -03001239#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001240
1241/**
David Vincze2d736ad2019-02-18 11:50:22 +01001242 * Overwrite primary slot with the image contained in the secondary slot.
1243 * If a prior copy operation was interrupted by a system reset, this function
1244 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001245 *
1246 * @param bs The current boot status. This function reads
1247 * this struct to determine if it is resuming
1248 * an interrupted swap operation. This
1249 * function writes the updated status to this
1250 * function on return.
1251 *
1252 * @return 0 on success; nonzero on failure.
1253 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001254#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001255static int
1256boot_copy_image(struct boot_status *bs)
1257{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001258 size_t sect_count;
1259 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001260 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001261 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001262 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001263 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +01001264 const struct flash_area *fap_primary_slot;
1265 const struct flash_area *fap_secondary_slot;
1266
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001267 (void)bs;
1268
Fabio Utzig13d9e352017-10-05 20:32:31 -03001269#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1270 uint32_t src_size = 0;
David Vincze2d736ad2019-02-18 11:50:22 +01001271 rc = boot_read_image_size(BOOT_SECONDARY_SLOT,
1272 boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT),
1273 &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001274 assert(rc == 0);
1275#endif
David Brown17609d82017-05-05 09:41:34 -06001276
David Vincze2d736ad2019-02-18 11:50:22 +01001277 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1278 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -06001279
David Vincze2d736ad2019-02-18 11:50:22 +01001280 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001281 assert (rc == 0);
1282
David Vincze2d736ad2019-02-18 11:50:22 +01001283 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001284 assert (rc == 0);
1285
David Vincze2d736ad2019-02-18 11:50:22 +01001286 sect_count = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001287 for (sect = 0, size = 0; sect < sect_count; sect++) {
David Vincze2d736ad2019-02-18 11:50:22 +01001288 this_size = boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, sect);
1289 rc = boot_erase_sector(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001290 assert(rc == 0);
1291
1292 size += this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001293
1294#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1295 if (size >= src_size) {
1296 break;
1297 }
1298#endif
David Brown17609d82017-05-05 09:41:34 -06001299 }
1300
Fabio Utzigba829042018-09-18 08:29:34 -03001301#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +01001302 if (IS_ENCRYPTED(boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT))) {
1303 rc = boot_enc_load(boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT),
1304 fap_secondary_slot,
1305 bs->enckey[1]);
1306
Fabio Utzigba829042018-09-18 08:29:34 -03001307 if (rc < 0) {
1308 return BOOT_EBADIMAGE;
1309 }
Fabio Utzige641ea52018-12-03 10:37:53 -02001310 if (rc == 0 && boot_enc_set_key(1, bs->enckey[1])) {
Fabio Utzigba829042018-09-18 08:29:34 -03001311 return BOOT_EBADIMAGE;
1312 }
1313 }
1314#endif
1315
David Vincze2d736ad2019-02-18 11:50:22 +01001316 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1317 size);
1318 rc = boot_copy_sector(fap_secondary_slot, fap_primary_slot, 0, 0, size);
David Brown17609d82017-05-05 09:41:34 -06001319
Fabio Utzig13d9e352017-10-05 20:32:31 -03001320 /*
1321 * Erases header and trailer. The trailer is erased because when a new
1322 * image is written without a trailer as is the case when using newt, the
1323 * trailer that was left might trigger a new upgrade.
1324 */
Christopher Collins2c88e692019-05-22 15:10:14 -07001325 BOOT_LOG_DBG("erasing secondary header");
David Vincze2d736ad2019-02-18 11:50:22 +01001326 rc = boot_erase_sector(fap_secondary_slot,
1327 boot_img_sector_off(&boot_data,
1328 BOOT_SECONDARY_SLOT, 0),
1329 boot_img_sector_size(&boot_data,
1330 BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001331 assert(rc == 0);
David Vincze2d736ad2019-02-18 11:50:22 +01001332 last_sector = boot_img_num_sectors(&boot_data, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -07001333 BOOT_LOG_DBG("erasing secondary trailer");
David Vincze2d736ad2019-02-18 11:50:22 +01001334 rc = boot_erase_sector(fap_secondary_slot,
1335 boot_img_sector_off(&boot_data,
1336 BOOT_SECONDARY_SLOT, last_sector),
1337 boot_img_sector_size(&boot_data,
1338 BOOT_SECONDARY_SLOT, last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001339 assert(rc == 0);
1340
David Vincze2d736ad2019-02-18 11:50:22 +01001341 flash_area_close(fap_primary_slot);
1342 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001343
David Vincze2d736ad2019-02-18 11:50:22 +01001344 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001345
1346 return 0;
1347}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001348#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001349
Christopher Collinsa1c12042019-05-23 14:00:28 -07001350#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -03001351/**
1352 * Swaps the two images in flash. If a prior copy operation was interrupted
1353 * by a system reset, this function completes that operation.
1354 *
1355 * @param bs The current boot status. This function reads
1356 * this struct to determine if it is resuming
1357 * an interrupted swap operation. This
1358 * function writes the updated status to this
1359 * function on return.
1360 *
1361 * @return 0 on success; nonzero on failure.
1362 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001363static int
Fabio Utzig338a19f2018-12-03 08:37:08 -02001364boot_swap_image(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001365{
1366 uint32_t sz;
1367 int first_sector_idx;
1368 int last_sector_idx;
David Vincze2d736ad2019-02-18 11:50:22 +01001369 int last_idx_secondary_slot;
Fabio Utzigcd5774b2017-11-29 10:18:26 -02001370 uint32_t swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001371 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001372#ifdef MCUBOOT_ENC_IMAGES
1373 const struct flash_area *fap;
1374 uint8_t slot;
1375 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001376#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001377 uint32_t size;
1378 uint32_t copy_size;
David Vincze2d736ad2019-02-18 11:50:22 +01001379 uint32_t primary_slot_size;
1380 uint32_t secondary_slot_size;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001381 int rc;
1382
1383 /* FIXME: just do this if asked by user? */
1384
1385 size = copy_size = 0;
1386
Fabio Utzig39000012018-07-30 12:40:20 -03001387 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Fabio Utzig46490722017-09-04 15:34:32 -03001388 /*
1389 * No swap ever happened, so need to find the largest image which
1390 * will be used to determine the amount of sectors to swap.
1391 */
David Vincze2d736ad2019-02-18 11:50:22 +01001392 hdr = boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001393 if (hdr->ih_magic == IMAGE_MAGIC) {
David Vincze2d736ad2019-02-18 11:50:22 +01001394 rc = boot_read_image_size(BOOT_PRIMARY_SLOT, hdr, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001395 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001396 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001397
Fabio Utzigba829042018-09-18 08:29:34 -03001398#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001399 if (IS_ENCRYPTED(hdr)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001400 fap = BOOT_IMG_AREA(&boot_data, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001401 rc = boot_enc_load(hdr, fap, bs->enckey[0]);
1402 assert(rc >= 0);
1403
1404 if (rc == 0) {
1405 rc = boot_enc_set_key(0, bs->enckey[0]);
1406 assert(rc == 0);
1407 } else {
1408 rc = 0;
1409 }
1410 } else {
1411 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1412 }
1413#endif
1414
David Vincze2d736ad2019-02-18 11:50:22 +01001415 hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001416 if (hdr->ih_magic == IMAGE_MAGIC) {
David Vincze2d736ad2019-02-18 11:50:22 +01001417 rc = boot_read_image_size(BOOT_SECONDARY_SLOT, hdr, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001418 assert(rc == 0);
1419 }
1420
Fabio Utzigba829042018-09-18 08:29:34 -03001421#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +01001422 hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001423 if (IS_ENCRYPTED(hdr)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001424 fap = BOOT_IMG_AREA(&boot_data, BOOT_SECONDARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001425 rc = boot_enc_load(hdr, fap, bs->enckey[1]);
1426 assert(rc >= 0);
1427
1428 if (rc == 0) {
1429 rc = boot_enc_set_key(1, bs->enckey[1]);
1430 assert(rc == 0);
1431 } else {
1432 rc = 0;
1433 }
1434 } else {
1435 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1436 }
1437#endif
1438
Fabio Utzig46490722017-09-04 15:34:32 -03001439 if (size > copy_size) {
1440 copy_size = size;
1441 }
1442
1443 bs->swap_size = copy_size;
1444 } else {
1445 /*
1446 * If a swap was under way, the swap_size should already be present
1447 * in the trailer...
1448 */
1449 rc = boot_read_swap_size(&bs->swap_size);
1450 assert(rc == 0);
1451
1452 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001453
1454#ifdef MCUBOOT_ENC_IMAGES
1455 for (slot = 0; slot <= 1; slot++) {
1456 rc = boot_read_enc_key(slot, bs->enckey[slot]);
1457 assert(rc == 0);
1458
1459 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001460 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001461 break;
1462 }
1463 }
1464
1465 if (i != BOOT_ENC_KEY_SIZE) {
1466 boot_enc_set_key(slot, bs->enckey[slot]);
1467 }
1468 }
1469#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001470 }
1471
David Vincze2d736ad2019-02-18 11:50:22 +01001472 primary_slot_size = 0;
1473 secondary_slot_size = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001474 last_sector_idx = 0;
David Vincze2d736ad2019-02-18 11:50:22 +01001475 last_idx_secondary_slot = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001476
1477 /*
1478 * Knowing the size of the largest image between both slots, here we
David Vincze2d736ad2019-02-18 11:50:22 +01001479 * find what is the last sector in the primary slot that needs swapping.
1480 * Since we already know that both slots are compatible, the secondary
1481 * slot's last sector is not really required after this check is finished.
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001482 */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001483 while (1) {
David Vincze2d736ad2019-02-18 11:50:22 +01001484 if ((primary_slot_size < copy_size) ||
1485 (primary_slot_size < secondary_slot_size)) {
1486 primary_slot_size += boot_img_sector_size(&boot_data,
1487 BOOT_PRIMARY_SLOT,
1488 last_sector_idx);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001489 }
David Vincze2d736ad2019-02-18 11:50:22 +01001490 if ((secondary_slot_size < copy_size) ||
1491 (secondary_slot_size < primary_slot_size)) {
1492 secondary_slot_size += boot_img_sector_size(&boot_data,
1493 BOOT_SECONDARY_SLOT,
1494 last_idx_secondary_slot);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001495 }
David Vincze2d736ad2019-02-18 11:50:22 +01001496 if (primary_slot_size >= copy_size &&
1497 secondary_slot_size >= copy_size &&
1498 primary_slot_size == secondary_slot_size) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001499 break;
1500 }
1501 last_sector_idx++;
David Vincze2d736ad2019-02-18 11:50:22 +01001502 last_idx_secondary_slot++;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001503 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001504
1505 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001506 while (last_sector_idx >= 0) {
1507 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
Fabio Utzig39000012018-07-30 12:40:20 -03001508 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001509 boot_swap_sectors(first_sector_idx, sz, bs);
1510 }
1511
1512 last_sector_idx = first_sector_idx - 1;
1513 swap_idx++;
1514 }
1515
David Vincze2d736ad2019-02-18 11:50:22 +01001516#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001517 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001518 BOOT_LOG_WRN("%d status write fails performing the swap",
1519 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001520 }
1521#endif
1522
Christopher Collins92ea77f2016-12-12 15:59:26 -08001523 return 0;
1524}
David Brown17609d82017-05-05 09:41:34 -06001525#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001526
1527/**
David Vincze2d736ad2019-02-18 11:50:22 +01001528 * Marks the image in the primary slot as fully copied.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001529 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001530#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001531static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001532boot_set_copy_done(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001533{
1534 const struct flash_area *fap;
1535 int rc;
1536
David Vincze2d736ad2019-02-18 11:50:22 +01001537 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001538 if (rc != 0) {
1539 return BOOT_EFLASH;
1540 }
1541
1542 rc = boot_write_copy_done(fap);
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001543 flash_area_close(fap);
1544 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001545}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001546#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001547
1548/**
David Vincze2d736ad2019-02-18 11:50:22 +01001549 * Marks a reverted image in the primary slot as confirmed. This is necessary to
1550 * ensure the status bytes from the image revert operation don't get processed
1551 * on a subsequent boot.
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001552 *
1553 * NOTE: image_ok is tested before writing because if there's a valid permanent
David Vincze2d736ad2019-02-18 11:50:22 +01001554 * image installed on the primary slot and the new image to be upgrade to has a
1555 * bad sig, image_ok would be overwritten.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001556 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001557#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001558static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001559boot_set_image_ok(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001560{
1561 const struct flash_area *fap;
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001562 struct boot_swap_state state;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001563 int rc;
1564
David Vincze2d736ad2019-02-18 11:50:22 +01001565 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001566 if (rc != 0) {
1567 return BOOT_EFLASH;
1568 }
1569
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001570 rc = boot_read_swap_state(fap, &state);
1571 if (rc != 0) {
1572 rc = BOOT_EFLASH;
1573 goto out;
1574 }
1575
1576 if (state.image_ok == BOOT_FLAG_UNSET) {
1577 rc = boot_write_image_ok(fap);
1578 }
1579
1580out:
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001581 flash_area_close(fap);
1582 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001583}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001584#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001585
1586/**
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001587 * Performs an image swap if one is required.
1588 *
1589 * @param out_swap_type On success, the type of swap performed gets
1590 * written here.
1591 *
1592 * @return 0 on success; nonzero on failure.
1593 */
1594static int
1595boot_swap_if_needed(int *out_swap_type)
1596{
1597 struct boot_status bs;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001598 int rc;
1599
Christopher Collinsa1c12042019-05-23 14:00:28 -07001600 /* Determine if we rebooted in the middle of an image swap operation. */
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001601 rc = boot_read_status(&bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001602 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001603 if (rc != 0) {
1604 return rc;
1605 }
1606
1607 /* If a partial swap was detected, complete it. */
Fabio Utzig39000012018-07-30 12:40:20 -03001608 if (bs.idx != BOOT_STATUS_IDX_0 || bs.state != BOOT_STATUS_STATE_0) {
Fabio Utziga32f1af2019-01-07 06:50:58 -02001609#ifdef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig338a19f2018-12-03 08:37:08 -02001610 /* Should never arrive here, overwrite-only mode has no swap state. */
1611 assert(0);
1612#else
Christopher Collinsa1c12042019-05-23 14:00:28 -07001613 /* Determine the type of swap operation being resumed from the
1614 * `swap-type` trailer field.
1615 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001616 rc = boot_swap_image(&bs);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001617 assert(rc == 0);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001618#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001619
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001620 } else {
Christopher Collinsa1c12042019-05-23 14:00:28 -07001621 if (bs.swap_type == BOOT_SWAP_TYPE_NONE) {
1622 bs.swap_type = boot_validated_swap_type(&bs);
1623 } else if (boot_validate_slot(BOOT_SECONDARY_SLOT, &bs) != 0) {
1624 bs.swap_type = BOOT_SWAP_TYPE_FAIL;
1625 }
1626 switch (bs.swap_type) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001627 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001628 case BOOT_SWAP_TYPE_PERM:
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001629 case BOOT_SWAP_TYPE_REVERT:
Fabio Utziga32f1af2019-01-07 06:50:58 -02001630#ifdef MCUBOOT_OVERWRITE_ONLY
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001631 rc = boot_copy_image(&bs);
Fabio Utzig338a19f2018-12-03 08:37:08 -02001632#else
1633 rc = boot_swap_image(&bs);
1634#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001635 assert(rc == 0);
1636 break;
Fabio Utzig338a19f2018-12-03 08:37:08 -02001637#ifdef MCUBOOT_BOOTSTRAP
1638 case BOOT_SWAP_TYPE_NONE:
1639 /*
1640 * Header checks are done first because they are inexpensive.
1641 * Since overwrite-only copies starting from offset 0, if
1642 * interrupted, it might leave a valid header magic, so also
David Vincze2d736ad2019-02-18 11:50:22 +01001643 * run validation on the primary slot to be sure it's not OK.
Fabio Utzig338a19f2018-12-03 08:37:08 -02001644 */
David Vincze2d736ad2019-02-18 11:50:22 +01001645 if (boot_check_header_erased(BOOT_PRIMARY_SLOT) == 0 ||
1646 boot_validate_slot(BOOT_PRIMARY_SLOT, &bs) != 0) {
1647 if ((boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT)->ih_magic
1648 == IMAGE_MAGIC ) &&
1649 (boot_validate_slot(BOOT_SECONDARY_SLOT, &bs) == 0)) {
Fabio Utzig338a19f2018-12-03 08:37:08 -02001650 rc = boot_copy_image(&bs);
1651 assert(rc == 0);
1652
1653 /* Returns fail here to trigger a re-read of the headers. */
Christopher Collinsa1c12042019-05-23 14:00:28 -07001654 bs.swap_type = BOOT_SWAP_TYPE_FAIL;
Fabio Utzig338a19f2018-12-03 08:37:08 -02001655 }
1656 }
1657 break;
1658#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001659 }
1660 }
1661
Christopher Collinsa1c12042019-05-23 14:00:28 -07001662 *out_swap_type = bs.swap_type;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001663 return 0;
1664}
1665
1666/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08001667 * Prepares the booting process. This function moves images around in flash as
1668 * appropriate, and tells you what address to boot from.
1669 *
1670 * @param rsp On success, indicates how booting should occur.
1671 *
1672 * @return 0 on success; nonzero on failure.
1673 */
1674int
1675boot_go(struct boot_rsp *rsp)
1676{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001677 int swap_type;
Marti Bolivar84898652017-06-13 17:20:22 -04001678 size_t slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001679 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001680 int fa_id;
David Brown52eee562017-07-05 11:25:09 -06001681 bool reload_headers = false;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001682
1683 /* The array of slot sectors are defined here (as opposed to file scope) so
1684 * that they don't get allocated for non-boot-loader apps. This is
1685 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001686 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001687 */
David Vincze2d736ad2019-02-18 11:50:22 +01001688 static boot_sector_t primary_slot_sectors[BOOT_MAX_IMG_SECTORS];
1689 static boot_sector_t secondary_slot_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001690 static boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
David Vincze2d736ad2019-02-18 11:50:22 +01001691 boot_data.imgs[BOOT_PRIMARY_SLOT].sectors = primary_slot_sectors;
1692 boot_data.imgs[BOOT_SECONDARY_SLOT].sectors = secondary_slot_sectors;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001693 boot_data.scratch.sectors = scratch_sectors;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001694
Fabio Utzigba829042018-09-18 08:29:34 -03001695#ifdef MCUBOOT_ENC_IMAGES
1696 /* FIXME: remove this after RAM is cleared by sim */
1697 boot_enc_zeroize();
1698#endif
1699
Marti Bolivarc0b47912017-06-13 17:18:09 -04001700 /* Open boot_data image areas for the duration of this call. */
1701 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1702 fa_id = flash_area_id_from_image_slot(slot);
1703 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot));
1704 assert(rc == 0);
1705 }
1706 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
1707 &BOOT_SCRATCH_AREA(&boot_data));
1708 assert(rc == 0);
1709
Christopher Collins92ea77f2016-12-12 15:59:26 -08001710 /* Determine the sector layout of the image slots and scratch area. */
1711 rc = boot_read_sectors();
1712 if (rc != 0) {
Fabio Utziga1fae672018-03-30 10:52:38 -03001713 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - too small?",
1714 BOOT_MAX_IMG_SECTORS);
Marti Bolivarc0b47912017-06-13 17:18:09 -04001715 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001716 }
1717
1718 /* Attempt to read an image header from each slot. */
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001719 rc = boot_read_image_headers(false);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001720 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001721 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001722 }
1723
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001724 /* If the image slots aren't compatible, no swap is possible. Just boot
David Vincze2d736ad2019-02-18 11:50:22 +01001725 * into the primary slot.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001726 */
1727 if (boot_slots_compatible()) {
1728 rc = boot_swap_if_needed(&swap_type);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001729 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001730 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001731 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001732 }
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001733
1734 /*
1735 * The following states need image_ok be explicitly set after the
1736 * swap was finished to avoid a new revert.
1737 */
David Vincze2d736ad2019-02-18 11:50:22 +01001738 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
Christopher Collinsa1c12042019-05-23 14:00:28 -07001739 swap_type == BOOT_SWAP_TYPE_FAIL ||
1740 swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001741#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig695d5642017-07-20 09:47:16 -03001742 rc = boot_set_image_ok();
1743 if (rc != 0) {
1744 swap_type = BOOT_SWAP_TYPE_PANIC;
1745 }
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001746#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001747 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001748 } else {
1749 swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001750 }
1751
1752 switch (swap_type) {
1753 case BOOT_SWAP_TYPE_NONE:
David Vincze2d736ad2019-02-18 11:50:22 +01001754 slot = BOOT_PRIMARY_SLOT;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001755 break;
1756
Fabio Utzig695d5642017-07-20 09:47:16 -03001757 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1758 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001759 case BOOT_SWAP_TYPE_REVERT:
David Vincze2d736ad2019-02-18 11:50:22 +01001760 slot = BOOT_SECONDARY_SLOT;
David Brown52eee562017-07-05 11:25:09 -06001761 reload_headers = true;
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001762#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig695d5642017-07-20 09:47:16 -03001763 rc = boot_set_copy_done();
1764 if (rc != 0) {
1765 swap_type = BOOT_SWAP_TYPE_PANIC;
1766 }
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001767#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001768 break;
1769
1770 case BOOT_SWAP_TYPE_FAIL:
David Vincze2d736ad2019-02-18 11:50:22 +01001771 /* The image in the secondary slot was invalid and is now erased.
1772 * Ensure we don't try to boot into it again on the next reboot.
1773 * Do this by pretending we just reverted back to the primary slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001774 */
David Vincze2d736ad2019-02-18 11:50:22 +01001775 slot = BOOT_PRIMARY_SLOT;
David Brown52eee562017-07-05 11:25:09 -06001776 reload_headers = true;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001777 break;
1778
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001779 default:
Fabio Utzig695d5642017-07-20 09:47:16 -03001780 swap_type = BOOT_SWAP_TYPE_PANIC;
1781 }
1782
1783 if (swap_type == BOOT_SWAP_TYPE_PANIC) {
1784 BOOT_LOG_ERR("panic!");
Fabio Utzigb5b2f552017-06-30 10:03:47 -03001785 assert(0);
Fabio Utzig695d5642017-07-20 09:47:16 -03001786
1787 /* Loop forever... */
1788 while (1) {}
Christopher Collins92ea77f2016-12-12 15:59:26 -08001789 }
1790
David Brown52eee562017-07-05 11:25:09 -06001791 if (reload_headers) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001792 rc = boot_read_image_headers(false);
Fabio Utzigc6a7b0c2017-09-13 19:01:15 -03001793 if (rc != 0) {
1794 goto out;
1795 }
1796 /* Since headers were reloaded, it can be assumed we just performed a
1797 * swap or overwrite. Now the header info that should be used to
David Vincze2d736ad2019-02-18 11:50:22 +01001798 * provide the data for the bootstrap, which previously was at the
1799 * secondary slot, was updated to the primary slot.
Fabio Utzigc6a7b0c2017-09-13 19:01:15 -03001800 */
David Vincze2d736ad2019-02-18 11:50:22 +01001801 slot = BOOT_PRIMARY_SLOT;
David Brown52eee562017-07-05 11:25:09 -06001802 }
1803
David Vincze2d736ad2019-02-18 11:50:22 +01001804#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
1805 rc = boot_validate_slot(BOOT_PRIMARY_SLOT, NULL);
David Brown554c52e2017-06-30 16:01:07 -06001806 if (rc != 0) {
1807 rc = BOOT_EBADIMAGE;
1808 goto out;
1809 }
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001810#else
David Vincze2d736ad2019-02-18 11:50:22 +01001811 /* Even if we're not re-validating the primary slot, we could be booting
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001812 * onto an empty flash chip. At least do a basic sanity check that
1813 * the magic number on the image is OK.
1814 */
David Vincze2d736ad2019-02-18 11:50:22 +01001815 if (boot_data.imgs[BOOT_PRIMARY_SLOT].hdr.ih_magic != IMAGE_MAGIC) {
1816 BOOT_LOG_ERR("bad image magic 0x%lx",
1817 (unsigned long)boot_data.imgs[BOOT_PRIMARY_SLOT].hdr.ih_magic);
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001818 rc = BOOT_EBADIMAGE;
1819 goto out;
1820 }
David Brown554c52e2017-06-30 16:01:07 -06001821#endif
1822
Christopher Collins92ea77f2016-12-12 15:59:26 -08001823 /* Always boot from the primary slot. */
David Vincze2d736ad2019-02-18 11:50:22 +01001824 rsp->br_flash_dev_id = boot_data.imgs[BOOT_PRIMARY_SLOT].area->fa_device_id;
1825 rsp->br_image_off = boot_img_slot_off(&boot_data, BOOT_PRIMARY_SLOT);
Marti Bolivarf804f622017-06-12 15:41:48 -04001826 rsp->br_hdr = boot_img_hdr(&boot_data, slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001827
Marti Bolivarc0b47912017-06-13 17:18:09 -04001828 out:
1829 flash_area_close(BOOT_SCRATCH_AREA(&boot_data));
1830 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1831 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
1832 }
1833 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001834}
1835
1836int
1837split_go(int loader_slot, int split_slot, void **entry)
1838{
Marti Bolivarc50926f2017-06-14 09:35:40 -04001839 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001840 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001841 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001842 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001843 int rc;
1844
Christopher Collins92ea77f2016-12-12 15:59:26 -08001845 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1846 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001847 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001848 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04001849 boot_data.imgs[loader_slot].sectors = sectors + 0;
1850 boot_data.imgs[split_slot].sectors = sectors + BOOT_MAX_IMG_SECTORS;
1851
1852 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1853 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07001854 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04001855 assert(rc == 0);
1856 split_flash_id = flash_area_id_from_image_slot(split_slot);
1857 rc = flash_area_open(split_flash_id,
1858 &BOOT_IMG_AREA(&boot_data, split_slot));
1859 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001860
1861 /* Determine the sector layout of the image slots and scratch area. */
1862 rc = boot_read_sectors();
1863 if (rc != 0) {
1864 rc = SPLIT_GO_ERR;
1865 goto done;
1866 }
1867
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001868 rc = boot_read_image_headers(true);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001869 if (rc != 0) {
1870 goto done;
1871 }
1872
Christopher Collins92ea77f2016-12-12 15:59:26 -08001873 /* Don't check the bootable image flag because we could really call a
1874 * bootable or non-bootable image. Just validate that the image check
1875 * passes which is distinct from the normal check.
1876 */
Marti Bolivarf804f622017-06-12 15:41:48 -04001877 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001878 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04001879 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001880 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001881 if (rc != 0) {
1882 rc = SPLIT_GO_NON_MATCHING;
1883 goto done;
1884 }
1885
Marti Bolivarea088872017-06-12 17:10:49 -04001886 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04001887 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001888 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001889 rc = SPLIT_GO_OK;
1890
1891done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04001892 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
1893 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001894 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001895 return rc;
1896}