blob: e340b44278940535cf4af5c419e9430e0a04cc9f [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 Vincze2ddc1372019-10-25 11:10:08 +020023 * Git SHA of the original version: ac55554059147fff718015be9f4bd3108123f50a
David Vincze225c58f2019-12-09 17:32:48 +010024 * Modifications are Copyright (c) 2018-2020 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>
David Vincze225c58f2019-12-09 17:32:48 +010038#include "sysflash/sysflash.h"
Tamas Banc3828852018-02-01 12:24:16 +000039#include "flash_map/flash_map.h"
David Vincze225c58f2019-12-09 17:32:48 +010040#include "flash_map_backend/flash_map_backend.h"
Tamas Banf70ef8c2017-12-19 15:35:09 +000041#include "bootutil/bootutil.h"
42#include "bootutil/image.h"
43#include "bootutil_priv.h"
David Vincze73dfbc52019-10-11 13:54:58 +020044#include "bootutil/bootutil_log.h"
Raef Coles8efad882020-07-10 09:46:00 +010045#include "tfm_boot_status.h"
46#include "bootutil/boot_record.h"
David Vincze060968d2019-05-23 01:13:14 +020047#include "security_cnt.h"
Balint Matyi2fe04922020-02-18 12:27:38 +000048#include "mcuboot_config/mcuboot_config.h"
Tamas Banf70ef8c2017-12-19 15:35:09 +000049
Tamas Banf70ef8c2017-12-19 15:35:09 +000050static struct boot_loader_state boot_data;
David Vinczecea8b592019-10-29 16:09:51 +010051
52#if (BOOT_IMAGE_NUMBER > 1)
53#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
54#else
55#define IMAGES_ITER(x)
56#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +000057
Tamas Ban0bd0dfc2020-09-11 15:25:48 +010058#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOADING) && \
David Vinczedf12de02020-06-05 10:59:27 +020059 !defined(MCUBOOT_OVERWRITE_ONLY)
David Vincze39e78552018-10-10 17:10:01 +020060
David Vincze8bdfc2d2019-03-18 15:49:23 +010061#if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT) && !defined(MCUBOOT_OVERWRITE_ONLY)
David Vincze39e78552018-10-10 17:10:01 +020062static int boot_status_fails = 0;
63#define BOOT_STATUS_ASSERT(x) \
64 do { \
65 if (!(x)) { \
66 boot_status_fails++; \
67 } \
68 } while (0)
69#else
David Vincze401c7422019-06-21 20:44:05 +020070#define BOOT_STATUS_ASSERT(x) ASSERT(x)
David Vinczedf12de02020-06-05 10:59:27 +020071#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT && !MCUBOOT_OVERWRITE_ONLY */
David Vincze39e78552018-10-10 17:10:01 +020072
Tamas Banf70ef8c2017-12-19 15:35:09 +000073struct boot_status_table {
David Vincze8bdfc2d2019-03-18 15:49:23 +010074 uint8_t bst_magic_primary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +000075 uint8_t bst_magic_scratch;
David Vincze8bdfc2d2019-03-18 15:49:23 +010076 uint8_t bst_copy_done_primary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +000077 uint8_t bst_status_source;
78};
79
80/**
81 * This set of tables maps swap state contents to boot status location.
82 * When searching for a match, these tables must be iterated in order.
83 */
84static const struct boot_status_table boot_status_tables[] = {
85 {
David Vincze8bdfc2d2019-03-18 15:49:23 +010086 /* | primary slot | scratch |
87 * ----------+--------------+--------------|
88 * magic | Good | Any |
89 * copy-done | Set | N/A |
90 * ----------+--------------+--------------'
91 * source: none |
92 * ----------------------------------------'
Tamas Banf70ef8c2017-12-19 15:35:09 +000093 */
David Vincze8bdfc2d2019-03-18 15:49:23 +010094 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
David Vincze401c7422019-06-21 20:44:05 +020095 .bst_magic_scratch = BOOT_MAGIC_NOTGOOD,
David Vincze8bdfc2d2019-03-18 15:49:23 +010096 .bst_copy_done_primary_slot = BOOT_FLAG_SET,
97 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
Tamas Banf70ef8c2017-12-19 15:35:09 +000098 },
99
100 {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100101 /* | primary slot | scratch |
102 * ----------+--------------+--------------|
103 * magic | Good | Any |
104 * copy-done | Unset | N/A |
105 * ----------+--------------+--------------'
106 * source: primary slot |
107 * ----------------------------------------'
Tamas Banf70ef8c2017-12-19 15:35:09 +0000108 */
David Vincze8bdfc2d2019-03-18 15:49:23 +0100109 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
David Vincze401c7422019-06-21 20:44:05 +0200110 .bst_magic_scratch = BOOT_MAGIC_NOTGOOD,
David Vincze8bdfc2d2019-03-18 15:49:23 +0100111 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
112 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Tamas Banf70ef8c2017-12-19 15:35:09 +0000113 },
114
115 {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100116 /* | primary slot | scratch |
117 * ----------+--------------+--------------|
118 * magic | Any | Good |
119 * copy-done | Any | N/A |
120 * ----------+--------------+--------------'
121 * source: scratch |
122 * ----------------------------------------'
Tamas Banf70ef8c2017-12-19 15:35:09 +0000123 */
David Vincze8bdfc2d2019-03-18 15:49:23 +0100124 .bst_magic_primary_slot = BOOT_MAGIC_ANY,
125 .bst_magic_scratch = BOOT_MAGIC_GOOD,
126 .bst_copy_done_primary_slot = BOOT_FLAG_ANY,
127 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
Tamas Banf70ef8c2017-12-19 15:35:09 +0000128 },
129
130 {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100131 /* | primary slot | scratch |
132 * ----------+--------------+--------------|
133 * magic | Unset | Any |
134 * copy-done | Unset | N/A |
135 * ----------+--------------+--------------|
136 * source: varies |
137 * ----------------------------------------+--------------------------+
Tamas Banf70ef8c2017-12-19 15:35:09 +0000138 * This represents one of two cases: |
139 * o No swaps ever (no status to read, so no harm in checking). |
David Vincze8bdfc2d2019-03-18 15:49:23 +0100140 * o Mid-revert; status in the primary slot. |
Tamas Banf70ef8c2017-12-19 15:35:09 +0000141 * -------------------------------------------------------------------'
142 */
David Vincze8bdfc2d2019-03-18 15:49:23 +0100143 .bst_magic_primary_slot = BOOT_MAGIC_UNSET,
144 .bst_magic_scratch = BOOT_MAGIC_ANY,
145 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
146 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Tamas Banf70ef8c2017-12-19 15:35:09 +0000147 },
148};
149
150#define BOOT_STATUS_TABLES_COUNT \
Tamas Ban581034a2017-12-19 19:54:37 +0000151 (sizeof(boot_status_tables) / sizeof(boot_status_tables[0]))
Tamas Banf70ef8c2017-12-19 15:35:09 +0000152
153#define BOOT_LOG_SWAP_STATE(area, state) \
David Vincze401c7422019-06-21 20:44:05 +0200154 BOOT_LOG_INF("%s: magic=%5s, swap_type=0x%x, copy_done=0x%x, " \
155 "image_ok=0x%x", \
Tamas Banf70ef8c2017-12-19 15:35:09 +0000156 (area), \
157 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
158 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
159 "bad"), \
David Vincze401c7422019-06-21 20:44:05 +0200160 (state)->swap_type, \
Tamas Banf70ef8c2017-12-19 15:35:09 +0000161 (state)->copy_done, \
162 (state)->image_ok)
Tamas Ban0bd0dfc2020-09-11 15:25:48 +0100163#endif /* !MCUBOOT_DIRECT_XIP && !MCUBOOT_RAM_LOADING && !MCUBOOT_OVERWRITE_ONLY */
Tamas Banf70ef8c2017-12-19 15:35:09 +0000164
Tamas Ban056ed0b2019-09-16 12:48:32 +0100165/*
166 * \brief Verifies the image header: magic value, flags, integer overflow.
167 *
168 * \retval 0
169 * \retval BOOT_EBADIMAGE
170 */
171static int
172boot_verify_image_header(struct image_header *hdr)
173{
174 uint32_t image_end;
David Vincze0dc41d22019-10-28 14:56:29 +0100175 uint32_t x;
Tamas Ban056ed0b2019-09-16 12:48:32 +0100176
177 if (hdr->ih_magic != IMAGE_MAGIC) {
178 return BOOT_EBADIMAGE;
179 }
180
181 /* Check input parameters against integer overflow */
David Vincze0dc41d22019-10-28 14:56:29 +0100182 if (!boot_u32_safe_add(&image_end, hdr->ih_hdr_size, hdr->ih_img_size)) {
Tamas Ban056ed0b2019-09-16 12:48:32 +0100183 return BOOT_EBADIMAGE;
184 }
185
David Vincze0dc41d22019-10-28 14:56:29 +0100186 if (!boot_u32_safe_add(&x, image_end, hdr->ih_protect_tlv_size)) {
Tamas Ban056ed0b2019-09-16 12:48:32 +0100187 return BOOT_EBADIMAGE;
188 }
189
Raef Coles1e089702020-04-20 13:18:04 +0100190#ifdef MCUBOOT_RAM_LOADING
Tamas Ban056ed0b2019-09-16 12:48:32 +0100191 if (!(hdr->ih_flags & IMAGE_F_RAM_LOAD)) {
192 return BOOT_EBADIMAGE;
193 }
194
195 /* Check input parameters against integer overflow */
David Vincze0dc41d22019-10-28 14:56:29 +0100196 if (!boot_u32_safe_add(&x, image_end, hdr->ih_load_addr)) {
Tamas Ban056ed0b2019-09-16 12:48:32 +0100197 return BOOT_EBADIMAGE;
198 }
199#endif
200
201 return 0;
202}
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000203
204static int
David Vinczecea8b592019-10-29 16:09:51 +0100205boot_read_image_header(struct boot_loader_state *state, int slot,
206 struct image_header *out_hdr)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000207{
208 const struct flash_area *fap = NULL;
209 int area_id;
210 int rc;
211
David Vinczecea8b592019-10-29 16:09:51 +0100212#if (BOOT_IMAGE_NUMBER == 1)
213 (void)state;
214#endif
215
216 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000217 rc = flash_area_open(area_id, &fap);
218 if (rc != 0) {
219 rc = BOOT_EFLASH;
220 goto done;
221 }
222
223 rc = flash_area_read(fap, 0, out_hdr, sizeof(*out_hdr));
224 if (rc != 0) {
225 rc = BOOT_EFLASH;
226 goto done;
227 }
228
Tamas Ban056ed0b2019-09-16 12:48:32 +0100229 rc = boot_verify_image_header(out_hdr);
David Vinczecea8b592019-10-29 16:09:51 +0100230 BOOT_IMG_HDR_IS_VALID(state, slot) = (rc == 0);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000231
232done:
233 flash_area_close(fap);
234 return rc;
235}
236
237static int
David Vinczecea8b592019-10-29 16:09:51 +0100238boot_read_image_headers(struct boot_loader_state *state, bool require_all)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000239{
240 int rc;
241 int i;
242
243 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
David Vinczecea8b592019-10-29 16:09:51 +0100244 rc = boot_read_image_header(state, i, boot_img_hdr(state, i));
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000245 if (rc != 0) {
David Vincze39e78552018-10-10 17:10:01 +0200246 /* If `require_all` is set, fail on any single fail, otherwise
247 * if at least the first slot's header was read successfully,
248 * then the boot loader can attempt a boot.
249 *
250 * Failure to read any headers is a fatal error.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000251 */
David Vincze39e78552018-10-10 17:10:01 +0200252 if (i > 0 && !require_all) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000253 return 0;
254 } else {
255 return rc;
256 }
257 }
258 }
259
260 return 0;
261}
262
Raef Coles204c5b42019-09-05 09:18:47 +0100263static uint32_t
David Vinczecea8b592019-10-29 16:09:51 +0100264boot_write_sz(struct boot_loader_state *state)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000265{
Raef Coles204c5b42019-09-05 09:18:47 +0100266 uint32_t elem_sz;
267 uint32_t align;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000268
269 /* Figure out what size to write update status update as. The size depends
270 * on what the minimum write size is for scratch area, active image slot.
271 * We need to use the bigger of those 2 values.
272 */
David Vinczecea8b592019-10-29 16:09:51 +0100273 elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
274 align = flash_area_align(BOOT_SCRATCH_AREA(state));
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000275 if (align > elem_sz) {
276 elem_sz = align;
277 }
278
279 return elem_sz;
280}
281
David Vinczecea8b592019-10-29 16:09:51 +0100282#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
283static int
284boot_initialize_area(struct boot_loader_state *state, int flash_area)
285{
286 int num_sectors = BOOT_MAX_IMG_SECTORS;
287 int rc;
288
289 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
290 rc = flash_area_to_sectors(flash_area, &num_sectors,
291 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors);
292 BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors = (size_t)num_sectors;
293
294 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
295 rc = flash_area_to_sectors(flash_area, &num_sectors,
296 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors);
297 BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors = (size_t)num_sectors;
298
299 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
300 rc = flash_area_to_sectors(flash_area, &num_sectors,
301 state->scratch.sectors);
302 state->scratch.num_sectors = (size_t)num_sectors;
303 } else {
304 return BOOT_EFLASH;
305 }
306
307 return rc;
308}
309#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
310static int
311boot_initialize_area(struct boot_loader_state *state, int flash_area)
312{
313 uint32_t num_sectors;
314 struct flash_sector *out_sectors;
315 size_t *out_num_sectors;
316 int rc;
317
318 num_sectors = BOOT_MAX_IMG_SECTORS;
319
320 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
321 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
322 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
323 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
324 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
325 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
326 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
327 out_sectors = state->scratch.sectors;
328 out_num_sectors = &state->scratch.num_sectors;
329 } else {
330 return BOOT_EFLASH;
331 }
332
333 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
334 if (rc != 0) {
335 return rc;
336 }
337 *out_num_sectors = num_sectors;
338 return 0;
339}
340#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
341
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000342/**
343 * Determines the sector layout of both image slots and the scratch area.
344 * This information is necessary for calculating the number of bytes to erase
345 * and copy during an image swap. The information collected during this
David Vinczecea8b592019-10-29 16:09:51 +0100346 * function is used to populate the state.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000347 */
348static int
David Vinczecea8b592019-10-29 16:09:51 +0100349boot_read_sectors(struct boot_loader_state *state)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000350{
David Vinczecea8b592019-10-29 16:09:51 +0100351 uint8_t image_index;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000352 int rc;
353
David Vinczecea8b592019-10-29 16:09:51 +0100354 image_index = BOOT_CURR_IMG(state);
355
356 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000357 if (rc != 0) {
358 return BOOT_EFLASH;
359 }
360
David Vinczecea8b592019-10-29 16:09:51 +0100361 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000362 if (rc != 0) {
363 return BOOT_EFLASH;
364 }
365
David Vinczecea8b592019-10-29 16:09:51 +0100366 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
David Vincze401c7422019-06-21 20:44:05 +0200367 if (rc != 0) {
368 return BOOT_EFLASH;
369 }
370
David Vinczecea8b592019-10-29 16:09:51 +0100371 BOOT_WRITE_SZ(state) = boot_write_sz(state);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000372
373 return 0;
374}
375
David Vincze060968d2019-05-23 01:13:14 +0200376/**
377 * Validate image hash/signature and security counter in a slot.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000378 */
379static int
David Vinczecea8b592019-10-29 16:09:51 +0100380boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
381 const struct flash_area *fap, struct boot_status *bs)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000382{
383 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
David Vinczecea8b592019-10-29 16:09:51 +0100384 uint8_t image_index;
385
386#if (BOOT_IMAGE_NUMBER == 1)
387 (void)state;
388#endif
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000389
David Vincze401c7422019-06-21 20:44:05 +0200390 (void)bs;
391
David Vinczecea8b592019-10-29 16:09:51 +0100392 image_index = BOOT_CURR_IMG(state);
393
394 if (bootutil_img_validate(image_index, hdr, fap, tmpbuf,
395 BOOT_TMPBUF_SZ, NULL, 0, NULL)) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000396 return BOOT_EBADIMAGE;
397 }
David Vinczecea8b592019-10-29 16:09:51 +0100398
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000399 return 0;
400}
401
David Vincze401c7422019-06-21 20:44:05 +0200402/*
403 * Check that a memory area consists of a given value.
404 */
405static inline bool
406boot_data_is_set_to(uint8_t val, void *data, size_t len)
David Vincze39e78552018-10-10 17:10:01 +0200407{
408 uint8_t i;
David Vincze401c7422019-06-21 20:44:05 +0200409 uint8_t *p = (uint8_t *)data;
410 for (i = 0; i < len; i++) {
411 if (val != p[i]) {
412 return false;
David Vincze39e78552018-10-10 17:10:01 +0200413 }
414 }
David Vincze401c7422019-06-21 20:44:05 +0200415 return true;
David Vincze39e78552018-10-10 17:10:01 +0200416}
417
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000418static int
David Vinczecea8b592019-10-29 16:09:51 +0100419boot_check_header_erased(struct boot_loader_state *state, int slot)
David Vincze401c7422019-06-21 20:44:05 +0200420{
421 const struct flash_area *fap;
422 struct image_header *hdr;
423 uint8_t erased_val;
David Vinczecea8b592019-10-29 16:09:51 +0100424 int area_id;
David Vincze401c7422019-06-21 20:44:05 +0200425 int rc;
426
David Vinczecea8b592019-10-29 16:09:51 +0100427 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
428 rc = flash_area_open(area_id, &fap);
David Vincze401c7422019-06-21 20:44:05 +0200429 if (rc != 0) {
430 return -1;
431 }
432
433 erased_val = flash_area_erased_val(fap);
434 flash_area_close(fap);
435
David Vinczecea8b592019-10-29 16:09:51 +0100436 hdr = boot_img_hdr(state, slot);
David Vincze401c7422019-06-21 20:44:05 +0200437 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic,
438 sizeof(hdr->ih_magic))) {
439 return -1;
440 }
441
442 return 0;
443}
444
David Vinczecea8b592019-10-29 16:09:51 +0100445/*
446 * Check that there is a valid image in a slot
447 *
448 * @returns
449 * 0 if image was succesfully validated
450 * 1 if no bootloable image was found
451 * -1 on any errors
452 */
David Vincze401c7422019-06-21 20:44:05 +0200453static int
David Vinczecea8b592019-10-29 16:09:51 +0100454boot_validate_slot(struct boot_loader_state *state, int slot,
455 struct boot_status *bs)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000456{
457 const struct flash_area *fap;
458 struct image_header *hdr;
David Vinczecea8b592019-10-29 16:09:51 +0100459 int area_id;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000460 int rc;
461
David Vinczecea8b592019-10-29 16:09:51 +0100462 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
463 rc = flash_area_open(area_id, &fap);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000464 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +0100465 return -1;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000466 }
467
David Vinczecea8b592019-10-29 16:09:51 +0100468 hdr = boot_img_hdr(state, slot);
469 if ((boot_check_header_erased(state, slot) == 0) ||
David Vincze401c7422019-06-21 20:44:05 +0200470 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100471 /* No bootable image in slot; continue booting from the primary slot. */
David Vinczecea8b592019-10-29 16:09:51 +0100472 rc = 1;
David Vincze401c7422019-06-21 20:44:05 +0200473 goto out;
David Vincze39e78552018-10-10 17:10:01 +0200474 }
475
David Vinczecea8b592019-10-29 16:09:51 +0100476 if ((!BOOT_IMG_HDR_IS_VALID(state, slot)) ||
477 (boot_image_check(state, hdr, fap, bs) != 0)) {
David Vincze401c7422019-06-21 20:44:05 +0200478 if (slot != BOOT_PRIMARY_SLOT) {
David Vinczecea8b592019-10-29 16:09:51 +0100479 flash_area_erase(fap, 0, fap->fa_size);
David Vincze8bdfc2d2019-03-18 15:49:23 +0100480 /* Image in the secondary slot is invalid. Erase the image and
481 * continue booting from the primary slot.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000482 */
483 }
David Vinczecea8b592019-10-29 16:09:51 +0100484 BOOT_LOG_ERR("Image in the %s slot is not valid!",
485 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Vincze401c7422019-06-21 20:44:05 +0200486 rc = -1;
487 goto out;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000488 }
489
David Vincze8bdfc2d2019-03-18 15:49:23 +0100490 /* Image in the secondary slot is valid. */
David Vincze401c7422019-06-21 20:44:05 +0200491 rc = 0;
492
493out:
494 flash_area_close(fap);
495 return rc;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000496}
497
David Vincze060968d2019-05-23 01:13:14 +0200498/**
499 * Updates the stored security counter value with the image's security counter
500 * value which resides in the given slot if it's greater than the stored value.
501 *
David Vinczecea8b592019-10-29 16:09:51 +0100502 * @param image_index Index of the image to determine which security
503 * counter to update.
504 * @param slot Slot number of the image.
505 * @param hdr Pointer to the image header structure of the image
506 * that is currently stored in the given slot.
David Vincze060968d2019-05-23 01:13:14 +0200507 *
David Vinczecea8b592019-10-29 16:09:51 +0100508 * @return 0 on success; nonzero on failure.
David Vincze060968d2019-05-23 01:13:14 +0200509 */
510static int
David Vinczecea8b592019-10-29 16:09:51 +0100511boot_update_security_counter(uint8_t image_index, int slot,
512 struct image_header *hdr)
David Vincze060968d2019-05-23 01:13:14 +0200513{
514 const struct flash_area *fap = NULL;
515 uint32_t img_security_cnt;
516 int rc;
517
David Vinczecea8b592019-10-29 16:09:51 +0100518 rc = flash_area_open(flash_area_id_from_multi_image_slot(image_index, slot),
519 &fap);
David Vincze060968d2019-05-23 01:13:14 +0200520 if (rc != 0) {
521 rc = BOOT_EFLASH;
522 goto done;
523 }
524
525 rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt);
526 if (rc != 0) {
527 goto done;
528 }
529
David Vinczecea8b592019-10-29 16:09:51 +0100530 rc = boot_nv_security_counter_update(image_index, img_security_cnt);
David Vincze060968d2019-05-23 01:13:14 +0200531 if (rc != 0) {
532 goto done;
533 }
534
535done:
536 flash_area_close(fap);
537 return rc;
538}
539
Tamas Ban0bd0dfc2020-09-11 15:25:48 +0100540#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_OVERWRITE_ONLY)
Oliver Swedef9982442018-08-24 18:37:44 +0100541/*
542 * Compute the total size of the given image. Includes the size of
543 * the TLVs.
544 */
545static int
David Vinczecea8b592019-10-29 16:09:51 +0100546boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
Oliver Swedef9982442018-08-24 18:37:44 +0100547{
548 const struct flash_area *fap = NULL;
David Vincze07706a42019-12-12 18:20:12 +0100549 struct image_tlv_info info;
David Vinczecea8b592019-10-29 16:09:51 +0100550 uint32_t off;
David Vincze61bd1e52019-10-24 16:47:31 +0200551 uint32_t protect_tlv_size;
Oliver Swedef9982442018-08-24 18:37:44 +0100552 int area_id;
553 int rc;
554
David Vinczecea8b592019-10-29 16:09:51 +0100555#if (BOOT_IMAGE_NUMBER == 1)
556 (void)state;
557#endif
558
559 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Oliver Swedef9982442018-08-24 18:37:44 +0100560 rc = flash_area_open(area_id, &fap);
561 if (rc != 0) {
562 rc = BOOT_EFLASH;
563 goto done;
564 }
565
David Vincze07706a42019-12-12 18:20:12 +0100566 off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
567
568 if (flash_area_read(fap, off, &info, sizeof(info))) {
569 rc = BOOT_EFLASH;
Oliver Swedef9982442018-08-24 18:37:44 +0100570 goto done;
571 }
David Vincze07706a42019-12-12 18:20:12 +0100572
David Vincze61bd1e52019-10-24 16:47:31 +0200573 protect_tlv_size = boot_img_hdr(state, slot)->ih_protect_tlv_size;
574 if (info.it_magic == IMAGE_TLV_PROT_INFO_MAGIC) {
575 if (protect_tlv_size != info.it_tlv_tot) {
576 rc = BOOT_EBADIMAGE;
577 goto done;
578 }
579
580 if (flash_area_read(fap, off + info.it_tlv_tot, &info, sizeof(info))) {
581 rc = BOOT_EFLASH;
582 goto done;
583 }
584 } else if (protect_tlv_size != 0) {
585 rc = BOOT_EBADIMAGE;
586 goto done;
587 }
588
David Vincze07706a42019-12-12 18:20:12 +0100589 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
590 rc = BOOT_EBADIMAGE;
591 goto done;
592 }
593
David Vincze61bd1e52019-10-24 16:47:31 +0200594 *size = off + protect_tlv_size + info.it_tlv_tot;
Oliver Swedef9982442018-08-24 18:37:44 +0100595 rc = 0;
596
597done:
598 flash_area_close(fap);
599 return rc;
600}
Tamas Ban0bd0dfc2020-09-11 15:25:48 +0100601#endif /* !MCUBOOT_DIRECT_XIP && !MCUBOOT_OVERWRITE_ONLY */
Oliver Swedef9982442018-08-24 18:37:44 +0100602
Tamas Ban0bd0dfc2020-09-11 15:25:48 +0100603#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOADING)
David Vincze401c7422019-06-21 20:44:05 +0200604/*
David Vincze4af67832019-12-11 18:31:34 +0100605 * Slots are compatible when all sectors that store up to to size of the image
David Vincze401c7422019-06-21 20:44:05 +0200606 * round up to sector size, in both slot's are able to fit in the scratch
607 * area, and have sizes that are a multiple of each other (powers of two
608 * presumably!).
Tamas Banf70ef8c2017-12-19 15:35:09 +0000609 */
610static int
David Vinczecea8b592019-10-29 16:09:51 +0100611boot_slots_compatible(struct boot_loader_state *state)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000612{
David Vincze401c7422019-06-21 20:44:05 +0200613 size_t num_sectors_primary;
614 size_t num_sectors_secondary;
615 size_t sz0, sz1;
616 size_t primary_slot_sz, secondary_slot_sz;
David Vincze4af67832019-12-11 18:31:34 +0100617#ifndef MCUBOOT_OVERWRITE_ONLY
David Vincze401c7422019-06-21 20:44:05 +0200618 size_t scratch_sz;
David Vincze4af67832019-12-11 18:31:34 +0100619#endif
David Vincze401c7422019-06-21 20:44:05 +0200620 size_t i, j;
621 int8_t smaller;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000622
David Vinczecea8b592019-10-29 16:09:51 +0100623 num_sectors_primary = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
624 num_sectors_secondary = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT);
David Vincze401c7422019-06-21 20:44:05 +0200625 if ((num_sectors_primary > BOOT_MAX_IMG_SECTORS) ||
626 (num_sectors_secondary > BOOT_MAX_IMG_SECTORS)) {
David Vincze39e78552018-10-10 17:10:01 +0200627 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
Tamas Banf70ef8c2017-12-19 15:35:09 +0000628 return 0;
629 }
David Vincze39e78552018-10-10 17:10:01 +0200630
David Vincze4af67832019-12-11 18:31:34 +0100631#ifndef MCUBOOT_OVERWRITE_ONLY
David Vinczecea8b592019-10-29 16:09:51 +0100632 scratch_sz = boot_scratch_area_size(state);
David Vincze4af67832019-12-11 18:31:34 +0100633#endif
David Vincze401c7422019-06-21 20:44:05 +0200634
635 /*
636 * The following loop scans all sectors in a linear fashion, assuring that
637 * for each possible sector in each slot, it is able to fit in the other
638 * slot's sector or sectors. Slot's should be compatible as long as any
639 * number of a slot's sectors are able to fit into another, which only
640 * excludes cases where sector sizes are not a multiple of each other.
641 */
642 i = sz0 = primary_slot_sz = 0;
643 j = sz1 = secondary_slot_sz = 0;
644 smaller = 0;
645 while (i < num_sectors_primary || j < num_sectors_secondary) {
646 if (sz0 == sz1) {
David Vinczecea8b592019-10-29 16:09:51 +0100647 sz0 += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
648 sz1 += boot_img_sector_size(state, BOOT_SECONDARY_SLOT, j);
David Vincze401c7422019-06-21 20:44:05 +0200649 i++;
650 j++;
651 } else if (sz0 < sz1) {
David Vinczecea8b592019-10-29 16:09:51 +0100652 sz0 += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
David Vincze401c7422019-06-21 20:44:05 +0200653 /* Guarantee that multiple sectors of the secondary slot
654 * fit into the primary slot.
655 */
656 if (smaller == 2) {
657 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible"
658 " sectors");
659 return 0;
660 }
661 smaller = 1;
662 i++;
663 } else {
David Vinczecea8b592019-10-29 16:09:51 +0100664 sz1 += boot_img_sector_size(state, BOOT_SECONDARY_SLOT, j);
David Vincze401c7422019-06-21 20:44:05 +0200665 /* Guarantee that multiple sectors of the primary slot
666 * fit into the secondary slot.
667 */
668 if (smaller == 1) {
669 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible"
670 " sectors");
671 return 0;
672 }
673 smaller = 2;
674 j++;
675 }
David Vincze4af67832019-12-11 18:31:34 +0100676#ifndef MCUBOOT_OVERWRITE_ONLY
David Vincze401c7422019-06-21 20:44:05 +0200677 if (sz0 == sz1) {
678 primary_slot_sz += sz0;
679 secondary_slot_sz += sz1;
680 /* Scratch has to fit each swap operation to the size of the larger
681 * sector among the primary slot and the secondary slot.
682 */
683 if (sz0 > scratch_sz || sz1 > scratch_sz) {
684 BOOT_LOG_WRN("Cannot upgrade: not all sectors fit inside"
685 " scratch");
686 return 0;
687 }
688 smaller = sz0 = sz1 = 0;
689 }
David Vincze4af67832019-12-11 18:31:34 +0100690#endif
David Vincze39e78552018-10-10 17:10:01 +0200691 }
692
David Vincze401c7422019-06-21 20:44:05 +0200693 if ((i != num_sectors_primary) ||
694 (j != num_sectors_secondary) ||
695 (primary_slot_sz != secondary_slot_sz)) {
696 BOOT_LOG_WRN("Cannot upgrade: slots are not compatible");
697 return 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000698 }
699
700 return 1;
701}
702
Tamas Banf70ef8c2017-12-19 15:35:09 +0000703static uint32_t
704boot_status_internal_off(int idx, int state, int elem_sz)
705{
706 int idx_sz;
707
708 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
709
David Vincze39e78552018-10-10 17:10:01 +0200710 return (idx - BOOT_STATUS_IDX_0) * idx_sz +
711 (state - BOOT_STATUS_STATE_0) * elem_sz;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000712}
713
David Vinczedf12de02020-06-05 10:59:27 +0200714#ifndef MCUBOOT_OVERWRITE_ONLY
715/**
716 * Determines where in flash the most recent boot status is stored. The boot
717 * status is necessary for completing a swap that was interrupted by a boot
718 * loader reset.
719 *
720 * @return A BOOT_STATUS_SOURCE_[...] code indicating where status should
721 * be read from.
722 */
723static int
724boot_status_source(struct boot_loader_state *state)
725{
726 const struct boot_status_table *table;
727 struct boot_swap_state state_scratch;
728 struct boot_swap_state state_primary_slot;
729 int rc;
730 size_t i;
731 uint8_t source;
732 uint8_t image_index;
733
734#if (BOOT_IMAGE_NUMBER == 1)
735 (void)state;
736#endif
737
738 image_index = BOOT_CURR_IMG(state);
739 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
740 &state_primary_slot);
741 assert(rc == 0);
742
743 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
744 assert(rc == 0);
745
746 BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot);
747 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
748
749 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
750 table = &boot_status_tables[i];
751
752 if (boot_magic_compatible_check(table->bst_magic_primary_slot,
753 state_primary_slot.magic) &&
754 boot_magic_compatible_check(table->bst_magic_scratch,
755 state_scratch.magic) &&
756 (table->bst_copy_done_primary_slot == BOOT_FLAG_ANY ||
757 table->bst_copy_done_primary_slot == state_primary_slot.copy_done))
758 {
759 source = table->bst_status_source;
760
761#if (BOOT_IMAGE_NUMBER > 1)
762 /* In case of multi-image boot it can happen that if boot status
763 * info is found on scratch area then it does not belong to the
764 * currently examined image.
765 */
766 if (source == BOOT_STATUS_SOURCE_SCRATCH &&
767 state_scratch.image_num != BOOT_CURR_IMG(state)) {
768 source = BOOT_STATUS_SOURCE_NONE;
769 }
770#endif
771
772 BOOT_LOG_INF("Boot source: %s",
773 source == BOOT_STATUS_SOURCE_NONE ? "none" :
774 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
775 source == BOOT_STATUS_SOURCE_PRIMARY_SLOT ?
776 "primary slot" : "BUG; can't happen");
777 return source;
778 }
779 }
780
781 BOOT_LOG_INF("Boot source: none");
782 return BOOT_STATUS_SOURCE_NONE;
783}
784
Tamas Banf70ef8c2017-12-19 15:35:09 +0000785/**
786 * Reads the status of a partially-completed swap, if any. This is necessary
787 * to recover in case the boot lodaer was reset in the middle of a swap
788 * operation.
789 */
790static int
David Vinczecea8b592019-10-29 16:09:51 +0100791boot_read_status_bytes(const struct flash_area *fap,
792 struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000793{
794 uint32_t off;
795 uint8_t status;
796 int max_entries;
797 int found;
David Vincze39e78552018-10-10 17:10:01 +0200798 int found_idx;
799 int invalid;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000800 int rc;
801 int i;
802
803 off = boot_status_off(fap);
David Vinczecea8b592019-10-29 16:09:51 +0100804 max_entries = boot_status_entries(BOOT_CURR_IMG(state), fap);
805 if (max_entries < 0) {
806 return BOOT_EBADARGS;
807 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000808
809 found = 0;
David Vincze39e78552018-10-10 17:10:01 +0200810 found_idx = 0;
811 invalid = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000812 for (i = 0; i < max_entries; i++) {
David Vinczecea8b592019-10-29 16:09:51 +0100813 rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(state),
David Vincze39e78552018-10-10 17:10:01 +0200814 &status, 1);
815 if (rc < 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000816 return BOOT_EFLASH;
817 }
818
David Vincze39e78552018-10-10 17:10:01 +0200819 if (rc == 1) {
820 if (found && !found_idx) {
821 found_idx = i;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000822 }
823 } else if (!found) {
824 found = 1;
David Vincze39e78552018-10-10 17:10:01 +0200825 } else if (found_idx) {
826 invalid = 1;
827 break;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000828 }
829 }
830
David Vincze39e78552018-10-10 17:10:01 +0200831 if (invalid) {
832 /* This means there was an error writing status on the last
833 * swap. Tell user and move on to validation!
834 */
835 BOOT_LOG_ERR("Detected inconsistent status!");
836
David Vincze8bdfc2d2019-03-18 15:49:23 +0100837#if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
838 /* With validation of the primary slot disabled, there is no way
839 * to be sure the swapped primary slot is OK, so abort!
David Vincze39e78552018-10-10 17:10:01 +0200840 */
841 assert(0);
842#endif
843 }
844
Tamas Banf70ef8c2017-12-19 15:35:09 +0000845 if (found) {
David Vincze39e78552018-10-10 17:10:01 +0200846 if (!found_idx) {
847 found_idx = i;
848 }
David Vincze39e78552018-10-10 17:10:01 +0200849 bs->idx = (found_idx / BOOT_STATUS_STATE_COUNT) + 1;
850 bs->state = (found_idx % BOOT_STATUS_STATE_COUNT) + 1;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000851 }
852
853 return 0;
854}
David Vinczedf12de02020-06-05 10:59:27 +0200855#endif /* !MCUBOOT_OVERWRITE_ONLY */
Tamas Banf70ef8c2017-12-19 15:35:09 +0000856
857/**
858 * Reads the boot status from the flash. The boot status contains
859 * the current state of an interrupted image copy operation. If the boot
860 * status is not present, or it indicates that previous copy finished,
861 * there is no operation in progress.
862 */
863static int
David Vinczecea8b592019-10-29 16:09:51 +0100864boot_read_status(struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000865{
David Vincze39e78552018-10-10 17:10:01 +0200866 memset(bs, 0, sizeof *bs);
867 bs->idx = BOOT_STATUS_IDX_0;
868 bs->state = BOOT_STATUS_STATE_0;
David Vincze401c7422019-06-21 20:44:05 +0200869 bs->swap_type = BOOT_SWAP_TYPE_NONE;
David Vincze39e78552018-10-10 17:10:01 +0200870
871#ifdef MCUBOOT_OVERWRITE_ONLY
872 /* Overwrite-only doesn't make use of the swap status area. */
873 return 0;
TTornblomfaf74f52020-03-04 17:56:27 +0100874#else
875 const struct flash_area *fap;
876 uint32_t off;
877 uint8_t swap_info;
878 int status_loc;
879 int area_id;
880 int rc;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000881
David Vinczecea8b592019-10-29 16:09:51 +0100882 status_loc = boot_status_source(state);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000883 switch (status_loc) {
884 case BOOT_STATUS_SOURCE_NONE:
885 return 0;
886
887 case BOOT_STATUS_SOURCE_SCRATCH:
888 area_id = FLASH_AREA_IMAGE_SCRATCH;
889 break;
890
David Vincze8bdfc2d2019-03-18 15:49:23 +0100891 case BOOT_STATUS_SOURCE_PRIMARY_SLOT:
David Vinczecea8b592019-10-29 16:09:51 +0100892 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000893 break;
894
895 default:
896 assert(0);
897 return BOOT_EBADARGS;
898 }
899
900 rc = flash_area_open(area_id, &fap);
901 if (rc != 0) {
902 return BOOT_EFLASH;
903 }
904
David Vinczecea8b592019-10-29 16:09:51 +0100905 rc = boot_read_status_bytes(fap, state, bs);
David Vincze401c7422019-06-21 20:44:05 +0200906 if (rc == 0) {
David Vincze91b71ef2019-06-24 13:06:47 +0200907 off = boot_swap_info_off(fap);
908 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
David Vincze401c7422019-06-21 20:44:05 +0200909 if (rc == 1) {
David Vincze91b71ef2019-06-24 13:06:47 +0200910 BOOT_SET_SWAP_INFO(swap_info, 0, BOOT_SWAP_TYPE_NONE);
David Vincze401c7422019-06-21 20:44:05 +0200911 rc = 0;
912 }
David Vincze91b71ef2019-06-24 13:06:47 +0200913
914 /* Extract the swap type info */
915 bs->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
David Vincze401c7422019-06-21 20:44:05 +0200916 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000917
918 flash_area_close(fap);
David Vincze39e78552018-10-10 17:10:01 +0200919
Tamas Banf70ef8c2017-12-19 15:35:09 +0000920 return rc;
TTornblomfaf74f52020-03-04 17:56:27 +0100921#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +0000922}
923
924/**
925 * Writes the supplied boot status to the flash file system. The boot status
926 * contains the current state of an in-progress image copy operation.
927 *
928 * @param bs The boot status to write.
929 *
930 * @return 0 on success; nonzero on failure.
931 */
932int
David Vinczecea8b592019-10-29 16:09:51 +0100933boot_write_status(struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000934{
Tamas Ban581034a2017-12-19 19:54:37 +0000935 const struct flash_area *fap = NULL;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000936 uint32_t off;
937 int area_id;
938 int rc;
939 uint8_t buf[BOOT_MAX_ALIGN];
Raef Coles204c5b42019-09-05 09:18:47 +0100940 uint32_t align;
David Vincze39e78552018-10-10 17:10:01 +0200941 uint8_t erased_val;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000942
943 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze8bdfc2d2019-03-18 15:49:23 +0100944 * the trailer. Since in the last step the primary slot is erased, the
945 * first two status writes go to the scratch which will be copied to
946 * the primary slot!
Tamas Banf70ef8c2017-12-19 15:35:09 +0000947 */
948
949 if (bs->use_scratch) {
950 /* Write to scratch. */
951 area_id = FLASH_AREA_IMAGE_SCRATCH;
952 } else {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100953 /* Write to the primary slot. */
David Vinczecea8b592019-10-29 16:09:51 +0100954 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000955 }
956
957 rc = flash_area_open(area_id, &fap);
958 if (rc != 0) {
959 rc = BOOT_EFLASH;
960 goto done;
961 }
962
963 off = boot_status_off(fap) +
David Vinczecea8b592019-10-29 16:09:51 +0100964 boot_status_internal_off(bs->idx, bs->state, BOOT_WRITE_SZ(state));
Tamas Banc3828852018-02-01 12:24:16 +0000965 align = flash_area_align(fap);
David Vincze39e78552018-10-10 17:10:01 +0200966 erased_val = flash_area_erased_val(fap);
967 memset(buf, erased_val, BOOT_MAX_ALIGN);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000968 buf[0] = bs->state;
969
970 rc = flash_area_write(fap, off, buf, align);
971 if (rc != 0) {
972 rc = BOOT_EFLASH;
973 goto done;
974 }
975
976 rc = 0;
977
978done:
979 flash_area_close(fap);
980 return rc;
981}
982
Tamas Banf70ef8c2017-12-19 15:35:09 +0000983/**
984 * Determines which swap operation to perform, if any. If it is determined
David Vincze8bdfc2d2019-03-18 15:49:23 +0100985 * that a swap operation is required, the image in the secondary slot is checked
986 * for validity. If the image in the secondary slot is invalid, it is erased,
987 * and a swap type of "none" is indicated.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000988 *
989 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
990 */
991static int
David Vinczecea8b592019-10-29 16:09:51 +0100992boot_validated_swap_type(struct boot_loader_state *state,
993 struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000994{
995 int swap_type;
David Vinczecea8b592019-10-29 16:09:51 +0100996 int rc;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000997
David Vinczecea8b592019-10-29 16:09:51 +0100998 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
999 if (BOOT_IS_UPGRADE(swap_type)) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001000 /* Boot loader wants to switch to the secondary slot.
1001 * Ensure image is valid.
1002 */
David Vinczecea8b592019-10-29 16:09:51 +01001003 rc = boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs);
1004 if (rc == 1) {
1005 swap_type = BOOT_SWAP_TYPE_NONE;
1006 } else if (rc != 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001007 swap_type = BOOT_SWAP_TYPE_FAIL;
1008 }
1009 }
1010
1011 return swap_type;
1012}
1013
1014/**
1015 * Calculates the number of sectors the scratch area can contain. A "last"
1016 * source sector is specified because images are copied backwards in flash
1017 * (final index to index number 0).
1018 *
1019 * @param last_sector_idx The index of the last source sector
1020 * (inclusive).
1021 * @param out_first_sector_idx The index of the first source sector
1022 * (inclusive) gets written here.
1023 *
1024 * @return The number of bytes comprised by the
1025 * [first-sector, last-sector] range.
1026 */
1027#ifndef MCUBOOT_OVERWRITE_ONLY
1028static uint32_t
David Vinczecea8b592019-10-29 16:09:51 +01001029boot_copy_sz(struct boot_loader_state *state, int last_sector_idx,
1030 int *out_first_sector_idx)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001031{
1032 size_t scratch_sz;
1033 uint32_t new_sz;
1034 uint32_t sz;
1035 int i;
1036
1037 sz = 0;
1038
David Vinczecea8b592019-10-29 16:09:51 +01001039 scratch_sz = boot_scratch_area_size(state);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001040 for (i = last_sector_idx; i >= 0; i--) {
David Vinczecea8b592019-10-29 16:09:51 +01001041 new_sz = sz + boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
David Vincze401c7422019-06-21 20:44:05 +02001042 /*
1043 * The secondary slot is not being checked here, because
1044 * `boot_slots_compatible` already provides assurance that the copy size
1045 * will be compatible with the primary slot and scratch.
1046 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001047 if (new_sz > scratch_sz) {
1048 break;
1049 }
1050 sz = new_sz;
1051 }
1052
1053 /* i currently refers to a sector that doesn't fit or it is -1 because all
1054 * sectors have been processed. In both cases, exclude sector i.
1055 */
1056 *out_first_sector_idx = i + 1;
1057 return sz;
1058}
1059#endif /* !MCUBOOT_OVERWRITE_ONLY */
1060
1061/**
David Vinczef7641fa2018-09-04 18:29:46 +02001062 * Erases a region of flash.
1063 *
David Vincze401c7422019-06-21 20:44:05 +02001064 * @param flash_area The flash_area containing the region to erase.
David Vinczef7641fa2018-09-04 18:29:46 +02001065 * @param off The offset within the flash area to start the
1066 * erase.
1067 * @param sz The number of bytes to erase.
1068 *
1069 * @return 0 on success; nonzero on failure.
1070 */
David Vincze401c7422019-06-21 20:44:05 +02001071static inline int
David Vinczecea8b592019-10-29 16:09:51 +01001072boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
David Vinczef7641fa2018-09-04 18:29:46 +02001073{
David Vincze401c7422019-06-21 20:44:05 +02001074 return flash_area_erase(fap, off, sz);
David Vinczef7641fa2018-09-04 18:29:46 +02001075}
1076
1077/**
Tamas Banf70ef8c2017-12-19 15:35:09 +00001078 * Copies the contents of one flash region to another. You must erase the
1079 * destination region prior to calling this function.
1080 *
1081 * @param flash_area_id_src The ID of the source flash area.
1082 * @param flash_area_id_dst The ID of the destination flash area.
1083 * @param off_src The offset within the source flash area to
1084 * copy from.
1085 * @param off_dst The offset within the destination flash area to
1086 * copy to.
1087 * @param sz The number of bytes to copy.
1088 *
1089 * @return 0 on success; nonzero on failure.
1090 */
1091static int
David Vinczecea8b592019-10-29 16:09:51 +01001092boot_copy_region(struct boot_loader_state *state,
1093 const struct flash_area *fap_src,
David Vincze401c7422019-06-21 20:44:05 +02001094 const struct flash_area *fap_dst,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001095 uint32_t off_src, uint32_t off_dst, uint32_t sz)
1096{
Tamas Banf70ef8c2017-12-19 15:35:09 +00001097 uint32_t bytes_copied;
1098 int chunk_sz;
1099 int rc;
1100
1101 static uint8_t buf[1024];
1102
David Vinczecea8b592019-10-29 16:09:51 +01001103 (void)state;
1104
Tamas Banf70ef8c2017-12-19 15:35:09 +00001105 bytes_copied = 0;
1106 while (bytes_copied < sz) {
Tamas Ban581034a2017-12-19 19:54:37 +00001107 if (sz - bytes_copied > sizeof(buf)) {
1108 chunk_sz = sizeof(buf);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001109 } else {
1110 chunk_sz = sz - bytes_copied;
1111 }
1112
1113 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
1114 if (rc != 0) {
David Vincze401c7422019-06-21 20:44:05 +02001115 return BOOT_EFLASH;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001116 }
1117
1118 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
1119 if (rc != 0) {
David Vincze401c7422019-06-21 20:44:05 +02001120 return BOOT_EFLASH;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001121 }
1122
1123 bytes_copied += chunk_sz;
1124 }
1125
David Vincze401c7422019-06-21 20:44:05 +02001126 return 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001127}
1128
1129#ifndef MCUBOOT_OVERWRITE_ONLY
1130static inline int
David Vinczecea8b592019-10-29 16:09:51 +01001131boot_status_init(const struct boot_loader_state *state,
1132 const struct flash_area *fap,
1133 const struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001134{
Tamas Banf70ef8c2017-12-19 15:35:09 +00001135 struct boot_swap_state swap_state;
David Vinczecea8b592019-10-29 16:09:51 +01001136 uint8_t image_index;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001137 int rc;
1138
David Vinczecea8b592019-10-29 16:09:51 +01001139#if (BOOT_IMAGE_NUMBER == 1)
1140 (void)state;
1141#endif
1142
1143 image_index = BOOT_CURR_IMG(state);
1144
David Vincze401c7422019-06-21 20:44:05 +02001145 BOOT_LOG_DBG("initializing status; fa_id=%d", fap->fa_id);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001146
David Vinczecea8b592019-10-29 16:09:51 +01001147 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
1148 &swap_state);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001149 assert(rc == 0);
1150
David Vincze401c7422019-06-21 20:44:05 +02001151 if (bs->swap_type != BOOT_SWAP_TYPE_NONE) {
David Vinczecea8b592019-10-29 16:09:51 +01001152 rc = boot_write_swap_info(fap, bs->swap_type, image_index);
David Vincze401c7422019-06-21 20:44:05 +02001153 assert(rc == 0);
1154 }
1155
Tamas Banf70ef8c2017-12-19 15:35:09 +00001156 if (swap_state.image_ok == BOOT_FLAG_SET) {
1157 rc = boot_write_image_ok(fap);
1158 assert(rc == 0);
1159 }
1160
1161 rc = boot_write_swap_size(fap, bs->swap_size);
1162 assert(rc == 0);
1163
1164 rc = boot_write_magic(fap);
1165 assert(rc == 0);
1166
Tamas Banf70ef8c2017-12-19 15:35:09 +00001167 return 0;
1168}
David Vinczef7641fa2018-09-04 18:29:46 +02001169
1170static int
David Vinczecea8b592019-10-29 16:09:51 +01001171boot_erase_trailer_sectors(const struct boot_loader_state *state,
1172 const struct flash_area *fap)
David Vinczef7641fa2018-09-04 18:29:46 +02001173{
1174 uint8_t slot;
David Vincze401c7422019-06-21 20:44:05 +02001175 uint32_t sector;
1176 uint32_t trailer_sz;
1177 uint32_t total_sz;
1178 uint32_t off;
1179 uint32_t sz;
David Vinczebb207982019-08-21 15:13:04 +02001180 int fa_id_primary;
1181 int fa_id_secondary;
David Vinczecea8b592019-10-29 16:09:51 +01001182 uint8_t image_index;
David Vinczef7641fa2018-09-04 18:29:46 +02001183 int rc;
1184
David Vincze401c7422019-06-21 20:44:05 +02001185 BOOT_LOG_DBG("erasing trailer; fa_id=%d", fap->fa_id);
1186
David Vinczecea8b592019-10-29 16:09:51 +01001187 image_index = BOOT_CURR_IMG(state);
1188 fa_id_primary = flash_area_id_from_multi_image_slot(image_index,
1189 BOOT_PRIMARY_SLOT);
1190 fa_id_secondary = flash_area_id_from_multi_image_slot(image_index,
1191 BOOT_SECONDARY_SLOT);
David Vinczebb207982019-08-21 15:13:04 +02001192
1193 if (fap->fa_id == fa_id_primary) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001194 slot = BOOT_PRIMARY_SLOT;
David Vinczebb207982019-08-21 15:13:04 +02001195 } else if (fap->fa_id == fa_id_secondary) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001196 slot = BOOT_SECONDARY_SLOT;
David Vinczebb207982019-08-21 15:13:04 +02001197 } else {
David Vinczef7641fa2018-09-04 18:29:46 +02001198 return BOOT_EFLASH;
1199 }
1200
David Vincze401c7422019-06-21 20:44:05 +02001201 /* delete starting from last sector and moving to beginning */
David Vinczecea8b592019-10-29 16:09:51 +01001202 sector = boot_img_num_sectors(state, slot) - 1;
1203 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
David Vincze401c7422019-06-21 20:44:05 +02001204 total_sz = 0;
1205 do {
David Vinczecea8b592019-10-29 16:09:51 +01001206 sz = boot_img_sector_size(state, slot, sector);
1207 off = boot_img_sector_off(state, slot, sector);
1208 rc = boot_erase_region(fap, off, sz);
David Vincze401c7422019-06-21 20:44:05 +02001209 assert(rc == 0);
1210
1211 sector--;
1212 total_sz += sz;
1213 } while (total_sz < trailer_sz);
David Vinczef7641fa2018-09-04 18:29:46 +02001214
1215 return rc;
1216}
Tamas Banf70ef8c2017-12-19 15:35:09 +00001217
Tamas Banf70ef8c2017-12-19 15:35:09 +00001218/**
1219 * Swaps the contents of two flash regions within the two image slots.
1220 *
1221 * @param idx The index of the first sector in the range of
1222 * sectors being swapped.
1223 * @param sz The number of bytes to swap.
1224 * @param bs The current boot status. This struct gets
1225 * updated according to the outcome.
1226 *
1227 * @return 0 on success; nonzero on failure.
1228 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001229static void
David Vinczecea8b592019-10-29 16:09:51 +01001230boot_swap_sectors(int idx, uint32_t sz, struct boot_loader_state *state,
1231 struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001232{
David Vincze401c7422019-06-21 20:44:05 +02001233 const struct flash_area *fap_primary_slot;
1234 const struct flash_area *fap_secondary_slot;
1235 const struct flash_area *fap_scratch;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001236 uint32_t copy_sz;
1237 uint32_t trailer_sz;
1238 uint32_t img_off;
1239 uint32_t scratch_trailer_off;
1240 struct boot_swap_state swap_state;
1241 size_t last_sector;
David Vincze401c7422019-06-21 20:44:05 +02001242 bool erase_scratch;
David Vinczecea8b592019-10-29 16:09:51 +01001243 uint8_t image_index;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001244 int rc;
1245
1246 /* Calculate offset from start of image area. */
David Vinczecea8b592019-10-29 16:09:51 +01001247 img_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001248
1249 copy_sz = sz;
David Vinczecea8b592019-10-29 16:09:51 +01001250 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
Tamas Banf70ef8c2017-12-19 15:35:09 +00001251
David Vincze401c7422019-06-21 20:44:05 +02001252 /* sz in this function is always sized on a multiple of the sector size.
1253 * The check against the start offset of the last sector
Tamas Banf70ef8c2017-12-19 15:35:09 +00001254 * is to determine if we're swapping the last sector. The last sector
1255 * needs special handling because it's where the trailer lives. If we're
1256 * copying it, we need to use scratch to write the trailer temporarily.
1257 *
1258 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
1259 * controls if special handling is needed (swapping last sector).
1260 */
David Vinczecea8b592019-10-29 16:09:51 +01001261 last_sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
David Vincze7384ee72019-07-23 17:00:42 +02001262 if ((img_off + sz) >
David Vinczecea8b592019-10-29 16:09:51 +01001263 boot_img_sector_off(state, BOOT_PRIMARY_SLOT, last_sector)) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001264 copy_sz -= trailer_sz;
1265 }
1266
David Vincze39e78552018-10-10 17:10:01 +02001267 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001268
David Vinczecea8b592019-10-29 16:09:51 +01001269 image_index = BOOT_CURR_IMG(state);
1270
1271 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1272 &fap_primary_slot);
David Vincze401c7422019-06-21 20:44:05 +02001273 assert (rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001274
David Vinczecea8b592019-10-29 16:09:51 +01001275 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1276 &fap_secondary_slot);
David Vincze401c7422019-06-21 20:44:05 +02001277 assert (rc == 0);
1278
1279 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch);
1280 assert (rc == 0);
1281
1282 if (bs->state == BOOT_STATUS_STATE_0) {
1283 BOOT_LOG_DBG("erasing scratch area");
David Vinczecea8b592019-10-29 16:09:51 +01001284 rc = boot_erase_region(fap_scratch, 0, fap_scratch->fa_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001285 assert(rc == 0);
1286
David Vincze39e78552018-10-10 17:10:01 +02001287 if (bs->idx == BOOT_STATUS_IDX_0) {
David Vincze401c7422019-06-21 20:44:05 +02001288 /* Write a trailer to the scratch area, even if we don't need the
1289 * scratch area for status. We need a temporary place to store the
1290 * `swap-type` while we erase the primary trailer.
1291 */
David Vinczecea8b592019-10-29 16:09:51 +01001292 rc = boot_status_init(state, fap_scratch, bs);
David Vincze401c7422019-06-21 20:44:05 +02001293 assert(rc == 0);
1294
1295 if (!bs->use_scratch) {
1296 /* Prepare the primary status area... here it is known that the
1297 * last sector is not being used by the image data so it's safe
1298 * to erase.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001299 */
David Vinczecea8b592019-10-29 16:09:51 +01001300 rc = boot_erase_trailer_sectors(state, fap_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001301 assert(rc == 0);
1302
David Vinczecea8b592019-10-29 16:09:51 +01001303 rc = boot_status_init(state, fap_primary_slot, bs);
David Vincze401c7422019-06-21 20:44:05 +02001304 assert(rc == 0);
1305
1306 /* Erase the temporary trailer from the scratch area. */
David Vinczecea8b592019-10-29 16:09:51 +01001307 rc = boot_erase_region(fap_scratch, 0, fap_scratch->fa_size);
David Vincze401c7422019-06-21 20:44:05 +02001308 assert(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001309 }
1310 }
1311
David Vinczecea8b592019-10-29 16:09:51 +01001312 rc = boot_copy_region(state, fap_secondary_slot, fap_scratch,
David Vincze401c7422019-06-21 20:44:05 +02001313 img_off, 0, copy_sz);
1314 assert(rc == 0);
1315
David Vinczecea8b592019-10-29 16:09:51 +01001316 rc = boot_write_status(state, bs);
David Vincze39e78552018-10-10 17:10:01 +02001317 bs->state = BOOT_STATUS_STATE_1;
David Vincze39e78552018-10-10 17:10:01 +02001318 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001319 }
1320
David Vincze39e78552018-10-10 17:10:01 +02001321 if (bs->state == BOOT_STATUS_STATE_1) {
David Vinczecea8b592019-10-29 16:09:51 +01001322 rc = boot_erase_region(fap_secondary_slot, img_off, sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001323 assert(rc == 0);
1324
David Vinczecea8b592019-10-29 16:09:51 +01001325 rc = boot_copy_region(state, fap_primary_slot, fap_secondary_slot,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001326 img_off, img_off, copy_sz);
1327 assert(rc == 0);
1328
David Vincze39e78552018-10-10 17:10:01 +02001329 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001330 /* If not all sectors of the slot are being swapped,
David Vincze8bdfc2d2019-03-18 15:49:23 +01001331 * guarantee here that only the primary slot will have the state.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001332 */
David Vinczecea8b592019-10-29 16:09:51 +01001333 rc = boot_erase_trailer_sectors(state, fap_secondary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001334 assert(rc == 0);
1335 }
1336
David Vinczecea8b592019-10-29 16:09:51 +01001337 rc = boot_write_status(state, bs);
David Vincze39e78552018-10-10 17:10:01 +02001338 bs->state = BOOT_STATUS_STATE_2;
David Vincze39e78552018-10-10 17:10:01 +02001339 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001340 }
1341
David Vincze39e78552018-10-10 17:10:01 +02001342 if (bs->state == BOOT_STATUS_STATE_2) {
David Vinczecea8b592019-10-29 16:09:51 +01001343 rc = boot_erase_region(fap_primary_slot, img_off, sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001344 assert(rc == 0);
1345
David Vincze401c7422019-06-21 20:44:05 +02001346 /* NOTE: If this is the final sector, we exclude the image trailer from
1347 * this copy (copy_sz was truncated earlier).
1348 */
David Vinczecea8b592019-10-29 16:09:51 +01001349 rc = boot_copy_region(state, fap_scratch, fap_primary_slot,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001350 0, img_off, copy_sz);
1351 assert(rc == 0);
1352
1353 if (bs->use_scratch) {
David Vincze401c7422019-06-21 20:44:05 +02001354 scratch_trailer_off = boot_status_off(fap_scratch);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001355
1356 /* copy current status that is being maintained in scratch */
David Vinczecea8b592019-10-29 16:09:51 +01001357 rc = boot_copy_region(state, fap_scratch, fap_primary_slot,
David Vincze401c7422019-06-21 20:44:05 +02001358 scratch_trailer_off, img_off + copy_sz,
David Vinczecea8b592019-10-29 16:09:51 +01001359 (BOOT_STATUS_STATE_COUNT - 1) * BOOT_WRITE_SZ(state));
David Vincze39e78552018-10-10 17:10:01 +02001360 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001361
1362 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1363 &swap_state);
1364 assert(rc == 0);
1365
1366 if (swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze401c7422019-06-21 20:44:05 +02001367 rc = boot_write_image_ok(fap_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001368 assert(rc == 0);
1369 }
1370
David Vincze401c7422019-06-21 20:44:05 +02001371 if (swap_state.swap_type != BOOT_SWAP_TYPE_NONE) {
David Vincze91b71ef2019-06-24 13:06:47 +02001372 rc = boot_write_swap_info(fap_primary_slot,
1373 swap_state.swap_type,
David Vinczecea8b592019-10-29 16:09:51 +01001374 image_index);
David Vincze401c7422019-06-21 20:44:05 +02001375 assert(rc == 0);
1376 }
1377
1378 rc = boot_write_swap_size(fap_primary_slot, bs->swap_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001379 assert(rc == 0);
1380
David Vincze401c7422019-06-21 20:44:05 +02001381 rc = boot_write_magic(fap_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001382 assert(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001383 }
1384
David Vincze401c7422019-06-21 20:44:05 +02001385 /* If we wrote a trailer to the scratch area, erase it after we persist
1386 * a trailer to the primary slot. We do this to prevent mcuboot from
1387 * reading a stale status from the scratch area in case of immediate
1388 * reset.
1389 */
1390 erase_scratch = bs->use_scratch;
1391 bs->use_scratch = 0;
1392
David Vinczecea8b592019-10-29 16:09:51 +01001393 rc = boot_write_status(state, bs);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001394 bs->idx++;
David Vincze39e78552018-10-10 17:10:01 +02001395 bs->state = BOOT_STATUS_STATE_0;
David Vincze39e78552018-10-10 17:10:01 +02001396 BOOT_STATUS_ASSERT(rc == 0);
David Vincze401c7422019-06-21 20:44:05 +02001397
1398 if (erase_scratch) {
David Vinczecea8b592019-10-29 16:09:51 +01001399 rc = boot_erase_region(fap_scratch, 0, sz);
David Vincze401c7422019-06-21 20:44:05 +02001400 assert(rc == 0);
1401 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001402 }
David Vincze401c7422019-06-21 20:44:05 +02001403
1404 flash_area_close(fap_primary_slot);
1405 flash_area_close(fap_secondary_slot);
1406 flash_area_close(fap_scratch);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001407}
1408#endif /* !MCUBOOT_OVERWRITE_ONLY */
1409
1410/**
David Vincze401c7422019-06-21 20:44:05 +02001411 * Overwrite primary slot with the image contained in the secondary slot.
1412 * If a prior copy operation was interrupted by a system reset, this function
1413 * redos the copy.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001414 *
1415 * @param bs The current boot status. This function reads
1416 * this struct to determine if it is resuming
1417 * an interrupted swap operation. This
1418 * function writes the updated status to this
1419 * function on return.
1420 *
1421 * @return 0 on success; nonzero on failure.
1422 */
1423#ifdef MCUBOOT_OVERWRITE_ONLY
1424static int
David Vinczecea8b592019-10-29 16:09:51 +01001425boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001426{
1427 size_t sect_count;
1428 size_t sect;
1429 int rc;
David Vinczecea8b592019-10-29 16:09:51 +01001430 size_t size;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001431 size_t this_size;
David Vincze39e78552018-10-10 17:10:01 +02001432 size_t last_sector;
David Vincze401c7422019-06-21 20:44:05 +02001433 const struct flash_area *fap_primary_slot;
1434 const struct flash_area *fap_secondary_slot;
David Vinczecea8b592019-10-29 16:09:51 +01001435 uint8_t image_index;
David Vincze39e78552018-10-10 17:10:01 +02001436
1437 (void)bs;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001438
David Vincze8bdfc2d2019-03-18 15:49:23 +01001439 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1440 BOOT_LOG_INF("Erasing the primary slot");
Tamas Banf70ef8c2017-12-19 15:35:09 +00001441
David Vinczecea8b592019-10-29 16:09:51 +01001442 image_index = BOOT_CURR_IMG(state);
1443
1444 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1445 &fap_primary_slot);
David Vincze401c7422019-06-21 20:44:05 +02001446 assert (rc == 0);
1447
David Vinczecea8b592019-10-29 16:09:51 +01001448 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1449 &fap_secondary_slot);
David Vincze401c7422019-06-21 20:44:05 +02001450 assert (rc == 0);
1451
David Vinczecea8b592019-10-29 16:09:51 +01001452 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
1453 for (sect = 0, size = 0; sect < sect_count; sect++) {
1454 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
1455 rc = boot_erase_region(fap_primary_slot, size, this_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001456 assert(rc == 0);
1457
1458 size += this_size;
1459 }
1460
David Vincze8bdfc2d2019-03-18 15:49:23 +01001461 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1462 size);
David Vinczecea8b592019-10-29 16:09:51 +01001463 rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot,
1464 0, 0, size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001465
David Vincze060968d2019-05-23 01:13:14 +02001466 /* Update the stored security counter with the new image's security counter
David Vincze8bdfc2d2019-03-18 15:49:23 +01001467 * value. Both slots hold the new image at this point, but the secondary
1468 * slot's image header must be passed because the read image headers in the
1469 * boot_data structure have not been updated yet.
David Vincze060968d2019-05-23 01:13:14 +02001470 */
David Vinczecea8b592019-10-29 16:09:51 +01001471 rc = boot_update_security_counter(BOOT_CURR_IMG(state), BOOT_PRIMARY_SLOT,
1472 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
David Vincze060968d2019-05-23 01:13:14 +02001473 if (rc != 0) {
1474 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
1475 return rc;
1476 }
1477
David Vincze39e78552018-10-10 17:10:01 +02001478 /*
1479 * Erases header and trailer. The trailer is erased because when a new
1480 * image is written without a trailer as is the case when using newt, the
1481 * trailer that was left might trigger a new upgrade.
1482 */
David Vincze401c7422019-06-21 20:44:05 +02001483 BOOT_LOG_DBG("erasing secondary header");
David Vinczecea8b592019-10-29 16:09:51 +01001484 rc = boot_erase_region(fap_secondary_slot,
1485 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
1486 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
Tamas Banf70ef8c2017-12-19 15:35:09 +00001487 assert(rc == 0);
David Vinczecea8b592019-10-29 16:09:51 +01001488 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
David Vincze401c7422019-06-21 20:44:05 +02001489 BOOT_LOG_DBG("erasing secondary trailer");
David Vinczecea8b592019-10-29 16:09:51 +01001490 rc = boot_erase_region(fap_secondary_slot,
1491 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
1492 last_sector),
1493 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
1494 last_sector));
David Vincze39e78552018-10-10 17:10:01 +02001495 assert(rc == 0);
1496
David Vincze401c7422019-06-21 20:44:05 +02001497 flash_area_close(fap_primary_slot);
1498 flash_area_close(fap_secondary_slot);
1499
David Vincze8bdfc2d2019-03-18 15:49:23 +01001500 /* TODO: Perhaps verify the primary slot's signature again? */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001501
1502 return 0;
1503}
1504#else
David Vincze401c7422019-06-21 20:44:05 +02001505/**
1506 * Swaps the two images in flash. If a prior copy operation was interrupted
1507 * by a system reset, this function completes that operation.
1508 *
1509 * @param bs The current boot status. This function reads
1510 * this struct to determine if it is resuming
1511 * an interrupted swap operation. This
1512 * function writes the updated status to this
1513 * function on return.
1514 *
1515 * @return 0 on success; nonzero on failure.
1516 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001517static int
David Vinczecea8b592019-10-29 16:09:51 +01001518boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001519{
1520 uint32_t sz;
1521 int first_sector_idx;
1522 int last_sector_idx;
David Vincze401c7422019-06-21 20:44:05 +02001523 int last_idx_secondary_slot;
David Vincze39e78552018-10-10 17:10:01 +02001524 uint32_t swap_idx;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001525 struct image_header *hdr;
1526 uint32_t size;
1527 uint32_t copy_size;
David Vincze401c7422019-06-21 20:44:05 +02001528 uint32_t primary_slot_size;
1529 uint32_t secondary_slot_size;
David Vinczecea8b592019-10-29 16:09:51 +01001530 uint8_t image_index;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001531 int rc;
1532
1533 /* FIXME: just do this if asked by user? */
1534
1535 size = copy_size = 0;
David Vinczecea8b592019-10-29 16:09:51 +01001536 image_index = BOOT_CURR_IMG(state);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001537
David Vincze39e78552018-10-10 17:10:01 +02001538 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001539 /*
1540 * No swap ever happened, so need to find the largest image which
1541 * will be used to determine the amount of sectors to swap.
1542 */
David Vinczecea8b592019-10-29 16:09:51 +01001543 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
1544 if (hdr->ih_magic == IMAGE_MAGIC) {
1545 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
1546 assert(rc == 0);
1547 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001548
David Vinczecea8b592019-10-29 16:09:51 +01001549 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
1550 if (hdr->ih_magic == IMAGE_MAGIC) {
1551 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
1552 assert(rc == 0);
1553 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001554
1555 if (size > copy_size) {
1556 copy_size = size;
1557 }
1558
1559 bs->swap_size = copy_size;
1560 } else {
1561 /*
1562 * If a swap was under way, the swap_size should already be present
1563 * in the trailer...
1564 */
David Vinczecea8b592019-10-29 16:09:51 +01001565 rc = boot_read_swap_size(image_index, &bs->swap_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001566 assert(rc == 0);
1567
1568 copy_size = bs->swap_size;
1569 }
1570
David Vincze401c7422019-06-21 20:44:05 +02001571 primary_slot_size = 0;
1572 secondary_slot_size = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001573 last_sector_idx = 0;
David Vincze401c7422019-06-21 20:44:05 +02001574 last_idx_secondary_slot = 0;
1575
1576 /*
1577 * Knowing the size of the largest image between both slots, here we
1578 * find what is the last sector in the primary slot that needs swapping.
1579 * Since we already know that both slots are compatible, the secondary
1580 * slot's last sector is not really required after this check is finished.
1581 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001582 while (1) {
David Vincze401c7422019-06-21 20:44:05 +02001583 if ((primary_slot_size < copy_size) ||
1584 (primary_slot_size < secondary_slot_size)) {
David Vinczecea8b592019-10-29 16:09:51 +01001585 primary_slot_size += boot_img_sector_size(state,
David Vincze401c7422019-06-21 20:44:05 +02001586 BOOT_PRIMARY_SLOT,
1587 last_sector_idx);
1588 }
1589 if ((secondary_slot_size < copy_size) ||
1590 (secondary_slot_size < primary_slot_size)) {
David Vinczecea8b592019-10-29 16:09:51 +01001591 secondary_slot_size += boot_img_sector_size(state,
David Vincze401c7422019-06-21 20:44:05 +02001592 BOOT_SECONDARY_SLOT,
1593 last_idx_secondary_slot);
1594 }
1595 if (primary_slot_size >= copy_size &&
1596 secondary_slot_size >= copy_size &&
1597 primary_slot_size == secondary_slot_size) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001598 break;
1599 }
1600 last_sector_idx++;
David Vincze401c7422019-06-21 20:44:05 +02001601 last_idx_secondary_slot++;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001602 }
1603
1604 swap_idx = 0;
1605 while (last_sector_idx >= 0) {
David Vinczecea8b592019-10-29 16:09:51 +01001606 sz = boot_copy_sz(state, last_sector_idx, &first_sector_idx);
David Vincze39e78552018-10-10 17:10:01 +02001607 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
David Vinczecea8b592019-10-29 16:09:51 +01001608 boot_swap_sectors(first_sector_idx, sz, state, bs);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001609 }
1610
1611 last_sector_idx = first_sector_idx - 1;
1612 swap_idx++;
1613 }
1614
David Vincze8bdfc2d2019-03-18 15:49:23 +01001615#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
David Vincze39e78552018-10-10 17:10:01 +02001616 if (boot_status_fails > 0) {
David Vincze401c7422019-06-21 20:44:05 +02001617 BOOT_LOG_WRN("%d status write fails performing the swap",
1618 boot_status_fails);
David Vincze39e78552018-10-10 17:10:01 +02001619 }
1620#endif
1621
Tamas Banf70ef8c2017-12-19 15:35:09 +00001622 return 0;
1623}
1624#endif
1625
David Vincze44b79f42019-10-25 15:39:59 +02001626#ifndef MCUBOOT_OVERWRITE_ONLY
Tamas Banf70ef8c2017-12-19 15:35:09 +00001627/**
David Vincze8bdfc2d2019-03-18 15:49:23 +01001628 * Marks the image in the primary slot as fully copied.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001629 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001630static int
David Vinczecea8b592019-10-29 16:09:51 +01001631boot_set_copy_done(uint8_t image_index)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001632{
1633 const struct flash_area *fap;
1634 int rc;
1635
David Vinczecea8b592019-10-29 16:09:51 +01001636 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1637 &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001638 if (rc != 0) {
1639 return BOOT_EFLASH;
1640 }
1641
1642 rc = boot_write_copy_done(fap);
1643 flash_area_close(fap);
1644 return rc;
1645}
Tamas Banf70ef8c2017-12-19 15:35:09 +00001646
1647/**
David Vincze8bdfc2d2019-03-18 15:49:23 +01001648 * Marks a reverted image in the primary slot as confirmed. This is necessary to
1649 * ensure the status bytes from the image revert operation don't get processed
1650 * on a subsequent boot.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001651 *
1652 * NOTE: image_ok is tested before writing because if there's a valid permanent
David Vincze8bdfc2d2019-03-18 15:49:23 +01001653 * image installed on the primary slot and the new image to be upgrade to has a
1654 * bad sig, image_ok would be overwritten.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001655 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001656static int
David Vinczecea8b592019-10-29 16:09:51 +01001657boot_set_image_ok(uint8_t image_index)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001658{
1659 const struct flash_area *fap;
1660 struct boot_swap_state state;
1661 int rc;
1662
David Vinczecea8b592019-10-29 16:09:51 +01001663 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1664 &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001665 if (rc != 0) {
1666 return BOOT_EFLASH;
1667 }
1668
1669 rc = boot_read_swap_state(fap, &state);
1670 if (rc != 0) {
1671 rc = BOOT_EFLASH;
1672 goto out;
1673 }
1674
1675 if (state.image_ok == BOOT_FLAG_UNSET) {
1676 rc = boot_write_image_ok(fap);
1677 }
1678
1679out:
1680 flash_area_close(fap);
1681 return rc;
1682}
1683#endif /* !MCUBOOT_OVERWRITE_ONLY */
1684
David Vinczebb6e3b62019-07-31 14:24:05 +02001685#if (BOOT_IMAGE_NUMBER > 1)
1686/**
David Vinczecea8b592019-10-29 16:09:51 +01001687 * Check if the version of the image is not older than required.
1688 *
1689 * @param req Required minimal image version.
1690 * @param ver Version of the image to be checked.
1691 *
1692 * @return 0 if the version is sufficient, nonzero otherwise.
1693 */
1694static int
1695boot_is_version_sufficient(struct image_version *req,
1696 struct image_version *ver)
1697{
1698 if (ver->iv_major > req->iv_major) {
1699 return 0;
1700 }
1701 if (ver->iv_major < req->iv_major) {
1702 return BOOT_EBADVERSION;
1703 }
1704 /* The major version numbers are equal. */
1705 if (ver->iv_minor > req->iv_minor) {
1706 return 0;
1707 }
1708 if (ver->iv_minor < req->iv_minor) {
1709 return BOOT_EBADVERSION;
1710 }
1711 /* The minor version numbers are equal. */
1712 if (ver->iv_revision < req->iv_revision) {
1713 return BOOT_EBADVERSION;
1714 }
1715
1716 return 0;
1717}
1718
1719/**
David Vinczebb6e3b62019-07-31 14:24:05 +02001720 * Check the image dependency whether it is satisfied and modify
1721 * the swap type if necessary.
1722 *
1723 * @param dep Image dependency which has to be verified.
1724 *
1725 * @return 0 on success; nonzero on failure.
1726 */
1727static int
David Vinczecea8b592019-10-29 16:09:51 +01001728boot_verify_slot_dependency(struct boot_loader_state *state,
1729 struct image_dependency *dep)
David Vinczebb6e3b62019-07-31 14:24:05 +02001730{
1731 struct image_version *dep_version;
1732 size_t dep_slot;
1733 int rc;
David Vinczecea8b592019-10-29 16:09:51 +01001734 uint8_t swap_type;
David Vinczebb6e3b62019-07-31 14:24:05 +02001735
1736 /* Determine the source of the image which is the subject of
1737 * the dependency and get it's version. */
David Vinczecea8b592019-10-29 16:09:51 +01001738 swap_type = state->swap_type[dep->image_id];
1739 dep_slot = (swap_type != BOOT_SWAP_TYPE_NONE) ?
David Vinczebb6e3b62019-07-31 14:24:05 +02001740 BOOT_SECONDARY_SLOT : BOOT_PRIMARY_SLOT;
David Vinczecea8b592019-10-29 16:09:51 +01001741 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczebb6e3b62019-07-31 14:24:05 +02001742
1743 rc = boot_is_version_sufficient(&dep->image_min_version, dep_version);
1744 if (rc != 0) {
1745 /* Dependency not satisfied.
1746 * Modify the swap type to decrease the version number of the image
1747 * (which will be located in the primary slot after the boot process),
1748 * consequently the number of unsatisfied dependencies will be
1749 * decreased or remain the same.
1750 */
David Vinczecea8b592019-10-29 16:09:51 +01001751 switch (BOOT_SWAP_TYPE(state)) {
David Vinczebb6e3b62019-07-31 14:24:05 +02001752 case BOOT_SWAP_TYPE_TEST:
1753 case BOOT_SWAP_TYPE_PERM:
David Vinczecea8b592019-10-29 16:09:51 +01001754 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczebb6e3b62019-07-31 14:24:05 +02001755 break;
1756 case BOOT_SWAP_TYPE_NONE:
David Vinczecea8b592019-10-29 16:09:51 +01001757 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczebb6e3b62019-07-31 14:24:05 +02001758 break;
1759 default:
1760 break;
1761 }
1762 }
1763
1764 return rc;
1765}
1766
1767/**
1768 * Read all dependency TLVs of an image from the flash and verify
1769 * one after another to see if they are all satisfied.
1770 *
1771 * @param slot Image slot number.
1772 *
1773 * @return 0 on success; nonzero on failure.
1774 */
1775static int
David Vinczecea8b592019-10-29 16:09:51 +01001776boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
David Vinczebb6e3b62019-07-31 14:24:05 +02001777{
1778 const struct flash_area *fap;
David Vincze07706a42019-12-12 18:20:12 +01001779 struct image_tlv_iter it;
David Vinczebb6e3b62019-07-31 14:24:05 +02001780 struct image_dependency dep;
1781 uint32_t off;
David Vincze07706a42019-12-12 18:20:12 +01001782 uint16_t len;
David Vinczecea8b592019-10-29 16:09:51 +01001783 int area_id;
David Vinczebb6e3b62019-07-31 14:24:05 +02001784 int rc;
1785
David Vinczecea8b592019-10-29 16:09:51 +01001786 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
1787 rc = flash_area_open(area_id, &fap);
David Vinczebb6e3b62019-07-31 14:24:05 +02001788 if (rc != 0) {
1789 rc = BOOT_EFLASH;
1790 goto done;
1791 }
1792
David Vincze07706a42019-12-12 18:20:12 +01001793 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
1794 IMAGE_TLV_DEPENDENCY, true);
David Vinczebb6e3b62019-07-31 14:24:05 +02001795 if (rc != 0) {
David Vinczebb6e3b62019-07-31 14:24:05 +02001796 goto done;
1797 }
1798
David Vincze07706a42019-12-12 18:20:12 +01001799 while (true) {
1800 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
1801 if (rc < 0) {
1802 return -1;
1803 } else if (rc > 0) {
1804 rc = 0;
David Vinczebb6e3b62019-07-31 14:24:05 +02001805 break;
1806 }
David Vincze07706a42019-12-12 18:20:12 +01001807
1808 if (len != sizeof(dep)) {
1809 rc = BOOT_EBADIMAGE;
1810 goto done;
1811 }
1812
1813 rc = flash_area_read(fap, off, &dep, len);
1814 if (rc != 0) {
1815 rc = BOOT_EFLASH;
1816 goto done;
1817 }
1818
1819 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
1820 rc = BOOT_EBADARGS;
1821 goto done;
1822 }
1823
1824 /* Verify dependency and modify the swap type if not satisfied. */
1825 rc = boot_verify_slot_dependency(state, &dep);
1826 if (rc != 0) {
1827 /* Dependency not satisfied. */
1828 goto done;
Tamas Ban056ed0b2019-09-16 12:48:32 +01001829 }
David Vinczebb6e3b62019-07-31 14:24:05 +02001830 }
1831
1832done:
1833 flash_area_close(fap);
1834 return rc;
1835}
1836
1837/**
David Vinczebb6e3b62019-07-31 14:24:05 +02001838 * Iterate over all the images and verify whether the image dependencies in the
1839 * TLV area are all satisfied and update the related swap type if necessary.
1840 */
David Vinczecea8b592019-10-29 16:09:51 +01001841static int
1842boot_verify_dependencies(struct boot_loader_state *state)
David Vinczebb6e3b62019-07-31 14:24:05 +02001843{
David Vinczebb6e3b62019-07-31 14:24:05 +02001844 int rc;
David Vinczecea8b592019-10-29 16:09:51 +01001845 uint8_t slot;
David Vinczebb6e3b62019-07-31 14:24:05 +02001846
David Vinczecea8b592019-10-29 16:09:51 +01001847 BOOT_CURR_IMG(state) = 0;
1848 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
1849 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1850 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
1851 slot = BOOT_SECONDARY_SLOT;
1852 } else {
1853 slot = BOOT_PRIMARY_SLOT;
1854 }
1855
1856 rc = boot_verify_slot_dependencies(state, slot);
David Vinczebb6e3b62019-07-31 14:24:05 +02001857 if (rc == 0) {
1858 /* All dependencies've been satisfied, continue with next image. */
David Vinczecea8b592019-10-29 16:09:51 +01001859 BOOT_CURR_IMG(state)++;
David Vinczebb6e3b62019-07-31 14:24:05 +02001860 } else if (rc == BOOT_EBADVERSION) {
David Vinczecea8b592019-10-29 16:09:51 +01001861 /* Cannot upgrade due to non-met dependencies, so disable all
1862 * image upgrades.
1863 */
1864 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
1865 BOOT_CURR_IMG(state) = idx;
1866 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1867 }
1868 break;
David Vinczebb6e3b62019-07-31 14:24:05 +02001869 } else {
1870 /* Other error happened, images are inconsistent */
David Vinczecea8b592019-10-29 16:09:51 +01001871 return rc;
David Vinczebb6e3b62019-07-31 14:24:05 +02001872 }
1873 }
David Vinczecea8b592019-10-29 16:09:51 +01001874 return rc;
David Vinczebb6e3b62019-07-31 14:24:05 +02001875}
1876#endif /* (BOOT_IMAGE_NUMBER > 1) */
1877
Tamas Banf70ef8c2017-12-19 15:35:09 +00001878/**
David Vincze7384ee72019-07-23 17:00:42 +02001879 * Performs a clean (not aborted) image update.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001880 *
David Vincze7384ee72019-07-23 17:00:42 +02001881 * @param bs The current boot status.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001882 *
1883 * @return 0 on success; nonzero on failure.
1884 */
1885static int
David Vinczecea8b592019-10-29 16:09:51 +01001886boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001887{
Tamas Banf70ef8c2017-12-19 15:35:09 +00001888 int rc;
David Vinczecea8b592019-10-29 16:09:51 +01001889#ifndef MCUBOOT_OVERWRITE_ONLY
1890 uint8_t swap_type;
1891#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +00001892
David Vincze7384ee72019-07-23 17:00:42 +02001893 /* At this point there are no aborted swaps. */
1894#if defined(MCUBOOT_OVERWRITE_ONLY)
David Vinczecea8b592019-10-29 16:09:51 +01001895 rc = boot_copy_image(state, bs);
David Vincze7384ee72019-07-23 17:00:42 +02001896#else
David Vinczecea8b592019-10-29 16:09:51 +01001897 rc = boot_swap_image(state, bs);
David Vincze7384ee72019-07-23 17:00:42 +02001898#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +00001899 assert(rc == 0);
David Vincze7384ee72019-07-23 17:00:42 +02001900
1901#ifndef MCUBOOT_OVERWRITE_ONLY
1902 /* The following state needs image_ok be explicitly set after the
1903 * swap was finished to avoid a new revert.
1904 */
David Vinczecea8b592019-10-29 16:09:51 +01001905 swap_type = BOOT_SWAP_TYPE(state);
1906 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1907 swap_type == BOOT_SWAP_TYPE_PERM) {
1908 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02001909 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01001910 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vincze7384ee72019-07-23 17:00:42 +02001911 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001912 }
1913
David Vinczecea8b592019-10-29 16:09:51 +01001914 if (swap_type == BOOT_SWAP_TYPE_PERM) {
David Vincze7384ee72019-07-23 17:00:42 +02001915 /* Update the stored security counter with the new image's security
1916 * counter value. The primary slot holds the new image at this
1917 * point, but the secondary slot's image header must be passed
1918 * because the read image headers in the boot_data structure have
1919 * not been updated yet.
1920 *
1921 * In case of a permanent image swap mcuboot will never attempt to
1922 * revert the images on the next reboot. Therefore, the security
1923 * counter must be increased right after the image upgrade.
David Vincze401c7422019-06-21 20:44:05 +02001924 */
David Vinczecea8b592019-10-29 16:09:51 +01001925 rc = boot_update_security_counter(
1926 BOOT_CURR_IMG(state),
1927 BOOT_PRIMARY_SLOT,
1928 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
David Vincze7384ee72019-07-23 17:00:42 +02001929 if (rc != 0) {
1930 BOOT_LOG_ERR("Security counter update failed after "
1931 "image upgrade.");
David Vinczecea8b592019-10-29 16:09:51 +01001932 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001933 }
1934 }
1935
David Vinczecea8b592019-10-29 16:09:51 +01001936 if (BOOT_IS_UPGRADE(swap_type)) {
1937 rc = boot_set_copy_done(BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02001938 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01001939 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vincze7384ee72019-07-23 17:00:42 +02001940 }
1941 }
1942#endif /* !MCUBOOT_OVERWRITE_ONLY */
1943
1944 return rc;
1945}
1946
1947/**
1948 * Completes a previously aborted image swap.
1949 *
1950 * @param bs The current boot status.
1951 *
1952 * @return 0 on success; nonzero on failure.
1953 */
1954#if !defined(MCUBOOT_OVERWRITE_ONLY)
1955static int
David Vinczecea8b592019-10-29 16:09:51 +01001956boot_complete_partial_swap(struct boot_loader_state *state,
1957 struct boot_status *bs)
David Vincze7384ee72019-07-23 17:00:42 +02001958{
1959 int rc;
1960
1961 /* Determine the type of swap operation being resumed from the
1962 * `swap-type` trailer field.
1963 */
David Vinczecea8b592019-10-29 16:09:51 +01001964 rc = boot_swap_image(state, bs);
David Vincze7384ee72019-07-23 17:00:42 +02001965 assert(rc == 0);
1966
David Vinczecea8b592019-10-29 16:09:51 +01001967 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vincze7384ee72019-07-23 17:00:42 +02001968
1969 /* The following states need image_ok be explicitly set after the
1970 * swap was finished to avoid a new revert.
1971 */
1972 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1973 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
David Vinczecea8b592019-10-29 16:09:51 +01001974 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02001975 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01001976 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vincze7384ee72019-07-23 17:00:42 +02001977 }
1978 }
1979
David Vinczecea8b592019-10-29 16:09:51 +01001980 if (BOOT_IS_UPGRADE(bs->swap_type)) {
1981 rc = boot_set_copy_done(BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02001982 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01001983 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vincze7384ee72019-07-23 17:00:42 +02001984 }
1985 }
1986
David Vinczecea8b592019-10-29 16:09:51 +01001987 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vincze7384ee72019-07-23 17:00:42 +02001988 BOOT_LOG_ERR("panic!");
1989 assert(0);
1990
1991 /* Loop forever... */
1992 while (1) {}
1993 }
1994
1995 return rc;
1996}
1997#endif /* !MCUBOOT_OVERWRITE_ONLY */
1998
1999#if (BOOT_IMAGE_NUMBER > 1)
2000/**
2001 * Review the validity of previously determined swap types of other images.
2002 *
2003 * @param aborted_swap The current image upgrade is a
2004 * partial/aborted swap.
2005 */
2006static void
David Vinczecea8b592019-10-29 16:09:51 +01002007boot_review_image_swap_types(struct boot_loader_state *state,
2008 bool aborted_swap)
David Vincze7384ee72019-07-23 17:00:42 +02002009{
2010 /* In that case if we rebooted in the middle of an image upgrade process, we
2011 * must review the validity of swap types, that were previously determined
2012 * for other images. The image_ok flag had not been set before the reboot
2013 * for any of the updated images (only the copy_done flag) and thus falsely
2014 * the REVERT swap type has been determined for the previous images that had
2015 * been updated before the reboot.
2016 *
2017 * There are two separate scenarios that we have to deal with:
2018 *
2019 * 1. The reboot has happened during swapping an image:
2020 * The current image upgrade has been determined as a
2021 * partial/aborted swap.
2022 * 2. The reboot has happened between two separate image upgrades:
2023 * In this scenario we must check the swap type of the current image.
2024 * In those cases if it is NONE or REVERT we cannot certainly determine
2025 * the fact of a reboot. In a consistent state images must move in the
2026 * same direction or stay in place, e.g. in practice REVERT and TEST
2027 * swap types cannot be present at the same time. If the swap type of
2028 * the current image is either TEST, PERM or FAIL we must review the
2029 * already determined swap types of other images and set each false
2030 * REVERT swap types to NONE (these images had been successfully
2031 * updated before the system rebooted between two separate image
2032 * upgrades).
2033 */
2034
David Vinczecea8b592019-10-29 16:09:51 +01002035 if (BOOT_CURR_IMG(state) == 0) {
David Vincze7384ee72019-07-23 17:00:42 +02002036 /* Nothing to do */
2037 return;
2038 }
2039
2040 if (!aborted_swap) {
David Vinczecea8b592019-10-29 16:09:51 +01002041 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
2042 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vincze7384ee72019-07-23 17:00:42 +02002043 /* Nothing to do */
2044 return;
2045 }
2046 }
2047
David Vinczecea8b592019-10-29 16:09:51 +01002048 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
2049 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
2050 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002051 }
2052 }
2053}
2054#endif
2055
2056/**
2057 * Prepare image to be updated if required.
2058 *
2059 * Prepare image to be updated if required with completing an image swap
2060 * operation if one was aborted and/or determining the type of the
2061 * swap operation. In case of any error set the swap type to NONE.
2062 *
David Vinczecea8b592019-10-29 16:09:51 +01002063 * @param state Boot loader status information.
David Vincze7384ee72019-07-23 17:00:42 +02002064 * @param bs Pointer where the read and possibly updated
2065 * boot status can be written to.
2066 */
2067static void
David Vinczecea8b592019-10-29 16:09:51 +01002068boot_prepare_image_for_update(struct boot_loader_state *state,
2069 struct boot_status *bs)
David Vincze7384ee72019-07-23 17:00:42 +02002070{
2071 int rc;
2072
2073 /* Determine the sector layout of the image slots and scratch area. */
David Vinczecea8b592019-10-29 16:09:51 +01002074 rc = boot_read_sectors(state);
David Vincze7384ee72019-07-23 17:00:42 +02002075 if (rc != 0) {
2076 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
2077 " - too small?", BOOT_MAX_IMG_SECTORS);
2078 /* Unable to determine sector layout, continue with next image
2079 * if there is one.
2080 */
David Vinczecea8b592019-10-29 16:09:51 +01002081 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002082 return;
2083 }
2084
2085 /* Attempt to read an image header from each slot. */
David Vinczecea8b592019-10-29 16:09:51 +01002086 rc = boot_read_image_headers(state, false);
David Vincze7384ee72019-07-23 17:00:42 +02002087 if (rc != 0) {
2088 /* Continue with next image if there is one. */
David Vinczecea8b592019-10-29 16:09:51 +01002089 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
2090 BOOT_CURR_IMG(state));
2091 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002092 return;
2093 }
2094
2095 /* If the current image's slots aren't compatible, no swap is possible.
2096 * Just boot into primary slot.
2097 */
David Vinczecea8b592019-10-29 16:09:51 +01002098 if (boot_slots_compatible(state)) {
2099 rc = boot_read_status(state, bs);
David Vincze7384ee72019-07-23 17:00:42 +02002100 if (rc != 0) {
2101 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
David Vinczecea8b592019-10-29 16:09:51 +01002102 BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02002103 /* Continue with next image if there is one. */
David Vinczecea8b592019-10-29 16:09:51 +01002104 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002105 return;
2106 }
2107
2108 /* Determine if we rebooted in the middle of an image swap
2109 * operation. If a partial swap was detected, complete it.
2110 */
2111 if (bs->idx != BOOT_STATUS_IDX_0 || bs->state != BOOT_STATUS_STATE_0) {
2112
2113#if (BOOT_IMAGE_NUMBER > 1)
David Vinczecea8b592019-10-29 16:09:51 +01002114 boot_review_image_swap_types(state, true);
David Vincze7384ee72019-07-23 17:00:42 +02002115#endif
2116
2117#ifdef MCUBOOT_OVERWRITE_ONLY
2118 /* Should never arrive here, overwrite-only mode has
2119 * no swap state.
2120 */
2121 assert(0);
2122#else
2123 /* Determine the type of swap operation being resumed from the
2124 * `swap-type` trailer field.
2125 */
David Vinczecea8b592019-10-29 16:09:51 +01002126 rc = boot_complete_partial_swap(state, bs);
David Vincze7384ee72019-07-23 17:00:42 +02002127 assert(rc == 0);
2128#endif
2129 /* Attempt to read an image header from each slot. Ensure that
2130 * image headers in slots are aligned with headers in boot_data.
2131 */
David Vinczecea8b592019-10-29 16:09:51 +01002132 rc = boot_read_image_headers(state, false);
David Vincze7384ee72019-07-23 17:00:42 +02002133 assert(rc == 0);
2134
2135 /* Swap has finished set to NONE */
David Vinczecea8b592019-10-29 16:09:51 +01002136 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002137 } else {
2138 /* There was no partial swap, determine swap type. */
2139 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
David Vinczecea8b592019-10-29 16:09:51 +01002140 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
2141 } else if (boot_validate_slot(state,
2142 BOOT_SECONDARY_SLOT, bs) != 0) {
2143 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
David Vincze7384ee72019-07-23 17:00:42 +02002144 } else {
David Vinczecea8b592019-10-29 16:09:51 +01002145 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vincze7384ee72019-07-23 17:00:42 +02002146 }
2147
2148#if (BOOT_IMAGE_NUMBER > 1)
David Vinczecea8b592019-10-29 16:09:51 +01002149 boot_review_image_swap_types(state, false);
David Vincze7384ee72019-07-23 17:00:42 +02002150#endif
2151 }
2152 } else {
2153 /* In that case if slots are not compatible. */
David Vinczecea8b592019-10-29 16:09:51 +01002154 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002155 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00002156}
2157
2158/**
2159 * Prepares the booting process. This function moves images around in flash as
2160 * appropriate, and tells you what address to boot from.
2161 *
David Vinczecea8b592019-10-29 16:09:51 +01002162 * @param state Boot loader status information.
Tamas Banf70ef8c2017-12-19 15:35:09 +00002163 * @param rsp On success, indicates how booting should occur.
2164 *
2165 * @return 0 on success; nonzero on failure.
2166 */
2167int
David Vinczecea8b592019-10-29 16:09:51 +01002168context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Tamas Banf70ef8c2017-12-19 15:35:09 +00002169{
Tamas Banf70ef8c2017-12-19 15:35:09 +00002170 size_t slot;
David Vincze7384ee72019-07-23 17:00:42 +02002171 struct boot_status bs;
Tamas Ban4e5ed8d2019-09-17 09:31:11 +01002172 int rc = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002173 int fa_id;
David Vinczecea8b592019-10-29 16:09:51 +01002174 int image_index;
2175 bool has_upgrade;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002176
2177 /* The array of slot sectors are defined here (as opposed to file scope) so
2178 * that they don't get allocated for non-boot-loader apps. This is
2179 * necessary because the gcc option "-fdata-sections" doesn't seem to have
2180 * any effect in older gcc versions (e.g., 4.8.4).
2181 */
David Vincze7384ee72019-07-23 17:00:42 +02002182 static boot_sector_t
2183 primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
2184 static boot_sector_t
2185 secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
David Vincze401c7422019-06-21 20:44:05 +02002186 static boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Tamas Banf70ef8c2017-12-19 15:35:09 +00002187
David Vincze7384ee72019-07-23 17:00:42 +02002188 /* Iterate over all the images. By the end of the loop the swap type has
2189 * to be determined for each image and all aborted swaps have to be
2190 * completed.
Tamas Banf70ef8c2017-12-19 15:35:09 +00002191 */
David Vinczecea8b592019-10-29 16:09:51 +01002192 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2193
2194 image_index = BOOT_CURR_IMG(state);
2195
2196 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
2197 primary_slot_sectors[image_index];
2198 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
2199 secondary_slot_sectors[image_index];
2200 state->scratch.sectors = scratch_sectors;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002201
David Vincze7384ee72019-07-23 17:00:42 +02002202 /* Open primary and secondary image areas for the duration
2203 * of this call.
Tamas Banf70ef8c2017-12-19 15:35:09 +00002204 */
David Vincze7384ee72019-07-23 17:00:42 +02002205 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
David Vinczecea8b592019-10-29 16:09:51 +01002206 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
2207 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vincze7384ee72019-07-23 17:00:42 +02002208 assert(rc == 0);
2209 }
2210 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
David Vinczecea8b592019-10-29 16:09:51 +01002211 &BOOT_SCRATCH_AREA(state));
David Vincze7384ee72019-07-23 17:00:42 +02002212 assert(rc == 0);
2213
2214 /* Determine swap type and complete swap if it has been aborted. */
David Vinczecea8b592019-10-29 16:09:51 +01002215 boot_prepare_image_for_update(state, &bs);
2216
2217 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
2218 has_upgrade = true;
2219 }
David Vincze7384ee72019-07-23 17:00:42 +02002220 }
2221
David Vinczebb6e3b62019-07-31 14:24:05 +02002222#if (BOOT_IMAGE_NUMBER > 1)
David Vinczecea8b592019-10-29 16:09:51 +01002223 if (has_upgrade) {
2224 /* Iterate over all the images and verify whether the image dependencies
2225 * are all satisfied and update swap type if necessary.
2226 */
2227 rc = boot_verify_dependencies(state);
2228 if (rc == BOOT_EBADVERSION) {
2229 /*
2230 * It was impossible to upgrade because the expected dependency
2231 * version was not available. Here we already changed the swap_type
2232 * so that instead of asserting the bootloader, we continue and no
2233 * upgrade is performed.
2234 */
2235 rc = 0;
2236 }
2237 }
David Vinczebb6e3b62019-07-31 14:24:05 +02002238#endif
2239
David Vincze7384ee72019-07-23 17:00:42 +02002240 /* Iterate over all the images. At this point there are no aborted swaps
2241 * and the swap types are determined for each image. By the end of the loop
2242 * all required update operations will have been finished.
2243 */
David Vinczecea8b592019-10-29 16:09:51 +01002244 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vincze7384ee72019-07-23 17:00:42 +02002245
2246#if (BOOT_IMAGE_NUMBER > 1)
2247 /* Indicate that swap is not aborted */
2248 memset(&bs, 0, sizeof bs);
2249 bs.idx = BOOT_STATUS_IDX_0;
2250 bs.state = BOOT_STATUS_STATE_0;
2251#endif /* (BOOT_IMAGE_NUMBER > 1) */
2252
2253 /* Set the previously determined swap type */
David Vinczecea8b592019-10-29 16:09:51 +01002254 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vincze7384ee72019-07-23 17:00:42 +02002255
David Vinczecea8b592019-10-29 16:09:51 +01002256 switch (BOOT_SWAP_TYPE(state)) {
David Vincze7384ee72019-07-23 17:00:42 +02002257 case BOOT_SWAP_TYPE_NONE:
2258 break;
2259
2260 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
2261 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
2262 case BOOT_SWAP_TYPE_REVERT:
David Vinczecea8b592019-10-29 16:09:51 +01002263 rc = boot_perform_update(state, &bs);
David Vincze7384ee72019-07-23 17:00:42 +02002264 assert(rc == 0);
2265 break;
2266
2267 case BOOT_SWAP_TYPE_FAIL:
2268 /* The image in secondary slot was invalid and is now erased. Ensure
2269 * we don't try to boot into it again on the next reboot. Do this by
2270 * pretending we just reverted back to primary slot.
2271 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00002272#ifndef MCUBOOT_OVERWRITE_ONLY
David Vincze7384ee72019-07-23 17:00:42 +02002273 /* image_ok needs to be explicitly set to avoid a new revert. */
David Vinczecea8b592019-10-29 16:09:51 +01002274 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
Tamas Banf70ef8c2017-12-19 15:35:09 +00002275 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01002276 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002277 }
2278#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Vincze7384ee72019-07-23 17:00:42 +02002279 break;
2280
2281 default:
David Vinczecea8b592019-10-29 16:09:51 +01002282 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002283 }
David Vincze7384ee72019-07-23 17:00:42 +02002284
David Vinczecea8b592019-10-29 16:09:51 +01002285 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vincze7384ee72019-07-23 17:00:42 +02002286 BOOT_LOG_ERR("panic!");
2287 assert(0);
2288
2289 /* Loop forever... */
2290 while (1) {}
2291 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00002292 }
2293
David Vincze7384ee72019-07-23 17:00:42 +02002294 /* Iterate over all the images. At this point all required update operations
2295 * have finished. By the end of the loop each image in the primary slot will
2296 * have been re-validated.
2297 */
David Vinczecea8b592019-10-29 16:09:51 +01002298 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2299 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vincze7384ee72019-07-23 17:00:42 +02002300 /* Attempt to read an image header from each slot. Ensure that image
2301 * headers in slots are aligned with headers in boot_data.
David Vincze060968d2019-05-23 01:13:14 +02002302 */
David Vinczecea8b592019-10-29 16:09:51 +01002303 rc = boot_read_image_headers(state, false);
David Vincze060968d2019-05-23 01:13:14 +02002304 if (rc != 0) {
David Vincze7384ee72019-07-23 17:00:42 +02002305 goto out;
2306 }
2307 /* Since headers were reloaded, it can be assumed we just performed
2308 * a swap or overwrite. Now the header info that should be used to
2309 * provide the data for the bootstrap, which previously was at
2310 * secondary slot, was updated to primary slot.
2311 */
2312 }
2313
2314#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
David Vinczecea8b592019-10-29 16:09:51 +01002315 rc = boot_validate_slot(state, BOOT_PRIMARY_SLOT, NULL);
David Vincze7384ee72019-07-23 17:00:42 +02002316 if (rc != 0) {
2317 rc = BOOT_EBADIMAGE;
2318 goto out;
2319 }
2320#else
2321 /* Even if we're not re-validating the primary slot, we could be booting
2322 * onto an empty flash chip. At least do a basic sanity check that
2323 * the magic number on the image is OK.
2324 */
David Vinczecea8b592019-10-29 16:09:51 +01002325 if (!BOOT_IMG_HDR_IS_VALID(state, BOOT_PRIMARY_SLOT)) {
2326 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
2327 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_magic,
2328 BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02002329 rc = BOOT_EBADIMAGE;
2330 goto out;
2331 }
2332#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
2333
2334 /* Update the stored security counter with the active image's security
2335 * counter value. It will be updated only if the new security counter is
2336 * greater than the stored value.
2337 *
2338 * In case of a successful image swapping when the swap type is TEST the
2339 * security counter can be increased only after a reset, when the swap
2340 * type is NONE and the image has marked itself "OK" (the image_ok flag
2341 * has been set). This way a "revert" swap can be performed if it's
2342 * necessary.
2343 */
David Vinczecea8b592019-10-29 16:09:51 +01002344 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
2345 rc = boot_update_security_counter(
2346 BOOT_CURR_IMG(state),
2347 BOOT_PRIMARY_SLOT,
2348 boot_img_hdr(state, BOOT_PRIMARY_SLOT));
David Vincze7384ee72019-07-23 17:00:42 +02002349 if (rc != 0) {
2350 BOOT_LOG_ERR("Security counter update failed after image "
2351 "validation.");
David Vincze060968d2019-05-23 01:13:14 +02002352 goto out;
2353 }
2354 }
2355
David Vincze7384ee72019-07-23 17:00:42 +02002356 /* Save boot status to shared memory area */
2357#if (BOOT_IMAGE_NUMBER > 1)
David Vinczecea8b592019-10-29 16:09:51 +01002358 rc = boot_save_boot_status((BOOT_CURR_IMG(state) == 0) ?
2359 SW_SPE : SW_NSPE,
2360 boot_img_hdr(state, BOOT_PRIMARY_SLOT),
2361 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)
David Vincze7384ee72019-07-23 17:00:42 +02002362 );
David Vincze8bdfc2d2019-03-18 15:49:23 +01002363#else
David Vincze7384ee72019-07-23 17:00:42 +02002364 rc = boot_save_boot_status(SW_S_NS,
David Vinczecea8b592019-10-29 16:09:51 +01002365 boot_img_hdr(state, BOOT_PRIMARY_SLOT),
2366 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)
David Vincze7384ee72019-07-23 17:00:42 +02002367 );
2368#endif
2369 if (rc) {
2370 BOOT_LOG_ERR("Failed to add Image %u data to shared area",
David Vinczecea8b592019-10-29 16:09:51 +01002371 BOOT_CURR_IMG(state));
David Vincze060968d2019-05-23 01:13:14 +02002372 }
2373 }
2374
David Vinczecea8b592019-10-29 16:09:51 +01002375#if (BOOT_IMAGE_NUMBER > 1)
David Vincze7384ee72019-07-23 17:00:42 +02002376 /* Always boot from the primary slot of Image 0. */
David Vinczecea8b592019-10-29 16:09:51 +01002377 BOOT_CURR_IMG(state) = 0;
2378#endif
Tamas Ban0e8ab302019-01-17 11:45:31 +00002379
David Vinczecea8b592019-10-29 16:09:51 +01002380 rsp->br_flash_dev_id =
2381 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)->fa_device_id;
2382 rsp->br_image_off =
2383 boot_img_slot_off(state, BOOT_PRIMARY_SLOT);
2384 rsp->br_hdr =
2385 boot_img_hdr(state, BOOT_PRIMARY_SLOT);
2386
2387out:
2388 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2389 flash_area_close(BOOT_SCRATCH_AREA(state));
David Vincze7384ee72019-07-23 17:00:42 +02002390 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
David Vinczecea8b592019-10-29 16:09:51 +01002391 flash_area_close(BOOT_IMG_AREA(state,
David Vincze7384ee72019-07-23 17:00:42 +02002392 BOOT_NUM_SLOTS - 1 - slot));
2393 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00002394 }
2395 return rc;
2396}
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002397
Tamas Ban0bd0dfc2020-09-11 15:25:48 +01002398#else /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOADING */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002399
David Vinczedcba70b2019-05-28 12:02:52 +02002400#define BOOT_LOG_IMAGE_INFO(area, hdr, state) \
2401 BOOT_LOG_INF("Image %u: version=%u.%u.%u+%u, magic=%5s, image_ok=0x%x", \
2402 (area), \
2403 (hdr)->ih_ver.iv_major, \
2404 (hdr)->ih_ver.iv_minor, \
2405 (hdr)->ih_ver.iv_revision, \
2406 (hdr)->ih_ver.iv_build_num, \
2407 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
2408 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
2409 "bad"), \
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002410 (state)->image_ok)
2411
2412struct image_slot_version {
2413 uint64_t version;
2414 uint32_t slot_number;
2415};
2416
2417/**
2418 * Extract the version number from the image header. This function must be
2419 * ported if version number format has changed in the image header.
2420 *
2421 * @param hdr Pointer to an image header structure
2422 *
Oliver Swedef9982442018-08-24 18:37:44 +01002423 * @return Version number casted to uint64_t
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002424 */
2425static uint64_t
2426boot_get_version_number(struct image_header *hdr)
2427{
Oliver Swedef9982442018-08-24 18:37:44 +01002428 uint64_t version = 0;
2429 version |= (uint64_t)hdr->ih_ver.iv_major << (IMAGE_VER_MINOR_LENGTH
2430 + IMAGE_VER_REVISION_LENGTH
2431 + IMAGE_VER_BUILD_NUM_LENGTH);
2432 version |= (uint64_t)hdr->ih_ver.iv_minor << (IMAGE_VER_REVISION_LENGTH
2433 + IMAGE_VER_BUILD_NUM_LENGTH);
2434 version |= (uint64_t)hdr->ih_ver.iv_revision << IMAGE_VER_BUILD_NUM_LENGTH;
2435 version |= hdr->ih_ver.iv_build_num;
2436 return version;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002437}
2438
2439/**
2440 * Comparator function for `qsort` to compare version numbers. This function
2441 * must be ported if version number format has changed in the image header.
2442 *
2443 * @param ver1 Pointer to an array element which holds the version number
2444 * @param ver2 Pointer to another array element which holds the version
2445 * number
2446 *
2447 * @return if version1 > version2 -1
2448 * if version1 == version2 0
2449 * if version1 < version2 1
2450 */
2451static int
2452boot_compare_version_numbers(const void *ver1, const void *ver2)
2453{
2454 if (((struct image_slot_version *)ver1)->version <
2455 ((struct image_slot_version *)ver2)->version) {
2456 return 1;
2457 }
2458
2459 if (((struct image_slot_version *)ver1)->version ==
2460 ((struct image_slot_version *)ver2)->version) {
2461 return 0;
2462 }
2463
2464 return -1;
2465}
2466
2467/**
2468 * Sort the available images based on the version number and puts them in
2469 * a list.
2470 *
David Vinczecea8b592019-10-29 16:09:51 +01002471 * @param state Boot loader status information.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002472 * @param boot_sequence A pointer to an array, whose aim is to carry
2473 * the boot order of candidate images.
2474 * @param slot_cnt The number of flash areas, which can contains firmware
2475 * images.
2476 *
2477 * @return The number of valid images.
2478 */
2479uint32_t
David Vinczecea8b592019-10-29 16:09:51 +01002480boot_get_boot_sequence(struct boot_loader_state *state,
2481 uint32_t *boot_sequence, uint32_t slot_cnt)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002482{
2483 struct boot_swap_state slot_state;
2484 struct image_header *hdr;
2485 struct image_slot_version image_versions[BOOT_NUM_SLOTS] = {{0}};
2486 uint32_t image_cnt = 0;
2487 uint32_t slot;
2488 int32_t rc;
2489 int32_t fa_id;
2490
2491 for (slot = 0; slot < slot_cnt; slot++) {
David Vinczecea8b592019-10-29 16:09:51 +01002492 hdr = boot_img_hdr(state, slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002493 fa_id = flash_area_id_from_image_slot(slot);
2494 rc = boot_read_swap_state_by_id(fa_id, &slot_state);
2495 if (rc != 0) {
David Vinczedcba70b2019-05-28 12:02:52 +02002496 BOOT_LOG_ERR("Error during reading image trailer from slot: %u",
2497 slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002498 continue;
2499 }
2500
David Vinczecea8b592019-10-29 16:09:51 +01002501 if (BOOT_IMG_HDR_IS_VALID(state, slot)) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002502 if (slot_state.magic == BOOT_MAGIC_GOOD ||
David Vincze39e78552018-10-10 17:10:01 +02002503 slot_state.image_ok == BOOT_FLAG_SET) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002504 /* Valid cases:
2505 * - Test mode: magic is OK in image trailer
2506 * - Permanent mode: image_ok flag has previously set
2507 */
2508 image_versions[slot].slot_number = slot;
2509 image_versions[slot].version = boot_get_version_number(hdr);
2510 image_cnt++;
2511 }
2512
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002513 BOOT_LOG_IMAGE_INFO(slot, hdr, &slot_state);
2514 } else {
David Vinczedcba70b2019-05-28 12:02:52 +02002515 BOOT_LOG_INF("Image %u: No valid image", slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002516 }
2517 }
2518
2519 /* Sort the images based on version number */
2520 qsort(&image_versions[0],
2521 slot_cnt,
2522 sizeof(struct image_slot_version),
2523 boot_compare_version_numbers);
2524
2525 /* Copy the calculated boot sequence to boot_sequence array */
2526 for (slot = 0; slot < slot_cnt; slot++) {
2527 boot_sequence[slot] = image_versions[slot].slot_number;
2528 }
2529
2530 return image_cnt;
2531}
2532
Oliver Swedef9982442018-08-24 18:37:44 +01002533#ifdef MCUBOOT_RAM_LOADING
Raef Colesaf082382019-10-01 11:10:33 +01002534
2535/**
2536 * Verifies that the image in a slot lies within the predefined bounds that are
2537 * allowed to be used by executable images.
2538 *
2539 * @param img_dst The address to which the image is going to be copied.
2540 *
2541 * @param img_sz The size of the image.
2542 *
2543 * @return 0 on success; nonzero on failure.
2544 */
2545static int
2546boot_verify_ram_loading_address(uint32_t img_dst, uint32_t img_sz)
2547{
David Vincze0dc41d22019-10-28 14:56:29 +01002548 uint32_t img_end_addr;
2549
Raef Colesaf082382019-10-01 11:10:33 +01002550 if (img_dst < IMAGE_EXECUTABLE_RAM_START) {
2551 return BOOT_EBADIMAGE;
2552 }
2553
David Vincze0dc41d22019-10-28 14:56:29 +01002554 if (!boot_u32_safe_add(&img_end_addr, img_dst, img_sz)) {
Raef Colesaf082382019-10-01 11:10:33 +01002555 return BOOT_EBADIMAGE;
2556 }
2557
David Vincze0dc41d22019-10-28 14:56:29 +01002558 if (img_end_addr > (IMAGE_EXECUTABLE_RAM_START +
2559 IMAGE_EXECUTABLE_RAM_SIZE)) {
Raef Colesaf082382019-10-01 11:10:33 +01002560 return BOOT_EBADIMAGE;
2561 }
2562
2563 return 0;
2564}
2565
Oliver Swedef9982442018-08-24 18:37:44 +01002566/**
2567 * Copies an image from a slot in the flash to an SRAM address, where the load
2568 * address has already been inserted into the image header by this point and is
2569 * extracted from it within this method. The copying is done sector-by-sector.
2570 *
David Vinczecea8b592019-10-29 16:09:51 +01002571 * @param state Boot loader status information.
Oliver Swedef9982442018-08-24 18:37:44 +01002572 * @param slot The flash slot of the image to be copied to SRAM.
2573 *
2574 * @param hdr Pointer to the image header structure of the image
Raef Colesaf082382019-10-01 11:10:33 +01002575 *
2576 * @param img_dst The address at which the image needs to be copied to
2577 * SRAM.
2578 *
2579 * @param img_sz The size of the image that needs to be copied to SRAM.
Oliver Swedef9982442018-08-24 18:37:44 +01002580 *
2581 * @return 0 on success; nonzero on failure.
2582 */
2583static int
David Vinczecea8b592019-10-29 16:09:51 +01002584boot_copy_image_to_sram(struct boot_loader_state *state, int slot,
2585 struct image_header *hdr,
Raef Colesaf082382019-10-01 11:10:33 +01002586 uint32_t img_dst, uint32_t img_sz)
Oliver Swedef9982442018-08-24 18:37:44 +01002587{
2588 int rc;
2589 uint32_t sect_sz;
2590 uint32_t sect = 0;
2591 uint32_t bytes_copied = 0;
2592 const struct flash_area *fap_src = NULL;
Oliver Swedef9982442018-08-24 18:37:44 +01002593
Raef Colesaf082382019-10-01 11:10:33 +01002594 if (img_dst % 4 != 0) {
Tamas Banc27b5c32019-05-28 16:30:19 +01002595 BOOT_LOG_INF("Cannot copy the image to the SRAM address 0x%x "
Oliver Swedef9982442018-08-24 18:37:44 +01002596 "- the load address must be aligned with 4 bytes due to SRAM "
Raef Colesaf082382019-10-01 11:10:33 +01002597 "restrictions", img_dst);
Oliver Swedef9982442018-08-24 18:37:44 +01002598 return BOOT_EBADARGS;
2599 }
2600
2601 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap_src);
2602 if (rc != 0) {
2603 return BOOT_EFLASH;
2604 }
2605
Oliver Swedef9982442018-08-24 18:37:44 +01002606 while (bytes_copied < img_sz) {
David Vinczecea8b592019-10-29 16:09:51 +01002607 sect_sz = boot_img_sector_size(state, slot, sect);
Oliver Swedef9982442018-08-24 18:37:44 +01002608 /*
2609 * Direct copy from where the image sector resides in flash to its new
2610 * location in SRAM
2611 */
2612 rc = flash_area_read(fap_src,
2613 bytes_copied,
Raef Colesaf082382019-10-01 11:10:33 +01002614 (void *)(img_dst + bytes_copied),
Oliver Swedef9982442018-08-24 18:37:44 +01002615 sect_sz);
2616 if (rc != 0) {
2617 BOOT_LOG_INF("Error whilst copying image from Flash to SRAM");
2618 break;
2619 } else {
2620 bytes_copied += sect_sz;
2621 }
2622 sect++;
2623 }
2624
2625 if (fap_src) {
2626 flash_area_close(fap_src);
2627 }
2628 return rc;
2629}
Raef Coles27a61452019-09-25 15:32:25 +01002630
2631/**
2632 * Removes an image from SRAM, by overwriting it with zeros.
2633 *
Raef Colesaf082382019-10-01 11:10:33 +01002634 * @param img_dst The address of the image that needs to be removed from
2635 * SRAM.
Raef Coles27a61452019-09-25 15:32:25 +01002636 *
Raef Colesaf082382019-10-01 11:10:33 +01002637 * @param img_sz The size of the image that needs to be removed from
2638 * SRAM.
Raef Coles27a61452019-09-25 15:32:25 +01002639 *
2640 * @return 0 on success; nonzero on failure.
2641 */
2642static int
Raef Colesaf082382019-10-01 11:10:33 +01002643boot_remove_image_from_sram(uint32_t img_dst, uint32_t img_sz)
Raef Coles27a61452019-09-25 15:32:25 +01002644{
Raef Colesaf082382019-10-01 11:10:33 +01002645 BOOT_LOG_INF("Removing image from SRAM at address 0x%x", img_dst);
2646 memset((void*)img_dst, 0, img_sz);
Raef Coles27a61452019-09-25 15:32:25 +01002647
2648 return 0;
2649}
Oliver Swedef9982442018-08-24 18:37:44 +01002650#endif /* MCUBOOT_RAM_LOADING */
2651
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002652/**
2653 * Prepares the booting process. This function choose the newer image in flash
David Vinczecea8b592019-10-29 16:09:51 +01002654 * as appropriate, and tells you what address to boot from.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002655 *
David Vinczecea8b592019-10-29 16:09:51 +01002656 * @param state Boot loader status information.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002657 * @param rsp On success, indicates how booting should occur.
2658 *
2659 * @return 0 on success; nonzero on failure.
2660 */
2661int
David Vinczecea8b592019-10-29 16:09:51 +01002662context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002663{
2664 size_t slot = 0;
2665 int32_t i;
2666 int rc;
2667 int fa_id;
2668 uint32_t boot_sequence[BOOT_NUM_SLOTS];
2669 uint32_t img_cnt;
Tamas Banaf8584e2020-04-27 09:09:44 +01002670 struct image_header *selected_image_header = NULL;
Raef Coles27a61452019-09-25 15:32:25 +01002671#ifdef MCUBOOT_RAM_LOADING
2672 int image_copied = 0;
Raef Colesaf082382019-10-01 11:10:33 +01002673 uint32_t img_dst = 0;
2674 uint32_t img_sz = 0;
Raef Coles27a61452019-09-25 15:32:25 +01002675#endif /* MCUBOOT_RAM_LOADING */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002676
David Vincze8bdfc2d2019-03-18 15:49:23 +01002677 static boot_sector_t primary_slot_sectors[BOOT_MAX_IMG_SECTORS];
2678 static boot_sector_t secondary_slot_sectors[BOOT_MAX_IMG_SECTORS];
Raef Coles35ff8162019-12-12 13:43:15 +00002679 static boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002680
Raef Coles35ff8162019-12-12 13:43:15 +00002681 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors = primary_slot_sectors;
2682 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors = secondary_slot_sectors;
2683 state->scratch.sectors = scratch_sectors;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002684
2685 /* Open boot_data image areas for the duration of this call. */
2686 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
2687 fa_id = flash_area_id_from_image_slot(i);
David Vinczecea8b592019-10-29 16:09:51 +01002688 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, i));
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002689 assert(rc == 0);
2690 }
2691
2692 /* Determine the sector layout of the image slots. */
David Vinczecea8b592019-10-29 16:09:51 +01002693 rc = boot_read_sectors(state);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002694 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01002695 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - "
2696 "too small?", BOOT_MAX_IMG_SECTORS);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002697 goto out;
2698 }
2699
2700 /* Attempt to read an image header from each slot. */
David Vinczecea8b592019-10-29 16:09:51 +01002701 rc = boot_read_image_headers(state, false);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002702 if (rc != 0) {
2703 goto out;
2704 }
2705
David Vinczecea8b592019-10-29 16:09:51 +01002706 img_cnt = boot_get_boot_sequence(state, boot_sequence, BOOT_NUM_SLOTS);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002707 if (img_cnt) {
2708 /* Authenticate images */
2709 for (i = 0; i < img_cnt; i++) {
Raef Coles27a61452019-09-25 15:32:25 +01002710
2711 slot = boot_sequence[i];
David Vinczecea8b592019-10-29 16:09:51 +01002712 selected_image_header = boot_img_hdr(state, slot);
Raef Coles27a61452019-09-25 15:32:25 +01002713
2714#ifdef MCUBOOT_RAM_LOADING
2715 if (selected_image_header->ih_flags & IMAGE_F_RAM_LOAD) {
Raef Colesaf082382019-10-01 11:10:33 +01002716
2717 img_dst = selected_image_header->ih_load_addr;
2718
David Vinczecea8b592019-10-29 16:09:51 +01002719 rc = boot_read_image_size(state, slot, &img_sz);
Raef Colesaf082382019-10-01 11:10:33 +01002720 if (rc != 0) {
2721 rc = BOOT_EFLASH;
2722 BOOT_LOG_INF("Could not load image headers from the image"
2723 "in the %s slot.",
2724 (slot == BOOT_PRIMARY_SLOT) ?
2725 "primary" : "secondary");
2726 continue;
2727 }
2728
2729 rc = boot_verify_ram_loading_address(img_dst, img_sz);
2730 if (rc != 0) {
2731 BOOT_LOG_INF("Could not copy image from the %s slot in "
2732 "the Flash to load address 0x%x in SRAM as"
2733 " the image would overlap memory outside"
2734 " the defined executable region.",
2735 (slot == BOOT_PRIMARY_SLOT) ?
2736 "primary" : "secondary",
2737 selected_image_header->ih_load_addr);
2738 continue;
2739 }
2740
Raef Coles27a61452019-09-25 15:32:25 +01002741 /* Copy image to the load address from where it
2742 * currently resides in flash
2743 */
David Vinczecea8b592019-10-29 16:09:51 +01002744 rc = boot_copy_image_to_sram(state, slot, selected_image_header,
Raef Colesaf082382019-10-01 11:10:33 +01002745 img_dst, img_sz);
Raef Coles27a61452019-09-25 15:32:25 +01002746 if (rc != 0) {
2747 rc = BOOT_EBADIMAGE;
2748 BOOT_LOG_INF("Could not copy image from the %s slot in "
2749 "the Flash to load address 0x%x in SRAM, "
2750 "aborting..", (slot == BOOT_PRIMARY_SLOT) ?
2751 "primary" : "secondary",
2752 selected_image_header->ih_load_addr);
2753 continue;
2754 } else {
2755 BOOT_LOG_INF("Image has been copied from the %s slot in "
2756 "the flash to SRAM address 0x%x",
2757 (slot == BOOT_PRIMARY_SLOT) ?
2758 "primary" : "secondary",
2759 selected_image_header->ih_load_addr);
2760 image_copied = 1;
2761 }
2762 } else {
2763 /* Only images that support IMAGE_F_RAM_LOAD are allowed if
2764 * MCUBOOT_RAM_LOADING is set.
2765 */
2766 rc = BOOT_EBADIMAGE;
2767 continue;
2768 }
2769#endif /* MCUBOOT_RAM_LOADING */
David Vinczecea8b592019-10-29 16:09:51 +01002770 rc = boot_validate_slot(state, slot, NULL);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002771 if (rc == 0) {
Raef Coles27a61452019-09-25 15:32:25 +01002772 /* If a valid image is found then there is no reason to check
2773 * the rest of the images, as they were already ordered by
2774 * preference.
2775 */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002776 break;
2777 }
Raef Coles27a61452019-09-25 15:32:25 +01002778#ifdef MCUBOOT_RAM_LOADING
2779 else if (image_copied) {
2780 /* If an image is found to be invalid then it is removed from
2781 * RAM to prevent it being a shellcode vector.
2782 */
Raef Colesaf082382019-10-01 11:10:33 +01002783 boot_remove_image_from_sram(img_dst, img_sz);
Raef Coles27a61452019-09-25 15:32:25 +01002784 image_copied = 0;
2785 }
2786#endif /* MCUBOOT_RAM_LOADING */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002787 }
2788 if (rc) {
2789 /* If there was no valid image at all */
2790 rc = BOOT_EBADIMAGE;
2791 goto out;
2792 }
2793
David Vincze060968d2019-05-23 01:13:14 +02002794 /* Update the security counter with the newest image's security
2795 * counter value.
2796 */
David Vinczecea8b592019-10-29 16:09:51 +01002797 rc = boot_update_security_counter(BOOT_CURR_IMG(state), slot,
2798 selected_image_header);
David Vincze060968d2019-05-23 01:13:14 +02002799 if (rc != 0) {
2800 BOOT_LOG_ERR("Security counter update failed after image "
2801 "validation.");
2802 goto out;
2803 }
2804
Oliver Swedef9982442018-08-24 18:37:44 +01002805
David Vincze8a2a4e22019-05-24 10:14:23 +02002806#ifdef MCUBOOT_RAM_LOADING
Raef Coles27a61452019-09-25 15:32:25 +01002807 BOOT_LOG_INF("Booting image from SRAM at address 0x%x",
2808 selected_image_header->ih_load_addr);
2809#else
2810 BOOT_LOG_INF("Booting image from the %s slot",
2811 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
2812#endif /* MCUBOOT_RAM_LOADING */
Oliver Swedef9982442018-08-24 18:37:44 +01002813
Raef Coles27a61452019-09-25 15:32:25 +01002814 rsp->br_hdr = selected_image_header;
David Vinczecea8b592019-10-29 16:09:51 +01002815 rsp->br_image_off = boot_img_slot_off(state, slot);
2816 rsp->br_flash_dev_id = BOOT_IMG_AREA(state, slot)->fa_device_id;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002817 } else {
2818 /* No candidate image available */
2819 rc = BOOT_EBADIMAGE;
David Vincze060968d2019-05-23 01:13:14 +02002820 goto out;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002821 }
2822
Tamas Ban0e8ab302019-01-17 11:45:31 +00002823 /* Save boot status to shared memory area */
2824 rc = boot_save_boot_status(SW_S_NS,
2825 rsp->br_hdr,
David Vinczecea8b592019-10-29 16:09:51 +01002826 BOOT_IMG_AREA(state, slot));
Tamas Ban0e8ab302019-01-17 11:45:31 +00002827 if (rc) {
2828 BOOT_LOG_ERR("Failed to add data to shared area");
2829 }
2830
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002831out:
2832 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
David Vinczecea8b592019-10-29 16:09:51 +01002833 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002834 }
2835 return rc;
2836}
Tamas Ban0bd0dfc2020-09-11 15:25:48 +01002837#endif /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOADING */
David Vinczecea8b592019-10-29 16:09:51 +01002838
2839int
2840boot_go(struct boot_rsp *rsp)
2841{
2842 return context_boot_go(&boot_data, rsp);
Robert Rostohar4b630372019-12-23 13:24:50 +01002843}