blob: 3631018ee78873581d36206350106f2bdae7f8eb [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;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000681 int status_loc;
682 int area_id;
683 int rc;
684
David Vincze39e78552018-10-10 17:10:01 +0200685 memset(bs, 0, sizeof *bs);
686 bs->idx = BOOT_STATUS_IDX_0;
687 bs->state = BOOT_STATUS_STATE_0;
David Vincze401c7422019-06-21 20:44:05 +0200688 bs->swap_type = BOOT_SWAP_TYPE_NONE;
David Vincze39e78552018-10-10 17:10:01 +0200689
690#ifdef MCUBOOT_OVERWRITE_ONLY
691 /* Overwrite-only doesn't make use of the swap status area. */
692 return 0;
693#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +0000694
695 status_loc = boot_status_source();
696 switch (status_loc) {
697 case BOOT_STATUS_SOURCE_NONE:
698 return 0;
699
700 case BOOT_STATUS_SOURCE_SCRATCH:
701 area_id = FLASH_AREA_IMAGE_SCRATCH;
702 break;
703
David Vincze8bdfc2d2019-03-18 15:49:23 +0100704 case BOOT_STATUS_SOURCE_PRIMARY_SLOT:
705 area_id = FLASH_AREA_IMAGE_PRIMARY;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000706 break;
707
708 default:
709 assert(0);
710 return BOOT_EBADARGS;
711 }
712
713 rc = flash_area_open(area_id, &fap);
714 if (rc != 0) {
715 return BOOT_EFLASH;
716 }
717
718 rc = boot_read_status_bytes(fap, bs);
David Vincze401c7422019-06-21 20:44:05 +0200719 if (rc == 0) {
720 off = boot_swap_type_off(fap);
721 rc = flash_area_read_is_empty(fap, off, &bs->swap_type,
722 sizeof bs->swap_type);
723 if (rc == 1) {
724 bs->swap_type = BOOT_SWAP_TYPE_NONE;
725 rc = 0;
726 }
727 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000728
729 flash_area_close(fap);
David Vincze39e78552018-10-10 17:10:01 +0200730
Tamas Banf70ef8c2017-12-19 15:35:09 +0000731 return rc;
732}
733
734/**
735 * Writes the supplied boot status to the flash file system. The boot status
736 * contains the current state of an in-progress image copy operation.
737 *
738 * @param bs The boot status to write.
739 *
740 * @return 0 on success; nonzero on failure.
741 */
742int
743boot_write_status(struct boot_status *bs)
744{
Tamas Ban581034a2017-12-19 19:54:37 +0000745 const struct flash_area *fap = NULL;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000746 uint32_t off;
747 int area_id;
748 int rc;
749 uint8_t buf[BOOT_MAX_ALIGN];
750 uint8_t align;
David Vincze39e78552018-10-10 17:10:01 +0200751 uint8_t erased_val;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000752
753 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze8bdfc2d2019-03-18 15:49:23 +0100754 * the trailer. Since in the last step the primary slot is erased, the
755 * first two status writes go to the scratch which will be copied to
756 * the primary slot!
Tamas Banf70ef8c2017-12-19 15:35:09 +0000757 */
758
759 if (bs->use_scratch) {
760 /* Write to scratch. */
761 area_id = FLASH_AREA_IMAGE_SCRATCH;
762 } else {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100763 /* Write to the primary slot. */
764 area_id = FLASH_AREA_IMAGE_PRIMARY;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000765 }
766
767 rc = flash_area_open(area_id, &fap);
768 if (rc != 0) {
769 rc = BOOT_EFLASH;
770 goto done;
771 }
772
773 off = boot_status_off(fap) +
774 boot_status_internal_off(bs->idx, bs->state,
775 BOOT_WRITE_SZ(&boot_data));
776
Tamas Banc3828852018-02-01 12:24:16 +0000777 align = flash_area_align(fap);
David Vincze39e78552018-10-10 17:10:01 +0200778 erased_val = flash_area_erased_val(fap);
779 memset(buf, erased_val, BOOT_MAX_ALIGN);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000780 buf[0] = bs->state;
781
782 rc = flash_area_write(fap, off, buf, align);
783 if (rc != 0) {
784 rc = BOOT_EFLASH;
785 goto done;
786 }
787
788 rc = 0;
789
790done:
791 flash_area_close(fap);
792 return rc;
793}
794
Tamas Banf70ef8c2017-12-19 15:35:09 +0000795/**
796 * Determines which swap operation to perform, if any. If it is determined
David Vincze8bdfc2d2019-03-18 15:49:23 +0100797 * that a swap operation is required, the image in the secondary slot is checked
798 * for validity. If the image in the secondary slot is invalid, it is erased,
799 * and a swap type of "none" is indicated.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000800 *
801 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
802 */
803static int
David Vincze401c7422019-06-21 20:44:05 +0200804boot_validated_swap_type(struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000805{
806 int swap_type;
807
808 swap_type = boot_swap_type();
809 switch (swap_type) {
810 case BOOT_SWAP_TYPE_TEST:
811 case BOOT_SWAP_TYPE_PERM:
812 case BOOT_SWAP_TYPE_REVERT:
David Vincze8bdfc2d2019-03-18 15:49:23 +0100813 /* Boot loader wants to switch to the secondary slot.
814 * Ensure image is valid.
815 */
David Vincze401c7422019-06-21 20:44:05 +0200816 if (boot_validate_slot(BOOT_SECONDARY_SLOT, bs) != 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000817 swap_type = BOOT_SWAP_TYPE_FAIL;
818 }
819 }
820
821 return swap_type;
822}
823
824/**
825 * Calculates the number of sectors the scratch area can contain. A "last"
826 * source sector is specified because images are copied backwards in flash
827 * (final index to index number 0).
828 *
829 * @param last_sector_idx The index of the last source sector
830 * (inclusive).
831 * @param out_first_sector_idx The index of the first source sector
832 * (inclusive) gets written here.
833 *
834 * @return The number of bytes comprised by the
835 * [first-sector, last-sector] range.
836 */
837#ifndef MCUBOOT_OVERWRITE_ONLY
838static uint32_t
839boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
840{
841 size_t scratch_sz;
842 uint32_t new_sz;
843 uint32_t sz;
844 int i;
845
846 sz = 0;
847
848 scratch_sz = boot_scratch_area_size(&boot_data);
849 for (i = last_sector_idx; i >= 0; i--) {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100850 new_sz = sz + boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
David Vincze401c7422019-06-21 20:44:05 +0200851 /*
852 * The secondary slot is not being checked here, because
853 * `boot_slots_compatible` already provides assurance that the copy size
854 * will be compatible with the primary slot and scratch.
855 */
Tamas Banf70ef8c2017-12-19 15:35:09 +0000856 if (new_sz > scratch_sz) {
857 break;
858 }
859 sz = new_sz;
860 }
861
862 /* i currently refers to a sector that doesn't fit or it is -1 because all
863 * sectors have been processed. In both cases, exclude sector i.
864 */
865 *out_first_sector_idx = i + 1;
866 return sz;
867}
868#endif /* !MCUBOOT_OVERWRITE_ONLY */
869
870/**
David Vinczef7641fa2018-09-04 18:29:46 +0200871 * Erases a region of flash.
872 *
David Vincze401c7422019-06-21 20:44:05 +0200873 * @param flash_area The flash_area containing the region to erase.
David Vinczef7641fa2018-09-04 18:29:46 +0200874 * @param off The offset within the flash area to start the
875 * erase.
876 * @param sz The number of bytes to erase.
877 *
878 * @return 0 on success; nonzero on failure.
879 */
David Vincze401c7422019-06-21 20:44:05 +0200880static inline int
881boot_erase_sector(const struct flash_area *fap, uint32_t off, uint32_t sz)
David Vinczef7641fa2018-09-04 18:29:46 +0200882{
David Vincze401c7422019-06-21 20:44:05 +0200883 return flash_area_erase(fap, off, sz);
David Vinczef7641fa2018-09-04 18:29:46 +0200884}
885
886/**
Tamas Banf70ef8c2017-12-19 15:35:09 +0000887 * Copies the contents of one flash region to another. You must erase the
888 * destination region prior to calling this function.
889 *
890 * @param flash_area_id_src The ID of the source flash area.
891 * @param flash_area_id_dst The ID of the destination flash area.
892 * @param off_src The offset within the source flash area to
893 * copy from.
894 * @param off_dst The offset within the destination flash area to
895 * copy to.
896 * @param sz The number of bytes to copy.
897 *
898 * @return 0 on success; nonzero on failure.
899 */
900static int
David Vincze401c7422019-06-21 20:44:05 +0200901boot_copy_sector(const struct flash_area *fap_src,
902 const struct flash_area *fap_dst,
Tamas Banf70ef8c2017-12-19 15:35:09 +0000903 uint32_t off_src, uint32_t off_dst, uint32_t sz)
904{
Tamas Banf70ef8c2017-12-19 15:35:09 +0000905 uint32_t bytes_copied;
906 int chunk_sz;
907 int rc;
908
909 static uint8_t buf[1024];
910
Tamas Banf70ef8c2017-12-19 15:35:09 +0000911 bytes_copied = 0;
912 while (bytes_copied < sz) {
Tamas Ban581034a2017-12-19 19:54:37 +0000913 if (sz - bytes_copied > sizeof(buf)) {
914 chunk_sz = sizeof(buf);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000915 } else {
916 chunk_sz = sz - bytes_copied;
917 }
918
919 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
920 if (rc != 0) {
David Vincze401c7422019-06-21 20:44:05 +0200921 return BOOT_EFLASH;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000922 }
923
924 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
925 if (rc != 0) {
David Vincze401c7422019-06-21 20:44:05 +0200926 return BOOT_EFLASH;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000927 }
928
929 bytes_copied += chunk_sz;
930 }
931
David Vincze401c7422019-06-21 20:44:05 +0200932 return 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000933}
934
935#ifndef MCUBOOT_OVERWRITE_ONLY
936static inline int
David Vincze401c7422019-06-21 20:44:05 +0200937boot_status_init(const struct flash_area *fap, const struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000938{
Tamas Banf70ef8c2017-12-19 15:35:09 +0000939 struct boot_swap_state swap_state;
940 int rc;
941
David Vincze401c7422019-06-21 20:44:05 +0200942 BOOT_LOG_DBG("initializing status; fa_id=%d", fap->fa_id);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000943
David Vincze8bdfc2d2019-03-18 15:49:23 +0100944 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY, &swap_state);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000945 assert(rc == 0);
946
David Vincze401c7422019-06-21 20:44:05 +0200947 if (bs->swap_type != BOOT_SWAP_TYPE_NONE) {
948 rc = boot_write_swap_type(fap, bs->swap_type);
949 assert(rc == 0);
950 }
951
Tamas Banf70ef8c2017-12-19 15:35:09 +0000952 if (swap_state.image_ok == BOOT_FLAG_SET) {
953 rc = boot_write_image_ok(fap);
954 assert(rc == 0);
955 }
956
957 rc = boot_write_swap_size(fap, bs->swap_size);
958 assert(rc == 0);
959
960 rc = boot_write_magic(fap);
961 assert(rc == 0);
962
Tamas Banf70ef8c2017-12-19 15:35:09 +0000963 return 0;
964}
David Vinczef7641fa2018-09-04 18:29:46 +0200965
966static int
David Vincze401c7422019-06-21 20:44:05 +0200967boot_erase_trailer_sectors(const struct flash_area *fap)
David Vinczef7641fa2018-09-04 18:29:46 +0200968{
969 uint8_t slot;
David Vincze401c7422019-06-21 20:44:05 +0200970 uint32_t sector;
971 uint32_t trailer_sz;
972 uint32_t total_sz;
973 uint32_t off;
974 uint32_t sz;
David Vinczef7641fa2018-09-04 18:29:46 +0200975 int rc;
976
David Vincze401c7422019-06-21 20:44:05 +0200977 BOOT_LOG_DBG("erasing trailer; fa_id=%d", fap->fa_id);
978
979 switch (fap->fa_id) {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100980 case FLASH_AREA_IMAGE_PRIMARY:
981 slot = BOOT_PRIMARY_SLOT;
David Vinczef7641fa2018-09-04 18:29:46 +0200982 break;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100983 case FLASH_AREA_IMAGE_SECONDARY:
984 slot = BOOT_SECONDARY_SLOT;
David Vinczef7641fa2018-09-04 18:29:46 +0200985 break;
986 default:
987 return BOOT_EFLASH;
988 }
989
David Vincze401c7422019-06-21 20:44:05 +0200990 /* delete starting from last sector and moving to beginning */
991 sector = boot_img_num_sectors(&boot_data, slot) - 1;
992 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(&boot_data));
993 total_sz = 0;
994 do {
995 sz = boot_img_sector_size(&boot_data, slot, sector);
996 off = boot_img_sector_off(&boot_data, slot, sector);
997 rc = boot_erase_sector(fap, off, sz);
998 assert(rc == 0);
999
1000 sector--;
1001 total_sz += sz;
1002 } while (total_sz < trailer_sz);
David Vinczef7641fa2018-09-04 18:29:46 +02001003
1004 return rc;
1005}
1006#endif /* !MCUBOOT_OVERWRITE_ONLY */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001007
Tamas Banf70ef8c2017-12-19 15:35:09 +00001008/**
1009 * Swaps the contents of two flash regions within the two image slots.
1010 *
1011 * @param idx The index of the first sector in the range of
1012 * sectors being swapped.
1013 * @param sz The number of bytes to swap.
1014 * @param bs The current boot status. This struct gets
1015 * updated according to the outcome.
1016 *
1017 * @return 0 on success; nonzero on failure.
1018 */
1019#ifndef MCUBOOT_OVERWRITE_ONLY
1020static void
1021boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
1022{
David Vincze401c7422019-06-21 20:44:05 +02001023 const struct flash_area *fap_primary_slot;
1024 const struct flash_area *fap_secondary_slot;
1025 const struct flash_area *fap_scratch;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001026 uint32_t copy_sz;
1027 uint32_t trailer_sz;
1028 uint32_t img_off;
1029 uint32_t scratch_trailer_off;
1030 struct boot_swap_state swap_state;
1031 size_t last_sector;
David Vincze401c7422019-06-21 20:44:05 +02001032 bool erase_scratch;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001033 int rc;
1034
1035 /* Calculate offset from start of image area. */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001036 img_off = boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT, idx);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001037
1038 copy_sz = sz;
David Vincze401c7422019-06-21 20:44:05 +02001039 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Tamas Banf70ef8c2017-12-19 15:35:09 +00001040
David Vincze401c7422019-06-21 20:44:05 +02001041 /* sz in this function is always sized on a multiple of the sector size.
1042 * The check against the start offset of the last sector
Tamas Banf70ef8c2017-12-19 15:35:09 +00001043 * is to determine if we're swapping the last sector. The last sector
1044 * needs special handling because it's where the trailer lives. If we're
1045 * copying it, we need to use scratch to write the trailer temporarily.
1046 *
1047 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
1048 * controls if special handling is needed (swapping last sector).
1049 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001050 last_sector = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT) - 1;
1051 if (img_off + sz > boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT,
1052 last_sector)) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001053 copy_sz -= trailer_sz;
1054 }
1055
David Vincze39e78552018-10-10 17:10:01 +02001056 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001057
David Vincze401c7422019-06-21 20:44:05 +02001058 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap_primary_slot);
1059 assert (rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001060
David Vincze401c7422019-06-21 20:44:05 +02001061 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap_secondary_slot);
1062 assert (rc == 0);
1063
1064 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch);
1065 assert (rc == 0);
1066
1067 if (bs->state == BOOT_STATUS_STATE_0) {
1068 BOOT_LOG_DBG("erasing scratch area");
1069 rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001070 assert(rc == 0);
1071
David Vincze39e78552018-10-10 17:10:01 +02001072 if (bs->idx == BOOT_STATUS_IDX_0) {
David Vincze401c7422019-06-21 20:44:05 +02001073 /* Write a trailer to the scratch area, even if we don't need the
1074 * scratch area for status. We need a temporary place to store the
1075 * `swap-type` while we erase the primary trailer.
1076 */
1077 rc = boot_status_init(fap_scratch, bs);
1078 assert(rc == 0);
1079
1080 if (!bs->use_scratch) {
1081 /* Prepare the primary status area... here it is known that the
1082 * last sector is not being used by the image data so it's safe
1083 * to erase.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001084 */
David Vincze401c7422019-06-21 20:44:05 +02001085 rc = boot_erase_trailer_sectors(fap_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001086 assert(rc == 0);
1087
David Vincze401c7422019-06-21 20:44:05 +02001088 rc = boot_status_init(fap_primary_slot, bs);
1089 assert(rc == 0);
1090
1091 /* Erase the temporary trailer from the scratch area. */
1092 rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size);
1093 assert(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001094 }
1095 }
1096
David Vincze401c7422019-06-21 20:44:05 +02001097 rc = boot_copy_sector(fap_secondary_slot, fap_scratch,
1098 img_off, 0, copy_sz);
1099 assert(rc == 0);
1100
David Vincze39e78552018-10-10 17:10:01 +02001101 bs->state = BOOT_STATUS_STATE_1;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001102 rc = boot_write_status(bs);
David Vincze39e78552018-10-10 17:10:01 +02001103 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001104 }
1105
David Vincze39e78552018-10-10 17:10:01 +02001106 if (bs->state == BOOT_STATUS_STATE_1) {
David Vincze401c7422019-06-21 20:44:05 +02001107 rc = boot_erase_sector(fap_secondary_slot, img_off, sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001108 assert(rc == 0);
1109
David Vincze401c7422019-06-21 20:44:05 +02001110 rc = boot_copy_sector(fap_primary_slot, fap_secondary_slot,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001111 img_off, img_off, copy_sz);
1112 assert(rc == 0);
1113
David Vincze39e78552018-10-10 17:10:01 +02001114 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001115 /* If not all sectors of the slot are being swapped,
David Vincze8bdfc2d2019-03-18 15:49:23 +01001116 * guarantee here that only the primary slot will have the state.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001117 */
David Vincze401c7422019-06-21 20:44:05 +02001118 rc = boot_erase_trailer_sectors(fap_secondary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001119 assert(rc == 0);
1120 }
1121
David Vincze39e78552018-10-10 17:10:01 +02001122 bs->state = BOOT_STATUS_STATE_2;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001123 rc = boot_write_status(bs);
David Vincze39e78552018-10-10 17:10:01 +02001124 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001125 }
1126
David Vincze39e78552018-10-10 17:10:01 +02001127 if (bs->state == BOOT_STATUS_STATE_2) {
David Vincze401c7422019-06-21 20:44:05 +02001128 rc = boot_erase_sector(fap_primary_slot, img_off, sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001129 assert(rc == 0);
1130
David Vincze401c7422019-06-21 20:44:05 +02001131 /* NOTE: If this is the final sector, we exclude the image trailer from
1132 * this copy (copy_sz was truncated earlier).
1133 */
1134 rc = boot_copy_sector(fap_scratch, fap_primary_slot,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001135 0, img_off, copy_sz);
1136 assert(rc == 0);
1137
1138 if (bs->use_scratch) {
David Vincze401c7422019-06-21 20:44:05 +02001139 scratch_trailer_off = boot_status_off(fap_scratch);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001140
1141 /* copy current status that is being maintained in scratch */
David Vincze401c7422019-06-21 20:44:05 +02001142 rc = boot_copy_sector(fap_scratch, fap_primary_slot,
1143 scratch_trailer_off, img_off + copy_sz,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001144 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data));
David Vincze39e78552018-10-10 17:10:01 +02001145 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001146
1147 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1148 &swap_state);
1149 assert(rc == 0);
1150
1151 if (swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze401c7422019-06-21 20:44:05 +02001152 rc = boot_write_image_ok(fap_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001153 assert(rc == 0);
1154 }
1155
David Vincze401c7422019-06-21 20:44:05 +02001156 if (swap_state.swap_type != BOOT_SWAP_TYPE_NONE) {
1157 rc = boot_write_swap_type(fap_primary_slot,
1158 swap_state.swap_type);
1159 assert(rc == 0);
1160 }
1161
1162 rc = boot_write_swap_size(fap_primary_slot, bs->swap_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001163 assert(rc == 0);
1164
David Vincze401c7422019-06-21 20:44:05 +02001165 rc = boot_write_magic(fap_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001166 assert(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001167 }
1168
David Vincze401c7422019-06-21 20:44:05 +02001169 /* If we wrote a trailer to the scratch area, erase it after we persist
1170 * a trailer to the primary slot. We do this to prevent mcuboot from
1171 * reading a stale status from the scratch area in case of immediate
1172 * reset.
1173 */
1174 erase_scratch = bs->use_scratch;
1175 bs->use_scratch = 0;
1176
Tamas Banf70ef8c2017-12-19 15:35:09 +00001177 bs->idx++;
David Vincze39e78552018-10-10 17:10:01 +02001178 bs->state = BOOT_STATUS_STATE_0;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001179 rc = boot_write_status(bs);
David Vincze39e78552018-10-10 17:10:01 +02001180 BOOT_STATUS_ASSERT(rc == 0);
David Vincze401c7422019-06-21 20:44:05 +02001181
1182 if (erase_scratch) {
1183 rc = boot_erase_sector(fap_scratch, 0, sz);
1184 assert(rc == 0);
1185 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001186 }
David Vincze401c7422019-06-21 20:44:05 +02001187
1188 flash_area_close(fap_primary_slot);
1189 flash_area_close(fap_secondary_slot);
1190 flash_area_close(fap_scratch);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001191}
1192#endif /* !MCUBOOT_OVERWRITE_ONLY */
1193
1194/**
David Vincze401c7422019-06-21 20:44:05 +02001195 * Overwrite primary slot with the image contained in the secondary slot.
1196 * If a prior copy operation was interrupted by a system reset, this function
1197 * redos the copy.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001198 *
1199 * @param bs The current boot status. This function reads
1200 * this struct to determine if it is resuming
1201 * an interrupted swap operation. This
1202 * function writes the updated status to this
1203 * function on return.
1204 *
1205 * @return 0 on success; nonzero on failure.
1206 */
1207#ifdef MCUBOOT_OVERWRITE_ONLY
1208static int
1209boot_copy_image(struct boot_status *bs)
1210{
1211 size_t sect_count;
1212 size_t sect;
1213 int rc;
1214 size_t size = 0;
1215 size_t this_size;
David Vincze39e78552018-10-10 17:10:01 +02001216 size_t last_sector;
David Vincze401c7422019-06-21 20:44:05 +02001217 const struct flash_area *fap_primary_slot;
1218 const struct flash_area *fap_secondary_slot;
David Vincze39e78552018-10-10 17:10:01 +02001219
1220 (void)bs;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001221
David Vincze8bdfc2d2019-03-18 15:49:23 +01001222 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1223 BOOT_LOG_INF("Erasing the primary slot");
Tamas Banf70ef8c2017-12-19 15:35:09 +00001224
David Vincze401c7422019-06-21 20:44:05 +02001225 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap_primary_slot);
1226 assert (rc == 0);
1227
1228 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap_secondary_slot);
1229 assert (rc == 0);
1230
David Vincze8bdfc2d2019-03-18 15:49:23 +01001231 sect_count = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001232 for (sect = 0; sect < sect_count; sect++) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001233 this_size = boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, sect);
David Vincze401c7422019-06-21 20:44:05 +02001234 rc = boot_erase_sector(fap_primary_slot, size, this_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001235 assert(rc == 0);
1236
1237 size += this_size;
1238 }
1239
David Vincze8bdfc2d2019-03-18 15:49:23 +01001240 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1241 size);
David Vincze401c7422019-06-21 20:44:05 +02001242 rc = boot_copy_sector(fap_secondary_slot, fap_primary_slot, 0, 0, size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001243
David Vincze060968d2019-05-23 01:13:14 +02001244 /* Update the stored security counter with the new image's security counter
David Vincze8bdfc2d2019-03-18 15:49:23 +01001245 * value. Both slots hold the new image at this point, but the secondary
1246 * slot's image header must be passed because the read image headers in the
1247 * boot_data structure have not been updated yet.
David Vincze060968d2019-05-23 01:13:14 +02001248 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001249 rc = boot_update_security_counter(BOOT_PRIMARY_SLOT,
1250 boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT));
David Vincze060968d2019-05-23 01:13:14 +02001251 if (rc != 0) {
1252 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
1253 return rc;
1254 }
1255
David Vincze39e78552018-10-10 17:10:01 +02001256 /*
1257 * Erases header and trailer. The trailer is erased because when a new
1258 * image is written without a trailer as is the case when using newt, the
1259 * trailer that was left might trigger a new upgrade.
1260 */
David Vincze401c7422019-06-21 20:44:05 +02001261 BOOT_LOG_DBG("erasing secondary header");
1262 rc = boot_erase_sector(fap_secondary_slot,
David Vincze8bdfc2d2019-03-18 15:49:23 +01001263 boot_img_sector_off(&boot_data,
1264 BOOT_SECONDARY_SLOT, 0),
1265 boot_img_sector_size(&boot_data,
1266 BOOT_SECONDARY_SLOT, 0));
Tamas Banf70ef8c2017-12-19 15:35:09 +00001267 assert(rc == 0);
David Vincze8bdfc2d2019-03-18 15:49:23 +01001268 last_sector = boot_img_num_sectors(&boot_data, BOOT_SECONDARY_SLOT) - 1;
David Vincze401c7422019-06-21 20:44:05 +02001269 BOOT_LOG_DBG("erasing secondary trailer");
1270 rc = boot_erase_sector(fap_secondary_slot,
David Vincze8bdfc2d2019-03-18 15:49:23 +01001271 boot_img_sector_off(&boot_data, BOOT_SECONDARY_SLOT,
1272 last_sector),
1273 boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT,
1274 last_sector));
David Vincze39e78552018-10-10 17:10:01 +02001275 assert(rc == 0);
1276
David Vincze401c7422019-06-21 20:44:05 +02001277 flash_area_close(fap_primary_slot);
1278 flash_area_close(fap_secondary_slot);
1279
David Vincze8bdfc2d2019-03-18 15:49:23 +01001280 /* TODO: Perhaps verify the primary slot's signature again? */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001281
1282 return 0;
1283}
1284#else
David Vincze401c7422019-06-21 20:44:05 +02001285/**
1286 * Swaps the two images in flash. If a prior copy operation was interrupted
1287 * by a system reset, this function completes that operation.
1288 *
1289 * @param bs The current boot status. This function reads
1290 * this struct to determine if it is resuming
1291 * an interrupted swap operation. This
1292 * function writes the updated status to this
1293 * function on return.
1294 *
1295 * @return 0 on success; nonzero on failure.
1296 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001297static int
David Vincze401c7422019-06-21 20:44:05 +02001298boot_swap_image(struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001299{
1300 uint32_t sz;
1301 int first_sector_idx;
1302 int last_sector_idx;
David Vincze401c7422019-06-21 20:44:05 +02001303 int last_idx_secondary_slot;
David Vincze39e78552018-10-10 17:10:01 +02001304 uint32_t swap_idx;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001305 struct image_header *hdr;
1306 uint32_t size;
1307 uint32_t copy_size;
David Vincze401c7422019-06-21 20:44:05 +02001308 uint32_t primary_slot_size;
1309 uint32_t secondary_slot_size;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001310 int rc;
1311
1312 /* FIXME: just do this if asked by user? */
1313
1314 size = copy_size = 0;
1315
David Vincze39e78552018-10-10 17:10:01 +02001316 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001317 /*
1318 * No swap ever happened, so need to find the largest image which
1319 * will be used to determine the amount of sectors to swap.
1320 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001321 hdr = boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001322 if (hdr->ih_magic == IMAGE_MAGIC) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001323 rc = boot_read_image_size(BOOT_PRIMARY_SLOT, hdr, &copy_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001324 assert(rc == 0);
1325 }
1326
David Vincze8bdfc2d2019-03-18 15:49:23 +01001327 hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_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_SECONDARY_SLOT, hdr, &size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001330 assert(rc == 0);
1331 }
1332
1333 if (size > copy_size) {
1334 copy_size = size;
1335 }
1336
1337 bs->swap_size = copy_size;
1338 } else {
1339 /*
1340 * If a swap was under way, the swap_size should already be present
1341 * in the trailer...
1342 */
1343 rc = boot_read_swap_size(&bs->swap_size);
1344 assert(rc == 0);
1345
1346 copy_size = bs->swap_size;
1347 }
1348
David Vincze401c7422019-06-21 20:44:05 +02001349 primary_slot_size = 0;
1350 secondary_slot_size = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001351 last_sector_idx = 0;
David Vincze401c7422019-06-21 20:44:05 +02001352 last_idx_secondary_slot = 0;
1353
1354 /*
1355 * Knowing the size of the largest image between both slots, here we
1356 * find what is the last sector in the primary slot that needs swapping.
1357 * Since we already know that both slots are compatible, the secondary
1358 * slot's last sector is not really required after this check is finished.
1359 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001360 while (1) {
David Vincze401c7422019-06-21 20:44:05 +02001361 if ((primary_slot_size < copy_size) ||
1362 (primary_slot_size < secondary_slot_size)) {
1363 primary_slot_size += boot_img_sector_size(&boot_data,
1364 BOOT_PRIMARY_SLOT,
1365 last_sector_idx);
1366 }
1367 if ((secondary_slot_size < copy_size) ||
1368 (secondary_slot_size < primary_slot_size)) {
1369 secondary_slot_size += boot_img_sector_size(&boot_data,
1370 BOOT_SECONDARY_SLOT,
1371 last_idx_secondary_slot);
1372 }
1373 if (primary_slot_size >= copy_size &&
1374 secondary_slot_size >= copy_size &&
1375 primary_slot_size == secondary_slot_size) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001376 break;
1377 }
1378 last_sector_idx++;
David Vincze401c7422019-06-21 20:44:05 +02001379 last_idx_secondary_slot++;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001380 }
1381
1382 swap_idx = 0;
1383 while (last_sector_idx >= 0) {
1384 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
David Vincze39e78552018-10-10 17:10:01 +02001385 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001386 boot_swap_sectors(first_sector_idx, sz, bs);
1387 }
1388
1389 last_sector_idx = first_sector_idx - 1;
1390 swap_idx++;
1391 }
1392
David Vincze8bdfc2d2019-03-18 15:49:23 +01001393#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
David Vincze39e78552018-10-10 17:10:01 +02001394 if (boot_status_fails > 0) {
David Vincze401c7422019-06-21 20:44:05 +02001395 BOOT_LOG_WRN("%d status write fails performing the swap",
1396 boot_status_fails);
David Vincze39e78552018-10-10 17:10:01 +02001397 }
1398#endif
1399
Tamas Banf70ef8c2017-12-19 15:35:09 +00001400 return 0;
1401}
1402#endif
1403
1404/**
David Vincze8bdfc2d2019-03-18 15:49:23 +01001405 * Marks the image in the primary slot as fully copied.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001406 */
1407#ifndef MCUBOOT_OVERWRITE_ONLY
1408static int
1409boot_set_copy_done(void)
1410{
1411 const struct flash_area *fap;
1412 int rc;
1413
David Vincze8bdfc2d2019-03-18 15:49:23 +01001414 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001415 if (rc != 0) {
1416 return BOOT_EFLASH;
1417 }
1418
1419 rc = boot_write_copy_done(fap);
1420 flash_area_close(fap);
1421 return rc;
1422}
1423#endif /* !MCUBOOT_OVERWRITE_ONLY */
1424
1425/**
David Vincze8bdfc2d2019-03-18 15:49:23 +01001426 * Marks a reverted image in the primary slot as confirmed. This is necessary to
1427 * ensure the status bytes from the image revert operation don't get processed
1428 * on a subsequent boot.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001429 *
1430 * NOTE: image_ok is tested before writing because if there's a valid permanent
David Vincze8bdfc2d2019-03-18 15:49:23 +01001431 * image installed on the primary slot and the new image to be upgrade to has a
1432 * bad sig, image_ok would be overwritten.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001433 */
1434#ifndef MCUBOOT_OVERWRITE_ONLY
1435static int
1436boot_set_image_ok(void)
1437{
1438 const struct flash_area *fap;
1439 struct boot_swap_state state;
1440 int rc;
1441
David Vincze8bdfc2d2019-03-18 15:49:23 +01001442 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001443 if (rc != 0) {
1444 return BOOT_EFLASH;
1445 }
1446
1447 rc = boot_read_swap_state(fap, &state);
1448 if (rc != 0) {
1449 rc = BOOT_EFLASH;
1450 goto out;
1451 }
1452
1453 if (state.image_ok == BOOT_FLAG_UNSET) {
1454 rc = boot_write_image_ok(fap);
1455 }
1456
1457out:
1458 flash_area_close(fap);
1459 return rc;
1460}
1461#endif /* !MCUBOOT_OVERWRITE_ONLY */
1462
1463/**
1464 * Performs an image swap if one is required.
1465 *
1466 * @param out_swap_type On success, the type of swap performed gets
1467 * written here.
1468 *
1469 * @return 0 on success; nonzero on failure.
1470 */
1471static int
1472boot_swap_if_needed(int *out_swap_type)
1473{
1474 struct boot_status bs;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001475 int rc;
1476
1477 /* Determine if we rebooted in the middle of an image swap
1478 * operation.
1479 */
1480 rc = boot_read_status(&bs);
1481 assert(rc == 0);
1482 if (rc != 0) {
1483 return rc;
1484 }
1485
1486 /* If a partial swap was detected, complete it. */
David Vincze39e78552018-10-10 17:10:01 +02001487 if (bs.idx != BOOT_STATUS_IDX_0 || bs.state != BOOT_STATUS_STATE_0) {
David Vincze401c7422019-06-21 20:44:05 +02001488#ifdef MCUBOOT_OVERWRITE_ONLY
1489 /* Should never arrive here, overwrite-only mode has no swap state. */
1490 assert(0);
1491#else
1492 /* Determine the type of swap operation being resumed from the
1493 * `swap-type` trailer field.
1494 */
1495 rc = boot_swap_image(&bs);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001496 assert(rc == 0);
David Vincze401c7422019-06-21 20:44:05 +02001497#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +00001498
Tamas Banf70ef8c2017-12-19 15:35:09 +00001499 } else {
David Vincze401c7422019-06-21 20:44:05 +02001500 if (bs.swap_type == BOOT_SWAP_TYPE_NONE) {
1501 bs.swap_type = boot_validated_swap_type(&bs);
1502 } else if (boot_validate_slot(BOOT_SECONDARY_SLOT, &bs) != 0) {
1503 bs.swap_type = BOOT_SWAP_TYPE_FAIL;
1504 }
1505 switch (bs.swap_type) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001506 case BOOT_SWAP_TYPE_TEST:
1507 case BOOT_SWAP_TYPE_PERM:
1508 case BOOT_SWAP_TYPE_REVERT:
David Vincze401c7422019-06-21 20:44:05 +02001509#ifdef MCUBOOT_OVERWRITE_ONLY
Tamas Banf70ef8c2017-12-19 15:35:09 +00001510 rc = boot_copy_image(&bs);
David Vincze401c7422019-06-21 20:44:05 +02001511#else
1512 rc = boot_swap_image(&bs);
1513#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +00001514 assert(rc == 0);
1515 break;
1516 }
1517 }
1518
David Vincze401c7422019-06-21 20:44:05 +02001519 *out_swap_type = bs.swap_type;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001520 return 0;
1521}
1522
1523/**
1524 * Prepares the booting process. This function moves images around in flash as
1525 * appropriate, and tells you what address to boot from.
1526 *
1527 * @param rsp On success, indicates how booting should occur.
1528 *
1529 * @return 0 on success; nonzero on failure.
1530 */
1531int
1532boot_go(struct boot_rsp *rsp)
1533{
1534 int swap_type;
1535 size_t slot;
1536 int rc;
1537 int fa_id;
1538 bool reload_headers = false;
1539
1540 /* The array of slot sectors are defined here (as opposed to file scope) so
1541 * that they don't get allocated for non-boot-loader apps. This is
1542 * necessary because the gcc option "-fdata-sections" doesn't seem to have
1543 * any effect in older gcc versions (e.g., 4.8.4).
1544 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001545 static boot_sector_t primary_slot_sectors[BOOT_MAX_IMG_SECTORS];
1546 static boot_sector_t secondary_slot_sectors[BOOT_MAX_IMG_SECTORS];
David Vincze401c7422019-06-21 20:44:05 +02001547 static boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
David Vincze8bdfc2d2019-03-18 15:49:23 +01001548 boot_data.imgs[BOOT_PRIMARY_SLOT].sectors = primary_slot_sectors;
1549 boot_data.imgs[BOOT_SECONDARY_SLOT].sectors = secondary_slot_sectors;
David Vincze401c7422019-06-21 20:44:05 +02001550 boot_data.scratch.sectors = scratch_sectors;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001551
1552 /* Open boot_data image areas for the duration of this call. */
1553 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1554 fa_id = flash_area_id_from_image_slot(slot);
1555 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot));
1556 assert(rc == 0);
1557 }
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001558
Tamas Banf70ef8c2017-12-19 15:35:09 +00001559 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
1560 &BOOT_SCRATCH_AREA(&boot_data));
1561 assert(rc == 0);
1562
1563 /* Determine the sector layout of the image slots and scratch area. */
1564 rc = boot_read_sectors();
1565 if (rc != 0) {
David Vincze39e78552018-10-10 17:10:01 +02001566 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - too small?",
1567 BOOT_MAX_IMG_SECTORS);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001568 goto out;
1569 }
1570
1571 /* Attempt to read an image header from each slot. */
David Vincze39e78552018-10-10 17:10:01 +02001572 rc = boot_read_image_headers(false);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001573 if (rc != 0) {
1574 goto out;
1575 }
1576
1577 /* If the image slots aren't compatible, no swap is possible. Just boot
David Vincze8bdfc2d2019-03-18 15:49:23 +01001578 * into the primary slot.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001579 */
1580 if (boot_slots_compatible()) {
1581 rc = boot_swap_if_needed(&swap_type);
1582 assert(rc == 0);
1583 if (rc != 0) {
1584 goto out;
1585 }
1586
1587 /*
1588 * The following states need image_ok be explicitly set after the
1589 * swap was finished to avoid a new revert.
1590 */
Tamas Ban581034a2017-12-19 19:54:37 +00001591 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
David Vincze401c7422019-06-21 20:44:05 +02001592 swap_type == BOOT_SWAP_TYPE_FAIL ||
1593 swap_type == BOOT_SWAP_TYPE_PERM) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001594#ifndef MCUBOOT_OVERWRITE_ONLY
1595 rc = boot_set_image_ok();
1596 if (rc != 0) {
1597 swap_type = BOOT_SWAP_TYPE_PANIC;
1598 }
1599#endif /* !MCUBOOT_OVERWRITE_ONLY */
1600 }
1601 } else {
1602 swap_type = BOOT_SWAP_TYPE_NONE;
1603 }
1604
1605 switch (swap_type) {
1606 case BOOT_SWAP_TYPE_NONE:
David Vincze8bdfc2d2019-03-18 15:49:23 +01001607 slot = BOOT_PRIMARY_SLOT;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001608 break;
1609
1610 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1611 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
1612 case BOOT_SWAP_TYPE_REVERT:
David Vincze8bdfc2d2019-03-18 15:49:23 +01001613 slot = BOOT_SECONDARY_SLOT;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001614 reload_headers = true;
1615#ifndef MCUBOOT_OVERWRITE_ONLY
David Vincze060968d2019-05-23 01:13:14 +02001616 if (swap_type == BOOT_SWAP_TYPE_PERM) {
1617 /* Update the stored security counter with the new image's security
David Vincze8bdfc2d2019-03-18 15:49:23 +01001618 * counter value. The primary slot holds the new image at this
1619 * point, but the secondary slot's image header must be passed
1620 * because the read image headers in the boot_data structure have
1621 * not been updated yet.
David Vincze060968d2019-05-23 01:13:14 +02001622 *
1623 * In case of a permanent image swap mcuboot will never attempt to
1624 * revert the images on the next reboot. Therefore, the security
1625 * counter must be increased right after the image upgrade.
1626 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001627 rc = boot_update_security_counter(BOOT_PRIMARY_SLOT,
1628 boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT));
David Vincze060968d2019-05-23 01:13:14 +02001629 if (rc != 0) {
1630 BOOT_LOG_ERR("Security counter update failed after "
1631 "image upgrade.");
1632 goto out;
1633 }
1634 }
1635
Tamas Banf70ef8c2017-12-19 15:35:09 +00001636 rc = boot_set_copy_done();
1637 if (rc != 0) {
1638 swap_type = BOOT_SWAP_TYPE_PANIC;
1639 }
1640#endif /* !MCUBOOT_OVERWRITE_ONLY */
1641 break;
1642
1643 case BOOT_SWAP_TYPE_FAIL:
David Vincze8bdfc2d2019-03-18 15:49:23 +01001644 /* The image in the secondary slot was invalid and is now erased.
1645 * Ensure we don't try to boot into it again on the next reboot.
1646 * Do this by pretending we just reverted back to the primary slot.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001647 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001648 slot = BOOT_PRIMARY_SLOT;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001649 reload_headers = true;
1650 break;
1651
1652 default:
1653 swap_type = BOOT_SWAP_TYPE_PANIC;
1654 }
1655
1656 if (swap_type == BOOT_SWAP_TYPE_PANIC) {
1657 BOOT_LOG_ERR("panic!");
1658 assert(0);
1659
1660 /* Loop forever... */
David Vincze39e78552018-10-10 17:10:01 +02001661 while (1) {}
Tamas Banf70ef8c2017-12-19 15:35:09 +00001662 }
1663
Tamas Banf70ef8c2017-12-19 15:35:09 +00001664 if (reload_headers) {
David Vincze39e78552018-10-10 17:10:01 +02001665 rc = boot_read_image_headers(false);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001666 if (rc != 0) {
1667 goto out;
1668 }
1669 /* Since headers were reloaded, it can be assumed we just performed a
1670 * swap or overwrite. Now the header info that should be used to
David Vincze8bdfc2d2019-03-18 15:49:23 +01001671 * provide the data for the bootstrap, which previously was at the
1672 * secondary slot, was updated to the primary slot.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001673 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001674 slot = BOOT_PRIMARY_SLOT;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001675 }
1676
David Vincze8bdfc2d2019-03-18 15:49:23 +01001677#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
David Vincze401c7422019-06-21 20:44:05 +02001678 rc = boot_validate_slot(BOOT_PRIMARY_SLOT, NULL);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001679 if (rc != 0) {
1680 rc = BOOT_EBADIMAGE;
1681 goto out;
1682 }
David Vincze8bdfc2d2019-03-18 15:49:23 +01001683#else
1684 /* Even if we're not re-validating the primary slot, we could be booting
David Vincze39e78552018-10-10 17:10:01 +02001685 * onto an empty flash chip. At least do a basic sanity check that
1686 * the magic number on the image is OK.
1687 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001688 if (boot_data.imgs[BOOT_PRIMARY_SLOT].hdr.ih_magic != IMAGE_MAGIC) {
1689 BOOT_LOG_ERR("bad image magic 0x%lx",
1690 (unsigned long)boot_data.imgs[BOOT_PRIMARY_SLOT].hdr.ih_magic);
David Vincze39e78552018-10-10 17:10:01 +02001691 rc = BOOT_EBADIMAGE;
1692 goto out;
1693 }
David Vincze8bdfc2d2019-03-18 15:49:23 +01001694#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001695
David Vincze060968d2019-05-23 01:13:14 +02001696 /* Update the stored security counter with the active image's security
1697 * counter value. It will be updated only if the new security counter is
1698 * greater than the stored value.
1699 *
1700 * In case of a successful image swapping when the swap type is TEST the
1701 * security counter can be increased only after a reset, when the swap type
1702 * is NONE and the image has marked itself "OK" (the image_ok flag has been
1703 * set). This way a "revert" swap can be performed if it's necessary.
1704 */
1705 if (swap_type == BOOT_SWAP_TYPE_NONE) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001706 rc = boot_update_security_counter(BOOT_PRIMARY_SLOT,
1707 boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT));
David Vincze060968d2019-05-23 01:13:14 +02001708 if (rc != 0) {
1709 BOOT_LOG_ERR("Security counter update failed after image "
1710 "validation.");
1711 goto out;
1712 }
1713 }
1714
Tamas Banf70ef8c2017-12-19 15:35:09 +00001715 /* Always boot from the primary slot. */
David Vincze401c7422019-06-21 20:44:05 +02001716 rsp->br_flash_dev_id = boot_data.imgs[BOOT_PRIMARY_SLOT].area->fa_device_id;
David Vincze8bdfc2d2019-03-18 15:49:23 +01001717 rsp->br_image_off = boot_img_slot_off(&boot_data, BOOT_PRIMARY_SLOT);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001718 rsp->br_hdr = boot_img_hdr(&boot_data, slot);
1719
Tamas Ban0e8ab302019-01-17 11:45:31 +00001720 /* Save boot status to shared memory area */
1721 rc = boot_save_boot_status(SW_S_NS,
1722 rsp->br_hdr,
1723 BOOT_IMG_AREA(&boot_data, slot));
1724 if (rc) {
1725 BOOT_LOG_ERR("Failed to add data to shared area");
1726 }
1727
Tamas Banf70ef8c2017-12-19 15:35:09 +00001728 out:
1729 flash_area_close(BOOT_SCRATCH_AREA(&boot_data));
1730 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1731 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
1732 }
1733 return rc;
1734}
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001735
Oliver Swedef9982442018-08-24 18:37:44 +01001736#else /* MCUBOOT_NO_SWAP || MCUBOOT_RAM_LOADING */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001737
David Vinczedcba70b2019-05-28 12:02:52 +02001738#define BOOT_LOG_IMAGE_INFO(area, hdr, state) \
1739 BOOT_LOG_INF("Image %u: version=%u.%u.%u+%u, magic=%5s, image_ok=0x%x", \
1740 (area), \
1741 (hdr)->ih_ver.iv_major, \
1742 (hdr)->ih_ver.iv_minor, \
1743 (hdr)->ih_ver.iv_revision, \
1744 (hdr)->ih_ver.iv_build_num, \
1745 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
1746 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
1747 "bad"), \
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001748 (state)->image_ok)
1749
1750struct image_slot_version {
1751 uint64_t version;
1752 uint32_t slot_number;
1753};
1754
1755/**
1756 * Extract the version number from the image header. This function must be
1757 * ported if version number format has changed in the image header.
1758 *
1759 * @param hdr Pointer to an image header structure
1760 *
Oliver Swedef9982442018-08-24 18:37:44 +01001761 * @return Version number casted to uint64_t
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001762 */
1763static uint64_t
1764boot_get_version_number(struct image_header *hdr)
1765{
Oliver Swedef9982442018-08-24 18:37:44 +01001766 uint64_t version = 0;
1767 version |= (uint64_t)hdr->ih_ver.iv_major << (IMAGE_VER_MINOR_LENGTH
1768 + IMAGE_VER_REVISION_LENGTH
1769 + IMAGE_VER_BUILD_NUM_LENGTH);
1770 version |= (uint64_t)hdr->ih_ver.iv_minor << (IMAGE_VER_REVISION_LENGTH
1771 + IMAGE_VER_BUILD_NUM_LENGTH);
1772 version |= (uint64_t)hdr->ih_ver.iv_revision << IMAGE_VER_BUILD_NUM_LENGTH;
1773 version |= hdr->ih_ver.iv_build_num;
1774 return version;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001775}
1776
1777/**
1778 * Comparator function for `qsort` to compare version numbers. This function
1779 * must be ported if version number format has changed in the image header.
1780 *
1781 * @param ver1 Pointer to an array element which holds the version number
1782 * @param ver2 Pointer to another array element which holds the version
1783 * number
1784 *
1785 * @return if version1 > version2 -1
1786 * if version1 == version2 0
1787 * if version1 < version2 1
1788 */
1789static int
1790boot_compare_version_numbers(const void *ver1, const void *ver2)
1791{
1792 if (((struct image_slot_version *)ver1)->version <
1793 ((struct image_slot_version *)ver2)->version) {
1794 return 1;
1795 }
1796
1797 if (((struct image_slot_version *)ver1)->version ==
1798 ((struct image_slot_version *)ver2)->version) {
1799 return 0;
1800 }
1801
1802 return -1;
1803}
1804
1805/**
1806 * Sort the available images based on the version number and puts them in
1807 * a list.
1808 *
1809 * @param boot_sequence A pointer to an array, whose aim is to carry
1810 * the boot order of candidate images.
1811 * @param slot_cnt The number of flash areas, which can contains firmware
1812 * images.
1813 *
1814 * @return The number of valid images.
1815 */
1816uint32_t
1817boot_get_boot_sequence(uint32_t *boot_sequence, uint32_t slot_cnt)
1818{
1819 struct boot_swap_state slot_state;
1820 struct image_header *hdr;
1821 struct image_slot_version image_versions[BOOT_NUM_SLOTS] = {{0}};
1822 uint32_t image_cnt = 0;
1823 uint32_t slot;
1824 int32_t rc;
1825 int32_t fa_id;
1826
1827 for (slot = 0; slot < slot_cnt; slot++) {
1828 hdr = boot_img_hdr(&boot_data, slot);
1829 fa_id = flash_area_id_from_image_slot(slot);
1830 rc = boot_read_swap_state_by_id(fa_id, &slot_state);
1831 if (rc != 0) {
David Vinczedcba70b2019-05-28 12:02:52 +02001832 BOOT_LOG_ERR("Error during reading image trailer from slot: %u",
1833 slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001834 continue;
1835 }
1836
1837 if (hdr->ih_magic == IMAGE_MAGIC) {
1838 if (slot_state.magic == BOOT_MAGIC_GOOD ||
David Vincze39e78552018-10-10 17:10:01 +02001839 slot_state.image_ok == BOOT_FLAG_SET) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001840 /* Valid cases:
1841 * - Test mode: magic is OK in image trailer
1842 * - Permanent mode: image_ok flag has previously set
1843 */
1844 image_versions[slot].slot_number = slot;
1845 image_versions[slot].version = boot_get_version_number(hdr);
1846 image_cnt++;
1847 }
1848
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001849 BOOT_LOG_IMAGE_INFO(slot, hdr, &slot_state);
1850 } else {
David Vinczedcba70b2019-05-28 12:02:52 +02001851 BOOT_LOG_INF("Image %u: No valid image", slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001852 }
1853 }
1854
1855 /* Sort the images based on version number */
1856 qsort(&image_versions[0],
1857 slot_cnt,
1858 sizeof(struct image_slot_version),
1859 boot_compare_version_numbers);
1860
1861 /* Copy the calculated boot sequence to boot_sequence array */
1862 for (slot = 0; slot < slot_cnt; slot++) {
1863 boot_sequence[slot] = image_versions[slot].slot_number;
1864 }
1865
1866 return image_cnt;
1867}
1868
Oliver Swedef9982442018-08-24 18:37:44 +01001869#ifdef MCUBOOT_RAM_LOADING
1870/**
1871 * Copies an image from a slot in the flash to an SRAM address, where the load
1872 * address has already been inserted into the image header by this point and is
1873 * extracted from it within this method. The copying is done sector-by-sector.
1874 *
1875 * @param slot The flash slot of the image to be copied to SRAM.
1876 *
1877 * @param hdr Pointer to the image header structure of the image
1878 * that needs to be copid to SRAM
1879 *
1880 * @return 0 on success; nonzero on failure.
1881 */
1882static int
1883boot_copy_image_to_sram(int slot, struct image_header *hdr)
1884{
1885 int rc;
1886 uint32_t sect_sz;
1887 uint32_t sect = 0;
1888 uint32_t bytes_copied = 0;
1889 const struct flash_area *fap_src = NULL;
1890 uint32_t dst = (uint32_t) hdr->ih_load_addr;
1891 uint32_t img_sz;
1892
1893 if (dst % 4 != 0) {
Tamas Banc27b5c32019-05-28 16:30:19 +01001894 BOOT_LOG_INF("Cannot copy the image to the SRAM address 0x%x "
Oliver Swedef9982442018-08-24 18:37:44 +01001895 "- the load address must be aligned with 4 bytes due to SRAM "
1896 "restrictions", dst);
1897 return BOOT_EBADARGS;
1898 }
1899
1900 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap_src);
1901 if (rc != 0) {
1902 return BOOT_EFLASH;
1903 }
1904
1905 rc = boot_read_image_size(slot, hdr, &img_sz);
1906 if (rc != 0) {
1907 return BOOT_EFLASH;
1908 }
1909
1910 while (bytes_copied < img_sz) {
1911 sect_sz = boot_img_sector_size(&boot_data, slot, sect);
1912 /*
1913 * Direct copy from where the image sector resides in flash to its new
1914 * location in SRAM
1915 */
1916 rc = flash_area_read(fap_src,
1917 bytes_copied,
1918 (void *)(dst + bytes_copied),
1919 sect_sz);
1920 if (rc != 0) {
1921 BOOT_LOG_INF("Error whilst copying image from Flash to SRAM");
1922 break;
1923 } else {
1924 bytes_copied += sect_sz;
1925 }
1926 sect++;
1927 }
1928
1929 if (fap_src) {
1930 flash_area_close(fap_src);
1931 }
1932 return rc;
1933}
1934#endif /* MCUBOOT_RAM_LOADING */
1935
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001936/**
1937 * Prepares the booting process. This function choose the newer image in flash
1938 * as appropriate, and returns the address to boot from.
1939 *
1940 * @param rsp On success, indicates how booting should occur.
1941 *
1942 * @return 0 on success; nonzero on failure.
1943 */
1944int
1945boot_go(struct boot_rsp *rsp)
1946{
1947 size_t slot = 0;
1948 int32_t i;
1949 int rc;
1950 int fa_id;
1951 uint32_t boot_sequence[BOOT_NUM_SLOTS];
1952 uint32_t img_cnt;
Oliver Swedef9982442018-08-24 18:37:44 +01001953 struct image_header *newest_image_header;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001954
David Vincze8bdfc2d2019-03-18 15:49:23 +01001955 static boot_sector_t primary_slot_sectors[BOOT_MAX_IMG_SECTORS];
1956 static boot_sector_t secondary_slot_sectors[BOOT_MAX_IMG_SECTORS];
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001957
David Vincze8bdfc2d2019-03-18 15:49:23 +01001958 boot_data.imgs[BOOT_PRIMARY_SLOT].sectors = &primary_slot_sectors[0];
1959 boot_data.imgs[BOOT_SECONDARY_SLOT].sectors = &secondary_slot_sectors[0];
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001960
1961 /* Open boot_data image areas for the duration of this call. */
1962 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
1963 fa_id = flash_area_id_from_image_slot(i);
1964 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, i));
1965 assert(rc == 0);
1966 }
1967
1968 /* Determine the sector layout of the image slots. */
1969 rc = boot_read_sectors();
1970 if (rc != 0) {
David Vincze39e78552018-10-10 17:10:01 +02001971 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - too small?",
1972 BOOT_MAX_IMG_SECTORS);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001973 goto out;
1974 }
1975
1976 /* Attempt to read an image header from each slot. */
David Vincze39e78552018-10-10 17:10:01 +02001977 rc = boot_read_image_headers(false);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001978 if (rc != 0) {
1979 goto out;
1980 }
1981
1982 img_cnt = boot_get_boot_sequence(boot_sequence, BOOT_NUM_SLOTS);
1983 if (img_cnt) {
1984 /* Authenticate images */
1985 for (i = 0; i < img_cnt; i++) {
David Vincze401c7422019-06-21 20:44:05 +02001986 rc = boot_validate_slot(boot_sequence[i], NULL);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001987 if (rc == 0) {
1988 slot = boot_sequence[i];
1989 break;
1990 }
1991 }
1992 if (rc) {
1993 /* If there was no valid image at all */
1994 rc = BOOT_EBADIMAGE;
1995 goto out;
1996 }
1997
Oliver Swedef9982442018-08-24 18:37:44 +01001998 /* The slot variable now refers to the newest image's slot in flash */
1999 newest_image_header = boot_img_hdr(&boot_data, slot);
2000
David Vincze060968d2019-05-23 01:13:14 +02002001 /* Update the security counter with the newest image's security
2002 * counter value.
2003 */
2004 rc = boot_update_security_counter(slot, newest_image_header);
2005 if (rc != 0) {
2006 BOOT_LOG_ERR("Security counter update failed after image "
2007 "validation.");
2008 goto out;
2009 }
2010
Oliver Swedef9982442018-08-24 18:37:44 +01002011 #ifdef MCUBOOT_RAM_LOADING
2012 if (newest_image_header->ih_flags & IMAGE_F_RAM_LOAD) {
2013 /* Copy image to the load address from where it
2014 * currently resides in flash */
2015 rc = boot_copy_image_to_sram(slot, newest_image_header);
2016 if (rc != 0) {
2017 rc = BOOT_EBADIMAGE;
David Vincze8bdfc2d2019-03-18 15:49:23 +01002018 BOOT_LOG_INF("Could not copy image from the %s slot in "
Tamas Banc27b5c32019-05-28 16:30:19 +01002019 "the Flash to load address 0x%x in SRAM, "
David Vincze8bdfc2d2019-03-18 15:49:23 +01002020 "aborting..", (slot == BOOT_PRIMARY_SLOT) ?
2021 "primary" : "secondary",
Oliver Swedef9982442018-08-24 18:37:44 +01002022 newest_image_header->ih_load_addr);
2023 goto out;
2024 } else {
David Vincze8bdfc2d2019-03-18 15:49:23 +01002025 BOOT_LOG_INF("Image has been copied from the %s slot in "
2026 "the flash to SRAM address 0x%x",
2027 (slot == BOOT_PRIMARY_SLOT) ?
2028 "primary" : "secondary",
Oliver Swedef9982442018-08-24 18:37:44 +01002029 newest_image_header->ih_load_addr);
2030 }
2031
2032 /* Validate the image hash in SRAM after the copy was successful */
2033 rc = bootutil_check_hash_after_loading(newest_image_header);
2034 if (rc != 0) {
2035 rc = BOOT_EBADIMAGE;
2036 BOOT_LOG_INF("Cannot validate the hash of the image that was "
2037 "copied to SRAM, aborting..");
2038 goto out;
2039 }
2040
Tamas Banc27b5c32019-05-28 16:30:19 +01002041 BOOT_LOG_INF("Booting image from SRAM at address 0x%x",
Oliver Swedef9982442018-08-24 18:37:44 +01002042 newest_image_header->ih_load_addr);
2043 } else {
David Vincze8a2a4e22019-05-24 10:14:23 +02002044#endif /* MCUBOOT_RAM_LOADING */
David Vincze8bdfc2d2019-03-18 15:49:23 +01002045 BOOT_LOG_INF("Booting image from the %s slot",
2046 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Vincze8a2a4e22019-05-24 10:14:23 +02002047#ifdef MCUBOOT_RAM_LOADING
Oliver Swedef9982442018-08-24 18:37:44 +01002048 }
David Vincze8a2a4e22019-05-24 10:14:23 +02002049#endif
Oliver Swedef9982442018-08-24 18:37:44 +01002050
2051 rsp->br_hdr = newest_image_header;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002052 rsp->br_image_off = boot_img_slot_off(&boot_data, slot);
David Vincze401c7422019-06-21 20:44:05 +02002053 rsp->br_flash_dev_id = boot_data.imgs[slot].area->fa_device_id;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002054 } else {
2055 /* No candidate image available */
2056 rc = BOOT_EBADIMAGE;
David Vincze060968d2019-05-23 01:13:14 +02002057 goto out;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002058 }
2059
Tamas Ban0e8ab302019-01-17 11:45:31 +00002060 /* Save boot status to shared memory area */
2061 rc = boot_save_boot_status(SW_S_NS,
2062 rsp->br_hdr,
2063 BOOT_IMG_AREA(&boot_data, slot));
2064 if (rc) {
2065 BOOT_LOG_ERR("Failed to add data to shared area");
2066 }
2067
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002068out:
2069 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2070 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
2071 }
2072 return rc;
2073}
Oliver Swedef9982442018-08-24 18:37:44 +01002074#endif /* MCUBOOT_NO_SWAP || MCUBOOT_RAM_LOADING */