blob: d7484398ecc7049f82721f78af3d94302c14726a [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;
957 }
958
Fabio Utzigba829042018-09-18 08:29:34 -0300959 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800960}
961
David Brown6b1b3b92017-09-19 08:59:10 -0600962#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -0300963static inline int
Fabio Utzigba829042018-09-18 08:29:34 -0300964boot_status_init(const struct flash_area *fap, const struct boot_status *bs)
Fabio Utzig2473ac02017-05-02 12:45:02 -0300965{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300966 struct boot_swap_state swap_state;
967 int rc;
968
Christopher Collins2c88e692019-05-22 15:10:14 -0700969 BOOT_LOG_DBG("initializing status; fa_id=%d", fap->fa_id);
970
David Vincze2d736ad2019-02-18 11:50:22 +0100971 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY, &swap_state);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300972 assert(rc == 0);
973
Christopher Collinsa1c12042019-05-23 14:00:28 -0700974 if (bs->swap_type != BOOT_SWAP_TYPE_NONE) {
975 rc = boot_write_swap_type(fap, bs->swap_type);
976 assert(rc == 0);
977 }
978
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400979 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300980 rc = boot_write_image_ok(fap);
981 assert(rc == 0);
982 }
983
Fabio Utzig46490722017-09-04 15:34:32 -0300984 rc = boot_write_swap_size(fap, bs->swap_size);
985 assert(rc == 0);
986
Fabio Utzigba829042018-09-18 08:29:34 -0300987#ifdef MCUBOOT_ENC_IMAGES
988 rc = boot_write_enc_key(fap, 0, bs->enckey[0]);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300989 assert(rc == 0);
990
Fabio Utzigba829042018-09-18 08:29:34 -0300991 rc = boot_write_enc_key(fap, 1, bs->enckey[1]);
992 assert(rc == 0);
993#endif
994
995 rc = boot_write_magic(fap);
996 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300997
998 return 0;
999}
Christopher Collinsa1c12042019-05-23 14:00:28 -07001000
David Brown6b1b3b92017-09-19 08:59:10 -06001001#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001002
Fabio Utzig358c9352017-07-25 22:10:45 -03001003#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -03001004static int
Fabio Utziged0ca432019-01-23 14:50:11 -02001005boot_erase_trailer_sectors(const struct flash_area *fap)
Fabio Utzig2473ac02017-05-02 12:45:02 -03001006{
1007 uint8_t slot;
Fabio Utziged0ca432019-01-23 14:50:11 -02001008 uint32_t sector;
1009 uint32_t trailer_sz;
1010 uint32_t total_sz;
1011 uint32_t off;
1012 uint32_t sz;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001013 int rc;
1014
Christopher Collins2c88e692019-05-22 15:10:14 -07001015 BOOT_LOG_DBG("erasing trailer; fa_id=%d", fap->fa_id);
1016
Fabio Utzigba829042018-09-18 08:29:34 -03001017 switch (fap->fa_id) {
David Vincze2d736ad2019-02-18 11:50:22 +01001018 case FLASH_AREA_IMAGE_PRIMARY:
1019 slot = BOOT_PRIMARY_SLOT;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001020 break;
David Vincze2d736ad2019-02-18 11:50:22 +01001021 case FLASH_AREA_IMAGE_SECONDARY:
1022 slot = BOOT_SECONDARY_SLOT;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001023 break;
1024 default:
1025 return BOOT_EFLASH;
1026 }
1027
Fabio Utziged0ca432019-01-23 14:50:11 -02001028 /* delete starting from last sector and moving to beginning */
1029 sector = boot_img_num_sectors(&boot_data, slot) - 1;
Christopher Collins2adef702019-05-22 14:37:31 -07001030 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Fabio Utziged0ca432019-01-23 14:50:11 -02001031 total_sz = 0;
1032 do {
1033 sz = boot_img_sector_size(&boot_data, slot, sector);
1034 off = boot_img_sector_off(&boot_data, slot, sector);
1035 rc = boot_erase_sector(fap, off, sz);
1036 assert(rc == 0);
1037
1038 sector--;
1039 total_sz += sz;
1040 } while (total_sz < trailer_sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001041
1042 return rc;
1043}
Fabio Utzig358c9352017-07-25 22:10:45 -03001044#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001045
Christopher Collins92ea77f2016-12-12 15:59:26 -08001046/**
1047 * Swaps the contents of two flash regions within the two image slots.
1048 *
1049 * @param idx The index of the first sector in the range of
1050 * sectors being swapped.
1051 * @param sz The number of bytes to swap.
1052 * @param bs The current boot status. This struct gets
1053 * updated according to the outcome.
1054 *
1055 * @return 0 on success; nonzero on failure.
1056 */
Fabio Utzig3488eef2017-06-12 10:25:43 -03001057#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -08001058static void
Christopher Collins92ea77f2016-12-12 15:59:26 -08001059boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
1060{
David Vincze2d736ad2019-02-18 11:50:22 +01001061 const struct flash_area *fap_primary_slot;
1062 const struct flash_area *fap_secondary_slot;
Fabio Utzigba829042018-09-18 08:29:34 -03001063 const struct flash_area *fap_scratch;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001064 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001065 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001066 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001067 uint32_t scratch_trailer_off;
1068 struct boot_swap_state swap_state;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001069 size_t last_sector;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001070 bool erase_scratch;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001071 int rc;
1072
1073 /* Calculate offset from start of image area. */
David Vincze2d736ad2019-02-18 11:50:22 +01001074 img_off = boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT, idx);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001075
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001076 copy_sz = sz;
Christopher Collins2adef702019-05-22 14:37:31 -07001077 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Fabio Utzig9678c972017-05-23 11:28:56 -04001078
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001079 /* sz in this function is always sized on a multiple of the sector size.
1080 * The check against the start offset of the last sector
Fabio Utzig9678c972017-05-23 11:28:56 -04001081 * is to determine if we're swapping the last sector. The last sector
1082 * needs special handling because it's where the trailer lives. If we're
1083 * copying it, we need to use scratch to write the trailer temporarily.
1084 *
1085 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
1086 * controls if special handling is needed (swapping last sector).
1087 */
David Vincze2d736ad2019-02-18 11:50:22 +01001088 last_sector = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT) - 1;
1089 if (img_off + sz > boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT,
1090 last_sector)) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001091 copy_sz -= trailer_sz;
1092 }
1093
Fabio Utzig39000012018-07-30 12:40:20 -03001094 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001095
David Vincze2d736ad2019-02-18 11:50:22 +01001096 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001097 assert (rc == 0);
1098
David Vincze2d736ad2019-02-18 11:50:22 +01001099 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001100 assert (rc == 0);
1101
1102 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch);
1103 assert (rc == 0);
1104
Fabio Utzig39000012018-07-30 12:40:20 -03001105 if (bs->state == BOOT_STATUS_STATE_0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001106 BOOT_LOG_DBG("erasing scratch area");
Christopher Collinsa1c12042019-05-23 14:00:28 -07001107 rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size);
Christopher Collins4772ac42017-02-27 20:08:01 -08001108 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001109
Fabio Utzig39000012018-07-30 12:40:20 -03001110 if (bs->idx == BOOT_STATUS_IDX_0) {
Christopher Collinsa1c12042019-05-23 14:00:28 -07001111 /* Write a trailer to the scratch area, even if we don't need the
1112 * scratch area for status. We need a temporary place to store the
1113 * `swap-type` while we erase the primary trailer.
1114 */
1115 rc = boot_status_init(fap_scratch, bs);
1116 assert(rc == 0);
1117
1118 if (!bs->use_scratch) {
1119 /* Prepare the primary status area... here it is known that the
1120 * last sector is not being used by the image data so it's safe
1121 * to erase.
Fabio Utzig2473ac02017-05-02 12:45:02 -03001122 */
David Vincze2d736ad2019-02-18 11:50:22 +01001123 rc = boot_erase_trailer_sectors(fap_primary_slot);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001124 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001125
Christopher Collinsa1c12042019-05-23 14:00:28 -07001126 rc = boot_status_init(fap_primary_slot, bs);
1127 assert(rc == 0);
1128
1129 /* Erase the temporary trailer from the scratch area. */
1130 rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size);
1131 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001132 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001133 }
1134
Christopher Collinsa1c12042019-05-23 14:00:28 -07001135 rc = boot_copy_sector(fap_secondary_slot, fap_scratch,
1136 img_off, 0, copy_sz);
1137 assert(rc == 0);
1138
Fabio Utzig39000012018-07-30 12:40:20 -03001139 bs->state = BOOT_STATUS_STATE_1;
Christopher Collins4772ac42017-02-27 20:08:01 -08001140 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001141 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001142 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001143
Fabio Utzig39000012018-07-30 12:40:20 -03001144 if (bs->state == BOOT_STATUS_STATE_1) {
David Vincze2d736ad2019-02-18 11:50:22 +01001145 rc = boot_erase_sector(fap_secondary_slot, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001146 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001147
David Vincze2d736ad2019-02-18 11:50:22 +01001148 rc = boot_copy_sector(fap_primary_slot, fap_secondary_slot,
1149 img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001150 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001151
Fabio Utzig39000012018-07-30 12:40:20 -03001152 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001153 /* If not all sectors of the slot are being swapped,
David Vincze2d736ad2019-02-18 11:50:22 +01001154 * guarantee here that only the primary slot will have the state.
Fabio Utzig2473ac02017-05-02 12:45:02 -03001155 */
David Vincze2d736ad2019-02-18 11:50:22 +01001156 rc = boot_erase_trailer_sectors(fap_secondary_slot);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001157 assert(rc == 0);
1158 }
1159
Fabio Utzig39000012018-07-30 12:40:20 -03001160 bs->state = BOOT_STATUS_STATE_2;
Christopher Collins4772ac42017-02-27 20:08:01 -08001161 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001162 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001163 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001164
Fabio Utzig39000012018-07-30 12:40:20 -03001165 if (bs->state == BOOT_STATUS_STATE_2) {
David Vincze2d736ad2019-02-18 11:50:22 +01001166 rc = boot_erase_sector(fap_primary_slot, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001167 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001168
Christopher Collinsa1c12042019-05-23 14:00:28 -07001169 /* NOTE: If this is the final sector, we exclude the image trailer from
1170 * this copy (copy_sz was truncated earlier).
1171 */
David Vincze2d736ad2019-02-18 11:50:22 +01001172 rc = boot_copy_sector(fap_scratch, fap_primary_slot,
1173 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001174 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001175
Fabio Utzig94d998c2017-05-22 11:02:41 -04001176 if (bs->use_scratch) {
Fabio Utzigba829042018-09-18 08:29:34 -03001177 scratch_trailer_off = boot_status_off(fap_scratch);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001178
1179 /* copy current status that is being maintained in scratch */
David Vincze2d736ad2019-02-18 11:50:22 +01001180 rc = boot_copy_sector(fap_scratch, fap_primary_slot,
1181 scratch_trailer_off, img_off + copy_sz,
Marti Bolivare10a7392017-06-14 16:20:07 -04001182 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data));
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001183 BOOT_STATUS_ASSERT(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001184
Fabio Utzig2473ac02017-05-02 12:45:02 -03001185 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1186 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001187 assert(rc == 0);
1188
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001189 if (swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze2d736ad2019-02-18 11:50:22 +01001190 rc = boot_write_image_ok(fap_primary_slot);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001191 assert(rc == 0);
1192 }
1193
Christopher Collinsa1c12042019-05-23 14:00:28 -07001194 if (swap_state.swap_type != BOOT_SWAP_TYPE_NONE) {
1195 rc = boot_write_swap_type(fap_primary_slot,
1196 swap_state.swap_type);
1197 assert(rc == 0);
1198 }
1199
David Vincze2d736ad2019-02-18 11:50:22 +01001200 rc = boot_write_swap_size(fap_primary_slot, bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001201 assert(rc == 0);
1202
Fabio Utzigba829042018-09-18 08:29:34 -03001203#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +01001204 rc = boot_write_enc_key(fap_primary_slot, 0, bs->enckey[0]);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001205 assert(rc == 0);
1206
David Vincze2d736ad2019-02-18 11:50:22 +01001207 rc = boot_write_enc_key(fap_primary_slot, 1, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001208 assert(rc == 0);
1209#endif
David Vincze2d736ad2019-02-18 11:50:22 +01001210 rc = boot_write_magic(fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001211 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001212 }
1213
Christopher Collinsa1c12042019-05-23 14:00:28 -07001214 /* If we wrote a trailer to the scratch area, erase it after we persist
1215 * a trailer to the primary slot. We do this to prevent mcuboot from
1216 * reading a stale status from the scratch area in case of immediate
1217 * reset.
1218 */
1219 erase_scratch = bs->use_scratch;
1220 bs->use_scratch = 0;
1221
Christopher Collins92ea77f2016-12-12 15:59:26 -08001222 bs->idx++;
Fabio Utzig39000012018-07-30 12:40:20 -03001223 bs->state = BOOT_STATUS_STATE_0;
Christopher Collins4772ac42017-02-27 20:08:01 -08001224 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001225 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001226
1227 if (erase_scratch) {
1228 rc = boot_erase_sector(fap_scratch, 0, sz);
1229 assert(rc == 0);
1230 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001231 }
Fabio Utzigba829042018-09-18 08:29:34 -03001232
David Vincze2d736ad2019-02-18 11:50:22 +01001233 flash_area_close(fap_primary_slot);
1234 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001235 flash_area_close(fap_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001236}
Fabio Utzig3488eef2017-06-12 10:25:43 -03001237#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001238
1239/**
David Vincze2d736ad2019-02-18 11:50:22 +01001240 * Overwrite primary slot with the image contained in the secondary slot.
1241 * If a prior copy operation was interrupted by a system reset, this function
1242 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001243 *
1244 * @param bs The current boot status. This function reads
1245 * this struct to determine if it is resuming
1246 * an interrupted swap operation. This
1247 * function writes the updated status to this
1248 * function on return.
1249 *
1250 * @return 0 on success; nonzero on failure.
1251 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001252#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001253static int
1254boot_copy_image(struct boot_status *bs)
1255{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001256 size_t sect_count;
1257 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001258 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001259 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001260 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001261 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +01001262 const struct flash_area *fap_primary_slot;
1263 const struct flash_area *fap_secondary_slot;
1264
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001265 (void)bs;
1266
Fabio Utzig13d9e352017-10-05 20:32:31 -03001267#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1268 uint32_t src_size = 0;
David Vincze2d736ad2019-02-18 11:50:22 +01001269 rc = boot_read_image_size(BOOT_SECONDARY_SLOT,
1270 boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT),
1271 &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001272 assert(rc == 0);
1273#endif
David Brown17609d82017-05-05 09:41:34 -06001274
David Vincze2d736ad2019-02-18 11:50:22 +01001275 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1276 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -06001277
David Vincze2d736ad2019-02-18 11:50:22 +01001278 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001279 assert (rc == 0);
1280
David Vincze2d736ad2019-02-18 11:50:22 +01001281 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001282 assert (rc == 0);
1283
David Vincze2d736ad2019-02-18 11:50:22 +01001284 sect_count = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001285 for (sect = 0, size = 0; sect < sect_count; sect++) {
David Vincze2d736ad2019-02-18 11:50:22 +01001286 this_size = boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, sect);
1287 rc = boot_erase_sector(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001288 assert(rc == 0);
1289
1290 size += this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001291
1292#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1293 if (size >= src_size) {
1294 break;
1295 }
1296#endif
David Brown17609d82017-05-05 09:41:34 -06001297 }
1298
Fabio Utzigba829042018-09-18 08:29:34 -03001299#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +01001300 if (IS_ENCRYPTED(boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT))) {
1301 rc = boot_enc_load(boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT),
1302 fap_secondary_slot,
1303 bs->enckey[1]);
1304
Fabio Utzigba829042018-09-18 08:29:34 -03001305 if (rc < 0) {
1306 return BOOT_EBADIMAGE;
1307 }
Fabio Utzige641ea52018-12-03 10:37:53 -02001308 if (rc == 0 && boot_enc_set_key(1, bs->enckey[1])) {
Fabio Utzigba829042018-09-18 08:29:34 -03001309 return BOOT_EBADIMAGE;
1310 }
1311 }
1312#endif
1313
David Vincze2d736ad2019-02-18 11:50:22 +01001314 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1315 size);
1316 rc = boot_copy_sector(fap_secondary_slot, fap_primary_slot, 0, 0, size);
David Brown17609d82017-05-05 09:41:34 -06001317
Fabio Utzig13d9e352017-10-05 20:32:31 -03001318 /*
1319 * Erases header and trailer. The trailer is erased because when a new
1320 * image is written without a trailer as is the case when using newt, the
1321 * trailer that was left might trigger a new upgrade.
1322 */
Christopher Collins2c88e692019-05-22 15:10:14 -07001323 BOOT_LOG_DBG("erasing secondary header");
David Vincze2d736ad2019-02-18 11:50:22 +01001324 rc = boot_erase_sector(fap_secondary_slot,
1325 boot_img_sector_off(&boot_data,
1326 BOOT_SECONDARY_SLOT, 0),
1327 boot_img_sector_size(&boot_data,
1328 BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001329 assert(rc == 0);
David Vincze2d736ad2019-02-18 11:50:22 +01001330 last_sector = boot_img_num_sectors(&boot_data, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -07001331 BOOT_LOG_DBG("erasing secondary trailer");
David Vincze2d736ad2019-02-18 11:50:22 +01001332 rc = boot_erase_sector(fap_secondary_slot,
1333 boot_img_sector_off(&boot_data,
1334 BOOT_SECONDARY_SLOT, last_sector),
1335 boot_img_sector_size(&boot_data,
1336 BOOT_SECONDARY_SLOT, last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001337 assert(rc == 0);
1338
David Vincze2d736ad2019-02-18 11:50:22 +01001339 flash_area_close(fap_primary_slot);
1340 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001341
David Vincze2d736ad2019-02-18 11:50:22 +01001342 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001343
1344 return 0;
1345}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001346#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001347
Christopher Collinsa1c12042019-05-23 14:00:28 -07001348#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -03001349/**
1350 * Swaps the two images in flash. If a prior copy operation was interrupted
1351 * by a system reset, this function completes that operation.
1352 *
1353 * @param bs The current boot status. This function reads
1354 * this struct to determine if it is resuming
1355 * an interrupted swap operation. This
1356 * function writes the updated status to this
1357 * function on return.
1358 *
1359 * @return 0 on success; nonzero on failure.
1360 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001361static int
Fabio Utzig338a19f2018-12-03 08:37:08 -02001362boot_swap_image(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001363{
1364 uint32_t sz;
1365 int first_sector_idx;
1366 int last_sector_idx;
David Vincze2d736ad2019-02-18 11:50:22 +01001367 int last_idx_secondary_slot;
Fabio Utzigcd5774b2017-11-29 10:18:26 -02001368 uint32_t swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001369 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001370#ifdef MCUBOOT_ENC_IMAGES
1371 const struct flash_area *fap;
1372 uint8_t slot;
1373 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001374#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001375 uint32_t size;
1376 uint32_t copy_size;
David Vincze2d736ad2019-02-18 11:50:22 +01001377 uint32_t primary_slot_size;
1378 uint32_t secondary_slot_size;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001379 int rc;
1380
1381 /* FIXME: just do this if asked by user? */
1382
1383 size = copy_size = 0;
1384
Fabio Utzig39000012018-07-30 12:40:20 -03001385 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Fabio Utzig46490722017-09-04 15:34:32 -03001386 /*
1387 * No swap ever happened, so need to find the largest image which
1388 * will be used to determine the amount of sectors to swap.
1389 */
David Vincze2d736ad2019-02-18 11:50:22 +01001390 hdr = boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001391 if (hdr->ih_magic == IMAGE_MAGIC) {
David Vincze2d736ad2019-02-18 11:50:22 +01001392 rc = boot_read_image_size(BOOT_PRIMARY_SLOT, hdr, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001393 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001394 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001395
Fabio Utzigba829042018-09-18 08:29:34 -03001396#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001397 if (IS_ENCRYPTED(hdr)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001398 fap = BOOT_IMG_AREA(&boot_data, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001399 rc = boot_enc_load(hdr, fap, bs->enckey[0]);
1400 assert(rc >= 0);
1401
1402 if (rc == 0) {
1403 rc = boot_enc_set_key(0, bs->enckey[0]);
1404 assert(rc == 0);
1405 } else {
1406 rc = 0;
1407 }
1408 } else {
1409 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1410 }
1411#endif
1412
David Vincze2d736ad2019-02-18 11:50:22 +01001413 hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001414 if (hdr->ih_magic == IMAGE_MAGIC) {
David Vincze2d736ad2019-02-18 11:50:22 +01001415 rc = boot_read_image_size(BOOT_SECONDARY_SLOT, hdr, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001416 assert(rc == 0);
1417 }
1418
Fabio Utzigba829042018-09-18 08:29:34 -03001419#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +01001420 hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001421 if (IS_ENCRYPTED(hdr)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001422 fap = BOOT_IMG_AREA(&boot_data, BOOT_SECONDARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001423 rc = boot_enc_load(hdr, fap, bs->enckey[1]);
1424 assert(rc >= 0);
1425
1426 if (rc == 0) {
1427 rc = boot_enc_set_key(1, bs->enckey[1]);
1428 assert(rc == 0);
1429 } else {
1430 rc = 0;
1431 }
1432 } else {
1433 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1434 }
1435#endif
1436
Fabio Utzig46490722017-09-04 15:34:32 -03001437 if (size > copy_size) {
1438 copy_size = size;
1439 }
1440
1441 bs->swap_size = copy_size;
1442 } else {
1443 /*
1444 * If a swap was under way, the swap_size should already be present
1445 * in the trailer...
1446 */
1447 rc = boot_read_swap_size(&bs->swap_size);
1448 assert(rc == 0);
1449
1450 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001451
1452#ifdef MCUBOOT_ENC_IMAGES
1453 for (slot = 0; slot <= 1; slot++) {
1454 rc = boot_read_enc_key(slot, bs->enckey[slot]);
1455 assert(rc == 0);
1456
1457 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001458 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001459 break;
1460 }
1461 }
1462
1463 if (i != BOOT_ENC_KEY_SIZE) {
1464 boot_enc_set_key(slot, bs->enckey[slot]);
1465 }
1466 }
1467#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001468 }
1469
David Vincze2d736ad2019-02-18 11:50:22 +01001470 primary_slot_size = 0;
1471 secondary_slot_size = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001472 last_sector_idx = 0;
David Vincze2d736ad2019-02-18 11:50:22 +01001473 last_idx_secondary_slot = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001474
1475 /*
1476 * Knowing the size of the largest image between both slots, here we
David Vincze2d736ad2019-02-18 11:50:22 +01001477 * find what is the last sector in the primary slot that needs swapping.
1478 * Since we already know that both slots are compatible, the secondary
1479 * slot's last sector is not really required after this check is finished.
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001480 */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001481 while (1) {
David Vincze2d736ad2019-02-18 11:50:22 +01001482 if ((primary_slot_size < copy_size) ||
1483 (primary_slot_size < secondary_slot_size)) {
1484 primary_slot_size += boot_img_sector_size(&boot_data,
1485 BOOT_PRIMARY_SLOT,
1486 last_sector_idx);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001487 }
David Vincze2d736ad2019-02-18 11:50:22 +01001488 if ((secondary_slot_size < copy_size) ||
1489 (secondary_slot_size < primary_slot_size)) {
1490 secondary_slot_size += boot_img_sector_size(&boot_data,
1491 BOOT_SECONDARY_SLOT,
1492 last_idx_secondary_slot);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001493 }
David Vincze2d736ad2019-02-18 11:50:22 +01001494 if (primary_slot_size >= copy_size &&
1495 secondary_slot_size >= copy_size &&
1496 primary_slot_size == secondary_slot_size) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001497 break;
1498 }
1499 last_sector_idx++;
David Vincze2d736ad2019-02-18 11:50:22 +01001500 last_idx_secondary_slot++;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001501 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001502
1503 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001504 while (last_sector_idx >= 0) {
1505 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
Fabio Utzig39000012018-07-30 12:40:20 -03001506 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001507 boot_swap_sectors(first_sector_idx, sz, bs);
1508 }
1509
1510 last_sector_idx = first_sector_idx - 1;
1511 swap_idx++;
1512 }
1513
David Vincze2d736ad2019-02-18 11:50:22 +01001514#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001515 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001516 BOOT_LOG_WRN("%d status write fails performing the swap",
1517 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001518 }
1519#endif
1520
Christopher Collins92ea77f2016-12-12 15:59:26 -08001521 return 0;
1522}
David Brown17609d82017-05-05 09:41:34 -06001523#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001524
1525/**
David Vincze2d736ad2019-02-18 11:50:22 +01001526 * Marks the image in the primary slot as fully copied.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001527 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001528#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001529static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001530boot_set_copy_done(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001531{
1532 const struct flash_area *fap;
1533 int rc;
1534
David Vincze2d736ad2019-02-18 11:50:22 +01001535 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001536 if (rc != 0) {
1537 return BOOT_EFLASH;
1538 }
1539
1540 rc = boot_write_copy_done(fap);
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001541 flash_area_close(fap);
1542 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001543}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001544#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001545
1546/**
David Vincze2d736ad2019-02-18 11:50:22 +01001547 * Marks a reverted image in the primary slot as confirmed. This is necessary to
1548 * ensure the status bytes from the image revert operation don't get processed
1549 * on a subsequent boot.
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001550 *
1551 * NOTE: image_ok is tested before writing because if there's a valid permanent
David Vincze2d736ad2019-02-18 11:50:22 +01001552 * image installed on the primary slot and the new image to be upgrade to has a
1553 * bad sig, image_ok would be overwritten.
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_image_ok(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001558{
1559 const struct flash_area *fap;
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001560 struct boot_swap_state state;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001561 int rc;
1562
David Vincze2d736ad2019-02-18 11:50:22 +01001563 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001564 if (rc != 0) {
1565 return BOOT_EFLASH;
1566 }
1567
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001568 rc = boot_read_swap_state(fap, &state);
1569 if (rc != 0) {
1570 rc = BOOT_EFLASH;
1571 goto out;
1572 }
1573
1574 if (state.image_ok == BOOT_FLAG_UNSET) {
1575 rc = boot_write_image_ok(fap);
1576 }
1577
1578out:
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001579 flash_area_close(fap);
1580 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001581}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001582#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001583
1584/**
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001585 * Performs an image swap if one is required.
1586 *
1587 * @param out_swap_type On success, the type of swap performed gets
1588 * written here.
1589 *
1590 * @return 0 on success; nonzero on failure.
1591 */
1592static int
1593boot_swap_if_needed(int *out_swap_type)
1594{
1595 struct boot_status bs;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001596 int rc;
1597
Christopher Collinsa1c12042019-05-23 14:00:28 -07001598 /* Determine if we rebooted in the middle of an image swap operation. */
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001599 rc = boot_read_status(&bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001600 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001601 if (rc != 0) {
1602 return rc;
1603 }
1604
1605 /* If a partial swap was detected, complete it. */
Fabio Utzig39000012018-07-30 12:40:20 -03001606 if (bs.idx != BOOT_STATUS_IDX_0 || bs.state != BOOT_STATUS_STATE_0) {
Fabio Utziga32f1af2019-01-07 06:50:58 -02001607#ifdef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig338a19f2018-12-03 08:37:08 -02001608 /* Should never arrive here, overwrite-only mode has no swap state. */
1609 assert(0);
1610#else
Christopher Collinsa1c12042019-05-23 14:00:28 -07001611 /* Determine the type of swap operation being resumed from the
1612 * `swap-type` trailer field.
1613 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001614 rc = boot_swap_image(&bs);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001615 assert(rc == 0);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001616#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001617
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001618 } else {
Christopher Collinsa1c12042019-05-23 14:00:28 -07001619 if (bs.swap_type == BOOT_SWAP_TYPE_NONE) {
1620 bs.swap_type = boot_validated_swap_type(&bs);
1621 } else if (boot_validate_slot(BOOT_SECONDARY_SLOT, &bs) != 0) {
1622 bs.swap_type = BOOT_SWAP_TYPE_FAIL;
1623 }
1624 switch (bs.swap_type) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001625 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001626 case BOOT_SWAP_TYPE_PERM:
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001627 case BOOT_SWAP_TYPE_REVERT:
Fabio Utziga32f1af2019-01-07 06:50:58 -02001628#ifdef MCUBOOT_OVERWRITE_ONLY
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001629 rc = boot_copy_image(&bs);
Fabio Utzig338a19f2018-12-03 08:37:08 -02001630#else
1631 rc = boot_swap_image(&bs);
1632#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001633 assert(rc == 0);
1634 break;
Fabio Utzig338a19f2018-12-03 08:37:08 -02001635#ifdef MCUBOOT_BOOTSTRAP
1636 case BOOT_SWAP_TYPE_NONE:
1637 /*
1638 * Header checks are done first because they are inexpensive.
1639 * Since overwrite-only copies starting from offset 0, if
1640 * interrupted, it might leave a valid header magic, so also
David Vincze2d736ad2019-02-18 11:50:22 +01001641 * run validation on the primary slot to be sure it's not OK.
Fabio Utzig338a19f2018-12-03 08:37:08 -02001642 */
David Vincze2d736ad2019-02-18 11:50:22 +01001643 if (boot_check_header_erased(BOOT_PRIMARY_SLOT) == 0 ||
1644 boot_validate_slot(BOOT_PRIMARY_SLOT, &bs) != 0) {
1645 if ((boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT)->ih_magic
1646 == IMAGE_MAGIC ) &&
1647 (boot_validate_slot(BOOT_SECONDARY_SLOT, &bs) == 0)) {
Fabio Utzig338a19f2018-12-03 08:37:08 -02001648 rc = boot_copy_image(&bs);
1649 assert(rc == 0);
1650
1651 /* Returns fail here to trigger a re-read of the headers. */
Christopher Collinsa1c12042019-05-23 14:00:28 -07001652 bs.swap_type = BOOT_SWAP_TYPE_FAIL;
Fabio Utzig338a19f2018-12-03 08:37:08 -02001653 }
1654 }
1655 break;
1656#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001657 }
1658 }
1659
Christopher Collinsa1c12042019-05-23 14:00:28 -07001660 *out_swap_type = bs.swap_type;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001661 return 0;
1662}
1663
1664/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08001665 * Prepares the booting process. This function moves images around in flash as
1666 * appropriate, and tells you what address to boot from.
1667 *
1668 * @param rsp On success, indicates how booting should occur.
1669 *
1670 * @return 0 on success; nonzero on failure.
1671 */
1672int
1673boot_go(struct boot_rsp *rsp)
1674{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001675 int swap_type;
Marti Bolivar84898652017-06-13 17:20:22 -04001676 size_t slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001677 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001678 int fa_id;
David Brown52eee562017-07-05 11:25:09 -06001679 bool reload_headers = false;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001680
1681 /* The array of slot sectors are defined here (as opposed to file scope) so
1682 * that they don't get allocated for non-boot-loader apps. This is
1683 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001684 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001685 */
David Vincze2d736ad2019-02-18 11:50:22 +01001686 static boot_sector_t primary_slot_sectors[BOOT_MAX_IMG_SECTORS];
1687 static boot_sector_t secondary_slot_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001688 static boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
David Vincze2d736ad2019-02-18 11:50:22 +01001689 boot_data.imgs[BOOT_PRIMARY_SLOT].sectors = primary_slot_sectors;
1690 boot_data.imgs[BOOT_SECONDARY_SLOT].sectors = secondary_slot_sectors;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001691 boot_data.scratch.sectors = scratch_sectors;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001692
Fabio Utzigba829042018-09-18 08:29:34 -03001693#ifdef MCUBOOT_ENC_IMAGES
1694 /* FIXME: remove this after RAM is cleared by sim */
1695 boot_enc_zeroize();
1696#endif
1697
Marti Bolivarc0b47912017-06-13 17:18:09 -04001698 /* Open boot_data image areas for the duration of this call. */
1699 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1700 fa_id = flash_area_id_from_image_slot(slot);
1701 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot));
1702 assert(rc == 0);
1703 }
1704 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
1705 &BOOT_SCRATCH_AREA(&boot_data));
1706 assert(rc == 0);
1707
Christopher Collins92ea77f2016-12-12 15:59:26 -08001708 /* Determine the sector layout of the image slots and scratch area. */
1709 rc = boot_read_sectors();
1710 if (rc != 0) {
Fabio Utziga1fae672018-03-30 10:52:38 -03001711 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - too small?",
1712 BOOT_MAX_IMG_SECTORS);
Marti Bolivarc0b47912017-06-13 17:18:09 -04001713 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001714 }
1715
1716 /* Attempt to read an image header from each slot. */
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001717 rc = boot_read_image_headers(false);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001718 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001719 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001720 }
1721
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001722 /* If the image slots aren't compatible, no swap is possible. Just boot
David Vincze2d736ad2019-02-18 11:50:22 +01001723 * into the primary slot.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001724 */
1725 if (boot_slots_compatible()) {
1726 rc = boot_swap_if_needed(&swap_type);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001727 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001728 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001729 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001730 }
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001731
1732 /*
1733 * The following states need image_ok be explicitly set after the
1734 * swap was finished to avoid a new revert.
1735 */
David Vincze2d736ad2019-02-18 11:50:22 +01001736 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
Christopher Collinsa1c12042019-05-23 14:00:28 -07001737 swap_type == BOOT_SWAP_TYPE_FAIL ||
1738 swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001739#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig695d5642017-07-20 09:47:16 -03001740 rc = boot_set_image_ok();
1741 if (rc != 0) {
1742 swap_type = BOOT_SWAP_TYPE_PANIC;
1743 }
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001744#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001745 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001746 } else {
1747 swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001748 }
1749
1750 switch (swap_type) {
1751 case BOOT_SWAP_TYPE_NONE:
David Vincze2d736ad2019-02-18 11:50:22 +01001752 slot = BOOT_PRIMARY_SLOT;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001753 break;
1754
Fabio Utzig695d5642017-07-20 09:47:16 -03001755 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1756 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001757 case BOOT_SWAP_TYPE_REVERT:
David Vincze2d736ad2019-02-18 11:50:22 +01001758 slot = BOOT_SECONDARY_SLOT;
David Brown52eee562017-07-05 11:25:09 -06001759 reload_headers = true;
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001760#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig695d5642017-07-20 09:47:16 -03001761 rc = boot_set_copy_done();
1762 if (rc != 0) {
1763 swap_type = BOOT_SWAP_TYPE_PANIC;
1764 }
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001765#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001766 break;
1767
1768 case BOOT_SWAP_TYPE_FAIL:
David Vincze2d736ad2019-02-18 11:50:22 +01001769 /* The image in the secondary slot was invalid and is now erased.
1770 * Ensure we don't try to boot into it again on the next reboot.
1771 * Do this by pretending we just reverted back to the primary slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001772 */
David Vincze2d736ad2019-02-18 11:50:22 +01001773 slot = BOOT_PRIMARY_SLOT;
David Brown52eee562017-07-05 11:25:09 -06001774 reload_headers = true;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001775 break;
1776
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001777 default:
Fabio Utzig695d5642017-07-20 09:47:16 -03001778 swap_type = BOOT_SWAP_TYPE_PANIC;
1779 }
1780
1781 if (swap_type == BOOT_SWAP_TYPE_PANIC) {
1782 BOOT_LOG_ERR("panic!");
Fabio Utzigb5b2f552017-06-30 10:03:47 -03001783 assert(0);
Fabio Utzig695d5642017-07-20 09:47:16 -03001784
1785 /* Loop forever... */
1786 while (1) {}
Christopher Collins92ea77f2016-12-12 15:59:26 -08001787 }
1788
David Brown52eee562017-07-05 11:25:09 -06001789 if (reload_headers) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001790 rc = boot_read_image_headers(false);
Fabio Utzigc6a7b0c2017-09-13 19:01:15 -03001791 if (rc != 0) {
1792 goto out;
1793 }
1794 /* Since headers were reloaded, it can be assumed we just performed a
1795 * swap or overwrite. Now the header info that should be used to
David Vincze2d736ad2019-02-18 11:50:22 +01001796 * provide the data for the bootstrap, which previously was at the
1797 * secondary slot, was updated to the primary slot.
Fabio Utzigc6a7b0c2017-09-13 19:01:15 -03001798 */
David Vincze2d736ad2019-02-18 11:50:22 +01001799 slot = BOOT_PRIMARY_SLOT;
David Brown52eee562017-07-05 11:25:09 -06001800 }
1801
David Vincze2d736ad2019-02-18 11:50:22 +01001802#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
1803 rc = boot_validate_slot(BOOT_PRIMARY_SLOT, NULL);
David Brown554c52e2017-06-30 16:01:07 -06001804 if (rc != 0) {
1805 rc = BOOT_EBADIMAGE;
1806 goto out;
1807 }
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001808#else
David Vincze2d736ad2019-02-18 11:50:22 +01001809 /* Even if we're not re-validating the primary slot, we could be booting
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001810 * onto an empty flash chip. At least do a basic sanity check that
1811 * the magic number on the image is OK.
1812 */
David Vincze2d736ad2019-02-18 11:50:22 +01001813 if (boot_data.imgs[BOOT_PRIMARY_SLOT].hdr.ih_magic != IMAGE_MAGIC) {
1814 BOOT_LOG_ERR("bad image magic 0x%lx",
1815 (unsigned long)boot_data.imgs[BOOT_PRIMARY_SLOT].hdr.ih_magic);
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001816 rc = BOOT_EBADIMAGE;
1817 goto out;
1818 }
David Brown554c52e2017-06-30 16:01:07 -06001819#endif
1820
Christopher Collins92ea77f2016-12-12 15:59:26 -08001821 /* Always boot from the primary slot. */
David Vincze2d736ad2019-02-18 11:50:22 +01001822 rsp->br_flash_dev_id = boot_data.imgs[BOOT_PRIMARY_SLOT].area->fa_device_id;
1823 rsp->br_image_off = boot_img_slot_off(&boot_data, BOOT_PRIMARY_SLOT);
Marti Bolivarf804f622017-06-12 15:41:48 -04001824 rsp->br_hdr = boot_img_hdr(&boot_data, slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001825
Marti Bolivarc0b47912017-06-13 17:18:09 -04001826 out:
1827 flash_area_close(BOOT_SCRATCH_AREA(&boot_data));
1828 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1829 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
1830 }
1831 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001832}
1833
1834int
1835split_go(int loader_slot, int split_slot, void **entry)
1836{
Marti Bolivarc50926f2017-06-14 09:35:40 -04001837 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001838 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001839 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001840 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001841 int rc;
1842
Christopher Collins92ea77f2016-12-12 15:59:26 -08001843 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1844 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001845 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001846 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04001847 boot_data.imgs[loader_slot].sectors = sectors + 0;
1848 boot_data.imgs[split_slot].sectors = sectors + BOOT_MAX_IMG_SECTORS;
1849
1850 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1851 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07001852 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04001853 assert(rc == 0);
1854 split_flash_id = flash_area_id_from_image_slot(split_slot);
1855 rc = flash_area_open(split_flash_id,
1856 &BOOT_IMG_AREA(&boot_data, split_slot));
1857 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001858
1859 /* Determine the sector layout of the image slots and scratch area. */
1860 rc = boot_read_sectors();
1861 if (rc != 0) {
1862 rc = SPLIT_GO_ERR;
1863 goto done;
1864 }
1865
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001866 rc = boot_read_image_headers(true);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001867 if (rc != 0) {
1868 goto done;
1869 }
1870
Christopher Collins92ea77f2016-12-12 15:59:26 -08001871 /* Don't check the bootable image flag because we could really call a
1872 * bootable or non-bootable image. Just validate that the image check
1873 * passes which is distinct from the normal check.
1874 */
Marti Bolivarf804f622017-06-12 15:41:48 -04001875 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001876 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04001877 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001878 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001879 if (rc != 0) {
1880 rc = SPLIT_GO_NON_MATCHING;
1881 goto done;
1882 }
1883
Marti Bolivarea088872017-06-12 17:10:49 -04001884 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04001885 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001886 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001887 rc = SPLIT_GO_OK;
1888
1889done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04001890 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
1891 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001892 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001893 return rc;
1894}