blob: 80789e5b5b1fefbccc5d1417d78bac70793f56c7 [file] [log] [blame]
Tamas Banf70ef8c2017-12-19 15:35:09 +00001/*
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
Tamas Ban581034a2017-12-19 19:54:37 +000020/*
Tamas Ban4fb8e9d2018-02-23 14:22:03 +000021 * Original code taken from mcuboot project at:
David Vincze39e78552018-10-10 17:10:01 +020022 * https://github.com/JuulLabs-OSS/mcuboot
David Vincze401c7422019-06-21 20:44:05 +020023 * Git SHA of the original version: 3c469bc698a9767859ed73cd0201c44161204d5c
Tamas Ban5b647472019-01-05 08:59:30 +000024 * Modifications are Copyright (c) 2018-2019 Arm Limited.
Tamas Ban581034a2017-12-19 19:54:37 +000025 */
26
Tamas Banf70ef8c2017-12-19 15:35:09 +000027/**
28 * This file provides an interface to the boot loader. Functions defined in
29 * this file should only be called while the boot loader is running.
30 */
31
32#include <assert.h>
33#include <stddef.h>
34#include <stdbool.h>
35#include <inttypes.h>
36#include <stdlib.h>
37#include <string.h>
Tamas Banc3828852018-02-01 12:24:16 +000038#include "flash_map/flash_map.h"
Tamas Banf70ef8c2017-12-19 15:35:09 +000039#include "bootutil/bootutil.h"
40#include "bootutil/image.h"
41#include "bootutil_priv.h"
Tamas Bana9de4a62018-09-18 08:09:45 +010042#include "bl2/include/tfm_boot_status.h"
43#include "bl2/include/boot_record.h"
David Vincze060968d2019-05-23 01:13:14 +020044#include "security_cnt.h"
Tamas Banf70ef8c2017-12-19 15:35:09 +000045
46#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
47#include "bootutil/bootutil_log.h"
48
Tamas Banf70ef8c2017-12-19 15:35:09 +000049static struct boot_loader_state boot_data;
50
Oliver Swedef9982442018-08-24 18:37:44 +010051#if !defined(MCUBOOT_NO_SWAP) && !defined(MCUBOOT_RAM_LOADING)
David Vincze39e78552018-10-10 17:10:01 +020052
David Vincze8bdfc2d2019-03-18 15:49:23 +010053#if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT) && !defined(MCUBOOT_OVERWRITE_ONLY)
David Vincze39e78552018-10-10 17:10:01 +020054static int boot_status_fails = 0;
55#define BOOT_STATUS_ASSERT(x) \
56 do { \
57 if (!(x)) { \
58 boot_status_fails++; \
59 } \
60 } while (0)
61#else
David Vincze401c7422019-06-21 20:44:05 +020062#define BOOT_STATUS_ASSERT(x) ASSERT(x)
David Vincze39e78552018-10-10 17:10:01 +020063#endif
64
Tamas Banf70ef8c2017-12-19 15:35:09 +000065struct boot_status_table {
David Vincze8bdfc2d2019-03-18 15:49:23 +010066 uint8_t bst_magic_primary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +000067 uint8_t bst_magic_scratch;
David Vincze8bdfc2d2019-03-18 15:49:23 +010068 uint8_t bst_copy_done_primary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +000069 uint8_t bst_status_source;
70};
71
72/**
73 * This set of tables maps swap state contents to boot status location.
74 * When searching for a match, these tables must be iterated in order.
75 */
76static const struct boot_status_table boot_status_tables[] = {
77 {
David Vincze8bdfc2d2019-03-18 15:49:23 +010078 /* | primary slot | scratch |
79 * ----------+--------------+--------------|
80 * magic | Good | Any |
81 * copy-done | Set | N/A |
82 * ----------+--------------+--------------'
83 * source: none |
84 * ----------------------------------------'
Tamas Banf70ef8c2017-12-19 15:35:09 +000085 */
David Vincze8bdfc2d2019-03-18 15:49:23 +010086 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
David Vincze401c7422019-06-21 20:44:05 +020087 .bst_magic_scratch = BOOT_MAGIC_NOTGOOD,
David Vincze8bdfc2d2019-03-18 15:49:23 +010088 .bst_copy_done_primary_slot = BOOT_FLAG_SET,
89 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
Tamas Banf70ef8c2017-12-19 15:35:09 +000090 },
91
92 {
David Vincze8bdfc2d2019-03-18 15:49:23 +010093 /* | primary slot | scratch |
94 * ----------+--------------+--------------|
95 * magic | Good | Any |
96 * copy-done | Unset | N/A |
97 * ----------+--------------+--------------'
98 * source: primary slot |
99 * ----------------------------------------'
Tamas Banf70ef8c2017-12-19 15:35:09 +0000100 */
David Vincze8bdfc2d2019-03-18 15:49:23 +0100101 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
David Vincze401c7422019-06-21 20:44:05 +0200102 .bst_magic_scratch = BOOT_MAGIC_NOTGOOD,
David Vincze8bdfc2d2019-03-18 15:49:23 +0100103 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
104 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Tamas Banf70ef8c2017-12-19 15:35:09 +0000105 },
106
107 {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100108 /* | primary slot | scratch |
109 * ----------+--------------+--------------|
110 * magic | Any | Good |
111 * copy-done | Any | N/A |
112 * ----------+--------------+--------------'
113 * source: scratch |
114 * ----------------------------------------'
Tamas Banf70ef8c2017-12-19 15:35:09 +0000115 */
David Vincze8bdfc2d2019-03-18 15:49:23 +0100116 .bst_magic_primary_slot = BOOT_MAGIC_ANY,
117 .bst_magic_scratch = BOOT_MAGIC_GOOD,
118 .bst_copy_done_primary_slot = BOOT_FLAG_ANY,
119 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
Tamas Banf70ef8c2017-12-19 15:35:09 +0000120 },
121
122 {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100123 /* | primary slot | scratch |
124 * ----------+--------------+--------------|
125 * magic | Unset | Any |
126 * copy-done | Unset | N/A |
127 * ----------+--------------+--------------|
128 * source: varies |
129 * ----------------------------------------+--------------------------+
Tamas Banf70ef8c2017-12-19 15:35:09 +0000130 * This represents one of two cases: |
131 * o No swaps ever (no status to read, so no harm in checking). |
David Vincze8bdfc2d2019-03-18 15:49:23 +0100132 * o Mid-revert; status in the primary slot. |
Tamas Banf70ef8c2017-12-19 15:35:09 +0000133 * -------------------------------------------------------------------'
134 */
David Vincze8bdfc2d2019-03-18 15:49:23 +0100135 .bst_magic_primary_slot = BOOT_MAGIC_UNSET,
136 .bst_magic_scratch = BOOT_MAGIC_ANY,
137 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
138 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Tamas Banf70ef8c2017-12-19 15:35:09 +0000139 },
140};
141
142#define BOOT_STATUS_TABLES_COUNT \
Tamas Ban581034a2017-12-19 19:54:37 +0000143 (sizeof(boot_status_tables) / sizeof(boot_status_tables[0]))
Tamas Banf70ef8c2017-12-19 15:35:09 +0000144
145#define BOOT_LOG_SWAP_STATE(area, state) \
David Vincze401c7422019-06-21 20:44:05 +0200146 BOOT_LOG_INF("%s: magic=%5s, swap_type=0x%x, copy_done=0x%x, " \
147 "image_ok=0x%x", \
Tamas Banf70ef8c2017-12-19 15:35:09 +0000148 (area), \
149 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
150 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
151 "bad"), \
David Vincze401c7422019-06-21 20:44:05 +0200152 (state)->swap_type, \
Tamas Banf70ef8c2017-12-19 15:35:09 +0000153 (state)->copy_done, \
154 (state)->image_ok)
Oliver Swedef9982442018-08-24 18:37:44 +0100155#endif /* !MCUBOOT_NO_SWAP && !MCUBOOT_RAM_LOADING */
Tamas Banf70ef8c2017-12-19 15:35:09 +0000156
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000157
158static int
159boot_read_image_header(int slot, struct image_header *out_hdr)
160{
161 const struct flash_area *fap = NULL;
162 int area_id;
163 int rc;
164
165 area_id = flash_area_id_from_image_slot(slot);
166 rc = flash_area_open(area_id, &fap);
167 if (rc != 0) {
168 rc = BOOT_EFLASH;
169 goto done;
170 }
171
172 rc = flash_area_read(fap, 0, out_hdr, sizeof(*out_hdr));
173 if (rc != 0) {
174 rc = BOOT_EFLASH;
175 goto done;
176 }
177
178 rc = 0;
179
180done:
181 flash_area_close(fap);
182 return rc;
183}
184
185static int
David Vincze39e78552018-10-10 17:10:01 +0200186boot_read_image_headers(bool require_all)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000187{
188 int rc;
189 int i;
190
191 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
192 rc = boot_read_image_header(i, boot_img_hdr(&boot_data, i));
193 if (rc != 0) {
David Vincze39e78552018-10-10 17:10:01 +0200194 /* If `require_all` is set, fail on any single fail, otherwise
195 * if at least the first slot's header was read successfully,
196 * then the boot loader can attempt a boot.
197 *
198 * Failure to read any headers is a fatal error.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000199 */
David Vincze39e78552018-10-10 17:10:01 +0200200 if (i > 0 && !require_all) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000201 return 0;
202 } else {
203 return rc;
204 }
205 }
206 }
207
208 return 0;
209}
210
211static uint8_t
212boot_write_sz(void)
213{
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000214 uint8_t elem_sz;
215 uint8_t align;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000216
217 /* Figure out what size to write update status update as. The size depends
218 * on what the minimum write size is for scratch area, active image slot.
219 * We need to use the bigger of those 2 values.
220 */
David Vincze401c7422019-06-21 20:44:05 +0200221 elem_sz = flash_area_align(boot_data.imgs[BOOT_PRIMARY_SLOT].area);
222 align = flash_area_align(boot_data.scratch.area);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000223 if (align > elem_sz) {
224 elem_sz = align;
225 }
226
227 return elem_sz;
228}
229
230/**
231 * Determines the sector layout of both image slots and the scratch area.
232 * This information is necessary for calculating the number of bytes to erase
233 * and copy during an image swap. The information collected during this
234 * function is used to populate the boot_data global.
235 */
236static int
237boot_read_sectors(void)
238{
239 int rc;
240
David Vincze8bdfc2d2019-03-18 15:49:23 +0100241 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_PRIMARY);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000242 if (rc != 0) {
243 return BOOT_EFLASH;
244 }
245
David Vincze8bdfc2d2019-03-18 15:49:23 +0100246 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_SECONDARY);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000247 if (rc != 0) {
248 return BOOT_EFLASH;
249 }
250
David Vincze401c7422019-06-21 20:44:05 +0200251 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_SCRATCH);
252 if (rc != 0) {
253 return BOOT_EFLASH;
254 }
255
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000256 BOOT_WRITE_SZ(&boot_data) = boot_write_sz();
257
258 return 0;
259}
260
David Vincze060968d2019-05-23 01:13:14 +0200261/**
262 * Validate image hash/signature and security counter in a slot.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000263 */
264static int
David Vincze401c7422019-06-21 20:44:05 +0200265boot_image_check(struct image_header *hdr, const struct flash_area *fap,
266 struct boot_status *bs)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000267{
268 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
269
David Vincze401c7422019-06-21 20:44:05 +0200270 (void)bs;
271
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000272 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
Tamas Ban0e8ab302019-01-17 11:45:31 +0000273 NULL, 0, NULL)) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000274 return BOOT_EBADIMAGE;
275 }
276 return 0;
277}
278
David Vincze401c7422019-06-21 20:44:05 +0200279/*
280 * Check that a memory area consists of a given value.
281 */
282static inline bool
283boot_data_is_set_to(uint8_t val, void *data, size_t len)
David Vincze39e78552018-10-10 17:10:01 +0200284{
285 uint8_t i;
David Vincze401c7422019-06-21 20:44:05 +0200286 uint8_t *p = (uint8_t *)data;
287 for (i = 0; i < len; i++) {
288 if (val != p[i]) {
289 return false;
David Vincze39e78552018-10-10 17:10:01 +0200290 }
291 }
David Vincze401c7422019-06-21 20:44:05 +0200292 return true;
David Vincze39e78552018-10-10 17:10:01 +0200293}
294
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000295static int
David Vincze401c7422019-06-21 20:44:05 +0200296boot_check_header_erased(int slot)
297{
298 const struct flash_area *fap;
299 struct image_header *hdr;
300 uint8_t erased_val;
301 int rc;
302
303 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
304 if (rc != 0) {
305 return -1;
306 }
307
308 erased_val = flash_area_erased_val(fap);
309 flash_area_close(fap);
310
311 hdr = boot_img_hdr(&boot_data, slot);
312 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic,
313 sizeof(hdr->ih_magic))) {
314 return -1;
315 }
316
317 return 0;
318}
319
320static int
321boot_validate_slot(int slot, struct boot_status *bs)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000322{
323 const struct flash_area *fap;
324 struct image_header *hdr;
325 int rc;
326
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000327 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
328 if (rc != 0) {
329 return BOOT_EFLASH;
330 }
331
David Vincze39e78552018-10-10 17:10:01 +0200332 hdr = boot_img_hdr(&boot_data, slot);
David Vincze401c7422019-06-21 20:44:05 +0200333 if ((boot_check_header_erased(slot) == 0) ||
334 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100335 /* No bootable image in slot; continue booting from the primary slot. */
David Vincze401c7422019-06-21 20:44:05 +0200336 rc = -1;
337 goto out;
David Vincze39e78552018-10-10 17:10:01 +0200338 }
339
Tamas Bana9de4a62018-09-18 08:09:45 +0100340 if ((hdr->ih_magic != IMAGE_MAGIC ||
David Vincze401c7422019-06-21 20:44:05 +0200341 boot_image_check(hdr, fap, bs) != 0)) {
342 if (slot != BOOT_PRIMARY_SLOT) {
David Vincze26e8c8a2018-08-28 16:59:41 +0200343 rc = flash_area_erase(fap, 0, fap->fa_size);
344 if(rc != 0) {
David Vincze401c7422019-06-21 20:44:05 +0200345 rc = BOOT_EFLASH;
346 goto out;
David Vincze26e8c8a2018-08-28 16:59:41 +0200347 }
David Vincze8bdfc2d2019-03-18 15:49:23 +0100348 /* Image in the secondary slot is invalid. Erase the image and
349 * continue booting from the primary slot.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000350 */
351 }
David Vincze8bdfc2d2019-03-18 15:49:23 +0100352 BOOT_LOG_ERR("Authentication failed! Image in the %s slot is not valid."
353 , (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Vincze401c7422019-06-21 20:44:05 +0200354 rc = -1;
355 goto out;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000356 }
357
David Vincze8bdfc2d2019-03-18 15:49:23 +0100358 /* Image in the secondary slot is valid. */
David Vincze401c7422019-06-21 20:44:05 +0200359 rc = 0;
360
361out:
362 flash_area_close(fap);
363 return rc;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000364}
365
David Vincze060968d2019-05-23 01:13:14 +0200366/**
367 * Updates the stored security counter value with the image's security counter
368 * value which resides in the given slot if it's greater than the stored value.
369 *
370 * @param slot Slot number of the image.
371 * @param hdr Pointer to the image header structure of the image that is
372 * currently stored in the given slot.
373 *
374 * @return 0 on success; nonzero on failure.
375 */
376static int
377boot_update_security_counter(int slot, struct image_header *hdr)
378{
379 const struct flash_area *fap = NULL;
380 uint32_t img_security_cnt;
381 int rc;
382
383 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
384 if (rc != 0) {
385 rc = BOOT_EFLASH;
386 goto done;
387 }
388
389 rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt);
390 if (rc != 0) {
391 goto done;
392 }
393
394 rc = boot_nv_security_counter_update(0, img_security_cnt);
395 if (rc != 0) {
396 goto done;
397 }
398
399done:
400 flash_area_close(fap);
401 return rc;
402}
403
Oliver Swedef9982442018-08-24 18:37:44 +0100404#if !defined(MCUBOOT_NO_SWAP) && !defined(MCUBOOT_OVERWRITE_ONLY)
405/*
406 * Compute the total size of the given image. Includes the size of
407 * the TLVs.
408 */
409static int
410boot_read_image_size(int slot, struct image_header *hdr, uint32_t *size)
411{
412 const struct flash_area *fap = NULL;
413 struct image_tlv_info info;
414 int area_id;
415 int rc;
416
417 area_id = flash_area_id_from_image_slot(slot);
418 rc = flash_area_open(area_id, &fap);
419 if (rc != 0) {
420 rc = BOOT_EFLASH;
421 goto done;
422 }
423
424 rc = flash_area_read(fap, hdr->ih_hdr_size + hdr->ih_img_size,
425 &info, sizeof(info));
426 if (rc != 0) {
427 rc = BOOT_EFLASH;
428 goto done;
429 }
430 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
431 rc = BOOT_EBADIMAGE;
432 goto done;
433 }
434 *size = hdr->ih_hdr_size + hdr->ih_img_size + info.it_tlv_tot;
435 rc = 0;
436
437done:
438 flash_area_close(fap);
439 return rc;
440}
441#endif /* !MCUBOOT_NO_SWAP && !MCUBOOT_OVERWRITE_ONLY */
442
443#if !defined(MCUBOOT_NO_SWAP) && !defined(MCUBOOT_RAM_LOADING)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000444/**
Tamas Ban581034a2017-12-19 19:54:37 +0000445 * Determines where in flash the most recent boot status is stored. The boot
Tamas Banf70ef8c2017-12-19 15:35:09 +0000446 * status is necessary for completing a swap that was interrupted by a boot
447 * loader reset.
448 *
David Vincze401c7422019-06-21 20:44:05 +0200449 * @return A BOOT_STATUS_SOURCE_[...] code indicating where status should
450 * be read from.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000451 */
452static int
453boot_status_source(void)
454{
455 const struct boot_status_table *table;
456 struct boot_swap_state state_scratch;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100457 struct boot_swap_state state_primary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000458 int rc;
David Vincze39e78552018-10-10 17:10:01 +0200459 size_t i;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000460 uint8_t source;
461
David Vincze8bdfc2d2019-03-18 15:49:23 +0100462 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY,
463 &state_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000464 assert(rc == 0);
465
466 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
467 assert(rc == 0);
468
David Vincze401c7422019-06-21 20:44:05 +0200469 BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000470 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
471
472 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
473 table = &boot_status_tables[i];
474
David Vincze401c7422019-06-21 20:44:05 +0200475 if (boot_magic_compatible_check(table->bst_magic_primary_slot,
476 state_primary_slot.magic) &&
477 boot_magic_compatible_check(table->bst_magic_scratch,
478 state_scratch.magic) &&
David Vincze8bdfc2d2019-03-18 15:49:23 +0100479 (table->bst_copy_done_primary_slot == BOOT_FLAG_ANY ||
480 table->bst_copy_done_primary_slot == state_primary_slot.copy_done))
481 {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000482 source = table->bst_status_source;
483 BOOT_LOG_INF("Boot source: %s",
484 source == BOOT_STATUS_SOURCE_NONE ? "none" :
485 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
David Vincze8bdfc2d2019-03-18 15:49:23 +0100486 source == BOOT_STATUS_SOURCE_PRIMARY_SLOT ?
487 "primary slot" : "BUG; can't happen");
Tamas Banf70ef8c2017-12-19 15:35:09 +0000488 return source;
489 }
490 }
491
492 BOOT_LOG_INF("Boot source: none");
493 return BOOT_STATUS_SOURCE_NONE;
494}
495
David Vincze401c7422019-06-21 20:44:05 +0200496/*
497 * Slots are compatible when all sectors that store upto to size of the image
498 * round up to sector size, in both slot's are able to fit in the scratch
499 * area, and have sizes that are a multiple of each other (powers of two
500 * presumably!).
Tamas Banf70ef8c2017-12-19 15:35:09 +0000501 */
502static int
Tamas Banf70ef8c2017-12-19 15:35:09 +0000503boot_slots_compatible(void)
504{
David Vincze401c7422019-06-21 20:44:05 +0200505 size_t num_sectors_primary;
506 size_t num_sectors_secondary;
507 size_t sz0, sz1;
508 size_t primary_slot_sz, secondary_slot_sz;
509 size_t scratch_sz;
510 size_t i, j;
511 int8_t smaller;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000512
David Vincze401c7422019-06-21 20:44:05 +0200513 num_sectors_primary =
514 boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT);
515 num_sectors_secondary =
516 boot_img_num_sectors(&boot_data, BOOT_SECONDARY_SLOT);
517 if ((num_sectors_primary > BOOT_MAX_IMG_SECTORS) ||
518 (num_sectors_secondary > BOOT_MAX_IMG_SECTORS)) {
David Vincze39e78552018-10-10 17:10:01 +0200519 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
Tamas Banf70ef8c2017-12-19 15:35:09 +0000520 return 0;
521 }
David Vincze39e78552018-10-10 17:10:01 +0200522
David Vincze401c7422019-06-21 20:44:05 +0200523 scratch_sz = boot_scratch_area_size(&boot_data);
524
525 /*
526 * The following loop scans all sectors in a linear fashion, assuring that
527 * for each possible sector in each slot, it is able to fit in the other
528 * slot's sector or sectors. Slot's should be compatible as long as any
529 * number of a slot's sectors are able to fit into another, which only
530 * excludes cases where sector sizes are not a multiple of each other.
531 */
532 i = sz0 = primary_slot_sz = 0;
533 j = sz1 = secondary_slot_sz = 0;
534 smaller = 0;
535 while (i < num_sectors_primary || j < num_sectors_secondary) {
536 if (sz0 == sz1) {
537 sz0 += boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
538 sz1 += boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT, j);
539 i++;
540 j++;
541 } else if (sz0 < sz1) {
542 sz0 += boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
543 /* Guarantee that multiple sectors of the secondary slot
544 * fit into the primary slot.
545 */
546 if (smaller == 2) {
547 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible"
548 " sectors");
549 return 0;
550 }
551 smaller = 1;
552 i++;
553 } else {
554 sz1 += boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT, j);
555 /* Guarantee that multiple sectors of the primary slot
556 * fit into the secondary slot.
557 */
558 if (smaller == 1) {
559 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible"
560 " sectors");
561 return 0;
562 }
563 smaller = 2;
564 j++;
565 }
566 if (sz0 == sz1) {
567 primary_slot_sz += sz0;
568 secondary_slot_sz += sz1;
569 /* Scratch has to fit each swap operation to the size of the larger
570 * sector among the primary slot and the secondary slot.
571 */
572 if (sz0 > scratch_sz || sz1 > scratch_sz) {
573 BOOT_LOG_WRN("Cannot upgrade: not all sectors fit inside"
574 " scratch");
575 return 0;
576 }
577 smaller = sz0 = sz1 = 0;
578 }
David Vincze39e78552018-10-10 17:10:01 +0200579 }
580
David Vincze401c7422019-06-21 20:44:05 +0200581 if ((i != num_sectors_primary) ||
582 (j != num_sectors_secondary) ||
583 (primary_slot_sz != secondary_slot_sz)) {
584 BOOT_LOG_WRN("Cannot upgrade: slots are not compatible");
585 return 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000586 }
587
588 return 1;
589}
590
Tamas Banf70ef8c2017-12-19 15:35:09 +0000591static uint32_t
592boot_status_internal_off(int idx, int state, int elem_sz)
593{
594 int idx_sz;
595
596 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
597
David Vincze39e78552018-10-10 17:10:01 +0200598 return (idx - BOOT_STATUS_IDX_0) * idx_sz +
599 (state - BOOT_STATUS_STATE_0) * elem_sz;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000600}
601
602/**
603 * Reads the status of a partially-completed swap, if any. This is necessary
604 * to recover in case the boot lodaer was reset in the middle of a swap
605 * operation.
606 */
607static int
608boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs)
609{
610 uint32_t off;
611 uint8_t status;
612 int max_entries;
613 int found;
David Vincze39e78552018-10-10 17:10:01 +0200614 int found_idx;
615 int invalid;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000616 int rc;
617 int i;
618
619 off = boot_status_off(fap);
620 max_entries = boot_status_entries(fap);
621
622 found = 0;
David Vincze39e78552018-10-10 17:10:01 +0200623 found_idx = 0;
624 invalid = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000625 for (i = 0; i < max_entries; i++) {
David Vincze39e78552018-10-10 17:10:01 +0200626 rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(&boot_data),
627 &status, 1);
628 if (rc < 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000629 return BOOT_EFLASH;
630 }
631
David Vincze39e78552018-10-10 17:10:01 +0200632 if (rc == 1) {
633 if (found && !found_idx) {
634 found_idx = i;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000635 }
636 } else if (!found) {
637 found = 1;
David Vincze39e78552018-10-10 17:10:01 +0200638 } else if (found_idx) {
639 invalid = 1;
640 break;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000641 }
642 }
643
David Vincze39e78552018-10-10 17:10:01 +0200644 if (invalid) {
645 /* This means there was an error writing status on the last
646 * swap. Tell user and move on to validation!
647 */
648 BOOT_LOG_ERR("Detected inconsistent status!");
649
David Vincze8bdfc2d2019-03-18 15:49:23 +0100650#if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
651 /* With validation of the primary slot disabled, there is no way
652 * to be sure the swapped primary slot is OK, so abort!
David Vincze39e78552018-10-10 17:10:01 +0200653 */
654 assert(0);
655#endif
656 }
657
Tamas Banf70ef8c2017-12-19 15:35:09 +0000658 if (found) {
David Vincze39e78552018-10-10 17:10:01 +0200659 if (!found_idx) {
660 found_idx = i;
661 }
662 found_idx--;
663 bs->idx = (found_idx / BOOT_STATUS_STATE_COUNT) + 1;
664 bs->state = (found_idx % BOOT_STATUS_STATE_COUNT) + 1;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000665 }
666
667 return 0;
668}
669
670/**
671 * Reads the boot status from the flash. The boot status contains
672 * the current state of an interrupted image copy operation. If the boot
673 * status is not present, or it indicates that previous copy finished,
674 * there is no operation in progress.
675 */
676static int
677boot_read_status(struct boot_status *bs)
678{
679 const struct flash_area *fap;
David Vincze401c7422019-06-21 20:44:05 +0200680 uint32_t off;
David Vincze91b71ef2019-06-24 13:06:47 +0200681 uint8_t swap_info;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000682 int status_loc;
683 int area_id;
684 int rc;
685
David Vincze39e78552018-10-10 17:10:01 +0200686 memset(bs, 0, sizeof *bs);
687 bs->idx = BOOT_STATUS_IDX_0;
688 bs->state = BOOT_STATUS_STATE_0;
David Vincze401c7422019-06-21 20:44:05 +0200689 bs->swap_type = BOOT_SWAP_TYPE_NONE;
David Vincze39e78552018-10-10 17:10:01 +0200690
691#ifdef MCUBOOT_OVERWRITE_ONLY
692 /* Overwrite-only doesn't make use of the swap status area. */
693 return 0;
694#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +0000695
696 status_loc = boot_status_source();
697 switch (status_loc) {
698 case BOOT_STATUS_SOURCE_NONE:
699 return 0;
700
701 case BOOT_STATUS_SOURCE_SCRATCH:
702 area_id = FLASH_AREA_IMAGE_SCRATCH;
703 break;
704
David Vincze8bdfc2d2019-03-18 15:49:23 +0100705 case BOOT_STATUS_SOURCE_PRIMARY_SLOT:
706 area_id = FLASH_AREA_IMAGE_PRIMARY;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000707 break;
708
709 default:
710 assert(0);
711 return BOOT_EBADARGS;
712 }
713
714 rc = flash_area_open(area_id, &fap);
715 if (rc != 0) {
716 return BOOT_EFLASH;
717 }
718
719 rc = boot_read_status_bytes(fap, bs);
David Vincze401c7422019-06-21 20:44:05 +0200720 if (rc == 0) {
David Vincze91b71ef2019-06-24 13:06:47 +0200721 off = boot_swap_info_off(fap);
722 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
David Vincze401c7422019-06-21 20:44:05 +0200723 if (rc == 1) {
David Vincze91b71ef2019-06-24 13:06:47 +0200724 BOOT_SET_SWAP_INFO(swap_info, 0, BOOT_SWAP_TYPE_NONE);
David Vincze401c7422019-06-21 20:44:05 +0200725 rc = 0;
726 }
David Vincze91b71ef2019-06-24 13:06:47 +0200727
728 /* Extract the swap type info */
729 bs->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
David Vincze401c7422019-06-21 20:44:05 +0200730 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000731
732 flash_area_close(fap);
David Vincze39e78552018-10-10 17:10:01 +0200733
Tamas Banf70ef8c2017-12-19 15:35:09 +0000734 return rc;
735}
736
737/**
738 * Writes the supplied boot status to the flash file system. The boot status
739 * contains the current state of an in-progress image copy operation.
740 *
741 * @param bs The boot status to write.
742 *
743 * @return 0 on success; nonzero on failure.
744 */
745int
746boot_write_status(struct boot_status *bs)
747{
Tamas Ban581034a2017-12-19 19:54:37 +0000748 const struct flash_area *fap = NULL;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000749 uint32_t off;
750 int area_id;
751 int rc;
752 uint8_t buf[BOOT_MAX_ALIGN];
753 uint8_t align;
David Vincze39e78552018-10-10 17:10:01 +0200754 uint8_t erased_val;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000755
756 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze8bdfc2d2019-03-18 15:49:23 +0100757 * the trailer. Since in the last step the primary slot is erased, the
758 * first two status writes go to the scratch which will be copied to
759 * the primary slot!
Tamas Banf70ef8c2017-12-19 15:35:09 +0000760 */
761
762 if (bs->use_scratch) {
763 /* Write to scratch. */
764 area_id = FLASH_AREA_IMAGE_SCRATCH;
765 } else {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100766 /* Write to the primary slot. */
767 area_id = FLASH_AREA_IMAGE_PRIMARY;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000768 }
769
770 rc = flash_area_open(area_id, &fap);
771 if (rc != 0) {
772 rc = BOOT_EFLASH;
773 goto done;
774 }
775
776 off = boot_status_off(fap) +
777 boot_status_internal_off(bs->idx, bs->state,
778 BOOT_WRITE_SZ(&boot_data));
779
Tamas Banc3828852018-02-01 12:24:16 +0000780 align = flash_area_align(fap);
David Vincze39e78552018-10-10 17:10:01 +0200781 erased_val = flash_area_erased_val(fap);
782 memset(buf, erased_val, BOOT_MAX_ALIGN);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000783 buf[0] = bs->state;
784
785 rc = flash_area_write(fap, off, buf, align);
786 if (rc != 0) {
787 rc = BOOT_EFLASH;
788 goto done;
789 }
790
791 rc = 0;
792
793done:
794 flash_area_close(fap);
795 return rc;
796}
797
Tamas Banf70ef8c2017-12-19 15:35:09 +0000798/**
799 * Determines which swap operation to perform, if any. If it is determined
David Vincze8bdfc2d2019-03-18 15:49:23 +0100800 * that a swap operation is required, the image in the secondary slot is checked
801 * for validity. If the image in the secondary slot is invalid, it is erased,
802 * and a swap type of "none" is indicated.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000803 *
804 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
805 */
806static int
David Vincze401c7422019-06-21 20:44:05 +0200807boot_validated_swap_type(struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000808{
809 int swap_type;
810
811 swap_type = boot_swap_type();
812 switch (swap_type) {
813 case BOOT_SWAP_TYPE_TEST:
814 case BOOT_SWAP_TYPE_PERM:
815 case BOOT_SWAP_TYPE_REVERT:
David Vincze8bdfc2d2019-03-18 15:49:23 +0100816 /* Boot loader wants to switch to the secondary slot.
817 * Ensure image is valid.
818 */
David Vincze401c7422019-06-21 20:44:05 +0200819 if (boot_validate_slot(BOOT_SECONDARY_SLOT, bs) != 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000820 swap_type = BOOT_SWAP_TYPE_FAIL;
821 }
822 }
823
824 return swap_type;
825}
826
827/**
828 * Calculates the number of sectors the scratch area can contain. A "last"
829 * source sector is specified because images are copied backwards in flash
830 * (final index to index number 0).
831 *
832 * @param last_sector_idx The index of the last source sector
833 * (inclusive).
834 * @param out_first_sector_idx The index of the first source sector
835 * (inclusive) gets written here.
836 *
837 * @return The number of bytes comprised by the
838 * [first-sector, last-sector] range.
839 */
840#ifndef MCUBOOT_OVERWRITE_ONLY
841static uint32_t
842boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
843{
844 size_t scratch_sz;
845 uint32_t new_sz;
846 uint32_t sz;
847 int i;
848
849 sz = 0;
850
851 scratch_sz = boot_scratch_area_size(&boot_data);
852 for (i = last_sector_idx; i >= 0; i--) {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100853 new_sz = sz + boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
David Vincze401c7422019-06-21 20:44:05 +0200854 /*
855 * The secondary slot is not being checked here, because
856 * `boot_slots_compatible` already provides assurance that the copy size
857 * will be compatible with the primary slot and scratch.
858 */
Tamas Banf70ef8c2017-12-19 15:35:09 +0000859 if (new_sz > scratch_sz) {
860 break;
861 }
862 sz = new_sz;
863 }
864
865 /* i currently refers to a sector that doesn't fit or it is -1 because all
866 * sectors have been processed. In both cases, exclude sector i.
867 */
868 *out_first_sector_idx = i + 1;
869 return sz;
870}
871#endif /* !MCUBOOT_OVERWRITE_ONLY */
872
873/**
David Vinczef7641fa2018-09-04 18:29:46 +0200874 * Erases a region of flash.
875 *
David Vincze401c7422019-06-21 20:44:05 +0200876 * @param flash_area The flash_area containing the region to erase.
David Vinczef7641fa2018-09-04 18:29:46 +0200877 * @param off The offset within the flash area to start the
878 * erase.
879 * @param sz The number of bytes to erase.
880 *
881 * @return 0 on success; nonzero on failure.
882 */
David Vincze401c7422019-06-21 20:44:05 +0200883static inline int
884boot_erase_sector(const struct flash_area *fap, uint32_t off, uint32_t sz)
David Vinczef7641fa2018-09-04 18:29:46 +0200885{
David Vincze401c7422019-06-21 20:44:05 +0200886 return flash_area_erase(fap, off, sz);
David Vinczef7641fa2018-09-04 18:29:46 +0200887}
888
889/**
Tamas Banf70ef8c2017-12-19 15:35:09 +0000890 * Copies the contents of one flash region to another. You must erase the
891 * destination region prior to calling this function.
892 *
893 * @param flash_area_id_src The ID of the source flash area.
894 * @param flash_area_id_dst The ID of the destination flash area.
895 * @param off_src The offset within the source flash area to
896 * copy from.
897 * @param off_dst The offset within the destination flash area to
898 * copy to.
899 * @param sz The number of bytes to copy.
900 *
901 * @return 0 on success; nonzero on failure.
902 */
903static int
David Vincze401c7422019-06-21 20:44:05 +0200904boot_copy_sector(const struct flash_area *fap_src,
905 const struct flash_area *fap_dst,
Tamas Banf70ef8c2017-12-19 15:35:09 +0000906 uint32_t off_src, uint32_t off_dst, uint32_t sz)
907{
Tamas Banf70ef8c2017-12-19 15:35:09 +0000908 uint32_t bytes_copied;
909 int chunk_sz;
910 int rc;
911
912 static uint8_t buf[1024];
913
Tamas Banf70ef8c2017-12-19 15:35:09 +0000914 bytes_copied = 0;
915 while (bytes_copied < sz) {
Tamas Ban581034a2017-12-19 19:54:37 +0000916 if (sz - bytes_copied > sizeof(buf)) {
917 chunk_sz = sizeof(buf);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000918 } else {
919 chunk_sz = sz - bytes_copied;
920 }
921
922 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
923 if (rc != 0) {
David Vincze401c7422019-06-21 20:44:05 +0200924 return BOOT_EFLASH;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000925 }
926
927 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
928 if (rc != 0) {
David Vincze401c7422019-06-21 20:44:05 +0200929 return BOOT_EFLASH;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000930 }
931
932 bytes_copied += chunk_sz;
933 }
934
David Vincze401c7422019-06-21 20:44:05 +0200935 return 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000936}
937
938#ifndef MCUBOOT_OVERWRITE_ONLY
939static inline int
David Vincze401c7422019-06-21 20:44:05 +0200940boot_status_init(const struct flash_area *fap, const struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000941{
Tamas Banf70ef8c2017-12-19 15:35:09 +0000942 struct boot_swap_state swap_state;
943 int rc;
944
David Vincze401c7422019-06-21 20:44:05 +0200945 BOOT_LOG_DBG("initializing status; fa_id=%d", fap->fa_id);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000946
David Vincze8bdfc2d2019-03-18 15:49:23 +0100947 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY, &swap_state);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000948 assert(rc == 0);
949
David Vincze401c7422019-06-21 20:44:05 +0200950 if (bs->swap_type != BOOT_SWAP_TYPE_NONE) {
David Vincze91b71ef2019-06-24 13:06:47 +0200951 rc = boot_write_swap_info(fap,
952 bs->swap_type,
953 0);
David Vincze401c7422019-06-21 20:44:05 +0200954 assert(rc == 0);
955 }
956
Tamas Banf70ef8c2017-12-19 15:35:09 +0000957 if (swap_state.image_ok == BOOT_FLAG_SET) {
958 rc = boot_write_image_ok(fap);
959 assert(rc == 0);
960 }
961
962 rc = boot_write_swap_size(fap, bs->swap_size);
963 assert(rc == 0);
964
965 rc = boot_write_magic(fap);
966 assert(rc == 0);
967
Tamas Banf70ef8c2017-12-19 15:35:09 +0000968 return 0;
969}
David Vinczef7641fa2018-09-04 18:29:46 +0200970
971static int
David Vincze401c7422019-06-21 20:44:05 +0200972boot_erase_trailer_sectors(const struct flash_area *fap)
David Vinczef7641fa2018-09-04 18:29:46 +0200973{
974 uint8_t slot;
David Vincze401c7422019-06-21 20:44:05 +0200975 uint32_t sector;
976 uint32_t trailer_sz;
977 uint32_t total_sz;
978 uint32_t off;
979 uint32_t sz;
David Vinczef7641fa2018-09-04 18:29:46 +0200980 int rc;
981
David Vincze401c7422019-06-21 20:44:05 +0200982 BOOT_LOG_DBG("erasing trailer; fa_id=%d", fap->fa_id);
983
984 switch (fap->fa_id) {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100985 case FLASH_AREA_IMAGE_PRIMARY:
986 slot = BOOT_PRIMARY_SLOT;
David Vinczef7641fa2018-09-04 18:29:46 +0200987 break;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100988 case FLASH_AREA_IMAGE_SECONDARY:
989 slot = BOOT_SECONDARY_SLOT;
David Vinczef7641fa2018-09-04 18:29:46 +0200990 break;
991 default:
992 return BOOT_EFLASH;
993 }
994
David Vincze401c7422019-06-21 20:44:05 +0200995 /* delete starting from last sector and moving to beginning */
996 sector = boot_img_num_sectors(&boot_data, slot) - 1;
997 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(&boot_data));
998 total_sz = 0;
999 do {
1000 sz = boot_img_sector_size(&boot_data, slot, sector);
1001 off = boot_img_sector_off(&boot_data, slot, sector);
1002 rc = boot_erase_sector(fap, off, sz);
1003 assert(rc == 0);
1004
1005 sector--;
1006 total_sz += sz;
1007 } while (total_sz < trailer_sz);
David Vinczef7641fa2018-09-04 18:29:46 +02001008
1009 return rc;
1010}
1011#endif /* !MCUBOOT_OVERWRITE_ONLY */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001012
Tamas Banf70ef8c2017-12-19 15:35:09 +00001013/**
1014 * Swaps the contents of two flash regions within the two image slots.
1015 *
1016 * @param idx The index of the first sector in the range of
1017 * sectors being swapped.
1018 * @param sz The number of bytes to swap.
1019 * @param bs The current boot status. This struct gets
1020 * updated according to the outcome.
1021 *
1022 * @return 0 on success; nonzero on failure.
1023 */
1024#ifndef MCUBOOT_OVERWRITE_ONLY
1025static void
1026boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
1027{
David Vincze401c7422019-06-21 20:44:05 +02001028 const struct flash_area *fap_primary_slot;
1029 const struct flash_area *fap_secondary_slot;
1030 const struct flash_area *fap_scratch;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001031 uint32_t copy_sz;
1032 uint32_t trailer_sz;
1033 uint32_t img_off;
1034 uint32_t scratch_trailer_off;
1035 struct boot_swap_state swap_state;
1036 size_t last_sector;
David Vincze401c7422019-06-21 20:44:05 +02001037 bool erase_scratch;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001038 int rc;
1039
1040 /* Calculate offset from start of image area. */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001041 img_off = boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT, idx);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001042
1043 copy_sz = sz;
David Vincze401c7422019-06-21 20:44:05 +02001044 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Tamas Banf70ef8c2017-12-19 15:35:09 +00001045
David Vincze401c7422019-06-21 20:44:05 +02001046 /* sz in this function is always sized on a multiple of the sector size.
1047 * The check against the start offset of the last sector
Tamas Banf70ef8c2017-12-19 15:35:09 +00001048 * is to determine if we're swapping the last sector. The last sector
1049 * needs special handling because it's where the trailer lives. If we're
1050 * copying it, we need to use scratch to write the trailer temporarily.
1051 *
1052 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
1053 * controls if special handling is needed (swapping last sector).
1054 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001055 last_sector = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT) - 1;
1056 if (img_off + sz > boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT,
1057 last_sector)) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001058 copy_sz -= trailer_sz;
1059 }
1060
David Vincze39e78552018-10-10 17:10:01 +02001061 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001062
David Vincze401c7422019-06-21 20:44:05 +02001063 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap_primary_slot);
1064 assert (rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001065
David Vincze401c7422019-06-21 20:44:05 +02001066 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap_secondary_slot);
1067 assert (rc == 0);
1068
1069 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch);
1070 assert (rc == 0);
1071
1072 if (bs->state == BOOT_STATUS_STATE_0) {
1073 BOOT_LOG_DBG("erasing scratch area");
1074 rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001075 assert(rc == 0);
1076
David Vincze39e78552018-10-10 17:10:01 +02001077 if (bs->idx == BOOT_STATUS_IDX_0) {
David Vincze401c7422019-06-21 20:44:05 +02001078 /* Write a trailer to the scratch area, even if we don't need the
1079 * scratch area for status. We need a temporary place to store the
1080 * `swap-type` while we erase the primary trailer.
1081 */
1082 rc = boot_status_init(fap_scratch, bs);
1083 assert(rc == 0);
1084
1085 if (!bs->use_scratch) {
1086 /* Prepare the primary status area... here it is known that the
1087 * last sector is not being used by the image data so it's safe
1088 * to erase.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001089 */
David Vincze401c7422019-06-21 20:44:05 +02001090 rc = boot_erase_trailer_sectors(fap_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001091 assert(rc == 0);
1092
David Vincze401c7422019-06-21 20:44:05 +02001093 rc = boot_status_init(fap_primary_slot, bs);
1094 assert(rc == 0);
1095
1096 /* Erase the temporary trailer from the scratch area. */
1097 rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size);
1098 assert(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001099 }
1100 }
1101
David Vincze401c7422019-06-21 20:44:05 +02001102 rc = boot_copy_sector(fap_secondary_slot, fap_scratch,
1103 img_off, 0, copy_sz);
1104 assert(rc == 0);
1105
David Vincze39e78552018-10-10 17:10:01 +02001106 bs->state = BOOT_STATUS_STATE_1;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001107 rc = boot_write_status(bs);
David Vincze39e78552018-10-10 17:10:01 +02001108 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001109 }
1110
David Vincze39e78552018-10-10 17:10:01 +02001111 if (bs->state == BOOT_STATUS_STATE_1) {
David Vincze401c7422019-06-21 20:44:05 +02001112 rc = boot_erase_sector(fap_secondary_slot, img_off, sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001113 assert(rc == 0);
1114
David Vincze401c7422019-06-21 20:44:05 +02001115 rc = boot_copy_sector(fap_primary_slot, fap_secondary_slot,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001116 img_off, img_off, copy_sz);
1117 assert(rc == 0);
1118
David Vincze39e78552018-10-10 17:10:01 +02001119 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001120 /* If not all sectors of the slot are being swapped,
David Vincze8bdfc2d2019-03-18 15:49:23 +01001121 * guarantee here that only the primary slot will have the state.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001122 */
David Vincze401c7422019-06-21 20:44:05 +02001123 rc = boot_erase_trailer_sectors(fap_secondary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001124 assert(rc == 0);
1125 }
1126
David Vincze39e78552018-10-10 17:10:01 +02001127 bs->state = BOOT_STATUS_STATE_2;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001128 rc = boot_write_status(bs);
David Vincze39e78552018-10-10 17:10:01 +02001129 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001130 }
1131
David Vincze39e78552018-10-10 17:10:01 +02001132 if (bs->state == BOOT_STATUS_STATE_2) {
David Vincze401c7422019-06-21 20:44:05 +02001133 rc = boot_erase_sector(fap_primary_slot, img_off, sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001134 assert(rc == 0);
1135
David Vincze401c7422019-06-21 20:44:05 +02001136 /* NOTE: If this is the final sector, we exclude the image trailer from
1137 * this copy (copy_sz was truncated earlier).
1138 */
1139 rc = boot_copy_sector(fap_scratch, fap_primary_slot,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001140 0, img_off, copy_sz);
1141 assert(rc == 0);
1142
1143 if (bs->use_scratch) {
David Vincze401c7422019-06-21 20:44:05 +02001144 scratch_trailer_off = boot_status_off(fap_scratch);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001145
1146 /* copy current status that is being maintained in scratch */
David Vincze401c7422019-06-21 20:44:05 +02001147 rc = boot_copy_sector(fap_scratch, fap_primary_slot,
1148 scratch_trailer_off, img_off + copy_sz,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001149 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data));
David Vincze39e78552018-10-10 17:10:01 +02001150 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001151
1152 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1153 &swap_state);
1154 assert(rc == 0);
1155
1156 if (swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze401c7422019-06-21 20:44:05 +02001157 rc = boot_write_image_ok(fap_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001158 assert(rc == 0);
1159 }
1160
David Vincze401c7422019-06-21 20:44:05 +02001161 if (swap_state.swap_type != BOOT_SWAP_TYPE_NONE) {
David Vincze91b71ef2019-06-24 13:06:47 +02001162 rc = boot_write_swap_info(fap_primary_slot,
1163 swap_state.swap_type,
1164 0);
David Vincze401c7422019-06-21 20:44:05 +02001165 assert(rc == 0);
1166 }
1167
1168 rc = boot_write_swap_size(fap_primary_slot, bs->swap_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001169 assert(rc == 0);
1170
David Vincze401c7422019-06-21 20:44:05 +02001171 rc = boot_write_magic(fap_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001172 assert(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001173 }
1174
David Vincze401c7422019-06-21 20:44:05 +02001175 /* If we wrote a trailer to the scratch area, erase it after we persist
1176 * a trailer to the primary slot. We do this to prevent mcuboot from
1177 * reading a stale status from the scratch area in case of immediate
1178 * reset.
1179 */
1180 erase_scratch = bs->use_scratch;
1181 bs->use_scratch = 0;
1182
Tamas Banf70ef8c2017-12-19 15:35:09 +00001183 bs->idx++;
David Vincze39e78552018-10-10 17:10:01 +02001184 bs->state = BOOT_STATUS_STATE_0;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001185 rc = boot_write_status(bs);
David Vincze39e78552018-10-10 17:10:01 +02001186 BOOT_STATUS_ASSERT(rc == 0);
David Vincze401c7422019-06-21 20:44:05 +02001187
1188 if (erase_scratch) {
1189 rc = boot_erase_sector(fap_scratch, 0, sz);
1190 assert(rc == 0);
1191 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001192 }
David Vincze401c7422019-06-21 20:44:05 +02001193
1194 flash_area_close(fap_primary_slot);
1195 flash_area_close(fap_secondary_slot);
1196 flash_area_close(fap_scratch);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001197}
1198#endif /* !MCUBOOT_OVERWRITE_ONLY */
1199
1200/**
David Vincze401c7422019-06-21 20:44:05 +02001201 * Overwrite primary slot with the image contained in the secondary slot.
1202 * If a prior copy operation was interrupted by a system reset, this function
1203 * redos the copy.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001204 *
1205 * @param bs The current boot status. This function reads
1206 * this struct to determine if it is resuming
1207 * an interrupted swap operation. This
1208 * function writes the updated status to this
1209 * function on return.
1210 *
1211 * @return 0 on success; nonzero on failure.
1212 */
1213#ifdef MCUBOOT_OVERWRITE_ONLY
1214static int
1215boot_copy_image(struct boot_status *bs)
1216{
1217 size_t sect_count;
1218 size_t sect;
1219 int rc;
1220 size_t size = 0;
1221 size_t this_size;
David Vincze39e78552018-10-10 17:10:01 +02001222 size_t last_sector;
David Vincze401c7422019-06-21 20:44:05 +02001223 const struct flash_area *fap_primary_slot;
1224 const struct flash_area *fap_secondary_slot;
David Vincze39e78552018-10-10 17:10:01 +02001225
1226 (void)bs;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001227
David Vincze8bdfc2d2019-03-18 15:49:23 +01001228 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1229 BOOT_LOG_INF("Erasing the primary slot");
Tamas Banf70ef8c2017-12-19 15:35:09 +00001230
David Vincze401c7422019-06-21 20:44:05 +02001231 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap_primary_slot);
1232 assert (rc == 0);
1233
1234 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap_secondary_slot);
1235 assert (rc == 0);
1236
David Vincze8bdfc2d2019-03-18 15:49:23 +01001237 sect_count = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001238 for (sect = 0; sect < sect_count; sect++) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001239 this_size = boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, sect);
David Vincze401c7422019-06-21 20:44:05 +02001240 rc = boot_erase_sector(fap_primary_slot, size, this_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001241 assert(rc == 0);
1242
1243 size += this_size;
1244 }
1245
David Vincze8bdfc2d2019-03-18 15:49:23 +01001246 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1247 size);
David Vincze401c7422019-06-21 20:44:05 +02001248 rc = boot_copy_sector(fap_secondary_slot, fap_primary_slot, 0, 0, size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001249
David Vincze060968d2019-05-23 01:13:14 +02001250 /* Update the stored security counter with the new image's security counter
David Vincze8bdfc2d2019-03-18 15:49:23 +01001251 * value. Both slots hold the new image at this point, but the secondary
1252 * slot's image header must be passed because the read image headers in the
1253 * boot_data structure have not been updated yet.
David Vincze060968d2019-05-23 01:13:14 +02001254 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001255 rc = boot_update_security_counter(BOOT_PRIMARY_SLOT,
1256 boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT));
David Vincze060968d2019-05-23 01:13:14 +02001257 if (rc != 0) {
1258 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
1259 return rc;
1260 }
1261
David Vincze39e78552018-10-10 17:10:01 +02001262 /*
1263 * Erases header and trailer. The trailer is erased because when a new
1264 * image is written without a trailer as is the case when using newt, the
1265 * trailer that was left might trigger a new upgrade.
1266 */
David Vincze401c7422019-06-21 20:44:05 +02001267 BOOT_LOG_DBG("erasing secondary header");
1268 rc = boot_erase_sector(fap_secondary_slot,
David Vincze8bdfc2d2019-03-18 15:49:23 +01001269 boot_img_sector_off(&boot_data,
1270 BOOT_SECONDARY_SLOT, 0),
1271 boot_img_sector_size(&boot_data,
1272 BOOT_SECONDARY_SLOT, 0));
Tamas Banf70ef8c2017-12-19 15:35:09 +00001273 assert(rc == 0);
David Vincze8bdfc2d2019-03-18 15:49:23 +01001274 last_sector = boot_img_num_sectors(&boot_data, BOOT_SECONDARY_SLOT) - 1;
David Vincze401c7422019-06-21 20:44:05 +02001275 BOOT_LOG_DBG("erasing secondary trailer");
1276 rc = boot_erase_sector(fap_secondary_slot,
David Vincze8bdfc2d2019-03-18 15:49:23 +01001277 boot_img_sector_off(&boot_data, BOOT_SECONDARY_SLOT,
1278 last_sector),
1279 boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT,
1280 last_sector));
David Vincze39e78552018-10-10 17:10:01 +02001281 assert(rc == 0);
1282
David Vincze401c7422019-06-21 20:44:05 +02001283 flash_area_close(fap_primary_slot);
1284 flash_area_close(fap_secondary_slot);
1285
David Vincze8bdfc2d2019-03-18 15:49:23 +01001286 /* TODO: Perhaps verify the primary slot's signature again? */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001287
1288 return 0;
1289}
1290#else
David Vincze401c7422019-06-21 20:44:05 +02001291/**
1292 * Swaps the two images in flash. If a prior copy operation was interrupted
1293 * by a system reset, this function completes that operation.
1294 *
1295 * @param bs The current boot status. This function reads
1296 * this struct to determine if it is resuming
1297 * an interrupted swap operation. This
1298 * function writes the updated status to this
1299 * function on return.
1300 *
1301 * @return 0 on success; nonzero on failure.
1302 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001303static int
David Vincze401c7422019-06-21 20:44:05 +02001304boot_swap_image(struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001305{
1306 uint32_t sz;
1307 int first_sector_idx;
1308 int last_sector_idx;
David Vincze401c7422019-06-21 20:44:05 +02001309 int last_idx_secondary_slot;
David Vincze39e78552018-10-10 17:10:01 +02001310 uint32_t swap_idx;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001311 struct image_header *hdr;
1312 uint32_t size;
1313 uint32_t copy_size;
David Vincze401c7422019-06-21 20:44:05 +02001314 uint32_t primary_slot_size;
1315 uint32_t secondary_slot_size;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001316 int rc;
1317
1318 /* FIXME: just do this if asked by user? */
1319
1320 size = copy_size = 0;
1321
David Vincze39e78552018-10-10 17:10:01 +02001322 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001323 /*
1324 * No swap ever happened, so need to find the largest image which
1325 * will be used to determine the amount of sectors to swap.
1326 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001327 hdr = boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001328 if (hdr->ih_magic == IMAGE_MAGIC) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001329 rc = boot_read_image_size(BOOT_PRIMARY_SLOT, hdr, &copy_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001330 assert(rc == 0);
1331 }
1332
David Vincze8bdfc2d2019-03-18 15:49:23 +01001333 hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001334 if (hdr->ih_magic == IMAGE_MAGIC) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001335 rc = boot_read_image_size(BOOT_SECONDARY_SLOT, hdr, &size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001336 assert(rc == 0);
1337 }
1338
1339 if (size > copy_size) {
1340 copy_size = size;
1341 }
1342
1343 bs->swap_size = copy_size;
1344 } else {
1345 /*
1346 * If a swap was under way, the swap_size should already be present
1347 * in the trailer...
1348 */
1349 rc = boot_read_swap_size(&bs->swap_size);
1350 assert(rc == 0);
1351
1352 copy_size = bs->swap_size;
1353 }
1354
David Vincze401c7422019-06-21 20:44:05 +02001355 primary_slot_size = 0;
1356 secondary_slot_size = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001357 last_sector_idx = 0;
David Vincze401c7422019-06-21 20:44:05 +02001358 last_idx_secondary_slot = 0;
1359
1360 /*
1361 * Knowing the size of the largest image between both slots, here we
1362 * find what is the last sector in the primary slot that needs swapping.
1363 * Since we already know that both slots are compatible, the secondary
1364 * slot's last sector is not really required after this check is finished.
1365 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001366 while (1) {
David Vincze401c7422019-06-21 20:44:05 +02001367 if ((primary_slot_size < copy_size) ||
1368 (primary_slot_size < secondary_slot_size)) {
1369 primary_slot_size += boot_img_sector_size(&boot_data,
1370 BOOT_PRIMARY_SLOT,
1371 last_sector_idx);
1372 }
1373 if ((secondary_slot_size < copy_size) ||
1374 (secondary_slot_size < primary_slot_size)) {
1375 secondary_slot_size += boot_img_sector_size(&boot_data,
1376 BOOT_SECONDARY_SLOT,
1377 last_idx_secondary_slot);
1378 }
1379 if (primary_slot_size >= copy_size &&
1380 secondary_slot_size >= copy_size &&
1381 primary_slot_size == secondary_slot_size) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001382 break;
1383 }
1384 last_sector_idx++;
David Vincze401c7422019-06-21 20:44:05 +02001385 last_idx_secondary_slot++;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001386 }
1387
1388 swap_idx = 0;
1389 while (last_sector_idx >= 0) {
1390 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
David Vincze39e78552018-10-10 17:10:01 +02001391 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001392 boot_swap_sectors(first_sector_idx, sz, bs);
1393 }
1394
1395 last_sector_idx = first_sector_idx - 1;
1396 swap_idx++;
1397 }
1398
David Vincze8bdfc2d2019-03-18 15:49:23 +01001399#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
David Vincze39e78552018-10-10 17:10:01 +02001400 if (boot_status_fails > 0) {
David Vincze401c7422019-06-21 20:44:05 +02001401 BOOT_LOG_WRN("%d status write fails performing the swap",
1402 boot_status_fails);
David Vincze39e78552018-10-10 17:10:01 +02001403 }
1404#endif
1405
Tamas Banf70ef8c2017-12-19 15:35:09 +00001406 return 0;
1407}
1408#endif
1409
1410/**
David Vincze8bdfc2d2019-03-18 15:49:23 +01001411 * Marks the image in the primary slot as fully copied.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001412 */
1413#ifndef MCUBOOT_OVERWRITE_ONLY
1414static int
1415boot_set_copy_done(void)
1416{
1417 const struct flash_area *fap;
1418 int rc;
1419
David Vincze8bdfc2d2019-03-18 15:49:23 +01001420 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001421 if (rc != 0) {
1422 return BOOT_EFLASH;
1423 }
1424
1425 rc = boot_write_copy_done(fap);
1426 flash_area_close(fap);
1427 return rc;
1428}
1429#endif /* !MCUBOOT_OVERWRITE_ONLY */
1430
1431/**
David Vincze8bdfc2d2019-03-18 15:49:23 +01001432 * Marks a reverted image in the primary slot as confirmed. This is necessary to
1433 * ensure the status bytes from the image revert operation don't get processed
1434 * on a subsequent boot.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001435 *
1436 * NOTE: image_ok is tested before writing because if there's a valid permanent
David Vincze8bdfc2d2019-03-18 15:49:23 +01001437 * image installed on the primary slot and the new image to be upgrade to has a
1438 * bad sig, image_ok would be overwritten.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001439 */
1440#ifndef MCUBOOT_OVERWRITE_ONLY
1441static int
1442boot_set_image_ok(void)
1443{
1444 const struct flash_area *fap;
1445 struct boot_swap_state state;
1446 int rc;
1447
David Vincze8bdfc2d2019-03-18 15:49:23 +01001448 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001449 if (rc != 0) {
1450 return BOOT_EFLASH;
1451 }
1452
1453 rc = boot_read_swap_state(fap, &state);
1454 if (rc != 0) {
1455 rc = BOOT_EFLASH;
1456 goto out;
1457 }
1458
1459 if (state.image_ok == BOOT_FLAG_UNSET) {
1460 rc = boot_write_image_ok(fap);
1461 }
1462
1463out:
1464 flash_area_close(fap);
1465 return rc;
1466}
1467#endif /* !MCUBOOT_OVERWRITE_ONLY */
1468
1469/**
1470 * Performs an image swap if one is required.
1471 *
1472 * @param out_swap_type On success, the type of swap performed gets
1473 * written here.
1474 *
1475 * @return 0 on success; nonzero on failure.
1476 */
1477static int
1478boot_swap_if_needed(int *out_swap_type)
1479{
1480 struct boot_status bs;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001481 int rc;
1482
1483 /* Determine if we rebooted in the middle of an image swap
1484 * operation.
1485 */
1486 rc = boot_read_status(&bs);
1487 assert(rc == 0);
1488 if (rc != 0) {
1489 return rc;
1490 }
1491
1492 /* If a partial swap was detected, complete it. */
David Vincze39e78552018-10-10 17:10:01 +02001493 if (bs.idx != BOOT_STATUS_IDX_0 || bs.state != BOOT_STATUS_STATE_0) {
David Vincze401c7422019-06-21 20:44:05 +02001494#ifdef MCUBOOT_OVERWRITE_ONLY
1495 /* Should never arrive here, overwrite-only mode has no swap state. */
1496 assert(0);
1497#else
1498 /* Determine the type of swap operation being resumed from the
1499 * `swap-type` trailer field.
1500 */
1501 rc = boot_swap_image(&bs);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001502 assert(rc == 0);
David Vincze401c7422019-06-21 20:44:05 +02001503#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +00001504
Tamas Banf70ef8c2017-12-19 15:35:09 +00001505 } else {
David Vincze401c7422019-06-21 20:44:05 +02001506 if (bs.swap_type == BOOT_SWAP_TYPE_NONE) {
1507 bs.swap_type = boot_validated_swap_type(&bs);
1508 } else if (boot_validate_slot(BOOT_SECONDARY_SLOT, &bs) != 0) {
1509 bs.swap_type = BOOT_SWAP_TYPE_FAIL;
1510 }
1511 switch (bs.swap_type) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001512 case BOOT_SWAP_TYPE_TEST:
1513 case BOOT_SWAP_TYPE_PERM:
1514 case BOOT_SWAP_TYPE_REVERT:
David Vincze401c7422019-06-21 20:44:05 +02001515#ifdef MCUBOOT_OVERWRITE_ONLY
Tamas Banf70ef8c2017-12-19 15:35:09 +00001516 rc = boot_copy_image(&bs);
David Vincze401c7422019-06-21 20:44:05 +02001517#else
1518 rc = boot_swap_image(&bs);
1519#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +00001520 assert(rc == 0);
1521 break;
1522 }
1523 }
1524
David Vincze401c7422019-06-21 20:44:05 +02001525 *out_swap_type = bs.swap_type;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001526 return 0;
1527}
1528
1529/**
1530 * Prepares the booting process. This function moves images around in flash as
1531 * appropriate, and tells you what address to boot from.
1532 *
1533 * @param rsp On success, indicates how booting should occur.
1534 *
1535 * @return 0 on success; nonzero on failure.
1536 */
1537int
1538boot_go(struct boot_rsp *rsp)
1539{
1540 int swap_type;
1541 size_t slot;
1542 int rc;
1543 int fa_id;
1544 bool reload_headers = false;
1545
1546 /* The array of slot sectors are defined here (as opposed to file scope) so
1547 * that they don't get allocated for non-boot-loader apps. This is
1548 * necessary because the gcc option "-fdata-sections" doesn't seem to have
1549 * any effect in older gcc versions (e.g., 4.8.4).
1550 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001551 static boot_sector_t primary_slot_sectors[BOOT_MAX_IMG_SECTORS];
1552 static boot_sector_t secondary_slot_sectors[BOOT_MAX_IMG_SECTORS];
David Vincze401c7422019-06-21 20:44:05 +02001553 static boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
David Vincze8bdfc2d2019-03-18 15:49:23 +01001554 boot_data.imgs[BOOT_PRIMARY_SLOT].sectors = primary_slot_sectors;
1555 boot_data.imgs[BOOT_SECONDARY_SLOT].sectors = secondary_slot_sectors;
David Vincze401c7422019-06-21 20:44:05 +02001556 boot_data.scratch.sectors = scratch_sectors;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001557
1558 /* Open boot_data image areas for the duration of this call. */
1559 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1560 fa_id = flash_area_id_from_image_slot(slot);
1561 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot));
1562 assert(rc == 0);
1563 }
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001564
Tamas Banf70ef8c2017-12-19 15:35:09 +00001565 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
1566 &BOOT_SCRATCH_AREA(&boot_data));
1567 assert(rc == 0);
1568
1569 /* Determine the sector layout of the image slots and scratch area. */
1570 rc = boot_read_sectors();
1571 if (rc != 0) {
David Vincze39e78552018-10-10 17:10:01 +02001572 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - too small?",
1573 BOOT_MAX_IMG_SECTORS);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001574 goto out;
1575 }
1576
1577 /* Attempt to read an image header from each slot. */
David Vincze39e78552018-10-10 17:10:01 +02001578 rc = boot_read_image_headers(false);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001579 if (rc != 0) {
1580 goto out;
1581 }
1582
1583 /* If the image slots aren't compatible, no swap is possible. Just boot
David Vincze8bdfc2d2019-03-18 15:49:23 +01001584 * into the primary slot.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001585 */
1586 if (boot_slots_compatible()) {
1587 rc = boot_swap_if_needed(&swap_type);
1588 assert(rc == 0);
1589 if (rc != 0) {
1590 goto out;
1591 }
1592
1593 /*
1594 * The following states need image_ok be explicitly set after the
1595 * swap was finished to avoid a new revert.
1596 */
Tamas Ban581034a2017-12-19 19:54:37 +00001597 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
David Vincze401c7422019-06-21 20:44:05 +02001598 swap_type == BOOT_SWAP_TYPE_FAIL ||
1599 swap_type == BOOT_SWAP_TYPE_PERM) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001600#ifndef MCUBOOT_OVERWRITE_ONLY
1601 rc = boot_set_image_ok();
1602 if (rc != 0) {
1603 swap_type = BOOT_SWAP_TYPE_PANIC;
1604 }
1605#endif /* !MCUBOOT_OVERWRITE_ONLY */
1606 }
1607 } else {
1608 swap_type = BOOT_SWAP_TYPE_NONE;
1609 }
1610
1611 switch (swap_type) {
1612 case BOOT_SWAP_TYPE_NONE:
David Vincze8bdfc2d2019-03-18 15:49:23 +01001613 slot = BOOT_PRIMARY_SLOT;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001614 break;
1615
1616 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1617 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
1618 case BOOT_SWAP_TYPE_REVERT:
David Vincze8bdfc2d2019-03-18 15:49:23 +01001619 slot = BOOT_SECONDARY_SLOT;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001620 reload_headers = true;
1621#ifndef MCUBOOT_OVERWRITE_ONLY
David Vincze060968d2019-05-23 01:13:14 +02001622 if (swap_type == BOOT_SWAP_TYPE_PERM) {
1623 /* Update the stored security counter with the new image's security
David Vincze8bdfc2d2019-03-18 15:49:23 +01001624 * counter value. The primary slot holds the new image at this
1625 * point, but the secondary slot's image header must be passed
1626 * because the read image headers in the boot_data structure have
1627 * not been updated yet.
David Vincze060968d2019-05-23 01:13:14 +02001628 *
1629 * In case of a permanent image swap mcuboot will never attempt to
1630 * revert the images on the next reboot. Therefore, the security
1631 * counter must be increased right after the image upgrade.
1632 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001633 rc = boot_update_security_counter(BOOT_PRIMARY_SLOT,
1634 boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT));
David Vincze060968d2019-05-23 01:13:14 +02001635 if (rc != 0) {
1636 BOOT_LOG_ERR("Security counter update failed after "
1637 "image upgrade.");
1638 goto out;
1639 }
1640 }
1641
Tamas Banf70ef8c2017-12-19 15:35:09 +00001642 rc = boot_set_copy_done();
1643 if (rc != 0) {
1644 swap_type = BOOT_SWAP_TYPE_PANIC;
1645 }
1646#endif /* !MCUBOOT_OVERWRITE_ONLY */
1647 break;
1648
1649 case BOOT_SWAP_TYPE_FAIL:
David Vincze8bdfc2d2019-03-18 15:49:23 +01001650 /* The image in the secondary slot was invalid and is now erased.
1651 * Ensure we don't try to boot into it again on the next reboot.
1652 * Do this by pretending we just reverted back to the primary slot.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001653 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001654 slot = BOOT_PRIMARY_SLOT;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001655 reload_headers = true;
1656 break;
1657
1658 default:
1659 swap_type = BOOT_SWAP_TYPE_PANIC;
1660 }
1661
1662 if (swap_type == BOOT_SWAP_TYPE_PANIC) {
1663 BOOT_LOG_ERR("panic!");
1664 assert(0);
1665
1666 /* Loop forever... */
David Vincze39e78552018-10-10 17:10:01 +02001667 while (1) {}
Tamas Banf70ef8c2017-12-19 15:35:09 +00001668 }
1669
Tamas Banf70ef8c2017-12-19 15:35:09 +00001670 if (reload_headers) {
David Vincze39e78552018-10-10 17:10:01 +02001671 rc = boot_read_image_headers(false);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001672 if (rc != 0) {
1673 goto out;
1674 }
1675 /* Since headers were reloaded, it can be assumed we just performed a
1676 * swap or overwrite. Now the header info that should be used to
David Vincze8bdfc2d2019-03-18 15:49:23 +01001677 * provide the data for the bootstrap, which previously was at the
1678 * secondary slot, was updated to the primary slot.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001679 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001680 slot = BOOT_PRIMARY_SLOT;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001681 }
1682
David Vincze8bdfc2d2019-03-18 15:49:23 +01001683#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
David Vincze401c7422019-06-21 20:44:05 +02001684 rc = boot_validate_slot(BOOT_PRIMARY_SLOT, NULL);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001685 if (rc != 0) {
1686 rc = BOOT_EBADIMAGE;
1687 goto out;
1688 }
David Vincze8bdfc2d2019-03-18 15:49:23 +01001689#else
1690 /* Even if we're not re-validating the primary slot, we could be booting
David Vincze39e78552018-10-10 17:10:01 +02001691 * onto an empty flash chip. At least do a basic sanity check that
1692 * the magic number on the image is OK.
1693 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001694 if (boot_data.imgs[BOOT_PRIMARY_SLOT].hdr.ih_magic != IMAGE_MAGIC) {
1695 BOOT_LOG_ERR("bad image magic 0x%lx",
1696 (unsigned long)boot_data.imgs[BOOT_PRIMARY_SLOT].hdr.ih_magic);
David Vincze39e78552018-10-10 17:10:01 +02001697 rc = BOOT_EBADIMAGE;
1698 goto out;
1699 }
David Vincze8bdfc2d2019-03-18 15:49:23 +01001700#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001701
David Vincze060968d2019-05-23 01:13:14 +02001702 /* Update the stored security counter with the active image's security
1703 * counter value. It will be updated only if the new security counter is
1704 * greater than the stored value.
1705 *
1706 * In case of a successful image swapping when the swap type is TEST the
1707 * security counter can be increased only after a reset, when the swap type
1708 * is NONE and the image has marked itself "OK" (the image_ok flag has been
1709 * set). This way a "revert" swap can be performed if it's necessary.
1710 */
1711 if (swap_type == BOOT_SWAP_TYPE_NONE) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001712 rc = boot_update_security_counter(BOOT_PRIMARY_SLOT,
1713 boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT));
David Vincze060968d2019-05-23 01:13:14 +02001714 if (rc != 0) {
1715 BOOT_LOG_ERR("Security counter update failed after image "
1716 "validation.");
1717 goto out;
1718 }
1719 }
1720
Tamas Banf70ef8c2017-12-19 15:35:09 +00001721 /* Always boot from the primary slot. */
David Vincze401c7422019-06-21 20:44:05 +02001722 rsp->br_flash_dev_id = boot_data.imgs[BOOT_PRIMARY_SLOT].area->fa_device_id;
David Vincze8bdfc2d2019-03-18 15:49:23 +01001723 rsp->br_image_off = boot_img_slot_off(&boot_data, BOOT_PRIMARY_SLOT);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001724 rsp->br_hdr = boot_img_hdr(&boot_data, slot);
1725
Tamas Ban0e8ab302019-01-17 11:45:31 +00001726 /* Save boot status to shared memory area */
1727 rc = boot_save_boot_status(SW_S_NS,
1728 rsp->br_hdr,
1729 BOOT_IMG_AREA(&boot_data, slot));
1730 if (rc) {
1731 BOOT_LOG_ERR("Failed to add data to shared area");
1732 }
1733
Tamas Banf70ef8c2017-12-19 15:35:09 +00001734 out:
1735 flash_area_close(BOOT_SCRATCH_AREA(&boot_data));
1736 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1737 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
1738 }
1739 return rc;
1740}
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001741
Oliver Swedef9982442018-08-24 18:37:44 +01001742#else /* MCUBOOT_NO_SWAP || MCUBOOT_RAM_LOADING */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001743
David Vinczedcba70b2019-05-28 12:02:52 +02001744#define BOOT_LOG_IMAGE_INFO(area, hdr, state) \
1745 BOOT_LOG_INF("Image %u: version=%u.%u.%u+%u, magic=%5s, image_ok=0x%x", \
1746 (area), \
1747 (hdr)->ih_ver.iv_major, \
1748 (hdr)->ih_ver.iv_minor, \
1749 (hdr)->ih_ver.iv_revision, \
1750 (hdr)->ih_ver.iv_build_num, \
1751 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
1752 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
1753 "bad"), \
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001754 (state)->image_ok)
1755
1756struct image_slot_version {
1757 uint64_t version;
1758 uint32_t slot_number;
1759};
1760
1761/**
1762 * Extract the version number from the image header. This function must be
1763 * ported if version number format has changed in the image header.
1764 *
1765 * @param hdr Pointer to an image header structure
1766 *
Oliver Swedef9982442018-08-24 18:37:44 +01001767 * @return Version number casted to uint64_t
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001768 */
1769static uint64_t
1770boot_get_version_number(struct image_header *hdr)
1771{
Oliver Swedef9982442018-08-24 18:37:44 +01001772 uint64_t version = 0;
1773 version |= (uint64_t)hdr->ih_ver.iv_major << (IMAGE_VER_MINOR_LENGTH
1774 + IMAGE_VER_REVISION_LENGTH
1775 + IMAGE_VER_BUILD_NUM_LENGTH);
1776 version |= (uint64_t)hdr->ih_ver.iv_minor << (IMAGE_VER_REVISION_LENGTH
1777 + IMAGE_VER_BUILD_NUM_LENGTH);
1778 version |= (uint64_t)hdr->ih_ver.iv_revision << IMAGE_VER_BUILD_NUM_LENGTH;
1779 version |= hdr->ih_ver.iv_build_num;
1780 return version;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001781}
1782
1783/**
1784 * Comparator function for `qsort` to compare version numbers. This function
1785 * must be ported if version number format has changed in the image header.
1786 *
1787 * @param ver1 Pointer to an array element which holds the version number
1788 * @param ver2 Pointer to another array element which holds the version
1789 * number
1790 *
1791 * @return if version1 > version2 -1
1792 * if version1 == version2 0
1793 * if version1 < version2 1
1794 */
1795static int
1796boot_compare_version_numbers(const void *ver1, const void *ver2)
1797{
1798 if (((struct image_slot_version *)ver1)->version <
1799 ((struct image_slot_version *)ver2)->version) {
1800 return 1;
1801 }
1802
1803 if (((struct image_slot_version *)ver1)->version ==
1804 ((struct image_slot_version *)ver2)->version) {
1805 return 0;
1806 }
1807
1808 return -1;
1809}
1810
1811/**
1812 * Sort the available images based on the version number and puts them in
1813 * a list.
1814 *
1815 * @param boot_sequence A pointer to an array, whose aim is to carry
1816 * the boot order of candidate images.
1817 * @param slot_cnt The number of flash areas, which can contains firmware
1818 * images.
1819 *
1820 * @return The number of valid images.
1821 */
1822uint32_t
1823boot_get_boot_sequence(uint32_t *boot_sequence, uint32_t slot_cnt)
1824{
1825 struct boot_swap_state slot_state;
1826 struct image_header *hdr;
1827 struct image_slot_version image_versions[BOOT_NUM_SLOTS] = {{0}};
1828 uint32_t image_cnt = 0;
1829 uint32_t slot;
1830 int32_t rc;
1831 int32_t fa_id;
1832
1833 for (slot = 0; slot < slot_cnt; slot++) {
1834 hdr = boot_img_hdr(&boot_data, slot);
1835 fa_id = flash_area_id_from_image_slot(slot);
1836 rc = boot_read_swap_state_by_id(fa_id, &slot_state);
1837 if (rc != 0) {
David Vinczedcba70b2019-05-28 12:02:52 +02001838 BOOT_LOG_ERR("Error during reading image trailer from slot: %u",
1839 slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001840 continue;
1841 }
1842
1843 if (hdr->ih_magic == IMAGE_MAGIC) {
1844 if (slot_state.magic == BOOT_MAGIC_GOOD ||
David Vincze39e78552018-10-10 17:10:01 +02001845 slot_state.image_ok == BOOT_FLAG_SET) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001846 /* Valid cases:
1847 * - Test mode: magic is OK in image trailer
1848 * - Permanent mode: image_ok flag has previously set
1849 */
1850 image_versions[slot].slot_number = slot;
1851 image_versions[slot].version = boot_get_version_number(hdr);
1852 image_cnt++;
1853 }
1854
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001855 BOOT_LOG_IMAGE_INFO(slot, hdr, &slot_state);
1856 } else {
David Vinczedcba70b2019-05-28 12:02:52 +02001857 BOOT_LOG_INF("Image %u: No valid image", slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001858 }
1859 }
1860
1861 /* Sort the images based on version number */
1862 qsort(&image_versions[0],
1863 slot_cnt,
1864 sizeof(struct image_slot_version),
1865 boot_compare_version_numbers);
1866
1867 /* Copy the calculated boot sequence to boot_sequence array */
1868 for (slot = 0; slot < slot_cnt; slot++) {
1869 boot_sequence[slot] = image_versions[slot].slot_number;
1870 }
1871
1872 return image_cnt;
1873}
1874
Oliver Swedef9982442018-08-24 18:37:44 +01001875#ifdef MCUBOOT_RAM_LOADING
1876/**
1877 * Copies an image from a slot in the flash to an SRAM address, where the load
1878 * address has already been inserted into the image header by this point and is
1879 * extracted from it within this method. The copying is done sector-by-sector.
1880 *
1881 * @param slot The flash slot of the image to be copied to SRAM.
1882 *
1883 * @param hdr Pointer to the image header structure of the image
1884 * that needs to be copid to SRAM
1885 *
1886 * @return 0 on success; nonzero on failure.
1887 */
1888static int
1889boot_copy_image_to_sram(int slot, struct image_header *hdr)
1890{
1891 int rc;
1892 uint32_t sect_sz;
1893 uint32_t sect = 0;
1894 uint32_t bytes_copied = 0;
1895 const struct flash_area *fap_src = NULL;
1896 uint32_t dst = (uint32_t) hdr->ih_load_addr;
1897 uint32_t img_sz;
1898
1899 if (dst % 4 != 0) {
Tamas Banc27b5c32019-05-28 16:30:19 +01001900 BOOT_LOG_INF("Cannot copy the image to the SRAM address 0x%x "
Oliver Swedef9982442018-08-24 18:37:44 +01001901 "- the load address must be aligned with 4 bytes due to SRAM "
1902 "restrictions", dst);
1903 return BOOT_EBADARGS;
1904 }
1905
1906 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap_src);
1907 if (rc != 0) {
1908 return BOOT_EFLASH;
1909 }
1910
1911 rc = boot_read_image_size(slot, hdr, &img_sz);
1912 if (rc != 0) {
1913 return BOOT_EFLASH;
1914 }
1915
1916 while (bytes_copied < img_sz) {
1917 sect_sz = boot_img_sector_size(&boot_data, slot, sect);
1918 /*
1919 * Direct copy from where the image sector resides in flash to its new
1920 * location in SRAM
1921 */
1922 rc = flash_area_read(fap_src,
1923 bytes_copied,
1924 (void *)(dst + bytes_copied),
1925 sect_sz);
1926 if (rc != 0) {
1927 BOOT_LOG_INF("Error whilst copying image from Flash to SRAM");
1928 break;
1929 } else {
1930 bytes_copied += sect_sz;
1931 }
1932 sect++;
1933 }
1934
1935 if (fap_src) {
1936 flash_area_close(fap_src);
1937 }
1938 return rc;
1939}
1940#endif /* MCUBOOT_RAM_LOADING */
1941
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001942/**
1943 * Prepares the booting process. This function choose the newer image in flash
1944 * as appropriate, and returns the address to boot from.
1945 *
1946 * @param rsp On success, indicates how booting should occur.
1947 *
1948 * @return 0 on success; nonzero on failure.
1949 */
1950int
1951boot_go(struct boot_rsp *rsp)
1952{
1953 size_t slot = 0;
1954 int32_t i;
1955 int rc;
1956 int fa_id;
1957 uint32_t boot_sequence[BOOT_NUM_SLOTS];
1958 uint32_t img_cnt;
Oliver Swedef9982442018-08-24 18:37:44 +01001959 struct image_header *newest_image_header;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001960
David Vincze8bdfc2d2019-03-18 15:49:23 +01001961 static boot_sector_t primary_slot_sectors[BOOT_MAX_IMG_SECTORS];
1962 static boot_sector_t secondary_slot_sectors[BOOT_MAX_IMG_SECTORS];
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001963
David Vincze8bdfc2d2019-03-18 15:49:23 +01001964 boot_data.imgs[BOOT_PRIMARY_SLOT].sectors = &primary_slot_sectors[0];
1965 boot_data.imgs[BOOT_SECONDARY_SLOT].sectors = &secondary_slot_sectors[0];
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001966
1967 /* Open boot_data image areas for the duration of this call. */
1968 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
1969 fa_id = flash_area_id_from_image_slot(i);
1970 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, i));
1971 assert(rc == 0);
1972 }
1973
1974 /* Determine the sector layout of the image slots. */
1975 rc = boot_read_sectors();
1976 if (rc != 0) {
David Vincze39e78552018-10-10 17:10:01 +02001977 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - too small?",
1978 BOOT_MAX_IMG_SECTORS);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001979 goto out;
1980 }
1981
1982 /* Attempt to read an image header from each slot. */
David Vincze39e78552018-10-10 17:10:01 +02001983 rc = boot_read_image_headers(false);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001984 if (rc != 0) {
1985 goto out;
1986 }
1987
1988 img_cnt = boot_get_boot_sequence(boot_sequence, BOOT_NUM_SLOTS);
1989 if (img_cnt) {
1990 /* Authenticate images */
1991 for (i = 0; i < img_cnt; i++) {
David Vincze401c7422019-06-21 20:44:05 +02001992 rc = boot_validate_slot(boot_sequence[i], NULL);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001993 if (rc == 0) {
1994 slot = boot_sequence[i];
1995 break;
1996 }
1997 }
1998 if (rc) {
1999 /* If there was no valid image at all */
2000 rc = BOOT_EBADIMAGE;
2001 goto out;
2002 }
2003
Oliver Swedef9982442018-08-24 18:37:44 +01002004 /* The slot variable now refers to the newest image's slot in flash */
2005 newest_image_header = boot_img_hdr(&boot_data, slot);
2006
David Vincze060968d2019-05-23 01:13:14 +02002007 /* Update the security counter with the newest image's security
2008 * counter value.
2009 */
2010 rc = boot_update_security_counter(slot, newest_image_header);
2011 if (rc != 0) {
2012 BOOT_LOG_ERR("Security counter update failed after image "
2013 "validation.");
2014 goto out;
2015 }
2016
Oliver Swedef9982442018-08-24 18:37:44 +01002017 #ifdef MCUBOOT_RAM_LOADING
2018 if (newest_image_header->ih_flags & IMAGE_F_RAM_LOAD) {
2019 /* Copy image to the load address from where it
2020 * currently resides in flash */
2021 rc = boot_copy_image_to_sram(slot, newest_image_header);
2022 if (rc != 0) {
2023 rc = BOOT_EBADIMAGE;
David Vincze8bdfc2d2019-03-18 15:49:23 +01002024 BOOT_LOG_INF("Could not copy image from the %s slot in "
Tamas Banc27b5c32019-05-28 16:30:19 +01002025 "the Flash to load address 0x%x in SRAM, "
David Vincze8bdfc2d2019-03-18 15:49:23 +01002026 "aborting..", (slot == BOOT_PRIMARY_SLOT) ?
2027 "primary" : "secondary",
Oliver Swedef9982442018-08-24 18:37:44 +01002028 newest_image_header->ih_load_addr);
2029 goto out;
2030 } else {
David Vincze8bdfc2d2019-03-18 15:49:23 +01002031 BOOT_LOG_INF("Image has been copied from the %s slot in "
2032 "the flash to SRAM address 0x%x",
2033 (slot == BOOT_PRIMARY_SLOT) ?
2034 "primary" : "secondary",
Oliver Swedef9982442018-08-24 18:37:44 +01002035 newest_image_header->ih_load_addr);
2036 }
2037
2038 /* Validate the image hash in SRAM after the copy was successful */
2039 rc = bootutil_check_hash_after_loading(newest_image_header);
2040 if (rc != 0) {
2041 rc = BOOT_EBADIMAGE;
2042 BOOT_LOG_INF("Cannot validate the hash of the image that was "
2043 "copied to SRAM, aborting..");
2044 goto out;
2045 }
2046
Tamas Banc27b5c32019-05-28 16:30:19 +01002047 BOOT_LOG_INF("Booting image from SRAM at address 0x%x",
Oliver Swedef9982442018-08-24 18:37:44 +01002048 newest_image_header->ih_load_addr);
2049 } else {
David Vincze8a2a4e22019-05-24 10:14:23 +02002050#endif /* MCUBOOT_RAM_LOADING */
David Vincze8bdfc2d2019-03-18 15:49:23 +01002051 BOOT_LOG_INF("Booting image from the %s slot",
2052 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Vincze8a2a4e22019-05-24 10:14:23 +02002053#ifdef MCUBOOT_RAM_LOADING
Oliver Swedef9982442018-08-24 18:37:44 +01002054 }
David Vincze8a2a4e22019-05-24 10:14:23 +02002055#endif
Oliver Swedef9982442018-08-24 18:37:44 +01002056
2057 rsp->br_hdr = newest_image_header;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002058 rsp->br_image_off = boot_img_slot_off(&boot_data, slot);
David Vincze401c7422019-06-21 20:44:05 +02002059 rsp->br_flash_dev_id = boot_data.imgs[slot].area->fa_device_id;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002060 } else {
2061 /* No candidate image available */
2062 rc = BOOT_EBADIMAGE;
David Vincze060968d2019-05-23 01:13:14 +02002063 goto out;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002064 }
2065
Tamas Ban0e8ab302019-01-17 11:45:31 +00002066 /* Save boot status to shared memory area */
2067 rc = boot_save_boot_status(SW_S_NS,
2068 rsp->br_hdr,
2069 BOOT_IMG_AREA(&boot_data, slot));
2070 if (rc) {
2071 BOOT_LOG_ERR("Failed to add data to shared area");
2072 }
2073
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002074out:
2075 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2076 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
2077 }
2078 return rc;
2079}
Oliver Swedef9982442018-08-24 18:37:44 +01002080#endif /* MCUBOOT_NO_SWAP || MCUBOOT_RAM_LOADING */