blob: 6e202aa1d1dcffd517fb44f038c714946f9ef5fd [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
David Brownaac71112020-02-03 16:13:42 -07002 * SPDX-License-Identifier: Apache-2.0
3 *
4 * Copyright (c) 2016-2020 Linaro LTD
5 * Copyright (c) 2016-2019 JUUL Labs
Antonio de Angelisba5fb1c2022-10-11 10:10:37 +01006 * Copyright (c) 2019-2023 Arm Limited
Dominik Ermel6f7f8732024-02-01 11:59:24 +00007 * Copyright (c) 2024 Nordic Semiconductor ASA
David Brownaac71112020-02-03 16:13:42 -07008 *
9 * Original license:
10 *
Christopher Collins92ea77f2016-12-12 15:59:26 -080011 * Licensed to the Apache Software Foundation (ASF) under one
12 * or more contributor license agreements. See the NOTICE file
13 * distributed with this work for additional information
14 * regarding copyright ownership. The ASF licenses this file
15 * to you under the Apache License, Version 2.0 (the
16 * "License"); you may not use this file except in compliance
17 * with the License. You may obtain a copy of the License at
18 *
19 * http://www.apache.org/licenses/LICENSE-2.0
20 *
21 * Unless required by applicable law or agreed to in writing,
22 * software distributed under the License is distributed on an
23 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
24 * KIND, either express or implied. See the License for the
25 * specific language governing permissions and limitations
26 * under the License.
27 */
28
29/**
30 * This file provides an interface to the boot loader. Functions defined in
31 * this file should only be called while the boot loader is running.
32 */
33
Christopher Collins92ea77f2016-12-12 15:59:26 -080034#include <stddef.h>
David Brown52eee562017-07-05 11:25:09 -060035#include <stdbool.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080036#include <inttypes.h>
37#include <stdlib.h>
38#include <string.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080039#include "bootutil/bootutil.h"
Kristine Jassmann73c38c62021-02-03 16:56:14 +000040#include "bootutil/bootutil_public.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080041#include "bootutil/image.h"
42#include "bootutil_priv.h"
Fabio Utzig12d59162019-11-28 10:01:59 -030043#include "swap_priv.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050044#include "bootutil/bootutil_log.h"
David Vinczec3084132020-02-18 14:50:47 +010045#include "bootutil/security_cnt.h"
David Vincze1cf11b52020-03-24 07:51:09 +010046#include "bootutil/boot_record.h"
Raef Colese8fe6cf2020-05-26 13:07:40 +010047#include "bootutil/fault_injection_hardening.h"
Mark Horvathccaf7f82021-01-04 18:16:42 +010048#include "bootutil/ramload.h"
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +020049#include "bootutil/boot_hooks.h"
Jamie McCrae56cb6102022-03-23 11:57:03 +000050#include "bootutil/mcuboot_status.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050051
Fabio Utzigba829042018-09-18 08:29:34 -030052#ifdef MCUBOOT_ENC_IMAGES
53#include "bootutil/enc_key.h"
54#endif
55
Carlos Falgueras García391b1972021-06-21 16:58:07 +020056#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
57#include <os/os_malloc.h>
58#endif
59
Fabio Utzigba1fbe62017-07-21 14:01:20 -030060#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030061
Carlos Falgueras Garcíaa4b4b0f2021-06-22 10:00:22 +020062BOOT_LOG_MODULE_DECLARE(mcuboot);
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010063
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040064static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080065
Fabio Utzigabec0732019-07-31 08:40:22 -030066#if (BOOT_IMAGE_NUMBER > 1)
67#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
68#else
69#define IMAGES_ITER(x)
70#endif
71
Fabio Utzig10ee6482019-08-01 12:04:52 -030072/*
73 * This macro allows some control on the allocation of local variables.
74 * When running natively on a target, we don't want to allocated huge
75 * variables on the stack, so make them global instead. For the simulator
76 * we want to run as many threads as there are tests, and it's safer
77 * to just make those variables stack allocated.
78 */
79#if !defined(__BOOTSIM__)
80#define TARGET_STATIC static
81#else
82#define TARGET_STATIC
83#endif
84
Kristine Jassmann73c38c62021-02-03 16:56:14 +000085#if BOOT_MAX_ALIGN > 1024
86#define BUF_SZ BOOT_MAX_ALIGN
87#else
88#define BUF_SZ 1024
89#endif
90
David Vinczee574f2d2020-07-10 11:42:03 +020091static int
92boot_read_image_headers(struct boot_loader_state *state, bool require_all,
93 struct boot_status *bs)
94{
95 int rc;
96 int i;
97
98 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +020099 rc = BOOT_HOOK_CALL(boot_read_image_header_hook, BOOT_HOOK_REGULAR,
100 BOOT_CURR_IMG(state), i, boot_img_hdr(state, i));
101 if (rc == BOOT_HOOK_REGULAR)
102 {
103 rc = boot_read_image_header(state, i, boot_img_hdr(state, i), bs);
104 }
David Vinczee574f2d2020-07-10 11:42:03 +0200105 if (rc != 0) {
106 /* If `require_all` is set, fail on any single fail, otherwise
107 * if at least the first slot's header was read successfully,
108 * then the boot loader can attempt a boot.
109 *
110 * Failure to read any headers is a fatal error.
111 */
112 if (i > 0 && !require_all) {
113 return 0;
114 } else {
115 return rc;
116 }
117 }
118 }
119
120 return 0;
121}
122
Mark Horvathccaf7f82021-01-04 18:16:42 +0100123/**
124 * Saves boot status and shared data for current image.
125 *
126 * @param state Boot loader status information.
127 * @param active_slot Index of the slot will be loaded for current image.
128 *
129 * @return 0 on success; nonzero on failure.
130 */
131static int
132boot_add_shared_data(struct boot_loader_state *state,
Jamie McCrae3016d002023-03-14 12:35:51 +0000133 uint8_t active_slot)
Mark Horvathccaf7f82021-01-04 18:16:42 +0100134{
135#if defined(MCUBOOT_MEASURED_BOOT) || defined(MCUBOOT_DATA_SHARING)
136 int rc;
137
Jamie McCrae3016d002023-03-14 12:35:51 +0000138#ifdef MCUBOOT_DATA_SHARING
139 int max_app_size;
140#endif
141
Mark Horvathccaf7f82021-01-04 18:16:42 +0100142#ifdef MCUBOOT_MEASURED_BOOT
143 rc = boot_save_boot_status(BOOT_CURR_IMG(state),
144 boot_img_hdr(state, active_slot),
145 BOOT_IMG_AREA(state, active_slot));
146 if (rc != 0) {
147 BOOT_LOG_ERR("Failed to add image data to shared area");
148 return rc;
149 }
150#endif /* MCUBOOT_MEASURED_BOOT */
151
152#ifdef MCUBOOT_DATA_SHARING
Jamie McCrae3016d002023-03-14 12:35:51 +0000153 max_app_size = app_max_size(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +0100154 rc = boot_save_shared_data(boot_img_hdr(state, active_slot),
Jamie McCrae3016d002023-03-14 12:35:51 +0000155 BOOT_IMG_AREA(state, active_slot),
156 active_slot, max_app_size);
Mark Horvathccaf7f82021-01-04 18:16:42 +0100157 if (rc != 0) {
158 BOOT_LOG_ERR("Failed to add data to shared memory area.");
159 return rc;
160 }
161#endif /* MCUBOOT_DATA_SHARING */
162
163 return 0;
164
165#else /* MCUBOOT_MEASURED_BOOT || MCUBOOT_DATA_SHARING */
166 (void) (state);
167 (void) (active_slot);
168
169 return 0;
170#endif
171}
172
173/**
174 * Fills rsp to indicate how booting should occur.
175 *
176 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +0100177 * @param rsp boot_rsp struct to fill.
178 */
179static void
Raef Colesfe57e7d2021-10-15 11:07:09 +0100180fill_rsp(struct boot_loader_state *state, struct boot_rsp *rsp)
Mark Horvathccaf7f82021-01-04 18:16:42 +0100181{
182 uint32_t active_slot;
183
184#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesf11de642021-10-15 11:11:56 +0100185 /* Always boot from the first enabled image. */
Mark Horvathccaf7f82021-01-04 18:16:42 +0100186 BOOT_CURR_IMG(state) = 0;
Raef Colesf11de642021-10-15 11:11:56 +0100187 IMAGES_ITER(BOOT_CURR_IMG(state)) {
188 if (!state->img_mask[BOOT_CURR_IMG(state)]) {
189 break;
190 }
191 }
INFINEON\DovhalA94360d52023-01-18 17:21:56 +0200192 /* At least one image must be active, otherwise skip the execution */
193 if (BOOT_CURR_IMG(state) >= BOOT_IMAGE_NUMBER) {
194 return;
195 }
Mark Horvathccaf7f82021-01-04 18:16:42 +0100196#endif
197
198#if defined(MCUBOOT_DIRECT_XIP) || defined(MCUBOOT_RAM_LOAD)
Raef Colesfe57e7d2021-10-15 11:07:09 +0100199 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +0100200#else
Mark Horvathccaf7f82021-01-04 18:16:42 +0100201 active_slot = BOOT_PRIMARY_SLOT;
202#endif
203
Dominik Ermel260ae092021-04-23 05:38:45 +0000204 rsp->br_flash_dev_id = flash_area_get_device_id(BOOT_IMG_AREA(state, active_slot));
Mark Horvathccaf7f82021-01-04 18:16:42 +0100205 rsp->br_image_off = boot_img_slot_off(state, active_slot);
206 rsp->br_hdr = boot_img_hdr(state, active_slot);
207}
208
209/**
210 * Closes all flash areas.
211 *
212 * @param state Boot loader status information.
213 */
214static void
215close_all_flash_areas(struct boot_loader_state *state)
216{
217 uint32_t slot;
218
219 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +0100220#if BOOT_IMAGE_NUMBER > 1
221 if (state->img_mask[BOOT_CURR_IMG(state)]) {
222 continue;
223 }
224#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +0100225#if MCUBOOT_SWAP_USING_SCRATCH
226 flash_area_close(BOOT_SCRATCH_AREA(state));
227#endif
228 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
229 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
230 }
231 }
232}
233
Tamas Banfe031092020-09-10 17:32:39 +0200234#if !defined(MCUBOOT_DIRECT_XIP)
David Brownf5b33d82017-09-01 10:58:27 -0600235/*
236 * Compute the total size of the given image. Includes the size of
237 * the TLVs.
238 */
Tamas Banfe031092020-09-10 17:32:39 +0200239#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600240static int
Fabio Utzigd638b172019-08-09 10:38:05 -0300241boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
David Brownf5b33d82017-09-01 10:58:27 -0600242{
243 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300244 struct image_tlv_info info;
Fabio Utzig233af7d2019-08-26 12:06:16 -0300245 uint32_t off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300246 uint32_t protect_tlv_size;
David Brownf5b33d82017-09-01 10:58:27 -0600247 int area_id;
248 int rc;
249
Fabio Utzig10ee6482019-08-01 12:04:52 -0300250#if (BOOT_IMAGE_NUMBER == 1)
251 (void)state;
252#endif
253
254 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
David Brownf5b33d82017-09-01 10:58:27 -0600255 rc = flash_area_open(area_id, &fap);
256 if (rc != 0) {
257 rc = BOOT_EFLASH;
258 goto done;
259 }
260
Fabio Utzig61fd8882019-09-14 20:00:20 -0300261 off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
262
263 if (flash_area_read(fap, off, &info, sizeof(info))) {
264 rc = BOOT_EFLASH;
David Brownf5b33d82017-09-01 10:58:27 -0600265 goto done;
266 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300267
Fabio Utzige52c08e2019-09-11 19:32:00 -0300268 protect_tlv_size = boot_img_hdr(state, slot)->ih_protect_tlv_size;
269 if (info.it_magic == IMAGE_TLV_PROT_INFO_MAGIC) {
270 if (protect_tlv_size != info.it_tlv_tot) {
271 rc = BOOT_EBADIMAGE;
272 goto done;
273 }
274
275 if (flash_area_read(fap, off + info.it_tlv_tot, &info, sizeof(info))) {
276 rc = BOOT_EFLASH;
277 goto done;
278 }
279 } else if (protect_tlv_size != 0) {
280 rc = BOOT_EBADIMAGE;
281 goto done;
282 }
283
Fabio Utzig61fd8882019-09-14 20:00:20 -0300284 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
285 rc = BOOT_EBADIMAGE;
286 goto done;
287 }
288
Fabio Utzige52c08e2019-09-11 19:32:00 -0300289 *size = off + protect_tlv_size + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600290 rc = 0;
291
292done:
293 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300294 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600295}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300296#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600297
Tamas Banfe031092020-09-10 17:32:39 +0200298#if !defined(MCUBOOT_RAM_LOAD)
David Brownab449182019-11-15 09:32:52 -0700299static uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300300boot_write_sz(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800301{
David Brownab449182019-11-15 09:32:52 -0700302 uint32_t elem_sz;
Fabio Utzig12d59162019-11-28 10:01:59 -0300303#if MCUBOOT_SWAP_USING_SCRATCH
David Brownab449182019-11-15 09:32:52 -0700304 uint32_t align;
Fabio Utzig12d59162019-11-28 10:01:59 -0300305#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800306
307 /* Figure out what size to write update status update as. The size depends
308 * on what the minimum write size is for scratch area, active image slot.
309 * We need to use the bigger of those 2 values.
310 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300311 elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
Fabio Utzig12d59162019-11-28 10:01:59 -0300312#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300313 align = flash_area_align(BOOT_SCRATCH_AREA(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800314 if (align > elem_sz) {
315 elem_sz = align;
316 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300317#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800318
319 return elem_sz;
320}
321
Fabio Utzig10ee6482019-08-01 12:04:52 -0300322static int
323boot_initialize_area(struct boot_loader_state *state, int flash_area)
324{
Dominik Ermel51c8d762021-05-24 15:34:01 +0000325 uint32_t num_sectors = BOOT_MAX_IMG_SECTORS;
326 boot_sector_t *out_sectors;
327 uint32_t *out_num_sectors;
Fabio Utzig10ee6482019-08-01 12:04:52 -0300328 int rc;
329
330 num_sectors = BOOT_MAX_IMG_SECTORS;
331
332 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
333 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
334 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
335 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
336 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
337 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300338#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300339 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
340 out_sectors = state->scratch.sectors;
341 out_num_sectors = &state->scratch.num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300342#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -0300343 } else {
344 return BOOT_EFLASH;
345 }
346
Dominik Ermel51c8d762021-05-24 15:34:01 +0000347#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
Fabio Utzig10ee6482019-08-01 12:04:52 -0300348 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
Dominik Ermel51c8d762021-05-24 15:34:01 +0000349#else
350 _Static_assert(sizeof(int) <= sizeof(uint32_t), "Fix needed");
351 rc = flash_area_to_sectors(flash_area, (int *)&num_sectors, out_sectors);
352#endif /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300353 if (rc != 0) {
354 return rc;
355 }
356 *out_num_sectors = num_sectors;
357 return 0;
358}
Fabio Utzig10ee6482019-08-01 12:04:52 -0300359
Christopher Collins92ea77f2016-12-12 15:59:26 -0800360/**
361 * Determines the sector layout of both image slots and the scratch area.
362 * This information is necessary for calculating the number of bytes to erase
363 * and copy during an image swap. The information collected during this
Fabio Utzig10ee6482019-08-01 12:04:52 -0300364 * function is used to populate the state.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800365 */
366static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300367boot_read_sectors(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800368{
Fabio Utzigb0f04732019-07-31 09:49:19 -0300369 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800370 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800371
Fabio Utzig10ee6482019-08-01 12:04:52 -0300372 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300373
374 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800375 if (rc != 0) {
376 return BOOT_EFLASH;
377 }
378
Fabio Utzig10ee6482019-08-01 12:04:52 -0300379 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800380 if (rc != 0) {
Andrzej Puzdrowski54b4ad92021-06-22 13:21:22 +0200381 /* We need to differentiate from the primary image issue */
382 return BOOT_EFLASH_SEC;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800383 }
384
Fabio Utzig12d59162019-11-28 10:01:59 -0300385#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300386 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200387 if (rc != 0) {
388 return BOOT_EFLASH;
389 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300390#endif
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200391
Fabio Utzig10ee6482019-08-01 12:04:52 -0300392 BOOT_WRITE_SZ(state) = boot_write_sz(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800393
394 return 0;
395}
396
Fabio Utzig12d59162019-11-28 10:01:59 -0300397void
398boot_status_reset(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800399{
Fabio Utzig4741c452019-12-19 15:32:41 -0300400#ifdef MCUBOOT_ENC_IMAGES
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000401 memset(&bs->enckey, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_KEY_ALIGN_SIZE);
Fabio Utzig4741c452019-12-19 15:32:41 -0300402#if MCUBOOT_SWAP_SAVE_ENCTLV
403 memset(&bs->enctlv, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_TLV_ALIGN_SIZE);
404#endif
405#endif /* MCUBOOT_ENC_IMAGES */
406
407 bs->use_scratch = 0;
408 bs->swap_size = 0;
409 bs->source = 0;
410
Fabio Utzig74aef312019-11-28 11:05:34 -0300411 bs->op = BOOT_STATUS_OP_MOVE;
Fabio Utzig39000012018-07-30 12:40:20 -0300412 bs->idx = BOOT_STATUS_IDX_0;
413 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700414 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Fabio Utzig12d59162019-11-28 10:01:59 -0300415}
Christopher Collins92ea77f2016-12-12 15:59:26 -0800416
Fabio Utzig12d59162019-11-28 10:01:59 -0300417bool
418boot_status_is_reset(const struct boot_status *bs)
419{
Fabio Utzig74aef312019-11-28 11:05:34 -0300420 return (bs->op == BOOT_STATUS_OP_MOVE &&
421 bs->idx == BOOT_STATUS_IDX_0 &&
422 bs->state == BOOT_STATUS_STATE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800423}
424
425/**
426 * Writes the supplied boot status to the flash file system. The boot status
427 * contains the current state of an in-progress image copy operation.
428 *
429 * @param bs The boot status to write.
430 *
431 * @return 0 on success; nonzero on failure.
432 */
433int
Fabio Utzig12d59162019-11-28 10:01:59 -0300434boot_write_status(const struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800435{
436 const struct flash_area *fap;
437 uint32_t off;
438 int area_id;
Dominik Ermel9479af02021-10-19 10:06:44 +0000439 int rc = 0;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300440 uint8_t buf[BOOT_MAX_ALIGN];
Gustavo Henrique Nihei4aa286d2021-11-24 14:54:56 -0300441 uint32_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300442 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800443
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300444 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100445 * the trailer. Since in the last step the primary slot is erased, the
446 * first two status writes go to the scratch which will be copied to
447 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300448 */
449
Fabio Utzig12d59162019-11-28 10:01:59 -0300450#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig2473ac02017-05-02 12:45:02 -0300451 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800452 /* Write to scratch. */
453 area_id = FLASH_AREA_IMAGE_SCRATCH;
454 } else {
Fabio Utzig12d59162019-11-28 10:01:59 -0300455#endif
David Vincze2d736ad2019-02-18 11:50:22 +0100456 /* Write to the primary slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300457 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Fabio Utzig12d59162019-11-28 10:01:59 -0300458#if MCUBOOT_SWAP_USING_SCRATCH
Christopher Collins92ea77f2016-12-12 15:59:26 -0800459 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300460#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800461
462 rc = flash_area_open(area_id, &fap);
463 if (rc != 0) {
Dominik Ermel9479af02021-10-19 10:06:44 +0000464 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800465 }
466
467 off = boot_status_off(fap) +
Fabio Utzig12d59162019-11-28 10:01:59 -0300468 boot_status_internal_off(bs, BOOT_WRITE_SZ(state));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200469 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300470 erased_val = flash_area_erased_val(fap);
471 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700472 buf[0] = bs->state;
473
Jamie McCrae35e99312024-01-03 07:37:55 +0000474 BOOT_LOG_DBG("writing swap status; fa_id=%d off=0x%lx (0x%lx)",
475 flash_area_get_id(fap), (unsigned long)off,
476 (unsigned long)flash_area_get_off(fap) + off);
477
David Brown9d725462017-01-23 15:50:58 -0700478 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800479 if (rc != 0) {
480 rc = BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800481 }
482
Christopher Collins92ea77f2016-12-12 15:59:26 -0800483 flash_area_close(fap);
Dominik Ermel9479af02021-10-19 10:06:44 +0000484
Christopher Collins92ea77f2016-12-12 15:59:26 -0800485 return rc;
486}
Tamas Banfe031092020-09-10 17:32:39 +0200487#endif /* !MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +0200488#endif /* !MCUBOOT_DIRECT_XIP */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800489
490/*
David Vinczec3084132020-02-18 14:50:47 +0100491 * Validate image hash/signature and optionally the security counter in a slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800492 */
Michael Grand5047f032022-11-24 16:49:56 +0100493static fih_ret
Fabio Utzig10ee6482019-08-01 12:04:52 -0300494boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
495 const struct flash_area *fap, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800496{
Fabio Utzig10ee6482019-08-01 12:04:52 -0300497 TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigb0f04732019-07-31 09:49:19 -0300498 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300499 int rc;
Michael Grand5047f032022-11-24 16:49:56 +0100500 FIH_DECLARE(fih_rc, FIH_FAILURE);
Fabio Utzigba829042018-09-18 08:29:34 -0300501
Fabio Utzig10ee6482019-08-01 12:04:52 -0300502#if (BOOT_IMAGE_NUMBER == 1)
503 (void)state;
504#endif
505
Fabio Utzigba829042018-09-18 08:29:34 -0300506 (void)bs;
507 (void)rc;
Fabio Utzigbc077932019-08-26 11:16:34 -0300508
509 image_index = BOOT_CURR_IMG(state);
510
Hugo L'Hostisdb543e52021-03-09 18:00:31 +0000511/* In the case of ram loading the image has already been decrypted as it is
512 * decrypted when copied in ram */
513#if defined(MCUBOOT_ENC_IMAGES) && !defined(MCUBOOT_RAM_LOAD)
Fabio Utzigbc077932019-08-26 11:16:34 -0300514 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzig4741c452019-12-19 15:32:41 -0300515 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300516 if (rc < 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100517 FIH_RET(fih_rc);
Fabio Utzigba829042018-09-18 08:29:34 -0300518 }
Fabio Utzig4741c452019-12-19 15:32:41 -0300519 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100520 FIH_RET(fih_rc);
Fabio Utzigba829042018-09-18 08:29:34 -0300521 }
522 }
Fabio Utzigbc077932019-08-26 11:16:34 -0300523#endif
524
Raef Colese8fe6cf2020-05-26 13:07:40 +0100525 FIH_CALL(bootutil_img_validate, fih_rc, BOOT_CURR_ENC(state), image_index,
526 hdr, fap, tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, NULL);
Fabio Utzig10ee6482019-08-01 12:04:52 -0300527
Raef Colese8fe6cf2020-05-26 13:07:40 +0100528 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800529}
530
Tamas Banfe031092020-09-10 17:32:39 +0200531#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
Michael Grand5047f032022-11-24 16:49:56 +0100532static fih_ret
Christopher Collins92ea77f2016-12-12 15:59:26 -0800533split_image_check(struct image_header *app_hdr,
534 const struct flash_area *app_fap,
535 struct image_header *loader_hdr,
536 const struct flash_area *loader_fap)
537{
538 static void *tmpbuf;
539 uint8_t loader_hash[32];
Michael Grand5047f032022-11-24 16:49:56 +0100540 FIH_DECLARE(fih_rc, FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800541
542 if (!tmpbuf) {
543 tmpbuf = malloc(BOOT_TMPBUF_SZ);
544 if (!tmpbuf) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100545 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800546 }
547 }
548
Raef Colese8fe6cf2020-05-26 13:07:40 +0100549 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, loader_hdr, loader_fap,
550 tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, loader_hash);
Michael Grand5047f032022-11-24 16:49:56 +0100551 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100552 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800553 }
554
Raef Colese8fe6cf2020-05-26 13:07:40 +0100555 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, app_hdr, app_fap,
556 tmpbuf, BOOT_TMPBUF_SZ, loader_hash, 32, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800557
Raef Colese8fe6cf2020-05-26 13:07:40 +0100558out:
559 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800560}
Tamas Banfe031092020-09-10 17:32:39 +0200561#endif /* !MCUBOOT_DIRECT_XIP && !MCUBOOT_RAM_LOAD */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800562
Fabio Utzig338a19f2018-12-03 08:37:08 -0200563/*
David Brown9bf95af2019-10-10 15:36:36 -0600564 * Check that this is a valid header. Valid means that the magic is
565 * correct, and that the sizes/offsets are "sane". Sane means that
566 * there is no overflow on the arithmetic, and that the result fits
567 * within the flash area we are in.
568 */
569static bool
570boot_is_header_valid(const struct image_header *hdr, const struct flash_area *fap)
571{
572 uint32_t size;
573
574 if (hdr->ih_magic != IMAGE_MAGIC) {
575 return false;
576 }
577
578 if (!boot_u32_safe_add(&size, hdr->ih_img_size, hdr->ih_hdr_size)) {
579 return false;
580 }
581
Dominik Ermel260ae092021-04-23 05:38:45 +0000582 if (size >= flash_area_get_size(fap)) {
David Brown9bf95af2019-10-10 15:36:36 -0600583 return false;
584 }
585
586 return true;
587}
588
589/*
Fabio Utzig338a19f2018-12-03 08:37:08 -0200590 * Check that a memory area consists of a given value.
591 */
592static inline bool
593boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300594{
595 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200596 uint8_t *p = (uint8_t *)data;
597 for (i = 0; i < len; i++) {
598 if (val != p[i]) {
599 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300600 }
601 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200602 return true;
603}
604
605static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300606boot_check_header_erased(struct boot_loader_state *state, int slot)
Fabio Utzig338a19f2018-12-03 08:37:08 -0200607{
608 const struct flash_area *fap;
609 struct image_header *hdr;
610 uint8_t erased_val;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300611 int area_id;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200612 int rc;
613
Fabio Utzig10ee6482019-08-01 12:04:52 -0300614 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300615 rc = flash_area_open(area_id, &fap);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200616 if (rc != 0) {
617 return -1;
618 }
619
620 erased_val = flash_area_erased_val(fap);
621 flash_area_close(fap);
622
Fabio Utzig10ee6482019-08-01 12:04:52 -0300623 hdr = boot_img_hdr(state, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200624 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
625 return -1;
626 }
627
628 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300629}
630
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000631#if (BOOT_IMAGE_NUMBER > 1) || \
David Vinczee574f2d2020-07-10 11:42:03 +0200632 defined(MCUBOOT_DIRECT_XIP) || \
Tamas Banfe031092020-09-10 17:32:39 +0200633 defined(MCUBOOT_RAM_LOAD) || \
Jerzy Kasenberge3f895d2022-04-12 15:05:33 +0200634 defined(MCUBOOT_DOWNGRADE_PREVENTION)
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000635/**
Marek Pietaa95a41b2023-04-21 14:03:19 +0200636 * Compare image version numbers
637 *
638 * By default, the comparison does not take build number into account.
639 * Enable MCUBOOT_VERSION_CMP_USE_BUILD_NUMBER to take the build number into account.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000640 *
David Vincze8b0b6372020-05-20 19:54:44 +0200641 * @param ver1 Pointer to the first image version to compare.
642 * @param ver2 Pointer to the second image version to compare.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000643 *
Marek Pietaa95a41b2023-04-21 14:03:19 +0200644 * @retval -1 If ver1 is less than ver2.
645 * @retval 0 If the image version numbers are equal.
646 * @retval 1 If ver1 is greater than ver2.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000647 */
648static int
David Vincze8b0b6372020-05-20 19:54:44 +0200649boot_version_cmp(const struct image_version *ver1,
650 const struct image_version *ver2)
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000651{
David Vincze8b0b6372020-05-20 19:54:44 +0200652 if (ver1->iv_major > ver2->iv_major) {
653 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000654 }
David Vincze8b0b6372020-05-20 19:54:44 +0200655 if (ver1->iv_major < ver2->iv_major) {
656 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000657 }
David Vincze8b0b6372020-05-20 19:54:44 +0200658 /* The major version numbers are equal, continue comparison. */
659 if (ver1->iv_minor > ver2->iv_minor) {
660 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000661 }
David Vincze8b0b6372020-05-20 19:54:44 +0200662 if (ver1->iv_minor < ver2->iv_minor) {
663 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000664 }
David Vincze8b0b6372020-05-20 19:54:44 +0200665 /* The minor version numbers are equal, continue comparison. */
666 if (ver1->iv_revision > ver2->iv_revision) {
667 return 1;
668 }
669 if (ver1->iv_revision < ver2->iv_revision) {
670 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000671 }
672
Marek Pietaa95a41b2023-04-21 14:03:19 +0200673#if defined(MCUBOOT_VERSION_CMP_USE_BUILD_NUMBER)
674 /* The revisions are equal, continue comparison. */
675 if (ver1->iv_build_num > ver2->iv_build_num) {
676 return 1;
677 }
678 if (ver1->iv_build_num < ver2->iv_build_num) {
679 return -1;
680 }
681#endif
682
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000683 return 0;
684}
685#endif
686
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000687#if defined(MCUBOOT_DIRECT_XIP)
688/**
689 * Check if image in slot has been set with specific ROM address to run from
690 * and whether the slot starts at that address.
691 *
692 * @returns 0 if IMAGE_F_ROM_FIXED flag is not set;
693 * 0 if IMAGE_F_ROM_FIXED flag is set and ROM address specified in
694 * header matches the slot address;
695 * 1 if IMF_F_ROM_FIXED flag is set but ROM address specified in header
696 * does not match the slot address.
697 */
698static bool
Raef Colesfe57e7d2021-10-15 11:07:09 +0100699boot_rom_address_check(struct boot_loader_state *state)
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000700{
Mark Horvathccaf7f82021-01-04 18:16:42 +0100701 uint32_t active_slot;
702 const struct image_header *hdr;
703 uint32_t f_off;
704
Raef Colesfe57e7d2021-10-15 11:07:09 +0100705 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +0100706 hdr = boot_img_hdr(state, active_slot);
707 f_off = boot_img_slot_off(state, active_slot);
708
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000709 if (hdr->ih_flags & IMAGE_F_ROM_FIXED && hdr->ih_load_addr != f_off) {
710 BOOT_LOG_WRN("Image in %s slot at 0x%x has been built for offset 0x%x"\
Mark Horvathccaf7f82021-01-04 18:16:42 +0100711 ", skipping",
712 active_slot == 0 ? "primary" : "secondary", f_off,
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000713 hdr->ih_load_addr);
714
715 /* If there is address mismatch, the image is not bootable from this
716 * slot.
717 */
718 return 1;
719 }
720 return 0;
721}
722#endif
723
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300724/*
725 * Check that there is a valid image in a slot
726 *
727 * @returns
Raef Colese8fe6cf2020-05-26 13:07:40 +0100728 * FIH_SUCCESS if image was successfully validated
Michael Grand5047f032022-11-24 16:49:56 +0100729 * FIH_NO_BOOTABLE_IMAGE if no bootloable image was found
Raef Colese8fe6cf2020-05-26 13:07:40 +0100730 * FIH_FAILURE on any errors
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300731 */
Michael Grand5047f032022-11-24 16:49:56 +0100732static fih_ret
Fabio Utzig10ee6482019-08-01 12:04:52 -0300733boot_validate_slot(struct boot_loader_state *state, int slot,
734 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800735{
736 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400737 struct image_header *hdr;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300738 int area_id;
Michael Grand5047f032022-11-24 16:49:56 +0100739 FIH_DECLARE(fih_rc, FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800740 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300741
Fabio Utzig10ee6482019-08-01 12:04:52 -0300742 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300743 rc = flash_area_open(area_id, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800744 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100745 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800746 }
747
Fabio Utzig10ee6482019-08-01 12:04:52 -0300748 hdr = boot_img_hdr(state, slot);
749 if (boot_check_header_erased(state, slot) == 0 ||
750 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
Fabio Utzig260ec452020-07-09 18:40:07 -0300751
752#if defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE)
753 /*
754 * This fixes an issue where an image might be erased, but a trailer
755 * be left behind. It can happen if the image is in the secondary slot
756 * and did not pass validation, in which case the whole slot is erased.
757 * If during the erase operation, a reset occurs, parts of the slot
758 * might have been erased while some did not. The concerning part is
759 * the trailer because it might disable a new image from being loaded
760 * through mcumgr; so we just get rid of the trailer here, if the header
761 * is erased.
762 */
763 if (slot != BOOT_PRIMARY_SLOT) {
764 swap_erase_trailer_sectors(state, fap);
765 }
766#endif
767
David Vincze2d736ad2019-02-18 11:50:22 +0100768 /* No bootable image in slot; continue booting from the primary slot. */
Michael Grand5047f032022-11-24 16:49:56 +0100769 fih_rc = FIH_NO_BOOTABLE_IMAGE;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200770 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300771 }
772
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000773#if defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION)
774 if (slot != BOOT_PRIMARY_SLOT) {
775 /* Check if version of secondary slot is sufficient */
David Vincze8b0b6372020-05-20 19:54:44 +0200776 rc = boot_version_cmp(
777 &boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
778 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
779 if (rc < 0 && boot_check_header_erased(state, BOOT_PRIMARY_SLOT)) {
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000780 BOOT_LOG_ERR("insufficient version in secondary slot");
Dominik Ermel260ae092021-04-23 05:38:45 +0000781 flash_area_erase(fap, 0, flash_area_get_size(fap));
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000782 /* Image in the secondary slot does not satisfy version requirement.
783 * Erase the image and continue booting from the primary slot.
784 */
Michael Grand5047f032022-11-24 16:49:56 +0100785 fih_rc = FIH_NO_BOOTABLE_IMAGE;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000786 goto out;
787 }
788 }
789#endif
Michael Grand5047f032022-11-24 16:49:56 +0100790 BOOT_HOOK_CALL_FIH(boot_image_check_hook, FIH_BOOT_HOOK_REGULAR,
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +0200791 fih_rc, BOOT_CURR_IMG(state), slot);
Michael Grand5047f032022-11-24 16:49:56 +0100792 if (FIH_EQ(fih_rc, FIH_BOOT_HOOK_REGULAR))
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +0200793 {
794 FIH_CALL(boot_image_check, fih_rc, state, hdr, fap, bs);
795 }
Michael Grand5047f032022-11-24 16:49:56 +0100796 if (!boot_is_header_valid(hdr, fap) || FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Tamas Banfe031092020-09-10 17:32:39 +0200797 if ((slot != BOOT_PRIMARY_SLOT) || ARE_SLOTS_EQUIVALENT()) {
Dominik Ermel260ae092021-04-23 05:38:45 +0000798 flash_area_erase(fap, 0, flash_area_get_size(fap));
David Vinczee574f2d2020-07-10 11:42:03 +0200799 /* Image is invalid, erase it to prevent further unnecessary
800 * attempts to validate and boot it.
David Brownb38e0442017-02-24 13:57:12 -0700801 */
802 }
David Brown098de832019-12-10 11:58:01 -0700803#if !defined(__BOOTSIM__)
David Vincze2d736ad2019-02-18 11:50:22 +0100804 BOOT_LOG_ERR("Image in the %s slot is not valid!",
805 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Brown098de832019-12-10 11:58:01 -0700806#endif
Michael Grand5047f032022-11-24 16:49:56 +0100807 fih_rc = FIH_NO_BOOTABLE_IMAGE;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200808 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800809 }
810
Håkon Øye Amundsene829e9d2021-11-12 14:01:01 +0000811#if MCUBOOT_IMAGE_NUMBER > 1 && !defined(MCUBOOT_ENC_IMAGES) && defined(MCUBOOT_VERIFY_IMG_ADDRESS)
812 /* Verify that the image in the secondary slot has a reset address
813 * located in the primary slot. This is done to avoid users incorrectly
814 * overwriting an application written to the incorrect slot.
815 * This feature is only supported by ARM platforms.
816 */
817 if (area_id == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
818 const struct flash_area *pri_fa = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
819 struct image_header *secondary_hdr = boot_img_hdr(state, slot);
820 uint32_t reset_value = 0;
821 uint32_t reset_addr = secondary_hdr->ih_hdr_size + sizeof(reset_value);
822
823 rc = flash_area_read(fap, reset_addr, &reset_value, sizeof(reset_value));
824 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +0100825 fih_rc = FIH_NO_BOOTABLE_IMAGE;
Håkon Øye Amundsene829e9d2021-11-12 14:01:01 +0000826 goto out;
827 }
828
829 if (reset_value < pri_fa->fa_off || reset_value> (pri_fa->fa_off + pri_fa->fa_size)) {
830 BOOT_LOG_ERR("Reset address of image in secondary slot is not in the primary slot");
831 BOOT_LOG_ERR("Erasing image from secondary slot");
832
833 /* The vector table in the image located in the secondary
834 * slot does not target the primary slot. This might
835 * indicate that the image was loaded to the wrong slot.
836 *
837 * Erase the image and continue booting from the primary slot.
838 */
839 flash_area_erase(fap, 0, fap->fa_size);
Michael Grand5047f032022-11-24 16:49:56 +0100840 fih_rc = FIH_NO_BOOTABLE_IMAGE;
Håkon Øye Amundsene829e9d2021-11-12 14:01:01 +0000841 goto out;
842 }
843 }
844#endif
845
Fabio Utzig338a19f2018-12-03 08:37:08 -0200846out:
847 flash_area_close(fap);
Raef Colese8fe6cf2020-05-26 13:07:40 +0100848
849 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800850}
851
David Vinczec3084132020-02-18 14:50:47 +0100852#ifdef MCUBOOT_HW_ROLLBACK_PROT
853/**
854 * Updates the stored security counter value with the image's security counter
855 * value which resides in the given slot, only if it's greater than the stored
856 * value.
857 *
858 * @param image_index Index of the image to determine which security
859 * counter to update.
860 * @param slot Slot number of the image.
861 * @param hdr Pointer to the image header structure of the image
862 * that is currently stored in the given slot.
863 *
864 * @return 0 on success; nonzero on failure.
865 */
866static int
867boot_update_security_counter(uint8_t image_index, int slot,
868 struct image_header *hdr)
869{
870 const struct flash_area *fap = NULL;
871 uint32_t img_security_cnt;
872 int rc;
873
874 rc = flash_area_open(flash_area_id_from_multi_image_slot(image_index, slot),
875 &fap);
876 if (rc != 0) {
877 rc = BOOT_EFLASH;
878 goto done;
879 }
880
881 rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt);
882 if (rc != 0) {
883 goto done;
884 }
885
886 rc = boot_nv_security_counter_update(image_index, img_security_cnt);
887 if (rc != 0) {
888 goto done;
889 }
890
891done:
892 flash_area_close(fap);
893 return rc;
894}
895#endif /* MCUBOOT_HW_ROLLBACK_PROT */
896
Tamas Banfe031092020-09-10 17:32:39 +0200897#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
David Vinczee574f2d2020-07-10 11:42:03 +0200898/**
899 * Determines which swap operation to perform, if any. If it is determined
900 * that a swap operation is required, the image in the secondary slot is checked
901 * for validity. If the image in the secondary slot is invalid, it is erased,
902 * and a swap type of "none" is indicated.
903 *
904 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
905 */
906static int
907boot_validated_swap_type(struct boot_loader_state *state,
908 struct boot_status *bs)
909{
910 int swap_type;
Michael Grand5047f032022-11-24 16:49:56 +0100911 FIH_DECLARE(fih_rc, FIH_FAILURE);
David Vinczee574f2d2020-07-10 11:42:03 +0200912
913 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
914 if (BOOT_IS_UPGRADE(swap_type)) {
915 /* Boot loader wants to switch to the secondary slot.
916 * Ensure image is valid.
917 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100918 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_SECONDARY_SLOT, bs);
Michael Grand5047f032022-11-24 16:49:56 +0100919 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
920 if (FIH_EQ(fih_rc, FIH_NO_BOOTABLE_IMAGE)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100921 swap_type = BOOT_SWAP_TYPE_NONE;
922 } else {
923 swap_type = BOOT_SWAP_TYPE_FAIL;
924 }
David Vinczee574f2d2020-07-10 11:42:03 +0200925 }
926 }
927
928 return swap_type;
929}
David Brown94ed12c2021-05-26 16:28:14 -0600930#endif
David Vinczee574f2d2020-07-10 11:42:03 +0200931
Christopher Collins92ea77f2016-12-12 15:59:26 -0800932/**
Christopher Collins92ea77f2016-12-12 15:59:26 -0800933 * Erases a region of flash.
934 *
Fabio Utzigba829042018-09-18 08:29:34 -0300935 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800936 * @param off The offset within the flash area to start the
937 * erase.
938 * @param sz The number of bytes to erase.
939 *
940 * @return 0 on success; nonzero on failure.
941 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300942int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300943boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800944{
Fabio Utzigba829042018-09-18 08:29:34 -0300945 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800946}
947
David Brown94ed12c2021-05-26 16:28:14 -0600948#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
Dominik Ermel256bc372022-12-07 15:51:31 +0000949
950#if defined(MCUBOOT_ENC_IMAGES) || defined(MCUBOOT_SWAP_SAVE_ENCTLV)
951/* Replacement for memset(p, 0, sizeof(*p) that does not get
952 * optimized out.
953 */
954static void like_mbedtls_zeroize(void *p, size_t n)
955{
956 volatile unsigned char *v = (unsigned char *)p;
957
958 for (size_t i = 0; i < n; i++) {
959 v[i] = 0;
960 }
961}
962#endif
963
Christopher Collins92ea77f2016-12-12 15:59:26 -0800964/**
965 * Copies the contents of one flash region to another. You must erase the
966 * destination region prior to calling this function.
967 *
968 * @param flash_area_id_src The ID of the source flash area.
969 * @param flash_area_id_dst The ID of the destination flash area.
970 * @param off_src The offset within the source flash area to
971 * copy from.
972 * @param off_dst The offset within the destination flash area to
973 * copy to.
974 * @param sz The number of bytes to copy.
975 *
976 * @return 0 on success; nonzero on failure.
977 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300978int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300979boot_copy_region(struct boot_loader_state *state,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300980 const struct flash_area *fap_src,
Fabio Utzigba829042018-09-18 08:29:34 -0300981 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800982 uint32_t off_src, uint32_t off_dst, uint32_t sz)
983{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800984 uint32_t bytes_copied;
985 int chunk_sz;
986 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -0300987#ifdef MCUBOOT_ENC_IMAGES
988 uint32_t off;
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300989 uint32_t tlv_off;
Fabio Utzigba829042018-09-18 08:29:34 -0300990 size_t blk_off;
991 struct image_header *hdr;
992 uint16_t idx;
993 uint32_t blk_sz;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300994 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300995#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800996
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000997 TARGET_STATIC uint8_t buf[BUF_SZ] __attribute__((aligned(4)));
Fabio Utzig10ee6482019-08-01 12:04:52 -0300998
999#if !defined(MCUBOOT_ENC_IMAGES)
1000 (void)state;
1001#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001002
Christopher Collins92ea77f2016-12-12 15:59:26 -08001003 bytes_copied = 0;
1004 while (bytes_copied < sz) {
1005 if (sz - bytes_copied > sizeof buf) {
1006 chunk_sz = sizeof buf;
1007 } else {
1008 chunk_sz = sz - bytes_copied;
1009 }
1010
1011 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
1012 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001013 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001014 }
1015
Fabio Utzigba829042018-09-18 08:29:34 -03001016#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001017 image_index = BOOT_CURR_IMG(state);
Dominik Ermel260ae092021-04-23 05:38:45 +00001018 if ((flash_area_get_id(fap_src) == FLASH_AREA_IMAGE_SECONDARY(image_index) ||
1019 flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) &&
1020 !(flash_area_get_id(fap_src) == FLASH_AREA_IMAGE_SECONDARY(image_index) &&
1021 flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index))) {
David Vincze2d736ad2019-02-18 11:50:22 +01001022 /* assume the secondary slot as src, needs decryption */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001023 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig74aef312019-11-28 11:05:34 -03001024#if !defined(MCUBOOT_SWAP_USING_MOVE)
Fabio Utzigba829042018-09-18 08:29:34 -03001025 off = off_src;
Dominik Ermel260ae092021-04-23 05:38:45 +00001026 if (flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001027 /* might need encryption (metadata from the primary slot) */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001028 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001029 off = off_dst;
1030 }
Fabio Utzig74aef312019-11-28 11:05:34 -03001031#else
1032 off = off_dst;
Dominik Ermel260ae092021-04-23 05:38:45 +00001033 if (flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
Fabio Utzig74aef312019-11-28 11:05:34 -03001034 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
1035 }
1036#endif
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001037 if (IS_ENCRYPTED(hdr)) {
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001038 uint32_t abs_off = off + bytes_copied;
1039 if (abs_off < hdr->ih_hdr_size) {
Fabio Utzigba829042018-09-18 08:29:34 -03001040 /* do not decrypt header */
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001041 if (abs_off + chunk_sz > hdr->ih_hdr_size) {
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001042 /* The lower part of the chunk contains header data */
1043 blk_off = 0;
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001044 blk_sz = chunk_sz - (hdr->ih_hdr_size - abs_off);
1045 idx = hdr->ih_hdr_size - abs_off;
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001046 } else {
1047 /* The chunk contains exclusively header data */
1048 blk_sz = 0; /* nothing to decrypt */
1049 }
Fabio Utzigba829042018-09-18 08:29:34 -03001050 } else {
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001051 idx = 0;
1052 blk_sz = chunk_sz;
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001053 blk_off = (abs_off - hdr->ih_hdr_size) & 0xf;
Fabio Utzigba829042018-09-18 08:29:34 -03001054 }
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001055
1056 if (blk_sz > 0)
1057 {
1058 tlv_off = BOOT_TLV_OFF(hdr);
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001059 if (abs_off + chunk_sz > tlv_off) {
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001060 /* do not decrypt TLVs */
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001061 if (abs_off >= tlv_off) {
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001062 blk_sz = 0;
1063 } else {
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001064 blk_sz = tlv_off - abs_off;
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001065 }
Fabio Utzigba829042018-09-18 08:29:34 -03001066 }
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001067 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001068 (abs_off + idx) - hdr->ih_hdr_size, blk_sz,
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001069 blk_off, &buf[idx]);
Fabio Utzigba829042018-09-18 08:29:34 -03001070 }
Fabio Utzigba829042018-09-18 08:29:34 -03001071 }
1072 }
1073#endif
1074
Christopher Collins92ea77f2016-12-12 15:59:26 -08001075 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
1076 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001077 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001078 }
1079
1080 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -03001081
1082 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -08001083 }
1084
Fabio Utzigba829042018-09-18 08:29:34 -03001085 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001086}
1087
Christopher Collins92ea77f2016-12-12 15:59:26 -08001088/**
David Vincze2d736ad2019-02-18 11:50:22 +01001089 * Overwrite primary slot with the image contained in the secondary slot.
1090 * If a prior copy operation was interrupted by a system reset, this function
1091 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001092 *
1093 * @param bs The current boot status. This function reads
1094 * this struct to determine if it is resuming
1095 * an interrupted swap operation. This
1096 * function writes the updated status to this
1097 * function on return.
1098 *
1099 * @return 0 on success; nonzero on failure.
1100 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001101#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001102static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001103boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
David Brown17609d82017-05-05 09:41:34 -06001104{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001105 size_t sect_count;
1106 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001107 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001108 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001109 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001110 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +01001111 const struct flash_area *fap_primary_slot;
1112 const struct flash_area *fap_secondary_slot;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001113 uint8_t image_index;
David Vincze2d736ad2019-02-18 11:50:22 +01001114
Fabio Utzigb4f88102020-10-04 10:16:24 -03001115#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1116 uint32_t sector;
1117 uint32_t trailer_sz;
1118 uint32_t off;
1119 uint32_t sz;
1120#endif
1121
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001122 (void)bs;
1123
Fabio Utzig13d9e352017-10-05 20:32:31 -03001124#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1125 uint32_t src_size = 0;
Fabio Utzigd638b172019-08-09 10:38:05 -03001126 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001127 assert(rc == 0);
1128#endif
David Brown17609d82017-05-05 09:41:34 -06001129
Fabio Utzigb0f04732019-07-31 09:49:19 -03001130 image_index = BOOT_CURR_IMG(state);
1131
Antonio de Angelisba5fb1c2022-10-11 10:10:37 +01001132 BOOT_LOG_INF("Image %d upgrade secondary slot -> primary slot", image_index);
1133 BOOT_LOG_INF("Erasing the primary slot");
1134
Fabio Utzigb0f04732019-07-31 09:49:19 -03001135 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1136 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001137 assert (rc == 0);
1138
Fabio Utzigb0f04732019-07-31 09:49:19 -03001139 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1140 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001141 assert (rc == 0);
1142
Fabio Utzig10ee6482019-08-01 12:04:52 -03001143 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001144 for (sect = 0, size = 0; sect < sect_count; sect++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001145 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001146 rc = boot_erase_region(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001147 assert(rc == 0);
1148
Fabio Utzig13d9e352017-10-05 20:32:31 -03001149#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
Fabio Utzigb4f88102020-10-04 10:16:24 -03001150 if ((size + this_size) >= src_size) {
1151 size += src_size - size;
1152 size += BOOT_WRITE_SZ(state) - (size % BOOT_WRITE_SZ(state));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001153 break;
1154 }
1155#endif
Fabio Utzigb4f88102020-10-04 10:16:24 -03001156
1157 size += this_size;
David Brown17609d82017-05-05 09:41:34 -06001158 }
1159
Fabio Utzigb4f88102020-10-04 10:16:24 -03001160#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1161 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
1162 sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
1163 sz = 0;
1164 do {
1165 sz += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sector);
1166 off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, sector);
1167 sector--;
1168 } while (sz < trailer_sz);
1169
1170 rc = boot_erase_region(fap_primary_slot, off, sz);
1171 assert(rc == 0);
1172#endif
1173
Fabio Utzigba829042018-09-18 08:29:34 -03001174#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001175 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001176 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001177 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
Fabio Utzig4741c452019-12-19 15:32:41 -03001178 fap_secondary_slot, bs);
David Vincze2d736ad2019-02-18 11:50:22 +01001179
Fabio Utzigba829042018-09-18 08:29:34 -03001180 if (rc < 0) {
1181 return BOOT_EBADIMAGE;
1182 }
Fabio Utzig4741c452019-12-19 15:32:41 -03001183 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001184 return BOOT_EBADIMAGE;
1185 }
1186 }
1187#endif
1188
Antonio de Angelis48547002023-04-14 09:47:09 +01001189 BOOT_LOG_INF("Image %d copying the secondary slot to the primary slot: 0x%zx bytes",
1190 image_index, size);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001191 rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot, 0, 0, size);
Fabio Utzigb4f88102020-10-04 10:16:24 -03001192 if (rc != 0) {
1193 return rc;
1194 }
1195
1196#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1197 rc = boot_write_magic(fap_primary_slot);
1198 if (rc != 0) {
1199 return rc;
1200 }
1201#endif
David Brown17609d82017-05-05 09:41:34 -06001202
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +02001203 rc = BOOT_HOOK_CALL(boot_copy_region_post_hook, 0, BOOT_CURR_IMG(state),
1204 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT), size);
1205 if (rc != 0) {
1206 return rc;
1207 }
1208
David Vinczec3084132020-02-18 14:50:47 +01001209#ifdef MCUBOOT_HW_ROLLBACK_PROT
1210 /* Update the stored security counter with the new image's security counter
1211 * value. Both slots hold the new image at this point, but the secondary
1212 * slot's image header must be passed since the image headers in the
1213 * boot_data structure have not been updated yet.
1214 */
1215 rc = boot_update_security_counter(BOOT_CURR_IMG(state), BOOT_PRIMARY_SLOT,
1216 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1217 if (rc != 0) {
1218 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
1219 return rc;
1220 }
1221#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1222
Fabio Utzig13d9e352017-10-05 20:32:31 -03001223 /*
1224 * Erases header and trailer. The trailer is erased because when a new
1225 * image is written without a trailer as is the case when using newt, the
1226 * trailer that was left might trigger a new upgrade.
1227 */
Christopher Collins2c88e692019-05-22 15:10:14 -07001228 BOOT_LOG_DBG("erasing secondary header");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001229 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001230 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
1231 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001232 assert(rc == 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001233 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -07001234 BOOT_LOG_DBG("erasing secondary trailer");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001235 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001236 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
1237 last_sector),
1238 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
1239 last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001240 assert(rc == 0);
1241
David Vincze2d736ad2019-02-18 11:50:22 +01001242 flash_area_close(fap_primary_slot);
1243 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001244
David Vincze2d736ad2019-02-18 11:50:22 +01001245 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001246
1247 return 0;
1248}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001249#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001250
Christopher Collinsa1c12042019-05-23 14:00:28 -07001251#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -03001252/**
1253 * Swaps the two images in flash. If a prior copy operation was interrupted
1254 * by a system reset, this function completes that operation.
1255 *
1256 * @param bs The current boot status. This function reads
1257 * this struct to determine if it is resuming
1258 * an interrupted swap operation. This
1259 * function writes the updated status to this
1260 * function on return.
1261 *
1262 * @return 0 on success; nonzero on failure.
1263 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001264static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001265boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001266{
Fabio Utzig2473ac02017-05-02 12:45:02 -03001267 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001268 const struct flash_area *fap;
Dominik Ermel472d4c72023-02-10 13:29:58 +00001269#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzigba829042018-09-18 08:29:34 -03001270 uint8_t slot;
1271 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001272#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001273 uint32_t size;
1274 uint32_t copy_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001275 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001276 int rc;
1277
1278 /* FIXME: just do this if asked by user? */
1279
1280 size = copy_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001281 image_index = BOOT_CURR_IMG(state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001282
Fabio Utzig12d59162019-11-28 10:01:59 -03001283 if (boot_status_is_reset(bs)) {
Fabio Utzig46490722017-09-04 15:34:32 -03001284 /*
1285 * No swap ever happened, so need to find the largest image which
1286 * will be used to determine the amount of sectors to swap.
1287 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001288 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001289 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001290 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001291 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001292 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001293
Fabio Utzigba829042018-09-18 08:29:34 -03001294#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001295 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001296 fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001297 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001298 assert(rc >= 0);
1299
1300 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001301 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 0, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001302 assert(rc == 0);
1303 } else {
1304 rc = 0;
1305 }
1306 } else {
Kristine Jassmann73c38c62021-02-03 16:56:14 +00001307 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_ALIGN_SIZE);
Fabio Utzigba829042018-09-18 08:29:34 -03001308 }
1309#endif
1310
Fabio Utzig10ee6482019-08-01 12:04:52 -03001311 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001312 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001313 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001314 assert(rc == 0);
1315 }
1316
Fabio Utzigba829042018-09-18 08:29:34 -03001317#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001318 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001319 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001320 fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001321 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001322 assert(rc >= 0);
1323
1324 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001325 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001326 assert(rc == 0);
1327 } else {
1328 rc = 0;
1329 }
1330 } else {
Kristine Jassmann73c38c62021-02-03 16:56:14 +00001331 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_ALIGN_SIZE);
Fabio Utzigba829042018-09-18 08:29:34 -03001332 }
1333#endif
1334
Fabio Utzig46490722017-09-04 15:34:32 -03001335 if (size > copy_size) {
1336 copy_size = size;
1337 }
1338
1339 bs->swap_size = copy_size;
1340 } else {
1341 /*
1342 * If a swap was under way, the swap_size should already be present
1343 * in the trailer...
1344 */
Dominik Ermel472d4c72023-02-10 13:29:58 +00001345
1346 rc = boot_find_status(image_index, &fap);
1347 assert(fap != NULL);
1348 rc = boot_read_swap_size(fap, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001349 assert(rc == 0);
1350
1351 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001352
1353#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4741c452019-12-19 15:32:41 -03001354 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Dominik Ermel472d4c72023-02-10 13:29:58 +00001355 rc = boot_read_enc_key(fap, slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001356 assert(rc == 0);
1357
1358 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001359 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001360 break;
1361 }
1362 }
1363
1364 if (i != BOOT_ENC_KEY_SIZE) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001365 boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001366 }
1367 }
1368#endif
Dominik Ermel472d4c72023-02-10 13:29:58 +00001369 flash_area_close(fap);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001370 }
1371
Fabio Utzig12d59162019-11-28 10:01:59 -03001372 swap_run(state, bs, copy_size);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001373
David Vincze2d736ad2019-02-18 11:50:22 +01001374#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig12d59162019-11-28 10:01:59 -03001375 extern int boot_status_fails;
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001376 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001377 BOOT_LOG_WRN("%d status write fails performing the swap",
1378 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001379 }
1380#endif
Sigvart Hovland3fd4cd42022-09-09 13:21:18 +02001381 rc = BOOT_HOOK_CALL(boot_copy_region_post_hook, 0, BOOT_CURR_IMG(state),
1382 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT), size);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001383
Christopher Collins92ea77f2016-12-12 15:59:26 -08001384 return 0;
1385}
David Brown17609d82017-05-05 09:41:34 -06001386#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001387
David Vinczee32483f2019-06-13 10:46:24 +02001388#if (BOOT_IMAGE_NUMBER > 1)
1389/**
1390 * Check the image dependency whether it is satisfied and modify
1391 * the swap type if necessary.
1392 *
1393 * @param dep Image dependency which has to be verified.
1394 *
1395 * @return 0 on success; nonzero on failure.
1396 */
1397static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001398boot_verify_slot_dependency(struct boot_loader_state *state,
1399 struct image_dependency *dep)
David Vinczee32483f2019-06-13 10:46:24 +02001400{
1401 struct image_version *dep_version;
1402 size_t dep_slot;
1403 int rc;
David Browne6ab34c2019-09-03 12:24:21 -06001404 uint8_t swap_type;
David Vinczee32483f2019-06-13 10:46:24 +02001405
1406 /* Determine the source of the image which is the subject of
1407 * the dependency and get it's version. */
David Browne6ab34c2019-09-03 12:24:21 -06001408 swap_type = state->swap_type[dep->image_id];
Barry Solomon04075532020-03-18 09:33:32 -04001409 dep_slot = BOOT_IS_UPGRADE(swap_type) ? BOOT_SECONDARY_SLOT
1410 : BOOT_PRIMARY_SLOT;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001411 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +02001412
David Vincze8b0b6372020-05-20 19:54:44 +02001413 rc = boot_version_cmp(dep_version, &dep->image_min_version);
1414 if (rc < 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001415 /* Dependency not satisfied.
1416 * Modify the swap type to decrease the version number of the image
1417 * (which will be located in the primary slot after the boot process),
1418 * consequently the number of unsatisfied dependencies will be
1419 * decreased or remain the same.
1420 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001421 switch (BOOT_SWAP_TYPE(state)) {
David Vinczee32483f2019-06-13 10:46:24 +02001422 case BOOT_SWAP_TYPE_TEST:
1423 case BOOT_SWAP_TYPE_PERM:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001424 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczee32483f2019-06-13 10:46:24 +02001425 break;
1426 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001427 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczee32483f2019-06-13 10:46:24 +02001428 break;
1429 default:
1430 break;
1431 }
David Vincze8b0b6372020-05-20 19:54:44 +02001432 } else {
1433 /* Dependency satisfied. */
1434 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001435 }
1436
1437 return rc;
1438}
1439
1440/**
1441 * Read all dependency TLVs of an image from the flash and verify
1442 * one after another to see if they are all satisfied.
1443 *
1444 * @param slot Image slot number.
1445 *
1446 * @return 0 on success; nonzero on failure.
1447 */
1448static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001449boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
David Vinczee32483f2019-06-13 10:46:24 +02001450{
1451 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001452 struct image_tlv_iter it;
David Vinczee32483f2019-06-13 10:46:24 +02001453 struct image_dependency dep;
1454 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001455 uint16_t len;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001456 int area_id;
David Vinczee32483f2019-06-13 10:46:24 +02001457 int rc;
1458
Fabio Utzig10ee6482019-08-01 12:04:52 -03001459 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001460 rc = flash_area_open(area_id, &fap);
David Vinczee32483f2019-06-13 10:46:24 +02001461 if (rc != 0) {
1462 rc = BOOT_EFLASH;
1463 goto done;
1464 }
1465
Fabio Utzig61fd8882019-09-14 20:00:20 -03001466 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
1467 IMAGE_TLV_DEPENDENCY, true);
David Vinczee32483f2019-06-13 10:46:24 +02001468 if (rc != 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001469 goto done;
1470 }
1471
Fabio Utzig61fd8882019-09-14 20:00:20 -03001472 while (true) {
1473 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
1474 if (rc < 0) {
1475 return -1;
1476 } else if (rc > 0) {
1477 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001478 break;
1479 }
Fabio Utzig61fd8882019-09-14 20:00:20 -03001480
1481 if (len != sizeof(dep)) {
1482 rc = BOOT_EBADIMAGE;
1483 goto done;
1484 }
1485
1486 rc = flash_area_read(fap, off, &dep, len);
1487 if (rc != 0) {
1488 rc = BOOT_EFLASH;
1489 goto done;
1490 }
1491
1492 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
1493 rc = BOOT_EBADARGS;
1494 goto done;
1495 }
1496
1497 /* Verify dependency and modify the swap type if not satisfied. */
1498 rc = boot_verify_slot_dependency(state, &dep);
1499 if (rc != 0) {
1500 /* Dependency not satisfied. */
1501 goto done;
1502 }
David Vinczee32483f2019-06-13 10:46:24 +02001503 }
1504
1505done:
1506 flash_area_close(fap);
1507 return rc;
1508}
1509
1510/**
David Vinczee32483f2019-06-13 10:46:24 +02001511 * Iterate over all the images and verify whether the image dependencies in the
1512 * TLV area are all satisfied and update the related swap type if necessary.
1513 */
Fabio Utzig298913b2019-08-28 11:22:45 -03001514static int
1515boot_verify_dependencies(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001516{
Erik Johnson49063752020-02-06 09:59:31 -06001517 int rc = -1;
Fabio Utzig298913b2019-08-28 11:22:45 -03001518 uint8_t slot;
David Vinczee32483f2019-06-13 10:46:24 +02001519
Fabio Utzig10ee6482019-08-01 12:04:52 -03001520 BOOT_CURR_IMG(state) = 0;
1521 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
Raef Colesf11de642021-10-15 11:11:56 +01001522 if (state->img_mask[BOOT_CURR_IMG(state)]) {
1523 BOOT_CURR_IMG(state)++;
1524 continue;
1525 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001526 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1527 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
1528 slot = BOOT_SECONDARY_SLOT;
1529 } else {
1530 slot = BOOT_PRIMARY_SLOT;
1531 }
1532
1533 rc = boot_verify_slot_dependencies(state, slot);
Fabio Utzigabec0732019-07-31 08:40:22 -03001534 if (rc == 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001535 /* All dependencies've been satisfied, continue with next image. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001536 BOOT_CURR_IMG(state)++;
David Vincze8b0b6372020-05-20 19:54:44 +02001537 } else {
Fabio Utzig298913b2019-08-28 11:22:45 -03001538 /* Cannot upgrade due to non-met dependencies, so disable all
1539 * image upgrades.
1540 */
1541 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
1542 BOOT_CURR_IMG(state) = idx;
1543 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1544 }
1545 break;
David Vinczee32483f2019-06-13 10:46:24 +02001546 }
1547 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001548 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02001549}
1550#endif /* (BOOT_IMAGE_NUMBER > 1) */
1551
Christopher Collins92ea77f2016-12-12 15:59:26 -08001552/**
David Vinczeba3bd602019-06-17 16:01:43 +02001553 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001554 *
David Vinczeba3bd602019-06-17 16:01:43 +02001555 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001556 *
1557 * @return 0 on success; nonzero on failure.
1558 */
1559static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001560boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001561{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001562 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001563#ifndef MCUBOOT_OVERWRITE_ONLY
1564 uint8_t swap_type;
1565#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001566
David Vinczeba3bd602019-06-17 16:01:43 +02001567 /* At this point there are no aborted swaps. */
1568#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001569 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001570#elif defined(MCUBOOT_BOOTSTRAP)
1571 /* Check if the image update was triggered by a bad image in the
1572 * primary slot (the validity of the image in the secondary slot had
1573 * already been checked).
1574 */
Michael Grand5047f032022-11-24 16:49:56 +01001575 FIH_DECLARE(fih_rc, FIH_FAILURE);
Raef Colese8fe6cf2020-05-26 13:07:40 +01001576 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1577 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, bs);
Michael Grand5047f032022-11-24 16:49:56 +01001578 if (rc == 0 || FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001579 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001580 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001581 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001582 }
1583#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03001584 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001585#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001586 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001587
1588#ifndef MCUBOOT_OVERWRITE_ONLY
1589 /* The following state needs image_ok be explicitly set after the
1590 * swap was finished to avoid a new revert.
1591 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001592 swap_type = BOOT_SWAP_TYPE(state);
1593 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1594 swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001595 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001596 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001597 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001598 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001599 }
1600
David Vinczec3084132020-02-18 14:50:47 +01001601#ifdef MCUBOOT_HW_ROLLBACK_PROT
1602 if (swap_type == BOOT_SWAP_TYPE_PERM) {
1603 /* Update the stored security counter with the new image's security
1604 * counter value. The primary slot holds the new image at this point,
1605 * but the secondary slot's image header must be passed since image
1606 * headers in the boot_data structure have not been updated yet.
1607 *
1608 * In case of a permanent image swap mcuboot will never attempt to
1609 * revert the images on the next reboot. Therefore, the security
1610 * counter must be increased right after the image upgrade.
1611 */
1612 rc = boot_update_security_counter(
1613 BOOT_CURR_IMG(state),
1614 BOOT_PRIMARY_SLOT,
1615 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1616 if (rc != 0) {
1617 BOOT_LOG_ERR("Security counter update failed after "
1618 "image upgrade.");
1619 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
1620 }
1621 }
1622#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1623
Fabio Utzige575b0b2019-09-11 12:34:23 -03001624 if (BOOT_IS_UPGRADE(swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001625 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001626 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001627 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001628 }
David Vinczeba3bd602019-06-17 16:01:43 +02001629 }
1630#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001631
David Vinczeba3bd602019-06-17 16:01:43 +02001632 return rc;
1633}
1634
1635/**
1636 * Completes a previously aborted image swap.
1637 *
1638 * @param bs The current boot status.
1639 *
1640 * @return 0 on success; nonzero on failure.
1641 */
1642#if !defined(MCUBOOT_OVERWRITE_ONLY)
1643static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001644boot_complete_partial_swap(struct boot_loader_state *state,
1645 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001646{
1647 int rc;
1648
1649 /* Determine the type of swap operation being resumed from the
1650 * `swap-type` trailer field.
1651 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001652 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001653 assert(rc == 0);
1654
Fabio Utzig10ee6482019-08-01 12:04:52 -03001655 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001656
1657 /* The following states need image_ok be explicitly set after the
1658 * swap was finished to avoid a new revert.
1659 */
1660 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1661 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001662 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001663 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001664 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001665 }
1666 }
1667
Fabio Utzige575b0b2019-09-11 12:34:23 -03001668 if (BOOT_IS_UPGRADE(bs->swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001669 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001670 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001671 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001672 }
1673 }
1674
Fabio Utzig10ee6482019-08-01 12:04:52 -03001675 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001676 BOOT_LOG_ERR("panic!");
1677 assert(0);
1678
1679 /* Loop forever... */
1680 while (1) {}
1681 }
1682
1683 return rc;
1684}
1685#endif /* !MCUBOOT_OVERWRITE_ONLY */
1686
1687#if (BOOT_IMAGE_NUMBER > 1)
1688/**
1689 * Review the validity of previously determined swap types of other images.
1690 *
1691 * @param aborted_swap The current image upgrade is a
1692 * partial/aborted swap.
1693 */
1694static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001695boot_review_image_swap_types(struct boot_loader_state *state,
1696 bool aborted_swap)
David Vinczeba3bd602019-06-17 16:01:43 +02001697{
1698 /* In that case if we rebooted in the middle of an image upgrade process, we
1699 * must review the validity of swap types, that were previously determined
1700 * for other images. The image_ok flag had not been set before the reboot
1701 * for any of the updated images (only the copy_done flag) and thus falsely
1702 * the REVERT swap type has been determined for the previous images that had
1703 * been updated before the reboot.
1704 *
1705 * There are two separate scenarios that we have to deal with:
1706 *
1707 * 1. The reboot has happened during swapping an image:
1708 * The current image upgrade has been determined as a
1709 * partial/aborted swap.
1710 * 2. The reboot has happened between two separate image upgrades:
1711 * In this scenario we must check the swap type of the current image.
1712 * In those cases if it is NONE or REVERT we cannot certainly determine
1713 * the fact of a reboot. In a consistent state images must move in the
1714 * same direction or stay in place, e.g. in practice REVERT and TEST
1715 * swap types cannot be present at the same time. If the swap type of
1716 * the current image is either TEST, PERM or FAIL we must review the
1717 * already determined swap types of other images and set each false
1718 * REVERT swap types to NONE (these images had been successfully
1719 * updated before the system rebooted between two separate image
1720 * upgrades).
1721 */
1722
Fabio Utzig10ee6482019-08-01 12:04:52 -03001723 if (BOOT_CURR_IMG(state) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001724 /* Nothing to do */
1725 return;
1726 }
1727
1728 if (!aborted_swap) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001729 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
1730 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001731 /* Nothing to do */
1732 return;
1733 }
1734 }
1735
Fabio Utzig10ee6482019-08-01 12:04:52 -03001736 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
1737 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
1738 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001739 }
1740 }
1741}
1742#endif
1743
1744/**
1745 * Prepare image to be updated if required.
1746 *
1747 * Prepare image to be updated if required with completing an image swap
1748 * operation if one was aborted and/or determining the type of the
1749 * swap operation. In case of any error set the swap type to NONE.
1750 *
Fabio Utzig10ee6482019-08-01 12:04:52 -03001751 * @param state TODO
David Vinczeba3bd602019-06-17 16:01:43 +02001752 * @param bs Pointer where the read and possibly updated
1753 * boot status can be written to.
1754 */
1755static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001756boot_prepare_image_for_update(struct boot_loader_state *state,
1757 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001758{
1759 int rc;
Michael Grand5047f032022-11-24 16:49:56 +01001760 FIH_DECLARE(fih_rc, FIH_FAILURE);
David Vinczeba3bd602019-06-17 16:01:43 +02001761
1762 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001763 rc = boot_read_sectors(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001764 if (rc != 0) {
1765 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
1766 " - too small?", BOOT_MAX_IMG_SECTORS);
1767 /* Unable to determine sector layout, continue with next image
1768 * if there is one.
1769 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001770 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Andrzej Puzdrowski54b4ad92021-06-22 13:21:22 +02001771 if (rc == BOOT_EFLASH)
1772 {
1773 /* Only return on error from the primary image flash */
1774 return;
1775 }
David Vinczeba3bd602019-06-17 16:01:43 +02001776 }
1777
1778 /* Attempt to read an image header from each slot. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001779 rc = boot_read_image_headers(state, false, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02001780 if (rc != 0) {
1781 /* Continue with next image if there is one. */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001782 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001783 BOOT_CURR_IMG(state));
1784 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001785 return;
1786 }
1787
1788 /* If the current image's slots aren't compatible, no swap is possible.
1789 * Just boot into primary slot.
1790 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001791 if (boot_slots_compatible(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001792 boot_status_reset(bs);
1793
1794#ifndef MCUBOOT_OVERWRITE_ONLY
1795 rc = swap_read_status(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001796 if (rc != 0) {
1797 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001798 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001799 /* Continue with next image if there is one. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001800 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001801 return;
1802 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001803#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001804
Fabio Utzig74aef312019-11-28 11:05:34 -03001805#ifdef MCUBOOT_SWAP_USING_MOVE
1806 /*
1807 * Must re-read image headers because the boot status might
1808 * have been updated in the previous function call.
1809 */
1810 rc = boot_read_image_headers(state, !boot_status_is_reset(bs), bs);
Fabio Utzig32afe852020-10-04 10:36:02 -03001811#ifdef MCUBOOT_BOOTSTRAP
1812 /* When bootstrapping it's OK to not have image magic in the primary slot */
1813 if (rc != 0 && (BOOT_CURR_IMG(state) != BOOT_PRIMARY_SLOT ||
1814 boot_check_header_erased(state, BOOT_PRIMARY_SLOT) != 0)) {
1815#else
Fabio Utzig74aef312019-11-28 11:05:34 -03001816 if (rc != 0) {
Fabio Utzig32afe852020-10-04 10:36:02 -03001817#endif
1818
Fabio Utzig74aef312019-11-28 11:05:34 -03001819 /* Continue with next image if there is one. */
1820 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
1821 BOOT_CURR_IMG(state));
1822 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1823 return;
1824 }
1825#endif
1826
David Vinczeba3bd602019-06-17 16:01:43 +02001827 /* Determine if we rebooted in the middle of an image swap
1828 * operation. If a partial swap was detected, complete it.
1829 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001830 if (!boot_status_is_reset(bs)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001831
1832#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001833 boot_review_image_swap_types(state, true);
David Vinczeba3bd602019-06-17 16:01:43 +02001834#endif
1835
1836#ifdef MCUBOOT_OVERWRITE_ONLY
1837 /* Should never arrive here, overwrite-only mode has
1838 * no swap state.
1839 */
1840 assert(0);
1841#else
1842 /* Determine the type of swap operation being resumed from the
1843 * `swap-type` trailer field.
1844 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001845 rc = boot_complete_partial_swap(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001846 assert(rc == 0);
1847#endif
1848 /* Attempt to read an image header from each slot. Ensure that
1849 * image headers in slots are aligned with headers in boot_data.
1850 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001851 rc = boot_read_image_headers(state, false, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001852 assert(rc == 0);
1853
1854 /* Swap has finished set to NONE */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001855 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001856 } else {
1857 /* There was no partial swap, determine swap type. */
1858 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001859 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001860 } else {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001861 FIH_CALL(boot_validate_slot, fih_rc,
1862 state, BOOT_SECONDARY_SLOT, bs);
Michael Grand5047f032022-11-24 16:49:56 +01001863 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001864 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
1865 } else {
1866 BOOT_SWAP_TYPE(state) = bs->swap_type;
1867 }
David Vinczeba3bd602019-06-17 16:01:43 +02001868 }
1869
1870#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001871 boot_review_image_swap_types(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02001872#endif
1873
1874#ifdef MCUBOOT_BOOTSTRAP
Fabio Utzig10ee6482019-08-01 12:04:52 -03001875 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001876 /* Header checks are done first because they are
1877 * inexpensive. Since overwrite-only copies starting from
1878 * offset 0, if interrupted, it might leave a valid header
1879 * magic, so also run validation on the primary slot to be
1880 * sure it's not OK.
1881 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001882 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1883 FIH_CALL(boot_validate_slot, fih_rc,
1884 state, BOOT_PRIMARY_SLOT, bs);
1885
Michael Grand5047f032022-11-24 16:49:56 +01001886 if (rc == 0 || FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001887
Fabio Utzig3d77c952020-10-04 10:23:17 -03001888 rc = (boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC) ? 1: 0;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001889 FIH_CALL(boot_validate_slot, fih_rc,
1890 state, BOOT_SECONDARY_SLOT, bs);
1891
Michael Grand5047f032022-11-24 16:49:56 +01001892 if (rc == 1 && FIH_EQ(fih_rc, FIH_SUCCESS)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001893 /* Set swap type to REVERT to overwrite the primary
1894 * slot with the image contained in secondary slot
1895 * and to trigger the explicit setting of the
1896 * image_ok flag.
1897 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03001898 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02001899 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001900 }
1901 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001902#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001903 }
David Vinczeba3bd602019-06-17 16:01:43 +02001904 } else {
1905 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001906 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001907 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001908}
1909
Mark Horvathccaf7f82021-01-04 18:16:42 +01001910/**
1911 * Updates the security counter for the current image.
1912 *
1913 * @param state Boot loader status information.
1914 *
1915 * @return 0 on success; nonzero on failure.
1916 */
1917static int
1918boot_update_hw_rollback_protection(struct boot_loader_state *state)
1919{
1920#ifdef MCUBOOT_HW_ROLLBACK_PROT
1921 int rc;
1922
1923 /* Update the stored security counter with the active image's security
1924 * counter value. It will only be updated if the new security counter is
1925 * greater than the stored value.
1926 *
1927 * In case of a successful image swapping when the swap type is TEST the
1928 * security counter can be increased only after a reset, when the swap
1929 * type is NONE and the image has marked itself "OK" (the image_ok flag
1930 * has been set). This way a "revert" can be performed when it's
1931 * necessary.
1932 */
1933 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
1934 rc = boot_update_security_counter(
1935 BOOT_CURR_IMG(state),
1936 BOOT_PRIMARY_SLOT,
1937 boot_img_hdr(state, BOOT_PRIMARY_SLOT));
1938 if (rc != 0) {
1939 BOOT_LOG_ERR("Security counter update failed after image "
1940 "validation.");
1941 return rc;
1942 }
1943 }
1944
1945 return 0;
1946
1947#else /* MCUBOOT_HW_ROLLBACK_PROT */
1948 (void) (state);
1949
1950 return 0;
1951#endif
1952}
1953
Jerzy Kasenberge3f895d2022-04-12 15:05:33 +02001954/**
1955 * Checks test swap downgrade prevention conditions.
1956 *
1957 * Function called only for swap upgrades test run. It may prevent
1958 * swap if slot 1 image has <= version number or < security counter
1959 *
1960 * @param state Boot loader status information.
1961 *
1962 * @return 0 - image can be swapped, -1 downgrade prevention
1963 */
1964static int
1965check_downgrade_prevention(struct boot_loader_state *state)
1966{
1967#if defined(MCUBOOT_DOWNGRADE_PREVENTION) && \
1968 (defined(MCUBOOT_SWAP_USING_MOVE) || defined(MCUBOOT_SWAP_USING_SCRATCH))
1969 uint32_t security_counter[2];
1970 int rc;
1971
1972 if (MCUBOOT_DOWNGRADE_PREVENTION_SECURITY_COUNTER) {
1973 /* If there was security no counter in slot 0, allow swap */
1974 rc = bootutil_get_img_security_cnt(&(BOOT_IMG(state, 0).hdr),
1975 BOOT_IMG(state, 0).area,
1976 &security_counter[0]);
1977 if (rc != 0) {
1978 return 0;
1979 }
1980 /* If there is no security counter in slot 1, or it's lower than
1981 * that of slot 0, prevent downgrade */
1982 rc = bootutil_get_img_security_cnt(&(BOOT_IMG(state, 1).hdr),
1983 BOOT_IMG(state, 1).area,
1984 &security_counter[1]);
1985 if (rc != 0 || security_counter[0] > security_counter[1]) {
1986 rc = -1;
1987 }
1988 }
1989 else {
1990 rc = boot_version_cmp(&boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
1991 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
1992 }
1993 if (rc < 0) {
1994 /* Image in slot 0 prevents downgrade, delete image in slot 1 */
Antonio de Angelis48547002023-04-14 09:47:09 +01001995 BOOT_LOG_INF("Image %d in slot 1 erased due to downgrade prevention", BOOT_CURR_IMG(state));
Jerzy Kasenberge3f895d2022-04-12 15:05:33 +02001996 flash_area_erase(BOOT_IMG(state, 1).area, 0,
1997 flash_area_get_size(BOOT_IMG(state, 1).area));
1998 } else {
1999 rc = 0;
2000 }
2001 return rc;
2002#else
2003 (void)state;
2004 return 0;
2005#endif
2006}
2007
Michael Grand5047f032022-11-24 16:49:56 +01002008fih_ret
Fabio Utzig10ee6482019-08-01 12:04:52 -03002009context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08002010{
Marti Bolivar84898652017-06-13 17:20:22 -04002011 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02002012 struct boot_status bs;
David Vincze9015a5d2020-05-18 14:43:11 +02002013 int rc = -1;
Michael Grand5047f032022-11-24 16:49:56 +01002014 FIH_DECLARE(fih_rc, FIH_FAILURE);
Marti Bolivarc0b47912017-06-13 17:18:09 -04002015 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03002016 int image_index;
Fabio Utzig298913b2019-08-28 11:22:45 -03002017 bool has_upgrade;
Michael Grand5047f032022-11-24 16:49:56 +01002018 volatile int fih_cnt;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002019
2020 /* The array of slot sectors are defined here (as opposed to file scope) so
2021 * that they don't get allocated for non-boot-loader apps. This is
2022 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002023 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08002024 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002025 TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
2026 TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03002027#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03002028 TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03002029#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08002030
Fabio Utzig298913b2019-08-28 11:22:45 -03002031 has_upgrade = false;
2032
2033#if (BOOT_IMAGE_NUMBER == 1)
2034 (void)has_upgrade;
2035#endif
Fabio Utzigba829042018-09-18 08:29:34 -03002036
David Vinczeba3bd602019-06-17 16:01:43 +02002037 /* Iterate over all the images. By the end of the loop the swap type has
2038 * to be determined for each image and all aborted swaps have to be
2039 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002040 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002041 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002042#if BOOT_IMAGE_NUMBER > 1
2043 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2044 continue;
2045 }
2046#endif
David Vinczeba3bd602019-06-17 16:01:43 +02002047#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
2048 /* The keys used for encryption may no longer be valid (could belong to
2049 * another images). Therefore, mark them as invalid to force their reload
2050 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03002051 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03002052 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Brown554c52e2017-06-30 16:01:07 -06002053#endif
2054
Fabio Utzig10ee6482019-08-01 12:04:52 -03002055 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03002056
Fabio Utzig10ee6482019-08-01 12:04:52 -03002057 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002058 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03002059 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002060 secondary_slot_sectors[image_index];
Fabio Utzig12d59162019-11-28 10:01:59 -03002061#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03002062 state->scratch.sectors = scratch_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -03002063#endif
David Vinczeba3bd602019-06-17 16:01:43 +02002064
2065 /* Open primary and secondary image areas for the duration
2066 * of this call.
2067 */
2068 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03002069 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03002070 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02002071 assert(rc == 0);
Jamie McCrae2929a972023-09-25 11:12:20 +01002072
2073 if (rc != 0) {
2074 BOOT_LOG_ERR("Failed to open flash area ID %d (image %d slot %d): %d, "
2075 "cannot continue", fa_id, image_index, (int8_t)slot, rc);
2076 FIH_PANIC;
2077 }
David Vinczeba3bd602019-06-17 16:01:43 +02002078 }
Fabio Utzig12d59162019-11-28 10:01:59 -03002079#if MCUBOOT_SWAP_USING_SCRATCH
David Vinczeba3bd602019-06-17 16:01:43 +02002080 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03002081 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002082 assert(rc == 0);
Jamie McCrae2929a972023-09-25 11:12:20 +01002083
2084 if (rc != 0) {
2085 BOOT_LOG_ERR("Failed to open scratch flash area: %d, cannot continue", rc);
2086 FIH_PANIC;
2087 }
Fabio Utzig12d59162019-11-28 10:01:59 -03002088#endif
David Vinczeba3bd602019-06-17 16:01:43 +02002089
2090 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002091 boot_prepare_image_for_update(state, &bs);
Fabio Utzig298913b2019-08-28 11:22:45 -03002092
Fabio Utzige575b0b2019-09-11 12:34:23 -03002093 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
Fabio Utzig298913b2019-08-28 11:22:45 -03002094 has_upgrade = true;
2095 }
David Vinczeba3bd602019-06-17 16:01:43 +02002096 }
2097
David Vinczee32483f2019-06-13 10:46:24 +02002098#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig298913b2019-08-28 11:22:45 -03002099 if (has_upgrade) {
2100 /* Iterate over all the images and verify whether the image dependencies
2101 * are all satisfied and update swap type if necessary.
2102 */
2103 rc = boot_verify_dependencies(state);
David Vincze8b0b6372020-05-20 19:54:44 +02002104 if (rc != 0) {
Fabio Utzig298913b2019-08-28 11:22:45 -03002105 /*
2106 * It was impossible to upgrade because the expected dependency version
2107 * was not available. Here we already changed the swap_type so that
2108 * instead of asserting the bootloader, we continue and no upgrade is
2109 * performed.
2110 */
2111 rc = 0;
2112 }
2113 }
David Vinczee32483f2019-06-13 10:46:24 +02002114#endif
2115
Jamie McCrae56cb6102022-03-23 11:57:03 +00002116 /* Trigger status change callback with upgrading status */
2117 mcuboot_status_change(MCUBOOT_STATUS_UPGRADING);
2118
David Vinczeba3bd602019-06-17 16:01:43 +02002119 /* Iterate over all the images. At this point there are no aborted swaps
2120 * and the swap types are determined for each image. By the end of the loop
2121 * all required update operations will have been finished.
2122 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002123 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002124#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesf11de642021-10-15 11:11:56 +01002125 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2126 continue;
2127 }
2128
David Vinczeba3bd602019-06-17 16:01:43 +02002129#ifdef MCUBOOT_ENC_IMAGES
2130 /* The keys used for encryption may no longer be valid (could belong to
2131 * another images). Therefore, mark them as invalid to force their reload
2132 * by boot_enc_load().
2133 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03002134 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002135#endif /* MCUBOOT_ENC_IMAGES */
2136
2137 /* Indicate that swap is not aborted */
Fabio Utzig12d59162019-11-28 10:01:59 -03002138 boot_status_reset(&bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002139#endif /* (BOOT_IMAGE_NUMBER > 1) */
2140
2141 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002142 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02002143
Fabio Utzig10ee6482019-08-01 12:04:52 -03002144 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002145 case BOOT_SWAP_TYPE_NONE:
2146 break;
2147
Jerzy Kasenberge3f895d2022-04-12 15:05:33 +02002148 case BOOT_SWAP_TYPE_TEST:
Michael Grand99613c62023-06-20 15:01:00 +02002149 /* fallthrough */
2150 case BOOT_SWAP_TYPE_PERM:
Jerzy Kasenberge3f895d2022-04-12 15:05:33 +02002151 if (check_downgrade_prevention(state) != 0) {
2152 /* Downgrade prevented */
2153 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
2154 break;
2155 }
2156 /* fallthrough */
David Vinczeba3bd602019-06-17 16:01:43 +02002157 case BOOT_SWAP_TYPE_REVERT:
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +02002158 rc = BOOT_HOOK_CALL(boot_perform_update_hook, BOOT_HOOK_REGULAR,
2159 BOOT_CURR_IMG(state), &(BOOT_IMG(state, 1).hdr),
2160 BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT));
2161 if (rc == BOOT_HOOK_REGULAR)
2162 {
2163 rc = boot_perform_update(state, &bs);
2164 }
David Vinczeba3bd602019-06-17 16:01:43 +02002165 assert(rc == 0);
2166 break;
2167
2168 case BOOT_SWAP_TYPE_FAIL:
2169 /* The image in secondary slot was invalid and is now erased. Ensure
2170 * we don't try to boot into it again on the next reboot. Do this by
2171 * pretending we just reverted back to primary slot.
2172 */
2173#ifndef MCUBOOT_OVERWRITE_ONLY
2174 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig12d59162019-11-28 10:01:59 -03002175 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002176 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002177 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002178 }
2179#endif /* !MCUBOOT_OVERWRITE_ONLY */
2180 break;
2181
2182 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002183 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002184 }
2185
Fabio Utzig10ee6482019-08-01 12:04:52 -03002186 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002187 BOOT_LOG_ERR("panic!");
2188 assert(0);
2189
2190 /* Loop forever... */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002191 FIH_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002192 }
2193 }
2194
2195 /* Iterate over all the images. At this point all required update operations
2196 * have finished. By the end of the loop each image in the primary slot will
2197 * have been re-validated.
2198 */
Michael Grand5047f032022-11-24 16:49:56 +01002199 FIH_SET(fih_cnt, 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -03002200 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002201#if BOOT_IMAGE_NUMBER > 1
Michael Grand5047f032022-11-24 16:49:56 +01002202 /* Hardenned to prevent from skipping check of a given image,
2203 * tmp_img_mask is declared volatile
2204 */
2205 volatile bool tmp_img_mask;
2206 FIH_SET(tmp_img_mask, state->img_mask[BOOT_CURR_IMG(state)]);
2207 if (FIH_EQ(tmp_img_mask, true)) {
2208 ++fih_cnt;
Raef Colesf11de642021-10-15 11:11:56 +01002209 continue;
2210 }
2211#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -03002212 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02002213 /* Attempt to read an image header from each slot. Ensure that image
2214 * headers in slots are aligned with headers in boot_data.
Dominik Ermel6f7f8732024-02-01 11:59:24 +00002215 * Note: Quite complicated internal logic of boot_read_image_headers
2216 * uses boot state, the last parm, to figure out in which slot which
2217 * header is located; when boot state is not provided, then it
2218 * is assumed that headers are at proper slots (we are not in
2219 * the middle of moving images, etc).
David Vinczeba3bd602019-06-17 16:01:43 +02002220 */
Dominik Ermel6f7f8732024-02-01 11:59:24 +00002221 rc = boot_read_image_headers(state, false, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02002222 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01002223 FIH_SET(fih_rc, FIH_FAILURE);
David Vinczeba3bd602019-06-17 16:01:43 +02002224 goto out;
2225 }
2226 /* Since headers were reloaded, it can be assumed we just performed
2227 * a swap or overwrite. Now the header info that should be used to
2228 * provide the data for the bootstrap, which previously was at
2229 * secondary slot, was updated to primary slot.
2230 */
2231 }
2232
2233#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Raef Colese8fe6cf2020-05-26 13:07:40 +01002234 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, NULL);
Michael Grand5047f032022-11-24 16:49:56 +01002235 /* Check for all possible values is redundant in normal operation it
2236 * is meant to prevent FI attack.
2237 */
2238 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS) ||
2239 FIH_EQ(fih_rc, FIH_FAILURE) ||
2240 FIH_EQ(fih_rc, FIH_NO_BOOTABLE_IMAGE)) {
2241 FIH_SET(fih_rc, FIH_FAILURE);
David Vinczeba3bd602019-06-17 16:01:43 +02002242 goto out;
2243 }
2244#else
2245 /* Even if we're not re-validating the primary slot, we could be booting
2246 * onto an empty flash chip. At least do a basic sanity check that
2247 * the magic number on the image is OK.
2248 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002249 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002250 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
Dominik Ermel4a4d1ac2021-08-06 11:23:52 +00002251 BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic,
Fabio Utzig10ee6482019-08-01 12:04:52 -03002252 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002253 rc = BOOT_EBADIMAGE;
Michael Grand5047f032022-11-24 16:49:56 +01002254 FIH_SET(fih_rc, FIH_FAILURE);
David Vinczeba3bd602019-06-17 16:01:43 +02002255 goto out;
2256 }
David Vinczec3084132020-02-18 14:50:47 +01002257#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
2258
Mark Horvathccaf7f82021-01-04 18:16:42 +01002259 rc = boot_update_hw_rollback_protection(state);
David Vincze1cf11b52020-03-24 07:51:09 +01002260 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01002261 FIH_SET(fih_rc, FIH_FAILURE);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002262 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002263 }
David Vincze1cf11b52020-03-24 07:51:09 +01002264
Mark Horvathccaf7f82021-01-04 18:16:42 +01002265 rc = boot_add_shared_data(state, BOOT_PRIMARY_SLOT);
David Vincze1cf11b52020-03-24 07:51:09 +01002266 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01002267 FIH_SET(fih_rc, FIH_FAILURE);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002268 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002269 }
Michael Grand5047f032022-11-24 16:49:56 +01002270 ++fih_cnt;
David Vinczeba3bd602019-06-17 16:01:43 +02002271 }
Michael Grand5047f032022-11-24 16:49:56 +01002272 /*
2273 * fih_cnt should be equal to BOOT_IMAGE_NUMBER now.
2274 * If this is not the case, at least one iteration of the loop
2275 * has been skipped.
2276 */
2277 if(FIH_NOT_EQ(fih_cnt, BOOT_IMAGE_NUMBER)) {
2278 FIH_PANIC;
2279 }
Fabio Utzigf616c542019-12-19 15:23:32 -03002280
Raef Colesfe57e7d2021-10-15 11:07:09 +01002281 fill_rsp(state, rsp);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002282
Raef Colese8fe6cf2020-05-26 13:07:40 +01002283 fih_rc = FIH_SUCCESS;
Fabio Utzig298913b2019-08-28 11:22:45 -03002284out:
Dominik Ermel256bc372022-12-07 15:51:31 +00002285 /*
2286 * Since the boot_status struct stores plaintext encryption keys, reset
2287 * them here to avoid the possibility of jumping into an image that could
2288 * easily recover them.
2289 */
2290#if defined(MCUBOOT_ENC_IMAGES) || defined(MCUBOOT_SWAP_SAVE_ENCTLV)
2291 like_mbedtls_zeroize(&bs, sizeof(bs));
2292#else
2293 memset(&bs, 0, sizeof(struct boot_status));
2294#endif
2295
Mark Horvathccaf7f82021-01-04 18:16:42 +01002296 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002297 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002298}
2299
Michael Grand5047f032022-11-24 16:49:56 +01002300fih_ret
Christopher Collins92ea77f2016-12-12 15:59:26 -08002301split_go(int loader_slot, int split_slot, void **entry)
2302{
Marti Bolivarc50926f2017-06-14 09:35:40 -04002303 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08002304 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002305 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002306 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002307 int rc;
Michael Grand5047f032022-11-24 16:49:56 +01002308 FIH_DECLARE(fih_rc, FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002309
Christopher Collins92ea77f2016-12-12 15:59:26 -08002310 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
2311 if (sectors == NULL) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002312 FIH_RET(FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002313 }
David Vinczeba3bd602019-06-17 16:01:43 +02002314 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
2315 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002316
2317 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
2318 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07002319 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04002320 assert(rc == 0);
2321 split_flash_id = flash_area_id_from_image_slot(split_slot);
2322 rc = flash_area_open(split_flash_id,
2323 &BOOT_IMG_AREA(&boot_data, split_slot));
2324 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002325
2326 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002327 rc = boot_read_sectors(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002328 if (rc != 0) {
2329 rc = SPLIT_GO_ERR;
2330 goto done;
2331 }
2332
Fabio Utzig12d59162019-11-28 10:01:59 -03002333 rc = boot_read_image_headers(&boot_data, true, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002334 if (rc != 0) {
2335 goto done;
2336 }
2337
Christopher Collins92ea77f2016-12-12 15:59:26 -08002338 /* Don't check the bootable image flag because we could really call a
2339 * bootable or non-bootable image. Just validate that the image check
2340 * passes which is distinct from the normal check.
2341 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002342 FIH_CALL(split_image_check, fih_rc,
2343 boot_img_hdr(&boot_data, split_slot),
2344 BOOT_IMG_AREA(&boot_data, split_slot),
2345 boot_img_hdr(&boot_data, loader_slot),
2346 BOOT_IMG_AREA(&boot_data, loader_slot));
Michael Grand5047f032022-11-24 16:49:56 +01002347 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08002348 goto done;
2349 }
2350
Marti Bolivarea088872017-06-12 17:10:49 -04002351 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04002352 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08002353 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002354 rc = SPLIT_GO_OK;
2355
2356done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04002357 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
2358 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002359 free(sectors);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002360
2361 if (rc) {
Michael Grand5047f032022-11-24 16:49:56 +01002362 FIH_SET(fih_rc, FIH_FAILURE);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002363 }
2364
2365 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002366}
David Vinczee574f2d2020-07-10 11:42:03 +02002367
Tamas Banfe031092020-09-10 17:32:39 +02002368#else /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02002369
Mark Horvathccaf7f82021-01-04 18:16:42 +01002370#define NO_ACTIVE_SLOT UINT32_MAX
2371
David Vinczee574f2d2020-07-10 11:42:03 +02002372/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002373 * Opens all flash areas and checks which contain an image with a valid header.
David Vinczee574f2d2020-07-10 11:42:03 +02002374 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002375 * @param state Boot loader status information.
David Vinczee574f2d2020-07-10 11:42:03 +02002376 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002377 * @return 0 on success; nonzero on failure.
2378 */
2379static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002380boot_get_slot_usage(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002381{
2382 uint32_t slot;
2383 int fa_id;
2384 int rc;
2385 struct image_header *hdr = NULL;
2386
2387 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002388#if BOOT_IMAGE_NUMBER > 1
2389 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2390 continue;
2391 }
2392#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +01002393 /* Open all the slots */
2394 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2395 fa_id = flash_area_id_from_multi_image_slot(
2396 BOOT_CURR_IMG(state), slot);
2397 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
2398 assert(rc == 0);
2399 }
2400
2401 /* Attempt to read an image header from each slot. */
2402 rc = boot_read_image_headers(state, false, NULL);
2403 if (rc != 0) {
2404 BOOT_LOG_WRN("Failed reading image headers.");
2405 return rc;
2406 }
2407
2408 /* Check headers in all slots */
2409 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2410 hdr = boot_img_hdr(state, slot);
2411
2412 if (boot_is_header_valid(hdr, BOOT_IMG_AREA(state, slot))) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002413 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = true;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002414 BOOT_LOG_IMAGE_INFO(slot, hdr);
2415 } else {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002416 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = false;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002417 BOOT_LOG_INF("Image %d %s slot: Image not found",
2418 BOOT_CURR_IMG(state),
2419 (slot == BOOT_PRIMARY_SLOT)
2420 ? "Primary" : "Secondary");
2421 }
2422 }
2423
Raef Colesfe57e7d2021-10-15 11:07:09 +01002424 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002425 }
2426
2427 return 0;
2428}
2429
2430/**
2431 * Finds the slot containing the image with the highest version number for the
2432 * current image.
2433 *
2434 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002435 *
2436 * @return NO_ACTIVE_SLOT if no available slot found, number of
2437 * the found slot otherwise.
David Vinczee574f2d2020-07-10 11:42:03 +02002438 */
2439static uint32_t
Raef Colesfe57e7d2021-10-15 11:07:09 +01002440find_slot_with_highest_version(struct boot_loader_state *state)
David Vinczee574f2d2020-07-10 11:42:03 +02002441{
David Vinczee574f2d2020-07-10 11:42:03 +02002442 uint32_t slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002443 uint32_t candidate_slot = NO_ACTIVE_SLOT;
2444 int rc;
David Vinczee574f2d2020-07-10 11:42:03 +02002445
Mark Horvathccaf7f82021-01-04 18:16:42 +01002446 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002447 if (state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot]) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002448 if (candidate_slot == NO_ACTIVE_SLOT) {
2449 candidate_slot = slot;
2450 } else {
2451 rc = boot_version_cmp(
2452 &boot_img_hdr(state, slot)->ih_ver,
2453 &boot_img_hdr(state, candidate_slot)->ih_ver);
2454 if (rc == 1) {
2455 /* The version of the image being examined is greater than
2456 * the version of the current candidate.
2457 */
2458 candidate_slot = slot;
2459 }
2460 }
David Vinczee574f2d2020-07-10 11:42:03 +02002461 }
2462 }
2463
Mark Horvathccaf7f82021-01-04 18:16:42 +01002464 return candidate_slot;
David Vinczee574f2d2020-07-10 11:42:03 +02002465}
2466
Mark Horvathccaf7f82021-01-04 18:16:42 +01002467#ifdef MCUBOOT_HAVE_LOGGING
2468/**
2469 * Prints the state of the loaded images.
2470 *
2471 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002472 */
2473static void
Raef Colesfe57e7d2021-10-15 11:07:09 +01002474print_loaded_images(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002475{
2476 uint32_t active_slot;
2477
David Brown695e5912021-05-24 16:58:01 -06002478 (void)state;
2479
Mark Horvathccaf7f82021-01-04 18:16:42 +01002480 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002481#if BOOT_IMAGE_NUMBER > 1
2482 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2483 continue;
2484 }
2485#endif
Raef Colesfe57e7d2021-10-15 11:07:09 +01002486 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002487
2488 BOOT_LOG_INF("Image %d loaded from the %s slot",
2489 BOOT_CURR_IMG(state),
2490 (active_slot == BOOT_PRIMARY_SLOT) ?
2491 "primary" : "secondary");
2492 }
2493}
2494#endif
2495
David Vincze1c456242021-06-29 15:25:24 +02002496#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
David Vincze505fba22020-10-22 13:53:29 +02002497/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002498 * Checks whether the active slot of the current image was previously selected
2499 * to run. Erases the image if it was selected but its execution failed,
2500 * otherwise marks it as selected if it has not been before.
David Vincze505fba22020-10-22 13:53:29 +02002501 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002502 * @param state Boot loader status information.
David Vincze505fba22020-10-22 13:53:29 +02002503 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002504 * @return 0 on success; nonzero on failure.
David Vincze505fba22020-10-22 13:53:29 +02002505 */
2506static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002507boot_select_or_erase(struct boot_loader_state *state)
David Vincze505fba22020-10-22 13:53:29 +02002508{
2509 const struct flash_area *fap;
2510 int fa_id;
2511 int rc;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002512 uint32_t active_slot;
2513 struct boot_swap_state* active_swap_state;
David Vincze505fba22020-10-22 13:53:29 +02002514
Raef Colesfe57e7d2021-10-15 11:07:09 +01002515 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002516
2517 fa_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), active_slot);
David Vincze505fba22020-10-22 13:53:29 +02002518 rc = flash_area_open(fa_id, &fap);
2519 assert(rc == 0);
2520
Raef Colesfe57e7d2021-10-15 11:07:09 +01002521 active_swap_state = &(state->slot_usage[BOOT_CURR_IMG(state)].swap_state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002522
2523 memset(active_swap_state, 0, sizeof(struct boot_swap_state));
2524 rc = boot_read_swap_state(fap, active_swap_state);
David Vincze505fba22020-10-22 13:53:29 +02002525 assert(rc == 0);
2526
Mark Horvathccaf7f82021-01-04 18:16:42 +01002527 if (active_swap_state->magic != BOOT_MAGIC_GOOD ||
2528 (active_swap_state->copy_done == BOOT_FLAG_SET &&
2529 active_swap_state->image_ok != BOOT_FLAG_SET)) {
David Vincze505fba22020-10-22 13:53:29 +02002530 /*
2531 * A reboot happened without the image being confirmed at
2532 * runtime or its trailer is corrupted/invalid. Erase the image
2533 * to prevent it from being selected again on the next reboot.
2534 */
2535 BOOT_LOG_DBG("Erasing faulty image in the %s slot.",
Carlos Falgueras Garcíaae13c3c2021-06-21 17:20:31 +02002536 (active_slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
Dominik Ermel260ae092021-04-23 05:38:45 +00002537 rc = flash_area_erase(fap, 0, flash_area_get_size(fap));
David Vincze505fba22020-10-22 13:53:29 +02002538 assert(rc == 0);
2539
2540 flash_area_close(fap);
2541 rc = -1;
2542 } else {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002543 if (active_swap_state->copy_done != BOOT_FLAG_SET) {
2544 if (active_swap_state->copy_done == BOOT_FLAG_BAD) {
David Vincze505fba22020-10-22 13:53:29 +02002545 BOOT_LOG_DBG("The copy_done flag had an unexpected value. Its "
2546 "value was neither 'set' nor 'unset', but 'bad'.");
2547 }
2548 /*
2549 * Set the copy_done flag, indicating that the image has been
2550 * selected to boot. It can be set in advance, before even
2551 * validating the image, because in case the validation fails, the
2552 * entire image slot will be erased (including the trailer).
2553 */
2554 rc = boot_write_copy_done(fap);
2555 if (rc != 0) {
2556 BOOT_LOG_WRN("Failed to set copy_done flag of the image in "
Carlos Falgueras Garcíaae13c3c2021-06-21 17:20:31 +02002557 "the %s slot.", (active_slot == BOOT_PRIMARY_SLOT) ?
David Vincze505fba22020-10-22 13:53:29 +02002558 "primary" : "secondary");
2559 rc = 0;
2560 }
2561 }
2562 flash_area_close(fap);
2563 }
2564
2565 return rc;
2566}
David Vincze1c456242021-06-29 15:25:24 +02002567#endif /* MCUBOOT_DIRECT_XIP && MCUBOOT_DIRECT_XIP_REVERT */
David Vincze505fba22020-10-22 13:53:29 +02002568
Tamas Banfe031092020-09-10 17:32:39 +02002569#ifdef MCUBOOT_RAM_LOAD
2570
Mark Horvathccaf7f82021-01-04 18:16:42 +01002571#ifndef MULTIPLE_EXECUTABLE_RAM_REGIONS
Tamas Banfe031092020-09-10 17:32:39 +02002572#if !defined(IMAGE_EXECUTABLE_RAM_START) || !defined(IMAGE_EXECUTABLE_RAM_SIZE)
2573#error "Platform MUST define executable RAM bounds in case of RAM_LOAD"
2574#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +01002575#endif
Tamas Banfe031092020-09-10 17:32:39 +02002576
2577/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002578 * Verifies that the active slot of the current image can be loaded within the
2579 * predefined bounds that are allowed to be used by executable images.
Tamas Banfe031092020-09-10 17:32:39 +02002580 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002581 * @param state Boot loader status information.
Tamas Banfe031092020-09-10 17:32:39 +02002582 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002583 * @return 0 on success; nonzero on failure.
Tamas Banfe031092020-09-10 17:32:39 +02002584 */
2585static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002586boot_verify_ram_load_address(struct boot_loader_state *state)
Tamas Banfe031092020-09-10 17:32:39 +02002587{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002588 uint32_t img_dst;
2589 uint32_t img_sz;
Tamas Banfe031092020-09-10 17:32:39 +02002590 uint32_t img_end_addr;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002591 uint32_t exec_ram_start;
2592 uint32_t exec_ram_size;
David Brown695e5912021-05-24 16:58:01 -06002593
2594 (void)state;
2595
Mark Horvathccaf7f82021-01-04 18:16:42 +01002596#ifdef MULTIPLE_EXECUTABLE_RAM_REGIONS
2597 int rc;
Tamas Banfe031092020-09-10 17:32:39 +02002598
Mark Horvathccaf7f82021-01-04 18:16:42 +01002599 rc = boot_get_image_exec_ram_info(BOOT_CURR_IMG(state), &exec_ram_start,
2600 &exec_ram_size);
2601 if (rc != 0) {
2602 return BOOT_EBADSTATUS;
2603 }
2604#else
2605 exec_ram_start = IMAGE_EXECUTABLE_RAM_START;
2606 exec_ram_size = IMAGE_EXECUTABLE_RAM_SIZE;
2607#endif
2608
Raef Colesfe57e7d2021-10-15 11:07:09 +01002609 img_dst = state->slot_usage[BOOT_CURR_IMG(state)].img_dst;
2610 img_sz = state->slot_usage[BOOT_CURR_IMG(state)].img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002611
2612 if (img_dst < exec_ram_start) {
Tamas Banfe031092020-09-10 17:32:39 +02002613 return BOOT_EBADIMAGE;
2614 }
2615
2616 if (!boot_u32_safe_add(&img_end_addr, img_dst, img_sz)) {
2617 return BOOT_EBADIMAGE;
2618 }
2619
Mark Horvathccaf7f82021-01-04 18:16:42 +01002620 if (img_end_addr > (exec_ram_start + exec_ram_size)) {
Tamas Banfe031092020-09-10 17:32:39 +02002621 return BOOT_EBADIMAGE;
2622 }
2623
2624 return 0;
2625}
2626
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002627#ifdef MCUBOOT_ENC_IMAGES
2628
2629/**
2630 * Copies and decrypts an image from a slot in the flash to an SRAM address.
2631 *
2632 * @param state Boot loader status information.
2633 * @param slot The flash slot of the image to be copied to SRAM.
2634 * @param hdr The image header.
2635 * @param src_sz Size of the image.
2636 * @param img_dst Pointer to the address at which the image needs to be
2637 * copied to SRAM.
2638 *
2639 * @return 0 on success; nonzero on failure.
2640 */
2641static int
2642boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state,
2643 uint32_t slot, struct image_header *hdr,
2644 uint32_t src_sz, uint32_t img_dst)
2645{
2646 /* The flow for the decryption and copy of the image is as follows :
2647 * 1. The whole image is copied to the RAM (header + payload + TLV).
2648 * 2. The encryption key is loaded from the TLV in flash.
2649 * 3. The image is then decrypted chunk by chunk in RAM (1 chunk
2650 * is 1024 bytes). Only the payload section is decrypted.
2651 * 4. The image is authenticated in RAM.
2652 */
2653 const struct flash_area *fap_src = NULL;
2654 struct boot_status bs;
2655 uint32_t blk_off;
2656 uint32_t tlv_off;
2657 uint32_t blk_sz;
2658 uint32_t bytes_copied = hdr->ih_hdr_size;
2659 uint32_t chunk_sz;
2660 uint32_t max_sz = 1024;
2661 uint16_t idx;
2662 uint8_t image_index;
2663 uint8_t * cur_dst;
2664 int area_id;
2665 int rc;
2666 uint8_t * ram_dst = (void *)(IMAGE_RAM_BASE + img_dst);
2667
2668 image_index = BOOT_CURR_IMG(state);
2669 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2670 rc = flash_area_open(area_id, &fap_src);
2671 if (rc != 0){
2672 return BOOT_EFLASH;
2673 }
2674
2675 tlv_off = BOOT_TLV_OFF(hdr);
2676
2677 /* Copying the whole image in RAM */
2678 rc = flash_area_read(fap_src, 0, ram_dst, src_sz);
2679 if (rc != 0) {
2680 goto done;
2681 }
2682
2683 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap_src, &bs);
2684 if (rc < 0) {
2685 goto done;
2686 }
2687
2688 /* if rc > 0 then the key has already been loaded */
2689 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), slot, &bs)) {
2690 goto done;
2691 }
2692
2693 /* Starting at the end of the header as the header section is not encrypted */
2694 while (bytes_copied < tlv_off) { /* TLV section copied previously */
2695 if (src_sz - bytes_copied > max_sz) {
2696 chunk_sz = max_sz;
2697 } else {
2698 chunk_sz = src_sz - bytes_copied;
2699 }
2700
2701 cur_dst = ram_dst + bytes_copied;
2702 blk_sz = chunk_sz;
2703 idx = 0;
2704 if (bytes_copied + chunk_sz > tlv_off) {
2705 /* Going over TLV section
2706 * Part of the chunk is encrypted payload */
2707 blk_off = ((bytes_copied) - hdr->ih_hdr_size) & 0xf;
2708 blk_sz = tlv_off - (bytes_copied);
2709 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
2710 (bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
2711 blk_off, cur_dst);
2712 } else {
2713 /* Image encrypted payload section */
2714 blk_off = ((bytes_copied) - hdr->ih_hdr_size) & 0xf;
2715 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
2716 (bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
2717 blk_off, cur_dst);
2718 }
2719
2720 bytes_copied += chunk_sz;
2721 }
2722 rc = 0;
2723
2724done:
2725 flash_area_close(fap_src);
2726
2727 return rc;
2728}
2729
2730#endif /* MCUBOOT_ENC_IMAGES */
Tamas Banfe031092020-09-10 17:32:39 +02002731/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002732 * Copies a slot of the current image into SRAM.
Tamas Banfe031092020-09-10 17:32:39 +02002733 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002734 * @param state Boot loader status information.
Tamas Banfe031092020-09-10 17:32:39 +02002735 * @param slot The flash slot of the image to be copied to SRAM.
2736 * @param img_dst The address at which the image needs to be copied to
2737 * SRAM.
2738 * @param img_sz The size of the image that needs to be copied to SRAM.
2739 *
2740 * @return 0 on success; nonzero on failure.
2741 */
2742static int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002743boot_copy_image_to_sram(struct boot_loader_state *state, int slot,
2744 uint32_t img_dst, uint32_t img_sz)
Tamas Banfe031092020-09-10 17:32:39 +02002745{
2746 int rc;
2747 const struct flash_area *fap_src = NULL;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002748 int area_id;
Tamas Banfe031092020-09-10 17:32:39 +02002749
Mark Horvathccaf7f82021-01-04 18:16:42 +01002750#if (BOOT_IMAGE_NUMBER == 1)
2751 (void)state;
2752#endif
2753
2754 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2755
2756 rc = flash_area_open(area_id, &fap_src);
Tamas Banfe031092020-09-10 17:32:39 +02002757 if (rc != 0) {
2758 return BOOT_EFLASH;
2759 }
2760
2761 /* Direct copy from flash to its new location in SRAM. */
David Brown9bd7f902021-05-26 16:31:14 -06002762 rc = flash_area_read(fap_src, 0, (void *)(IMAGE_RAM_BASE + img_dst), img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002763 if (rc != 0) {
Antonio de Angelis48547002023-04-14 09:47:09 +01002764 BOOT_LOG_INF("Error whilst copying image %d from Flash to SRAM: %d",
2765 BOOT_CURR_IMG(state), rc);
Tamas Banfe031092020-09-10 17:32:39 +02002766 }
2767
2768 flash_area_close(fap_src);
2769
2770 return rc;
2771}
2772
Mark Horvathccaf7f82021-01-04 18:16:42 +01002773#if (BOOT_IMAGE_NUMBER > 1)
Tamas Banfe031092020-09-10 17:32:39 +02002774/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002775 * Checks if two memory regions (A and B) are overlap or not.
Tamas Banfe031092020-09-10 17:32:39 +02002776 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002777 * @param start_a Start of the A region.
2778 * @param end_a End of the A region.
2779 * @param start_b Start of the B region.
2780 * @param end_b End of the B region.
Tamas Banfe031092020-09-10 17:32:39 +02002781 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002782 * @return true if there is overlap; false otherwise.
2783 */
2784static bool
2785do_regions_overlap(uint32_t start_a, uint32_t end_a,
2786 uint32_t start_b, uint32_t end_b)
2787{
2788 if (start_b > end_a) {
2789 return false;
2790 } else if (start_b >= start_a) {
2791 return true;
2792 } else if (end_b > start_a) {
2793 return true;
2794 }
2795
2796 return false;
2797}
2798
2799/**
2800 * Checks if the image we want to load to memory overlap with an already
2801 * ramloaded image.
2802 *
Raef Colesfe57e7d2021-10-15 11:07:09 +01002803 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002804 *
2805 * @return 0 if there is no overlap; nonzero otherwise.
Tamas Banfe031092020-09-10 17:32:39 +02002806 */
2807static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002808boot_check_ram_load_overlapping(struct boot_loader_state *state)
Tamas Banfe031092020-09-10 17:32:39 +02002809{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002810 uint32_t i;
2811
2812 uint32_t start_a;
2813 uint32_t end_a;
2814 uint32_t start_b;
2815 uint32_t end_b;
Raef Colesfe57e7d2021-10-15 11:07:09 +01002816 uint32_t image_id_to_check = BOOT_CURR_IMG(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002817
Raef Colesfe57e7d2021-10-15 11:07:09 +01002818 start_a = state->slot_usage[image_id_to_check].img_dst;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002819 /* Safe to add here, values are already verified in
2820 * boot_verify_ram_load_address() */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002821 end_a = start_a + state->slot_usage[image_id_to_check].img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002822
2823 for (i = 0; i < BOOT_IMAGE_NUMBER; i++) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002824 if (state->slot_usage[i].active_slot == NO_ACTIVE_SLOT
Mark Horvathccaf7f82021-01-04 18:16:42 +01002825 || i == image_id_to_check) {
2826 continue;
2827 }
2828
Raef Colesfe57e7d2021-10-15 11:07:09 +01002829 start_b = state->slot_usage[i].img_dst;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002830 /* Safe to add here, values are already verified in
2831 * boot_verify_ram_load_address() */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002832 end_b = start_b + state->slot_usage[i].img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002833
2834 if (do_regions_overlap(start_a, end_a, start_b, end_b)) {
2835 return -1;
2836 }
2837 }
2838
2839 return 0;
2840}
2841#endif
2842
2843/**
2844 * Loads the active slot of the current image into SRAM. The load address and
2845 * image size is extracted from the image header.
2846 *
2847 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002848 *
2849 * @return 0 on success; nonzero on failure.
2850 */
2851static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002852boot_load_image_to_sram(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002853{
2854 uint32_t active_slot;
2855 struct image_header *hdr = NULL;
2856 uint32_t img_dst;
2857 uint32_t img_sz;
Tamas Banfe031092020-09-10 17:32:39 +02002858 int rc;
2859
Raef Colesfe57e7d2021-10-15 11:07:09 +01002860 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002861 hdr = boot_img_hdr(state, active_slot);
2862
Tamas Banfe031092020-09-10 17:32:39 +02002863 if (hdr->ih_flags & IMAGE_F_RAM_LOAD) {
2864
Mark Horvathccaf7f82021-01-04 18:16:42 +01002865 img_dst = hdr->ih_load_addr;
Tamas Banfe031092020-09-10 17:32:39 +02002866
Mark Horvathccaf7f82021-01-04 18:16:42 +01002867 rc = boot_read_image_size(state, active_slot, &img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002868 if (rc != 0) {
2869 return rc;
2870 }
2871
Raef Colesfe57e7d2021-10-15 11:07:09 +01002872 state->slot_usage[BOOT_CURR_IMG(state)].img_dst = img_dst;
2873 state->slot_usage[BOOT_CURR_IMG(state)].img_sz = img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002874
Raef Colesfe57e7d2021-10-15 11:07:09 +01002875 rc = boot_verify_ram_load_address(state);
Tamas Banfe031092020-09-10 17:32:39 +02002876 if (rc != 0) {
Antonio de Angelisba5fb1c2022-10-11 10:10:37 +01002877 BOOT_LOG_INF("Image %d RAM load address 0x%x is invalid.", BOOT_CURR_IMG(state), img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002878 return rc;
2879 }
2880
Mark Horvathccaf7f82021-01-04 18:16:42 +01002881#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesfe57e7d2021-10-15 11:07:09 +01002882 rc = boot_check_ram_load_overlapping(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002883 if (rc != 0) {
Antonio de Angelisba5fb1c2022-10-11 10:10:37 +01002884 BOOT_LOG_INF("Image %d RAM loading to address 0x%x would overlap with\
2885 another image.", BOOT_CURR_IMG(state), img_dst);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002886 return rc;
2887 }
2888#endif
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002889#ifdef MCUBOOT_ENC_IMAGES
2890 /* decrypt image if encrypted and copy it to RAM */
2891 if (IS_ENCRYPTED(hdr)) {
2892 rc = boot_decrypt_and_copy_image_to_sram(state, active_slot, hdr, img_sz, img_dst);
2893 } else {
2894 rc = boot_copy_image_to_sram(state, active_slot, img_dst, img_sz);
2895 }
2896#else
Tamas Banfe031092020-09-10 17:32:39 +02002897 /* Copy image to the load address from where it currently resides in
2898 * flash.
2899 */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002900 rc = boot_copy_image_to_sram(state, active_slot, img_dst, img_sz);
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002901#endif
Tamas Banfe031092020-09-10 17:32:39 +02002902 if (rc != 0) {
Antonio de Angelisba5fb1c2022-10-11 10:10:37 +01002903 BOOT_LOG_INF("Image %d RAM loading to 0x%x is failed.", BOOT_CURR_IMG(state), img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002904 } else {
Antonio de Angelisba5fb1c2022-10-11 10:10:37 +01002905 BOOT_LOG_INF("Image %d RAM loading to 0x%x is succeeded.", BOOT_CURR_IMG(state), img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002906 }
2907 } else {
2908 /* Only images that support IMAGE_F_RAM_LOAD are allowed if
2909 * MCUBOOT_RAM_LOAD is set.
2910 */
2911 rc = BOOT_EBADIMAGE;
2912 }
2913
Mark Horvathccaf7f82021-01-04 18:16:42 +01002914 if (rc != 0) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002915 state->slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
2916 state->slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002917 }
2918
Tamas Banfe031092020-09-10 17:32:39 +02002919 return rc;
2920}
2921
2922/**
2923 * Removes an image from SRAM, by overwriting it with zeros.
2924 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002925 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002926 *
2927 * @return 0 on success; nonzero on failure.
2928 */
2929static inline int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002930boot_remove_image_from_sram(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002931{
David Brown695e5912021-05-24 16:58:01 -06002932 (void)state;
2933
Antonio de Angelisba5fb1c2022-10-11 10:10:37 +01002934 BOOT_LOG_INF("Removing image %d from SRAM at address 0x%x",
2935 BOOT_CURR_IMG(state),
Raef Colesfe57e7d2021-10-15 11:07:09 +01002936 state->slot_usage[BOOT_CURR_IMG(state)].img_dst);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002937
Raef Colesfe57e7d2021-10-15 11:07:09 +01002938 memset((void*)(IMAGE_RAM_BASE + state->slot_usage[BOOT_CURR_IMG(state)].img_dst),
2939 0, state->slot_usage[BOOT_CURR_IMG(state)].img_sz);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002940
Raef Colesfe57e7d2021-10-15 11:07:09 +01002941 state->slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
2942 state->slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002943
2944 return 0;
2945}
2946
2947/**
2948 * Removes an image from flash by erasing the corresponding flash area
2949 *
2950 * @param state Boot loader status information.
2951 * @param slot The flash slot of the image to be erased.
Tamas Banfe031092020-09-10 17:32:39 +02002952 *
2953 * @return 0 on success; nonzero on failure.
2954 */
2955static inline int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002956boot_remove_image_from_flash(struct boot_loader_state *state, uint32_t slot)
Tamas Banfe031092020-09-10 17:32:39 +02002957{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002958 int area_id;
2959 int rc;
2960 const struct flash_area *fap;
Tamas Banfe031092020-09-10 17:32:39 +02002961
David Brown695e5912021-05-24 16:58:01 -06002962 (void)state;
2963
Mark Horvathccaf7f82021-01-04 18:16:42 +01002964 BOOT_LOG_INF("Removing image %d slot %d from flash", BOOT_CURR_IMG(state),
2965 slot);
2966 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2967 rc = flash_area_open(area_id, &fap);
2968 if (rc == 0) {
Dominik Ermel260ae092021-04-23 05:38:45 +00002969 flash_area_erase(fap, 0, flash_area_get_size(fap));
Dominik Ermel245de812022-09-02 13:39:23 +00002970 flash_area_close(fap);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002971 }
2972
2973 return rc;
Tamas Banfe031092020-09-10 17:32:39 +02002974}
2975#endif /* MCUBOOT_RAM_LOAD */
2976
Mark Horvathccaf7f82021-01-04 18:16:42 +01002977#if (BOOT_IMAGE_NUMBER > 1)
2978/**
2979 * Checks the image dependency whether it is satisfied.
2980 *
2981 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002982 * @param dep Image dependency which has to be verified.
2983 *
2984 * @return 0 if dependencies are met; nonzero otherwise.
2985 */
2986static int
2987boot_verify_slot_dependency(struct boot_loader_state *state,
Mark Horvathccaf7f82021-01-04 18:16:42 +01002988 struct image_dependency *dep)
David Vinczee574f2d2020-07-10 11:42:03 +02002989{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002990 struct image_version *dep_version;
2991 uint32_t dep_slot;
David Vinczee574f2d2020-07-10 11:42:03 +02002992 int rc;
2993
Mark Horvathccaf7f82021-01-04 18:16:42 +01002994 /* Determine the source of the image which is the subject of
2995 * the dependency and get it's version.
David Vinczee574f2d2020-07-10 11:42:03 +02002996 */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002997 dep_slot = state->slot_usage[dep->image_id].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002998 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
2999
3000 rc = boot_version_cmp(dep_version, &dep->image_min_version);
3001 if (rc >= 0) {
3002 /* Dependency satisfied. */
3003 rc = 0;
David Vinczee574f2d2020-07-10 11:42:03 +02003004 }
3005
Mark Horvathccaf7f82021-01-04 18:16:42 +01003006 return rc;
3007}
3008
3009/**
3010 * Reads all dependency TLVs of an image and verifies one after another to see
3011 * if they are all satisfied.
3012 *
3013 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01003014 *
3015 * @return 0 if dependencies are met; nonzero otherwise.
3016 */
3017static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01003018boot_verify_slot_dependencies(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003019{
3020 uint32_t active_slot;
3021 const struct flash_area *fap;
3022 struct image_tlv_iter it;
3023 struct image_dependency dep;
3024 uint32_t off;
3025 uint16_t len;
3026 int area_id;
3027 int rc;
3028
Raef Colesfe57e7d2021-10-15 11:07:09 +01003029 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003030
3031 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state),
3032 active_slot);
3033 rc = flash_area_open(area_id, &fap);
David Vinczee574f2d2020-07-10 11:42:03 +02003034 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01003035 rc = BOOT_EFLASH;
3036 goto done;
David Vinczee574f2d2020-07-10 11:42:03 +02003037 }
3038
Mark Horvathccaf7f82021-01-04 18:16:42 +01003039 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, active_slot), fap,
3040 IMAGE_TLV_DEPENDENCY, true);
3041 if (rc != 0) {
3042 goto done;
3043 }
David Vinczee574f2d2020-07-10 11:42:03 +02003044
Mark Horvathccaf7f82021-01-04 18:16:42 +01003045 while (true) {
3046 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
3047 if (rc < 0) {
3048 return -1;
3049 } else if (rc > 0) {
3050 rc = 0;
3051 break;
3052 }
David Vinczee574f2d2020-07-10 11:42:03 +02003053
Mark Horvathccaf7f82021-01-04 18:16:42 +01003054 if (len != sizeof(dep)) {
3055 rc = BOOT_EBADIMAGE;
3056 goto done;
3057 }
3058
3059 rc = LOAD_IMAGE_DATA(boot_img_hdr(state, active_slot),
3060 fap, off, &dep, len);
3061 if (rc != 0) {
3062 rc = BOOT_EFLASH;
3063 goto done;
3064 }
3065
3066 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
3067 rc = BOOT_EBADARGS;
3068 goto done;
3069 }
3070
Raef Colesfe57e7d2021-10-15 11:07:09 +01003071 rc = boot_verify_slot_dependency(state, &dep);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003072 if (rc != 0) {
3073 /* Dependency not satisfied. */
3074 goto done;
3075 }
3076 }
3077
3078done:
3079 flash_area_close(fap);
3080 return rc;
3081}
3082
3083/**
3084 * Checks the dependency of all the active slots. If an image found with
3085 * invalid or not satisfied dependencies the image is removed from SRAM (in
3086 * case of MCUBOOT_RAM_LOAD strategy) and its slot is set to unavailable.
3087 *
3088 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01003089 *
3090 * @return 0 if dependencies are met; nonzero otherwise.
3091 */
3092static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01003093boot_verify_dependencies(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003094{
3095 int rc = -1;
3096 uint32_t active_slot;
3097
3098 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01003099 if (state->img_mask[BOOT_CURR_IMG(state)]) {
3100 continue;
3101 }
Raef Colesfe57e7d2021-10-15 11:07:09 +01003102 rc = boot_verify_slot_dependencies(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003103 if (rc != 0) {
3104 /* Dependencies not met or invalid dependencies. */
3105
3106#ifdef MCUBOOT_RAM_LOAD
Raef Colesfe57e7d2021-10-15 11:07:09 +01003107 boot_remove_image_from_sram(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003108#endif /* MCUBOOT_RAM_LOAD */
3109
Raef Colesfe57e7d2021-10-15 11:07:09 +01003110 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
3111 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3112 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003113
3114 return rc;
3115 }
3116 }
3117
3118 return rc;
3119}
3120#endif /* (BOOT_IMAGE_NUMBER > 1) */
3121
3122/**
3123 * Tries to load a slot for all the images with validation.
3124 *
3125 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01003126 *
3127 * @return 0 on success; nonzero on failure.
3128 */
Michael Grand5047f032022-11-24 16:49:56 +01003129fih_ret
Raef Colesfe57e7d2021-10-15 11:07:09 +01003130boot_load_and_validate_images(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003131{
3132 uint32_t active_slot;
3133 int rc;
Michael Grand5047f032022-11-24 16:49:56 +01003134 fih_ret fih_rc;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003135
3136 /* Go over all the images and try to load one */
3137 IMAGES_ITER(BOOT_CURR_IMG(state)) {
3138 /* All slots tried until a valid image found. Breaking from this loop
3139 * means that a valid image found or already loaded. If no slot is
3140 * found the function returns with error code. */
3141 while (true) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01003142 /* Go over all the slots and try to load one */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003143 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003144 if (active_slot != NO_ACTIVE_SLOT){
3145 /* A slot is already active, go to next image. */
3146 break;
David Vinczee574f2d2020-07-10 11:42:03 +02003147 }
David Vincze505fba22020-10-22 13:53:29 +02003148
Raef Colesfe57e7d2021-10-15 11:07:09 +01003149 active_slot = find_slot_with_highest_version(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003150 if (active_slot == NO_ACTIVE_SLOT) {
3151 BOOT_LOG_INF("No slot to load for image %d",
3152 BOOT_CURR_IMG(state));
3153 FIH_RET(FIH_FAILURE);
3154 }
3155
3156 /* Save the number of the active slot. */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003157 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003158
Raef Colesf11de642021-10-15 11:11:56 +01003159#if BOOT_IMAGE_NUMBER > 1
3160 if (state->img_mask[BOOT_CURR_IMG(state)]) {
3161 continue;
3162 }
3163#endif
3164
Mark Horvathccaf7f82021-01-04 18:16:42 +01003165#ifdef MCUBOOT_DIRECT_XIP
Raef Colesfe57e7d2021-10-15 11:07:09 +01003166 rc = boot_rom_address_check(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003167 if (rc != 0) {
3168 /* The image is placed in an unsuitable slot. */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003169 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3170 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003171 continue;
3172 }
George Becksteind4d90f82021-05-11 02:00:00 -04003173
David Vincze505fba22020-10-22 13:53:29 +02003174#ifdef MCUBOOT_DIRECT_XIP_REVERT
Raef Colesfe57e7d2021-10-15 11:07:09 +01003175 rc = boot_select_or_erase(state);
David Vincze505fba22020-10-22 13:53:29 +02003176 if (rc != 0) {
3177 /* The selected image slot has been erased. */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003178 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3179 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
David Vincze505fba22020-10-22 13:53:29 +02003180 continue;
3181 }
3182#endif /* MCUBOOT_DIRECT_XIP_REVERT */
David Vincze1c456242021-06-29 15:25:24 +02003183#endif /* MCUBOOT_DIRECT_XIP */
David Vincze505fba22020-10-22 13:53:29 +02003184
Tamas Banfe031092020-09-10 17:32:39 +02003185#ifdef MCUBOOT_RAM_LOAD
3186 /* Image is first loaded to RAM and authenticated there in order to
3187 * prevent TOCTOU attack during image copy. This could be applied
3188 * when loading images from external (untrusted) flash to internal
3189 * (trusted) RAM and image is authenticated before copying.
3190 */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003191 rc = boot_load_image_to_sram(state);
Tamas Banfe031092020-09-10 17:32:39 +02003192 if (rc != 0 ) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01003193 /* Image cannot be ramloaded. */
3194 boot_remove_image_from_flash(state, active_slot);
Raef Colesfe57e7d2021-10-15 11:07:09 +01003195 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3196 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Tamas Banfe031092020-09-10 17:32:39 +02003197 continue;
Tamas Banfe031092020-09-10 17:32:39 +02003198 }
3199#endif /* MCUBOOT_RAM_LOAD */
Mark Horvathccaf7f82021-01-04 18:16:42 +01003200
3201 FIH_CALL(boot_validate_slot, fih_rc, state, active_slot, NULL);
Michael Grand5047f032022-11-24 16:49:56 +01003202 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01003203 /* Image is invalid. */
Tamas Banfe031092020-09-10 17:32:39 +02003204#ifdef MCUBOOT_RAM_LOAD
Raef Colesfe57e7d2021-10-15 11:07:09 +01003205 boot_remove_image_from_sram(state);
Tamas Banfe031092020-09-10 17:32:39 +02003206#endif /* MCUBOOT_RAM_LOAD */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003207 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3208 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003209 continue;
David Vincze505fba22020-10-22 13:53:29 +02003210 }
Mark Horvathccaf7f82021-01-04 18:16:42 +01003211
3212 /* Valid image loaded from a slot, go to next image. */
3213 break;
3214 }
3215 }
3216
3217 FIH_RET(FIH_SUCCESS);
3218}
3219
3220/**
3221 * Updates the security counter for the current image.
3222 *
3223 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01003224 *
3225 * @return 0 on success; nonzero on failure.
3226 */
3227static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01003228boot_update_hw_rollback_protection(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003229{
3230#ifdef MCUBOOT_HW_ROLLBACK_PROT
3231 int rc;
3232
3233 /* Update the stored security counter with the newer (active) image's
3234 * security counter value.
3235 */
David Vincze1c456242021-06-29 15:25:24 +02003236#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003237 /* When the 'revert' mechanism is enabled in direct-xip mode, the
3238 * security counter can be increased only after reboot, if the image
3239 * has been confirmed at runtime (the image_ok flag has been set).
3240 * This way a 'revert' can be performed when it's necessary.
3241 */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003242 if (state->slot_usage[BOOT_CURR_IMG(state)].swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze505fba22020-10-22 13:53:29 +02003243#endif
Sherry Zhang50b06ae2021-07-09 15:22:51 +08003244 rc = boot_update_security_counter(BOOT_CURR_IMG(state),
Raef Colesfe57e7d2021-10-15 11:07:09 +01003245 state->slot_usage[BOOT_CURR_IMG(state)].active_slot,
3246 boot_img_hdr(state, state->slot_usage[BOOT_CURR_IMG(state)].active_slot));
David Vinczee574f2d2020-07-10 11:42:03 +02003247 if (rc != 0) {
Antonio de Angelisba5fb1c2022-10-11 10:10:37 +01003248 BOOT_LOG_ERR("Security counter update failed after image %d validation.", BOOT_CURR_IMG(state));
Mark Horvathccaf7f82021-01-04 18:16:42 +01003249 return rc;
David Vinczee574f2d2020-07-10 11:42:03 +02003250 }
David Vincze1c456242021-06-29 15:25:24 +02003251#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003252 }
3253#endif
David Vinczee574f2d2020-07-10 11:42:03 +02003254
Mark Horvathccaf7f82021-01-04 18:16:42 +01003255 return 0;
David Vinczee574f2d2020-07-10 11:42:03 +02003256
Mark Horvathccaf7f82021-01-04 18:16:42 +01003257#else /* MCUBOOT_HW_ROLLBACK_PROT */
3258 (void) (state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003259 return 0;
3260#endif
3261}
3262
Michael Grand5047f032022-11-24 16:49:56 +01003263fih_ret
Mark Horvathccaf7f82021-01-04 18:16:42 +01003264context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
3265{
Mark Horvathccaf7f82021-01-04 18:16:42 +01003266 int rc;
Michael Grand5047f032022-11-24 16:49:56 +01003267 FIH_DECLARE(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003268
Raef Colesfe57e7d2021-10-15 11:07:09 +01003269 rc = boot_get_slot_usage(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003270 if (rc != 0) {
David Vinczee574f2d2020-07-10 11:42:03 +02003271 goto out;
3272 }
3273
Mark Horvathccaf7f82021-01-04 18:16:42 +01003274#if (BOOT_IMAGE_NUMBER > 1)
3275 while (true) {
3276#endif
Raef Colesfe57e7d2021-10-15 11:07:09 +01003277 FIH_CALL(boot_load_and_validate_images, fih_rc, state);
Michael Grand5047f032022-11-24 16:49:56 +01003278 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
3279 FIH_SET(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003280 goto out;
3281 }
3282
3283#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesfe57e7d2021-10-15 11:07:09 +01003284 rc = boot_verify_dependencies(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003285 if (rc != 0) {
3286 /* Dependency check failed for an image, it has been removed from
3287 * SRAM in case of MCUBOOT_RAM_LOAD strategy, and set to
3288 * unavailable. Try to load an image from another slot.
3289 */
3290 continue;
3291 }
3292 /* Dependency check was successful. */
3293 break;
3294 }
3295#endif
3296
3297 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01003298#if BOOT_IMAGE_NUMBER > 1
3299 if (state->img_mask[BOOT_CURR_IMG(state)]) {
3300 continue;
3301 }
3302#endif
Raef Colesfe57e7d2021-10-15 11:07:09 +01003303 rc = boot_update_hw_rollback_protection(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003304 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01003305 FIH_SET(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003306 goto out;
3307 }
3308
Jamie McCrae3016d002023-03-14 12:35:51 +00003309 rc = boot_add_shared_data(state, (uint8_t)state->slot_usage[BOOT_CURR_IMG(state)].active_slot);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003310 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01003311 FIH_SET(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003312 goto out;
3313 }
3314 }
3315
3316 /* All image loaded successfully. */
3317#ifdef MCUBOOT_HAVE_LOGGING
Raef Colesfe57e7d2021-10-15 11:07:09 +01003318 print_loaded_images(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003319#endif
3320
Raef Colesfe57e7d2021-10-15 11:07:09 +01003321 fill_rsp(state, rsp);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003322
David Vinczee574f2d2020-07-10 11:42:03 +02003323out:
Mark Horvathccaf7f82021-01-04 18:16:42 +01003324 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01003325
Michael Grand5047f032022-11-24 16:49:56 +01003326 if (rc != 0) {
3327 FIH_SET(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003328 }
Raef Colese8fe6cf2020-05-26 13:07:40 +01003329
Mark Horvathccaf7f82021-01-04 18:16:42 +01003330 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02003331}
Tamas Banfe031092020-09-10 17:32:39 +02003332#endif /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02003333
3334/**
Raef Colese8fe6cf2020-05-26 13:07:40 +01003335 * Prepares the booting process. This function moves images around in flash as
David Vinczee574f2d2020-07-10 11:42:03 +02003336 * appropriate, and tells you what address to boot from.
3337 *
3338 * @param rsp On success, indicates how booting should occur.
3339 *
Raef Colese8fe6cf2020-05-26 13:07:40 +01003340 * @return FIH_SUCCESS on success; nonzero on failure.
David Vinczee574f2d2020-07-10 11:42:03 +02003341 */
Michael Grand5047f032022-11-24 16:49:56 +01003342fih_ret
David Vinczee574f2d2020-07-10 11:42:03 +02003343boot_go(struct boot_rsp *rsp)
3344{
Michael Grand5047f032022-11-24 16:49:56 +01003345 FIH_DECLARE(fih_rc, FIH_FAILURE);
Raef Colesf11de642021-10-15 11:11:56 +01003346
3347 boot_state_clear(NULL);
3348
Raef Colese8fe6cf2020-05-26 13:07:40 +01003349 FIH_CALL(context_boot_go, fih_rc, &boot_data, rsp);
3350 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02003351}
Raef Colesf11de642021-10-15 11:11:56 +01003352
3353/**
3354 * Prepares the booting process, considering only a single image. This function
3355 * moves images around in flash as appropriate, and tells you what address to
3356 * boot from.
3357 *
3358 * @param rsp On success, indicates how booting should occur.
3359 *
3360 * @param image_id The image ID to prepare the boot process for.
3361 *
3362 * @return FIH_SUCCESS on success; nonzero on failure.
3363 */
Michael Grand5047f032022-11-24 16:49:56 +01003364fih_ret
Raef Colesf11de642021-10-15 11:11:56 +01003365boot_go_for_image_id(struct boot_rsp *rsp, uint32_t image_id)
3366{
Michael Grand5047f032022-11-24 16:49:56 +01003367 FIH_DECLARE(fih_rc, FIH_FAILURE);
Raef Colesf11de642021-10-15 11:11:56 +01003368
3369 if (image_id >= BOOT_IMAGE_NUMBER) {
3370 FIH_RET(FIH_FAILURE);
3371 }
3372
3373#if BOOT_IMAGE_NUMBER > 1
3374 memset(&boot_data.img_mask, 1, BOOT_IMAGE_NUMBER);
3375 boot_data.img_mask[image_id] = 0;
3376#endif
3377
3378 FIH_CALL(context_boot_go, fih_rc, &boot_data, rsp);
3379 FIH_RET(fih_rc);
3380}
3381
3382/**
3383 * Clears the boot state, so that previous operations have no effect on new
3384 * ones.
3385 *
3386 * @param state The state that should be cleared. If the value
3387 * is NULL, the default bootloader state will be
3388 * cleared.
3389 */
3390void boot_state_clear(struct boot_loader_state *state)
3391{
3392 if (state != NULL) {
3393 memset(state, 0, sizeof(struct boot_loader_state));
3394 } else {
3395 memset(&boot_data, 0, sizeof(struct boot_loader_state));
3396 }
3397}