blob: abf88dfe5e619eaf6cea7eee28eadf950df89181 [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 Ban4fb8e9d2018-02-23 14:22:03 +0000158
159static int
160boot_read_image_header(int slot, struct image_header *out_hdr)
161{
162 const struct flash_area *fap = NULL;
163 int area_id;
164 int rc;
165
166 area_id = flash_area_id_from_image_slot(slot);
167 rc = flash_area_open(area_id, &fap);
168 if (rc != 0) {
169 rc = BOOT_EFLASH;
170 goto done;
171 }
172
173 rc = flash_area_read(fap, 0, out_hdr, sizeof(*out_hdr));
174 if (rc != 0) {
175 rc = BOOT_EFLASH;
176 goto done;
177 }
178
179 rc = 0;
180
181done:
182 flash_area_close(fap);
183 return rc;
184}
185
186static int
David Vincze39e78552018-10-10 17:10:01 +0200187boot_read_image_headers(bool require_all)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000188{
189 int rc;
190 int i;
191
192 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
193 rc = boot_read_image_header(i, boot_img_hdr(&boot_data, i));
194 if (rc != 0) {
David Vincze39e78552018-10-10 17:10:01 +0200195 /* If `require_all` is set, fail on any single fail, otherwise
196 * if at least the first slot's header was read successfully,
197 * then the boot loader can attempt a boot.
198 *
199 * Failure to read any headers is a fatal error.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000200 */
David Vincze39e78552018-10-10 17:10:01 +0200201 if (i > 0 && !require_all) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000202 return 0;
203 } else {
204 return rc;
205 }
206 }
207 }
208
209 return 0;
210}
211
Raef Coles204c5b42019-09-05 09:18:47 +0100212static uint32_t
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000213boot_write_sz(void)
214{
Raef Coles204c5b42019-09-05 09:18:47 +0100215 uint32_t elem_sz;
216 uint32_t align;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000217
218 /* Figure out what size to write update status update as. The size depends
219 * on what the minimum write size is for scratch area, active image slot.
220 * We need to use the bigger of those 2 values.
221 */
David Vincze7384ee72019-07-23 17:00:42 +0200222 elem_sz = flash_area_align(BOOT_IMG_AREA(&boot_data, BOOT_PRIMARY_SLOT));
223 align = flash_area_align(BOOT_SCRATCH_AREA(&boot_data));
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000224 if (align > elem_sz) {
225 elem_sz = align;
226 }
227
228 return elem_sz;
229}
230
231/**
232 * Determines the sector layout of both image slots and the scratch area.
233 * This information is necessary for calculating the number of bytes to erase
234 * and copy during an image swap. The information collected during this
235 * function is used to populate the boot_data global.
236 */
237static int
238boot_read_sectors(void)
239{
240 int rc;
241
David Vincze8bdfc2d2019-03-18 15:49:23 +0100242 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_PRIMARY);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000243 if (rc != 0) {
244 return BOOT_EFLASH;
245 }
246
David Vincze8bdfc2d2019-03-18 15:49:23 +0100247 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_SECONDARY);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000248 if (rc != 0) {
249 return BOOT_EFLASH;
250 }
251
David Vincze401c7422019-06-21 20:44:05 +0200252 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_SCRATCH);
253 if (rc != 0) {
254 return BOOT_EFLASH;
255 }
256
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000257 BOOT_WRITE_SZ(&boot_data) = boot_write_sz();
258
259 return 0;
260}
261
David Vincze060968d2019-05-23 01:13:14 +0200262/**
263 * Validate image hash/signature and security counter in a slot.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000264 */
265static int
David Vincze401c7422019-06-21 20:44:05 +0200266boot_image_check(struct image_header *hdr, const struct flash_area *fap,
267 struct boot_status *bs)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000268{
269 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
270
David Vincze401c7422019-06-21 20:44:05 +0200271 (void)bs;
272
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000273 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
Tamas Ban0e8ab302019-01-17 11:45:31 +0000274 NULL, 0, NULL)) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000275 return BOOT_EBADIMAGE;
276 }
277 return 0;
278}
279
David Vincze401c7422019-06-21 20:44:05 +0200280/*
281 * Check that a memory area consists of a given value.
282 */
283static inline bool
284boot_data_is_set_to(uint8_t val, void *data, size_t len)
David Vincze39e78552018-10-10 17:10:01 +0200285{
286 uint8_t i;
David Vincze401c7422019-06-21 20:44:05 +0200287 uint8_t *p = (uint8_t *)data;
288 for (i = 0; i < len; i++) {
289 if (val != p[i]) {
290 return false;
David Vincze39e78552018-10-10 17:10:01 +0200291 }
292 }
David Vincze401c7422019-06-21 20:44:05 +0200293 return true;
David Vincze39e78552018-10-10 17:10:01 +0200294}
295
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000296static int
David Vincze401c7422019-06-21 20:44:05 +0200297boot_check_header_erased(int slot)
298{
299 const struct flash_area *fap;
300 struct image_header *hdr;
301 uint8_t erased_val;
302 int rc;
303
304 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
305 if (rc != 0) {
306 return -1;
307 }
308
309 erased_val = flash_area_erased_val(fap);
310 flash_area_close(fap);
311
312 hdr = boot_img_hdr(&boot_data, slot);
313 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic,
314 sizeof(hdr->ih_magic))) {
315 return -1;
316 }
317
318 return 0;
319}
320
321static int
322boot_validate_slot(int slot, struct boot_status *bs)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000323{
324 const struct flash_area *fap;
325 struct image_header *hdr;
326 int rc;
327
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000328 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
329 if (rc != 0) {
330 return BOOT_EFLASH;
331 }
332
David Vincze39e78552018-10-10 17:10:01 +0200333 hdr = boot_img_hdr(&boot_data, slot);
David Vincze401c7422019-06-21 20:44:05 +0200334 if ((boot_check_header_erased(slot) == 0) ||
335 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100336 /* No bootable image in slot; continue booting from the primary slot. */
David Vincze401c7422019-06-21 20:44:05 +0200337 rc = -1;
338 goto out;
David Vincze39e78552018-10-10 17:10:01 +0200339 }
340
Tamas Bana9de4a62018-09-18 08:09:45 +0100341 if ((hdr->ih_magic != IMAGE_MAGIC ||
David Vincze401c7422019-06-21 20:44:05 +0200342 boot_image_check(hdr, fap, bs) != 0)) {
343 if (slot != BOOT_PRIMARY_SLOT) {
David Vincze26e8c8a2018-08-28 16:59:41 +0200344 rc = flash_area_erase(fap, 0, fap->fa_size);
345 if(rc != 0) {
David Vincze401c7422019-06-21 20:44:05 +0200346 rc = BOOT_EFLASH;
347 goto out;
David Vincze26e8c8a2018-08-28 16:59:41 +0200348 }
David Vincze8bdfc2d2019-03-18 15:49:23 +0100349 /* Image in the secondary slot is invalid. Erase the image and
350 * continue booting from the primary slot.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000351 */
352 }
David Vincze8bdfc2d2019-03-18 15:49:23 +0100353 BOOT_LOG_ERR("Authentication failed! Image in the %s slot is not valid."
354 , (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Vincze401c7422019-06-21 20:44:05 +0200355 rc = -1;
356 goto out;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000357 }
358
David Vincze8bdfc2d2019-03-18 15:49:23 +0100359 /* Image in the secondary slot is valid. */
David Vincze401c7422019-06-21 20:44:05 +0200360 rc = 0;
361
362out:
363 flash_area_close(fap);
364 return rc;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000365}
366
David Vincze060968d2019-05-23 01:13:14 +0200367/**
368 * Updates the stored security counter value with the image's security counter
369 * value which resides in the given slot if it's greater than the stored value.
370 *
371 * @param slot Slot number of the image.
372 * @param hdr Pointer to the image header structure of the image that is
373 * currently stored in the given slot.
374 *
375 * @return 0 on success; nonzero on failure.
376 */
377static int
378boot_update_security_counter(int slot, struct image_header *hdr)
379{
380 const struct flash_area *fap = NULL;
381 uint32_t img_security_cnt;
382 int rc;
383
384 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
385 if (rc != 0) {
386 rc = BOOT_EFLASH;
387 goto done;
388 }
389
390 rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt);
391 if (rc != 0) {
392 goto done;
393 }
394
David Vincze0b41ee22019-06-28 14:28:20 +0200395 rc = boot_nv_security_counter_update(current_image, img_security_cnt);
David Vincze060968d2019-05-23 01:13:14 +0200396 if (rc != 0) {
397 goto done;
398 }
399
400done:
401 flash_area_close(fap);
402 return rc;
403}
404
Oliver Swedef9982442018-08-24 18:37:44 +0100405#if !defined(MCUBOOT_NO_SWAP) && !defined(MCUBOOT_OVERWRITE_ONLY)
406/*
407 * Compute the total size of the given image. Includes the size of
408 * the TLVs.
409 */
410static int
411boot_read_image_size(int slot, struct image_header *hdr, uint32_t *size)
412{
413 const struct flash_area *fap = NULL;
414 struct image_tlv_info info;
415 int area_id;
416 int rc;
417
418 area_id = flash_area_id_from_image_slot(slot);
419 rc = flash_area_open(area_id, &fap);
420 if (rc != 0) {
421 rc = BOOT_EFLASH;
422 goto done;
423 }
424
425 rc = flash_area_read(fap, hdr->ih_hdr_size + hdr->ih_img_size,
426 &info, sizeof(info));
427 if (rc != 0) {
428 rc = BOOT_EFLASH;
429 goto done;
430 }
431 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
432 rc = BOOT_EBADIMAGE;
433 goto done;
434 }
435 *size = hdr->ih_hdr_size + hdr->ih_img_size + info.it_tlv_tot;
436 rc = 0;
437
438done:
439 flash_area_close(fap);
440 return rc;
441}
442#endif /* !MCUBOOT_NO_SWAP && !MCUBOOT_OVERWRITE_ONLY */
443
444#if !defined(MCUBOOT_NO_SWAP) && !defined(MCUBOOT_RAM_LOADING)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000445/**
Tamas Ban581034a2017-12-19 19:54:37 +0000446 * Determines where in flash the most recent boot status is stored. The boot
Tamas Banf70ef8c2017-12-19 15:35:09 +0000447 * status is necessary for completing a swap that was interrupted by a boot
448 * loader reset.
449 *
David Vincze401c7422019-06-21 20:44:05 +0200450 * @return A BOOT_STATUS_SOURCE_[...] code indicating where status should
451 * be read from.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000452 */
453static int
454boot_status_source(void)
455{
456 const struct boot_status_table *table;
457 struct boot_swap_state state_scratch;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100458 struct boot_swap_state state_primary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000459 int rc;
David Vincze39e78552018-10-10 17:10:01 +0200460 size_t i;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000461 uint8_t source;
462
David Vincze8bdfc2d2019-03-18 15:49:23 +0100463 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY,
464 &state_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000465 assert(rc == 0);
466
467 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
468 assert(rc == 0);
469
David Vincze401c7422019-06-21 20:44:05 +0200470 BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000471 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
472
473 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
474 table = &boot_status_tables[i];
475
David Vincze401c7422019-06-21 20:44:05 +0200476 if (boot_magic_compatible_check(table->bst_magic_primary_slot,
477 state_primary_slot.magic) &&
478 boot_magic_compatible_check(table->bst_magic_scratch,
479 state_scratch.magic) &&
David Vincze8bdfc2d2019-03-18 15:49:23 +0100480 (table->bst_copy_done_primary_slot == BOOT_FLAG_ANY ||
481 table->bst_copy_done_primary_slot == state_primary_slot.copy_done))
482 {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000483 source = table->bst_status_source;
David Vincze7384ee72019-07-23 17:00:42 +0200484
485#if (BOOT_IMAGE_NUMBER > 1)
486 /* In case of multi-image boot it can happen that if boot status
487 * info is found on scratch area then it does not belong to the
488 * currently examined image.
489 */
490 if (source == BOOT_STATUS_SOURCE_SCRATCH &&
491 state_scratch.image_num != current_image) {
492 source = BOOT_STATUS_SOURCE_NONE;
493 }
494#endif
495
Tamas Banf70ef8c2017-12-19 15:35:09 +0000496 BOOT_LOG_INF("Boot source: %s",
497 source == BOOT_STATUS_SOURCE_NONE ? "none" :
498 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
David Vincze8bdfc2d2019-03-18 15:49:23 +0100499 source == BOOT_STATUS_SOURCE_PRIMARY_SLOT ?
500 "primary slot" : "BUG; can't happen");
Tamas Banf70ef8c2017-12-19 15:35:09 +0000501 return source;
502 }
503 }
504
505 BOOT_LOG_INF("Boot source: none");
506 return BOOT_STATUS_SOURCE_NONE;
507}
508
David Vincze401c7422019-06-21 20:44:05 +0200509/*
510 * Slots are compatible when all sectors that store upto to size of the image
511 * round up to sector size, in both slot's are able to fit in the scratch
512 * area, and have sizes that are a multiple of each other (powers of two
513 * presumably!).
Tamas Banf70ef8c2017-12-19 15:35:09 +0000514 */
515static int
Tamas Banf70ef8c2017-12-19 15:35:09 +0000516boot_slots_compatible(void)
517{
David Vincze401c7422019-06-21 20:44:05 +0200518 size_t num_sectors_primary;
519 size_t num_sectors_secondary;
520 size_t sz0, sz1;
521 size_t primary_slot_sz, secondary_slot_sz;
522 size_t scratch_sz;
523 size_t i, j;
524 int8_t smaller;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000525
David Vincze401c7422019-06-21 20:44:05 +0200526 num_sectors_primary =
527 boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT);
528 num_sectors_secondary =
529 boot_img_num_sectors(&boot_data, BOOT_SECONDARY_SLOT);
530 if ((num_sectors_primary > BOOT_MAX_IMG_SECTORS) ||
531 (num_sectors_secondary > BOOT_MAX_IMG_SECTORS)) {
David Vincze39e78552018-10-10 17:10:01 +0200532 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
Tamas Banf70ef8c2017-12-19 15:35:09 +0000533 return 0;
534 }
David Vincze39e78552018-10-10 17:10:01 +0200535
David Vincze401c7422019-06-21 20:44:05 +0200536 scratch_sz = boot_scratch_area_size(&boot_data);
537
538 /*
539 * The following loop scans all sectors in a linear fashion, assuring that
540 * for each possible sector in each slot, it is able to fit in the other
541 * slot's sector or sectors. Slot's should be compatible as long as any
542 * number of a slot's sectors are able to fit into another, which only
543 * excludes cases where sector sizes are not a multiple of each other.
544 */
545 i = sz0 = primary_slot_sz = 0;
546 j = sz1 = secondary_slot_sz = 0;
547 smaller = 0;
548 while (i < num_sectors_primary || j < num_sectors_secondary) {
549 if (sz0 == sz1) {
550 sz0 += boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
551 sz1 += boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT, j);
552 i++;
553 j++;
554 } else if (sz0 < sz1) {
555 sz0 += boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
556 /* Guarantee that multiple sectors of the secondary slot
557 * fit into the primary slot.
558 */
559 if (smaller == 2) {
560 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible"
561 " sectors");
562 return 0;
563 }
564 smaller = 1;
565 i++;
566 } else {
567 sz1 += boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT, j);
568 /* Guarantee that multiple sectors of the primary slot
569 * fit into the secondary slot.
570 */
571 if (smaller == 1) {
572 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible"
573 " sectors");
574 return 0;
575 }
576 smaller = 2;
577 j++;
578 }
579 if (sz0 == sz1) {
580 primary_slot_sz += sz0;
581 secondary_slot_sz += sz1;
582 /* Scratch has to fit each swap operation to the size of the larger
583 * sector among the primary slot and the secondary slot.
584 */
585 if (sz0 > scratch_sz || sz1 > scratch_sz) {
586 BOOT_LOG_WRN("Cannot upgrade: not all sectors fit inside"
587 " scratch");
588 return 0;
589 }
590 smaller = sz0 = sz1 = 0;
591 }
David Vincze39e78552018-10-10 17:10:01 +0200592 }
593
David Vincze401c7422019-06-21 20:44:05 +0200594 if ((i != num_sectors_primary) ||
595 (j != num_sectors_secondary) ||
596 (primary_slot_sz != secondary_slot_sz)) {
597 BOOT_LOG_WRN("Cannot upgrade: slots are not compatible");
598 return 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000599 }
600
601 return 1;
602}
603
Tamas Banf70ef8c2017-12-19 15:35:09 +0000604static uint32_t
605boot_status_internal_off(int idx, int state, int elem_sz)
606{
607 int idx_sz;
608
609 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
610
David Vincze39e78552018-10-10 17:10:01 +0200611 return (idx - BOOT_STATUS_IDX_0) * idx_sz +
612 (state - BOOT_STATUS_STATE_0) * elem_sz;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000613}
614
615/**
616 * Reads the status of a partially-completed swap, if any. This is necessary
617 * to recover in case the boot lodaer was reset in the middle of a swap
618 * operation.
619 */
620static int
621boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs)
622{
623 uint32_t off;
624 uint8_t status;
625 int max_entries;
626 int found;
David Vincze39e78552018-10-10 17:10:01 +0200627 int found_idx;
628 int invalid;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000629 int rc;
630 int i;
631
632 off = boot_status_off(fap);
633 max_entries = boot_status_entries(fap);
634
635 found = 0;
David Vincze39e78552018-10-10 17:10:01 +0200636 found_idx = 0;
637 invalid = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000638 for (i = 0; i < max_entries; i++) {
David Vincze39e78552018-10-10 17:10:01 +0200639 rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(&boot_data),
640 &status, 1);
641 if (rc < 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000642 return BOOT_EFLASH;
643 }
644
David Vincze39e78552018-10-10 17:10:01 +0200645 if (rc == 1) {
646 if (found && !found_idx) {
647 found_idx = i;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000648 }
649 } else if (!found) {
650 found = 1;
David Vincze39e78552018-10-10 17:10:01 +0200651 } else if (found_idx) {
652 invalid = 1;
653 break;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000654 }
655 }
656
David Vincze39e78552018-10-10 17:10:01 +0200657 if (invalid) {
658 /* This means there was an error writing status on the last
659 * swap. Tell user and move on to validation!
660 */
661 BOOT_LOG_ERR("Detected inconsistent status!");
662
David Vincze8bdfc2d2019-03-18 15:49:23 +0100663#if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
664 /* With validation of the primary slot disabled, there is no way
665 * to be sure the swapped primary slot is OK, so abort!
David Vincze39e78552018-10-10 17:10:01 +0200666 */
667 assert(0);
668#endif
669 }
670
Tamas Banf70ef8c2017-12-19 15:35:09 +0000671 if (found) {
David Vincze39e78552018-10-10 17:10:01 +0200672 if (!found_idx) {
673 found_idx = i;
674 }
675 found_idx--;
676 bs->idx = (found_idx / BOOT_STATUS_STATE_COUNT) + 1;
677 bs->state = (found_idx % BOOT_STATUS_STATE_COUNT) + 1;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000678 }
679
680 return 0;
681}
682
683/**
684 * Reads the boot status from the flash. The boot status contains
685 * the current state of an interrupted image copy operation. If the boot
686 * status is not present, or it indicates that previous copy finished,
687 * there is no operation in progress.
688 */
689static int
690boot_read_status(struct boot_status *bs)
691{
692 const struct flash_area *fap;
David Vincze401c7422019-06-21 20:44:05 +0200693 uint32_t off;
David Vincze91b71ef2019-06-24 13:06:47 +0200694 uint8_t swap_info;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000695 int status_loc;
696 int area_id;
697 int rc;
698
David Vincze39e78552018-10-10 17:10:01 +0200699 memset(bs, 0, sizeof *bs);
700 bs->idx = BOOT_STATUS_IDX_0;
701 bs->state = BOOT_STATUS_STATE_0;
David Vincze401c7422019-06-21 20:44:05 +0200702 bs->swap_type = BOOT_SWAP_TYPE_NONE;
David Vincze39e78552018-10-10 17:10:01 +0200703
704#ifdef MCUBOOT_OVERWRITE_ONLY
705 /* Overwrite-only doesn't make use of the swap status area. */
706 return 0;
707#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +0000708
709 status_loc = boot_status_source();
710 switch (status_loc) {
711 case BOOT_STATUS_SOURCE_NONE:
712 return 0;
713
714 case BOOT_STATUS_SOURCE_SCRATCH:
715 area_id = FLASH_AREA_IMAGE_SCRATCH;
716 break;
717
David Vincze8bdfc2d2019-03-18 15:49:23 +0100718 case BOOT_STATUS_SOURCE_PRIMARY_SLOT:
719 area_id = FLASH_AREA_IMAGE_PRIMARY;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000720 break;
721
722 default:
723 assert(0);
724 return BOOT_EBADARGS;
725 }
726
727 rc = flash_area_open(area_id, &fap);
728 if (rc != 0) {
729 return BOOT_EFLASH;
730 }
731
732 rc = boot_read_status_bytes(fap, bs);
David Vincze401c7422019-06-21 20:44:05 +0200733 if (rc == 0) {
David Vincze91b71ef2019-06-24 13:06:47 +0200734 off = boot_swap_info_off(fap);
735 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
David Vincze401c7422019-06-21 20:44:05 +0200736 if (rc == 1) {
David Vincze91b71ef2019-06-24 13:06:47 +0200737 BOOT_SET_SWAP_INFO(swap_info, 0, BOOT_SWAP_TYPE_NONE);
David Vincze401c7422019-06-21 20:44:05 +0200738 rc = 0;
739 }
David Vincze91b71ef2019-06-24 13:06:47 +0200740
741 /* Extract the swap type info */
742 bs->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
David Vincze401c7422019-06-21 20:44:05 +0200743 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000744
745 flash_area_close(fap);
David Vincze39e78552018-10-10 17:10:01 +0200746
Tamas Banf70ef8c2017-12-19 15:35:09 +0000747 return rc;
748}
749
750/**
751 * Writes the supplied boot status to the flash file system. The boot status
752 * contains the current state of an in-progress image copy operation.
753 *
754 * @param bs The boot status to write.
755 *
756 * @return 0 on success; nonzero on failure.
757 */
758int
759boot_write_status(struct boot_status *bs)
760{
Tamas Ban581034a2017-12-19 19:54:37 +0000761 const struct flash_area *fap = NULL;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000762 uint32_t off;
763 int area_id;
764 int rc;
765 uint8_t buf[BOOT_MAX_ALIGN];
Raef Coles204c5b42019-09-05 09:18:47 +0100766 uint32_t align;
David Vincze39e78552018-10-10 17:10:01 +0200767 uint8_t erased_val;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000768
769 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze8bdfc2d2019-03-18 15:49:23 +0100770 * the trailer. Since in the last step the primary slot is erased, the
771 * first two status writes go to the scratch which will be copied to
772 * the primary slot!
Tamas Banf70ef8c2017-12-19 15:35:09 +0000773 */
774
775 if (bs->use_scratch) {
776 /* Write to scratch. */
777 area_id = FLASH_AREA_IMAGE_SCRATCH;
778 } else {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100779 /* Write to the primary slot. */
780 area_id = FLASH_AREA_IMAGE_PRIMARY;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000781 }
782
783 rc = flash_area_open(area_id, &fap);
784 if (rc != 0) {
785 rc = BOOT_EFLASH;
786 goto done;
787 }
788
789 off = boot_status_off(fap) +
790 boot_status_internal_off(bs->idx, bs->state,
791 BOOT_WRITE_SZ(&boot_data));
792
Tamas Banc3828852018-02-01 12:24:16 +0000793 align = flash_area_align(fap);
David Vincze39e78552018-10-10 17:10:01 +0200794 erased_val = flash_area_erased_val(fap);
795 memset(buf, erased_val, BOOT_MAX_ALIGN);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000796 buf[0] = bs->state;
797
798 rc = flash_area_write(fap, off, buf, align);
799 if (rc != 0) {
800 rc = BOOT_EFLASH;
801 goto done;
802 }
803
804 rc = 0;
805
806done:
807 flash_area_close(fap);
808 return rc;
809}
810
Tamas Banf70ef8c2017-12-19 15:35:09 +0000811/**
812 * Determines which swap operation to perform, if any. If it is determined
David Vincze8bdfc2d2019-03-18 15:49:23 +0100813 * that a swap operation is required, the image in the secondary slot is checked
814 * for validity. If the image in the secondary slot is invalid, it is erased,
815 * and a swap type of "none" is indicated.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000816 *
817 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
818 */
819static int
David Vincze401c7422019-06-21 20:44:05 +0200820boot_validated_swap_type(struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000821{
822 int swap_type;
823
824 swap_type = boot_swap_type();
825 switch (swap_type) {
826 case BOOT_SWAP_TYPE_TEST:
827 case BOOT_SWAP_TYPE_PERM:
828 case BOOT_SWAP_TYPE_REVERT:
David Vincze8bdfc2d2019-03-18 15:49:23 +0100829 /* Boot loader wants to switch to the secondary slot.
830 * Ensure image is valid.
831 */
David Vincze401c7422019-06-21 20:44:05 +0200832 if (boot_validate_slot(BOOT_SECONDARY_SLOT, bs) != 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000833 swap_type = BOOT_SWAP_TYPE_FAIL;
834 }
835 }
836
837 return swap_type;
838}
839
840/**
841 * Calculates the number of sectors the scratch area can contain. A "last"
842 * source sector is specified because images are copied backwards in flash
843 * (final index to index number 0).
844 *
845 * @param last_sector_idx The index of the last source sector
846 * (inclusive).
847 * @param out_first_sector_idx The index of the first source sector
848 * (inclusive) gets written here.
849 *
850 * @return The number of bytes comprised by the
851 * [first-sector, last-sector] range.
852 */
853#ifndef MCUBOOT_OVERWRITE_ONLY
854static uint32_t
855boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
856{
857 size_t scratch_sz;
858 uint32_t new_sz;
859 uint32_t sz;
860 int i;
861
862 sz = 0;
863
864 scratch_sz = boot_scratch_area_size(&boot_data);
865 for (i = last_sector_idx; i >= 0; i--) {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100866 new_sz = sz + boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
David Vincze401c7422019-06-21 20:44:05 +0200867 /*
868 * The secondary slot is not being checked here, because
869 * `boot_slots_compatible` already provides assurance that the copy size
870 * will be compatible with the primary slot and scratch.
871 */
Tamas Banf70ef8c2017-12-19 15:35:09 +0000872 if (new_sz > scratch_sz) {
873 break;
874 }
875 sz = new_sz;
876 }
877
878 /* i currently refers to a sector that doesn't fit or it is -1 because all
879 * sectors have been processed. In both cases, exclude sector i.
880 */
881 *out_first_sector_idx = i + 1;
882 return sz;
883}
884#endif /* !MCUBOOT_OVERWRITE_ONLY */
885
886/**
David Vinczef7641fa2018-09-04 18:29:46 +0200887 * Erases a region of flash.
888 *
David Vincze401c7422019-06-21 20:44:05 +0200889 * @param flash_area The flash_area containing the region to erase.
David Vinczef7641fa2018-09-04 18:29:46 +0200890 * @param off The offset within the flash area to start the
891 * erase.
892 * @param sz The number of bytes to erase.
893 *
894 * @return 0 on success; nonzero on failure.
895 */
David Vincze401c7422019-06-21 20:44:05 +0200896static inline int
897boot_erase_sector(const struct flash_area *fap, uint32_t off, uint32_t sz)
David Vinczef7641fa2018-09-04 18:29:46 +0200898{
David Vincze401c7422019-06-21 20:44:05 +0200899 return flash_area_erase(fap, off, sz);
David Vinczef7641fa2018-09-04 18:29:46 +0200900}
901
902/**
Tamas Banf70ef8c2017-12-19 15:35:09 +0000903 * Copies the contents of one flash region to another. You must erase the
904 * destination region prior to calling this function.
905 *
906 * @param flash_area_id_src The ID of the source flash area.
907 * @param flash_area_id_dst The ID of the destination flash area.
908 * @param off_src The offset within the source flash area to
909 * copy from.
910 * @param off_dst The offset within the destination flash area to
911 * copy to.
912 * @param sz The number of bytes to copy.
913 *
914 * @return 0 on success; nonzero on failure.
915 */
916static int
David Vincze401c7422019-06-21 20:44:05 +0200917boot_copy_sector(const struct flash_area *fap_src,
918 const struct flash_area *fap_dst,
Tamas Banf70ef8c2017-12-19 15:35:09 +0000919 uint32_t off_src, uint32_t off_dst, uint32_t sz)
920{
Tamas Banf70ef8c2017-12-19 15:35:09 +0000921 uint32_t bytes_copied;
922 int chunk_sz;
923 int rc;
924
925 static uint8_t buf[1024];
926
Tamas Banf70ef8c2017-12-19 15:35:09 +0000927 bytes_copied = 0;
928 while (bytes_copied < sz) {
Tamas Ban581034a2017-12-19 19:54:37 +0000929 if (sz - bytes_copied > sizeof(buf)) {
930 chunk_sz = sizeof(buf);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000931 } else {
932 chunk_sz = sz - bytes_copied;
933 }
934
935 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
936 if (rc != 0) {
David Vincze401c7422019-06-21 20:44:05 +0200937 return BOOT_EFLASH;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000938 }
939
940 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
941 if (rc != 0) {
David Vincze401c7422019-06-21 20:44:05 +0200942 return BOOT_EFLASH;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000943 }
944
945 bytes_copied += chunk_sz;
946 }
947
David Vincze401c7422019-06-21 20:44:05 +0200948 return 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000949}
950
951#ifndef MCUBOOT_OVERWRITE_ONLY
952static inline int
David Vincze401c7422019-06-21 20:44:05 +0200953boot_status_init(const struct flash_area *fap, const struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000954{
Tamas Banf70ef8c2017-12-19 15:35:09 +0000955 struct boot_swap_state swap_state;
956 int rc;
957
David Vincze401c7422019-06-21 20:44:05 +0200958 BOOT_LOG_DBG("initializing status; fa_id=%d", fap->fa_id);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000959
David Vincze8bdfc2d2019-03-18 15:49:23 +0100960 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY, &swap_state);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000961 assert(rc == 0);
962
David Vincze401c7422019-06-21 20:44:05 +0200963 if (bs->swap_type != BOOT_SWAP_TYPE_NONE) {
David Vincze91b71ef2019-06-24 13:06:47 +0200964 rc = boot_write_swap_info(fap,
965 bs->swap_type,
David Vincze7384ee72019-07-23 17:00:42 +0200966 current_image);
David Vincze401c7422019-06-21 20:44:05 +0200967 assert(rc == 0);
968 }
969
Tamas Banf70ef8c2017-12-19 15:35:09 +0000970 if (swap_state.image_ok == BOOT_FLAG_SET) {
971 rc = boot_write_image_ok(fap);
972 assert(rc == 0);
973 }
974
975 rc = boot_write_swap_size(fap, bs->swap_size);
976 assert(rc == 0);
977
978 rc = boot_write_magic(fap);
979 assert(rc == 0);
980
Tamas Banf70ef8c2017-12-19 15:35:09 +0000981 return 0;
982}
David Vinczef7641fa2018-09-04 18:29:46 +0200983
984static int
David Vincze401c7422019-06-21 20:44:05 +0200985boot_erase_trailer_sectors(const struct flash_area *fap)
David Vinczef7641fa2018-09-04 18:29:46 +0200986{
987 uint8_t slot;
David Vincze401c7422019-06-21 20:44:05 +0200988 uint32_t sector;
989 uint32_t trailer_sz;
990 uint32_t total_sz;
991 uint32_t off;
992 uint32_t sz;
David Vinczebb207982019-08-21 15:13:04 +0200993 int fa_id_primary;
994 int fa_id_secondary;
David Vinczef7641fa2018-09-04 18:29:46 +0200995 int rc;
996
David Vincze401c7422019-06-21 20:44:05 +0200997 BOOT_LOG_DBG("erasing trailer; fa_id=%d", fap->fa_id);
998
David Vinczebb207982019-08-21 15:13:04 +0200999 fa_id_primary = flash_area_id_from_image_slot(BOOT_PRIMARY_SLOT);
1000 fa_id_secondary = flash_area_id_from_image_slot(BOOT_SECONDARY_SLOT);
1001
1002 if (fap->fa_id == fa_id_primary) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001003 slot = BOOT_PRIMARY_SLOT;
David Vinczebb207982019-08-21 15:13:04 +02001004 } else if (fap->fa_id == fa_id_secondary) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001005 slot = BOOT_SECONDARY_SLOT;
David Vinczebb207982019-08-21 15:13:04 +02001006 } else {
David Vinczef7641fa2018-09-04 18:29:46 +02001007 return BOOT_EFLASH;
1008 }
1009
David Vincze401c7422019-06-21 20:44:05 +02001010 /* delete starting from last sector and moving to beginning */
1011 sector = boot_img_num_sectors(&boot_data, slot) - 1;
1012 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(&boot_data));
1013 total_sz = 0;
1014 do {
1015 sz = boot_img_sector_size(&boot_data, slot, sector);
1016 off = boot_img_sector_off(&boot_data, slot, sector);
1017 rc = boot_erase_sector(fap, off, sz);
1018 assert(rc == 0);
1019
1020 sector--;
1021 total_sz += sz;
1022 } while (total_sz < trailer_sz);
David Vinczef7641fa2018-09-04 18:29:46 +02001023
1024 return rc;
1025}
1026#endif /* !MCUBOOT_OVERWRITE_ONLY */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001027
Tamas Banf70ef8c2017-12-19 15:35:09 +00001028/**
1029 * Swaps the contents of two flash regions within the two image slots.
1030 *
1031 * @param idx The index of the first sector in the range of
1032 * sectors being swapped.
1033 * @param sz The number of bytes to swap.
1034 * @param bs The current boot status. This struct gets
1035 * updated according to the outcome.
1036 *
1037 * @return 0 on success; nonzero on failure.
1038 */
1039#ifndef MCUBOOT_OVERWRITE_ONLY
1040static void
1041boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
1042{
David Vincze401c7422019-06-21 20:44:05 +02001043 const struct flash_area *fap_primary_slot;
1044 const struct flash_area *fap_secondary_slot;
1045 const struct flash_area *fap_scratch;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001046 uint32_t copy_sz;
1047 uint32_t trailer_sz;
1048 uint32_t img_off;
1049 uint32_t scratch_trailer_off;
1050 struct boot_swap_state swap_state;
1051 size_t last_sector;
David Vincze401c7422019-06-21 20:44:05 +02001052 bool erase_scratch;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001053 int rc;
1054
1055 /* Calculate offset from start of image area. */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001056 img_off = boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT, idx);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001057
1058 copy_sz = sz;
David Vincze401c7422019-06-21 20:44:05 +02001059 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Tamas Banf70ef8c2017-12-19 15:35:09 +00001060
David Vincze401c7422019-06-21 20:44:05 +02001061 /* sz in this function is always sized on a multiple of the sector size.
1062 * The check against the start offset of the last sector
Tamas Banf70ef8c2017-12-19 15:35:09 +00001063 * is to determine if we're swapping the last sector. The last sector
1064 * needs special handling because it's where the trailer lives. If we're
1065 * copying it, we need to use scratch to write the trailer temporarily.
1066 *
1067 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
1068 * controls if special handling is needed (swapping last sector).
1069 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001070 last_sector = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT) - 1;
David Vincze7384ee72019-07-23 17:00:42 +02001071 if ((img_off + sz) >
1072 boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT, last_sector)) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001073 copy_sz -= trailer_sz;
1074 }
1075
David Vincze39e78552018-10-10 17:10:01 +02001076 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001077
David Vincze401c7422019-06-21 20:44:05 +02001078 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap_primary_slot);
1079 assert (rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001080
David Vincze401c7422019-06-21 20:44:05 +02001081 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap_secondary_slot);
1082 assert (rc == 0);
1083
1084 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch);
1085 assert (rc == 0);
1086
1087 if (bs->state == BOOT_STATUS_STATE_0) {
1088 BOOT_LOG_DBG("erasing scratch area");
1089 rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001090 assert(rc == 0);
1091
David Vincze39e78552018-10-10 17:10:01 +02001092 if (bs->idx == BOOT_STATUS_IDX_0) {
David Vincze401c7422019-06-21 20:44:05 +02001093 /* Write a trailer to the scratch area, even if we don't need the
1094 * scratch area for status. We need a temporary place to store the
1095 * `swap-type` while we erase the primary trailer.
1096 */
1097 rc = boot_status_init(fap_scratch, bs);
1098 assert(rc == 0);
1099
1100 if (!bs->use_scratch) {
1101 /* Prepare the primary status area... here it is known that the
1102 * last sector is not being used by the image data so it's safe
1103 * to erase.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001104 */
David Vincze401c7422019-06-21 20:44:05 +02001105 rc = boot_erase_trailer_sectors(fap_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001106 assert(rc == 0);
1107
David Vincze401c7422019-06-21 20:44:05 +02001108 rc = boot_status_init(fap_primary_slot, bs);
1109 assert(rc == 0);
1110
1111 /* Erase the temporary trailer from the scratch area. */
1112 rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size);
1113 assert(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001114 }
1115 }
1116
David Vincze401c7422019-06-21 20:44:05 +02001117 rc = boot_copy_sector(fap_secondary_slot, fap_scratch,
1118 img_off, 0, copy_sz);
1119 assert(rc == 0);
1120
David Vincze39e78552018-10-10 17:10:01 +02001121 bs->state = BOOT_STATUS_STATE_1;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001122 rc = boot_write_status(bs);
David Vincze39e78552018-10-10 17:10:01 +02001123 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001124 }
1125
David Vincze39e78552018-10-10 17:10:01 +02001126 if (bs->state == BOOT_STATUS_STATE_1) {
David Vincze401c7422019-06-21 20:44:05 +02001127 rc = boot_erase_sector(fap_secondary_slot, img_off, sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001128 assert(rc == 0);
1129
David Vincze401c7422019-06-21 20:44:05 +02001130 rc = boot_copy_sector(fap_primary_slot, fap_secondary_slot,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001131 img_off, img_off, copy_sz);
1132 assert(rc == 0);
1133
David Vincze39e78552018-10-10 17:10:01 +02001134 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001135 /* If not all sectors of the slot are being swapped,
David Vincze8bdfc2d2019-03-18 15:49:23 +01001136 * guarantee here that only the primary slot will have the state.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001137 */
David Vincze401c7422019-06-21 20:44:05 +02001138 rc = boot_erase_trailer_sectors(fap_secondary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001139 assert(rc == 0);
1140 }
1141
David Vincze39e78552018-10-10 17:10:01 +02001142 bs->state = BOOT_STATUS_STATE_2;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001143 rc = boot_write_status(bs);
David Vincze39e78552018-10-10 17:10:01 +02001144 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001145 }
1146
David Vincze39e78552018-10-10 17:10:01 +02001147 if (bs->state == BOOT_STATUS_STATE_2) {
David Vincze401c7422019-06-21 20:44:05 +02001148 rc = boot_erase_sector(fap_primary_slot, img_off, sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001149 assert(rc == 0);
1150
David Vincze401c7422019-06-21 20:44:05 +02001151 /* NOTE: If this is the final sector, we exclude the image trailer from
1152 * this copy (copy_sz was truncated earlier).
1153 */
1154 rc = boot_copy_sector(fap_scratch, fap_primary_slot,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001155 0, img_off, copy_sz);
1156 assert(rc == 0);
1157
1158 if (bs->use_scratch) {
David Vincze401c7422019-06-21 20:44:05 +02001159 scratch_trailer_off = boot_status_off(fap_scratch);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001160
1161 /* copy current status that is being maintained in scratch */
David Vincze401c7422019-06-21 20:44:05 +02001162 rc = boot_copy_sector(fap_scratch, fap_primary_slot,
1163 scratch_trailer_off, img_off + copy_sz,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001164 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data));
David Vincze39e78552018-10-10 17:10:01 +02001165 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001166
1167 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1168 &swap_state);
1169 assert(rc == 0);
1170
1171 if (swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze401c7422019-06-21 20:44:05 +02001172 rc = boot_write_image_ok(fap_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001173 assert(rc == 0);
1174 }
1175
David Vincze401c7422019-06-21 20:44:05 +02001176 if (swap_state.swap_type != BOOT_SWAP_TYPE_NONE) {
David Vincze91b71ef2019-06-24 13:06:47 +02001177 rc = boot_write_swap_info(fap_primary_slot,
1178 swap_state.swap_type,
David Vincze7384ee72019-07-23 17:00:42 +02001179 current_image);
David Vincze401c7422019-06-21 20:44:05 +02001180 assert(rc == 0);
1181 }
1182
1183 rc = boot_write_swap_size(fap_primary_slot, bs->swap_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001184 assert(rc == 0);
1185
David Vincze401c7422019-06-21 20:44:05 +02001186 rc = boot_write_magic(fap_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001187 assert(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001188 }
1189
David Vincze401c7422019-06-21 20:44:05 +02001190 /* If we wrote a trailer to the scratch area, erase it after we persist
1191 * a trailer to the primary slot. We do this to prevent mcuboot from
1192 * reading a stale status from the scratch area in case of immediate
1193 * reset.
1194 */
1195 erase_scratch = bs->use_scratch;
1196 bs->use_scratch = 0;
1197
Tamas Banf70ef8c2017-12-19 15:35:09 +00001198 bs->idx++;
David Vincze39e78552018-10-10 17:10:01 +02001199 bs->state = BOOT_STATUS_STATE_0;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001200 rc = boot_write_status(bs);
David Vincze39e78552018-10-10 17:10:01 +02001201 BOOT_STATUS_ASSERT(rc == 0);
David Vincze401c7422019-06-21 20:44:05 +02001202
1203 if (erase_scratch) {
1204 rc = boot_erase_sector(fap_scratch, 0, sz);
1205 assert(rc == 0);
1206 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001207 }
David Vincze401c7422019-06-21 20:44:05 +02001208
1209 flash_area_close(fap_primary_slot);
1210 flash_area_close(fap_secondary_slot);
1211 flash_area_close(fap_scratch);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001212}
1213#endif /* !MCUBOOT_OVERWRITE_ONLY */
1214
1215/**
David Vincze401c7422019-06-21 20:44:05 +02001216 * Overwrite primary slot with the image contained in the secondary slot.
1217 * If a prior copy operation was interrupted by a system reset, this function
1218 * redos the copy.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001219 *
1220 * @param bs The current boot status. This function reads
1221 * this struct to determine if it is resuming
1222 * an interrupted swap operation. This
1223 * function writes the updated status to this
1224 * function on return.
1225 *
1226 * @return 0 on success; nonzero on failure.
1227 */
1228#ifdef MCUBOOT_OVERWRITE_ONLY
1229static int
1230boot_copy_image(struct boot_status *bs)
1231{
1232 size_t sect_count;
1233 size_t sect;
1234 int rc;
1235 size_t size = 0;
1236 size_t this_size;
David Vincze39e78552018-10-10 17:10:01 +02001237 size_t last_sector;
David Vincze401c7422019-06-21 20:44:05 +02001238 const struct flash_area *fap_primary_slot;
1239 const struct flash_area *fap_secondary_slot;
David Vincze39e78552018-10-10 17:10:01 +02001240
1241 (void)bs;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001242
David Vincze8bdfc2d2019-03-18 15:49:23 +01001243 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1244 BOOT_LOG_INF("Erasing the primary slot");
Tamas Banf70ef8c2017-12-19 15:35:09 +00001245
David Vincze401c7422019-06-21 20:44:05 +02001246 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap_primary_slot);
1247 assert (rc == 0);
1248
1249 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap_secondary_slot);
1250 assert (rc == 0);
1251
David Vincze8bdfc2d2019-03-18 15:49:23 +01001252 sect_count = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001253 for (sect = 0; sect < sect_count; sect++) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001254 this_size = boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, sect);
David Vincze401c7422019-06-21 20:44:05 +02001255 rc = boot_erase_sector(fap_primary_slot, size, this_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001256 assert(rc == 0);
1257
1258 size += this_size;
1259 }
1260
David Vincze8bdfc2d2019-03-18 15:49:23 +01001261 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1262 size);
David Vincze401c7422019-06-21 20:44:05 +02001263 rc = boot_copy_sector(fap_secondary_slot, fap_primary_slot, 0, 0, size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001264
David Vincze060968d2019-05-23 01:13:14 +02001265 /* Update the stored security counter with the new image's security counter
David Vincze8bdfc2d2019-03-18 15:49:23 +01001266 * value. Both slots hold the new image at this point, but the secondary
1267 * slot's image header must be passed because the read image headers in the
1268 * boot_data structure have not been updated yet.
David Vincze060968d2019-05-23 01:13:14 +02001269 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001270 rc = boot_update_security_counter(BOOT_PRIMARY_SLOT,
1271 boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT));
David Vincze060968d2019-05-23 01:13:14 +02001272 if (rc != 0) {
1273 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
1274 return rc;
1275 }
1276
David Vincze39e78552018-10-10 17:10:01 +02001277 /*
1278 * Erases header and trailer. The trailer is erased because when a new
1279 * image is written without a trailer as is the case when using newt, the
1280 * trailer that was left might trigger a new upgrade.
1281 */
David Vincze401c7422019-06-21 20:44:05 +02001282 BOOT_LOG_DBG("erasing secondary header");
1283 rc = boot_erase_sector(fap_secondary_slot,
David Vincze8bdfc2d2019-03-18 15:49:23 +01001284 boot_img_sector_off(&boot_data,
1285 BOOT_SECONDARY_SLOT, 0),
1286 boot_img_sector_size(&boot_data,
1287 BOOT_SECONDARY_SLOT, 0));
Tamas Banf70ef8c2017-12-19 15:35:09 +00001288 assert(rc == 0);
David Vincze8bdfc2d2019-03-18 15:49:23 +01001289 last_sector = boot_img_num_sectors(&boot_data, BOOT_SECONDARY_SLOT) - 1;
David Vincze401c7422019-06-21 20:44:05 +02001290 BOOT_LOG_DBG("erasing secondary trailer");
1291 rc = boot_erase_sector(fap_secondary_slot,
David Vincze8bdfc2d2019-03-18 15:49:23 +01001292 boot_img_sector_off(&boot_data, BOOT_SECONDARY_SLOT,
1293 last_sector),
1294 boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT,
1295 last_sector));
David Vincze39e78552018-10-10 17:10:01 +02001296 assert(rc == 0);
1297
David Vincze401c7422019-06-21 20:44:05 +02001298 flash_area_close(fap_primary_slot);
1299 flash_area_close(fap_secondary_slot);
1300
David Vincze8bdfc2d2019-03-18 15:49:23 +01001301 /* TODO: Perhaps verify the primary slot's signature again? */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001302
1303 return 0;
1304}
1305#else
David Vincze401c7422019-06-21 20:44:05 +02001306/**
1307 * Swaps the two images in flash. If a prior copy operation was interrupted
1308 * by a system reset, this function completes that operation.
1309 *
1310 * @param bs The current boot status. This function reads
1311 * this struct to determine if it is resuming
1312 * an interrupted swap operation. This
1313 * function writes the updated status to this
1314 * function on return.
1315 *
1316 * @return 0 on success; nonzero on failure.
1317 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001318static int
David Vincze401c7422019-06-21 20:44:05 +02001319boot_swap_image(struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001320{
1321 uint32_t sz;
1322 int first_sector_idx;
1323 int last_sector_idx;
David Vincze401c7422019-06-21 20:44:05 +02001324 int last_idx_secondary_slot;
David Vincze39e78552018-10-10 17:10:01 +02001325 uint32_t swap_idx;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001326 struct image_header *hdr;
1327 uint32_t size;
1328 uint32_t copy_size;
David Vincze401c7422019-06-21 20:44:05 +02001329 uint32_t primary_slot_size;
1330 uint32_t secondary_slot_size;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001331 int rc;
1332
1333 /* FIXME: just do this if asked by user? */
1334
1335 size = copy_size = 0;
1336
David Vincze39e78552018-10-10 17:10:01 +02001337 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001338 /*
1339 * No swap ever happened, so need to find the largest image which
1340 * will be used to determine the amount of sectors to swap.
1341 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001342 hdr = boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001343 if (hdr->ih_magic == IMAGE_MAGIC) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001344 rc = boot_read_image_size(BOOT_PRIMARY_SLOT, hdr, &copy_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001345 assert(rc == 0);
1346 }
1347
David Vincze8bdfc2d2019-03-18 15:49:23 +01001348 hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001349 if (hdr->ih_magic == IMAGE_MAGIC) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001350 rc = boot_read_image_size(BOOT_SECONDARY_SLOT, hdr, &size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001351 assert(rc == 0);
1352 }
1353
1354 if (size > copy_size) {
1355 copy_size = size;
1356 }
1357
1358 bs->swap_size = copy_size;
1359 } else {
1360 /*
1361 * If a swap was under way, the swap_size should already be present
1362 * in the trailer...
1363 */
1364 rc = boot_read_swap_size(&bs->swap_size);
1365 assert(rc == 0);
1366
1367 copy_size = bs->swap_size;
1368 }
1369
David Vincze401c7422019-06-21 20:44:05 +02001370 primary_slot_size = 0;
1371 secondary_slot_size = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001372 last_sector_idx = 0;
David Vincze401c7422019-06-21 20:44:05 +02001373 last_idx_secondary_slot = 0;
1374
1375 /*
1376 * Knowing the size of the largest image between both slots, here we
1377 * find what is the last sector in the primary slot that needs swapping.
1378 * Since we already know that both slots are compatible, the secondary
1379 * slot's last sector is not really required after this check is finished.
1380 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001381 while (1) {
David Vincze401c7422019-06-21 20:44:05 +02001382 if ((primary_slot_size < copy_size) ||
1383 (primary_slot_size < secondary_slot_size)) {
1384 primary_slot_size += boot_img_sector_size(&boot_data,
1385 BOOT_PRIMARY_SLOT,
1386 last_sector_idx);
1387 }
1388 if ((secondary_slot_size < copy_size) ||
1389 (secondary_slot_size < primary_slot_size)) {
1390 secondary_slot_size += boot_img_sector_size(&boot_data,
1391 BOOT_SECONDARY_SLOT,
1392 last_idx_secondary_slot);
1393 }
1394 if (primary_slot_size >= copy_size &&
1395 secondary_slot_size >= copy_size &&
1396 primary_slot_size == secondary_slot_size) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001397 break;
1398 }
1399 last_sector_idx++;
David Vincze401c7422019-06-21 20:44:05 +02001400 last_idx_secondary_slot++;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001401 }
1402
1403 swap_idx = 0;
1404 while (last_sector_idx >= 0) {
1405 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
David Vincze39e78552018-10-10 17:10:01 +02001406 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001407 boot_swap_sectors(first_sector_idx, sz, bs);
1408 }
1409
1410 last_sector_idx = first_sector_idx - 1;
1411 swap_idx++;
1412 }
1413
David Vincze8bdfc2d2019-03-18 15:49:23 +01001414#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
David Vincze39e78552018-10-10 17:10:01 +02001415 if (boot_status_fails > 0) {
David Vincze401c7422019-06-21 20:44:05 +02001416 BOOT_LOG_WRN("%d status write fails performing the swap",
1417 boot_status_fails);
David Vincze39e78552018-10-10 17:10:01 +02001418 }
1419#endif
1420
Tamas Banf70ef8c2017-12-19 15:35:09 +00001421 return 0;
1422}
1423#endif
1424
1425/**
David Vincze8bdfc2d2019-03-18 15:49:23 +01001426 * Marks the image in the primary slot as fully copied.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001427 */
1428#ifndef MCUBOOT_OVERWRITE_ONLY
1429static int
1430boot_set_copy_done(void)
1431{
1432 const struct flash_area *fap;
1433 int rc;
1434
David Vincze8bdfc2d2019-03-18 15:49:23 +01001435 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001436 if (rc != 0) {
1437 return BOOT_EFLASH;
1438 }
1439
1440 rc = boot_write_copy_done(fap);
1441 flash_area_close(fap);
1442 return rc;
1443}
1444#endif /* !MCUBOOT_OVERWRITE_ONLY */
1445
1446/**
David Vincze8bdfc2d2019-03-18 15:49:23 +01001447 * Marks a reverted image in the primary slot as confirmed. This is necessary to
1448 * ensure the status bytes from the image revert operation don't get processed
1449 * on a subsequent boot.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001450 *
1451 * NOTE: image_ok is tested before writing because if there's a valid permanent
David Vincze8bdfc2d2019-03-18 15:49:23 +01001452 * image installed on the primary slot and the new image to be upgrade to has a
1453 * bad sig, image_ok would be overwritten.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001454 */
1455#ifndef MCUBOOT_OVERWRITE_ONLY
1456static int
1457boot_set_image_ok(void)
1458{
1459 const struct flash_area *fap;
1460 struct boot_swap_state state;
1461 int rc;
1462
David Vincze8bdfc2d2019-03-18 15:49:23 +01001463 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001464 if (rc != 0) {
1465 return BOOT_EFLASH;
1466 }
1467
1468 rc = boot_read_swap_state(fap, &state);
1469 if (rc != 0) {
1470 rc = BOOT_EFLASH;
1471 goto out;
1472 }
1473
1474 if (state.image_ok == BOOT_FLAG_UNSET) {
1475 rc = boot_write_image_ok(fap);
1476 }
1477
1478out:
1479 flash_area_close(fap);
1480 return rc;
1481}
1482#endif /* !MCUBOOT_OVERWRITE_ONLY */
1483
David Vinczebb6e3b62019-07-31 14:24:05 +02001484#if (BOOT_IMAGE_NUMBER > 1)
1485/**
1486 * Check the image dependency whether it is satisfied and modify
1487 * the swap type if necessary.
1488 *
1489 * @param dep Image dependency which has to be verified.
1490 *
1491 * @return 0 on success; nonzero on failure.
1492 */
1493static int
1494boot_verify_single_dependency(struct image_dependency *dep)
1495{
1496 struct image_version *dep_version;
1497 size_t dep_slot;
1498 int rc;
1499
1500 /* Determine the source of the image which is the subject of
1501 * the dependency and get it's version. */
1502 dep_slot = (boot_data.swap_type[dep->image_id] != BOOT_SWAP_TYPE_NONE) ?
1503 BOOT_SECONDARY_SLOT : BOOT_PRIMARY_SLOT;
1504 dep_version = &boot_data.imgs[dep->image_id][dep_slot].hdr.ih_ver;
1505
1506 rc = boot_is_version_sufficient(&dep->image_min_version, dep_version);
1507 if (rc != 0) {
1508 /* Dependency not satisfied.
1509 * Modify the swap type to decrease the version number of the image
1510 * (which will be located in the primary slot after the boot process),
1511 * consequently the number of unsatisfied dependencies will be
1512 * decreased or remain the same.
1513 */
1514 switch (BOOT_SWAP_TYPE(&boot_data)) {
1515 case BOOT_SWAP_TYPE_TEST:
1516 case BOOT_SWAP_TYPE_PERM:
1517 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE;
1518 break;
1519 case BOOT_SWAP_TYPE_NONE:
1520 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_REVERT;
1521 break;
1522 default:
1523 break;
1524 }
1525 }
1526
1527 return rc;
1528}
1529
1530/**
1531 * Read all dependency TLVs of an image from the flash and verify
1532 * one after another to see if they are all satisfied.
1533 *
1534 * @param slot Image slot number.
1535 *
1536 * @return 0 on success; nonzero on failure.
1537 */
1538static int
1539boot_verify_all_dependency(uint32_t slot)
1540{
1541 const struct flash_area *fap;
1542 struct image_header *hdr;
1543 struct image_tlv_info info;
1544 struct image_tlv tlv;
1545 struct image_dependency dep;
1546 uint32_t off;
1547 uint32_t end;
1548 bool dep_tlvs_found = false;
1549 int rc;
1550
1551 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
1552 if (rc != 0) {
1553 rc = BOOT_EFLASH;
1554 goto done;
1555 }
1556
1557 hdr = boot_img_hdr(&boot_data, slot);
1558 /* The TLVs come after the image. */
1559 off = hdr->ih_hdr_size + hdr->ih_img_size;
1560
1561 /* The TLV area always starts with an image_tlv_info structure. */
1562 rc = flash_area_read(fap, off, &info, sizeof(info));
1563 if (rc != 0) {
1564 rc = BOOT_EFLASH;
1565 goto done;
1566 }
1567
1568 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
1569 rc = BOOT_EBADIMAGE;
1570 goto done;
1571 }
1572 end = off + info.it_tlv_tot;
1573 off += sizeof(info);
1574
1575 /* Traverse through all of the TLVs to find the dependency TLVs. */
1576 for (; off < end; off += sizeof(tlv) + tlv.it_len) {
1577 rc = flash_area_read(fap, off, &tlv, sizeof(tlv));
1578 if (rc != 0) {
1579 rc = BOOT_EFLASH;
1580 goto done;
1581 }
1582
1583 if (tlv.it_type == IMAGE_TLV_DEPENDENCY) {
1584 if (!dep_tlvs_found) {
1585 dep_tlvs_found = true;
1586 }
1587
1588 if (tlv.it_len != sizeof(dep)) {
1589 rc = BOOT_EBADIMAGE;
1590 goto done;
1591 }
1592
1593 rc = flash_area_read(fap, off + sizeof(tlv), &dep, tlv.it_len);
1594 if (rc != 0) {
1595 rc = BOOT_EFLASH;
1596 goto done;
1597 }
1598
1599 /* Verify dependency and modify the swap type if not satisfied. */
1600 rc = boot_verify_single_dependency(&dep);
1601 if (rc != 0) {
1602 /* Dependency not satisfied. */
1603 goto done;
1604 }
1605
1606 /* Dependency satisfied, no action needed.
1607 * Continue with the next TLV entry.
1608 */
1609 } else if (dep_tlvs_found) {
1610 /* The dependency TLVs are contiguous in the TLV area. If a
1611 * dependency had already been found and the last read TLV
1612 * has a different type then there are no more dependency TLVs.
1613 * The search can be finished.
1614 */
1615 break;
1616 }
1617 }
1618
1619done:
1620 flash_area_close(fap);
1621 return rc;
1622}
1623
1624/**
1625 * Verify whether the image dependencies in the TLV area are
1626 * all satisfied and modify the swap type if necessary.
1627 *
1628 * @return 0 if all dependencies are satisfied,
1629 * nonzero otherwise.
1630 */
1631static int
1632boot_verify_single_image_dependency(void)
1633{
1634 size_t slot;
1635
1636 /* Determine the source of the dependency TLVs. Those dependencies have to
1637 * be checked which belong to the image that will be located in the primary
1638 * slot after the firmware update process.
1639 */
1640 if (BOOT_SWAP_TYPE(&boot_data) != BOOT_SWAP_TYPE_NONE &&
1641 BOOT_SWAP_TYPE(&boot_data) != BOOT_SWAP_TYPE_FAIL) {
1642 slot = BOOT_SECONDARY_SLOT;
1643 } else {
1644 slot = BOOT_PRIMARY_SLOT;
1645 }
1646
1647 return boot_verify_all_dependency(slot);
1648}
1649
1650/**
1651 * Iterate over all the images and verify whether the image dependencies in the
1652 * TLV area are all satisfied and update the related swap type if necessary.
1653 */
1654static void
1655boot_verify_all_image_dependency(void)
1656{
1657 current_image = 0;
1658 int rc;
1659
1660 while (current_image < BOOT_IMAGE_NUMBER) {
1661 rc = boot_verify_single_image_dependency();
1662 if (rc == 0) {
1663 /* All dependencies've been satisfied, continue with next image. */
1664 current_image++;
1665 } else if (rc == BOOT_EBADVERSION) {
1666 /* Dependency check needs to be restarted. */
1667 current_image = 0;
1668 } else {
1669 /* Other error happened, images are inconsistent */
1670 return;
1671 }
1672 }
1673}
1674#endif /* (BOOT_IMAGE_NUMBER > 1) */
1675
Tamas Banf70ef8c2017-12-19 15:35:09 +00001676/**
David Vincze7384ee72019-07-23 17:00:42 +02001677 * Performs a clean (not aborted) image update.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001678 *
David Vincze7384ee72019-07-23 17:00:42 +02001679 * @param bs The current boot status.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001680 *
1681 * @return 0 on success; nonzero on failure.
1682 */
1683static int
David Vincze7384ee72019-07-23 17:00:42 +02001684boot_perform_update(struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001685{
Tamas Banf70ef8c2017-12-19 15:35:09 +00001686 int rc;
1687
David Vincze7384ee72019-07-23 17:00:42 +02001688 /* At this point there are no aborted swaps. */
1689#if defined(MCUBOOT_OVERWRITE_ONLY)
1690 rc = boot_copy_image(bs);
1691#else
1692 rc = boot_swap_image(bs);
1693#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +00001694 assert(rc == 0);
David Vincze7384ee72019-07-23 17:00:42 +02001695
1696#ifndef MCUBOOT_OVERWRITE_ONLY
1697 /* The following state needs image_ok be explicitly set after the
1698 * swap was finished to avoid a new revert.
1699 */
1700 if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_REVERT ||
1701 BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_PERM) {
1702 rc = boot_set_image_ok();
1703 if (rc != 0) {
1704 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
1705 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001706 }
1707
David Vincze7384ee72019-07-23 17:00:42 +02001708 if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_PERM) {
1709 /* Update the stored security counter with the new image's security
1710 * counter value. The primary slot holds the new image at this
1711 * point, but the secondary slot's image header must be passed
1712 * because the read image headers in the boot_data structure have
1713 * not been updated yet.
1714 *
1715 * In case of a permanent image swap mcuboot will never attempt to
1716 * revert the images on the next reboot. Therefore, the security
1717 * counter must be increased right after the image upgrade.
David Vincze401c7422019-06-21 20:44:05 +02001718 */
David Vincze7384ee72019-07-23 17:00:42 +02001719 rc = boot_update_security_counter(BOOT_PRIMARY_SLOT,
1720 boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT));
1721 if (rc != 0) {
1722 BOOT_LOG_ERR("Security counter update failed after "
1723 "image upgrade.");
1724 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001725 }
1726 }
1727
David Vincze7384ee72019-07-23 17:00:42 +02001728 if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_TEST ||
1729 BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_PERM ||
1730 BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_REVERT) {
1731 rc = boot_set_copy_done();
1732 if (rc != 0) {
1733 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
1734 }
1735 }
1736#endif /* !MCUBOOT_OVERWRITE_ONLY */
1737
1738 return rc;
1739}
1740
1741/**
1742 * Completes a previously aborted image swap.
1743 *
1744 * @param bs The current boot status.
1745 *
1746 * @return 0 on success; nonzero on failure.
1747 */
1748#if !defined(MCUBOOT_OVERWRITE_ONLY)
1749static int
1750boot_complete_partial_swap(struct boot_status *bs)
1751{
1752 int rc;
1753
1754 /* Determine the type of swap operation being resumed from the
1755 * `swap-type` trailer field.
1756 */
1757 rc = boot_swap_image(bs);
1758 assert(rc == 0);
1759
1760 BOOT_SWAP_TYPE(&boot_data) = bs->swap_type;
1761
1762 /* The following states need image_ok be explicitly set after the
1763 * swap was finished to avoid a new revert.
1764 */
1765 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1766 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
1767 rc = boot_set_image_ok();
1768 if (rc != 0) {
1769 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
1770 }
1771 }
1772
1773 if (bs->swap_type == BOOT_SWAP_TYPE_TEST ||
1774 bs->swap_type == BOOT_SWAP_TYPE_PERM ||
1775 bs->swap_type == BOOT_SWAP_TYPE_REVERT) {
1776 rc = boot_set_copy_done();
1777 if (rc != 0) {
1778 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
1779 }
1780 }
1781
1782 if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_PANIC) {
1783 BOOT_LOG_ERR("panic!");
1784 assert(0);
1785
1786 /* Loop forever... */
1787 while (1) {}
1788 }
1789
1790 return rc;
1791}
1792#endif /* !MCUBOOT_OVERWRITE_ONLY */
1793
1794#if (BOOT_IMAGE_NUMBER > 1)
1795/**
1796 * Review the validity of previously determined swap types of other images.
1797 *
1798 * @param aborted_swap The current image upgrade is a
1799 * partial/aborted swap.
1800 */
1801static void
1802boot_review_image_swap_types(bool aborted_swap)
1803{
1804 /* In that case if we rebooted in the middle of an image upgrade process, we
1805 * must review the validity of swap types, that were previously determined
1806 * for other images. The image_ok flag had not been set before the reboot
1807 * for any of the updated images (only the copy_done flag) and thus falsely
1808 * the REVERT swap type has been determined for the previous images that had
1809 * been updated before the reboot.
1810 *
1811 * There are two separate scenarios that we have to deal with:
1812 *
1813 * 1. The reboot has happened during swapping an image:
1814 * The current image upgrade has been determined as a
1815 * partial/aborted swap.
1816 * 2. The reboot has happened between two separate image upgrades:
1817 * In this scenario we must check the swap type of the current image.
1818 * In those cases if it is NONE or REVERT we cannot certainly determine
1819 * the fact of a reboot. In a consistent state images must move in the
1820 * same direction or stay in place, e.g. in practice REVERT and TEST
1821 * swap types cannot be present at the same time. If the swap type of
1822 * the current image is either TEST, PERM or FAIL we must review the
1823 * already determined swap types of other images and set each false
1824 * REVERT swap types to NONE (these images had been successfully
1825 * updated before the system rebooted between two separate image
1826 * upgrades).
1827 */
1828
1829 if (current_image == 0) {
1830 /* Nothing to do */
1831 return;
1832 }
1833
1834 if (!aborted_swap) {
1835 if ((BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_NONE) ||
1836 (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_REVERT)) {
1837 /* Nothing to do */
1838 return;
1839 }
1840 }
1841
1842 for (uint8_t i = 0; i < current_image; i++) {
1843 if (boot_data.swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
1844 boot_data.swap_type[i] = BOOT_SWAP_TYPE_NONE;
1845 }
1846 }
1847}
1848#endif
1849
1850/**
1851 * Prepare image to be updated if required.
1852 *
1853 * Prepare image to be updated if required with completing an image swap
1854 * operation if one was aborted and/or determining the type of the
1855 * swap operation. In case of any error set the swap type to NONE.
1856 *
1857 * @param bs Pointer where the read and possibly updated
1858 * boot status can be written to.
1859 */
1860static void
1861boot_prepare_image_for_update(struct boot_status *bs)
1862{
1863 int rc;
1864
1865 /* Determine the sector layout of the image slots and scratch area. */
1866 rc = boot_read_sectors();
1867 if (rc != 0) {
1868 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
1869 " - too small?", BOOT_MAX_IMG_SECTORS);
1870 /* Unable to determine sector layout, continue with next image
1871 * if there is one.
1872 */
1873 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE;
1874 return;
1875 }
1876
1877 /* Attempt to read an image header from each slot. */
1878 rc = boot_read_image_headers(false);
1879 if (rc != 0) {
1880 /* Continue with next image if there is one. */
1881 BOOT_LOG_WRN("Failed reading image headers; Image=%u", current_image);
1882 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE;
1883 return;
1884 }
1885
1886 /* If the current image's slots aren't compatible, no swap is possible.
1887 * Just boot into primary slot.
1888 */
1889 if (boot_slots_compatible()) {
1890
1891 rc = boot_read_status(bs);
1892 if (rc != 0) {
1893 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
1894 current_image);
1895 /* Continue with next image if there is one. */
1896 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE;
1897 return;
1898 }
1899
1900 /* Determine if we rebooted in the middle of an image swap
1901 * operation. If a partial swap was detected, complete it.
1902 */
1903 if (bs->idx != BOOT_STATUS_IDX_0 || bs->state != BOOT_STATUS_STATE_0) {
1904
1905#if (BOOT_IMAGE_NUMBER > 1)
1906 boot_review_image_swap_types(true);
1907#endif
1908
1909#ifdef MCUBOOT_OVERWRITE_ONLY
1910 /* Should never arrive here, overwrite-only mode has
1911 * no swap state.
1912 */
1913 assert(0);
1914#else
1915 /* Determine the type of swap operation being resumed from the
1916 * `swap-type` trailer field.
1917 */
1918 rc = boot_complete_partial_swap(bs);
1919 assert(rc == 0);
1920#endif
1921 /* Attempt to read an image header from each slot. Ensure that
1922 * image headers in slots are aligned with headers in boot_data.
1923 */
1924 rc = boot_read_image_headers(false);
1925 assert(rc == 0);
1926
1927 /* Swap has finished set to NONE */
1928 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE;
1929 } else {
1930 /* There was no partial swap, determine swap type. */
1931 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
1932 BOOT_SWAP_TYPE(&boot_data) = boot_validated_swap_type(bs);
1933 } else if (boot_validate_slot(BOOT_SECONDARY_SLOT, bs) != 0) {
1934 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_FAIL;
1935 } else {
1936 BOOT_SWAP_TYPE(&boot_data) = bs->swap_type;
1937 }
1938
1939#if (BOOT_IMAGE_NUMBER > 1)
1940 boot_review_image_swap_types(false);
1941#endif
1942 }
1943 } else {
1944 /* In that case if slots are not compatible. */
1945 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE;
1946 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001947}
1948
1949/**
1950 * Prepares the booting process. This function moves images around in flash as
1951 * appropriate, and tells you what address to boot from.
1952 *
1953 * @param rsp On success, indicates how booting should occur.
1954 *
1955 * @return 0 on success; nonzero on failure.
1956 */
1957int
1958boot_go(struct boot_rsp *rsp)
1959{
Tamas Banf70ef8c2017-12-19 15:35:09 +00001960 size_t slot;
David Vincze7384ee72019-07-23 17:00:42 +02001961 struct boot_status bs;
Tamas Ban4e5ed8d2019-09-17 09:31:11 +01001962 int rc = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001963 int fa_id;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001964
1965 /* The array of slot sectors are defined here (as opposed to file scope) so
1966 * that they don't get allocated for non-boot-loader apps. This is
1967 * necessary because the gcc option "-fdata-sections" doesn't seem to have
1968 * any effect in older gcc versions (e.g., 4.8.4).
1969 */
David Vincze7384ee72019-07-23 17:00:42 +02001970 static boot_sector_t
1971 primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
1972 static boot_sector_t
1973 secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
David Vincze401c7422019-06-21 20:44:05 +02001974 static boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Tamas Banf70ef8c2017-12-19 15:35:09 +00001975
David Vincze7384ee72019-07-23 17:00:42 +02001976 /* Iterate over all the images. By the end of the loop the swap type has
1977 * to be determined for each image and all aborted swaps have to be
1978 * completed.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001979 */
David Vincze7384ee72019-07-23 17:00:42 +02001980 for (current_image = 0; current_image < BOOT_IMAGE_NUMBER; ++current_image)
1981 {
1982 BOOT_IMG(&boot_data, BOOT_PRIMARY_SLOT).sectors =
1983 primary_slot_sectors[current_image];
1984 BOOT_IMG(&boot_data, BOOT_SECONDARY_SLOT).sectors =
1985 secondary_slot_sectors[current_image];
1986 boot_data.scratch.sectors = scratch_sectors;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001987
David Vincze7384ee72019-07-23 17:00:42 +02001988 /* Open primary and secondary image areas for the duration
1989 * of this call.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001990 */
David Vincze7384ee72019-07-23 17:00:42 +02001991 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1992 fa_id = flash_area_id_from_image_slot(slot);
1993 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot));
1994 assert(rc == 0);
1995 }
1996 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
1997 &BOOT_SCRATCH_AREA(&boot_data));
1998 assert(rc == 0);
1999
2000 /* Determine swap type and complete swap if it has been aborted. */
2001 boot_prepare_image_for_update(&bs);
2002 }
2003
David Vinczebb6e3b62019-07-31 14:24:05 +02002004#if (BOOT_IMAGE_NUMBER > 1)
2005 /* Iterate over all the images and verify whether the image dependencies
2006 * are all satisfied and update swap type if necessary.
2007 */
2008 boot_verify_all_image_dependency();
2009#endif
2010
David Vincze7384ee72019-07-23 17:00:42 +02002011 /* Iterate over all the images. At this point there are no aborted swaps
2012 * and the swap types are determined for each image. By the end of the loop
2013 * all required update operations will have been finished.
2014 */
2015 for (current_image = 0; current_image < BOOT_IMAGE_NUMBER; ++current_image)
2016 {
2017
2018#if (BOOT_IMAGE_NUMBER > 1)
2019 /* Indicate that swap is not aborted */
2020 memset(&bs, 0, sizeof bs);
2021 bs.idx = BOOT_STATUS_IDX_0;
2022 bs.state = BOOT_STATUS_STATE_0;
2023#endif /* (BOOT_IMAGE_NUMBER > 1) */
2024
2025 /* Set the previously determined swap type */
2026 bs.swap_type = BOOT_SWAP_TYPE(&boot_data);
2027
2028 switch (BOOT_SWAP_TYPE(&boot_data)) {
2029 case BOOT_SWAP_TYPE_NONE:
2030 break;
2031
2032 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
2033 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
2034 case BOOT_SWAP_TYPE_REVERT:
2035 rc = boot_perform_update(&bs);
2036 assert(rc == 0);
2037 break;
2038
2039 case BOOT_SWAP_TYPE_FAIL:
2040 /* The image in secondary slot was invalid and is now erased. Ensure
2041 * we don't try to boot into it again on the next reboot. Do this by
2042 * pretending we just reverted back to primary slot.
2043 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00002044#ifndef MCUBOOT_OVERWRITE_ONLY
David Vincze7384ee72019-07-23 17:00:42 +02002045 /* image_ok needs to be explicitly set to avoid a new revert. */
Tamas Banf70ef8c2017-12-19 15:35:09 +00002046 rc = boot_set_image_ok();
2047 if (rc != 0) {
David Vincze7384ee72019-07-23 17:00:42 +02002048 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002049 }
2050#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Vincze7384ee72019-07-23 17:00:42 +02002051 break;
2052
2053 default:
2054 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002055 }
David Vincze7384ee72019-07-23 17:00:42 +02002056
2057 if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_PANIC) {
2058 BOOT_LOG_ERR("panic!");
2059 assert(0);
2060
2061 /* Loop forever... */
2062 while (1) {}
2063 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00002064 }
2065
David Vincze7384ee72019-07-23 17:00:42 +02002066 /* Iterate over all the images. At this point all required update operations
2067 * have finished. By the end of the loop each image in the primary slot will
2068 * have been re-validated.
2069 */
2070 for (current_image = 0; current_image < BOOT_IMAGE_NUMBER; ++current_image)
2071 {
2072 if (BOOT_SWAP_TYPE(&boot_data) != BOOT_SWAP_TYPE_NONE) {
2073 /* Attempt to read an image header from each slot. Ensure that image
2074 * headers in slots are aligned with headers in boot_data.
David Vincze060968d2019-05-23 01:13:14 +02002075 */
David Vincze7384ee72019-07-23 17:00:42 +02002076 rc = boot_read_image_headers(false);
David Vincze060968d2019-05-23 01:13:14 +02002077 if (rc != 0) {
David Vincze7384ee72019-07-23 17:00:42 +02002078 goto out;
2079 }
2080 /* Since headers were reloaded, it can be assumed we just performed
2081 * a swap or overwrite. Now the header info that should be used to
2082 * provide the data for the bootstrap, which previously was at
2083 * secondary slot, was updated to primary slot.
2084 */
2085 }
2086
2087#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
2088 rc = boot_validate_slot(BOOT_PRIMARY_SLOT, NULL);
2089 if (rc != 0) {
2090 rc = BOOT_EBADIMAGE;
2091 goto out;
2092 }
2093#else
2094 /* Even if we're not re-validating the primary slot, we could be booting
2095 * onto an empty flash chip. At least do a basic sanity check that
2096 * the magic number on the image is OK.
2097 */
2098 if (BOOT_IMG(&boot_data, BOOT_PRIMARY_SLOT).hdr.ih_magic !=
2099 IMAGE_MAGIC) {
2100 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
2101 &boot_img_hdr(&boot_data,BOOT_PRIMARY_SLOT)->ih_magic,
2102 current_image);
2103 rc = BOOT_EBADIMAGE;
2104 goto out;
2105 }
2106#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
2107
2108 /* Update the stored security counter with the active image's security
2109 * counter value. It will be updated only if the new security counter is
2110 * greater than the stored value.
2111 *
2112 * In case of a successful image swapping when the swap type is TEST the
2113 * security counter can be increased only after a reset, when the swap
2114 * type is NONE and the image has marked itself "OK" (the image_ok flag
2115 * has been set). This way a "revert" swap can be performed if it's
2116 * necessary.
2117 */
2118 if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_NONE) {
2119 rc = boot_update_security_counter(BOOT_PRIMARY_SLOT,
2120 boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT));
2121 if (rc != 0) {
2122 BOOT_LOG_ERR("Security counter update failed after image "
2123 "validation.");
David Vincze060968d2019-05-23 01:13:14 +02002124 goto out;
2125 }
2126 }
2127
David Vincze7384ee72019-07-23 17:00:42 +02002128 /* Save boot status to shared memory area */
2129#if (BOOT_IMAGE_NUMBER > 1)
2130 rc = boot_save_boot_status((current_image == 0) ? SW_SPE : SW_NSPE,
2131 boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT),
2132 BOOT_IMG_AREA(&boot_data, BOOT_PRIMARY_SLOT)
2133 );
David Vincze8bdfc2d2019-03-18 15:49:23 +01002134#else
David Vincze7384ee72019-07-23 17:00:42 +02002135 rc = boot_save_boot_status(SW_S_NS,
2136 boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT),
2137 BOOT_IMG_AREA(&boot_data, BOOT_PRIMARY_SLOT)
2138 );
2139#endif
2140 if (rc) {
2141 BOOT_LOG_ERR("Failed to add Image %u data to shared area",
2142 current_image);
David Vincze060968d2019-05-23 01:13:14 +02002143 }
2144 }
2145
David Vincze7384ee72019-07-23 17:00:42 +02002146 /* Always boot from the primary slot of Image 0. */
2147 current_image = 0;
2148 rsp->br_flash_dev_id =
2149 BOOT_IMG_AREA(&boot_data, BOOT_PRIMARY_SLOT)->fa_device_id;
2150 rsp->br_image_off =
2151 boot_img_slot_off(&boot_data, BOOT_PRIMARY_SLOT);
2152 rsp->br_hdr =
2153 boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT);
Tamas Ban0e8ab302019-01-17 11:45:31 +00002154
Tamas Banf70ef8c2017-12-19 15:35:09 +00002155 out:
David Vincze7384ee72019-07-23 17:00:42 +02002156 for (current_image = 0; current_image < BOOT_IMAGE_NUMBER; ++current_image)
2157 {
2158 flash_area_close(BOOT_SCRATCH_AREA(&boot_data));
2159 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2160 flash_area_close(BOOT_IMG_AREA(&boot_data,
2161 BOOT_NUM_SLOTS - 1 - slot));
2162 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00002163 }
2164 return rc;
2165}
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002166
Oliver Swedef9982442018-08-24 18:37:44 +01002167#else /* MCUBOOT_NO_SWAP || MCUBOOT_RAM_LOADING */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002168
David Vinczedcba70b2019-05-28 12:02:52 +02002169#define BOOT_LOG_IMAGE_INFO(area, hdr, state) \
2170 BOOT_LOG_INF("Image %u: version=%u.%u.%u+%u, magic=%5s, image_ok=0x%x", \
2171 (area), \
2172 (hdr)->ih_ver.iv_major, \
2173 (hdr)->ih_ver.iv_minor, \
2174 (hdr)->ih_ver.iv_revision, \
2175 (hdr)->ih_ver.iv_build_num, \
2176 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
2177 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
2178 "bad"), \
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002179 (state)->image_ok)
2180
2181struct image_slot_version {
2182 uint64_t version;
2183 uint32_t slot_number;
2184};
2185
2186/**
2187 * Extract the version number from the image header. This function must be
2188 * ported if version number format has changed in the image header.
2189 *
2190 * @param hdr Pointer to an image header structure
2191 *
Oliver Swedef9982442018-08-24 18:37:44 +01002192 * @return Version number casted to uint64_t
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002193 */
2194static uint64_t
2195boot_get_version_number(struct image_header *hdr)
2196{
Oliver Swedef9982442018-08-24 18:37:44 +01002197 uint64_t version = 0;
2198 version |= (uint64_t)hdr->ih_ver.iv_major << (IMAGE_VER_MINOR_LENGTH
2199 + IMAGE_VER_REVISION_LENGTH
2200 + IMAGE_VER_BUILD_NUM_LENGTH);
2201 version |= (uint64_t)hdr->ih_ver.iv_minor << (IMAGE_VER_REVISION_LENGTH
2202 + IMAGE_VER_BUILD_NUM_LENGTH);
2203 version |= (uint64_t)hdr->ih_ver.iv_revision << IMAGE_VER_BUILD_NUM_LENGTH;
2204 version |= hdr->ih_ver.iv_build_num;
2205 return version;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002206}
2207
2208/**
2209 * Comparator function for `qsort` to compare version numbers. This function
2210 * must be ported if version number format has changed in the image header.
2211 *
2212 * @param ver1 Pointer to an array element which holds the version number
2213 * @param ver2 Pointer to another array element which holds the version
2214 * number
2215 *
2216 * @return if version1 > version2 -1
2217 * if version1 == version2 0
2218 * if version1 < version2 1
2219 */
2220static int
2221boot_compare_version_numbers(const void *ver1, const void *ver2)
2222{
2223 if (((struct image_slot_version *)ver1)->version <
2224 ((struct image_slot_version *)ver2)->version) {
2225 return 1;
2226 }
2227
2228 if (((struct image_slot_version *)ver1)->version ==
2229 ((struct image_slot_version *)ver2)->version) {
2230 return 0;
2231 }
2232
2233 return -1;
2234}
2235
2236/**
2237 * Sort the available images based on the version number and puts them in
2238 * a list.
2239 *
2240 * @param boot_sequence A pointer to an array, whose aim is to carry
2241 * the boot order of candidate images.
2242 * @param slot_cnt The number of flash areas, which can contains firmware
2243 * images.
2244 *
2245 * @return The number of valid images.
2246 */
2247uint32_t
2248boot_get_boot_sequence(uint32_t *boot_sequence, uint32_t slot_cnt)
2249{
2250 struct boot_swap_state slot_state;
2251 struct image_header *hdr;
2252 struct image_slot_version image_versions[BOOT_NUM_SLOTS] = {{0}};
2253 uint32_t image_cnt = 0;
2254 uint32_t slot;
2255 int32_t rc;
2256 int32_t fa_id;
2257
2258 for (slot = 0; slot < slot_cnt; slot++) {
2259 hdr = boot_img_hdr(&boot_data, slot);
2260 fa_id = flash_area_id_from_image_slot(slot);
2261 rc = boot_read_swap_state_by_id(fa_id, &slot_state);
2262 if (rc != 0) {
David Vinczedcba70b2019-05-28 12:02:52 +02002263 BOOT_LOG_ERR("Error during reading image trailer from slot: %u",
2264 slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002265 continue;
2266 }
2267
2268 if (hdr->ih_magic == IMAGE_MAGIC) {
2269 if (slot_state.magic == BOOT_MAGIC_GOOD ||
David Vincze39e78552018-10-10 17:10:01 +02002270 slot_state.image_ok == BOOT_FLAG_SET) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002271 /* Valid cases:
2272 * - Test mode: magic is OK in image trailer
2273 * - Permanent mode: image_ok flag has previously set
2274 */
2275 image_versions[slot].slot_number = slot;
2276 image_versions[slot].version = boot_get_version_number(hdr);
2277 image_cnt++;
2278 }
2279
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002280 BOOT_LOG_IMAGE_INFO(slot, hdr, &slot_state);
2281 } else {
David Vinczedcba70b2019-05-28 12:02:52 +02002282 BOOT_LOG_INF("Image %u: No valid image", slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002283 }
2284 }
2285
2286 /* Sort the images based on version number */
2287 qsort(&image_versions[0],
2288 slot_cnt,
2289 sizeof(struct image_slot_version),
2290 boot_compare_version_numbers);
2291
2292 /* Copy the calculated boot sequence to boot_sequence array */
2293 for (slot = 0; slot < slot_cnt; slot++) {
2294 boot_sequence[slot] = image_versions[slot].slot_number;
2295 }
2296
2297 return image_cnt;
2298}
2299
Oliver Swedef9982442018-08-24 18:37:44 +01002300#ifdef MCUBOOT_RAM_LOADING
2301/**
2302 * Copies an image from a slot in the flash to an SRAM address, where the load
2303 * address has already been inserted into the image header by this point and is
2304 * extracted from it within this method. The copying is done sector-by-sector.
2305 *
2306 * @param slot The flash slot of the image to be copied to SRAM.
2307 *
2308 * @param hdr Pointer to the image header structure of the image
2309 * that needs to be copid to SRAM
2310 *
2311 * @return 0 on success; nonzero on failure.
2312 */
2313static int
2314boot_copy_image_to_sram(int slot, struct image_header *hdr)
2315{
2316 int rc;
2317 uint32_t sect_sz;
2318 uint32_t sect = 0;
2319 uint32_t bytes_copied = 0;
2320 const struct flash_area *fap_src = NULL;
2321 uint32_t dst = (uint32_t) hdr->ih_load_addr;
2322 uint32_t img_sz;
2323
2324 if (dst % 4 != 0) {
Tamas Banc27b5c32019-05-28 16:30:19 +01002325 BOOT_LOG_INF("Cannot copy the image to the SRAM address 0x%x "
Oliver Swedef9982442018-08-24 18:37:44 +01002326 "- the load address must be aligned with 4 bytes due to SRAM "
2327 "restrictions", dst);
2328 return BOOT_EBADARGS;
2329 }
2330
2331 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap_src);
2332 if (rc != 0) {
2333 return BOOT_EFLASH;
2334 }
2335
2336 rc = boot_read_image_size(slot, hdr, &img_sz);
2337 if (rc != 0) {
2338 return BOOT_EFLASH;
2339 }
2340
2341 while (bytes_copied < img_sz) {
2342 sect_sz = boot_img_sector_size(&boot_data, slot, sect);
2343 /*
2344 * Direct copy from where the image sector resides in flash to its new
2345 * location in SRAM
2346 */
2347 rc = flash_area_read(fap_src,
2348 bytes_copied,
2349 (void *)(dst + bytes_copied),
2350 sect_sz);
2351 if (rc != 0) {
2352 BOOT_LOG_INF("Error whilst copying image from Flash to SRAM");
2353 break;
2354 } else {
2355 bytes_copied += sect_sz;
2356 }
2357 sect++;
2358 }
2359
2360 if (fap_src) {
2361 flash_area_close(fap_src);
2362 }
2363 return rc;
2364}
2365#endif /* MCUBOOT_RAM_LOADING */
2366
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002367/**
2368 * Prepares the booting process. This function choose the newer image in flash
2369 * as appropriate, and returns the address to boot from.
2370 *
2371 * @param rsp On success, indicates how booting should occur.
2372 *
2373 * @return 0 on success; nonzero on failure.
2374 */
2375int
2376boot_go(struct boot_rsp *rsp)
2377{
2378 size_t slot = 0;
2379 int32_t i;
2380 int rc;
2381 int fa_id;
2382 uint32_t boot_sequence[BOOT_NUM_SLOTS];
2383 uint32_t img_cnt;
Oliver Swedef9982442018-08-24 18:37:44 +01002384 struct image_header *newest_image_header;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002385
David Vincze8bdfc2d2019-03-18 15:49:23 +01002386 static boot_sector_t primary_slot_sectors[BOOT_MAX_IMG_SECTORS];
2387 static boot_sector_t secondary_slot_sectors[BOOT_MAX_IMG_SECTORS];
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002388
David Vincze7384ee72019-07-23 17:00:42 +02002389 BOOT_IMG(&boot_data, BOOT_PRIMARY_SLOT).sectors =
2390 &primary_slot_sectors[0];
2391 BOOT_IMG(&boot_data, BOOT_SECONDARY_SLOT).sectors =
2392 &secondary_slot_sectors[0];
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002393
2394 /* Open boot_data image areas for the duration of this call. */
2395 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
2396 fa_id = flash_area_id_from_image_slot(i);
2397 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, i));
2398 assert(rc == 0);
2399 }
2400
2401 /* Determine the sector layout of the image slots. */
2402 rc = boot_read_sectors();
2403 if (rc != 0) {
David Vincze39e78552018-10-10 17:10:01 +02002404 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - too small?",
2405 BOOT_MAX_IMG_SECTORS);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002406 goto out;
2407 }
2408
2409 /* Attempt to read an image header from each slot. */
David Vincze39e78552018-10-10 17:10:01 +02002410 rc = boot_read_image_headers(false);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002411 if (rc != 0) {
2412 goto out;
2413 }
2414
2415 img_cnt = boot_get_boot_sequence(boot_sequence, BOOT_NUM_SLOTS);
2416 if (img_cnt) {
2417 /* Authenticate images */
2418 for (i = 0; i < img_cnt; i++) {
David Vincze401c7422019-06-21 20:44:05 +02002419 rc = boot_validate_slot(boot_sequence[i], NULL);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002420 if (rc == 0) {
2421 slot = boot_sequence[i];
2422 break;
2423 }
2424 }
2425 if (rc) {
2426 /* If there was no valid image at all */
2427 rc = BOOT_EBADIMAGE;
2428 goto out;
2429 }
2430
Oliver Swedef9982442018-08-24 18:37:44 +01002431 /* The slot variable now refers to the newest image's slot in flash */
2432 newest_image_header = boot_img_hdr(&boot_data, slot);
2433
David Vincze060968d2019-05-23 01:13:14 +02002434 /* Update the security counter with the newest image's security
2435 * counter value.
2436 */
2437 rc = boot_update_security_counter(slot, newest_image_header);
2438 if (rc != 0) {
2439 BOOT_LOG_ERR("Security counter update failed after image "
2440 "validation.");
2441 goto out;
2442 }
2443
Oliver Swedef9982442018-08-24 18:37:44 +01002444 #ifdef MCUBOOT_RAM_LOADING
2445 if (newest_image_header->ih_flags & IMAGE_F_RAM_LOAD) {
2446 /* Copy image to the load address from where it
2447 * currently resides in flash */
2448 rc = boot_copy_image_to_sram(slot, newest_image_header);
2449 if (rc != 0) {
2450 rc = BOOT_EBADIMAGE;
David Vincze8bdfc2d2019-03-18 15:49:23 +01002451 BOOT_LOG_INF("Could not copy image from the %s slot in "
Tamas Banc27b5c32019-05-28 16:30:19 +01002452 "the Flash to load address 0x%x in SRAM, "
David Vincze8bdfc2d2019-03-18 15:49:23 +01002453 "aborting..", (slot == BOOT_PRIMARY_SLOT) ?
2454 "primary" : "secondary",
Oliver Swedef9982442018-08-24 18:37:44 +01002455 newest_image_header->ih_load_addr);
2456 goto out;
2457 } else {
David Vincze8bdfc2d2019-03-18 15:49:23 +01002458 BOOT_LOG_INF("Image has been copied from the %s slot in "
2459 "the flash to SRAM address 0x%x",
2460 (slot == BOOT_PRIMARY_SLOT) ?
2461 "primary" : "secondary",
Oliver Swedef9982442018-08-24 18:37:44 +01002462 newest_image_header->ih_load_addr);
2463 }
2464
2465 /* Validate the image hash in SRAM after the copy was successful */
2466 rc = bootutil_check_hash_after_loading(newest_image_header);
2467 if (rc != 0) {
2468 rc = BOOT_EBADIMAGE;
2469 BOOT_LOG_INF("Cannot validate the hash of the image that was "
2470 "copied to SRAM, aborting..");
2471 goto out;
2472 }
2473
Tamas Banc27b5c32019-05-28 16:30:19 +01002474 BOOT_LOG_INF("Booting image from SRAM at address 0x%x",
Oliver Swedef9982442018-08-24 18:37:44 +01002475 newest_image_header->ih_load_addr);
2476 } else {
David Vincze8a2a4e22019-05-24 10:14:23 +02002477#endif /* MCUBOOT_RAM_LOADING */
David Vincze8bdfc2d2019-03-18 15:49:23 +01002478 BOOT_LOG_INF("Booting image from the %s slot",
2479 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Vincze8a2a4e22019-05-24 10:14:23 +02002480#ifdef MCUBOOT_RAM_LOADING
Oliver Swedef9982442018-08-24 18:37:44 +01002481 }
David Vincze8a2a4e22019-05-24 10:14:23 +02002482#endif
Oliver Swedef9982442018-08-24 18:37:44 +01002483
2484 rsp->br_hdr = newest_image_header;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002485 rsp->br_image_off = boot_img_slot_off(&boot_data, slot);
David Vincze7384ee72019-07-23 17:00:42 +02002486 rsp->br_flash_dev_id = BOOT_IMG_AREA(&boot_data, slot)->fa_device_id;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002487 } else {
2488 /* No candidate image available */
2489 rc = BOOT_EBADIMAGE;
David Vincze060968d2019-05-23 01:13:14 +02002490 goto out;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002491 }
2492
Tamas Ban0e8ab302019-01-17 11:45:31 +00002493 /* Save boot status to shared memory area */
2494 rc = boot_save_boot_status(SW_S_NS,
2495 rsp->br_hdr,
2496 BOOT_IMG_AREA(&boot_data, slot));
2497 if (rc) {
2498 BOOT_LOG_ERR("Failed to add data to shared area");
2499 }
2500
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002501out:
2502 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2503 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
2504 }
2505 return rc;
2506}
Oliver Swedef9982442018-08-24 18:37:44 +01002507#endif /* MCUBOOT_NO_SWAP || MCUBOOT_RAM_LOADING */