blob: 5f99f002f064b38dbb4260387f93ff4b99a50db0 [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
David Brownaac71112020-02-03 16:13:42 -07007 *
8 * Original license:
9 *
Christopher Collins92ea77f2016-12-12 15:59:26 -080010 * Licensed to the Apache Software Foundation (ASF) under one
11 * or more contributor license agreements. See the NOTICE file
12 * distributed with this work for additional information
13 * regarding copyright ownership. The ASF licenses this file
14 * to you under the Apache License, Version 2.0 (the
15 * "License"); you may not use this file except in compliance
16 * with the License. You may obtain a copy of the License at
17 *
18 * http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing,
21 * software distributed under the License is distributed on an
22 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
23 * KIND, either express or implied. See the License for the
24 * specific language governing permissions and limitations
25 * under the License.
26 */
27
28/**
29 * This file provides an interface to the boot loader. Functions defined in
30 * this file should only be called while the boot loader is running.
31 */
32
Christopher Collins92ea77f2016-12-12 15:59:26 -080033#include <stddef.h>
David Brown52eee562017-07-05 11:25:09 -060034#include <stdbool.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080035#include <inttypes.h>
36#include <stdlib.h>
37#include <string.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080038#include "bootutil/bootutil.h"
Kristine Jassmann73c38c62021-02-03 16:56:14 +000039#include "bootutil/bootutil_public.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080040#include "bootutil/image.h"
41#include "bootutil_priv.h"
Fabio Utzig12d59162019-11-28 10:01:59 -030042#include "swap_priv.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050043#include "bootutil/bootutil_log.h"
David Vinczec3084132020-02-18 14:50:47 +010044#include "bootutil/security_cnt.h"
David Vincze1cf11b52020-03-24 07:51:09 +010045#include "bootutil/boot_record.h"
Raef Colese8fe6cf2020-05-26 13:07:40 +010046#include "bootutil/fault_injection_hardening.h"
Mark Horvathccaf7f82021-01-04 18:16:42 +010047#include "bootutil/ramload.h"
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +020048#include "bootutil/boot_hooks.h"
Jamie McCrae56cb6102022-03-23 11:57:03 +000049#include "bootutil/mcuboot_status.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050050
Fabio Utzigba829042018-09-18 08:29:34 -030051#ifdef MCUBOOT_ENC_IMAGES
52#include "bootutil/enc_key.h"
53#endif
54
Carlos Falgueras García391b1972021-06-21 16:58:07 +020055#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
56#include <os/os_malloc.h>
57#endif
58
Fabio Utzigba1fbe62017-07-21 14:01:20 -030059#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030060
Carlos Falgueras Garcíaa4b4b0f2021-06-22 10:00:22 +020061BOOT_LOG_MODULE_DECLARE(mcuboot);
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010062
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040063static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080064
Fabio Utzigabec0732019-07-31 08:40:22 -030065#if (BOOT_IMAGE_NUMBER > 1)
66#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
67#else
68#define IMAGES_ITER(x)
69#endif
70
Fabio Utzig10ee6482019-08-01 12:04:52 -030071/*
72 * This macro allows some control on the allocation of local variables.
73 * When running natively on a target, we don't want to allocated huge
74 * variables on the stack, so make them global instead. For the simulator
75 * we want to run as many threads as there are tests, and it's safer
76 * to just make those variables stack allocated.
77 */
78#if !defined(__BOOTSIM__)
79#define TARGET_STATIC static
80#else
81#define TARGET_STATIC
82#endif
83
Kristine Jassmann73c38c62021-02-03 16:56:14 +000084#if BOOT_MAX_ALIGN > 1024
85#define BUF_SZ BOOT_MAX_ALIGN
86#else
87#define BUF_SZ 1024
88#endif
89
David Vinczee574f2d2020-07-10 11:42:03 +020090static int
91boot_read_image_headers(struct boot_loader_state *state, bool require_all,
92 struct boot_status *bs)
93{
94 int rc;
95 int i;
96
97 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +020098 rc = BOOT_HOOK_CALL(boot_read_image_header_hook, BOOT_HOOK_REGULAR,
99 BOOT_CURR_IMG(state), i, boot_img_hdr(state, i));
100 if (rc == BOOT_HOOK_REGULAR)
101 {
102 rc = boot_read_image_header(state, i, boot_img_hdr(state, i), bs);
103 }
David Vinczee574f2d2020-07-10 11:42:03 +0200104 if (rc != 0) {
105 /* If `require_all` is set, fail on any single fail, otherwise
106 * if at least the first slot's header was read successfully,
107 * then the boot loader can attempt a boot.
108 *
109 * Failure to read any headers is a fatal error.
110 */
111 if (i > 0 && !require_all) {
112 return 0;
113 } else {
114 return rc;
115 }
116 }
117 }
118
119 return 0;
120}
121
Mark Horvathccaf7f82021-01-04 18:16:42 +0100122/**
123 * Saves boot status and shared data for current image.
124 *
125 * @param state Boot loader status information.
126 * @param active_slot Index of the slot will be loaded for current image.
127 *
128 * @return 0 on success; nonzero on failure.
129 */
130static int
131boot_add_shared_data(struct boot_loader_state *state,
Jamie McCrae3016d002023-03-14 12:35:51 +0000132 uint8_t active_slot)
Mark Horvathccaf7f82021-01-04 18:16:42 +0100133{
134#if defined(MCUBOOT_MEASURED_BOOT) || defined(MCUBOOT_DATA_SHARING)
135 int rc;
136
Jamie McCrae3016d002023-03-14 12:35:51 +0000137#ifdef MCUBOOT_DATA_SHARING
138 int max_app_size;
139#endif
140
Mark Horvathccaf7f82021-01-04 18:16:42 +0100141#ifdef MCUBOOT_MEASURED_BOOT
142 rc = boot_save_boot_status(BOOT_CURR_IMG(state),
143 boot_img_hdr(state, active_slot),
144 BOOT_IMG_AREA(state, active_slot));
145 if (rc != 0) {
146 BOOT_LOG_ERR("Failed to add image data to shared area");
147 return rc;
148 }
149#endif /* MCUBOOT_MEASURED_BOOT */
150
151#ifdef MCUBOOT_DATA_SHARING
Jamie McCrae3016d002023-03-14 12:35:51 +0000152 max_app_size = app_max_size(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +0100153 rc = boot_save_shared_data(boot_img_hdr(state, active_slot),
Jamie McCrae3016d002023-03-14 12:35:51 +0000154 BOOT_IMG_AREA(state, active_slot),
155 active_slot, max_app_size);
Mark Horvathccaf7f82021-01-04 18:16:42 +0100156 if (rc != 0) {
157 BOOT_LOG_ERR("Failed to add data to shared memory area.");
158 return rc;
159 }
160#endif /* MCUBOOT_DATA_SHARING */
161
162 return 0;
163
164#else /* MCUBOOT_MEASURED_BOOT || MCUBOOT_DATA_SHARING */
165 (void) (state);
166 (void) (active_slot);
167
168 return 0;
169#endif
170}
171
172/**
173 * Fills rsp to indicate how booting should occur.
174 *
175 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +0100176 * @param rsp boot_rsp struct to fill.
177 */
178static void
Raef Colesfe57e7d2021-10-15 11:07:09 +0100179fill_rsp(struct boot_loader_state *state, struct boot_rsp *rsp)
Mark Horvathccaf7f82021-01-04 18:16:42 +0100180{
181 uint32_t active_slot;
182
183#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesf11de642021-10-15 11:11:56 +0100184 /* Always boot from the first enabled image. */
Mark Horvathccaf7f82021-01-04 18:16:42 +0100185 BOOT_CURR_IMG(state) = 0;
Raef Colesf11de642021-10-15 11:11:56 +0100186 IMAGES_ITER(BOOT_CURR_IMG(state)) {
187 if (!state->img_mask[BOOT_CURR_IMG(state)]) {
188 break;
189 }
190 }
INFINEON\DovhalA94360d52023-01-18 17:21:56 +0200191 /* At least one image must be active, otherwise skip the execution */
192 if (BOOT_CURR_IMG(state) >= BOOT_IMAGE_NUMBER) {
193 return;
194 }
Mark Horvathccaf7f82021-01-04 18:16:42 +0100195#endif
196
197#if defined(MCUBOOT_DIRECT_XIP) || defined(MCUBOOT_RAM_LOAD)
Raef Colesfe57e7d2021-10-15 11:07:09 +0100198 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +0100199#else
Mark Horvathccaf7f82021-01-04 18:16:42 +0100200 active_slot = BOOT_PRIMARY_SLOT;
201#endif
202
Dominik Ermel260ae092021-04-23 05:38:45 +0000203 rsp->br_flash_dev_id = flash_area_get_device_id(BOOT_IMG_AREA(state, active_slot));
Mark Horvathccaf7f82021-01-04 18:16:42 +0100204 rsp->br_image_off = boot_img_slot_off(state, active_slot);
205 rsp->br_hdr = boot_img_hdr(state, active_slot);
206}
207
208/**
209 * Closes all flash areas.
210 *
211 * @param state Boot loader status information.
212 */
213static void
214close_all_flash_areas(struct boot_loader_state *state)
215{
216 uint32_t slot;
217
218 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +0100219#if BOOT_IMAGE_NUMBER > 1
220 if (state->img_mask[BOOT_CURR_IMG(state)]) {
221 continue;
222 }
223#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +0100224#if MCUBOOT_SWAP_USING_SCRATCH
225 flash_area_close(BOOT_SCRATCH_AREA(state));
226#endif
227 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
228 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
229 }
230 }
231}
232
Tamas Banfe031092020-09-10 17:32:39 +0200233#if !defined(MCUBOOT_DIRECT_XIP)
David Brownf5b33d82017-09-01 10:58:27 -0600234/*
235 * Compute the total size of the given image. Includes the size of
236 * the TLVs.
237 */
Tamas Banfe031092020-09-10 17:32:39 +0200238#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600239static int
Fabio Utzigd638b172019-08-09 10:38:05 -0300240boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
David Brownf5b33d82017-09-01 10:58:27 -0600241{
242 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300243 struct image_tlv_info info;
Fabio Utzig233af7d2019-08-26 12:06:16 -0300244 uint32_t off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300245 uint32_t protect_tlv_size;
David Brownf5b33d82017-09-01 10:58:27 -0600246 int area_id;
247 int rc;
248
Fabio Utzig10ee6482019-08-01 12:04:52 -0300249#if (BOOT_IMAGE_NUMBER == 1)
250 (void)state;
251#endif
252
253 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
David Brownf5b33d82017-09-01 10:58:27 -0600254 rc = flash_area_open(area_id, &fap);
255 if (rc != 0) {
256 rc = BOOT_EFLASH;
257 goto done;
258 }
259
Fabio Utzig61fd8882019-09-14 20:00:20 -0300260 off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
261
262 if (flash_area_read(fap, off, &info, sizeof(info))) {
263 rc = BOOT_EFLASH;
David Brownf5b33d82017-09-01 10:58:27 -0600264 goto done;
265 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300266
Fabio Utzige52c08e2019-09-11 19:32:00 -0300267 protect_tlv_size = boot_img_hdr(state, slot)->ih_protect_tlv_size;
268 if (info.it_magic == IMAGE_TLV_PROT_INFO_MAGIC) {
269 if (protect_tlv_size != info.it_tlv_tot) {
270 rc = BOOT_EBADIMAGE;
271 goto done;
272 }
273
274 if (flash_area_read(fap, off + info.it_tlv_tot, &info, sizeof(info))) {
275 rc = BOOT_EFLASH;
276 goto done;
277 }
278 } else if (protect_tlv_size != 0) {
279 rc = BOOT_EBADIMAGE;
280 goto done;
281 }
282
Fabio Utzig61fd8882019-09-14 20:00:20 -0300283 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
284 rc = BOOT_EBADIMAGE;
285 goto done;
286 }
287
Fabio Utzige52c08e2019-09-11 19:32:00 -0300288 *size = off + protect_tlv_size + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600289 rc = 0;
290
291done:
292 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300293 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600294}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300295#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600296
Tamas Banfe031092020-09-10 17:32:39 +0200297#if !defined(MCUBOOT_RAM_LOAD)
David Brownab449182019-11-15 09:32:52 -0700298static uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300299boot_write_sz(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800300{
David Brownab449182019-11-15 09:32:52 -0700301 uint32_t elem_sz;
Fabio Utzig12d59162019-11-28 10:01:59 -0300302#if MCUBOOT_SWAP_USING_SCRATCH
David Brownab449182019-11-15 09:32:52 -0700303 uint32_t align;
Fabio Utzig12d59162019-11-28 10:01:59 -0300304#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800305
306 /* Figure out what size to write update status update as. The size depends
307 * on what the minimum write size is for scratch area, active image slot.
308 * We need to use the bigger of those 2 values.
309 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300310 elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
Fabio Utzig12d59162019-11-28 10:01:59 -0300311#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300312 align = flash_area_align(BOOT_SCRATCH_AREA(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800313 if (align > elem_sz) {
314 elem_sz = align;
315 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300316#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800317
318 return elem_sz;
319}
320
Fabio Utzig10ee6482019-08-01 12:04:52 -0300321static int
322boot_initialize_area(struct boot_loader_state *state, int flash_area)
323{
Dominik Ermel51c8d762021-05-24 15:34:01 +0000324 uint32_t num_sectors = BOOT_MAX_IMG_SECTORS;
325 boot_sector_t *out_sectors;
326 uint32_t *out_num_sectors;
Fabio Utzig10ee6482019-08-01 12:04:52 -0300327 int rc;
328
329 num_sectors = BOOT_MAX_IMG_SECTORS;
330
331 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
332 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
333 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
334 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
335 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
336 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300337#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300338 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
339 out_sectors = state->scratch.sectors;
340 out_num_sectors = &state->scratch.num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300341#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -0300342 } else {
343 return BOOT_EFLASH;
344 }
345
Dominik Ermel51c8d762021-05-24 15:34:01 +0000346#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
Fabio Utzig10ee6482019-08-01 12:04:52 -0300347 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
Dominik Ermel51c8d762021-05-24 15:34:01 +0000348#else
349 _Static_assert(sizeof(int) <= sizeof(uint32_t), "Fix needed");
350 rc = flash_area_to_sectors(flash_area, (int *)&num_sectors, out_sectors);
351#endif /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300352 if (rc != 0) {
353 return rc;
354 }
355 *out_num_sectors = num_sectors;
356 return 0;
357}
Fabio Utzig10ee6482019-08-01 12:04:52 -0300358
Christopher Collins92ea77f2016-12-12 15:59:26 -0800359/**
360 * Determines the sector layout of both image slots and the scratch area.
361 * This information is necessary for calculating the number of bytes to erase
362 * and copy during an image swap. The information collected during this
Fabio Utzig10ee6482019-08-01 12:04:52 -0300363 * function is used to populate the state.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800364 */
365static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300366boot_read_sectors(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800367{
Fabio Utzigb0f04732019-07-31 09:49:19 -0300368 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800369 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800370
Fabio Utzig10ee6482019-08-01 12:04:52 -0300371 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300372
373 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800374 if (rc != 0) {
375 return BOOT_EFLASH;
376 }
377
Fabio Utzig10ee6482019-08-01 12:04:52 -0300378 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800379 if (rc != 0) {
Andrzej Puzdrowski54b4ad92021-06-22 13:21:22 +0200380 /* We need to differentiate from the primary image issue */
381 return BOOT_EFLASH_SEC;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800382 }
383
Fabio Utzig12d59162019-11-28 10:01:59 -0300384#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300385 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200386 if (rc != 0) {
387 return BOOT_EFLASH;
388 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300389#endif
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200390
Fabio Utzig10ee6482019-08-01 12:04:52 -0300391 BOOT_WRITE_SZ(state) = boot_write_sz(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800392
393 return 0;
394}
395
Fabio Utzig12d59162019-11-28 10:01:59 -0300396void
397boot_status_reset(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800398{
Fabio Utzig4741c452019-12-19 15:32:41 -0300399#ifdef MCUBOOT_ENC_IMAGES
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000400 memset(&bs->enckey, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_KEY_ALIGN_SIZE);
Fabio Utzig4741c452019-12-19 15:32:41 -0300401#if MCUBOOT_SWAP_SAVE_ENCTLV
402 memset(&bs->enctlv, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_TLV_ALIGN_SIZE);
403#endif
404#endif /* MCUBOOT_ENC_IMAGES */
405
406 bs->use_scratch = 0;
407 bs->swap_size = 0;
408 bs->source = 0;
409
Fabio Utzig74aef312019-11-28 11:05:34 -0300410 bs->op = BOOT_STATUS_OP_MOVE;
Fabio Utzig39000012018-07-30 12:40:20 -0300411 bs->idx = BOOT_STATUS_IDX_0;
412 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700413 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Fabio Utzig12d59162019-11-28 10:01:59 -0300414}
Christopher Collins92ea77f2016-12-12 15:59:26 -0800415
Fabio Utzig12d59162019-11-28 10:01:59 -0300416bool
417boot_status_is_reset(const struct boot_status *bs)
418{
Fabio Utzig74aef312019-11-28 11:05:34 -0300419 return (bs->op == BOOT_STATUS_OP_MOVE &&
420 bs->idx == BOOT_STATUS_IDX_0 &&
421 bs->state == BOOT_STATUS_STATE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800422}
423
424/**
425 * Writes the supplied boot status to the flash file system. The boot status
426 * contains the current state of an in-progress image copy operation.
427 *
428 * @param bs The boot status to write.
429 *
430 * @return 0 on success; nonzero on failure.
431 */
432int
Fabio Utzig12d59162019-11-28 10:01:59 -0300433boot_write_status(const struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800434{
435 const struct flash_area *fap;
436 uint32_t off;
437 int area_id;
Dominik Ermel9479af02021-10-19 10:06:44 +0000438 int rc = 0;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300439 uint8_t buf[BOOT_MAX_ALIGN];
Gustavo Henrique Nihei4aa286d2021-11-24 14:54:56 -0300440 uint32_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300441 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800442
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300443 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100444 * the trailer. Since in the last step the primary slot is erased, the
445 * first two status writes go to the scratch which will be copied to
446 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300447 */
448
Fabio Utzig12d59162019-11-28 10:01:59 -0300449#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig2473ac02017-05-02 12:45:02 -0300450 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800451 /* Write to scratch. */
452 area_id = FLASH_AREA_IMAGE_SCRATCH;
453 } else {
Fabio Utzig12d59162019-11-28 10:01:59 -0300454#endif
David Vincze2d736ad2019-02-18 11:50:22 +0100455 /* Write to the primary slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300456 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Fabio Utzig12d59162019-11-28 10:01:59 -0300457#if MCUBOOT_SWAP_USING_SCRATCH
Christopher Collins92ea77f2016-12-12 15:59:26 -0800458 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300459#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800460
461 rc = flash_area_open(area_id, &fap);
462 if (rc != 0) {
Dominik Ermel9479af02021-10-19 10:06:44 +0000463 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800464 }
465
466 off = boot_status_off(fap) +
Fabio Utzig12d59162019-11-28 10:01:59 -0300467 boot_status_internal_off(bs, BOOT_WRITE_SZ(state));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200468 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300469 erased_val = flash_area_erased_val(fap);
470 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700471 buf[0] = bs->state;
472
473 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800474 if (rc != 0) {
475 rc = BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800476 }
477
Christopher Collins92ea77f2016-12-12 15:59:26 -0800478 flash_area_close(fap);
Dominik Ermel9479af02021-10-19 10:06:44 +0000479
Christopher Collins92ea77f2016-12-12 15:59:26 -0800480 return rc;
481}
Tamas Banfe031092020-09-10 17:32:39 +0200482#endif /* !MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +0200483#endif /* !MCUBOOT_DIRECT_XIP */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800484
485/*
David Vinczec3084132020-02-18 14:50:47 +0100486 * Validate image hash/signature and optionally the security counter in a slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800487 */
Michael Grand5047f032022-11-24 16:49:56 +0100488static fih_ret
Fabio Utzig10ee6482019-08-01 12:04:52 -0300489boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
490 const struct flash_area *fap, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800491{
Fabio Utzig10ee6482019-08-01 12:04:52 -0300492 TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigb0f04732019-07-31 09:49:19 -0300493 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300494 int rc;
Michael Grand5047f032022-11-24 16:49:56 +0100495 FIH_DECLARE(fih_rc, FIH_FAILURE);
Fabio Utzigba829042018-09-18 08:29:34 -0300496
Fabio Utzig10ee6482019-08-01 12:04:52 -0300497#if (BOOT_IMAGE_NUMBER == 1)
498 (void)state;
499#endif
500
Fabio Utzigba829042018-09-18 08:29:34 -0300501 (void)bs;
502 (void)rc;
Fabio Utzigbc077932019-08-26 11:16:34 -0300503
504 image_index = BOOT_CURR_IMG(state);
505
Hugo L'Hostisdb543e52021-03-09 18:00:31 +0000506/* In the case of ram loading the image has already been decrypted as it is
507 * decrypted when copied in ram */
508#if defined(MCUBOOT_ENC_IMAGES) && !defined(MCUBOOT_RAM_LOAD)
Fabio Utzigbc077932019-08-26 11:16:34 -0300509 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzig4741c452019-12-19 15:32:41 -0300510 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300511 if (rc < 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100512 FIH_RET(fih_rc);
Fabio Utzigba829042018-09-18 08:29:34 -0300513 }
Fabio Utzig4741c452019-12-19 15:32:41 -0300514 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100515 FIH_RET(fih_rc);
Fabio Utzigba829042018-09-18 08:29:34 -0300516 }
517 }
Fabio Utzigbc077932019-08-26 11:16:34 -0300518#endif
519
Raef Colese8fe6cf2020-05-26 13:07:40 +0100520 FIH_CALL(bootutil_img_validate, fih_rc, BOOT_CURR_ENC(state), image_index,
521 hdr, fap, tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, NULL);
Fabio Utzig10ee6482019-08-01 12:04:52 -0300522
Raef Colese8fe6cf2020-05-26 13:07:40 +0100523 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800524}
525
Tamas Banfe031092020-09-10 17:32:39 +0200526#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
Michael Grand5047f032022-11-24 16:49:56 +0100527static fih_ret
Christopher Collins92ea77f2016-12-12 15:59:26 -0800528split_image_check(struct image_header *app_hdr,
529 const struct flash_area *app_fap,
530 struct image_header *loader_hdr,
531 const struct flash_area *loader_fap)
532{
533 static void *tmpbuf;
534 uint8_t loader_hash[32];
Michael Grand5047f032022-11-24 16:49:56 +0100535 FIH_DECLARE(fih_rc, FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800536
537 if (!tmpbuf) {
538 tmpbuf = malloc(BOOT_TMPBUF_SZ);
539 if (!tmpbuf) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100540 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800541 }
542 }
543
Raef Colese8fe6cf2020-05-26 13:07:40 +0100544 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, loader_hdr, loader_fap,
545 tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, loader_hash);
Michael Grand5047f032022-11-24 16:49:56 +0100546 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100547 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800548 }
549
Raef Colese8fe6cf2020-05-26 13:07:40 +0100550 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, app_hdr, app_fap,
551 tmpbuf, BOOT_TMPBUF_SZ, loader_hash, 32, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800552
Raef Colese8fe6cf2020-05-26 13:07:40 +0100553out:
554 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800555}
Tamas Banfe031092020-09-10 17:32:39 +0200556#endif /* !MCUBOOT_DIRECT_XIP && !MCUBOOT_RAM_LOAD */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800557
Fabio Utzig338a19f2018-12-03 08:37:08 -0200558/*
David Brown9bf95af2019-10-10 15:36:36 -0600559 * Check that this is a valid header. Valid means that the magic is
560 * correct, and that the sizes/offsets are "sane". Sane means that
561 * there is no overflow on the arithmetic, and that the result fits
562 * within the flash area we are in.
563 */
564static bool
565boot_is_header_valid(const struct image_header *hdr, const struct flash_area *fap)
566{
567 uint32_t size;
568
569 if (hdr->ih_magic != IMAGE_MAGIC) {
570 return false;
571 }
572
573 if (!boot_u32_safe_add(&size, hdr->ih_img_size, hdr->ih_hdr_size)) {
574 return false;
575 }
576
Dominik Ermel260ae092021-04-23 05:38:45 +0000577 if (size >= flash_area_get_size(fap)) {
David Brown9bf95af2019-10-10 15:36:36 -0600578 return false;
579 }
580
581 return true;
582}
583
584/*
Fabio Utzig338a19f2018-12-03 08:37:08 -0200585 * Check that a memory area consists of a given value.
586 */
587static inline bool
588boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300589{
590 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200591 uint8_t *p = (uint8_t *)data;
592 for (i = 0; i < len; i++) {
593 if (val != p[i]) {
594 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300595 }
596 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200597 return true;
598}
599
600static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300601boot_check_header_erased(struct boot_loader_state *state, int slot)
Fabio Utzig338a19f2018-12-03 08:37:08 -0200602{
603 const struct flash_area *fap;
604 struct image_header *hdr;
605 uint8_t erased_val;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300606 int area_id;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200607 int rc;
608
Fabio Utzig10ee6482019-08-01 12:04:52 -0300609 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300610 rc = flash_area_open(area_id, &fap);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200611 if (rc != 0) {
612 return -1;
613 }
614
615 erased_val = flash_area_erased_val(fap);
616 flash_area_close(fap);
617
Fabio Utzig10ee6482019-08-01 12:04:52 -0300618 hdr = boot_img_hdr(state, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200619 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
620 return -1;
621 }
622
623 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300624}
625
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000626#if (BOOT_IMAGE_NUMBER > 1) || \
David Vinczee574f2d2020-07-10 11:42:03 +0200627 defined(MCUBOOT_DIRECT_XIP) || \
Tamas Banfe031092020-09-10 17:32:39 +0200628 defined(MCUBOOT_RAM_LOAD) || \
Jerzy Kasenberge3f895d2022-04-12 15:05:33 +0200629 defined(MCUBOOT_DOWNGRADE_PREVENTION)
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000630/**
Marek Pietaa95a41b2023-04-21 14:03:19 +0200631 * Compare image version numbers
632 *
633 * By default, the comparison does not take build number into account.
634 * Enable MCUBOOT_VERSION_CMP_USE_BUILD_NUMBER to take the build number into account.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000635 *
David Vincze8b0b6372020-05-20 19:54:44 +0200636 * @param ver1 Pointer to the first image version to compare.
637 * @param ver2 Pointer to the second image version to compare.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000638 *
Marek Pietaa95a41b2023-04-21 14:03:19 +0200639 * @retval -1 If ver1 is less than ver2.
640 * @retval 0 If the image version numbers are equal.
641 * @retval 1 If ver1 is greater than ver2.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000642 */
643static int
David Vincze8b0b6372020-05-20 19:54:44 +0200644boot_version_cmp(const struct image_version *ver1,
645 const struct image_version *ver2)
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000646{
David Vincze8b0b6372020-05-20 19:54:44 +0200647 if (ver1->iv_major > ver2->iv_major) {
648 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000649 }
David Vincze8b0b6372020-05-20 19:54:44 +0200650 if (ver1->iv_major < ver2->iv_major) {
651 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000652 }
David Vincze8b0b6372020-05-20 19:54:44 +0200653 /* The major version numbers are equal, continue comparison. */
654 if (ver1->iv_minor > ver2->iv_minor) {
655 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000656 }
David Vincze8b0b6372020-05-20 19:54:44 +0200657 if (ver1->iv_minor < ver2->iv_minor) {
658 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000659 }
David Vincze8b0b6372020-05-20 19:54:44 +0200660 /* The minor version numbers are equal, continue comparison. */
661 if (ver1->iv_revision > ver2->iv_revision) {
662 return 1;
663 }
664 if (ver1->iv_revision < ver2->iv_revision) {
665 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000666 }
667
Marek Pietaa95a41b2023-04-21 14:03:19 +0200668#if defined(MCUBOOT_VERSION_CMP_USE_BUILD_NUMBER)
669 /* The revisions are equal, continue comparison. */
670 if (ver1->iv_build_num > ver2->iv_build_num) {
671 return 1;
672 }
673 if (ver1->iv_build_num < ver2->iv_build_num) {
674 return -1;
675 }
676#endif
677
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000678 return 0;
679}
680#endif
681
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000682#if defined(MCUBOOT_DIRECT_XIP)
683/**
684 * Check if image in slot has been set with specific ROM address to run from
685 * and whether the slot starts at that address.
686 *
687 * @returns 0 if IMAGE_F_ROM_FIXED flag is not set;
688 * 0 if IMAGE_F_ROM_FIXED flag is set and ROM address specified in
689 * header matches the slot address;
690 * 1 if IMF_F_ROM_FIXED flag is set but ROM address specified in header
691 * does not match the slot address.
692 */
693static bool
Raef Colesfe57e7d2021-10-15 11:07:09 +0100694boot_rom_address_check(struct boot_loader_state *state)
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000695{
Mark Horvathccaf7f82021-01-04 18:16:42 +0100696 uint32_t active_slot;
697 const struct image_header *hdr;
698 uint32_t f_off;
699
Raef Colesfe57e7d2021-10-15 11:07:09 +0100700 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +0100701 hdr = boot_img_hdr(state, active_slot);
702 f_off = boot_img_slot_off(state, active_slot);
703
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000704 if (hdr->ih_flags & IMAGE_F_ROM_FIXED && hdr->ih_load_addr != f_off) {
705 BOOT_LOG_WRN("Image in %s slot at 0x%x has been built for offset 0x%x"\
Mark Horvathccaf7f82021-01-04 18:16:42 +0100706 ", skipping",
707 active_slot == 0 ? "primary" : "secondary", f_off,
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000708 hdr->ih_load_addr);
709
710 /* If there is address mismatch, the image is not bootable from this
711 * slot.
712 */
713 return 1;
714 }
715 return 0;
716}
717#endif
718
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300719/*
720 * Check that there is a valid image in a slot
721 *
722 * @returns
Raef Colese8fe6cf2020-05-26 13:07:40 +0100723 * FIH_SUCCESS if image was successfully validated
Michael Grand5047f032022-11-24 16:49:56 +0100724 * FIH_NO_BOOTABLE_IMAGE if no bootloable image was found
Raef Colese8fe6cf2020-05-26 13:07:40 +0100725 * FIH_FAILURE on any errors
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300726 */
Michael Grand5047f032022-11-24 16:49:56 +0100727static fih_ret
Fabio Utzig10ee6482019-08-01 12:04:52 -0300728boot_validate_slot(struct boot_loader_state *state, int slot,
729 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800730{
731 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400732 struct image_header *hdr;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300733 int area_id;
Michael Grand5047f032022-11-24 16:49:56 +0100734 FIH_DECLARE(fih_rc, FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800735 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300736
Fabio Utzig10ee6482019-08-01 12:04:52 -0300737 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300738 rc = flash_area_open(area_id, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800739 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100740 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800741 }
742
Fabio Utzig10ee6482019-08-01 12:04:52 -0300743 hdr = boot_img_hdr(state, slot);
744 if (boot_check_header_erased(state, slot) == 0 ||
745 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
Fabio Utzig260ec452020-07-09 18:40:07 -0300746
747#if defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE)
748 /*
749 * This fixes an issue where an image might be erased, but a trailer
750 * be left behind. It can happen if the image is in the secondary slot
751 * and did not pass validation, in which case the whole slot is erased.
752 * If during the erase operation, a reset occurs, parts of the slot
753 * might have been erased while some did not. The concerning part is
754 * the trailer because it might disable a new image from being loaded
755 * through mcumgr; so we just get rid of the trailer here, if the header
756 * is erased.
757 */
758 if (slot != BOOT_PRIMARY_SLOT) {
759 swap_erase_trailer_sectors(state, fap);
760 }
761#endif
762
David Vincze2d736ad2019-02-18 11:50:22 +0100763 /* No bootable image in slot; continue booting from the primary slot. */
Michael Grand5047f032022-11-24 16:49:56 +0100764 fih_rc = FIH_NO_BOOTABLE_IMAGE;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200765 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300766 }
767
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000768#if defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION)
769 if (slot != BOOT_PRIMARY_SLOT) {
770 /* Check if version of secondary slot is sufficient */
David Vincze8b0b6372020-05-20 19:54:44 +0200771 rc = boot_version_cmp(
772 &boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
773 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
774 if (rc < 0 && boot_check_header_erased(state, BOOT_PRIMARY_SLOT)) {
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000775 BOOT_LOG_ERR("insufficient version in secondary slot");
Dominik Ermel260ae092021-04-23 05:38:45 +0000776 flash_area_erase(fap, 0, flash_area_get_size(fap));
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000777 /* Image in the secondary slot does not satisfy version requirement.
778 * Erase the image and continue booting from the primary slot.
779 */
Michael Grand5047f032022-11-24 16:49:56 +0100780 fih_rc = FIH_NO_BOOTABLE_IMAGE;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000781 goto out;
782 }
783 }
784#endif
Michael Grand5047f032022-11-24 16:49:56 +0100785 BOOT_HOOK_CALL_FIH(boot_image_check_hook, FIH_BOOT_HOOK_REGULAR,
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +0200786 fih_rc, BOOT_CURR_IMG(state), slot);
Michael Grand5047f032022-11-24 16:49:56 +0100787 if (FIH_EQ(fih_rc, FIH_BOOT_HOOK_REGULAR))
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +0200788 {
789 FIH_CALL(boot_image_check, fih_rc, state, hdr, fap, bs);
790 }
Michael Grand5047f032022-11-24 16:49:56 +0100791 if (!boot_is_header_valid(hdr, fap) || FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Tamas Banfe031092020-09-10 17:32:39 +0200792 if ((slot != BOOT_PRIMARY_SLOT) || ARE_SLOTS_EQUIVALENT()) {
Dominik Ermel260ae092021-04-23 05:38:45 +0000793 flash_area_erase(fap, 0, flash_area_get_size(fap));
David Vinczee574f2d2020-07-10 11:42:03 +0200794 /* Image is invalid, erase it to prevent further unnecessary
795 * attempts to validate and boot it.
David Brownb38e0442017-02-24 13:57:12 -0700796 */
797 }
David Brown098de832019-12-10 11:58:01 -0700798#if !defined(__BOOTSIM__)
David Vincze2d736ad2019-02-18 11:50:22 +0100799 BOOT_LOG_ERR("Image in the %s slot is not valid!",
800 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Brown098de832019-12-10 11:58:01 -0700801#endif
Michael Grand5047f032022-11-24 16:49:56 +0100802 fih_rc = FIH_NO_BOOTABLE_IMAGE;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200803 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800804 }
805
Håkon Øye Amundsene829e9d2021-11-12 14:01:01 +0000806#if MCUBOOT_IMAGE_NUMBER > 1 && !defined(MCUBOOT_ENC_IMAGES) && defined(MCUBOOT_VERIFY_IMG_ADDRESS)
807 /* Verify that the image in the secondary slot has a reset address
808 * located in the primary slot. This is done to avoid users incorrectly
809 * overwriting an application written to the incorrect slot.
810 * This feature is only supported by ARM platforms.
811 */
812 if (area_id == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
813 const struct flash_area *pri_fa = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
814 struct image_header *secondary_hdr = boot_img_hdr(state, slot);
815 uint32_t reset_value = 0;
816 uint32_t reset_addr = secondary_hdr->ih_hdr_size + sizeof(reset_value);
817
818 rc = flash_area_read(fap, reset_addr, &reset_value, sizeof(reset_value));
819 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +0100820 fih_rc = FIH_NO_BOOTABLE_IMAGE;
Håkon Øye Amundsene829e9d2021-11-12 14:01:01 +0000821 goto out;
822 }
823
824 if (reset_value < pri_fa->fa_off || reset_value> (pri_fa->fa_off + pri_fa->fa_size)) {
825 BOOT_LOG_ERR("Reset address of image in secondary slot is not in the primary slot");
826 BOOT_LOG_ERR("Erasing image from secondary slot");
827
828 /* The vector table in the image located in the secondary
829 * slot does not target the primary slot. This might
830 * indicate that the image was loaded to the wrong slot.
831 *
832 * Erase the image and continue booting from the primary slot.
833 */
834 flash_area_erase(fap, 0, fap->fa_size);
Michael Grand5047f032022-11-24 16:49:56 +0100835 fih_rc = FIH_NO_BOOTABLE_IMAGE;
Håkon Øye Amundsene829e9d2021-11-12 14:01:01 +0000836 goto out;
837 }
838 }
839#endif
840
Fabio Utzig338a19f2018-12-03 08:37:08 -0200841out:
842 flash_area_close(fap);
Raef Colese8fe6cf2020-05-26 13:07:40 +0100843
844 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800845}
846
David Vinczec3084132020-02-18 14:50:47 +0100847#ifdef MCUBOOT_HW_ROLLBACK_PROT
848/**
849 * Updates the stored security counter value with the image's security counter
850 * value which resides in the given slot, only if it's greater than the stored
851 * value.
852 *
853 * @param image_index Index of the image to determine which security
854 * counter to update.
855 * @param slot Slot number of the image.
856 * @param hdr Pointer to the image header structure of the image
857 * that is currently stored in the given slot.
858 *
859 * @return 0 on success; nonzero on failure.
860 */
861static int
862boot_update_security_counter(uint8_t image_index, int slot,
863 struct image_header *hdr)
864{
865 const struct flash_area *fap = NULL;
866 uint32_t img_security_cnt;
867 int rc;
868
869 rc = flash_area_open(flash_area_id_from_multi_image_slot(image_index, slot),
870 &fap);
871 if (rc != 0) {
872 rc = BOOT_EFLASH;
873 goto done;
874 }
875
876 rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt);
877 if (rc != 0) {
878 goto done;
879 }
880
881 rc = boot_nv_security_counter_update(image_index, img_security_cnt);
882 if (rc != 0) {
883 goto done;
884 }
885
886done:
887 flash_area_close(fap);
888 return rc;
889}
890#endif /* MCUBOOT_HW_ROLLBACK_PROT */
891
Tamas Banfe031092020-09-10 17:32:39 +0200892#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
David Vinczee574f2d2020-07-10 11:42:03 +0200893/**
894 * Determines which swap operation to perform, if any. If it is determined
895 * that a swap operation is required, the image in the secondary slot is checked
896 * for validity. If the image in the secondary slot is invalid, it is erased,
897 * and a swap type of "none" is indicated.
898 *
899 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
900 */
901static int
902boot_validated_swap_type(struct boot_loader_state *state,
903 struct boot_status *bs)
904{
905 int swap_type;
Michael Grand5047f032022-11-24 16:49:56 +0100906 FIH_DECLARE(fih_rc, FIH_FAILURE);
David Vinczee574f2d2020-07-10 11:42:03 +0200907
908 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
909 if (BOOT_IS_UPGRADE(swap_type)) {
910 /* Boot loader wants to switch to the secondary slot.
911 * Ensure image is valid.
912 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100913 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_SECONDARY_SLOT, bs);
Michael Grand5047f032022-11-24 16:49:56 +0100914 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
915 if (FIH_EQ(fih_rc, FIH_NO_BOOTABLE_IMAGE)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100916 swap_type = BOOT_SWAP_TYPE_NONE;
917 } else {
918 swap_type = BOOT_SWAP_TYPE_FAIL;
919 }
David Vinczee574f2d2020-07-10 11:42:03 +0200920 }
921 }
922
923 return swap_type;
924}
David Brown94ed12c2021-05-26 16:28:14 -0600925#endif
David Vinczee574f2d2020-07-10 11:42:03 +0200926
Christopher Collins92ea77f2016-12-12 15:59:26 -0800927/**
Christopher Collins92ea77f2016-12-12 15:59:26 -0800928 * Erases a region of flash.
929 *
Fabio Utzigba829042018-09-18 08:29:34 -0300930 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800931 * @param off The offset within the flash area to start the
932 * erase.
933 * @param sz The number of bytes to erase.
934 *
935 * @return 0 on success; nonzero on failure.
936 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300937int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300938boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800939{
Fabio Utzigba829042018-09-18 08:29:34 -0300940 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800941}
942
David Brown94ed12c2021-05-26 16:28:14 -0600943#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
Dominik Ermel256bc372022-12-07 15:51:31 +0000944
945#if defined(MCUBOOT_ENC_IMAGES) || defined(MCUBOOT_SWAP_SAVE_ENCTLV)
946/* Replacement for memset(p, 0, sizeof(*p) that does not get
947 * optimized out.
948 */
949static void like_mbedtls_zeroize(void *p, size_t n)
950{
951 volatile unsigned char *v = (unsigned char *)p;
952
953 for (size_t i = 0; i < n; i++) {
954 v[i] = 0;
955 }
956}
957#endif
958
Christopher Collins92ea77f2016-12-12 15:59:26 -0800959/**
960 * Copies the contents of one flash region to another. You must erase the
961 * destination region prior to calling this function.
962 *
963 * @param flash_area_id_src The ID of the source flash area.
964 * @param flash_area_id_dst The ID of the destination flash area.
965 * @param off_src The offset within the source flash area to
966 * copy from.
967 * @param off_dst The offset within the destination flash area to
968 * copy to.
969 * @param sz The number of bytes to copy.
970 *
971 * @return 0 on success; nonzero on failure.
972 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300973int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300974boot_copy_region(struct boot_loader_state *state,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300975 const struct flash_area *fap_src,
Fabio Utzigba829042018-09-18 08:29:34 -0300976 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800977 uint32_t off_src, uint32_t off_dst, uint32_t sz)
978{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800979 uint32_t bytes_copied;
980 int chunk_sz;
981 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -0300982#ifdef MCUBOOT_ENC_IMAGES
983 uint32_t off;
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300984 uint32_t tlv_off;
Fabio Utzigba829042018-09-18 08:29:34 -0300985 size_t blk_off;
986 struct image_header *hdr;
987 uint16_t idx;
988 uint32_t blk_sz;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300989 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300990#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800991
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000992 TARGET_STATIC uint8_t buf[BUF_SZ] __attribute__((aligned(4)));
Fabio Utzig10ee6482019-08-01 12:04:52 -0300993
994#if !defined(MCUBOOT_ENC_IMAGES)
995 (void)state;
996#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800997
Christopher Collins92ea77f2016-12-12 15:59:26 -0800998 bytes_copied = 0;
999 while (bytes_copied < sz) {
1000 if (sz - bytes_copied > sizeof buf) {
1001 chunk_sz = sizeof buf;
1002 } else {
1003 chunk_sz = sz - bytes_copied;
1004 }
1005
1006 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
1007 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001008 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001009 }
1010
Fabio Utzigba829042018-09-18 08:29:34 -03001011#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001012 image_index = BOOT_CURR_IMG(state);
Dominik Ermel260ae092021-04-23 05:38:45 +00001013 if ((flash_area_get_id(fap_src) == FLASH_AREA_IMAGE_SECONDARY(image_index) ||
1014 flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) &&
1015 !(flash_area_get_id(fap_src) == FLASH_AREA_IMAGE_SECONDARY(image_index) &&
1016 flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index))) {
David Vincze2d736ad2019-02-18 11:50:22 +01001017 /* assume the secondary slot as src, needs decryption */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001018 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig74aef312019-11-28 11:05:34 -03001019#if !defined(MCUBOOT_SWAP_USING_MOVE)
Fabio Utzigba829042018-09-18 08:29:34 -03001020 off = off_src;
Dominik Ermel260ae092021-04-23 05:38:45 +00001021 if (flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001022 /* might need encryption (metadata from the primary slot) */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001023 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001024 off = off_dst;
1025 }
Fabio Utzig74aef312019-11-28 11:05:34 -03001026#else
1027 off = off_dst;
Dominik Ermel260ae092021-04-23 05:38:45 +00001028 if (flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
Fabio Utzig74aef312019-11-28 11:05:34 -03001029 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
1030 }
1031#endif
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001032 if (IS_ENCRYPTED(hdr)) {
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001033 uint32_t abs_off = off + bytes_copied;
1034 if (abs_off < hdr->ih_hdr_size) {
Fabio Utzigba829042018-09-18 08:29:34 -03001035 /* do not decrypt header */
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001036 if (abs_off + chunk_sz > hdr->ih_hdr_size) {
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001037 /* The lower part of the chunk contains header data */
1038 blk_off = 0;
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001039 blk_sz = chunk_sz - (hdr->ih_hdr_size - abs_off);
1040 idx = hdr->ih_hdr_size - abs_off;
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001041 } else {
1042 /* The chunk contains exclusively header data */
1043 blk_sz = 0; /* nothing to decrypt */
1044 }
Fabio Utzigba829042018-09-18 08:29:34 -03001045 } else {
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001046 idx = 0;
1047 blk_sz = chunk_sz;
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001048 blk_off = (abs_off - hdr->ih_hdr_size) & 0xf;
Fabio Utzigba829042018-09-18 08:29:34 -03001049 }
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001050
1051 if (blk_sz > 0)
1052 {
1053 tlv_off = BOOT_TLV_OFF(hdr);
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001054 if (abs_off + chunk_sz > tlv_off) {
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001055 /* do not decrypt TLVs */
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001056 if (abs_off >= tlv_off) {
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001057 blk_sz = 0;
1058 } else {
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001059 blk_sz = tlv_off - abs_off;
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001060 }
Fabio Utzigba829042018-09-18 08:29:34 -03001061 }
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001062 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001063 (abs_off + idx) - hdr->ih_hdr_size, blk_sz,
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001064 blk_off, &buf[idx]);
Fabio Utzigba829042018-09-18 08:29:34 -03001065 }
Fabio Utzigba829042018-09-18 08:29:34 -03001066 }
1067 }
1068#endif
1069
Christopher Collins92ea77f2016-12-12 15:59:26 -08001070 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
1071 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001072 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001073 }
1074
1075 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -03001076
1077 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -08001078 }
1079
Fabio Utzigba829042018-09-18 08:29:34 -03001080 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001081}
1082
Christopher Collins92ea77f2016-12-12 15:59:26 -08001083/**
David Vincze2d736ad2019-02-18 11:50:22 +01001084 * Overwrite primary slot with the image contained in the secondary slot.
1085 * If a prior copy operation was interrupted by a system reset, this function
1086 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001087 *
1088 * @param bs The current boot status. This function reads
1089 * this struct to determine if it is resuming
1090 * an interrupted swap operation. This
1091 * function writes the updated status to this
1092 * function on return.
1093 *
1094 * @return 0 on success; nonzero on failure.
1095 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001096#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001097static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001098boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
David Brown17609d82017-05-05 09:41:34 -06001099{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001100 size_t sect_count;
1101 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001102 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001103 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001104 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001105 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +01001106 const struct flash_area *fap_primary_slot;
1107 const struct flash_area *fap_secondary_slot;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001108 uint8_t image_index;
David Vincze2d736ad2019-02-18 11:50:22 +01001109
Fabio Utzigb4f88102020-10-04 10:16:24 -03001110#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1111 uint32_t sector;
1112 uint32_t trailer_sz;
1113 uint32_t off;
1114 uint32_t sz;
1115#endif
1116
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001117 (void)bs;
1118
Fabio Utzig13d9e352017-10-05 20:32:31 -03001119#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1120 uint32_t src_size = 0;
Fabio Utzigd638b172019-08-09 10:38:05 -03001121 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001122 assert(rc == 0);
1123#endif
David Brown17609d82017-05-05 09:41:34 -06001124
Fabio Utzigb0f04732019-07-31 09:49:19 -03001125 image_index = BOOT_CURR_IMG(state);
1126
Antonio de Angelisba5fb1c2022-10-11 10:10:37 +01001127 BOOT_LOG_INF("Image %d upgrade secondary slot -> primary slot", image_index);
1128 BOOT_LOG_INF("Erasing the primary slot");
1129
Fabio Utzigb0f04732019-07-31 09:49:19 -03001130 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1131 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001132 assert (rc == 0);
1133
Fabio Utzigb0f04732019-07-31 09:49:19 -03001134 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1135 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001136 assert (rc == 0);
1137
Fabio Utzig10ee6482019-08-01 12:04:52 -03001138 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001139 for (sect = 0, size = 0; sect < sect_count; sect++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001140 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001141 rc = boot_erase_region(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001142 assert(rc == 0);
1143
Fabio Utzig13d9e352017-10-05 20:32:31 -03001144#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
Fabio Utzigb4f88102020-10-04 10:16:24 -03001145 if ((size + this_size) >= src_size) {
1146 size += src_size - size;
1147 size += BOOT_WRITE_SZ(state) - (size % BOOT_WRITE_SZ(state));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001148 break;
1149 }
1150#endif
Fabio Utzigb4f88102020-10-04 10:16:24 -03001151
1152 size += this_size;
David Brown17609d82017-05-05 09:41:34 -06001153 }
1154
Fabio Utzigb4f88102020-10-04 10:16:24 -03001155#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1156 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
1157 sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
1158 sz = 0;
1159 do {
1160 sz += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sector);
1161 off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, sector);
1162 sector--;
1163 } while (sz < trailer_sz);
1164
1165 rc = boot_erase_region(fap_primary_slot, off, sz);
1166 assert(rc == 0);
1167#endif
1168
Fabio Utzigba829042018-09-18 08:29:34 -03001169#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001170 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001171 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001172 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
Fabio Utzig4741c452019-12-19 15:32:41 -03001173 fap_secondary_slot, bs);
David Vincze2d736ad2019-02-18 11:50:22 +01001174
Fabio Utzigba829042018-09-18 08:29:34 -03001175 if (rc < 0) {
1176 return BOOT_EBADIMAGE;
1177 }
Fabio Utzig4741c452019-12-19 15:32:41 -03001178 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001179 return BOOT_EBADIMAGE;
1180 }
1181 }
1182#endif
1183
Antonio de Angelis48547002023-04-14 09:47:09 +01001184 BOOT_LOG_INF("Image %d copying the secondary slot to the primary slot: 0x%zx bytes",
1185 image_index, size);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001186 rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot, 0, 0, size);
Fabio Utzigb4f88102020-10-04 10:16:24 -03001187 if (rc != 0) {
1188 return rc;
1189 }
1190
1191#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1192 rc = boot_write_magic(fap_primary_slot);
1193 if (rc != 0) {
1194 return rc;
1195 }
1196#endif
David Brown17609d82017-05-05 09:41:34 -06001197
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +02001198 rc = BOOT_HOOK_CALL(boot_copy_region_post_hook, 0, BOOT_CURR_IMG(state),
1199 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT), size);
1200 if (rc != 0) {
1201 return rc;
1202 }
1203
David Vinczec3084132020-02-18 14:50:47 +01001204#ifdef MCUBOOT_HW_ROLLBACK_PROT
1205 /* Update the stored security counter with the new image's security counter
1206 * value. Both slots hold the new image at this point, but the secondary
1207 * slot's image header must be passed since the image headers in the
1208 * boot_data structure have not been updated yet.
1209 */
1210 rc = boot_update_security_counter(BOOT_CURR_IMG(state), BOOT_PRIMARY_SLOT,
1211 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1212 if (rc != 0) {
1213 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
1214 return rc;
1215 }
1216#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1217
Fabio Utzig13d9e352017-10-05 20:32:31 -03001218 /*
1219 * Erases header and trailer. The trailer is erased because when a new
1220 * image is written without a trailer as is the case when using newt, the
1221 * trailer that was left might trigger a new upgrade.
1222 */
Christopher Collins2c88e692019-05-22 15:10:14 -07001223 BOOT_LOG_DBG("erasing secondary header");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001224 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001225 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
1226 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001227 assert(rc == 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001228 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -07001229 BOOT_LOG_DBG("erasing secondary trailer");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001230 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001231 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
1232 last_sector),
1233 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
1234 last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001235 assert(rc == 0);
1236
David Vincze2d736ad2019-02-18 11:50:22 +01001237 flash_area_close(fap_primary_slot);
1238 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001239
David Vincze2d736ad2019-02-18 11:50:22 +01001240 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001241
1242 return 0;
1243}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001244#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001245
Christopher Collinsa1c12042019-05-23 14:00:28 -07001246#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -03001247/**
1248 * Swaps the two images in flash. If a prior copy operation was interrupted
1249 * by a system reset, this function completes that operation.
1250 *
1251 * @param bs The current boot status. This function reads
1252 * this struct to determine if it is resuming
1253 * an interrupted swap operation. This
1254 * function writes the updated status to this
1255 * function on return.
1256 *
1257 * @return 0 on success; nonzero on failure.
1258 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001259static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001260boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001261{
Fabio Utzig2473ac02017-05-02 12:45:02 -03001262 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001263 const struct flash_area *fap;
Dominik Ermel472d4c72023-02-10 13:29:58 +00001264#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzigba829042018-09-18 08:29:34 -03001265 uint8_t slot;
1266 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001267#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001268 uint32_t size;
1269 uint32_t copy_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001270 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001271 int rc;
1272
1273 /* FIXME: just do this if asked by user? */
1274
1275 size = copy_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001276 image_index = BOOT_CURR_IMG(state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001277
Fabio Utzig12d59162019-11-28 10:01:59 -03001278 if (boot_status_is_reset(bs)) {
Fabio Utzig46490722017-09-04 15:34:32 -03001279 /*
1280 * No swap ever happened, so need to find the largest image which
1281 * will be used to determine the amount of sectors to swap.
1282 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001283 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001284 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001285 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001286 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001287 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001288
Fabio Utzigba829042018-09-18 08:29:34 -03001289#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001290 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001291 fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001292 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001293 assert(rc >= 0);
1294
1295 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001296 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 0, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001297 assert(rc == 0);
1298 } else {
1299 rc = 0;
1300 }
1301 } else {
Kristine Jassmann73c38c62021-02-03 16:56:14 +00001302 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_ALIGN_SIZE);
Fabio Utzigba829042018-09-18 08:29:34 -03001303 }
1304#endif
1305
Fabio Utzig10ee6482019-08-01 12:04:52 -03001306 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001307 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001308 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001309 assert(rc == 0);
1310 }
1311
Fabio Utzigba829042018-09-18 08:29:34 -03001312#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001313 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001314 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001315 fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001316 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001317 assert(rc >= 0);
1318
1319 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001320 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001321 assert(rc == 0);
1322 } else {
1323 rc = 0;
1324 }
1325 } else {
Kristine Jassmann73c38c62021-02-03 16:56:14 +00001326 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_ALIGN_SIZE);
Fabio Utzigba829042018-09-18 08:29:34 -03001327 }
1328#endif
1329
Fabio Utzig46490722017-09-04 15:34:32 -03001330 if (size > copy_size) {
1331 copy_size = size;
1332 }
1333
1334 bs->swap_size = copy_size;
1335 } else {
1336 /*
1337 * If a swap was under way, the swap_size should already be present
1338 * in the trailer...
1339 */
Dominik Ermel472d4c72023-02-10 13:29:58 +00001340
1341 rc = boot_find_status(image_index, &fap);
1342 assert(fap != NULL);
1343 rc = boot_read_swap_size(fap, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001344 assert(rc == 0);
1345
1346 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001347
1348#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4741c452019-12-19 15:32:41 -03001349 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Dominik Ermel472d4c72023-02-10 13:29:58 +00001350 rc = boot_read_enc_key(fap, slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001351 assert(rc == 0);
1352
1353 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001354 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001355 break;
1356 }
1357 }
1358
1359 if (i != BOOT_ENC_KEY_SIZE) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001360 boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001361 }
1362 }
1363#endif
Dominik Ermel472d4c72023-02-10 13:29:58 +00001364 flash_area_close(fap);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001365 }
1366
Fabio Utzig12d59162019-11-28 10:01:59 -03001367 swap_run(state, bs, copy_size);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001368
David Vincze2d736ad2019-02-18 11:50:22 +01001369#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig12d59162019-11-28 10:01:59 -03001370 extern int boot_status_fails;
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001371 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001372 BOOT_LOG_WRN("%d status write fails performing the swap",
1373 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001374 }
1375#endif
Sigvart Hovland3fd4cd42022-09-09 13:21:18 +02001376 rc = BOOT_HOOK_CALL(boot_copy_region_post_hook, 0, BOOT_CURR_IMG(state),
1377 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT), size);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001378
Christopher Collins92ea77f2016-12-12 15:59:26 -08001379 return 0;
1380}
David Brown17609d82017-05-05 09:41:34 -06001381#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001382
David Vinczee32483f2019-06-13 10:46:24 +02001383#if (BOOT_IMAGE_NUMBER > 1)
1384/**
1385 * Check the image dependency whether it is satisfied and modify
1386 * the swap type if necessary.
1387 *
1388 * @param dep Image dependency which has to be verified.
1389 *
1390 * @return 0 on success; nonzero on failure.
1391 */
1392static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001393boot_verify_slot_dependency(struct boot_loader_state *state,
1394 struct image_dependency *dep)
David Vinczee32483f2019-06-13 10:46:24 +02001395{
1396 struct image_version *dep_version;
1397 size_t dep_slot;
1398 int rc;
David Browne6ab34c2019-09-03 12:24:21 -06001399 uint8_t swap_type;
David Vinczee32483f2019-06-13 10:46:24 +02001400
1401 /* Determine the source of the image which is the subject of
1402 * the dependency and get it's version. */
David Browne6ab34c2019-09-03 12:24:21 -06001403 swap_type = state->swap_type[dep->image_id];
Barry Solomon04075532020-03-18 09:33:32 -04001404 dep_slot = BOOT_IS_UPGRADE(swap_type) ? BOOT_SECONDARY_SLOT
1405 : BOOT_PRIMARY_SLOT;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001406 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +02001407
David Vincze8b0b6372020-05-20 19:54:44 +02001408 rc = boot_version_cmp(dep_version, &dep->image_min_version);
1409 if (rc < 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001410 /* Dependency not satisfied.
1411 * Modify the swap type to decrease the version number of the image
1412 * (which will be located in the primary slot after the boot process),
1413 * consequently the number of unsatisfied dependencies will be
1414 * decreased or remain the same.
1415 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001416 switch (BOOT_SWAP_TYPE(state)) {
David Vinczee32483f2019-06-13 10:46:24 +02001417 case BOOT_SWAP_TYPE_TEST:
1418 case BOOT_SWAP_TYPE_PERM:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001419 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczee32483f2019-06-13 10:46:24 +02001420 break;
1421 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001422 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczee32483f2019-06-13 10:46:24 +02001423 break;
1424 default:
1425 break;
1426 }
David Vincze8b0b6372020-05-20 19:54:44 +02001427 } else {
1428 /* Dependency satisfied. */
1429 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001430 }
1431
1432 return rc;
1433}
1434
1435/**
1436 * Read all dependency TLVs of an image from the flash and verify
1437 * one after another to see if they are all satisfied.
1438 *
1439 * @param slot Image slot number.
1440 *
1441 * @return 0 on success; nonzero on failure.
1442 */
1443static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001444boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
David Vinczee32483f2019-06-13 10:46:24 +02001445{
1446 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001447 struct image_tlv_iter it;
David Vinczee32483f2019-06-13 10:46:24 +02001448 struct image_dependency dep;
1449 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001450 uint16_t len;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001451 int area_id;
David Vinczee32483f2019-06-13 10:46:24 +02001452 int rc;
1453
Fabio Utzig10ee6482019-08-01 12:04:52 -03001454 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001455 rc = flash_area_open(area_id, &fap);
David Vinczee32483f2019-06-13 10:46:24 +02001456 if (rc != 0) {
1457 rc = BOOT_EFLASH;
1458 goto done;
1459 }
1460
Fabio Utzig61fd8882019-09-14 20:00:20 -03001461 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
1462 IMAGE_TLV_DEPENDENCY, true);
David Vinczee32483f2019-06-13 10:46:24 +02001463 if (rc != 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001464 goto done;
1465 }
1466
Fabio Utzig61fd8882019-09-14 20:00:20 -03001467 while (true) {
1468 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
1469 if (rc < 0) {
1470 return -1;
1471 } else if (rc > 0) {
1472 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001473 break;
1474 }
Fabio Utzig61fd8882019-09-14 20:00:20 -03001475
1476 if (len != sizeof(dep)) {
1477 rc = BOOT_EBADIMAGE;
1478 goto done;
1479 }
1480
1481 rc = flash_area_read(fap, off, &dep, len);
1482 if (rc != 0) {
1483 rc = BOOT_EFLASH;
1484 goto done;
1485 }
1486
1487 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
1488 rc = BOOT_EBADARGS;
1489 goto done;
1490 }
1491
1492 /* Verify dependency and modify the swap type if not satisfied. */
1493 rc = boot_verify_slot_dependency(state, &dep);
1494 if (rc != 0) {
1495 /* Dependency not satisfied. */
1496 goto done;
1497 }
David Vinczee32483f2019-06-13 10:46:24 +02001498 }
1499
1500done:
1501 flash_area_close(fap);
1502 return rc;
1503}
1504
1505/**
David Vinczee32483f2019-06-13 10:46:24 +02001506 * Iterate over all the images and verify whether the image dependencies in the
1507 * TLV area are all satisfied and update the related swap type if necessary.
1508 */
Fabio Utzig298913b2019-08-28 11:22:45 -03001509static int
1510boot_verify_dependencies(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001511{
Erik Johnson49063752020-02-06 09:59:31 -06001512 int rc = -1;
Fabio Utzig298913b2019-08-28 11:22:45 -03001513 uint8_t slot;
David Vinczee32483f2019-06-13 10:46:24 +02001514
Fabio Utzig10ee6482019-08-01 12:04:52 -03001515 BOOT_CURR_IMG(state) = 0;
1516 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
Raef Colesf11de642021-10-15 11:11:56 +01001517 if (state->img_mask[BOOT_CURR_IMG(state)]) {
1518 BOOT_CURR_IMG(state)++;
1519 continue;
1520 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001521 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1522 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
1523 slot = BOOT_SECONDARY_SLOT;
1524 } else {
1525 slot = BOOT_PRIMARY_SLOT;
1526 }
1527
1528 rc = boot_verify_slot_dependencies(state, slot);
Fabio Utzigabec0732019-07-31 08:40:22 -03001529 if (rc == 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001530 /* All dependencies've been satisfied, continue with next image. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001531 BOOT_CURR_IMG(state)++;
David Vincze8b0b6372020-05-20 19:54:44 +02001532 } else {
Fabio Utzig298913b2019-08-28 11:22:45 -03001533 /* Cannot upgrade due to non-met dependencies, so disable all
1534 * image upgrades.
1535 */
1536 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
1537 BOOT_CURR_IMG(state) = idx;
1538 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1539 }
1540 break;
David Vinczee32483f2019-06-13 10:46:24 +02001541 }
1542 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001543 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02001544}
1545#endif /* (BOOT_IMAGE_NUMBER > 1) */
1546
Christopher Collins92ea77f2016-12-12 15:59:26 -08001547/**
David Vinczeba3bd602019-06-17 16:01:43 +02001548 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001549 *
David Vinczeba3bd602019-06-17 16:01:43 +02001550 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001551 *
1552 * @return 0 on success; nonzero on failure.
1553 */
1554static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001555boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001556{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001557 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001558#ifndef MCUBOOT_OVERWRITE_ONLY
1559 uint8_t swap_type;
1560#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001561
David Vinczeba3bd602019-06-17 16:01:43 +02001562 /* At this point there are no aborted swaps. */
1563#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001564 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001565#elif defined(MCUBOOT_BOOTSTRAP)
1566 /* Check if the image update was triggered by a bad image in the
1567 * primary slot (the validity of the image in the secondary slot had
1568 * already been checked).
1569 */
Michael Grand5047f032022-11-24 16:49:56 +01001570 FIH_DECLARE(fih_rc, FIH_FAILURE);
Raef Colese8fe6cf2020-05-26 13:07:40 +01001571 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1572 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, bs);
Michael Grand5047f032022-11-24 16:49:56 +01001573 if (rc == 0 || FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001574 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001575 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001576 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001577 }
1578#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03001579 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001580#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001581 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001582
1583#ifndef MCUBOOT_OVERWRITE_ONLY
1584 /* The following state needs image_ok be explicitly set after the
1585 * swap was finished to avoid a new revert.
1586 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001587 swap_type = BOOT_SWAP_TYPE(state);
1588 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1589 swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001590 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001591 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001592 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001593 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001594 }
1595
David Vinczec3084132020-02-18 14:50:47 +01001596#ifdef MCUBOOT_HW_ROLLBACK_PROT
1597 if (swap_type == BOOT_SWAP_TYPE_PERM) {
1598 /* Update the stored security counter with the new image's security
1599 * counter value. The primary slot holds the new image at this point,
1600 * but the secondary slot's image header must be passed since image
1601 * headers in the boot_data structure have not been updated yet.
1602 *
1603 * In case of a permanent image swap mcuboot will never attempt to
1604 * revert the images on the next reboot. Therefore, the security
1605 * counter must be increased right after the image upgrade.
1606 */
1607 rc = boot_update_security_counter(
1608 BOOT_CURR_IMG(state),
1609 BOOT_PRIMARY_SLOT,
1610 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1611 if (rc != 0) {
1612 BOOT_LOG_ERR("Security counter update failed after "
1613 "image upgrade.");
1614 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
1615 }
1616 }
1617#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1618
Fabio Utzige575b0b2019-09-11 12:34:23 -03001619 if (BOOT_IS_UPGRADE(swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001620 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001621 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001622 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001623 }
David Vinczeba3bd602019-06-17 16:01:43 +02001624 }
1625#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001626
David Vinczeba3bd602019-06-17 16:01:43 +02001627 return rc;
1628}
1629
1630/**
1631 * Completes a previously aborted image swap.
1632 *
1633 * @param bs The current boot status.
1634 *
1635 * @return 0 on success; nonzero on failure.
1636 */
1637#if !defined(MCUBOOT_OVERWRITE_ONLY)
1638static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001639boot_complete_partial_swap(struct boot_loader_state *state,
1640 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001641{
1642 int rc;
1643
1644 /* Determine the type of swap operation being resumed from the
1645 * `swap-type` trailer field.
1646 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001647 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001648 assert(rc == 0);
1649
Fabio Utzig10ee6482019-08-01 12:04:52 -03001650 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001651
1652 /* The following states need image_ok be explicitly set after the
1653 * swap was finished to avoid a new revert.
1654 */
1655 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1656 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001657 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001658 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001659 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001660 }
1661 }
1662
Fabio Utzige575b0b2019-09-11 12:34:23 -03001663 if (BOOT_IS_UPGRADE(bs->swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001664 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001665 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001666 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001667 }
1668 }
1669
Fabio Utzig10ee6482019-08-01 12:04:52 -03001670 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001671 BOOT_LOG_ERR("panic!");
1672 assert(0);
1673
1674 /* Loop forever... */
1675 while (1) {}
1676 }
1677
1678 return rc;
1679}
1680#endif /* !MCUBOOT_OVERWRITE_ONLY */
1681
1682#if (BOOT_IMAGE_NUMBER > 1)
1683/**
1684 * Review the validity of previously determined swap types of other images.
1685 *
1686 * @param aborted_swap The current image upgrade is a
1687 * partial/aborted swap.
1688 */
1689static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001690boot_review_image_swap_types(struct boot_loader_state *state,
1691 bool aborted_swap)
David Vinczeba3bd602019-06-17 16:01:43 +02001692{
1693 /* In that case if we rebooted in the middle of an image upgrade process, we
1694 * must review the validity of swap types, that were previously determined
1695 * for other images. The image_ok flag had not been set before the reboot
1696 * for any of the updated images (only the copy_done flag) and thus falsely
1697 * the REVERT swap type has been determined for the previous images that had
1698 * been updated before the reboot.
1699 *
1700 * There are two separate scenarios that we have to deal with:
1701 *
1702 * 1. The reboot has happened during swapping an image:
1703 * The current image upgrade has been determined as a
1704 * partial/aborted swap.
1705 * 2. The reboot has happened between two separate image upgrades:
1706 * In this scenario we must check the swap type of the current image.
1707 * In those cases if it is NONE or REVERT we cannot certainly determine
1708 * the fact of a reboot. In a consistent state images must move in the
1709 * same direction or stay in place, e.g. in practice REVERT and TEST
1710 * swap types cannot be present at the same time. If the swap type of
1711 * the current image is either TEST, PERM or FAIL we must review the
1712 * already determined swap types of other images and set each false
1713 * REVERT swap types to NONE (these images had been successfully
1714 * updated before the system rebooted between two separate image
1715 * upgrades).
1716 */
1717
Fabio Utzig10ee6482019-08-01 12:04:52 -03001718 if (BOOT_CURR_IMG(state) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001719 /* Nothing to do */
1720 return;
1721 }
1722
1723 if (!aborted_swap) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001724 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
1725 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001726 /* Nothing to do */
1727 return;
1728 }
1729 }
1730
Fabio Utzig10ee6482019-08-01 12:04:52 -03001731 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
1732 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
1733 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001734 }
1735 }
1736}
1737#endif
1738
1739/**
1740 * Prepare image to be updated if required.
1741 *
1742 * Prepare image to be updated if required with completing an image swap
1743 * operation if one was aborted and/or determining the type of the
1744 * swap operation. In case of any error set the swap type to NONE.
1745 *
Fabio Utzig10ee6482019-08-01 12:04:52 -03001746 * @param state TODO
David Vinczeba3bd602019-06-17 16:01:43 +02001747 * @param bs Pointer where the read and possibly updated
1748 * boot status can be written to.
1749 */
1750static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001751boot_prepare_image_for_update(struct boot_loader_state *state,
1752 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001753{
1754 int rc;
Michael Grand5047f032022-11-24 16:49:56 +01001755 FIH_DECLARE(fih_rc, FIH_FAILURE);
David Vinczeba3bd602019-06-17 16:01:43 +02001756
1757 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001758 rc = boot_read_sectors(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001759 if (rc != 0) {
1760 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
1761 " - too small?", BOOT_MAX_IMG_SECTORS);
1762 /* Unable to determine sector layout, continue with next image
1763 * if there is one.
1764 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001765 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Andrzej Puzdrowski54b4ad92021-06-22 13:21:22 +02001766 if (rc == BOOT_EFLASH)
1767 {
1768 /* Only return on error from the primary image flash */
1769 return;
1770 }
David Vinczeba3bd602019-06-17 16:01:43 +02001771 }
1772
1773 /* Attempt to read an image header from each slot. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001774 rc = boot_read_image_headers(state, false, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02001775 if (rc != 0) {
1776 /* Continue with next image if there is one. */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001777 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001778 BOOT_CURR_IMG(state));
1779 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001780 return;
1781 }
1782
1783 /* If the current image's slots aren't compatible, no swap is possible.
1784 * Just boot into primary slot.
1785 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001786 if (boot_slots_compatible(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001787 boot_status_reset(bs);
1788
1789#ifndef MCUBOOT_OVERWRITE_ONLY
1790 rc = swap_read_status(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001791 if (rc != 0) {
1792 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001793 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001794 /* Continue with next image if there is one. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001795 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001796 return;
1797 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001798#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001799
Fabio Utzig74aef312019-11-28 11:05:34 -03001800#ifdef MCUBOOT_SWAP_USING_MOVE
1801 /*
1802 * Must re-read image headers because the boot status might
1803 * have been updated in the previous function call.
1804 */
1805 rc = boot_read_image_headers(state, !boot_status_is_reset(bs), bs);
Fabio Utzig32afe852020-10-04 10:36:02 -03001806#ifdef MCUBOOT_BOOTSTRAP
1807 /* When bootstrapping it's OK to not have image magic in the primary slot */
1808 if (rc != 0 && (BOOT_CURR_IMG(state) != BOOT_PRIMARY_SLOT ||
1809 boot_check_header_erased(state, BOOT_PRIMARY_SLOT) != 0)) {
1810#else
Fabio Utzig74aef312019-11-28 11:05:34 -03001811 if (rc != 0) {
Fabio Utzig32afe852020-10-04 10:36:02 -03001812#endif
1813
Fabio Utzig74aef312019-11-28 11:05:34 -03001814 /* Continue with next image if there is one. */
1815 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
1816 BOOT_CURR_IMG(state));
1817 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1818 return;
1819 }
1820#endif
1821
David Vinczeba3bd602019-06-17 16:01:43 +02001822 /* Determine if we rebooted in the middle of an image swap
1823 * operation. If a partial swap was detected, complete it.
1824 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001825 if (!boot_status_is_reset(bs)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001826
1827#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001828 boot_review_image_swap_types(state, true);
David Vinczeba3bd602019-06-17 16:01:43 +02001829#endif
1830
1831#ifdef MCUBOOT_OVERWRITE_ONLY
1832 /* Should never arrive here, overwrite-only mode has
1833 * no swap state.
1834 */
1835 assert(0);
1836#else
1837 /* Determine the type of swap operation being resumed from the
1838 * `swap-type` trailer field.
1839 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001840 rc = boot_complete_partial_swap(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001841 assert(rc == 0);
1842#endif
1843 /* Attempt to read an image header from each slot. Ensure that
1844 * image headers in slots are aligned with headers in boot_data.
1845 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001846 rc = boot_read_image_headers(state, false, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001847 assert(rc == 0);
1848
1849 /* Swap has finished set to NONE */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001850 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001851 } else {
1852 /* There was no partial swap, determine swap type. */
1853 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001854 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001855 } else {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001856 FIH_CALL(boot_validate_slot, fih_rc,
1857 state, BOOT_SECONDARY_SLOT, bs);
Michael Grand5047f032022-11-24 16:49:56 +01001858 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001859 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
1860 } else {
1861 BOOT_SWAP_TYPE(state) = bs->swap_type;
1862 }
David Vinczeba3bd602019-06-17 16:01:43 +02001863 }
1864
1865#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001866 boot_review_image_swap_types(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02001867#endif
1868
1869#ifdef MCUBOOT_BOOTSTRAP
Fabio Utzig10ee6482019-08-01 12:04:52 -03001870 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001871 /* Header checks are done first because they are
1872 * inexpensive. Since overwrite-only copies starting from
1873 * offset 0, if interrupted, it might leave a valid header
1874 * magic, so also run validation on the primary slot to be
1875 * sure it's not OK.
1876 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001877 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1878 FIH_CALL(boot_validate_slot, fih_rc,
1879 state, BOOT_PRIMARY_SLOT, bs);
1880
Michael Grand5047f032022-11-24 16:49:56 +01001881 if (rc == 0 || FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001882
Fabio Utzig3d77c952020-10-04 10:23:17 -03001883 rc = (boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC) ? 1: 0;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001884 FIH_CALL(boot_validate_slot, fih_rc,
1885 state, BOOT_SECONDARY_SLOT, bs);
1886
Michael Grand5047f032022-11-24 16:49:56 +01001887 if (rc == 1 && FIH_EQ(fih_rc, FIH_SUCCESS)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001888 /* Set swap type to REVERT to overwrite the primary
1889 * slot with the image contained in secondary slot
1890 * and to trigger the explicit setting of the
1891 * image_ok flag.
1892 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03001893 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02001894 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001895 }
1896 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001897#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001898 }
David Vinczeba3bd602019-06-17 16:01:43 +02001899 } else {
1900 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001901 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001902 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001903}
1904
Mark Horvathccaf7f82021-01-04 18:16:42 +01001905/**
1906 * Updates the security counter for the current image.
1907 *
1908 * @param state Boot loader status information.
1909 *
1910 * @return 0 on success; nonzero on failure.
1911 */
1912static int
1913boot_update_hw_rollback_protection(struct boot_loader_state *state)
1914{
1915#ifdef MCUBOOT_HW_ROLLBACK_PROT
1916 int rc;
1917
1918 /* Update the stored security counter with the active image's security
1919 * counter value. It will only be updated if the new security counter is
1920 * greater than the stored value.
1921 *
1922 * In case of a successful image swapping when the swap type is TEST the
1923 * security counter can be increased only after a reset, when the swap
1924 * type is NONE and the image has marked itself "OK" (the image_ok flag
1925 * has been set). This way a "revert" can be performed when it's
1926 * necessary.
1927 */
1928 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
1929 rc = boot_update_security_counter(
1930 BOOT_CURR_IMG(state),
1931 BOOT_PRIMARY_SLOT,
1932 boot_img_hdr(state, BOOT_PRIMARY_SLOT));
1933 if (rc != 0) {
1934 BOOT_LOG_ERR("Security counter update failed after image "
1935 "validation.");
1936 return rc;
1937 }
1938 }
1939
1940 return 0;
1941
1942#else /* MCUBOOT_HW_ROLLBACK_PROT */
1943 (void) (state);
1944
1945 return 0;
1946#endif
1947}
1948
Jerzy Kasenberge3f895d2022-04-12 15:05:33 +02001949/**
1950 * Checks test swap downgrade prevention conditions.
1951 *
1952 * Function called only for swap upgrades test run. It may prevent
1953 * swap if slot 1 image has <= version number or < security counter
1954 *
1955 * @param state Boot loader status information.
1956 *
1957 * @return 0 - image can be swapped, -1 downgrade prevention
1958 */
1959static int
1960check_downgrade_prevention(struct boot_loader_state *state)
1961{
1962#if defined(MCUBOOT_DOWNGRADE_PREVENTION) && \
1963 (defined(MCUBOOT_SWAP_USING_MOVE) || defined(MCUBOOT_SWAP_USING_SCRATCH))
1964 uint32_t security_counter[2];
1965 int rc;
1966
1967 if (MCUBOOT_DOWNGRADE_PREVENTION_SECURITY_COUNTER) {
1968 /* If there was security no counter in slot 0, allow swap */
1969 rc = bootutil_get_img_security_cnt(&(BOOT_IMG(state, 0).hdr),
1970 BOOT_IMG(state, 0).area,
1971 &security_counter[0]);
1972 if (rc != 0) {
1973 return 0;
1974 }
1975 /* If there is no security counter in slot 1, or it's lower than
1976 * that of slot 0, prevent downgrade */
1977 rc = bootutil_get_img_security_cnt(&(BOOT_IMG(state, 1).hdr),
1978 BOOT_IMG(state, 1).area,
1979 &security_counter[1]);
1980 if (rc != 0 || security_counter[0] > security_counter[1]) {
1981 rc = -1;
1982 }
1983 }
1984 else {
1985 rc = boot_version_cmp(&boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
1986 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
1987 }
1988 if (rc < 0) {
1989 /* Image in slot 0 prevents downgrade, delete image in slot 1 */
Antonio de Angelis48547002023-04-14 09:47:09 +01001990 BOOT_LOG_INF("Image %d in slot 1 erased due to downgrade prevention", BOOT_CURR_IMG(state));
Jerzy Kasenberge3f895d2022-04-12 15:05:33 +02001991 flash_area_erase(BOOT_IMG(state, 1).area, 0,
1992 flash_area_get_size(BOOT_IMG(state, 1).area));
1993 } else {
1994 rc = 0;
1995 }
1996 return rc;
1997#else
1998 (void)state;
1999 return 0;
2000#endif
2001}
2002
Michael Grand5047f032022-11-24 16:49:56 +01002003fih_ret
Fabio Utzig10ee6482019-08-01 12:04:52 -03002004context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08002005{
Marti Bolivar84898652017-06-13 17:20:22 -04002006 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02002007 struct boot_status bs;
David Vincze9015a5d2020-05-18 14:43:11 +02002008 int rc = -1;
Michael Grand5047f032022-11-24 16:49:56 +01002009 FIH_DECLARE(fih_rc, FIH_FAILURE);
Marti Bolivarc0b47912017-06-13 17:18:09 -04002010 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03002011 int image_index;
Fabio Utzig298913b2019-08-28 11:22:45 -03002012 bool has_upgrade;
Michael Grand5047f032022-11-24 16:49:56 +01002013 volatile int fih_cnt;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002014
2015 /* The array of slot sectors are defined here (as opposed to file scope) so
2016 * that they don't get allocated for non-boot-loader apps. This is
2017 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002018 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08002019 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002020 TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
2021 TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03002022#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03002023 TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03002024#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08002025
Fabio Utzig298913b2019-08-28 11:22:45 -03002026 has_upgrade = false;
2027
2028#if (BOOT_IMAGE_NUMBER == 1)
2029 (void)has_upgrade;
2030#endif
Fabio Utzigba829042018-09-18 08:29:34 -03002031
David Vinczeba3bd602019-06-17 16:01:43 +02002032 /* Iterate over all the images. By the end of the loop the swap type has
2033 * to be determined for each image and all aborted swaps have to be
2034 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002035 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002036 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002037#if BOOT_IMAGE_NUMBER > 1
2038 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2039 continue;
2040 }
2041#endif
David Vinczeba3bd602019-06-17 16:01:43 +02002042#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
2043 /* The keys used for encryption may no longer be valid (could belong to
2044 * another images). Therefore, mark them as invalid to force their reload
2045 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03002046 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03002047 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Brown554c52e2017-06-30 16:01:07 -06002048#endif
2049
Fabio Utzig10ee6482019-08-01 12:04:52 -03002050 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03002051
Fabio Utzig10ee6482019-08-01 12:04:52 -03002052 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002053 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03002054 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002055 secondary_slot_sectors[image_index];
Fabio Utzig12d59162019-11-28 10:01:59 -03002056#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03002057 state->scratch.sectors = scratch_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -03002058#endif
David Vinczeba3bd602019-06-17 16:01:43 +02002059
2060 /* Open primary and secondary image areas for the duration
2061 * of this call.
2062 */
2063 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03002064 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03002065 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02002066 assert(rc == 0);
2067 }
Fabio Utzig12d59162019-11-28 10:01:59 -03002068#if MCUBOOT_SWAP_USING_SCRATCH
David Vinczeba3bd602019-06-17 16:01:43 +02002069 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03002070 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002071 assert(rc == 0);
Fabio Utzig12d59162019-11-28 10:01:59 -03002072#endif
David Vinczeba3bd602019-06-17 16:01:43 +02002073
2074 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002075 boot_prepare_image_for_update(state, &bs);
Fabio Utzig298913b2019-08-28 11:22:45 -03002076
Fabio Utzige575b0b2019-09-11 12:34:23 -03002077 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
Fabio Utzig298913b2019-08-28 11:22:45 -03002078 has_upgrade = true;
2079 }
David Vinczeba3bd602019-06-17 16:01:43 +02002080 }
2081
David Vinczee32483f2019-06-13 10:46:24 +02002082#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig298913b2019-08-28 11:22:45 -03002083 if (has_upgrade) {
2084 /* Iterate over all the images and verify whether the image dependencies
2085 * are all satisfied and update swap type if necessary.
2086 */
2087 rc = boot_verify_dependencies(state);
David Vincze8b0b6372020-05-20 19:54:44 +02002088 if (rc != 0) {
Fabio Utzig298913b2019-08-28 11:22:45 -03002089 /*
2090 * It was impossible to upgrade because the expected dependency version
2091 * was not available. Here we already changed the swap_type so that
2092 * instead of asserting the bootloader, we continue and no upgrade is
2093 * performed.
2094 */
2095 rc = 0;
2096 }
2097 }
David Vinczee32483f2019-06-13 10:46:24 +02002098#endif
2099
Jamie McCrae56cb6102022-03-23 11:57:03 +00002100 /* Trigger status change callback with upgrading status */
2101 mcuboot_status_change(MCUBOOT_STATUS_UPGRADING);
2102
David Vinczeba3bd602019-06-17 16:01:43 +02002103 /* Iterate over all the images. At this point there are no aborted swaps
2104 * and the swap types are determined for each image. By the end of the loop
2105 * all required update operations will have been finished.
2106 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002107 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002108#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesf11de642021-10-15 11:11:56 +01002109 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2110 continue;
2111 }
2112
David Vinczeba3bd602019-06-17 16:01:43 +02002113#ifdef MCUBOOT_ENC_IMAGES
2114 /* The keys used for encryption may no longer be valid (could belong to
2115 * another images). Therefore, mark them as invalid to force their reload
2116 * by boot_enc_load().
2117 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03002118 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002119#endif /* MCUBOOT_ENC_IMAGES */
2120
2121 /* Indicate that swap is not aborted */
Fabio Utzig12d59162019-11-28 10:01:59 -03002122 boot_status_reset(&bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002123#endif /* (BOOT_IMAGE_NUMBER > 1) */
2124
2125 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002126 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02002127
Fabio Utzig10ee6482019-08-01 12:04:52 -03002128 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002129 case BOOT_SWAP_TYPE_NONE:
2130 break;
2131
Jerzy Kasenberge3f895d2022-04-12 15:05:33 +02002132 case BOOT_SWAP_TYPE_TEST:
Michael Grand99613c62023-06-20 15:01:00 +02002133 /* fallthrough */
2134 case BOOT_SWAP_TYPE_PERM:
Jerzy Kasenberge3f895d2022-04-12 15:05:33 +02002135 if (check_downgrade_prevention(state) != 0) {
2136 /* Downgrade prevented */
2137 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
2138 break;
2139 }
2140 /* fallthrough */
David Vinczeba3bd602019-06-17 16:01:43 +02002141 case BOOT_SWAP_TYPE_REVERT:
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +02002142 rc = BOOT_HOOK_CALL(boot_perform_update_hook, BOOT_HOOK_REGULAR,
2143 BOOT_CURR_IMG(state), &(BOOT_IMG(state, 1).hdr),
2144 BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT));
2145 if (rc == BOOT_HOOK_REGULAR)
2146 {
2147 rc = boot_perform_update(state, &bs);
2148 }
David Vinczeba3bd602019-06-17 16:01:43 +02002149 assert(rc == 0);
2150 break;
2151
2152 case BOOT_SWAP_TYPE_FAIL:
2153 /* The image in secondary slot was invalid and is now erased. Ensure
2154 * we don't try to boot into it again on the next reboot. Do this by
2155 * pretending we just reverted back to primary slot.
2156 */
2157#ifndef MCUBOOT_OVERWRITE_ONLY
2158 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig12d59162019-11-28 10:01:59 -03002159 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002160 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002161 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002162 }
2163#endif /* !MCUBOOT_OVERWRITE_ONLY */
2164 break;
2165
2166 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002167 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002168 }
2169
Fabio Utzig10ee6482019-08-01 12:04:52 -03002170 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002171 BOOT_LOG_ERR("panic!");
2172 assert(0);
2173
2174 /* Loop forever... */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002175 FIH_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002176 }
2177 }
2178
2179 /* Iterate over all the images. At this point all required update operations
2180 * have finished. By the end of the loop each image in the primary slot will
2181 * have been re-validated.
2182 */
Michael Grand5047f032022-11-24 16:49:56 +01002183 FIH_SET(fih_cnt, 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -03002184 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002185#if BOOT_IMAGE_NUMBER > 1
Michael Grand5047f032022-11-24 16:49:56 +01002186 /* Hardenned to prevent from skipping check of a given image,
2187 * tmp_img_mask is declared volatile
2188 */
2189 volatile bool tmp_img_mask;
2190 FIH_SET(tmp_img_mask, state->img_mask[BOOT_CURR_IMG(state)]);
2191 if (FIH_EQ(tmp_img_mask, true)) {
2192 ++fih_cnt;
Raef Colesf11de642021-10-15 11:11:56 +01002193 continue;
2194 }
2195#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -03002196 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02002197 /* Attempt to read an image header from each slot. Ensure that image
2198 * headers in slots are aligned with headers in boot_data.
2199 */
Fabio Utzig12d59162019-11-28 10:01:59 -03002200 rc = boot_read_image_headers(state, false, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002201 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01002202 FIH_SET(fih_rc, FIH_FAILURE);
David Vinczeba3bd602019-06-17 16:01:43 +02002203 goto out;
2204 }
2205 /* Since headers were reloaded, it can be assumed we just performed
2206 * a swap or overwrite. Now the header info that should be used to
2207 * provide the data for the bootstrap, which previously was at
2208 * secondary slot, was updated to primary slot.
2209 */
2210 }
2211
2212#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Raef Colese8fe6cf2020-05-26 13:07:40 +01002213 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, NULL);
Michael Grand5047f032022-11-24 16:49:56 +01002214 /* Check for all possible values is redundant in normal operation it
2215 * is meant to prevent FI attack.
2216 */
2217 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS) ||
2218 FIH_EQ(fih_rc, FIH_FAILURE) ||
2219 FIH_EQ(fih_rc, FIH_NO_BOOTABLE_IMAGE)) {
2220 FIH_SET(fih_rc, FIH_FAILURE);
David Vinczeba3bd602019-06-17 16:01:43 +02002221 goto out;
2222 }
2223#else
2224 /* Even if we're not re-validating the primary slot, we could be booting
2225 * onto an empty flash chip. At least do a basic sanity check that
2226 * the magic number on the image is OK.
2227 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002228 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002229 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
Dominik Ermel4a4d1ac2021-08-06 11:23:52 +00002230 BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic,
Fabio Utzig10ee6482019-08-01 12:04:52 -03002231 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002232 rc = BOOT_EBADIMAGE;
Michael Grand5047f032022-11-24 16:49:56 +01002233 FIH_SET(fih_rc, FIH_FAILURE);
David Vinczeba3bd602019-06-17 16:01:43 +02002234 goto out;
2235 }
David Vinczec3084132020-02-18 14:50:47 +01002236#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
2237
Mark Horvathccaf7f82021-01-04 18:16:42 +01002238 rc = boot_update_hw_rollback_protection(state);
David Vincze1cf11b52020-03-24 07:51:09 +01002239 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01002240 FIH_SET(fih_rc, FIH_FAILURE);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002241 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002242 }
David Vincze1cf11b52020-03-24 07:51:09 +01002243
Mark Horvathccaf7f82021-01-04 18:16:42 +01002244 rc = boot_add_shared_data(state, BOOT_PRIMARY_SLOT);
David Vincze1cf11b52020-03-24 07:51:09 +01002245 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01002246 FIH_SET(fih_rc, FIH_FAILURE);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002247 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002248 }
Michael Grand5047f032022-11-24 16:49:56 +01002249 ++fih_cnt;
David Vinczeba3bd602019-06-17 16:01:43 +02002250 }
Michael Grand5047f032022-11-24 16:49:56 +01002251 /*
2252 * fih_cnt should be equal to BOOT_IMAGE_NUMBER now.
2253 * If this is not the case, at least one iteration of the loop
2254 * has been skipped.
2255 */
2256 if(FIH_NOT_EQ(fih_cnt, BOOT_IMAGE_NUMBER)) {
2257 FIH_PANIC;
2258 }
Fabio Utzigf616c542019-12-19 15:23:32 -03002259
Raef Colesfe57e7d2021-10-15 11:07:09 +01002260 fill_rsp(state, rsp);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002261
Raef Colese8fe6cf2020-05-26 13:07:40 +01002262 fih_rc = FIH_SUCCESS;
Fabio Utzig298913b2019-08-28 11:22:45 -03002263out:
Dominik Ermel256bc372022-12-07 15:51:31 +00002264 /*
2265 * Since the boot_status struct stores plaintext encryption keys, reset
2266 * them here to avoid the possibility of jumping into an image that could
2267 * easily recover them.
2268 */
2269#if defined(MCUBOOT_ENC_IMAGES) || defined(MCUBOOT_SWAP_SAVE_ENCTLV)
2270 like_mbedtls_zeroize(&bs, sizeof(bs));
2271#else
2272 memset(&bs, 0, sizeof(struct boot_status));
2273#endif
2274
Mark Horvathccaf7f82021-01-04 18:16:42 +01002275 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002276 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002277}
2278
Michael Grand5047f032022-11-24 16:49:56 +01002279fih_ret
Christopher Collins92ea77f2016-12-12 15:59:26 -08002280split_go(int loader_slot, int split_slot, void **entry)
2281{
Marti Bolivarc50926f2017-06-14 09:35:40 -04002282 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08002283 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002284 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002285 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002286 int rc;
Michael Grand5047f032022-11-24 16:49:56 +01002287 FIH_DECLARE(fih_rc, FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002288
Christopher Collins92ea77f2016-12-12 15:59:26 -08002289 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
2290 if (sectors == NULL) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002291 FIH_RET(FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002292 }
David Vinczeba3bd602019-06-17 16:01:43 +02002293 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
2294 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002295
2296 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
2297 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07002298 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04002299 assert(rc == 0);
2300 split_flash_id = flash_area_id_from_image_slot(split_slot);
2301 rc = flash_area_open(split_flash_id,
2302 &BOOT_IMG_AREA(&boot_data, split_slot));
2303 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002304
2305 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002306 rc = boot_read_sectors(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002307 if (rc != 0) {
2308 rc = SPLIT_GO_ERR;
2309 goto done;
2310 }
2311
Fabio Utzig12d59162019-11-28 10:01:59 -03002312 rc = boot_read_image_headers(&boot_data, true, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002313 if (rc != 0) {
2314 goto done;
2315 }
2316
Christopher Collins92ea77f2016-12-12 15:59:26 -08002317 /* Don't check the bootable image flag because we could really call a
2318 * bootable or non-bootable image. Just validate that the image check
2319 * passes which is distinct from the normal check.
2320 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002321 FIH_CALL(split_image_check, fih_rc,
2322 boot_img_hdr(&boot_data, split_slot),
2323 BOOT_IMG_AREA(&boot_data, split_slot),
2324 boot_img_hdr(&boot_data, loader_slot),
2325 BOOT_IMG_AREA(&boot_data, loader_slot));
Michael Grand5047f032022-11-24 16:49:56 +01002326 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08002327 goto done;
2328 }
2329
Marti Bolivarea088872017-06-12 17:10:49 -04002330 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04002331 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08002332 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002333 rc = SPLIT_GO_OK;
2334
2335done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04002336 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
2337 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002338 free(sectors);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002339
2340 if (rc) {
Michael Grand5047f032022-11-24 16:49:56 +01002341 FIH_SET(fih_rc, FIH_FAILURE);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002342 }
2343
2344 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002345}
David Vinczee574f2d2020-07-10 11:42:03 +02002346
Tamas Banfe031092020-09-10 17:32:39 +02002347#else /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02002348
Mark Horvathccaf7f82021-01-04 18:16:42 +01002349#define NO_ACTIVE_SLOT UINT32_MAX
2350
David Vinczee574f2d2020-07-10 11:42:03 +02002351/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002352 * Opens all flash areas and checks which contain an image with a valid header.
David Vinczee574f2d2020-07-10 11:42:03 +02002353 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002354 * @param state Boot loader status information.
David Vinczee574f2d2020-07-10 11:42:03 +02002355 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002356 * @return 0 on success; nonzero on failure.
2357 */
2358static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002359boot_get_slot_usage(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002360{
2361 uint32_t slot;
2362 int fa_id;
2363 int rc;
2364 struct image_header *hdr = NULL;
2365
2366 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002367#if BOOT_IMAGE_NUMBER > 1
2368 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2369 continue;
2370 }
2371#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +01002372 /* Open all the slots */
2373 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2374 fa_id = flash_area_id_from_multi_image_slot(
2375 BOOT_CURR_IMG(state), slot);
2376 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
2377 assert(rc == 0);
2378 }
2379
2380 /* Attempt to read an image header from each slot. */
2381 rc = boot_read_image_headers(state, false, NULL);
2382 if (rc != 0) {
2383 BOOT_LOG_WRN("Failed reading image headers.");
2384 return rc;
2385 }
2386
2387 /* Check headers in all slots */
2388 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2389 hdr = boot_img_hdr(state, slot);
2390
2391 if (boot_is_header_valid(hdr, BOOT_IMG_AREA(state, slot))) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002392 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = true;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002393 BOOT_LOG_IMAGE_INFO(slot, hdr);
2394 } else {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002395 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = false;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002396 BOOT_LOG_INF("Image %d %s slot: Image not found",
2397 BOOT_CURR_IMG(state),
2398 (slot == BOOT_PRIMARY_SLOT)
2399 ? "Primary" : "Secondary");
2400 }
2401 }
2402
Raef Colesfe57e7d2021-10-15 11:07:09 +01002403 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002404 }
2405
2406 return 0;
2407}
2408
2409/**
2410 * Finds the slot containing the image with the highest version number for the
2411 * current image.
2412 *
2413 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002414 *
2415 * @return NO_ACTIVE_SLOT if no available slot found, number of
2416 * the found slot otherwise.
David Vinczee574f2d2020-07-10 11:42:03 +02002417 */
2418static uint32_t
Raef Colesfe57e7d2021-10-15 11:07:09 +01002419find_slot_with_highest_version(struct boot_loader_state *state)
David Vinczee574f2d2020-07-10 11:42:03 +02002420{
David Vinczee574f2d2020-07-10 11:42:03 +02002421 uint32_t slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002422 uint32_t candidate_slot = NO_ACTIVE_SLOT;
2423 int rc;
David Vinczee574f2d2020-07-10 11:42:03 +02002424
Mark Horvathccaf7f82021-01-04 18:16:42 +01002425 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002426 if (state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot]) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002427 if (candidate_slot == NO_ACTIVE_SLOT) {
2428 candidate_slot = slot;
2429 } else {
2430 rc = boot_version_cmp(
2431 &boot_img_hdr(state, slot)->ih_ver,
2432 &boot_img_hdr(state, candidate_slot)->ih_ver);
2433 if (rc == 1) {
2434 /* The version of the image being examined is greater than
2435 * the version of the current candidate.
2436 */
2437 candidate_slot = slot;
2438 }
2439 }
David Vinczee574f2d2020-07-10 11:42:03 +02002440 }
2441 }
2442
Mark Horvathccaf7f82021-01-04 18:16:42 +01002443 return candidate_slot;
David Vinczee574f2d2020-07-10 11:42:03 +02002444}
2445
Mark Horvathccaf7f82021-01-04 18:16:42 +01002446#ifdef MCUBOOT_HAVE_LOGGING
2447/**
2448 * Prints the state of the loaded images.
2449 *
2450 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002451 */
2452static void
Raef Colesfe57e7d2021-10-15 11:07:09 +01002453print_loaded_images(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002454{
2455 uint32_t active_slot;
2456
David Brown695e5912021-05-24 16:58:01 -06002457 (void)state;
2458
Mark Horvathccaf7f82021-01-04 18:16:42 +01002459 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002460#if BOOT_IMAGE_NUMBER > 1
2461 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2462 continue;
2463 }
2464#endif
Raef Colesfe57e7d2021-10-15 11:07:09 +01002465 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002466
2467 BOOT_LOG_INF("Image %d loaded from the %s slot",
2468 BOOT_CURR_IMG(state),
2469 (active_slot == BOOT_PRIMARY_SLOT) ?
2470 "primary" : "secondary");
2471 }
2472}
2473#endif
2474
David Vincze1c456242021-06-29 15:25:24 +02002475#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
David Vincze505fba22020-10-22 13:53:29 +02002476/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002477 * Checks whether the active slot of the current image was previously selected
2478 * to run. Erases the image if it was selected but its execution failed,
2479 * otherwise marks it as selected if it has not been before.
David Vincze505fba22020-10-22 13:53:29 +02002480 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002481 * @param state Boot loader status information.
David Vincze505fba22020-10-22 13:53:29 +02002482 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002483 * @return 0 on success; nonzero on failure.
David Vincze505fba22020-10-22 13:53:29 +02002484 */
2485static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002486boot_select_or_erase(struct boot_loader_state *state)
David Vincze505fba22020-10-22 13:53:29 +02002487{
2488 const struct flash_area *fap;
2489 int fa_id;
2490 int rc;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002491 uint32_t active_slot;
2492 struct boot_swap_state* active_swap_state;
David Vincze505fba22020-10-22 13:53:29 +02002493
Raef Colesfe57e7d2021-10-15 11:07:09 +01002494 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002495
2496 fa_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), active_slot);
David Vincze505fba22020-10-22 13:53:29 +02002497 rc = flash_area_open(fa_id, &fap);
2498 assert(rc == 0);
2499
Raef Colesfe57e7d2021-10-15 11:07:09 +01002500 active_swap_state = &(state->slot_usage[BOOT_CURR_IMG(state)].swap_state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002501
2502 memset(active_swap_state, 0, sizeof(struct boot_swap_state));
2503 rc = boot_read_swap_state(fap, active_swap_state);
David Vincze505fba22020-10-22 13:53:29 +02002504 assert(rc == 0);
2505
Mark Horvathccaf7f82021-01-04 18:16:42 +01002506 if (active_swap_state->magic != BOOT_MAGIC_GOOD ||
2507 (active_swap_state->copy_done == BOOT_FLAG_SET &&
2508 active_swap_state->image_ok != BOOT_FLAG_SET)) {
David Vincze505fba22020-10-22 13:53:29 +02002509 /*
2510 * A reboot happened without the image being confirmed at
2511 * runtime or its trailer is corrupted/invalid. Erase the image
2512 * to prevent it from being selected again on the next reboot.
2513 */
2514 BOOT_LOG_DBG("Erasing faulty image in the %s slot.",
Carlos Falgueras Garcíaae13c3c2021-06-21 17:20:31 +02002515 (active_slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
Dominik Ermel260ae092021-04-23 05:38:45 +00002516 rc = flash_area_erase(fap, 0, flash_area_get_size(fap));
David Vincze505fba22020-10-22 13:53:29 +02002517 assert(rc == 0);
2518
2519 flash_area_close(fap);
2520 rc = -1;
2521 } else {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002522 if (active_swap_state->copy_done != BOOT_FLAG_SET) {
2523 if (active_swap_state->copy_done == BOOT_FLAG_BAD) {
David Vincze505fba22020-10-22 13:53:29 +02002524 BOOT_LOG_DBG("The copy_done flag had an unexpected value. Its "
2525 "value was neither 'set' nor 'unset', but 'bad'.");
2526 }
2527 /*
2528 * Set the copy_done flag, indicating that the image has been
2529 * selected to boot. It can be set in advance, before even
2530 * validating the image, because in case the validation fails, the
2531 * entire image slot will be erased (including the trailer).
2532 */
2533 rc = boot_write_copy_done(fap);
2534 if (rc != 0) {
2535 BOOT_LOG_WRN("Failed to set copy_done flag of the image in "
Carlos Falgueras Garcíaae13c3c2021-06-21 17:20:31 +02002536 "the %s slot.", (active_slot == BOOT_PRIMARY_SLOT) ?
David Vincze505fba22020-10-22 13:53:29 +02002537 "primary" : "secondary");
2538 rc = 0;
2539 }
2540 }
2541 flash_area_close(fap);
2542 }
2543
2544 return rc;
2545}
David Vincze1c456242021-06-29 15:25:24 +02002546#endif /* MCUBOOT_DIRECT_XIP && MCUBOOT_DIRECT_XIP_REVERT */
David Vincze505fba22020-10-22 13:53:29 +02002547
Tamas Banfe031092020-09-10 17:32:39 +02002548#ifdef MCUBOOT_RAM_LOAD
2549
Mark Horvathccaf7f82021-01-04 18:16:42 +01002550#ifndef MULTIPLE_EXECUTABLE_RAM_REGIONS
Tamas Banfe031092020-09-10 17:32:39 +02002551#if !defined(IMAGE_EXECUTABLE_RAM_START) || !defined(IMAGE_EXECUTABLE_RAM_SIZE)
2552#error "Platform MUST define executable RAM bounds in case of RAM_LOAD"
2553#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +01002554#endif
Tamas Banfe031092020-09-10 17:32:39 +02002555
2556/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002557 * Verifies that the active slot of the current image can be loaded within the
2558 * predefined bounds that are allowed to be used by executable images.
Tamas Banfe031092020-09-10 17:32:39 +02002559 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002560 * @param state Boot loader status information.
Tamas Banfe031092020-09-10 17:32:39 +02002561 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002562 * @return 0 on success; nonzero on failure.
Tamas Banfe031092020-09-10 17:32:39 +02002563 */
2564static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002565boot_verify_ram_load_address(struct boot_loader_state *state)
Tamas Banfe031092020-09-10 17:32:39 +02002566{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002567 uint32_t img_dst;
2568 uint32_t img_sz;
Tamas Banfe031092020-09-10 17:32:39 +02002569 uint32_t img_end_addr;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002570 uint32_t exec_ram_start;
2571 uint32_t exec_ram_size;
David Brown695e5912021-05-24 16:58:01 -06002572
2573 (void)state;
2574
Mark Horvathccaf7f82021-01-04 18:16:42 +01002575#ifdef MULTIPLE_EXECUTABLE_RAM_REGIONS
2576 int rc;
Tamas Banfe031092020-09-10 17:32:39 +02002577
Mark Horvathccaf7f82021-01-04 18:16:42 +01002578 rc = boot_get_image_exec_ram_info(BOOT_CURR_IMG(state), &exec_ram_start,
2579 &exec_ram_size);
2580 if (rc != 0) {
2581 return BOOT_EBADSTATUS;
2582 }
2583#else
2584 exec_ram_start = IMAGE_EXECUTABLE_RAM_START;
2585 exec_ram_size = IMAGE_EXECUTABLE_RAM_SIZE;
2586#endif
2587
Raef Colesfe57e7d2021-10-15 11:07:09 +01002588 img_dst = state->slot_usage[BOOT_CURR_IMG(state)].img_dst;
2589 img_sz = state->slot_usage[BOOT_CURR_IMG(state)].img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002590
2591 if (img_dst < exec_ram_start) {
Tamas Banfe031092020-09-10 17:32:39 +02002592 return BOOT_EBADIMAGE;
2593 }
2594
2595 if (!boot_u32_safe_add(&img_end_addr, img_dst, img_sz)) {
2596 return BOOT_EBADIMAGE;
2597 }
2598
Mark Horvathccaf7f82021-01-04 18:16:42 +01002599 if (img_end_addr > (exec_ram_start + exec_ram_size)) {
Tamas Banfe031092020-09-10 17:32:39 +02002600 return BOOT_EBADIMAGE;
2601 }
2602
2603 return 0;
2604}
2605
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002606#ifdef MCUBOOT_ENC_IMAGES
2607
2608/**
2609 * Copies and decrypts an image from a slot in the flash to an SRAM address.
2610 *
2611 * @param state Boot loader status information.
2612 * @param slot The flash slot of the image to be copied to SRAM.
2613 * @param hdr The image header.
2614 * @param src_sz Size of the image.
2615 * @param img_dst Pointer to the address at which the image needs to be
2616 * copied to SRAM.
2617 *
2618 * @return 0 on success; nonzero on failure.
2619 */
2620static int
2621boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state,
2622 uint32_t slot, struct image_header *hdr,
2623 uint32_t src_sz, uint32_t img_dst)
2624{
2625 /* The flow for the decryption and copy of the image is as follows :
2626 * 1. The whole image is copied to the RAM (header + payload + TLV).
2627 * 2. The encryption key is loaded from the TLV in flash.
2628 * 3. The image is then decrypted chunk by chunk in RAM (1 chunk
2629 * is 1024 bytes). Only the payload section is decrypted.
2630 * 4. The image is authenticated in RAM.
2631 */
2632 const struct flash_area *fap_src = NULL;
2633 struct boot_status bs;
2634 uint32_t blk_off;
2635 uint32_t tlv_off;
2636 uint32_t blk_sz;
2637 uint32_t bytes_copied = hdr->ih_hdr_size;
2638 uint32_t chunk_sz;
2639 uint32_t max_sz = 1024;
2640 uint16_t idx;
2641 uint8_t image_index;
2642 uint8_t * cur_dst;
2643 int area_id;
2644 int rc;
2645 uint8_t * ram_dst = (void *)(IMAGE_RAM_BASE + img_dst);
2646
2647 image_index = BOOT_CURR_IMG(state);
2648 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2649 rc = flash_area_open(area_id, &fap_src);
2650 if (rc != 0){
2651 return BOOT_EFLASH;
2652 }
2653
2654 tlv_off = BOOT_TLV_OFF(hdr);
2655
2656 /* Copying the whole image in RAM */
2657 rc = flash_area_read(fap_src, 0, ram_dst, src_sz);
2658 if (rc != 0) {
2659 goto done;
2660 }
2661
2662 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap_src, &bs);
2663 if (rc < 0) {
2664 goto done;
2665 }
2666
2667 /* if rc > 0 then the key has already been loaded */
2668 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), slot, &bs)) {
2669 goto done;
2670 }
2671
2672 /* Starting at the end of the header as the header section is not encrypted */
2673 while (bytes_copied < tlv_off) { /* TLV section copied previously */
2674 if (src_sz - bytes_copied > max_sz) {
2675 chunk_sz = max_sz;
2676 } else {
2677 chunk_sz = src_sz - bytes_copied;
2678 }
2679
2680 cur_dst = ram_dst + bytes_copied;
2681 blk_sz = chunk_sz;
2682 idx = 0;
2683 if (bytes_copied + chunk_sz > tlv_off) {
2684 /* Going over TLV section
2685 * Part of the chunk is encrypted payload */
2686 blk_off = ((bytes_copied) - hdr->ih_hdr_size) & 0xf;
2687 blk_sz = tlv_off - (bytes_copied);
2688 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
2689 (bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
2690 blk_off, cur_dst);
2691 } else {
2692 /* Image encrypted payload section */
2693 blk_off = ((bytes_copied) - hdr->ih_hdr_size) & 0xf;
2694 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
2695 (bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
2696 blk_off, cur_dst);
2697 }
2698
2699 bytes_copied += chunk_sz;
2700 }
2701 rc = 0;
2702
2703done:
2704 flash_area_close(fap_src);
2705
2706 return rc;
2707}
2708
2709#endif /* MCUBOOT_ENC_IMAGES */
Tamas Banfe031092020-09-10 17:32:39 +02002710/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002711 * Copies a slot of the current image into SRAM.
Tamas Banfe031092020-09-10 17:32:39 +02002712 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002713 * @param state Boot loader status information.
Tamas Banfe031092020-09-10 17:32:39 +02002714 * @param slot The flash slot of the image to be copied to SRAM.
2715 * @param img_dst The address at which the image needs to be copied to
2716 * SRAM.
2717 * @param img_sz The size of the image that needs to be copied to SRAM.
2718 *
2719 * @return 0 on success; nonzero on failure.
2720 */
2721static int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002722boot_copy_image_to_sram(struct boot_loader_state *state, int slot,
2723 uint32_t img_dst, uint32_t img_sz)
Tamas Banfe031092020-09-10 17:32:39 +02002724{
2725 int rc;
2726 const struct flash_area *fap_src = NULL;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002727 int area_id;
Tamas Banfe031092020-09-10 17:32:39 +02002728
Mark Horvathccaf7f82021-01-04 18:16:42 +01002729#if (BOOT_IMAGE_NUMBER == 1)
2730 (void)state;
2731#endif
2732
2733 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2734
2735 rc = flash_area_open(area_id, &fap_src);
Tamas Banfe031092020-09-10 17:32:39 +02002736 if (rc != 0) {
2737 return BOOT_EFLASH;
2738 }
2739
2740 /* Direct copy from flash to its new location in SRAM. */
David Brown9bd7f902021-05-26 16:31:14 -06002741 rc = flash_area_read(fap_src, 0, (void *)(IMAGE_RAM_BASE + img_dst), img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002742 if (rc != 0) {
Antonio de Angelis48547002023-04-14 09:47:09 +01002743 BOOT_LOG_INF("Error whilst copying image %d from Flash to SRAM: %d",
2744 BOOT_CURR_IMG(state), rc);
Tamas Banfe031092020-09-10 17:32:39 +02002745 }
2746
2747 flash_area_close(fap_src);
2748
2749 return rc;
2750}
2751
Mark Horvathccaf7f82021-01-04 18:16:42 +01002752#if (BOOT_IMAGE_NUMBER > 1)
Tamas Banfe031092020-09-10 17:32:39 +02002753/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002754 * Checks if two memory regions (A and B) are overlap or not.
Tamas Banfe031092020-09-10 17:32:39 +02002755 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002756 * @param start_a Start of the A region.
2757 * @param end_a End of the A region.
2758 * @param start_b Start of the B region.
2759 * @param end_b End of the B region.
Tamas Banfe031092020-09-10 17:32:39 +02002760 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002761 * @return true if there is overlap; false otherwise.
2762 */
2763static bool
2764do_regions_overlap(uint32_t start_a, uint32_t end_a,
2765 uint32_t start_b, uint32_t end_b)
2766{
2767 if (start_b > end_a) {
2768 return false;
2769 } else if (start_b >= start_a) {
2770 return true;
2771 } else if (end_b > start_a) {
2772 return true;
2773 }
2774
2775 return false;
2776}
2777
2778/**
2779 * Checks if the image we want to load to memory overlap with an already
2780 * ramloaded image.
2781 *
Raef Colesfe57e7d2021-10-15 11:07:09 +01002782 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002783 *
2784 * @return 0 if there is no overlap; nonzero otherwise.
Tamas Banfe031092020-09-10 17:32:39 +02002785 */
2786static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002787boot_check_ram_load_overlapping(struct boot_loader_state *state)
Tamas Banfe031092020-09-10 17:32:39 +02002788{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002789 uint32_t i;
2790
2791 uint32_t start_a;
2792 uint32_t end_a;
2793 uint32_t start_b;
2794 uint32_t end_b;
Raef Colesfe57e7d2021-10-15 11:07:09 +01002795 uint32_t image_id_to_check = BOOT_CURR_IMG(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002796
Raef Colesfe57e7d2021-10-15 11:07:09 +01002797 start_a = state->slot_usage[image_id_to_check].img_dst;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002798 /* Safe to add here, values are already verified in
2799 * boot_verify_ram_load_address() */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002800 end_a = start_a + state->slot_usage[image_id_to_check].img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002801
2802 for (i = 0; i < BOOT_IMAGE_NUMBER; i++) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002803 if (state->slot_usage[i].active_slot == NO_ACTIVE_SLOT
Mark Horvathccaf7f82021-01-04 18:16:42 +01002804 || i == image_id_to_check) {
2805 continue;
2806 }
2807
Raef Colesfe57e7d2021-10-15 11:07:09 +01002808 start_b = state->slot_usage[i].img_dst;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002809 /* Safe to add here, values are already verified in
2810 * boot_verify_ram_load_address() */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002811 end_b = start_b + state->slot_usage[i].img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002812
2813 if (do_regions_overlap(start_a, end_a, start_b, end_b)) {
2814 return -1;
2815 }
2816 }
2817
2818 return 0;
2819}
2820#endif
2821
2822/**
2823 * Loads the active slot of the current image into SRAM. The load address and
2824 * image size is extracted from the image header.
2825 *
2826 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002827 *
2828 * @return 0 on success; nonzero on failure.
2829 */
2830static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002831boot_load_image_to_sram(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002832{
2833 uint32_t active_slot;
2834 struct image_header *hdr = NULL;
2835 uint32_t img_dst;
2836 uint32_t img_sz;
Tamas Banfe031092020-09-10 17:32:39 +02002837 int rc;
2838
Raef Colesfe57e7d2021-10-15 11:07:09 +01002839 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002840 hdr = boot_img_hdr(state, active_slot);
2841
Tamas Banfe031092020-09-10 17:32:39 +02002842 if (hdr->ih_flags & IMAGE_F_RAM_LOAD) {
2843
Mark Horvathccaf7f82021-01-04 18:16:42 +01002844 img_dst = hdr->ih_load_addr;
Tamas Banfe031092020-09-10 17:32:39 +02002845
Mark Horvathccaf7f82021-01-04 18:16:42 +01002846 rc = boot_read_image_size(state, active_slot, &img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002847 if (rc != 0) {
2848 return rc;
2849 }
2850
Raef Colesfe57e7d2021-10-15 11:07:09 +01002851 state->slot_usage[BOOT_CURR_IMG(state)].img_dst = img_dst;
2852 state->slot_usage[BOOT_CURR_IMG(state)].img_sz = img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002853
Raef Colesfe57e7d2021-10-15 11:07:09 +01002854 rc = boot_verify_ram_load_address(state);
Tamas Banfe031092020-09-10 17:32:39 +02002855 if (rc != 0) {
Antonio de Angelisba5fb1c2022-10-11 10:10:37 +01002856 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 +02002857 return rc;
2858 }
2859
Mark Horvathccaf7f82021-01-04 18:16:42 +01002860#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesfe57e7d2021-10-15 11:07:09 +01002861 rc = boot_check_ram_load_overlapping(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002862 if (rc != 0) {
Antonio de Angelisba5fb1c2022-10-11 10:10:37 +01002863 BOOT_LOG_INF("Image %d RAM loading to address 0x%x would overlap with\
2864 another image.", BOOT_CURR_IMG(state), img_dst);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002865 return rc;
2866 }
2867#endif
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002868#ifdef MCUBOOT_ENC_IMAGES
2869 /* decrypt image if encrypted and copy it to RAM */
2870 if (IS_ENCRYPTED(hdr)) {
2871 rc = boot_decrypt_and_copy_image_to_sram(state, active_slot, hdr, img_sz, img_dst);
2872 } else {
2873 rc = boot_copy_image_to_sram(state, active_slot, img_dst, img_sz);
2874 }
2875#else
Tamas Banfe031092020-09-10 17:32:39 +02002876 /* Copy image to the load address from where it currently resides in
2877 * flash.
2878 */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002879 rc = boot_copy_image_to_sram(state, active_slot, img_dst, img_sz);
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002880#endif
Tamas Banfe031092020-09-10 17:32:39 +02002881 if (rc != 0) {
Antonio de Angelisba5fb1c2022-10-11 10:10:37 +01002882 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 +02002883 } else {
Antonio de Angelisba5fb1c2022-10-11 10:10:37 +01002884 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 +02002885 }
2886 } else {
2887 /* Only images that support IMAGE_F_RAM_LOAD are allowed if
2888 * MCUBOOT_RAM_LOAD is set.
2889 */
2890 rc = BOOT_EBADIMAGE;
2891 }
2892
Mark Horvathccaf7f82021-01-04 18:16:42 +01002893 if (rc != 0) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002894 state->slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
2895 state->slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002896 }
2897
Tamas Banfe031092020-09-10 17:32:39 +02002898 return rc;
2899}
2900
2901/**
2902 * Removes an image from SRAM, by overwriting it with zeros.
2903 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002904 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002905 *
2906 * @return 0 on success; nonzero on failure.
2907 */
2908static inline int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002909boot_remove_image_from_sram(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002910{
David Brown695e5912021-05-24 16:58:01 -06002911 (void)state;
2912
Antonio de Angelisba5fb1c2022-10-11 10:10:37 +01002913 BOOT_LOG_INF("Removing image %d from SRAM at address 0x%x",
2914 BOOT_CURR_IMG(state),
Raef Colesfe57e7d2021-10-15 11:07:09 +01002915 state->slot_usage[BOOT_CURR_IMG(state)].img_dst);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002916
Raef Colesfe57e7d2021-10-15 11:07:09 +01002917 memset((void*)(IMAGE_RAM_BASE + state->slot_usage[BOOT_CURR_IMG(state)].img_dst),
2918 0, state->slot_usage[BOOT_CURR_IMG(state)].img_sz);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002919
Raef Colesfe57e7d2021-10-15 11:07:09 +01002920 state->slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
2921 state->slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002922
2923 return 0;
2924}
2925
2926/**
2927 * Removes an image from flash by erasing the corresponding flash area
2928 *
2929 * @param state Boot loader status information.
2930 * @param slot The flash slot of the image to be erased.
Tamas Banfe031092020-09-10 17:32:39 +02002931 *
2932 * @return 0 on success; nonzero on failure.
2933 */
2934static inline int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002935boot_remove_image_from_flash(struct boot_loader_state *state, uint32_t slot)
Tamas Banfe031092020-09-10 17:32:39 +02002936{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002937 int area_id;
2938 int rc;
2939 const struct flash_area *fap;
Tamas Banfe031092020-09-10 17:32:39 +02002940
David Brown695e5912021-05-24 16:58:01 -06002941 (void)state;
2942
Mark Horvathccaf7f82021-01-04 18:16:42 +01002943 BOOT_LOG_INF("Removing image %d slot %d from flash", BOOT_CURR_IMG(state),
2944 slot);
2945 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2946 rc = flash_area_open(area_id, &fap);
2947 if (rc == 0) {
Dominik Ermel260ae092021-04-23 05:38:45 +00002948 flash_area_erase(fap, 0, flash_area_get_size(fap));
Dominik Ermel245de812022-09-02 13:39:23 +00002949 flash_area_close(fap);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002950 }
2951
2952 return rc;
Tamas Banfe031092020-09-10 17:32:39 +02002953}
2954#endif /* MCUBOOT_RAM_LOAD */
2955
Mark Horvathccaf7f82021-01-04 18:16:42 +01002956#if (BOOT_IMAGE_NUMBER > 1)
2957/**
2958 * Checks the image dependency whether it is satisfied.
2959 *
2960 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002961 * @param dep Image dependency which has to be verified.
2962 *
2963 * @return 0 if dependencies are met; nonzero otherwise.
2964 */
2965static int
2966boot_verify_slot_dependency(struct boot_loader_state *state,
Mark Horvathccaf7f82021-01-04 18:16:42 +01002967 struct image_dependency *dep)
David Vinczee574f2d2020-07-10 11:42:03 +02002968{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002969 struct image_version *dep_version;
2970 uint32_t dep_slot;
David Vinczee574f2d2020-07-10 11:42:03 +02002971 int rc;
2972
Mark Horvathccaf7f82021-01-04 18:16:42 +01002973 /* Determine the source of the image which is the subject of
2974 * the dependency and get it's version.
David Vinczee574f2d2020-07-10 11:42:03 +02002975 */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002976 dep_slot = state->slot_usage[dep->image_id].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002977 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
2978
2979 rc = boot_version_cmp(dep_version, &dep->image_min_version);
2980 if (rc >= 0) {
2981 /* Dependency satisfied. */
2982 rc = 0;
David Vinczee574f2d2020-07-10 11:42:03 +02002983 }
2984
Mark Horvathccaf7f82021-01-04 18:16:42 +01002985 return rc;
2986}
2987
2988/**
2989 * Reads all dependency TLVs of an image and verifies one after another to see
2990 * if they are all satisfied.
2991 *
2992 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002993 *
2994 * @return 0 if dependencies are met; nonzero otherwise.
2995 */
2996static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002997boot_verify_slot_dependencies(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002998{
2999 uint32_t active_slot;
3000 const struct flash_area *fap;
3001 struct image_tlv_iter it;
3002 struct image_dependency dep;
3003 uint32_t off;
3004 uint16_t len;
3005 int area_id;
3006 int rc;
3007
Raef Colesfe57e7d2021-10-15 11:07:09 +01003008 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003009
3010 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state),
3011 active_slot);
3012 rc = flash_area_open(area_id, &fap);
David Vinczee574f2d2020-07-10 11:42:03 +02003013 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01003014 rc = BOOT_EFLASH;
3015 goto done;
David Vinczee574f2d2020-07-10 11:42:03 +02003016 }
3017
Mark Horvathccaf7f82021-01-04 18:16:42 +01003018 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, active_slot), fap,
3019 IMAGE_TLV_DEPENDENCY, true);
3020 if (rc != 0) {
3021 goto done;
3022 }
David Vinczee574f2d2020-07-10 11:42:03 +02003023
Mark Horvathccaf7f82021-01-04 18:16:42 +01003024 while (true) {
3025 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
3026 if (rc < 0) {
3027 return -1;
3028 } else if (rc > 0) {
3029 rc = 0;
3030 break;
3031 }
David Vinczee574f2d2020-07-10 11:42:03 +02003032
Mark Horvathccaf7f82021-01-04 18:16:42 +01003033 if (len != sizeof(dep)) {
3034 rc = BOOT_EBADIMAGE;
3035 goto done;
3036 }
3037
3038 rc = LOAD_IMAGE_DATA(boot_img_hdr(state, active_slot),
3039 fap, off, &dep, len);
3040 if (rc != 0) {
3041 rc = BOOT_EFLASH;
3042 goto done;
3043 }
3044
3045 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
3046 rc = BOOT_EBADARGS;
3047 goto done;
3048 }
3049
Raef Colesfe57e7d2021-10-15 11:07:09 +01003050 rc = boot_verify_slot_dependency(state, &dep);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003051 if (rc != 0) {
3052 /* Dependency not satisfied. */
3053 goto done;
3054 }
3055 }
3056
3057done:
3058 flash_area_close(fap);
3059 return rc;
3060}
3061
3062/**
3063 * Checks the dependency of all the active slots. If an image found with
3064 * invalid or not satisfied dependencies the image is removed from SRAM (in
3065 * case of MCUBOOT_RAM_LOAD strategy) and its slot is set to unavailable.
3066 *
3067 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01003068 *
3069 * @return 0 if dependencies are met; nonzero otherwise.
3070 */
3071static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01003072boot_verify_dependencies(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003073{
3074 int rc = -1;
3075 uint32_t active_slot;
3076
3077 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01003078 if (state->img_mask[BOOT_CURR_IMG(state)]) {
3079 continue;
3080 }
Raef Colesfe57e7d2021-10-15 11:07:09 +01003081 rc = boot_verify_slot_dependencies(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003082 if (rc != 0) {
3083 /* Dependencies not met or invalid dependencies. */
3084
3085#ifdef MCUBOOT_RAM_LOAD
Raef Colesfe57e7d2021-10-15 11:07:09 +01003086 boot_remove_image_from_sram(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003087#endif /* MCUBOOT_RAM_LOAD */
3088
Raef Colesfe57e7d2021-10-15 11:07:09 +01003089 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
3090 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3091 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003092
3093 return rc;
3094 }
3095 }
3096
3097 return rc;
3098}
3099#endif /* (BOOT_IMAGE_NUMBER > 1) */
3100
3101/**
3102 * Tries to load a slot for all the images with validation.
3103 *
3104 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01003105 *
3106 * @return 0 on success; nonzero on failure.
3107 */
Michael Grand5047f032022-11-24 16:49:56 +01003108fih_ret
Raef Colesfe57e7d2021-10-15 11:07:09 +01003109boot_load_and_validate_images(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003110{
3111 uint32_t active_slot;
3112 int rc;
Michael Grand5047f032022-11-24 16:49:56 +01003113 fih_ret fih_rc;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003114
3115 /* Go over all the images and try to load one */
3116 IMAGES_ITER(BOOT_CURR_IMG(state)) {
3117 /* All slots tried until a valid image found. Breaking from this loop
3118 * means that a valid image found or already loaded. If no slot is
3119 * found the function returns with error code. */
3120 while (true) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01003121 /* Go over all the slots and try to load one */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003122 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003123 if (active_slot != NO_ACTIVE_SLOT){
3124 /* A slot is already active, go to next image. */
3125 break;
David Vinczee574f2d2020-07-10 11:42:03 +02003126 }
David Vincze505fba22020-10-22 13:53:29 +02003127
Raef Colesfe57e7d2021-10-15 11:07:09 +01003128 active_slot = find_slot_with_highest_version(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003129 if (active_slot == NO_ACTIVE_SLOT) {
3130 BOOT_LOG_INF("No slot to load for image %d",
3131 BOOT_CURR_IMG(state));
3132 FIH_RET(FIH_FAILURE);
3133 }
3134
3135 /* Save the number of the active slot. */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003136 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003137
Raef Colesf11de642021-10-15 11:11:56 +01003138#if BOOT_IMAGE_NUMBER > 1
3139 if (state->img_mask[BOOT_CURR_IMG(state)]) {
3140 continue;
3141 }
3142#endif
3143
Mark Horvathccaf7f82021-01-04 18:16:42 +01003144#ifdef MCUBOOT_DIRECT_XIP
Raef Colesfe57e7d2021-10-15 11:07:09 +01003145 rc = boot_rom_address_check(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003146 if (rc != 0) {
3147 /* The image is placed in an unsuitable slot. */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003148 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3149 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003150 continue;
3151 }
George Becksteind4d90f82021-05-11 02:00:00 -04003152
David Vincze505fba22020-10-22 13:53:29 +02003153#ifdef MCUBOOT_DIRECT_XIP_REVERT
Raef Colesfe57e7d2021-10-15 11:07:09 +01003154 rc = boot_select_or_erase(state);
David Vincze505fba22020-10-22 13:53:29 +02003155 if (rc != 0) {
3156 /* The selected image slot has been erased. */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003157 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3158 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
David Vincze505fba22020-10-22 13:53:29 +02003159 continue;
3160 }
3161#endif /* MCUBOOT_DIRECT_XIP_REVERT */
David Vincze1c456242021-06-29 15:25:24 +02003162#endif /* MCUBOOT_DIRECT_XIP */
David Vincze505fba22020-10-22 13:53:29 +02003163
Tamas Banfe031092020-09-10 17:32:39 +02003164#ifdef MCUBOOT_RAM_LOAD
3165 /* Image is first loaded to RAM and authenticated there in order to
3166 * prevent TOCTOU attack during image copy. This could be applied
3167 * when loading images from external (untrusted) flash to internal
3168 * (trusted) RAM and image is authenticated before copying.
3169 */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003170 rc = boot_load_image_to_sram(state);
Tamas Banfe031092020-09-10 17:32:39 +02003171 if (rc != 0 ) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01003172 /* Image cannot be ramloaded. */
3173 boot_remove_image_from_flash(state, active_slot);
Raef Colesfe57e7d2021-10-15 11:07:09 +01003174 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3175 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Tamas Banfe031092020-09-10 17:32:39 +02003176 continue;
Tamas Banfe031092020-09-10 17:32:39 +02003177 }
3178#endif /* MCUBOOT_RAM_LOAD */
Mark Horvathccaf7f82021-01-04 18:16:42 +01003179
3180 FIH_CALL(boot_validate_slot, fih_rc, state, active_slot, NULL);
Michael Grand5047f032022-11-24 16:49:56 +01003181 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01003182 /* Image is invalid. */
Tamas Banfe031092020-09-10 17:32:39 +02003183#ifdef MCUBOOT_RAM_LOAD
Raef Colesfe57e7d2021-10-15 11:07:09 +01003184 boot_remove_image_from_sram(state);
Tamas Banfe031092020-09-10 17:32:39 +02003185#endif /* MCUBOOT_RAM_LOAD */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003186 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3187 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003188 continue;
David Vincze505fba22020-10-22 13:53:29 +02003189 }
Mark Horvathccaf7f82021-01-04 18:16:42 +01003190
3191 /* Valid image loaded from a slot, go to next image. */
3192 break;
3193 }
3194 }
3195
3196 FIH_RET(FIH_SUCCESS);
3197}
3198
3199/**
3200 * Updates the security counter for the current image.
3201 *
3202 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01003203 *
3204 * @return 0 on success; nonzero on failure.
3205 */
3206static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01003207boot_update_hw_rollback_protection(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003208{
3209#ifdef MCUBOOT_HW_ROLLBACK_PROT
3210 int rc;
3211
3212 /* Update the stored security counter with the newer (active) image's
3213 * security counter value.
3214 */
David Vincze1c456242021-06-29 15:25:24 +02003215#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003216 /* When the 'revert' mechanism is enabled in direct-xip mode, the
3217 * security counter can be increased only after reboot, if the image
3218 * has been confirmed at runtime (the image_ok flag has been set).
3219 * This way a 'revert' can be performed when it's necessary.
3220 */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003221 if (state->slot_usage[BOOT_CURR_IMG(state)].swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze505fba22020-10-22 13:53:29 +02003222#endif
Sherry Zhang50b06ae2021-07-09 15:22:51 +08003223 rc = boot_update_security_counter(BOOT_CURR_IMG(state),
Raef Colesfe57e7d2021-10-15 11:07:09 +01003224 state->slot_usage[BOOT_CURR_IMG(state)].active_slot,
3225 boot_img_hdr(state, state->slot_usage[BOOT_CURR_IMG(state)].active_slot));
David Vinczee574f2d2020-07-10 11:42:03 +02003226 if (rc != 0) {
Antonio de Angelisba5fb1c2022-10-11 10:10:37 +01003227 BOOT_LOG_ERR("Security counter update failed after image %d validation.", BOOT_CURR_IMG(state));
Mark Horvathccaf7f82021-01-04 18:16:42 +01003228 return rc;
David Vinczee574f2d2020-07-10 11:42:03 +02003229 }
David Vincze1c456242021-06-29 15:25:24 +02003230#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003231 }
3232#endif
David Vinczee574f2d2020-07-10 11:42:03 +02003233
Mark Horvathccaf7f82021-01-04 18:16:42 +01003234 return 0;
David Vinczee574f2d2020-07-10 11:42:03 +02003235
Mark Horvathccaf7f82021-01-04 18:16:42 +01003236#else /* MCUBOOT_HW_ROLLBACK_PROT */
3237 (void) (state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003238 return 0;
3239#endif
3240}
3241
Michael Grand5047f032022-11-24 16:49:56 +01003242fih_ret
Mark Horvathccaf7f82021-01-04 18:16:42 +01003243context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
3244{
Mark Horvathccaf7f82021-01-04 18:16:42 +01003245 int rc;
Michael Grand5047f032022-11-24 16:49:56 +01003246 FIH_DECLARE(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003247
Raef Colesfe57e7d2021-10-15 11:07:09 +01003248 rc = boot_get_slot_usage(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003249 if (rc != 0) {
David Vinczee574f2d2020-07-10 11:42:03 +02003250 goto out;
3251 }
3252
Mark Horvathccaf7f82021-01-04 18:16:42 +01003253#if (BOOT_IMAGE_NUMBER > 1)
3254 while (true) {
3255#endif
Raef Colesfe57e7d2021-10-15 11:07:09 +01003256 FIH_CALL(boot_load_and_validate_images, fih_rc, state);
Michael Grand5047f032022-11-24 16:49:56 +01003257 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
3258 FIH_SET(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003259 goto out;
3260 }
3261
3262#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesfe57e7d2021-10-15 11:07:09 +01003263 rc = boot_verify_dependencies(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003264 if (rc != 0) {
3265 /* Dependency check failed for an image, it has been removed from
3266 * SRAM in case of MCUBOOT_RAM_LOAD strategy, and set to
3267 * unavailable. Try to load an image from another slot.
3268 */
3269 continue;
3270 }
3271 /* Dependency check was successful. */
3272 break;
3273 }
3274#endif
3275
3276 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01003277#if BOOT_IMAGE_NUMBER > 1
3278 if (state->img_mask[BOOT_CURR_IMG(state)]) {
3279 continue;
3280 }
3281#endif
Raef Colesfe57e7d2021-10-15 11:07:09 +01003282 rc = boot_update_hw_rollback_protection(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003283 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01003284 FIH_SET(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003285 goto out;
3286 }
3287
Jamie McCrae3016d002023-03-14 12:35:51 +00003288 rc = boot_add_shared_data(state, (uint8_t)state->slot_usage[BOOT_CURR_IMG(state)].active_slot);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003289 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01003290 FIH_SET(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003291 goto out;
3292 }
3293 }
3294
3295 /* All image loaded successfully. */
3296#ifdef MCUBOOT_HAVE_LOGGING
Raef Colesfe57e7d2021-10-15 11:07:09 +01003297 print_loaded_images(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003298#endif
3299
Raef Colesfe57e7d2021-10-15 11:07:09 +01003300 fill_rsp(state, rsp);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003301
David Vinczee574f2d2020-07-10 11:42:03 +02003302out:
Mark Horvathccaf7f82021-01-04 18:16:42 +01003303 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01003304
Michael Grand5047f032022-11-24 16:49:56 +01003305 if (rc != 0) {
3306 FIH_SET(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003307 }
Raef Colese8fe6cf2020-05-26 13:07:40 +01003308
Mark Horvathccaf7f82021-01-04 18:16:42 +01003309 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02003310}
Tamas Banfe031092020-09-10 17:32:39 +02003311#endif /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02003312
3313/**
Raef Colese8fe6cf2020-05-26 13:07:40 +01003314 * Prepares the booting process. This function moves images around in flash as
David Vinczee574f2d2020-07-10 11:42:03 +02003315 * appropriate, and tells you what address to boot from.
3316 *
3317 * @param rsp On success, indicates how booting should occur.
3318 *
Raef Colese8fe6cf2020-05-26 13:07:40 +01003319 * @return FIH_SUCCESS on success; nonzero on failure.
David Vinczee574f2d2020-07-10 11:42:03 +02003320 */
Michael Grand5047f032022-11-24 16:49:56 +01003321fih_ret
David Vinczee574f2d2020-07-10 11:42:03 +02003322boot_go(struct boot_rsp *rsp)
3323{
Michael Grand5047f032022-11-24 16:49:56 +01003324 FIH_DECLARE(fih_rc, FIH_FAILURE);
Raef Colesf11de642021-10-15 11:11:56 +01003325
3326 boot_state_clear(NULL);
3327
Raef Colese8fe6cf2020-05-26 13:07:40 +01003328 FIH_CALL(context_boot_go, fih_rc, &boot_data, rsp);
3329 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02003330}
Raef Colesf11de642021-10-15 11:11:56 +01003331
3332/**
3333 * Prepares the booting process, considering only a single image. This function
3334 * moves images around in flash as appropriate, and tells you what address to
3335 * boot from.
3336 *
3337 * @param rsp On success, indicates how booting should occur.
3338 *
3339 * @param image_id The image ID to prepare the boot process for.
3340 *
3341 * @return FIH_SUCCESS on success; nonzero on failure.
3342 */
Michael Grand5047f032022-11-24 16:49:56 +01003343fih_ret
Raef Colesf11de642021-10-15 11:11:56 +01003344boot_go_for_image_id(struct boot_rsp *rsp, uint32_t image_id)
3345{
Michael Grand5047f032022-11-24 16:49:56 +01003346 FIH_DECLARE(fih_rc, FIH_FAILURE);
Raef Colesf11de642021-10-15 11:11:56 +01003347
3348 if (image_id >= BOOT_IMAGE_NUMBER) {
3349 FIH_RET(FIH_FAILURE);
3350 }
3351
3352#if BOOT_IMAGE_NUMBER > 1
3353 memset(&boot_data.img_mask, 1, BOOT_IMAGE_NUMBER);
3354 boot_data.img_mask[image_id] = 0;
3355#endif
3356
3357 FIH_CALL(context_boot_go, fih_rc, &boot_data, rsp);
3358 FIH_RET(fih_rc);
3359}
3360
3361/**
3362 * Clears the boot state, so that previous operations have no effect on new
3363 * ones.
3364 *
3365 * @param state The state that should be cleared. If the value
3366 * is NULL, the default bootloader state will be
3367 * cleared.
3368 */
3369void boot_state_clear(struct boot_loader_state *state)
3370{
3371 if (state != NULL) {
3372 memset(state, 0, sizeof(struct boot_loader_state));
3373 } else {
3374 memset(&boot_data, 0, sizeof(struct boot_loader_state));
3375 }
3376}