blob: 191171f7e2d5ba0447f940b2629c6e8eef172173 [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
David Vinczee2453472019-06-17 12:31:59 +020020/*
21 * Modifications are Copyright (c) 2019 Arm Limited.
22 */
23
Christopher Collins92ea77f2016-12-12 15:59:26 -080024/**
25 * This file provides an interface to the boot loader. Functions defined in
26 * this file should only be called while the boot loader is running.
27 */
28
29#include <assert.h>
30#include <stddef.h>
David Brown52eee562017-07-05 11:25:09 -060031#include <stdbool.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080032#include <inttypes.h>
33#include <stdlib.h>
34#include <string.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080035#include <os/os_malloc.h>
36#include "bootutil/bootutil.h"
37#include "bootutil/image.h"
38#include "bootutil_priv.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050039#include "bootutil/bootutil_log.h"
40
Fabio Utzigba829042018-09-18 08:29:34 -030041#ifdef MCUBOOT_ENC_IMAGES
42#include "bootutil/enc_key.h"
43#endif
44
Fabio Utzigba1fbe62017-07-21 14:01:20 -030045#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030046
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010047MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
48
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040049static struct boot_loader_state boot_data;
David Vinczeb75c12a2019-03-22 14:58:33 +010050uint8_t current_image = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -080051
David Vincze2d736ad2019-02-18 11:50:22 +010052#if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT) && !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utziga0e1cce2017-11-23 20:04:01 -020053static int boot_status_fails = 0;
54#define BOOT_STATUS_ASSERT(x) \
55 do { \
Johann Fischered8461b2018-02-15 16:50:31 +010056 if (!(x)) { \
Fabio Utziga0e1cce2017-11-23 20:04:01 -020057 boot_status_fails++; \
58 } \
59 } while (0)
60#else
Fabio Utzig57c40f72017-12-12 21:48:30 -020061#define BOOT_STATUS_ASSERT(x) ASSERT(x)
Fabio Utziga0e1cce2017-11-23 20:04:01 -020062#endif
63
Christopher Collins92ea77f2016-12-12 15:59:26 -080064struct boot_status_table {
David Vincze2d736ad2019-02-18 11:50:22 +010065 uint8_t bst_magic_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -080066 uint8_t bst_magic_scratch;
David Vincze2d736ad2019-02-18 11:50:22 +010067 uint8_t bst_copy_done_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -080068 uint8_t bst_status_source;
69};
70
71/**
72 * This set of tables maps swap state contents to boot status location.
73 * When searching for a match, these tables must be iterated in order.
74 */
75static const struct boot_status_table boot_status_tables[] = {
76 {
David Vincze2d736ad2019-02-18 11:50:22 +010077 /* | primary slot | scratch |
78 * ----------+--------------+--------------|
79 * magic | Good | Any |
80 * copy-done | Set | N/A |
81 * ----------+--------------+--------------'
82 * source: none |
83 * ----------------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -080084 */
David Vincze2d736ad2019-02-18 11:50:22 +010085 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
Christopher Collinsa1c12042019-05-23 14:00:28 -070086 .bst_magic_scratch = BOOT_MAGIC_NOTGOOD,
David Vincze2d736ad2019-02-18 11:50:22 +010087 .bst_copy_done_primary_slot = BOOT_FLAG_SET,
88 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
Christopher Collins92ea77f2016-12-12 15:59:26 -080089 },
90
91 {
David Vincze2d736ad2019-02-18 11:50:22 +010092 /* | primary slot | scratch |
93 * ----------+--------------+--------------|
94 * magic | Good | Any |
95 * copy-done | Unset | N/A |
96 * ----------+--------------+--------------'
97 * source: primary slot |
98 * ----------------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -080099 */
David Vincze2d736ad2019-02-18 11:50:22 +0100100 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
Christopher Collinsa1c12042019-05-23 14:00:28 -0700101 .bst_magic_scratch = BOOT_MAGIC_NOTGOOD,
David Vincze2d736ad2019-02-18 11:50:22 +0100102 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
103 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800104 },
105
106 {
David Vincze2d736ad2019-02-18 11:50:22 +0100107 /* | primary slot | scratch |
108 * ----------+--------------+--------------|
109 * magic | Any | Good |
110 * copy-done | Any | N/A |
111 * ----------+--------------+--------------'
112 * source: scratch |
113 * ----------------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -0800114 */
David Vincze2d736ad2019-02-18 11:50:22 +0100115 .bst_magic_primary_slot = BOOT_MAGIC_ANY,
116 .bst_magic_scratch = BOOT_MAGIC_GOOD,
117 .bst_copy_done_primary_slot = BOOT_FLAG_ANY,
118 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800119 },
Christopher Collins92ea77f2016-12-12 15:59:26 -0800120 {
David Vincze2d736ad2019-02-18 11:50:22 +0100121 /* | primary slot | scratch |
122 * ----------+--------------+--------------|
123 * magic | Unset | Any |
124 * copy-done | Unset | N/A |
125 * ----------+--------------+--------------|
126 * source: varies |
127 * ----------------------------------------+--------------------------+
Christopher Collins92ea77f2016-12-12 15:59:26 -0800128 * This represents one of two cases: |
129 * o No swaps ever (no status to read, so no harm in checking). |
David Vincze2d736ad2019-02-18 11:50:22 +0100130 * o Mid-revert; status in primary slot. |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800131 * -------------------------------------------------------------------'
132 */
David Vincze2d736ad2019-02-18 11:50:22 +0100133 .bst_magic_primary_slot = BOOT_MAGIC_UNSET,
134 .bst_magic_scratch = BOOT_MAGIC_ANY,
135 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
136 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800137 },
138};
139
140#define BOOT_STATUS_TABLES_COUNT \
141 (sizeof boot_status_tables / sizeof boot_status_tables[0])
142
Marti Bolivarfd20c762017-02-07 16:52:50 -0500143#define BOOT_LOG_SWAP_STATE(area, state) \
Christopher Collinsa1c12042019-05-23 14:00:28 -0700144 BOOT_LOG_INF("%s: magic=%s, swap_type=0x%x, copy_done=0x%x, " \
145 "image_ok=0x%x", \
Marti Bolivarfd20c762017-02-07 16:52:50 -0500146 (area), \
147 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
148 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
149 "bad"), \
Christopher Collinsa1c12042019-05-23 14:00:28 -0700150 (state)->swap_type, \
Marti Bolivarfd20c762017-02-07 16:52:50 -0500151 (state)->copy_done, \
152 (state)->image_ok)
153
Christopher Collins92ea77f2016-12-12 15:59:26 -0800154/**
David Vincze2d736ad2019-02-18 11:50:22 +0100155 * Determines where in flash the most recent boot status is stored. The boot
Christopher Collins92ea77f2016-12-12 15:59:26 -0800156 * status is necessary for completing a swap that was interrupted by a boot
157 * loader reset.
158 *
David Vincze2d736ad2019-02-18 11:50:22 +0100159 * @return A BOOT_STATUS_SOURCE_[...] code indicating where status should
160 * be read from.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800161 */
162static int
163boot_status_source(void)
164{
165 const struct boot_status_table *table;
166 struct boot_swap_state state_scratch;
David Vincze2d736ad2019-02-18 11:50:22 +0100167 struct boot_swap_state state_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800168 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200169 size_t i;
Marti Bolivarfd20c762017-02-07 16:52:50 -0500170 uint8_t source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800171
David Vincze2d736ad2019-02-18 11:50:22 +0100172 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY,
173 &state_primary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800174 assert(rc == 0);
175
Fabio Utzig2473ac02017-05-02 12:45:02 -0300176 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800177 assert(rc == 0);
178
David Vincze2d736ad2019-02-18 11:50:22 +0100179 BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot);
Marti Bolivarfd20c762017-02-07 16:52:50 -0500180 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
181
Christopher Collins92ea77f2016-12-12 15:59:26 -0800182 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300183 table = &boot_status_tables[i];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800184
Christopher Collinsa1c12042019-05-23 14:00:28 -0700185 if (boot_magic_compatible_check(table->bst_magic_primary_slot,
186 state_primary_slot.magic) &&
187 boot_magic_compatible_check(table->bst_magic_scratch,
188 state_scratch.magic) &&
David Vincze2d736ad2019-02-18 11:50:22 +0100189 (table->bst_copy_done_primary_slot == BOOT_FLAG_ANY ||
190 table->bst_copy_done_primary_slot == state_primary_slot.copy_done))
191 {
Marti Bolivarfd20c762017-02-07 16:52:50 -0500192 source = table->bst_status_source;
David Vinczeba3bd602019-06-17 16:01:43 +0200193
194#if (BOOT_IMAGE_NUMBER > 1)
195 /* In case of multi-image boot it can happen that if boot status
196 * info is found on scratch area then it does not belong to the
197 * currently examined image.
198 */
199 if (source == BOOT_STATUS_SOURCE_SCRATCH &&
200 state_scratch.image_num != current_image) {
201 source = BOOT_STATUS_SOURCE_NONE;
202 }
203#endif
204
Marti Bolivarfd20c762017-02-07 16:52:50 -0500205 BOOT_LOG_INF("Boot source: %s",
206 source == BOOT_STATUS_SOURCE_NONE ? "none" :
207 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
David Vincze2d736ad2019-02-18 11:50:22 +0100208 source == BOOT_STATUS_SOURCE_PRIMARY_SLOT ?
209 "primary slot" : "BUG; can't happen");
Marti Bolivarfd20c762017-02-07 16:52:50 -0500210 return source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800211 }
212 }
213
Marti Bolivarfd20c762017-02-07 16:52:50 -0500214 BOOT_LOG_INF("Boot source: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800215 return BOOT_STATUS_SOURCE_NONE;
216}
217
David Brownf5b33d82017-09-01 10:58:27 -0600218/*
219 * Compute the total size of the given image. Includes the size of
220 * the TLVs.
221 */
Fabio Utzig13d9e352017-10-05 20:32:31 -0300222#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600223static int
224boot_read_image_size(int slot, struct image_header *hdr, uint32_t *size)
225{
226 const struct flash_area *fap;
227 struct image_tlv_info info;
228 int area_id;
229 int rc;
230
231 area_id = flash_area_id_from_image_slot(slot);
232 rc = flash_area_open(area_id, &fap);
233 if (rc != 0) {
234 rc = BOOT_EFLASH;
235 goto done;
236 }
237
238 rc = flash_area_read(fap, hdr->ih_hdr_size + hdr->ih_img_size,
239 &info, sizeof(info));
240 if (rc != 0) {
241 rc = BOOT_EFLASH;
242 goto done;
243 }
244 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
245 rc = BOOT_EBADIMAGE;
246 goto done;
247 }
248 *size = hdr->ih_hdr_size + hdr->ih_img_size + info.it_tlv_tot;
249 rc = 0;
250
251done:
252 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300253 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600254}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300255#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600256
Fabio Utzigc08ed212017-06-20 19:28:36 -0300257static int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800258boot_read_image_header(int slot, struct image_header *out_hdr)
259{
260 const struct flash_area *fap;
261 int area_id;
262 int rc;
263
264 area_id = flash_area_id_from_image_slot(slot);
265 rc = flash_area_open(area_id, &fap);
266 if (rc != 0) {
267 rc = BOOT_EFLASH;
268 goto done;
269 }
270
271 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
272 if (rc != 0) {
273 rc = BOOT_EFLASH;
274 goto done;
275 }
276
277 rc = 0;
278
279done:
280 flash_area_close(fap);
281 return rc;
282}
283
284static int
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200285boot_read_image_headers(bool require_all)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800286{
287 int rc;
288 int i;
289
290 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Marti Bolivarf804f622017-06-12 15:41:48 -0400291 rc = boot_read_image_header(i, boot_img_hdr(&boot_data, i));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800292 if (rc != 0) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200293 /* If `require_all` is set, fail on any single fail, otherwise
294 * if at least the first slot's header was read successfully,
295 * then the boot loader can attempt a boot.
296 *
297 * Failure to read any headers is a fatal error.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800298 */
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200299 if (i > 0 && !require_all) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800300 return 0;
301 } else {
302 return rc;
303 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800304 }
305 }
306
307 return 0;
308}
309
310static uint8_t
311boot_write_sz(void)
312{
313 uint8_t elem_sz;
314 uint8_t align;
315
316 /* Figure out what size to write update status update as. The size depends
317 * on what the minimum write size is for scratch area, active image slot.
318 * We need to use the bigger of those 2 values.
319 */
David Vinczeba3bd602019-06-17 16:01:43 +0200320 elem_sz = flash_area_align(BOOT_IMG_AREA(&boot_data, BOOT_PRIMARY_SLOT));
321 align = flash_area_align(BOOT_SCRATCH_AREA(&boot_data));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800322 if (align > elem_sz) {
323 elem_sz = align;
324 }
325
326 return elem_sz;
327}
328
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200329/*
330 * Slots are compatible when all sectors that store upto to size of the image
331 * round up to sector size, in both slot's are able to fit in the scratch
332 * area, and have sizes that are a multiple of each other (powers of two
333 * presumably!).
334 */
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800335static int
336boot_slots_compatible(void)
337{
David Vincze2d736ad2019-02-18 11:50:22 +0100338 size_t num_sectors_primary;
339 size_t num_sectors_secondary;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200340 size_t sz0, sz1;
David Vincze2d736ad2019-02-18 11:50:22 +0100341 size_t primary_slot_sz, secondary_slot_sz;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200342 size_t scratch_sz;
343 size_t i, j;
344 int8_t smaller;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800345
David Vincze2d736ad2019-02-18 11:50:22 +0100346 num_sectors_primary =
347 boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT);
348 num_sectors_secondary =
349 boot_img_num_sectors(&boot_data, BOOT_SECONDARY_SLOT);
350 if ((num_sectors_primary > BOOT_MAX_IMG_SECTORS) ||
351 (num_sectors_secondary > BOOT_MAX_IMG_SECTORS)) {
Fabio Utziga1fae672018-03-30 10:52:38 -0300352 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800353 return 0;
354 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300355
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200356 scratch_sz = boot_scratch_area_size(&boot_data);
357
358 /*
359 * The following loop scans all sectors in a linear fashion, assuring that
360 * for each possible sector in each slot, it is able to fit in the other
361 * slot's sector or sectors. Slot's should be compatible as long as any
362 * number of a slot's sectors are able to fit into another, which only
363 * excludes cases where sector sizes are not a multiple of each other.
364 */
David Vincze2d736ad2019-02-18 11:50:22 +0100365 i = sz0 = primary_slot_sz = 0;
366 j = sz1 = secondary_slot_sz = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200367 smaller = 0;
David Vincze2d736ad2019-02-18 11:50:22 +0100368 while (i < num_sectors_primary || j < num_sectors_secondary) {
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200369 if (sz0 == sz1) {
David Vincze2d736ad2019-02-18 11:50:22 +0100370 sz0 += boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
371 sz1 += boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT, j);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200372 i++;
373 j++;
374 } else if (sz0 < sz1) {
David Vincze2d736ad2019-02-18 11:50:22 +0100375 sz0 += boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
376 /* Guarantee that multiple sectors of the secondary slot
377 * fit into the primary slot.
378 */
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200379 if (smaller == 2) {
380 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors");
381 return 0;
382 }
383 smaller = 1;
384 i++;
385 } else {
David Vincze2d736ad2019-02-18 11:50:22 +0100386 sz1 += boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT, j);
387 /* Guarantee that multiple sectors of the primary slot
388 * fit into the secondary slot.
389 */
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200390 if (smaller == 1) {
391 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors");
392 return 0;
393 }
394 smaller = 2;
395 j++;
396 }
397 if (sz0 == sz1) {
David Vincze2d736ad2019-02-18 11:50:22 +0100398 primary_slot_sz += sz0;
399 secondary_slot_sz += sz1;
400 /* Scratch has to fit each swap operation to the size of the larger
401 * sector among the primary slot and the secondary slot.
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200402 */
403 if (sz0 > scratch_sz || sz1 > scratch_sz) {
404 BOOT_LOG_WRN("Cannot upgrade: not all sectors fit inside scratch");
405 return 0;
406 }
407 smaller = sz0 = sz1 = 0;
408 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300409 }
410
David Vincze2d736ad2019-02-18 11:50:22 +0100411 if ((i != num_sectors_primary) ||
412 (j != num_sectors_secondary) ||
413 (primary_slot_sz != secondary_slot_sz)) {
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200414 BOOT_LOG_WRN("Cannot upgrade: slots are not compatible");
415 return 0;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800416 }
417
418 return 1;
419}
420
Christopher Collins92ea77f2016-12-12 15:59:26 -0800421/**
422 * Determines the sector layout of both image slots and the scratch area.
423 * This information is necessary for calculating the number of bytes to erase
424 * and copy during an image swap. The information collected during this
425 * function is used to populate the boot_data global.
426 */
427static int
428boot_read_sectors(void)
429{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800430 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800431
David Vincze2d736ad2019-02-18 11:50:22 +0100432 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_PRIMARY);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800433 if (rc != 0) {
434 return BOOT_EFLASH;
435 }
436
David Vincze2d736ad2019-02-18 11:50:22 +0100437 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_SECONDARY);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800438 if (rc != 0) {
439 return BOOT_EFLASH;
440 }
441
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200442 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_SCRATCH);
443 if (rc != 0) {
444 return BOOT_EFLASH;
445 }
446
Marti Bolivare10a7392017-06-14 16:20:07 -0400447 BOOT_WRITE_SZ(&boot_data) = boot_write_sz();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800448
449 return 0;
450}
451
452static uint32_t
453boot_status_internal_off(int idx, int state, int elem_sz)
454{
455 int idx_sz;
456
457 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
458
Fabio Utzig39000012018-07-30 12:40:20 -0300459 return (idx - BOOT_STATUS_IDX_0) * idx_sz +
460 (state - BOOT_STATUS_STATE_0) * elem_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800461}
462
463/**
464 * Reads the status of a partially-completed swap, if any. This is necessary
465 * to recover in case the boot lodaer was reset in the middle of a swap
466 * operation.
467 */
468static int
469boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs)
470{
471 uint32_t off;
472 uint8_t status;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300473 int max_entries;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800474 int found;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200475 int found_idx;
476 int invalid;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800477 int rc;
478 int i;
479
480 off = boot_status_off(fap);
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400481 max_entries = boot_status_entries(fap);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300482
Christopher Collins92ea77f2016-12-12 15:59:26 -0800483 found = 0;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200484 found_idx = 0;
485 invalid = 0;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300486 for (i = 0; i < max_entries; i++) {
Fabio Utzig178be542018-09-19 08:12:56 -0300487 rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(&boot_data),
488 &status, 1);
489 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800490 return BOOT_EFLASH;
491 }
492
Fabio Utzig178be542018-09-19 08:12:56 -0300493 if (rc == 1) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200494 if (found && !found_idx) {
495 found_idx = i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800496 }
497 } else if (!found) {
498 found = 1;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200499 } else if (found_idx) {
500 invalid = 1;
501 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800502 }
503 }
504
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200505 if (invalid) {
506 /* This means there was an error writing status on the last
507 * swap. Tell user and move on to validation!
508 */
509 BOOT_LOG_ERR("Detected inconsistent status!");
510
David Vincze2d736ad2019-02-18 11:50:22 +0100511#if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
512 /* With validation of the primary slot disabled, there is no way
513 * to be sure the swapped primary slot is OK, so abort!
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200514 */
515 assert(0);
516#endif
517 }
518
Christopher Collins92ea77f2016-12-12 15:59:26 -0800519 if (found) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200520 if (!found_idx) {
521 found_idx = i;
522 }
523 found_idx--;
Fabio Utzig39000012018-07-30 12:40:20 -0300524 bs->idx = (found_idx / BOOT_STATUS_STATE_COUNT) + 1;
525 bs->state = (found_idx % BOOT_STATUS_STATE_COUNT) + 1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800526 }
527
528 return 0;
529}
530
531/**
532 * Reads the boot status from the flash. The boot status contains
533 * the current state of an interrupted image copy operation. If the boot
534 * status is not present, or it indicates that previous copy finished,
535 * there is no operation in progress.
536 */
537static int
538boot_read_status(struct boot_status *bs)
539{
540 const struct flash_area *fap;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700541 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200542 uint8_t swap_info;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800543 int status_loc;
544 int area_id;
545 int rc;
546
547 memset(bs, 0, sizeof *bs);
Fabio Utzig39000012018-07-30 12:40:20 -0300548 bs->idx = BOOT_STATUS_IDX_0;
549 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700550 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800551
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700552#ifdef MCUBOOT_OVERWRITE_ONLY
553 /* Overwrite-only doesn't make use of the swap status area. */
554 return 0;
555#endif
556
Christopher Collins92ea77f2016-12-12 15:59:26 -0800557 status_loc = boot_status_source();
558 switch (status_loc) {
559 case BOOT_STATUS_SOURCE_NONE:
560 return 0;
561
562 case BOOT_STATUS_SOURCE_SCRATCH:
563 area_id = FLASH_AREA_IMAGE_SCRATCH;
564 break;
565
David Vincze2d736ad2019-02-18 11:50:22 +0100566 case BOOT_STATUS_SOURCE_PRIMARY_SLOT:
567 area_id = FLASH_AREA_IMAGE_PRIMARY;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800568 break;
569
570 default:
571 assert(0);
572 return BOOT_EBADARGS;
573 }
574
575 rc = flash_area_open(area_id, &fap);
576 if (rc != 0) {
577 return BOOT_EFLASH;
578 }
579
Fabio Utzig46490722017-09-04 15:34:32 -0300580 rc = boot_read_status_bytes(fap, bs);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700581 if (rc == 0) {
David Vinczee2453472019-06-17 12:31:59 +0200582 off = boot_swap_info_off(fap);
583 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700584 if (rc == 1) {
David Vinczee2453472019-06-17 12:31:59 +0200585 BOOT_SET_SWAP_INFO(swap_info, 0, BOOT_SWAP_TYPE_NONE);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700586 rc = 0;
587 }
David Vinczee2453472019-06-17 12:31:59 +0200588
589 /* Extract the swap type info */
590 bs->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700591 }
Fabio Utzig46490722017-09-04 15:34:32 -0300592
593 flash_area_close(fap);
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700594
Fabio Utzig46490722017-09-04 15:34:32 -0300595 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800596}
597
598/**
599 * Writes the supplied boot status to the flash file system. The boot status
600 * contains the current state of an in-progress image copy operation.
601 *
602 * @param bs The boot status to write.
603 *
604 * @return 0 on success; nonzero on failure.
605 */
606int
607boot_write_status(struct boot_status *bs)
608{
609 const struct flash_area *fap;
610 uint32_t off;
611 int area_id;
612 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300613 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700614 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300615 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800616
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300617 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100618 * the trailer. Since in the last step the primary slot is erased, the
619 * first two status writes go to the scratch which will be copied to
620 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300621 */
622
Fabio Utzig2473ac02017-05-02 12:45:02 -0300623 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800624 /* Write to scratch. */
625 area_id = FLASH_AREA_IMAGE_SCRATCH;
626 } else {
David Vincze2d736ad2019-02-18 11:50:22 +0100627 /* Write to the primary slot. */
628 area_id = FLASH_AREA_IMAGE_PRIMARY;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800629 }
630
631 rc = flash_area_open(area_id, &fap);
632 if (rc != 0) {
633 rc = BOOT_EFLASH;
634 goto done;
635 }
636
637 off = boot_status_off(fap) +
Marti Bolivare10a7392017-06-14 16:20:07 -0400638 boot_status_internal_off(bs->idx, bs->state,
639 BOOT_WRITE_SZ(&boot_data));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200640 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300641 erased_val = flash_area_erased_val(fap);
642 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700643 buf[0] = bs->state;
644
645 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800646 if (rc != 0) {
647 rc = BOOT_EFLASH;
648 goto done;
649 }
650
651 rc = 0;
652
653done:
654 flash_area_close(fap);
655 return rc;
656}
657
658/*
659 * Validate image hash/signature in a slot.
660 */
661static int
Fabio Utzigba829042018-09-18 08:29:34 -0300662boot_image_check(struct image_header *hdr, const struct flash_area *fap,
663 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800664{
David Browndb1d9d32017-01-06 11:07:54 -0700665 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigba829042018-09-18 08:29:34 -0300666 int rc;
667
668#ifndef MCUBOOT_ENC_IMAGES
669 (void)bs;
670 (void)rc;
671#else
David Vincze2d736ad2019-02-18 11:50:22 +0100672 if ((fap->fa_id == FLASH_AREA_IMAGE_SECONDARY) && IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300673 rc = boot_enc_load(hdr, fap, bs->enckey[1]);
674 if (rc < 0) {
675 return BOOT_EBADIMAGE;
676 }
677 if (rc == 0 && boot_enc_set_key(1, bs->enckey[1])) {
678 return BOOT_EBADIMAGE;
679 }
680 }
681#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800682
Christopher Collins92ea77f2016-12-12 15:59:26 -0800683 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
684 NULL, 0, NULL)) {
685 return BOOT_EBADIMAGE;
686 }
687 return 0;
688}
689
690static int
691split_image_check(struct image_header *app_hdr,
692 const struct flash_area *app_fap,
693 struct image_header *loader_hdr,
694 const struct flash_area *loader_fap)
695{
696 static void *tmpbuf;
697 uint8_t loader_hash[32];
698
699 if (!tmpbuf) {
700 tmpbuf = malloc(BOOT_TMPBUF_SZ);
701 if (!tmpbuf) {
702 return BOOT_ENOMEM;
703 }
704 }
705
706 if (bootutil_img_validate(loader_hdr, loader_fap, tmpbuf, BOOT_TMPBUF_SZ,
707 NULL, 0, loader_hash)) {
708 return BOOT_EBADIMAGE;
709 }
710
711 if (bootutil_img_validate(app_hdr, app_fap, tmpbuf, BOOT_TMPBUF_SZ,
712 loader_hash, 32, NULL)) {
713 return BOOT_EBADIMAGE;
714 }
715
716 return 0;
717}
718
Fabio Utzig338a19f2018-12-03 08:37:08 -0200719/*
720 * Check that a memory area consists of a given value.
721 */
722static inline bool
723boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300724{
725 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200726 uint8_t *p = (uint8_t *)data;
727 for (i = 0; i < len; i++) {
728 if (val != p[i]) {
729 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300730 }
731 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200732 return true;
733}
734
735static int
736boot_check_header_erased(int slot)
737{
738 const struct flash_area *fap;
739 struct image_header *hdr;
740 uint8_t erased_val;
741 int rc;
742
743 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
744 if (rc != 0) {
745 return -1;
746 }
747
748 erased_val = flash_area_erased_val(fap);
749 flash_area_close(fap);
750
751 hdr = boot_img_hdr(&boot_data, slot);
752 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
753 return -1;
754 }
755
756 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300757}
758
Christopher Collins92ea77f2016-12-12 15:59:26 -0800759static int
Fabio Utzigba829042018-09-18 08:29:34 -0300760boot_validate_slot(int slot, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800761{
762 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400763 struct image_header *hdr;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800764 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300765
David Brownd930ec62016-12-14 07:59:48 -0700766 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800767 if (rc != 0) {
768 return BOOT_EFLASH;
769 }
770
Fabio Utzig39000012018-07-30 12:40:20 -0300771 hdr = boot_img_hdr(&boot_data, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200772 if (boot_check_header_erased(slot) == 0 || (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100773 /* No bootable image in slot; continue booting from the primary slot. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200774 rc = -1;
775 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300776 }
777
Fabio Utzigba829042018-09-18 08:29:34 -0300778 if ((hdr->ih_magic != IMAGE_MAGIC || boot_image_check(hdr, fap, bs) != 0)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100779 if (slot != BOOT_PRIMARY_SLOT) {
David Brownb38e0442017-02-24 13:57:12 -0700780 flash_area_erase(fap, 0, fap->fa_size);
David Vincze2d736ad2019-02-18 11:50:22 +0100781 /* Image in the secondary slot is invalid. Erase the image and
782 * continue booting from the primary slot.
David Brownb38e0442017-02-24 13:57:12 -0700783 */
784 }
David Vincze2d736ad2019-02-18 11:50:22 +0100785 BOOT_LOG_ERR("Image in the %s slot is not valid!",
786 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
Fabio Utzig338a19f2018-12-03 08:37:08 -0200787 rc = -1;
788 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800789 }
790
David Vincze2d736ad2019-02-18 11:50:22 +0100791 /* Image in the secondary slot is valid. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200792 rc = 0;
793
794out:
795 flash_area_close(fap);
796 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800797}
798
799/**
800 * Determines which swap operation to perform, if any. If it is determined
David Vincze2d736ad2019-02-18 11:50:22 +0100801 * that a swap operation is required, the image in the secondary slot is checked
802 * for validity. If the image in the secondary slot is invalid, it is erased,
803 * and a swap type of "none" is indicated.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800804 *
805 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
806 */
807static int
Fabio Utzigba829042018-09-18 08:29:34 -0300808boot_validated_swap_type(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800809{
810 int swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800811
812 swap_type = boot_swap_type();
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300813 switch (swap_type) {
814 case BOOT_SWAP_TYPE_TEST:
815 case BOOT_SWAP_TYPE_PERM:
816 case BOOT_SWAP_TYPE_REVERT:
David Vincze2d736ad2019-02-18 11:50:22 +0100817 /* Boot loader wants to switch to the secondary slot.
818 * Ensure image is valid.
819 */
820 if (boot_validate_slot(BOOT_SECONDARY_SLOT, bs) != 0) {
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300821 swap_type = BOOT_SWAP_TYPE_FAIL;
822 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800823 }
824
825 return swap_type;
826}
827
828/**
829 * Calculates the number of sectors the scratch area can contain. A "last"
830 * source sector is specified because images are copied backwards in flash
831 * (final index to index number 0).
832 *
833 * @param last_sector_idx The index of the last source sector
834 * (inclusive).
835 * @param out_first_sector_idx The index of the first source sector
836 * (inclusive) gets written here.
837 *
838 * @return The number of bytes comprised by the
839 * [first-sector, last-sector] range.
840 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300841#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800842static uint32_t
843boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
844{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400845 size_t scratch_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800846 uint32_t new_sz;
847 uint32_t sz;
848 int i;
849
850 sz = 0;
851
Marti Bolivard3269fd2017-06-12 16:31:12 -0400852 scratch_sz = boot_scratch_area_size(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800853 for (i = last_sector_idx; i >= 0; i--) {
David Vincze2d736ad2019-02-18 11:50:22 +0100854 new_sz = sz + boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200855 /*
David Vincze2d736ad2019-02-18 11:50:22 +0100856 * The secondary slot is not being checked here, because
857 * `boot_slots_compatible` already provides assurance that the copy size
858 * will be compatible with the primary slot and scratch.
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200859 */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400860 if (new_sz > scratch_sz) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800861 break;
862 }
863 sz = new_sz;
864 }
865
866 /* i currently refers to a sector that doesn't fit or it is -1 because all
867 * sectors have been processed. In both cases, exclude sector i.
868 */
869 *out_first_sector_idx = i + 1;
870 return sz;
871}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300872#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800873
874/**
875 * Erases a region of flash.
876 *
Fabio Utzigba829042018-09-18 08:29:34 -0300877 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800878 * @param off The offset within the flash area to start the
879 * erase.
880 * @param sz The number of bytes to erase.
881 *
882 * @return 0 on success; nonzero on failure.
883 */
Fabio Utzigba829042018-09-18 08:29:34 -0300884static inline int
885boot_erase_sector(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800886{
Fabio Utzigba829042018-09-18 08:29:34 -0300887 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800888}
889
890/**
891 * Copies the contents of one flash region to another. You must erase the
892 * destination region prior to calling this function.
893 *
894 * @param flash_area_id_src The ID of the source flash area.
895 * @param flash_area_id_dst The ID of the destination flash area.
896 * @param off_src The offset within the source flash area to
897 * copy from.
898 * @param off_dst The offset within the destination flash area to
899 * copy to.
900 * @param sz The number of bytes to copy.
901 *
902 * @return 0 on success; nonzero on failure.
903 */
904static int
Fabio Utzigba829042018-09-18 08:29:34 -0300905boot_copy_sector(const struct flash_area *fap_src,
906 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800907 uint32_t off_src, uint32_t off_dst, uint32_t sz)
908{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800909 uint32_t bytes_copied;
910 int chunk_sz;
911 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -0300912#ifdef MCUBOOT_ENC_IMAGES
913 uint32_t off;
914 size_t blk_off;
915 struct image_header *hdr;
916 uint16_t idx;
917 uint32_t blk_sz;
918#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800919
920 static uint8_t buf[1024];
921
Christopher Collins92ea77f2016-12-12 15:59:26 -0800922 bytes_copied = 0;
923 while (bytes_copied < sz) {
924 if (sz - bytes_copied > sizeof buf) {
925 chunk_sz = sizeof buf;
926 } else {
927 chunk_sz = sz - bytes_copied;
928 }
929
930 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
931 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300932 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800933 }
934
Fabio Utzigba829042018-09-18 08:29:34 -0300935#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +0100936 if ((fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY) ||
937 (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY)) {
938 /* assume the secondary slot as src, needs decryption */
939 hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -0300940 off = off_src;
David Vincze2d736ad2019-02-18 11:50:22 +0100941 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY) {
942 /* might need encryption (metadata from the primary slot) */
943 hdr = boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -0300944 off = off_dst;
945 }
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200946 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300947 blk_sz = chunk_sz;
948 idx = 0;
949 if (off + bytes_copied < hdr->ih_hdr_size) {
950 /* do not decrypt header */
951 blk_off = 0;
952 blk_sz = chunk_sz - hdr->ih_hdr_size;
953 idx = hdr->ih_hdr_size;
954 } else {
955 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
956 }
957 if (off + bytes_copied + chunk_sz > hdr->ih_hdr_size + hdr->ih_img_size) {
958 /* do not decrypt TLVs */
959 if (off + bytes_copied >= hdr->ih_hdr_size + hdr->ih_img_size) {
960 blk_sz = 0;
961 } else {
962 blk_sz = (hdr->ih_hdr_size + hdr->ih_img_size) - (off + bytes_copied);
963 }
964 }
965 boot_encrypt(fap_src, (off + bytes_copied + idx) - hdr->ih_hdr_size,
966 blk_sz, blk_off, &buf[idx]);
967 }
968 }
969#endif
970
Christopher Collins92ea77f2016-12-12 15:59:26 -0800971 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
972 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300973 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800974 }
975
976 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -0300977
978 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800979 }
980
Fabio Utzigba829042018-09-18 08:29:34 -0300981 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800982}
983
David Brown6b1b3b92017-09-19 08:59:10 -0600984#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -0300985static inline int
Fabio Utzigba829042018-09-18 08:29:34 -0300986boot_status_init(const struct flash_area *fap, const struct boot_status *bs)
Fabio Utzig2473ac02017-05-02 12:45:02 -0300987{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300988 struct boot_swap_state swap_state;
989 int rc;
990
Christopher Collins2c88e692019-05-22 15:10:14 -0700991 BOOT_LOG_DBG("initializing status; fa_id=%d", fap->fa_id);
992
David Vincze2d736ad2019-02-18 11:50:22 +0100993 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY, &swap_state);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300994 assert(rc == 0);
995
Christopher Collinsa1c12042019-05-23 14:00:28 -0700996 if (bs->swap_type != BOOT_SWAP_TYPE_NONE) {
David Vinczee2453472019-06-17 12:31:59 +0200997 rc = boot_write_swap_info(fap,
998 bs->swap_type,
David Vinczeba3bd602019-06-17 16:01:43 +0200999 current_image);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001000 assert(rc == 0);
1001 }
1002
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001003 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001004 rc = boot_write_image_ok(fap);
1005 assert(rc == 0);
1006 }
1007
Fabio Utzig46490722017-09-04 15:34:32 -03001008 rc = boot_write_swap_size(fap, bs->swap_size);
1009 assert(rc == 0);
1010
Fabio Utzigba829042018-09-18 08:29:34 -03001011#ifdef MCUBOOT_ENC_IMAGES
1012 rc = boot_write_enc_key(fap, 0, bs->enckey[0]);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001013 assert(rc == 0);
1014
Fabio Utzigba829042018-09-18 08:29:34 -03001015 rc = boot_write_enc_key(fap, 1, bs->enckey[1]);
1016 assert(rc == 0);
1017#endif
1018
1019 rc = boot_write_magic(fap);
1020 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001021
1022 return 0;
1023}
Christopher Collinsa1c12042019-05-23 14:00:28 -07001024
David Brown6b1b3b92017-09-19 08:59:10 -06001025#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001026
Fabio Utzig358c9352017-07-25 22:10:45 -03001027#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -03001028static int
Fabio Utziged0ca432019-01-23 14:50:11 -02001029boot_erase_trailer_sectors(const struct flash_area *fap)
Fabio Utzig2473ac02017-05-02 12:45:02 -03001030{
1031 uint8_t slot;
Fabio Utziged0ca432019-01-23 14:50:11 -02001032 uint32_t sector;
1033 uint32_t trailer_sz;
1034 uint32_t total_sz;
1035 uint32_t off;
1036 uint32_t sz;
David Vinczeb75c12a2019-03-22 14:58:33 +01001037 int fa_id_primary;
1038 int fa_id_secondary;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001039 int rc;
1040
Christopher Collins2c88e692019-05-22 15:10:14 -07001041 BOOT_LOG_DBG("erasing trailer; fa_id=%d", fap->fa_id);
1042
David Vinczeb75c12a2019-03-22 14:58:33 +01001043 fa_id_primary = flash_area_id_from_image_slot(BOOT_PRIMARY_SLOT);
1044 fa_id_secondary = flash_area_id_from_image_slot(BOOT_SECONDARY_SLOT);
1045
1046 if (fap->fa_id == fa_id_primary) {
David Vincze2d736ad2019-02-18 11:50:22 +01001047 slot = BOOT_PRIMARY_SLOT;
David Vinczeb75c12a2019-03-22 14:58:33 +01001048 } else if (fap->fa_id == fa_id_secondary) {
David Vincze2d736ad2019-02-18 11:50:22 +01001049 slot = BOOT_SECONDARY_SLOT;
David Vinczeb75c12a2019-03-22 14:58:33 +01001050 } else {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001051 return BOOT_EFLASH;
1052 }
1053
Fabio Utziged0ca432019-01-23 14:50:11 -02001054 /* delete starting from last sector and moving to beginning */
1055 sector = boot_img_num_sectors(&boot_data, slot) - 1;
Christopher Collins2adef702019-05-22 14:37:31 -07001056 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Fabio Utziged0ca432019-01-23 14:50:11 -02001057 total_sz = 0;
1058 do {
1059 sz = boot_img_sector_size(&boot_data, slot, sector);
1060 off = boot_img_sector_off(&boot_data, slot, sector);
1061 rc = boot_erase_sector(fap, off, sz);
1062 assert(rc == 0);
1063
1064 sector--;
1065 total_sz += sz;
1066 } while (total_sz < trailer_sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001067
1068 return rc;
1069}
Fabio Utzig358c9352017-07-25 22:10:45 -03001070#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001071
Christopher Collins92ea77f2016-12-12 15:59:26 -08001072/**
1073 * Swaps the contents of two flash regions within the two image slots.
1074 *
1075 * @param idx The index of the first sector in the range of
1076 * sectors being swapped.
1077 * @param sz The number of bytes to swap.
1078 * @param bs The current boot status. This struct gets
1079 * updated according to the outcome.
1080 *
1081 * @return 0 on success; nonzero on failure.
1082 */
Fabio Utzig3488eef2017-06-12 10:25:43 -03001083#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -08001084static void
Christopher Collins92ea77f2016-12-12 15:59:26 -08001085boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
1086{
David Vincze2d736ad2019-02-18 11:50:22 +01001087 const struct flash_area *fap_primary_slot;
1088 const struct flash_area *fap_secondary_slot;
Fabio Utzigba829042018-09-18 08:29:34 -03001089 const struct flash_area *fap_scratch;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001090 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001091 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001092 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001093 uint32_t scratch_trailer_off;
1094 struct boot_swap_state swap_state;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001095 size_t last_sector;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001096 bool erase_scratch;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001097 int rc;
1098
1099 /* Calculate offset from start of image area. */
David Vincze2d736ad2019-02-18 11:50:22 +01001100 img_off = boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT, idx);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001101
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001102 copy_sz = sz;
Christopher Collins2adef702019-05-22 14:37:31 -07001103 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Fabio Utzig9678c972017-05-23 11:28:56 -04001104
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001105 /* sz in this function is always sized on a multiple of the sector size.
1106 * The check against the start offset of the last sector
Fabio Utzig9678c972017-05-23 11:28:56 -04001107 * is to determine if we're swapping the last sector. The last sector
1108 * needs special handling because it's where the trailer lives. If we're
1109 * copying it, we need to use scratch to write the trailer temporarily.
1110 *
1111 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
1112 * controls if special handling is needed (swapping last sector).
1113 */
David Vincze2d736ad2019-02-18 11:50:22 +01001114 last_sector = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT) - 1;
David Vinczeba3bd602019-06-17 16:01:43 +02001115 if ((img_off + sz) >
1116 boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT, last_sector)) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001117 copy_sz -= trailer_sz;
1118 }
1119
Fabio Utzig39000012018-07-30 12:40:20 -03001120 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001121
David Vincze2d736ad2019-02-18 11:50:22 +01001122 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001123 assert (rc == 0);
1124
David Vincze2d736ad2019-02-18 11:50:22 +01001125 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001126 assert (rc == 0);
1127
1128 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch);
1129 assert (rc == 0);
1130
Fabio Utzig39000012018-07-30 12:40:20 -03001131 if (bs->state == BOOT_STATUS_STATE_0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001132 BOOT_LOG_DBG("erasing scratch area");
Christopher Collinsa1c12042019-05-23 14:00:28 -07001133 rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size);
Christopher Collins4772ac42017-02-27 20:08:01 -08001134 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001135
Fabio Utzig39000012018-07-30 12:40:20 -03001136 if (bs->idx == BOOT_STATUS_IDX_0) {
Christopher Collinsa1c12042019-05-23 14:00:28 -07001137 /* Write a trailer to the scratch area, even if we don't need the
1138 * scratch area for status. We need a temporary place to store the
1139 * `swap-type` while we erase the primary trailer.
1140 */
1141 rc = boot_status_init(fap_scratch, bs);
1142 assert(rc == 0);
1143
1144 if (!bs->use_scratch) {
1145 /* Prepare the primary status area... here it is known that the
1146 * last sector is not being used by the image data so it's safe
1147 * to erase.
Fabio Utzig2473ac02017-05-02 12:45:02 -03001148 */
David Vincze2d736ad2019-02-18 11:50:22 +01001149 rc = boot_erase_trailer_sectors(fap_primary_slot);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001150 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001151
Christopher Collinsa1c12042019-05-23 14:00:28 -07001152 rc = boot_status_init(fap_primary_slot, bs);
1153 assert(rc == 0);
1154
1155 /* Erase the temporary trailer from the scratch area. */
1156 rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size);
1157 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001158 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001159 }
1160
Christopher Collinsa1c12042019-05-23 14:00:28 -07001161 rc = boot_copy_sector(fap_secondary_slot, fap_scratch,
1162 img_off, 0, copy_sz);
1163 assert(rc == 0);
1164
Fabio Utzig39000012018-07-30 12:40:20 -03001165 bs->state = BOOT_STATUS_STATE_1;
Christopher Collins4772ac42017-02-27 20:08:01 -08001166 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001167 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001168 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001169
Fabio Utzig39000012018-07-30 12:40:20 -03001170 if (bs->state == BOOT_STATUS_STATE_1) {
David Vincze2d736ad2019-02-18 11:50:22 +01001171 rc = boot_erase_sector(fap_secondary_slot, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001172 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001173
David Vincze2d736ad2019-02-18 11:50:22 +01001174 rc = boot_copy_sector(fap_primary_slot, fap_secondary_slot,
1175 img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001176 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001177
Fabio Utzig39000012018-07-30 12:40:20 -03001178 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001179 /* If not all sectors of the slot are being swapped,
David Vincze2d736ad2019-02-18 11:50:22 +01001180 * guarantee here that only the primary slot will have the state.
Fabio Utzig2473ac02017-05-02 12:45:02 -03001181 */
David Vincze2d736ad2019-02-18 11:50:22 +01001182 rc = boot_erase_trailer_sectors(fap_secondary_slot);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001183 assert(rc == 0);
1184 }
1185
Fabio Utzig39000012018-07-30 12:40:20 -03001186 bs->state = BOOT_STATUS_STATE_2;
Christopher Collins4772ac42017-02-27 20:08:01 -08001187 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001188 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001189 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001190
Fabio Utzig39000012018-07-30 12:40:20 -03001191 if (bs->state == BOOT_STATUS_STATE_2) {
David Vincze2d736ad2019-02-18 11:50:22 +01001192 rc = boot_erase_sector(fap_primary_slot, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001193 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001194
Christopher Collinsa1c12042019-05-23 14:00:28 -07001195 /* NOTE: If this is the final sector, we exclude the image trailer from
1196 * this copy (copy_sz was truncated earlier).
1197 */
David Vincze2d736ad2019-02-18 11:50:22 +01001198 rc = boot_copy_sector(fap_scratch, fap_primary_slot,
1199 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001200 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001201
Fabio Utzig94d998c2017-05-22 11:02:41 -04001202 if (bs->use_scratch) {
Fabio Utzigba829042018-09-18 08:29:34 -03001203 scratch_trailer_off = boot_status_off(fap_scratch);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001204
1205 /* copy current status that is being maintained in scratch */
David Vincze2d736ad2019-02-18 11:50:22 +01001206 rc = boot_copy_sector(fap_scratch, fap_primary_slot,
1207 scratch_trailer_off, img_off + copy_sz,
Marti Bolivare10a7392017-06-14 16:20:07 -04001208 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data));
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001209 BOOT_STATUS_ASSERT(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001210
Fabio Utzig2473ac02017-05-02 12:45:02 -03001211 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1212 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001213 assert(rc == 0);
1214
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001215 if (swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze2d736ad2019-02-18 11:50:22 +01001216 rc = boot_write_image_ok(fap_primary_slot);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001217 assert(rc == 0);
1218 }
1219
Christopher Collinsa1c12042019-05-23 14:00:28 -07001220 if (swap_state.swap_type != BOOT_SWAP_TYPE_NONE) {
David Vinczee2453472019-06-17 12:31:59 +02001221 rc = boot_write_swap_info(fap_primary_slot,
1222 swap_state.swap_type,
David Vinczeba3bd602019-06-17 16:01:43 +02001223 current_image);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001224 assert(rc == 0);
1225 }
1226
David Vincze2d736ad2019-02-18 11:50:22 +01001227 rc = boot_write_swap_size(fap_primary_slot, bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001228 assert(rc == 0);
1229
Fabio Utzigba829042018-09-18 08:29:34 -03001230#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +01001231 rc = boot_write_enc_key(fap_primary_slot, 0, bs->enckey[0]);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001232 assert(rc == 0);
1233
David Vincze2d736ad2019-02-18 11:50:22 +01001234 rc = boot_write_enc_key(fap_primary_slot, 1, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001235 assert(rc == 0);
1236#endif
David Vincze2d736ad2019-02-18 11:50:22 +01001237 rc = boot_write_magic(fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001238 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001239 }
1240
Christopher Collinsa1c12042019-05-23 14:00:28 -07001241 /* If we wrote a trailer to the scratch area, erase it after we persist
1242 * a trailer to the primary slot. We do this to prevent mcuboot from
1243 * reading a stale status from the scratch area in case of immediate
1244 * reset.
1245 */
1246 erase_scratch = bs->use_scratch;
1247 bs->use_scratch = 0;
1248
Christopher Collins92ea77f2016-12-12 15:59:26 -08001249 bs->idx++;
Fabio Utzig39000012018-07-30 12:40:20 -03001250 bs->state = BOOT_STATUS_STATE_0;
Christopher Collins4772ac42017-02-27 20:08:01 -08001251 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001252 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collinsa1c12042019-05-23 14:00:28 -07001253
1254 if (erase_scratch) {
1255 rc = boot_erase_sector(fap_scratch, 0, sz);
1256 assert(rc == 0);
1257 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001258 }
Fabio Utzigba829042018-09-18 08:29:34 -03001259
David Vincze2d736ad2019-02-18 11:50:22 +01001260 flash_area_close(fap_primary_slot);
1261 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001262 flash_area_close(fap_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001263}
Fabio Utzig3488eef2017-06-12 10:25:43 -03001264#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001265
1266/**
David Vincze2d736ad2019-02-18 11:50:22 +01001267 * Overwrite primary slot with the image contained in the secondary slot.
1268 * If a prior copy operation was interrupted by a system reset, this function
1269 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001270 *
1271 * @param bs The current boot status. This function reads
1272 * this struct to determine if it is resuming
1273 * an interrupted swap operation. This
1274 * function writes the updated status to this
1275 * function on return.
1276 *
1277 * @return 0 on success; nonzero on failure.
1278 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001279#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001280static int
1281boot_copy_image(struct boot_status *bs)
1282{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001283 size_t sect_count;
1284 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001285 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001286 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001287 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001288 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +01001289 const struct flash_area *fap_primary_slot;
1290 const struct flash_area *fap_secondary_slot;
1291
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001292 (void)bs;
1293
Fabio Utzig13d9e352017-10-05 20:32:31 -03001294#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1295 uint32_t src_size = 0;
David Vincze2d736ad2019-02-18 11:50:22 +01001296 rc = boot_read_image_size(BOOT_SECONDARY_SLOT,
1297 boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT),
1298 &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001299 assert(rc == 0);
1300#endif
David Brown17609d82017-05-05 09:41:34 -06001301
David Vincze2d736ad2019-02-18 11:50:22 +01001302 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1303 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -06001304
David Vincze2d736ad2019-02-18 11:50:22 +01001305 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001306 assert (rc == 0);
1307
David Vincze2d736ad2019-02-18 11:50:22 +01001308 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001309 assert (rc == 0);
1310
David Vincze2d736ad2019-02-18 11:50:22 +01001311 sect_count = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001312 for (sect = 0, size = 0; sect < sect_count; sect++) {
David Vincze2d736ad2019-02-18 11:50:22 +01001313 this_size = boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, sect);
1314 rc = boot_erase_sector(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001315 assert(rc == 0);
1316
1317 size += this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001318
1319#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1320 if (size >= src_size) {
1321 break;
1322 }
1323#endif
David Brown17609d82017-05-05 09:41:34 -06001324 }
1325
Fabio Utzigba829042018-09-18 08:29:34 -03001326#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +01001327 if (IS_ENCRYPTED(boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT))) {
1328 rc = boot_enc_load(boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT),
1329 fap_secondary_slot,
1330 bs->enckey[1]);
1331
Fabio Utzigba829042018-09-18 08:29:34 -03001332 if (rc < 0) {
1333 return BOOT_EBADIMAGE;
1334 }
Fabio Utzige641ea52018-12-03 10:37:53 -02001335 if (rc == 0 && boot_enc_set_key(1, bs->enckey[1])) {
Fabio Utzigba829042018-09-18 08:29:34 -03001336 return BOOT_EBADIMAGE;
1337 }
1338 }
1339#endif
1340
David Vincze2d736ad2019-02-18 11:50:22 +01001341 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1342 size);
1343 rc = boot_copy_sector(fap_secondary_slot, fap_primary_slot, 0, 0, size);
David Brown17609d82017-05-05 09:41:34 -06001344
Fabio Utzig13d9e352017-10-05 20:32:31 -03001345 /*
1346 * Erases header and trailer. The trailer is erased because when a new
1347 * image is written without a trailer as is the case when using newt, the
1348 * trailer that was left might trigger a new upgrade.
1349 */
Christopher Collins2c88e692019-05-22 15:10:14 -07001350 BOOT_LOG_DBG("erasing secondary header");
David Vincze2d736ad2019-02-18 11:50:22 +01001351 rc = boot_erase_sector(fap_secondary_slot,
1352 boot_img_sector_off(&boot_data,
1353 BOOT_SECONDARY_SLOT, 0),
1354 boot_img_sector_size(&boot_data,
1355 BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001356 assert(rc == 0);
David Vincze2d736ad2019-02-18 11:50:22 +01001357 last_sector = boot_img_num_sectors(&boot_data, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -07001358 BOOT_LOG_DBG("erasing secondary trailer");
David Vincze2d736ad2019-02-18 11:50:22 +01001359 rc = boot_erase_sector(fap_secondary_slot,
1360 boot_img_sector_off(&boot_data,
1361 BOOT_SECONDARY_SLOT, last_sector),
1362 boot_img_sector_size(&boot_data,
1363 BOOT_SECONDARY_SLOT, last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001364 assert(rc == 0);
1365
David Vincze2d736ad2019-02-18 11:50:22 +01001366 flash_area_close(fap_primary_slot);
1367 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001368
David Vincze2d736ad2019-02-18 11:50:22 +01001369 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001370
1371 return 0;
1372}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001373#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001374
Christopher Collinsa1c12042019-05-23 14:00:28 -07001375#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -03001376/**
1377 * Swaps the two images in flash. If a prior copy operation was interrupted
1378 * by a system reset, this function completes that operation.
1379 *
1380 * @param bs The current boot status. This function reads
1381 * this struct to determine if it is resuming
1382 * an interrupted swap operation. This
1383 * function writes the updated status to this
1384 * function on return.
1385 *
1386 * @return 0 on success; nonzero on failure.
1387 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001388static int
Fabio Utzig338a19f2018-12-03 08:37:08 -02001389boot_swap_image(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001390{
1391 uint32_t sz;
1392 int first_sector_idx;
1393 int last_sector_idx;
David Vincze2d736ad2019-02-18 11:50:22 +01001394 int last_idx_secondary_slot;
Fabio Utzigcd5774b2017-11-29 10:18:26 -02001395 uint32_t swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001396 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001397#ifdef MCUBOOT_ENC_IMAGES
1398 const struct flash_area *fap;
1399 uint8_t slot;
1400 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001401#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001402 uint32_t size;
1403 uint32_t copy_size;
David Vincze2d736ad2019-02-18 11:50:22 +01001404 uint32_t primary_slot_size;
1405 uint32_t secondary_slot_size;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001406 int rc;
1407
1408 /* FIXME: just do this if asked by user? */
1409
1410 size = copy_size = 0;
1411
Fabio Utzig39000012018-07-30 12:40:20 -03001412 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Fabio Utzig46490722017-09-04 15:34:32 -03001413 /*
1414 * No swap ever happened, so need to find the largest image which
1415 * will be used to determine the amount of sectors to swap.
1416 */
David Vincze2d736ad2019-02-18 11:50:22 +01001417 hdr = boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001418 if (hdr->ih_magic == IMAGE_MAGIC) {
David Vincze2d736ad2019-02-18 11:50:22 +01001419 rc = boot_read_image_size(BOOT_PRIMARY_SLOT, hdr, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001420 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001421 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001422
Fabio Utzigba829042018-09-18 08:29:34 -03001423#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001424 if (IS_ENCRYPTED(hdr)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001425 fap = BOOT_IMG_AREA(&boot_data, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001426 rc = boot_enc_load(hdr, fap, bs->enckey[0]);
1427 assert(rc >= 0);
1428
1429 if (rc == 0) {
1430 rc = boot_enc_set_key(0, bs->enckey[0]);
1431 assert(rc == 0);
1432 } else {
1433 rc = 0;
1434 }
1435 } else {
1436 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1437 }
1438#endif
1439
David Vincze2d736ad2019-02-18 11:50:22 +01001440 hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001441 if (hdr->ih_magic == IMAGE_MAGIC) {
David Vincze2d736ad2019-02-18 11:50:22 +01001442 rc = boot_read_image_size(BOOT_SECONDARY_SLOT, hdr, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001443 assert(rc == 0);
1444 }
1445
Fabio Utzigba829042018-09-18 08:29:34 -03001446#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +01001447 hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001448 if (IS_ENCRYPTED(hdr)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001449 fap = BOOT_IMG_AREA(&boot_data, BOOT_SECONDARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001450 rc = boot_enc_load(hdr, fap, bs->enckey[1]);
1451 assert(rc >= 0);
1452
1453 if (rc == 0) {
1454 rc = boot_enc_set_key(1, bs->enckey[1]);
1455 assert(rc == 0);
1456 } else {
1457 rc = 0;
1458 }
1459 } else {
1460 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1461 }
1462#endif
1463
Fabio Utzig46490722017-09-04 15:34:32 -03001464 if (size > copy_size) {
1465 copy_size = size;
1466 }
1467
1468 bs->swap_size = copy_size;
1469 } else {
1470 /*
1471 * If a swap was under way, the swap_size should already be present
1472 * in the trailer...
1473 */
1474 rc = boot_read_swap_size(&bs->swap_size);
1475 assert(rc == 0);
1476
1477 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001478
1479#ifdef MCUBOOT_ENC_IMAGES
1480 for (slot = 0; slot <= 1; slot++) {
1481 rc = boot_read_enc_key(slot, bs->enckey[slot]);
1482 assert(rc == 0);
1483
1484 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001485 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001486 break;
1487 }
1488 }
1489
1490 if (i != BOOT_ENC_KEY_SIZE) {
1491 boot_enc_set_key(slot, bs->enckey[slot]);
1492 }
1493 }
1494#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001495 }
1496
David Vincze2d736ad2019-02-18 11:50:22 +01001497 primary_slot_size = 0;
1498 secondary_slot_size = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001499 last_sector_idx = 0;
David Vincze2d736ad2019-02-18 11:50:22 +01001500 last_idx_secondary_slot = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001501
1502 /*
1503 * Knowing the size of the largest image between both slots, here we
David Vincze2d736ad2019-02-18 11:50:22 +01001504 * find what is the last sector in the primary slot that needs swapping.
1505 * Since we already know that both slots are compatible, the secondary
1506 * slot's last sector is not really required after this check is finished.
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001507 */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001508 while (1) {
David Vincze2d736ad2019-02-18 11:50:22 +01001509 if ((primary_slot_size < copy_size) ||
1510 (primary_slot_size < secondary_slot_size)) {
1511 primary_slot_size += boot_img_sector_size(&boot_data,
1512 BOOT_PRIMARY_SLOT,
1513 last_sector_idx);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001514 }
David Vincze2d736ad2019-02-18 11:50:22 +01001515 if ((secondary_slot_size < copy_size) ||
1516 (secondary_slot_size < primary_slot_size)) {
1517 secondary_slot_size += boot_img_sector_size(&boot_data,
1518 BOOT_SECONDARY_SLOT,
1519 last_idx_secondary_slot);
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001520 }
David Vincze2d736ad2019-02-18 11:50:22 +01001521 if (primary_slot_size >= copy_size &&
1522 secondary_slot_size >= copy_size &&
1523 primary_slot_size == secondary_slot_size) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001524 break;
1525 }
1526 last_sector_idx++;
David Vincze2d736ad2019-02-18 11:50:22 +01001527 last_idx_secondary_slot++;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001528 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001529
1530 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001531 while (last_sector_idx >= 0) {
1532 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
Fabio Utzig39000012018-07-30 12:40:20 -03001533 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001534 boot_swap_sectors(first_sector_idx, sz, bs);
1535 }
1536
1537 last_sector_idx = first_sector_idx - 1;
1538 swap_idx++;
1539 }
1540
David Vincze2d736ad2019-02-18 11:50:22 +01001541#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001542 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001543 BOOT_LOG_WRN("%d status write fails performing the swap",
1544 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001545 }
1546#endif
1547
Christopher Collins92ea77f2016-12-12 15:59:26 -08001548 return 0;
1549}
David Brown17609d82017-05-05 09:41:34 -06001550#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001551
1552/**
David Vincze2d736ad2019-02-18 11:50:22 +01001553 * Marks the image in the primary slot as fully copied.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001554 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001555#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001556static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001557boot_set_copy_done(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001558{
1559 const struct flash_area *fap;
1560 int rc;
1561
David Vincze2d736ad2019-02-18 11:50:22 +01001562 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001563 if (rc != 0) {
1564 return BOOT_EFLASH;
1565 }
1566
1567 rc = boot_write_copy_done(fap);
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001568 flash_area_close(fap);
1569 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001570}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001571#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001572
1573/**
David Vincze2d736ad2019-02-18 11:50:22 +01001574 * Marks a reverted image in the primary slot as confirmed. This is necessary to
1575 * ensure the status bytes from the image revert operation don't get processed
1576 * on a subsequent boot.
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001577 *
1578 * NOTE: image_ok is tested before writing because if there's a valid permanent
David Vincze2d736ad2019-02-18 11:50:22 +01001579 * image installed on the primary slot and the new image to be upgrade to has a
1580 * bad sig, image_ok would be overwritten.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001581 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001582#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001583static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001584boot_set_image_ok(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001585{
1586 const struct flash_area *fap;
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001587 struct boot_swap_state state;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001588 int rc;
1589
David Vincze2d736ad2019-02-18 11:50:22 +01001590 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001591 if (rc != 0) {
1592 return BOOT_EFLASH;
1593 }
1594
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001595 rc = boot_read_swap_state(fap, &state);
1596 if (rc != 0) {
1597 rc = BOOT_EFLASH;
1598 goto out;
1599 }
1600
1601 if (state.image_ok == BOOT_FLAG_UNSET) {
1602 rc = boot_write_image_ok(fap);
1603 }
1604
1605out:
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001606 flash_area_close(fap);
1607 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001608}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001609#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001610
David Vinczee32483f2019-06-13 10:46:24 +02001611#if (BOOT_IMAGE_NUMBER > 1)
1612/**
1613 * Check the image dependency whether it is satisfied and modify
1614 * the swap type if necessary.
1615 *
1616 * @param dep Image dependency which has to be verified.
1617 *
1618 * @return 0 on success; nonzero on failure.
1619 */
1620static int
1621boot_verify_single_dependency(struct image_dependency *dep)
1622{
1623 struct image_version *dep_version;
1624 size_t dep_slot;
1625 int rc;
1626
1627 /* Determine the source of the image which is the subject of
1628 * the dependency and get it's version. */
1629 dep_slot = (boot_data.swap_type[dep->image_id] != BOOT_SWAP_TYPE_NONE) ?
1630 BOOT_SECONDARY_SLOT : BOOT_PRIMARY_SLOT;
1631 dep_version = &boot_data.imgs[dep->image_id][dep_slot].hdr.ih_ver;
1632
1633 rc = boot_is_version_sufficient(&dep->image_min_version, dep_version);
1634 if (rc != 0) {
1635 /* Dependency not satisfied.
1636 * Modify the swap type to decrease the version number of the image
1637 * (which will be located in the primary slot after the boot process),
1638 * consequently the number of unsatisfied dependencies will be
1639 * decreased or remain the same.
1640 */
1641 switch (BOOT_SWAP_TYPE(&boot_data)) {
1642 case BOOT_SWAP_TYPE_TEST:
1643 case BOOT_SWAP_TYPE_PERM:
1644 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE;
1645 break;
1646 case BOOT_SWAP_TYPE_NONE:
1647 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_REVERT;
1648 break;
1649 default:
1650 break;
1651 }
1652 }
1653
1654 return rc;
1655}
1656
1657/**
1658 * Read all dependency TLVs of an image from the flash and verify
1659 * one after another to see if they are all satisfied.
1660 *
1661 * @param slot Image slot number.
1662 *
1663 * @return 0 on success; nonzero on failure.
1664 */
1665static int
1666boot_verify_all_dependency(uint32_t slot)
1667{
1668 const struct flash_area *fap;
1669 struct image_header *hdr;
1670 struct image_tlv_info info;
1671 struct image_tlv tlv;
1672 struct image_dependency dep;
1673 uint32_t off;
1674 uint32_t end;
1675 bool dep_tlvs_found = false;
1676 int rc;
1677
1678 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
1679 if (rc != 0) {
1680 rc = BOOT_EFLASH;
1681 goto done;
1682 }
1683
1684 hdr = boot_img_hdr(&boot_data, slot);
1685 /* The TLVs come after the image. */
1686 off = hdr->ih_hdr_size + hdr->ih_img_size;
1687
1688 /* The TLV area always starts with an image_tlv_info structure. */
1689 rc = flash_area_read(fap, off, &info, sizeof(info));
1690 if (rc != 0) {
1691 rc = BOOT_EFLASH;
1692 goto done;
1693 }
1694
1695 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
1696 rc = BOOT_EBADIMAGE;
1697 goto done;
1698 }
1699 end = off + info.it_tlv_tot;
1700 off += sizeof(info);
1701
1702 /* Traverse through all of the TLVs to find the dependency TLVs. */
1703 for (; off < end; off += sizeof(tlv) + tlv.it_len) {
1704 rc = flash_area_read(fap, off, &tlv, sizeof(tlv));
1705 if (rc != 0) {
1706 rc = BOOT_EFLASH;
1707 goto done;
1708 }
1709
1710 if (tlv.it_type == IMAGE_TLV_DEPENDENCY) {
1711 if (!dep_tlvs_found) {
1712 dep_tlvs_found = true;
1713 }
1714
1715 if (tlv.it_len != sizeof(dep)) {
1716 rc = BOOT_EBADIMAGE;
1717 goto done;
1718 }
1719
1720 rc = flash_area_read(fap, off + sizeof(tlv), &dep, tlv.it_len);
1721 if (rc != 0) {
1722 rc = BOOT_EFLASH;
1723 goto done;
1724 }
1725
1726 /* Verify dependency and modify the swap type if not satisfied. */
1727 rc = boot_verify_single_dependency(&dep);
1728 if (rc != 0) {
1729 /* Dependency not satisfied. */
1730 goto done;
1731 }
1732
1733 /* Dependency satisfied, no action needed.
1734 * Continue with the next TLV entry.
1735 */
1736 } else if (dep_tlvs_found) {
1737 /* The dependency TLVs are contiguous in the TLV area. If a
1738 * dependency had already been found and the last read TLV
1739 * has a different type then there are no more dependency TLVs.
1740 * The search can be finished.
1741 */
1742 break;
1743 }
1744 }
1745
1746done:
1747 flash_area_close(fap);
1748 return rc;
1749}
1750
1751/**
1752 * Verify whether the image dependencies in the TLV area are
1753 * all satisfied and modify the swap type if necessary.
1754 *
1755 * @return 0 if all dependencies are satisfied,
1756 * nonzero otherwise.
1757 */
1758static int
1759boot_verify_single_image_dependency(void)
1760{
1761 size_t slot;
1762
1763 /* Determine the source of the dependency TLVs. Those dependencies have to
1764 * be checked which belong to the image that will be located in the primary
1765 * slot after the firmware update process.
1766 */
1767 if (BOOT_SWAP_TYPE(&boot_data) != BOOT_SWAP_TYPE_NONE &&
1768 BOOT_SWAP_TYPE(&boot_data) != BOOT_SWAP_TYPE_FAIL) {
1769 slot = BOOT_SECONDARY_SLOT;
1770 } else {
1771 slot = BOOT_PRIMARY_SLOT;
1772 }
1773
1774 return boot_verify_all_dependency(slot);
1775}
1776
1777/**
1778 * Iterate over all the images and verify whether the image dependencies in the
1779 * TLV area are all satisfied and update the related swap type if necessary.
1780 */
1781static void
1782boot_verify_all_image_dependency(void)
1783{
1784 current_image = 0;
1785 int rc;
1786
1787 while (current_image < BOOT_IMAGE_NUMBER) {
1788 rc = boot_verify_single_image_dependency();
1789 if ( rc == 0) {
1790 /* All dependencies've been satisfied, continue with next image. */
1791 current_image++;
1792 } else if (rc == BOOT_EBADVERSION) {
1793 /* Dependency check needs to be restarted. */
1794 current_image = 0;
1795 } else {
1796 /* Other error happened, images are inconsistent */
1797 return;
1798 }
1799 }
1800}
1801#endif /* (BOOT_IMAGE_NUMBER > 1) */
1802
Christopher Collins92ea77f2016-12-12 15:59:26 -08001803/**
David Vinczeba3bd602019-06-17 16:01:43 +02001804 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001805 *
David Vinczeba3bd602019-06-17 16:01:43 +02001806 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001807 *
1808 * @return 0 on success; nonzero on failure.
1809 */
1810static int
David Vinczeba3bd602019-06-17 16:01:43 +02001811boot_perform_update(struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001812{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001813 int rc;
1814
David Vinczeba3bd602019-06-17 16:01:43 +02001815 /* At this point there are no aborted swaps. */
1816#if defined(MCUBOOT_OVERWRITE_ONLY)
1817 rc = boot_copy_image(bs);
1818#elif defined(MCUBOOT_BOOTSTRAP)
1819 /* Check if the image update was triggered by a bad image in the
1820 * primary slot (the validity of the image in the secondary slot had
1821 * already been checked).
1822 */
1823 if (boot_check_header_erased(BOOT_PRIMARY_SLOT) == 0 ||
1824 boot_validate_slot(BOOT_PRIMARY_SLOT, bs) != 0) {
1825 rc = boot_copy_image(bs);
1826 } else {
1827 rc = boot_swap_image(bs);
1828 }
1829#else
1830 rc = boot_swap_image(bs);
1831#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001832 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001833
1834#ifndef MCUBOOT_OVERWRITE_ONLY
1835 /* The following state needs image_ok be explicitly set after the
1836 * swap was finished to avoid a new revert.
1837 */
1838 if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_REVERT ||
1839 BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_PERM) {
1840 rc = boot_set_image_ok();
1841 if (rc != 0) {
1842 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
1843 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001844 }
1845
David Vinczeba3bd602019-06-17 16:01:43 +02001846 if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_TEST ||
1847 BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_PERM ||
1848 BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_REVERT) {
1849 rc = boot_set_copy_done();
1850 if (rc != 0) {
1851 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001852 }
David Vinczeba3bd602019-06-17 16:01:43 +02001853 }
1854#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001855
David Vinczeba3bd602019-06-17 16:01:43 +02001856 return rc;
1857}
1858
1859/**
1860 * Completes a previously aborted image swap.
1861 *
1862 * @param bs The current boot status.
1863 *
1864 * @return 0 on success; nonzero on failure.
1865 */
1866#if !defined(MCUBOOT_OVERWRITE_ONLY)
1867static int
1868boot_complete_partial_swap(struct boot_status *bs)
1869{
1870 int rc;
1871
1872 /* Determine the type of swap operation being resumed from the
1873 * `swap-type` trailer field.
1874 */
1875 rc = boot_swap_image(bs);
1876 assert(rc == 0);
1877
1878 BOOT_SWAP_TYPE(&boot_data) = bs->swap_type;
1879
1880 /* The following states need image_ok be explicitly set after the
1881 * swap was finished to avoid a new revert.
1882 */
1883 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1884 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
1885 rc = boot_set_image_ok();
1886 if (rc != 0) {
1887 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
1888 }
1889 }
1890
1891 if (bs->swap_type == BOOT_SWAP_TYPE_TEST ||
1892 bs->swap_type == BOOT_SWAP_TYPE_PERM ||
1893 bs->swap_type == BOOT_SWAP_TYPE_REVERT) {
1894 rc = boot_set_copy_done();
1895 if (rc != 0) {
1896 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
1897 }
1898 }
1899
1900 if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_PANIC) {
1901 BOOT_LOG_ERR("panic!");
1902 assert(0);
1903
1904 /* Loop forever... */
1905 while (1) {}
1906 }
1907
1908 return rc;
1909}
1910#endif /* !MCUBOOT_OVERWRITE_ONLY */
1911
1912#if (BOOT_IMAGE_NUMBER > 1)
1913/**
1914 * Review the validity of previously determined swap types of other images.
1915 *
1916 * @param aborted_swap The current image upgrade is a
1917 * partial/aborted swap.
1918 */
1919static void
1920boot_review_image_swap_types(bool aborted_swap)
1921{
1922 /* In that case if we rebooted in the middle of an image upgrade process, we
1923 * must review the validity of swap types, that were previously determined
1924 * for other images. The image_ok flag had not been set before the reboot
1925 * for any of the updated images (only the copy_done flag) and thus falsely
1926 * the REVERT swap type has been determined for the previous images that had
1927 * been updated before the reboot.
1928 *
1929 * There are two separate scenarios that we have to deal with:
1930 *
1931 * 1. The reboot has happened during swapping an image:
1932 * The current image upgrade has been determined as a
1933 * partial/aborted swap.
1934 * 2. The reboot has happened between two separate image upgrades:
1935 * In this scenario we must check the swap type of the current image.
1936 * In those cases if it is NONE or REVERT we cannot certainly determine
1937 * the fact of a reboot. In a consistent state images must move in the
1938 * same direction or stay in place, e.g. in practice REVERT and TEST
1939 * swap types cannot be present at the same time. If the swap type of
1940 * the current image is either TEST, PERM or FAIL we must review the
1941 * already determined swap types of other images and set each false
1942 * REVERT swap types to NONE (these images had been successfully
1943 * updated before the system rebooted between two separate image
1944 * upgrades).
1945 */
1946
1947 if (current_image == 0) {
1948 /* Nothing to do */
1949 return;
1950 }
1951
1952 if (!aborted_swap) {
1953 if ((BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_NONE) ||
1954 (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_REVERT)) {
1955 /* Nothing to do */
1956 return;
1957 }
1958 }
1959
1960 for (uint8_t i = 0; i < current_image; i++) {
1961 if (boot_data.swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
1962 boot_data.swap_type[i] = BOOT_SWAP_TYPE_NONE;
1963 }
1964 }
1965}
1966#endif
1967
1968/**
1969 * Prepare image to be updated if required.
1970 *
1971 * Prepare image to be updated if required with completing an image swap
1972 * operation if one was aborted and/or determining the type of the
1973 * swap operation. In case of any error set the swap type to NONE.
1974 *
1975 * @param bs Pointer where the read and possibly updated
1976 * boot status can be written to.
1977 */
1978static void
1979boot_prepare_image_for_update(struct boot_status *bs)
1980{
1981 int rc;
1982
1983 /* Determine the sector layout of the image slots and scratch area. */
1984 rc = boot_read_sectors();
1985 if (rc != 0) {
1986 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
1987 " - too small?", BOOT_MAX_IMG_SECTORS);
1988 /* Unable to determine sector layout, continue with next image
1989 * if there is one.
1990 */
1991 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE;
1992 return;
1993 }
1994
1995 /* Attempt to read an image header from each slot. */
1996 rc = boot_read_image_headers(false);
1997 if (rc != 0) {
1998 /* Continue with next image if there is one. */
1999 BOOT_LOG_WRN("Failed reading image headers; Image=%u", current_image);
2000 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE;
2001 return;
2002 }
2003
2004 /* If the current image's slots aren't compatible, no swap is possible.
2005 * Just boot into primary slot.
2006 */
2007 if (boot_slots_compatible()) {
2008
2009 rc = boot_read_status(bs);
2010 if (rc != 0) {
2011 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
2012 current_image);
2013 /* Continue with next image if there is one. */
2014 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE;
2015 return;
2016 }
2017
2018 /* Determine if we rebooted in the middle of an image swap
2019 * operation. If a partial swap was detected, complete it.
2020 */
2021 if (bs->idx != BOOT_STATUS_IDX_0 || bs->state != BOOT_STATUS_STATE_0) {
2022
2023#if (BOOT_IMAGE_NUMBER > 1)
2024 boot_review_image_swap_types(true);
2025#endif
2026
2027#ifdef MCUBOOT_OVERWRITE_ONLY
2028 /* Should never arrive here, overwrite-only mode has
2029 * no swap state.
2030 */
2031 assert(0);
2032#else
2033 /* Determine the type of swap operation being resumed from the
2034 * `swap-type` trailer field.
2035 */
2036 rc = boot_complete_partial_swap(bs);
2037 assert(rc == 0);
2038#endif
2039 /* Attempt to read an image header from each slot. Ensure that
2040 * image headers in slots are aligned with headers in boot_data.
2041 */
2042 rc = boot_read_image_headers(false);
2043 assert(rc == 0);
2044
2045 /* Swap has finished set to NONE */
2046 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE;
2047 } else {
2048 /* There was no partial swap, determine swap type. */
2049 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
2050 BOOT_SWAP_TYPE(&boot_data) = boot_validated_swap_type(bs);
2051 } else if (boot_validate_slot(BOOT_SECONDARY_SLOT, bs) != 0) {
2052 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_FAIL;
2053 } else {
2054 BOOT_SWAP_TYPE(&boot_data) = bs->swap_type;
2055 }
2056
2057#if (BOOT_IMAGE_NUMBER > 1)
2058 boot_review_image_swap_types(false);
2059#endif
2060
2061#ifdef MCUBOOT_BOOTSTRAP
2062 if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_NONE) {
2063 /* Header checks are done first because they are
2064 * inexpensive. Since overwrite-only copies starting from
2065 * offset 0, if interrupted, it might leave a valid header
2066 * magic, so also run validation on the primary slot to be
2067 * sure it's not OK.
2068 */
2069 if (boot_check_header_erased(BOOT_PRIMARY_SLOT) == 0 ||
2070 boot_validate_slot(BOOT_PRIMARY_SLOT, bs) != 0) {
2071 if (boot_img_hdr(&boot_data,
2072 BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC &&
2073 boot_validate_slot(BOOT_SECONDARY_SLOT, bs) == 0)
2074 {
2075 /* Set swap type to REVERT to overwrite the primary
2076 * slot with the image contained in secondary slot
2077 * and to trigger the explicit setting of the
2078 * image_ok flag.
2079 */
2080 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_REVERT;
2081 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02002082 }
2083 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02002084#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002085 }
David Vinczeba3bd602019-06-17 16:01:43 +02002086 } else {
2087 /* In that case if slots are not compatible. */
2088 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002089 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002090}
2091
2092/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08002093 * Prepares the booting process. This function moves images around in flash as
2094 * appropriate, and tells you what address to boot from.
2095 *
2096 * @param rsp On success, indicates how booting should occur.
2097 *
2098 * @return 0 on success; nonzero on failure.
2099 */
2100int
2101boot_go(struct boot_rsp *rsp)
2102{
Marti Bolivar84898652017-06-13 17:20:22 -04002103 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02002104 struct boot_status bs;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002105 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002106 int fa_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002107
2108 /* The array of slot sectors are defined here (as opposed to file scope) so
2109 * that they don't get allocated for non-boot-loader apps. This is
2110 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002111 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08002112 */
David Vinczeba3bd602019-06-17 16:01:43 +02002113 static boot_sector_t
2114 primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
2115 static boot_sector_t
2116 secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
Fabio Utzig2bd980a2018-11-26 10:38:17 -02002117 static boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Christopher Collins92ea77f2016-12-12 15:59:26 -08002118
Fabio Utzigba829042018-09-18 08:29:34 -03002119#ifdef MCUBOOT_ENC_IMAGES
2120 /* FIXME: remove this after RAM is cleared by sim */
2121 boot_enc_zeroize();
2122#endif
2123
David Vinczeba3bd602019-06-17 16:01:43 +02002124 /* Iterate over all the images. By the end of the loop the swap type has
2125 * to be determined for each image and all aborted swaps have to be
2126 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002127 */
David Vinczeba3bd602019-06-17 16:01:43 +02002128 for (current_image = 0; current_image < BOOT_IMAGE_NUMBER; ++current_image)
2129 {
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03002130
David Vinczeba3bd602019-06-17 16:01:43 +02002131#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
2132 /* The keys used for encryption may no longer be valid (could belong to
2133 * another images). Therefore, mark them as invalid to force their reload
2134 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03002135 */
David Vinczeba3bd602019-06-17 16:01:43 +02002136 boot_enc_mark_keys_invalid();
David Brown554c52e2017-06-30 16:01:07 -06002137#endif
2138
David Vinczeba3bd602019-06-17 16:01:43 +02002139 BOOT_IMG(&boot_data, BOOT_PRIMARY_SLOT).sectors =
2140 primary_slot_sectors[current_image];
2141 BOOT_IMG(&boot_data, BOOT_SECONDARY_SLOT).sectors =
2142 secondary_slot_sectors[current_image];
2143 boot_data.scratch.sectors = scratch_sectors;
2144
2145 /* Open primary and secondary image areas for the duration
2146 * of this call.
2147 */
2148 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2149 fa_id = flash_area_id_from_image_slot(slot);
2150 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot));
2151 assert(rc == 0);
2152 }
2153 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
2154 &BOOT_SCRATCH_AREA(&boot_data));
2155 assert(rc == 0);
2156
2157 /* Determine swap type and complete swap if it has been aborted. */
2158 boot_prepare_image_for_update(&bs);
2159 }
2160
David Vinczee32483f2019-06-13 10:46:24 +02002161#if (BOOT_IMAGE_NUMBER > 1)
2162 /* Iterate over all the images and verify whether the image dependencies
2163 * are all satisfied and update swap type if necessary.
2164 */
2165 boot_verify_all_image_dependency();
2166#endif
2167
David Vinczeba3bd602019-06-17 16:01:43 +02002168 /* Iterate over all the images. At this point there are no aborted swaps
2169 * and the swap types are determined for each image. By the end of the loop
2170 * all required update operations will have been finished.
2171 */
2172 for (current_image = 0; current_image < BOOT_IMAGE_NUMBER; ++current_image)
2173 {
2174
2175#if (BOOT_IMAGE_NUMBER > 1)
2176#ifdef MCUBOOT_ENC_IMAGES
2177 /* The keys used for encryption may no longer be valid (could belong to
2178 * another images). Therefore, mark them as invalid to force their reload
2179 * by boot_enc_load().
2180 */
2181 boot_enc_mark_keys_invalid();
2182#endif /* MCUBOOT_ENC_IMAGES */
2183
2184 /* Indicate that swap is not aborted */
2185 memset(&bs, 0, sizeof bs);
2186 bs.idx = BOOT_STATUS_IDX_0;
2187 bs.state = BOOT_STATUS_STATE_0;
2188#endif /* (BOOT_IMAGE_NUMBER > 1) */
2189
2190 /* Set the previously determined swap type */
2191 bs.swap_type = BOOT_SWAP_TYPE(&boot_data);
2192
2193 switch (BOOT_SWAP_TYPE(&boot_data)) {
2194 case BOOT_SWAP_TYPE_NONE:
2195 break;
2196
2197 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
2198 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
2199 case BOOT_SWAP_TYPE_REVERT:
2200 rc = boot_perform_update(&bs);
2201 assert(rc == 0);
2202 break;
2203
2204 case BOOT_SWAP_TYPE_FAIL:
2205 /* The image in secondary slot was invalid and is now erased. Ensure
2206 * we don't try to boot into it again on the next reboot. Do this by
2207 * pretending we just reverted back to primary slot.
2208 */
2209#ifndef MCUBOOT_OVERWRITE_ONLY
2210 /* image_ok needs to be explicitly set to avoid a new revert. */
2211 rc = boot_set_image_ok();
2212 if (rc != 0) {
2213 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
2214 }
2215#endif /* !MCUBOOT_OVERWRITE_ONLY */
2216 break;
2217
2218 default:
2219 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
2220 }
2221
2222 if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_PANIC) {
2223 BOOT_LOG_ERR("panic!");
2224 assert(0);
2225
2226 /* Loop forever... */
2227 while (1) {}
2228 }
2229 }
2230
2231 /* Iterate over all the images. At this point all required update operations
2232 * have finished. By the end of the loop each image in the primary slot will
2233 * have been re-validated.
2234 */
2235 for (current_image = 0; current_image < BOOT_IMAGE_NUMBER; ++current_image)
2236 {
2237 if (BOOT_SWAP_TYPE(&boot_data) != BOOT_SWAP_TYPE_NONE) {
2238 /* Attempt to read an image header from each slot. Ensure that image
2239 * headers in slots are aligned with headers in boot_data.
2240 */
2241 rc = boot_read_image_headers(false);
2242 if (rc != 0) {
2243 goto out;
2244 }
2245 /* Since headers were reloaded, it can be assumed we just performed
2246 * a swap or overwrite. Now the header info that should be used to
2247 * provide the data for the bootstrap, which previously was at
2248 * secondary slot, was updated to primary slot.
2249 */
2250 }
2251
2252#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
2253 rc = boot_validate_slot(BOOT_PRIMARY_SLOT, NULL);
2254 if (rc != 0) {
2255 rc = BOOT_EBADIMAGE;
2256 goto out;
2257 }
2258#else
2259 /* Even if we're not re-validating the primary slot, we could be booting
2260 * onto an empty flash chip. At least do a basic sanity check that
2261 * the magic number on the image is OK.
2262 */
2263 if (BOOT_IMG(&boot_data, BOOT_PRIMARY_SLOT).hdr.ih_magic !=
2264 IMAGE_MAGIC) {
2265 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
2266 &boot_img_hdr(&boot_data,BOOT_PRIMARY_SLOT)->ih_magic,
2267 current_image);
2268 rc = BOOT_EBADIMAGE;
2269 goto out;
2270 }
2271#endif
2272 }
2273
2274 /* Always boot from the primary slot of Image 0. */
2275 current_image = 0;
2276 rsp->br_flash_dev_id =
2277 BOOT_IMG_AREA(&boot_data, BOOT_PRIMARY_SLOT)->fa_device_id;
2278 rsp->br_image_off =
2279 boot_img_slot_off(&boot_data, BOOT_PRIMARY_SLOT);
2280 rsp->br_hdr =
2281 boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002282
Marti Bolivarc0b47912017-06-13 17:18:09 -04002283 out:
David Vinczeba3bd602019-06-17 16:01:43 +02002284 for (current_image = 0; current_image < BOOT_IMAGE_NUMBER; ++current_image)
2285 {
2286 flash_area_close(BOOT_SCRATCH_AREA(&boot_data));
2287 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2288 flash_area_close(BOOT_IMG_AREA(&boot_data,
2289 BOOT_NUM_SLOTS - 1 - slot));
2290 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04002291 }
2292 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002293}
2294
2295int
2296split_go(int loader_slot, int split_slot, void **entry)
2297{
Marti Bolivarc50926f2017-06-14 09:35:40 -04002298 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08002299 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002300 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002301 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002302 int rc;
2303
Christopher Collins92ea77f2016-12-12 15:59:26 -08002304 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
2305 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04002306 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002307 }
David Vinczeba3bd602019-06-17 16:01:43 +02002308 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
2309 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002310
2311 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
2312 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07002313 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04002314 assert(rc == 0);
2315 split_flash_id = flash_area_id_from_image_slot(split_slot);
2316 rc = flash_area_open(split_flash_id,
2317 &BOOT_IMG_AREA(&boot_data, split_slot));
2318 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002319
2320 /* Determine the sector layout of the image slots and scratch area. */
2321 rc = boot_read_sectors();
2322 if (rc != 0) {
2323 rc = SPLIT_GO_ERR;
2324 goto done;
2325 }
2326
Fabio Utzig9c25fa72017-12-12 14:57:20 -02002327 rc = boot_read_image_headers(true);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002328 if (rc != 0) {
2329 goto done;
2330 }
2331
Christopher Collins92ea77f2016-12-12 15:59:26 -08002332 /* Don't check the bootable image flag because we could really call a
2333 * bootable or non-bootable image. Just validate that the image check
2334 * passes which is distinct from the normal check.
2335 */
Marti Bolivarf804f622017-06-12 15:41:48 -04002336 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04002337 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04002338 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04002339 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002340 if (rc != 0) {
2341 rc = SPLIT_GO_NON_MATCHING;
2342 goto done;
2343 }
2344
Marti Bolivarea088872017-06-12 17:10:49 -04002345 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04002346 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08002347 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002348 rc = SPLIT_GO_OK;
2349
2350done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04002351 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
2352 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002353 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002354 return rc;
2355}