blob: b875a1a125da31acce33373202ec639b6c488400 [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
David Vinczee2453472019-06-17 12:31:59 +020020/*
21 * Modifications are Copyright (c) 2019 Arm Limited.
22 */
23
Christopher Collins92ea77f2016-12-12 15:59:26 -080024/**
25 * This file provides an interface to the boot loader. Functions defined in
26 * this file should only be called while the boot loader is running.
27 */
28
29#include <assert.h>
30#include <stddef.h>
David Brown52eee562017-07-05 11:25:09 -060031#include <stdbool.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080032#include <inttypes.h>
33#include <stdlib.h>
34#include <string.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080035#include <os/os_malloc.h>
36#include "bootutil/bootutil.h"
37#include "bootutil/image.h"
38#include "bootutil_priv.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050039#include "bootutil/bootutil_log.h"
40
Fabio Utzigba829042018-09-18 08:29:34 -030041#ifdef MCUBOOT_ENC_IMAGES
42#include "bootutil/enc_key.h"
43#endif
44
Fabio Utzigba1fbe62017-07-21 14:01:20 -030045#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030046
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010047MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
48
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040049static struct boot_loader_state boot_data;
David Vinczeb75c12a2019-03-22 14:58:33 +010050uint8_t current_image = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -080051
Fabio Utzigabec0732019-07-31 08:40:22 -030052#if (BOOT_IMAGE_NUMBER > 1)
53#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
54#else
55#define IMAGES_ITER(x)
56#endif
57
David Vincze2d736ad2019-02-18 11:50:22 +010058#if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT) && !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utziga0e1cce2017-11-23 20:04:01 -020059static int boot_status_fails = 0;
60#define BOOT_STATUS_ASSERT(x) \
61 do { \
Johann Fischered8461b2018-02-15 16:50:31 +010062 if (!(x)) { \
Fabio Utziga0e1cce2017-11-23 20:04:01 -020063 boot_status_fails++; \
64 } \
65 } while (0)
66#else
Fabio Utzig57c40f72017-12-12 21:48:30 -020067#define BOOT_STATUS_ASSERT(x) ASSERT(x)
Fabio Utziga0e1cce2017-11-23 20:04:01 -020068#endif
69
Christopher Collins92ea77f2016-12-12 15:59:26 -080070struct boot_status_table {
David Vincze2d736ad2019-02-18 11:50:22 +010071 uint8_t bst_magic_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -080072 uint8_t bst_magic_scratch;
David Vincze2d736ad2019-02-18 11:50:22 +010073 uint8_t bst_copy_done_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -080074 uint8_t bst_status_source;
75};
76
77/**
78 * This set of tables maps swap state contents to boot status location.
79 * When searching for a match, these tables must be iterated in order.
80 */
81static const struct boot_status_table boot_status_tables[] = {
82 {
David Vincze2d736ad2019-02-18 11:50:22 +010083 /* | primary slot | scratch |
84 * ----------+--------------+--------------|
85 * magic | Good | Any |
86 * copy-done | Set | N/A |
87 * ----------+--------------+--------------'
88 * source: none |
89 * ----------------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -080090 */
David Vincze2d736ad2019-02-18 11:50:22 +010091 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
Christopher Collinsa1c12042019-05-23 14:00:28 -070092 .bst_magic_scratch = BOOT_MAGIC_NOTGOOD,
David Vincze2d736ad2019-02-18 11:50:22 +010093 .bst_copy_done_primary_slot = BOOT_FLAG_SET,
94 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
Christopher Collins92ea77f2016-12-12 15:59:26 -080095 },
96
97 {
David Vincze2d736ad2019-02-18 11:50:22 +010098 /* | primary slot | scratch |
99 * ----------+--------------+--------------|
100 * magic | Good | Any |
101 * copy-done | Unset | N/A |
102 * ----------+--------------+--------------'
103 * source: primary slot |
104 * ----------------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -0800105 */
David Vincze2d736ad2019-02-18 11:50:22 +0100106 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
Christopher Collinsa1c12042019-05-23 14:00:28 -0700107 .bst_magic_scratch = BOOT_MAGIC_NOTGOOD,
David Vincze2d736ad2019-02-18 11:50:22 +0100108 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
109 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800110 },
111
112 {
David Vincze2d736ad2019-02-18 11:50:22 +0100113 /* | primary slot | scratch |
114 * ----------+--------------+--------------|
115 * magic | Any | Good |
116 * copy-done | Any | N/A |
117 * ----------+--------------+--------------'
118 * source: scratch |
119 * ----------------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -0800120 */
David Vincze2d736ad2019-02-18 11:50:22 +0100121 .bst_magic_primary_slot = BOOT_MAGIC_ANY,
122 .bst_magic_scratch = BOOT_MAGIC_GOOD,
123 .bst_copy_done_primary_slot = BOOT_FLAG_ANY,
124 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800125 },
Christopher Collins92ea77f2016-12-12 15:59:26 -0800126 {
David Vincze2d736ad2019-02-18 11:50:22 +0100127 /* | primary slot | scratch |
128 * ----------+--------------+--------------|
129 * magic | Unset | Any |
130 * copy-done | Unset | N/A |
131 * ----------+--------------+--------------|
132 * source: varies |
133 * ----------------------------------------+--------------------------+
Christopher Collins92ea77f2016-12-12 15:59:26 -0800134 * This represents one of two cases: |
135 * o No swaps ever (no status to read, so no harm in checking). |
David Vincze2d736ad2019-02-18 11:50:22 +0100136 * o Mid-revert; status in primary slot. |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800137 * -------------------------------------------------------------------'
138 */
David Vincze2d736ad2019-02-18 11:50:22 +0100139 .bst_magic_primary_slot = BOOT_MAGIC_UNSET,
140 .bst_magic_scratch = BOOT_MAGIC_ANY,
141 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
142 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800143 },
144};
145
146#define BOOT_STATUS_TABLES_COUNT \
147 (sizeof boot_status_tables / sizeof boot_status_tables[0])
148
Marti Bolivarfd20c762017-02-07 16:52:50 -0500149#define BOOT_LOG_SWAP_STATE(area, state) \
Christopher Collinsa1c12042019-05-23 14:00:28 -0700150 BOOT_LOG_INF("%s: magic=%s, swap_type=0x%x, copy_done=0x%x, " \
151 "image_ok=0x%x", \
Marti Bolivarfd20c762017-02-07 16:52:50 -0500152 (area), \
153 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
154 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
155 "bad"), \
Christopher Collinsa1c12042019-05-23 14:00:28 -0700156 (state)->swap_type, \
Marti Bolivarfd20c762017-02-07 16:52:50 -0500157 (state)->copy_done, \
158 (state)->image_ok)
159
Christopher Collins92ea77f2016-12-12 15:59:26 -0800160/**
David Vincze2d736ad2019-02-18 11:50:22 +0100161 * Determines where in flash the most recent boot status is stored. The boot
Christopher Collins92ea77f2016-12-12 15:59:26 -0800162 * status is necessary for completing a swap that was interrupted by a boot
163 * loader reset.
164 *
David Vincze2d736ad2019-02-18 11:50:22 +0100165 * @return A BOOT_STATUS_SOURCE_[...] code indicating where status should
166 * be read from.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800167 */
168static int
169boot_status_source(void)
170{
171 const struct boot_status_table *table;
172 struct boot_swap_state state_scratch;
David Vincze2d736ad2019-02-18 11:50:22 +0100173 struct boot_swap_state state_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800174 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200175 size_t i;
Marti Bolivarfd20c762017-02-07 16:52:50 -0500176 uint8_t source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800177
David Vincze2d736ad2019-02-18 11:50:22 +0100178 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY,
179 &state_primary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800180 assert(rc == 0);
181
Fabio Utzig2473ac02017-05-02 12:45:02 -0300182 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800183 assert(rc == 0);
184
David Vincze2d736ad2019-02-18 11:50:22 +0100185 BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot);
Marti Bolivarfd20c762017-02-07 16:52:50 -0500186 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
187
Christopher Collins92ea77f2016-12-12 15:59:26 -0800188 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300189 table = &boot_status_tables[i];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800190
Christopher Collinsa1c12042019-05-23 14:00:28 -0700191 if (boot_magic_compatible_check(table->bst_magic_primary_slot,
192 state_primary_slot.magic) &&
193 boot_magic_compatible_check(table->bst_magic_scratch,
194 state_scratch.magic) &&
David Vincze2d736ad2019-02-18 11:50:22 +0100195 (table->bst_copy_done_primary_slot == BOOT_FLAG_ANY ||
196 table->bst_copy_done_primary_slot == state_primary_slot.copy_done))
197 {
Marti Bolivarfd20c762017-02-07 16:52:50 -0500198 source = table->bst_status_source;
David Vinczeba3bd602019-06-17 16:01:43 +0200199
200#if (BOOT_IMAGE_NUMBER > 1)
201 /* In case of multi-image boot it can happen that if boot status
202 * info is found on scratch area then it does not belong to the
203 * currently examined image.
204 */
205 if (source == BOOT_STATUS_SOURCE_SCRATCH &&
206 state_scratch.image_num != current_image) {
207 source = BOOT_STATUS_SOURCE_NONE;
208 }
209#endif
210
Marti Bolivarfd20c762017-02-07 16:52:50 -0500211 BOOT_LOG_INF("Boot source: %s",
212 source == BOOT_STATUS_SOURCE_NONE ? "none" :
213 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
David Vincze2d736ad2019-02-18 11:50:22 +0100214 source == BOOT_STATUS_SOURCE_PRIMARY_SLOT ?
215 "primary slot" : "BUG; can't happen");
Marti Bolivarfd20c762017-02-07 16:52:50 -0500216 return source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800217 }
218 }
219
Marti Bolivarfd20c762017-02-07 16:52:50 -0500220 BOOT_LOG_INF("Boot source: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800221 return BOOT_STATUS_SOURCE_NONE;
222}
223
David Brownf5b33d82017-09-01 10:58:27 -0600224/*
225 * Compute the total size of the given image. Includes the size of
226 * the TLVs.
227 */
Fabio Utzig13d9e352017-10-05 20:32:31 -0300228#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600229static int
230boot_read_image_size(int slot, struct image_header *hdr, uint32_t *size)
231{
232 const struct flash_area *fap;
233 struct image_tlv_info info;
234 int area_id;
235 int rc;
236
237 area_id = flash_area_id_from_image_slot(slot);
238 rc = flash_area_open(area_id, &fap);
239 if (rc != 0) {
240 rc = BOOT_EFLASH;
241 goto done;
242 }
243
244 rc = flash_area_read(fap, hdr->ih_hdr_size + hdr->ih_img_size,
245 &info, sizeof(info));
246 if (rc != 0) {
247 rc = BOOT_EFLASH;
248 goto done;
249 }
250 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
251 rc = BOOT_EBADIMAGE;
252 goto done;
253 }
254 *size = hdr->ih_hdr_size + hdr->ih_img_size + info.it_tlv_tot;
255 rc = 0;
256
257done:
258 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300259 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600260}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300261#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600262
Fabio Utzigc08ed212017-06-20 19:28:36 -0300263static int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800264boot_read_image_header(int slot, struct image_header *out_hdr)
265{
266 const struct flash_area *fap;
267 int area_id;
268 int rc;
269
270 area_id = flash_area_id_from_image_slot(slot);
271 rc = flash_area_open(area_id, &fap);
272 if (rc != 0) {
273 rc = BOOT_EFLASH;
274 goto done;
275 }
276
277 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
278 if (rc != 0) {
279 rc = BOOT_EFLASH;
280 goto done;
281 }
282
283 rc = 0;
284
285done:
286 flash_area_close(fap);
287 return rc;
288}
289
290static int
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200291boot_read_image_headers(bool require_all)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800292{
293 int rc;
294 int i;
295
296 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Marti Bolivarf804f622017-06-12 15:41:48 -0400297 rc = boot_read_image_header(i, boot_img_hdr(&boot_data, i));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800298 if (rc != 0) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200299 /* If `require_all` is set, fail on any single fail, otherwise
300 * if at least the first slot's header was read successfully,
301 * then the boot loader can attempt a boot.
302 *
303 * Failure to read any headers is a fatal error.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800304 */
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200305 if (i > 0 && !require_all) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800306 return 0;
307 } else {
308 return rc;
309 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800310 }
311 }
312
313 return 0;
314}
315
316static uint8_t
317boot_write_sz(void)
318{
319 uint8_t elem_sz;
320 uint8_t align;
321
322 /* Figure out what size to write update status update as. The size depends
323 * on what the minimum write size is for scratch area, active image slot.
324 * We need to use the bigger of those 2 values.
325 */
David Vinczeba3bd602019-06-17 16:01:43 +0200326 elem_sz = flash_area_align(BOOT_IMG_AREA(&boot_data, BOOT_PRIMARY_SLOT));
327 align = flash_area_align(BOOT_SCRATCH_AREA(&boot_data));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800328 if (align > elem_sz) {
329 elem_sz = align;
330 }
331
332 return elem_sz;
333}
334
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200335/*
336 * Slots are compatible when all sectors that store upto to size of the image
337 * round up to sector size, in both slot's are able to fit in the scratch
338 * area, and have sizes that are a multiple of each other (powers of two
339 * presumably!).
340 */
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800341static int
342boot_slots_compatible(void)
343{
David Vincze2d736ad2019-02-18 11:50:22 +0100344 size_t num_sectors_primary;
345 size_t num_sectors_secondary;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200346 size_t sz0, sz1;
David Vincze2d736ad2019-02-18 11:50:22 +0100347 size_t primary_slot_sz, secondary_slot_sz;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200348 size_t scratch_sz;
349 size_t i, j;
350 int8_t smaller;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800351
David Vincze2d736ad2019-02-18 11:50:22 +0100352 num_sectors_primary =
353 boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT);
354 num_sectors_secondary =
355 boot_img_num_sectors(&boot_data, BOOT_SECONDARY_SLOT);
356 if ((num_sectors_primary > BOOT_MAX_IMG_SECTORS) ||
357 (num_sectors_secondary > BOOT_MAX_IMG_SECTORS)) {
Fabio Utziga1fae672018-03-30 10:52:38 -0300358 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800359 return 0;
360 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300361
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200362 scratch_sz = boot_scratch_area_size(&boot_data);
363
364 /*
365 * The following loop scans all sectors in a linear fashion, assuring that
366 * for each possible sector in each slot, it is able to fit in the other
367 * slot's sector or sectors. Slot's should be compatible as long as any
368 * number of a slot's sectors are able to fit into another, which only
369 * excludes cases where sector sizes are not a multiple of each other.
370 */
David Vincze2d736ad2019-02-18 11:50:22 +0100371 i = sz0 = primary_slot_sz = 0;
372 j = sz1 = secondary_slot_sz = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200373 smaller = 0;
David Vincze2d736ad2019-02-18 11:50:22 +0100374 while (i < num_sectors_primary || j < num_sectors_secondary) {
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200375 if (sz0 == sz1) {
David Vincze2d736ad2019-02-18 11:50:22 +0100376 sz0 += boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
377 sz1 += boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT, j);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200378 i++;
379 j++;
380 } else if (sz0 < sz1) {
David Vincze2d736ad2019-02-18 11:50:22 +0100381 sz0 += boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
382 /* Guarantee that multiple sectors of the secondary slot
383 * fit into the primary slot.
384 */
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200385 if (smaller == 2) {
386 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors");
387 return 0;
388 }
389 smaller = 1;
390 i++;
391 } else {
David Vincze2d736ad2019-02-18 11:50:22 +0100392 sz1 += boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT, j);
393 /* Guarantee that multiple sectors of the primary slot
394 * fit into the secondary slot.
395 */
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200396 if (smaller == 1) {
397 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors");
398 return 0;
399 }
400 smaller = 2;
401 j++;
402 }
403 if (sz0 == sz1) {
David Vincze2d736ad2019-02-18 11:50:22 +0100404 primary_slot_sz += sz0;
405 secondary_slot_sz += sz1;
406 /* Scratch has to fit each swap operation to the size of the larger
407 * sector among the primary slot and the secondary slot.
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200408 */
409 if (sz0 > scratch_sz || sz1 > scratch_sz) {
410 BOOT_LOG_WRN("Cannot upgrade: not all sectors fit inside scratch");
411 return 0;
412 }
413 smaller = sz0 = sz1 = 0;
414 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300415 }
416
David Vincze2d736ad2019-02-18 11:50:22 +0100417 if ((i != num_sectors_primary) ||
418 (j != num_sectors_secondary) ||
419 (primary_slot_sz != secondary_slot_sz)) {
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200420 BOOT_LOG_WRN("Cannot upgrade: slots are not compatible");
421 return 0;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800422 }
423
424 return 1;
425}
426
Christopher Collins92ea77f2016-12-12 15:59:26 -0800427/**
428 * Determines the sector layout of both image slots and the scratch area.
429 * This information is necessary for calculating the number of bytes to erase
430 * and copy during an image swap. The information collected during this
431 * function is used to populate the boot_data global.
432 */
433static int
434boot_read_sectors(void)
435{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800436 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800437
David Vincze2d736ad2019-02-18 11:50:22 +0100438 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_PRIMARY);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800439 if (rc != 0) {
440 return BOOT_EFLASH;
441 }
442
David Vincze2d736ad2019-02-18 11:50:22 +0100443 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_SECONDARY);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800444 if (rc != 0) {
445 return BOOT_EFLASH;
446 }
447
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200448 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_SCRATCH);
449 if (rc != 0) {
450 return BOOT_EFLASH;
451 }
452
Marti Bolivare10a7392017-06-14 16:20:07 -0400453 BOOT_WRITE_SZ(&boot_data) = boot_write_sz();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800454
455 return 0;
456}
457
458static uint32_t
459boot_status_internal_off(int idx, int state, int elem_sz)
460{
461 int idx_sz;
462
463 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
464
Fabio Utzig39000012018-07-30 12:40:20 -0300465 return (idx - BOOT_STATUS_IDX_0) * idx_sz +
466 (state - BOOT_STATUS_STATE_0) * elem_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800467}
468
469/**
470 * Reads the status of a partially-completed swap, if any. This is necessary
471 * to recover in case the boot lodaer was reset in the middle of a swap
472 * operation.
473 */
474static int
475boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs)
476{
477 uint32_t off;
478 uint8_t status;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300479 int max_entries;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800480 int found;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200481 int found_idx;
482 int invalid;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800483 int rc;
484 int i;
485
486 off = boot_status_off(fap);
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400487 max_entries = boot_status_entries(fap);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300488
Christopher Collins92ea77f2016-12-12 15:59:26 -0800489 found = 0;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200490 found_idx = 0;
491 invalid = 0;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300492 for (i = 0; i < max_entries; i++) {
Fabio Utzig178be542018-09-19 08:12:56 -0300493 rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(&boot_data),
494 &status, 1);
495 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800496 return BOOT_EFLASH;
497 }
498
Fabio Utzig178be542018-09-19 08:12:56 -0300499 if (rc == 1) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200500 if (found && !found_idx) {
501 found_idx = i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800502 }
503 } else if (!found) {
504 found = 1;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200505 } else if (found_idx) {
506 invalid = 1;
507 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800508 }
509 }
510
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200511 if (invalid) {
512 /* This means there was an error writing status on the last
513 * swap. Tell user and move on to validation!
514 */
515 BOOT_LOG_ERR("Detected inconsistent status!");
516
David Vincze2d736ad2019-02-18 11:50:22 +0100517#if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
518 /* With validation of the primary slot disabled, there is no way
519 * to be sure the swapped primary slot is OK, so abort!
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200520 */
521 assert(0);
522#endif
523 }
524
Christopher Collins92ea77f2016-12-12 15:59:26 -0800525 if (found) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200526 if (!found_idx) {
527 found_idx = i;
528 }
529 found_idx--;
Fabio Utzig39000012018-07-30 12:40:20 -0300530 bs->idx = (found_idx / BOOT_STATUS_STATE_COUNT) + 1;
531 bs->state = (found_idx % BOOT_STATUS_STATE_COUNT) + 1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800532 }
533
534 return 0;
535}
536
537/**
538 * Reads the boot status from the flash. The boot status contains
539 * the current state of an interrupted image copy operation. If the boot
540 * status is not present, or it indicates that previous copy finished,
541 * there is no operation in progress.
542 */
543static int
544boot_read_status(struct boot_status *bs)
545{
546 const struct flash_area *fap;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700547 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200548 uint8_t swap_info;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800549 int status_loc;
550 int area_id;
551 int rc;
552
553 memset(bs, 0, sizeof *bs);
Fabio Utzig39000012018-07-30 12:40:20 -0300554 bs->idx = BOOT_STATUS_IDX_0;
555 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700556 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800557
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700558#ifdef MCUBOOT_OVERWRITE_ONLY
559 /* Overwrite-only doesn't make use of the swap status area. */
560 return 0;
561#endif
562
Christopher Collins92ea77f2016-12-12 15:59:26 -0800563 status_loc = boot_status_source();
564 switch (status_loc) {
565 case BOOT_STATUS_SOURCE_NONE:
566 return 0;
567
568 case BOOT_STATUS_SOURCE_SCRATCH:
569 area_id = FLASH_AREA_IMAGE_SCRATCH;
570 break;
571
David Vincze2d736ad2019-02-18 11:50:22 +0100572 case BOOT_STATUS_SOURCE_PRIMARY_SLOT:
573 area_id = FLASH_AREA_IMAGE_PRIMARY;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800574 break;
575
576 default:
577 assert(0);
578 return BOOT_EBADARGS;
579 }
580
581 rc = flash_area_open(area_id, &fap);
582 if (rc != 0) {
583 return BOOT_EFLASH;
584 }
585
Fabio Utzig46490722017-09-04 15:34:32 -0300586 rc = boot_read_status_bytes(fap, bs);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700587 if (rc == 0) {
David Vinczee2453472019-06-17 12:31:59 +0200588 off = boot_swap_info_off(fap);
589 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700590 if (rc == 1) {
David Vinczee2453472019-06-17 12:31:59 +0200591 BOOT_SET_SWAP_INFO(swap_info, 0, BOOT_SWAP_TYPE_NONE);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700592 rc = 0;
593 }
David Vinczee2453472019-06-17 12:31:59 +0200594
595 /* Extract the swap type info */
596 bs->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700597 }
Fabio Utzig46490722017-09-04 15:34:32 -0300598
599 flash_area_close(fap);
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700600
Fabio Utzig46490722017-09-04 15:34:32 -0300601 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800602}
603
604/**
605 * Writes the supplied boot status to the flash file system. The boot status
606 * contains the current state of an in-progress image copy operation.
607 *
608 * @param bs The boot status to write.
609 *
610 * @return 0 on success; nonzero on failure.
611 */
612int
613boot_write_status(struct boot_status *bs)
614{
615 const struct flash_area *fap;
616 uint32_t off;
617 int area_id;
618 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300619 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700620 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300621 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800622
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300623 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100624 * the trailer. Since in the last step the primary slot is erased, the
625 * first two status writes go to the scratch which will be copied to
626 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300627 */
628
Fabio Utzig2473ac02017-05-02 12:45:02 -0300629 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800630 /* Write to scratch. */
631 area_id = FLASH_AREA_IMAGE_SCRATCH;
632 } else {
David Vincze2d736ad2019-02-18 11:50:22 +0100633 /* Write to the primary slot. */
634 area_id = FLASH_AREA_IMAGE_PRIMARY;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800635 }
636
637 rc = flash_area_open(area_id, &fap);
638 if (rc != 0) {
639 rc = BOOT_EFLASH;
640 goto done;
641 }
642
643 off = boot_status_off(fap) +
Marti Bolivare10a7392017-06-14 16:20:07 -0400644 boot_status_internal_off(bs->idx, bs->state,
645 BOOT_WRITE_SZ(&boot_data));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200646 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300647 erased_val = flash_area_erased_val(fap);
648 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700649 buf[0] = bs->state;
650
651 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800652 if (rc != 0) {
653 rc = BOOT_EFLASH;
654 goto done;
655 }
656
657 rc = 0;
658
659done:
660 flash_area_close(fap);
661 return rc;
662}
663
664/*
665 * Validate image hash/signature in a slot.
666 */
667static int
Fabio Utzigba829042018-09-18 08:29:34 -0300668boot_image_check(struct image_header *hdr, const struct flash_area *fap,
669 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800670{
David Browndb1d9d32017-01-06 11:07:54 -0700671 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigba829042018-09-18 08:29:34 -0300672 int rc;
673
674#ifndef MCUBOOT_ENC_IMAGES
675 (void)bs;
676 (void)rc;
677#else
David Vincze2d736ad2019-02-18 11:50:22 +0100678 if ((fap->fa_id == FLASH_AREA_IMAGE_SECONDARY) && IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300679 rc = boot_enc_load(hdr, fap, bs->enckey[1]);
680 if (rc < 0) {
681 return BOOT_EBADIMAGE;
682 }
683 if (rc == 0 && boot_enc_set_key(1, bs->enckey[1])) {
684 return BOOT_EBADIMAGE;
685 }
686 }
687#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800688
Christopher Collins92ea77f2016-12-12 15:59:26 -0800689 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
690 NULL, 0, NULL)) {
691 return BOOT_EBADIMAGE;
692 }
693 return 0;
694}
695
696static int
697split_image_check(struct image_header *app_hdr,
698 const struct flash_area *app_fap,
699 struct image_header *loader_hdr,
700 const struct flash_area *loader_fap)
701{
702 static void *tmpbuf;
703 uint8_t loader_hash[32];
704
705 if (!tmpbuf) {
706 tmpbuf = malloc(BOOT_TMPBUF_SZ);
707 if (!tmpbuf) {
708 return BOOT_ENOMEM;
709 }
710 }
711
712 if (bootutil_img_validate(loader_hdr, loader_fap, tmpbuf, BOOT_TMPBUF_SZ,
713 NULL, 0, loader_hash)) {
714 return BOOT_EBADIMAGE;
715 }
716
717 if (bootutil_img_validate(app_hdr, app_fap, tmpbuf, BOOT_TMPBUF_SZ,
718 loader_hash, 32, NULL)) {
719 return BOOT_EBADIMAGE;
720 }
721
722 return 0;
723}
724
Fabio Utzig338a19f2018-12-03 08:37:08 -0200725/*
726 * Check that a memory area consists of a given value.
727 */
728static inline bool
729boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300730{
731 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200732 uint8_t *p = (uint8_t *)data;
733 for (i = 0; i < len; i++) {
734 if (val != p[i]) {
735 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300736 }
737 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200738 return true;
739}
740
741static int
742boot_check_header_erased(int slot)
743{
744 const struct flash_area *fap;
745 struct image_header *hdr;
746 uint8_t erased_val;
747 int rc;
748
749 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
750 if (rc != 0) {
751 return -1;
752 }
753
754 erased_val = flash_area_erased_val(fap);
755 flash_area_close(fap);
756
757 hdr = boot_img_hdr(&boot_data, slot);
758 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
759 return -1;
760 }
761
762 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300763}
764
Christopher Collins92ea77f2016-12-12 15:59:26 -0800765static int
Fabio Utzigba829042018-09-18 08:29:34 -0300766boot_validate_slot(int slot, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800767{
768 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400769 struct image_header *hdr;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800770 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300771
David Brownd930ec62016-12-14 07:59:48 -0700772 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800773 if (rc != 0) {
774 return BOOT_EFLASH;
775 }
776
Fabio Utzig39000012018-07-30 12:40:20 -0300777 hdr = boot_img_hdr(&boot_data, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200778 if (boot_check_header_erased(slot) == 0 || (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100779 /* No bootable image in slot; continue booting from the primary slot. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200780 rc = -1;
781 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300782 }
783
Fabio Utzigba829042018-09-18 08:29:34 -0300784 if ((hdr->ih_magic != IMAGE_MAGIC || boot_image_check(hdr, fap, bs) != 0)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100785 if (slot != BOOT_PRIMARY_SLOT) {
David Brownb38e0442017-02-24 13:57:12 -0700786 flash_area_erase(fap, 0, fap->fa_size);
David Vincze2d736ad2019-02-18 11:50:22 +0100787 /* Image in the secondary slot is invalid. Erase the image and
788 * continue booting from the primary slot.
David Brownb38e0442017-02-24 13:57:12 -0700789 */
790 }
David Vincze2d736ad2019-02-18 11:50:22 +0100791 BOOT_LOG_ERR("Image in the %s slot is not valid!",
792 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
Fabio Utzig338a19f2018-12-03 08:37:08 -0200793 rc = -1;
794 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800795 }
796
David Vincze2d736ad2019-02-18 11:50:22 +0100797 /* Image in the secondary slot is valid. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200798 rc = 0;
799
800out:
801 flash_area_close(fap);
802 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800803}
804
805/**
806 * Determines which swap operation to perform, if any. If it is determined
David Vincze2d736ad2019-02-18 11:50:22 +0100807 * that a swap operation is required, the image in the secondary slot is checked
808 * for validity. If the image in the secondary slot is invalid, it is erased,
809 * and a swap type of "none" is indicated.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800810 *
811 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
812 */
813static int
Fabio Utzigba829042018-09-18 08:29:34 -0300814boot_validated_swap_type(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800815{
816 int swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800817
818 swap_type = boot_swap_type();
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300819 switch (swap_type) {
820 case BOOT_SWAP_TYPE_TEST:
821 case BOOT_SWAP_TYPE_PERM:
822 case BOOT_SWAP_TYPE_REVERT:
David Vincze2d736ad2019-02-18 11:50:22 +0100823 /* Boot loader wants to switch to the secondary slot.
824 * Ensure image is valid.
825 */
826 if (boot_validate_slot(BOOT_SECONDARY_SLOT, bs) != 0) {
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300827 swap_type = BOOT_SWAP_TYPE_FAIL;
828 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800829 }
830
831 return swap_type;
832}
833
834/**
835 * Calculates the number of sectors the scratch area can contain. A "last"
836 * source sector is specified because images are copied backwards in flash
837 * (final index to index number 0).
838 *
839 * @param last_sector_idx The index of the last source sector
840 * (inclusive).
841 * @param out_first_sector_idx The index of the first source sector
842 * (inclusive) gets written here.
843 *
844 * @return The number of bytes comprised by the
845 * [first-sector, last-sector] range.
846 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300847#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800848static uint32_t
849boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
850{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400851 size_t scratch_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800852 uint32_t new_sz;
853 uint32_t sz;
854 int i;
855
856 sz = 0;
857
Marti Bolivard3269fd2017-06-12 16:31:12 -0400858 scratch_sz = boot_scratch_area_size(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800859 for (i = last_sector_idx; i >= 0; i--) {
David Vincze2d736ad2019-02-18 11:50:22 +0100860 new_sz = sz + boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200861 /*
David Vincze2d736ad2019-02-18 11:50:22 +0100862 * The secondary slot is not being checked here, because
863 * `boot_slots_compatible` already provides assurance that the copy size
864 * will be compatible with the primary slot and scratch.
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200865 */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400866 if (new_sz > scratch_sz) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800867 break;
868 }
869 sz = new_sz;
870 }
871
872 /* i currently refers to a sector that doesn't fit or it is -1 because all
873 * sectors have been processed. In both cases, exclude sector i.
874 */
875 *out_first_sector_idx = i + 1;
876 return sz;
877}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300878#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800879
880/**
881 * Erases a region of flash.
882 *
Fabio Utzigba829042018-09-18 08:29:34 -0300883 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800884 * @param off The offset within the flash area to start the
885 * erase.
886 * @param sz The number of bytes to erase.
887 *
888 * @return 0 on success; nonzero on failure.
889 */
Fabio Utzigba829042018-09-18 08:29:34 -0300890static inline int
891boot_erase_sector(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800892{
Fabio Utzigba829042018-09-18 08:29:34 -0300893 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800894}
895
896/**
897 * Copies the contents of one flash region to another. You must erase the
898 * destination region prior to calling this function.
899 *
900 * @param flash_area_id_src The ID of the source flash area.
901 * @param flash_area_id_dst The ID of the destination flash area.
902 * @param off_src The offset within the source flash area to
903 * copy from.
904 * @param off_dst The offset within the destination flash area to
905 * copy to.
906 * @param sz The number of bytes to copy.
907 *
908 * @return 0 on success; nonzero on failure.
909 */
910static int
Fabio Utzigba829042018-09-18 08:29:34 -0300911boot_copy_sector(const struct flash_area *fap_src,
912 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800913 uint32_t off_src, uint32_t off_dst, uint32_t sz)
914{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800915 uint32_t bytes_copied;
916 int chunk_sz;
917 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -0300918#ifdef MCUBOOT_ENC_IMAGES
919 uint32_t off;
920 size_t blk_off;
921 struct image_header *hdr;
922 uint16_t idx;
923 uint32_t blk_sz;
924#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800925
926 static uint8_t buf[1024];
927
Christopher Collins92ea77f2016-12-12 15:59:26 -0800928 bytes_copied = 0;
929 while (bytes_copied < sz) {
930 if (sz - bytes_copied > sizeof buf) {
931 chunk_sz = sizeof buf;
932 } else {
933 chunk_sz = sz - bytes_copied;
934 }
935
936 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
937 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300938 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800939 }
940
Fabio Utzigba829042018-09-18 08:29:34 -0300941#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +0100942 if ((fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY) ||
943 (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY)) {
944 /* assume the secondary slot as src, needs decryption */
945 hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -0300946 off = off_src;
David Vincze2d736ad2019-02-18 11:50:22 +0100947 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY) {
948 /* might need encryption (metadata from the primary slot) */
949 hdr = boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -0300950 off = off_dst;
951 }
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200952 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300953 blk_sz = chunk_sz;
954 idx = 0;
955 if (off + bytes_copied < hdr->ih_hdr_size) {
956 /* do not decrypt header */
957 blk_off = 0;
958 blk_sz = chunk_sz - hdr->ih_hdr_size;
959 idx = hdr->ih_hdr_size;
960 } else {
961 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
962 }
963 if (off + bytes_copied + chunk_sz > hdr->ih_hdr_size + hdr->ih_img_size) {
964 /* do not decrypt TLVs */
965 if (off + bytes_copied >= hdr->ih_hdr_size + hdr->ih_img_size) {
966 blk_sz = 0;
967 } else {
968 blk_sz = (hdr->ih_hdr_size + hdr->ih_img_size) - (off + bytes_copied);
969 }
970 }
971 boot_encrypt(fap_src, (off + bytes_copied + idx) - hdr->ih_hdr_size,
972 blk_sz, blk_off, &buf[idx]);
973 }
974 }
975#endif
976
Christopher Collins92ea77f2016-12-12 15:59:26 -0800977 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
978 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300979 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800980 }
981
982 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -0300983
984 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800985 }
986
Fabio Utzigba829042018-09-18 08:29:34 -0300987 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800988}
989
David Brown6b1b3b92017-09-19 08:59:10 -0600990#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -0300991static inline int
Fabio Utzigba829042018-09-18 08:29:34 -0300992boot_status_init(const struct flash_area *fap, const struct boot_status *bs)
Fabio Utzig2473ac02017-05-02 12:45:02 -0300993{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300994 struct boot_swap_state swap_state;
995 int rc;
996
Christopher Collins2c88e692019-05-22 15:10:14 -0700997 BOOT_LOG_DBG("initializing status; fa_id=%d", fap->fa_id);
998
David Vincze2d736ad2019-02-18 11:50:22 +0100999 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY, &swap_state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001000 assert(rc == 0);
1001
Christopher Collinsa1c12042019-05-23 14:00:28 -07001002 if (bs->swap_type != BOOT_SWAP_TYPE_NONE) {
David Vinczee2453472019-06-17 12:31:59 +02001003 rc = boot_write_swap_info(fap,
1004 bs->swap_type,
David Vinczeba3bd602019-06-17 16:01:43 +02001005 current_image);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001006 assert(rc == 0);
1007 }
1008
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001009 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001010 rc = boot_write_image_ok(fap);
1011 assert(rc == 0);
1012 }
1013
Fabio Utzig46490722017-09-04 15:34:32 -03001014 rc = boot_write_swap_size(fap, bs->swap_size);
1015 assert(rc == 0);
1016
Fabio Utzigba829042018-09-18 08:29:34 -03001017#ifdef MCUBOOT_ENC_IMAGES
1018 rc = boot_write_enc_key(fap, 0, bs->enckey[0]);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001019 assert(rc == 0);
1020
Fabio Utzigba829042018-09-18 08:29:34 -03001021 rc = boot_write_enc_key(fap, 1, bs->enckey[1]);
1022 assert(rc == 0);
1023#endif
1024
1025 rc = boot_write_magic(fap);
1026 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001027
1028 return 0;
1029}
Christopher Collinsa1c12042019-05-23 14:00:28 -07001030
David Brown6b1b3b92017-09-19 08:59:10 -06001031#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001032
Fabio Utzig358c9352017-07-25 22:10:45 -03001033#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -03001034static int
Fabio Utziged0ca432019-01-23 14:50:11 -02001035boot_erase_trailer_sectors(const struct flash_area *fap)
Fabio Utzig2473ac02017-05-02 12:45:02 -03001036{
1037 uint8_t slot;
Fabio Utziged0ca432019-01-23 14:50:11 -02001038 uint32_t sector;
1039 uint32_t trailer_sz;
1040 uint32_t total_sz;
1041 uint32_t off;
1042 uint32_t sz;
David Vinczeb75c12a2019-03-22 14:58:33 +01001043 int fa_id_primary;
1044 int fa_id_secondary;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001045 int rc;
1046
Christopher Collins2c88e692019-05-22 15:10:14 -07001047 BOOT_LOG_DBG("erasing trailer; fa_id=%d", fap->fa_id);
1048
David Vinczeb75c12a2019-03-22 14:58:33 +01001049 fa_id_primary = flash_area_id_from_image_slot(BOOT_PRIMARY_SLOT);
1050 fa_id_secondary = flash_area_id_from_image_slot(BOOT_SECONDARY_SLOT);
1051
1052 if (fap->fa_id == fa_id_primary) {
David Vincze2d736ad2019-02-18 11:50:22 +01001053 slot = BOOT_PRIMARY_SLOT;
David Vinczeb75c12a2019-03-22 14:58:33 +01001054 } else if (fap->fa_id == fa_id_secondary) {
David Vincze2d736ad2019-02-18 11:50:22 +01001055 slot = BOOT_SECONDARY_SLOT;
David Vinczeb75c12a2019-03-22 14:58:33 +01001056 } else {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001057 return BOOT_EFLASH;
1058 }
1059
Fabio Utziged0ca432019-01-23 14:50:11 -02001060 /* delete starting from last sector and moving to beginning */
1061 sector = boot_img_num_sectors(&boot_data, slot) - 1;
Christopher Collins2adef702019-05-22 14:37:31 -07001062 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Fabio Utziged0ca432019-01-23 14:50:11 -02001063 total_sz = 0;
1064 do {
1065 sz = boot_img_sector_size(&boot_data, slot, sector);
1066 off = boot_img_sector_off(&boot_data, slot, sector);
1067 rc = boot_erase_sector(fap, off, sz);
1068 assert(rc == 0);
1069
1070 sector--;
1071 total_sz += sz;
1072 } while (total_sz < trailer_sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001073
1074 return rc;
1075}
Fabio Utzig358c9352017-07-25 22:10:45 -03001076#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001077
Christopher Collins92ea77f2016-12-12 15:59:26 -08001078/**
1079 * Swaps the contents of two flash regions within the two image slots.
1080 *
1081 * @param idx The index of the first sector in the range of
1082 * sectors being swapped.
1083 * @param sz The number of bytes to swap.
1084 * @param bs The current boot status. This struct gets
1085 * updated according to the outcome.
1086 *
1087 * @return 0 on success; nonzero on failure.
1088 */
Fabio Utzig3488eef2017-06-12 10:25:43 -03001089#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -08001090static void
Christopher Collins92ea77f2016-12-12 15:59:26 -08001091boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
1092{
David Vincze2d736ad2019-02-18 11:50:22 +01001093 const struct flash_area *fap_primary_slot;
1094 const struct flash_area *fap_secondary_slot;
Fabio Utzigba829042018-09-18 08:29:34 -03001095 const struct flash_area *fap_scratch;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001096 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001097 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001098 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001099 uint32_t scratch_trailer_off;
1100 struct boot_swap_state swap_state;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001101 size_t last_sector;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001102 bool erase_scratch;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001103 int rc;
1104
1105 /* Calculate offset from start of image area. */
David Vincze2d736ad2019-02-18 11:50:22 +01001106 img_off = boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT, idx);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001107
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001108 copy_sz = sz;
Christopher Collins2adef702019-05-22 14:37:31 -07001109 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Fabio Utzig9678c972017-05-23 11:28:56 -04001110
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001111 /* sz in this function is always sized on a multiple of the sector size.
1112 * The check against the start offset of the last sector
Fabio Utzig9678c972017-05-23 11:28:56 -04001113 * is to determine if we're swapping the last sector. The last sector
1114 * needs special handling because it's where the trailer lives. If we're
1115 * copying it, we need to use scratch to write the trailer temporarily.
1116 *
1117 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
1118 * controls if special handling is needed (swapping last sector).
1119 */
David Vincze2d736ad2019-02-18 11:50:22 +01001120 last_sector = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT) - 1;
David Vinczeba3bd602019-06-17 16:01:43 +02001121 if ((img_off + sz) >
1122 boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT, last_sector)) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001123 copy_sz -= trailer_sz;
1124 }
1125
Fabio Utzig39000012018-07-30 12:40:20 -03001126 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001127
David Vincze2d736ad2019-02-18 11:50:22 +01001128 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001129 assert (rc == 0);
1130
David Vincze2d736ad2019-02-18 11:50:22 +01001131 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001132 assert (rc == 0);
1133
1134 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch);
1135 assert (rc == 0);
1136
Fabio Utzig39000012018-07-30 12:40:20 -03001137 if (bs->state == BOOT_STATUS_STATE_0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001138 BOOT_LOG_DBG("erasing scratch area");
Christopher Collinsa1c12042019-05-23 14:00:28 -07001139 rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size);
Christopher Collins4772ac42017-02-27 20:08:01 -08001140 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001141
Fabio Utzig39000012018-07-30 12:40:20 -03001142 if (bs->idx == BOOT_STATUS_IDX_0) {
Christopher Collinsa1c12042019-05-23 14:00:28 -07001143 /* Write a trailer to the scratch area, even if we don't need the
1144 * scratch area for status. We need a temporary place to store the
1145 * `swap-type` while we erase the primary trailer.
1146 */
1147 rc = boot_status_init(fap_scratch, bs);
1148 assert(rc == 0);
1149
1150 if (!bs->use_scratch) {
1151 /* Prepare the primary status area... here it is known that the
1152 * last sector is not being used by the image data so it's safe
1153 * to erase.
Fabio Utzig2473ac02017-05-02 12:45:02 -03001154 */
David Vincze2d736ad2019-02-18 11:50:22 +01001155 rc = boot_erase_trailer_sectors(fap_primary_slot);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001156 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001157
Christopher Collinsa1c12042019-05-23 14:00:28 -07001158 rc = boot_status_init(fap_primary_slot, bs);
1159 assert(rc == 0);
1160
1161 /* Erase the temporary trailer from the scratch area. */
1162 rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size);
1163 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001164 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001165 }
1166
Christopher Collinsa1c12042019-05-23 14:00:28 -07001167 rc = boot_copy_sector(fap_secondary_slot, fap_scratch,
1168 img_off, 0, copy_sz);
1169 assert(rc == 0);
1170
Fabio Utzig39000012018-07-30 12:40:20 -03001171 bs->state = BOOT_STATUS_STATE_1;
Christopher Collins4772ac42017-02-27 20:08:01 -08001172 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001173 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001174 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001175
Fabio Utzig39000012018-07-30 12:40:20 -03001176 if (bs->state == BOOT_STATUS_STATE_1) {
David Vincze2d736ad2019-02-18 11:50:22 +01001177 rc = boot_erase_sector(fap_secondary_slot, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001178 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001179
David Vincze2d736ad2019-02-18 11:50:22 +01001180 rc = boot_copy_sector(fap_primary_slot, fap_secondary_slot,
1181 img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001182 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001183
Fabio Utzig39000012018-07-30 12:40:20 -03001184 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001185 /* If not all sectors of the slot are being swapped,
David Vincze2d736ad2019-02-18 11:50:22 +01001186 * guarantee here that only the primary slot will have the state.
Fabio Utzig2473ac02017-05-02 12:45:02 -03001187 */
David Vincze2d736ad2019-02-18 11:50:22 +01001188 rc = boot_erase_trailer_sectors(fap_secondary_slot);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001189 assert(rc == 0);
1190 }
1191
Fabio Utzig39000012018-07-30 12:40:20 -03001192 bs->state = BOOT_STATUS_STATE_2;
Christopher Collins4772ac42017-02-27 20:08:01 -08001193 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001194 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001195 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001196
Fabio Utzig39000012018-07-30 12:40:20 -03001197 if (bs->state == BOOT_STATUS_STATE_2) {
David Vincze2d736ad2019-02-18 11:50:22 +01001198 rc = boot_erase_sector(fap_primary_slot, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001199 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001200
Christopher Collinsa1c12042019-05-23 14:00:28 -07001201 /* NOTE: If this is the final sector, we exclude the image trailer from
1202 * this copy (copy_sz was truncated earlier).
1203 */
David Vincze2d736ad2019-02-18 11:50:22 +01001204 rc = boot_copy_sector(fap_scratch, fap_primary_slot,
1205 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001206 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001207
Fabio Utzig94d998c2017-05-22 11:02:41 -04001208 if (bs->use_scratch) {
Fabio Utzigba829042018-09-18 08:29:34 -03001209 scratch_trailer_off = boot_status_off(fap_scratch);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001210
1211 /* copy current status that is being maintained in scratch */
David Vincze2d736ad2019-02-18 11:50:22 +01001212 rc = boot_copy_sector(fap_scratch, fap_primary_slot,
1213 scratch_trailer_off, img_off + copy_sz,
Marti Bolivare10a7392017-06-14 16:20:07 -04001214 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data));
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001215 BOOT_STATUS_ASSERT(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001216
Fabio Utzig2473ac02017-05-02 12:45:02 -03001217 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1218 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001219 assert(rc == 0);
1220
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001221 if (swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze2d736ad2019-02-18 11:50:22 +01001222 rc = boot_write_image_ok(fap_primary_slot);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001223 assert(rc == 0);
1224 }
1225
Christopher Collinsa1c12042019-05-23 14:00:28 -07001226 if (swap_state.swap_type != BOOT_SWAP_TYPE_NONE) {
David Vinczee2453472019-06-17 12:31:59 +02001227 rc = boot_write_swap_info(fap_primary_slot,
1228 swap_state.swap_type,
David Vinczeba3bd602019-06-17 16:01:43 +02001229 current_image);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001230 assert(rc == 0);
1231 }
1232
David Vincze2d736ad2019-02-18 11:50:22 +01001233 rc = boot_write_swap_size(fap_primary_slot, bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001234 assert(rc == 0);
1235
Fabio Utzigba829042018-09-18 08:29:34 -03001236#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +01001237 rc = boot_write_enc_key(fap_primary_slot, 0, bs->enckey[0]);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001238 assert(rc == 0);
1239
David Vincze2d736ad2019-02-18 11:50:22 +01001240 rc = boot_write_enc_key(fap_primary_slot, 1, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001241 assert(rc == 0);
1242#endif
David Vincze2d736ad2019-02-18 11:50:22 +01001243 rc = boot_write_magic(fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001244 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001245 }
1246
Christopher Collinsa1c12042019-05-23 14:00:28 -07001247 /* If we wrote a trailer to the scratch area, erase it after we persist
1248 * a trailer to the primary slot. We do this to prevent mcuboot from
1249 * reading a stale status from the scratch area in case of immediate
1250 * reset.
1251 */
1252 erase_scratch = bs->use_scratch;
1253 bs->use_scratch = 0;
1254
Christopher Collins92ea77f2016-12-12 15:59:26 -08001255 bs->idx++;
Fabio Utzig39000012018-07-30 12:40:20 -03001256 bs->state = BOOT_STATUS_STATE_0;
Christopher Collins4772ac42017-02-27 20:08:01 -08001257 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001258 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001259
1260 if (erase_scratch) {
1261 rc = boot_erase_sector(fap_scratch, 0, sz);
1262 assert(rc == 0);
1263 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001264 }
Fabio Utzigba829042018-09-18 08:29:34 -03001265
David Vincze2d736ad2019-02-18 11:50:22 +01001266 flash_area_close(fap_primary_slot);
1267 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001268 flash_area_close(fap_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001269}
Fabio Utzig3488eef2017-06-12 10:25:43 -03001270#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001271
1272/**
David Vincze2d736ad2019-02-18 11:50:22 +01001273 * Overwrite primary slot with the image contained in the secondary slot.
1274 * If a prior copy operation was interrupted by a system reset, this function
1275 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001276 *
1277 * @param bs The current boot status. This function reads
1278 * this struct to determine if it is resuming
1279 * an interrupted swap operation. This
1280 * function writes the updated status to this
1281 * function on return.
1282 *
1283 * @return 0 on success; nonzero on failure.
1284 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001285#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001286static int
1287boot_copy_image(struct boot_status *bs)
1288{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001289 size_t sect_count;
1290 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001291 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001292 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001293 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001294 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +01001295 const struct flash_area *fap_primary_slot;
1296 const struct flash_area *fap_secondary_slot;
1297
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001298 (void)bs;
1299
Fabio Utzig13d9e352017-10-05 20:32:31 -03001300#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1301 uint32_t src_size = 0;
David Vincze2d736ad2019-02-18 11:50:22 +01001302 rc = boot_read_image_size(BOOT_SECONDARY_SLOT,
1303 boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT),
1304 &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001305 assert(rc == 0);
1306#endif
David Brown17609d82017-05-05 09:41:34 -06001307
David Vincze2d736ad2019-02-18 11:50:22 +01001308 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1309 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -06001310
David Vincze2d736ad2019-02-18 11:50:22 +01001311 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001312 assert (rc == 0);
1313
David Vincze2d736ad2019-02-18 11:50:22 +01001314 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001315 assert (rc == 0);
1316
David Vincze2d736ad2019-02-18 11:50:22 +01001317 sect_count = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001318 for (sect = 0, size = 0; sect < sect_count; sect++) {
David Vincze2d736ad2019-02-18 11:50:22 +01001319 this_size = boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, sect);
1320 rc = boot_erase_sector(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001321 assert(rc == 0);
1322
1323 size += this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001324
1325#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1326 if (size >= src_size) {
1327 break;
1328 }
1329#endif
David Brown17609d82017-05-05 09:41:34 -06001330 }
1331
Fabio Utzigba829042018-09-18 08:29:34 -03001332#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +01001333 if (IS_ENCRYPTED(boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT))) {
1334 rc = boot_enc_load(boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT),
1335 fap_secondary_slot,
1336 bs->enckey[1]);
1337
Fabio Utzigba829042018-09-18 08:29:34 -03001338 if (rc < 0) {
1339 return BOOT_EBADIMAGE;
1340 }
Fabio Utzige641ea52018-12-03 10:37:53 -02001341 if (rc == 0 && boot_enc_set_key(1, bs->enckey[1])) {
Fabio Utzigba829042018-09-18 08:29:34 -03001342 return BOOT_EBADIMAGE;
1343 }
1344 }
1345#endif
1346
David Vincze2d736ad2019-02-18 11:50:22 +01001347 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1348 size);
1349 rc = boot_copy_sector(fap_secondary_slot, fap_primary_slot, 0, 0, size);
David Brown17609d82017-05-05 09:41:34 -06001350
Fabio Utzig13d9e352017-10-05 20:32:31 -03001351 /*
1352 * Erases header and trailer. The trailer is erased because when a new
1353 * image is written without a trailer as is the case when using newt, the
1354 * trailer that was left might trigger a new upgrade.
1355 */
Christopher Collins2c88e692019-05-22 15:10:14 -07001356 BOOT_LOG_DBG("erasing secondary header");
David Vincze2d736ad2019-02-18 11:50:22 +01001357 rc = boot_erase_sector(fap_secondary_slot,
1358 boot_img_sector_off(&boot_data,
1359 BOOT_SECONDARY_SLOT, 0),
1360 boot_img_sector_size(&boot_data,
1361 BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001362 assert(rc == 0);
David Vincze2d736ad2019-02-18 11:50:22 +01001363 last_sector = boot_img_num_sectors(&boot_data, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -07001364 BOOT_LOG_DBG("erasing secondary trailer");
David Vincze2d736ad2019-02-18 11:50:22 +01001365 rc = boot_erase_sector(fap_secondary_slot,
1366 boot_img_sector_off(&boot_data,
1367 BOOT_SECONDARY_SLOT, last_sector),
1368 boot_img_sector_size(&boot_data,
1369 BOOT_SECONDARY_SLOT, last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001370 assert(rc == 0);
1371
David Vincze2d736ad2019-02-18 11:50:22 +01001372 flash_area_close(fap_primary_slot);
1373 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001374
David Vincze2d736ad2019-02-18 11:50:22 +01001375 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001376
1377 return 0;
1378}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001379#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001380
Christopher Collinsa1c12042019-05-23 14:00:28 -07001381#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -03001382/**
1383 * Swaps the two images in flash. If a prior copy operation was interrupted
1384 * by a system reset, this function completes that operation.
1385 *
1386 * @param bs The current boot status. This function reads
1387 * this struct to determine if it is resuming
1388 * an interrupted swap operation. This
1389 * function writes the updated status to this
1390 * function on return.
1391 *
1392 * @return 0 on success; nonzero on failure.
1393 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001394static int
Fabio Utzig338a19f2018-12-03 08:37:08 -02001395boot_swap_image(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001396{
1397 uint32_t sz;
1398 int first_sector_idx;
1399 int last_sector_idx;
David Vincze2d736ad2019-02-18 11:50:22 +01001400 int last_idx_secondary_slot;
Fabio Utzigcd5774b2017-11-29 10:18:26 -02001401 uint32_t swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001402 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001403#ifdef MCUBOOT_ENC_IMAGES
1404 const struct flash_area *fap;
1405 uint8_t slot;
1406 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001407#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001408 uint32_t size;
1409 uint32_t copy_size;
David Vincze2d736ad2019-02-18 11:50:22 +01001410 uint32_t primary_slot_size;
1411 uint32_t secondary_slot_size;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001412 int rc;
1413
1414 /* FIXME: just do this if asked by user? */
1415
1416 size = copy_size = 0;
1417
Fabio Utzig39000012018-07-30 12:40:20 -03001418 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Fabio Utzig46490722017-09-04 15:34:32 -03001419 /*
1420 * No swap ever happened, so need to find the largest image which
1421 * will be used to determine the amount of sectors to swap.
1422 */
David Vincze2d736ad2019-02-18 11:50:22 +01001423 hdr = boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001424 if (hdr->ih_magic == IMAGE_MAGIC) {
David Vincze2d736ad2019-02-18 11:50:22 +01001425 rc = boot_read_image_size(BOOT_PRIMARY_SLOT, hdr, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001426 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001427 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001428
Fabio Utzigba829042018-09-18 08:29:34 -03001429#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001430 if (IS_ENCRYPTED(hdr)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001431 fap = BOOT_IMG_AREA(&boot_data, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001432 rc = boot_enc_load(hdr, fap, bs->enckey[0]);
1433 assert(rc >= 0);
1434
1435 if (rc == 0) {
1436 rc = boot_enc_set_key(0, bs->enckey[0]);
1437 assert(rc == 0);
1438 } else {
1439 rc = 0;
1440 }
1441 } else {
1442 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1443 }
1444#endif
1445
David Vincze2d736ad2019-02-18 11:50:22 +01001446 hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001447 if (hdr->ih_magic == IMAGE_MAGIC) {
David Vincze2d736ad2019-02-18 11:50:22 +01001448 rc = boot_read_image_size(BOOT_SECONDARY_SLOT, hdr, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001449 assert(rc == 0);
1450 }
1451
Fabio Utzigba829042018-09-18 08:29:34 -03001452#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +01001453 hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001454 if (IS_ENCRYPTED(hdr)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001455 fap = BOOT_IMG_AREA(&boot_data, BOOT_SECONDARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001456 rc = boot_enc_load(hdr, fap, bs->enckey[1]);
1457 assert(rc >= 0);
1458
1459 if (rc == 0) {
1460 rc = boot_enc_set_key(1, bs->enckey[1]);
1461 assert(rc == 0);
1462 } else {
1463 rc = 0;
1464 }
1465 } else {
1466 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1467 }
1468#endif
1469
Fabio Utzig46490722017-09-04 15:34:32 -03001470 if (size > copy_size) {
1471 copy_size = size;
1472 }
1473
1474 bs->swap_size = copy_size;
1475 } else {
1476 /*
1477 * If a swap was under way, the swap_size should already be present
1478 * in the trailer...
1479 */
1480 rc = boot_read_swap_size(&bs->swap_size);
1481 assert(rc == 0);
1482
1483 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001484
1485#ifdef MCUBOOT_ENC_IMAGES
1486 for (slot = 0; slot <= 1; slot++) {
1487 rc = boot_read_enc_key(slot, bs->enckey[slot]);
1488 assert(rc == 0);
1489
1490 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001491 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001492 break;
1493 }
1494 }
1495
1496 if (i != BOOT_ENC_KEY_SIZE) {
1497 boot_enc_set_key(slot, bs->enckey[slot]);
1498 }
1499 }
1500#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001501 }
1502
David Vincze2d736ad2019-02-18 11:50:22 +01001503 primary_slot_size = 0;
1504 secondary_slot_size = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001505 last_sector_idx = 0;
David Vincze2d736ad2019-02-18 11:50:22 +01001506 last_idx_secondary_slot = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001507
1508 /*
1509 * Knowing the size of the largest image between both slots, here we
David Vincze2d736ad2019-02-18 11:50:22 +01001510 * find what is the last sector in the primary slot that needs swapping.
1511 * Since we already know that both slots are compatible, the secondary
1512 * slot's last sector is not really required after this check is finished.
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001513 */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001514 while (1) {
David Vincze2d736ad2019-02-18 11:50:22 +01001515 if ((primary_slot_size < copy_size) ||
1516 (primary_slot_size < secondary_slot_size)) {
1517 primary_slot_size += boot_img_sector_size(&boot_data,
1518 BOOT_PRIMARY_SLOT,
1519 last_sector_idx);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001520 }
David Vincze2d736ad2019-02-18 11:50:22 +01001521 if ((secondary_slot_size < copy_size) ||
1522 (secondary_slot_size < primary_slot_size)) {
1523 secondary_slot_size += boot_img_sector_size(&boot_data,
1524 BOOT_SECONDARY_SLOT,
1525 last_idx_secondary_slot);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001526 }
David Vincze2d736ad2019-02-18 11:50:22 +01001527 if (primary_slot_size >= copy_size &&
1528 secondary_slot_size >= copy_size &&
1529 primary_slot_size == secondary_slot_size) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001530 break;
1531 }
1532 last_sector_idx++;
David Vincze2d736ad2019-02-18 11:50:22 +01001533 last_idx_secondary_slot++;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001534 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001535
1536 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001537 while (last_sector_idx >= 0) {
1538 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
Fabio Utzig39000012018-07-30 12:40:20 -03001539 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001540 boot_swap_sectors(first_sector_idx, sz, bs);
1541 }
1542
1543 last_sector_idx = first_sector_idx - 1;
1544 swap_idx++;
1545 }
1546
David Vincze2d736ad2019-02-18 11:50:22 +01001547#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001548 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001549 BOOT_LOG_WRN("%d status write fails performing the swap",
1550 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001551 }
1552#endif
1553
Christopher Collins92ea77f2016-12-12 15:59:26 -08001554 return 0;
1555}
David Brown17609d82017-05-05 09:41:34 -06001556#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001557
1558/**
David Vincze2d736ad2019-02-18 11:50:22 +01001559 * Marks the image in the primary slot as fully copied.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001560 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001561#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001562static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001563boot_set_copy_done(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001564{
1565 const struct flash_area *fap;
1566 int rc;
1567
David Vincze2d736ad2019-02-18 11:50:22 +01001568 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001569 if (rc != 0) {
1570 return BOOT_EFLASH;
1571 }
1572
1573 rc = boot_write_copy_done(fap);
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001574 flash_area_close(fap);
1575 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001576}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001577#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001578
1579/**
David Vincze2d736ad2019-02-18 11:50:22 +01001580 * Marks a reverted image in the primary slot as confirmed. This is necessary to
1581 * ensure the status bytes from the image revert operation don't get processed
1582 * on a subsequent boot.
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001583 *
1584 * NOTE: image_ok is tested before writing because if there's a valid permanent
David Vincze2d736ad2019-02-18 11:50:22 +01001585 * image installed on the primary slot and the new image to be upgrade to has a
1586 * bad sig, image_ok would be overwritten.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001587 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001588#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001589static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001590boot_set_image_ok(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001591{
1592 const struct flash_area *fap;
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001593 struct boot_swap_state state;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001594 int rc;
1595
David Vincze2d736ad2019-02-18 11:50:22 +01001596 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001597 if (rc != 0) {
1598 return BOOT_EFLASH;
1599 }
1600
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001601 rc = boot_read_swap_state(fap, &state);
1602 if (rc != 0) {
1603 rc = BOOT_EFLASH;
1604 goto out;
1605 }
1606
1607 if (state.image_ok == BOOT_FLAG_UNSET) {
1608 rc = boot_write_image_ok(fap);
1609 }
1610
1611out:
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001612 flash_area_close(fap);
1613 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001614}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001615#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001616
David Vinczee32483f2019-06-13 10:46:24 +02001617#if (BOOT_IMAGE_NUMBER > 1)
1618/**
1619 * Check the image dependency whether it is satisfied and modify
1620 * the swap type if necessary.
1621 *
1622 * @param dep Image dependency which has to be verified.
1623 *
1624 * @return 0 on success; nonzero on failure.
1625 */
1626static int
1627boot_verify_single_dependency(struct image_dependency *dep)
1628{
1629 struct image_version *dep_version;
1630 size_t dep_slot;
1631 int rc;
1632
1633 /* Determine the source of the image which is the subject of
1634 * the dependency and get it's version. */
1635 dep_slot = (boot_data.swap_type[dep->image_id] != BOOT_SWAP_TYPE_NONE) ?
1636 BOOT_SECONDARY_SLOT : BOOT_PRIMARY_SLOT;
1637 dep_version = &boot_data.imgs[dep->image_id][dep_slot].hdr.ih_ver;
1638
1639 rc = boot_is_version_sufficient(&dep->image_min_version, dep_version);
1640 if (rc != 0) {
1641 /* Dependency not satisfied.
1642 * Modify the swap type to decrease the version number of the image
1643 * (which will be located in the primary slot after the boot process),
1644 * consequently the number of unsatisfied dependencies will be
1645 * decreased or remain the same.
1646 */
1647 switch (BOOT_SWAP_TYPE(&boot_data)) {
1648 case BOOT_SWAP_TYPE_TEST:
1649 case BOOT_SWAP_TYPE_PERM:
1650 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE;
1651 break;
1652 case BOOT_SWAP_TYPE_NONE:
1653 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_REVERT;
1654 break;
1655 default:
1656 break;
1657 }
1658 }
1659
1660 return rc;
1661}
1662
1663/**
1664 * Read all dependency TLVs of an image from the flash and verify
1665 * one after another to see if they are all satisfied.
1666 *
1667 * @param slot Image slot number.
1668 *
1669 * @return 0 on success; nonzero on failure.
1670 */
1671static int
1672boot_verify_all_dependency(uint32_t slot)
1673{
1674 const struct flash_area *fap;
1675 struct image_header *hdr;
1676 struct image_tlv_info info;
1677 struct image_tlv tlv;
1678 struct image_dependency dep;
1679 uint32_t off;
1680 uint32_t end;
1681 bool dep_tlvs_found = false;
1682 int rc;
1683
1684 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
1685 if (rc != 0) {
1686 rc = BOOT_EFLASH;
1687 goto done;
1688 }
1689
1690 hdr = boot_img_hdr(&boot_data, slot);
1691 /* The TLVs come after the image. */
1692 off = hdr->ih_hdr_size + hdr->ih_img_size;
1693
1694 /* The TLV area always starts with an image_tlv_info structure. */
1695 rc = flash_area_read(fap, off, &info, sizeof(info));
1696 if (rc != 0) {
1697 rc = BOOT_EFLASH;
1698 goto done;
1699 }
1700
1701 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
1702 rc = BOOT_EBADIMAGE;
1703 goto done;
1704 }
1705 end = off + info.it_tlv_tot;
1706 off += sizeof(info);
1707
1708 /* Traverse through all of the TLVs to find the dependency TLVs. */
1709 for (; off < end; off += sizeof(tlv) + tlv.it_len) {
1710 rc = flash_area_read(fap, off, &tlv, sizeof(tlv));
1711 if (rc != 0) {
1712 rc = BOOT_EFLASH;
1713 goto done;
1714 }
1715
1716 if (tlv.it_type == IMAGE_TLV_DEPENDENCY) {
1717 if (!dep_tlvs_found) {
1718 dep_tlvs_found = true;
1719 }
1720
1721 if (tlv.it_len != sizeof(dep)) {
1722 rc = BOOT_EBADIMAGE;
1723 goto done;
1724 }
1725
1726 rc = flash_area_read(fap, off + sizeof(tlv), &dep, tlv.it_len);
1727 if (rc != 0) {
1728 rc = BOOT_EFLASH;
1729 goto done;
1730 }
1731
1732 /* Verify dependency and modify the swap type if not satisfied. */
1733 rc = boot_verify_single_dependency(&dep);
1734 if (rc != 0) {
1735 /* Dependency not satisfied. */
1736 goto done;
1737 }
1738
1739 /* Dependency satisfied, no action needed.
1740 * Continue with the next TLV entry.
1741 */
1742 } else if (dep_tlvs_found) {
1743 /* The dependency TLVs are contiguous in the TLV area. If a
1744 * dependency had already been found and the last read TLV
1745 * has a different type then there are no more dependency TLVs.
1746 * The search can be finished.
1747 */
1748 break;
1749 }
1750 }
1751
1752done:
1753 flash_area_close(fap);
1754 return rc;
1755}
1756
1757/**
1758 * Verify whether the image dependencies in the TLV area are
1759 * all satisfied and modify the swap type if necessary.
1760 *
1761 * @return 0 if all dependencies are satisfied,
1762 * nonzero otherwise.
1763 */
1764static int
1765boot_verify_single_image_dependency(void)
1766{
1767 size_t slot;
1768
1769 /* Determine the source of the dependency TLVs. Those dependencies have to
1770 * be checked which belong to the image that will be located in the primary
1771 * slot after the firmware update process.
1772 */
1773 if (BOOT_SWAP_TYPE(&boot_data) != BOOT_SWAP_TYPE_NONE &&
1774 BOOT_SWAP_TYPE(&boot_data) != BOOT_SWAP_TYPE_FAIL) {
1775 slot = BOOT_SECONDARY_SLOT;
1776 } else {
1777 slot = BOOT_PRIMARY_SLOT;
1778 }
1779
1780 return boot_verify_all_dependency(slot);
1781}
1782
1783/**
1784 * Iterate over all the images and verify whether the image dependencies in the
1785 * TLV area are all satisfied and update the related swap type if necessary.
1786 */
1787static void
1788boot_verify_all_image_dependency(void)
1789{
1790 current_image = 0;
1791 int rc;
1792
1793 while (current_image < BOOT_IMAGE_NUMBER) {
1794 rc = boot_verify_single_image_dependency();
Fabio Utzigabec0732019-07-31 08:40:22 -03001795 if (rc == 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001796 /* All dependencies've been satisfied, continue with next image. */
1797 current_image++;
1798 } else if (rc == BOOT_EBADVERSION) {
1799 /* Dependency check needs to be restarted. */
1800 current_image = 0;
1801 } else {
1802 /* Other error happened, images are inconsistent */
1803 return;
1804 }
1805 }
1806}
1807#endif /* (BOOT_IMAGE_NUMBER > 1) */
1808
Christopher Collins92ea77f2016-12-12 15:59:26 -08001809/**
David Vinczeba3bd602019-06-17 16:01:43 +02001810 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001811 *
David Vinczeba3bd602019-06-17 16:01:43 +02001812 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001813 *
1814 * @return 0 on success; nonzero on failure.
1815 */
1816static int
David Vinczeba3bd602019-06-17 16:01:43 +02001817boot_perform_update(struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001818{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001819 int rc;
1820
David Vinczeba3bd602019-06-17 16:01:43 +02001821 /* At this point there are no aborted swaps. */
1822#if defined(MCUBOOT_OVERWRITE_ONLY)
1823 rc = boot_copy_image(bs);
1824#elif defined(MCUBOOT_BOOTSTRAP)
1825 /* Check if the image update was triggered by a bad image in the
1826 * primary slot (the validity of the image in the secondary slot had
1827 * already been checked).
1828 */
1829 if (boot_check_header_erased(BOOT_PRIMARY_SLOT) == 0 ||
1830 boot_validate_slot(BOOT_PRIMARY_SLOT, bs) != 0) {
1831 rc = boot_copy_image(bs);
1832 } else {
1833 rc = boot_swap_image(bs);
1834 }
1835#else
1836 rc = boot_swap_image(bs);
1837#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001838 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001839
1840#ifndef MCUBOOT_OVERWRITE_ONLY
1841 /* The following state needs image_ok be explicitly set after the
1842 * swap was finished to avoid a new revert.
1843 */
1844 if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_REVERT ||
1845 BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_PERM) {
1846 rc = boot_set_image_ok();
1847 if (rc != 0) {
1848 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
1849 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001850 }
1851
David Vinczeba3bd602019-06-17 16:01:43 +02001852 if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_TEST ||
1853 BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_PERM ||
1854 BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_REVERT) {
1855 rc = boot_set_copy_done();
1856 if (rc != 0) {
1857 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001858 }
David Vinczeba3bd602019-06-17 16:01:43 +02001859 }
1860#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001861
David Vinczeba3bd602019-06-17 16:01:43 +02001862 return rc;
1863}
1864
1865/**
1866 * Completes a previously aborted image swap.
1867 *
1868 * @param bs The current boot status.
1869 *
1870 * @return 0 on success; nonzero on failure.
1871 */
1872#if !defined(MCUBOOT_OVERWRITE_ONLY)
1873static int
1874boot_complete_partial_swap(struct boot_status *bs)
1875{
1876 int rc;
1877
1878 /* Determine the type of swap operation being resumed from the
1879 * `swap-type` trailer field.
1880 */
1881 rc = boot_swap_image(bs);
1882 assert(rc == 0);
1883
1884 BOOT_SWAP_TYPE(&boot_data) = bs->swap_type;
1885
1886 /* The following states need image_ok be explicitly set after the
1887 * swap was finished to avoid a new revert.
1888 */
1889 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1890 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
1891 rc = boot_set_image_ok();
1892 if (rc != 0) {
1893 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
1894 }
1895 }
1896
1897 if (bs->swap_type == BOOT_SWAP_TYPE_TEST ||
1898 bs->swap_type == BOOT_SWAP_TYPE_PERM ||
1899 bs->swap_type == BOOT_SWAP_TYPE_REVERT) {
1900 rc = boot_set_copy_done();
1901 if (rc != 0) {
1902 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
1903 }
1904 }
1905
1906 if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_PANIC) {
1907 BOOT_LOG_ERR("panic!");
1908 assert(0);
1909
1910 /* Loop forever... */
1911 while (1) {}
1912 }
1913
1914 return rc;
1915}
1916#endif /* !MCUBOOT_OVERWRITE_ONLY */
1917
1918#if (BOOT_IMAGE_NUMBER > 1)
1919/**
1920 * Review the validity of previously determined swap types of other images.
1921 *
1922 * @param aborted_swap The current image upgrade is a
1923 * partial/aborted swap.
1924 */
1925static void
1926boot_review_image_swap_types(bool aborted_swap)
1927{
1928 /* In that case if we rebooted in the middle of an image upgrade process, we
1929 * must review the validity of swap types, that were previously determined
1930 * for other images. The image_ok flag had not been set before the reboot
1931 * for any of the updated images (only the copy_done flag) and thus falsely
1932 * the REVERT swap type has been determined for the previous images that had
1933 * been updated before the reboot.
1934 *
1935 * There are two separate scenarios that we have to deal with:
1936 *
1937 * 1. The reboot has happened during swapping an image:
1938 * The current image upgrade has been determined as a
1939 * partial/aborted swap.
1940 * 2. The reboot has happened between two separate image upgrades:
1941 * In this scenario we must check the swap type of the current image.
1942 * In those cases if it is NONE or REVERT we cannot certainly determine
1943 * the fact of a reboot. In a consistent state images must move in the
1944 * same direction or stay in place, e.g. in practice REVERT and TEST
1945 * swap types cannot be present at the same time. If the swap type of
1946 * the current image is either TEST, PERM or FAIL we must review the
1947 * already determined swap types of other images and set each false
1948 * REVERT swap types to NONE (these images had been successfully
1949 * updated before the system rebooted between two separate image
1950 * upgrades).
1951 */
1952
1953 if (current_image == 0) {
1954 /* Nothing to do */
1955 return;
1956 }
1957
1958 if (!aborted_swap) {
1959 if ((BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_NONE) ||
1960 (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_REVERT)) {
1961 /* Nothing to do */
1962 return;
1963 }
1964 }
1965
1966 for (uint8_t i = 0; i < current_image; i++) {
1967 if (boot_data.swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
1968 boot_data.swap_type[i] = BOOT_SWAP_TYPE_NONE;
1969 }
1970 }
1971}
1972#endif
1973
1974/**
1975 * Prepare image to be updated if required.
1976 *
1977 * Prepare image to be updated if required with completing an image swap
1978 * operation if one was aborted and/or determining the type of the
1979 * swap operation. In case of any error set the swap type to NONE.
1980 *
1981 * @param bs Pointer where the read and possibly updated
1982 * boot status can be written to.
1983 */
1984static void
1985boot_prepare_image_for_update(struct boot_status *bs)
1986{
1987 int rc;
1988
1989 /* Determine the sector layout of the image slots and scratch area. */
1990 rc = boot_read_sectors();
1991 if (rc != 0) {
1992 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
1993 " - too small?", BOOT_MAX_IMG_SECTORS);
1994 /* Unable to determine sector layout, continue with next image
1995 * if there is one.
1996 */
1997 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE;
1998 return;
1999 }
2000
2001 /* Attempt to read an image header from each slot. */
2002 rc = boot_read_image_headers(false);
2003 if (rc != 0) {
2004 /* Continue with next image if there is one. */
2005 BOOT_LOG_WRN("Failed reading image headers; Image=%u", current_image);
2006 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE;
2007 return;
2008 }
2009
2010 /* If the current image's slots aren't compatible, no swap is possible.
2011 * Just boot into primary slot.
2012 */
2013 if (boot_slots_compatible()) {
2014
2015 rc = boot_read_status(bs);
2016 if (rc != 0) {
2017 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
2018 current_image);
2019 /* Continue with next image if there is one. */
2020 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE;
2021 return;
2022 }
2023
2024 /* Determine if we rebooted in the middle of an image swap
2025 * operation. If a partial swap was detected, complete it.
2026 */
2027 if (bs->idx != BOOT_STATUS_IDX_0 || bs->state != BOOT_STATUS_STATE_0) {
2028
2029#if (BOOT_IMAGE_NUMBER > 1)
2030 boot_review_image_swap_types(true);
2031#endif
2032
2033#ifdef MCUBOOT_OVERWRITE_ONLY
2034 /* Should never arrive here, overwrite-only mode has
2035 * no swap state.
2036 */
2037 assert(0);
2038#else
2039 /* Determine the type of swap operation being resumed from the
2040 * `swap-type` trailer field.
2041 */
2042 rc = boot_complete_partial_swap(bs);
2043 assert(rc == 0);
2044#endif
2045 /* Attempt to read an image header from each slot. Ensure that
2046 * image headers in slots are aligned with headers in boot_data.
2047 */
2048 rc = boot_read_image_headers(false);
2049 assert(rc == 0);
2050
2051 /* Swap has finished set to NONE */
2052 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE;
2053 } else {
2054 /* There was no partial swap, determine swap type. */
2055 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
2056 BOOT_SWAP_TYPE(&boot_data) = boot_validated_swap_type(bs);
2057 } else if (boot_validate_slot(BOOT_SECONDARY_SLOT, bs) != 0) {
2058 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_FAIL;
2059 } else {
2060 BOOT_SWAP_TYPE(&boot_data) = bs->swap_type;
2061 }
2062
2063#if (BOOT_IMAGE_NUMBER > 1)
2064 boot_review_image_swap_types(false);
2065#endif
2066
2067#ifdef MCUBOOT_BOOTSTRAP
2068 if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_NONE) {
2069 /* Header checks are done first because they are
2070 * inexpensive. Since overwrite-only copies starting from
2071 * offset 0, if interrupted, it might leave a valid header
2072 * magic, so also run validation on the primary slot to be
2073 * sure it's not OK.
2074 */
2075 if (boot_check_header_erased(BOOT_PRIMARY_SLOT) == 0 ||
2076 boot_validate_slot(BOOT_PRIMARY_SLOT, bs) != 0) {
2077 if (boot_img_hdr(&boot_data,
2078 BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC &&
2079 boot_validate_slot(BOOT_SECONDARY_SLOT, bs) == 0)
2080 {
2081 /* Set swap type to REVERT to overwrite the primary
2082 * slot with the image contained in secondary slot
2083 * and to trigger the explicit setting of the
2084 * image_ok flag.
2085 */
2086 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_REVERT;
2087 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02002088 }
2089 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02002090#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002091 }
David Vinczeba3bd602019-06-17 16:01:43 +02002092 } else {
2093 /* In that case if slots are not compatible. */
2094 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002095 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002096}
2097
2098/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08002099 * Prepares the booting process. This function moves images around in flash as
2100 * appropriate, and tells you what address to boot from.
2101 *
2102 * @param rsp On success, indicates how booting should occur.
2103 *
2104 * @return 0 on success; nonzero on failure.
2105 */
2106int
2107boot_go(struct boot_rsp *rsp)
2108{
Marti Bolivar84898652017-06-13 17:20:22 -04002109 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02002110 struct boot_status bs;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002111 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002112 int fa_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002113
2114 /* The array of slot sectors are defined here (as opposed to file scope) so
2115 * that they don't get allocated for non-boot-loader apps. This is
2116 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002117 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08002118 */
David Vinczeba3bd602019-06-17 16:01:43 +02002119 static boot_sector_t
2120 primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
2121 static boot_sector_t
2122 secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
Fabio Utzig2bd980a2018-11-26 10:38:17 -02002123 static boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Christopher Collins92ea77f2016-12-12 15:59:26 -08002124
Fabio Utzigba829042018-09-18 08:29:34 -03002125#ifdef MCUBOOT_ENC_IMAGES
2126 /* FIXME: remove this after RAM is cleared by sim */
2127 boot_enc_zeroize();
2128#endif
2129
David Vinczeba3bd602019-06-17 16:01:43 +02002130 /* Iterate over all the images. By the end of the loop the swap type has
2131 * to be determined for each image and all aborted swaps have to be
2132 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002133 */
Fabio Utzigabec0732019-07-31 08:40:22 -03002134 IMAGES_ITER(current_image) {
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03002135
David Vinczeba3bd602019-06-17 16:01:43 +02002136#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
2137 /* The keys used for encryption may no longer be valid (could belong to
2138 * another images). Therefore, mark them as invalid to force their reload
2139 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03002140 */
David Vinczeba3bd602019-06-17 16:01:43 +02002141 boot_enc_mark_keys_invalid();
David Brown554c52e2017-06-30 16:01:07 -06002142#endif
2143
David Vinczeba3bd602019-06-17 16:01:43 +02002144 BOOT_IMG(&boot_data, BOOT_PRIMARY_SLOT).sectors =
2145 primary_slot_sectors[current_image];
2146 BOOT_IMG(&boot_data, BOOT_SECONDARY_SLOT).sectors =
2147 secondary_slot_sectors[current_image];
2148 boot_data.scratch.sectors = scratch_sectors;
2149
2150 /* Open primary and secondary image areas for the duration
2151 * of this call.
2152 */
2153 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2154 fa_id = flash_area_id_from_image_slot(slot);
2155 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot));
2156 assert(rc == 0);
2157 }
2158 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
2159 &BOOT_SCRATCH_AREA(&boot_data));
2160 assert(rc == 0);
2161
2162 /* Determine swap type and complete swap if it has been aborted. */
2163 boot_prepare_image_for_update(&bs);
2164 }
2165
David Vinczee32483f2019-06-13 10:46:24 +02002166#if (BOOT_IMAGE_NUMBER > 1)
2167 /* Iterate over all the images and verify whether the image dependencies
2168 * are all satisfied and update swap type if necessary.
2169 */
2170 boot_verify_all_image_dependency();
2171#endif
2172
David Vinczeba3bd602019-06-17 16:01:43 +02002173 /* Iterate over all the images. At this point there are no aborted swaps
2174 * and the swap types are determined for each image. By the end of the loop
2175 * all required update operations will have been finished.
2176 */
Fabio Utzigabec0732019-07-31 08:40:22 -03002177 IMAGES_ITER(current_image) {
David Vinczeba3bd602019-06-17 16:01:43 +02002178
2179#if (BOOT_IMAGE_NUMBER > 1)
2180#ifdef MCUBOOT_ENC_IMAGES
2181 /* The keys used for encryption may no longer be valid (could belong to
2182 * another images). Therefore, mark them as invalid to force their reload
2183 * by boot_enc_load().
2184 */
2185 boot_enc_mark_keys_invalid();
2186#endif /* MCUBOOT_ENC_IMAGES */
2187
2188 /* Indicate that swap is not aborted */
2189 memset(&bs, 0, sizeof bs);
2190 bs.idx = BOOT_STATUS_IDX_0;
2191 bs.state = BOOT_STATUS_STATE_0;
2192#endif /* (BOOT_IMAGE_NUMBER > 1) */
2193
2194 /* Set the previously determined swap type */
2195 bs.swap_type = BOOT_SWAP_TYPE(&boot_data);
2196
2197 switch (BOOT_SWAP_TYPE(&boot_data)) {
2198 case BOOT_SWAP_TYPE_NONE:
2199 break;
2200
2201 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
2202 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
2203 case BOOT_SWAP_TYPE_REVERT:
2204 rc = boot_perform_update(&bs);
2205 assert(rc == 0);
2206 break;
2207
2208 case BOOT_SWAP_TYPE_FAIL:
2209 /* The image in secondary slot was invalid and is now erased. Ensure
2210 * we don't try to boot into it again on the next reboot. Do this by
2211 * pretending we just reverted back to primary slot.
2212 */
2213#ifndef MCUBOOT_OVERWRITE_ONLY
2214 /* image_ok needs to be explicitly set to avoid a new revert. */
2215 rc = boot_set_image_ok();
2216 if (rc != 0) {
2217 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
2218 }
2219#endif /* !MCUBOOT_OVERWRITE_ONLY */
2220 break;
2221
2222 default:
2223 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
2224 }
2225
2226 if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_PANIC) {
2227 BOOT_LOG_ERR("panic!");
2228 assert(0);
2229
2230 /* Loop forever... */
2231 while (1) {}
2232 }
2233 }
2234
2235 /* Iterate over all the images. At this point all required update operations
2236 * have finished. By the end of the loop each image in the primary slot will
2237 * have been re-validated.
2238 */
Fabio Utzigabec0732019-07-31 08:40:22 -03002239 IMAGES_ITER(current_image) {
David Vinczeba3bd602019-06-17 16:01:43 +02002240 if (BOOT_SWAP_TYPE(&boot_data) != BOOT_SWAP_TYPE_NONE) {
2241 /* Attempt to read an image header from each slot. Ensure that image
2242 * headers in slots are aligned with headers in boot_data.
2243 */
2244 rc = boot_read_image_headers(false);
2245 if (rc != 0) {
2246 goto out;
2247 }
2248 /* Since headers were reloaded, it can be assumed we just performed
2249 * a swap or overwrite. Now the header info that should be used to
2250 * provide the data for the bootstrap, which previously was at
2251 * secondary slot, was updated to primary slot.
2252 */
2253 }
2254
2255#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
2256 rc = boot_validate_slot(BOOT_PRIMARY_SLOT, NULL);
2257 if (rc != 0) {
2258 rc = BOOT_EBADIMAGE;
2259 goto out;
2260 }
2261#else
2262 /* Even if we're not re-validating the primary slot, we could be booting
2263 * onto an empty flash chip. At least do a basic sanity check that
2264 * the magic number on the image is OK.
2265 */
2266 if (BOOT_IMG(&boot_data, BOOT_PRIMARY_SLOT).hdr.ih_magic !=
2267 IMAGE_MAGIC) {
2268 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
2269 &boot_img_hdr(&boot_data,BOOT_PRIMARY_SLOT)->ih_magic,
2270 current_image);
2271 rc = BOOT_EBADIMAGE;
2272 goto out;
2273 }
2274#endif
2275 }
2276
2277 /* Always boot from the primary slot of Image 0. */
2278 current_image = 0;
2279 rsp->br_flash_dev_id =
2280 BOOT_IMG_AREA(&boot_data, BOOT_PRIMARY_SLOT)->fa_device_id;
2281 rsp->br_image_off =
2282 boot_img_slot_off(&boot_data, BOOT_PRIMARY_SLOT);
2283 rsp->br_hdr =
2284 boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002285
Marti Bolivarc0b47912017-06-13 17:18:09 -04002286 out:
Fabio Utzigabec0732019-07-31 08:40:22 -03002287 IMAGES_ITER(current_image) {
David Vinczeba3bd602019-06-17 16:01:43 +02002288 flash_area_close(BOOT_SCRATCH_AREA(&boot_data));
2289 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2290 flash_area_close(BOOT_IMG_AREA(&boot_data,
2291 BOOT_NUM_SLOTS - 1 - slot));
2292 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04002293 }
2294 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002295}
2296
2297int
2298split_go(int loader_slot, int split_slot, void **entry)
2299{
Marti Bolivarc50926f2017-06-14 09:35:40 -04002300 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08002301 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002302 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002303 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002304 int rc;
2305
Christopher Collins92ea77f2016-12-12 15:59:26 -08002306 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
2307 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04002308 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002309 }
David Vinczeba3bd602019-06-17 16:01:43 +02002310 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
2311 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002312
2313 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
2314 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07002315 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04002316 assert(rc == 0);
2317 split_flash_id = flash_area_id_from_image_slot(split_slot);
2318 rc = flash_area_open(split_flash_id,
2319 &BOOT_IMG_AREA(&boot_data, split_slot));
2320 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002321
2322 /* Determine the sector layout of the image slots and scratch area. */
2323 rc = boot_read_sectors();
2324 if (rc != 0) {
2325 rc = SPLIT_GO_ERR;
2326 goto done;
2327 }
2328
Fabio Utzig9c25fa72017-12-12 14:57:20 -02002329 rc = boot_read_image_headers(true);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002330 if (rc != 0) {
2331 goto done;
2332 }
2333
Christopher Collins92ea77f2016-12-12 15:59:26 -08002334 /* Don't check the bootable image flag because we could really call a
2335 * bootable or non-bootable image. Just validate that the image check
2336 * passes which is distinct from the normal check.
2337 */
Marti Bolivarf804f622017-06-12 15:41:48 -04002338 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04002339 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04002340 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04002341 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002342 if (rc != 0) {
2343 rc = SPLIT_GO_NON_MATCHING;
2344 goto done;
2345 }
2346
Marti Bolivarea088872017-06-12 17:10:49 -04002347 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04002348 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08002349 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002350 rc = SPLIT_GO_OK;
2351
2352done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04002353 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
2354 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002355 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002356 return rc;
2357}