blob: a24e332122107cfe05dc1636b2d632261eab9ca9 [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;
David Vinczebb207982019-08-21 15:13:04 +020050uint8_t current_image = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +000051
Oliver Swedef9982442018-08-24 18:37:44 +010052#if !defined(MCUBOOT_NO_SWAP) && !defined(MCUBOOT_RAM_LOADING)
David Vincze39e78552018-10-10 17:10:01 +020053
David Vincze8bdfc2d2019-03-18 15:49:23 +010054#if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT) && !defined(MCUBOOT_OVERWRITE_ONLY)
David Vincze39e78552018-10-10 17:10:01 +020055static int boot_status_fails = 0;
56#define BOOT_STATUS_ASSERT(x) \
57 do { \
58 if (!(x)) { \
59 boot_status_fails++; \
60 } \
61 } while (0)
62#else
David Vincze401c7422019-06-21 20:44:05 +020063#define BOOT_STATUS_ASSERT(x) ASSERT(x)
David Vincze39e78552018-10-10 17:10:01 +020064#endif
65
Tamas Banf70ef8c2017-12-19 15:35:09 +000066struct boot_status_table {
David Vincze8bdfc2d2019-03-18 15:49:23 +010067 uint8_t bst_magic_primary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +000068 uint8_t bst_magic_scratch;
David Vincze8bdfc2d2019-03-18 15:49:23 +010069 uint8_t bst_copy_done_primary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +000070 uint8_t bst_status_source;
71};
72
73/**
74 * This set of tables maps swap state contents to boot status location.
75 * When searching for a match, these tables must be iterated in order.
76 */
77static const struct boot_status_table boot_status_tables[] = {
78 {
David Vincze8bdfc2d2019-03-18 15:49:23 +010079 /* | primary slot | scratch |
80 * ----------+--------------+--------------|
81 * magic | Good | Any |
82 * copy-done | Set | N/A |
83 * ----------+--------------+--------------'
84 * source: none |
85 * ----------------------------------------'
Tamas Banf70ef8c2017-12-19 15:35:09 +000086 */
David Vincze8bdfc2d2019-03-18 15:49:23 +010087 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
David Vincze401c7422019-06-21 20:44:05 +020088 .bst_magic_scratch = BOOT_MAGIC_NOTGOOD,
David Vincze8bdfc2d2019-03-18 15:49:23 +010089 .bst_copy_done_primary_slot = BOOT_FLAG_SET,
90 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
Tamas Banf70ef8c2017-12-19 15:35:09 +000091 },
92
93 {
David Vincze8bdfc2d2019-03-18 15:49:23 +010094 /* | primary slot | scratch |
95 * ----------+--------------+--------------|
96 * magic | Good | Any |
97 * copy-done | Unset | N/A |
98 * ----------+--------------+--------------'
99 * source: primary slot |
100 * ----------------------------------------'
Tamas Banf70ef8c2017-12-19 15:35:09 +0000101 */
David Vincze8bdfc2d2019-03-18 15:49:23 +0100102 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
David Vincze401c7422019-06-21 20:44:05 +0200103 .bst_magic_scratch = BOOT_MAGIC_NOTGOOD,
David Vincze8bdfc2d2019-03-18 15:49:23 +0100104 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
105 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Tamas Banf70ef8c2017-12-19 15:35:09 +0000106 },
107
108 {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100109 /* | primary slot | scratch |
110 * ----------+--------------+--------------|
111 * magic | Any | Good |
112 * copy-done | Any | N/A |
113 * ----------+--------------+--------------'
114 * source: scratch |
115 * ----------------------------------------'
Tamas Banf70ef8c2017-12-19 15:35:09 +0000116 */
David Vincze8bdfc2d2019-03-18 15:49:23 +0100117 .bst_magic_primary_slot = BOOT_MAGIC_ANY,
118 .bst_magic_scratch = BOOT_MAGIC_GOOD,
119 .bst_copy_done_primary_slot = BOOT_FLAG_ANY,
120 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
Tamas Banf70ef8c2017-12-19 15:35:09 +0000121 },
122
123 {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100124 /* | primary slot | scratch |
125 * ----------+--------------+--------------|
126 * magic | Unset | Any |
127 * copy-done | Unset | N/A |
128 * ----------+--------------+--------------|
129 * source: varies |
130 * ----------------------------------------+--------------------------+
Tamas Banf70ef8c2017-12-19 15:35:09 +0000131 * This represents one of two cases: |
132 * o No swaps ever (no status to read, so no harm in checking). |
David Vincze8bdfc2d2019-03-18 15:49:23 +0100133 * o Mid-revert; status in the primary slot. |
Tamas Banf70ef8c2017-12-19 15:35:09 +0000134 * -------------------------------------------------------------------'
135 */
David Vincze8bdfc2d2019-03-18 15:49:23 +0100136 .bst_magic_primary_slot = BOOT_MAGIC_UNSET,
137 .bst_magic_scratch = BOOT_MAGIC_ANY,
138 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
139 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Tamas Banf70ef8c2017-12-19 15:35:09 +0000140 },
141};
142
143#define BOOT_STATUS_TABLES_COUNT \
Tamas Ban581034a2017-12-19 19:54:37 +0000144 (sizeof(boot_status_tables) / sizeof(boot_status_tables[0]))
Tamas Banf70ef8c2017-12-19 15:35:09 +0000145
146#define BOOT_LOG_SWAP_STATE(area, state) \
David Vincze401c7422019-06-21 20:44:05 +0200147 BOOT_LOG_INF("%s: magic=%5s, swap_type=0x%x, copy_done=0x%x, " \
148 "image_ok=0x%x", \
Tamas Banf70ef8c2017-12-19 15:35:09 +0000149 (area), \
150 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
151 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
152 "bad"), \
David Vincze401c7422019-06-21 20:44:05 +0200153 (state)->swap_type, \
Tamas Banf70ef8c2017-12-19 15:35:09 +0000154 (state)->copy_done, \
155 (state)->image_ok)
Oliver Swedef9982442018-08-24 18:37:44 +0100156#endif /* !MCUBOOT_NO_SWAP && !MCUBOOT_RAM_LOADING */
Tamas Banf70ef8c2017-12-19 15:35:09 +0000157
Tamas Ban056ed0b2019-09-16 12:48:32 +0100158/*
159 * \brief Verifies the image header: magic value, flags, integer overflow.
160 *
161 * \retval 0
162 * \retval BOOT_EBADIMAGE
163 */
164static int
165boot_verify_image_header(struct image_header *hdr)
166{
167 uint32_t image_end;
168
169 if (hdr->ih_magic != IMAGE_MAGIC) {
170 return BOOT_EBADIMAGE;
171 }
172
173 /* Check input parameters against integer overflow */
174 if (boot_add_uint32_overflow_check(hdr->ih_hdr_size, hdr->ih_img_size)) {
175 return BOOT_EBADIMAGE;
176 }
177
178 image_end = hdr->ih_hdr_size + hdr->ih_img_size;
179 if (boot_add_uint32_overflow_check(image_end, hdr->ih_protect_tlv_size)) {
180 return BOOT_EBADIMAGE;
181 }
182
183
184#if MCUBOOT_RAM_LOADING
185 if (!(hdr->ih_flags & IMAGE_F_RAM_LOAD)) {
186 return BOOT_EBADIMAGE;
187 }
188
189 /* Check input parameters against integer overflow */
190 if (boot_add_uint32_overflow_check(image_end, hdr->ih_load_addr)) {
191 return BOOT_EBADIMAGE;
192 }
193#endif
194
195 return 0;
196}
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000197
198static int
199boot_read_image_header(int slot, struct image_header *out_hdr)
200{
201 const struct flash_area *fap = NULL;
202 int area_id;
203 int rc;
204
205 area_id = flash_area_id_from_image_slot(slot);
206 rc = flash_area_open(area_id, &fap);
207 if (rc != 0) {
208 rc = BOOT_EFLASH;
209 goto done;
210 }
211
212 rc = flash_area_read(fap, 0, out_hdr, sizeof(*out_hdr));
213 if (rc != 0) {
214 rc = BOOT_EFLASH;
215 goto done;
216 }
217
Tamas Ban056ed0b2019-09-16 12:48:32 +0100218 rc = boot_verify_image_header(out_hdr);
219 BOOT_IMG_HDR_IS_VALID(&boot_data, slot) = (rc == 0);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000220
221done:
222 flash_area_close(fap);
223 return rc;
224}
225
226static int
David Vincze39e78552018-10-10 17:10:01 +0200227boot_read_image_headers(bool require_all)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000228{
229 int rc;
230 int i;
231
232 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
233 rc = boot_read_image_header(i, boot_img_hdr(&boot_data, i));
234 if (rc != 0) {
David Vincze39e78552018-10-10 17:10:01 +0200235 /* If `require_all` is set, fail on any single fail, otherwise
236 * if at least the first slot's header was read successfully,
237 * then the boot loader can attempt a boot.
238 *
239 * Failure to read any headers is a fatal error.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000240 */
David Vincze39e78552018-10-10 17:10:01 +0200241 if (i > 0 && !require_all) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000242 return 0;
243 } else {
244 return rc;
245 }
246 }
247 }
248
249 return 0;
250}
251
Raef Coles204c5b42019-09-05 09:18:47 +0100252static uint32_t
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000253boot_write_sz(void)
254{
Raef Coles204c5b42019-09-05 09:18:47 +0100255 uint32_t elem_sz;
256 uint32_t align;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000257
258 /* Figure out what size to write update status update as. The size depends
259 * on what the minimum write size is for scratch area, active image slot.
260 * We need to use the bigger of those 2 values.
261 */
David Vincze7384ee72019-07-23 17:00:42 +0200262 elem_sz = flash_area_align(BOOT_IMG_AREA(&boot_data, BOOT_PRIMARY_SLOT));
263 align = flash_area_align(BOOT_SCRATCH_AREA(&boot_data));
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000264 if (align > elem_sz) {
265 elem_sz = align;
266 }
267
268 return elem_sz;
269}
270
271/**
272 * Determines the sector layout of both image slots and the scratch area.
273 * This information is necessary for calculating the number of bytes to erase
274 * and copy during an image swap. The information collected during this
275 * function is used to populate the boot_data global.
276 */
277static int
278boot_read_sectors(void)
279{
280 int rc;
281
David Vincze8bdfc2d2019-03-18 15:49:23 +0100282 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_PRIMARY);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000283 if (rc != 0) {
284 return BOOT_EFLASH;
285 }
286
David Vincze8bdfc2d2019-03-18 15:49:23 +0100287 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_SECONDARY);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000288 if (rc != 0) {
289 return BOOT_EFLASH;
290 }
291
David Vincze401c7422019-06-21 20:44:05 +0200292 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_SCRATCH);
293 if (rc != 0) {
294 return BOOT_EFLASH;
295 }
296
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000297 BOOT_WRITE_SZ(&boot_data) = boot_write_sz();
298
299 return 0;
300}
301
David Vincze060968d2019-05-23 01:13:14 +0200302/**
303 * Validate image hash/signature and security counter in a slot.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000304 */
305static int
David Vincze401c7422019-06-21 20:44:05 +0200306boot_image_check(struct image_header *hdr, const struct flash_area *fap,
307 struct boot_status *bs)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000308{
309 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
310
David Vincze401c7422019-06-21 20:44:05 +0200311 (void)bs;
312
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000313 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
Tamas Ban0e8ab302019-01-17 11:45:31 +0000314 NULL, 0, NULL)) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000315 return BOOT_EBADIMAGE;
316 }
317 return 0;
318}
319
David Vincze401c7422019-06-21 20:44:05 +0200320/*
321 * Check that a memory area consists of a given value.
322 */
323static inline bool
324boot_data_is_set_to(uint8_t val, void *data, size_t len)
David Vincze39e78552018-10-10 17:10:01 +0200325{
326 uint8_t i;
David Vincze401c7422019-06-21 20:44:05 +0200327 uint8_t *p = (uint8_t *)data;
328 for (i = 0; i < len; i++) {
329 if (val != p[i]) {
330 return false;
David Vincze39e78552018-10-10 17:10:01 +0200331 }
332 }
David Vincze401c7422019-06-21 20:44:05 +0200333 return true;
David Vincze39e78552018-10-10 17:10:01 +0200334}
335
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000336static int
David Vincze401c7422019-06-21 20:44:05 +0200337boot_check_header_erased(int slot)
338{
339 const struct flash_area *fap;
340 struct image_header *hdr;
341 uint8_t erased_val;
342 int rc;
343
344 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
345 if (rc != 0) {
346 return -1;
347 }
348
349 erased_val = flash_area_erased_val(fap);
350 flash_area_close(fap);
351
352 hdr = boot_img_hdr(&boot_data, slot);
353 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic,
354 sizeof(hdr->ih_magic))) {
355 return -1;
356 }
357
358 return 0;
359}
360
361static int
362boot_validate_slot(int slot, struct boot_status *bs)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000363{
364 const struct flash_area *fap;
365 struct image_header *hdr;
366 int rc;
367
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000368 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
369 if (rc != 0) {
370 return BOOT_EFLASH;
371 }
372
David Vincze39e78552018-10-10 17:10:01 +0200373 hdr = boot_img_hdr(&boot_data, slot);
David Vincze401c7422019-06-21 20:44:05 +0200374 if ((boot_check_header_erased(slot) == 0) ||
375 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100376 /* No bootable image in slot; continue booting from the primary slot. */
David Vincze401c7422019-06-21 20:44:05 +0200377 rc = -1;
378 goto out;
David Vincze39e78552018-10-10 17:10:01 +0200379 }
380
Tamas Ban056ed0b2019-09-16 12:48:32 +0100381 if ((!BOOT_IMG_HDR_IS_VALID(&boot_data, slot)) ||
382 (boot_image_check(hdr, fap, bs) != 0)) {
David Vincze401c7422019-06-21 20:44:05 +0200383 if (slot != BOOT_PRIMARY_SLOT) {
David Vincze26e8c8a2018-08-28 16:59:41 +0200384 rc = flash_area_erase(fap, 0, fap->fa_size);
385 if(rc != 0) {
David Vincze401c7422019-06-21 20:44:05 +0200386 rc = BOOT_EFLASH;
387 goto out;
David Vincze26e8c8a2018-08-28 16:59:41 +0200388 }
David Vincze8bdfc2d2019-03-18 15:49:23 +0100389 /* Image in the secondary slot is invalid. Erase the image and
390 * continue booting from the primary slot.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000391 */
392 }
David Vincze8bdfc2d2019-03-18 15:49:23 +0100393 BOOT_LOG_ERR("Authentication failed! Image in the %s slot is not valid."
394 , (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Vincze401c7422019-06-21 20:44:05 +0200395 rc = -1;
396 goto out;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000397 }
398
David Vincze8bdfc2d2019-03-18 15:49:23 +0100399 /* Image in the secondary slot is valid. */
David Vincze401c7422019-06-21 20:44:05 +0200400 rc = 0;
401
402out:
403 flash_area_close(fap);
404 return rc;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000405}
406
David Vincze060968d2019-05-23 01:13:14 +0200407/**
408 * Updates the stored security counter value with the image's security counter
409 * value which resides in the given slot if it's greater than the stored value.
410 *
411 * @param slot Slot number of the image.
412 * @param hdr Pointer to the image header structure of the image that is
413 * currently stored in the given slot.
414 *
415 * @return 0 on success; nonzero on failure.
416 */
417static int
418boot_update_security_counter(int slot, struct image_header *hdr)
419{
420 const struct flash_area *fap = NULL;
421 uint32_t img_security_cnt;
422 int rc;
423
424 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
425 if (rc != 0) {
426 rc = BOOT_EFLASH;
427 goto done;
428 }
429
430 rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt);
431 if (rc != 0) {
432 goto done;
433 }
434
David Vincze0b41ee22019-06-28 14:28:20 +0200435 rc = boot_nv_security_counter_update(current_image, img_security_cnt);
David Vincze060968d2019-05-23 01:13:14 +0200436 if (rc != 0) {
437 goto done;
438 }
439
440done:
441 flash_area_close(fap);
442 return rc;
443}
444
Oliver Swedef9982442018-08-24 18:37:44 +0100445#if !defined(MCUBOOT_NO_SWAP) && !defined(MCUBOOT_OVERWRITE_ONLY)
446/*
447 * Compute the total size of the given image. Includes the size of
448 * the TLVs.
449 */
450static int
451boot_read_image_size(int slot, struct image_header *hdr, uint32_t *size)
452{
453 const struct flash_area *fap = NULL;
454 struct image_tlv_info info;
455 int area_id;
456 int rc;
457
458 area_id = flash_area_id_from_image_slot(slot);
459 rc = flash_area_open(area_id, &fap);
460 if (rc != 0) {
461 rc = BOOT_EFLASH;
462 goto done;
463 }
464
465 rc = flash_area_read(fap, hdr->ih_hdr_size + hdr->ih_img_size,
466 &info, sizeof(info));
467 if (rc != 0) {
468 rc = BOOT_EFLASH;
469 goto done;
470 }
471 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
472 rc = BOOT_EBADIMAGE;
473 goto done;
474 }
475 *size = hdr->ih_hdr_size + hdr->ih_img_size + info.it_tlv_tot;
476 rc = 0;
477
478done:
479 flash_area_close(fap);
480 return rc;
481}
482#endif /* !MCUBOOT_NO_SWAP && !MCUBOOT_OVERWRITE_ONLY */
483
484#if !defined(MCUBOOT_NO_SWAP) && !defined(MCUBOOT_RAM_LOADING)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000485/**
Tamas Ban581034a2017-12-19 19:54:37 +0000486 * Determines where in flash the most recent boot status is stored. The boot
Tamas Banf70ef8c2017-12-19 15:35:09 +0000487 * status is necessary for completing a swap that was interrupted by a boot
488 * loader reset.
489 *
David Vincze401c7422019-06-21 20:44:05 +0200490 * @return A BOOT_STATUS_SOURCE_[...] code indicating where status should
491 * be read from.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000492 */
493static int
494boot_status_source(void)
495{
496 const struct boot_status_table *table;
497 struct boot_swap_state state_scratch;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100498 struct boot_swap_state state_primary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000499 int rc;
David Vincze39e78552018-10-10 17:10:01 +0200500 size_t i;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000501 uint8_t source;
502
David Vincze8bdfc2d2019-03-18 15:49:23 +0100503 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY,
504 &state_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000505 assert(rc == 0);
506
507 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
508 assert(rc == 0);
509
David Vincze401c7422019-06-21 20:44:05 +0200510 BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000511 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
512
513 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
514 table = &boot_status_tables[i];
515
David Vincze401c7422019-06-21 20:44:05 +0200516 if (boot_magic_compatible_check(table->bst_magic_primary_slot,
517 state_primary_slot.magic) &&
518 boot_magic_compatible_check(table->bst_magic_scratch,
519 state_scratch.magic) &&
David Vincze8bdfc2d2019-03-18 15:49:23 +0100520 (table->bst_copy_done_primary_slot == BOOT_FLAG_ANY ||
521 table->bst_copy_done_primary_slot == state_primary_slot.copy_done))
522 {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000523 source = table->bst_status_source;
David Vincze7384ee72019-07-23 17:00:42 +0200524
525#if (BOOT_IMAGE_NUMBER > 1)
526 /* In case of multi-image boot it can happen that if boot status
527 * info is found on scratch area then it does not belong to the
528 * currently examined image.
529 */
530 if (source == BOOT_STATUS_SOURCE_SCRATCH &&
531 state_scratch.image_num != current_image) {
532 source = BOOT_STATUS_SOURCE_NONE;
533 }
534#endif
535
Tamas Banf70ef8c2017-12-19 15:35:09 +0000536 BOOT_LOG_INF("Boot source: %s",
537 source == BOOT_STATUS_SOURCE_NONE ? "none" :
538 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
David Vincze8bdfc2d2019-03-18 15:49:23 +0100539 source == BOOT_STATUS_SOURCE_PRIMARY_SLOT ?
540 "primary slot" : "BUG; can't happen");
Tamas Banf70ef8c2017-12-19 15:35:09 +0000541 return source;
542 }
543 }
544
545 BOOT_LOG_INF("Boot source: none");
546 return BOOT_STATUS_SOURCE_NONE;
547}
548
David Vincze401c7422019-06-21 20:44:05 +0200549/*
550 * Slots are compatible when all sectors that store upto to size of the image
551 * round up to sector size, in both slot's are able to fit in the scratch
552 * area, and have sizes that are a multiple of each other (powers of two
553 * presumably!).
Tamas Banf70ef8c2017-12-19 15:35:09 +0000554 */
555static int
Tamas Banf70ef8c2017-12-19 15:35:09 +0000556boot_slots_compatible(void)
557{
David Vincze401c7422019-06-21 20:44:05 +0200558 size_t num_sectors_primary;
559 size_t num_sectors_secondary;
560 size_t sz0, sz1;
561 size_t primary_slot_sz, secondary_slot_sz;
562 size_t scratch_sz;
563 size_t i, j;
564 int8_t smaller;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000565
David Vincze401c7422019-06-21 20:44:05 +0200566 num_sectors_primary =
567 boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT);
568 num_sectors_secondary =
569 boot_img_num_sectors(&boot_data, BOOT_SECONDARY_SLOT);
570 if ((num_sectors_primary > BOOT_MAX_IMG_SECTORS) ||
571 (num_sectors_secondary > BOOT_MAX_IMG_SECTORS)) {
David Vincze39e78552018-10-10 17:10:01 +0200572 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
Tamas Banf70ef8c2017-12-19 15:35:09 +0000573 return 0;
574 }
David Vincze39e78552018-10-10 17:10:01 +0200575
David Vincze401c7422019-06-21 20:44:05 +0200576 scratch_sz = boot_scratch_area_size(&boot_data);
577
578 /*
579 * The following loop scans all sectors in a linear fashion, assuring that
580 * for each possible sector in each slot, it is able to fit in the other
581 * slot's sector or sectors. Slot's should be compatible as long as any
582 * number of a slot's sectors are able to fit into another, which only
583 * excludes cases where sector sizes are not a multiple of each other.
584 */
585 i = sz0 = primary_slot_sz = 0;
586 j = sz1 = secondary_slot_sz = 0;
587 smaller = 0;
588 while (i < num_sectors_primary || j < num_sectors_secondary) {
589 if (sz0 == sz1) {
590 sz0 += boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
591 sz1 += boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT, j);
592 i++;
593 j++;
594 } else if (sz0 < sz1) {
595 sz0 += boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
596 /* Guarantee that multiple sectors of the secondary slot
597 * fit into the primary slot.
598 */
599 if (smaller == 2) {
600 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible"
601 " sectors");
602 return 0;
603 }
604 smaller = 1;
605 i++;
606 } else {
607 sz1 += boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT, j);
608 /* Guarantee that multiple sectors of the primary slot
609 * fit into the secondary slot.
610 */
611 if (smaller == 1) {
612 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible"
613 " sectors");
614 return 0;
615 }
616 smaller = 2;
617 j++;
618 }
619 if (sz0 == sz1) {
620 primary_slot_sz += sz0;
621 secondary_slot_sz += sz1;
622 /* Scratch has to fit each swap operation to the size of the larger
623 * sector among the primary slot and the secondary slot.
624 */
625 if (sz0 > scratch_sz || sz1 > scratch_sz) {
626 BOOT_LOG_WRN("Cannot upgrade: not all sectors fit inside"
627 " scratch");
628 return 0;
629 }
630 smaller = sz0 = sz1 = 0;
631 }
David Vincze39e78552018-10-10 17:10:01 +0200632 }
633
David Vincze401c7422019-06-21 20:44:05 +0200634 if ((i != num_sectors_primary) ||
635 (j != num_sectors_secondary) ||
636 (primary_slot_sz != secondary_slot_sz)) {
637 BOOT_LOG_WRN("Cannot upgrade: slots are not compatible");
638 return 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000639 }
640
641 return 1;
642}
643
Tamas Banf70ef8c2017-12-19 15:35:09 +0000644static uint32_t
645boot_status_internal_off(int idx, int state, int elem_sz)
646{
647 int idx_sz;
648
649 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
650
David Vincze39e78552018-10-10 17:10:01 +0200651 return (idx - BOOT_STATUS_IDX_0) * idx_sz +
652 (state - BOOT_STATUS_STATE_0) * elem_sz;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000653}
654
655/**
656 * Reads the status of a partially-completed swap, if any. This is necessary
657 * to recover in case the boot lodaer was reset in the middle of a swap
658 * operation.
659 */
660static int
661boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs)
662{
663 uint32_t off;
664 uint8_t status;
665 int max_entries;
666 int found;
David Vincze39e78552018-10-10 17:10:01 +0200667 int found_idx;
668 int invalid;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000669 int rc;
670 int i;
671
672 off = boot_status_off(fap);
673 max_entries = boot_status_entries(fap);
674
675 found = 0;
David Vincze39e78552018-10-10 17:10:01 +0200676 found_idx = 0;
677 invalid = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000678 for (i = 0; i < max_entries; i++) {
David Vincze39e78552018-10-10 17:10:01 +0200679 rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(&boot_data),
680 &status, 1);
681 if (rc < 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000682 return BOOT_EFLASH;
683 }
684
David Vincze39e78552018-10-10 17:10:01 +0200685 if (rc == 1) {
686 if (found && !found_idx) {
687 found_idx = i;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000688 }
689 } else if (!found) {
690 found = 1;
David Vincze39e78552018-10-10 17:10:01 +0200691 } else if (found_idx) {
692 invalid = 1;
693 break;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000694 }
695 }
696
David Vincze39e78552018-10-10 17:10:01 +0200697 if (invalid) {
698 /* This means there was an error writing status on the last
699 * swap. Tell user and move on to validation!
700 */
701 BOOT_LOG_ERR("Detected inconsistent status!");
702
David Vincze8bdfc2d2019-03-18 15:49:23 +0100703#if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
704 /* With validation of the primary slot disabled, there is no way
705 * to be sure the swapped primary slot is OK, so abort!
David Vincze39e78552018-10-10 17:10:01 +0200706 */
707 assert(0);
708#endif
709 }
710
Tamas Banf70ef8c2017-12-19 15:35:09 +0000711 if (found) {
David Vincze39e78552018-10-10 17:10:01 +0200712 if (!found_idx) {
713 found_idx = i;
714 }
715 found_idx--;
716 bs->idx = (found_idx / BOOT_STATUS_STATE_COUNT) + 1;
717 bs->state = (found_idx % BOOT_STATUS_STATE_COUNT) + 1;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000718 }
719
720 return 0;
721}
722
723/**
724 * Reads the boot status from the flash. The boot status contains
725 * the current state of an interrupted image copy operation. If the boot
726 * status is not present, or it indicates that previous copy finished,
727 * there is no operation in progress.
728 */
729static int
730boot_read_status(struct boot_status *bs)
731{
732 const struct flash_area *fap;
David Vincze401c7422019-06-21 20:44:05 +0200733 uint32_t off;
David Vincze91b71ef2019-06-24 13:06:47 +0200734 uint8_t swap_info;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000735 int status_loc;
736 int area_id;
737 int rc;
738
David Vincze39e78552018-10-10 17:10:01 +0200739 memset(bs, 0, sizeof *bs);
740 bs->idx = BOOT_STATUS_IDX_0;
741 bs->state = BOOT_STATUS_STATE_0;
David Vincze401c7422019-06-21 20:44:05 +0200742 bs->swap_type = BOOT_SWAP_TYPE_NONE;
David Vincze39e78552018-10-10 17:10:01 +0200743
744#ifdef MCUBOOT_OVERWRITE_ONLY
745 /* Overwrite-only doesn't make use of the swap status area. */
746 return 0;
747#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +0000748
749 status_loc = boot_status_source();
750 switch (status_loc) {
751 case BOOT_STATUS_SOURCE_NONE:
752 return 0;
753
754 case BOOT_STATUS_SOURCE_SCRATCH:
755 area_id = FLASH_AREA_IMAGE_SCRATCH;
756 break;
757
David Vincze8bdfc2d2019-03-18 15:49:23 +0100758 case BOOT_STATUS_SOURCE_PRIMARY_SLOT:
759 area_id = FLASH_AREA_IMAGE_PRIMARY;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000760 break;
761
762 default:
763 assert(0);
764 return BOOT_EBADARGS;
765 }
766
767 rc = flash_area_open(area_id, &fap);
768 if (rc != 0) {
769 return BOOT_EFLASH;
770 }
771
772 rc = boot_read_status_bytes(fap, bs);
David Vincze401c7422019-06-21 20:44:05 +0200773 if (rc == 0) {
David Vincze91b71ef2019-06-24 13:06:47 +0200774 off = boot_swap_info_off(fap);
775 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
David Vincze401c7422019-06-21 20:44:05 +0200776 if (rc == 1) {
David Vincze91b71ef2019-06-24 13:06:47 +0200777 BOOT_SET_SWAP_INFO(swap_info, 0, BOOT_SWAP_TYPE_NONE);
David Vincze401c7422019-06-21 20:44:05 +0200778 rc = 0;
779 }
David Vincze91b71ef2019-06-24 13:06:47 +0200780
781 /* Extract the swap type info */
782 bs->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
David Vincze401c7422019-06-21 20:44:05 +0200783 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000784
785 flash_area_close(fap);
David Vincze39e78552018-10-10 17:10:01 +0200786
Tamas Banf70ef8c2017-12-19 15:35:09 +0000787 return rc;
788}
789
790/**
791 * Writes the supplied boot status to the flash file system. The boot status
792 * contains the current state of an in-progress image copy operation.
793 *
794 * @param bs The boot status to write.
795 *
796 * @return 0 on success; nonzero on failure.
797 */
798int
799boot_write_status(struct boot_status *bs)
800{
Tamas Ban581034a2017-12-19 19:54:37 +0000801 const struct flash_area *fap = NULL;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000802 uint32_t off;
803 int area_id;
804 int rc;
805 uint8_t buf[BOOT_MAX_ALIGN];
Raef Coles204c5b42019-09-05 09:18:47 +0100806 uint32_t align;
David Vincze39e78552018-10-10 17:10:01 +0200807 uint8_t erased_val;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000808
809 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze8bdfc2d2019-03-18 15:49:23 +0100810 * the trailer. Since in the last step the primary slot is erased, the
811 * first two status writes go to the scratch which will be copied to
812 * the primary slot!
Tamas Banf70ef8c2017-12-19 15:35:09 +0000813 */
814
815 if (bs->use_scratch) {
816 /* Write to scratch. */
817 area_id = FLASH_AREA_IMAGE_SCRATCH;
818 } else {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100819 /* Write to the primary slot. */
820 area_id = FLASH_AREA_IMAGE_PRIMARY;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000821 }
822
823 rc = flash_area_open(area_id, &fap);
824 if (rc != 0) {
825 rc = BOOT_EFLASH;
826 goto done;
827 }
828
829 off = boot_status_off(fap) +
830 boot_status_internal_off(bs->idx, bs->state,
831 BOOT_WRITE_SZ(&boot_data));
832
Tamas Banc3828852018-02-01 12:24:16 +0000833 align = flash_area_align(fap);
David Vincze39e78552018-10-10 17:10:01 +0200834 erased_val = flash_area_erased_val(fap);
835 memset(buf, erased_val, BOOT_MAX_ALIGN);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000836 buf[0] = bs->state;
837
838 rc = flash_area_write(fap, off, buf, align);
839 if (rc != 0) {
840 rc = BOOT_EFLASH;
841 goto done;
842 }
843
844 rc = 0;
845
846done:
847 flash_area_close(fap);
848 return rc;
849}
850
Tamas Banf70ef8c2017-12-19 15:35:09 +0000851/**
852 * Determines which swap operation to perform, if any. If it is determined
David Vincze8bdfc2d2019-03-18 15:49:23 +0100853 * that a swap operation is required, the image in the secondary slot is checked
854 * for validity. If the image in the secondary slot is invalid, it is erased,
855 * and a swap type of "none" is indicated.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000856 *
857 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
858 */
859static int
David Vincze401c7422019-06-21 20:44:05 +0200860boot_validated_swap_type(struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000861{
862 int swap_type;
863
864 swap_type = boot_swap_type();
865 switch (swap_type) {
866 case BOOT_SWAP_TYPE_TEST:
867 case BOOT_SWAP_TYPE_PERM:
868 case BOOT_SWAP_TYPE_REVERT:
David Vincze8bdfc2d2019-03-18 15:49:23 +0100869 /* Boot loader wants to switch to the secondary slot.
870 * Ensure image is valid.
871 */
David Vincze401c7422019-06-21 20:44:05 +0200872 if (boot_validate_slot(BOOT_SECONDARY_SLOT, bs) != 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000873 swap_type = BOOT_SWAP_TYPE_FAIL;
874 }
875 }
876
877 return swap_type;
878}
879
880/**
881 * Calculates the number of sectors the scratch area can contain. A "last"
882 * source sector is specified because images are copied backwards in flash
883 * (final index to index number 0).
884 *
885 * @param last_sector_idx The index of the last source sector
886 * (inclusive).
887 * @param out_first_sector_idx The index of the first source sector
888 * (inclusive) gets written here.
889 *
890 * @return The number of bytes comprised by the
891 * [first-sector, last-sector] range.
892 */
893#ifndef MCUBOOT_OVERWRITE_ONLY
894static uint32_t
895boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
896{
897 size_t scratch_sz;
898 uint32_t new_sz;
899 uint32_t sz;
900 int i;
901
902 sz = 0;
903
904 scratch_sz = boot_scratch_area_size(&boot_data);
905 for (i = last_sector_idx; i >= 0; i--) {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100906 new_sz = sz + boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
David Vincze401c7422019-06-21 20:44:05 +0200907 /*
908 * The secondary slot is not being checked here, because
909 * `boot_slots_compatible` already provides assurance that the copy size
910 * will be compatible with the primary slot and scratch.
911 */
Tamas Banf70ef8c2017-12-19 15:35:09 +0000912 if (new_sz > scratch_sz) {
913 break;
914 }
915 sz = new_sz;
916 }
917
918 /* i currently refers to a sector that doesn't fit or it is -1 because all
919 * sectors have been processed. In both cases, exclude sector i.
920 */
921 *out_first_sector_idx = i + 1;
922 return sz;
923}
924#endif /* !MCUBOOT_OVERWRITE_ONLY */
925
926/**
David Vinczef7641fa2018-09-04 18:29:46 +0200927 * Erases a region of flash.
928 *
David Vincze401c7422019-06-21 20:44:05 +0200929 * @param flash_area The flash_area containing the region to erase.
David Vinczef7641fa2018-09-04 18:29:46 +0200930 * @param off The offset within the flash area to start the
931 * erase.
932 * @param sz The number of bytes to erase.
933 *
934 * @return 0 on success; nonzero on failure.
935 */
David Vincze401c7422019-06-21 20:44:05 +0200936static inline int
937boot_erase_sector(const struct flash_area *fap, uint32_t off, uint32_t sz)
David Vinczef7641fa2018-09-04 18:29:46 +0200938{
David Vincze401c7422019-06-21 20:44:05 +0200939 return flash_area_erase(fap, off, sz);
David Vinczef7641fa2018-09-04 18:29:46 +0200940}
941
942/**
Tamas Banf70ef8c2017-12-19 15:35:09 +0000943 * Copies the contents of one flash region to another. You must erase the
944 * destination region prior to calling this function.
945 *
946 * @param flash_area_id_src The ID of the source flash area.
947 * @param flash_area_id_dst The ID of the destination flash area.
948 * @param off_src The offset within the source flash area to
949 * copy from.
950 * @param off_dst The offset within the destination flash area to
951 * copy to.
952 * @param sz The number of bytes to copy.
953 *
954 * @return 0 on success; nonzero on failure.
955 */
956static int
David Vincze401c7422019-06-21 20:44:05 +0200957boot_copy_sector(const struct flash_area *fap_src,
958 const struct flash_area *fap_dst,
Tamas Banf70ef8c2017-12-19 15:35:09 +0000959 uint32_t off_src, uint32_t off_dst, uint32_t sz)
960{
Tamas Banf70ef8c2017-12-19 15:35:09 +0000961 uint32_t bytes_copied;
962 int chunk_sz;
963 int rc;
964
965 static uint8_t buf[1024];
966
Tamas Banf70ef8c2017-12-19 15:35:09 +0000967 bytes_copied = 0;
968 while (bytes_copied < sz) {
Tamas Ban581034a2017-12-19 19:54:37 +0000969 if (sz - bytes_copied > sizeof(buf)) {
970 chunk_sz = sizeof(buf);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000971 } else {
972 chunk_sz = sz - bytes_copied;
973 }
974
975 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
976 if (rc != 0) {
David Vincze401c7422019-06-21 20:44:05 +0200977 return BOOT_EFLASH;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000978 }
979
980 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
981 if (rc != 0) {
David Vincze401c7422019-06-21 20:44:05 +0200982 return BOOT_EFLASH;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000983 }
984
985 bytes_copied += chunk_sz;
986 }
987
David Vincze401c7422019-06-21 20:44:05 +0200988 return 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000989}
990
991#ifndef MCUBOOT_OVERWRITE_ONLY
992static inline int
David Vincze401c7422019-06-21 20:44:05 +0200993boot_status_init(const struct flash_area *fap, const struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000994{
Tamas Banf70ef8c2017-12-19 15:35:09 +0000995 struct boot_swap_state swap_state;
996 int rc;
997
David Vincze401c7422019-06-21 20:44:05 +0200998 BOOT_LOG_DBG("initializing status; fa_id=%d", fap->fa_id);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000999
David Vincze8bdfc2d2019-03-18 15:49:23 +01001000 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY, &swap_state);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001001 assert(rc == 0);
1002
David Vincze401c7422019-06-21 20:44:05 +02001003 if (bs->swap_type != BOOT_SWAP_TYPE_NONE) {
David Vincze91b71ef2019-06-24 13:06:47 +02001004 rc = boot_write_swap_info(fap,
1005 bs->swap_type,
David Vincze7384ee72019-07-23 17:00:42 +02001006 current_image);
David Vincze401c7422019-06-21 20:44:05 +02001007 assert(rc == 0);
1008 }
1009
Tamas Banf70ef8c2017-12-19 15:35:09 +00001010 if (swap_state.image_ok == BOOT_FLAG_SET) {
1011 rc = boot_write_image_ok(fap);
1012 assert(rc == 0);
1013 }
1014
1015 rc = boot_write_swap_size(fap, bs->swap_size);
1016 assert(rc == 0);
1017
1018 rc = boot_write_magic(fap);
1019 assert(rc == 0);
1020
Tamas Banf70ef8c2017-12-19 15:35:09 +00001021 return 0;
1022}
David Vinczef7641fa2018-09-04 18:29:46 +02001023
1024static int
David Vincze401c7422019-06-21 20:44:05 +02001025boot_erase_trailer_sectors(const struct flash_area *fap)
David Vinczef7641fa2018-09-04 18:29:46 +02001026{
1027 uint8_t slot;
David Vincze401c7422019-06-21 20:44:05 +02001028 uint32_t sector;
1029 uint32_t trailer_sz;
1030 uint32_t total_sz;
1031 uint32_t off;
1032 uint32_t sz;
David Vinczebb207982019-08-21 15:13:04 +02001033 int fa_id_primary;
1034 int fa_id_secondary;
David Vinczef7641fa2018-09-04 18:29:46 +02001035 int rc;
1036
David Vincze401c7422019-06-21 20:44:05 +02001037 BOOT_LOG_DBG("erasing trailer; fa_id=%d", fap->fa_id);
1038
David Vinczebb207982019-08-21 15:13:04 +02001039 fa_id_primary = flash_area_id_from_image_slot(BOOT_PRIMARY_SLOT);
1040 fa_id_secondary = flash_area_id_from_image_slot(BOOT_SECONDARY_SLOT);
1041
1042 if (fap->fa_id == fa_id_primary) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001043 slot = BOOT_PRIMARY_SLOT;
David Vinczebb207982019-08-21 15:13:04 +02001044 } else if (fap->fa_id == fa_id_secondary) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001045 slot = BOOT_SECONDARY_SLOT;
David Vinczebb207982019-08-21 15:13:04 +02001046 } else {
David Vinczef7641fa2018-09-04 18:29:46 +02001047 return BOOT_EFLASH;
1048 }
1049
David Vincze401c7422019-06-21 20:44:05 +02001050 /* delete starting from last sector and moving to beginning */
1051 sector = boot_img_num_sectors(&boot_data, slot) - 1;
1052 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(&boot_data));
1053 total_sz = 0;
1054 do {
1055 sz = boot_img_sector_size(&boot_data, slot, sector);
1056 off = boot_img_sector_off(&boot_data, slot, sector);
1057 rc = boot_erase_sector(fap, off, sz);
1058 assert(rc == 0);
1059
1060 sector--;
1061 total_sz += sz;
1062 } while (total_sz < trailer_sz);
David Vinczef7641fa2018-09-04 18:29:46 +02001063
1064 return rc;
1065}
1066#endif /* !MCUBOOT_OVERWRITE_ONLY */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001067
Tamas Banf70ef8c2017-12-19 15:35:09 +00001068/**
1069 * Swaps the contents of two flash regions within the two image slots.
1070 *
1071 * @param idx The index of the first sector in the range of
1072 * sectors being swapped.
1073 * @param sz The number of bytes to swap.
1074 * @param bs The current boot status. This struct gets
1075 * updated according to the outcome.
1076 *
1077 * @return 0 on success; nonzero on failure.
1078 */
1079#ifndef MCUBOOT_OVERWRITE_ONLY
1080static void
1081boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
1082{
David Vincze401c7422019-06-21 20:44:05 +02001083 const struct flash_area *fap_primary_slot;
1084 const struct flash_area *fap_secondary_slot;
1085 const struct flash_area *fap_scratch;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001086 uint32_t copy_sz;
1087 uint32_t trailer_sz;
1088 uint32_t img_off;
1089 uint32_t scratch_trailer_off;
1090 struct boot_swap_state swap_state;
1091 size_t last_sector;
David Vincze401c7422019-06-21 20:44:05 +02001092 bool erase_scratch;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001093 int rc;
1094
1095 /* Calculate offset from start of image area. */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001096 img_off = boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT, idx);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001097
1098 copy_sz = sz;
David Vincze401c7422019-06-21 20:44:05 +02001099 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Tamas Banf70ef8c2017-12-19 15:35:09 +00001100
David Vincze401c7422019-06-21 20:44:05 +02001101 /* sz in this function is always sized on a multiple of the sector size.
1102 * The check against the start offset of the last sector
Tamas Banf70ef8c2017-12-19 15:35:09 +00001103 * is to determine if we're swapping the last sector. The last sector
1104 * needs special handling because it's where the trailer lives. If we're
1105 * copying it, we need to use scratch to write the trailer temporarily.
1106 *
1107 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
1108 * controls if special handling is needed (swapping last sector).
1109 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001110 last_sector = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT) - 1;
David Vincze7384ee72019-07-23 17:00:42 +02001111 if ((img_off + sz) >
1112 boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT, last_sector)) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001113 copy_sz -= trailer_sz;
1114 }
1115
David Vincze39e78552018-10-10 17:10:01 +02001116 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001117
David Vincze401c7422019-06-21 20:44:05 +02001118 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap_primary_slot);
1119 assert (rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001120
David Vincze401c7422019-06-21 20:44:05 +02001121 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap_secondary_slot);
1122 assert (rc == 0);
1123
1124 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch);
1125 assert (rc == 0);
1126
1127 if (bs->state == BOOT_STATUS_STATE_0) {
1128 BOOT_LOG_DBG("erasing scratch area");
1129 rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001130 assert(rc == 0);
1131
David Vincze39e78552018-10-10 17:10:01 +02001132 if (bs->idx == BOOT_STATUS_IDX_0) {
David Vincze401c7422019-06-21 20:44:05 +02001133 /* Write a trailer to the scratch area, even if we don't need the
1134 * scratch area for status. We need a temporary place to store the
1135 * `swap-type` while we erase the primary trailer.
1136 */
1137 rc = boot_status_init(fap_scratch, bs);
1138 assert(rc == 0);
1139
1140 if (!bs->use_scratch) {
1141 /* Prepare the primary status area... here it is known that the
1142 * last sector is not being used by the image data so it's safe
1143 * to erase.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001144 */
David Vincze401c7422019-06-21 20:44:05 +02001145 rc = boot_erase_trailer_sectors(fap_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001146 assert(rc == 0);
1147
David Vincze401c7422019-06-21 20:44:05 +02001148 rc = boot_status_init(fap_primary_slot, bs);
1149 assert(rc == 0);
1150
1151 /* Erase the temporary trailer from the scratch area. */
1152 rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size);
1153 assert(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001154 }
1155 }
1156
David Vincze401c7422019-06-21 20:44:05 +02001157 rc = boot_copy_sector(fap_secondary_slot, fap_scratch,
1158 img_off, 0, copy_sz);
1159 assert(rc == 0);
1160
David Vincze39e78552018-10-10 17:10:01 +02001161 bs->state = BOOT_STATUS_STATE_1;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001162 rc = boot_write_status(bs);
David Vincze39e78552018-10-10 17:10:01 +02001163 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001164 }
1165
David Vincze39e78552018-10-10 17:10:01 +02001166 if (bs->state == BOOT_STATUS_STATE_1) {
David Vincze401c7422019-06-21 20:44:05 +02001167 rc = boot_erase_sector(fap_secondary_slot, img_off, sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001168 assert(rc == 0);
1169
David Vincze401c7422019-06-21 20:44:05 +02001170 rc = boot_copy_sector(fap_primary_slot, fap_secondary_slot,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001171 img_off, img_off, copy_sz);
1172 assert(rc == 0);
1173
David Vincze39e78552018-10-10 17:10:01 +02001174 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001175 /* If not all sectors of the slot are being swapped,
David Vincze8bdfc2d2019-03-18 15:49:23 +01001176 * guarantee here that only the primary slot will have the state.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001177 */
David Vincze401c7422019-06-21 20:44:05 +02001178 rc = boot_erase_trailer_sectors(fap_secondary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001179 assert(rc == 0);
1180 }
1181
David Vincze39e78552018-10-10 17:10:01 +02001182 bs->state = BOOT_STATUS_STATE_2;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001183 rc = boot_write_status(bs);
David Vincze39e78552018-10-10 17:10:01 +02001184 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001185 }
1186
David Vincze39e78552018-10-10 17:10:01 +02001187 if (bs->state == BOOT_STATUS_STATE_2) {
David Vincze401c7422019-06-21 20:44:05 +02001188 rc = boot_erase_sector(fap_primary_slot, img_off, sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001189 assert(rc == 0);
1190
David Vincze401c7422019-06-21 20:44:05 +02001191 /* NOTE: If this is the final sector, we exclude the image trailer from
1192 * this copy (copy_sz was truncated earlier).
1193 */
1194 rc = boot_copy_sector(fap_scratch, fap_primary_slot,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001195 0, img_off, copy_sz);
1196 assert(rc == 0);
1197
1198 if (bs->use_scratch) {
David Vincze401c7422019-06-21 20:44:05 +02001199 scratch_trailer_off = boot_status_off(fap_scratch);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001200
1201 /* copy current status that is being maintained in scratch */
David Vincze401c7422019-06-21 20:44:05 +02001202 rc = boot_copy_sector(fap_scratch, fap_primary_slot,
1203 scratch_trailer_off, img_off + copy_sz,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001204 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data));
David Vincze39e78552018-10-10 17:10:01 +02001205 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001206
1207 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1208 &swap_state);
1209 assert(rc == 0);
1210
1211 if (swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze401c7422019-06-21 20:44:05 +02001212 rc = boot_write_image_ok(fap_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001213 assert(rc == 0);
1214 }
1215
David Vincze401c7422019-06-21 20:44:05 +02001216 if (swap_state.swap_type != BOOT_SWAP_TYPE_NONE) {
David Vincze91b71ef2019-06-24 13:06:47 +02001217 rc = boot_write_swap_info(fap_primary_slot,
1218 swap_state.swap_type,
David Vincze7384ee72019-07-23 17:00:42 +02001219 current_image);
David Vincze401c7422019-06-21 20:44:05 +02001220 assert(rc == 0);
1221 }
1222
1223 rc = boot_write_swap_size(fap_primary_slot, bs->swap_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001224 assert(rc == 0);
1225
David Vincze401c7422019-06-21 20:44:05 +02001226 rc = boot_write_magic(fap_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001227 assert(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001228 }
1229
David Vincze401c7422019-06-21 20:44:05 +02001230 /* If we wrote a trailer to the scratch area, erase it after we persist
1231 * a trailer to the primary slot. We do this to prevent mcuboot from
1232 * reading a stale status from the scratch area in case of immediate
1233 * reset.
1234 */
1235 erase_scratch = bs->use_scratch;
1236 bs->use_scratch = 0;
1237
Tamas Banf70ef8c2017-12-19 15:35:09 +00001238 bs->idx++;
David Vincze39e78552018-10-10 17:10:01 +02001239 bs->state = BOOT_STATUS_STATE_0;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001240 rc = boot_write_status(bs);
David Vincze39e78552018-10-10 17:10:01 +02001241 BOOT_STATUS_ASSERT(rc == 0);
David Vincze401c7422019-06-21 20:44:05 +02001242
1243 if (erase_scratch) {
1244 rc = boot_erase_sector(fap_scratch, 0, sz);
1245 assert(rc == 0);
1246 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001247 }
David Vincze401c7422019-06-21 20:44:05 +02001248
1249 flash_area_close(fap_primary_slot);
1250 flash_area_close(fap_secondary_slot);
1251 flash_area_close(fap_scratch);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001252}
1253#endif /* !MCUBOOT_OVERWRITE_ONLY */
1254
1255/**
David Vincze401c7422019-06-21 20:44:05 +02001256 * Overwrite primary slot with the image contained in the secondary slot.
1257 * If a prior copy operation was interrupted by a system reset, this function
1258 * redos the copy.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001259 *
1260 * @param bs The current boot status. This function reads
1261 * this struct to determine if it is resuming
1262 * an interrupted swap operation. This
1263 * function writes the updated status to this
1264 * function on return.
1265 *
1266 * @return 0 on success; nonzero on failure.
1267 */
1268#ifdef MCUBOOT_OVERWRITE_ONLY
1269static int
1270boot_copy_image(struct boot_status *bs)
1271{
1272 size_t sect_count;
1273 size_t sect;
1274 int rc;
1275 size_t size = 0;
1276 size_t this_size;
David Vincze39e78552018-10-10 17:10:01 +02001277 size_t last_sector;
David Vincze401c7422019-06-21 20:44:05 +02001278 const struct flash_area *fap_primary_slot;
1279 const struct flash_area *fap_secondary_slot;
David Vincze39e78552018-10-10 17:10:01 +02001280
1281 (void)bs;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001282
David Vincze8bdfc2d2019-03-18 15:49:23 +01001283 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1284 BOOT_LOG_INF("Erasing the primary slot");
Tamas Banf70ef8c2017-12-19 15:35:09 +00001285
David Vincze401c7422019-06-21 20:44:05 +02001286 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap_primary_slot);
1287 assert (rc == 0);
1288
1289 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap_secondary_slot);
1290 assert (rc == 0);
1291
David Vincze8bdfc2d2019-03-18 15:49:23 +01001292 sect_count = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001293 for (sect = 0; sect < sect_count; sect++) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001294 this_size = boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, sect);
David Vincze401c7422019-06-21 20:44:05 +02001295 rc = boot_erase_sector(fap_primary_slot, size, this_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001296 assert(rc == 0);
1297
1298 size += this_size;
1299 }
1300
David Vincze8bdfc2d2019-03-18 15:49:23 +01001301 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1302 size);
David Vincze401c7422019-06-21 20:44:05 +02001303 rc = boot_copy_sector(fap_secondary_slot, fap_primary_slot, 0, 0, size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001304
David Vincze060968d2019-05-23 01:13:14 +02001305 /* Update the stored security counter with the new image's security counter
David Vincze8bdfc2d2019-03-18 15:49:23 +01001306 * value. Both slots hold the new image at this point, but the secondary
1307 * slot's image header must be passed because the read image headers in the
1308 * boot_data structure have not been updated yet.
David Vincze060968d2019-05-23 01:13:14 +02001309 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001310 rc = boot_update_security_counter(BOOT_PRIMARY_SLOT,
1311 boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT));
David Vincze060968d2019-05-23 01:13:14 +02001312 if (rc != 0) {
1313 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
1314 return rc;
1315 }
1316
David Vincze39e78552018-10-10 17:10:01 +02001317 /*
1318 * Erases header and trailer. The trailer is erased because when a new
1319 * image is written without a trailer as is the case when using newt, the
1320 * trailer that was left might trigger a new upgrade.
1321 */
David Vincze401c7422019-06-21 20:44:05 +02001322 BOOT_LOG_DBG("erasing secondary header");
1323 rc = boot_erase_sector(fap_secondary_slot,
David Vincze8bdfc2d2019-03-18 15:49:23 +01001324 boot_img_sector_off(&boot_data,
1325 BOOT_SECONDARY_SLOT, 0),
1326 boot_img_sector_size(&boot_data,
1327 BOOT_SECONDARY_SLOT, 0));
Tamas Banf70ef8c2017-12-19 15:35:09 +00001328 assert(rc == 0);
David Vincze8bdfc2d2019-03-18 15:49:23 +01001329 last_sector = boot_img_num_sectors(&boot_data, BOOT_SECONDARY_SLOT) - 1;
David Vincze401c7422019-06-21 20:44:05 +02001330 BOOT_LOG_DBG("erasing secondary trailer");
1331 rc = boot_erase_sector(fap_secondary_slot,
David Vincze8bdfc2d2019-03-18 15:49:23 +01001332 boot_img_sector_off(&boot_data, BOOT_SECONDARY_SLOT,
1333 last_sector),
1334 boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT,
1335 last_sector));
David Vincze39e78552018-10-10 17:10:01 +02001336 assert(rc == 0);
1337
David Vincze401c7422019-06-21 20:44:05 +02001338 flash_area_close(fap_primary_slot);
1339 flash_area_close(fap_secondary_slot);
1340
David Vincze8bdfc2d2019-03-18 15:49:23 +01001341 /* TODO: Perhaps verify the primary slot's signature again? */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001342
1343 return 0;
1344}
1345#else
David Vincze401c7422019-06-21 20:44:05 +02001346/**
1347 * Swaps the two images in flash. If a prior copy operation was interrupted
1348 * by a system reset, this function completes that operation.
1349 *
1350 * @param bs The current boot status. This function reads
1351 * this struct to determine if it is resuming
1352 * an interrupted swap operation. This
1353 * function writes the updated status to this
1354 * function on return.
1355 *
1356 * @return 0 on success; nonzero on failure.
1357 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001358static int
David Vincze401c7422019-06-21 20:44:05 +02001359boot_swap_image(struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001360{
1361 uint32_t sz;
1362 int first_sector_idx;
1363 int last_sector_idx;
David Vincze401c7422019-06-21 20:44:05 +02001364 int last_idx_secondary_slot;
David Vincze39e78552018-10-10 17:10:01 +02001365 uint32_t swap_idx;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001366 struct image_header *hdr;
1367 uint32_t size;
1368 uint32_t copy_size;
David Vincze401c7422019-06-21 20:44:05 +02001369 uint32_t primary_slot_size;
1370 uint32_t secondary_slot_size;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001371 int rc;
1372
1373 /* FIXME: just do this if asked by user? */
1374
1375 size = copy_size = 0;
1376
David Vincze39e78552018-10-10 17:10:01 +02001377 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001378 /*
1379 * No swap ever happened, so need to find the largest image which
1380 * will be used to determine the amount of sectors to swap.
1381 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001382 hdr = boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT);
Tamas Ban056ed0b2019-09-16 12:48:32 +01001383 rc = boot_read_image_size(BOOT_PRIMARY_SLOT, hdr, &copy_size);
1384 assert(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001385
David Vincze8bdfc2d2019-03-18 15:49:23 +01001386 hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT);
Tamas Ban056ed0b2019-09-16 12:48:32 +01001387 rc = boot_read_image_size(BOOT_SECONDARY_SLOT, hdr, &size);
1388 assert(rc == 0);
1389
Tamas Banf70ef8c2017-12-19 15:35:09 +00001390
1391 if (size > copy_size) {
1392 copy_size = size;
1393 }
1394
1395 bs->swap_size = copy_size;
1396 } else {
1397 /*
1398 * If a swap was under way, the swap_size should already be present
1399 * in the trailer...
1400 */
1401 rc = boot_read_swap_size(&bs->swap_size);
1402 assert(rc == 0);
1403
1404 copy_size = bs->swap_size;
1405 }
1406
David Vincze401c7422019-06-21 20:44:05 +02001407 primary_slot_size = 0;
1408 secondary_slot_size = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001409 last_sector_idx = 0;
David Vincze401c7422019-06-21 20:44:05 +02001410 last_idx_secondary_slot = 0;
1411
1412 /*
1413 * Knowing the size of the largest image between both slots, here we
1414 * find what is the last sector in the primary slot that needs swapping.
1415 * Since we already know that both slots are compatible, the secondary
1416 * slot's last sector is not really required after this check is finished.
1417 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001418 while (1) {
David Vincze401c7422019-06-21 20:44:05 +02001419 if ((primary_slot_size < copy_size) ||
1420 (primary_slot_size < secondary_slot_size)) {
1421 primary_slot_size += boot_img_sector_size(&boot_data,
1422 BOOT_PRIMARY_SLOT,
1423 last_sector_idx);
1424 }
1425 if ((secondary_slot_size < copy_size) ||
1426 (secondary_slot_size < primary_slot_size)) {
1427 secondary_slot_size += boot_img_sector_size(&boot_data,
1428 BOOT_SECONDARY_SLOT,
1429 last_idx_secondary_slot);
1430 }
1431 if (primary_slot_size >= copy_size &&
1432 secondary_slot_size >= copy_size &&
1433 primary_slot_size == secondary_slot_size) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001434 break;
1435 }
1436 last_sector_idx++;
David Vincze401c7422019-06-21 20:44:05 +02001437 last_idx_secondary_slot++;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001438 }
1439
1440 swap_idx = 0;
1441 while (last_sector_idx >= 0) {
1442 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
David Vincze39e78552018-10-10 17:10:01 +02001443 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001444 boot_swap_sectors(first_sector_idx, sz, bs);
1445 }
1446
1447 last_sector_idx = first_sector_idx - 1;
1448 swap_idx++;
1449 }
1450
David Vincze8bdfc2d2019-03-18 15:49:23 +01001451#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
David Vincze39e78552018-10-10 17:10:01 +02001452 if (boot_status_fails > 0) {
David Vincze401c7422019-06-21 20:44:05 +02001453 BOOT_LOG_WRN("%d status write fails performing the swap",
1454 boot_status_fails);
David Vincze39e78552018-10-10 17:10:01 +02001455 }
1456#endif
1457
Tamas Banf70ef8c2017-12-19 15:35:09 +00001458 return 0;
1459}
1460#endif
1461
1462/**
David Vincze8bdfc2d2019-03-18 15:49:23 +01001463 * Marks the image in the primary slot as fully copied.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001464 */
1465#ifndef MCUBOOT_OVERWRITE_ONLY
1466static int
1467boot_set_copy_done(void)
1468{
1469 const struct flash_area *fap;
1470 int rc;
1471
David Vincze8bdfc2d2019-03-18 15:49:23 +01001472 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001473 if (rc != 0) {
1474 return BOOT_EFLASH;
1475 }
1476
1477 rc = boot_write_copy_done(fap);
1478 flash_area_close(fap);
1479 return rc;
1480}
1481#endif /* !MCUBOOT_OVERWRITE_ONLY */
1482
1483/**
David Vincze8bdfc2d2019-03-18 15:49:23 +01001484 * Marks a reverted image in the primary slot as confirmed. This is necessary to
1485 * ensure the status bytes from the image revert operation don't get processed
1486 * on a subsequent boot.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001487 *
1488 * NOTE: image_ok is tested before writing because if there's a valid permanent
David Vincze8bdfc2d2019-03-18 15:49:23 +01001489 * image installed on the primary slot and the new image to be upgrade to has a
1490 * bad sig, image_ok would be overwritten.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001491 */
1492#ifndef MCUBOOT_OVERWRITE_ONLY
1493static int
1494boot_set_image_ok(void)
1495{
1496 const struct flash_area *fap;
1497 struct boot_swap_state state;
1498 int rc;
1499
David Vincze8bdfc2d2019-03-18 15:49:23 +01001500 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001501 if (rc != 0) {
1502 return BOOT_EFLASH;
1503 }
1504
1505 rc = boot_read_swap_state(fap, &state);
1506 if (rc != 0) {
1507 rc = BOOT_EFLASH;
1508 goto out;
1509 }
1510
1511 if (state.image_ok == BOOT_FLAG_UNSET) {
1512 rc = boot_write_image_ok(fap);
1513 }
1514
1515out:
1516 flash_area_close(fap);
1517 return rc;
1518}
1519#endif /* !MCUBOOT_OVERWRITE_ONLY */
1520
David Vinczebb6e3b62019-07-31 14:24:05 +02001521#if (BOOT_IMAGE_NUMBER > 1)
1522/**
1523 * Check the image dependency whether it is satisfied and modify
1524 * the swap type if necessary.
1525 *
1526 * @param dep Image dependency which has to be verified.
1527 *
1528 * @return 0 on success; nonzero on failure.
1529 */
1530static int
1531boot_verify_single_dependency(struct image_dependency *dep)
1532{
1533 struct image_version *dep_version;
1534 size_t dep_slot;
1535 int rc;
1536
1537 /* Determine the source of the image which is the subject of
1538 * the dependency and get it's version. */
1539 dep_slot = (boot_data.swap_type[dep->image_id] != BOOT_SWAP_TYPE_NONE) ?
1540 BOOT_SECONDARY_SLOT : BOOT_PRIMARY_SLOT;
1541 dep_version = &boot_data.imgs[dep->image_id][dep_slot].hdr.ih_ver;
1542
1543 rc = boot_is_version_sufficient(&dep->image_min_version, dep_version);
1544 if (rc != 0) {
1545 /* Dependency not satisfied.
1546 * Modify the swap type to decrease the version number of the image
1547 * (which will be located in the primary slot after the boot process),
1548 * consequently the number of unsatisfied dependencies will be
1549 * decreased or remain the same.
1550 */
1551 switch (BOOT_SWAP_TYPE(&boot_data)) {
1552 case BOOT_SWAP_TYPE_TEST:
1553 case BOOT_SWAP_TYPE_PERM:
1554 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE;
1555 break;
1556 case BOOT_SWAP_TYPE_NONE:
1557 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_REVERT;
1558 break;
1559 default:
1560 break;
1561 }
1562 }
1563
1564 return rc;
1565}
1566
1567/**
1568 * Read all dependency TLVs of an image from the flash and verify
1569 * one after another to see if they are all satisfied.
1570 *
1571 * @param slot Image slot number.
1572 *
1573 * @return 0 on success; nonzero on failure.
1574 */
1575static int
1576boot_verify_all_dependency(uint32_t slot)
1577{
1578 const struct flash_area *fap;
1579 struct image_header *hdr;
1580 struct image_tlv_info info;
1581 struct image_tlv tlv;
1582 struct image_dependency dep;
1583 uint32_t off;
1584 uint32_t end;
1585 bool dep_tlvs_found = false;
1586 int rc;
1587
1588 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
1589 if (rc != 0) {
1590 rc = BOOT_EFLASH;
1591 goto done;
1592 }
1593
1594 hdr = boot_img_hdr(&boot_data, slot);
1595 /* The TLVs come after the image. */
1596 off = hdr->ih_hdr_size + hdr->ih_img_size;
1597
1598 /* The TLV area always starts with an image_tlv_info structure. */
1599 rc = flash_area_read(fap, off, &info, sizeof(info));
1600 if (rc != 0) {
1601 rc = BOOT_EFLASH;
1602 goto done;
1603 }
1604
1605 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
1606 rc = BOOT_EBADIMAGE;
1607 goto done;
1608 }
Tamas Ban056ed0b2019-09-16 12:48:32 +01001609 if (boot_add_uint32_overflow_check(off, (info.it_tlv_tot + sizeof(info)))) {
1610 return -1;
1611 }
David Vinczebb6e3b62019-07-31 14:24:05 +02001612 end = off + info.it_tlv_tot;
1613 off += sizeof(info);
1614
1615 /* Traverse through all of the TLVs to find the dependency TLVs. */
Tamas Ban056ed0b2019-09-16 12:48:32 +01001616 while(off < end) {
David Vinczebb6e3b62019-07-31 14:24:05 +02001617 rc = flash_area_read(fap, off, &tlv, sizeof(tlv));
1618 if (rc != 0) {
1619 rc = BOOT_EFLASH;
1620 goto done;
1621 }
1622
1623 if (tlv.it_type == IMAGE_TLV_DEPENDENCY) {
1624 if (!dep_tlvs_found) {
1625 dep_tlvs_found = true;
1626 }
1627
1628 if (tlv.it_len != sizeof(dep)) {
1629 rc = BOOT_EBADIMAGE;
1630 goto done;
1631 }
1632
1633 rc = flash_area_read(fap, off + sizeof(tlv), &dep, tlv.it_len);
1634 if (rc != 0) {
1635 rc = BOOT_EFLASH;
1636 goto done;
1637 }
1638
1639 /* Verify dependency and modify the swap type if not satisfied. */
1640 rc = boot_verify_single_dependency(&dep);
1641 if (rc != 0) {
1642 /* Dependency not satisfied. */
1643 goto done;
1644 }
1645
1646 /* Dependency satisfied, no action needed.
1647 * Continue with the next TLV entry.
1648 */
1649 } else if (dep_tlvs_found) {
1650 /* The dependency TLVs are contiguous in the TLV area. If a
1651 * dependency had already been found and the last read TLV
1652 * has a different type then there are no more dependency TLVs.
1653 * The search can be finished.
1654 */
1655 break;
1656 }
Tamas Ban056ed0b2019-09-16 12:48:32 +01001657 /* Avoid integer overflow. */
1658 if (boot_add_uint32_overflow_check(off, (sizeof(tlv) + tlv.it_len))) {
1659 /* Potential overflow. */
1660 return BOOT_EBADIMAGE;
1661 } else {
1662 off += sizeof(tlv) + tlv.it_len;
1663 }
David Vinczebb6e3b62019-07-31 14:24:05 +02001664 }
1665
1666done:
1667 flash_area_close(fap);
1668 return rc;
1669}
1670
1671/**
1672 * Verify whether the image dependencies in the TLV area are
1673 * all satisfied and modify the swap type if necessary.
1674 *
1675 * @return 0 if all dependencies are satisfied,
1676 * nonzero otherwise.
1677 */
1678static int
1679boot_verify_single_image_dependency(void)
1680{
1681 size_t slot;
1682
1683 /* Determine the source of the dependency TLVs. Those dependencies have to
1684 * be checked which belong to the image that will be located in the primary
1685 * slot after the firmware update process.
1686 */
1687 if (BOOT_SWAP_TYPE(&boot_data) != BOOT_SWAP_TYPE_NONE &&
1688 BOOT_SWAP_TYPE(&boot_data) != BOOT_SWAP_TYPE_FAIL) {
1689 slot = BOOT_SECONDARY_SLOT;
1690 } else {
1691 slot = BOOT_PRIMARY_SLOT;
1692 }
1693
1694 return boot_verify_all_dependency(slot);
1695}
1696
1697/**
1698 * Iterate over all the images and verify whether the image dependencies in the
1699 * TLV area are all satisfied and update the related swap type if necessary.
1700 */
1701static void
1702boot_verify_all_image_dependency(void)
1703{
1704 current_image = 0;
1705 int rc;
1706
1707 while (current_image < BOOT_IMAGE_NUMBER) {
1708 rc = boot_verify_single_image_dependency();
1709 if (rc == 0) {
1710 /* All dependencies've been satisfied, continue with next image. */
1711 current_image++;
1712 } else if (rc == BOOT_EBADVERSION) {
1713 /* Dependency check needs to be restarted. */
1714 current_image = 0;
1715 } else {
1716 /* Other error happened, images are inconsistent */
1717 return;
1718 }
1719 }
1720}
1721#endif /* (BOOT_IMAGE_NUMBER > 1) */
1722
Tamas Banf70ef8c2017-12-19 15:35:09 +00001723/**
David Vincze7384ee72019-07-23 17:00:42 +02001724 * Performs a clean (not aborted) image update.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001725 *
David Vincze7384ee72019-07-23 17:00:42 +02001726 * @param bs The current boot status.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001727 *
1728 * @return 0 on success; nonzero on failure.
1729 */
1730static int
David Vincze7384ee72019-07-23 17:00:42 +02001731boot_perform_update(struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001732{
Tamas Banf70ef8c2017-12-19 15:35:09 +00001733 int rc;
1734
David Vincze7384ee72019-07-23 17:00:42 +02001735 /* At this point there are no aborted swaps. */
1736#if defined(MCUBOOT_OVERWRITE_ONLY)
1737 rc = boot_copy_image(bs);
1738#else
1739 rc = boot_swap_image(bs);
1740#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +00001741 assert(rc == 0);
David Vincze7384ee72019-07-23 17:00:42 +02001742
1743#ifndef MCUBOOT_OVERWRITE_ONLY
1744 /* The following state needs image_ok be explicitly set after the
1745 * swap was finished to avoid a new revert.
1746 */
1747 if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_REVERT ||
1748 BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_PERM) {
1749 rc = boot_set_image_ok();
1750 if (rc != 0) {
1751 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
1752 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001753 }
1754
David Vincze7384ee72019-07-23 17:00:42 +02001755 if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_PERM) {
1756 /* Update the stored security counter with the new image's security
1757 * counter value. The primary slot holds the new image at this
1758 * point, but the secondary slot's image header must be passed
1759 * because the read image headers in the boot_data structure have
1760 * not been updated yet.
1761 *
1762 * In case of a permanent image swap mcuboot will never attempt to
1763 * revert the images on the next reboot. Therefore, the security
1764 * counter must be increased right after the image upgrade.
David Vincze401c7422019-06-21 20:44:05 +02001765 */
David Vincze7384ee72019-07-23 17:00:42 +02001766 rc = boot_update_security_counter(BOOT_PRIMARY_SLOT,
1767 boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT));
1768 if (rc != 0) {
1769 BOOT_LOG_ERR("Security counter update failed after "
1770 "image upgrade.");
1771 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001772 }
1773 }
1774
David Vincze7384ee72019-07-23 17:00:42 +02001775 if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_TEST ||
1776 BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_PERM ||
1777 BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_REVERT) {
1778 rc = boot_set_copy_done();
1779 if (rc != 0) {
1780 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
1781 }
1782 }
1783#endif /* !MCUBOOT_OVERWRITE_ONLY */
1784
1785 return rc;
1786}
1787
1788/**
1789 * Completes a previously aborted image swap.
1790 *
1791 * @param bs The current boot status.
1792 *
1793 * @return 0 on success; nonzero on failure.
1794 */
1795#if !defined(MCUBOOT_OVERWRITE_ONLY)
1796static int
1797boot_complete_partial_swap(struct boot_status *bs)
1798{
1799 int rc;
1800
1801 /* Determine the type of swap operation being resumed from the
1802 * `swap-type` trailer field.
1803 */
1804 rc = boot_swap_image(bs);
1805 assert(rc == 0);
1806
1807 BOOT_SWAP_TYPE(&boot_data) = bs->swap_type;
1808
1809 /* The following states need image_ok be explicitly set after the
1810 * swap was finished to avoid a new revert.
1811 */
1812 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1813 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
1814 rc = boot_set_image_ok();
1815 if (rc != 0) {
1816 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
1817 }
1818 }
1819
1820 if (bs->swap_type == BOOT_SWAP_TYPE_TEST ||
1821 bs->swap_type == BOOT_SWAP_TYPE_PERM ||
1822 bs->swap_type == BOOT_SWAP_TYPE_REVERT) {
1823 rc = boot_set_copy_done();
1824 if (rc != 0) {
1825 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
1826 }
1827 }
1828
1829 if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_PANIC) {
1830 BOOT_LOG_ERR("panic!");
1831 assert(0);
1832
1833 /* Loop forever... */
1834 while (1) {}
1835 }
1836
1837 return rc;
1838}
1839#endif /* !MCUBOOT_OVERWRITE_ONLY */
1840
1841#if (BOOT_IMAGE_NUMBER > 1)
1842/**
1843 * Review the validity of previously determined swap types of other images.
1844 *
1845 * @param aborted_swap The current image upgrade is a
1846 * partial/aborted swap.
1847 */
1848static void
1849boot_review_image_swap_types(bool aborted_swap)
1850{
1851 /* In that case if we rebooted in the middle of an image upgrade process, we
1852 * must review the validity of swap types, that were previously determined
1853 * for other images. The image_ok flag had not been set before the reboot
1854 * for any of the updated images (only the copy_done flag) and thus falsely
1855 * the REVERT swap type has been determined for the previous images that had
1856 * been updated before the reboot.
1857 *
1858 * There are two separate scenarios that we have to deal with:
1859 *
1860 * 1. The reboot has happened during swapping an image:
1861 * The current image upgrade has been determined as a
1862 * partial/aborted swap.
1863 * 2. The reboot has happened between two separate image upgrades:
1864 * In this scenario we must check the swap type of the current image.
1865 * In those cases if it is NONE or REVERT we cannot certainly determine
1866 * the fact of a reboot. In a consistent state images must move in the
1867 * same direction or stay in place, e.g. in practice REVERT and TEST
1868 * swap types cannot be present at the same time. If the swap type of
1869 * the current image is either TEST, PERM or FAIL we must review the
1870 * already determined swap types of other images and set each false
1871 * REVERT swap types to NONE (these images had been successfully
1872 * updated before the system rebooted between two separate image
1873 * upgrades).
1874 */
1875
1876 if (current_image == 0) {
1877 /* Nothing to do */
1878 return;
1879 }
1880
1881 if (!aborted_swap) {
1882 if ((BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_NONE) ||
1883 (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_REVERT)) {
1884 /* Nothing to do */
1885 return;
1886 }
1887 }
1888
1889 for (uint8_t i = 0; i < current_image; i++) {
1890 if (boot_data.swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
1891 boot_data.swap_type[i] = BOOT_SWAP_TYPE_NONE;
1892 }
1893 }
1894}
1895#endif
1896
1897/**
1898 * Prepare image to be updated if required.
1899 *
1900 * Prepare image to be updated if required with completing an image swap
1901 * operation if one was aborted and/or determining the type of the
1902 * swap operation. In case of any error set the swap type to NONE.
1903 *
1904 * @param bs Pointer where the read and possibly updated
1905 * boot status can be written to.
1906 */
1907static void
1908boot_prepare_image_for_update(struct boot_status *bs)
1909{
1910 int rc;
1911
1912 /* Determine the sector layout of the image slots and scratch area. */
1913 rc = boot_read_sectors();
1914 if (rc != 0) {
1915 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
1916 " - too small?", BOOT_MAX_IMG_SECTORS);
1917 /* Unable to determine sector layout, continue with next image
1918 * if there is one.
1919 */
1920 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE;
1921 return;
1922 }
1923
1924 /* Attempt to read an image header from each slot. */
1925 rc = boot_read_image_headers(false);
1926 if (rc != 0) {
1927 /* Continue with next image if there is one. */
1928 BOOT_LOG_WRN("Failed reading image headers; Image=%u", current_image);
1929 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE;
1930 return;
1931 }
1932
1933 /* If the current image's slots aren't compatible, no swap is possible.
1934 * Just boot into primary slot.
1935 */
1936 if (boot_slots_compatible()) {
1937
1938 rc = boot_read_status(bs);
1939 if (rc != 0) {
1940 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
1941 current_image);
1942 /* Continue with next image if there is one. */
1943 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE;
1944 return;
1945 }
1946
1947 /* Determine if we rebooted in the middle of an image swap
1948 * operation. If a partial swap was detected, complete it.
1949 */
1950 if (bs->idx != BOOT_STATUS_IDX_0 || bs->state != BOOT_STATUS_STATE_0) {
1951
1952#if (BOOT_IMAGE_NUMBER > 1)
1953 boot_review_image_swap_types(true);
1954#endif
1955
1956#ifdef MCUBOOT_OVERWRITE_ONLY
1957 /* Should never arrive here, overwrite-only mode has
1958 * no swap state.
1959 */
1960 assert(0);
1961#else
1962 /* Determine the type of swap operation being resumed from the
1963 * `swap-type` trailer field.
1964 */
1965 rc = boot_complete_partial_swap(bs);
1966 assert(rc == 0);
1967#endif
1968 /* Attempt to read an image header from each slot. Ensure that
1969 * image headers in slots are aligned with headers in boot_data.
1970 */
1971 rc = boot_read_image_headers(false);
1972 assert(rc == 0);
1973
1974 /* Swap has finished set to NONE */
1975 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE;
1976 } else {
1977 /* There was no partial swap, determine swap type. */
1978 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
1979 BOOT_SWAP_TYPE(&boot_data) = boot_validated_swap_type(bs);
1980 } else if (boot_validate_slot(BOOT_SECONDARY_SLOT, bs) != 0) {
1981 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_FAIL;
1982 } else {
1983 BOOT_SWAP_TYPE(&boot_data) = bs->swap_type;
1984 }
1985
1986#if (BOOT_IMAGE_NUMBER > 1)
1987 boot_review_image_swap_types(false);
1988#endif
1989 }
1990 } else {
1991 /* In that case if slots are not compatible. */
1992 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE;
1993 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001994}
1995
1996/**
1997 * Prepares the booting process. This function moves images around in flash as
1998 * appropriate, and tells you what address to boot from.
1999 *
2000 * @param rsp On success, indicates how booting should occur.
2001 *
2002 * @return 0 on success; nonzero on failure.
2003 */
2004int
2005boot_go(struct boot_rsp *rsp)
2006{
Tamas Banf70ef8c2017-12-19 15:35:09 +00002007 size_t slot;
David Vincze7384ee72019-07-23 17:00:42 +02002008 struct boot_status bs;
Tamas Ban4e5ed8d2019-09-17 09:31:11 +01002009 int rc = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002010 int fa_id;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002011
2012 /* The array of slot sectors are defined here (as opposed to file scope) so
2013 * that they don't get allocated for non-boot-loader apps. This is
2014 * necessary because the gcc option "-fdata-sections" doesn't seem to have
2015 * any effect in older gcc versions (e.g., 4.8.4).
2016 */
David Vincze7384ee72019-07-23 17:00:42 +02002017 static boot_sector_t
2018 primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
2019 static boot_sector_t
2020 secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
David Vincze401c7422019-06-21 20:44:05 +02002021 static boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Tamas Banf70ef8c2017-12-19 15:35:09 +00002022
David Vincze7384ee72019-07-23 17:00:42 +02002023 /* Iterate over all the images. By the end of the loop the swap type has
2024 * to be determined for each image and all aborted swaps have to be
2025 * completed.
Tamas Banf70ef8c2017-12-19 15:35:09 +00002026 */
David Vincze7384ee72019-07-23 17:00:42 +02002027 for (current_image = 0; current_image < BOOT_IMAGE_NUMBER; ++current_image)
2028 {
2029 BOOT_IMG(&boot_data, BOOT_PRIMARY_SLOT).sectors =
2030 primary_slot_sectors[current_image];
2031 BOOT_IMG(&boot_data, BOOT_SECONDARY_SLOT).sectors =
2032 secondary_slot_sectors[current_image];
2033 boot_data.scratch.sectors = scratch_sectors;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002034
David Vincze7384ee72019-07-23 17:00:42 +02002035 /* Open primary and secondary image areas for the duration
2036 * of this call.
Tamas Banf70ef8c2017-12-19 15:35:09 +00002037 */
David Vincze7384ee72019-07-23 17:00:42 +02002038 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2039 fa_id = flash_area_id_from_image_slot(slot);
2040 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot));
2041 assert(rc == 0);
2042 }
2043 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
2044 &BOOT_SCRATCH_AREA(&boot_data));
2045 assert(rc == 0);
2046
2047 /* Determine swap type and complete swap if it has been aborted. */
2048 boot_prepare_image_for_update(&bs);
2049 }
2050
David Vinczebb6e3b62019-07-31 14:24:05 +02002051#if (BOOT_IMAGE_NUMBER > 1)
2052 /* Iterate over all the images and verify whether the image dependencies
2053 * are all satisfied and update swap type if necessary.
2054 */
2055 boot_verify_all_image_dependency();
2056#endif
2057
David Vincze7384ee72019-07-23 17:00:42 +02002058 /* Iterate over all the images. At this point there are no aborted swaps
2059 * and the swap types are determined for each image. By the end of the loop
2060 * all required update operations will have been finished.
2061 */
2062 for (current_image = 0; current_image < BOOT_IMAGE_NUMBER; ++current_image)
2063 {
2064
2065#if (BOOT_IMAGE_NUMBER > 1)
2066 /* Indicate that swap is not aborted */
2067 memset(&bs, 0, sizeof bs);
2068 bs.idx = BOOT_STATUS_IDX_0;
2069 bs.state = BOOT_STATUS_STATE_0;
2070#endif /* (BOOT_IMAGE_NUMBER > 1) */
2071
2072 /* Set the previously determined swap type */
2073 bs.swap_type = BOOT_SWAP_TYPE(&boot_data);
2074
2075 switch (BOOT_SWAP_TYPE(&boot_data)) {
2076 case BOOT_SWAP_TYPE_NONE:
2077 break;
2078
2079 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
2080 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
2081 case BOOT_SWAP_TYPE_REVERT:
2082 rc = boot_perform_update(&bs);
2083 assert(rc == 0);
2084 break;
2085
2086 case BOOT_SWAP_TYPE_FAIL:
2087 /* The image in secondary slot was invalid and is now erased. Ensure
2088 * we don't try to boot into it again on the next reboot. Do this by
2089 * pretending we just reverted back to primary slot.
2090 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00002091#ifndef MCUBOOT_OVERWRITE_ONLY
David Vincze7384ee72019-07-23 17:00:42 +02002092 /* image_ok needs to be explicitly set to avoid a new revert. */
Tamas Banf70ef8c2017-12-19 15:35:09 +00002093 rc = boot_set_image_ok();
2094 if (rc != 0) {
David Vincze7384ee72019-07-23 17:00:42 +02002095 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002096 }
2097#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Vincze7384ee72019-07-23 17:00:42 +02002098 break;
2099
2100 default:
2101 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002102 }
David Vincze7384ee72019-07-23 17:00:42 +02002103
2104 if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_PANIC) {
2105 BOOT_LOG_ERR("panic!");
2106 assert(0);
2107
2108 /* Loop forever... */
2109 while (1) {}
2110 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00002111 }
2112
David Vincze7384ee72019-07-23 17:00:42 +02002113 /* Iterate over all the images. At this point all required update operations
2114 * have finished. By the end of the loop each image in the primary slot will
2115 * have been re-validated.
2116 */
2117 for (current_image = 0; current_image < BOOT_IMAGE_NUMBER; ++current_image)
2118 {
2119 if (BOOT_SWAP_TYPE(&boot_data) != BOOT_SWAP_TYPE_NONE) {
2120 /* Attempt to read an image header from each slot. Ensure that image
2121 * headers in slots are aligned with headers in boot_data.
David Vincze060968d2019-05-23 01:13:14 +02002122 */
David Vincze7384ee72019-07-23 17:00:42 +02002123 rc = boot_read_image_headers(false);
David Vincze060968d2019-05-23 01:13:14 +02002124 if (rc != 0) {
David Vincze7384ee72019-07-23 17:00:42 +02002125 goto out;
2126 }
2127 /* Since headers were reloaded, it can be assumed we just performed
2128 * a swap or overwrite. Now the header info that should be used to
2129 * provide the data for the bootstrap, which previously was at
2130 * secondary slot, was updated to primary slot.
2131 */
2132 }
2133
2134#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
2135 rc = boot_validate_slot(BOOT_PRIMARY_SLOT, NULL);
2136 if (rc != 0) {
2137 rc = BOOT_EBADIMAGE;
2138 goto out;
2139 }
2140#else
2141 /* Even if we're not re-validating the primary slot, we could be booting
2142 * onto an empty flash chip. At least do a basic sanity check that
2143 * the magic number on the image is OK.
2144 */
Tamas Ban056ed0b2019-09-16 12:48:32 +01002145 if (!BOOT_IMG_HDR_IS_VALID(&boot_data, slot)) {
2146 BOOT_LOG_ERR("Invalid image header Image=%u", current_image);
David Vincze7384ee72019-07-23 17:00:42 +02002147 rc = BOOT_EBADIMAGE;
2148 goto out;
2149 }
2150#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
2151
2152 /* Update the stored security counter with the active image's security
2153 * counter value. It will be updated only if the new security counter is
2154 * greater than the stored value.
2155 *
2156 * In case of a successful image swapping when the swap type is TEST the
2157 * security counter can be increased only after a reset, when the swap
2158 * type is NONE and the image has marked itself "OK" (the image_ok flag
2159 * has been set). This way a "revert" swap can be performed if it's
2160 * necessary.
2161 */
2162 if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_NONE) {
2163 rc = boot_update_security_counter(BOOT_PRIMARY_SLOT,
2164 boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT));
2165 if (rc != 0) {
2166 BOOT_LOG_ERR("Security counter update failed after image "
2167 "validation.");
David Vincze060968d2019-05-23 01:13:14 +02002168 goto out;
2169 }
2170 }
2171
David Vincze7384ee72019-07-23 17:00:42 +02002172 /* Save boot status to shared memory area */
2173#if (BOOT_IMAGE_NUMBER > 1)
2174 rc = boot_save_boot_status((current_image == 0) ? SW_SPE : SW_NSPE,
2175 boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT),
2176 BOOT_IMG_AREA(&boot_data, BOOT_PRIMARY_SLOT)
2177 );
David Vincze8bdfc2d2019-03-18 15:49:23 +01002178#else
David Vincze7384ee72019-07-23 17:00:42 +02002179 rc = boot_save_boot_status(SW_S_NS,
2180 boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT),
2181 BOOT_IMG_AREA(&boot_data, BOOT_PRIMARY_SLOT)
2182 );
2183#endif
2184 if (rc) {
2185 BOOT_LOG_ERR("Failed to add Image %u data to shared area",
2186 current_image);
David Vincze060968d2019-05-23 01:13:14 +02002187 }
2188 }
2189
David Vincze7384ee72019-07-23 17:00:42 +02002190 /* Always boot from the primary slot of Image 0. */
2191 current_image = 0;
2192 rsp->br_flash_dev_id =
2193 BOOT_IMG_AREA(&boot_data, BOOT_PRIMARY_SLOT)->fa_device_id;
2194 rsp->br_image_off =
2195 boot_img_slot_off(&boot_data, BOOT_PRIMARY_SLOT);
2196 rsp->br_hdr =
2197 boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT);
Tamas Ban0e8ab302019-01-17 11:45:31 +00002198
Tamas Banf70ef8c2017-12-19 15:35:09 +00002199 out:
David Vincze7384ee72019-07-23 17:00:42 +02002200 for (current_image = 0; current_image < BOOT_IMAGE_NUMBER; ++current_image)
2201 {
2202 flash_area_close(BOOT_SCRATCH_AREA(&boot_data));
2203 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2204 flash_area_close(BOOT_IMG_AREA(&boot_data,
2205 BOOT_NUM_SLOTS - 1 - slot));
2206 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00002207 }
2208 return rc;
2209}
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002210
Oliver Swedef9982442018-08-24 18:37:44 +01002211#else /* MCUBOOT_NO_SWAP || MCUBOOT_RAM_LOADING */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002212
David Vinczedcba70b2019-05-28 12:02:52 +02002213#define BOOT_LOG_IMAGE_INFO(area, hdr, state) \
2214 BOOT_LOG_INF("Image %u: version=%u.%u.%u+%u, magic=%5s, image_ok=0x%x", \
2215 (area), \
2216 (hdr)->ih_ver.iv_major, \
2217 (hdr)->ih_ver.iv_minor, \
2218 (hdr)->ih_ver.iv_revision, \
2219 (hdr)->ih_ver.iv_build_num, \
2220 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
2221 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
2222 "bad"), \
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002223 (state)->image_ok)
2224
2225struct image_slot_version {
2226 uint64_t version;
2227 uint32_t slot_number;
2228};
2229
2230/**
2231 * Extract the version number from the image header. This function must be
2232 * ported if version number format has changed in the image header.
2233 *
2234 * @param hdr Pointer to an image header structure
2235 *
Oliver Swedef9982442018-08-24 18:37:44 +01002236 * @return Version number casted to uint64_t
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002237 */
2238static uint64_t
2239boot_get_version_number(struct image_header *hdr)
2240{
Oliver Swedef9982442018-08-24 18:37:44 +01002241 uint64_t version = 0;
2242 version |= (uint64_t)hdr->ih_ver.iv_major << (IMAGE_VER_MINOR_LENGTH
2243 + IMAGE_VER_REVISION_LENGTH
2244 + IMAGE_VER_BUILD_NUM_LENGTH);
2245 version |= (uint64_t)hdr->ih_ver.iv_minor << (IMAGE_VER_REVISION_LENGTH
2246 + IMAGE_VER_BUILD_NUM_LENGTH);
2247 version |= (uint64_t)hdr->ih_ver.iv_revision << IMAGE_VER_BUILD_NUM_LENGTH;
2248 version |= hdr->ih_ver.iv_build_num;
2249 return version;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002250}
2251
2252/**
2253 * Comparator function for `qsort` to compare version numbers. This function
2254 * must be ported if version number format has changed in the image header.
2255 *
2256 * @param ver1 Pointer to an array element which holds the version number
2257 * @param ver2 Pointer to another array element which holds the version
2258 * number
2259 *
2260 * @return if version1 > version2 -1
2261 * if version1 == version2 0
2262 * if version1 < version2 1
2263 */
2264static int
2265boot_compare_version_numbers(const void *ver1, const void *ver2)
2266{
2267 if (((struct image_slot_version *)ver1)->version <
2268 ((struct image_slot_version *)ver2)->version) {
2269 return 1;
2270 }
2271
2272 if (((struct image_slot_version *)ver1)->version ==
2273 ((struct image_slot_version *)ver2)->version) {
2274 return 0;
2275 }
2276
2277 return -1;
2278}
2279
2280/**
2281 * Sort the available images based on the version number and puts them in
2282 * a list.
2283 *
2284 * @param boot_sequence A pointer to an array, whose aim is to carry
2285 * the boot order of candidate images.
2286 * @param slot_cnt The number of flash areas, which can contains firmware
2287 * images.
2288 *
2289 * @return The number of valid images.
2290 */
2291uint32_t
2292boot_get_boot_sequence(uint32_t *boot_sequence, uint32_t slot_cnt)
2293{
2294 struct boot_swap_state slot_state;
2295 struct image_header *hdr;
2296 struct image_slot_version image_versions[BOOT_NUM_SLOTS] = {{0}};
2297 uint32_t image_cnt = 0;
2298 uint32_t slot;
2299 int32_t rc;
2300 int32_t fa_id;
2301
2302 for (slot = 0; slot < slot_cnt; slot++) {
2303 hdr = boot_img_hdr(&boot_data, slot);
2304 fa_id = flash_area_id_from_image_slot(slot);
2305 rc = boot_read_swap_state_by_id(fa_id, &slot_state);
2306 if (rc != 0) {
David Vinczedcba70b2019-05-28 12:02:52 +02002307 BOOT_LOG_ERR("Error during reading image trailer from slot: %u",
2308 slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002309 continue;
2310 }
2311
Tamas Ban056ed0b2019-09-16 12:48:32 +01002312 if (BOOT_IMG_HDR_IS_VALID(&boot_data, slot)) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002313 if (slot_state.magic == BOOT_MAGIC_GOOD ||
David Vincze39e78552018-10-10 17:10:01 +02002314 slot_state.image_ok == BOOT_FLAG_SET) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002315 /* Valid cases:
2316 * - Test mode: magic is OK in image trailer
2317 * - Permanent mode: image_ok flag has previously set
2318 */
2319 image_versions[slot].slot_number = slot;
2320 image_versions[slot].version = boot_get_version_number(hdr);
2321 image_cnt++;
2322 }
2323
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002324 BOOT_LOG_IMAGE_INFO(slot, hdr, &slot_state);
2325 } else {
David Vinczedcba70b2019-05-28 12:02:52 +02002326 BOOT_LOG_INF("Image %u: No valid image", slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002327 }
2328 }
2329
2330 /* Sort the images based on version number */
2331 qsort(&image_versions[0],
2332 slot_cnt,
2333 sizeof(struct image_slot_version),
2334 boot_compare_version_numbers);
2335
2336 /* Copy the calculated boot sequence to boot_sequence array */
2337 for (slot = 0; slot < slot_cnt; slot++) {
2338 boot_sequence[slot] = image_versions[slot].slot_number;
2339 }
2340
2341 return image_cnt;
2342}
2343
Oliver Swedef9982442018-08-24 18:37:44 +01002344#ifdef MCUBOOT_RAM_LOADING
Raef Colesaf082382019-10-01 11:10:33 +01002345
2346/**
2347 * Verifies that the image in a slot lies within the predefined bounds that are
2348 * allowed to be used by executable images.
2349 *
2350 * @param img_dst The address to which the image is going to be copied.
2351 *
2352 * @param img_sz The size of the image.
2353 *
2354 * @return 0 on success; nonzero on failure.
2355 */
2356static int
2357boot_verify_ram_loading_address(uint32_t img_dst, uint32_t img_sz)
2358{
2359 if (img_dst < IMAGE_EXECUTABLE_RAM_START) {
2360 return BOOT_EBADIMAGE;
2361 }
2362
2363 if (boot_add_uint32_overflow_check(img_dst, img_sz)) {
2364 return BOOT_EBADIMAGE;
2365 }
2366
2367 if (img_dst + img_sz > IMAGE_EXECUTABLE_RAM_START +
2368 IMAGE_EXECUTABLE_RAM_SIZE) {
2369 return BOOT_EBADIMAGE;
2370 }
2371
2372 return 0;
2373}
2374
Oliver Swedef9982442018-08-24 18:37:44 +01002375/**
2376 * Copies an image from a slot in the flash to an SRAM address, where the load
2377 * address has already been inserted into the image header by this point and is
2378 * extracted from it within this method. The copying is done sector-by-sector.
2379 *
2380 * @param slot The flash slot of the image to be copied to SRAM.
2381 *
2382 * @param hdr Pointer to the image header structure of the image
Raef Colesaf082382019-10-01 11:10:33 +01002383 *
2384 * @param img_dst The address at which the image needs to be copied to
2385 * SRAM.
2386 *
2387 * @param img_sz The size of the image that needs to be copied to SRAM.
Oliver Swedef9982442018-08-24 18:37:44 +01002388 *
2389 * @return 0 on success; nonzero on failure.
2390 */
2391static int
Raef Colesaf082382019-10-01 11:10:33 +01002392boot_copy_image_to_sram(int slot, struct image_header *hdr,
2393 uint32_t img_dst, uint32_t img_sz)
Oliver Swedef9982442018-08-24 18:37:44 +01002394{
2395 int rc;
2396 uint32_t sect_sz;
2397 uint32_t sect = 0;
2398 uint32_t bytes_copied = 0;
2399 const struct flash_area *fap_src = NULL;
Oliver Swedef9982442018-08-24 18:37:44 +01002400
Raef Colesaf082382019-10-01 11:10:33 +01002401 if (img_dst % 4 != 0) {
Tamas Banc27b5c32019-05-28 16:30:19 +01002402 BOOT_LOG_INF("Cannot copy the image to the SRAM address 0x%x "
Oliver Swedef9982442018-08-24 18:37:44 +01002403 "- the load address must be aligned with 4 bytes due to SRAM "
Raef Colesaf082382019-10-01 11:10:33 +01002404 "restrictions", img_dst);
Oliver Swedef9982442018-08-24 18:37:44 +01002405 return BOOT_EBADARGS;
2406 }
2407
2408 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap_src);
2409 if (rc != 0) {
2410 return BOOT_EFLASH;
2411 }
2412
Oliver Swedef9982442018-08-24 18:37:44 +01002413 while (bytes_copied < img_sz) {
2414 sect_sz = boot_img_sector_size(&boot_data, slot, sect);
2415 /*
2416 * Direct copy from where the image sector resides in flash to its new
2417 * location in SRAM
2418 */
2419 rc = flash_area_read(fap_src,
2420 bytes_copied,
Raef Colesaf082382019-10-01 11:10:33 +01002421 (void *)(img_dst + bytes_copied),
Oliver Swedef9982442018-08-24 18:37:44 +01002422 sect_sz);
2423 if (rc != 0) {
2424 BOOT_LOG_INF("Error whilst copying image from Flash to SRAM");
2425 break;
2426 } else {
2427 bytes_copied += sect_sz;
2428 }
2429 sect++;
2430 }
2431
2432 if (fap_src) {
2433 flash_area_close(fap_src);
2434 }
2435 return rc;
2436}
Raef Coles27a61452019-09-25 15:32:25 +01002437
2438/**
2439 * Removes an image from SRAM, by overwriting it with zeros.
2440 *
Raef Colesaf082382019-10-01 11:10:33 +01002441 * @param img_dst The address of the image that needs to be removed from
2442 * SRAM.
Raef Coles27a61452019-09-25 15:32:25 +01002443 *
Raef Colesaf082382019-10-01 11:10:33 +01002444 * @param img_sz The size of the image that needs to be removed from
2445 * SRAM.
Raef Coles27a61452019-09-25 15:32:25 +01002446 *
2447 * @return 0 on success; nonzero on failure.
2448 */
2449static int
Raef Colesaf082382019-10-01 11:10:33 +01002450boot_remove_image_from_sram(uint32_t img_dst, uint32_t img_sz)
Raef Coles27a61452019-09-25 15:32:25 +01002451{
Raef Colesaf082382019-10-01 11:10:33 +01002452 BOOT_LOG_INF("Removing image from SRAM at address 0x%x", img_dst);
2453 memset((void*)img_dst, 0, img_sz);
Raef Coles27a61452019-09-25 15:32:25 +01002454
2455 return 0;
2456}
Oliver Swedef9982442018-08-24 18:37:44 +01002457#endif /* MCUBOOT_RAM_LOADING */
2458
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002459/**
2460 * Prepares the booting process. This function choose the newer image in flash
2461 * as appropriate, and returns the address to boot from.
2462 *
2463 * @param rsp On success, indicates how booting should occur.
2464 *
2465 * @return 0 on success; nonzero on failure.
2466 */
2467int
2468boot_go(struct boot_rsp *rsp)
2469{
2470 size_t slot = 0;
2471 int32_t i;
2472 int rc;
2473 int fa_id;
2474 uint32_t boot_sequence[BOOT_NUM_SLOTS];
2475 uint32_t img_cnt;
Raef Coles27a61452019-09-25 15:32:25 +01002476 struct image_header *selected_image_header;
2477#ifdef MCUBOOT_RAM_LOADING
2478 int image_copied = 0;
Raef Colesaf082382019-10-01 11:10:33 +01002479 uint32_t img_dst = 0;
2480 uint32_t img_sz = 0;
Raef Coles27a61452019-09-25 15:32:25 +01002481#endif /* MCUBOOT_RAM_LOADING */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002482
David Vincze8bdfc2d2019-03-18 15:49:23 +01002483 static boot_sector_t primary_slot_sectors[BOOT_MAX_IMG_SECTORS];
2484 static boot_sector_t secondary_slot_sectors[BOOT_MAX_IMG_SECTORS];
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002485
David Vincze7384ee72019-07-23 17:00:42 +02002486 BOOT_IMG(&boot_data, BOOT_PRIMARY_SLOT).sectors =
2487 &primary_slot_sectors[0];
2488 BOOT_IMG(&boot_data, BOOT_SECONDARY_SLOT).sectors =
2489 &secondary_slot_sectors[0];
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002490
2491 /* Open boot_data image areas for the duration of this call. */
2492 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
2493 fa_id = flash_area_id_from_image_slot(i);
2494 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, i));
2495 assert(rc == 0);
2496 }
2497
2498 /* Determine the sector layout of the image slots. */
2499 rc = boot_read_sectors();
2500 if (rc != 0) {
David Vincze39e78552018-10-10 17:10:01 +02002501 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - too small?",
2502 BOOT_MAX_IMG_SECTORS);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002503 goto out;
2504 }
2505
2506 /* Attempt to read an image header from each slot. */
David Vincze39e78552018-10-10 17:10:01 +02002507 rc = boot_read_image_headers(false);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002508 if (rc != 0) {
2509 goto out;
2510 }
2511
2512 img_cnt = boot_get_boot_sequence(boot_sequence, BOOT_NUM_SLOTS);
2513 if (img_cnt) {
2514 /* Authenticate images */
2515 for (i = 0; i < img_cnt; i++) {
Raef Coles27a61452019-09-25 15:32:25 +01002516
2517 slot = boot_sequence[i];
2518 selected_image_header = boot_img_hdr(&boot_data, slot);
2519
2520#ifdef MCUBOOT_RAM_LOADING
2521 if (selected_image_header->ih_flags & IMAGE_F_RAM_LOAD) {
Raef Colesaf082382019-10-01 11:10:33 +01002522
2523 img_dst = selected_image_header->ih_load_addr;
2524
2525 rc = boot_read_image_size(slot, selected_image_header, &img_sz);
2526 if (rc != 0) {
2527 rc = BOOT_EFLASH;
2528 BOOT_LOG_INF("Could not load image headers from the image"
2529 "in the %s slot.",
2530 (slot == BOOT_PRIMARY_SLOT) ?
2531 "primary" : "secondary");
2532 continue;
2533 }
2534
2535 rc = boot_verify_ram_loading_address(img_dst, img_sz);
2536 if (rc != 0) {
2537 BOOT_LOG_INF("Could not copy image from the %s slot in "
2538 "the Flash to load address 0x%x in SRAM as"
2539 " the image would overlap memory outside"
2540 " the defined executable region.",
2541 (slot == BOOT_PRIMARY_SLOT) ?
2542 "primary" : "secondary",
2543 selected_image_header->ih_load_addr);
2544 continue;
2545 }
2546
Raef Coles27a61452019-09-25 15:32:25 +01002547 /* Copy image to the load address from where it
2548 * currently resides in flash
2549 */
Raef Colesaf082382019-10-01 11:10:33 +01002550 rc = boot_copy_image_to_sram(slot, selected_image_header,
2551 img_dst, img_sz);
Raef Coles27a61452019-09-25 15:32:25 +01002552 if (rc != 0) {
2553 rc = BOOT_EBADIMAGE;
2554 BOOT_LOG_INF("Could not copy image from the %s slot in "
2555 "the Flash to load address 0x%x in SRAM, "
2556 "aborting..", (slot == BOOT_PRIMARY_SLOT) ?
2557 "primary" : "secondary",
2558 selected_image_header->ih_load_addr);
2559 continue;
2560 } else {
2561 BOOT_LOG_INF("Image has been copied from the %s slot in "
2562 "the flash to SRAM address 0x%x",
2563 (slot == BOOT_PRIMARY_SLOT) ?
2564 "primary" : "secondary",
2565 selected_image_header->ih_load_addr);
2566 image_copied = 1;
2567 }
2568 } else {
2569 /* Only images that support IMAGE_F_RAM_LOAD are allowed if
2570 * MCUBOOT_RAM_LOADING is set.
2571 */
2572 rc = BOOT_EBADIMAGE;
2573 continue;
2574 }
2575#endif /* MCUBOOT_RAM_LOADING */
2576 rc = boot_validate_slot(slot, NULL);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002577 if (rc == 0) {
Raef Coles27a61452019-09-25 15:32:25 +01002578 /* If a valid image is found then there is no reason to check
2579 * the rest of the images, as they were already ordered by
2580 * preference.
2581 */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002582 break;
2583 }
Raef Coles27a61452019-09-25 15:32:25 +01002584#ifdef MCUBOOT_RAM_LOADING
2585 else if (image_copied) {
2586 /* If an image is found to be invalid then it is removed from
2587 * RAM to prevent it being a shellcode vector.
2588 */
Raef Colesaf082382019-10-01 11:10:33 +01002589 boot_remove_image_from_sram(img_dst, img_sz);
Raef Coles27a61452019-09-25 15:32:25 +01002590 image_copied = 0;
2591 }
2592#endif /* MCUBOOT_RAM_LOADING */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002593 }
2594 if (rc) {
2595 /* If there was no valid image at all */
2596 rc = BOOT_EBADIMAGE;
2597 goto out;
2598 }
2599
David Vincze060968d2019-05-23 01:13:14 +02002600 /* Update the security counter with the newest image's security
2601 * counter value.
2602 */
Raef Coles27a61452019-09-25 15:32:25 +01002603 rc = boot_update_security_counter(slot, selected_image_header);
David Vincze060968d2019-05-23 01:13:14 +02002604 if (rc != 0) {
2605 BOOT_LOG_ERR("Security counter update failed after image "
2606 "validation.");
2607 goto out;
2608 }
2609
Oliver Swedef9982442018-08-24 18:37:44 +01002610
David Vincze8a2a4e22019-05-24 10:14:23 +02002611#ifdef MCUBOOT_RAM_LOADING
Raef Coles27a61452019-09-25 15:32:25 +01002612 BOOT_LOG_INF("Booting image from SRAM at address 0x%x",
2613 selected_image_header->ih_load_addr);
2614#else
2615 BOOT_LOG_INF("Booting image from the %s slot",
2616 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
2617#endif /* MCUBOOT_RAM_LOADING */
Oliver Swedef9982442018-08-24 18:37:44 +01002618
Raef Coles27a61452019-09-25 15:32:25 +01002619 rsp->br_hdr = selected_image_header;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002620 rsp->br_image_off = boot_img_slot_off(&boot_data, slot);
David Vincze7384ee72019-07-23 17:00:42 +02002621 rsp->br_flash_dev_id = BOOT_IMG_AREA(&boot_data, slot)->fa_device_id;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002622 } else {
2623 /* No candidate image available */
2624 rc = BOOT_EBADIMAGE;
David Vincze060968d2019-05-23 01:13:14 +02002625 goto out;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002626 }
2627
Tamas Ban0e8ab302019-01-17 11:45:31 +00002628 /* Save boot status to shared memory area */
2629 rc = boot_save_boot_status(SW_S_NS,
2630 rsp->br_hdr,
2631 BOOT_IMG_AREA(&boot_data, slot));
2632 if (rc) {
2633 BOOT_LOG_ERR("Failed to add data to shared area");
2634 }
2635
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002636out:
2637 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2638 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
2639 }
2640 return rc;
2641}
Oliver Swedef9982442018-08-24 18:37:44 +01002642#endif /* MCUBOOT_NO_SWAP || MCUBOOT_RAM_LOADING */