blob: c86f2910da54b49afdf233fd928d09c49fcd9878 [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
212static uint8_t
213boot_write_sz(void)
214{
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000215 uint8_t elem_sz;
216 uint8_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];
766 uint8_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
1484/**
David Vincze7384ee72019-07-23 17:00:42 +02001485 * Performs a clean (not aborted) image update.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001486 *
David Vincze7384ee72019-07-23 17:00:42 +02001487 * @param bs The current boot status.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001488 *
1489 * @return 0 on success; nonzero on failure.
1490 */
1491static int
David Vincze7384ee72019-07-23 17:00:42 +02001492boot_perform_update(struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001493{
Tamas Banf70ef8c2017-12-19 15:35:09 +00001494 int rc;
1495
David Vincze7384ee72019-07-23 17:00:42 +02001496 /* At this point there are no aborted swaps. */
1497#if defined(MCUBOOT_OVERWRITE_ONLY)
1498 rc = boot_copy_image(bs);
1499#else
1500 rc = boot_swap_image(bs);
1501#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +00001502 assert(rc == 0);
David Vincze7384ee72019-07-23 17:00:42 +02001503
1504#ifndef MCUBOOT_OVERWRITE_ONLY
1505 /* The following state needs image_ok be explicitly set after the
1506 * swap was finished to avoid a new revert.
1507 */
1508 if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_REVERT ||
1509 BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_PERM) {
1510 rc = boot_set_image_ok();
1511 if (rc != 0) {
1512 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
1513 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001514 }
1515
David Vincze7384ee72019-07-23 17:00:42 +02001516 if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_PERM) {
1517 /* Update the stored security counter with the new image's security
1518 * counter value. The primary slot holds the new image at this
1519 * point, but the secondary slot's image header must be passed
1520 * because the read image headers in the boot_data structure have
1521 * not been updated yet.
1522 *
1523 * In case of a permanent image swap mcuboot will never attempt to
1524 * revert the images on the next reboot. Therefore, the security
1525 * counter must be increased right after the image upgrade.
David Vincze401c7422019-06-21 20:44:05 +02001526 */
David Vincze7384ee72019-07-23 17:00:42 +02001527 rc = boot_update_security_counter(BOOT_PRIMARY_SLOT,
1528 boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT));
1529 if (rc != 0) {
1530 BOOT_LOG_ERR("Security counter update failed after "
1531 "image upgrade.");
1532 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001533 }
1534 }
1535
David Vincze7384ee72019-07-23 17:00:42 +02001536 if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_TEST ||
1537 BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_PERM ||
1538 BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_REVERT) {
1539 rc = boot_set_copy_done();
1540 if (rc != 0) {
1541 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
1542 }
1543 }
1544#endif /* !MCUBOOT_OVERWRITE_ONLY */
1545
1546 return rc;
1547}
1548
1549/**
1550 * Completes a previously aborted image swap.
1551 *
1552 * @param bs The current boot status.
1553 *
1554 * @return 0 on success; nonzero on failure.
1555 */
1556#if !defined(MCUBOOT_OVERWRITE_ONLY)
1557static int
1558boot_complete_partial_swap(struct boot_status *bs)
1559{
1560 int rc;
1561
1562 /* Determine the type of swap operation being resumed from the
1563 * `swap-type` trailer field.
1564 */
1565 rc = boot_swap_image(bs);
1566 assert(rc == 0);
1567
1568 BOOT_SWAP_TYPE(&boot_data) = bs->swap_type;
1569
1570 /* The following states need image_ok be explicitly set after the
1571 * swap was finished to avoid a new revert.
1572 */
1573 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1574 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
1575 rc = boot_set_image_ok();
1576 if (rc != 0) {
1577 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
1578 }
1579 }
1580
1581 if (bs->swap_type == BOOT_SWAP_TYPE_TEST ||
1582 bs->swap_type == BOOT_SWAP_TYPE_PERM ||
1583 bs->swap_type == BOOT_SWAP_TYPE_REVERT) {
1584 rc = boot_set_copy_done();
1585 if (rc != 0) {
1586 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
1587 }
1588 }
1589
1590 if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_PANIC) {
1591 BOOT_LOG_ERR("panic!");
1592 assert(0);
1593
1594 /* Loop forever... */
1595 while (1) {}
1596 }
1597
1598 return rc;
1599}
1600#endif /* !MCUBOOT_OVERWRITE_ONLY */
1601
1602#if (BOOT_IMAGE_NUMBER > 1)
1603/**
1604 * Review the validity of previously determined swap types of other images.
1605 *
1606 * @param aborted_swap The current image upgrade is a
1607 * partial/aborted swap.
1608 */
1609static void
1610boot_review_image_swap_types(bool aborted_swap)
1611{
1612 /* In that case if we rebooted in the middle of an image upgrade process, we
1613 * must review the validity of swap types, that were previously determined
1614 * for other images. The image_ok flag had not been set before the reboot
1615 * for any of the updated images (only the copy_done flag) and thus falsely
1616 * the REVERT swap type has been determined for the previous images that had
1617 * been updated before the reboot.
1618 *
1619 * There are two separate scenarios that we have to deal with:
1620 *
1621 * 1. The reboot has happened during swapping an image:
1622 * The current image upgrade has been determined as a
1623 * partial/aborted swap.
1624 * 2. The reboot has happened between two separate image upgrades:
1625 * In this scenario we must check the swap type of the current image.
1626 * In those cases if it is NONE or REVERT we cannot certainly determine
1627 * the fact of a reboot. In a consistent state images must move in the
1628 * same direction or stay in place, e.g. in practice REVERT and TEST
1629 * swap types cannot be present at the same time. If the swap type of
1630 * the current image is either TEST, PERM or FAIL we must review the
1631 * already determined swap types of other images and set each false
1632 * REVERT swap types to NONE (these images had been successfully
1633 * updated before the system rebooted between two separate image
1634 * upgrades).
1635 */
1636
1637 if (current_image == 0) {
1638 /* Nothing to do */
1639 return;
1640 }
1641
1642 if (!aborted_swap) {
1643 if ((BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_NONE) ||
1644 (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_REVERT)) {
1645 /* Nothing to do */
1646 return;
1647 }
1648 }
1649
1650 for (uint8_t i = 0; i < current_image; i++) {
1651 if (boot_data.swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
1652 boot_data.swap_type[i] = BOOT_SWAP_TYPE_NONE;
1653 }
1654 }
1655}
1656#endif
1657
1658/**
1659 * Prepare image to be updated if required.
1660 *
1661 * Prepare image to be updated if required with completing an image swap
1662 * operation if one was aborted and/or determining the type of the
1663 * swap operation. In case of any error set the swap type to NONE.
1664 *
1665 * @param bs Pointer where the read and possibly updated
1666 * boot status can be written to.
1667 */
1668static void
1669boot_prepare_image_for_update(struct boot_status *bs)
1670{
1671 int rc;
1672
1673 /* Determine the sector layout of the image slots and scratch area. */
1674 rc = boot_read_sectors();
1675 if (rc != 0) {
1676 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
1677 " - too small?", BOOT_MAX_IMG_SECTORS);
1678 /* Unable to determine sector layout, continue with next image
1679 * if there is one.
1680 */
1681 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE;
1682 return;
1683 }
1684
1685 /* Attempt to read an image header from each slot. */
1686 rc = boot_read_image_headers(false);
1687 if (rc != 0) {
1688 /* Continue with next image if there is one. */
1689 BOOT_LOG_WRN("Failed reading image headers; Image=%u", current_image);
1690 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE;
1691 return;
1692 }
1693
1694 /* If the current image's slots aren't compatible, no swap is possible.
1695 * Just boot into primary slot.
1696 */
1697 if (boot_slots_compatible()) {
1698
1699 rc = boot_read_status(bs);
1700 if (rc != 0) {
1701 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
1702 current_image);
1703 /* Continue with next image if there is one. */
1704 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE;
1705 return;
1706 }
1707
1708 /* Determine if we rebooted in the middle of an image swap
1709 * operation. If a partial swap was detected, complete it.
1710 */
1711 if (bs->idx != BOOT_STATUS_IDX_0 || bs->state != BOOT_STATUS_STATE_0) {
1712
1713#if (BOOT_IMAGE_NUMBER > 1)
1714 boot_review_image_swap_types(true);
1715#endif
1716
1717#ifdef MCUBOOT_OVERWRITE_ONLY
1718 /* Should never arrive here, overwrite-only mode has
1719 * no swap state.
1720 */
1721 assert(0);
1722#else
1723 /* Determine the type of swap operation being resumed from the
1724 * `swap-type` trailer field.
1725 */
1726 rc = boot_complete_partial_swap(bs);
1727 assert(rc == 0);
1728#endif
1729 /* Attempt to read an image header from each slot. Ensure that
1730 * image headers in slots are aligned with headers in boot_data.
1731 */
1732 rc = boot_read_image_headers(false);
1733 assert(rc == 0);
1734
1735 /* Swap has finished set to NONE */
1736 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE;
1737 } else {
1738 /* There was no partial swap, determine swap type. */
1739 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
1740 BOOT_SWAP_TYPE(&boot_data) = boot_validated_swap_type(bs);
1741 } else if (boot_validate_slot(BOOT_SECONDARY_SLOT, bs) != 0) {
1742 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_FAIL;
1743 } else {
1744 BOOT_SWAP_TYPE(&boot_data) = bs->swap_type;
1745 }
1746
1747#if (BOOT_IMAGE_NUMBER > 1)
1748 boot_review_image_swap_types(false);
1749#endif
1750 }
1751 } else {
1752 /* In that case if slots are not compatible. */
1753 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE;
1754 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001755}
1756
1757/**
1758 * Prepares the booting process. This function moves images around in flash as
1759 * appropriate, and tells you what address to boot from.
1760 *
1761 * @param rsp On success, indicates how booting should occur.
1762 *
1763 * @return 0 on success; nonzero on failure.
1764 */
1765int
1766boot_go(struct boot_rsp *rsp)
1767{
Tamas Banf70ef8c2017-12-19 15:35:09 +00001768 size_t slot;
David Vincze7384ee72019-07-23 17:00:42 +02001769 struct boot_status bs;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001770 int rc;
1771 int fa_id;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001772
1773 /* The array of slot sectors are defined here (as opposed to file scope) so
1774 * that they don't get allocated for non-boot-loader apps. This is
1775 * necessary because the gcc option "-fdata-sections" doesn't seem to have
1776 * any effect in older gcc versions (e.g., 4.8.4).
1777 */
David Vincze7384ee72019-07-23 17:00:42 +02001778 static boot_sector_t
1779 primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
1780 static boot_sector_t
1781 secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
David Vincze401c7422019-06-21 20:44:05 +02001782 static boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Tamas Banf70ef8c2017-12-19 15:35:09 +00001783
David Vincze7384ee72019-07-23 17:00:42 +02001784 /* Iterate over all the images. By the end of the loop the swap type has
1785 * to be determined for each image and all aborted swaps have to be
1786 * completed.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001787 */
David Vincze7384ee72019-07-23 17:00:42 +02001788 for (current_image = 0; current_image < BOOT_IMAGE_NUMBER; ++current_image)
1789 {
1790 BOOT_IMG(&boot_data, BOOT_PRIMARY_SLOT).sectors =
1791 primary_slot_sectors[current_image];
1792 BOOT_IMG(&boot_data, BOOT_SECONDARY_SLOT).sectors =
1793 secondary_slot_sectors[current_image];
1794 boot_data.scratch.sectors = scratch_sectors;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001795
David Vincze7384ee72019-07-23 17:00:42 +02001796 /* Open primary and secondary image areas for the duration
1797 * of this call.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001798 */
David Vincze7384ee72019-07-23 17:00:42 +02001799 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1800 fa_id = flash_area_id_from_image_slot(slot);
1801 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot));
1802 assert(rc == 0);
1803 }
1804 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
1805 &BOOT_SCRATCH_AREA(&boot_data));
1806 assert(rc == 0);
1807
1808 /* Determine swap type and complete swap if it has been aborted. */
1809 boot_prepare_image_for_update(&bs);
1810 }
1811
1812 /* Iterate over all the images. At this point there are no aborted swaps
1813 * and the swap types are determined for each image. By the end of the loop
1814 * all required update operations will have been finished.
1815 */
1816 for (current_image = 0; current_image < BOOT_IMAGE_NUMBER; ++current_image)
1817 {
1818
1819#if (BOOT_IMAGE_NUMBER > 1)
1820 /* Indicate that swap is not aborted */
1821 memset(&bs, 0, sizeof bs);
1822 bs.idx = BOOT_STATUS_IDX_0;
1823 bs.state = BOOT_STATUS_STATE_0;
1824#endif /* (BOOT_IMAGE_NUMBER > 1) */
1825
1826 /* Set the previously determined swap type */
1827 bs.swap_type = BOOT_SWAP_TYPE(&boot_data);
1828
1829 switch (BOOT_SWAP_TYPE(&boot_data)) {
1830 case BOOT_SWAP_TYPE_NONE:
1831 break;
1832
1833 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1834 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
1835 case BOOT_SWAP_TYPE_REVERT:
1836 rc = boot_perform_update(&bs);
1837 assert(rc == 0);
1838 break;
1839
1840 case BOOT_SWAP_TYPE_FAIL:
1841 /* The image in secondary slot was invalid and is now erased. Ensure
1842 * we don't try to boot into it again on the next reboot. Do this by
1843 * pretending we just reverted back to primary slot.
1844 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001845#ifndef MCUBOOT_OVERWRITE_ONLY
David Vincze7384ee72019-07-23 17:00:42 +02001846 /* image_ok needs to be explicitly set to avoid a new revert. */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001847 rc = boot_set_image_ok();
1848 if (rc != 0) {
David Vincze7384ee72019-07-23 17:00:42 +02001849 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001850 }
1851#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Vincze7384ee72019-07-23 17:00:42 +02001852 break;
1853
1854 default:
1855 BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001856 }
David Vincze7384ee72019-07-23 17:00:42 +02001857
1858 if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_PANIC) {
1859 BOOT_LOG_ERR("panic!");
1860 assert(0);
1861
1862 /* Loop forever... */
1863 while (1) {}
1864 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001865 }
1866
David Vincze7384ee72019-07-23 17:00:42 +02001867 /* Iterate over all the images. At this point all required update operations
1868 * have finished. By the end of the loop each image in the primary slot will
1869 * have been re-validated.
1870 */
1871 for (current_image = 0; current_image < BOOT_IMAGE_NUMBER; ++current_image)
1872 {
1873 if (BOOT_SWAP_TYPE(&boot_data) != BOOT_SWAP_TYPE_NONE) {
1874 /* Attempt to read an image header from each slot. Ensure that image
1875 * headers in slots are aligned with headers in boot_data.
David Vincze060968d2019-05-23 01:13:14 +02001876 */
David Vincze7384ee72019-07-23 17:00:42 +02001877 rc = boot_read_image_headers(false);
David Vincze060968d2019-05-23 01:13:14 +02001878 if (rc != 0) {
David Vincze7384ee72019-07-23 17:00:42 +02001879 goto out;
1880 }
1881 /* Since headers were reloaded, it can be assumed we just performed
1882 * a swap or overwrite. Now the header info that should be used to
1883 * provide the data for the bootstrap, which previously was at
1884 * secondary slot, was updated to primary slot.
1885 */
1886 }
1887
1888#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
1889 rc = boot_validate_slot(BOOT_PRIMARY_SLOT, NULL);
1890 if (rc != 0) {
1891 rc = BOOT_EBADIMAGE;
1892 goto out;
1893 }
1894#else
1895 /* Even if we're not re-validating the primary slot, we could be booting
1896 * onto an empty flash chip. At least do a basic sanity check that
1897 * the magic number on the image is OK.
1898 */
1899 if (BOOT_IMG(&boot_data, BOOT_PRIMARY_SLOT).hdr.ih_magic !=
1900 IMAGE_MAGIC) {
1901 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
1902 &boot_img_hdr(&boot_data,BOOT_PRIMARY_SLOT)->ih_magic,
1903 current_image);
1904 rc = BOOT_EBADIMAGE;
1905 goto out;
1906 }
1907#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
1908
1909 /* Update the stored security counter with the active image's security
1910 * counter value. It will be updated only if the new security counter is
1911 * greater than the stored value.
1912 *
1913 * In case of a successful image swapping when the swap type is TEST the
1914 * security counter can be increased only after a reset, when the swap
1915 * type is NONE and the image has marked itself "OK" (the image_ok flag
1916 * has been set). This way a "revert" swap can be performed if it's
1917 * necessary.
1918 */
1919 if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_NONE) {
1920 rc = boot_update_security_counter(BOOT_PRIMARY_SLOT,
1921 boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT));
1922 if (rc != 0) {
1923 BOOT_LOG_ERR("Security counter update failed after image "
1924 "validation.");
David Vincze060968d2019-05-23 01:13:14 +02001925 goto out;
1926 }
1927 }
1928
David Vincze7384ee72019-07-23 17:00:42 +02001929 /* Save boot status to shared memory area */
1930#if (BOOT_IMAGE_NUMBER > 1)
1931 rc = boot_save_boot_status((current_image == 0) ? SW_SPE : SW_NSPE,
1932 boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT),
1933 BOOT_IMG_AREA(&boot_data, BOOT_PRIMARY_SLOT)
1934 );
David Vincze8bdfc2d2019-03-18 15:49:23 +01001935#else
David Vincze7384ee72019-07-23 17:00:42 +02001936 rc = boot_save_boot_status(SW_S_NS,
1937 boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT),
1938 BOOT_IMG_AREA(&boot_data, BOOT_PRIMARY_SLOT)
1939 );
1940#endif
1941 if (rc) {
1942 BOOT_LOG_ERR("Failed to add Image %u data to shared area",
1943 current_image);
David Vincze060968d2019-05-23 01:13:14 +02001944 }
1945 }
1946
David Vincze7384ee72019-07-23 17:00:42 +02001947 /* Always boot from the primary slot of Image 0. */
1948 current_image = 0;
1949 rsp->br_flash_dev_id =
1950 BOOT_IMG_AREA(&boot_data, BOOT_PRIMARY_SLOT)->fa_device_id;
1951 rsp->br_image_off =
1952 boot_img_slot_off(&boot_data, BOOT_PRIMARY_SLOT);
1953 rsp->br_hdr =
1954 boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT);
Tamas Ban0e8ab302019-01-17 11:45:31 +00001955
Tamas Banf70ef8c2017-12-19 15:35:09 +00001956 out:
David Vincze7384ee72019-07-23 17:00:42 +02001957 for (current_image = 0; current_image < BOOT_IMAGE_NUMBER; ++current_image)
1958 {
1959 flash_area_close(BOOT_SCRATCH_AREA(&boot_data));
1960 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1961 flash_area_close(BOOT_IMG_AREA(&boot_data,
1962 BOOT_NUM_SLOTS - 1 - slot));
1963 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001964 }
1965 return rc;
1966}
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001967
Oliver Swedef9982442018-08-24 18:37:44 +01001968#else /* MCUBOOT_NO_SWAP || MCUBOOT_RAM_LOADING */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001969
David Vinczedcba70b2019-05-28 12:02:52 +02001970#define BOOT_LOG_IMAGE_INFO(area, hdr, state) \
1971 BOOT_LOG_INF("Image %u: version=%u.%u.%u+%u, magic=%5s, image_ok=0x%x", \
1972 (area), \
1973 (hdr)->ih_ver.iv_major, \
1974 (hdr)->ih_ver.iv_minor, \
1975 (hdr)->ih_ver.iv_revision, \
1976 (hdr)->ih_ver.iv_build_num, \
1977 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
1978 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
1979 "bad"), \
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001980 (state)->image_ok)
1981
1982struct image_slot_version {
1983 uint64_t version;
1984 uint32_t slot_number;
1985};
1986
1987/**
1988 * Extract the version number from the image header. This function must be
1989 * ported if version number format has changed in the image header.
1990 *
1991 * @param hdr Pointer to an image header structure
1992 *
Oliver Swedef9982442018-08-24 18:37:44 +01001993 * @return Version number casted to uint64_t
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001994 */
1995static uint64_t
1996boot_get_version_number(struct image_header *hdr)
1997{
Oliver Swedef9982442018-08-24 18:37:44 +01001998 uint64_t version = 0;
1999 version |= (uint64_t)hdr->ih_ver.iv_major << (IMAGE_VER_MINOR_LENGTH
2000 + IMAGE_VER_REVISION_LENGTH
2001 + IMAGE_VER_BUILD_NUM_LENGTH);
2002 version |= (uint64_t)hdr->ih_ver.iv_minor << (IMAGE_VER_REVISION_LENGTH
2003 + IMAGE_VER_BUILD_NUM_LENGTH);
2004 version |= (uint64_t)hdr->ih_ver.iv_revision << IMAGE_VER_BUILD_NUM_LENGTH;
2005 version |= hdr->ih_ver.iv_build_num;
2006 return version;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002007}
2008
2009/**
2010 * Comparator function for `qsort` to compare version numbers. This function
2011 * must be ported if version number format has changed in the image header.
2012 *
2013 * @param ver1 Pointer to an array element which holds the version number
2014 * @param ver2 Pointer to another array element which holds the version
2015 * number
2016 *
2017 * @return if version1 > version2 -1
2018 * if version1 == version2 0
2019 * if version1 < version2 1
2020 */
2021static int
2022boot_compare_version_numbers(const void *ver1, const void *ver2)
2023{
2024 if (((struct image_slot_version *)ver1)->version <
2025 ((struct image_slot_version *)ver2)->version) {
2026 return 1;
2027 }
2028
2029 if (((struct image_slot_version *)ver1)->version ==
2030 ((struct image_slot_version *)ver2)->version) {
2031 return 0;
2032 }
2033
2034 return -1;
2035}
2036
2037/**
2038 * Sort the available images based on the version number and puts them in
2039 * a list.
2040 *
2041 * @param boot_sequence A pointer to an array, whose aim is to carry
2042 * the boot order of candidate images.
2043 * @param slot_cnt The number of flash areas, which can contains firmware
2044 * images.
2045 *
2046 * @return The number of valid images.
2047 */
2048uint32_t
2049boot_get_boot_sequence(uint32_t *boot_sequence, uint32_t slot_cnt)
2050{
2051 struct boot_swap_state slot_state;
2052 struct image_header *hdr;
2053 struct image_slot_version image_versions[BOOT_NUM_SLOTS] = {{0}};
2054 uint32_t image_cnt = 0;
2055 uint32_t slot;
2056 int32_t rc;
2057 int32_t fa_id;
2058
2059 for (slot = 0; slot < slot_cnt; slot++) {
2060 hdr = boot_img_hdr(&boot_data, slot);
2061 fa_id = flash_area_id_from_image_slot(slot);
2062 rc = boot_read_swap_state_by_id(fa_id, &slot_state);
2063 if (rc != 0) {
David Vinczedcba70b2019-05-28 12:02:52 +02002064 BOOT_LOG_ERR("Error during reading image trailer from slot: %u",
2065 slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002066 continue;
2067 }
2068
2069 if (hdr->ih_magic == IMAGE_MAGIC) {
2070 if (slot_state.magic == BOOT_MAGIC_GOOD ||
David Vincze39e78552018-10-10 17:10:01 +02002071 slot_state.image_ok == BOOT_FLAG_SET) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002072 /* Valid cases:
2073 * - Test mode: magic is OK in image trailer
2074 * - Permanent mode: image_ok flag has previously set
2075 */
2076 image_versions[slot].slot_number = slot;
2077 image_versions[slot].version = boot_get_version_number(hdr);
2078 image_cnt++;
2079 }
2080
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002081 BOOT_LOG_IMAGE_INFO(slot, hdr, &slot_state);
2082 } else {
David Vinczedcba70b2019-05-28 12:02:52 +02002083 BOOT_LOG_INF("Image %u: No valid image", slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002084 }
2085 }
2086
2087 /* Sort the images based on version number */
2088 qsort(&image_versions[0],
2089 slot_cnt,
2090 sizeof(struct image_slot_version),
2091 boot_compare_version_numbers);
2092
2093 /* Copy the calculated boot sequence to boot_sequence array */
2094 for (slot = 0; slot < slot_cnt; slot++) {
2095 boot_sequence[slot] = image_versions[slot].slot_number;
2096 }
2097
2098 return image_cnt;
2099}
2100
Oliver Swedef9982442018-08-24 18:37:44 +01002101#ifdef MCUBOOT_RAM_LOADING
2102/**
2103 * Copies an image from a slot in the flash to an SRAM address, where the load
2104 * address has already been inserted into the image header by this point and is
2105 * extracted from it within this method. The copying is done sector-by-sector.
2106 *
2107 * @param slot The flash slot of the image to be copied to SRAM.
2108 *
2109 * @param hdr Pointer to the image header structure of the image
2110 * that needs to be copid to SRAM
2111 *
2112 * @return 0 on success; nonzero on failure.
2113 */
2114static int
2115boot_copy_image_to_sram(int slot, struct image_header *hdr)
2116{
2117 int rc;
2118 uint32_t sect_sz;
2119 uint32_t sect = 0;
2120 uint32_t bytes_copied = 0;
2121 const struct flash_area *fap_src = NULL;
2122 uint32_t dst = (uint32_t) hdr->ih_load_addr;
2123 uint32_t img_sz;
2124
2125 if (dst % 4 != 0) {
Tamas Banc27b5c32019-05-28 16:30:19 +01002126 BOOT_LOG_INF("Cannot copy the image to the SRAM address 0x%x "
Oliver Swedef9982442018-08-24 18:37:44 +01002127 "- the load address must be aligned with 4 bytes due to SRAM "
2128 "restrictions", dst);
2129 return BOOT_EBADARGS;
2130 }
2131
2132 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap_src);
2133 if (rc != 0) {
2134 return BOOT_EFLASH;
2135 }
2136
2137 rc = boot_read_image_size(slot, hdr, &img_sz);
2138 if (rc != 0) {
2139 return BOOT_EFLASH;
2140 }
2141
2142 while (bytes_copied < img_sz) {
2143 sect_sz = boot_img_sector_size(&boot_data, slot, sect);
2144 /*
2145 * Direct copy from where the image sector resides in flash to its new
2146 * location in SRAM
2147 */
2148 rc = flash_area_read(fap_src,
2149 bytes_copied,
2150 (void *)(dst + bytes_copied),
2151 sect_sz);
2152 if (rc != 0) {
2153 BOOT_LOG_INF("Error whilst copying image from Flash to SRAM");
2154 break;
2155 } else {
2156 bytes_copied += sect_sz;
2157 }
2158 sect++;
2159 }
2160
2161 if (fap_src) {
2162 flash_area_close(fap_src);
2163 }
2164 return rc;
2165}
2166#endif /* MCUBOOT_RAM_LOADING */
2167
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002168/**
2169 * Prepares the booting process. This function choose the newer image in flash
2170 * as appropriate, and returns the address to boot from.
2171 *
2172 * @param rsp On success, indicates how booting should occur.
2173 *
2174 * @return 0 on success; nonzero on failure.
2175 */
2176int
2177boot_go(struct boot_rsp *rsp)
2178{
2179 size_t slot = 0;
2180 int32_t i;
2181 int rc;
2182 int fa_id;
2183 uint32_t boot_sequence[BOOT_NUM_SLOTS];
2184 uint32_t img_cnt;
Oliver Swedef9982442018-08-24 18:37:44 +01002185 struct image_header *newest_image_header;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002186
David Vincze8bdfc2d2019-03-18 15:49:23 +01002187 static boot_sector_t primary_slot_sectors[BOOT_MAX_IMG_SECTORS];
2188 static boot_sector_t secondary_slot_sectors[BOOT_MAX_IMG_SECTORS];
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002189
David Vincze7384ee72019-07-23 17:00:42 +02002190 BOOT_IMG(&boot_data, BOOT_PRIMARY_SLOT).sectors =
2191 &primary_slot_sectors[0];
2192 BOOT_IMG(&boot_data, BOOT_SECONDARY_SLOT).sectors =
2193 &secondary_slot_sectors[0];
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002194
2195 /* Open boot_data image areas for the duration of this call. */
2196 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
2197 fa_id = flash_area_id_from_image_slot(i);
2198 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, i));
2199 assert(rc == 0);
2200 }
2201
2202 /* Determine the sector layout of the image slots. */
2203 rc = boot_read_sectors();
2204 if (rc != 0) {
David Vincze39e78552018-10-10 17:10:01 +02002205 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - too small?",
2206 BOOT_MAX_IMG_SECTORS);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002207 goto out;
2208 }
2209
2210 /* Attempt to read an image header from each slot. */
David Vincze39e78552018-10-10 17:10:01 +02002211 rc = boot_read_image_headers(false);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002212 if (rc != 0) {
2213 goto out;
2214 }
2215
2216 img_cnt = boot_get_boot_sequence(boot_sequence, BOOT_NUM_SLOTS);
2217 if (img_cnt) {
2218 /* Authenticate images */
2219 for (i = 0; i < img_cnt; i++) {
David Vincze401c7422019-06-21 20:44:05 +02002220 rc = boot_validate_slot(boot_sequence[i], NULL);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002221 if (rc == 0) {
2222 slot = boot_sequence[i];
2223 break;
2224 }
2225 }
2226 if (rc) {
2227 /* If there was no valid image at all */
2228 rc = BOOT_EBADIMAGE;
2229 goto out;
2230 }
2231
Oliver Swedef9982442018-08-24 18:37:44 +01002232 /* The slot variable now refers to the newest image's slot in flash */
2233 newest_image_header = boot_img_hdr(&boot_data, slot);
2234
David Vincze060968d2019-05-23 01:13:14 +02002235 /* Update the security counter with the newest image's security
2236 * counter value.
2237 */
2238 rc = boot_update_security_counter(slot, newest_image_header);
2239 if (rc != 0) {
2240 BOOT_LOG_ERR("Security counter update failed after image "
2241 "validation.");
2242 goto out;
2243 }
2244
Oliver Swedef9982442018-08-24 18:37:44 +01002245 #ifdef MCUBOOT_RAM_LOADING
2246 if (newest_image_header->ih_flags & IMAGE_F_RAM_LOAD) {
2247 /* Copy image to the load address from where it
2248 * currently resides in flash */
2249 rc = boot_copy_image_to_sram(slot, newest_image_header);
2250 if (rc != 0) {
2251 rc = BOOT_EBADIMAGE;
David Vincze8bdfc2d2019-03-18 15:49:23 +01002252 BOOT_LOG_INF("Could not copy image from the %s slot in "
Tamas Banc27b5c32019-05-28 16:30:19 +01002253 "the Flash to load address 0x%x in SRAM, "
David Vincze8bdfc2d2019-03-18 15:49:23 +01002254 "aborting..", (slot == BOOT_PRIMARY_SLOT) ?
2255 "primary" : "secondary",
Oliver Swedef9982442018-08-24 18:37:44 +01002256 newest_image_header->ih_load_addr);
2257 goto out;
2258 } else {
David Vincze8bdfc2d2019-03-18 15:49:23 +01002259 BOOT_LOG_INF("Image has been copied from the %s slot in "
2260 "the flash to SRAM address 0x%x",
2261 (slot == BOOT_PRIMARY_SLOT) ?
2262 "primary" : "secondary",
Oliver Swedef9982442018-08-24 18:37:44 +01002263 newest_image_header->ih_load_addr);
2264 }
2265
2266 /* Validate the image hash in SRAM after the copy was successful */
2267 rc = bootutil_check_hash_after_loading(newest_image_header);
2268 if (rc != 0) {
2269 rc = BOOT_EBADIMAGE;
2270 BOOT_LOG_INF("Cannot validate the hash of the image that was "
2271 "copied to SRAM, aborting..");
2272 goto out;
2273 }
2274
Tamas Banc27b5c32019-05-28 16:30:19 +01002275 BOOT_LOG_INF("Booting image from SRAM at address 0x%x",
Oliver Swedef9982442018-08-24 18:37:44 +01002276 newest_image_header->ih_load_addr);
2277 } else {
David Vincze8a2a4e22019-05-24 10:14:23 +02002278#endif /* MCUBOOT_RAM_LOADING */
David Vincze8bdfc2d2019-03-18 15:49:23 +01002279 BOOT_LOG_INF("Booting image from the %s slot",
2280 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Vincze8a2a4e22019-05-24 10:14:23 +02002281#ifdef MCUBOOT_RAM_LOADING
Oliver Swedef9982442018-08-24 18:37:44 +01002282 }
David Vincze8a2a4e22019-05-24 10:14:23 +02002283#endif
Oliver Swedef9982442018-08-24 18:37:44 +01002284
2285 rsp->br_hdr = newest_image_header;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002286 rsp->br_image_off = boot_img_slot_off(&boot_data, slot);
David Vincze7384ee72019-07-23 17:00:42 +02002287 rsp->br_flash_dev_id = BOOT_IMG_AREA(&boot_data, slot)->fa_device_id;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002288 } else {
2289 /* No candidate image available */
2290 rc = BOOT_EBADIMAGE;
David Vincze060968d2019-05-23 01:13:14 +02002291 goto out;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002292 }
2293
Tamas Ban0e8ab302019-01-17 11:45:31 +00002294 /* Save boot status to shared memory area */
2295 rc = boot_save_boot_status(SW_S_NS,
2296 rsp->br_hdr,
2297 BOOT_IMG_AREA(&boot_data, slot));
2298 if (rc) {
2299 BOOT_LOG_ERR("Failed to add data to shared area");
2300 }
2301
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002302out:
2303 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2304 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
2305 }
2306 return rc;
2307}
Oliver Swedef9982442018-08-24 18:37:44 +01002308#endif /* MCUBOOT_NO_SWAP || MCUBOOT_RAM_LOADING */