blob: 8ede0f108aca51847a4689f84e5124f261efb921 [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
Roman Okhrimenko977b3752022-03-31 14:40:48 +03006 * 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"
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +030039#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"
Roman Okhrimenko977b3752022-03-31 14:40:48 +030047#include "bootutil/ramload.h"
48#include "bootutil/boot_hooks.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050049
Fabio Utzigba829042018-09-18 08:29:34 -030050#ifdef MCUBOOT_ENC_IMAGES
51#include "bootutil/enc_key.h"
52#endif
53
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +030054#if (!defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)) || defined(MCUBOOT_MULTI_MEMORY_LOAD)
Roman Okhrimenko977b3752022-03-31 14:40:48 +030055#include <os/os_malloc.h>
56#endif
57
Fabio Utzigba1fbe62017-07-21 14:01:20 -030058#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030059
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +020060#ifdef USE_IFX_SE_CRYPTO
61#include "ifx_se_utils.h"
62#endif /* USE_IFX_SE_CRYPTO */
63
Roman Okhrimenko977b3752022-03-31 14:40:48 +030064BOOT_LOG_MODULE_DECLARE(mcuboot);
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010065
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +020066bool boot_ram = false;
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040067static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080068
Fabio Utzigabec0732019-07-31 08:40:22 -030069#if (BOOT_IMAGE_NUMBER > 1)
70#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
71#else
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +030072#define IMAGES_ITER(x) for (int iter = 0; iter < 1; ++iter)
Fabio Utzigabec0732019-07-31 08:40:22 -030073#endif
74
Fabio Utzig10ee6482019-08-01 12:04:52 -030075/*
76 * This macro allows some control on the allocation of local variables.
77 * When running natively on a target, we don't want to allocated huge
78 * variables on the stack, so make them global instead. For the simulator
79 * we want to run as many threads as there are tests, and it's safer
80 * to just make those variables stack allocated.
81 */
82#if !defined(__BOOTSIM__)
83#define TARGET_STATIC static
84#else
85#define TARGET_STATIC
86#endif
87
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +030088#if BOOT_MAX_ALIGN > 1024
89#define BUF_SZ BOOT_MAX_ALIGN
90#else
91#define BUF_SZ 1024U
92#endif
93
94static fih_int FIH_SWAP_TYPE_NONE = FIH_INT_INIT(0x3A5C742E);
95
David Vinczee574f2d2020-07-10 11:42:03 +020096static int
97boot_read_image_headers(struct boot_loader_state *state, bool require_all,
98 struct boot_status *bs)
99{
100 int rc;
101 int i;
102
103 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300104 rc = BOOT_HOOK_CALL(boot_read_image_header_hook, BOOT_HOOK_REGULAR,
105 BOOT_CURR_IMG(state), i, boot_img_hdr(state, i));
106 if (rc == BOOT_HOOK_REGULAR)
107 {
108 rc = boot_read_image_header(state, i, boot_img_hdr(state, i), bs);
109 }
David Vinczee574f2d2020-07-10 11:42:03 +0200110 if (rc != 0) {
111 /* If `require_all` is set, fail on any single fail, otherwise
112 * if at least the first slot's header was read successfully,
113 * then the boot loader can attempt a boot.
114 *
115 * Failure to read any headers is a fatal error.
116 */
117 if (i > 0 && !require_all) {
118 return 0;
119 } else {
120 return rc;
121 }
122 }
123 }
124
125 return 0;
126}
127
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300128/**
129 * Saves boot status and shared data for current image.
130 *
131 * @param state Boot loader status information.
132 * @param active_slot Index of the slot will be loaded for current image.
133 *
134 * @return 0 on success; nonzero on failure.
135 */
136static int
137boot_add_shared_data(struct boot_loader_state *state,
138 uint32_t active_slot)
139{
140#if defined(MCUBOOT_MEASURED_BOOT) || defined(MCUBOOT_DATA_SHARING)
141 int rc;
142
143#ifdef MCUBOOT_MEASURED_BOOT
144 rc = boot_save_boot_status(BOOT_CURR_IMG(state),
145 boot_img_hdr(state, active_slot),
146 BOOT_IMG_AREA(state, active_slot));
147 if (rc != 0) {
148 BOOT_LOG_ERR("Failed to add image data to shared area");
149 return rc;
150 }
151#endif /* MCUBOOT_MEASURED_BOOT */
152
153#ifdef MCUBOOT_DATA_SHARING
154 rc = boot_save_shared_data(boot_img_hdr(state, active_slot),
155 BOOT_IMG_AREA(state, active_slot));
156 if (rc != 0) {
157 BOOT_LOG_ERR("Failed to add data to shared memory area.");
158 return rc;
159 }
160#endif /* MCUBOOT_DATA_SHARING */
161
162 return 0;
163
164#else /* MCUBOOT_MEASURED_BOOT || MCUBOOT_DATA_SHARING */
165 (void) (state);
166 (void) (active_slot);
167
168 return 0;
169#endif
170}
171
172/**
173 * Fills rsp to indicate how booting should occur.
174 *
175 * @param state Boot loader status information.
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300176 * @param rsp boot_rsp struct to fill.
177 */
178static void
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300179fill_rsp(struct boot_loader_state *state, struct boot_rsp *rsp)
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300180{
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300181 uint32_t active_slot = BOOT_PRIMARY_SLOT;
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300182
183#if (BOOT_IMAGE_NUMBER > 1)
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300184 /* Always boot from the first enabled image. */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300185 BOOT_CURR_IMG(state) = 0;
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300186 IMAGES_ITER(BOOT_CURR_IMG(state)) {
187 if (!state->img_mask[BOOT_CURR_IMG(state)]) {
188 break;
189 }
190 }
191 /* At least one image must be active, otherwise skip the execution */
192 if(BOOT_CURR_IMG(state) >= BOOT_IMAGE_NUMBER)
193 {
194 return;
195 }
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300196#endif
197
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300198#if defined(MCUBOOT_MULTI_MEMORY_LOAD)
199 if ((state->slot_usage[BOOT_CURR_IMG(state)].active_slot != BOOT_PRIMARY_SLOT) &&
200 (state->slot_usage[BOOT_CURR_IMG(state)].active_slot != NO_ACTIVE_SLOT))
201#endif
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300202#if defined(MCUBOOT_DIRECT_XIP) || defined(MCUBOOT_RAM_LOAD)
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300203 {
204 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
205 }
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300206#endif
207
208 rsp->br_flash_dev_id = flash_area_get_device_id(BOOT_IMG_AREA(state, active_slot));
209 rsp->br_image_off = boot_img_slot_off(state, active_slot);
210 rsp->br_hdr = boot_img_hdr(state, active_slot);
211}
212
213/**
214 * Closes all flash areas.
215 *
216 * @param state Boot loader status information.
217 */
218static void
219close_all_flash_areas(struct boot_loader_state *state)
220{
221 uint32_t slot;
222
223 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300224#if BOOT_IMAGE_NUMBER > 1
225 if (state->img_mask[BOOT_CURR_IMG(state)]) {
226 continue;
227 }
228#endif
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300229#if MCUBOOT_SWAP_USING_SCRATCH
230 flash_area_close(BOOT_SCRATCH_AREA(state));
231#endif
232 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
233 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
234 }
235 }
236}
237
Tamas Banfe031092020-09-10 17:32:39 +0200238#if !defined(MCUBOOT_DIRECT_XIP)
David Brownf5b33d82017-09-01 10:58:27 -0600239/*
240 * Compute the total size of the given image. Includes the size of
241 * the TLVs.
242 */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300243#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST) || defined(MCUBOOT_RAM_LOAD)
David Brownf5b33d82017-09-01 10:58:27 -0600244static int
Fabio Utzigd638b172019-08-09 10:38:05 -0300245boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
David Brownf5b33d82017-09-01 10:58:27 -0600246{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300247 const struct flash_area *fap = NULL;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300248 struct image_tlv_info info;
Fabio Utzig233af7d2019-08-26 12:06:16 -0300249 uint32_t off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300250 uint32_t protect_tlv_size;
David Brownf5b33d82017-09-01 10:58:27 -0600251 int area_id;
252 int rc;
253
Fabio Utzig10ee6482019-08-01 12:04:52 -0300254#if (BOOT_IMAGE_NUMBER == 1)
255 (void)state;
256#endif
257
258 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
David Brownf5b33d82017-09-01 10:58:27 -0600259 rc = flash_area_open(area_id, &fap);
260 if (rc != 0) {
261 rc = BOOT_EFLASH;
262 goto done;
263 }
264
Fabio Utzig61fd8882019-09-14 20:00:20 -0300265 off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
266
267 if (flash_area_read(fap, off, &info, sizeof(info))) {
268 rc = BOOT_EFLASH;
David Brownf5b33d82017-09-01 10:58:27 -0600269 goto done;
270 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300271
Fabio Utzige52c08e2019-09-11 19:32:00 -0300272 protect_tlv_size = boot_img_hdr(state, slot)->ih_protect_tlv_size;
273 if (info.it_magic == IMAGE_TLV_PROT_INFO_MAGIC) {
274 if (protect_tlv_size != info.it_tlv_tot) {
275 rc = BOOT_EBADIMAGE;
276 goto done;
277 }
278
279 if (flash_area_read(fap, off + info.it_tlv_tot, &info, sizeof(info))) {
280 rc = BOOT_EFLASH;
281 goto done;
282 }
283 } else if (protect_tlv_size != 0) {
284 rc = BOOT_EBADIMAGE;
285 goto done;
286 }
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300287 else
288 {
289 /* acc. to MISRA R.15.7 */
290 }
Fabio Utzige52c08e2019-09-11 19:32:00 -0300291
Fabio Utzig61fd8882019-09-14 20:00:20 -0300292 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
293 rc = BOOT_EBADIMAGE;
294 goto done;
295 }
296
Fabio Utzige52c08e2019-09-11 19:32:00 -0300297 *size = off + protect_tlv_size + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600298 rc = 0;
299
300done:
301 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300302 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600303}
304
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300305#endif
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200306
David Brownab449182019-11-15 09:32:52 -0700307static uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300308boot_write_sz(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800309{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300310 size_t elem_sz;
Fabio Utzig12d59162019-11-28 10:01:59 -0300311#if MCUBOOT_SWAP_USING_SCRATCH
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300312 size_t align;
Fabio Utzig12d59162019-11-28 10:01:59 -0300313#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800314
315 /* Figure out what size to write update status update as. The size depends
316 * on what the minimum write size is for scratch area, active image slot.
317 * We need to use the bigger of those 2 values.
318 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300319
Fabio Utzig10ee6482019-08-01 12:04:52 -0300320 elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300321 assert(elem_sz != 0u);
322
Fabio Utzig12d59162019-11-28 10:01:59 -0300323#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300324 align = flash_area_align(BOOT_SCRATCH_AREA(state));
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300325 assert(align != 0u);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800326 if (align > elem_sz) {
327 elem_sz = align;
328 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300329#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800330
331 return elem_sz;
332}
333
Fabio Utzig10ee6482019-08-01 12:04:52 -0300334static int
335boot_initialize_area(struct boot_loader_state *state, int flash_area)
336{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300337 uint32_t num_sectors = BOOT_MAX_IMG_SECTORS;
338 boot_sector_t *out_sectors;
Fabio Utzig10ee6482019-08-01 12:04:52 -0300339 size_t *out_num_sectors;
340 int rc;
341
342 num_sectors = BOOT_MAX_IMG_SECTORS;
343
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300344 if (flash_area == (int) FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300345 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
346 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300347 } else if (flash_area == (int) FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300348 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
349 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300350#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300351 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
352 out_sectors = state->scratch.sectors;
353 out_num_sectors = &state->scratch.num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300354#endif
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200355#if MCUBOOT_SWAP_USING_STATUS
356 } else if (flash_area == FLASH_AREA_IMAGE_SWAP_STATUS) {
357 out_sectors = state->status.sectors;
358 out_num_sectors = &state->status.num_sectors;
359#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -0300360 } else {
361 return BOOT_EFLASH;
362 }
363
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300364#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
Fabio Utzig10ee6482019-08-01 12:04:52 -0300365 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300366#else
367 _Static_assert(sizeof(int) <= sizeof(uint32_t), "Fix needed");
368 rc = flash_area_to_sectors(flash_area, (int *)&num_sectors, out_sectors);
369#endif /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300370 if (rc != 0) {
371 return rc;
372 }
373 *out_num_sectors = num_sectors;
374 return 0;
375}
Fabio Utzig10ee6482019-08-01 12:04:52 -0300376
Christopher Collins92ea77f2016-12-12 15:59:26 -0800377/**
378 * Determines the sector layout of both image slots and the scratch area.
379 * This information is necessary for calculating the number of bytes to erase
380 * and copy during an image swap. The information collected during this
Fabio Utzig10ee6482019-08-01 12:04:52 -0300381 * function is used to populate the state.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800382 */
383static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300384boot_read_sectors(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800385{
Fabio Utzigb0f04732019-07-31 09:49:19 -0300386 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800387 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800388
Fabio Utzig10ee6482019-08-01 12:04:52 -0300389 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300390
391 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800392 if (rc != 0) {
393 return BOOT_EFLASH;
394 }
395
Fabio Utzig10ee6482019-08-01 12:04:52 -0300396 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800397 if (rc != 0) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300398 /* We need to differentiate from the primary image issue */
399 return BOOT_EFLASH_SEC;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800400 }
401
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200402#if MCUBOOT_SWAP_USING_STATUS
403 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SWAP_STATUS);
404 if (rc != 0) {
405 return BOOT_EFLASH;
406 }
407#endif
408
Fabio Utzig12d59162019-11-28 10:01:59 -0300409#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300410 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200411 if (rc != 0) {
412 return BOOT_EFLASH;
413 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300414#endif
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200415
Fabio Utzig10ee6482019-08-01 12:04:52 -0300416 BOOT_WRITE_SZ(state) = boot_write_sz(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800417
418 return 0;
419}
420
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200421static void
Fabio Utzig12d59162019-11-28 10:01:59 -0300422boot_status_reset(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800423{
Fabio Utzig4741c452019-12-19 15:32:41 -0300424#ifdef MCUBOOT_ENC_IMAGES
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300425 (void)memset(&bs->enckey, BOOT_UNINITIALIZED_KEY_FILL,
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300426 BOOT_NUM_SLOTS * BOOT_ENC_KEY_ALIGN_SIZE);
427#ifdef MCUBOOT_SWAP_SAVE_ENCTLV
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300428 (void)memset(&bs->enctlv, BOOT_UNINITIALIZED_TLV_FILL,
429 BOOT_NUM_SLOTS * BOOT_ENC_TLV_ALIGN_SIZE);
Fabio Utzig4741c452019-12-19 15:32:41 -0300430#endif
431#endif /* MCUBOOT_ENC_IMAGES */
432
433 bs->use_scratch = 0;
434 bs->swap_size = 0;
435 bs->source = 0;
436
Fabio Utzig74aef312019-11-28 11:05:34 -0300437 bs->op = BOOT_STATUS_OP_MOVE;
Fabio Utzig39000012018-07-30 12:40:20 -0300438 bs->idx = BOOT_STATUS_IDX_0;
439 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700440 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Fabio Utzig12d59162019-11-28 10:01:59 -0300441}
Christopher Collins92ea77f2016-12-12 15:59:26 -0800442
Fabio Utzig12d59162019-11-28 10:01:59 -0300443bool
444boot_status_is_reset(const struct boot_status *bs)
445{
Fabio Utzig74aef312019-11-28 11:05:34 -0300446 return (bs->op == BOOT_STATUS_OP_MOVE &&
447 bs->idx == BOOT_STATUS_IDX_0 &&
448 bs->state == BOOT_STATUS_STATE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800449}
450
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200451#ifndef MCUBOOT_SWAP_USING_STATUS
Christopher Collins92ea77f2016-12-12 15:59:26 -0800452/**
453 * Writes the supplied boot status to the flash file system. The boot status
454 * contains the current state of an in-progress image copy operation.
455 *
456 * @param bs The boot status to write.
457 *
458 * @return 0 on success; nonzero on failure.
459 */
460int
Fabio Utzig12d59162019-11-28 10:01:59 -0300461boot_write_status(const struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800462{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300463 const struct flash_area *fap = NULL;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800464 uint32_t off;
465 int area_id;
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300466 int rc = 0;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300467 uint8_t buf[BOOT_MAX_ALIGN];
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300468 uint32_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300469 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800470
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300471 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100472 * the trailer. Since in the last step the primary slot is erased, the
473 * first two status writes go to the scratch which will be copied to
474 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300475 */
476
Fabio Utzig12d59162019-11-28 10:01:59 -0300477#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig2473ac02017-05-02 12:45:02 -0300478 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800479 /* Write to scratch. */
480 area_id = FLASH_AREA_IMAGE_SCRATCH;
481 } else {
Fabio Utzig12d59162019-11-28 10:01:59 -0300482#endif
David Vincze2d736ad2019-02-18 11:50:22 +0100483 /* Write to the primary slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300484 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Fabio Utzig12d59162019-11-28 10:01:59 -0300485#if MCUBOOT_SWAP_USING_SCRATCH
Christopher Collins92ea77f2016-12-12 15:59:26 -0800486 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300487#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800488
489 rc = flash_area_open(area_id, &fap);
490 if (rc != 0) {
491 rc = BOOT_EFLASH;
492 goto done;
493 }
494
495 off = boot_status_off(fap) +
Fabio Utzig12d59162019-11-28 10:01:59 -0300496 boot_status_internal_off(bs, BOOT_WRITE_SZ(state));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200497 align = flash_area_align(fap);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300498 if (align == 0u) {
499 rc = BOOT_EFLASH;
500 goto done;
501 }
502
Fabio Utzig39000012018-07-30 12:40:20 -0300503 erased_val = flash_area_erased_val(fap);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300504 (void)memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700505 buf[0] = bs->state;
506
507 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800508 if (rc != 0) {
509 rc = BOOT_EFLASH;
510 goto done;
511 }
512
513 rc = 0;
514
515done:
516 flash_area_close(fap);
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300517
Christopher Collins92ea77f2016-12-12 15:59:26 -0800518 return rc;
519}
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200520#endif /* MCUBOOT_SWAP_USING_STATUS */
521
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300522
David Vinczee574f2d2020-07-10 11:42:03 +0200523#endif /* !MCUBOOT_DIRECT_XIP */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800524
525/*
David Vinczec3084132020-02-18 14:50:47 +0100526 * Validate image hash/signature and optionally the security counter in a slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800527 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100528static fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300529boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
530 const struct flash_area *fap, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800531{
Fabio Utzig10ee6482019-08-01 12:04:52 -0300532 TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigb0f04732019-07-31 09:49:19 -0300533 uint8_t image_index;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100534 fih_int fih_rc = FIH_FAILURE;
Fabio Utzigba829042018-09-18 08:29:34 -0300535
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200536#ifdef USE_IFX_SE_CRYPTO
537 fih_uint fih_complex_result = FIH_UINT_ZERO;
538 extern fih_uint IFX_FIH_IMG_VALIDATE_COMPLEX_OK;
539#else
540 fih_int fih_complex_result = FIH_FAILURE;
541 extern fih_int FIH_IMG_VALIDATE_COMPLEX_OK;
542#endif /* USE_IFX_SE_CRYPTO */
543
Fabio Utzig10ee6482019-08-01 12:04:52 -0300544#if (BOOT_IMAGE_NUMBER == 1)
545 (void)state;
546#endif
547
Fabio Utzigba829042018-09-18 08:29:34 -0300548 (void)bs;
Fabio Utzigbc077932019-08-26 11:16:34 -0300549
550 image_index = BOOT_CURR_IMG(state);
551
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300552/* In the case of ram loading the image has already been decrypted as it is
553 * decrypted when copied in ram */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300554#if defined(MCUBOOT_ENC_IMAGES)
555 if (MUST_DECRYPT(fap, image_index, hdr) && !IS_RAM_BOOTABLE(hdr)) {
556 int rc = flash_area_id_to_multi_image_slot(image_index, fap->fa_id);
Fabio Utzigba829042018-09-18 08:29:34 -0300557 if (rc < 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100558 FIH_RET(fih_rc);
Fabio Utzigba829042018-09-18 08:29:34 -0300559 }
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300560 else {
561 uint8_t slot = (uint8_t)rc;
562
563 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
564 if (rc < 0) {
565 FIH_RET(fih_rc);
566 }
567 if (0 == rc && boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs)) {
568 FIH_RET(fih_rc);
569 }
Fabio Utzigba829042018-09-18 08:29:34 -0300570 }
571 }
Fabio Utzigbc077932019-08-26 11:16:34 -0300572#endif
573
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200574#ifdef USE_IFX_SE_CRYPTO
575 FIH_UCALL(bootutil_psa_img_validate, fih_complex_result, \
576 BOOT_CURR_ENC(state), image_index, hdr, fap, \
577 tmpbuf, BOOT_TMPBUF_SZ, NULL, 0);
578
579 BOOT_LOG_DBG(" * bootutil_psa_img_validate expected = 0x%x, " \
580 "returned = 0x%x", \
581 fih_uint_decode(IFX_FIH_IMG_VALIDATE_COMPLEX_OK), \
582 fih_uint_decode(fih_complex_result));
583
584 if (FIH_TRUE == fih_uint_eq(fih_complex_result,
585 IFX_FIH_IMG_VALIDATE_COMPLEX_OK)) {
586 fih_rc = fih_int_encode_zero_equality(
587 fih_uint_decode(IFX_FIH_IMG_VALIDATE_COMPLEX_OK) &
588 ~fih_uint_decode(fih_complex_result));
589 }
590 else {
591 fih_rc = FIH_FAILURE;
592 }
593#else
594 FIH_CALL(bootutil_img_validate, fih_complex_result, BOOT_CURR_ENC(state), image_index,
Raef Colese8fe6cf2020-05-26 13:07:40 +0100595 hdr, fap, tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, NULL);
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200596 BOOT_LOG_DBG(" * bootutil_img_validate expected = 0x%x, " \
597 "returned = 0x%x", \
598 fih_int_decode(FIH_IMG_VALIDATE_COMPLEX_OK), \
599 fih_int_decode(fih_complex_result));
600
601 if (FIH_TRUE == fih_eq(fih_complex_result,
602 FIH_IMG_VALIDATE_COMPLEX_OK)) {
603 fih_rc = fih_int_encode_zero_equality(
604 fih_int_decode(FIH_IMG_VALIDATE_COMPLEX_OK) &
605 ~fih_int_decode(fih_complex_result));
606 }
607 else {
608 fih_rc = FIH_FAILURE;
609 }
610
611#endif /* USE_IFX_SE_CRYPTO */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300612
Raef Colese8fe6cf2020-05-26 13:07:40 +0100613 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800614}
615
Raef Colese8fe6cf2020-05-26 13:07:40 +0100616static fih_int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800617split_image_check(struct image_header *app_hdr,
618 const struct flash_area *app_fap,
619 struct image_header *loader_hdr,
620 const struct flash_area *loader_fap)
621{
622 static void *tmpbuf;
623 uint8_t loader_hash[32];
Raef Colese8fe6cf2020-05-26 13:07:40 +0100624 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800625
626 if (!tmpbuf) {
627 tmpbuf = malloc(BOOT_TMPBUF_SZ);
628 if (!tmpbuf) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100629 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800630 }
631 }
632
Raef Colese8fe6cf2020-05-26 13:07:40 +0100633 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, loader_hdr, loader_fap,
634 tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, loader_hash);
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300635 if (fih_eq(fih_rc, FIH_SUCCESS) != FIH_TRUE) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100636 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800637 }
638
Raef Colese8fe6cf2020-05-26 13:07:40 +0100639 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, app_hdr, app_fap,
640 tmpbuf, BOOT_TMPBUF_SZ, loader_hash, 32, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800641
Raef Colese8fe6cf2020-05-26 13:07:40 +0100642out:
643 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800644}
645
Fabio Utzig338a19f2018-12-03 08:37:08 -0200646/*
David Brown9bf95af2019-10-10 15:36:36 -0600647 * Check that this is a valid header. Valid means that the magic is
648 * correct, and that the sizes/offsets are "sane". Sane means that
649 * there is no overflow on the arithmetic, and that the result fits
650 * within the flash area we are in.
651 */
652static bool
653boot_is_header_valid(const struct image_header *hdr, const struct flash_area *fap)
654{
655 uint32_t size;
656
657 if (hdr->ih_magic != IMAGE_MAGIC) {
658 return false;
659 }
660
661 if (!boot_u32_safe_add(&size, hdr->ih_img_size, hdr->ih_hdr_size)) {
662 return false;
663 }
664
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300665 if (size >= flash_area_get_size(fap)) {
David Brown9bf95af2019-10-10 15:36:36 -0600666 return false;
667 }
668
669 return true;
670}
671
672/*
Fabio Utzig338a19f2018-12-03 08:37:08 -0200673 * Check that a memory area consists of a given value.
674 */
675static inline bool
676boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300677{
678 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200679 uint8_t *p = (uint8_t *)data;
680 for (i = 0; i < len; i++) {
681 if (val != p[i]) {
682 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300683 }
684 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200685 return true;
686}
687
688static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300689boot_check_header_erased(struct boot_loader_state *state, int slot)
Fabio Utzig338a19f2018-12-03 08:37:08 -0200690{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300691 const struct flash_area *fap = NULL;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200692 struct image_header *hdr;
693 uint8_t erased_val;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300694 int area_id;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200695 int rc;
696
Fabio Utzig10ee6482019-08-01 12:04:52 -0300697 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300698 rc = flash_area_open(area_id, &fap);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200699 if (rc != 0) {
700 return -1;
701 }
702
703 erased_val = flash_area_erased_val(fap);
704 flash_area_close(fap);
705
Fabio Utzig10ee6482019-08-01 12:04:52 -0300706 hdr = boot_img_hdr(state, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200707 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
708 return -1;
709 }
710
711 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300712}
713
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000714#if (BOOT_IMAGE_NUMBER > 1) || \
David Vinczee574f2d2020-07-10 11:42:03 +0200715 defined(MCUBOOT_DIRECT_XIP) || \
Tamas Banfe031092020-09-10 17:32:39 +0200716 defined(MCUBOOT_RAM_LOAD) || \
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000717 (defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION))
718/**
David Vincze8b0b6372020-05-20 19:54:44 +0200719 * Compare image version numbers not including the build number
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000720 *
David Vincze8b0b6372020-05-20 19:54:44 +0200721 * @param ver1 Pointer to the first image version to compare.
722 * @param ver2 Pointer to the second image version to compare.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000723 *
David Vincze8b0b6372020-05-20 19:54:44 +0200724 * @retval -1 If ver1 is strictly less than ver2.
725 * @retval 0 If the image version numbers are equal,
726 * (not including the build number).
727 * @retval 1 If ver1 is strictly greater than ver2.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000728 */
729static int
David Vincze8b0b6372020-05-20 19:54:44 +0200730boot_version_cmp(const struct image_version *ver1,
731 const struct image_version *ver2)
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000732{
David Vincze8b0b6372020-05-20 19:54:44 +0200733 if (ver1->iv_major > ver2->iv_major) {
734 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000735 }
David Vincze8b0b6372020-05-20 19:54:44 +0200736 if (ver1->iv_major < ver2->iv_major) {
737 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000738 }
David Vincze8b0b6372020-05-20 19:54:44 +0200739 /* The major version numbers are equal, continue comparison. */
740 if (ver1->iv_minor > ver2->iv_minor) {
741 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000742 }
David Vincze8b0b6372020-05-20 19:54:44 +0200743 if (ver1->iv_minor < ver2->iv_minor) {
744 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000745 }
David Vincze8b0b6372020-05-20 19:54:44 +0200746 /* The minor version numbers are equal, continue comparison. */
747 if (ver1->iv_revision > ver2->iv_revision) {
748 return 1;
749 }
750 if (ver1->iv_revision < ver2->iv_revision) {
751 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000752 }
753
754 return 0;
755}
756#endif
757
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000758#if defined(MCUBOOT_DIRECT_XIP)
759/**
760 * Check if image in slot has been set with specific ROM address to run from
761 * and whether the slot starts at that address.
762 *
763 * @returns 0 if IMAGE_F_ROM_FIXED flag is not set;
764 * 0 if IMAGE_F_ROM_FIXED flag is set and ROM address specified in
765 * header matches the slot address;
766 * 1 if IMF_F_ROM_FIXED flag is set but ROM address specified in header
767 * does not match the slot address.
768 */
769static bool
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300770boot_rom_address_check(struct boot_loader_state *state)
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000771{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300772 uint32_t active_slot;
773 const struct image_header *hdr;
774 uint32_t f_off;
775
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300776 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300777 hdr = boot_img_hdr(state, active_slot);
778 f_off = boot_img_slot_off(state, active_slot);
779
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000780 if (hdr->ih_flags & IMAGE_F_ROM_FIXED && hdr->ih_load_addr != f_off) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300781 BOOT_LOG_WRN("Image in %s slot at 0x%" PRIx32
782 " has been built for offset 0x%" PRIx32 ", skipping",
783 active_slot == 0 ? "primary" : "secondary", f_off,
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000784 hdr->ih_load_addr);
785
786 /* If there is address mismatch, the image is not bootable from this
787 * slot.
788 */
789 return 1;
790 }
791 return 0;
792}
793#endif
794
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300795/*
796 * Check that there is a valid image in a slot
797 *
798 * @returns
Raef Colese8fe6cf2020-05-26 13:07:40 +0100799 * FIH_SUCCESS if image was successfully validated
800 * 1 (or its fih_int encoded form) if no bootloable image was found
801 * FIH_FAILURE on any errors
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300802 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100803static fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300804boot_validate_slot(struct boot_loader_state *state, int slot,
805 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800806{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300807 const struct flash_area *fap = NULL;
Marti Bolivarf804f622017-06-12 15:41:48 -0400808 struct image_header *hdr;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300809 int area_id;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100810 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800811 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300812
Fabio Utzig10ee6482019-08-01 12:04:52 -0300813 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300814 rc = flash_area_open(area_id, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800815 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100816 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800817 }
818
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300819 BOOT_LOG_DBG("> boot_validate_slot: fa_id = %u", (unsigned)fap->fa_id);
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200820
Fabio Utzig10ee6482019-08-01 12:04:52 -0300821 hdr = boot_img_hdr(state, slot);
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300822
Fabio Utzig10ee6482019-08-01 12:04:52 -0300823 if (boot_check_header_erased(state, slot) == 0 ||
824 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
Fabio Utzig260ec452020-07-09 18:40:07 -0300825
826#if defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE)
827 /*
828 * This fixes an issue where an image might be erased, but a trailer
829 * be left behind. It can happen if the image is in the secondary slot
830 * and did not pass validation, in which case the whole slot is erased.
831 * If during the erase operation, a reset occurs, parts of the slot
832 * might have been erased while some did not. The concerning part is
833 * the trailer because it might disable a new image from being loaded
834 * through mcumgr; so we just get rid of the trailer here, if the header
835 * is erased.
836 */
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200837 BOOT_LOG_DBG(" * Fix the secondary slot when image is invalid.");
Fabio Utzig260ec452020-07-09 18:40:07 -0300838 if (slot != BOOT_PRIMARY_SLOT) {
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200839 BOOT_LOG_DBG(" * Erase secondary image trailer.");
Fabio Utzig260ec452020-07-09 18:40:07 -0300840 swap_erase_trailer_sectors(state, fap);
841 }
842#endif
843
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200844 BOOT_LOG_DBG(" * No bootable image in slot(%d); continue booting from the primary slot.", slot);
David Vincze2d736ad2019-02-18 11:50:22 +0100845 /* No bootable image in slot; continue booting from the primary slot. */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300846 fih_rc = FIH_SWAP_TYPE_NONE;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200847 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300848 }
849
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000850#if defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION)
851 if (slot != BOOT_PRIMARY_SLOT) {
852 /* Check if version of secondary slot is sufficient */
David Vincze8b0b6372020-05-20 19:54:44 +0200853 rc = boot_version_cmp(
854 &boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
855 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
856 if (rc < 0 && boot_check_header_erased(state, BOOT_PRIMARY_SLOT)) {
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000857 BOOT_LOG_ERR("insufficient version in secondary slot");
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300858 flash_area_erase(fap, 0, flash_area_get_size(fap));
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000859 /* Image in the secondary slot does not satisfy version requirement.
860 * Erase the image and continue booting from the primary slot.
861 */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300862 fih_rc = FIH_SWAP_TYPE_NONE;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000863 goto out;
864 }
865 }
866#endif
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300867 BOOT_HOOK_CALL_FIH(boot_image_check_hook, fih_int_encode(BOOT_HOOK_REGULAR),
868 fih_rc, BOOT_CURR_IMG(state), slot);
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300869 if (FIH_TRUE == fih_eq(fih_rc, fih_int_encode(BOOT_HOOK_REGULAR)))
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300870 {
871 FIH_CALL(boot_image_check, fih_rc, state, hdr, fap, bs);
872 }
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300873 if (!boot_is_header_valid(hdr, fap) || fih_eq(fih_rc, FIH_SUCCESS) != FIH_TRUE) {
Tamas Banfe031092020-09-10 17:32:39 +0200874 if ((slot != BOOT_PRIMARY_SLOT) || ARE_SLOTS_EQUIVALENT()) {
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200875 BOOT_LOG_DBG(" * Image in the secondary slot is invalid. Erase the image");
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300876 flash_area_erase(fap, 0, flash_area_get_size(fap));
David Vinczee574f2d2020-07-10 11:42:03 +0200877 /* Image is invalid, erase it to prevent further unnecessary
878 * attempts to validate and boot it.
David Brownb38e0442017-02-24 13:57:12 -0700879 */
880 }
David Brown098de832019-12-10 11:58:01 -0700881#if !defined(__BOOTSIM__)
David Vincze2d736ad2019-02-18 11:50:22 +0100882 BOOT_LOG_ERR("Image in the %s slot is not valid!",
883 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Brown098de832019-12-10 11:58:01 -0700884#endif
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300885 fih_rc = FIH_SWAP_TYPE_NONE;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200886 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800887 }
888
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300889#if MCUBOOT_IMAGE_NUMBER > 1 && !defined(MCUBOOT_ENC_IMAGES) && defined(MCUBOOT_VERIFY_IMG_ADDRESS)
890 /* Verify that the image in the secondary slot has a reset address
891 * located in the primary slot. This is done to avoid users incorrectly
892 * overwriting an application written to the incorrect slot.
893 * This feature is only supported by ARM platforms.
894 */
895 if (area_id == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
896 const struct flash_area *pri_fa = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
897 struct image_header *secondary_hdr = boot_img_hdr(state, slot);
898 uint32_t reset_value = 0;
899 uint32_t reset_addr = secondary_hdr->ih_hdr_size + sizeof(reset_value);
900
901 rc = flash_area_read(fap, reset_addr, &reset_value, sizeof(reset_value));
902 if (rc != 0) {
903 fih_rc = fih_int_encode(1);
904 goto out;
905 }
906
907 if (reset_value < pri_fa->fa_off || reset_value> (pri_fa->fa_off + pri_fa->fa_size)) {
908 BOOT_LOG_ERR("Reset address of image in secondary slot is not in the primary slot");
909 BOOT_LOG_ERR("Erasing image from secondary slot");
910
911 /* The vector table in the image located in the secondary
912 * slot does not target the primary slot. This might
913 * indicate that the image was loaded to the wrong slot.
914 *
915 * Erase the image and continue booting from the primary slot.
916 */
917 flash_area_erase(fap, 0, fap->fa_size);
918 fih_rc = fih_int_encode(1);
919 goto out;
920 }
921 }
922#endif
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200923
Fabio Utzig338a19f2018-12-03 08:37:08 -0200924out:
925 flash_area_close(fap);
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300926 BOOT_LOG_DBG("< boot_validate_slot: fa_id = %u", (unsigned)fap->fa_id);
Raef Colese8fe6cf2020-05-26 13:07:40 +0100927 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800928}
929
David Vinczec3084132020-02-18 14:50:47 +0100930#ifdef MCUBOOT_HW_ROLLBACK_PROT
931/**
932 * Updates the stored security counter value with the image's security counter
933 * value which resides in the given slot, only if it's greater than the stored
934 * value.
935 *
936 * @param image_index Index of the image to determine which security
937 * counter to update.
938 * @param slot Slot number of the image.
939 * @param hdr Pointer to the image header structure of the image
940 * that is currently stored in the given slot.
941 *
942 * @return 0 on success; nonzero on failure.
943 */
944static int
945boot_update_security_counter(uint8_t image_index, int slot,
946 struct image_header *hdr)
947{
948 const struct flash_area *fap = NULL;
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300949 fih_int fih_rc = FIH_FAILURE;
950 fih_uint img_security_cnt = FIH_UINT_ZERO;
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300951 void * custom_data = NULL;
David Vinczec3084132020-02-18 14:50:47 +0100952 int rc;
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300953#if defined CYW20829
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300954 uint8_t buff[REPROV_PACK_SIZE];
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300955#endif /* defined CYW20829 */
David Vinczec3084132020-02-18 14:50:47 +0100956
957 rc = flash_area_open(flash_area_id_from_multi_image_slot(image_index, slot),
958 &fap);
959 if (rc != 0) {
960 rc = BOOT_EFLASH;
961 goto done;
962 }
963
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300964 rc = -1;
965 FIH_CALL(bootutil_get_img_security_cnt, fih_rc, hdr, fap, &img_security_cnt);
966 if (fih_eq(fih_rc, FIH_SUCCESS) != FIH_TRUE) {
David Vinczec3084132020-02-18 14:50:47 +0100967 goto done;
968 }
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300969 else
970 {
971 fih_rc = FIH_FAILURE;
972 }
David Vinczec3084132020-02-18 14:50:47 +0100973
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300974#if defined CYW20829
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300975 rc = bootutil_get_img_reprov_packet(hdr, fap, buff);
976 if (rc == 0) {
977 custom_data = (void *)buff;
David Vinczec3084132020-02-18 14:50:47 +0100978 }
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300979#endif /* defined CYW20829 */
David Vinczec3084132020-02-18 14:50:47 +0100980
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300981 rc = boot_nv_security_counter_update(image_index, img_security_cnt, custom_data);
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200982#ifdef USE_IFX_SE_CRYPTO
983 fih_uint img_security_check = FIH_UINT_ZERO;
984 FIH_CALL(boot_nv_security_counter_get, fih_rc, image_index, &img_security_check);
985 if (fih_eq(fih_rc, FIH_SUCCESS) != FIH_TRUE) {
986 goto done;
987 }
988 else
989 {
990 fih_rc = FIH_FAILURE;
991 BOOT_LOG_INF("[SUCCESS] security_counter_get called right after security_counter_update" \
992 "to check if update is successful upd_cnt = %u, read_cnt = %u",
993 fih_uint_decode(img_security_check), img_security_cnt);
994 }
995#endif /* IFX_SE_RT_CRYPTO */
David Vinczec3084132020-02-18 14:50:47 +0100996done:
997 flash_area_close(fap);
998 return rc;
999}
1000#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1001
David Vinczee574f2d2020-07-10 11:42:03 +02001002/**
1003 * Determines which swap operation to perform, if any. If it is determined
1004 * that a swap operation is required, the image in the secondary slot is checked
1005 * for validity. If the image in the secondary slot is invalid, it is erased,
1006 * and a swap type of "none" is indicated.
1007 *
1008 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
1009 */
1010static int
1011boot_validated_swap_type(struct boot_loader_state *state,
1012 struct boot_status *bs)
1013{
1014 int swap_type;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001015 fih_int fih_rc = FIH_FAILURE;
David Vinczee574f2d2020-07-10 11:42:03 +02001016
1017 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
1018 if (BOOT_IS_UPGRADE(swap_type)) {
1019 /* Boot loader wants to switch to the secondary slot.
1020 * Ensure image is valid.
1021 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001022 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_SECONDARY_SLOT, bs);
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03001023 if (fih_eq(fih_rc, FIH_SUCCESS) != FIH_TRUE) {
1024 if (FIH_TRUE == fih_eq(fih_rc, FIH_SWAP_TYPE_NONE)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001025 swap_type = BOOT_SWAP_TYPE_NONE;
1026 } else {
1027 swap_type = BOOT_SWAP_TYPE_FAIL;
1028 }
David Vinczee574f2d2020-07-10 11:42:03 +02001029 }
1030 }
1031
1032 return swap_type;
1033}
1034
Christopher Collins92ea77f2016-12-12 15:59:26 -08001035/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08001036 * Erases a region of flash.
1037 *
Fabio Utzigba829042018-09-18 08:29:34 -03001038 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001039 * @param off The offset within the flash area to start the
1040 * erase.
1041 * @param sz The number of bytes to erase.
1042 *
1043 * @return 0 on success; nonzero on failure.
1044 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001045int
Fabio Utzigc28005b2019-09-10 12:18:29 -03001046boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001047{
Fabio Utzigba829042018-09-18 08:29:34 -03001048 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001049}
1050
1051/**
1052 * Copies the contents of one flash region to another. You must erase the
1053 * destination region prior to calling this function.
1054 *
1055 * @param flash_area_id_src The ID of the source flash area.
1056 * @param flash_area_id_dst The ID of the destination flash area.
1057 * @param off_src The offset within the source flash area to
1058 * copy from.
1059 * @param off_dst The offset within the destination flash area to
1060 * copy to.
1061 * @param sz The number of bytes to copy.
1062 *
1063 * @return 0 on success; nonzero on failure.
1064 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001065int
Fabio Utzigc28005b2019-09-10 12:18:29 -03001066boot_copy_region(struct boot_loader_state *state,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001067 const struct flash_area *fap_src,
Fabio Utzigba829042018-09-18 08:29:34 -03001068 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -08001069 uint32_t off_src, uint32_t off_dst, uint32_t sz)
1070{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001071 uint32_t bytes_copied;
1072 int chunk_sz;
1073 int rc;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001074#if defined (MCUBOOT_ENC_IMAGES) && !defined(MCUBOOT_ENC_IMAGES_XIP)
Fabio Utzigba829042018-09-18 08:29:34 -03001075 uint32_t off;
Fabio Utziga87cc7d2019-08-26 11:21:45 -03001076 uint32_t tlv_off;
Fabio Utzigba829042018-09-18 08:29:34 -03001077 size_t blk_off;
1078 struct image_header *hdr;
1079 uint16_t idx;
1080 uint32_t blk_sz;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001081 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -03001082#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001083
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001084/* NOTE:
1085 * Default value 1024 is not suitable for platforms with larger erase size.
1086 * Give user ability to define platform tolerant chunk size. In most cases
1087 * it would be flash erase alignment.
1088 */
1089#ifdef MCUBOOT_PLATFORM_CHUNK_SIZE
1090 #define MCUBOOT_CHUNK_SIZE MCUBOOT_PLATFORM_CHUNK_SIZE
1091#else
1092 #define MCUBOOT_CHUNK_SIZE 1024
1093#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -03001094
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001095 TARGET_STATIC uint8_t buf[MCUBOOT_CHUNK_SIZE] __attribute__((aligned(4)));
1096
1097#if !defined(MCUBOOT_ENC_IMAGES) || defined(MCUBOOT_ENC_IMAGES_XIP)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001098 (void)state;
1099#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001100
Christopher Collins92ea77f2016-12-12 15:59:26 -08001101 bytes_copied = 0;
1102 while (bytes_copied < sz) {
1103 if (sz - bytes_copied > sizeof buf) {
1104 chunk_sz = sizeof buf;
1105 } else {
1106 chunk_sz = sz - bytes_copied;
1107 }
1108
1109 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
1110 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001111 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001112 }
1113
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001114#if defined(MCUBOOT_ENC_IMAGES) && !defined(MCUBOOT_ENC_IMAGES_XIP)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001115 image_index = BOOT_CURR_IMG(state);
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001116 if ((flash_area_get_id(fap_src) == FLASH_AREA_IMAGE_PRIMARY(image_index) ||
1117 flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_PRIMARY(image_index)) &&
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +02001118
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001119 !(flash_area_get_id(fap_src) == FLASH_AREA_IMAGE_PRIMARY(image_index) &&
1120 flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_PRIMARY(image_index)) &&
1121 !(flash_area_get_id(fap_src) == FLASH_AREA_IMAGE_SECONDARY(image_index) &&
1122 flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)))
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +02001123 {
1124 /* assume the primary slot as src, needs encryption */
1125 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig74aef312019-11-28 11:05:34 -03001126#if !defined(MCUBOOT_SWAP_USING_MOVE)
Fabio Utzigba829042018-09-18 08:29:34 -03001127 off = off_src;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001128 if (flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_PRIMARY(image_index)) {
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +02001129 /* might need decryption (metadata from the secondary slot) */
1130 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001131 off = off_dst;
1132 }
Fabio Utzig74aef312019-11-28 11:05:34 -03001133#else
1134 off = off_dst;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001135 if (flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_PRIMARY(image_index)) {
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +02001136 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig74aef312019-11-28 11:05:34 -03001137 }
1138#endif
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001139 if (IS_ENCRYPTED(hdr)) {
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03001140 uint32_t abs_off = off + bytes_copied;
1141 if (abs_off < hdr->ih_hdr_size) {
Fabio Utzigba829042018-09-18 08:29:34 -03001142 /* do not decrypt header */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03001143 if (abs_off + chunk_sz > hdr->ih_hdr_size) {
1144 /* The lower part of the chunk contains header data */
1145 blk_off = 0;
1146 blk_sz = chunk_sz - (hdr->ih_hdr_size - abs_off);
1147 idx = hdr->ih_hdr_size - abs_off;
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +02001148 } else {
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03001149 /* The chunk contains exclusively header data */
1150 blk_sz = 0; /* nothing to decrypt */
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +02001151 }
Fabio Utzigba829042018-09-18 08:29:34 -03001152 } else {
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03001153 idx = 0;
1154 blk_sz = chunk_sz;
1155 blk_off = (abs_off - hdr->ih_hdr_size) & 0xf;
Fabio Utzigba829042018-09-18 08:29:34 -03001156 }
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03001157
1158 if (blk_sz > 0)
1159 {
1160 tlv_off = BOOT_TLV_OFF(hdr);
1161 if (abs_off + chunk_sz > tlv_off) {
1162 /* do not decrypt TLVs */
1163 if (abs_off >= tlv_off) {
1164 blk_sz = 0;
1165 } else {
1166 blk_sz = tlv_off - abs_off;
1167 }
Fabio Utzigba829042018-09-18 08:29:34 -03001168 }
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03001169 rc = boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
1170 (abs_off + idx) - hdr->ih_hdr_size, blk_sz,
1171 blk_off, &buf[idx]);
1172 if (rc != 0) {
1173 return rc;
1174 }
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +02001175 }
Fabio Utzigba829042018-09-18 08:29:34 -03001176 }
1177 }
1178#endif
1179
Christopher Collins92ea77f2016-12-12 15:59:26 -08001180 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
1181 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001182 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001183 }
1184
1185 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -03001186
1187 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -08001188 }
1189
Fabio Utzigba829042018-09-18 08:29:34 -03001190 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001191}
1192
Christopher Collins92ea77f2016-12-12 15:59:26 -08001193/**
David Vincze2d736ad2019-02-18 11:50:22 +01001194 * Overwrite primary slot with the image contained in the secondary slot.
1195 * If a prior copy operation was interrupted by a system reset, this function
1196 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001197 *
1198 * @param bs The current boot status. This function reads
1199 * this struct to determine if it is resuming
1200 * an interrupted swap operation. This
1201 * function writes the updated status to this
1202 * function on return.
1203 *
1204 * @return 0 on success; nonzero on failure.
1205 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001206#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001207static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001208boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
David Brown17609d82017-05-05 09:41:34 -06001209{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001210 size_t sect_count;
1211 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001212 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001213 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001214 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001215 size_t last_sector;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001216 const struct flash_area *fap_primary_slot = NULL;
1217 const struct flash_area *fap_secondary_slot = NULL;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001218 uint8_t image_index;
David Vincze2d736ad2019-02-18 11:50:22 +01001219
Fabio Utzigb4f88102020-10-04 10:16:24 -03001220#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1221 uint32_t sector;
1222 uint32_t trailer_sz;
1223 uint32_t off;
1224 uint32_t sz;
1225#endif
1226
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001227 (void)bs;
1228
Fabio Utzig13d9e352017-10-05 20:32:31 -03001229#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1230 uint32_t src_size = 0;
Fabio Utzigd638b172019-08-09 10:38:05 -03001231 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001232 assert(rc == 0);
1233#endif
David Brown17609d82017-05-05 09:41:34 -06001234
David Vincze2d736ad2019-02-18 11:50:22 +01001235 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1236 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -06001237
Fabio Utzigb0f04732019-07-31 09:49:19 -03001238 image_index = BOOT_CURR_IMG(state);
1239
1240 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1241 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001242 assert (rc == 0);
1243
Fabio Utzigb0f04732019-07-31 09:49:19 -03001244 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1245 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001246 assert (rc == 0);
1247
Fabio Utzig10ee6482019-08-01 12:04:52 -03001248 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001249 BOOT_LOG_DBG(" * primary slot sectors: %lu", (unsigned long)sect_count);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001250 for (sect = 0, size = 0; sect < sect_count; sect++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001251 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001252 rc = boot_erase_region(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001253 assert(rc == 0);
1254
Fabio Utzig13d9e352017-10-05 20:32:31 -03001255#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
Fabio Utzigb4f88102020-10-04 10:16:24 -03001256 if ((size + this_size) >= src_size) {
1257 size += src_size - size;
1258 size += BOOT_WRITE_SZ(state) - (size % BOOT_WRITE_SZ(state));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001259 break;
1260 }
1261#endif
Fabio Utzigb4f88102020-10-04 10:16:24 -03001262
1263 size += this_size;
David Brown17609d82017-05-05 09:41:34 -06001264 }
1265
Fabio Utzigb4f88102020-10-04 10:16:24 -03001266#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1267 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
1268 sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
1269 sz = 0;
1270 do {
1271 sz += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sector);
1272 off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, sector);
1273 sector--;
1274 } while (sz < trailer_sz);
1275
1276 rc = boot_erase_region(fap_primary_slot, off, sz);
1277 assert(rc == 0);
1278#endif
1279
Fabio Utzigba829042018-09-18 08:29:34 -03001280#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001281 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001282 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001283 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
Fabio Utzig4741c452019-12-19 15:32:41 -03001284 fap_secondary_slot, bs);
David Vincze2d736ad2019-02-18 11:50:22 +01001285
Fabio Utzigba829042018-09-18 08:29:34 -03001286 if (rc < 0) {
1287 return BOOT_EBADIMAGE;
1288 }
Fabio Utzig4741c452019-12-19 15:32:41 -03001289 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001290 return BOOT_EBADIMAGE;
1291 }
1292 }
1293#endif
1294
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001295 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%lx bytes", (unsigned long)size);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001296 rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot, 0, 0, size);
Fabio Utzigb4f88102020-10-04 10:16:24 -03001297 if (rc != 0) {
1298 return rc;
1299 }
1300
1301#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1302 rc = boot_write_magic(fap_primary_slot);
1303 if (rc != 0) {
1304 return rc;
1305 }
1306#endif
David Brown17609d82017-05-05 09:41:34 -06001307
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001308 rc = BOOT_HOOK_CALL(boot_copy_region_post_hook, 0, BOOT_CURR_IMG(state),
1309 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT), size);
1310 if (rc != 0) {
1311 return rc;
1312 }
1313
David Vinczec3084132020-02-18 14:50:47 +01001314#ifdef MCUBOOT_HW_ROLLBACK_PROT
1315 /* Update the stored security counter with the new image's security counter
1316 * value. Both slots hold the new image at this point, but the secondary
1317 * slot's image header must be passed since the image headers in the
1318 * boot_data structure have not been updated yet.
1319 */
1320 rc = boot_update_security_counter(BOOT_CURR_IMG(state), BOOT_PRIMARY_SLOT,
1321 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1322 if (rc != 0) {
1323 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
1324 return rc;
1325 }
1326#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1327
Fabio Utzig13d9e352017-10-05 20:32:31 -03001328 /*
1329 * Erases header and trailer. The trailer is erased because when a new
1330 * image is written without a trailer as is the case when using newt, the
1331 * trailer that was left might trigger a new upgrade.
1332 */
Christopher Collins2c88e692019-05-22 15:10:14 -07001333 BOOT_LOG_DBG("erasing secondary header");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001334 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001335 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
1336 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001337 assert(rc == 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001338 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -07001339 BOOT_LOG_DBG("erasing secondary trailer");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001340 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001341 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
1342 last_sector),
1343 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
1344 last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001345 assert(rc == 0);
1346
David Vincze2d736ad2019-02-18 11:50:22 +01001347 flash_area_close(fap_primary_slot);
1348 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001349
David Vincze2d736ad2019-02-18 11:50:22 +01001350 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001351
1352 return 0;
1353}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001354#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001355
Christopher Collinsa1c12042019-05-23 14:00:28 -07001356#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -03001357/**
1358 * Swaps the two images in flash. If a prior copy operation was interrupted
1359 * by a system reset, this function completes that operation.
1360 *
1361 * @param bs The current boot status. This function reads
1362 * this struct to determine if it is resuming
1363 * an interrupted swap operation. This
1364 * function writes the updated status to this
1365 * function on return.
1366 *
1367 * @return 0 on success; nonzero on failure.
1368 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001369static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001370boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001371{
Fabio Utzig2473ac02017-05-02 12:45:02 -03001372 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001373#ifdef MCUBOOT_ENC_IMAGES
1374 const struct flash_area *fap;
1375 uint8_t slot;
Fabio Utzigba829042018-09-18 08:29:34 -03001376#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001377 uint32_t size;
1378 uint32_t copy_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001379 uint8_t image_index;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001380 int rc = -1;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001381
1382 /* FIXME: just do this if asked by user? */
1383
1384 size = copy_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001385 image_index = BOOT_CURR_IMG(state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001386
Fabio Utzig12d59162019-11-28 10:01:59 -03001387 if (boot_status_is_reset(bs)) {
Fabio Utzig46490722017-09-04 15:34:32 -03001388 /*
1389 * No swap ever happened, so need to find the largest image which
1390 * will be used to determine the amount of sectors to swap.
1391 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001392 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001393 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001394 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001395 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001396 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001397
Fabio Utzigba829042018-09-18 08:29:34 -03001398#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001399 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001400 fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001401 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001402 assert(rc >= 0);
1403
1404 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001405 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 0, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001406 assert(rc == 0);
1407 } else {
1408 rc = 0;
1409 }
1410 } else {
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001411 (void)memset(bs->enckey[0], BOOT_UNINITIALIZED_KEY_FILL,
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03001412 BOOT_ENC_KEY_ALIGN_SIZE);
Fabio Utzigba829042018-09-18 08:29:34 -03001413 }
1414#endif
1415
Fabio Utzig10ee6482019-08-01 12:04:52 -03001416 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001417 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001418 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001419 assert(rc == 0);
1420 }
1421
Fabio Utzigba829042018-09-18 08:29:34 -03001422#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001423 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001424 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001425 fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001426 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001427 assert(rc >= 0);
1428
1429 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001430 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001431 assert(rc == 0);
1432 } else {
1433 rc = 0;
1434 }
1435 } else {
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001436 (void)memset(bs->enckey[1], BOOT_UNINITIALIZED_KEY_FILL,
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03001437 BOOT_ENC_KEY_ALIGN_SIZE);
Fabio Utzigba829042018-09-18 08:29:34 -03001438 }
1439#endif
1440
Fabio Utzig46490722017-09-04 15:34:32 -03001441 if (size > copy_size) {
1442 copy_size = size;
1443 }
1444
1445 bs->swap_size = copy_size;
1446 } else {
1447 /*
1448 * If a swap was under way, the swap_size should already be present
1449 * in the trailer...
1450 */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001451 rc = boot_read_swap_size(image_index, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001452 assert(rc == 0);
1453
1454 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001455
1456#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4741c452019-12-19 15:32:41 -03001457 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1458 rc = boot_read_enc_key(image_index, slot, bs);
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001459 assert(0 == rc);
Fabio Utzigba829042018-09-18 08:29:34 -03001460
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001461 /* Set only an initialized key */
1462 if (!bootutil_buffer_is_filled(bs->enckey[slot],
1463 BOOT_UNINITIALIZED_KEY_FILL,
1464 BOOT_ENC_KEY_SIZE)) {
1465 rc = boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs);
1466 assert(rc == 0);
Fabio Utzigba829042018-09-18 08:29:34 -03001467 }
1468 }
1469#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001470 }
1471
Fabio Utzig12d59162019-11-28 10:01:59 -03001472 swap_run(state, bs, copy_size);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001473
David Vincze2d736ad2019-02-18 11:50:22 +01001474#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig12d59162019-11-28 10:01:59 -03001475 extern int boot_status_fails;
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001476 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001477 BOOT_LOG_WRN("%d status write fails performing the swap",
1478 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001479 }
1480#endif
1481
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001482 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001483}
David Brown17609d82017-05-05 09:41:34 -06001484#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001485
David Vinczee32483f2019-06-13 10:46:24 +02001486#if (BOOT_IMAGE_NUMBER > 1)
1487/**
1488 * Check the image dependency whether it is satisfied and modify
1489 * the swap type if necessary.
1490 *
1491 * @param dep Image dependency which has to be verified.
1492 *
1493 * @return 0 on success; nonzero on failure.
1494 */
1495static int
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03001496boot_verify_slot_dependency_flash(struct boot_loader_state *state, struct image_dependency *dep)
David Vinczee32483f2019-06-13 10:46:24 +02001497{
1498 struct image_version *dep_version;
1499 size_t dep_slot;
1500 int rc;
David Browne6ab34c2019-09-03 12:24:21 -06001501 uint8_t swap_type;
David Vinczee32483f2019-06-13 10:46:24 +02001502
1503 /* Determine the source of the image which is the subject of
1504 * the dependency and get it's version. */
David Browne6ab34c2019-09-03 12:24:21 -06001505 swap_type = state->swap_type[dep->image_id];
Barry Solomon04075532020-03-18 09:33:32 -04001506 dep_slot = BOOT_IS_UPGRADE(swap_type) ? BOOT_SECONDARY_SLOT
1507 : BOOT_PRIMARY_SLOT;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001508 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +02001509
David Vincze8b0b6372020-05-20 19:54:44 +02001510 rc = boot_version_cmp(dep_version, &dep->image_min_version);
1511 if (rc < 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001512 /* Dependency not satisfied.
1513 * Modify the swap type to decrease the version number of the image
1514 * (which will be located in the primary slot after the boot process),
1515 * consequently the number of unsatisfied dependencies will be
1516 * decreased or remain the same.
1517 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001518 switch (BOOT_SWAP_TYPE(state)) {
David Vinczee32483f2019-06-13 10:46:24 +02001519 case BOOT_SWAP_TYPE_TEST:
1520 case BOOT_SWAP_TYPE_PERM:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001521 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczee32483f2019-06-13 10:46:24 +02001522 break;
1523 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001524 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczee32483f2019-06-13 10:46:24 +02001525 break;
1526 default:
1527 break;
1528 }
David Vincze8b0b6372020-05-20 19:54:44 +02001529 } else {
1530 /* Dependency satisfied. */
1531 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001532 }
1533
1534 return rc;
1535}
1536
1537/**
1538 * Read all dependency TLVs of an image from the flash and verify
1539 * one after another to see if they are all satisfied.
1540 *
1541 * @param slot Image slot number.
1542 *
1543 * @return 0 on success; nonzero on failure.
1544 */
1545static int
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03001546boot_verify_slot_dependencies_flash(struct boot_loader_state *state, uint32_t slot)
David Vinczee32483f2019-06-13 10:46:24 +02001547{
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03001548 const struct flash_area *fap = NULL;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001549 struct image_tlv_iter it;
David Vinczee32483f2019-06-13 10:46:24 +02001550 struct image_dependency dep;
1551 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001552 uint16_t len;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001553 int area_id;
David Vinczee32483f2019-06-13 10:46:24 +02001554 int rc;
1555
Fabio Utzig10ee6482019-08-01 12:04:52 -03001556 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001557 rc = flash_area_open(area_id, &fap);
David Vinczee32483f2019-06-13 10:46:24 +02001558 if (rc != 0) {
1559 rc = BOOT_EFLASH;
1560 goto done;
1561 }
1562
Fabio Utzig61fd8882019-09-14 20:00:20 -03001563 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
1564 IMAGE_TLV_DEPENDENCY, true);
David Vinczee32483f2019-06-13 10:46:24 +02001565 if (rc != 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001566 goto done;
1567 }
1568
Fabio Utzig61fd8882019-09-14 20:00:20 -03001569 while (true) {
1570 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
1571 if (rc < 0) {
1572 return -1;
1573 } else if (rc > 0) {
1574 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001575 break;
1576 }
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03001577 else
1578 {
1579 /* acc. to MISRA R.15.7 */
1580 }
Fabio Utzig61fd8882019-09-14 20:00:20 -03001581
1582 if (len != sizeof(dep)) {
1583 rc = BOOT_EBADIMAGE;
1584 goto done;
1585 }
1586
1587 rc = flash_area_read(fap, off, &dep, len);
1588 if (rc != 0) {
1589 rc = BOOT_EFLASH;
1590 goto done;
1591 }
1592
1593 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
1594 rc = BOOT_EBADARGS;
1595 goto done;
1596 }
1597
1598 /* Verify dependency and modify the swap type if not satisfied. */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03001599 rc = boot_verify_slot_dependency_flash(state, &dep);
Fabio Utzig61fd8882019-09-14 20:00:20 -03001600 if (rc != 0) {
1601 /* Dependency not satisfied. */
1602 goto done;
1603 }
David Vinczee32483f2019-06-13 10:46:24 +02001604 }
1605
1606done:
1607 flash_area_close(fap);
1608 return rc;
1609}
1610
1611/**
David Vinczee32483f2019-06-13 10:46:24 +02001612 * Iterate over all the images and verify whether the image dependencies in the
1613 * TLV area are all satisfied and update the related swap type if necessary.
1614 */
Fabio Utzig298913b2019-08-28 11:22:45 -03001615static int
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03001616boot_verify_dependencies_flash(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001617{
Erik Johnson49063752020-02-06 09:59:31 -06001618 int rc = -1;
Fabio Utzig298913b2019-08-28 11:22:45 -03001619 uint8_t slot;
David Vinczee32483f2019-06-13 10:46:24 +02001620
Fabio Utzig10ee6482019-08-01 12:04:52 -03001621 BOOT_CURR_IMG(state) = 0;
1622 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03001623 if (state->img_mask[BOOT_CURR_IMG(state)]) {
1624 BOOT_CURR_IMG(state)++;
1625 continue;
1626 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001627 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1628 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
1629 slot = BOOT_SECONDARY_SLOT;
1630 } else {
1631 slot = BOOT_PRIMARY_SLOT;
1632 }
1633
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03001634 rc = boot_verify_slot_dependencies_flash(state, slot);
Fabio Utzigabec0732019-07-31 08:40:22 -03001635 if (rc == 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001636 /* All dependencies've been satisfied, continue with next image. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001637 BOOT_CURR_IMG(state)++;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001638 } else {
1639#if (USE_SHARED_SLOT == 1)
1640 /* Disable an upgrading of this image.*/
1641 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1642 BOOT_CURR_IMG(state)++;
1643#else
Fabio Utzig298913b2019-08-28 11:22:45 -03001644 /* Cannot upgrade due to non-met dependencies, so disable all
1645 * image upgrades.
1646 */
1647 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
1648 BOOT_CURR_IMG(state) = idx;
1649 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1650 }
1651 break;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001652#endif /* (USE_SHARED_SLOT == 1) */
David Vinczee32483f2019-06-13 10:46:24 +02001653 }
1654 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001655 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02001656}
1657#endif /* (BOOT_IMAGE_NUMBER > 1) */
1658
Christopher Collins92ea77f2016-12-12 15:59:26 -08001659/**
David Vinczeba3bd602019-06-17 16:01:43 +02001660 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001661 *
David Vinczeba3bd602019-06-17 16:01:43 +02001662 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001663 *
1664 * @return 0 on success; nonzero on failure.
1665 */
1666static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001667boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001668{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001669 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001670#ifndef MCUBOOT_OVERWRITE_ONLY
1671 uint8_t swap_type;
1672#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001673
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001674 BOOT_LOG_DBG("> boot_perform_update: bs->idx = %" PRIu32, bs->idx);
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +02001675
David Vinczeba3bd602019-06-17 16:01:43 +02001676 /* At this point there are no aborted swaps. */
1677#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001678 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001679#elif defined(MCUBOOT_BOOTSTRAP)
1680 /* Check if the image update was triggered by a bad image in the
1681 * primary slot (the validity of the image in the secondary slot had
1682 * already been checked).
1683 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001684 fih_int fih_rc = FIH_FAILURE;
1685 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +02001686
1687#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Raef Colese8fe6cf2020-05-26 13:07:40 +01001688 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, bs);
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +02001689#else
1690 fih_rc = FIH_SUCCESS;
1691#endif
1692
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03001693 if (rc == 0 || fih_eq(fih_rc, FIH_SUCCESS) != FIH_TRUE) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001694 /* Initialize swap status partition for primary slot, because
1695 * in swap mode it is needed to properly complete copying the image
1696 * to the primary slot.
1697 */
1698 const struct flash_area *fap_primary_slot = NULL;
1699
1700 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state)),
1701 &fap_primary_slot);
1702 assert(rc == 0);
1703
1704 rc = swap_status_init(state, fap_primary_slot, bs);
1705 assert(rc == 0);
1706
1707 flash_area_close(fap_primary_slot);
1708
Fabio Utzig10ee6482019-08-01 12:04:52 -03001709 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001710 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001711 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001712 }
1713#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03001714 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001715#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001716 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001717
1718#ifndef MCUBOOT_OVERWRITE_ONLY
1719 /* The following state needs image_ok be explicitly set after the
1720 * swap was finished to avoid a new revert.
1721 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001722 swap_type = BOOT_SWAP_TYPE(state);
1723 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1724 swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001725 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001726 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001727 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001728 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001729 }
1730
David Vinczec3084132020-02-18 14:50:47 +01001731#ifdef MCUBOOT_HW_ROLLBACK_PROT
1732 if (swap_type == BOOT_SWAP_TYPE_PERM) {
1733 /* Update the stored security counter with the new image's security
1734 * counter value. The primary slot holds the new image at this point,
1735 * but the secondary slot's image header must be passed since image
1736 * headers in the boot_data structure have not been updated yet.
1737 *
1738 * In case of a permanent image swap mcuboot will never attempt to
1739 * revert the images on the next reboot. Therefore, the security
1740 * counter must be increased right after the image upgrade.
1741 */
1742 rc = boot_update_security_counter(
1743 BOOT_CURR_IMG(state),
1744 BOOT_PRIMARY_SLOT,
1745 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1746 if (rc != 0) {
1747 BOOT_LOG_ERR("Security counter update failed after "
1748 "image upgrade.");
1749 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
1750 }
1751 }
1752#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1753
Fabio Utzige575b0b2019-09-11 12:34:23 -03001754 if (BOOT_IS_UPGRADE(swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001755 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
INFINEON\DovhalAe54c0a32024-07-31 22:45:38 +03001756#if defined(MCUBOOT_ENC_IMAGES_SMIF)
1757 rc |= swap_clear_magic_upgrade(BOOT_CURR_IMG(state));
1758#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001759 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001760 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001761 }
David Vinczeba3bd602019-06-17 16:01:43 +02001762 }
1763#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001764
David Vinczeba3bd602019-06-17 16:01:43 +02001765 return rc;
1766}
1767
1768/**
1769 * Completes a previously aborted image swap.
1770 *
1771 * @param bs The current boot status.
1772 *
1773 * @return 0 on success; nonzero on failure.
1774 */
1775#if !defined(MCUBOOT_OVERWRITE_ONLY)
1776static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001777boot_complete_partial_swap(struct boot_loader_state *state,
1778 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001779{
1780 int rc;
1781
1782 /* Determine the type of swap operation being resumed from the
1783 * `swap-type` trailer field.
1784 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001785 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001786 assert(rc == 0);
1787
Fabio Utzig10ee6482019-08-01 12:04:52 -03001788 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001789
1790 /* The following states need image_ok be explicitly set after the
1791 * swap was finished to avoid a new revert.
1792 */
1793 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1794 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001795 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001796 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001797 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001798 }
1799 }
1800
Fabio Utzige575b0b2019-09-11 12:34:23 -03001801 if (BOOT_IS_UPGRADE(bs->swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001802 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001803 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001804 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001805 }
1806 }
1807
Fabio Utzig10ee6482019-08-01 12:04:52 -03001808 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001809 BOOT_LOG_ERR("panic!");
1810 assert(0);
1811
1812 /* Loop forever... */
1813 while (1) {}
1814 }
1815
1816 return rc;
1817}
1818#endif /* !MCUBOOT_OVERWRITE_ONLY */
1819
1820#if (BOOT_IMAGE_NUMBER > 1)
1821/**
1822 * Review the validity of previously determined swap types of other images.
1823 *
1824 * @param aborted_swap The current image upgrade is a
1825 * partial/aborted swap.
1826 */
1827static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001828boot_review_image_swap_types(struct boot_loader_state *state,
1829 bool aborted_swap)
David Vinczeba3bd602019-06-17 16:01:43 +02001830{
1831 /* In that case if we rebooted in the middle of an image upgrade process, we
1832 * must review the validity of swap types, that were previously determined
1833 * for other images. The image_ok flag had not been set before the reboot
1834 * for any of the updated images (only the copy_done flag) and thus falsely
1835 * the REVERT swap type has been determined for the previous images that had
1836 * been updated before the reboot.
1837 *
1838 * There are two separate scenarios that we have to deal with:
1839 *
1840 * 1. The reboot has happened during swapping an image:
1841 * The current image upgrade has been determined as a
1842 * partial/aborted swap.
1843 * 2. The reboot has happened between two separate image upgrades:
1844 * In this scenario we must check the swap type of the current image.
1845 * In those cases if it is NONE or REVERT we cannot certainly determine
1846 * the fact of a reboot. In a consistent state images must move in the
1847 * same direction or stay in place, e.g. in practice REVERT and TEST
1848 * swap types cannot be present at the same time. If the swap type of
1849 * the current image is either TEST, PERM or FAIL we must review the
1850 * already determined swap types of other images and set each false
1851 * REVERT swap types to NONE (these images had been successfully
1852 * updated before the system rebooted between two separate image
1853 * upgrades).
1854 */
1855
Fabio Utzig10ee6482019-08-01 12:04:52 -03001856 if (BOOT_CURR_IMG(state) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001857 /* Nothing to do */
1858 return;
1859 }
1860
1861 if (!aborted_swap) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001862 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
1863 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001864 /* Nothing to do */
1865 return;
1866 }
1867 }
1868
Fabio Utzig10ee6482019-08-01 12:04:52 -03001869 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
1870 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
1871 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001872 }
1873 }
1874}
1875#endif
1876
1877/**
1878 * Prepare image to be updated if required.
1879 *
1880 * Prepare image to be updated if required with completing an image swap
1881 * operation if one was aborted and/or determining the type of the
1882 * swap operation. In case of any error set the swap type to NONE.
1883 *
Fabio Utzig10ee6482019-08-01 12:04:52 -03001884 * @param state TODO
David Vinczeba3bd602019-06-17 16:01:43 +02001885 * @param bs Pointer where the read and possibly updated
1886 * boot status can be written to.
1887 */
1888static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001889boot_prepare_image_for_update(struct boot_loader_state *state,
1890 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001891{
1892 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001893 fih_int fih_rc = FIH_FAILURE;
David Vinczeba3bd602019-06-17 16:01:43 +02001894
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001895 BOOT_LOG_DBG("> boot_prepare_image_for_update: image = %u",
1896 (unsigned)BOOT_CURR_IMG(state));
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +02001897
David Vinczeba3bd602019-06-17 16:01:43 +02001898 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001899 rc = boot_read_sectors(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001900 if (rc != 0) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001901 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%u"
1902 " - too small?", (unsigned int) BOOT_MAX_IMG_SECTORS);
David Vinczeba3bd602019-06-17 16:01:43 +02001903 /* Unable to determine sector layout, continue with next image
1904 * if there is one.
1905 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001906 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001907 if (rc == BOOT_EFLASH)
1908 {
1909 /* Only return on error from the primary image flash */
1910 return;
1911 }
David Vinczeba3bd602019-06-17 16:01:43 +02001912 }
1913
1914 /* Attempt to read an image header from each slot. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001915 rc = boot_read_image_headers(state, false, NULL);
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001916 BOOT_LOG_DBG(" * Read an image (%u) header from each slot: rc = %d",
1917 (unsigned)BOOT_CURR_IMG(state), rc);
David Vinczeba3bd602019-06-17 16:01:43 +02001918 if (rc != 0) {
1919 /* Continue with next image if there is one. */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001920 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001921 (unsigned)BOOT_CURR_IMG(state));
Fabio Utzig10ee6482019-08-01 12:04:52 -03001922 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001923 return;
1924 }
1925
1926 /* If the current image's slots aren't compatible, no swap is possible.
1927 * Just boot into primary slot.
1928 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001929 if (boot_slots_compatible(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001930 boot_status_reset(bs);
1931
1932#ifndef MCUBOOT_OVERWRITE_ONLY
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03001933#ifdef MCUBOOT_SWAP_USING_STATUS
1934
1935 const struct flash_area *fap;
1936 uint32_t img_size = 0;
1937
1938 /* Check here if image firmware + tlvs in slot do not
1939 * overlap with last sector of slot. Last sector of slot
1940 * contains trailer of the image which needs to be
1941 * manupulated independently of other image parts.
1942 * If firmware overlaps with trailer sector it does not
1943 * make sense to move further since any attemps to perform
1944 * swap upgrade would lead to failure or unexpected behaviour
1945 */
1946
1947 for (uint32_t i = 0; i < BOOT_NUM_SLOTS; i++) {
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +02001948 if ((&state->imgs[BOOT_CURR_IMG(state)][i].hdr)->ih_magic == IMAGE_MAGIC) {
1949 rc = boot_read_image_size(state, i, &img_size);
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03001950
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +02001951 if (rc == 0) {
1952 fap = BOOT_IMG(state, i).area;
1953 if (fap != NULL) {
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03001954
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +02001955 uint32_t trailer_sector_off = (BOOT_WRITE_SZ(state)) * boot_img_num_sectors(state, i) - BOOT_WRITE_SZ(state);
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03001956
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +02001957 BOOT_LOG_DBG("Slot %u firmware + tlvs size = %u, "
1958 "slot size = %u, write_size = %u, "
1959 "img sectors num = %u, "
1960 "write_size * sect_num - write_size = %u",
1961 i , img_size, fap->fa_size, BOOT_WRITE_SZ(state),
1962 (uint32_t)boot_img_num_sectors(state, i), trailer_sector_off);
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03001963
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +02001964 if (img_size > trailer_sector_off) {
1965 BOOT_LOG_ERR("Firmware + tlvs in slot %u overlaps with last sector, which contains trailer, erasing this image", i);
1966 rc = flash_area_erase(fap, 0, flash_area_get_size(fap));
1967 }
1968 else {
1969 /* image firmware + tlvs do not overlap with last sector of slot, continue */
1970 }
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03001971 }
1972 }
1973 }
1974 }
1975#endif /* MCUBOOT_SWAP_USING_STATUS */
1976
Fabio Utzig12d59162019-11-28 10:01:59 -03001977 rc = swap_read_status(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001978 if (rc != 0) {
1979 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001980 (unsigned)BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001981 /* Continue with next image if there is one. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001982 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001983 return;
1984 }
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +02001985#endif /* ifndef MCUBOOT_OVERWRITE_ONLY */
David Vinczeba3bd602019-06-17 16:01:43 +02001986
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +02001987#if defined (MCUBOOT_SWAP_USING_MOVE) || defined(MCUBOOT_SWAP_USING_SCRATCH)
Fabio Utzig74aef312019-11-28 11:05:34 -03001988 /*
1989 * Must re-read image headers because the boot status might
1990 * have been updated in the previous function call.
1991 */
1992 rc = boot_read_image_headers(state, !boot_status_is_reset(bs), bs);
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001993 BOOT_LOG_DBG(" * re-read image(%u) headers: rc = %d.",
1994 (unsigned)BOOT_CURR_IMG(state), rc);
Fabio Utzig32afe852020-10-04 10:36:02 -03001995#ifdef MCUBOOT_BOOTSTRAP
1996 /* When bootstrapping it's OK to not have image magic in the primary slot */
1997 if (rc != 0 && (BOOT_CURR_IMG(state) != BOOT_PRIMARY_SLOT ||
1998 boot_check_header_erased(state, BOOT_PRIMARY_SLOT) != 0)) {
1999#else
Fabio Utzig74aef312019-11-28 11:05:34 -03002000 if (rc != 0) {
Fabio Utzig32afe852020-10-04 10:36:02 -03002001#endif
Fabio Utzig74aef312019-11-28 11:05:34 -03002002 /* Continue with next image if there is one. */
2003 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
2004 BOOT_CURR_IMG(state));
2005 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
2006 return;
2007 }
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +02002008#endif /* (MCUBOOT_SWAP_USING_MOVE) || defined(MCUBOOT_SWAP_USING_SCRATCH) */
Fabio Utzig74aef312019-11-28 11:05:34 -03002009
David Vinczeba3bd602019-06-17 16:01:43 +02002010 /* Determine if we rebooted in the middle of an image swap
2011 * operation. If a partial swap was detected, complete it.
2012 */
Fabio Utzig12d59162019-11-28 10:01:59 -03002013 if (!boot_status_is_reset(bs)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002014
2015#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03002016 boot_review_image_swap_types(state, true);
David Vinczeba3bd602019-06-17 16:01:43 +02002017#endif
2018
2019#ifdef MCUBOOT_OVERWRITE_ONLY
2020 /* Should never arrive here, overwrite-only mode has
2021 * no swap state.
2022 */
2023 assert(0);
2024#else
2025 /* Determine the type of swap operation being resumed from the
2026 * `swap-type` trailer field.
2027 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002028 rc = boot_complete_partial_swap(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002029 assert(rc == 0);
2030#endif
2031 /* Attempt to read an image header from each slot. Ensure that
2032 * image headers in slots are aligned with headers in boot_data.
2033 */
Fabio Utzig12d59162019-11-28 10:01:59 -03002034 rc = boot_read_image_headers(state, false, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002035 assert(rc == 0);
2036
2037 /* Swap has finished set to NONE */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002038 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02002039 } else {
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +02002040 BOOT_LOG_DBG(" * There was no partial swap, determine swap type.");
2041
David Vinczeba3bd602019-06-17 16:01:43 +02002042 /* There was no partial swap, determine swap type. */
2043 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002044 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002045 } else {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002046 FIH_CALL(boot_validate_slot, fih_rc,
2047 state, BOOT_SECONDARY_SLOT, bs);
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002048 if (fih_eq(fih_rc, FIH_SUCCESS) != FIH_TRUE) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002049 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
2050 } else {
2051 BOOT_SWAP_TYPE(state) = bs->swap_type;
2052 }
David Vinczeba3bd602019-06-17 16:01:43 +02002053 }
2054
2055#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03002056 boot_review_image_swap_types(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02002057#endif
2058
2059#ifdef MCUBOOT_BOOTSTRAP
Fabio Utzig10ee6482019-08-01 12:04:52 -03002060 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02002061 /* Header checks are done first because they are
2062 * inexpensive. Since overwrite-only copies starting from
2063 * offset 0, if interrupted, it might leave a valid header
2064 * magic, so also run validation on the primary slot to be
2065 * sure it's not OK.
2066 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002067 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +02002068#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Raef Colese8fe6cf2020-05-26 13:07:40 +01002069 FIH_CALL(boot_validate_slot, fih_rc,
2070 state, BOOT_PRIMARY_SLOT, bs);
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +02002071#else
2072 fih_rc = FIH_SUCCESS;
2073#endif
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002074 if (rc == 0 || fih_eq(fih_rc, FIH_SUCCESS) != FIH_TRUE) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002075
Fabio Utzig3d77c952020-10-04 10:23:17 -03002076 rc = (boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC) ? 1: 0;
Raef Colese8fe6cf2020-05-26 13:07:40 +01002077 FIH_CALL(boot_validate_slot, fih_rc,
2078 state, BOOT_SECONDARY_SLOT, bs);
2079
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002080 if (rc == 1 && FIH_TRUE == fih_eq(fih_rc, FIH_SUCCESS)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002081 /* Set swap type to REVERT to overwrite the primary
2082 * slot with the image contained in secondary slot
2083 * and to trigger the explicit setting of the
2084 * image_ok flag.
2085 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03002086 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02002087 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02002088 }
2089 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02002090#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002091 }
David Vinczeba3bd602019-06-17 16:01:43 +02002092 } else {
2093 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002094 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002095 }
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +02002096 BOOT_LOG_DBG("< boot_prepare_image_for_update");
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002097}
2098
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002099/**
2100 * Updates the security counter for the current image.
2101 *
2102 * @param state Boot loader status information.
2103 *
2104 * @return 0 on success; nonzero on failure.
2105 */
2106static int
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002107boot_update_hw_rollback_protection_flash(struct boot_loader_state *state)
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002108{
2109#ifdef MCUBOOT_HW_ROLLBACK_PROT
2110 int rc;
2111
2112 /* Update the stored security counter with the active image's security
2113 * counter value. It will only be updated if the new security counter is
2114 * greater than the stored value.
2115 *
2116 * In case of a successful image swapping when the swap type is TEST the
2117 * security counter can be increased only after a reset, when the swap
2118 * type is NONE and the image has marked itself "OK" (the image_ok flag
2119 * has been set). This way a "revert" can be performed when it's
2120 * necessary.
2121 */
2122 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
2123 rc = boot_update_security_counter(
2124 BOOT_CURR_IMG(state),
2125 BOOT_PRIMARY_SLOT,
2126 boot_img_hdr(state, BOOT_PRIMARY_SLOT));
2127 if (rc != 0) {
2128 BOOT_LOG_ERR("Security counter update failed after image "
2129 "validation.");
2130 return rc;
2131 }
2132 }
2133
2134 return 0;
2135
2136#else /* MCUBOOT_HW_ROLLBACK_PROT */
2137 (void) (state);
2138
2139 return 0;
2140#endif
2141}
2142
Raef Colese8fe6cf2020-05-26 13:07:40 +01002143fih_int
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002144context_boot_go_flash(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08002145{
Marti Bolivar84898652017-06-13 17:20:22 -04002146 size_t slot;
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002147 struct boot_status bs = {0};
David Vincze9015a5d2020-05-18 14:43:11 +02002148 int rc = -1;
Raef Colese8fe6cf2020-05-26 13:07:40 +01002149 fih_int fih_rc = FIH_FAILURE;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002150 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03002151 int image_index;
Fabio Utzig298913b2019-08-28 11:22:45 -03002152 bool has_upgrade;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002153
2154 /* The array of slot sectors are defined here (as opposed to file scope) so
2155 * that they don't get allocated for non-boot-loader apps. This is
2156 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002157 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08002158 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002159 TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
2160 TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03002161#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03002162 TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03002163#endif
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +02002164#if MCUBOOT_SWAP_USING_STATUS
2165 TARGET_STATIC boot_sector_t status_sectors[BOOT_MAX_SWAP_STATUS_SECTORS];
2166#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08002167
Fabio Utzig298913b2019-08-28 11:22:45 -03002168 has_upgrade = false;
2169
2170#if (BOOT_IMAGE_NUMBER == 1)
2171 (void)has_upgrade;
2172#endif
Fabio Utzigba829042018-09-18 08:29:34 -03002173
David Vinczeba3bd602019-06-17 16:01:43 +02002174 /* Iterate over all the images. By the end of the loop the swap type has
2175 * to be determined for each image and all aborted swaps have to be
2176 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002177 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002178 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002179#if BOOT_IMAGE_NUMBER > 1
2180 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2181 continue;
2182 }
2183#endif
David Vinczeba3bd602019-06-17 16:01:43 +02002184#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
2185 /* The keys used for encryption may no longer be valid (could belong to
2186 * another images). Therefore, mark them as invalid to force their reload
2187 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03002188 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03002189 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Brown554c52e2017-06-30 16:01:07 -06002190#endif
2191
Fabio Utzig10ee6482019-08-01 12:04:52 -03002192 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03002193
Fabio Utzig10ee6482019-08-01 12:04:52 -03002194 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002195 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03002196 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002197 secondary_slot_sectors[image_index];
Fabio Utzig12d59162019-11-28 10:01:59 -03002198#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03002199 state->scratch.sectors = scratch_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -03002200#endif
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +02002201#if MCUBOOT_SWAP_USING_STATUS
2202 state->status.sectors = status_sectors;
2203#endif
David Vinczeba3bd602019-06-17 16:01:43 +02002204
2205 /* Open primary and secondary image areas for the duration
2206 * of this call.
2207 */
2208 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03002209 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03002210 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02002211 assert(rc == 0);
2212 }
Fabio Utzig12d59162019-11-28 10:01:59 -03002213#if MCUBOOT_SWAP_USING_SCRATCH
David Vinczeba3bd602019-06-17 16:01:43 +02002214 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03002215 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002216 assert(rc == 0);
Fabio Utzig12d59162019-11-28 10:01:59 -03002217#endif
David Vinczeba3bd602019-06-17 16:01:43 +02002218
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +02002219 BOOT_LOG_DBG(" * boot_prepare_image_for_update...");
David Vinczeba3bd602019-06-17 16:01:43 +02002220 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002221 boot_prepare_image_for_update(state, &bs);
Fabio Utzig298913b2019-08-28 11:22:45 -03002222
Fabio Utzige575b0b2019-09-11 12:34:23 -03002223 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
Fabio Utzig298913b2019-08-28 11:22:45 -03002224 has_upgrade = true;
2225 }
David Vinczeba3bd602019-06-17 16:01:43 +02002226 }
2227
David Vinczee32483f2019-06-13 10:46:24 +02002228#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig298913b2019-08-28 11:22:45 -03002229 if (has_upgrade) {
2230 /* Iterate over all the images and verify whether the image dependencies
2231 * are all satisfied and update swap type if necessary.
2232 */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002233 rc = boot_verify_dependencies_flash(state);
David Vincze8b0b6372020-05-20 19:54:44 +02002234 if (rc != 0) {
Fabio Utzig298913b2019-08-28 11:22:45 -03002235 /*
2236 * It was impossible to upgrade because the expected dependency version
2237 * was not available. Here we already changed the swap_type so that
2238 * instead of asserting the bootloader, we continue and no upgrade is
2239 * performed.
2240 */
2241 rc = 0;
2242 }
2243 }
David Vinczee32483f2019-06-13 10:46:24 +02002244#endif
2245
David Vinczeba3bd602019-06-17 16:01:43 +02002246 /* Iterate over all the images. At this point there are no aborted swaps
2247 * and the swap types are determined for each image. By the end of the loop
2248 * all required update operations will have been finished.
2249 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002250 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002251#if (BOOT_IMAGE_NUMBER > 1)
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002252 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2253 continue;
2254 }
2255
David Vinczeba3bd602019-06-17 16:01:43 +02002256#ifdef MCUBOOT_ENC_IMAGES
2257 /* The keys used for encryption may no longer be valid (could belong to
2258 * another images). Therefore, mark them as invalid to force their reload
2259 * by boot_enc_load().
2260 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03002261 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002262#endif /* MCUBOOT_ENC_IMAGES */
2263
2264 /* Indicate that swap is not aborted */
Fabio Utzig12d59162019-11-28 10:01:59 -03002265 boot_status_reset(&bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002266#endif /* (BOOT_IMAGE_NUMBER > 1) */
2267
2268 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002269 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02002270
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002271 BOOT_LOG_DBG(" * process swap_type = %u", (unsigned)bs.swap_type);
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +02002272
Fabio Utzig10ee6482019-08-01 12:04:52 -03002273 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002274 case BOOT_SWAP_TYPE_NONE:
2275 break;
2276
2277 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
2278 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
2279 case BOOT_SWAP_TYPE_REVERT:
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002280 BOOT_LOG_DBG(" * perform update, mode %u...", (unsigned)bs.swap_type);
2281 rc = BOOT_HOOK_CALL(boot_perform_update_hook, BOOT_HOOK_REGULAR,
2282 BOOT_CURR_IMG(state), &(BOOT_IMG(state, 1).hdr),
2283 BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT));
2284 if (rc == BOOT_HOOK_REGULAR)
2285 {
2286 rc = boot_perform_update(state, &bs);
2287 }
David Vinczeba3bd602019-06-17 16:01:43 +02002288 assert(rc == 0);
2289 break;
2290
2291 case BOOT_SWAP_TYPE_FAIL:
2292 /* The image in secondary slot was invalid and is now erased. Ensure
2293 * we don't try to boot into it again on the next reboot. Do this by
2294 * pretending we just reverted back to primary slot.
2295 */
2296#ifndef MCUBOOT_OVERWRITE_ONLY
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002297 BOOT_LOG_DBG(" * update failed! Set image_ok manually for image(%u)",
2298 (unsigned)BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002299 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig12d59162019-11-28 10:01:59 -03002300 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002301 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002302 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002303 }
2304#endif /* !MCUBOOT_OVERWRITE_ONLY */
2305 break;
2306
2307 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002308 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002309 }
2310
Fabio Utzig10ee6482019-08-01 12:04:52 -03002311 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002312 BOOT_LOG_ERR("panic!");
2313 assert(0);
2314
2315 /* Loop forever... */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002316 FIH_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002317 }
2318 }
2319
2320 /* Iterate over all the images. At this point all required update operations
2321 * have finished. By the end of the loop each image in the primary slot will
2322 * have been re-validated.
2323 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002324 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002325#if BOOT_IMAGE_NUMBER > 1
2326 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2327 continue;
2328 }
2329#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -03002330 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02002331 /* Attempt to read an image header from each slot. Ensure that image
2332 * headers in slots are aligned with headers in boot_data.
2333 */
Fabio Utzig12d59162019-11-28 10:01:59 -03002334 rc = boot_read_image_headers(state, false, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002335 if (rc != 0) {
2336 goto out;
2337 }
2338 /* Since headers were reloaded, it can be assumed we just performed
2339 * a swap or overwrite. Now the header info that should be used to
2340 * provide the data for the bootstrap, which previously was at
2341 * secondary slot, was updated to primary slot.
2342 */
2343 }
2344
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +02002345#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
2346#if defined(MCUBOOT_RAM_LOAD) /* to fix Rule 14.3 violation */
2347 if(IS_RAM_BOOTABLE(boot_img_hdr(state, BOOT_PRIMARY_SLOT)) == false) {
2348#endif /* defined(MCUBOOT_RAM_LOAD) */
2349 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, &bs);
2350 if (fih_eq(fih_rc, FIH_SUCCESS) != FIH_TRUE) {
2351 goto out;
2352 }
2353#if defined(MCUBOOT_RAM_LOAD) /* to fix Rule 14.3 violation */
David Vinczeba3bd602019-06-17 16:01:43 +02002354 }
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +02002355#endif /* defined(MCUBOOT_RAM_LOAD) */
David Vinczeba3bd602019-06-17 16:01:43 +02002356#else
2357 /* Even if we're not re-validating the primary slot, we could be booting
2358 * onto an empty flash chip. At least do a basic sanity check that
2359 * the magic number on the image is OK.
2360 */
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +02002361
2362 BOOT_LOG_INF("Since boot image validation was skipped, "\
2363 "at least IMAGE_MAGIC should be checked");
2364
Fabio Utzig10ee6482019-08-01 12:04:52 -03002365 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002366 BOOT_LOG_ERR("bad image magic 0x%" PRIx32 "; Image=%u",
2367 BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic,
2368 (unsigned)BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002369 rc = BOOT_EBADIMAGE;
2370 goto out;
2371 }
David Vinczec3084132020-02-18 14:50:47 +01002372#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
2373
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002374#ifdef MCUBOOT_ENC_IMAGES_XIP
2375 if (0 == BOOT_CURR_IMG(state)) {
2376 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_PRIMARY_SLOT)))
2377 {
2378 (void)memcpy((uint8_t*)rsp->xip_iv, BOOT_CURR_ENC(state)->aes_iv, BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE);
2379 (void)memcpy((uint8_t*)rsp->xip_key, bs.enckey[BOOT_CURR_IMG(state)], BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE);
David Vinczec3084132020-02-18 14:50:47 +01002380 }
2381 }
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002382#endif /* MCUBOOT_ENC_IMAGES_XIP */
David Vincze1cf11b52020-03-24 07:51:09 +01002383
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002384 rc = boot_update_hw_rollback_protection_flash(state);
David Vincze1cf11b52020-03-24 07:51:09 +01002385 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002386 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002387 }
David Vincze1cf11b52020-03-24 07:51:09 +01002388
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002389 rc = boot_add_shared_data(state, BOOT_PRIMARY_SLOT);
David Vincze1cf11b52020-03-24 07:51:09 +01002390 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002391 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002392 }
David Vinczeba3bd602019-06-17 16:01:43 +02002393 }
2394
Fabio Utzigb0f04732019-07-31 09:49:19 -03002395#if (BOOT_IMAGE_NUMBER > 1)
David Vinczeba3bd602019-06-17 16:01:43 +02002396 /* Always boot from the primary slot of Image 0. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002397 BOOT_CURR_IMG(state) = 0;
Fabio Utzigb0f04732019-07-31 09:49:19 -03002398#endif
Fabio Utzig298913b2019-08-28 11:22:45 -03002399
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002400 rsp->br_flash_dev_id = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)->fa_device_id;
2401 rsp->br_image_off = boot_img_slot_off(state, BOOT_PRIMARY_SLOT);
2402 rsp->br_hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzigf616c542019-12-19 15:23:32 -03002403 /*
2404 * Since the boot_status struct stores plaintext encryption keys, reset
2405 * them here to avoid the possibility of jumping into an image that could
2406 * easily recover them.
2407 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002408 (void)memset(&bs, 0, sizeof(struct boot_status));
Fabio Utzigf616c542019-12-19 15:23:32 -03002409
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002410 fill_rsp(state, rsp);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002411
Raef Colese8fe6cf2020-05-26 13:07:40 +01002412 fih_rc = FIH_SUCCESS;
Fabio Utzig298913b2019-08-28 11:22:45 -03002413out:
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002414 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002415
2416 if (rc) {
2417 fih_rc = fih_int_encode(rc);
2418 }
2419
2420 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002421}
2422
Raef Colese8fe6cf2020-05-26 13:07:40 +01002423fih_int
Christopher Collins92ea77f2016-12-12 15:59:26 -08002424split_go(int loader_slot, int split_slot, void **entry)
2425{
Marti Bolivarc50926f2017-06-14 09:35:40 -04002426 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08002427 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002428 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002429 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002430 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +01002431 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002432
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002433 if ((loader_slot < 0) || (split_slot < 0)) {
2434 FIH_RET(FIH_FAILURE);
2435 }
2436
Christopher Collins92ea77f2016-12-12 15:59:26 -08002437 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
2438 if (sectors == NULL) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002439 FIH_RET(FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002440 }
David Vinczeba3bd602019-06-17 16:01:43 +02002441 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
2442 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002443
2444 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
2445 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07002446 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04002447 assert(rc == 0);
2448 split_flash_id = flash_area_id_from_image_slot(split_slot);
2449 rc = flash_area_open(split_flash_id,
2450 &BOOT_IMG_AREA(&boot_data, split_slot));
2451 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002452
2453 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002454 rc = boot_read_sectors(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002455 if (rc != 0) {
2456 rc = SPLIT_GO_ERR;
2457 goto done;
2458 }
2459
Fabio Utzig12d59162019-11-28 10:01:59 -03002460 rc = boot_read_image_headers(&boot_data, true, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002461 if (rc != 0) {
2462 goto done;
2463 }
2464
Christopher Collins92ea77f2016-12-12 15:59:26 -08002465 /* Don't check the bootable image flag because we could really call a
2466 * bootable or non-bootable image. Just validate that the image check
2467 * passes which is distinct from the normal check.
2468 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002469 FIH_CALL(split_image_check, fih_rc,
2470 boot_img_hdr(&boot_data, split_slot),
2471 BOOT_IMG_AREA(&boot_data, split_slot),
2472 boot_img_hdr(&boot_data, loader_slot),
2473 BOOT_IMG_AREA(&boot_data, loader_slot));
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002474 if (fih_eq(fih_rc, FIH_SUCCESS) != FIH_TRUE) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08002475 goto done;
2476 }
2477
Marti Bolivarea088872017-06-12 17:10:49 -04002478 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04002479 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08002480 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002481 rc = SPLIT_GO_OK;
2482
2483done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04002484 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
2485 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002486 free(sectors);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002487
2488 if (rc) {
2489 fih_rc = fih_int_encode(rc);
2490 }
2491
2492 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002493}
David Vinczee574f2d2020-07-10 11:42:03 +02002494
David Vinczee574f2d2020-07-10 11:42:03 +02002495
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002496#if defined(MCUBOOT_DIRECT_XIP) || defined(MCUBOOT_RAM_LOAD)
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002497
David Vinczee574f2d2020-07-10 11:42:03 +02002498/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002499 * Opens all flash areas and checks which contain an image with a valid header.
David Vinczee574f2d2020-07-10 11:42:03 +02002500 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002501 * @param state Boot loader status information.
David Vinczee574f2d2020-07-10 11:42:03 +02002502 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002503 * @return 0 on success; nonzero on failure.
2504 */
2505static int
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002506boot_get_slot_usage(struct boot_loader_state *state)
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002507{
2508 uint32_t slot;
2509 int fa_id;
2510 int rc;
2511 struct image_header *hdr = NULL;
2512
2513 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002514#if BOOT_IMAGE_NUMBER > 1
2515 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2516 continue;
2517 }
2518#endif
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002519 /* Open all the slots */
2520 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2521 fa_id = flash_area_id_from_multi_image_slot(
2522 BOOT_CURR_IMG(state), slot);
2523 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
2524 assert(rc == 0);
2525 }
2526
2527 /* Attempt to read an image header from each slot. */
2528 rc = boot_read_image_headers(state, false, NULL);
2529 if (rc != 0) {
2530 BOOT_LOG_WRN("Failed reading image headers.");
2531 return rc;
2532 }
2533
2534 /* Check headers in all slots */
2535 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2536 hdr = boot_img_hdr(state, slot);
2537
2538 if (boot_is_header_valid(hdr, BOOT_IMG_AREA(state, slot))) {
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002539 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = true;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002540 BOOT_LOG_IMAGE_INFO(slot, hdr);
2541 } else {
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002542 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = false;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002543 BOOT_LOG_INF("Image %u %s slot: Image not found",
2544 (unsigned)BOOT_CURR_IMG(state),
2545 (slot == BOOT_PRIMARY_SLOT)
2546 ? "Primary" : "Secondary");
2547 }
2548 }
2549
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002550 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002551 }
2552
2553 return 0;
2554}
2555
2556/**
2557 * Finds the slot containing the image with the highest version number for the
2558 * current image.
2559 *
2560 * @param state Boot loader status information.
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002561 *
2562 * @return NO_ACTIVE_SLOT if no available slot found, number of
2563 * the found slot otherwise.
David Vinczee574f2d2020-07-10 11:42:03 +02002564 */
2565static uint32_t
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002566find_slot_with_highest_version(struct boot_loader_state *state)
David Vinczee574f2d2020-07-10 11:42:03 +02002567{
David Vinczee574f2d2020-07-10 11:42:03 +02002568 uint32_t slot;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002569 uint32_t candidate_slot = NO_ACTIVE_SLOT;
2570 int rc;
David Vinczee574f2d2020-07-10 11:42:03 +02002571
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002572 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002573 if (state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot]) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002574 if (candidate_slot == NO_ACTIVE_SLOT) {
2575 candidate_slot = slot;
2576 } else {
2577 rc = boot_version_cmp(
2578 &boot_img_hdr(state, slot)->ih_ver,
2579 &boot_img_hdr(state, candidate_slot)->ih_ver);
2580 if (rc == 1) {
2581 /* The version of the image being examined is greater than
2582 * the version of the current candidate.
2583 */
2584 candidate_slot = slot;
2585 }
2586 }
David Vinczee574f2d2020-07-10 11:42:03 +02002587 }
2588 }
2589
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002590 return candidate_slot;
David Vinczee574f2d2020-07-10 11:42:03 +02002591}
2592
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002593#ifdef MCUBOOT_HAVE_LOGGING
David Vincze505fba22020-10-22 13:53:29 +02002594/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002595 * Prints the state of the loaded images.
David Vincze505fba22020-10-22 13:53:29 +02002596 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002597 * @param state Boot loader status information.
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002598 */
2599static void
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002600print_loaded_images(struct boot_loader_state *state)
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002601{
2602 uint32_t active_slot;
2603
2604 (void)state;
2605
2606 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002607#if BOOT_IMAGE_NUMBER > 1
2608 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2609 continue;
2610 }
2611#endif
2612 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002613
2614 BOOT_LOG_INF("Image %u loaded from the %s slot",
2615 (unsigned)BOOT_CURR_IMG(state),
2616 (active_slot == BOOT_PRIMARY_SLOT) ?
2617 "primary" : "secondary");
2618 }
2619}
2620#endif
2621
2622#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
2623/**
2624 * Checks whether the active slot of the current image was previously selected
2625 * to run. Erases the image if it was selected but its execution failed,
2626 * otherwise marks it as selected if it has not been before.
David Vincze505fba22020-10-22 13:53:29 +02002627 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002628 * @param state Boot loader status information.
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002629 *
2630 * @return 0 on success; nonzero on failure.
David Vincze505fba22020-10-22 13:53:29 +02002631 */
2632static int
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002633boot_select_or_erase(struct boot_loader_state *state)
David Vincze505fba22020-10-22 13:53:29 +02002634{
2635 const struct flash_area *fap;
2636 int fa_id;
2637 int rc;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002638 uint32_t active_slot;
2639 struct boot_swap_state* active_swap_state;
David Vincze505fba22020-10-22 13:53:29 +02002640
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002641 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002642
2643 fa_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), active_slot);
David Vincze505fba22020-10-22 13:53:29 +02002644 rc = flash_area_open(fa_id, &fap);
2645 assert(rc == 0);
2646
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002647 active_swap_state = &(state->slot_usage[BOOT_CURR_IMG(state)].swap_state);
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002648
2649 (void)memset(active_swap_state, 0, sizeof(struct boot_swap_state));
2650 rc = boot_read_swap_state(fap, active_swap_state);
David Vincze505fba22020-10-22 13:53:29 +02002651 assert(rc == 0);
2652
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002653 if (active_swap_state->magic != BOOT_MAGIC_GOOD ||
2654 (active_swap_state->copy_done == BOOT_FLAG_SET &&
2655 active_swap_state->image_ok != BOOT_FLAG_SET)) {
David Vincze505fba22020-10-22 13:53:29 +02002656 /*
2657 * A reboot happened without the image being confirmed at
2658 * runtime or its trailer is corrupted/invalid. Erase the image
2659 * to prevent it from being selected again on the next reboot.
2660 */
2661 BOOT_LOG_DBG("Erasing faulty image in the %s slot.",
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002662 (active_slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
2663 rc = flash_area_erase(fap, 0, flash_area_get_size(fap));
David Vincze505fba22020-10-22 13:53:29 +02002664 assert(rc == 0);
2665
2666 flash_area_close(fap);
2667 rc = -1;
2668 } else {
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002669 if (active_swap_state->copy_done != BOOT_FLAG_SET) {
2670 if (active_swap_state->copy_done == BOOT_FLAG_BAD) {
David Vincze505fba22020-10-22 13:53:29 +02002671 BOOT_LOG_DBG("The copy_done flag had an unexpected value. Its "
2672 "value was neither 'set' nor 'unset', but 'bad'.");
2673 }
2674 /*
2675 * Set the copy_done flag, indicating that the image has been
2676 * selected to boot. It can be set in advance, before even
2677 * validating the image, because in case the validation fails, the
2678 * entire image slot will be erased (including the trailer).
2679 */
2680 rc = boot_write_copy_done(fap);
2681 if (rc != 0) {
2682 BOOT_LOG_WRN("Failed to set copy_done flag of the image in "
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002683 "the %s slot.", (active_slot == BOOT_PRIMARY_SLOT) ?
David Vincze505fba22020-10-22 13:53:29 +02002684 "primary" : "secondary");
2685 rc = 0;
2686 }
2687 }
2688 flash_area_close(fap);
2689 }
2690
2691 return rc;
2692}
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002693#endif /* MCUBOOT_DIRECT_XIP && MCUBOOT_DIRECT_XIP_REVERT */
David Vincze505fba22020-10-22 13:53:29 +02002694
Tamas Banfe031092020-09-10 17:32:39 +02002695#ifdef MCUBOOT_RAM_LOAD
2696
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002697#ifndef MULTIPLE_EXECUTABLE_RAM_REGIONS
Tamas Banfe031092020-09-10 17:32:39 +02002698#if !defined(IMAGE_EXECUTABLE_RAM_START) || !defined(IMAGE_EXECUTABLE_RAM_SIZE)
2699#error "Platform MUST define executable RAM bounds in case of RAM_LOAD"
2700#endif
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002701#endif
Tamas Banfe031092020-09-10 17:32:39 +02002702
2703/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002704 * Verifies that the active slot of the current image can be loaded within the
2705 * predefined bounds that are allowed to be used by executable images.
Tamas Banfe031092020-09-10 17:32:39 +02002706 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002707 * @param state Boot loader status information.
Tamas Banfe031092020-09-10 17:32:39 +02002708 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002709 * @return 0 on success; nonzero on failure.
Tamas Banfe031092020-09-10 17:32:39 +02002710 */
2711static int
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002712boot_verify_ram_load_address(struct boot_loader_state *state)
Tamas Banfe031092020-09-10 17:32:39 +02002713{
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002714 uint32_t img_dst;
2715 uint32_t img_sz;
Tamas Banfe031092020-09-10 17:32:39 +02002716 uint32_t img_end_addr;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002717 uint32_t exec_ram_start;
2718 uint32_t exec_ram_size;
Tamas Banfe031092020-09-10 17:32:39 +02002719
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002720 (void)state;
2721
2722#ifdef MULTIPLE_EXECUTABLE_RAM_REGIONS
2723 int rc;
2724
2725 rc = boot_get_image_exec_ram_info(BOOT_CURR_IMG(state), &exec_ram_start,
2726 &exec_ram_size);
2727 if (rc != 0) {
2728 return BOOT_EBADSTATUS;
2729 }
2730#else
2731 exec_ram_start = IMAGE_EXECUTABLE_RAM_START;
2732 exec_ram_size = IMAGE_EXECUTABLE_RAM_SIZE;
2733#endif
2734
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002735 img_dst = state->slot_usage[BOOT_CURR_IMG(state)].img_dst;
2736 img_sz = state->slot_usage[BOOT_CURR_IMG(state)].img_sz;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002737
2738 if (img_dst < exec_ram_start) {
Tamas Banfe031092020-09-10 17:32:39 +02002739 return BOOT_EBADIMAGE;
2740 }
2741
2742 if (!boot_u32_safe_add(&img_end_addr, img_dst, img_sz)) {
2743 return BOOT_EBADIMAGE;
2744 }
2745
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002746 if (img_end_addr > (exec_ram_start + exec_ram_size)) {
Tamas Banfe031092020-09-10 17:32:39 +02002747 return BOOT_EBADIMAGE;
2748 }
2749
2750 return 0;
2751}
2752
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002753#ifdef MCUBOOT_ENC_IMAGES
2754
Tamas Banfe031092020-09-10 17:32:39 +02002755/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002756 * Copies and decrypts an image from a slot in the flash to an SRAM address.
Tamas Banfe031092020-09-10 17:32:39 +02002757 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002758 * @param state Boot loader status information.
2759 * @param slot The flash slot of the image to be copied to SRAM.
2760 * @param hdr The image header.
2761 * @param src_sz Size of the image.
2762 * @param img_dst Pointer to the address at which the image needs to be
2763 * copied to SRAM.
2764 *
2765 * @return 0 on success; nonzero on failure.
2766 */
2767static int
2768boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state,
2769 uint32_t slot, struct image_header *hdr,
2770 uint32_t src_sz, uint32_t img_dst)
2771{
2772 /* The flow for the decryption and copy of the image is as follows :
2773 * 1. The whole image is copied to the RAM (header + payload + TLV).
2774 * 2. The encryption key is loaded from the TLV in flash.
2775 * 3. The image is then decrypted chunk by chunk in RAM (1 chunk
2776 * is 1024 bytes). Only the payload section is decrypted.
2777 * 4. The image is authenticated in RAM.
2778 */
2779 const struct flash_area *fap_src = NULL;
2780 struct boot_status bs;
2781 uint32_t blk_off;
2782 uint32_t tlv_off;
2783 uint32_t blk_sz;
2784 uint32_t bytes_copied = hdr->ih_hdr_size;
2785 uint32_t chunk_sz;
2786 uint32_t max_sz = 1024;
2787 uint16_t idx;
2788 uint8_t image_index;
2789 uint8_t * cur_dst;
2790 int area_id;
2791 int rc;
2792 uint8_t * ram_dst = (void *)(IMAGE_RAM_BASE + img_dst);
2793
2794 image_index = BOOT_CURR_IMG(state);
2795 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2796 rc = flash_area_open(area_id, &fap_src);
2797 if (rc != 0){
2798 return BOOT_EFLASH;
2799 }
2800
2801 tlv_off = BOOT_TLV_OFF(hdr);
2802
2803 /* Copying the whole image in RAM */
2804 rc = flash_area_read(fap_src, 0, ram_dst, src_sz);
2805 if (rc != 0) {
2806 goto done;
2807 }
2808
2809 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap_src, &bs);
2810 if (rc < 0) {
2811 goto done;
2812 }
2813
2814 /* if rc > 0 then the key has already been loaded */
2815 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), slot, &bs)) {
2816 goto done;
2817 }
2818
2819 /* Starting at the end of the header as the header section is not encrypted */
2820 while (bytes_copied < tlv_off) { /* TLV section copied previously */
2821 if (src_sz - bytes_copied > max_sz) {
2822 chunk_sz = max_sz;
2823 } else {
2824 chunk_sz = src_sz - bytes_copied;
2825 }
2826
2827 cur_dst = ram_dst + bytes_copied;
2828 blk_sz = chunk_sz;
2829 idx = 0;
2830 if (bytes_copied + chunk_sz > tlv_off) {
2831 /* Going over TLV section
2832 * Part of the chunk is encrypted payload */
2833 blk_off = ((bytes_copied) - hdr->ih_hdr_size) & 0xf;
2834 blk_sz = tlv_off - (bytes_copied);
2835 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
2836 (bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
2837 blk_off, cur_dst);
2838 } else {
2839 /* Image encrypted payload section */
2840 blk_off = ((bytes_copied) - hdr->ih_hdr_size) & 0xf;
2841 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
2842 (bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
2843 blk_off, cur_dst);
2844 }
2845
2846 bytes_copied += chunk_sz;
2847 }
2848 rc = 0;
2849
2850done:
2851 flash_area_close(fap_src);
2852
2853 return rc;
2854}
2855
2856#endif /* MCUBOOT_ENC_IMAGES */
2857/**
2858 * Copies a slot of the current image into SRAM.
2859 *
2860 * @param state Boot loader status information.
Tamas Banfe031092020-09-10 17:32:39 +02002861 * @param slot The flash slot of the image to be copied to SRAM.
2862 * @param img_dst The address at which the image needs to be copied to
2863 * SRAM.
2864 * @param img_sz The size of the image that needs to be copied to SRAM.
2865 *
2866 * @return 0 on success; nonzero on failure.
2867 */
2868static int
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002869boot_copy_image_to_sram(struct boot_loader_state *state, int slot,
2870 uint32_t img_dst, uint32_t img_sz)
Tamas Banfe031092020-09-10 17:32:39 +02002871{
2872 int rc;
2873 const struct flash_area *fap_src = NULL;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002874 int area_id;
Tamas Banfe031092020-09-10 17:32:39 +02002875
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002876#if (BOOT_IMAGE_NUMBER == 1)
2877 (void)state;
2878#endif
2879
2880 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2881
2882 rc = flash_area_open(area_id, &fap_src);
Tamas Banfe031092020-09-10 17:32:39 +02002883 if (rc != 0) {
2884 return BOOT_EFLASH;
2885 }
2886
2887 /* Direct copy from flash to its new location in SRAM. */
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002888 rc = flash_area_read(fap_src, 0, (void *)(IMAGE_RAM_BASE + img_dst), img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002889 if (rc != 0) {
2890 BOOT_LOG_INF("Error whilst copying image from Flash to SRAM: %d", rc);
2891 }
2892
2893 flash_area_close(fap_src);
2894
2895 return rc;
2896}
2897
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002898#if (BOOT_IMAGE_NUMBER > 1)
Tamas Banfe031092020-09-10 17:32:39 +02002899/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002900 * Checks if two memory regions (A and B) are overlap or not.
Tamas Banfe031092020-09-10 17:32:39 +02002901 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002902 * @param start_a Start of the A region.
2903 * @param end_a End of the A region.
2904 * @param start_b Start of the B region.
2905 * @param end_b End of the B region.
Tamas Banfe031092020-09-10 17:32:39 +02002906 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002907 * @return true if there is overlap; false otherwise.
2908 */
2909static bool
2910do_regions_overlap(uint32_t start_a, uint32_t end_a,
2911 uint32_t start_b, uint32_t end_b)
2912{
2913 if (start_b > end_a) {
2914 return false;
2915 } else if (start_b >= start_a) {
2916 return true;
2917 } else if (end_b > start_a) {
2918 return true;
2919 }
2920
2921 return false;
2922}
2923
2924/**
2925 * Checks if the image we want to load to memory overlap with an already
2926 * ramloaded image.
2927 *
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002928 * @param state Boot loader status information.
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002929 *
2930 * @return 0 if there is no overlap; nonzero otherwise.
Tamas Banfe031092020-09-10 17:32:39 +02002931 */
2932static int
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002933boot_check_ram_load_overlapping(struct boot_loader_state *state)
Tamas Banfe031092020-09-10 17:32:39 +02002934{
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002935 uint32_t i;
2936
2937 uint32_t start_a;
2938 uint32_t end_a;
2939 uint32_t start_b;
2940 uint32_t end_b;
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002941 uint32_t image_id_to_check = BOOT_CURR_IMG(state);
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002942
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002943 start_a = state->slot_usage[image_id_to_check].img_dst;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002944 /* Safe to add here, values are already verified in
2945 * boot_verify_ram_load_address() */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002946 end_a = start_a + state->slot_usage[image_id_to_check].img_sz;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002947
2948 for (i = 0; i < BOOT_IMAGE_NUMBER; i++) {
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002949 if (state->slot_usage[i].active_slot == NO_ACTIVE_SLOT
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002950 || i == image_id_to_check) {
2951 continue;
2952 }
2953
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002954 start_b = state->slot_usage[i].img_dst;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002955 /* Safe to add here, values are already verified in
2956 * boot_verify_ram_load_address() */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002957 end_b = start_b + state->slot_usage[i].img_sz;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002958
2959 if (do_regions_overlap(start_a, end_a, start_b, end_b)) {
2960 return -1;
2961 }
2962 }
2963
2964 return 0;
2965}
2966#endif
2967
2968/**
2969 * Loads the active slot of the current image into SRAM. The load address and
2970 * image size is extracted from the image header.
2971 *
2972 * @param state Boot loader status information.
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002973 *
2974 * @return 0 on success; nonzero on failure.
2975 */
2976static int
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002977boot_load_image_to_sram(struct boot_loader_state *state)
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002978{
2979 uint32_t active_slot;
2980 struct image_header *hdr = NULL;
2981 uint32_t img_dst;
2982 uint32_t img_sz;
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002983 int rc = 0;
Tamas Banfe031092020-09-10 17:32:39 +02002984
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002985 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002986 hdr = boot_img_hdr(state, active_slot);
2987
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002988 if (IS_RAM_BOOTABLE(hdr)) {
Tamas Banfe031092020-09-10 17:32:39 +02002989
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002990 img_dst = hdr->ih_load_addr;
Tamas Banfe031092020-09-10 17:32:39 +02002991
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002992 rc = boot_read_image_size(state, active_slot, &img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002993 if (rc != 0) {
2994 return rc;
2995 }
2996
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03002997 state->slot_usage[BOOT_CURR_IMG(state)].img_dst = img_dst;
2998 state->slot_usage[BOOT_CURR_IMG(state)].img_sz = img_sz;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002999
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003000 rc = boot_verify_ram_load_address(state);
Tamas Banfe031092020-09-10 17:32:39 +02003001 if (rc != 0) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003002 BOOT_LOG_INF("Image RAM load address 0x%" PRIx32 " is invalid.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02003003 return rc;
3004 }
3005
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003006#if (BOOT_IMAGE_NUMBER > 1)
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003007 rc = boot_check_ram_load_overlapping(state);
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003008 if (rc != 0) {
3009 BOOT_LOG_INF("Image RAM loading to address 0x%" PRIx32
3010 " would overlap with another image.", img_dst);
3011 return rc;
3012 }
3013#endif
3014#ifdef MCUBOOT_ENC_IMAGES
3015 /* decrypt image if encrypted and copy it to RAM */
3016 if (IS_ENCRYPTED(hdr)) {
3017 rc = boot_decrypt_and_copy_image_to_sram(state, active_slot, hdr, img_sz, img_dst);
3018 } else {
3019 rc = boot_copy_image_to_sram(state, active_slot, img_dst, img_sz);
3020 }
3021#else
Tamas Banfe031092020-09-10 17:32:39 +02003022 /* Copy image to the load address from where it currently resides in
3023 * flash.
3024 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003025 rc = boot_copy_image_to_sram(state, active_slot, img_dst, img_sz);
3026#endif
Tamas Banfe031092020-09-10 17:32:39 +02003027 if (rc != 0) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003028 BOOT_LOG_INF("RAM loading to 0x%" PRIx32 " is failed.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02003029 } else {
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003030 BOOT_LOG_INF("RAM loading to 0x%" PRIx32 " is succeeded.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02003031 }
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003032 }
3033 else {
Tamas Banfe031092020-09-10 17:32:39 +02003034 /* Only images that support IMAGE_F_RAM_LOAD are allowed if
3035 * MCUBOOT_RAM_LOAD is set.
3036 */
3037 rc = BOOT_EBADIMAGE;
3038 }
3039
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003040 if (rc != 0) {
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003041 state->slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
3042 state->slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003043 }
3044
Tamas Banfe031092020-09-10 17:32:39 +02003045 return rc;
3046}
3047
3048/**
3049 * Removes an image from SRAM, by overwriting it with zeros.
3050 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003051 * @param state Boot loader status information.
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003052 *
3053 * @return 0 on success; nonzero on failure.
3054 */
3055static inline int
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003056boot_remove_image_from_sram(struct boot_loader_state *state)
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003057{
3058 (void)state;
3059
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003060 BOOT_LOG_INF("Removing image from SRAM at address 0x%x",
3061 state->slot_usage[BOOT_CURR_IMG(state)].img_dst);
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003062
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003063 (void)memset((void*)(IMAGE_RAM_BASE + state->slot_usage[BOOT_CURR_IMG(state)].img_dst),
3064 0, state->slot_usage[BOOT_CURR_IMG(state)].img_sz);
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003065
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003066 state->slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
3067 state->slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003068
3069 return 0;
3070}
3071
3072/**
3073 * Removes an image from flash by erasing the corresponding flash area
3074 *
3075 * @param state Boot loader status information.
3076 * @param slot The flash slot of the image to be erased.
Tamas Banfe031092020-09-10 17:32:39 +02003077 *
3078 * @return 0 on success; nonzero on failure.
3079 */
3080static inline int
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003081boot_remove_image_from_flash(struct boot_loader_state *state, uint32_t slot)
Tamas Banfe031092020-09-10 17:32:39 +02003082{
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003083 int area_id;
3084 int rc;
3085 const struct flash_area *fap;
Tamas Banfe031092020-09-10 17:32:39 +02003086
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003087 (void)state;
3088
3089 BOOT_LOG_INF("Removing image %u slot %" PRIu32 " from flash",
3090 (unsigned)BOOT_CURR_IMG(state), slot);
3091 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
3092 rc = flash_area_open(area_id, &fap);
3093 if (rc == 0) {
3094 flash_area_erase(fap, 0, flash_area_get_size(fap));
3095 }
3096
3097 return rc;
Tamas Banfe031092020-09-10 17:32:39 +02003098}
3099#endif /* MCUBOOT_RAM_LOAD */
3100
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003101#if (BOOT_IMAGE_NUMBER > 1)
3102/**
3103 * Checks the image dependency whether it is satisfied.
3104 *
3105 * @param state Boot loader status information.
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003106 * @param dep Image dependency which has to be verified.
3107 *
3108 * @return 0 if dependencies are met; nonzero otherwise.
3109 */
3110static int
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003111boot_verify_slot_dependency_ram(struct boot_loader_state *state,
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003112 struct image_dependency *dep)
David Vinczee574f2d2020-07-10 11:42:03 +02003113{
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003114 struct image_version *dep_version;
3115 uint32_t dep_slot;
David Vinczee574f2d2020-07-10 11:42:03 +02003116 int rc;
3117
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003118 /* Determine the source of the image which is the subject of
3119 * the dependency and get it's version.
David Vinczee574f2d2020-07-10 11:42:03 +02003120 */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003121 dep_slot = state->slot_usage[dep->image_id].active_slot;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003122 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
3123
3124 rc = boot_version_cmp(dep_version, &dep->image_min_version);
3125 if (rc >= 0) {
3126 /* Dependency satisfied. */
3127 rc = 0;
David Vinczee574f2d2020-07-10 11:42:03 +02003128 }
3129
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003130 return rc;
3131}
3132
3133/**
3134 * Reads all dependency TLVs of an image and verifies one after another to see
3135 * if they are all satisfied.
3136 *
3137 * @param state Boot loader status information.
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003138 *
3139 * @return 0 if dependencies are met; nonzero otherwise.
3140 */
3141static int
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003142boot_verify_slot_dependencies_ram(struct boot_loader_state *state)
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003143{
3144 uint32_t active_slot;
3145 const struct flash_area *fap;
3146 struct image_tlv_iter it;
3147 struct image_dependency dep;
3148 uint32_t off;
3149 uint16_t len;
3150 int area_id;
3151 int rc;
3152
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003153 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003154
3155 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state),
3156 active_slot);
3157 rc = flash_area_open(area_id, &fap);
David Vinczee574f2d2020-07-10 11:42:03 +02003158 if (rc != 0) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003159 rc = BOOT_EFLASH;
3160 goto done;
David Vinczee574f2d2020-07-10 11:42:03 +02003161 }
3162
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003163 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, active_slot), fap,
3164 IMAGE_TLV_DEPENDENCY, true);
3165 if (rc != 0) {
3166 goto done;
3167 }
David Vinczee574f2d2020-07-10 11:42:03 +02003168
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003169 while (true) {
3170 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
3171 if (rc < 0) {
3172 return -1;
3173 } else if (rc > 0) {
3174 rc = 0;
3175 break;
3176 }
David Vinczee574f2d2020-07-10 11:42:03 +02003177
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003178 if (len != sizeof(dep)) {
3179 rc = BOOT_EBADIMAGE;
3180 goto done;
3181 }
3182
3183 rc = LOAD_IMAGE_DATA(boot_img_hdr(state, active_slot),
3184 fap, off, &dep, len);
3185 if (rc != 0) {
3186 rc = BOOT_EFLASH;
3187 goto done;
3188 }
3189
3190 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
3191 rc = BOOT_EBADARGS;
3192 goto done;
3193 }
3194
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003195 rc = boot_verify_slot_dependency_ram(state, &dep);
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003196 if (rc != 0) {
3197 /* Dependency not satisfied. */
3198 goto done;
3199 }
3200 }
3201
3202done:
3203 flash_area_close(fap);
3204 return rc;
3205}
3206
3207/**
3208 * Checks the dependency of all the active slots. If an image found with
3209 * invalid or not satisfied dependencies the image is removed from SRAM (in
3210 * case of MCUBOOT_RAM_LOAD strategy) and its slot is set to unavailable.
3211 *
3212 * @param state Boot loader status information.
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003213 *
3214 * @return 0 if dependencies are met; nonzero otherwise.
3215 */
3216static int
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003217boot_verify_dependencies_ram(struct boot_loader_state *state)
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003218{
3219 int rc = -1;
3220 uint32_t active_slot;
3221
3222 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003223 if (state->img_mask[BOOT_CURR_IMG(state)]) {
3224 continue;
3225 }
3226 rc = boot_verify_slot_dependencies_ram(state);
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003227 if (rc != 0) {
3228 /* Dependencies not met or invalid dependencies. */
3229
3230#ifdef MCUBOOT_RAM_LOAD
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003231 boot_remove_image_from_sram(state);
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003232#endif /* MCUBOOT_RAM_LOAD */
3233
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003234 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
3235 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3236 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003237
3238 return rc;
3239 }
3240 }
3241
3242 return rc;
3243}
3244#endif /* (BOOT_IMAGE_NUMBER > 1) */
3245
3246/**
3247 * Tries to load a slot for all the images with validation.
3248 *
3249 * @param state Boot loader status information.
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003250 *
3251 * @return 0 on success; nonzero on failure.
3252 */
3253fih_int
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003254boot_load_and_validate_images(struct boot_loader_state *state)
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003255{
3256 uint32_t active_slot;
3257 int rc;
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +02003258#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
3259 fih_int fih_rc = FIH_FAILURE;
3260#endif
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003261
3262 /* Go over all the images and try to load one */
3263 IMAGES_ITER(BOOT_CURR_IMG(state)) {
3264 /* All slots tried until a valid image found. Breaking from this loop
3265 * means that a valid image found or already loaded. If no slot is
3266 * found the function returns with error code. */
3267 while (true) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003268 /* Go over all the slots and try to load one */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003269 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003270 if (active_slot != NO_ACTIVE_SLOT){
3271 /* A slot is already active, go to next image. */
3272 break;
3273 }
3274
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003275 active_slot = find_slot_with_highest_version(state);
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003276 if (active_slot == NO_ACTIVE_SLOT) {
3277 BOOT_LOG_INF("No slot to load for image %u",
3278 (unsigned)BOOT_CURR_IMG(state));
3279 FIH_RET(FIH_FAILURE);
3280 }
3281
3282 /* Save the number of the active slot. */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003283 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = active_slot;
3284
3285#if BOOT_IMAGE_NUMBER > 1
3286 if (state->img_mask[BOOT_CURR_IMG(state)]) {
3287 continue;
3288 }
3289#endif
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003290
3291#ifdef MCUBOOT_DIRECT_XIP
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003292 rc = boot_rom_address_check(state);
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003293 if (rc != 0) {
3294 /* The image is placed in an unsuitable slot. */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003295 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3296 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003297 continue;
David Vinczee574f2d2020-07-10 11:42:03 +02003298 }
David Vincze505fba22020-10-22 13:53:29 +02003299
3300#ifdef MCUBOOT_DIRECT_XIP_REVERT
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003301 rc = boot_select_or_erase(state);
David Vincze505fba22020-10-22 13:53:29 +02003302 if (rc != 0) {
3303 /* The selected image slot has been erased. */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003304 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3305 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
David Vincze505fba22020-10-22 13:53:29 +02003306 continue;
3307 }
3308#endif /* MCUBOOT_DIRECT_XIP_REVERT */
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003309#endif /* MCUBOOT_DIRECT_XIP */
David Vincze505fba22020-10-22 13:53:29 +02003310
Tamas Banfe031092020-09-10 17:32:39 +02003311#ifdef MCUBOOT_RAM_LOAD
3312 /* Image is first loaded to RAM and authenticated there in order to
3313 * prevent TOCTOU attack during image copy. This could be applied
3314 * when loading images from external (untrusted) flash to internal
3315 * (trusted) RAM and image is authenticated before copying.
3316 */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003317 rc = boot_load_image_to_sram(state);
Tamas Banfe031092020-09-10 17:32:39 +02003318 if (rc != 0 ) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003319 /* Image cannot be ramloaded. */
3320 boot_remove_image_from_flash(state, active_slot);
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003321 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3322 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Tamas Banfe031092020-09-10 17:32:39 +02003323 continue;
Tamas Banfe031092020-09-10 17:32:39 +02003324 }
3325#endif /* MCUBOOT_RAM_LOAD */
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +02003326#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003327 FIH_CALL(boot_validate_slot, fih_rc, state, active_slot, NULL);
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003328 if (fih_eq(fih_rc, FIH_SUCCESS) != FIH_TRUE) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003329 /* Image is invalid. */
Tamas Banfe031092020-09-10 17:32:39 +02003330#ifdef MCUBOOT_RAM_LOAD
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003331 boot_remove_image_from_sram(state);
Tamas Banfe031092020-09-10 17:32:39 +02003332#endif /* MCUBOOT_RAM_LOAD */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003333 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3334 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003335 continue;
David Vincze505fba22020-10-22 13:53:29 +02003336 }
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +02003337#endif
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003338 /* Valid image loaded from a slot, go to next image. */
3339 break;
David Vinczee574f2d2020-07-10 11:42:03 +02003340 }
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003341 }
3342
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003343 (void) rc;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003344 FIH_RET(FIH_SUCCESS);
3345}
3346
3347/**
3348 * Updates the security counter for the current image.
3349 *
3350 * @param state Boot loader status information.
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003351 *
3352 * @return 0 on success; nonzero on failure.
3353 */
3354static int
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003355boot_update_hw_rollback_protection_ram(struct boot_loader_state *state)
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003356{
3357#ifdef MCUBOOT_HW_ROLLBACK_PROT
3358 int rc;
3359
3360 /* Update the stored security counter with the newer (active) image's
3361 * security counter value.
3362 */
3363#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
3364 /* When the 'revert' mechanism is enabled in direct-xip mode, the
3365 * security counter can be increased only after reboot, if the image
3366 * has been confirmed at runtime (the image_ok flag has been set).
3367 * This way a 'revert' can be performed when it's necessary.
3368 */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003369 if (state->slot_usage[BOOT_CURR_IMG(state)].swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze505fba22020-10-22 13:53:29 +02003370#endif
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003371 rc = boot_update_security_counter(BOOT_CURR_IMG(state),
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003372 state->slot_usage[BOOT_CURR_IMG(state)].active_slot,
3373 boot_img_hdr(state, state->slot_usage[BOOT_CURR_IMG(state)].active_slot));
David Vinczee574f2d2020-07-10 11:42:03 +02003374 if (rc != 0) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003375 BOOT_LOG_ERR("Security counter update failed after image "
3376 "validation.");
3377 return rc;
David Vinczee574f2d2020-07-10 11:42:03 +02003378 }
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003379#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
3380 }
3381#endif
David Vinczee574f2d2020-07-10 11:42:03 +02003382
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003383 return 0;
David Vinczee574f2d2020-07-10 11:42:03 +02003384
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003385#else /* MCUBOOT_HW_ROLLBACK_PROT */
3386 (void) (state);
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003387 return 0;
3388#endif
3389}
David Vinczee574f2d2020-07-10 11:42:03 +02003390
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003391fih_int
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003392context_boot_go_ram(struct boot_loader_state *state, struct boot_rsp *rsp)
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003393{
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003394 int rc;
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003395 fih_int fih_rc = FIH_FAILURE;
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +02003396 boot_ram = true;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003397
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003398 rc = boot_get_slot_usage(state);
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003399 if (rc != 0) {
David Vinczee574f2d2020-07-10 11:42:03 +02003400 goto out;
3401 }
3402
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003403#if (BOOT_IMAGE_NUMBER > 1)
3404 while (true) {
3405#endif
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003406 FIH_CALL(boot_load_and_validate_images, fih_rc, state);
3407 if (fih_eq(fih_rc, FIH_SUCCESS) != FIH_TRUE) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003408 goto out;
3409 }
3410
3411#if (BOOT_IMAGE_NUMBER > 1)
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003412 rc = boot_verify_dependencies_ram(state);
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003413 if (rc != 0) {
3414 /* Dependency check failed for an image, it has been removed from
3415 * SRAM in case of MCUBOOT_RAM_LOAD strategy, and set to
3416 * unavailable. Try to load an image from another slot.
3417 */
3418 continue;
3419 }
3420 /* Dependency check was successful. */
3421 break;
3422 }
3423#endif
3424
3425 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003426#if BOOT_IMAGE_NUMBER > 1
3427 if (state->img_mask[BOOT_CURR_IMG(state)]) {
3428 continue;
3429 }
3430#endif
3431 rc = boot_update_hw_rollback_protection_ram(state);
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003432 if (rc != 0) {
3433 goto out;
3434 }
3435
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003436 rc = boot_add_shared_data(state, state->slot_usage[BOOT_CURR_IMG(state)].active_slot);
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003437 if (rc != 0) {
3438 goto out;
3439 }
3440 }
3441
3442 /* All image loaded successfully. */
3443#ifdef MCUBOOT_HAVE_LOGGING
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003444 print_loaded_images(state);
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003445#endif
3446
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003447 fill_rsp(state, rsp);
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003448
David Vinczee574f2d2020-07-10 11:42:03 +02003449out:
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003450 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01003451
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003452 if (FIH_TRUE == fih_eq(fih_rc, FIH_SUCCESS)) {
3453 fih_rc = fih_int_encode_zero_equality(rc);
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003454 }
Raef Colese8fe6cf2020-05-26 13:07:40 +01003455
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +02003456 boot_ram = false;
3457
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003458 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02003459}
Tamas Banfe031092020-09-10 17:32:39 +02003460#endif /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02003461
3462/**
Raef Colese8fe6cf2020-05-26 13:07:40 +01003463 * Prepares the booting process. This function moves images around in flash as
David Vinczee574f2d2020-07-10 11:42:03 +02003464 * appropriate, and tells you what address to boot from.
3465 *
3466 * @param rsp On success, indicates how booting should occur.
3467 *
Raef Colese8fe6cf2020-05-26 13:07:40 +01003468 * @return FIH_SUCCESS on success; nonzero on failure.
David Vinczee574f2d2020-07-10 11:42:03 +02003469 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01003470fih_int
David Vinczee574f2d2020-07-10 11:42:03 +02003471boot_go(struct boot_rsp *rsp)
3472{
Raef Colese8fe6cf2020-05-26 13:07:40 +01003473 fih_int fih_rc = FIH_FAILURE;
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003474
3475 boot_state_clear(NULL);
3476
3477 FIH_CALL(context_boot_go_flash, fih_rc, &boot_data, rsp);
Raef Colese8fe6cf2020-05-26 13:07:40 +01003478 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02003479}
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03003480
3481/**
3482 * Prepares the booting process, considering only a single image. This function
3483 * moves images around in flash as appropriate, and tells you what address to
3484 * boot from.
3485 *
3486 * @param rsp On success, indicates how booting should occur.
3487 *
3488 * @param image_id The image ID to prepare the boot process for.
3489 *
3490 * @return FIH_SUCCESS on success; nonzero on failure.
3491 */
3492fih_int
3493boot_go_for_image_id(struct boot_rsp *rsp, uint32_t image_id)
3494{
3495 fih_int fih_rc = FIH_FAILURE;
3496
3497 if (image_id >= BOOT_IMAGE_NUMBER) {
3498 FIH_RET(FIH_FAILURE);
3499 }
3500
3501#if BOOT_IMAGE_NUMBER > 1
3502 (void)memset(&boot_data.img_mask, 1, BOOT_IMAGE_NUMBER);
3503 boot_data.img_mask[image_id] = 0;
3504#endif
3505
3506 FIH_CALL(context_boot_go_flash, fih_rc, &boot_data, rsp);
3507 FIH_RET(fih_rc);
3508}
3509
3510#if defined(MCUBOOT_RAM_LOAD)
3511/**
3512 * Prepares the booting process, considering only a single image. This function
3513 * moves images around in flash as appropriate, and tells you what address to
3514 * boot from.
3515 *
3516 * @param rsp On success, indicates how booting should occur.
3517 *
3518 * @param image_id The image ID to prepare the boot process for.
3519 *
3520 * @return FIH_SUCCESS on success; nonzero on failure.
3521 */
3522fih_int
3523boot_go_for_image_id_ram(struct boot_rsp *rsp, uint32_t image_id)
3524{
3525 fih_int fih_rc = FIH_FAILURE;
3526
3527 if (image_id >= BOOT_IMAGE_NUMBER) {
3528 FIH_RET(FIH_FAILURE);
3529 }
3530
3531#if BOOT_IMAGE_NUMBER > 1
3532 (void)memset(&boot_data.img_mask, 1, BOOT_IMAGE_NUMBER);
3533 boot_data.img_mask[image_id] = 0;
3534#endif
3535
3536 FIH_CALL(context_boot_go_ram, fih_rc, &boot_data, rsp);
3537 FIH_RET(fih_rc);
3538}
3539
3540#endif /* MCUBOOT_RAM_LOAD */
3541
3542/**
3543 * Clears the boot state, so that previous operations have no effect on new
3544 * ones.
3545 *
3546 * @param state The state that should be cleared. If the value
3547 * is NULL, the default bootloader state will be
3548 * cleared.
3549 */
3550void boot_state_clear(struct boot_loader_state *state)
3551{
3552 if (state != NULL) {
3553 (void)memset(state, 0, sizeof(struct boot_loader_state));
3554 } else {
3555 (void)memset(&boot_data, 0, sizeof(struct boot_loader_state));
3556 }
3557}