blob: 6aecfce9f88d12fceaa98fdff4da86f58567ee73 [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"
Tamas Bana9de4a62018-09-18 08:09:45 +010045#include "bl2/include/tfm_boot_status.h"
46#include "bl2/include/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
TTornblomfaf74f52020-03-04 17:56:27 +010058#if !defined(MCUBOOT_NO_SWAP) && !defined(MCUBOOT_RAM_LOADING) && !defined(MCUBOOT_OVERWRITE_ONLY)
59
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 Vincze39e78552018-10-10 17:10:01 +020071#endif
72
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)
TTornblomfaf74f52020-03-04 17:56:27 +0100163#endif /* !MCUBOOT_NO_SWAP && !MCUBOOT_RAM_LOADING && !defined(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
Oliver Swedef9982442018-08-24 18:37:44 +0100540#if !defined(MCUBOOT_NO_SWAP) && !defined(MCUBOOT_OVERWRITE_ONLY)
541/*
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}
601#endif /* !MCUBOOT_NO_SWAP && !MCUBOOT_OVERWRITE_ONLY */
602
TTornblomfaf74f52020-03-04 17:56:27 +0100603#if !defined(MCUBOOT_NO_SWAP) && !defined(MCUBOOT_RAM_LOADING) && !defined(MCUBOOT_OVERWRITE_ONLY)
604
Tamas Banf70ef8c2017-12-19 15:35:09 +0000605/**
Tamas Ban581034a2017-12-19 19:54:37 +0000606 * Determines where in flash the most recent boot status is stored. The boot
Tamas Banf70ef8c2017-12-19 15:35:09 +0000607 * status is necessary for completing a swap that was interrupted by a boot
608 * loader reset.
609 *
David Vincze401c7422019-06-21 20:44:05 +0200610 * @return A BOOT_STATUS_SOURCE_[...] code indicating where status should
611 * be read from.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000612 */
613static int
David Vinczecea8b592019-10-29 16:09:51 +0100614boot_status_source(struct boot_loader_state *state)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000615{
616 const struct boot_status_table *table;
617 struct boot_swap_state state_scratch;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100618 struct boot_swap_state state_primary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000619 int rc;
David Vincze39e78552018-10-10 17:10:01 +0200620 size_t i;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000621 uint8_t source;
David Vinczecea8b592019-10-29 16:09:51 +0100622 uint8_t image_index;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000623
David Vinczecea8b592019-10-29 16:09:51 +0100624#if (BOOT_IMAGE_NUMBER == 1)
625 (void)state;
626#endif
627
628 image_index = BOOT_CURR_IMG(state);
629 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
630 &state_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000631 assert(rc == 0);
632
633 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
634 assert(rc == 0);
635
David Vincze401c7422019-06-21 20:44:05 +0200636 BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000637 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
638
639 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
640 table = &boot_status_tables[i];
641
David Vincze401c7422019-06-21 20:44:05 +0200642 if (boot_magic_compatible_check(table->bst_magic_primary_slot,
643 state_primary_slot.magic) &&
644 boot_magic_compatible_check(table->bst_magic_scratch,
645 state_scratch.magic) &&
David Vincze8bdfc2d2019-03-18 15:49:23 +0100646 (table->bst_copy_done_primary_slot == BOOT_FLAG_ANY ||
647 table->bst_copy_done_primary_slot == state_primary_slot.copy_done))
648 {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000649 source = table->bst_status_source;
David Vincze7384ee72019-07-23 17:00:42 +0200650
651#if (BOOT_IMAGE_NUMBER > 1)
652 /* In case of multi-image boot it can happen that if boot status
653 * info is found on scratch area then it does not belong to the
654 * currently examined image.
655 */
656 if (source == BOOT_STATUS_SOURCE_SCRATCH &&
David Vinczecea8b592019-10-29 16:09:51 +0100657 state_scratch.image_num != BOOT_CURR_IMG(state)) {
David Vincze7384ee72019-07-23 17:00:42 +0200658 source = BOOT_STATUS_SOURCE_NONE;
659 }
660#endif
661
Tamas Banf70ef8c2017-12-19 15:35:09 +0000662 BOOT_LOG_INF("Boot source: %s",
663 source == BOOT_STATUS_SOURCE_NONE ? "none" :
664 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
David Vincze8bdfc2d2019-03-18 15:49:23 +0100665 source == BOOT_STATUS_SOURCE_PRIMARY_SLOT ?
666 "primary slot" : "BUG; can't happen");
Tamas Banf70ef8c2017-12-19 15:35:09 +0000667 return source;
668 }
669 }
670
671 BOOT_LOG_INF("Boot source: none");
672 return BOOT_STATUS_SOURCE_NONE;
673}
674
David Vincze401c7422019-06-21 20:44:05 +0200675/*
David Vincze4af67832019-12-11 18:31:34 +0100676 * Slots are compatible when all sectors that store up to to size of the image
David Vincze401c7422019-06-21 20:44:05 +0200677 * round up to sector size, in both slot's are able to fit in the scratch
678 * area, and have sizes that are a multiple of each other (powers of two
679 * presumably!).
Tamas Banf70ef8c2017-12-19 15:35:09 +0000680 */
681static int
David Vinczecea8b592019-10-29 16:09:51 +0100682boot_slots_compatible(struct boot_loader_state *state)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000683{
David Vincze401c7422019-06-21 20:44:05 +0200684 size_t num_sectors_primary;
685 size_t num_sectors_secondary;
686 size_t sz0, sz1;
687 size_t primary_slot_sz, secondary_slot_sz;
David Vincze4af67832019-12-11 18:31:34 +0100688#ifndef MCUBOOT_OVERWRITE_ONLY
David Vincze401c7422019-06-21 20:44:05 +0200689 size_t scratch_sz;
David Vincze4af67832019-12-11 18:31:34 +0100690#endif
David Vincze401c7422019-06-21 20:44:05 +0200691 size_t i, j;
692 int8_t smaller;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000693
David Vinczecea8b592019-10-29 16:09:51 +0100694 num_sectors_primary = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
695 num_sectors_secondary = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT);
David Vincze401c7422019-06-21 20:44:05 +0200696 if ((num_sectors_primary > BOOT_MAX_IMG_SECTORS) ||
697 (num_sectors_secondary > BOOT_MAX_IMG_SECTORS)) {
David Vincze39e78552018-10-10 17:10:01 +0200698 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
Tamas Banf70ef8c2017-12-19 15:35:09 +0000699 return 0;
700 }
David Vincze39e78552018-10-10 17:10:01 +0200701
David Vincze4af67832019-12-11 18:31:34 +0100702#ifndef MCUBOOT_OVERWRITE_ONLY
David Vinczecea8b592019-10-29 16:09:51 +0100703 scratch_sz = boot_scratch_area_size(state);
David Vincze4af67832019-12-11 18:31:34 +0100704#endif
David Vincze401c7422019-06-21 20:44:05 +0200705
706 /*
707 * The following loop scans all sectors in a linear fashion, assuring that
708 * for each possible sector in each slot, it is able to fit in the other
709 * slot's sector or sectors. Slot's should be compatible as long as any
710 * number of a slot's sectors are able to fit into another, which only
711 * excludes cases where sector sizes are not a multiple of each other.
712 */
713 i = sz0 = primary_slot_sz = 0;
714 j = sz1 = secondary_slot_sz = 0;
715 smaller = 0;
716 while (i < num_sectors_primary || j < num_sectors_secondary) {
717 if (sz0 == sz1) {
David Vinczecea8b592019-10-29 16:09:51 +0100718 sz0 += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
719 sz1 += boot_img_sector_size(state, BOOT_SECONDARY_SLOT, j);
David Vincze401c7422019-06-21 20:44:05 +0200720 i++;
721 j++;
722 } else if (sz0 < sz1) {
David Vinczecea8b592019-10-29 16:09:51 +0100723 sz0 += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
David Vincze401c7422019-06-21 20:44:05 +0200724 /* Guarantee that multiple sectors of the secondary slot
725 * fit into the primary slot.
726 */
727 if (smaller == 2) {
728 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible"
729 " sectors");
730 return 0;
731 }
732 smaller = 1;
733 i++;
734 } else {
David Vinczecea8b592019-10-29 16:09:51 +0100735 sz1 += boot_img_sector_size(state, BOOT_SECONDARY_SLOT, j);
David Vincze401c7422019-06-21 20:44:05 +0200736 /* Guarantee that multiple sectors of the primary slot
737 * fit into the secondary slot.
738 */
739 if (smaller == 1) {
740 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible"
741 " sectors");
742 return 0;
743 }
744 smaller = 2;
745 j++;
746 }
David Vincze4af67832019-12-11 18:31:34 +0100747#ifndef MCUBOOT_OVERWRITE_ONLY
David Vincze401c7422019-06-21 20:44:05 +0200748 if (sz0 == sz1) {
749 primary_slot_sz += sz0;
750 secondary_slot_sz += sz1;
751 /* Scratch has to fit each swap operation to the size of the larger
752 * sector among the primary slot and the secondary slot.
753 */
754 if (sz0 > scratch_sz || sz1 > scratch_sz) {
755 BOOT_LOG_WRN("Cannot upgrade: not all sectors fit inside"
756 " scratch");
757 return 0;
758 }
759 smaller = sz0 = sz1 = 0;
760 }
David Vincze4af67832019-12-11 18:31:34 +0100761#endif
David Vincze39e78552018-10-10 17:10:01 +0200762 }
763
David Vincze401c7422019-06-21 20:44:05 +0200764 if ((i != num_sectors_primary) ||
765 (j != num_sectors_secondary) ||
766 (primary_slot_sz != secondary_slot_sz)) {
767 BOOT_LOG_WRN("Cannot upgrade: slots are not compatible");
768 return 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000769 }
770
771 return 1;
772}
773
Tamas Banf70ef8c2017-12-19 15:35:09 +0000774static uint32_t
775boot_status_internal_off(int idx, int state, int elem_sz)
776{
777 int idx_sz;
778
779 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
780
David Vincze39e78552018-10-10 17:10:01 +0200781 return (idx - BOOT_STATUS_IDX_0) * idx_sz +
782 (state - BOOT_STATUS_STATE_0) * elem_sz;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000783}
784
785/**
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}
855
856/**
857 * Reads the boot status from the flash. The boot status contains
858 * the current state of an interrupted image copy operation. If the boot
859 * status is not present, or it indicates that previous copy finished,
860 * there is no operation in progress.
861 */
862static int
David Vinczecea8b592019-10-29 16:09:51 +0100863boot_read_status(struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000864{
David Vincze39e78552018-10-10 17:10:01 +0200865 memset(bs, 0, sizeof *bs);
866 bs->idx = BOOT_STATUS_IDX_0;
867 bs->state = BOOT_STATUS_STATE_0;
David Vincze401c7422019-06-21 20:44:05 +0200868 bs->swap_type = BOOT_SWAP_TYPE_NONE;
David Vincze39e78552018-10-10 17:10:01 +0200869
870#ifdef MCUBOOT_OVERWRITE_ONLY
871 /* Overwrite-only doesn't make use of the swap status area. */
872 return 0;
TTornblomfaf74f52020-03-04 17:56:27 +0100873#else
874 const struct flash_area *fap;
875 uint32_t off;
876 uint8_t swap_info;
877 int status_loc;
878 int area_id;
879 int rc;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000880
David Vinczecea8b592019-10-29 16:09:51 +0100881 status_loc = boot_status_source(state);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000882 switch (status_loc) {
883 case BOOT_STATUS_SOURCE_NONE:
884 return 0;
885
886 case BOOT_STATUS_SOURCE_SCRATCH:
887 area_id = FLASH_AREA_IMAGE_SCRATCH;
888 break;
889
David Vincze8bdfc2d2019-03-18 15:49:23 +0100890 case BOOT_STATUS_SOURCE_PRIMARY_SLOT:
David Vinczecea8b592019-10-29 16:09:51 +0100891 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000892 break;
893
894 default:
895 assert(0);
896 return BOOT_EBADARGS;
897 }
898
899 rc = flash_area_open(area_id, &fap);
900 if (rc != 0) {
901 return BOOT_EFLASH;
902 }
903
David Vinczecea8b592019-10-29 16:09:51 +0100904 rc = boot_read_status_bytes(fap, state, bs);
David Vincze401c7422019-06-21 20:44:05 +0200905 if (rc == 0) {
David Vincze91b71ef2019-06-24 13:06:47 +0200906 off = boot_swap_info_off(fap);
907 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
David Vincze401c7422019-06-21 20:44:05 +0200908 if (rc == 1) {
David Vincze91b71ef2019-06-24 13:06:47 +0200909 BOOT_SET_SWAP_INFO(swap_info, 0, BOOT_SWAP_TYPE_NONE);
David Vincze401c7422019-06-21 20:44:05 +0200910 rc = 0;
911 }
David Vincze91b71ef2019-06-24 13:06:47 +0200912
913 /* Extract the swap type info */
914 bs->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
David Vincze401c7422019-06-21 20:44:05 +0200915 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000916
917 flash_area_close(fap);
David Vincze39e78552018-10-10 17:10:01 +0200918
Tamas Banf70ef8c2017-12-19 15:35:09 +0000919 return rc;
TTornblomfaf74f52020-03-04 17:56:27 +0100920#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +0000921}
922
923/**
924 * Writes the supplied boot status to the flash file system. The boot status
925 * contains the current state of an in-progress image copy operation.
926 *
927 * @param bs The boot status to write.
928 *
929 * @return 0 on success; nonzero on failure.
930 */
931int
David Vinczecea8b592019-10-29 16:09:51 +0100932boot_write_status(struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000933{
Tamas Ban581034a2017-12-19 19:54:37 +0000934 const struct flash_area *fap = NULL;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000935 uint32_t off;
936 int area_id;
937 int rc;
938 uint8_t buf[BOOT_MAX_ALIGN];
Raef Coles204c5b42019-09-05 09:18:47 +0100939 uint32_t align;
David Vincze39e78552018-10-10 17:10:01 +0200940 uint8_t erased_val;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000941
942 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze8bdfc2d2019-03-18 15:49:23 +0100943 * the trailer. Since in the last step the primary slot is erased, the
944 * first two status writes go to the scratch which will be copied to
945 * the primary slot!
Tamas Banf70ef8c2017-12-19 15:35:09 +0000946 */
947
948 if (bs->use_scratch) {
949 /* Write to scratch. */
950 area_id = FLASH_AREA_IMAGE_SCRATCH;
951 } else {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100952 /* Write to the primary slot. */
David Vinczecea8b592019-10-29 16:09:51 +0100953 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000954 }
955
956 rc = flash_area_open(area_id, &fap);
957 if (rc != 0) {
958 rc = BOOT_EFLASH;
959 goto done;
960 }
961
962 off = boot_status_off(fap) +
David Vinczecea8b592019-10-29 16:09:51 +0100963 boot_status_internal_off(bs->idx, bs->state, BOOT_WRITE_SZ(state));
Tamas Banc3828852018-02-01 12:24:16 +0000964 align = flash_area_align(fap);
David Vincze39e78552018-10-10 17:10:01 +0200965 erased_val = flash_area_erased_val(fap);
966 memset(buf, erased_val, BOOT_MAX_ALIGN);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000967 buf[0] = bs->state;
968
969 rc = flash_area_write(fap, off, buf, align);
970 if (rc != 0) {
971 rc = BOOT_EFLASH;
972 goto done;
973 }
974
975 rc = 0;
976
977done:
978 flash_area_close(fap);
979 return rc;
980}
981
Tamas Banf70ef8c2017-12-19 15:35:09 +0000982/**
983 * Determines which swap operation to perform, if any. If it is determined
David Vincze8bdfc2d2019-03-18 15:49:23 +0100984 * that a swap operation is required, the image in the secondary slot is checked
985 * for validity. If the image in the secondary slot is invalid, it is erased,
986 * and a swap type of "none" is indicated.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000987 *
988 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
989 */
990static int
David Vinczecea8b592019-10-29 16:09:51 +0100991boot_validated_swap_type(struct boot_loader_state *state,
992 struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000993{
994 int swap_type;
David Vinczecea8b592019-10-29 16:09:51 +0100995 int rc;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000996
David Vinczecea8b592019-10-29 16:09:51 +0100997 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
998 if (BOOT_IS_UPGRADE(swap_type)) {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100999 /* Boot loader wants to switch to the secondary slot.
1000 * Ensure image is valid.
1001 */
David Vinczecea8b592019-10-29 16:09:51 +01001002 rc = boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs);
1003 if (rc == 1) {
1004 swap_type = BOOT_SWAP_TYPE_NONE;
1005 } else if (rc != 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001006 swap_type = BOOT_SWAP_TYPE_FAIL;
1007 }
1008 }
1009
1010 return swap_type;
1011}
1012
1013/**
1014 * Calculates the number of sectors the scratch area can contain. A "last"
1015 * source sector is specified because images are copied backwards in flash
1016 * (final index to index number 0).
1017 *
1018 * @param last_sector_idx The index of the last source sector
1019 * (inclusive).
1020 * @param out_first_sector_idx The index of the first source sector
1021 * (inclusive) gets written here.
1022 *
1023 * @return The number of bytes comprised by the
1024 * [first-sector, last-sector] range.
1025 */
1026#ifndef MCUBOOT_OVERWRITE_ONLY
1027static uint32_t
David Vinczecea8b592019-10-29 16:09:51 +01001028boot_copy_sz(struct boot_loader_state *state, int last_sector_idx,
1029 int *out_first_sector_idx)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001030{
1031 size_t scratch_sz;
1032 uint32_t new_sz;
1033 uint32_t sz;
1034 int i;
1035
1036 sz = 0;
1037
David Vinczecea8b592019-10-29 16:09:51 +01001038 scratch_sz = boot_scratch_area_size(state);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001039 for (i = last_sector_idx; i >= 0; i--) {
David Vinczecea8b592019-10-29 16:09:51 +01001040 new_sz = sz + boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
David Vincze401c7422019-06-21 20:44:05 +02001041 /*
1042 * The secondary slot is not being checked here, because
1043 * `boot_slots_compatible` already provides assurance that the copy size
1044 * will be compatible with the primary slot and scratch.
1045 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001046 if (new_sz > scratch_sz) {
1047 break;
1048 }
1049 sz = new_sz;
1050 }
1051
1052 /* i currently refers to a sector that doesn't fit or it is -1 because all
1053 * sectors have been processed. In both cases, exclude sector i.
1054 */
1055 *out_first_sector_idx = i + 1;
1056 return sz;
1057}
1058#endif /* !MCUBOOT_OVERWRITE_ONLY */
1059
1060/**
David Vinczef7641fa2018-09-04 18:29:46 +02001061 * Erases a region of flash.
1062 *
David Vincze401c7422019-06-21 20:44:05 +02001063 * @param flash_area The flash_area containing the region to erase.
David Vinczef7641fa2018-09-04 18:29:46 +02001064 * @param off The offset within the flash area to start the
1065 * erase.
1066 * @param sz The number of bytes to erase.
1067 *
1068 * @return 0 on success; nonzero on failure.
1069 */
David Vincze401c7422019-06-21 20:44:05 +02001070static inline int
David Vinczecea8b592019-10-29 16:09:51 +01001071boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
David Vinczef7641fa2018-09-04 18:29:46 +02001072{
David Vincze401c7422019-06-21 20:44:05 +02001073 return flash_area_erase(fap, off, sz);
David Vinczef7641fa2018-09-04 18:29:46 +02001074}
1075
1076/**
Tamas Banf70ef8c2017-12-19 15:35:09 +00001077 * Copies the contents of one flash region to another. You must erase the
1078 * destination region prior to calling this function.
1079 *
1080 * @param flash_area_id_src The ID of the source flash area.
1081 * @param flash_area_id_dst The ID of the destination flash area.
1082 * @param off_src The offset within the source flash area to
1083 * copy from.
1084 * @param off_dst The offset within the destination flash area to
1085 * copy to.
1086 * @param sz The number of bytes to copy.
1087 *
1088 * @return 0 on success; nonzero on failure.
1089 */
1090static int
David Vinczecea8b592019-10-29 16:09:51 +01001091boot_copy_region(struct boot_loader_state *state,
1092 const struct flash_area *fap_src,
David Vincze401c7422019-06-21 20:44:05 +02001093 const struct flash_area *fap_dst,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001094 uint32_t off_src, uint32_t off_dst, uint32_t sz)
1095{
Tamas Banf70ef8c2017-12-19 15:35:09 +00001096 uint32_t bytes_copied;
1097 int chunk_sz;
1098 int rc;
1099
1100 static uint8_t buf[1024];
1101
David Vinczecea8b592019-10-29 16:09:51 +01001102 (void)state;
1103
Tamas Banf70ef8c2017-12-19 15:35:09 +00001104 bytes_copied = 0;
1105 while (bytes_copied < sz) {
Tamas Ban581034a2017-12-19 19:54:37 +00001106 if (sz - bytes_copied > sizeof(buf)) {
1107 chunk_sz = sizeof(buf);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001108 } else {
1109 chunk_sz = sz - bytes_copied;
1110 }
1111
1112 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
1113 if (rc != 0) {
David Vincze401c7422019-06-21 20:44:05 +02001114 return BOOT_EFLASH;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001115 }
1116
1117 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
1118 if (rc != 0) {
David Vincze401c7422019-06-21 20:44:05 +02001119 return BOOT_EFLASH;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001120 }
1121
1122 bytes_copied += chunk_sz;
1123 }
1124
David Vincze401c7422019-06-21 20:44:05 +02001125 return 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001126}
1127
1128#ifndef MCUBOOT_OVERWRITE_ONLY
1129static inline int
David Vinczecea8b592019-10-29 16:09:51 +01001130boot_status_init(const struct boot_loader_state *state,
1131 const struct flash_area *fap,
1132 const struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001133{
Tamas Banf70ef8c2017-12-19 15:35:09 +00001134 struct boot_swap_state swap_state;
David Vinczecea8b592019-10-29 16:09:51 +01001135 uint8_t image_index;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001136 int rc;
1137
David Vinczecea8b592019-10-29 16:09:51 +01001138#if (BOOT_IMAGE_NUMBER == 1)
1139 (void)state;
1140#endif
1141
1142 image_index = BOOT_CURR_IMG(state);
1143
David Vincze401c7422019-06-21 20:44:05 +02001144 BOOT_LOG_DBG("initializing status; fa_id=%d", fap->fa_id);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001145
David Vinczecea8b592019-10-29 16:09:51 +01001146 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
1147 &swap_state);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001148 assert(rc == 0);
1149
David Vincze401c7422019-06-21 20:44:05 +02001150 if (bs->swap_type != BOOT_SWAP_TYPE_NONE) {
David Vinczecea8b592019-10-29 16:09:51 +01001151 rc = boot_write_swap_info(fap, bs->swap_type, image_index);
David Vincze401c7422019-06-21 20:44:05 +02001152 assert(rc == 0);
1153 }
1154
Tamas Banf70ef8c2017-12-19 15:35:09 +00001155 if (swap_state.image_ok == BOOT_FLAG_SET) {
1156 rc = boot_write_image_ok(fap);
1157 assert(rc == 0);
1158 }
1159
1160 rc = boot_write_swap_size(fap, bs->swap_size);
1161 assert(rc == 0);
1162
1163 rc = boot_write_magic(fap);
1164 assert(rc == 0);
1165
Tamas Banf70ef8c2017-12-19 15:35:09 +00001166 return 0;
1167}
David Vinczef7641fa2018-09-04 18:29:46 +02001168
1169static int
David Vinczecea8b592019-10-29 16:09:51 +01001170boot_erase_trailer_sectors(const struct boot_loader_state *state,
1171 const struct flash_area *fap)
David Vinczef7641fa2018-09-04 18:29:46 +02001172{
1173 uint8_t slot;
David Vincze401c7422019-06-21 20:44:05 +02001174 uint32_t sector;
1175 uint32_t trailer_sz;
1176 uint32_t total_sz;
1177 uint32_t off;
1178 uint32_t sz;
David Vinczebb207982019-08-21 15:13:04 +02001179 int fa_id_primary;
1180 int fa_id_secondary;
David Vinczecea8b592019-10-29 16:09:51 +01001181 uint8_t image_index;
David Vinczef7641fa2018-09-04 18:29:46 +02001182 int rc;
1183
David Vincze401c7422019-06-21 20:44:05 +02001184 BOOT_LOG_DBG("erasing trailer; fa_id=%d", fap->fa_id);
1185
David Vinczecea8b592019-10-29 16:09:51 +01001186 image_index = BOOT_CURR_IMG(state);
1187 fa_id_primary = flash_area_id_from_multi_image_slot(image_index,
1188 BOOT_PRIMARY_SLOT);
1189 fa_id_secondary = flash_area_id_from_multi_image_slot(image_index,
1190 BOOT_SECONDARY_SLOT);
David Vinczebb207982019-08-21 15:13:04 +02001191
1192 if (fap->fa_id == fa_id_primary) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001193 slot = BOOT_PRIMARY_SLOT;
David Vinczebb207982019-08-21 15:13:04 +02001194 } else if (fap->fa_id == fa_id_secondary) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001195 slot = BOOT_SECONDARY_SLOT;
David Vinczebb207982019-08-21 15:13:04 +02001196 } else {
David Vinczef7641fa2018-09-04 18:29:46 +02001197 return BOOT_EFLASH;
1198 }
1199
David Vincze401c7422019-06-21 20:44:05 +02001200 /* delete starting from last sector and moving to beginning */
David Vinczecea8b592019-10-29 16:09:51 +01001201 sector = boot_img_num_sectors(state, slot) - 1;
1202 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
David Vincze401c7422019-06-21 20:44:05 +02001203 total_sz = 0;
1204 do {
David Vinczecea8b592019-10-29 16:09:51 +01001205 sz = boot_img_sector_size(state, slot, sector);
1206 off = boot_img_sector_off(state, slot, sector);
1207 rc = boot_erase_region(fap, off, sz);
David Vincze401c7422019-06-21 20:44:05 +02001208 assert(rc == 0);
1209
1210 sector--;
1211 total_sz += sz;
1212 } while (total_sz < trailer_sz);
David Vinczef7641fa2018-09-04 18:29:46 +02001213
1214 return rc;
1215}
Tamas Banf70ef8c2017-12-19 15:35:09 +00001216
Tamas Banf70ef8c2017-12-19 15:35:09 +00001217/**
1218 * Swaps the contents of two flash regions within the two image slots.
1219 *
1220 * @param idx The index of the first sector in the range of
1221 * sectors being swapped.
1222 * @param sz The number of bytes to swap.
1223 * @param bs The current boot status. This struct gets
1224 * updated according to the outcome.
1225 *
1226 * @return 0 on success; nonzero on failure.
1227 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001228static void
David Vinczecea8b592019-10-29 16:09:51 +01001229boot_swap_sectors(int idx, uint32_t sz, struct boot_loader_state *state,
1230 struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001231{
David Vincze401c7422019-06-21 20:44:05 +02001232 const struct flash_area *fap_primary_slot;
1233 const struct flash_area *fap_secondary_slot;
1234 const struct flash_area *fap_scratch;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001235 uint32_t copy_sz;
1236 uint32_t trailer_sz;
1237 uint32_t img_off;
1238 uint32_t scratch_trailer_off;
1239 struct boot_swap_state swap_state;
1240 size_t last_sector;
David Vincze401c7422019-06-21 20:44:05 +02001241 bool erase_scratch;
David Vinczecea8b592019-10-29 16:09:51 +01001242 uint8_t image_index;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001243 int rc;
1244
1245 /* Calculate offset from start of image area. */
David Vinczecea8b592019-10-29 16:09:51 +01001246 img_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001247
1248 copy_sz = sz;
David Vinczecea8b592019-10-29 16:09:51 +01001249 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
Tamas Banf70ef8c2017-12-19 15:35:09 +00001250
David Vincze401c7422019-06-21 20:44:05 +02001251 /* sz in this function is always sized on a multiple of the sector size.
1252 * The check against the start offset of the last sector
Tamas Banf70ef8c2017-12-19 15:35:09 +00001253 * is to determine if we're swapping the last sector. The last sector
1254 * needs special handling because it's where the trailer lives. If we're
1255 * copying it, we need to use scratch to write the trailer temporarily.
1256 *
1257 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
1258 * controls if special handling is needed (swapping last sector).
1259 */
David Vinczecea8b592019-10-29 16:09:51 +01001260 last_sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
David Vincze7384ee72019-07-23 17:00:42 +02001261 if ((img_off + sz) >
David Vinczecea8b592019-10-29 16:09:51 +01001262 boot_img_sector_off(state, BOOT_PRIMARY_SLOT, last_sector)) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001263 copy_sz -= trailer_sz;
1264 }
1265
David Vincze39e78552018-10-10 17:10:01 +02001266 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001267
David Vinczecea8b592019-10-29 16:09:51 +01001268 image_index = BOOT_CURR_IMG(state);
1269
1270 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1271 &fap_primary_slot);
David Vincze401c7422019-06-21 20:44:05 +02001272 assert (rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001273
David Vinczecea8b592019-10-29 16:09:51 +01001274 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1275 &fap_secondary_slot);
David Vincze401c7422019-06-21 20:44:05 +02001276 assert (rc == 0);
1277
1278 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch);
1279 assert (rc == 0);
1280
1281 if (bs->state == BOOT_STATUS_STATE_0) {
1282 BOOT_LOG_DBG("erasing scratch area");
David Vinczecea8b592019-10-29 16:09:51 +01001283 rc = boot_erase_region(fap_scratch, 0, fap_scratch->fa_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001284 assert(rc == 0);
1285
David Vincze39e78552018-10-10 17:10:01 +02001286 if (bs->idx == BOOT_STATUS_IDX_0) {
David Vincze401c7422019-06-21 20:44:05 +02001287 /* Write a trailer to the scratch area, even if we don't need the
1288 * scratch area for status. We need a temporary place to store the
1289 * `swap-type` while we erase the primary trailer.
1290 */
David Vinczecea8b592019-10-29 16:09:51 +01001291 rc = boot_status_init(state, fap_scratch, bs);
David Vincze401c7422019-06-21 20:44:05 +02001292 assert(rc == 0);
1293
1294 if (!bs->use_scratch) {
1295 /* Prepare the primary status area... here it is known that the
1296 * last sector is not being used by the image data so it's safe
1297 * to erase.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001298 */
David Vinczecea8b592019-10-29 16:09:51 +01001299 rc = boot_erase_trailer_sectors(state, fap_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001300 assert(rc == 0);
1301
David Vinczecea8b592019-10-29 16:09:51 +01001302 rc = boot_status_init(state, fap_primary_slot, bs);
David Vincze401c7422019-06-21 20:44:05 +02001303 assert(rc == 0);
1304
1305 /* Erase the temporary trailer from the scratch area. */
David Vinczecea8b592019-10-29 16:09:51 +01001306 rc = boot_erase_region(fap_scratch, 0, fap_scratch->fa_size);
David Vincze401c7422019-06-21 20:44:05 +02001307 assert(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001308 }
1309 }
1310
David Vinczecea8b592019-10-29 16:09:51 +01001311 rc = boot_copy_region(state, fap_secondary_slot, fap_scratch,
David Vincze401c7422019-06-21 20:44:05 +02001312 img_off, 0, copy_sz);
1313 assert(rc == 0);
1314
David Vinczecea8b592019-10-29 16:09:51 +01001315 rc = boot_write_status(state, bs);
David Vincze39e78552018-10-10 17:10:01 +02001316 bs->state = BOOT_STATUS_STATE_1;
David Vincze39e78552018-10-10 17:10:01 +02001317 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001318 }
1319
David Vincze39e78552018-10-10 17:10:01 +02001320 if (bs->state == BOOT_STATUS_STATE_1) {
David Vinczecea8b592019-10-29 16:09:51 +01001321 rc = boot_erase_region(fap_secondary_slot, img_off, sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001322 assert(rc == 0);
1323
David Vinczecea8b592019-10-29 16:09:51 +01001324 rc = boot_copy_region(state, fap_primary_slot, fap_secondary_slot,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001325 img_off, img_off, copy_sz);
1326 assert(rc == 0);
1327
David Vincze39e78552018-10-10 17:10:01 +02001328 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001329 /* If not all sectors of the slot are being swapped,
David Vincze8bdfc2d2019-03-18 15:49:23 +01001330 * guarantee here that only the primary slot will have the state.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001331 */
David Vinczecea8b592019-10-29 16:09:51 +01001332 rc = boot_erase_trailer_sectors(state, fap_secondary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001333 assert(rc == 0);
1334 }
1335
David Vinczecea8b592019-10-29 16:09:51 +01001336 rc = boot_write_status(state, bs);
David Vincze39e78552018-10-10 17:10:01 +02001337 bs->state = BOOT_STATUS_STATE_2;
David Vincze39e78552018-10-10 17:10:01 +02001338 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001339 }
1340
David Vincze39e78552018-10-10 17:10:01 +02001341 if (bs->state == BOOT_STATUS_STATE_2) {
David Vinczecea8b592019-10-29 16:09:51 +01001342 rc = boot_erase_region(fap_primary_slot, img_off, sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001343 assert(rc == 0);
1344
David Vincze401c7422019-06-21 20:44:05 +02001345 /* NOTE: If this is the final sector, we exclude the image trailer from
1346 * this copy (copy_sz was truncated earlier).
1347 */
David Vinczecea8b592019-10-29 16:09:51 +01001348 rc = boot_copy_region(state, fap_scratch, fap_primary_slot,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001349 0, img_off, copy_sz);
1350 assert(rc == 0);
1351
1352 if (bs->use_scratch) {
David Vincze401c7422019-06-21 20:44:05 +02001353 scratch_trailer_off = boot_status_off(fap_scratch);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001354
1355 /* copy current status that is being maintained in scratch */
David Vinczecea8b592019-10-29 16:09:51 +01001356 rc = boot_copy_region(state, fap_scratch, fap_primary_slot,
David Vincze401c7422019-06-21 20:44:05 +02001357 scratch_trailer_off, img_off + copy_sz,
David Vinczecea8b592019-10-29 16:09:51 +01001358 (BOOT_STATUS_STATE_COUNT - 1) * BOOT_WRITE_SZ(state));
David Vincze39e78552018-10-10 17:10:01 +02001359 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001360
1361 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1362 &swap_state);
1363 assert(rc == 0);
1364
1365 if (swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze401c7422019-06-21 20:44:05 +02001366 rc = boot_write_image_ok(fap_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001367 assert(rc == 0);
1368 }
1369
David Vincze401c7422019-06-21 20:44:05 +02001370 if (swap_state.swap_type != BOOT_SWAP_TYPE_NONE) {
David Vincze91b71ef2019-06-24 13:06:47 +02001371 rc = boot_write_swap_info(fap_primary_slot,
1372 swap_state.swap_type,
David Vinczecea8b592019-10-29 16:09:51 +01001373 image_index);
David Vincze401c7422019-06-21 20:44:05 +02001374 assert(rc == 0);
1375 }
1376
1377 rc = boot_write_swap_size(fap_primary_slot, bs->swap_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001378 assert(rc == 0);
1379
David Vincze401c7422019-06-21 20:44:05 +02001380 rc = boot_write_magic(fap_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001381 assert(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001382 }
1383
David Vincze401c7422019-06-21 20:44:05 +02001384 /* If we wrote a trailer to the scratch area, erase it after we persist
1385 * a trailer to the primary slot. We do this to prevent mcuboot from
1386 * reading a stale status from the scratch area in case of immediate
1387 * reset.
1388 */
1389 erase_scratch = bs->use_scratch;
1390 bs->use_scratch = 0;
1391
David Vinczecea8b592019-10-29 16:09:51 +01001392 rc = boot_write_status(state, bs);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001393 bs->idx++;
David Vincze39e78552018-10-10 17:10:01 +02001394 bs->state = BOOT_STATUS_STATE_0;
David Vincze39e78552018-10-10 17:10:01 +02001395 BOOT_STATUS_ASSERT(rc == 0);
David Vincze401c7422019-06-21 20:44:05 +02001396
1397 if (erase_scratch) {
David Vinczecea8b592019-10-29 16:09:51 +01001398 rc = boot_erase_region(fap_scratch, 0, sz);
David Vincze401c7422019-06-21 20:44:05 +02001399 assert(rc == 0);
1400 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001401 }
David Vincze401c7422019-06-21 20:44:05 +02001402
1403 flash_area_close(fap_primary_slot);
1404 flash_area_close(fap_secondary_slot);
1405 flash_area_close(fap_scratch);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001406}
1407#endif /* !MCUBOOT_OVERWRITE_ONLY */
1408
1409/**
David Vincze401c7422019-06-21 20:44:05 +02001410 * Overwrite primary slot with the image contained in the secondary slot.
1411 * If a prior copy operation was interrupted by a system reset, this function
1412 * redos the copy.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001413 *
1414 * @param bs The current boot status. This function reads
1415 * this struct to determine if it is resuming
1416 * an interrupted swap operation. This
1417 * function writes the updated status to this
1418 * function on return.
1419 *
1420 * @return 0 on success; nonzero on failure.
1421 */
1422#ifdef MCUBOOT_OVERWRITE_ONLY
1423static int
David Vinczecea8b592019-10-29 16:09:51 +01001424boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001425{
1426 size_t sect_count;
1427 size_t sect;
1428 int rc;
David Vinczecea8b592019-10-29 16:09:51 +01001429 size_t size;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001430 size_t this_size;
David Vincze39e78552018-10-10 17:10:01 +02001431 size_t last_sector;
David Vincze401c7422019-06-21 20:44:05 +02001432 const struct flash_area *fap_primary_slot;
1433 const struct flash_area *fap_secondary_slot;
David Vinczecea8b592019-10-29 16:09:51 +01001434 uint8_t image_index;
David Vincze39e78552018-10-10 17:10:01 +02001435
1436 (void)bs;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001437
David Vincze8bdfc2d2019-03-18 15:49:23 +01001438 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1439 BOOT_LOG_INF("Erasing the primary slot");
Tamas Banf70ef8c2017-12-19 15:35:09 +00001440
David Vinczecea8b592019-10-29 16:09:51 +01001441 image_index = BOOT_CURR_IMG(state);
1442
1443 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1444 &fap_primary_slot);
David Vincze401c7422019-06-21 20:44:05 +02001445 assert (rc == 0);
1446
David Vinczecea8b592019-10-29 16:09:51 +01001447 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1448 &fap_secondary_slot);
David Vincze401c7422019-06-21 20:44:05 +02001449 assert (rc == 0);
1450
David Vinczecea8b592019-10-29 16:09:51 +01001451 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
1452 for (sect = 0, size = 0; sect < sect_count; sect++) {
1453 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
1454 rc = boot_erase_region(fap_primary_slot, size, this_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001455 assert(rc == 0);
1456
1457 size += this_size;
1458 }
1459
David Vincze8bdfc2d2019-03-18 15:49:23 +01001460 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1461 size);
David Vinczecea8b592019-10-29 16:09:51 +01001462 rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot,
1463 0, 0, size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001464
David Vincze060968d2019-05-23 01:13:14 +02001465 /* Update the stored security counter with the new image's security counter
David Vincze8bdfc2d2019-03-18 15:49:23 +01001466 * value. Both slots hold the new image at this point, but the secondary
1467 * slot's image header must be passed because the read image headers in the
1468 * boot_data structure have not been updated yet.
David Vincze060968d2019-05-23 01:13:14 +02001469 */
David Vinczecea8b592019-10-29 16:09:51 +01001470 rc = boot_update_security_counter(BOOT_CURR_IMG(state), BOOT_PRIMARY_SLOT,
1471 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
David Vincze060968d2019-05-23 01:13:14 +02001472 if (rc != 0) {
1473 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
1474 return rc;
1475 }
1476
David Vincze39e78552018-10-10 17:10:01 +02001477 /*
1478 * Erases header and trailer. The trailer is erased because when a new
1479 * image is written without a trailer as is the case when using newt, the
1480 * trailer that was left might trigger a new upgrade.
1481 */
David Vincze401c7422019-06-21 20:44:05 +02001482 BOOT_LOG_DBG("erasing secondary header");
David Vinczecea8b592019-10-29 16:09:51 +01001483 rc = boot_erase_region(fap_secondary_slot,
1484 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
1485 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
Tamas Banf70ef8c2017-12-19 15:35:09 +00001486 assert(rc == 0);
David Vinczecea8b592019-10-29 16:09:51 +01001487 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
David Vincze401c7422019-06-21 20:44:05 +02001488 BOOT_LOG_DBG("erasing secondary trailer");
David Vinczecea8b592019-10-29 16:09:51 +01001489 rc = boot_erase_region(fap_secondary_slot,
1490 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
1491 last_sector),
1492 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
1493 last_sector));
David Vincze39e78552018-10-10 17:10:01 +02001494 assert(rc == 0);
1495
David Vincze401c7422019-06-21 20:44:05 +02001496 flash_area_close(fap_primary_slot);
1497 flash_area_close(fap_secondary_slot);
1498
David Vincze8bdfc2d2019-03-18 15:49:23 +01001499 /* TODO: Perhaps verify the primary slot's signature again? */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001500
1501 return 0;
1502}
1503#else
David Vincze401c7422019-06-21 20:44:05 +02001504/**
1505 * Swaps the two images in flash. If a prior copy operation was interrupted
1506 * by a system reset, this function completes that operation.
1507 *
1508 * @param bs The current boot status. This function reads
1509 * this struct to determine if it is resuming
1510 * an interrupted swap operation. This
1511 * function writes the updated status to this
1512 * function on return.
1513 *
1514 * @return 0 on success; nonzero on failure.
1515 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001516static int
David Vinczecea8b592019-10-29 16:09:51 +01001517boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001518{
1519 uint32_t sz;
1520 int first_sector_idx;
1521 int last_sector_idx;
David Vincze401c7422019-06-21 20:44:05 +02001522 int last_idx_secondary_slot;
David Vincze39e78552018-10-10 17:10:01 +02001523 uint32_t swap_idx;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001524 struct image_header *hdr;
1525 uint32_t size;
1526 uint32_t copy_size;
David Vincze401c7422019-06-21 20:44:05 +02001527 uint32_t primary_slot_size;
1528 uint32_t secondary_slot_size;
David Vinczecea8b592019-10-29 16:09:51 +01001529 uint8_t image_index;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001530 int rc;
1531
1532 /* FIXME: just do this if asked by user? */
1533
1534 size = copy_size = 0;
David Vinczecea8b592019-10-29 16:09:51 +01001535 image_index = BOOT_CURR_IMG(state);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001536
David Vincze39e78552018-10-10 17:10:01 +02001537 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001538 /*
1539 * No swap ever happened, so need to find the largest image which
1540 * will be used to determine the amount of sectors to swap.
1541 */
David Vinczecea8b592019-10-29 16:09:51 +01001542 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
1543 if (hdr->ih_magic == IMAGE_MAGIC) {
1544 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
1545 assert(rc == 0);
1546 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001547
David Vinczecea8b592019-10-29 16:09:51 +01001548 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
1549 if (hdr->ih_magic == IMAGE_MAGIC) {
1550 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
1551 assert(rc == 0);
1552 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001553
1554 if (size > copy_size) {
1555 copy_size = size;
1556 }
1557
1558 bs->swap_size = copy_size;
1559 } else {
1560 /*
1561 * If a swap was under way, the swap_size should already be present
1562 * in the trailer...
1563 */
David Vinczecea8b592019-10-29 16:09:51 +01001564 rc = boot_read_swap_size(image_index, &bs->swap_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001565 assert(rc == 0);
1566
1567 copy_size = bs->swap_size;
1568 }
1569
David Vincze401c7422019-06-21 20:44:05 +02001570 primary_slot_size = 0;
1571 secondary_slot_size = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001572 last_sector_idx = 0;
David Vincze401c7422019-06-21 20:44:05 +02001573 last_idx_secondary_slot = 0;
1574
1575 /*
1576 * Knowing the size of the largest image between both slots, here we
1577 * find what is the last sector in the primary slot that needs swapping.
1578 * Since we already know that both slots are compatible, the secondary
1579 * slot's last sector is not really required after this check is finished.
1580 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001581 while (1) {
David Vincze401c7422019-06-21 20:44:05 +02001582 if ((primary_slot_size < copy_size) ||
1583 (primary_slot_size < secondary_slot_size)) {
David Vinczecea8b592019-10-29 16:09:51 +01001584 primary_slot_size += boot_img_sector_size(state,
David Vincze401c7422019-06-21 20:44:05 +02001585 BOOT_PRIMARY_SLOT,
1586 last_sector_idx);
1587 }
1588 if ((secondary_slot_size < copy_size) ||
1589 (secondary_slot_size < primary_slot_size)) {
David Vinczecea8b592019-10-29 16:09:51 +01001590 secondary_slot_size += boot_img_sector_size(state,
David Vincze401c7422019-06-21 20:44:05 +02001591 BOOT_SECONDARY_SLOT,
1592 last_idx_secondary_slot);
1593 }
1594 if (primary_slot_size >= copy_size &&
1595 secondary_slot_size >= copy_size &&
1596 primary_slot_size == secondary_slot_size) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001597 break;
1598 }
1599 last_sector_idx++;
David Vincze401c7422019-06-21 20:44:05 +02001600 last_idx_secondary_slot++;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001601 }
1602
1603 swap_idx = 0;
1604 while (last_sector_idx >= 0) {
David Vinczecea8b592019-10-29 16:09:51 +01001605 sz = boot_copy_sz(state, last_sector_idx, &first_sector_idx);
David Vincze39e78552018-10-10 17:10:01 +02001606 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
David Vinczecea8b592019-10-29 16:09:51 +01001607 boot_swap_sectors(first_sector_idx, sz, state, bs);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001608 }
1609
1610 last_sector_idx = first_sector_idx - 1;
1611 swap_idx++;
1612 }
1613
David Vincze8bdfc2d2019-03-18 15:49:23 +01001614#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
David Vincze39e78552018-10-10 17:10:01 +02001615 if (boot_status_fails > 0) {
David Vincze401c7422019-06-21 20:44:05 +02001616 BOOT_LOG_WRN("%d status write fails performing the swap",
1617 boot_status_fails);
David Vincze39e78552018-10-10 17:10:01 +02001618 }
1619#endif
1620
Tamas Banf70ef8c2017-12-19 15:35:09 +00001621 return 0;
1622}
1623#endif
1624
David Vincze44b79f42019-10-25 15:39:59 +02001625#ifndef MCUBOOT_OVERWRITE_ONLY
Tamas Banf70ef8c2017-12-19 15:35:09 +00001626/**
David Vincze8bdfc2d2019-03-18 15:49:23 +01001627 * Marks the image in the primary slot as fully copied.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001628 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001629static int
David Vinczecea8b592019-10-29 16:09:51 +01001630boot_set_copy_done(uint8_t image_index)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001631{
1632 const struct flash_area *fap;
1633 int rc;
1634
David Vinczecea8b592019-10-29 16:09:51 +01001635 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1636 &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001637 if (rc != 0) {
1638 return BOOT_EFLASH;
1639 }
1640
1641 rc = boot_write_copy_done(fap);
1642 flash_area_close(fap);
1643 return rc;
1644}
Tamas Banf70ef8c2017-12-19 15:35:09 +00001645
1646/**
David Vincze8bdfc2d2019-03-18 15:49:23 +01001647 * Marks a reverted image in the primary slot as confirmed. This is necessary to
1648 * ensure the status bytes from the image revert operation don't get processed
1649 * on a subsequent boot.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001650 *
1651 * NOTE: image_ok is tested before writing because if there's a valid permanent
David Vincze8bdfc2d2019-03-18 15:49:23 +01001652 * image installed on the primary slot and the new image to be upgrade to has a
1653 * bad sig, image_ok would be overwritten.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001654 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001655static int
David Vinczecea8b592019-10-29 16:09:51 +01001656boot_set_image_ok(uint8_t image_index)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001657{
1658 const struct flash_area *fap;
1659 struct boot_swap_state state;
1660 int rc;
1661
David Vinczecea8b592019-10-29 16:09:51 +01001662 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1663 &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001664 if (rc != 0) {
1665 return BOOT_EFLASH;
1666 }
1667
1668 rc = boot_read_swap_state(fap, &state);
1669 if (rc != 0) {
1670 rc = BOOT_EFLASH;
1671 goto out;
1672 }
1673
1674 if (state.image_ok == BOOT_FLAG_UNSET) {
1675 rc = boot_write_image_ok(fap);
1676 }
1677
1678out:
1679 flash_area_close(fap);
1680 return rc;
1681}
1682#endif /* !MCUBOOT_OVERWRITE_ONLY */
1683
David Vinczebb6e3b62019-07-31 14:24:05 +02001684#if (BOOT_IMAGE_NUMBER > 1)
1685/**
David Vinczecea8b592019-10-29 16:09:51 +01001686 * Check if the version of the image is not older than required.
1687 *
1688 * @param req Required minimal image version.
1689 * @param ver Version of the image to be checked.
1690 *
1691 * @return 0 if the version is sufficient, nonzero otherwise.
1692 */
1693static int
1694boot_is_version_sufficient(struct image_version *req,
1695 struct image_version *ver)
1696{
1697 if (ver->iv_major > req->iv_major) {
1698 return 0;
1699 }
1700 if (ver->iv_major < req->iv_major) {
1701 return BOOT_EBADVERSION;
1702 }
1703 /* The major version numbers are equal. */
1704 if (ver->iv_minor > req->iv_minor) {
1705 return 0;
1706 }
1707 if (ver->iv_minor < req->iv_minor) {
1708 return BOOT_EBADVERSION;
1709 }
1710 /* The minor version numbers are equal. */
1711 if (ver->iv_revision < req->iv_revision) {
1712 return BOOT_EBADVERSION;
1713 }
1714
1715 return 0;
1716}
1717
1718/**
David Vinczebb6e3b62019-07-31 14:24:05 +02001719 * Check the image dependency whether it is satisfied and modify
1720 * the swap type if necessary.
1721 *
1722 * @param dep Image dependency which has to be verified.
1723 *
1724 * @return 0 on success; nonzero on failure.
1725 */
1726static int
David Vinczecea8b592019-10-29 16:09:51 +01001727boot_verify_slot_dependency(struct boot_loader_state *state,
1728 struct image_dependency *dep)
David Vinczebb6e3b62019-07-31 14:24:05 +02001729{
1730 struct image_version *dep_version;
1731 size_t dep_slot;
1732 int rc;
David Vinczecea8b592019-10-29 16:09:51 +01001733 uint8_t swap_type;
David Vinczebb6e3b62019-07-31 14:24:05 +02001734
1735 /* Determine the source of the image which is the subject of
1736 * the dependency and get it's version. */
David Vinczecea8b592019-10-29 16:09:51 +01001737 swap_type = state->swap_type[dep->image_id];
1738 dep_slot = (swap_type != BOOT_SWAP_TYPE_NONE) ?
David Vinczebb6e3b62019-07-31 14:24:05 +02001739 BOOT_SECONDARY_SLOT : BOOT_PRIMARY_SLOT;
David Vinczecea8b592019-10-29 16:09:51 +01001740 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczebb6e3b62019-07-31 14:24:05 +02001741
1742 rc = boot_is_version_sufficient(&dep->image_min_version, dep_version);
1743 if (rc != 0) {
1744 /* Dependency not satisfied.
1745 * Modify the swap type to decrease the version number of the image
1746 * (which will be located in the primary slot after the boot process),
1747 * consequently the number of unsatisfied dependencies will be
1748 * decreased or remain the same.
1749 */
David Vinczecea8b592019-10-29 16:09:51 +01001750 switch (BOOT_SWAP_TYPE(state)) {
David Vinczebb6e3b62019-07-31 14:24:05 +02001751 case BOOT_SWAP_TYPE_TEST:
1752 case BOOT_SWAP_TYPE_PERM:
David Vinczecea8b592019-10-29 16:09:51 +01001753 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczebb6e3b62019-07-31 14:24:05 +02001754 break;
1755 case BOOT_SWAP_TYPE_NONE:
David Vinczecea8b592019-10-29 16:09:51 +01001756 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczebb6e3b62019-07-31 14:24:05 +02001757 break;
1758 default:
1759 break;
1760 }
1761 }
1762
1763 return rc;
1764}
1765
1766/**
1767 * Read all dependency TLVs of an image from the flash and verify
1768 * one after another to see if they are all satisfied.
1769 *
1770 * @param slot Image slot number.
1771 *
1772 * @return 0 on success; nonzero on failure.
1773 */
1774static int
David Vinczecea8b592019-10-29 16:09:51 +01001775boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
David Vinczebb6e3b62019-07-31 14:24:05 +02001776{
1777 const struct flash_area *fap;
David Vincze07706a42019-12-12 18:20:12 +01001778 struct image_tlv_iter it;
David Vinczebb6e3b62019-07-31 14:24:05 +02001779 struct image_dependency dep;
1780 uint32_t off;
David Vincze07706a42019-12-12 18:20:12 +01001781 uint16_t len;
David Vinczecea8b592019-10-29 16:09:51 +01001782 int area_id;
David Vinczebb6e3b62019-07-31 14:24:05 +02001783 int rc;
1784
David Vinczecea8b592019-10-29 16:09:51 +01001785 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
1786 rc = flash_area_open(area_id, &fap);
David Vinczebb6e3b62019-07-31 14:24:05 +02001787 if (rc != 0) {
1788 rc = BOOT_EFLASH;
1789 goto done;
1790 }
1791
David Vincze07706a42019-12-12 18:20:12 +01001792 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
1793 IMAGE_TLV_DEPENDENCY, true);
David Vinczebb6e3b62019-07-31 14:24:05 +02001794 if (rc != 0) {
David Vinczebb6e3b62019-07-31 14:24:05 +02001795 goto done;
1796 }
1797
David Vincze07706a42019-12-12 18:20:12 +01001798 while (true) {
1799 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
1800 if (rc < 0) {
1801 return -1;
1802 } else if (rc > 0) {
1803 rc = 0;
David Vinczebb6e3b62019-07-31 14:24:05 +02001804 break;
1805 }
David Vincze07706a42019-12-12 18:20:12 +01001806
1807 if (len != sizeof(dep)) {
1808 rc = BOOT_EBADIMAGE;
1809 goto done;
1810 }
1811
1812 rc = flash_area_read(fap, off, &dep, len);
1813 if (rc != 0) {
1814 rc = BOOT_EFLASH;
1815 goto done;
1816 }
1817
1818 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
1819 rc = BOOT_EBADARGS;
1820 goto done;
1821 }
1822
1823 /* Verify dependency and modify the swap type if not satisfied. */
1824 rc = boot_verify_slot_dependency(state, &dep);
1825 if (rc != 0) {
1826 /* Dependency not satisfied. */
1827 goto done;
Tamas Ban056ed0b2019-09-16 12:48:32 +01001828 }
David Vinczebb6e3b62019-07-31 14:24:05 +02001829 }
1830
1831done:
1832 flash_area_close(fap);
1833 return rc;
1834}
1835
1836/**
David Vinczebb6e3b62019-07-31 14:24:05 +02001837 * Iterate over all the images and verify whether the image dependencies in the
1838 * TLV area are all satisfied and update the related swap type if necessary.
1839 */
David Vinczecea8b592019-10-29 16:09:51 +01001840static int
1841boot_verify_dependencies(struct boot_loader_state *state)
David Vinczebb6e3b62019-07-31 14:24:05 +02001842{
David Vinczebb6e3b62019-07-31 14:24:05 +02001843 int rc;
David Vinczecea8b592019-10-29 16:09:51 +01001844 uint8_t slot;
David Vinczebb6e3b62019-07-31 14:24:05 +02001845
David Vinczecea8b592019-10-29 16:09:51 +01001846 BOOT_CURR_IMG(state) = 0;
1847 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
1848 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1849 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
1850 slot = BOOT_SECONDARY_SLOT;
1851 } else {
1852 slot = BOOT_PRIMARY_SLOT;
1853 }
1854
1855 rc = boot_verify_slot_dependencies(state, slot);
David Vinczebb6e3b62019-07-31 14:24:05 +02001856 if (rc == 0) {
1857 /* All dependencies've been satisfied, continue with next image. */
David Vinczecea8b592019-10-29 16:09:51 +01001858 BOOT_CURR_IMG(state)++;
David Vinczebb6e3b62019-07-31 14:24:05 +02001859 } else if (rc == BOOT_EBADVERSION) {
David Vinczecea8b592019-10-29 16:09:51 +01001860 /* Cannot upgrade due to non-met dependencies, so disable all
1861 * image upgrades.
1862 */
1863 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
1864 BOOT_CURR_IMG(state) = idx;
1865 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1866 }
1867 break;
David Vinczebb6e3b62019-07-31 14:24:05 +02001868 } else {
1869 /* Other error happened, images are inconsistent */
David Vinczecea8b592019-10-29 16:09:51 +01001870 return rc;
David Vinczebb6e3b62019-07-31 14:24:05 +02001871 }
1872 }
David Vinczecea8b592019-10-29 16:09:51 +01001873 return rc;
David Vinczebb6e3b62019-07-31 14:24:05 +02001874}
1875#endif /* (BOOT_IMAGE_NUMBER > 1) */
1876
Tamas Banf70ef8c2017-12-19 15:35:09 +00001877/**
David Vincze7384ee72019-07-23 17:00:42 +02001878 * Performs a clean (not aborted) image update.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001879 *
David Vincze7384ee72019-07-23 17:00:42 +02001880 * @param bs The current boot status.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001881 *
1882 * @return 0 on success; nonzero on failure.
1883 */
1884static int
David Vinczecea8b592019-10-29 16:09:51 +01001885boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001886{
Tamas Banf70ef8c2017-12-19 15:35:09 +00001887 int rc;
David Vinczecea8b592019-10-29 16:09:51 +01001888#ifndef MCUBOOT_OVERWRITE_ONLY
1889 uint8_t swap_type;
1890#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +00001891
David Vincze7384ee72019-07-23 17:00:42 +02001892 /* At this point there are no aborted swaps. */
1893#if defined(MCUBOOT_OVERWRITE_ONLY)
David Vinczecea8b592019-10-29 16:09:51 +01001894 rc = boot_copy_image(state, bs);
David Vincze7384ee72019-07-23 17:00:42 +02001895#else
David Vinczecea8b592019-10-29 16:09:51 +01001896 rc = boot_swap_image(state, bs);
David Vincze7384ee72019-07-23 17:00:42 +02001897#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +00001898 assert(rc == 0);
David Vincze7384ee72019-07-23 17:00:42 +02001899
1900#ifndef MCUBOOT_OVERWRITE_ONLY
1901 /* The following state needs image_ok be explicitly set after the
1902 * swap was finished to avoid a new revert.
1903 */
David Vinczecea8b592019-10-29 16:09:51 +01001904 swap_type = BOOT_SWAP_TYPE(state);
1905 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1906 swap_type == BOOT_SWAP_TYPE_PERM) {
1907 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02001908 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01001909 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vincze7384ee72019-07-23 17:00:42 +02001910 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001911 }
1912
David Vinczecea8b592019-10-29 16:09:51 +01001913 if (swap_type == BOOT_SWAP_TYPE_PERM) {
David Vincze7384ee72019-07-23 17:00:42 +02001914 /* Update the stored security counter with the new image's security
1915 * counter value. The primary slot holds the new image at this
1916 * point, but the secondary slot's image header must be passed
1917 * because the read image headers in the boot_data structure have
1918 * not been updated yet.
1919 *
1920 * In case of a permanent image swap mcuboot will never attempt to
1921 * revert the images on the next reboot. Therefore, the security
1922 * counter must be increased right after the image upgrade.
David Vincze401c7422019-06-21 20:44:05 +02001923 */
David Vinczecea8b592019-10-29 16:09:51 +01001924 rc = boot_update_security_counter(
1925 BOOT_CURR_IMG(state),
1926 BOOT_PRIMARY_SLOT,
1927 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
David Vincze7384ee72019-07-23 17:00:42 +02001928 if (rc != 0) {
1929 BOOT_LOG_ERR("Security counter update failed after "
1930 "image upgrade.");
David Vinczecea8b592019-10-29 16:09:51 +01001931 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001932 }
1933 }
1934
David Vinczecea8b592019-10-29 16:09:51 +01001935 if (BOOT_IS_UPGRADE(swap_type)) {
1936 rc = boot_set_copy_done(BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02001937 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01001938 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vincze7384ee72019-07-23 17:00:42 +02001939 }
1940 }
1941#endif /* !MCUBOOT_OVERWRITE_ONLY */
1942
1943 return rc;
1944}
1945
1946/**
1947 * Completes a previously aborted image swap.
1948 *
1949 * @param bs The current boot status.
1950 *
1951 * @return 0 on success; nonzero on failure.
1952 */
1953#if !defined(MCUBOOT_OVERWRITE_ONLY)
1954static int
David Vinczecea8b592019-10-29 16:09:51 +01001955boot_complete_partial_swap(struct boot_loader_state *state,
1956 struct boot_status *bs)
David Vincze7384ee72019-07-23 17:00:42 +02001957{
1958 int rc;
1959
1960 /* Determine the type of swap operation being resumed from the
1961 * `swap-type` trailer field.
1962 */
David Vinczecea8b592019-10-29 16:09:51 +01001963 rc = boot_swap_image(state, bs);
David Vincze7384ee72019-07-23 17:00:42 +02001964 assert(rc == 0);
1965
David Vinczecea8b592019-10-29 16:09:51 +01001966 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vincze7384ee72019-07-23 17:00:42 +02001967
1968 /* The following states need image_ok be explicitly set after the
1969 * swap was finished to avoid a new revert.
1970 */
1971 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1972 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
David Vinczecea8b592019-10-29 16:09:51 +01001973 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02001974 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01001975 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vincze7384ee72019-07-23 17:00:42 +02001976 }
1977 }
1978
David Vinczecea8b592019-10-29 16:09:51 +01001979 if (BOOT_IS_UPGRADE(bs->swap_type)) {
1980 rc = boot_set_copy_done(BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02001981 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01001982 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vincze7384ee72019-07-23 17:00:42 +02001983 }
1984 }
1985
David Vinczecea8b592019-10-29 16:09:51 +01001986 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vincze7384ee72019-07-23 17:00:42 +02001987 BOOT_LOG_ERR("panic!");
1988 assert(0);
1989
1990 /* Loop forever... */
1991 while (1) {}
1992 }
1993
1994 return rc;
1995}
1996#endif /* !MCUBOOT_OVERWRITE_ONLY */
1997
1998#if (BOOT_IMAGE_NUMBER > 1)
1999/**
2000 * Review the validity of previously determined swap types of other images.
2001 *
2002 * @param aborted_swap The current image upgrade is a
2003 * partial/aborted swap.
2004 */
2005static void
David Vinczecea8b592019-10-29 16:09:51 +01002006boot_review_image_swap_types(struct boot_loader_state *state,
2007 bool aborted_swap)
David Vincze7384ee72019-07-23 17:00:42 +02002008{
2009 /* In that case if we rebooted in the middle of an image upgrade process, we
2010 * must review the validity of swap types, that were previously determined
2011 * for other images. The image_ok flag had not been set before the reboot
2012 * for any of the updated images (only the copy_done flag) and thus falsely
2013 * the REVERT swap type has been determined for the previous images that had
2014 * been updated before the reboot.
2015 *
2016 * There are two separate scenarios that we have to deal with:
2017 *
2018 * 1. The reboot has happened during swapping an image:
2019 * The current image upgrade has been determined as a
2020 * partial/aborted swap.
2021 * 2. The reboot has happened between two separate image upgrades:
2022 * In this scenario we must check the swap type of the current image.
2023 * In those cases if it is NONE or REVERT we cannot certainly determine
2024 * the fact of a reboot. In a consistent state images must move in the
2025 * same direction or stay in place, e.g. in practice REVERT and TEST
2026 * swap types cannot be present at the same time. If the swap type of
2027 * the current image is either TEST, PERM or FAIL we must review the
2028 * already determined swap types of other images and set each false
2029 * REVERT swap types to NONE (these images had been successfully
2030 * updated before the system rebooted between two separate image
2031 * upgrades).
2032 */
2033
David Vinczecea8b592019-10-29 16:09:51 +01002034 if (BOOT_CURR_IMG(state) == 0) {
David Vincze7384ee72019-07-23 17:00:42 +02002035 /* Nothing to do */
2036 return;
2037 }
2038
2039 if (!aborted_swap) {
David Vinczecea8b592019-10-29 16:09:51 +01002040 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
2041 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vincze7384ee72019-07-23 17:00:42 +02002042 /* Nothing to do */
2043 return;
2044 }
2045 }
2046
David Vinczecea8b592019-10-29 16:09:51 +01002047 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
2048 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
2049 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002050 }
2051 }
2052}
2053#endif
2054
2055/**
2056 * Prepare image to be updated if required.
2057 *
2058 * Prepare image to be updated if required with completing an image swap
2059 * operation if one was aborted and/or determining the type of the
2060 * swap operation. In case of any error set the swap type to NONE.
2061 *
David Vinczecea8b592019-10-29 16:09:51 +01002062 * @param state Boot loader status information.
David Vincze7384ee72019-07-23 17:00:42 +02002063 * @param bs Pointer where the read and possibly updated
2064 * boot status can be written to.
2065 */
2066static void
David Vinczecea8b592019-10-29 16:09:51 +01002067boot_prepare_image_for_update(struct boot_loader_state *state,
2068 struct boot_status *bs)
David Vincze7384ee72019-07-23 17:00:42 +02002069{
2070 int rc;
2071
2072 /* Determine the sector layout of the image slots and scratch area. */
David Vinczecea8b592019-10-29 16:09:51 +01002073 rc = boot_read_sectors(state);
David Vincze7384ee72019-07-23 17:00:42 +02002074 if (rc != 0) {
2075 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
2076 " - too small?", BOOT_MAX_IMG_SECTORS);
2077 /* Unable to determine sector layout, continue with next image
2078 * if there is one.
2079 */
David Vinczecea8b592019-10-29 16:09:51 +01002080 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002081 return;
2082 }
2083
2084 /* Attempt to read an image header from each slot. */
David Vinczecea8b592019-10-29 16:09:51 +01002085 rc = boot_read_image_headers(state, false);
David Vincze7384ee72019-07-23 17:00:42 +02002086 if (rc != 0) {
2087 /* Continue with next image if there is one. */
David Vinczecea8b592019-10-29 16:09:51 +01002088 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
2089 BOOT_CURR_IMG(state));
2090 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002091 return;
2092 }
2093
2094 /* If the current image's slots aren't compatible, no swap is possible.
2095 * Just boot into primary slot.
2096 */
David Vinczecea8b592019-10-29 16:09:51 +01002097 if (boot_slots_compatible(state)) {
2098 rc = boot_read_status(state, bs);
David Vincze7384ee72019-07-23 17:00:42 +02002099 if (rc != 0) {
2100 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
David Vinczecea8b592019-10-29 16:09:51 +01002101 BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02002102 /* Continue with next image if there is one. */
David Vinczecea8b592019-10-29 16:09:51 +01002103 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002104 return;
2105 }
2106
2107 /* Determine if we rebooted in the middle of an image swap
2108 * operation. If a partial swap was detected, complete it.
2109 */
2110 if (bs->idx != BOOT_STATUS_IDX_0 || bs->state != BOOT_STATUS_STATE_0) {
2111
2112#if (BOOT_IMAGE_NUMBER > 1)
David Vinczecea8b592019-10-29 16:09:51 +01002113 boot_review_image_swap_types(state, true);
David Vincze7384ee72019-07-23 17:00:42 +02002114#endif
2115
2116#ifdef MCUBOOT_OVERWRITE_ONLY
2117 /* Should never arrive here, overwrite-only mode has
2118 * no swap state.
2119 */
2120 assert(0);
2121#else
2122 /* Determine the type of swap operation being resumed from the
2123 * `swap-type` trailer field.
2124 */
David Vinczecea8b592019-10-29 16:09:51 +01002125 rc = boot_complete_partial_swap(state, bs);
David Vincze7384ee72019-07-23 17:00:42 +02002126 assert(rc == 0);
2127#endif
2128 /* Attempt to read an image header from each slot. Ensure that
2129 * image headers in slots are aligned with headers in boot_data.
2130 */
David Vinczecea8b592019-10-29 16:09:51 +01002131 rc = boot_read_image_headers(state, false);
David Vincze7384ee72019-07-23 17:00:42 +02002132 assert(rc == 0);
2133
2134 /* Swap has finished set to NONE */
David Vinczecea8b592019-10-29 16:09:51 +01002135 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002136 } else {
2137 /* There was no partial swap, determine swap type. */
2138 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
David Vinczecea8b592019-10-29 16:09:51 +01002139 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
2140 } else if (boot_validate_slot(state,
2141 BOOT_SECONDARY_SLOT, bs) != 0) {
2142 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
David Vincze7384ee72019-07-23 17:00:42 +02002143 } else {
David Vinczecea8b592019-10-29 16:09:51 +01002144 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vincze7384ee72019-07-23 17:00:42 +02002145 }
2146
2147#if (BOOT_IMAGE_NUMBER > 1)
David Vinczecea8b592019-10-29 16:09:51 +01002148 boot_review_image_swap_types(state, false);
David Vincze7384ee72019-07-23 17:00:42 +02002149#endif
2150 }
2151 } else {
2152 /* In that case if slots are not compatible. */
David Vinczecea8b592019-10-29 16:09:51 +01002153 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002154 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00002155}
2156
2157/**
2158 * Prepares the booting process. This function moves images around in flash as
2159 * appropriate, and tells you what address to boot from.
2160 *
David Vinczecea8b592019-10-29 16:09:51 +01002161 * @param state Boot loader status information.
Tamas Banf70ef8c2017-12-19 15:35:09 +00002162 * @param rsp On success, indicates how booting should occur.
2163 *
2164 * @return 0 on success; nonzero on failure.
2165 */
2166int
David Vinczecea8b592019-10-29 16:09:51 +01002167context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Tamas Banf70ef8c2017-12-19 15:35:09 +00002168{
Tamas Banf70ef8c2017-12-19 15:35:09 +00002169 size_t slot;
David Vincze7384ee72019-07-23 17:00:42 +02002170 struct boot_status bs;
Tamas Ban4e5ed8d2019-09-17 09:31:11 +01002171 int rc = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002172 int fa_id;
David Vinczecea8b592019-10-29 16:09:51 +01002173 int image_index;
2174 bool has_upgrade;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002175
2176 /* The array of slot sectors are defined here (as opposed to file scope) so
2177 * that they don't get allocated for non-boot-loader apps. This is
2178 * necessary because the gcc option "-fdata-sections" doesn't seem to have
2179 * any effect in older gcc versions (e.g., 4.8.4).
2180 */
David Vincze7384ee72019-07-23 17:00:42 +02002181 static boot_sector_t
2182 primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
2183 static boot_sector_t
2184 secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
David Vincze401c7422019-06-21 20:44:05 +02002185 static boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Tamas Banf70ef8c2017-12-19 15:35:09 +00002186
David Vincze7384ee72019-07-23 17:00:42 +02002187 /* Iterate over all the images. By the end of the loop the swap type has
2188 * to be determined for each image and all aborted swaps have to be
2189 * completed.
Tamas Banf70ef8c2017-12-19 15:35:09 +00002190 */
David Vinczecea8b592019-10-29 16:09:51 +01002191 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2192
2193 image_index = BOOT_CURR_IMG(state);
2194
2195 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
2196 primary_slot_sectors[image_index];
2197 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
2198 secondary_slot_sectors[image_index];
2199 state->scratch.sectors = scratch_sectors;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002200
David Vincze7384ee72019-07-23 17:00:42 +02002201 /* Open primary and secondary image areas for the duration
2202 * of this call.
Tamas Banf70ef8c2017-12-19 15:35:09 +00002203 */
David Vincze7384ee72019-07-23 17:00:42 +02002204 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
David Vinczecea8b592019-10-29 16:09:51 +01002205 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
2206 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vincze7384ee72019-07-23 17:00:42 +02002207 assert(rc == 0);
2208 }
2209 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
David Vinczecea8b592019-10-29 16:09:51 +01002210 &BOOT_SCRATCH_AREA(state));
David Vincze7384ee72019-07-23 17:00:42 +02002211 assert(rc == 0);
2212
2213 /* Determine swap type and complete swap if it has been aborted. */
David Vinczecea8b592019-10-29 16:09:51 +01002214 boot_prepare_image_for_update(state, &bs);
2215
2216 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
2217 has_upgrade = true;
2218 }
David Vincze7384ee72019-07-23 17:00:42 +02002219 }
2220
David Vinczebb6e3b62019-07-31 14:24:05 +02002221#if (BOOT_IMAGE_NUMBER > 1)
David Vinczecea8b592019-10-29 16:09:51 +01002222 if (has_upgrade) {
2223 /* Iterate over all the images and verify whether the image dependencies
2224 * are all satisfied and update swap type if necessary.
2225 */
2226 rc = boot_verify_dependencies(state);
2227 if (rc == BOOT_EBADVERSION) {
2228 /*
2229 * It was impossible to upgrade because the expected dependency
2230 * version was not available. Here we already changed the swap_type
2231 * so that instead of asserting the bootloader, we continue and no
2232 * upgrade is performed.
2233 */
2234 rc = 0;
2235 }
2236 }
David Vinczebb6e3b62019-07-31 14:24:05 +02002237#endif
2238
David Vincze7384ee72019-07-23 17:00:42 +02002239 /* Iterate over all the images. At this point there are no aborted swaps
2240 * and the swap types are determined for each image. By the end of the loop
2241 * all required update operations will have been finished.
2242 */
David Vinczecea8b592019-10-29 16:09:51 +01002243 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vincze7384ee72019-07-23 17:00:42 +02002244
2245#if (BOOT_IMAGE_NUMBER > 1)
2246 /* Indicate that swap is not aborted */
2247 memset(&bs, 0, sizeof bs);
2248 bs.idx = BOOT_STATUS_IDX_0;
2249 bs.state = BOOT_STATUS_STATE_0;
2250#endif /* (BOOT_IMAGE_NUMBER > 1) */
2251
2252 /* Set the previously determined swap type */
David Vinczecea8b592019-10-29 16:09:51 +01002253 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vincze7384ee72019-07-23 17:00:42 +02002254
David Vinczecea8b592019-10-29 16:09:51 +01002255 switch (BOOT_SWAP_TYPE(state)) {
David Vincze7384ee72019-07-23 17:00:42 +02002256 case BOOT_SWAP_TYPE_NONE:
2257 break;
2258
2259 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
2260 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
2261 case BOOT_SWAP_TYPE_REVERT:
David Vinczecea8b592019-10-29 16:09:51 +01002262 rc = boot_perform_update(state, &bs);
David Vincze7384ee72019-07-23 17:00:42 +02002263 assert(rc == 0);
2264 break;
2265
2266 case BOOT_SWAP_TYPE_FAIL:
2267 /* The image in secondary slot was invalid and is now erased. Ensure
2268 * we don't try to boot into it again on the next reboot. Do this by
2269 * pretending we just reverted back to primary slot.
2270 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00002271#ifndef MCUBOOT_OVERWRITE_ONLY
David Vincze7384ee72019-07-23 17:00:42 +02002272 /* image_ok needs to be explicitly set to avoid a new revert. */
David Vinczecea8b592019-10-29 16:09:51 +01002273 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
Tamas Banf70ef8c2017-12-19 15:35:09 +00002274 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01002275 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002276 }
2277#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Vincze7384ee72019-07-23 17:00:42 +02002278 break;
2279
2280 default:
David Vinczecea8b592019-10-29 16:09:51 +01002281 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002282 }
David Vincze7384ee72019-07-23 17:00:42 +02002283
David Vinczecea8b592019-10-29 16:09:51 +01002284 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vincze7384ee72019-07-23 17:00:42 +02002285 BOOT_LOG_ERR("panic!");
2286 assert(0);
2287
2288 /* Loop forever... */
2289 while (1) {}
2290 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00002291 }
2292
David Vincze7384ee72019-07-23 17:00:42 +02002293 /* Iterate over all the images. At this point all required update operations
2294 * have finished. By the end of the loop each image in the primary slot will
2295 * have been re-validated.
2296 */
David Vinczecea8b592019-10-29 16:09:51 +01002297 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2298 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vincze7384ee72019-07-23 17:00:42 +02002299 /* Attempt to read an image header from each slot. Ensure that image
2300 * headers in slots are aligned with headers in boot_data.
David Vincze060968d2019-05-23 01:13:14 +02002301 */
David Vinczecea8b592019-10-29 16:09:51 +01002302 rc = boot_read_image_headers(state, false);
David Vincze060968d2019-05-23 01:13:14 +02002303 if (rc != 0) {
David Vincze7384ee72019-07-23 17:00:42 +02002304 goto out;
2305 }
2306 /* Since headers were reloaded, it can be assumed we just performed
2307 * a swap or overwrite. Now the header info that should be used to
2308 * provide the data for the bootstrap, which previously was at
2309 * secondary slot, was updated to primary slot.
2310 */
2311 }
2312
2313#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
David Vinczecea8b592019-10-29 16:09:51 +01002314 rc = boot_validate_slot(state, BOOT_PRIMARY_SLOT, NULL);
David Vincze7384ee72019-07-23 17:00:42 +02002315 if (rc != 0) {
2316 rc = BOOT_EBADIMAGE;
2317 goto out;
2318 }
2319#else
2320 /* Even if we're not re-validating the primary slot, we could be booting
2321 * onto an empty flash chip. At least do a basic sanity check that
2322 * the magic number on the image is OK.
2323 */
David Vinczecea8b592019-10-29 16:09:51 +01002324 if (!BOOT_IMG_HDR_IS_VALID(state, BOOT_PRIMARY_SLOT)) {
2325 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
2326 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_magic,
2327 BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02002328 rc = BOOT_EBADIMAGE;
2329 goto out;
2330 }
2331#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
2332
2333 /* Update the stored security counter with the active image's security
2334 * counter value. It will be updated only if the new security counter is
2335 * greater than the stored value.
2336 *
2337 * In case of a successful image swapping when the swap type is TEST the
2338 * security counter can be increased only after a reset, when the swap
2339 * type is NONE and the image has marked itself "OK" (the image_ok flag
2340 * has been set). This way a "revert" swap can be performed if it's
2341 * necessary.
2342 */
David Vinczecea8b592019-10-29 16:09:51 +01002343 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
2344 rc = boot_update_security_counter(
2345 BOOT_CURR_IMG(state),
2346 BOOT_PRIMARY_SLOT,
2347 boot_img_hdr(state, BOOT_PRIMARY_SLOT));
David Vincze7384ee72019-07-23 17:00:42 +02002348 if (rc != 0) {
2349 BOOT_LOG_ERR("Security counter update failed after image "
2350 "validation.");
David Vincze060968d2019-05-23 01:13:14 +02002351 goto out;
2352 }
2353 }
2354
David Vincze7384ee72019-07-23 17:00:42 +02002355 /* Save boot status to shared memory area */
2356#if (BOOT_IMAGE_NUMBER > 1)
David Vinczecea8b592019-10-29 16:09:51 +01002357 rc = boot_save_boot_status((BOOT_CURR_IMG(state) == 0) ?
2358 SW_SPE : SW_NSPE,
2359 boot_img_hdr(state, BOOT_PRIMARY_SLOT),
2360 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)
David Vincze7384ee72019-07-23 17:00:42 +02002361 );
David Vincze8bdfc2d2019-03-18 15:49:23 +01002362#else
David Vincze7384ee72019-07-23 17:00:42 +02002363 rc = boot_save_boot_status(SW_S_NS,
David Vinczecea8b592019-10-29 16:09:51 +01002364 boot_img_hdr(state, BOOT_PRIMARY_SLOT),
2365 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)
David Vincze7384ee72019-07-23 17:00:42 +02002366 );
2367#endif
2368 if (rc) {
2369 BOOT_LOG_ERR("Failed to add Image %u data to shared area",
David Vinczecea8b592019-10-29 16:09:51 +01002370 BOOT_CURR_IMG(state));
David Vincze060968d2019-05-23 01:13:14 +02002371 }
2372 }
2373
David Vinczecea8b592019-10-29 16:09:51 +01002374#if (BOOT_IMAGE_NUMBER > 1)
David Vincze7384ee72019-07-23 17:00:42 +02002375 /* Always boot from the primary slot of Image 0. */
David Vinczecea8b592019-10-29 16:09:51 +01002376 BOOT_CURR_IMG(state) = 0;
2377#endif
Tamas Ban0e8ab302019-01-17 11:45:31 +00002378
David Vinczecea8b592019-10-29 16:09:51 +01002379 rsp->br_flash_dev_id =
2380 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)->fa_device_id;
2381 rsp->br_image_off =
2382 boot_img_slot_off(state, BOOT_PRIMARY_SLOT);
2383 rsp->br_hdr =
2384 boot_img_hdr(state, BOOT_PRIMARY_SLOT);
2385
2386out:
2387 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2388 flash_area_close(BOOT_SCRATCH_AREA(state));
David Vincze7384ee72019-07-23 17:00:42 +02002389 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
David Vinczecea8b592019-10-29 16:09:51 +01002390 flash_area_close(BOOT_IMG_AREA(state,
David Vincze7384ee72019-07-23 17:00:42 +02002391 BOOT_NUM_SLOTS - 1 - slot));
2392 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00002393 }
2394 return rc;
2395}
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002396
Oliver Swedef9982442018-08-24 18:37:44 +01002397#else /* MCUBOOT_NO_SWAP || MCUBOOT_RAM_LOADING */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002398
David Vinczedcba70b2019-05-28 12:02:52 +02002399#define BOOT_LOG_IMAGE_INFO(area, hdr, state) \
2400 BOOT_LOG_INF("Image %u: version=%u.%u.%u+%u, magic=%5s, image_ok=0x%x", \
2401 (area), \
2402 (hdr)->ih_ver.iv_major, \
2403 (hdr)->ih_ver.iv_minor, \
2404 (hdr)->ih_ver.iv_revision, \
2405 (hdr)->ih_ver.iv_build_num, \
2406 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
2407 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
2408 "bad"), \
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002409 (state)->image_ok)
2410
2411struct image_slot_version {
2412 uint64_t version;
2413 uint32_t slot_number;
2414};
2415
2416/**
2417 * Extract the version number from the image header. This function must be
2418 * ported if version number format has changed in the image header.
2419 *
2420 * @param hdr Pointer to an image header structure
2421 *
Oliver Swedef9982442018-08-24 18:37:44 +01002422 * @return Version number casted to uint64_t
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002423 */
2424static uint64_t
2425boot_get_version_number(struct image_header *hdr)
2426{
Oliver Swedef9982442018-08-24 18:37:44 +01002427 uint64_t version = 0;
2428 version |= (uint64_t)hdr->ih_ver.iv_major << (IMAGE_VER_MINOR_LENGTH
2429 + IMAGE_VER_REVISION_LENGTH
2430 + IMAGE_VER_BUILD_NUM_LENGTH);
2431 version |= (uint64_t)hdr->ih_ver.iv_minor << (IMAGE_VER_REVISION_LENGTH
2432 + IMAGE_VER_BUILD_NUM_LENGTH);
2433 version |= (uint64_t)hdr->ih_ver.iv_revision << IMAGE_VER_BUILD_NUM_LENGTH;
2434 version |= hdr->ih_ver.iv_build_num;
2435 return version;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002436}
2437
2438/**
2439 * Comparator function for `qsort` to compare version numbers. This function
2440 * must be ported if version number format has changed in the image header.
2441 *
2442 * @param ver1 Pointer to an array element which holds the version number
2443 * @param ver2 Pointer to another array element which holds the version
2444 * number
2445 *
2446 * @return if version1 > version2 -1
2447 * if version1 == version2 0
2448 * if version1 < version2 1
2449 */
2450static int
2451boot_compare_version_numbers(const void *ver1, const void *ver2)
2452{
2453 if (((struct image_slot_version *)ver1)->version <
2454 ((struct image_slot_version *)ver2)->version) {
2455 return 1;
2456 }
2457
2458 if (((struct image_slot_version *)ver1)->version ==
2459 ((struct image_slot_version *)ver2)->version) {
2460 return 0;
2461 }
2462
2463 return -1;
2464}
2465
2466/**
2467 * Sort the available images based on the version number and puts them in
2468 * a list.
2469 *
David Vinczecea8b592019-10-29 16:09:51 +01002470 * @param state Boot loader status information.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002471 * @param boot_sequence A pointer to an array, whose aim is to carry
2472 * the boot order of candidate images.
2473 * @param slot_cnt The number of flash areas, which can contains firmware
2474 * images.
2475 *
2476 * @return The number of valid images.
2477 */
2478uint32_t
David Vinczecea8b592019-10-29 16:09:51 +01002479boot_get_boot_sequence(struct boot_loader_state *state,
2480 uint32_t *boot_sequence, uint32_t slot_cnt)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002481{
2482 struct boot_swap_state slot_state;
2483 struct image_header *hdr;
2484 struct image_slot_version image_versions[BOOT_NUM_SLOTS] = {{0}};
2485 uint32_t image_cnt = 0;
2486 uint32_t slot;
2487 int32_t rc;
2488 int32_t fa_id;
2489
2490 for (slot = 0; slot < slot_cnt; slot++) {
David Vinczecea8b592019-10-29 16:09:51 +01002491 hdr = boot_img_hdr(state, slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002492 fa_id = flash_area_id_from_image_slot(slot);
2493 rc = boot_read_swap_state_by_id(fa_id, &slot_state);
2494 if (rc != 0) {
David Vinczedcba70b2019-05-28 12:02:52 +02002495 BOOT_LOG_ERR("Error during reading image trailer from slot: %u",
2496 slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002497 continue;
2498 }
2499
David Vinczecea8b592019-10-29 16:09:51 +01002500 if (BOOT_IMG_HDR_IS_VALID(state, slot)) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002501 if (slot_state.magic == BOOT_MAGIC_GOOD ||
David Vincze39e78552018-10-10 17:10:01 +02002502 slot_state.image_ok == BOOT_FLAG_SET) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002503 /* Valid cases:
2504 * - Test mode: magic is OK in image trailer
2505 * - Permanent mode: image_ok flag has previously set
2506 */
2507 image_versions[slot].slot_number = slot;
2508 image_versions[slot].version = boot_get_version_number(hdr);
2509 image_cnt++;
2510 }
2511
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002512 BOOT_LOG_IMAGE_INFO(slot, hdr, &slot_state);
2513 } else {
David Vinczedcba70b2019-05-28 12:02:52 +02002514 BOOT_LOG_INF("Image %u: No valid image", slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002515 }
2516 }
2517
2518 /* Sort the images based on version number */
2519 qsort(&image_versions[0],
2520 slot_cnt,
2521 sizeof(struct image_slot_version),
2522 boot_compare_version_numbers);
2523
2524 /* Copy the calculated boot sequence to boot_sequence array */
2525 for (slot = 0; slot < slot_cnt; slot++) {
2526 boot_sequence[slot] = image_versions[slot].slot_number;
2527 }
2528
2529 return image_cnt;
2530}
2531
Oliver Swedef9982442018-08-24 18:37:44 +01002532#ifdef MCUBOOT_RAM_LOADING
Raef Colesaf082382019-10-01 11:10:33 +01002533
2534/**
2535 * Verifies that the image in a slot lies within the predefined bounds that are
2536 * allowed to be used by executable images.
2537 *
2538 * @param img_dst The address to which the image is going to be copied.
2539 *
2540 * @param img_sz The size of the image.
2541 *
2542 * @return 0 on success; nonzero on failure.
2543 */
2544static int
2545boot_verify_ram_loading_address(uint32_t img_dst, uint32_t img_sz)
2546{
David Vincze0dc41d22019-10-28 14:56:29 +01002547 uint32_t img_end_addr;
2548
Raef Colesaf082382019-10-01 11:10:33 +01002549 if (img_dst < IMAGE_EXECUTABLE_RAM_START) {
2550 return BOOT_EBADIMAGE;
2551 }
2552
David Vincze0dc41d22019-10-28 14:56:29 +01002553 if (!boot_u32_safe_add(&img_end_addr, img_dst, img_sz)) {
Raef Colesaf082382019-10-01 11:10:33 +01002554 return BOOT_EBADIMAGE;
2555 }
2556
David Vincze0dc41d22019-10-28 14:56:29 +01002557 if (img_end_addr > (IMAGE_EXECUTABLE_RAM_START +
2558 IMAGE_EXECUTABLE_RAM_SIZE)) {
Raef Colesaf082382019-10-01 11:10:33 +01002559 return BOOT_EBADIMAGE;
2560 }
2561
2562 return 0;
2563}
2564
Oliver Swedef9982442018-08-24 18:37:44 +01002565/**
2566 * Copies an image from a slot in the flash to an SRAM address, where the load
2567 * address has already been inserted into the image header by this point and is
2568 * extracted from it within this method. The copying is done sector-by-sector.
2569 *
David Vinczecea8b592019-10-29 16:09:51 +01002570 * @param state Boot loader status information.
Oliver Swedef9982442018-08-24 18:37:44 +01002571 * @param slot The flash slot of the image to be copied to SRAM.
2572 *
2573 * @param hdr Pointer to the image header structure of the image
Raef Colesaf082382019-10-01 11:10:33 +01002574 *
2575 * @param img_dst The address at which the image needs to be copied to
2576 * SRAM.
2577 *
2578 * @param img_sz The size of the image that needs to be copied to SRAM.
Oliver Swedef9982442018-08-24 18:37:44 +01002579 *
2580 * @return 0 on success; nonzero on failure.
2581 */
2582static int
David Vinczecea8b592019-10-29 16:09:51 +01002583boot_copy_image_to_sram(struct boot_loader_state *state, int slot,
2584 struct image_header *hdr,
Raef Colesaf082382019-10-01 11:10:33 +01002585 uint32_t img_dst, uint32_t img_sz)
Oliver Swedef9982442018-08-24 18:37:44 +01002586{
2587 int rc;
2588 uint32_t sect_sz;
2589 uint32_t sect = 0;
2590 uint32_t bytes_copied = 0;
2591 const struct flash_area *fap_src = NULL;
Oliver Swedef9982442018-08-24 18:37:44 +01002592
Raef Colesaf082382019-10-01 11:10:33 +01002593 if (img_dst % 4 != 0) {
Tamas Banc27b5c32019-05-28 16:30:19 +01002594 BOOT_LOG_INF("Cannot copy the image to the SRAM address 0x%x "
Oliver Swedef9982442018-08-24 18:37:44 +01002595 "- the load address must be aligned with 4 bytes due to SRAM "
Raef Colesaf082382019-10-01 11:10:33 +01002596 "restrictions", img_dst);
Oliver Swedef9982442018-08-24 18:37:44 +01002597 return BOOT_EBADARGS;
2598 }
2599
2600 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap_src);
2601 if (rc != 0) {
2602 return BOOT_EFLASH;
2603 }
2604
Oliver Swedef9982442018-08-24 18:37:44 +01002605 while (bytes_copied < img_sz) {
David Vinczecea8b592019-10-29 16:09:51 +01002606 sect_sz = boot_img_sector_size(state, slot, sect);
Oliver Swedef9982442018-08-24 18:37:44 +01002607 /*
2608 * Direct copy from where the image sector resides in flash to its new
2609 * location in SRAM
2610 */
2611 rc = flash_area_read(fap_src,
2612 bytes_copied,
Raef Colesaf082382019-10-01 11:10:33 +01002613 (void *)(img_dst + bytes_copied),
Oliver Swedef9982442018-08-24 18:37:44 +01002614 sect_sz);
2615 if (rc != 0) {
2616 BOOT_LOG_INF("Error whilst copying image from Flash to SRAM");
2617 break;
2618 } else {
2619 bytes_copied += sect_sz;
2620 }
2621 sect++;
2622 }
2623
2624 if (fap_src) {
2625 flash_area_close(fap_src);
2626 }
2627 return rc;
2628}
Raef Coles27a61452019-09-25 15:32:25 +01002629
2630/**
2631 * Removes an image from SRAM, by overwriting it with zeros.
2632 *
Raef Colesaf082382019-10-01 11:10:33 +01002633 * @param img_dst The address of the image that needs to be removed from
2634 * SRAM.
Raef Coles27a61452019-09-25 15:32:25 +01002635 *
Raef Colesaf082382019-10-01 11:10:33 +01002636 * @param img_sz The size of the image that needs to be removed from
2637 * SRAM.
Raef Coles27a61452019-09-25 15:32:25 +01002638 *
2639 * @return 0 on success; nonzero on failure.
2640 */
2641static int
Raef Colesaf082382019-10-01 11:10:33 +01002642boot_remove_image_from_sram(uint32_t img_dst, uint32_t img_sz)
Raef Coles27a61452019-09-25 15:32:25 +01002643{
Raef Colesaf082382019-10-01 11:10:33 +01002644 BOOT_LOG_INF("Removing image from SRAM at address 0x%x", img_dst);
2645 memset((void*)img_dst, 0, img_sz);
Raef Coles27a61452019-09-25 15:32:25 +01002646
2647 return 0;
2648}
Oliver Swedef9982442018-08-24 18:37:44 +01002649#endif /* MCUBOOT_RAM_LOADING */
2650
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002651/**
2652 * Prepares the booting process. This function choose the newer image in flash
David Vinczecea8b592019-10-29 16:09:51 +01002653 * as appropriate, and tells you what address to boot from.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002654 *
David Vinczecea8b592019-10-29 16:09:51 +01002655 * @param state Boot loader status information.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002656 * @param rsp On success, indicates how booting should occur.
2657 *
2658 * @return 0 on success; nonzero on failure.
2659 */
2660int
David Vinczecea8b592019-10-29 16:09:51 +01002661context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002662{
2663 size_t slot = 0;
2664 int32_t i;
2665 int rc;
2666 int fa_id;
2667 uint32_t boot_sequence[BOOT_NUM_SLOTS];
2668 uint32_t img_cnt;
Raef Coles27a61452019-09-25 15:32:25 +01002669 struct image_header *selected_image_header;
2670#ifdef MCUBOOT_RAM_LOADING
2671 int image_copied = 0;
Raef Colesaf082382019-10-01 11:10:33 +01002672 uint32_t img_dst = 0;
2673 uint32_t img_sz = 0;
Raef Coles27a61452019-09-25 15:32:25 +01002674#endif /* MCUBOOT_RAM_LOADING */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002675
David Vincze8bdfc2d2019-03-18 15:49:23 +01002676 static boot_sector_t primary_slot_sectors[BOOT_MAX_IMG_SECTORS];
2677 static boot_sector_t secondary_slot_sectors[BOOT_MAX_IMG_SECTORS];
Raef Coles35ff8162019-12-12 13:43:15 +00002678 static boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002679
Raef Coles35ff8162019-12-12 13:43:15 +00002680 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors = primary_slot_sectors;
2681 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors = secondary_slot_sectors;
2682 state->scratch.sectors = scratch_sectors;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002683
2684 /* Open boot_data image areas for the duration of this call. */
2685 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
2686 fa_id = flash_area_id_from_image_slot(i);
David Vinczecea8b592019-10-29 16:09:51 +01002687 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, i));
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002688 assert(rc == 0);
2689 }
2690
2691 /* Determine the sector layout of the image slots. */
David Vinczecea8b592019-10-29 16:09:51 +01002692 rc = boot_read_sectors(state);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002693 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01002694 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - "
2695 "too small?", BOOT_MAX_IMG_SECTORS);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002696 goto out;
2697 }
2698
2699 /* Attempt to read an image header from each slot. */
David Vinczecea8b592019-10-29 16:09:51 +01002700 rc = boot_read_image_headers(state, false);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002701 if (rc != 0) {
2702 goto out;
2703 }
2704
David Vinczecea8b592019-10-29 16:09:51 +01002705 img_cnt = boot_get_boot_sequence(state, boot_sequence, BOOT_NUM_SLOTS);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002706 if (img_cnt) {
2707 /* Authenticate images */
2708 for (i = 0; i < img_cnt; i++) {
Raef Coles27a61452019-09-25 15:32:25 +01002709
2710 slot = boot_sequence[i];
David Vinczecea8b592019-10-29 16:09:51 +01002711 selected_image_header = boot_img_hdr(state, slot);
Raef Coles27a61452019-09-25 15:32:25 +01002712
2713#ifdef MCUBOOT_RAM_LOADING
2714 if (selected_image_header->ih_flags & IMAGE_F_RAM_LOAD) {
Raef Colesaf082382019-10-01 11:10:33 +01002715
2716 img_dst = selected_image_header->ih_load_addr;
2717
David Vinczecea8b592019-10-29 16:09:51 +01002718 rc = boot_read_image_size(state, slot, &img_sz);
Raef Colesaf082382019-10-01 11:10:33 +01002719 if (rc != 0) {
2720 rc = BOOT_EFLASH;
2721 BOOT_LOG_INF("Could not load image headers from the image"
2722 "in the %s slot.",
2723 (slot == BOOT_PRIMARY_SLOT) ?
2724 "primary" : "secondary");
2725 continue;
2726 }
2727
2728 rc = boot_verify_ram_loading_address(img_dst, img_sz);
2729 if (rc != 0) {
2730 BOOT_LOG_INF("Could not copy image from the %s slot in "
2731 "the Flash to load address 0x%x in SRAM as"
2732 " the image would overlap memory outside"
2733 " the defined executable region.",
2734 (slot == BOOT_PRIMARY_SLOT) ?
2735 "primary" : "secondary",
2736 selected_image_header->ih_load_addr);
2737 continue;
2738 }
2739
Raef Coles27a61452019-09-25 15:32:25 +01002740 /* Copy image to the load address from where it
2741 * currently resides in flash
2742 */
David Vinczecea8b592019-10-29 16:09:51 +01002743 rc = boot_copy_image_to_sram(state, slot, selected_image_header,
Raef Colesaf082382019-10-01 11:10:33 +01002744 img_dst, img_sz);
Raef Coles27a61452019-09-25 15:32:25 +01002745 if (rc != 0) {
2746 rc = BOOT_EBADIMAGE;
2747 BOOT_LOG_INF("Could not copy image from the %s slot in "
2748 "the Flash to load address 0x%x in SRAM, "
2749 "aborting..", (slot == BOOT_PRIMARY_SLOT) ?
2750 "primary" : "secondary",
2751 selected_image_header->ih_load_addr);
2752 continue;
2753 } else {
2754 BOOT_LOG_INF("Image has been copied from the %s slot in "
2755 "the flash to SRAM address 0x%x",
2756 (slot == BOOT_PRIMARY_SLOT) ?
2757 "primary" : "secondary",
2758 selected_image_header->ih_load_addr);
2759 image_copied = 1;
2760 }
2761 } else {
2762 /* Only images that support IMAGE_F_RAM_LOAD are allowed if
2763 * MCUBOOT_RAM_LOADING is set.
2764 */
2765 rc = BOOT_EBADIMAGE;
2766 continue;
2767 }
2768#endif /* MCUBOOT_RAM_LOADING */
David Vinczecea8b592019-10-29 16:09:51 +01002769 rc = boot_validate_slot(state, slot, NULL);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002770 if (rc == 0) {
Raef Coles27a61452019-09-25 15:32:25 +01002771 /* If a valid image is found then there is no reason to check
2772 * the rest of the images, as they were already ordered by
2773 * preference.
2774 */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002775 break;
2776 }
Raef Coles27a61452019-09-25 15:32:25 +01002777#ifdef MCUBOOT_RAM_LOADING
2778 else if (image_copied) {
2779 /* If an image is found to be invalid then it is removed from
2780 * RAM to prevent it being a shellcode vector.
2781 */
Raef Colesaf082382019-10-01 11:10:33 +01002782 boot_remove_image_from_sram(img_dst, img_sz);
Raef Coles27a61452019-09-25 15:32:25 +01002783 image_copied = 0;
2784 }
2785#endif /* MCUBOOT_RAM_LOADING */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002786 }
2787 if (rc) {
2788 /* If there was no valid image at all */
2789 rc = BOOT_EBADIMAGE;
2790 goto out;
2791 }
2792
David Vincze060968d2019-05-23 01:13:14 +02002793 /* Update the security counter with the newest image's security
2794 * counter value.
2795 */
David Vinczecea8b592019-10-29 16:09:51 +01002796 rc = boot_update_security_counter(BOOT_CURR_IMG(state), slot,
2797 selected_image_header);
David Vincze060968d2019-05-23 01:13:14 +02002798 if (rc != 0) {
2799 BOOT_LOG_ERR("Security counter update failed after image "
2800 "validation.");
2801 goto out;
2802 }
2803
Oliver Swedef9982442018-08-24 18:37:44 +01002804
David Vincze8a2a4e22019-05-24 10:14:23 +02002805#ifdef MCUBOOT_RAM_LOADING
Raef Coles27a61452019-09-25 15:32:25 +01002806 BOOT_LOG_INF("Booting image from SRAM at address 0x%x",
2807 selected_image_header->ih_load_addr);
2808#else
2809 BOOT_LOG_INF("Booting image from the %s slot",
2810 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
2811#endif /* MCUBOOT_RAM_LOADING */
Oliver Swedef9982442018-08-24 18:37:44 +01002812
Raef Coles27a61452019-09-25 15:32:25 +01002813 rsp->br_hdr = selected_image_header;
David Vinczecea8b592019-10-29 16:09:51 +01002814 rsp->br_image_off = boot_img_slot_off(state, slot);
2815 rsp->br_flash_dev_id = BOOT_IMG_AREA(state, slot)->fa_device_id;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002816 } else {
2817 /* No candidate image available */
2818 rc = BOOT_EBADIMAGE;
David Vincze060968d2019-05-23 01:13:14 +02002819 goto out;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002820 }
2821
Tamas Ban0e8ab302019-01-17 11:45:31 +00002822 /* Save boot status to shared memory area */
2823 rc = boot_save_boot_status(SW_S_NS,
2824 rsp->br_hdr,
David Vinczecea8b592019-10-29 16:09:51 +01002825 BOOT_IMG_AREA(state, slot));
Tamas Ban0e8ab302019-01-17 11:45:31 +00002826 if (rc) {
2827 BOOT_LOG_ERR("Failed to add data to shared area");
2828 }
2829
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002830out:
2831 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
David Vinczecea8b592019-10-29 16:09:51 +01002832 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002833 }
2834 return rc;
2835}
Oliver Swedef9982442018-08-24 18:37:44 +01002836#endif /* MCUBOOT_NO_SWAP || MCUBOOT_RAM_LOADING */
David Vinczecea8b592019-10-29 16:09:51 +01002837
2838int
2839boot_go(struct boot_rsp *rsp)
2840{
2841 return context_boot_go(&boot_data, rsp);
Robert Rostohar4b630372019-12-23 13:24:50 +01002842}