blob: b49de9221c799b8f16769a4829d546c87711ec06 [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
Mark Horvathccaf7f82021-01-04 18:16:42 +01006 * Copyright (c) 2019-2021 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,
132 uint32_t active_slot)
133{
134#if defined(MCUBOOT_MEASURED_BOOT) || defined(MCUBOOT_DATA_SHARING)
135 int rc;
136
137#ifdef MCUBOOT_MEASURED_BOOT
138 rc = boot_save_boot_status(BOOT_CURR_IMG(state),
139 boot_img_hdr(state, active_slot),
140 BOOT_IMG_AREA(state, active_slot));
141 if (rc != 0) {
142 BOOT_LOG_ERR("Failed to add image data to shared area");
143 return rc;
144 }
145#endif /* MCUBOOT_MEASURED_BOOT */
146
147#ifdef MCUBOOT_DATA_SHARING
148 rc = boot_save_shared_data(boot_img_hdr(state, active_slot),
149 BOOT_IMG_AREA(state, active_slot));
150 if (rc != 0) {
151 BOOT_LOG_ERR("Failed to add data to shared memory area.");
152 return rc;
153 }
154#endif /* MCUBOOT_DATA_SHARING */
155
156 return 0;
157
158#else /* MCUBOOT_MEASURED_BOOT || MCUBOOT_DATA_SHARING */
159 (void) (state);
160 (void) (active_slot);
161
162 return 0;
163#endif
164}
165
166/**
167 * Fills rsp to indicate how booting should occur.
168 *
169 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +0100170 * @param rsp boot_rsp struct to fill.
171 */
172static void
Raef Colesfe57e7d2021-10-15 11:07:09 +0100173fill_rsp(struct boot_loader_state *state, struct boot_rsp *rsp)
Mark Horvathccaf7f82021-01-04 18:16:42 +0100174{
175 uint32_t active_slot;
176
177#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesf11de642021-10-15 11:11:56 +0100178 /* Always boot from the first enabled image. */
Mark Horvathccaf7f82021-01-04 18:16:42 +0100179 BOOT_CURR_IMG(state) = 0;
Raef Colesf11de642021-10-15 11:11:56 +0100180 IMAGES_ITER(BOOT_CURR_IMG(state)) {
181 if (!state->img_mask[BOOT_CURR_IMG(state)]) {
182 break;
183 }
184 }
INFINEON\DovhalA94360d52023-01-18 17:21:56 +0200185 /* At least one image must be active, otherwise skip the execution */
186 if (BOOT_CURR_IMG(state) >= BOOT_IMAGE_NUMBER) {
187 return;
188 }
Mark Horvathccaf7f82021-01-04 18:16:42 +0100189#endif
190
191#if defined(MCUBOOT_DIRECT_XIP) || defined(MCUBOOT_RAM_LOAD)
Raef Colesfe57e7d2021-10-15 11:07:09 +0100192 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +0100193#else
Mark Horvathccaf7f82021-01-04 18:16:42 +0100194 active_slot = BOOT_PRIMARY_SLOT;
195#endif
196
Dominik Ermel260ae092021-04-23 05:38:45 +0000197 rsp->br_flash_dev_id = flash_area_get_device_id(BOOT_IMG_AREA(state, active_slot));
Mark Horvathccaf7f82021-01-04 18:16:42 +0100198 rsp->br_image_off = boot_img_slot_off(state, active_slot);
199 rsp->br_hdr = boot_img_hdr(state, active_slot);
200}
201
202/**
203 * Closes all flash areas.
204 *
205 * @param state Boot loader status information.
206 */
207static void
208close_all_flash_areas(struct boot_loader_state *state)
209{
210 uint32_t slot;
211
212 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +0100213#if BOOT_IMAGE_NUMBER > 1
214 if (state->img_mask[BOOT_CURR_IMG(state)]) {
215 continue;
216 }
217#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +0100218#if MCUBOOT_SWAP_USING_SCRATCH
219 flash_area_close(BOOT_SCRATCH_AREA(state));
220#endif
221 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
222 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
223 }
224 }
225}
226
Tamas Banfe031092020-09-10 17:32:39 +0200227#if !defined(MCUBOOT_DIRECT_XIP)
David Brownf5b33d82017-09-01 10:58:27 -0600228/*
229 * Compute the total size of the given image. Includes the size of
230 * the TLVs.
231 */
Tamas Banfe031092020-09-10 17:32:39 +0200232#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600233static int
Fabio Utzigd638b172019-08-09 10:38:05 -0300234boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
David Brownf5b33d82017-09-01 10:58:27 -0600235{
236 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300237 struct image_tlv_info info;
Fabio Utzig233af7d2019-08-26 12:06:16 -0300238 uint32_t off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300239 uint32_t protect_tlv_size;
David Brownf5b33d82017-09-01 10:58:27 -0600240 int area_id;
241 int rc;
242
Fabio Utzig10ee6482019-08-01 12:04:52 -0300243#if (BOOT_IMAGE_NUMBER == 1)
244 (void)state;
245#endif
246
247 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
David Brownf5b33d82017-09-01 10:58:27 -0600248 rc = flash_area_open(area_id, &fap);
249 if (rc != 0) {
250 rc = BOOT_EFLASH;
251 goto done;
252 }
253
Fabio Utzig61fd8882019-09-14 20:00:20 -0300254 off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
255
256 if (flash_area_read(fap, off, &info, sizeof(info))) {
257 rc = BOOT_EFLASH;
David Brownf5b33d82017-09-01 10:58:27 -0600258 goto done;
259 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300260
Fabio Utzige52c08e2019-09-11 19:32:00 -0300261 protect_tlv_size = boot_img_hdr(state, slot)->ih_protect_tlv_size;
262 if (info.it_magic == IMAGE_TLV_PROT_INFO_MAGIC) {
263 if (protect_tlv_size != info.it_tlv_tot) {
264 rc = BOOT_EBADIMAGE;
265 goto done;
266 }
267
268 if (flash_area_read(fap, off + info.it_tlv_tot, &info, sizeof(info))) {
269 rc = BOOT_EFLASH;
270 goto done;
271 }
272 } else if (protect_tlv_size != 0) {
273 rc = BOOT_EBADIMAGE;
274 goto done;
275 }
276
Fabio Utzig61fd8882019-09-14 20:00:20 -0300277 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
278 rc = BOOT_EBADIMAGE;
279 goto done;
280 }
281
Fabio Utzige52c08e2019-09-11 19:32:00 -0300282 *size = off + protect_tlv_size + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600283 rc = 0;
284
285done:
286 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300287 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600288}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300289#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600290
Tamas Banfe031092020-09-10 17:32:39 +0200291#if !defined(MCUBOOT_RAM_LOAD)
David Brownab449182019-11-15 09:32:52 -0700292static uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300293boot_write_sz(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800294{
David Brownab449182019-11-15 09:32:52 -0700295 uint32_t elem_sz;
Fabio Utzig12d59162019-11-28 10:01:59 -0300296#if MCUBOOT_SWAP_USING_SCRATCH
David Brownab449182019-11-15 09:32:52 -0700297 uint32_t align;
Fabio Utzig12d59162019-11-28 10:01:59 -0300298#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800299
300 /* Figure out what size to write update status update as. The size depends
301 * on what the minimum write size is for scratch area, active image slot.
302 * We need to use the bigger of those 2 values.
303 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300304 elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
Fabio Utzig12d59162019-11-28 10:01:59 -0300305#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300306 align = flash_area_align(BOOT_SCRATCH_AREA(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800307 if (align > elem_sz) {
308 elem_sz = align;
309 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300310#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800311
312 return elem_sz;
313}
314
Fabio Utzig10ee6482019-08-01 12:04:52 -0300315static int
316boot_initialize_area(struct boot_loader_state *state, int flash_area)
317{
Dominik Ermel51c8d762021-05-24 15:34:01 +0000318 uint32_t num_sectors = BOOT_MAX_IMG_SECTORS;
319 boot_sector_t *out_sectors;
320 uint32_t *out_num_sectors;
Fabio Utzig10ee6482019-08-01 12:04:52 -0300321 int rc;
322
323 num_sectors = BOOT_MAX_IMG_SECTORS;
324
325 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
326 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
327 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
328 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
329 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
330 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300331#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300332 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
333 out_sectors = state->scratch.sectors;
334 out_num_sectors = &state->scratch.num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300335#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -0300336 } else {
337 return BOOT_EFLASH;
338 }
339
Dominik Ermel51c8d762021-05-24 15:34:01 +0000340#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
Fabio Utzig10ee6482019-08-01 12:04:52 -0300341 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
Dominik Ermel51c8d762021-05-24 15:34:01 +0000342#else
343 _Static_assert(sizeof(int) <= sizeof(uint32_t), "Fix needed");
344 rc = flash_area_to_sectors(flash_area, (int *)&num_sectors, out_sectors);
345#endif /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300346 if (rc != 0) {
347 return rc;
348 }
349 *out_num_sectors = num_sectors;
350 return 0;
351}
Fabio Utzig10ee6482019-08-01 12:04:52 -0300352
Christopher Collins92ea77f2016-12-12 15:59:26 -0800353/**
354 * Determines the sector layout of both image slots and the scratch area.
355 * This information is necessary for calculating the number of bytes to erase
356 * and copy during an image swap. The information collected during this
Fabio Utzig10ee6482019-08-01 12:04:52 -0300357 * function is used to populate the state.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800358 */
359static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300360boot_read_sectors(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800361{
Fabio Utzigb0f04732019-07-31 09:49:19 -0300362 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800363 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800364
Fabio Utzig10ee6482019-08-01 12:04:52 -0300365 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300366
367 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800368 if (rc != 0) {
369 return BOOT_EFLASH;
370 }
371
Fabio Utzig10ee6482019-08-01 12:04:52 -0300372 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800373 if (rc != 0) {
Andrzej Puzdrowski54b4ad92021-06-22 13:21:22 +0200374 /* We need to differentiate from the primary image issue */
375 return BOOT_EFLASH_SEC;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800376 }
377
Fabio Utzig12d59162019-11-28 10:01:59 -0300378#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300379 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200380 if (rc != 0) {
381 return BOOT_EFLASH;
382 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300383#endif
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200384
Fabio Utzig10ee6482019-08-01 12:04:52 -0300385 BOOT_WRITE_SZ(state) = boot_write_sz(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800386
387 return 0;
388}
389
Fabio Utzig12d59162019-11-28 10:01:59 -0300390void
391boot_status_reset(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800392{
Fabio Utzig4741c452019-12-19 15:32:41 -0300393#ifdef MCUBOOT_ENC_IMAGES
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000394 memset(&bs->enckey, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_KEY_ALIGN_SIZE);
Fabio Utzig4741c452019-12-19 15:32:41 -0300395#if MCUBOOT_SWAP_SAVE_ENCTLV
396 memset(&bs->enctlv, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_TLV_ALIGN_SIZE);
397#endif
398#endif /* MCUBOOT_ENC_IMAGES */
399
400 bs->use_scratch = 0;
401 bs->swap_size = 0;
402 bs->source = 0;
403
Fabio Utzig74aef312019-11-28 11:05:34 -0300404 bs->op = BOOT_STATUS_OP_MOVE;
Fabio Utzig39000012018-07-30 12:40:20 -0300405 bs->idx = BOOT_STATUS_IDX_0;
406 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700407 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Fabio Utzig12d59162019-11-28 10:01:59 -0300408}
Christopher Collins92ea77f2016-12-12 15:59:26 -0800409
Fabio Utzig12d59162019-11-28 10:01:59 -0300410bool
411boot_status_is_reset(const struct boot_status *bs)
412{
Fabio Utzig74aef312019-11-28 11:05:34 -0300413 return (bs->op == BOOT_STATUS_OP_MOVE &&
414 bs->idx == BOOT_STATUS_IDX_0 &&
415 bs->state == BOOT_STATUS_STATE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800416}
417
418/**
419 * Writes the supplied boot status to the flash file system. The boot status
420 * contains the current state of an in-progress image copy operation.
421 *
422 * @param bs The boot status to write.
423 *
424 * @return 0 on success; nonzero on failure.
425 */
426int
Fabio Utzig12d59162019-11-28 10:01:59 -0300427boot_write_status(const struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800428{
429 const struct flash_area *fap;
430 uint32_t off;
431 int area_id;
Dominik Ermel9479af02021-10-19 10:06:44 +0000432 int rc = 0;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300433 uint8_t buf[BOOT_MAX_ALIGN];
Gustavo Henrique Nihei4aa286d2021-11-24 14:54:56 -0300434 uint32_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300435 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800436
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300437 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100438 * the trailer. Since in the last step the primary slot is erased, the
439 * first two status writes go to the scratch which will be copied to
440 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300441 */
442
Fabio Utzig12d59162019-11-28 10:01:59 -0300443#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig2473ac02017-05-02 12:45:02 -0300444 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800445 /* Write to scratch. */
446 area_id = FLASH_AREA_IMAGE_SCRATCH;
447 } else {
Fabio Utzig12d59162019-11-28 10:01:59 -0300448#endif
David Vincze2d736ad2019-02-18 11:50:22 +0100449 /* Write to the primary slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300450 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Fabio Utzig12d59162019-11-28 10:01:59 -0300451#if MCUBOOT_SWAP_USING_SCRATCH
Christopher Collins92ea77f2016-12-12 15:59:26 -0800452 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300453#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800454
455 rc = flash_area_open(area_id, &fap);
456 if (rc != 0) {
Dominik Ermel9479af02021-10-19 10:06:44 +0000457 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800458 }
459
460 off = boot_status_off(fap) +
Fabio Utzig12d59162019-11-28 10:01:59 -0300461 boot_status_internal_off(bs, BOOT_WRITE_SZ(state));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200462 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300463 erased_val = flash_area_erased_val(fap);
464 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700465 buf[0] = bs->state;
466
467 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800468 if (rc != 0) {
469 rc = BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800470 }
471
Christopher Collins92ea77f2016-12-12 15:59:26 -0800472 flash_area_close(fap);
Dominik Ermel9479af02021-10-19 10:06:44 +0000473
Christopher Collins92ea77f2016-12-12 15:59:26 -0800474 return rc;
475}
Tamas Banfe031092020-09-10 17:32:39 +0200476#endif /* !MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +0200477#endif /* !MCUBOOT_DIRECT_XIP */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800478
479/*
David Vinczec3084132020-02-18 14:50:47 +0100480 * Validate image hash/signature and optionally the security counter in a slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800481 */
Michael Grand5047f032022-11-24 16:49:56 +0100482static fih_ret
Fabio Utzig10ee6482019-08-01 12:04:52 -0300483boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
484 const struct flash_area *fap, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800485{
Fabio Utzig10ee6482019-08-01 12:04:52 -0300486 TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigb0f04732019-07-31 09:49:19 -0300487 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300488 int rc;
Michael Grand5047f032022-11-24 16:49:56 +0100489 FIH_DECLARE(fih_rc, FIH_FAILURE);
Fabio Utzigba829042018-09-18 08:29:34 -0300490
Fabio Utzig10ee6482019-08-01 12:04:52 -0300491#if (BOOT_IMAGE_NUMBER == 1)
492 (void)state;
493#endif
494
Fabio Utzigba829042018-09-18 08:29:34 -0300495 (void)bs;
496 (void)rc;
Fabio Utzigbc077932019-08-26 11:16:34 -0300497
498 image_index = BOOT_CURR_IMG(state);
499
Hugo L'Hostisdb543e52021-03-09 18:00:31 +0000500/* In the case of ram loading the image has already been decrypted as it is
501 * decrypted when copied in ram */
502#if defined(MCUBOOT_ENC_IMAGES) && !defined(MCUBOOT_RAM_LOAD)
Fabio Utzigbc077932019-08-26 11:16:34 -0300503 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzig4741c452019-12-19 15:32:41 -0300504 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300505 if (rc < 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100506 FIH_RET(fih_rc);
Fabio Utzigba829042018-09-18 08:29:34 -0300507 }
Fabio Utzig4741c452019-12-19 15:32:41 -0300508 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100509 FIH_RET(fih_rc);
Fabio Utzigba829042018-09-18 08:29:34 -0300510 }
511 }
Fabio Utzigbc077932019-08-26 11:16:34 -0300512#endif
513
Raef Colese8fe6cf2020-05-26 13:07:40 +0100514 FIH_CALL(bootutil_img_validate, fih_rc, BOOT_CURR_ENC(state), image_index,
515 hdr, fap, tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, NULL);
Fabio Utzig10ee6482019-08-01 12:04:52 -0300516
Raef Colese8fe6cf2020-05-26 13:07:40 +0100517 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800518}
519
Tamas Banfe031092020-09-10 17:32:39 +0200520#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
Michael Grand5047f032022-11-24 16:49:56 +0100521static fih_ret
Christopher Collins92ea77f2016-12-12 15:59:26 -0800522split_image_check(struct image_header *app_hdr,
523 const struct flash_area *app_fap,
524 struct image_header *loader_hdr,
525 const struct flash_area *loader_fap)
526{
527 static void *tmpbuf;
528 uint8_t loader_hash[32];
Michael Grand5047f032022-11-24 16:49:56 +0100529 FIH_DECLARE(fih_rc, FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800530
531 if (!tmpbuf) {
532 tmpbuf = malloc(BOOT_TMPBUF_SZ);
533 if (!tmpbuf) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100534 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800535 }
536 }
537
Raef Colese8fe6cf2020-05-26 13:07:40 +0100538 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, loader_hdr, loader_fap,
539 tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, loader_hash);
Michael Grand5047f032022-11-24 16:49:56 +0100540 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100541 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800542 }
543
Raef Colese8fe6cf2020-05-26 13:07:40 +0100544 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, app_hdr, app_fap,
545 tmpbuf, BOOT_TMPBUF_SZ, loader_hash, 32, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800546
Raef Colese8fe6cf2020-05-26 13:07:40 +0100547out:
548 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800549}
Tamas Banfe031092020-09-10 17:32:39 +0200550#endif /* !MCUBOOT_DIRECT_XIP && !MCUBOOT_RAM_LOAD */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800551
Fabio Utzig338a19f2018-12-03 08:37:08 -0200552/*
David Brown9bf95af2019-10-10 15:36:36 -0600553 * Check that this is a valid header. Valid means that the magic is
554 * correct, and that the sizes/offsets are "sane". Sane means that
555 * there is no overflow on the arithmetic, and that the result fits
556 * within the flash area we are in.
557 */
558static bool
559boot_is_header_valid(const struct image_header *hdr, const struct flash_area *fap)
560{
561 uint32_t size;
562
563 if (hdr->ih_magic != IMAGE_MAGIC) {
564 return false;
565 }
566
567 if (!boot_u32_safe_add(&size, hdr->ih_img_size, hdr->ih_hdr_size)) {
568 return false;
569 }
570
Dominik Ermel260ae092021-04-23 05:38:45 +0000571 if (size >= flash_area_get_size(fap)) {
David Brown9bf95af2019-10-10 15:36:36 -0600572 return false;
573 }
574
575 return true;
576}
577
578/*
Fabio Utzig338a19f2018-12-03 08:37:08 -0200579 * Check that a memory area consists of a given value.
580 */
581static inline bool
582boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300583{
584 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200585 uint8_t *p = (uint8_t *)data;
586 for (i = 0; i < len; i++) {
587 if (val != p[i]) {
588 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300589 }
590 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200591 return true;
592}
593
594static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300595boot_check_header_erased(struct boot_loader_state *state, int slot)
Fabio Utzig338a19f2018-12-03 08:37:08 -0200596{
597 const struct flash_area *fap;
598 struct image_header *hdr;
599 uint8_t erased_val;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300600 int area_id;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200601 int rc;
602
Fabio Utzig10ee6482019-08-01 12:04:52 -0300603 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300604 rc = flash_area_open(area_id, &fap);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200605 if (rc != 0) {
606 return -1;
607 }
608
609 erased_val = flash_area_erased_val(fap);
610 flash_area_close(fap);
611
Fabio Utzig10ee6482019-08-01 12:04:52 -0300612 hdr = boot_img_hdr(state, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200613 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
614 return -1;
615 }
616
617 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300618}
619
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000620#if (BOOT_IMAGE_NUMBER > 1) || \
David Vinczee574f2d2020-07-10 11:42:03 +0200621 defined(MCUBOOT_DIRECT_XIP) || \
Tamas Banfe031092020-09-10 17:32:39 +0200622 defined(MCUBOOT_RAM_LOAD) || \
Jerzy Kasenberge3f895d2022-04-12 15:05:33 +0200623 defined(MCUBOOT_DOWNGRADE_PREVENTION)
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000624/**
David Vincze8b0b6372020-05-20 19:54:44 +0200625 * Compare image version numbers not including the build number
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000626 *
David Vincze8b0b6372020-05-20 19:54:44 +0200627 * @param ver1 Pointer to the first image version to compare.
628 * @param ver2 Pointer to the second image version to compare.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000629 *
David Vincze8b0b6372020-05-20 19:54:44 +0200630 * @retval -1 If ver1 is strictly less than ver2.
631 * @retval 0 If the image version numbers are equal,
632 * (not including the build number).
633 * @retval 1 If ver1 is strictly greater than ver2.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000634 */
635static int
David Vincze8b0b6372020-05-20 19:54:44 +0200636boot_version_cmp(const struct image_version *ver1,
637 const struct image_version *ver2)
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000638{
David Vincze8b0b6372020-05-20 19:54:44 +0200639 if (ver1->iv_major > ver2->iv_major) {
640 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000641 }
David Vincze8b0b6372020-05-20 19:54:44 +0200642 if (ver1->iv_major < ver2->iv_major) {
643 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000644 }
David Vincze8b0b6372020-05-20 19:54:44 +0200645 /* The major version numbers are equal, continue comparison. */
646 if (ver1->iv_minor > ver2->iv_minor) {
647 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000648 }
David Vincze8b0b6372020-05-20 19:54:44 +0200649 if (ver1->iv_minor < ver2->iv_minor) {
650 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000651 }
David Vincze8b0b6372020-05-20 19:54:44 +0200652 /* The minor version numbers are equal, continue comparison. */
653 if (ver1->iv_revision > ver2->iv_revision) {
654 return 1;
655 }
656 if (ver1->iv_revision < ver2->iv_revision) {
657 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000658 }
659
660 return 0;
661}
662#endif
663
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000664#if defined(MCUBOOT_DIRECT_XIP)
665/**
666 * Check if image in slot has been set with specific ROM address to run from
667 * and whether the slot starts at that address.
668 *
669 * @returns 0 if IMAGE_F_ROM_FIXED flag is not set;
670 * 0 if IMAGE_F_ROM_FIXED flag is set and ROM address specified in
671 * header matches the slot address;
672 * 1 if IMF_F_ROM_FIXED flag is set but ROM address specified in header
673 * does not match the slot address.
674 */
675static bool
Raef Colesfe57e7d2021-10-15 11:07:09 +0100676boot_rom_address_check(struct boot_loader_state *state)
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000677{
Mark Horvathccaf7f82021-01-04 18:16:42 +0100678 uint32_t active_slot;
679 const struct image_header *hdr;
680 uint32_t f_off;
681
Raef Colesfe57e7d2021-10-15 11:07:09 +0100682 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +0100683 hdr = boot_img_hdr(state, active_slot);
684 f_off = boot_img_slot_off(state, active_slot);
685
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000686 if (hdr->ih_flags & IMAGE_F_ROM_FIXED && hdr->ih_load_addr != f_off) {
687 BOOT_LOG_WRN("Image in %s slot at 0x%x has been built for offset 0x%x"\
Mark Horvathccaf7f82021-01-04 18:16:42 +0100688 ", skipping",
689 active_slot == 0 ? "primary" : "secondary", f_off,
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000690 hdr->ih_load_addr);
691
692 /* If there is address mismatch, the image is not bootable from this
693 * slot.
694 */
695 return 1;
696 }
697 return 0;
698}
699#endif
700
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300701/*
702 * Check that there is a valid image in a slot
703 *
704 * @returns
Raef Colese8fe6cf2020-05-26 13:07:40 +0100705 * FIH_SUCCESS if image was successfully validated
Michael Grand5047f032022-11-24 16:49:56 +0100706 * FIH_NO_BOOTABLE_IMAGE if no bootloable image was found
Raef Colese8fe6cf2020-05-26 13:07:40 +0100707 * FIH_FAILURE on any errors
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300708 */
Michael Grand5047f032022-11-24 16:49:56 +0100709static fih_ret
Fabio Utzig10ee6482019-08-01 12:04:52 -0300710boot_validate_slot(struct boot_loader_state *state, int slot,
711 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800712{
713 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400714 struct image_header *hdr;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300715 int area_id;
Michael Grand5047f032022-11-24 16:49:56 +0100716 FIH_DECLARE(fih_rc, FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800717 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300718
Fabio Utzig10ee6482019-08-01 12:04:52 -0300719 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300720 rc = flash_area_open(area_id, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800721 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100722 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800723 }
724
Fabio Utzig10ee6482019-08-01 12:04:52 -0300725 hdr = boot_img_hdr(state, slot);
726 if (boot_check_header_erased(state, slot) == 0 ||
727 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
Fabio Utzig260ec452020-07-09 18:40:07 -0300728
729#if defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE)
730 /*
731 * This fixes an issue where an image might be erased, but a trailer
732 * be left behind. It can happen if the image is in the secondary slot
733 * and did not pass validation, in which case the whole slot is erased.
734 * If during the erase operation, a reset occurs, parts of the slot
735 * might have been erased while some did not. The concerning part is
736 * the trailer because it might disable a new image from being loaded
737 * through mcumgr; so we just get rid of the trailer here, if the header
738 * is erased.
739 */
740 if (slot != BOOT_PRIMARY_SLOT) {
741 swap_erase_trailer_sectors(state, fap);
742 }
743#endif
744
David Vincze2d736ad2019-02-18 11:50:22 +0100745 /* No bootable image in slot; continue booting from the primary slot. */
Michael Grand5047f032022-11-24 16:49:56 +0100746 fih_rc = FIH_NO_BOOTABLE_IMAGE;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200747 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300748 }
749
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000750#if defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION)
751 if (slot != BOOT_PRIMARY_SLOT) {
752 /* Check if version of secondary slot is sufficient */
David Vincze8b0b6372020-05-20 19:54:44 +0200753 rc = boot_version_cmp(
754 &boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
755 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
756 if (rc < 0 && boot_check_header_erased(state, BOOT_PRIMARY_SLOT)) {
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000757 BOOT_LOG_ERR("insufficient version in secondary slot");
Dominik Ermel260ae092021-04-23 05:38:45 +0000758 flash_area_erase(fap, 0, flash_area_get_size(fap));
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000759 /* Image in the secondary slot does not satisfy version requirement.
760 * Erase the image and continue booting from the primary slot.
761 */
Michael Grand5047f032022-11-24 16:49:56 +0100762 fih_rc = FIH_NO_BOOTABLE_IMAGE;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000763 goto out;
764 }
765 }
766#endif
Michael Grand5047f032022-11-24 16:49:56 +0100767 BOOT_HOOK_CALL_FIH(boot_image_check_hook, FIH_BOOT_HOOK_REGULAR,
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +0200768 fih_rc, BOOT_CURR_IMG(state), slot);
Michael Grand5047f032022-11-24 16:49:56 +0100769 if (FIH_EQ(fih_rc, FIH_BOOT_HOOK_REGULAR))
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +0200770 {
771 FIH_CALL(boot_image_check, fih_rc, state, hdr, fap, bs);
772 }
Michael Grand5047f032022-11-24 16:49:56 +0100773 if (!boot_is_header_valid(hdr, fap) || FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Tamas Banfe031092020-09-10 17:32:39 +0200774 if ((slot != BOOT_PRIMARY_SLOT) || ARE_SLOTS_EQUIVALENT()) {
Dominik Ermel260ae092021-04-23 05:38:45 +0000775 flash_area_erase(fap, 0, flash_area_get_size(fap));
David Vinczee574f2d2020-07-10 11:42:03 +0200776 /* Image is invalid, erase it to prevent further unnecessary
777 * attempts to validate and boot it.
David Brownb38e0442017-02-24 13:57:12 -0700778 */
779 }
David Brown098de832019-12-10 11:58:01 -0700780#if !defined(__BOOTSIM__)
David Vincze2d736ad2019-02-18 11:50:22 +0100781 BOOT_LOG_ERR("Image in the %s slot is not valid!",
782 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Brown098de832019-12-10 11:58:01 -0700783#endif
Michael Grand5047f032022-11-24 16:49:56 +0100784 fih_rc = FIH_NO_BOOTABLE_IMAGE;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200785 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800786 }
787
Håkon Øye Amundsene829e9d2021-11-12 14:01:01 +0000788#if MCUBOOT_IMAGE_NUMBER > 1 && !defined(MCUBOOT_ENC_IMAGES) && defined(MCUBOOT_VERIFY_IMG_ADDRESS)
789 /* Verify that the image in the secondary slot has a reset address
790 * located in the primary slot. This is done to avoid users incorrectly
791 * overwriting an application written to the incorrect slot.
792 * This feature is only supported by ARM platforms.
793 */
794 if (area_id == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
795 const struct flash_area *pri_fa = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
796 struct image_header *secondary_hdr = boot_img_hdr(state, slot);
797 uint32_t reset_value = 0;
798 uint32_t reset_addr = secondary_hdr->ih_hdr_size + sizeof(reset_value);
799
800 rc = flash_area_read(fap, reset_addr, &reset_value, sizeof(reset_value));
801 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +0100802 fih_rc = FIH_NO_BOOTABLE_IMAGE;
Håkon Øye Amundsene829e9d2021-11-12 14:01:01 +0000803 goto out;
804 }
805
806 if (reset_value < pri_fa->fa_off || reset_value> (pri_fa->fa_off + pri_fa->fa_size)) {
807 BOOT_LOG_ERR("Reset address of image in secondary slot is not in the primary slot");
808 BOOT_LOG_ERR("Erasing image from secondary slot");
809
810 /* The vector table in the image located in the secondary
811 * slot does not target the primary slot. This might
812 * indicate that the image was loaded to the wrong slot.
813 *
814 * Erase the image and continue booting from the primary slot.
815 */
816 flash_area_erase(fap, 0, fap->fa_size);
Michael Grand5047f032022-11-24 16:49:56 +0100817 fih_rc = FIH_NO_BOOTABLE_IMAGE;
Håkon Øye Amundsene829e9d2021-11-12 14:01:01 +0000818 goto out;
819 }
820 }
821#endif
822
Fabio Utzig338a19f2018-12-03 08:37:08 -0200823out:
824 flash_area_close(fap);
Raef Colese8fe6cf2020-05-26 13:07:40 +0100825
826 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800827}
828
David Vinczec3084132020-02-18 14:50:47 +0100829#ifdef MCUBOOT_HW_ROLLBACK_PROT
830/**
831 * Updates the stored security counter value with the image's security counter
832 * value which resides in the given slot, only if it's greater than the stored
833 * value.
834 *
835 * @param image_index Index of the image to determine which security
836 * counter to update.
837 * @param slot Slot number of the image.
838 * @param hdr Pointer to the image header structure of the image
839 * that is currently stored in the given slot.
840 *
841 * @return 0 on success; nonzero on failure.
842 */
843static int
844boot_update_security_counter(uint8_t image_index, int slot,
845 struct image_header *hdr)
846{
847 const struct flash_area *fap = NULL;
848 uint32_t img_security_cnt;
849 int rc;
850
851 rc = flash_area_open(flash_area_id_from_multi_image_slot(image_index, slot),
852 &fap);
853 if (rc != 0) {
854 rc = BOOT_EFLASH;
855 goto done;
856 }
857
858 rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt);
859 if (rc != 0) {
860 goto done;
861 }
862
863 rc = boot_nv_security_counter_update(image_index, img_security_cnt);
864 if (rc != 0) {
865 goto done;
866 }
867
868done:
869 flash_area_close(fap);
870 return rc;
871}
872#endif /* MCUBOOT_HW_ROLLBACK_PROT */
873
Tamas Banfe031092020-09-10 17:32:39 +0200874#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
David Vinczee574f2d2020-07-10 11:42:03 +0200875/**
876 * Determines which swap operation to perform, if any. If it is determined
877 * that a swap operation is required, the image in the secondary slot is checked
878 * for validity. If the image in the secondary slot is invalid, it is erased,
879 * and a swap type of "none" is indicated.
880 *
881 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
882 */
883static int
884boot_validated_swap_type(struct boot_loader_state *state,
885 struct boot_status *bs)
886{
887 int swap_type;
Michael Grand5047f032022-11-24 16:49:56 +0100888 FIH_DECLARE(fih_rc, FIH_FAILURE);
David Vinczee574f2d2020-07-10 11:42:03 +0200889
890 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
891 if (BOOT_IS_UPGRADE(swap_type)) {
892 /* Boot loader wants to switch to the secondary slot.
893 * Ensure image is valid.
894 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100895 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_SECONDARY_SLOT, bs);
Michael Grand5047f032022-11-24 16:49:56 +0100896 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
897 if (FIH_EQ(fih_rc, FIH_NO_BOOTABLE_IMAGE)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100898 swap_type = BOOT_SWAP_TYPE_NONE;
899 } else {
900 swap_type = BOOT_SWAP_TYPE_FAIL;
901 }
David Vinczee574f2d2020-07-10 11:42:03 +0200902 }
903 }
904
905 return swap_type;
906}
David Brown94ed12c2021-05-26 16:28:14 -0600907#endif
David Vinczee574f2d2020-07-10 11:42:03 +0200908
Christopher Collins92ea77f2016-12-12 15:59:26 -0800909/**
Christopher Collins92ea77f2016-12-12 15:59:26 -0800910 * Erases a region of flash.
911 *
Fabio Utzigba829042018-09-18 08:29:34 -0300912 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800913 * @param off The offset within the flash area to start the
914 * erase.
915 * @param sz The number of bytes to erase.
916 *
917 * @return 0 on success; nonzero on failure.
918 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300919int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300920boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800921{
Fabio Utzigba829042018-09-18 08:29:34 -0300922 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800923}
924
David Brown94ed12c2021-05-26 16:28:14 -0600925#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800926/**
927 * Copies the contents of one flash region to another. You must erase the
928 * destination region prior to calling this function.
929 *
930 * @param flash_area_id_src The ID of the source flash area.
931 * @param flash_area_id_dst The ID of the destination flash area.
932 * @param off_src The offset within the source flash area to
933 * copy from.
934 * @param off_dst The offset within the destination flash area to
935 * copy to.
936 * @param sz The number of bytes to copy.
937 *
938 * @return 0 on success; nonzero on failure.
939 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300940int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300941boot_copy_region(struct boot_loader_state *state,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300942 const struct flash_area *fap_src,
Fabio Utzigba829042018-09-18 08:29:34 -0300943 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800944 uint32_t off_src, uint32_t off_dst, uint32_t sz)
945{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800946 uint32_t bytes_copied;
947 int chunk_sz;
948 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -0300949#ifdef MCUBOOT_ENC_IMAGES
950 uint32_t off;
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300951 uint32_t tlv_off;
Fabio Utzigba829042018-09-18 08:29:34 -0300952 size_t blk_off;
953 struct image_header *hdr;
954 uint16_t idx;
955 uint32_t blk_sz;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300956 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300957#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800958
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000959 TARGET_STATIC uint8_t buf[BUF_SZ] __attribute__((aligned(4)));
Fabio Utzig10ee6482019-08-01 12:04:52 -0300960
961#if !defined(MCUBOOT_ENC_IMAGES)
962 (void)state;
963#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800964
Christopher Collins92ea77f2016-12-12 15:59:26 -0800965 bytes_copied = 0;
966 while (bytes_copied < sz) {
967 if (sz - bytes_copied > sizeof buf) {
968 chunk_sz = sizeof buf;
969 } else {
970 chunk_sz = sz - bytes_copied;
971 }
972
973 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
974 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300975 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800976 }
977
Fabio Utzigba829042018-09-18 08:29:34 -0300978#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -0300979 image_index = BOOT_CURR_IMG(state);
Dominik Ermel260ae092021-04-23 05:38:45 +0000980 if ((flash_area_get_id(fap_src) == FLASH_AREA_IMAGE_SECONDARY(image_index) ||
981 flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) &&
982 !(flash_area_get_id(fap_src) == FLASH_AREA_IMAGE_SECONDARY(image_index) &&
983 flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index))) {
David Vincze2d736ad2019-02-18 11:50:22 +0100984 /* assume the secondary slot as src, needs decryption */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300985 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig74aef312019-11-28 11:05:34 -0300986#if !defined(MCUBOOT_SWAP_USING_MOVE)
Fabio Utzigba829042018-09-18 08:29:34 -0300987 off = off_src;
Dominik Ermel260ae092021-04-23 05:38:45 +0000988 if (flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100989 /* might need encryption (metadata from the primary slot) */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300990 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -0300991 off = off_dst;
992 }
Fabio Utzig74aef312019-11-28 11:05:34 -0300993#else
994 off = off_dst;
Dominik Ermel260ae092021-04-23 05:38:45 +0000995 if (flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300996 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
997 }
998#endif
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200999 if (IS_ENCRYPTED(hdr)) {
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001000 uint32_t abs_off = off + bytes_copied;
1001 if (abs_off < hdr->ih_hdr_size) {
Fabio Utzigba829042018-09-18 08:29:34 -03001002 /* do not decrypt header */
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001003 if (abs_off + chunk_sz > hdr->ih_hdr_size) {
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001004 /* The lower part of the chunk contains header data */
1005 blk_off = 0;
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001006 blk_sz = chunk_sz - (hdr->ih_hdr_size - abs_off);
1007 idx = hdr->ih_hdr_size - abs_off;
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001008 } else {
1009 /* The chunk contains exclusively header data */
1010 blk_sz = 0; /* nothing to decrypt */
1011 }
Fabio Utzigba829042018-09-18 08:29:34 -03001012 } else {
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001013 idx = 0;
1014 blk_sz = chunk_sz;
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001015 blk_off = (abs_off - hdr->ih_hdr_size) & 0xf;
Fabio Utzigba829042018-09-18 08:29:34 -03001016 }
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001017
1018 if (blk_sz > 0)
1019 {
1020 tlv_off = BOOT_TLV_OFF(hdr);
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001021 if (abs_off + chunk_sz > tlv_off) {
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001022 /* do not decrypt TLVs */
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001023 if (abs_off >= tlv_off) {
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001024 blk_sz = 0;
1025 } else {
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001026 blk_sz = tlv_off - abs_off;
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001027 }
Fabio Utzigba829042018-09-18 08:29:34 -03001028 }
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001029 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001030 (abs_off + idx) - hdr->ih_hdr_size, blk_sz,
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001031 blk_off, &buf[idx]);
Fabio Utzigba829042018-09-18 08:29:34 -03001032 }
Fabio Utzigba829042018-09-18 08:29:34 -03001033 }
1034 }
1035#endif
1036
Christopher Collins92ea77f2016-12-12 15:59:26 -08001037 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
1038 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001039 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001040 }
1041
1042 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -03001043
1044 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -08001045 }
1046
Fabio Utzigba829042018-09-18 08:29:34 -03001047 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001048}
1049
Christopher Collins92ea77f2016-12-12 15:59:26 -08001050/**
David Vincze2d736ad2019-02-18 11:50:22 +01001051 * Overwrite primary slot with the image contained in the secondary slot.
1052 * If a prior copy operation was interrupted by a system reset, this function
1053 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001054 *
1055 * @param bs The current boot status. This function reads
1056 * this struct to determine if it is resuming
1057 * an interrupted swap operation. This
1058 * function writes the updated status to this
1059 * function on return.
1060 *
1061 * @return 0 on success; nonzero on failure.
1062 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001063#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001064static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001065boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
David Brown17609d82017-05-05 09:41:34 -06001066{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001067 size_t sect_count;
1068 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001069 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001070 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001071 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001072 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +01001073 const struct flash_area *fap_primary_slot;
1074 const struct flash_area *fap_secondary_slot;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001075 uint8_t image_index;
David Vincze2d736ad2019-02-18 11:50:22 +01001076
Fabio Utzigb4f88102020-10-04 10:16:24 -03001077#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1078 uint32_t sector;
1079 uint32_t trailer_sz;
1080 uint32_t off;
1081 uint32_t sz;
1082#endif
1083
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001084 (void)bs;
1085
Fabio Utzig13d9e352017-10-05 20:32:31 -03001086#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1087 uint32_t src_size = 0;
Fabio Utzigd638b172019-08-09 10:38:05 -03001088 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001089 assert(rc == 0);
1090#endif
David Brown17609d82017-05-05 09:41:34 -06001091
David Vincze2d736ad2019-02-18 11:50:22 +01001092 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1093 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -06001094
Fabio Utzigb0f04732019-07-31 09:49:19 -03001095 image_index = BOOT_CURR_IMG(state);
1096
1097 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1098 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001099 assert (rc == 0);
1100
Fabio Utzigb0f04732019-07-31 09:49:19 -03001101 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1102 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001103 assert (rc == 0);
1104
Fabio Utzig10ee6482019-08-01 12:04:52 -03001105 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001106 for (sect = 0, size = 0; sect < sect_count; sect++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001107 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001108 rc = boot_erase_region(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001109 assert(rc == 0);
1110
Fabio Utzig13d9e352017-10-05 20:32:31 -03001111#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
Fabio Utzigb4f88102020-10-04 10:16:24 -03001112 if ((size + this_size) >= src_size) {
1113 size += src_size - size;
1114 size += BOOT_WRITE_SZ(state) - (size % BOOT_WRITE_SZ(state));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001115 break;
1116 }
1117#endif
Fabio Utzigb4f88102020-10-04 10:16:24 -03001118
1119 size += this_size;
David Brown17609d82017-05-05 09:41:34 -06001120 }
1121
Fabio Utzigb4f88102020-10-04 10:16:24 -03001122#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1123 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
1124 sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
1125 sz = 0;
1126 do {
1127 sz += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sector);
1128 off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, sector);
1129 sector--;
1130 } while (sz < trailer_sz);
1131
1132 rc = boot_erase_region(fap_primary_slot, off, sz);
1133 assert(rc == 0);
1134#endif
1135
Fabio Utzigba829042018-09-18 08:29:34 -03001136#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001137 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001138 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001139 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
Fabio Utzig4741c452019-12-19 15:32:41 -03001140 fap_secondary_slot, bs);
David Vincze2d736ad2019-02-18 11:50:22 +01001141
Fabio Utzigba829042018-09-18 08:29:34 -03001142 if (rc < 0) {
1143 return BOOT_EBADIMAGE;
1144 }
Fabio Utzig4741c452019-12-19 15:32:41 -03001145 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001146 return BOOT_EBADIMAGE;
1147 }
1148 }
1149#endif
1150
David Vincze2d736ad2019-02-18 11:50:22 +01001151 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1152 size);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001153 rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot, 0, 0, size);
Fabio Utzigb4f88102020-10-04 10:16:24 -03001154 if (rc != 0) {
1155 return rc;
1156 }
1157
1158#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1159 rc = boot_write_magic(fap_primary_slot);
1160 if (rc != 0) {
1161 return rc;
1162 }
1163#endif
David Brown17609d82017-05-05 09:41:34 -06001164
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +02001165 rc = BOOT_HOOK_CALL(boot_copy_region_post_hook, 0, BOOT_CURR_IMG(state),
1166 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT), size);
1167 if (rc != 0) {
1168 return rc;
1169 }
1170
David Vinczec3084132020-02-18 14:50:47 +01001171#ifdef MCUBOOT_HW_ROLLBACK_PROT
1172 /* Update the stored security counter with the new image's security counter
1173 * value. Both slots hold the new image at this point, but the secondary
1174 * slot's image header must be passed since the image headers in the
1175 * boot_data structure have not been updated yet.
1176 */
1177 rc = boot_update_security_counter(BOOT_CURR_IMG(state), BOOT_PRIMARY_SLOT,
1178 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1179 if (rc != 0) {
1180 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
1181 return rc;
1182 }
1183#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1184
Fabio Utzig13d9e352017-10-05 20:32:31 -03001185 /*
1186 * Erases header and trailer. The trailer is erased because when a new
1187 * image is written without a trailer as is the case when using newt, the
1188 * trailer that was left might trigger a new upgrade.
1189 */
Christopher Collins2c88e692019-05-22 15:10:14 -07001190 BOOT_LOG_DBG("erasing secondary header");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001191 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001192 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
1193 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001194 assert(rc == 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001195 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -07001196 BOOT_LOG_DBG("erasing secondary trailer");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001197 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001198 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
1199 last_sector),
1200 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
1201 last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001202 assert(rc == 0);
1203
David Vincze2d736ad2019-02-18 11:50:22 +01001204 flash_area_close(fap_primary_slot);
1205 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001206
David Vincze2d736ad2019-02-18 11:50:22 +01001207 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001208
1209 return 0;
1210}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001211#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001212
Christopher Collinsa1c12042019-05-23 14:00:28 -07001213#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -03001214/**
1215 * Swaps the two images in flash. If a prior copy operation was interrupted
1216 * by a system reset, this function completes that operation.
1217 *
1218 * @param bs The current boot status. This function reads
1219 * this struct to determine if it is resuming
1220 * an interrupted swap operation. This
1221 * function writes the updated status to this
1222 * function on return.
1223 *
1224 * @return 0 on success; nonzero on failure.
1225 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001226static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001227boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001228{
Fabio Utzig2473ac02017-05-02 12:45:02 -03001229 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001230 const struct flash_area *fap;
Dominik Ermel472d4c72023-02-10 13:29:58 +00001231#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzigba829042018-09-18 08:29:34 -03001232 uint8_t slot;
1233 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001234#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001235 uint32_t size;
1236 uint32_t copy_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001237 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001238 int rc;
1239
1240 /* FIXME: just do this if asked by user? */
1241
1242 size = copy_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001243 image_index = BOOT_CURR_IMG(state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001244
Fabio Utzig12d59162019-11-28 10:01:59 -03001245 if (boot_status_is_reset(bs)) {
Fabio Utzig46490722017-09-04 15:34:32 -03001246 /*
1247 * No swap ever happened, so need to find the largest image which
1248 * will be used to determine the amount of sectors to swap.
1249 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001250 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001251 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001252 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001253 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001254 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001255
Fabio Utzigba829042018-09-18 08:29:34 -03001256#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001257 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001258 fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001259 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001260 assert(rc >= 0);
1261
1262 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001263 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 0, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001264 assert(rc == 0);
1265 } else {
1266 rc = 0;
1267 }
1268 } else {
Kristine Jassmann73c38c62021-02-03 16:56:14 +00001269 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_ALIGN_SIZE);
Fabio Utzigba829042018-09-18 08:29:34 -03001270 }
1271#endif
1272
Fabio Utzig10ee6482019-08-01 12:04:52 -03001273 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001274 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001275 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001276 assert(rc == 0);
1277 }
1278
Fabio Utzigba829042018-09-18 08:29:34 -03001279#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001280 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001281 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001282 fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001283 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001284 assert(rc >= 0);
1285
1286 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001287 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001288 assert(rc == 0);
1289 } else {
1290 rc = 0;
1291 }
1292 } else {
Kristine Jassmann73c38c62021-02-03 16:56:14 +00001293 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_ALIGN_SIZE);
Fabio Utzigba829042018-09-18 08:29:34 -03001294 }
1295#endif
1296
Fabio Utzig46490722017-09-04 15:34:32 -03001297 if (size > copy_size) {
1298 copy_size = size;
1299 }
1300
1301 bs->swap_size = copy_size;
1302 } else {
1303 /*
1304 * If a swap was under way, the swap_size should already be present
1305 * in the trailer...
1306 */
Dominik Ermel472d4c72023-02-10 13:29:58 +00001307
1308 rc = boot_find_status(image_index, &fap);
1309 assert(fap != NULL);
1310 rc = boot_read_swap_size(fap, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001311 assert(rc == 0);
1312
1313 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001314
1315#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4741c452019-12-19 15:32:41 -03001316 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Dominik Ermel472d4c72023-02-10 13:29:58 +00001317 rc = boot_read_enc_key(fap, slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001318 assert(rc == 0);
1319
1320 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001321 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001322 break;
1323 }
1324 }
1325
1326 if (i != BOOT_ENC_KEY_SIZE) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001327 boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001328 }
1329 }
1330#endif
Dominik Ermel472d4c72023-02-10 13:29:58 +00001331 flash_area_close(fap);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001332 }
1333
Fabio Utzig12d59162019-11-28 10:01:59 -03001334 swap_run(state, bs, copy_size);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001335
David Vincze2d736ad2019-02-18 11:50:22 +01001336#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig12d59162019-11-28 10:01:59 -03001337 extern int boot_status_fails;
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001338 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001339 BOOT_LOG_WRN("%d status write fails performing the swap",
1340 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001341 }
1342#endif
Sigvart Hovland3fd4cd42022-09-09 13:21:18 +02001343 rc = BOOT_HOOK_CALL(boot_copy_region_post_hook, 0, BOOT_CURR_IMG(state),
1344 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT), size);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001345
Christopher Collins92ea77f2016-12-12 15:59:26 -08001346 return 0;
1347}
David Brown17609d82017-05-05 09:41:34 -06001348#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001349
David Vinczee32483f2019-06-13 10:46:24 +02001350#if (BOOT_IMAGE_NUMBER > 1)
1351/**
1352 * Check the image dependency whether it is satisfied and modify
1353 * the swap type if necessary.
1354 *
1355 * @param dep Image dependency which has to be verified.
1356 *
1357 * @return 0 on success; nonzero on failure.
1358 */
1359static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001360boot_verify_slot_dependency(struct boot_loader_state *state,
1361 struct image_dependency *dep)
David Vinczee32483f2019-06-13 10:46:24 +02001362{
1363 struct image_version *dep_version;
1364 size_t dep_slot;
1365 int rc;
David Browne6ab34c2019-09-03 12:24:21 -06001366 uint8_t swap_type;
David Vinczee32483f2019-06-13 10:46:24 +02001367
1368 /* Determine the source of the image which is the subject of
1369 * the dependency and get it's version. */
David Browne6ab34c2019-09-03 12:24:21 -06001370 swap_type = state->swap_type[dep->image_id];
Barry Solomon04075532020-03-18 09:33:32 -04001371 dep_slot = BOOT_IS_UPGRADE(swap_type) ? BOOT_SECONDARY_SLOT
1372 : BOOT_PRIMARY_SLOT;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001373 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +02001374
David Vincze8b0b6372020-05-20 19:54:44 +02001375 rc = boot_version_cmp(dep_version, &dep->image_min_version);
1376 if (rc < 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001377 /* Dependency not satisfied.
1378 * Modify the swap type to decrease the version number of the image
1379 * (which will be located in the primary slot after the boot process),
1380 * consequently the number of unsatisfied dependencies will be
1381 * decreased or remain the same.
1382 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001383 switch (BOOT_SWAP_TYPE(state)) {
David Vinczee32483f2019-06-13 10:46:24 +02001384 case BOOT_SWAP_TYPE_TEST:
1385 case BOOT_SWAP_TYPE_PERM:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001386 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczee32483f2019-06-13 10:46:24 +02001387 break;
1388 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001389 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczee32483f2019-06-13 10:46:24 +02001390 break;
1391 default:
1392 break;
1393 }
David Vincze8b0b6372020-05-20 19:54:44 +02001394 } else {
1395 /* Dependency satisfied. */
1396 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001397 }
1398
1399 return rc;
1400}
1401
1402/**
1403 * Read all dependency TLVs of an image from the flash and verify
1404 * one after another to see if they are all satisfied.
1405 *
1406 * @param slot Image slot number.
1407 *
1408 * @return 0 on success; nonzero on failure.
1409 */
1410static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001411boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
David Vinczee32483f2019-06-13 10:46:24 +02001412{
1413 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001414 struct image_tlv_iter it;
David Vinczee32483f2019-06-13 10:46:24 +02001415 struct image_dependency dep;
1416 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001417 uint16_t len;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001418 int area_id;
David Vinczee32483f2019-06-13 10:46:24 +02001419 int rc;
1420
Fabio Utzig10ee6482019-08-01 12:04:52 -03001421 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001422 rc = flash_area_open(area_id, &fap);
David Vinczee32483f2019-06-13 10:46:24 +02001423 if (rc != 0) {
1424 rc = BOOT_EFLASH;
1425 goto done;
1426 }
1427
Fabio Utzig61fd8882019-09-14 20:00:20 -03001428 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
1429 IMAGE_TLV_DEPENDENCY, true);
David Vinczee32483f2019-06-13 10:46:24 +02001430 if (rc != 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001431 goto done;
1432 }
1433
Fabio Utzig61fd8882019-09-14 20:00:20 -03001434 while (true) {
1435 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
1436 if (rc < 0) {
1437 return -1;
1438 } else if (rc > 0) {
1439 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001440 break;
1441 }
Fabio Utzig61fd8882019-09-14 20:00:20 -03001442
1443 if (len != sizeof(dep)) {
1444 rc = BOOT_EBADIMAGE;
1445 goto done;
1446 }
1447
1448 rc = flash_area_read(fap, off, &dep, len);
1449 if (rc != 0) {
1450 rc = BOOT_EFLASH;
1451 goto done;
1452 }
1453
1454 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
1455 rc = BOOT_EBADARGS;
1456 goto done;
1457 }
1458
1459 /* Verify dependency and modify the swap type if not satisfied. */
1460 rc = boot_verify_slot_dependency(state, &dep);
1461 if (rc != 0) {
1462 /* Dependency not satisfied. */
1463 goto done;
1464 }
David Vinczee32483f2019-06-13 10:46:24 +02001465 }
1466
1467done:
1468 flash_area_close(fap);
1469 return rc;
1470}
1471
1472/**
David Vinczee32483f2019-06-13 10:46:24 +02001473 * Iterate over all the images and verify whether the image dependencies in the
1474 * TLV area are all satisfied and update the related swap type if necessary.
1475 */
Fabio Utzig298913b2019-08-28 11:22:45 -03001476static int
1477boot_verify_dependencies(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001478{
Erik Johnson49063752020-02-06 09:59:31 -06001479 int rc = -1;
Fabio Utzig298913b2019-08-28 11:22:45 -03001480 uint8_t slot;
David Vinczee32483f2019-06-13 10:46:24 +02001481
Fabio Utzig10ee6482019-08-01 12:04:52 -03001482 BOOT_CURR_IMG(state) = 0;
1483 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
Raef Colesf11de642021-10-15 11:11:56 +01001484 if (state->img_mask[BOOT_CURR_IMG(state)]) {
1485 BOOT_CURR_IMG(state)++;
1486 continue;
1487 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001488 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1489 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
1490 slot = BOOT_SECONDARY_SLOT;
1491 } else {
1492 slot = BOOT_PRIMARY_SLOT;
1493 }
1494
1495 rc = boot_verify_slot_dependencies(state, slot);
Fabio Utzigabec0732019-07-31 08:40:22 -03001496 if (rc == 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001497 /* All dependencies've been satisfied, continue with next image. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001498 BOOT_CURR_IMG(state)++;
David Vincze8b0b6372020-05-20 19:54:44 +02001499 } else {
Fabio Utzig298913b2019-08-28 11:22:45 -03001500 /* Cannot upgrade due to non-met dependencies, so disable all
1501 * image upgrades.
1502 */
1503 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
1504 BOOT_CURR_IMG(state) = idx;
1505 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1506 }
1507 break;
David Vinczee32483f2019-06-13 10:46:24 +02001508 }
1509 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001510 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02001511}
1512#endif /* (BOOT_IMAGE_NUMBER > 1) */
1513
Christopher Collins92ea77f2016-12-12 15:59:26 -08001514/**
David Vinczeba3bd602019-06-17 16:01:43 +02001515 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001516 *
David Vinczeba3bd602019-06-17 16:01:43 +02001517 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001518 *
1519 * @return 0 on success; nonzero on failure.
1520 */
1521static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001522boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001523{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001524 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001525#ifndef MCUBOOT_OVERWRITE_ONLY
1526 uint8_t swap_type;
1527#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001528
David Vinczeba3bd602019-06-17 16:01:43 +02001529 /* At this point there are no aborted swaps. */
1530#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001531 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001532#elif defined(MCUBOOT_BOOTSTRAP)
1533 /* Check if the image update was triggered by a bad image in the
1534 * primary slot (the validity of the image in the secondary slot had
1535 * already been checked).
1536 */
Michael Grand5047f032022-11-24 16:49:56 +01001537 FIH_DECLARE(fih_rc, FIH_FAILURE);
Raef Colese8fe6cf2020-05-26 13:07:40 +01001538 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1539 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, bs);
Michael Grand5047f032022-11-24 16:49:56 +01001540 if (rc == 0 || FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001541 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001542 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001543 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001544 }
1545#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03001546 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001547#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001548 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001549
1550#ifndef MCUBOOT_OVERWRITE_ONLY
1551 /* The following state needs image_ok be explicitly set after the
1552 * swap was finished to avoid a new revert.
1553 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001554 swap_type = BOOT_SWAP_TYPE(state);
1555 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1556 swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001557 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001558 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001559 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001560 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001561 }
1562
David Vinczec3084132020-02-18 14:50:47 +01001563#ifdef MCUBOOT_HW_ROLLBACK_PROT
1564 if (swap_type == BOOT_SWAP_TYPE_PERM) {
1565 /* Update the stored security counter with the new image's security
1566 * counter value. The primary slot holds the new image at this point,
1567 * but the secondary slot's image header must be passed since image
1568 * headers in the boot_data structure have not been updated yet.
1569 *
1570 * In case of a permanent image swap mcuboot will never attempt to
1571 * revert the images on the next reboot. Therefore, the security
1572 * counter must be increased right after the image upgrade.
1573 */
1574 rc = boot_update_security_counter(
1575 BOOT_CURR_IMG(state),
1576 BOOT_PRIMARY_SLOT,
1577 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1578 if (rc != 0) {
1579 BOOT_LOG_ERR("Security counter update failed after "
1580 "image upgrade.");
1581 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
1582 }
1583 }
1584#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1585
Fabio Utzige575b0b2019-09-11 12:34:23 -03001586 if (BOOT_IS_UPGRADE(swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001587 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001588 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001589 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001590 }
David Vinczeba3bd602019-06-17 16:01:43 +02001591 }
1592#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001593
David Vinczeba3bd602019-06-17 16:01:43 +02001594 return rc;
1595}
1596
1597/**
1598 * Completes a previously aborted image swap.
1599 *
1600 * @param bs The current boot status.
1601 *
1602 * @return 0 on success; nonzero on failure.
1603 */
1604#if !defined(MCUBOOT_OVERWRITE_ONLY)
1605static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001606boot_complete_partial_swap(struct boot_loader_state *state,
1607 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001608{
1609 int rc;
1610
1611 /* Determine the type of swap operation being resumed from the
1612 * `swap-type` trailer field.
1613 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001614 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001615 assert(rc == 0);
1616
Fabio Utzig10ee6482019-08-01 12:04:52 -03001617 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001618
1619 /* The following states need image_ok be explicitly set after the
1620 * swap was finished to avoid a new revert.
1621 */
1622 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1623 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001624 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001625 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001626 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001627 }
1628 }
1629
Fabio Utzige575b0b2019-09-11 12:34:23 -03001630 if (BOOT_IS_UPGRADE(bs->swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001631 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001632 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001633 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001634 }
1635 }
1636
Fabio Utzig10ee6482019-08-01 12:04:52 -03001637 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001638 BOOT_LOG_ERR("panic!");
1639 assert(0);
1640
1641 /* Loop forever... */
1642 while (1) {}
1643 }
1644
1645 return rc;
1646}
1647#endif /* !MCUBOOT_OVERWRITE_ONLY */
1648
1649#if (BOOT_IMAGE_NUMBER > 1)
1650/**
1651 * Review the validity of previously determined swap types of other images.
1652 *
1653 * @param aborted_swap The current image upgrade is a
1654 * partial/aborted swap.
1655 */
1656static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001657boot_review_image_swap_types(struct boot_loader_state *state,
1658 bool aborted_swap)
David Vinczeba3bd602019-06-17 16:01:43 +02001659{
1660 /* In that case if we rebooted in the middle of an image upgrade process, we
1661 * must review the validity of swap types, that were previously determined
1662 * for other images. The image_ok flag had not been set before the reboot
1663 * for any of the updated images (only the copy_done flag) and thus falsely
1664 * the REVERT swap type has been determined for the previous images that had
1665 * been updated before the reboot.
1666 *
1667 * There are two separate scenarios that we have to deal with:
1668 *
1669 * 1. The reboot has happened during swapping an image:
1670 * The current image upgrade has been determined as a
1671 * partial/aborted swap.
1672 * 2. The reboot has happened between two separate image upgrades:
1673 * In this scenario we must check the swap type of the current image.
1674 * In those cases if it is NONE or REVERT we cannot certainly determine
1675 * the fact of a reboot. In a consistent state images must move in the
1676 * same direction or stay in place, e.g. in practice REVERT and TEST
1677 * swap types cannot be present at the same time. If the swap type of
1678 * the current image is either TEST, PERM or FAIL we must review the
1679 * already determined swap types of other images and set each false
1680 * REVERT swap types to NONE (these images had been successfully
1681 * updated before the system rebooted between two separate image
1682 * upgrades).
1683 */
1684
Fabio Utzig10ee6482019-08-01 12:04:52 -03001685 if (BOOT_CURR_IMG(state) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001686 /* Nothing to do */
1687 return;
1688 }
1689
1690 if (!aborted_swap) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001691 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
1692 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001693 /* Nothing to do */
1694 return;
1695 }
1696 }
1697
Fabio Utzig10ee6482019-08-01 12:04:52 -03001698 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
1699 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
1700 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001701 }
1702 }
1703}
1704#endif
1705
1706/**
1707 * Prepare image to be updated if required.
1708 *
1709 * Prepare image to be updated if required with completing an image swap
1710 * operation if one was aborted and/or determining the type of the
1711 * swap operation. In case of any error set the swap type to NONE.
1712 *
Fabio Utzig10ee6482019-08-01 12:04:52 -03001713 * @param state TODO
David Vinczeba3bd602019-06-17 16:01:43 +02001714 * @param bs Pointer where the read and possibly updated
1715 * boot status can be written to.
1716 */
1717static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001718boot_prepare_image_for_update(struct boot_loader_state *state,
1719 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001720{
1721 int rc;
Michael Grand5047f032022-11-24 16:49:56 +01001722 FIH_DECLARE(fih_rc, FIH_FAILURE);
David Vinczeba3bd602019-06-17 16:01:43 +02001723
1724 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001725 rc = boot_read_sectors(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001726 if (rc != 0) {
1727 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
1728 " - too small?", BOOT_MAX_IMG_SECTORS);
1729 /* Unable to determine sector layout, continue with next image
1730 * if there is one.
1731 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001732 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Andrzej Puzdrowski54b4ad92021-06-22 13:21:22 +02001733 if (rc == BOOT_EFLASH)
1734 {
1735 /* Only return on error from the primary image flash */
1736 return;
1737 }
David Vinczeba3bd602019-06-17 16:01:43 +02001738 }
1739
1740 /* Attempt to read an image header from each slot. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001741 rc = boot_read_image_headers(state, false, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02001742 if (rc != 0) {
1743 /* Continue with next image if there is one. */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001744 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001745 BOOT_CURR_IMG(state));
1746 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001747 return;
1748 }
1749
1750 /* If the current image's slots aren't compatible, no swap is possible.
1751 * Just boot into primary slot.
1752 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001753 if (boot_slots_compatible(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001754 boot_status_reset(bs);
1755
1756#ifndef MCUBOOT_OVERWRITE_ONLY
1757 rc = swap_read_status(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001758 if (rc != 0) {
1759 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001760 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001761 /* Continue with next image if there is one. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001762 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001763 return;
1764 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001765#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001766
Fabio Utzig74aef312019-11-28 11:05:34 -03001767#ifdef MCUBOOT_SWAP_USING_MOVE
1768 /*
1769 * Must re-read image headers because the boot status might
1770 * have been updated in the previous function call.
1771 */
1772 rc = boot_read_image_headers(state, !boot_status_is_reset(bs), bs);
Fabio Utzig32afe852020-10-04 10:36:02 -03001773#ifdef MCUBOOT_BOOTSTRAP
1774 /* When bootstrapping it's OK to not have image magic in the primary slot */
1775 if (rc != 0 && (BOOT_CURR_IMG(state) != BOOT_PRIMARY_SLOT ||
1776 boot_check_header_erased(state, BOOT_PRIMARY_SLOT) != 0)) {
1777#else
Fabio Utzig74aef312019-11-28 11:05:34 -03001778 if (rc != 0) {
Fabio Utzig32afe852020-10-04 10:36:02 -03001779#endif
1780
Fabio Utzig74aef312019-11-28 11:05:34 -03001781 /* Continue with next image if there is one. */
1782 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
1783 BOOT_CURR_IMG(state));
1784 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1785 return;
1786 }
1787#endif
1788
David Vinczeba3bd602019-06-17 16:01:43 +02001789 /* Determine if we rebooted in the middle of an image swap
1790 * operation. If a partial swap was detected, complete it.
1791 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001792 if (!boot_status_is_reset(bs)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001793
1794#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001795 boot_review_image_swap_types(state, true);
David Vinczeba3bd602019-06-17 16:01:43 +02001796#endif
1797
1798#ifdef MCUBOOT_OVERWRITE_ONLY
1799 /* Should never arrive here, overwrite-only mode has
1800 * no swap state.
1801 */
1802 assert(0);
1803#else
1804 /* Determine the type of swap operation being resumed from the
1805 * `swap-type` trailer field.
1806 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001807 rc = boot_complete_partial_swap(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001808 assert(rc == 0);
1809#endif
1810 /* Attempt to read an image header from each slot. Ensure that
1811 * image headers in slots are aligned with headers in boot_data.
1812 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001813 rc = boot_read_image_headers(state, false, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001814 assert(rc == 0);
1815
1816 /* Swap has finished set to NONE */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001817 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001818 } else {
1819 /* There was no partial swap, determine swap type. */
1820 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001821 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001822 } else {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001823 FIH_CALL(boot_validate_slot, fih_rc,
1824 state, BOOT_SECONDARY_SLOT, bs);
Michael Grand5047f032022-11-24 16:49:56 +01001825 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001826 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
1827 } else {
1828 BOOT_SWAP_TYPE(state) = bs->swap_type;
1829 }
David Vinczeba3bd602019-06-17 16:01:43 +02001830 }
1831
1832#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001833 boot_review_image_swap_types(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02001834#endif
1835
1836#ifdef MCUBOOT_BOOTSTRAP
Fabio Utzig10ee6482019-08-01 12:04:52 -03001837 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001838 /* Header checks are done first because they are
1839 * inexpensive. Since overwrite-only copies starting from
1840 * offset 0, if interrupted, it might leave a valid header
1841 * magic, so also run validation on the primary slot to be
1842 * sure it's not OK.
1843 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001844 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1845 FIH_CALL(boot_validate_slot, fih_rc,
1846 state, BOOT_PRIMARY_SLOT, bs);
1847
Michael Grand5047f032022-11-24 16:49:56 +01001848 if (rc == 0 || FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001849
Fabio Utzig3d77c952020-10-04 10:23:17 -03001850 rc = (boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC) ? 1: 0;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001851 FIH_CALL(boot_validate_slot, fih_rc,
1852 state, BOOT_SECONDARY_SLOT, bs);
1853
Michael Grand5047f032022-11-24 16:49:56 +01001854 if (rc == 1 && FIH_EQ(fih_rc, FIH_SUCCESS)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001855 /* Set swap type to REVERT to overwrite the primary
1856 * slot with the image contained in secondary slot
1857 * and to trigger the explicit setting of the
1858 * image_ok flag.
1859 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03001860 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02001861 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001862 }
1863 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001864#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001865 }
David Vinczeba3bd602019-06-17 16:01:43 +02001866 } else {
1867 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001868 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001869 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001870}
1871
Mark Horvathccaf7f82021-01-04 18:16:42 +01001872/**
1873 * Updates the security counter for the current image.
1874 *
1875 * @param state Boot loader status information.
1876 *
1877 * @return 0 on success; nonzero on failure.
1878 */
1879static int
1880boot_update_hw_rollback_protection(struct boot_loader_state *state)
1881{
1882#ifdef MCUBOOT_HW_ROLLBACK_PROT
1883 int rc;
1884
1885 /* Update the stored security counter with the active image's security
1886 * counter value. It will only be updated if the new security counter is
1887 * greater than the stored value.
1888 *
1889 * In case of a successful image swapping when the swap type is TEST the
1890 * security counter can be increased only after a reset, when the swap
1891 * type is NONE and the image has marked itself "OK" (the image_ok flag
1892 * has been set). This way a "revert" can be performed when it's
1893 * necessary.
1894 */
1895 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
1896 rc = boot_update_security_counter(
1897 BOOT_CURR_IMG(state),
1898 BOOT_PRIMARY_SLOT,
1899 boot_img_hdr(state, BOOT_PRIMARY_SLOT));
1900 if (rc != 0) {
1901 BOOT_LOG_ERR("Security counter update failed after image "
1902 "validation.");
1903 return rc;
1904 }
1905 }
1906
1907 return 0;
1908
1909#else /* MCUBOOT_HW_ROLLBACK_PROT */
1910 (void) (state);
1911
1912 return 0;
1913#endif
1914}
1915
Jerzy Kasenberge3f895d2022-04-12 15:05:33 +02001916/**
1917 * Checks test swap downgrade prevention conditions.
1918 *
1919 * Function called only for swap upgrades test run. It may prevent
1920 * swap if slot 1 image has <= version number or < security counter
1921 *
1922 * @param state Boot loader status information.
1923 *
1924 * @return 0 - image can be swapped, -1 downgrade prevention
1925 */
1926static int
1927check_downgrade_prevention(struct boot_loader_state *state)
1928{
1929#if defined(MCUBOOT_DOWNGRADE_PREVENTION) && \
1930 (defined(MCUBOOT_SWAP_USING_MOVE) || defined(MCUBOOT_SWAP_USING_SCRATCH))
1931 uint32_t security_counter[2];
1932 int rc;
1933
1934 if (MCUBOOT_DOWNGRADE_PREVENTION_SECURITY_COUNTER) {
1935 /* If there was security no counter in slot 0, allow swap */
1936 rc = bootutil_get_img_security_cnt(&(BOOT_IMG(state, 0).hdr),
1937 BOOT_IMG(state, 0).area,
1938 &security_counter[0]);
1939 if (rc != 0) {
1940 return 0;
1941 }
1942 /* If there is no security counter in slot 1, or it's lower than
1943 * that of slot 0, prevent downgrade */
1944 rc = bootutil_get_img_security_cnt(&(BOOT_IMG(state, 1).hdr),
1945 BOOT_IMG(state, 1).area,
1946 &security_counter[1]);
1947 if (rc != 0 || security_counter[0] > security_counter[1]) {
1948 rc = -1;
1949 }
1950 }
1951 else {
1952 rc = boot_version_cmp(&boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
1953 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
1954 }
1955 if (rc < 0) {
1956 /* Image in slot 0 prevents downgrade, delete image in slot 1 */
1957 BOOT_LOG_INF("Image in slot 1 erased due to downgrade prevention");
1958 flash_area_erase(BOOT_IMG(state, 1).area, 0,
1959 flash_area_get_size(BOOT_IMG(state, 1).area));
1960 } else {
1961 rc = 0;
1962 }
1963 return rc;
1964#else
1965 (void)state;
1966 return 0;
1967#endif
1968}
1969
Michael Grand5047f032022-11-24 16:49:56 +01001970fih_ret
Fabio Utzig10ee6482019-08-01 12:04:52 -03001971context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001972{
Marti Bolivar84898652017-06-13 17:20:22 -04001973 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02001974 struct boot_status bs;
David Vincze9015a5d2020-05-18 14:43:11 +02001975 int rc = -1;
Michael Grand5047f032022-11-24 16:49:56 +01001976 FIH_DECLARE(fih_rc, FIH_FAILURE);
Marti Bolivarc0b47912017-06-13 17:18:09 -04001977 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001978 int image_index;
Fabio Utzig298913b2019-08-28 11:22:45 -03001979 bool has_upgrade;
Michael Grand5047f032022-11-24 16:49:56 +01001980 volatile int fih_cnt;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001981
1982 /* The array of slot sectors are defined here (as opposed to file scope) so
1983 * that they don't get allocated for non-boot-loader apps. This is
1984 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001985 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001986 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001987 TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
1988 TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001989#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001990 TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001991#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001992
Fabio Utzig298913b2019-08-28 11:22:45 -03001993 has_upgrade = false;
1994
1995#if (BOOT_IMAGE_NUMBER == 1)
1996 (void)has_upgrade;
1997#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001998
David Vinczeba3bd602019-06-17 16:01:43 +02001999 /* Iterate over all the images. By the end of the loop the swap type has
2000 * to be determined for each image and all aborted swaps have to be
2001 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002002 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002003 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002004#if BOOT_IMAGE_NUMBER > 1
2005 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2006 continue;
2007 }
2008#endif
David Vinczeba3bd602019-06-17 16:01:43 +02002009#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
2010 /* The keys used for encryption may no longer be valid (could belong to
2011 * another images). Therefore, mark them as invalid to force their reload
2012 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03002013 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03002014 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Brown554c52e2017-06-30 16:01:07 -06002015#endif
2016
Fabio Utzig10ee6482019-08-01 12:04:52 -03002017 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03002018
Fabio Utzig10ee6482019-08-01 12:04:52 -03002019 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002020 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03002021 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002022 secondary_slot_sectors[image_index];
Fabio Utzig12d59162019-11-28 10:01:59 -03002023#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03002024 state->scratch.sectors = scratch_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -03002025#endif
David Vinczeba3bd602019-06-17 16:01:43 +02002026
2027 /* Open primary and secondary image areas for the duration
2028 * of this call.
2029 */
2030 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03002031 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03002032 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02002033 assert(rc == 0);
2034 }
Fabio Utzig12d59162019-11-28 10:01:59 -03002035#if MCUBOOT_SWAP_USING_SCRATCH
David Vinczeba3bd602019-06-17 16:01:43 +02002036 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03002037 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002038 assert(rc == 0);
Fabio Utzig12d59162019-11-28 10:01:59 -03002039#endif
David Vinczeba3bd602019-06-17 16:01:43 +02002040
2041 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002042 boot_prepare_image_for_update(state, &bs);
Fabio Utzig298913b2019-08-28 11:22:45 -03002043
Fabio Utzige575b0b2019-09-11 12:34:23 -03002044 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
Fabio Utzig298913b2019-08-28 11:22:45 -03002045 has_upgrade = true;
2046 }
David Vinczeba3bd602019-06-17 16:01:43 +02002047 }
2048
David Vinczee32483f2019-06-13 10:46:24 +02002049#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig298913b2019-08-28 11:22:45 -03002050 if (has_upgrade) {
2051 /* Iterate over all the images and verify whether the image dependencies
2052 * are all satisfied and update swap type if necessary.
2053 */
2054 rc = boot_verify_dependencies(state);
David Vincze8b0b6372020-05-20 19:54:44 +02002055 if (rc != 0) {
Fabio Utzig298913b2019-08-28 11:22:45 -03002056 /*
2057 * It was impossible to upgrade because the expected dependency version
2058 * was not available. Here we already changed the swap_type so that
2059 * instead of asserting the bootloader, we continue and no upgrade is
2060 * performed.
2061 */
2062 rc = 0;
2063 }
2064 }
David Vinczee32483f2019-06-13 10:46:24 +02002065#endif
2066
Jamie McCrae56cb6102022-03-23 11:57:03 +00002067 /* Trigger status change callback with upgrading status */
2068 mcuboot_status_change(MCUBOOT_STATUS_UPGRADING);
2069
David Vinczeba3bd602019-06-17 16:01:43 +02002070 /* Iterate over all the images. At this point there are no aborted swaps
2071 * and the swap types are determined for each image. By the end of the loop
2072 * all required update operations will have been finished.
2073 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002074 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002075#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesf11de642021-10-15 11:11:56 +01002076 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2077 continue;
2078 }
2079
David Vinczeba3bd602019-06-17 16:01:43 +02002080#ifdef MCUBOOT_ENC_IMAGES
2081 /* The keys used for encryption may no longer be valid (could belong to
2082 * another images). Therefore, mark them as invalid to force their reload
2083 * by boot_enc_load().
2084 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03002085 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002086#endif /* MCUBOOT_ENC_IMAGES */
2087
2088 /* Indicate that swap is not aborted */
Fabio Utzig12d59162019-11-28 10:01:59 -03002089 boot_status_reset(&bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002090#endif /* (BOOT_IMAGE_NUMBER > 1) */
2091
2092 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002093 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02002094
Fabio Utzig10ee6482019-08-01 12:04:52 -03002095 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002096 case BOOT_SWAP_TYPE_NONE:
2097 break;
2098
Jerzy Kasenberge3f895d2022-04-12 15:05:33 +02002099 case BOOT_SWAP_TYPE_TEST:
2100 if (check_downgrade_prevention(state) != 0) {
2101 /* Downgrade prevented */
2102 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
2103 break;
2104 }
2105 /* fallthrough */
David Vinczeba3bd602019-06-17 16:01:43 +02002106 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
2107 case BOOT_SWAP_TYPE_REVERT:
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +02002108 rc = BOOT_HOOK_CALL(boot_perform_update_hook, BOOT_HOOK_REGULAR,
2109 BOOT_CURR_IMG(state), &(BOOT_IMG(state, 1).hdr),
2110 BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT));
2111 if (rc == BOOT_HOOK_REGULAR)
2112 {
2113 rc = boot_perform_update(state, &bs);
2114 }
David Vinczeba3bd602019-06-17 16:01:43 +02002115 assert(rc == 0);
2116 break;
2117
2118 case BOOT_SWAP_TYPE_FAIL:
2119 /* The image in secondary slot was invalid and is now erased. Ensure
2120 * we don't try to boot into it again on the next reboot. Do this by
2121 * pretending we just reverted back to primary slot.
2122 */
2123#ifndef MCUBOOT_OVERWRITE_ONLY
2124 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig12d59162019-11-28 10:01:59 -03002125 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002126 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002127 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002128 }
2129#endif /* !MCUBOOT_OVERWRITE_ONLY */
2130 break;
2131
2132 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002133 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002134 }
2135
Fabio Utzig10ee6482019-08-01 12:04:52 -03002136 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002137 BOOT_LOG_ERR("panic!");
2138 assert(0);
2139
2140 /* Loop forever... */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002141 FIH_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002142 }
2143 }
2144
2145 /* Iterate over all the images. At this point all required update operations
2146 * have finished. By the end of the loop each image in the primary slot will
2147 * have been re-validated.
2148 */
Michael Grand5047f032022-11-24 16:49:56 +01002149 FIH_SET(fih_cnt, 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -03002150 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002151#if BOOT_IMAGE_NUMBER > 1
Michael Grand5047f032022-11-24 16:49:56 +01002152 /* Hardenned to prevent from skipping check of a given image,
2153 * tmp_img_mask is declared volatile
2154 */
2155 volatile bool tmp_img_mask;
2156 FIH_SET(tmp_img_mask, state->img_mask[BOOT_CURR_IMG(state)]);
2157 if (FIH_EQ(tmp_img_mask, true)) {
2158 ++fih_cnt;
Raef Colesf11de642021-10-15 11:11:56 +01002159 continue;
2160 }
2161#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -03002162 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02002163 /* Attempt to read an image header from each slot. Ensure that image
2164 * headers in slots are aligned with headers in boot_data.
2165 */
Fabio Utzig12d59162019-11-28 10:01:59 -03002166 rc = boot_read_image_headers(state, false, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002167 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01002168 FIH_SET(fih_rc, FIH_FAILURE);
David Vinczeba3bd602019-06-17 16:01:43 +02002169 goto out;
2170 }
2171 /* Since headers were reloaded, it can be assumed we just performed
2172 * a swap or overwrite. Now the header info that should be used to
2173 * provide the data for the bootstrap, which previously was at
2174 * secondary slot, was updated to primary slot.
2175 */
2176 }
2177
2178#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Raef Colese8fe6cf2020-05-26 13:07:40 +01002179 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, NULL);
Michael Grand5047f032022-11-24 16:49:56 +01002180 /* Check for all possible values is redundant in normal operation it
2181 * is meant to prevent FI attack.
2182 */
2183 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS) ||
2184 FIH_EQ(fih_rc, FIH_FAILURE) ||
2185 FIH_EQ(fih_rc, FIH_NO_BOOTABLE_IMAGE)) {
2186 FIH_SET(fih_rc, FIH_FAILURE);
David Vinczeba3bd602019-06-17 16:01:43 +02002187 goto out;
2188 }
2189#else
2190 /* Even if we're not re-validating the primary slot, we could be booting
2191 * onto an empty flash chip. At least do a basic sanity check that
2192 * the magic number on the image is OK.
2193 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002194 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002195 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
Dominik Ermel4a4d1ac2021-08-06 11:23:52 +00002196 BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic,
Fabio Utzig10ee6482019-08-01 12:04:52 -03002197 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002198 rc = BOOT_EBADIMAGE;
Michael Grand5047f032022-11-24 16:49:56 +01002199 FIH_SET(fih_rc, FIH_FAILURE);
David Vinczeba3bd602019-06-17 16:01:43 +02002200 goto out;
2201 }
David Vinczec3084132020-02-18 14:50:47 +01002202#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
2203
Mark Horvathccaf7f82021-01-04 18:16:42 +01002204 rc = boot_update_hw_rollback_protection(state);
David Vincze1cf11b52020-03-24 07:51:09 +01002205 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01002206 FIH_SET(fih_rc, FIH_FAILURE);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002207 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002208 }
David Vincze1cf11b52020-03-24 07:51:09 +01002209
Mark Horvathccaf7f82021-01-04 18:16:42 +01002210 rc = boot_add_shared_data(state, BOOT_PRIMARY_SLOT);
David Vincze1cf11b52020-03-24 07:51:09 +01002211 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01002212 FIH_SET(fih_rc, FIH_FAILURE);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002213 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002214 }
Michael Grand5047f032022-11-24 16:49:56 +01002215 ++fih_cnt;
David Vinczeba3bd602019-06-17 16:01:43 +02002216 }
Michael Grand5047f032022-11-24 16:49:56 +01002217 /*
2218 * fih_cnt should be equal to BOOT_IMAGE_NUMBER now.
2219 * If this is not the case, at least one iteration of the loop
2220 * has been skipped.
2221 */
2222 if(FIH_NOT_EQ(fih_cnt, BOOT_IMAGE_NUMBER)) {
2223 FIH_PANIC;
2224 }
Fabio Utzigf616c542019-12-19 15:23:32 -03002225 /*
2226 * Since the boot_status struct stores plaintext encryption keys, reset
2227 * them here to avoid the possibility of jumping into an image that could
2228 * easily recover them.
2229 */
2230 memset(&bs, 0, sizeof(struct boot_status));
2231
Raef Colesfe57e7d2021-10-15 11:07:09 +01002232 fill_rsp(state, rsp);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002233
Raef Colese8fe6cf2020-05-26 13:07:40 +01002234 fih_rc = FIH_SUCCESS;
Fabio Utzig298913b2019-08-28 11:22:45 -03002235out:
Mark Horvathccaf7f82021-01-04 18:16:42 +01002236 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002237 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002238}
2239
Michael Grand5047f032022-11-24 16:49:56 +01002240fih_ret
Christopher Collins92ea77f2016-12-12 15:59:26 -08002241split_go(int loader_slot, int split_slot, void **entry)
2242{
Marti Bolivarc50926f2017-06-14 09:35:40 -04002243 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08002244 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002245 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002246 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002247 int rc;
Michael Grand5047f032022-11-24 16:49:56 +01002248 FIH_DECLARE(fih_rc, FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002249
Christopher Collins92ea77f2016-12-12 15:59:26 -08002250 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
2251 if (sectors == NULL) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002252 FIH_RET(FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002253 }
David Vinczeba3bd602019-06-17 16:01:43 +02002254 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
2255 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002256
2257 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
2258 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07002259 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04002260 assert(rc == 0);
2261 split_flash_id = flash_area_id_from_image_slot(split_slot);
2262 rc = flash_area_open(split_flash_id,
2263 &BOOT_IMG_AREA(&boot_data, split_slot));
2264 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002265
2266 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002267 rc = boot_read_sectors(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002268 if (rc != 0) {
2269 rc = SPLIT_GO_ERR;
2270 goto done;
2271 }
2272
Fabio Utzig12d59162019-11-28 10:01:59 -03002273 rc = boot_read_image_headers(&boot_data, true, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002274 if (rc != 0) {
2275 goto done;
2276 }
2277
Christopher Collins92ea77f2016-12-12 15:59:26 -08002278 /* Don't check the bootable image flag because we could really call a
2279 * bootable or non-bootable image. Just validate that the image check
2280 * passes which is distinct from the normal check.
2281 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002282 FIH_CALL(split_image_check, fih_rc,
2283 boot_img_hdr(&boot_data, split_slot),
2284 BOOT_IMG_AREA(&boot_data, split_slot),
2285 boot_img_hdr(&boot_data, loader_slot),
2286 BOOT_IMG_AREA(&boot_data, loader_slot));
Michael Grand5047f032022-11-24 16:49:56 +01002287 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08002288 goto done;
2289 }
2290
Marti Bolivarea088872017-06-12 17:10:49 -04002291 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04002292 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08002293 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002294 rc = SPLIT_GO_OK;
2295
2296done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04002297 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
2298 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002299 free(sectors);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002300
2301 if (rc) {
Michael Grand5047f032022-11-24 16:49:56 +01002302 FIH_SET(fih_rc, FIH_FAILURE);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002303 }
2304
2305 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002306}
David Vinczee574f2d2020-07-10 11:42:03 +02002307
Tamas Banfe031092020-09-10 17:32:39 +02002308#else /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02002309
Mark Horvathccaf7f82021-01-04 18:16:42 +01002310#define NO_ACTIVE_SLOT UINT32_MAX
2311
David Vinczee574f2d2020-07-10 11:42:03 +02002312/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002313 * Opens all flash areas and checks which contain an image with a valid header.
David Vinczee574f2d2020-07-10 11:42:03 +02002314 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002315 * @param state Boot loader status information.
David Vinczee574f2d2020-07-10 11:42:03 +02002316 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002317 * @return 0 on success; nonzero on failure.
2318 */
2319static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002320boot_get_slot_usage(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002321{
2322 uint32_t slot;
2323 int fa_id;
2324 int rc;
2325 struct image_header *hdr = NULL;
2326
2327 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002328#if BOOT_IMAGE_NUMBER > 1
2329 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2330 continue;
2331 }
2332#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +01002333 /* Open all the slots */
2334 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2335 fa_id = flash_area_id_from_multi_image_slot(
2336 BOOT_CURR_IMG(state), slot);
2337 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
2338 assert(rc == 0);
2339 }
2340
2341 /* Attempt to read an image header from each slot. */
2342 rc = boot_read_image_headers(state, false, NULL);
2343 if (rc != 0) {
2344 BOOT_LOG_WRN("Failed reading image headers.");
2345 return rc;
2346 }
2347
2348 /* Check headers in all slots */
2349 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2350 hdr = boot_img_hdr(state, slot);
2351
2352 if (boot_is_header_valid(hdr, BOOT_IMG_AREA(state, slot))) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002353 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = true;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002354 BOOT_LOG_IMAGE_INFO(slot, hdr);
2355 } else {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002356 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = false;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002357 BOOT_LOG_INF("Image %d %s slot: Image not found",
2358 BOOT_CURR_IMG(state),
2359 (slot == BOOT_PRIMARY_SLOT)
2360 ? "Primary" : "Secondary");
2361 }
2362 }
2363
Raef Colesfe57e7d2021-10-15 11:07:09 +01002364 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002365 }
2366
2367 return 0;
2368}
2369
2370/**
2371 * Finds the slot containing the image with the highest version number for the
2372 * current image.
2373 *
2374 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002375 *
2376 * @return NO_ACTIVE_SLOT if no available slot found, number of
2377 * the found slot otherwise.
David Vinczee574f2d2020-07-10 11:42:03 +02002378 */
2379static uint32_t
Raef Colesfe57e7d2021-10-15 11:07:09 +01002380find_slot_with_highest_version(struct boot_loader_state *state)
David Vinczee574f2d2020-07-10 11:42:03 +02002381{
David Vinczee574f2d2020-07-10 11:42:03 +02002382 uint32_t slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002383 uint32_t candidate_slot = NO_ACTIVE_SLOT;
2384 int rc;
David Vinczee574f2d2020-07-10 11:42:03 +02002385
Mark Horvathccaf7f82021-01-04 18:16:42 +01002386 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002387 if (state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot]) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002388 if (candidate_slot == NO_ACTIVE_SLOT) {
2389 candidate_slot = slot;
2390 } else {
2391 rc = boot_version_cmp(
2392 &boot_img_hdr(state, slot)->ih_ver,
2393 &boot_img_hdr(state, candidate_slot)->ih_ver);
2394 if (rc == 1) {
2395 /* The version of the image being examined is greater than
2396 * the version of the current candidate.
2397 */
2398 candidate_slot = slot;
2399 }
2400 }
David Vinczee574f2d2020-07-10 11:42:03 +02002401 }
2402 }
2403
Mark Horvathccaf7f82021-01-04 18:16:42 +01002404 return candidate_slot;
David Vinczee574f2d2020-07-10 11:42:03 +02002405}
2406
Mark Horvathccaf7f82021-01-04 18:16:42 +01002407#ifdef MCUBOOT_HAVE_LOGGING
2408/**
2409 * Prints the state of the loaded images.
2410 *
2411 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002412 */
2413static void
Raef Colesfe57e7d2021-10-15 11:07:09 +01002414print_loaded_images(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002415{
2416 uint32_t active_slot;
2417
David Brown695e5912021-05-24 16:58:01 -06002418 (void)state;
2419
Mark Horvathccaf7f82021-01-04 18:16:42 +01002420 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002421#if BOOT_IMAGE_NUMBER > 1
2422 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2423 continue;
2424 }
2425#endif
Raef Colesfe57e7d2021-10-15 11:07:09 +01002426 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002427
2428 BOOT_LOG_INF("Image %d loaded from the %s slot",
2429 BOOT_CURR_IMG(state),
2430 (active_slot == BOOT_PRIMARY_SLOT) ?
2431 "primary" : "secondary");
2432 }
2433}
2434#endif
2435
David Vincze1c456242021-06-29 15:25:24 +02002436#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
David Vincze505fba22020-10-22 13:53:29 +02002437/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002438 * Checks whether the active slot of the current image was previously selected
2439 * to run. Erases the image if it was selected but its execution failed,
2440 * otherwise marks it as selected if it has not been before.
David Vincze505fba22020-10-22 13:53:29 +02002441 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002442 * @param state Boot loader status information.
David Vincze505fba22020-10-22 13:53:29 +02002443 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002444 * @return 0 on success; nonzero on failure.
David Vincze505fba22020-10-22 13:53:29 +02002445 */
2446static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002447boot_select_or_erase(struct boot_loader_state *state)
David Vincze505fba22020-10-22 13:53:29 +02002448{
2449 const struct flash_area *fap;
2450 int fa_id;
2451 int rc;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002452 uint32_t active_slot;
2453 struct boot_swap_state* active_swap_state;
David Vincze505fba22020-10-22 13:53:29 +02002454
Raef Colesfe57e7d2021-10-15 11:07:09 +01002455 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002456
2457 fa_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), active_slot);
David Vincze505fba22020-10-22 13:53:29 +02002458 rc = flash_area_open(fa_id, &fap);
2459 assert(rc == 0);
2460
Raef Colesfe57e7d2021-10-15 11:07:09 +01002461 active_swap_state = &(state->slot_usage[BOOT_CURR_IMG(state)].swap_state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002462
2463 memset(active_swap_state, 0, sizeof(struct boot_swap_state));
2464 rc = boot_read_swap_state(fap, active_swap_state);
David Vincze505fba22020-10-22 13:53:29 +02002465 assert(rc == 0);
2466
Mark Horvathccaf7f82021-01-04 18:16:42 +01002467 if (active_swap_state->magic != BOOT_MAGIC_GOOD ||
2468 (active_swap_state->copy_done == BOOT_FLAG_SET &&
2469 active_swap_state->image_ok != BOOT_FLAG_SET)) {
David Vincze505fba22020-10-22 13:53:29 +02002470 /*
2471 * A reboot happened without the image being confirmed at
2472 * runtime or its trailer is corrupted/invalid. Erase the image
2473 * to prevent it from being selected again on the next reboot.
2474 */
2475 BOOT_LOG_DBG("Erasing faulty image in the %s slot.",
Carlos Falgueras Garcíaae13c3c2021-06-21 17:20:31 +02002476 (active_slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
Dominik Ermel260ae092021-04-23 05:38:45 +00002477 rc = flash_area_erase(fap, 0, flash_area_get_size(fap));
David Vincze505fba22020-10-22 13:53:29 +02002478 assert(rc == 0);
2479
2480 flash_area_close(fap);
2481 rc = -1;
2482 } else {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002483 if (active_swap_state->copy_done != BOOT_FLAG_SET) {
2484 if (active_swap_state->copy_done == BOOT_FLAG_BAD) {
David Vincze505fba22020-10-22 13:53:29 +02002485 BOOT_LOG_DBG("The copy_done flag had an unexpected value. Its "
2486 "value was neither 'set' nor 'unset', but 'bad'.");
2487 }
2488 /*
2489 * Set the copy_done flag, indicating that the image has been
2490 * selected to boot. It can be set in advance, before even
2491 * validating the image, because in case the validation fails, the
2492 * entire image slot will be erased (including the trailer).
2493 */
2494 rc = boot_write_copy_done(fap);
2495 if (rc != 0) {
2496 BOOT_LOG_WRN("Failed to set copy_done flag of the image in "
Carlos Falgueras Garcíaae13c3c2021-06-21 17:20:31 +02002497 "the %s slot.", (active_slot == BOOT_PRIMARY_SLOT) ?
David Vincze505fba22020-10-22 13:53:29 +02002498 "primary" : "secondary");
2499 rc = 0;
2500 }
2501 }
2502 flash_area_close(fap);
2503 }
2504
2505 return rc;
2506}
David Vincze1c456242021-06-29 15:25:24 +02002507#endif /* MCUBOOT_DIRECT_XIP && MCUBOOT_DIRECT_XIP_REVERT */
David Vincze505fba22020-10-22 13:53:29 +02002508
Tamas Banfe031092020-09-10 17:32:39 +02002509#ifdef MCUBOOT_RAM_LOAD
2510
Mark Horvathccaf7f82021-01-04 18:16:42 +01002511#ifndef MULTIPLE_EXECUTABLE_RAM_REGIONS
Tamas Banfe031092020-09-10 17:32:39 +02002512#if !defined(IMAGE_EXECUTABLE_RAM_START) || !defined(IMAGE_EXECUTABLE_RAM_SIZE)
2513#error "Platform MUST define executable RAM bounds in case of RAM_LOAD"
2514#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +01002515#endif
Tamas Banfe031092020-09-10 17:32:39 +02002516
2517/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002518 * Verifies that the active slot of the current image can be loaded within the
2519 * predefined bounds that are allowed to be used by executable images.
Tamas Banfe031092020-09-10 17:32:39 +02002520 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002521 * @param state Boot loader status information.
Tamas Banfe031092020-09-10 17:32:39 +02002522 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002523 * @return 0 on success; nonzero on failure.
Tamas Banfe031092020-09-10 17:32:39 +02002524 */
2525static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002526boot_verify_ram_load_address(struct boot_loader_state *state)
Tamas Banfe031092020-09-10 17:32:39 +02002527{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002528 uint32_t img_dst;
2529 uint32_t img_sz;
Tamas Banfe031092020-09-10 17:32:39 +02002530 uint32_t img_end_addr;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002531 uint32_t exec_ram_start;
2532 uint32_t exec_ram_size;
David Brown695e5912021-05-24 16:58:01 -06002533
2534 (void)state;
2535
Mark Horvathccaf7f82021-01-04 18:16:42 +01002536#ifdef MULTIPLE_EXECUTABLE_RAM_REGIONS
2537 int rc;
Tamas Banfe031092020-09-10 17:32:39 +02002538
Mark Horvathccaf7f82021-01-04 18:16:42 +01002539 rc = boot_get_image_exec_ram_info(BOOT_CURR_IMG(state), &exec_ram_start,
2540 &exec_ram_size);
2541 if (rc != 0) {
2542 return BOOT_EBADSTATUS;
2543 }
2544#else
2545 exec_ram_start = IMAGE_EXECUTABLE_RAM_START;
2546 exec_ram_size = IMAGE_EXECUTABLE_RAM_SIZE;
2547#endif
2548
Raef Colesfe57e7d2021-10-15 11:07:09 +01002549 img_dst = state->slot_usage[BOOT_CURR_IMG(state)].img_dst;
2550 img_sz = state->slot_usage[BOOT_CURR_IMG(state)].img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002551
2552 if (img_dst < exec_ram_start) {
Tamas Banfe031092020-09-10 17:32:39 +02002553 return BOOT_EBADIMAGE;
2554 }
2555
2556 if (!boot_u32_safe_add(&img_end_addr, img_dst, img_sz)) {
2557 return BOOT_EBADIMAGE;
2558 }
2559
Mark Horvathccaf7f82021-01-04 18:16:42 +01002560 if (img_end_addr > (exec_ram_start + exec_ram_size)) {
Tamas Banfe031092020-09-10 17:32:39 +02002561 return BOOT_EBADIMAGE;
2562 }
2563
2564 return 0;
2565}
2566
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002567#ifdef MCUBOOT_ENC_IMAGES
2568
2569/**
2570 * Copies and decrypts an image from a slot in the flash to an SRAM address.
2571 *
2572 * @param state Boot loader status information.
2573 * @param slot The flash slot of the image to be copied to SRAM.
2574 * @param hdr The image header.
2575 * @param src_sz Size of the image.
2576 * @param img_dst Pointer to the address at which the image needs to be
2577 * copied to SRAM.
2578 *
2579 * @return 0 on success; nonzero on failure.
2580 */
2581static int
2582boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state,
2583 uint32_t slot, struct image_header *hdr,
2584 uint32_t src_sz, uint32_t img_dst)
2585{
2586 /* The flow for the decryption and copy of the image is as follows :
2587 * 1. The whole image is copied to the RAM (header + payload + TLV).
2588 * 2. The encryption key is loaded from the TLV in flash.
2589 * 3. The image is then decrypted chunk by chunk in RAM (1 chunk
2590 * is 1024 bytes). Only the payload section is decrypted.
2591 * 4. The image is authenticated in RAM.
2592 */
2593 const struct flash_area *fap_src = NULL;
2594 struct boot_status bs;
2595 uint32_t blk_off;
2596 uint32_t tlv_off;
2597 uint32_t blk_sz;
2598 uint32_t bytes_copied = hdr->ih_hdr_size;
2599 uint32_t chunk_sz;
2600 uint32_t max_sz = 1024;
2601 uint16_t idx;
2602 uint8_t image_index;
2603 uint8_t * cur_dst;
2604 int area_id;
2605 int rc;
2606 uint8_t * ram_dst = (void *)(IMAGE_RAM_BASE + img_dst);
2607
2608 image_index = BOOT_CURR_IMG(state);
2609 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2610 rc = flash_area_open(area_id, &fap_src);
2611 if (rc != 0){
2612 return BOOT_EFLASH;
2613 }
2614
2615 tlv_off = BOOT_TLV_OFF(hdr);
2616
2617 /* Copying the whole image in RAM */
2618 rc = flash_area_read(fap_src, 0, ram_dst, src_sz);
2619 if (rc != 0) {
2620 goto done;
2621 }
2622
2623 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap_src, &bs);
2624 if (rc < 0) {
2625 goto done;
2626 }
2627
2628 /* if rc > 0 then the key has already been loaded */
2629 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), slot, &bs)) {
2630 goto done;
2631 }
2632
2633 /* Starting at the end of the header as the header section is not encrypted */
2634 while (bytes_copied < tlv_off) { /* TLV section copied previously */
2635 if (src_sz - bytes_copied > max_sz) {
2636 chunk_sz = max_sz;
2637 } else {
2638 chunk_sz = src_sz - bytes_copied;
2639 }
2640
2641 cur_dst = ram_dst + bytes_copied;
2642 blk_sz = chunk_sz;
2643 idx = 0;
2644 if (bytes_copied + chunk_sz > tlv_off) {
2645 /* Going over TLV section
2646 * Part of the chunk is encrypted payload */
2647 blk_off = ((bytes_copied) - hdr->ih_hdr_size) & 0xf;
2648 blk_sz = tlv_off - (bytes_copied);
2649 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
2650 (bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
2651 blk_off, cur_dst);
2652 } else {
2653 /* Image encrypted payload section */
2654 blk_off = ((bytes_copied) - hdr->ih_hdr_size) & 0xf;
2655 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
2656 (bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
2657 blk_off, cur_dst);
2658 }
2659
2660 bytes_copied += chunk_sz;
2661 }
2662 rc = 0;
2663
2664done:
2665 flash_area_close(fap_src);
2666
2667 return rc;
2668}
2669
2670#endif /* MCUBOOT_ENC_IMAGES */
Tamas Banfe031092020-09-10 17:32:39 +02002671/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002672 * Copies a slot of the current image into SRAM.
Tamas Banfe031092020-09-10 17:32:39 +02002673 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002674 * @param state Boot loader status information.
Tamas Banfe031092020-09-10 17:32:39 +02002675 * @param slot The flash slot of the image to be copied to SRAM.
2676 * @param img_dst The address at which the image needs to be copied to
2677 * SRAM.
2678 * @param img_sz The size of the image that needs to be copied to SRAM.
2679 *
2680 * @return 0 on success; nonzero on failure.
2681 */
2682static int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002683boot_copy_image_to_sram(struct boot_loader_state *state, int slot,
2684 uint32_t img_dst, uint32_t img_sz)
Tamas Banfe031092020-09-10 17:32:39 +02002685{
2686 int rc;
2687 const struct flash_area *fap_src = NULL;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002688 int area_id;
Tamas Banfe031092020-09-10 17:32:39 +02002689
Mark Horvathccaf7f82021-01-04 18:16:42 +01002690#if (BOOT_IMAGE_NUMBER == 1)
2691 (void)state;
2692#endif
2693
2694 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2695
2696 rc = flash_area_open(area_id, &fap_src);
Tamas Banfe031092020-09-10 17:32:39 +02002697 if (rc != 0) {
2698 return BOOT_EFLASH;
2699 }
2700
2701 /* Direct copy from flash to its new location in SRAM. */
David Brown9bd7f902021-05-26 16:31:14 -06002702 rc = flash_area_read(fap_src, 0, (void *)(IMAGE_RAM_BASE + img_dst), img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002703 if (rc != 0) {
2704 BOOT_LOG_INF("Error whilst copying image from Flash to SRAM: %d", rc);
2705 }
2706
2707 flash_area_close(fap_src);
2708
2709 return rc;
2710}
2711
Mark Horvathccaf7f82021-01-04 18:16:42 +01002712#if (BOOT_IMAGE_NUMBER > 1)
Tamas Banfe031092020-09-10 17:32:39 +02002713/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002714 * Checks if two memory regions (A and B) are overlap or not.
Tamas Banfe031092020-09-10 17:32:39 +02002715 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002716 * @param start_a Start of the A region.
2717 * @param end_a End of the A region.
2718 * @param start_b Start of the B region.
2719 * @param end_b End of the B region.
Tamas Banfe031092020-09-10 17:32:39 +02002720 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002721 * @return true if there is overlap; false otherwise.
2722 */
2723static bool
2724do_regions_overlap(uint32_t start_a, uint32_t end_a,
2725 uint32_t start_b, uint32_t end_b)
2726{
2727 if (start_b > end_a) {
2728 return false;
2729 } else if (start_b >= start_a) {
2730 return true;
2731 } else if (end_b > start_a) {
2732 return true;
2733 }
2734
2735 return false;
2736}
2737
2738/**
2739 * Checks if the image we want to load to memory overlap with an already
2740 * ramloaded image.
2741 *
Raef Colesfe57e7d2021-10-15 11:07:09 +01002742 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002743 *
2744 * @return 0 if there is no overlap; nonzero otherwise.
Tamas Banfe031092020-09-10 17:32:39 +02002745 */
2746static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002747boot_check_ram_load_overlapping(struct boot_loader_state *state)
Tamas Banfe031092020-09-10 17:32:39 +02002748{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002749 uint32_t i;
2750
2751 uint32_t start_a;
2752 uint32_t end_a;
2753 uint32_t start_b;
2754 uint32_t end_b;
Raef Colesfe57e7d2021-10-15 11:07:09 +01002755 uint32_t image_id_to_check = BOOT_CURR_IMG(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002756
Raef Colesfe57e7d2021-10-15 11:07:09 +01002757 start_a = state->slot_usage[image_id_to_check].img_dst;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002758 /* Safe to add here, values are already verified in
2759 * boot_verify_ram_load_address() */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002760 end_a = start_a + state->slot_usage[image_id_to_check].img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002761
2762 for (i = 0; i < BOOT_IMAGE_NUMBER; i++) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002763 if (state->slot_usage[i].active_slot == NO_ACTIVE_SLOT
Mark Horvathccaf7f82021-01-04 18:16:42 +01002764 || i == image_id_to_check) {
2765 continue;
2766 }
2767
Raef Colesfe57e7d2021-10-15 11:07:09 +01002768 start_b = state->slot_usage[i].img_dst;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002769 /* Safe to add here, values are already verified in
2770 * boot_verify_ram_load_address() */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002771 end_b = start_b + state->slot_usage[i].img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002772
2773 if (do_regions_overlap(start_a, end_a, start_b, end_b)) {
2774 return -1;
2775 }
2776 }
2777
2778 return 0;
2779}
2780#endif
2781
2782/**
2783 * Loads the active slot of the current image into SRAM. The load address and
2784 * image size is extracted from the image header.
2785 *
2786 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002787 *
2788 * @return 0 on success; nonzero on failure.
2789 */
2790static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002791boot_load_image_to_sram(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002792{
2793 uint32_t active_slot;
2794 struct image_header *hdr = NULL;
2795 uint32_t img_dst;
2796 uint32_t img_sz;
Tamas Banfe031092020-09-10 17:32:39 +02002797 int rc;
2798
Raef Colesfe57e7d2021-10-15 11:07:09 +01002799 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002800 hdr = boot_img_hdr(state, active_slot);
2801
Tamas Banfe031092020-09-10 17:32:39 +02002802 if (hdr->ih_flags & IMAGE_F_RAM_LOAD) {
2803
Mark Horvathccaf7f82021-01-04 18:16:42 +01002804 img_dst = hdr->ih_load_addr;
Tamas Banfe031092020-09-10 17:32:39 +02002805
Mark Horvathccaf7f82021-01-04 18:16:42 +01002806 rc = boot_read_image_size(state, active_slot, &img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002807 if (rc != 0) {
2808 return rc;
2809 }
2810
Raef Colesfe57e7d2021-10-15 11:07:09 +01002811 state->slot_usage[BOOT_CURR_IMG(state)].img_dst = img_dst;
2812 state->slot_usage[BOOT_CURR_IMG(state)].img_sz = img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002813
Raef Colesfe57e7d2021-10-15 11:07:09 +01002814 rc = boot_verify_ram_load_address(state);
Tamas Banfe031092020-09-10 17:32:39 +02002815 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002816 BOOT_LOG_INF("Image RAM load address 0x%x is invalid.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002817 return rc;
2818 }
2819
Mark Horvathccaf7f82021-01-04 18:16:42 +01002820#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesfe57e7d2021-10-15 11:07:09 +01002821 rc = boot_check_ram_load_overlapping(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002822 if (rc != 0) {
2823 BOOT_LOG_INF("Image RAM loading to address 0x%x would overlap with\
2824 another image.", img_dst);
2825 return rc;
2826 }
2827#endif
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002828#ifdef MCUBOOT_ENC_IMAGES
2829 /* decrypt image if encrypted and copy it to RAM */
2830 if (IS_ENCRYPTED(hdr)) {
2831 rc = boot_decrypt_and_copy_image_to_sram(state, active_slot, hdr, img_sz, img_dst);
2832 } else {
2833 rc = boot_copy_image_to_sram(state, active_slot, img_dst, img_sz);
2834 }
2835#else
Tamas Banfe031092020-09-10 17:32:39 +02002836 /* Copy image to the load address from where it currently resides in
2837 * flash.
2838 */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002839 rc = boot_copy_image_to_sram(state, active_slot, img_dst, img_sz);
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002840#endif
Tamas Banfe031092020-09-10 17:32:39 +02002841 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002842 BOOT_LOG_INF("RAM loading to 0x%x is failed.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002843 } else {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002844 BOOT_LOG_INF("RAM loading to 0x%x is succeeded.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002845 }
2846 } else {
2847 /* Only images that support IMAGE_F_RAM_LOAD are allowed if
2848 * MCUBOOT_RAM_LOAD is set.
2849 */
2850 rc = BOOT_EBADIMAGE;
2851 }
2852
Mark Horvathccaf7f82021-01-04 18:16:42 +01002853 if (rc != 0) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002854 state->slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
2855 state->slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002856 }
2857
Tamas Banfe031092020-09-10 17:32:39 +02002858 return rc;
2859}
2860
2861/**
2862 * Removes an image from SRAM, by overwriting it with zeros.
2863 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002864 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002865 *
2866 * @return 0 on success; nonzero on failure.
2867 */
2868static inline int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002869boot_remove_image_from_sram(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002870{
David Brown695e5912021-05-24 16:58:01 -06002871 (void)state;
2872
Mark Horvathccaf7f82021-01-04 18:16:42 +01002873 BOOT_LOG_INF("Removing image from SRAM at address 0x%x",
Raef Colesfe57e7d2021-10-15 11:07:09 +01002874 state->slot_usage[BOOT_CURR_IMG(state)].img_dst);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002875
Raef Colesfe57e7d2021-10-15 11:07:09 +01002876 memset((void*)(IMAGE_RAM_BASE + state->slot_usage[BOOT_CURR_IMG(state)].img_dst),
2877 0, state->slot_usage[BOOT_CURR_IMG(state)].img_sz);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002878
Raef Colesfe57e7d2021-10-15 11:07:09 +01002879 state->slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
2880 state->slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002881
2882 return 0;
2883}
2884
2885/**
2886 * Removes an image from flash by erasing the corresponding flash area
2887 *
2888 * @param state Boot loader status information.
2889 * @param slot The flash slot of the image to be erased.
Tamas Banfe031092020-09-10 17:32:39 +02002890 *
2891 * @return 0 on success; nonzero on failure.
2892 */
2893static inline int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002894boot_remove_image_from_flash(struct boot_loader_state *state, uint32_t slot)
Tamas Banfe031092020-09-10 17:32:39 +02002895{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002896 int area_id;
2897 int rc;
2898 const struct flash_area *fap;
Tamas Banfe031092020-09-10 17:32:39 +02002899
David Brown695e5912021-05-24 16:58:01 -06002900 (void)state;
2901
Mark Horvathccaf7f82021-01-04 18:16:42 +01002902 BOOT_LOG_INF("Removing image %d slot %d from flash", BOOT_CURR_IMG(state),
2903 slot);
2904 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2905 rc = flash_area_open(area_id, &fap);
2906 if (rc == 0) {
Dominik Ermel260ae092021-04-23 05:38:45 +00002907 flash_area_erase(fap, 0, flash_area_get_size(fap));
Dominik Ermel245de812022-09-02 13:39:23 +00002908 flash_area_close(fap);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002909 }
2910
2911 return rc;
Tamas Banfe031092020-09-10 17:32:39 +02002912}
2913#endif /* MCUBOOT_RAM_LOAD */
2914
Mark Horvathccaf7f82021-01-04 18:16:42 +01002915#if (BOOT_IMAGE_NUMBER > 1)
2916/**
2917 * Checks the image dependency whether it is satisfied.
2918 *
2919 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002920 * @param dep Image dependency which has to be verified.
2921 *
2922 * @return 0 if dependencies are met; nonzero otherwise.
2923 */
2924static int
2925boot_verify_slot_dependency(struct boot_loader_state *state,
Mark Horvathccaf7f82021-01-04 18:16:42 +01002926 struct image_dependency *dep)
David Vinczee574f2d2020-07-10 11:42:03 +02002927{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002928 struct image_version *dep_version;
2929 uint32_t dep_slot;
David Vinczee574f2d2020-07-10 11:42:03 +02002930 int rc;
2931
Mark Horvathccaf7f82021-01-04 18:16:42 +01002932 /* Determine the source of the image which is the subject of
2933 * the dependency and get it's version.
David Vinczee574f2d2020-07-10 11:42:03 +02002934 */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002935 dep_slot = state->slot_usage[dep->image_id].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002936 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
2937
2938 rc = boot_version_cmp(dep_version, &dep->image_min_version);
2939 if (rc >= 0) {
2940 /* Dependency satisfied. */
2941 rc = 0;
David Vinczee574f2d2020-07-10 11:42:03 +02002942 }
2943
Mark Horvathccaf7f82021-01-04 18:16:42 +01002944 return rc;
2945}
2946
2947/**
2948 * Reads all dependency TLVs of an image and verifies one after another to see
2949 * if they are all satisfied.
2950 *
2951 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002952 *
2953 * @return 0 if dependencies are met; nonzero otherwise.
2954 */
2955static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002956boot_verify_slot_dependencies(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002957{
2958 uint32_t active_slot;
2959 const struct flash_area *fap;
2960 struct image_tlv_iter it;
2961 struct image_dependency dep;
2962 uint32_t off;
2963 uint16_t len;
2964 int area_id;
2965 int rc;
2966
Raef Colesfe57e7d2021-10-15 11:07:09 +01002967 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002968
2969 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state),
2970 active_slot);
2971 rc = flash_area_open(area_id, &fap);
David Vinczee574f2d2020-07-10 11:42:03 +02002972 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002973 rc = BOOT_EFLASH;
2974 goto done;
David Vinczee574f2d2020-07-10 11:42:03 +02002975 }
2976
Mark Horvathccaf7f82021-01-04 18:16:42 +01002977 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, active_slot), fap,
2978 IMAGE_TLV_DEPENDENCY, true);
2979 if (rc != 0) {
2980 goto done;
2981 }
David Vinczee574f2d2020-07-10 11:42:03 +02002982
Mark Horvathccaf7f82021-01-04 18:16:42 +01002983 while (true) {
2984 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
2985 if (rc < 0) {
2986 return -1;
2987 } else if (rc > 0) {
2988 rc = 0;
2989 break;
2990 }
David Vinczee574f2d2020-07-10 11:42:03 +02002991
Mark Horvathccaf7f82021-01-04 18:16:42 +01002992 if (len != sizeof(dep)) {
2993 rc = BOOT_EBADIMAGE;
2994 goto done;
2995 }
2996
2997 rc = LOAD_IMAGE_DATA(boot_img_hdr(state, active_slot),
2998 fap, off, &dep, len);
2999 if (rc != 0) {
3000 rc = BOOT_EFLASH;
3001 goto done;
3002 }
3003
3004 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
3005 rc = BOOT_EBADARGS;
3006 goto done;
3007 }
3008
Raef Colesfe57e7d2021-10-15 11:07:09 +01003009 rc = boot_verify_slot_dependency(state, &dep);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003010 if (rc != 0) {
3011 /* Dependency not satisfied. */
3012 goto done;
3013 }
3014 }
3015
3016done:
3017 flash_area_close(fap);
3018 return rc;
3019}
3020
3021/**
3022 * Checks the dependency of all the active slots. If an image found with
3023 * invalid or not satisfied dependencies the image is removed from SRAM (in
3024 * case of MCUBOOT_RAM_LOAD strategy) and its slot is set to unavailable.
3025 *
3026 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01003027 *
3028 * @return 0 if dependencies are met; nonzero otherwise.
3029 */
3030static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01003031boot_verify_dependencies(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003032{
3033 int rc = -1;
3034 uint32_t active_slot;
3035
3036 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01003037 if (state->img_mask[BOOT_CURR_IMG(state)]) {
3038 continue;
3039 }
Raef Colesfe57e7d2021-10-15 11:07:09 +01003040 rc = boot_verify_slot_dependencies(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003041 if (rc != 0) {
3042 /* Dependencies not met or invalid dependencies. */
3043
3044#ifdef MCUBOOT_RAM_LOAD
Raef Colesfe57e7d2021-10-15 11:07:09 +01003045 boot_remove_image_from_sram(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003046#endif /* MCUBOOT_RAM_LOAD */
3047
Raef Colesfe57e7d2021-10-15 11:07:09 +01003048 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
3049 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3050 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003051
3052 return rc;
3053 }
3054 }
3055
3056 return rc;
3057}
3058#endif /* (BOOT_IMAGE_NUMBER > 1) */
3059
3060/**
3061 * Tries to load a slot for all the images with validation.
3062 *
3063 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01003064 *
3065 * @return 0 on success; nonzero on failure.
3066 */
Michael Grand5047f032022-11-24 16:49:56 +01003067fih_ret
Raef Colesfe57e7d2021-10-15 11:07:09 +01003068boot_load_and_validate_images(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003069{
3070 uint32_t active_slot;
3071 int rc;
Michael Grand5047f032022-11-24 16:49:56 +01003072 fih_ret fih_rc;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003073
3074 /* Go over all the images and try to load one */
3075 IMAGES_ITER(BOOT_CURR_IMG(state)) {
3076 /* All slots tried until a valid image found. Breaking from this loop
3077 * means that a valid image found or already loaded. If no slot is
3078 * found the function returns with error code. */
3079 while (true) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01003080 /* Go over all the slots and try to load one */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003081 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003082 if (active_slot != NO_ACTIVE_SLOT){
3083 /* A slot is already active, go to next image. */
3084 break;
David Vinczee574f2d2020-07-10 11:42:03 +02003085 }
David Vincze505fba22020-10-22 13:53:29 +02003086
Raef Colesfe57e7d2021-10-15 11:07:09 +01003087 active_slot = find_slot_with_highest_version(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003088 if (active_slot == NO_ACTIVE_SLOT) {
3089 BOOT_LOG_INF("No slot to load for image %d",
3090 BOOT_CURR_IMG(state));
3091 FIH_RET(FIH_FAILURE);
3092 }
3093
3094 /* Save the number of the active slot. */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003095 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003096
Raef Colesf11de642021-10-15 11:11:56 +01003097#if BOOT_IMAGE_NUMBER > 1
3098 if (state->img_mask[BOOT_CURR_IMG(state)]) {
3099 continue;
3100 }
3101#endif
3102
Mark Horvathccaf7f82021-01-04 18:16:42 +01003103#ifdef MCUBOOT_DIRECT_XIP
Raef Colesfe57e7d2021-10-15 11:07:09 +01003104 rc = boot_rom_address_check(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003105 if (rc != 0) {
3106 /* The image is placed in an unsuitable slot. */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003107 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3108 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003109 continue;
3110 }
George Becksteind4d90f82021-05-11 02:00:00 -04003111
David Vincze505fba22020-10-22 13:53:29 +02003112#ifdef MCUBOOT_DIRECT_XIP_REVERT
Raef Colesfe57e7d2021-10-15 11:07:09 +01003113 rc = boot_select_or_erase(state);
David Vincze505fba22020-10-22 13:53:29 +02003114 if (rc != 0) {
3115 /* The selected image slot has been erased. */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003116 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3117 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
David Vincze505fba22020-10-22 13:53:29 +02003118 continue;
3119 }
3120#endif /* MCUBOOT_DIRECT_XIP_REVERT */
David Vincze1c456242021-06-29 15:25:24 +02003121#endif /* MCUBOOT_DIRECT_XIP */
David Vincze505fba22020-10-22 13:53:29 +02003122
Tamas Banfe031092020-09-10 17:32:39 +02003123#ifdef MCUBOOT_RAM_LOAD
3124 /* Image is first loaded to RAM and authenticated there in order to
3125 * prevent TOCTOU attack during image copy. This could be applied
3126 * when loading images from external (untrusted) flash to internal
3127 * (trusted) RAM and image is authenticated before copying.
3128 */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003129 rc = boot_load_image_to_sram(state);
Tamas Banfe031092020-09-10 17:32:39 +02003130 if (rc != 0 ) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01003131 /* Image cannot be ramloaded. */
3132 boot_remove_image_from_flash(state, active_slot);
Raef Colesfe57e7d2021-10-15 11:07:09 +01003133 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3134 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Tamas Banfe031092020-09-10 17:32:39 +02003135 continue;
Tamas Banfe031092020-09-10 17:32:39 +02003136 }
3137#endif /* MCUBOOT_RAM_LOAD */
Mark Horvathccaf7f82021-01-04 18:16:42 +01003138
3139 FIH_CALL(boot_validate_slot, fih_rc, state, active_slot, NULL);
Michael Grand5047f032022-11-24 16:49:56 +01003140 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01003141 /* Image is invalid. */
Tamas Banfe031092020-09-10 17:32:39 +02003142#ifdef MCUBOOT_RAM_LOAD
Raef Colesfe57e7d2021-10-15 11:07:09 +01003143 boot_remove_image_from_sram(state);
Tamas Banfe031092020-09-10 17:32:39 +02003144#endif /* MCUBOOT_RAM_LOAD */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003145 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3146 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003147 continue;
David Vincze505fba22020-10-22 13:53:29 +02003148 }
Mark Horvathccaf7f82021-01-04 18:16:42 +01003149
3150 /* Valid image loaded from a slot, go to next image. */
3151 break;
3152 }
3153 }
3154
3155 FIH_RET(FIH_SUCCESS);
3156}
3157
3158/**
3159 * Updates the security counter for the current image.
3160 *
3161 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01003162 *
3163 * @return 0 on success; nonzero on failure.
3164 */
3165static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01003166boot_update_hw_rollback_protection(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003167{
3168#ifdef MCUBOOT_HW_ROLLBACK_PROT
3169 int rc;
3170
3171 /* Update the stored security counter with the newer (active) image's
3172 * security counter value.
3173 */
David Vincze1c456242021-06-29 15:25:24 +02003174#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003175 /* When the 'revert' mechanism is enabled in direct-xip mode, the
3176 * security counter can be increased only after reboot, if the image
3177 * has been confirmed at runtime (the image_ok flag has been set).
3178 * This way a 'revert' can be performed when it's necessary.
3179 */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003180 if (state->slot_usage[BOOT_CURR_IMG(state)].swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze505fba22020-10-22 13:53:29 +02003181#endif
Sherry Zhang50b06ae2021-07-09 15:22:51 +08003182 rc = boot_update_security_counter(BOOT_CURR_IMG(state),
Raef Colesfe57e7d2021-10-15 11:07:09 +01003183 state->slot_usage[BOOT_CURR_IMG(state)].active_slot,
3184 boot_img_hdr(state, state->slot_usage[BOOT_CURR_IMG(state)].active_slot));
David Vinczee574f2d2020-07-10 11:42:03 +02003185 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01003186 BOOT_LOG_ERR("Security counter update failed after image "
3187 "validation.");
3188 return rc;
David Vinczee574f2d2020-07-10 11:42:03 +02003189 }
David Vincze1c456242021-06-29 15:25:24 +02003190#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003191 }
3192#endif
David Vinczee574f2d2020-07-10 11:42:03 +02003193
Mark Horvathccaf7f82021-01-04 18:16:42 +01003194 return 0;
David Vinczee574f2d2020-07-10 11:42:03 +02003195
Mark Horvathccaf7f82021-01-04 18:16:42 +01003196#else /* MCUBOOT_HW_ROLLBACK_PROT */
3197 (void) (state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003198 return 0;
3199#endif
3200}
3201
Michael Grand5047f032022-11-24 16:49:56 +01003202fih_ret
Mark Horvathccaf7f82021-01-04 18:16:42 +01003203context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
3204{
Mark Horvathccaf7f82021-01-04 18:16:42 +01003205 int rc;
Michael Grand5047f032022-11-24 16:49:56 +01003206 FIH_DECLARE(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003207
Raef Colesfe57e7d2021-10-15 11:07:09 +01003208 rc = boot_get_slot_usage(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003209 if (rc != 0) {
David Vinczee574f2d2020-07-10 11:42:03 +02003210 goto out;
3211 }
3212
Mark Horvathccaf7f82021-01-04 18:16:42 +01003213#if (BOOT_IMAGE_NUMBER > 1)
3214 while (true) {
3215#endif
Raef Colesfe57e7d2021-10-15 11:07:09 +01003216 FIH_CALL(boot_load_and_validate_images, fih_rc, state);
Michael Grand5047f032022-11-24 16:49:56 +01003217 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
3218 FIH_SET(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003219 goto out;
3220 }
3221
3222#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesfe57e7d2021-10-15 11:07:09 +01003223 rc = boot_verify_dependencies(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003224 if (rc != 0) {
3225 /* Dependency check failed for an image, it has been removed from
3226 * SRAM in case of MCUBOOT_RAM_LOAD strategy, and set to
3227 * unavailable. Try to load an image from another slot.
3228 */
3229 continue;
3230 }
3231 /* Dependency check was successful. */
3232 break;
3233 }
3234#endif
3235
3236 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01003237#if BOOT_IMAGE_NUMBER > 1
3238 if (state->img_mask[BOOT_CURR_IMG(state)]) {
3239 continue;
3240 }
3241#endif
Raef Colesfe57e7d2021-10-15 11:07:09 +01003242 rc = boot_update_hw_rollback_protection(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003243 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01003244 FIH_SET(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003245 goto out;
3246 }
3247
Raef Colesfe57e7d2021-10-15 11:07:09 +01003248 rc = boot_add_shared_data(state, state->slot_usage[BOOT_CURR_IMG(state)].active_slot);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003249 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01003250 FIH_SET(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003251 goto out;
3252 }
3253 }
3254
3255 /* All image loaded successfully. */
3256#ifdef MCUBOOT_HAVE_LOGGING
Raef Colesfe57e7d2021-10-15 11:07:09 +01003257 print_loaded_images(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003258#endif
3259
Raef Colesfe57e7d2021-10-15 11:07:09 +01003260 fill_rsp(state, rsp);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003261
David Vinczee574f2d2020-07-10 11:42:03 +02003262out:
Mark Horvathccaf7f82021-01-04 18:16:42 +01003263 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01003264
Michael Grand5047f032022-11-24 16:49:56 +01003265 if (rc != 0) {
3266 FIH_SET(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003267 }
Raef Colese8fe6cf2020-05-26 13:07:40 +01003268
Mark Horvathccaf7f82021-01-04 18:16:42 +01003269 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02003270}
Tamas Banfe031092020-09-10 17:32:39 +02003271#endif /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02003272
3273/**
Raef Colese8fe6cf2020-05-26 13:07:40 +01003274 * Prepares the booting process. This function moves images around in flash as
David Vinczee574f2d2020-07-10 11:42:03 +02003275 * appropriate, and tells you what address to boot from.
3276 *
3277 * @param rsp On success, indicates how booting should occur.
3278 *
Raef Colese8fe6cf2020-05-26 13:07:40 +01003279 * @return FIH_SUCCESS on success; nonzero on failure.
David Vinczee574f2d2020-07-10 11:42:03 +02003280 */
Michael Grand5047f032022-11-24 16:49:56 +01003281fih_ret
David Vinczee574f2d2020-07-10 11:42:03 +02003282boot_go(struct boot_rsp *rsp)
3283{
Michael Grand5047f032022-11-24 16:49:56 +01003284 FIH_DECLARE(fih_rc, FIH_FAILURE);
Raef Colesf11de642021-10-15 11:11:56 +01003285
3286 boot_state_clear(NULL);
3287
Raef Colese8fe6cf2020-05-26 13:07:40 +01003288 FIH_CALL(context_boot_go, fih_rc, &boot_data, rsp);
3289 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02003290}
Raef Colesf11de642021-10-15 11:11:56 +01003291
3292/**
3293 * Prepares the booting process, considering only a single image. This function
3294 * moves images around in flash as appropriate, and tells you what address to
3295 * boot from.
3296 *
3297 * @param rsp On success, indicates how booting should occur.
3298 *
3299 * @param image_id The image ID to prepare the boot process for.
3300 *
3301 * @return FIH_SUCCESS on success; nonzero on failure.
3302 */
Michael Grand5047f032022-11-24 16:49:56 +01003303fih_ret
Raef Colesf11de642021-10-15 11:11:56 +01003304boot_go_for_image_id(struct boot_rsp *rsp, uint32_t image_id)
3305{
Michael Grand5047f032022-11-24 16:49:56 +01003306 FIH_DECLARE(fih_rc, FIH_FAILURE);
Raef Colesf11de642021-10-15 11:11:56 +01003307
3308 if (image_id >= BOOT_IMAGE_NUMBER) {
3309 FIH_RET(FIH_FAILURE);
3310 }
3311
3312#if BOOT_IMAGE_NUMBER > 1
3313 memset(&boot_data.img_mask, 1, BOOT_IMAGE_NUMBER);
3314 boot_data.img_mask[image_id] = 0;
3315#endif
3316
3317 FIH_CALL(context_boot_go, fih_rc, &boot_data, rsp);
3318 FIH_RET(fih_rc);
3319}
3320
3321/**
3322 * Clears the boot state, so that previous operations have no effect on new
3323 * ones.
3324 *
3325 * @param state The state that should be cleared. If the value
3326 * is NULL, the default bootloader state will be
3327 * cleared.
3328 */
3329void boot_state_clear(struct boot_loader_state *state)
3330{
3331 if (state != NULL) {
3332 memset(state, 0, sizeof(struct boot_loader_state));
3333 } else {
3334 memset(&boot_data, 0, sizeof(struct boot_loader_state));
3335 }
3336}