blob: d8ce649c62fe060822231c8f918d10ab8b89e095 [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
David Brownaac71112020-02-03 16:13:42 -07002 * SPDX-License-Identifier: Apache-2.0
3 *
4 * Copyright (c) 2016-2020 Linaro LTD
5 * Copyright (c) 2016-2019 JUUL Labs
Antonio de Angelisba5fb1c2022-10-11 10:10:37 +01006 * Copyright (c) 2019-2023 Arm Limited
Dominik Ermel6f7f8732024-02-01 11:59:24 +00007 * Copyright (c) 2024 Nordic Semiconductor ASA
David Brownaac71112020-02-03 16:13:42 -07008 *
9 * Original license:
10 *
Christopher Collins92ea77f2016-12-12 15:59:26 -080011 * Licensed to the Apache Software Foundation (ASF) under one
12 * or more contributor license agreements. See the NOTICE file
13 * distributed with this work for additional information
14 * regarding copyright ownership. The ASF licenses this file
15 * to you under the Apache License, Version 2.0 (the
16 * "License"); you may not use this file except in compliance
17 * with the License. You may obtain a copy of the License at
18 *
19 * http://www.apache.org/licenses/LICENSE-2.0
20 *
21 * Unless required by applicable law or agreed to in writing,
22 * software distributed under the License is distributed on an
23 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
24 * KIND, either express or implied. See the License for the
25 * specific language governing permissions and limitations
26 * under the License.
27 */
28
29/**
30 * This file provides an interface to the boot loader. Functions defined in
31 * this file should only be called while the boot loader is running.
32 */
33
Christopher Collins92ea77f2016-12-12 15:59:26 -080034#include <stddef.h>
David Brown52eee562017-07-05 11:25:09 -060035#include <stdbool.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080036#include <inttypes.h>
37#include <stdlib.h>
38#include <string.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080039#include "bootutil/bootutil.h"
Kristine Jassmann73c38c62021-02-03 16:56:14 +000040#include "bootutil/bootutil_public.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080041#include "bootutil/image.h"
42#include "bootutil_priv.h"
Fabio Utzig12d59162019-11-28 10:01:59 -030043#include "swap_priv.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050044#include "bootutil/bootutil_log.h"
David Vinczec3084132020-02-18 14:50:47 +010045#include "bootutil/security_cnt.h"
David Vincze1cf11b52020-03-24 07:51:09 +010046#include "bootutil/boot_record.h"
Raef Colese8fe6cf2020-05-26 13:07:40 +010047#include "bootutil/fault_injection_hardening.h"
Mark Horvathccaf7f82021-01-04 18:16:42 +010048#include "bootutil/ramload.h"
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +020049#include "bootutil/boot_hooks.h"
Jamie McCrae56cb6102022-03-23 11:57:03 +000050#include "bootutil/mcuboot_status.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050051
Fabio Utzigba829042018-09-18 08:29:34 -030052#ifdef MCUBOOT_ENC_IMAGES
53#include "bootutil/enc_key.h"
54#endif
55
Carlos Falgueras García391b1972021-06-21 16:58:07 +020056#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
57#include <os/os_malloc.h>
58#endif
59
Fabio Utzigba1fbe62017-07-21 14:01:20 -030060#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030061
Carlos Falgueras Garcíaa4b4b0f2021-06-22 10:00:22 +020062BOOT_LOG_MODULE_DECLARE(mcuboot);
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010063
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040064static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080065
Fabio Utzigabec0732019-07-31 08:40:22 -030066#if (BOOT_IMAGE_NUMBER > 1)
67#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
68#else
69#define IMAGES_ITER(x)
70#endif
71
Fabio Utzig10ee6482019-08-01 12:04:52 -030072/*
73 * This macro allows some control on the allocation of local variables.
74 * When running natively on a target, we don't want to allocated huge
75 * variables on the stack, so make them global instead. For the simulator
76 * we want to run as many threads as there are tests, and it's safer
77 * to just make those variables stack allocated.
78 */
79#if !defined(__BOOTSIM__)
80#define TARGET_STATIC static
81#else
82#define TARGET_STATIC
83#endif
84
Kristine Jassmann73c38c62021-02-03 16:56:14 +000085#if BOOT_MAX_ALIGN > 1024
86#define BUF_SZ BOOT_MAX_ALIGN
87#else
88#define BUF_SZ 1024
89#endif
90
Dominik Ermelaafcbad2024-02-29 20:40:20 +000091#define NO_ACTIVE_SLOT UINT32_MAX
92
David Vinczee574f2d2020-07-10 11:42:03 +020093static int
94boot_read_image_headers(struct boot_loader_state *state, bool require_all,
95 struct boot_status *bs)
96{
97 int rc;
98 int i;
99
100 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +0200101 rc = BOOT_HOOK_CALL(boot_read_image_header_hook, BOOT_HOOK_REGULAR,
102 BOOT_CURR_IMG(state), i, boot_img_hdr(state, i));
103 if (rc == BOOT_HOOK_REGULAR)
104 {
105 rc = boot_read_image_header(state, i, boot_img_hdr(state, i), bs);
106 }
David Vinczee574f2d2020-07-10 11:42:03 +0200107 if (rc != 0) {
108 /* If `require_all` is set, fail on any single fail, otherwise
109 * if at least the first slot's header was read successfully,
110 * then the boot loader can attempt a boot.
111 *
112 * Failure to read any headers is a fatal error.
113 */
114 if (i > 0 && !require_all) {
115 return 0;
116 } else {
117 return rc;
118 }
119 }
120 }
121
122 return 0;
123}
124
Mark Horvathccaf7f82021-01-04 18:16:42 +0100125/**
126 * Saves boot status and shared data for current image.
127 *
128 * @param state Boot loader status information.
129 * @param active_slot Index of the slot will be loaded for current image.
130 *
131 * @return 0 on success; nonzero on failure.
132 */
133static int
134boot_add_shared_data(struct boot_loader_state *state,
Jamie McCrae3016d002023-03-14 12:35:51 +0000135 uint8_t active_slot)
Mark Horvathccaf7f82021-01-04 18:16:42 +0100136{
137#if defined(MCUBOOT_MEASURED_BOOT) || defined(MCUBOOT_DATA_SHARING)
138 int rc;
139
Jamie McCrae3016d002023-03-14 12:35:51 +0000140#ifdef MCUBOOT_DATA_SHARING
141 int max_app_size;
142#endif
143
Mark Horvathccaf7f82021-01-04 18:16:42 +0100144#ifdef MCUBOOT_MEASURED_BOOT
145 rc = boot_save_boot_status(BOOT_CURR_IMG(state),
146 boot_img_hdr(state, active_slot),
147 BOOT_IMG_AREA(state, active_slot));
148 if (rc != 0) {
149 BOOT_LOG_ERR("Failed to add image data to shared area");
150 return rc;
151 }
152#endif /* MCUBOOT_MEASURED_BOOT */
153
154#ifdef MCUBOOT_DATA_SHARING
Jamie McCrae3016d002023-03-14 12:35:51 +0000155 max_app_size = app_max_size(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +0100156 rc = boot_save_shared_data(boot_img_hdr(state, active_slot),
Jamie McCrae3016d002023-03-14 12:35:51 +0000157 BOOT_IMG_AREA(state, active_slot),
158 active_slot, max_app_size);
Mark Horvathccaf7f82021-01-04 18:16:42 +0100159 if (rc != 0) {
160 BOOT_LOG_ERR("Failed to add data to shared memory area.");
161 return rc;
162 }
163#endif /* MCUBOOT_DATA_SHARING */
164
165 return 0;
166
167#else /* MCUBOOT_MEASURED_BOOT || MCUBOOT_DATA_SHARING */
168 (void) (state);
169 (void) (active_slot);
170
171 return 0;
172#endif
173}
174
175/**
176 * Fills rsp to indicate how booting should occur.
177 *
178 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +0100179 * @param rsp boot_rsp struct to fill.
180 */
181static void
Raef Colesfe57e7d2021-10-15 11:07:09 +0100182fill_rsp(struct boot_loader_state *state, struct boot_rsp *rsp)
Mark Horvathccaf7f82021-01-04 18:16:42 +0100183{
184 uint32_t active_slot;
185
186#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesf11de642021-10-15 11:11:56 +0100187 /* Always boot from the first enabled image. */
Mark Horvathccaf7f82021-01-04 18:16:42 +0100188 BOOT_CURR_IMG(state) = 0;
Raef Colesf11de642021-10-15 11:11:56 +0100189 IMAGES_ITER(BOOT_CURR_IMG(state)) {
190 if (!state->img_mask[BOOT_CURR_IMG(state)]) {
191 break;
192 }
193 }
INFINEON\DovhalA94360d52023-01-18 17:21:56 +0200194 /* At least one image must be active, otherwise skip the execution */
195 if (BOOT_CURR_IMG(state) >= BOOT_IMAGE_NUMBER) {
196 return;
197 }
Mark Horvathccaf7f82021-01-04 18:16:42 +0100198#endif
199
200#if defined(MCUBOOT_DIRECT_XIP) || defined(MCUBOOT_RAM_LOAD)
Raef Colesfe57e7d2021-10-15 11:07:09 +0100201 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +0100202#else
Mark Horvathccaf7f82021-01-04 18:16:42 +0100203 active_slot = BOOT_PRIMARY_SLOT;
204#endif
205
Dominik Ermel260ae092021-04-23 05:38:45 +0000206 rsp->br_flash_dev_id = flash_area_get_device_id(BOOT_IMG_AREA(state, active_slot));
Mark Horvathccaf7f82021-01-04 18:16:42 +0100207 rsp->br_image_off = boot_img_slot_off(state, active_slot);
208 rsp->br_hdr = boot_img_hdr(state, active_slot);
209}
210
211/**
212 * Closes all flash areas.
213 *
214 * @param state Boot loader status information.
215 */
216static void
217close_all_flash_areas(struct boot_loader_state *state)
218{
219 uint32_t slot;
220
221 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +0100222#if BOOT_IMAGE_NUMBER > 1
223 if (state->img_mask[BOOT_CURR_IMG(state)]) {
224 continue;
225 }
226#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +0100227#if MCUBOOT_SWAP_USING_SCRATCH
228 flash_area_close(BOOT_SCRATCH_AREA(state));
229#endif
230 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
231 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
232 }
233 }
234}
235
Dominik Ermelaafcbad2024-02-29 20:40:20 +0000236#if (BOOT_IMAGE_NUMBER > 1) || \
237 defined(MCUBOOT_DIRECT_XIP) || \
238 defined(MCUBOOT_RAM_LOAD) || \
239 defined(MCUBOOT_DOWNGRADE_PREVENTION)
240/**
241 * Compare image version numbers
242 *
243 * By default, the comparison does not take build number into account.
244 * Enable MCUBOOT_VERSION_CMP_USE_BUILD_NUMBER to take the build number into account.
245 *
246 * @param ver1 Pointer to the first image version to compare.
247 * @param ver2 Pointer to the second image version to compare.
248 *
249 * @retval -1 If ver1 is less than ver2.
250 * @retval 0 If the image version numbers are equal.
251 * @retval 1 If ver1 is greater than ver2.
252 */
253static int
254boot_version_cmp(const struct image_version *ver1,
255 const struct image_version *ver2)
256{
257 if (ver1->iv_major > ver2->iv_major) {
258 return 1;
259 }
260 if (ver1->iv_major < ver2->iv_major) {
261 return -1;
262 }
263 /* The major version numbers are equal, continue comparison. */
264 if (ver1->iv_minor > ver2->iv_minor) {
265 return 1;
266 }
267 if (ver1->iv_minor < ver2->iv_minor) {
268 return -1;
269 }
270 /* The minor version numbers are equal, continue comparison. */
271 if (ver1->iv_revision > ver2->iv_revision) {
272 return 1;
273 }
274 if (ver1->iv_revision < ver2->iv_revision) {
275 return -1;
276 }
277
278#if defined(MCUBOOT_VERSION_CMP_USE_BUILD_NUMBER)
279 /* The revisions are equal, continue comparison. */
280 if (ver1->iv_build_num > ver2->iv_build_num) {
281 return 1;
282 }
283 if (ver1->iv_build_num < ver2->iv_build_num) {
284 return -1;
285 }
286#endif
287
288 return 0;
289}
290#endif
291
292
293#if (BOOT_IMAGE_NUMBER > 1)
294
295static int
296boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot);
297
298/**
299 * Check the image dependency whether it is satisfied and modify
300 * the swap type if necessary.
301 *
302 * @param dep Image dependency which has to be verified.
303 *
304 * @return 0 on success; nonzero on failure.
305 */
306static int
307boot_verify_slot_dependency(struct boot_loader_state *state,
308 struct image_dependency *dep)
309{
310 struct image_version *dep_version;
311 size_t dep_slot;
312 int rc;
313
314 /* Determine the source of the image which is the subject of
315 * the dependency and get it's version. */
316#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
317 uint8_t swap_type = state->swap_type[dep->image_id];
318 dep_slot = BOOT_IS_UPGRADE(swap_type) ? BOOT_SECONDARY_SLOT
319 : BOOT_PRIMARY_SLOT;
320#else
321 dep_slot = state->slot_usage[dep->image_id].active_slot;
322#endif
323
324 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
325
326 rc = boot_version_cmp(dep_version, &dep->image_min_version);
327#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
328 if (rc < 0) {
329 /* Dependency not satisfied.
330 * Modify the swap type to decrease the version number of the image
331 * (which will be located in the primary slot after the boot process),
332 * consequently the number of unsatisfied dependencies will be
333 * decreased or remain the same.
334 */
335 switch (BOOT_SWAP_TYPE(state)) {
336 case BOOT_SWAP_TYPE_TEST:
337 case BOOT_SWAP_TYPE_PERM:
338 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
339 break;
340 case BOOT_SWAP_TYPE_NONE:
341 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
342 break;
343 default:
344 break;
345 }
346 } else {
347 /* Dependency satisfied. */
348 rc = 0;
349 }
350#else
351 if (rc >= 0) {
352 /* Dependency satisfied. */
353 rc = 0;
354 }
355#endif
356
357 return rc;
358}
359
360#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
361/**
362 * Iterate over all the images and verify whether the image dependencies in the
363 * TLV area are all satisfied and update the related swap type if necessary.
364 */
365static int
366boot_verify_dependencies(struct boot_loader_state *state)
367{
368 int rc = -1;
369 uint8_t slot;
370
371 BOOT_CURR_IMG(state) = 0;
372 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
373 if (state->img_mask[BOOT_CURR_IMG(state)]) {
374 BOOT_CURR_IMG(state)++;
375 continue;
376 }
377 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
378 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
379 slot = BOOT_SECONDARY_SLOT;
380 } else {
381 slot = BOOT_PRIMARY_SLOT;
382 }
383
384 rc = boot_verify_slot_dependencies(state, slot);
385 if (rc == 0) {
386 /* All dependencies've been satisfied, continue with next image. */
387 BOOT_CURR_IMG(state)++;
388 } else {
389 /* Cannot upgrade due to non-met dependencies, so disable all
390 * image upgrades.
391 */
392 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
393 BOOT_CURR_IMG(state) = idx;
394 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
395 }
396 break;
397 }
398 }
399 return rc;
400}
401#else
402
403#if defined MCUBOOT_RAM_LOAD
404static inline int
405boot_remove_image_from_sram(struct boot_loader_state *state);
406#endif
407
408/**
409 * Checks the dependency of all the active slots. If an image found with
410 * invalid or not satisfied dependencies the image is removed from SRAM (in
411 * case of MCUBOOT_RAM_LOAD strategy) and its slot is set to unavailable.
412 *
413 * @param state Boot loader status information.
414 *
415 * @return 0 if dependencies are met; nonzero otherwise.
416 */
417static int
418boot_verify_dependencies(struct boot_loader_state *state)
419{
420 int rc = -1;
421 uint32_t active_slot;
422
423 IMAGES_ITER(BOOT_CURR_IMG(state)) {
424 if (state->img_mask[BOOT_CURR_IMG(state)]) {
425 continue;
426 }
427 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
428 rc = boot_verify_slot_dependencies(state, active_slot);
429 if (rc != 0) {
430 /* Dependencies not met or invalid dependencies. */
431
432#ifdef MCUBOOT_RAM_LOAD
433 boot_remove_image_from_sram(state);
434#endif /* MCUBOOT_RAM_LOAD */
435
436 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
437 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
438
439 return rc;
440 }
441 }
442
443 return rc;
444}
445#endif
446
447/**
448 * Read all dependency TLVs of an image from the flash and verify
449 * one after another to see if they are all satisfied.
450 *
451 * @param slot Image slot number.
452 *
453 * @return 0 on success; nonzero on failure.
454 */
455static int
456boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
457{
458 const struct flash_area *fap;
459 struct image_tlv_iter it;
460 struct image_dependency dep;
461 uint32_t off;
462 uint16_t len;
463 int area_id;
464 int rc;
465
466 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
467 rc = flash_area_open(area_id, &fap);
468 if (rc != 0) {
469 rc = BOOT_EFLASH;
470 goto done;
471 }
472
473 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
474 IMAGE_TLV_DEPENDENCY, true);
475 if (rc != 0) {
476 goto done;
477 }
478
479 while (true) {
480 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
481 if (rc < 0) {
482 return -1;
483 } else if (rc > 0) {
484 rc = 0;
485 break;
486 }
487
488 if (len != sizeof(dep)) {
489 rc = BOOT_EBADIMAGE;
490 goto done;
491 }
492
493 rc = LOAD_IMAGE_DATA(boot_img_hdr(state, slot),
494 fap, off, &dep, len);
495 if (rc != 0) {
496 rc = BOOT_EFLASH;
497 goto done;
498 }
499
500 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
501 rc = BOOT_EBADARGS;
502 goto done;
503 }
504
505 /* Verify dependency and modify the swap type if not satisfied. */
506 rc = boot_verify_slot_dependency(state, &dep);
507 if (rc != 0) {
508 /* Dependency not satisfied */
509 goto done;
510 }
511 }
512
513done:
514 flash_area_close(fap);
515 return rc;
516}
517
518#endif /* (BOOT_IMAGE_NUMBER > 1) */
519
Tamas Banfe031092020-09-10 17:32:39 +0200520#if !defined(MCUBOOT_DIRECT_XIP)
David Brownf5b33d82017-09-01 10:58:27 -0600521/*
522 * Compute the total size of the given image. Includes the size of
523 * the TLVs.
524 */
Tamas Banfe031092020-09-10 17:32:39 +0200525#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600526static int
Fabio Utzigd638b172019-08-09 10:38:05 -0300527boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
David Brownf5b33d82017-09-01 10:58:27 -0600528{
529 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300530 struct image_tlv_info info;
Fabio Utzig233af7d2019-08-26 12:06:16 -0300531 uint32_t off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300532 uint32_t protect_tlv_size;
David Brownf5b33d82017-09-01 10:58:27 -0600533 int area_id;
534 int rc;
535
Fabio Utzig10ee6482019-08-01 12:04:52 -0300536#if (BOOT_IMAGE_NUMBER == 1)
537 (void)state;
538#endif
539
540 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
David Brownf5b33d82017-09-01 10:58:27 -0600541 rc = flash_area_open(area_id, &fap);
542 if (rc != 0) {
543 rc = BOOT_EFLASH;
544 goto done;
545 }
546
Fabio Utzig61fd8882019-09-14 20:00:20 -0300547 off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
548
549 if (flash_area_read(fap, off, &info, sizeof(info))) {
550 rc = BOOT_EFLASH;
David Brownf5b33d82017-09-01 10:58:27 -0600551 goto done;
552 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300553
Fabio Utzige52c08e2019-09-11 19:32:00 -0300554 protect_tlv_size = boot_img_hdr(state, slot)->ih_protect_tlv_size;
555 if (info.it_magic == IMAGE_TLV_PROT_INFO_MAGIC) {
556 if (protect_tlv_size != info.it_tlv_tot) {
557 rc = BOOT_EBADIMAGE;
558 goto done;
559 }
560
561 if (flash_area_read(fap, off + info.it_tlv_tot, &info, sizeof(info))) {
562 rc = BOOT_EFLASH;
563 goto done;
564 }
565 } else if (protect_tlv_size != 0) {
566 rc = BOOT_EBADIMAGE;
567 goto done;
568 }
569
Fabio Utzig61fd8882019-09-14 20:00:20 -0300570 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
571 rc = BOOT_EBADIMAGE;
572 goto done;
573 }
574
Fabio Utzige52c08e2019-09-11 19:32:00 -0300575 *size = off + protect_tlv_size + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600576 rc = 0;
577
578done:
579 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300580 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600581}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300582#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600583
Tamas Banfe031092020-09-10 17:32:39 +0200584#if !defined(MCUBOOT_RAM_LOAD)
David Brownab449182019-11-15 09:32:52 -0700585static uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300586boot_write_sz(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800587{
David Brownab449182019-11-15 09:32:52 -0700588 uint32_t elem_sz;
Fabio Utzig12d59162019-11-28 10:01:59 -0300589#if MCUBOOT_SWAP_USING_SCRATCH
David Brownab449182019-11-15 09:32:52 -0700590 uint32_t align;
Fabio Utzig12d59162019-11-28 10:01:59 -0300591#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800592
593 /* Figure out what size to write update status update as. The size depends
594 * on what the minimum write size is for scratch area, active image slot.
595 * We need to use the bigger of those 2 values.
596 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300597 elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
Fabio Utzig12d59162019-11-28 10:01:59 -0300598#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300599 align = flash_area_align(BOOT_SCRATCH_AREA(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800600 if (align > elem_sz) {
601 elem_sz = align;
602 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300603#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800604
605 return elem_sz;
606}
607
Fabio Utzig10ee6482019-08-01 12:04:52 -0300608static int
609boot_initialize_area(struct boot_loader_state *state, int flash_area)
610{
Dominik Ermel51c8d762021-05-24 15:34:01 +0000611 uint32_t num_sectors = BOOT_MAX_IMG_SECTORS;
612 boot_sector_t *out_sectors;
613 uint32_t *out_num_sectors;
Fabio Utzig10ee6482019-08-01 12:04:52 -0300614 int rc;
615
616 num_sectors = BOOT_MAX_IMG_SECTORS;
617
618 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
619 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
620 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
621 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
622 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
623 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300624#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300625 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
626 out_sectors = state->scratch.sectors;
627 out_num_sectors = &state->scratch.num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300628#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -0300629 } else {
630 return BOOT_EFLASH;
631 }
632
Dominik Ermel51c8d762021-05-24 15:34:01 +0000633#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
Fabio Utzig10ee6482019-08-01 12:04:52 -0300634 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
Dominik Ermel51c8d762021-05-24 15:34:01 +0000635#else
636 _Static_assert(sizeof(int) <= sizeof(uint32_t), "Fix needed");
637 rc = flash_area_to_sectors(flash_area, (int *)&num_sectors, out_sectors);
638#endif /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300639 if (rc != 0) {
640 return rc;
641 }
642 *out_num_sectors = num_sectors;
643 return 0;
644}
Fabio Utzig10ee6482019-08-01 12:04:52 -0300645
Christopher Collins92ea77f2016-12-12 15:59:26 -0800646/**
647 * Determines the sector layout of both image slots and the scratch area.
648 * This information is necessary for calculating the number of bytes to erase
649 * and copy during an image swap. The information collected during this
Fabio Utzig10ee6482019-08-01 12:04:52 -0300650 * function is used to populate the state.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800651 */
652static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300653boot_read_sectors(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800654{
Fabio Utzigb0f04732019-07-31 09:49:19 -0300655 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800656 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800657
Fabio Utzig10ee6482019-08-01 12:04:52 -0300658 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300659
660 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800661 if (rc != 0) {
662 return BOOT_EFLASH;
663 }
664
Fabio Utzig10ee6482019-08-01 12:04:52 -0300665 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800666 if (rc != 0) {
Andrzej Puzdrowski54b4ad92021-06-22 13:21:22 +0200667 /* We need to differentiate from the primary image issue */
668 return BOOT_EFLASH_SEC;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800669 }
670
Fabio Utzig12d59162019-11-28 10:01:59 -0300671#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300672 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200673 if (rc != 0) {
674 return BOOT_EFLASH;
675 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300676#endif
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200677
Fabio Utzig10ee6482019-08-01 12:04:52 -0300678 BOOT_WRITE_SZ(state) = boot_write_sz(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800679
680 return 0;
681}
682
Fabio Utzig12d59162019-11-28 10:01:59 -0300683void
684boot_status_reset(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800685{
Fabio Utzig4741c452019-12-19 15:32:41 -0300686#ifdef MCUBOOT_ENC_IMAGES
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000687 memset(&bs->enckey, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_KEY_ALIGN_SIZE);
Fabio Utzig4741c452019-12-19 15:32:41 -0300688#if MCUBOOT_SWAP_SAVE_ENCTLV
689 memset(&bs->enctlv, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_TLV_ALIGN_SIZE);
690#endif
691#endif /* MCUBOOT_ENC_IMAGES */
692
693 bs->use_scratch = 0;
694 bs->swap_size = 0;
695 bs->source = 0;
696
Fabio Utzig74aef312019-11-28 11:05:34 -0300697 bs->op = BOOT_STATUS_OP_MOVE;
Fabio Utzig39000012018-07-30 12:40:20 -0300698 bs->idx = BOOT_STATUS_IDX_0;
699 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700700 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Fabio Utzig12d59162019-11-28 10:01:59 -0300701}
Christopher Collins92ea77f2016-12-12 15:59:26 -0800702
Fabio Utzig12d59162019-11-28 10:01:59 -0300703bool
704boot_status_is_reset(const struct boot_status *bs)
705{
Fabio Utzig74aef312019-11-28 11:05:34 -0300706 return (bs->op == BOOT_STATUS_OP_MOVE &&
707 bs->idx == BOOT_STATUS_IDX_0 &&
708 bs->state == BOOT_STATUS_STATE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800709}
710
711/**
712 * Writes the supplied boot status to the flash file system. The boot status
713 * contains the current state of an in-progress image copy operation.
714 *
715 * @param bs The boot status to write.
716 *
717 * @return 0 on success; nonzero on failure.
718 */
719int
Fabio Utzig12d59162019-11-28 10:01:59 -0300720boot_write_status(const struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800721{
722 const struct flash_area *fap;
723 uint32_t off;
724 int area_id;
Dominik Ermel9479af02021-10-19 10:06:44 +0000725 int rc = 0;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300726 uint8_t buf[BOOT_MAX_ALIGN];
Gustavo Henrique Nihei4aa286d2021-11-24 14:54:56 -0300727 uint32_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300728 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800729
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300730 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100731 * the trailer. Since in the last step the primary slot is erased, the
732 * first two status writes go to the scratch which will be copied to
733 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300734 */
735
Fabio Utzig12d59162019-11-28 10:01:59 -0300736#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig2473ac02017-05-02 12:45:02 -0300737 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800738 /* Write to scratch. */
739 area_id = FLASH_AREA_IMAGE_SCRATCH;
740 } else {
Fabio Utzig12d59162019-11-28 10:01:59 -0300741#endif
David Vincze2d736ad2019-02-18 11:50:22 +0100742 /* Write to the primary slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300743 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Fabio Utzig12d59162019-11-28 10:01:59 -0300744#if MCUBOOT_SWAP_USING_SCRATCH
Christopher Collins92ea77f2016-12-12 15:59:26 -0800745 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300746#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800747
748 rc = flash_area_open(area_id, &fap);
749 if (rc != 0) {
Dominik Ermel9479af02021-10-19 10:06:44 +0000750 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800751 }
752
753 off = boot_status_off(fap) +
Fabio Utzig12d59162019-11-28 10:01:59 -0300754 boot_status_internal_off(bs, BOOT_WRITE_SZ(state));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200755 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300756 erased_val = flash_area_erased_val(fap);
757 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700758 buf[0] = bs->state;
759
Jamie McCrae35e99312024-01-03 07:37:55 +0000760 BOOT_LOG_DBG("writing swap status; fa_id=%d off=0x%lx (0x%lx)",
761 flash_area_get_id(fap), (unsigned long)off,
762 (unsigned long)flash_area_get_off(fap) + off);
763
David Brown9d725462017-01-23 15:50:58 -0700764 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800765 if (rc != 0) {
766 rc = BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800767 }
768
Christopher Collins92ea77f2016-12-12 15:59:26 -0800769 flash_area_close(fap);
Dominik Ermel9479af02021-10-19 10:06:44 +0000770
Christopher Collins92ea77f2016-12-12 15:59:26 -0800771 return rc;
772}
Tamas Banfe031092020-09-10 17:32:39 +0200773#endif /* !MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +0200774#endif /* !MCUBOOT_DIRECT_XIP */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800775
776/*
David Vinczec3084132020-02-18 14:50:47 +0100777 * Validate image hash/signature and optionally the security counter in a slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800778 */
Michael Grand5047f032022-11-24 16:49:56 +0100779static fih_ret
Fabio Utzig10ee6482019-08-01 12:04:52 -0300780boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
781 const struct flash_area *fap, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800782{
Fabio Utzig10ee6482019-08-01 12:04:52 -0300783 TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigb0f04732019-07-31 09:49:19 -0300784 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300785 int rc;
Michael Grand5047f032022-11-24 16:49:56 +0100786 FIH_DECLARE(fih_rc, FIH_FAILURE);
Fabio Utzigba829042018-09-18 08:29:34 -0300787
Fabio Utzig10ee6482019-08-01 12:04:52 -0300788#if (BOOT_IMAGE_NUMBER == 1)
789 (void)state;
790#endif
791
Fabio Utzigba829042018-09-18 08:29:34 -0300792 (void)bs;
793 (void)rc;
Fabio Utzigbc077932019-08-26 11:16:34 -0300794
795 image_index = BOOT_CURR_IMG(state);
796
Hugo L'Hostisdb543e52021-03-09 18:00:31 +0000797/* In the case of ram loading the image has already been decrypted as it is
798 * decrypted when copied in ram */
799#if defined(MCUBOOT_ENC_IMAGES) && !defined(MCUBOOT_RAM_LOAD)
Fabio Utzigbc077932019-08-26 11:16:34 -0300800 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzig4741c452019-12-19 15:32:41 -0300801 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300802 if (rc < 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100803 FIH_RET(fih_rc);
Fabio Utzigba829042018-09-18 08:29:34 -0300804 }
Fabio Utzig4741c452019-12-19 15:32:41 -0300805 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100806 FIH_RET(fih_rc);
Fabio Utzigba829042018-09-18 08:29:34 -0300807 }
808 }
Fabio Utzigbc077932019-08-26 11:16:34 -0300809#endif
810
Raef Colese8fe6cf2020-05-26 13:07:40 +0100811 FIH_CALL(bootutil_img_validate, fih_rc, BOOT_CURR_ENC(state), image_index,
812 hdr, fap, tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, NULL);
Fabio Utzig10ee6482019-08-01 12:04:52 -0300813
Raef Colese8fe6cf2020-05-26 13:07:40 +0100814 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800815}
816
Tamas Banfe031092020-09-10 17:32:39 +0200817#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
Michael Grand5047f032022-11-24 16:49:56 +0100818static fih_ret
Christopher Collins92ea77f2016-12-12 15:59:26 -0800819split_image_check(struct image_header *app_hdr,
820 const struct flash_area *app_fap,
821 struct image_header *loader_hdr,
822 const struct flash_area *loader_fap)
823{
824 static void *tmpbuf;
825 uint8_t loader_hash[32];
Michael Grand5047f032022-11-24 16:49:56 +0100826 FIH_DECLARE(fih_rc, FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800827
828 if (!tmpbuf) {
829 tmpbuf = malloc(BOOT_TMPBUF_SZ);
830 if (!tmpbuf) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100831 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800832 }
833 }
834
Raef Colese8fe6cf2020-05-26 13:07:40 +0100835 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, loader_hdr, loader_fap,
836 tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, loader_hash);
Michael Grand5047f032022-11-24 16:49:56 +0100837 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100838 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800839 }
840
Raef Colese8fe6cf2020-05-26 13:07:40 +0100841 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, app_hdr, app_fap,
842 tmpbuf, BOOT_TMPBUF_SZ, loader_hash, 32, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800843
Raef Colese8fe6cf2020-05-26 13:07:40 +0100844out:
845 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800846}
Tamas Banfe031092020-09-10 17:32:39 +0200847#endif /* !MCUBOOT_DIRECT_XIP && !MCUBOOT_RAM_LOAD */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800848
Fabio Utzig338a19f2018-12-03 08:37:08 -0200849/*
David Brown9bf95af2019-10-10 15:36:36 -0600850 * Check that this is a valid header. Valid means that the magic is
851 * correct, and that the sizes/offsets are "sane". Sane means that
852 * there is no overflow on the arithmetic, and that the result fits
853 * within the flash area we are in.
854 */
855static bool
856boot_is_header_valid(const struct image_header *hdr, const struct flash_area *fap)
857{
858 uint32_t size;
859
860 if (hdr->ih_magic != IMAGE_MAGIC) {
861 return false;
862 }
863
864 if (!boot_u32_safe_add(&size, hdr->ih_img_size, hdr->ih_hdr_size)) {
865 return false;
866 }
867
Dominik Ermel260ae092021-04-23 05:38:45 +0000868 if (size >= flash_area_get_size(fap)) {
David Brown9bf95af2019-10-10 15:36:36 -0600869 return false;
870 }
871
872 return true;
873}
874
875/*
Fabio Utzig338a19f2018-12-03 08:37:08 -0200876 * Check that a memory area consists of a given value.
877 */
878static inline bool
879boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300880{
881 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200882 uint8_t *p = (uint8_t *)data;
883 for (i = 0; i < len; i++) {
884 if (val != p[i]) {
885 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300886 }
887 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200888 return true;
889}
890
891static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300892boot_check_header_erased(struct boot_loader_state *state, int slot)
Fabio Utzig338a19f2018-12-03 08:37:08 -0200893{
894 const struct flash_area *fap;
895 struct image_header *hdr;
896 uint8_t erased_val;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300897 int area_id;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200898 int rc;
899
Fabio Utzig10ee6482019-08-01 12:04:52 -0300900 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300901 rc = flash_area_open(area_id, &fap);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200902 if (rc != 0) {
903 return -1;
904 }
905
906 erased_val = flash_area_erased_val(fap);
907 flash_area_close(fap);
908
Fabio Utzig10ee6482019-08-01 12:04:52 -0300909 hdr = boot_img_hdr(state, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200910 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
911 return -1;
912 }
913
914 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300915}
916
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000917#if defined(MCUBOOT_DIRECT_XIP)
918/**
919 * Check if image in slot has been set with specific ROM address to run from
920 * and whether the slot starts at that address.
921 *
922 * @returns 0 if IMAGE_F_ROM_FIXED flag is not set;
923 * 0 if IMAGE_F_ROM_FIXED flag is set and ROM address specified in
924 * header matches the slot address;
925 * 1 if IMF_F_ROM_FIXED flag is set but ROM address specified in header
926 * does not match the slot address.
927 */
928static bool
Raef Colesfe57e7d2021-10-15 11:07:09 +0100929boot_rom_address_check(struct boot_loader_state *state)
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000930{
Mark Horvathccaf7f82021-01-04 18:16:42 +0100931 uint32_t active_slot;
932 const struct image_header *hdr;
933 uint32_t f_off;
934
Raef Colesfe57e7d2021-10-15 11:07:09 +0100935 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +0100936 hdr = boot_img_hdr(state, active_slot);
937 f_off = boot_img_slot_off(state, active_slot);
938
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000939 if (hdr->ih_flags & IMAGE_F_ROM_FIXED && hdr->ih_load_addr != f_off) {
940 BOOT_LOG_WRN("Image in %s slot at 0x%x has been built for offset 0x%x"\
Mark Horvathccaf7f82021-01-04 18:16:42 +0100941 ", skipping",
942 active_slot == 0 ? "primary" : "secondary", f_off,
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000943 hdr->ih_load_addr);
944
945 /* If there is address mismatch, the image is not bootable from this
946 * slot.
947 */
948 return 1;
949 }
950 return 0;
951}
952#endif
953
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300954/*
955 * Check that there is a valid image in a slot
956 *
957 * @returns
Raef Colese8fe6cf2020-05-26 13:07:40 +0100958 * FIH_SUCCESS if image was successfully validated
Michael Grand5047f032022-11-24 16:49:56 +0100959 * FIH_NO_BOOTABLE_IMAGE if no bootloable image was found
Raef Colese8fe6cf2020-05-26 13:07:40 +0100960 * FIH_FAILURE on any errors
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300961 */
Michael Grand5047f032022-11-24 16:49:56 +0100962static fih_ret
Fabio Utzig10ee6482019-08-01 12:04:52 -0300963boot_validate_slot(struct boot_loader_state *state, int slot,
964 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800965{
966 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400967 struct image_header *hdr;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300968 int area_id;
Michael Grand5047f032022-11-24 16:49:56 +0100969 FIH_DECLARE(fih_rc, FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800970 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300971
Fabio Utzig10ee6482019-08-01 12:04:52 -0300972 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300973 rc = flash_area_open(area_id, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800974 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100975 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800976 }
977
Fabio Utzig10ee6482019-08-01 12:04:52 -0300978 hdr = boot_img_hdr(state, slot);
979 if (boot_check_header_erased(state, slot) == 0 ||
980 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
Fabio Utzig260ec452020-07-09 18:40:07 -0300981
982#if defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE)
983 /*
984 * This fixes an issue where an image might be erased, but a trailer
985 * be left behind. It can happen if the image is in the secondary slot
986 * and did not pass validation, in which case the whole slot is erased.
987 * If during the erase operation, a reset occurs, parts of the slot
988 * might have been erased while some did not. The concerning part is
989 * the trailer because it might disable a new image from being loaded
990 * through mcumgr; so we just get rid of the trailer here, if the header
991 * is erased.
992 */
993 if (slot != BOOT_PRIMARY_SLOT) {
994 swap_erase_trailer_sectors(state, fap);
995 }
996#endif
997
David Vincze2d736ad2019-02-18 11:50:22 +0100998 /* No bootable image in slot; continue booting from the primary slot. */
Michael Grand5047f032022-11-24 16:49:56 +0100999 fih_rc = FIH_NO_BOOTABLE_IMAGE;
Fabio Utzig338a19f2018-12-03 08:37:08 -02001000 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -03001001 }
1002
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +00001003#if defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION)
1004 if (slot != BOOT_PRIMARY_SLOT) {
1005 /* Check if version of secondary slot is sufficient */
David Vincze8b0b6372020-05-20 19:54:44 +02001006 rc = boot_version_cmp(
1007 &boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
1008 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
1009 if (rc < 0 && boot_check_header_erased(state, BOOT_PRIMARY_SLOT)) {
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +00001010 BOOT_LOG_ERR("insufficient version in secondary slot");
Dominik Ermel260ae092021-04-23 05:38:45 +00001011 flash_area_erase(fap, 0, flash_area_get_size(fap));
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +00001012 /* Image in the secondary slot does not satisfy version requirement.
1013 * Erase the image and continue booting from the primary slot.
1014 */
Michael Grand5047f032022-11-24 16:49:56 +01001015 fih_rc = FIH_NO_BOOTABLE_IMAGE;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +00001016 goto out;
1017 }
1018 }
1019#endif
Michael Grand5047f032022-11-24 16:49:56 +01001020 BOOT_HOOK_CALL_FIH(boot_image_check_hook, FIH_BOOT_HOOK_REGULAR,
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +02001021 fih_rc, BOOT_CURR_IMG(state), slot);
Michael Grand5047f032022-11-24 16:49:56 +01001022 if (FIH_EQ(fih_rc, FIH_BOOT_HOOK_REGULAR))
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +02001023 {
1024 FIH_CALL(boot_image_check, fih_rc, state, hdr, fap, bs);
1025 }
Michael Grand5047f032022-11-24 16:49:56 +01001026 if (!boot_is_header_valid(hdr, fap) || FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Tamas Banfe031092020-09-10 17:32:39 +02001027 if ((slot != BOOT_PRIMARY_SLOT) || ARE_SLOTS_EQUIVALENT()) {
Dominik Ermel260ae092021-04-23 05:38:45 +00001028 flash_area_erase(fap, 0, flash_area_get_size(fap));
David Vinczee574f2d2020-07-10 11:42:03 +02001029 /* Image is invalid, erase it to prevent further unnecessary
1030 * attempts to validate and boot it.
David Brownb38e0442017-02-24 13:57:12 -07001031 */
1032 }
David Brown098de832019-12-10 11:58:01 -07001033#if !defined(__BOOTSIM__)
David Vincze2d736ad2019-02-18 11:50:22 +01001034 BOOT_LOG_ERR("Image in the %s slot is not valid!",
1035 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Brown098de832019-12-10 11:58:01 -07001036#endif
Michael Grand5047f032022-11-24 16:49:56 +01001037 fih_rc = FIH_NO_BOOTABLE_IMAGE;
Fabio Utzig338a19f2018-12-03 08:37:08 -02001038 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001039 }
1040
Håkon Øye Amundsene829e9d2021-11-12 14:01:01 +00001041#if MCUBOOT_IMAGE_NUMBER > 1 && !defined(MCUBOOT_ENC_IMAGES) && defined(MCUBOOT_VERIFY_IMG_ADDRESS)
1042 /* Verify that the image in the secondary slot has a reset address
1043 * located in the primary slot. This is done to avoid users incorrectly
1044 * overwriting an application written to the incorrect slot.
1045 * This feature is only supported by ARM platforms.
1046 */
1047 if (area_id == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
1048 const struct flash_area *pri_fa = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
1049 struct image_header *secondary_hdr = boot_img_hdr(state, slot);
1050 uint32_t reset_value = 0;
1051 uint32_t reset_addr = secondary_hdr->ih_hdr_size + sizeof(reset_value);
1052
1053 rc = flash_area_read(fap, reset_addr, &reset_value, sizeof(reset_value));
1054 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01001055 fih_rc = FIH_NO_BOOTABLE_IMAGE;
Håkon Øye Amundsene829e9d2021-11-12 14:01:01 +00001056 goto out;
1057 }
1058
1059 if (reset_value < pri_fa->fa_off || reset_value> (pri_fa->fa_off + pri_fa->fa_size)) {
1060 BOOT_LOG_ERR("Reset address of image in secondary slot is not in the primary slot");
1061 BOOT_LOG_ERR("Erasing image from secondary slot");
1062
1063 /* The vector table in the image located in the secondary
1064 * slot does not target the primary slot. This might
1065 * indicate that the image was loaded to the wrong slot.
1066 *
1067 * Erase the image and continue booting from the primary slot.
1068 */
1069 flash_area_erase(fap, 0, fap->fa_size);
Michael Grand5047f032022-11-24 16:49:56 +01001070 fih_rc = FIH_NO_BOOTABLE_IMAGE;
Håkon Øye Amundsene829e9d2021-11-12 14:01:01 +00001071 goto out;
1072 }
1073 }
1074#endif
1075
Fabio Utzig338a19f2018-12-03 08:37:08 -02001076out:
1077 flash_area_close(fap);
Raef Colese8fe6cf2020-05-26 13:07:40 +01001078
1079 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001080}
1081
David Vinczec3084132020-02-18 14:50:47 +01001082#ifdef MCUBOOT_HW_ROLLBACK_PROT
1083/**
1084 * Updates the stored security counter value with the image's security counter
1085 * value which resides in the given slot, only if it's greater than the stored
1086 * value.
1087 *
1088 * @param image_index Index of the image to determine which security
1089 * counter to update.
1090 * @param slot Slot number of the image.
1091 * @param hdr Pointer to the image header structure of the image
1092 * that is currently stored in the given slot.
1093 *
1094 * @return 0 on success; nonzero on failure.
1095 */
1096static int
1097boot_update_security_counter(uint8_t image_index, int slot,
1098 struct image_header *hdr)
1099{
1100 const struct flash_area *fap = NULL;
1101 uint32_t img_security_cnt;
1102 int rc;
1103
1104 rc = flash_area_open(flash_area_id_from_multi_image_slot(image_index, slot),
1105 &fap);
1106 if (rc != 0) {
1107 rc = BOOT_EFLASH;
1108 goto done;
1109 }
1110
1111 rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt);
1112 if (rc != 0) {
1113 goto done;
1114 }
1115
1116 rc = boot_nv_security_counter_update(image_index, img_security_cnt);
1117 if (rc != 0) {
1118 goto done;
1119 }
1120
1121done:
1122 flash_area_close(fap);
1123 return rc;
1124}
1125#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1126
Tamas Banfe031092020-09-10 17:32:39 +02001127#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
David Vinczee574f2d2020-07-10 11:42:03 +02001128/**
1129 * Determines which swap operation to perform, if any. If it is determined
1130 * that a swap operation is required, the image in the secondary slot is checked
1131 * for validity. If the image in the secondary slot is invalid, it is erased,
1132 * and a swap type of "none" is indicated.
1133 *
1134 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
1135 */
1136static int
1137boot_validated_swap_type(struct boot_loader_state *state,
1138 struct boot_status *bs)
1139{
1140 int swap_type;
Michael Grand5047f032022-11-24 16:49:56 +01001141 FIH_DECLARE(fih_rc, FIH_FAILURE);
David Vinczee574f2d2020-07-10 11:42:03 +02001142
1143 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
1144 if (BOOT_IS_UPGRADE(swap_type)) {
1145 /* Boot loader wants to switch to the secondary slot.
1146 * Ensure image is valid.
1147 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001148 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_SECONDARY_SLOT, bs);
Michael Grand5047f032022-11-24 16:49:56 +01001149 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
1150 if (FIH_EQ(fih_rc, FIH_NO_BOOTABLE_IMAGE)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001151 swap_type = BOOT_SWAP_TYPE_NONE;
1152 } else {
1153 swap_type = BOOT_SWAP_TYPE_FAIL;
1154 }
David Vinczee574f2d2020-07-10 11:42:03 +02001155 }
1156 }
1157
1158 return swap_type;
1159}
David Brown94ed12c2021-05-26 16:28:14 -06001160#endif
David Vinczee574f2d2020-07-10 11:42:03 +02001161
Christopher Collins92ea77f2016-12-12 15:59:26 -08001162/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08001163 * Erases a region of flash.
1164 *
Fabio Utzigba829042018-09-18 08:29:34 -03001165 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001166 * @param off The offset within the flash area to start the
1167 * erase.
1168 * @param sz The number of bytes to erase.
1169 *
1170 * @return 0 on success; nonzero on failure.
1171 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001172int
Fabio Utzigc28005b2019-09-10 12:18:29 -03001173boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001174{
Fabio Utzigba829042018-09-18 08:29:34 -03001175 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001176}
1177
David Brown94ed12c2021-05-26 16:28:14 -06001178#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
Dominik Ermel256bc372022-12-07 15:51:31 +00001179
1180#if defined(MCUBOOT_ENC_IMAGES) || defined(MCUBOOT_SWAP_SAVE_ENCTLV)
1181/* Replacement for memset(p, 0, sizeof(*p) that does not get
1182 * optimized out.
1183 */
1184static void like_mbedtls_zeroize(void *p, size_t n)
1185{
1186 volatile unsigned char *v = (unsigned char *)p;
1187
1188 for (size_t i = 0; i < n; i++) {
1189 v[i] = 0;
1190 }
1191}
1192#endif
1193
Christopher Collins92ea77f2016-12-12 15:59:26 -08001194/**
1195 * Copies the contents of one flash region to another. You must erase the
1196 * destination region prior to calling this function.
1197 *
1198 * @param flash_area_id_src The ID of the source flash area.
1199 * @param flash_area_id_dst The ID of the destination flash area.
1200 * @param off_src The offset within the source flash area to
1201 * copy from.
1202 * @param off_dst The offset within the destination flash area to
1203 * copy to.
1204 * @param sz The number of bytes to copy.
1205 *
1206 * @return 0 on success; nonzero on failure.
1207 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001208int
Fabio Utzigc28005b2019-09-10 12:18:29 -03001209boot_copy_region(struct boot_loader_state *state,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001210 const struct flash_area *fap_src,
Fabio Utzigba829042018-09-18 08:29:34 -03001211 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -08001212 uint32_t off_src, uint32_t off_dst, uint32_t sz)
1213{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001214 uint32_t bytes_copied;
1215 int chunk_sz;
1216 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -03001217#ifdef MCUBOOT_ENC_IMAGES
1218 uint32_t off;
Fabio Utziga87cc7d2019-08-26 11:21:45 -03001219 uint32_t tlv_off;
Fabio Utzigba829042018-09-18 08:29:34 -03001220 size_t blk_off;
Thomas Altenbach08d2d942024-04-25 15:29:21 +02001221 int enc_area_id;
Fabio Utzigba829042018-09-18 08:29:34 -03001222 struct image_header *hdr;
1223 uint16_t idx;
1224 uint32_t blk_sz;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001225 uint8_t image_index;
Thomas Altenbach08d2d942024-04-25 15:29:21 +02001226 bool encrypted_src;
1227 bool encrypted_dst;
Fabio Utzigba829042018-09-18 08:29:34 -03001228#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001229
Kristine Jassmann73c38c62021-02-03 16:56:14 +00001230 TARGET_STATIC uint8_t buf[BUF_SZ] __attribute__((aligned(4)));
Fabio Utzig10ee6482019-08-01 12:04:52 -03001231
1232#if !defined(MCUBOOT_ENC_IMAGES)
1233 (void)state;
1234#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001235
Christopher Collins92ea77f2016-12-12 15:59:26 -08001236 bytes_copied = 0;
1237 while (bytes_copied < sz) {
1238 if (sz - bytes_copied > sizeof buf) {
1239 chunk_sz = sizeof buf;
1240 } else {
1241 chunk_sz = sz - bytes_copied;
1242 }
1243
1244 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
1245 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001246 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001247 }
1248
Fabio Utzigba829042018-09-18 08:29:34 -03001249#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001250 image_index = BOOT_CURR_IMG(state);
Thomas Altenbach08d2d942024-04-25 15:29:21 +02001251 encrypted_src = (flash_area_get_id(fap_src) != FLASH_AREA_IMAGE_PRIMARY(image_index));
1252 encrypted_dst = (flash_area_get_id(fap_dst) != FLASH_AREA_IMAGE_PRIMARY(image_index));
1253
1254 if (encrypted_src != encrypted_dst) {
Fabio Utzig74aef312019-11-28 11:05:34 -03001255 off = off_dst;
Thomas Altenbach08d2d942024-04-25 15:29:21 +02001256
1257 if (encrypted_dst) {
1258 /* Need encryption, metadata from the primary slot */
Fabio Utzig74aef312019-11-28 11:05:34 -03001259 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Thomas Altenbach08d2d942024-04-25 15:29:21 +02001260 enc_area_id = FLASH_AREA_IMAGE_PRIMARY(image_index);
1261 } else {
1262 /* Need decryption, metadata from the secondary slot */
1263 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
1264 enc_area_id = FLASH_AREA_IMAGE_SECONDARY(image_index);
Fabio Utzig74aef312019-11-28 11:05:34 -03001265 }
Thomas Altenbach08d2d942024-04-25 15:29:21 +02001266
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001267 if (IS_ENCRYPTED(hdr)) {
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001268 uint32_t abs_off = off + bytes_copied;
1269 if (abs_off < hdr->ih_hdr_size) {
Fabio Utzigba829042018-09-18 08:29:34 -03001270 /* do not decrypt header */
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001271 if (abs_off + chunk_sz > hdr->ih_hdr_size) {
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001272 /* The lower part of the chunk contains header data */
1273 blk_off = 0;
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001274 blk_sz = chunk_sz - (hdr->ih_hdr_size - abs_off);
1275 idx = hdr->ih_hdr_size - abs_off;
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001276 } else {
1277 /* The chunk contains exclusively header data */
1278 blk_sz = 0; /* nothing to decrypt */
1279 }
Fabio Utzigba829042018-09-18 08:29:34 -03001280 } else {
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001281 idx = 0;
1282 blk_sz = chunk_sz;
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001283 blk_off = (abs_off - hdr->ih_hdr_size) & 0xf;
Fabio Utzigba829042018-09-18 08:29:34 -03001284 }
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001285
1286 if (blk_sz > 0)
1287 {
1288 tlv_off = BOOT_TLV_OFF(hdr);
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001289 if (abs_off + chunk_sz > tlv_off) {
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001290 /* do not decrypt TLVs */
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001291 if (abs_off >= tlv_off) {
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001292 blk_sz = 0;
1293 } else {
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001294 blk_sz = tlv_off - abs_off;
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001295 }
Fabio Utzigba829042018-09-18 08:29:34 -03001296 }
Thomas Altenbach08d2d942024-04-25 15:29:21 +02001297 boot_encrypt(BOOT_CURR_ENC(state), image_index, enc_area_id,
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001298 (abs_off + idx) - hdr->ih_hdr_size, blk_sz,
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001299 blk_off, &buf[idx]);
Fabio Utzigba829042018-09-18 08:29:34 -03001300 }
Fabio Utzigba829042018-09-18 08:29:34 -03001301 }
1302 }
1303#endif
1304
Christopher Collins92ea77f2016-12-12 15:59:26 -08001305 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
1306 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001307 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001308 }
1309
1310 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -03001311
1312 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -08001313 }
1314
Fabio Utzigba829042018-09-18 08:29:34 -03001315 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001316}
1317
Christopher Collins92ea77f2016-12-12 15:59:26 -08001318/**
David Vincze2d736ad2019-02-18 11:50:22 +01001319 * Overwrite primary slot with the image contained in the secondary slot.
1320 * If a prior copy operation was interrupted by a system reset, this function
1321 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001322 *
1323 * @param bs The current boot status. This function reads
1324 * this struct to determine if it is resuming
1325 * an interrupted swap operation. This
1326 * function writes the updated status to this
1327 * function on return.
1328 *
1329 * @return 0 on success; nonzero on failure.
1330 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001331#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001332static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001333boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
David Brown17609d82017-05-05 09:41:34 -06001334{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001335 size_t sect_count;
1336 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001337 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001338 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001339 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001340 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +01001341 const struct flash_area *fap_primary_slot;
1342 const struct flash_area *fap_secondary_slot;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001343 uint8_t image_index;
David Vincze2d736ad2019-02-18 11:50:22 +01001344
Fabio Utzigb4f88102020-10-04 10:16:24 -03001345#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1346 uint32_t sector;
1347 uint32_t trailer_sz;
1348 uint32_t off;
1349 uint32_t sz;
1350#endif
1351
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001352 (void)bs;
1353
Fabio Utzig13d9e352017-10-05 20:32:31 -03001354#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1355 uint32_t src_size = 0;
Fabio Utzigd638b172019-08-09 10:38:05 -03001356 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001357 assert(rc == 0);
1358#endif
David Brown17609d82017-05-05 09:41:34 -06001359
Fabio Utzigb0f04732019-07-31 09:49:19 -03001360 image_index = BOOT_CURR_IMG(state);
1361
Antonio de Angelisba5fb1c2022-10-11 10:10:37 +01001362 BOOT_LOG_INF("Image %d upgrade secondary slot -> primary slot", image_index);
1363 BOOT_LOG_INF("Erasing the primary slot");
1364
Fabio Utzigb0f04732019-07-31 09:49:19 -03001365 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1366 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001367 assert (rc == 0);
1368
Fabio Utzigb0f04732019-07-31 09:49:19 -03001369 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1370 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001371 assert (rc == 0);
1372
Fabio Utzig10ee6482019-08-01 12:04:52 -03001373 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001374 for (sect = 0, size = 0; sect < sect_count; sect++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001375 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001376 rc = boot_erase_region(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001377 assert(rc == 0);
1378
Fabio Utzig13d9e352017-10-05 20:32:31 -03001379#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
Fabio Utzigb4f88102020-10-04 10:16:24 -03001380 if ((size + this_size) >= src_size) {
1381 size += src_size - size;
1382 size += BOOT_WRITE_SZ(state) - (size % BOOT_WRITE_SZ(state));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001383 break;
1384 }
1385#endif
Fabio Utzigb4f88102020-10-04 10:16:24 -03001386
1387 size += this_size;
David Brown17609d82017-05-05 09:41:34 -06001388 }
1389
Fabio Utzigb4f88102020-10-04 10:16:24 -03001390#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1391 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
1392 sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
1393 sz = 0;
1394 do {
1395 sz += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sector);
1396 off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, sector);
1397 sector--;
1398 } while (sz < trailer_sz);
1399
1400 rc = boot_erase_region(fap_primary_slot, off, sz);
1401 assert(rc == 0);
1402#endif
1403
Fabio Utzigba829042018-09-18 08:29:34 -03001404#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001405 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001406 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001407 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
Fabio Utzig4741c452019-12-19 15:32:41 -03001408 fap_secondary_slot, bs);
David Vincze2d736ad2019-02-18 11:50:22 +01001409
Fabio Utzigba829042018-09-18 08:29:34 -03001410 if (rc < 0) {
1411 return BOOT_EBADIMAGE;
1412 }
Fabio Utzig4741c452019-12-19 15:32:41 -03001413 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001414 return BOOT_EBADIMAGE;
1415 }
1416 }
1417#endif
1418
Antonio de Angelis48547002023-04-14 09:47:09 +01001419 BOOT_LOG_INF("Image %d copying the secondary slot to the primary slot: 0x%zx bytes",
1420 image_index, size);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001421 rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot, 0, 0, size);
Fabio Utzigb4f88102020-10-04 10:16:24 -03001422 if (rc != 0) {
1423 return rc;
1424 }
1425
1426#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1427 rc = boot_write_magic(fap_primary_slot);
1428 if (rc != 0) {
1429 return rc;
1430 }
1431#endif
David Brown17609d82017-05-05 09:41:34 -06001432
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +02001433 rc = BOOT_HOOK_CALL(boot_copy_region_post_hook, 0, BOOT_CURR_IMG(state),
1434 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT), size);
1435 if (rc != 0) {
1436 return rc;
1437 }
1438
David Vinczec3084132020-02-18 14:50:47 +01001439#ifdef MCUBOOT_HW_ROLLBACK_PROT
1440 /* Update the stored security counter with the new image's security counter
1441 * value. Both slots hold the new image at this point, but the secondary
1442 * slot's image header must be passed since the image headers in the
1443 * boot_data structure have not been updated yet.
1444 */
1445 rc = boot_update_security_counter(BOOT_CURR_IMG(state), BOOT_PRIMARY_SLOT,
1446 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1447 if (rc != 0) {
1448 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
1449 return rc;
1450 }
1451#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1452
Petr Buchtac5a528b2024-02-24 08:10:38 +01001453#ifndef MCUBOOT_OVERWRITE_ONLY_KEEP_BACKUP
Fabio Utzig13d9e352017-10-05 20:32:31 -03001454 /*
1455 * Erases header and trailer. The trailer is erased because when a new
1456 * image is written without a trailer as is the case when using newt, the
1457 * trailer that was left might trigger a new upgrade.
1458 */
Christopher Collins2c88e692019-05-22 15:10:14 -07001459 BOOT_LOG_DBG("erasing secondary header");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001460 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001461 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
1462 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001463 assert(rc == 0);
Petr Buchtac5a528b2024-02-24 08:10:38 +01001464#endif
1465
Fabio Utzig10ee6482019-08-01 12:04:52 -03001466 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -07001467 BOOT_LOG_DBG("erasing secondary trailer");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001468 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001469 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
1470 last_sector),
1471 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
1472 last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001473 assert(rc == 0);
1474
David Vincze2d736ad2019-02-18 11:50:22 +01001475 flash_area_close(fap_primary_slot);
1476 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001477
David Vincze2d736ad2019-02-18 11:50:22 +01001478 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001479
1480 return 0;
1481}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001482#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001483
Christopher Collinsa1c12042019-05-23 14:00:28 -07001484#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -03001485/**
1486 * Swaps the two images in flash. If a prior copy operation was interrupted
1487 * by a system reset, this function completes that operation.
1488 *
1489 * @param bs The current boot status. This function reads
1490 * this struct to determine if it is resuming
1491 * an interrupted swap operation. This
1492 * function writes the updated status to this
1493 * function on return.
1494 *
1495 * @return 0 on success; nonzero on failure.
1496 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001497static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001498boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001499{
Fabio Utzig2473ac02017-05-02 12:45:02 -03001500 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001501 const struct flash_area *fap;
Dominik Ermel472d4c72023-02-10 13:29:58 +00001502#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzigba829042018-09-18 08:29:34 -03001503 uint8_t slot;
1504 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001505#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001506 uint32_t size;
1507 uint32_t copy_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001508 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001509 int rc;
1510
1511 /* FIXME: just do this if asked by user? */
1512
1513 size = copy_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001514 image_index = BOOT_CURR_IMG(state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001515
Fabio Utzig12d59162019-11-28 10:01:59 -03001516 if (boot_status_is_reset(bs)) {
Fabio Utzig46490722017-09-04 15:34:32 -03001517 /*
1518 * No swap ever happened, so need to find the largest image which
1519 * will be used to determine the amount of sectors to swap.
1520 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001521 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001522 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001523 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001524 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001525 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001526
Fabio Utzigba829042018-09-18 08:29:34 -03001527#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001528 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001529 fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001530 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001531 assert(rc >= 0);
1532
1533 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001534 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 0, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001535 assert(rc == 0);
1536 } else {
1537 rc = 0;
1538 }
1539 } else {
Kristine Jassmann73c38c62021-02-03 16:56:14 +00001540 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_ALIGN_SIZE);
Fabio Utzigba829042018-09-18 08:29:34 -03001541 }
1542#endif
1543
Fabio Utzig10ee6482019-08-01 12:04:52 -03001544 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001545 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001546 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001547 assert(rc == 0);
1548 }
1549
Fabio Utzigba829042018-09-18 08:29:34 -03001550#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001551 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001552 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001553 fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001554 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001555 assert(rc >= 0);
1556
1557 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001558 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001559 assert(rc == 0);
1560 } else {
1561 rc = 0;
1562 }
1563 } else {
Kristine Jassmann73c38c62021-02-03 16:56:14 +00001564 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_ALIGN_SIZE);
Fabio Utzigba829042018-09-18 08:29:34 -03001565 }
1566#endif
1567
Fabio Utzig46490722017-09-04 15:34:32 -03001568 if (size > copy_size) {
1569 copy_size = size;
1570 }
1571
1572 bs->swap_size = copy_size;
1573 } else {
1574 /*
1575 * If a swap was under way, the swap_size should already be present
1576 * in the trailer...
1577 */
Dominik Ermel472d4c72023-02-10 13:29:58 +00001578
1579 rc = boot_find_status(image_index, &fap);
1580 assert(fap != NULL);
1581 rc = boot_read_swap_size(fap, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001582 assert(rc == 0);
1583
1584 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001585
1586#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4741c452019-12-19 15:32:41 -03001587 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Dominik Ermel472d4c72023-02-10 13:29:58 +00001588 rc = boot_read_enc_key(fap, slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001589 assert(rc == 0);
1590
1591 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001592 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001593 break;
1594 }
1595 }
1596
Dominik Ermel7e3a1ce2024-07-12 17:19:17 +00001597 boot_enc_init(BOOT_CURR_ENC(state), slot);
1598
Fabio Utzigba829042018-09-18 08:29:34 -03001599 if (i != BOOT_ENC_KEY_SIZE) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001600 boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001601 }
1602 }
1603#endif
Dominik Ermel472d4c72023-02-10 13:29:58 +00001604 flash_area_close(fap);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001605 }
1606
Fabio Utzig12d59162019-11-28 10:01:59 -03001607 swap_run(state, bs, copy_size);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001608
David Vincze2d736ad2019-02-18 11:50:22 +01001609#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig12d59162019-11-28 10:01:59 -03001610 extern int boot_status_fails;
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001611 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001612 BOOT_LOG_WRN("%d status write fails performing the swap",
1613 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001614 }
1615#endif
Sigvart Hovland3fd4cd42022-09-09 13:21:18 +02001616 rc = BOOT_HOOK_CALL(boot_copy_region_post_hook, 0, BOOT_CURR_IMG(state),
1617 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT), size);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001618
Christopher Collins92ea77f2016-12-12 15:59:26 -08001619 return 0;
1620}
David Brown17609d82017-05-05 09:41:34 -06001621#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001622
David Vinczee32483f2019-06-13 10:46:24 +02001623
Christopher Collins92ea77f2016-12-12 15:59:26 -08001624/**
David Vinczeba3bd602019-06-17 16:01:43 +02001625 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001626 *
David Vinczeba3bd602019-06-17 16:01:43 +02001627 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001628 *
1629 * @return 0 on success; nonzero on failure.
1630 */
1631static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001632boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001633{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001634 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001635#ifndef MCUBOOT_OVERWRITE_ONLY
1636 uint8_t swap_type;
1637#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001638
David Vinczeba3bd602019-06-17 16:01:43 +02001639 /* At this point there are no aborted swaps. */
1640#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001641 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001642#elif defined(MCUBOOT_BOOTSTRAP)
1643 /* Check if the image update was triggered by a bad image in the
1644 * primary slot (the validity of the image in the secondary slot had
1645 * already been checked).
1646 */
Michael Grand5047f032022-11-24 16:49:56 +01001647 FIH_DECLARE(fih_rc, FIH_FAILURE);
Raef Colese8fe6cf2020-05-26 13:07:40 +01001648 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1649 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, bs);
Michael Grand5047f032022-11-24 16:49:56 +01001650 if (rc == 0 || FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001651 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001652 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001653 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001654 }
1655#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03001656 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001657#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001658 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001659
1660#ifndef MCUBOOT_OVERWRITE_ONLY
1661 /* The following state needs image_ok be explicitly set after the
1662 * swap was finished to avoid a new revert.
1663 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001664 swap_type = BOOT_SWAP_TYPE(state);
1665 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1666 swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001667 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001668 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001669 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001670 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001671 }
1672
David Vinczec3084132020-02-18 14:50:47 +01001673#ifdef MCUBOOT_HW_ROLLBACK_PROT
1674 if (swap_type == BOOT_SWAP_TYPE_PERM) {
1675 /* Update the stored security counter with the new image's security
1676 * counter value. The primary slot holds the new image at this point,
1677 * but the secondary slot's image header must be passed since image
1678 * headers in the boot_data structure have not been updated yet.
1679 *
1680 * In case of a permanent image swap mcuboot will never attempt to
1681 * revert the images on the next reboot. Therefore, the security
1682 * counter must be increased right after the image upgrade.
1683 */
1684 rc = boot_update_security_counter(
1685 BOOT_CURR_IMG(state),
1686 BOOT_PRIMARY_SLOT,
1687 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1688 if (rc != 0) {
1689 BOOT_LOG_ERR("Security counter update failed after "
1690 "image upgrade.");
1691 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
1692 }
1693 }
1694#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1695
Fabio Utzige575b0b2019-09-11 12:34:23 -03001696 if (BOOT_IS_UPGRADE(swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001697 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001698 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001699 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001700 }
David Vinczeba3bd602019-06-17 16:01:43 +02001701 }
1702#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001703
David Vinczeba3bd602019-06-17 16:01:43 +02001704 return rc;
1705}
1706
1707/**
1708 * Completes a previously aborted image swap.
1709 *
1710 * @param bs The current boot status.
1711 *
1712 * @return 0 on success; nonzero on failure.
1713 */
1714#if !defined(MCUBOOT_OVERWRITE_ONLY)
1715static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001716boot_complete_partial_swap(struct boot_loader_state *state,
1717 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001718{
1719 int rc;
1720
1721 /* Determine the type of swap operation being resumed from the
1722 * `swap-type` trailer field.
1723 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001724 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001725 assert(rc == 0);
1726
Fabio Utzig10ee6482019-08-01 12:04:52 -03001727 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001728
1729 /* The following states need image_ok be explicitly set after the
1730 * swap was finished to avoid a new revert.
1731 */
1732 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1733 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001734 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001735 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001736 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001737 }
1738 }
1739
Fabio Utzige575b0b2019-09-11 12:34:23 -03001740 if (BOOT_IS_UPGRADE(bs->swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001741 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001742 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001743 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001744 }
1745 }
1746
Fabio Utzig10ee6482019-08-01 12:04:52 -03001747 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001748 BOOT_LOG_ERR("panic!");
1749 assert(0);
1750
1751 /* Loop forever... */
1752 while (1) {}
1753 }
1754
1755 return rc;
1756}
1757#endif /* !MCUBOOT_OVERWRITE_ONLY */
1758
1759#if (BOOT_IMAGE_NUMBER > 1)
1760/**
1761 * Review the validity of previously determined swap types of other images.
1762 *
1763 * @param aborted_swap The current image upgrade is a
1764 * partial/aborted swap.
1765 */
1766static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001767boot_review_image_swap_types(struct boot_loader_state *state,
1768 bool aborted_swap)
David Vinczeba3bd602019-06-17 16:01:43 +02001769{
1770 /* In that case if we rebooted in the middle of an image upgrade process, we
1771 * must review the validity of swap types, that were previously determined
1772 * for other images. The image_ok flag had not been set before the reboot
1773 * for any of the updated images (only the copy_done flag) and thus falsely
1774 * the REVERT swap type has been determined for the previous images that had
1775 * been updated before the reboot.
1776 *
1777 * There are two separate scenarios that we have to deal with:
1778 *
1779 * 1. The reboot has happened during swapping an image:
1780 * The current image upgrade has been determined as a
1781 * partial/aborted swap.
1782 * 2. The reboot has happened between two separate image upgrades:
1783 * In this scenario we must check the swap type of the current image.
1784 * In those cases if it is NONE or REVERT we cannot certainly determine
1785 * the fact of a reboot. In a consistent state images must move in the
1786 * same direction or stay in place, e.g. in practice REVERT and TEST
1787 * swap types cannot be present at the same time. If the swap type of
1788 * the current image is either TEST, PERM or FAIL we must review the
1789 * already determined swap types of other images and set each false
1790 * REVERT swap types to NONE (these images had been successfully
1791 * updated before the system rebooted between two separate image
1792 * upgrades).
1793 */
1794
Fabio Utzig10ee6482019-08-01 12:04:52 -03001795 if (BOOT_CURR_IMG(state) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001796 /* Nothing to do */
1797 return;
1798 }
1799
1800 if (!aborted_swap) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001801 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
1802 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001803 /* Nothing to do */
1804 return;
1805 }
1806 }
1807
Fabio Utzig10ee6482019-08-01 12:04:52 -03001808 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
1809 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
1810 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001811 }
1812 }
1813}
1814#endif
1815
1816/**
1817 * Prepare image to be updated if required.
1818 *
1819 * Prepare image to be updated if required with completing an image swap
1820 * operation if one was aborted and/or determining the type of the
1821 * swap operation. In case of any error set the swap type to NONE.
1822 *
Fabio Utzig10ee6482019-08-01 12:04:52 -03001823 * @param state TODO
David Vinczeba3bd602019-06-17 16:01:43 +02001824 * @param bs Pointer where the read and possibly updated
1825 * boot status can be written to.
1826 */
1827static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001828boot_prepare_image_for_update(struct boot_loader_state *state,
1829 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001830{
1831 int rc;
Michael Grand5047f032022-11-24 16:49:56 +01001832 FIH_DECLARE(fih_rc, FIH_FAILURE);
David Vinczeba3bd602019-06-17 16:01:43 +02001833
1834 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001835 rc = boot_read_sectors(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001836 if (rc != 0) {
1837 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
1838 " - too small?", BOOT_MAX_IMG_SECTORS);
1839 /* Unable to determine sector layout, continue with next image
1840 * if there is one.
1841 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001842 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Andrzej Puzdrowski54b4ad92021-06-22 13:21:22 +02001843 if (rc == BOOT_EFLASH)
1844 {
1845 /* Only return on error from the primary image flash */
1846 return;
1847 }
David Vinczeba3bd602019-06-17 16:01:43 +02001848 }
1849
1850 /* Attempt to read an image header from each slot. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001851 rc = boot_read_image_headers(state, false, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02001852 if (rc != 0) {
1853 /* Continue with next image if there is one. */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001854 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001855 BOOT_CURR_IMG(state));
1856 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001857 return;
1858 }
1859
1860 /* If the current image's slots aren't compatible, no swap is possible.
1861 * Just boot into primary slot.
1862 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001863 if (boot_slots_compatible(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001864 boot_status_reset(bs);
1865
1866#ifndef MCUBOOT_OVERWRITE_ONLY
1867 rc = swap_read_status(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001868 if (rc != 0) {
1869 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001870 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001871 /* Continue with next image if there is one. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001872 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001873 return;
1874 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001875#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001876
Thomas Altenbachf515bb12024-04-26 18:31:57 +02001877#if defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE)
Fabio Utzig74aef312019-11-28 11:05:34 -03001878 /*
1879 * Must re-read image headers because the boot status might
1880 * have been updated in the previous function call.
1881 */
1882 rc = boot_read_image_headers(state, !boot_status_is_reset(bs), bs);
Fabio Utzig32afe852020-10-04 10:36:02 -03001883#ifdef MCUBOOT_BOOTSTRAP
1884 /* When bootstrapping it's OK to not have image magic in the primary slot */
1885 if (rc != 0 && (BOOT_CURR_IMG(state) != BOOT_PRIMARY_SLOT ||
1886 boot_check_header_erased(state, BOOT_PRIMARY_SLOT) != 0)) {
1887#else
Fabio Utzig74aef312019-11-28 11:05:34 -03001888 if (rc != 0) {
Fabio Utzig32afe852020-10-04 10:36:02 -03001889#endif
1890
Fabio Utzig74aef312019-11-28 11:05:34 -03001891 /* Continue with next image if there is one. */
1892 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
1893 BOOT_CURR_IMG(state));
1894 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1895 return;
1896 }
1897#endif
1898
David Vinczeba3bd602019-06-17 16:01:43 +02001899 /* Determine if we rebooted in the middle of an image swap
1900 * operation. If a partial swap was detected, complete it.
1901 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001902 if (!boot_status_is_reset(bs)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001903
1904#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001905 boot_review_image_swap_types(state, true);
David Vinczeba3bd602019-06-17 16:01:43 +02001906#endif
1907
1908#ifdef MCUBOOT_OVERWRITE_ONLY
1909 /* Should never arrive here, overwrite-only mode has
1910 * no swap state.
1911 */
1912 assert(0);
1913#else
1914 /* Determine the type of swap operation being resumed from the
1915 * `swap-type` trailer field.
1916 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001917 rc = boot_complete_partial_swap(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001918 assert(rc == 0);
1919#endif
1920 /* Attempt to read an image header from each slot. Ensure that
1921 * image headers in slots are aligned with headers in boot_data.
1922 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001923 rc = boot_read_image_headers(state, false, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001924 assert(rc == 0);
1925
1926 /* Swap has finished set to NONE */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001927 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001928 } else {
1929 /* There was no partial swap, determine swap type. */
1930 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001931 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001932 } else {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001933 FIH_CALL(boot_validate_slot, fih_rc,
1934 state, BOOT_SECONDARY_SLOT, bs);
Michael Grand5047f032022-11-24 16:49:56 +01001935 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001936 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
1937 } else {
1938 BOOT_SWAP_TYPE(state) = bs->swap_type;
1939 }
David Vinczeba3bd602019-06-17 16:01:43 +02001940 }
1941
1942#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001943 boot_review_image_swap_types(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02001944#endif
1945
1946#ifdef MCUBOOT_BOOTSTRAP
Fabio Utzig10ee6482019-08-01 12:04:52 -03001947 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001948 /* Header checks are done first because they are
1949 * inexpensive. Since overwrite-only copies starting from
1950 * offset 0, if interrupted, it might leave a valid header
1951 * magic, so also run validation on the primary slot to be
1952 * sure it's not OK.
1953 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001954 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1955 FIH_CALL(boot_validate_slot, fih_rc,
1956 state, BOOT_PRIMARY_SLOT, bs);
1957
Michael Grand5047f032022-11-24 16:49:56 +01001958 if (rc == 0 || FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001959
Fabio Utzig3d77c952020-10-04 10:23:17 -03001960 rc = (boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC) ? 1: 0;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001961 FIH_CALL(boot_validate_slot, fih_rc,
1962 state, BOOT_SECONDARY_SLOT, bs);
1963
Michael Grand5047f032022-11-24 16:49:56 +01001964 if (rc == 1 && FIH_EQ(fih_rc, FIH_SUCCESS)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001965 /* Set swap type to REVERT to overwrite the primary
1966 * slot with the image contained in secondary slot
1967 * and to trigger the explicit setting of the
1968 * image_ok flag.
1969 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03001970 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02001971 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001972 }
1973 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001974#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001975 }
David Vinczeba3bd602019-06-17 16:01:43 +02001976 } else {
1977 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001978 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001979 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001980}
1981
Mark Horvathccaf7f82021-01-04 18:16:42 +01001982/**
1983 * Updates the security counter for the current image.
1984 *
1985 * @param state Boot loader status information.
1986 *
1987 * @return 0 on success; nonzero on failure.
1988 */
1989static int
1990boot_update_hw_rollback_protection(struct boot_loader_state *state)
1991{
1992#ifdef MCUBOOT_HW_ROLLBACK_PROT
1993 int rc;
1994
1995 /* Update the stored security counter with the active image's security
1996 * counter value. It will only be updated if the new security counter is
1997 * greater than the stored value.
1998 *
1999 * In case of a successful image swapping when the swap type is TEST the
2000 * security counter can be increased only after a reset, when the swap
2001 * type is NONE and the image has marked itself "OK" (the image_ok flag
2002 * has been set). This way a "revert" can be performed when it's
2003 * necessary.
2004 */
2005 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
2006 rc = boot_update_security_counter(
2007 BOOT_CURR_IMG(state),
2008 BOOT_PRIMARY_SLOT,
2009 boot_img_hdr(state, BOOT_PRIMARY_SLOT));
2010 if (rc != 0) {
2011 BOOT_LOG_ERR("Security counter update failed after image "
2012 "validation.");
2013 return rc;
2014 }
2015 }
2016
2017 return 0;
2018
2019#else /* MCUBOOT_HW_ROLLBACK_PROT */
2020 (void) (state);
2021
2022 return 0;
2023#endif
2024}
2025
Jerzy Kasenberge3f895d2022-04-12 15:05:33 +02002026/**
2027 * Checks test swap downgrade prevention conditions.
2028 *
2029 * Function called only for swap upgrades test run. It may prevent
2030 * swap if slot 1 image has <= version number or < security counter
2031 *
2032 * @param state Boot loader status information.
2033 *
2034 * @return 0 - image can be swapped, -1 downgrade prevention
2035 */
2036static int
2037check_downgrade_prevention(struct boot_loader_state *state)
2038{
2039#if defined(MCUBOOT_DOWNGRADE_PREVENTION) && \
2040 (defined(MCUBOOT_SWAP_USING_MOVE) || defined(MCUBOOT_SWAP_USING_SCRATCH))
2041 uint32_t security_counter[2];
2042 int rc;
2043
2044 if (MCUBOOT_DOWNGRADE_PREVENTION_SECURITY_COUNTER) {
2045 /* If there was security no counter in slot 0, allow swap */
2046 rc = bootutil_get_img_security_cnt(&(BOOT_IMG(state, 0).hdr),
2047 BOOT_IMG(state, 0).area,
2048 &security_counter[0]);
2049 if (rc != 0) {
2050 return 0;
2051 }
2052 /* If there is no security counter in slot 1, or it's lower than
2053 * that of slot 0, prevent downgrade */
2054 rc = bootutil_get_img_security_cnt(&(BOOT_IMG(state, 1).hdr),
2055 BOOT_IMG(state, 1).area,
2056 &security_counter[1]);
2057 if (rc != 0 || security_counter[0] > security_counter[1]) {
2058 rc = -1;
2059 }
2060 }
2061 else {
2062 rc = boot_version_cmp(&boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
2063 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
2064 }
2065 if (rc < 0) {
2066 /* Image in slot 0 prevents downgrade, delete image in slot 1 */
Antonio de Angelis48547002023-04-14 09:47:09 +01002067 BOOT_LOG_INF("Image %d in slot 1 erased due to downgrade prevention", BOOT_CURR_IMG(state));
Jerzy Kasenberge3f895d2022-04-12 15:05:33 +02002068 flash_area_erase(BOOT_IMG(state, 1).area, 0,
2069 flash_area_get_size(BOOT_IMG(state, 1).area));
2070 } else {
2071 rc = 0;
2072 }
2073 return rc;
2074#else
2075 (void)state;
2076 return 0;
2077#endif
2078}
2079
Michael Grand5047f032022-11-24 16:49:56 +01002080fih_ret
Fabio Utzig10ee6482019-08-01 12:04:52 -03002081context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08002082{
Marti Bolivar84898652017-06-13 17:20:22 -04002083 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02002084 struct boot_status bs;
David Vincze9015a5d2020-05-18 14:43:11 +02002085 int rc = -1;
Michael Grand5047f032022-11-24 16:49:56 +01002086 FIH_DECLARE(fih_rc, FIH_FAILURE);
Marti Bolivarc0b47912017-06-13 17:18:09 -04002087 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03002088 int image_index;
Fabio Utzig298913b2019-08-28 11:22:45 -03002089 bool has_upgrade;
Michael Grand5047f032022-11-24 16:49:56 +01002090 volatile int fih_cnt;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002091
2092 /* The array of slot sectors are defined here (as opposed to file scope) so
2093 * that they don't get allocated for non-boot-loader apps. This is
2094 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002095 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08002096 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002097 TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
2098 TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03002099#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03002100 TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03002101#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08002102
Fabio Utzig298913b2019-08-28 11:22:45 -03002103 has_upgrade = false;
2104
2105#if (BOOT_IMAGE_NUMBER == 1)
2106 (void)has_upgrade;
2107#endif
Fabio Utzigba829042018-09-18 08:29:34 -03002108
David Vinczeba3bd602019-06-17 16:01:43 +02002109 /* Iterate over all the images. By the end of the loop the swap type has
2110 * to be determined for each image and all aborted swaps have to be
2111 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002112 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002113 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002114#if BOOT_IMAGE_NUMBER > 1
2115 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2116 continue;
2117 }
2118#endif
David Vinczeba3bd602019-06-17 16:01:43 +02002119#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
2120 /* The keys used for encryption may no longer be valid (could belong to
2121 * another images). Therefore, mark them as invalid to force their reload
2122 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03002123 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03002124 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Brown554c52e2017-06-30 16:01:07 -06002125#endif
2126
Fabio Utzig10ee6482019-08-01 12:04:52 -03002127 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03002128
Fabio Utzig10ee6482019-08-01 12:04:52 -03002129 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002130 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03002131 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002132 secondary_slot_sectors[image_index];
Fabio Utzig12d59162019-11-28 10:01:59 -03002133#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03002134 state->scratch.sectors = scratch_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -03002135#endif
David Vinczeba3bd602019-06-17 16:01:43 +02002136
2137 /* Open primary and secondary image areas for the duration
2138 * of this call.
2139 */
2140 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03002141 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03002142 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02002143 assert(rc == 0);
Jamie McCrae2929a972023-09-25 11:12:20 +01002144
2145 if (rc != 0) {
2146 BOOT_LOG_ERR("Failed to open flash area ID %d (image %d slot %d): %d, "
2147 "cannot continue", fa_id, image_index, (int8_t)slot, rc);
2148 FIH_PANIC;
2149 }
David Vinczeba3bd602019-06-17 16:01:43 +02002150 }
Fabio Utzig12d59162019-11-28 10:01:59 -03002151#if MCUBOOT_SWAP_USING_SCRATCH
David Vinczeba3bd602019-06-17 16:01:43 +02002152 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03002153 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002154 assert(rc == 0);
Jamie McCrae2929a972023-09-25 11:12:20 +01002155
2156 if (rc != 0) {
2157 BOOT_LOG_ERR("Failed to open scratch flash area: %d, cannot continue", rc);
2158 FIH_PANIC;
2159 }
Fabio Utzig12d59162019-11-28 10:01:59 -03002160#endif
David Vinczeba3bd602019-06-17 16:01:43 +02002161
2162 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002163 boot_prepare_image_for_update(state, &bs);
Fabio Utzig298913b2019-08-28 11:22:45 -03002164
Fabio Utzige575b0b2019-09-11 12:34:23 -03002165 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
Fabio Utzig298913b2019-08-28 11:22:45 -03002166 has_upgrade = true;
2167 }
David Vinczeba3bd602019-06-17 16:01:43 +02002168 }
2169
David Vinczee32483f2019-06-13 10:46:24 +02002170#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig298913b2019-08-28 11:22:45 -03002171 if (has_upgrade) {
2172 /* Iterate over all the images and verify whether the image dependencies
2173 * are all satisfied and update swap type if necessary.
2174 */
2175 rc = boot_verify_dependencies(state);
David Vincze8b0b6372020-05-20 19:54:44 +02002176 if (rc != 0) {
Fabio Utzig298913b2019-08-28 11:22:45 -03002177 /*
2178 * It was impossible to upgrade because the expected dependency version
2179 * was not available. Here we already changed the swap_type so that
2180 * instead of asserting the bootloader, we continue and no upgrade is
2181 * performed.
2182 */
2183 rc = 0;
2184 }
2185 }
David Vinczee32483f2019-06-13 10:46:24 +02002186#endif
2187
Jamie McCrae56cb6102022-03-23 11:57:03 +00002188 /* Trigger status change callback with upgrading status */
2189 mcuboot_status_change(MCUBOOT_STATUS_UPGRADING);
2190
David Vinczeba3bd602019-06-17 16:01:43 +02002191 /* Iterate over all the images. At this point there are no aborted swaps
2192 * and the swap types are determined for each image. By the end of the loop
2193 * all required update operations will have been finished.
2194 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002195 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002196#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesf11de642021-10-15 11:11:56 +01002197 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2198 continue;
2199 }
2200
David Vinczeba3bd602019-06-17 16:01:43 +02002201#ifdef MCUBOOT_ENC_IMAGES
2202 /* The keys used for encryption may no longer be valid (could belong to
2203 * another images). Therefore, mark them as invalid to force their reload
2204 * by boot_enc_load().
2205 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03002206 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002207#endif /* MCUBOOT_ENC_IMAGES */
2208
2209 /* Indicate that swap is not aborted */
Fabio Utzig12d59162019-11-28 10:01:59 -03002210 boot_status_reset(&bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002211#endif /* (BOOT_IMAGE_NUMBER > 1) */
2212
2213 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002214 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02002215
Fabio Utzig10ee6482019-08-01 12:04:52 -03002216 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002217 case BOOT_SWAP_TYPE_NONE:
2218 break;
2219
Jerzy Kasenberge3f895d2022-04-12 15:05:33 +02002220 case BOOT_SWAP_TYPE_TEST:
Michael Grand99613c62023-06-20 15:01:00 +02002221 /* fallthrough */
2222 case BOOT_SWAP_TYPE_PERM:
Jerzy Kasenberge3f895d2022-04-12 15:05:33 +02002223 if (check_downgrade_prevention(state) != 0) {
2224 /* Downgrade prevented */
2225 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
2226 break;
2227 }
2228 /* fallthrough */
David Vinczeba3bd602019-06-17 16:01:43 +02002229 case BOOT_SWAP_TYPE_REVERT:
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +02002230 rc = BOOT_HOOK_CALL(boot_perform_update_hook, BOOT_HOOK_REGULAR,
2231 BOOT_CURR_IMG(state), &(BOOT_IMG(state, 1).hdr),
2232 BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT));
2233 if (rc == BOOT_HOOK_REGULAR)
2234 {
2235 rc = boot_perform_update(state, &bs);
2236 }
David Vinczeba3bd602019-06-17 16:01:43 +02002237 assert(rc == 0);
2238 break;
2239
2240 case BOOT_SWAP_TYPE_FAIL:
2241 /* The image in secondary slot was invalid and is now erased. Ensure
2242 * we don't try to boot into it again on the next reboot. Do this by
2243 * pretending we just reverted back to primary slot.
2244 */
2245#ifndef MCUBOOT_OVERWRITE_ONLY
2246 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig12d59162019-11-28 10:01:59 -03002247 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002248 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002249 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002250 }
2251#endif /* !MCUBOOT_OVERWRITE_ONLY */
2252 break;
2253
2254 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002255 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002256 }
2257
Fabio Utzig10ee6482019-08-01 12:04:52 -03002258 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002259 BOOT_LOG_ERR("panic!");
2260 assert(0);
2261
2262 /* Loop forever... */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002263 FIH_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002264 }
2265 }
2266
2267 /* Iterate over all the images. At this point all required update operations
2268 * have finished. By the end of the loop each image in the primary slot will
2269 * have been re-validated.
2270 */
Michael Grand5047f032022-11-24 16:49:56 +01002271 FIH_SET(fih_cnt, 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -03002272 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002273#if BOOT_IMAGE_NUMBER > 1
Michael Grand5047f032022-11-24 16:49:56 +01002274 /* Hardenned to prevent from skipping check of a given image,
2275 * tmp_img_mask is declared volatile
2276 */
2277 volatile bool tmp_img_mask;
2278 FIH_SET(tmp_img_mask, state->img_mask[BOOT_CURR_IMG(state)]);
2279 if (FIH_EQ(tmp_img_mask, true)) {
2280 ++fih_cnt;
Raef Colesf11de642021-10-15 11:11:56 +01002281 continue;
2282 }
2283#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -03002284 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02002285 /* Attempt to read an image header from each slot. Ensure that image
2286 * headers in slots are aligned with headers in boot_data.
Dominik Ermel6f7f8732024-02-01 11:59:24 +00002287 * Note: Quite complicated internal logic of boot_read_image_headers
2288 * uses boot state, the last parm, to figure out in which slot which
2289 * header is located; when boot state is not provided, then it
2290 * is assumed that headers are at proper slots (we are not in
2291 * the middle of moving images, etc).
David Vinczeba3bd602019-06-17 16:01:43 +02002292 */
Dominik Ermel6f7f8732024-02-01 11:59:24 +00002293 rc = boot_read_image_headers(state, false, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02002294 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01002295 FIH_SET(fih_rc, FIH_FAILURE);
David Vinczeba3bd602019-06-17 16:01:43 +02002296 goto out;
2297 }
2298 /* Since headers were reloaded, it can be assumed we just performed
2299 * a swap or overwrite. Now the header info that should be used to
2300 * provide the data for the bootstrap, which previously was at
2301 * secondary slot, was updated to primary slot.
2302 */
2303 }
2304
2305#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Raef Colese8fe6cf2020-05-26 13:07:40 +01002306 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, NULL);
Michael Grand5047f032022-11-24 16:49:56 +01002307 /* Check for all possible values is redundant in normal operation it
2308 * is meant to prevent FI attack.
2309 */
2310 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS) ||
2311 FIH_EQ(fih_rc, FIH_FAILURE) ||
2312 FIH_EQ(fih_rc, FIH_NO_BOOTABLE_IMAGE)) {
2313 FIH_SET(fih_rc, FIH_FAILURE);
David Vinczeba3bd602019-06-17 16:01:43 +02002314 goto out;
2315 }
2316#else
2317 /* Even if we're not re-validating the primary slot, we could be booting
2318 * onto an empty flash chip. At least do a basic sanity check that
2319 * the magic number on the image is OK.
2320 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002321 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002322 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
Dominik Ermel4a4d1ac2021-08-06 11:23:52 +00002323 BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic,
Fabio Utzig10ee6482019-08-01 12:04:52 -03002324 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002325 rc = BOOT_EBADIMAGE;
Michael Grand5047f032022-11-24 16:49:56 +01002326 FIH_SET(fih_rc, FIH_FAILURE);
David Vinczeba3bd602019-06-17 16:01:43 +02002327 goto out;
2328 }
David Vinczec3084132020-02-18 14:50:47 +01002329#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
2330
Mark Horvathccaf7f82021-01-04 18:16:42 +01002331 rc = boot_update_hw_rollback_protection(state);
David Vincze1cf11b52020-03-24 07:51:09 +01002332 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01002333 FIH_SET(fih_rc, FIH_FAILURE);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002334 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002335 }
David Vincze1cf11b52020-03-24 07:51:09 +01002336
Mark Horvathccaf7f82021-01-04 18:16:42 +01002337 rc = boot_add_shared_data(state, BOOT_PRIMARY_SLOT);
David Vincze1cf11b52020-03-24 07:51:09 +01002338 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01002339 FIH_SET(fih_rc, FIH_FAILURE);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002340 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002341 }
Michael Grand5047f032022-11-24 16:49:56 +01002342 ++fih_cnt;
David Vinczeba3bd602019-06-17 16:01:43 +02002343 }
Michael Grand5047f032022-11-24 16:49:56 +01002344 /*
2345 * fih_cnt should be equal to BOOT_IMAGE_NUMBER now.
2346 * If this is not the case, at least one iteration of the loop
2347 * has been skipped.
2348 */
2349 if(FIH_NOT_EQ(fih_cnt, BOOT_IMAGE_NUMBER)) {
2350 FIH_PANIC;
2351 }
Fabio Utzigf616c542019-12-19 15:23:32 -03002352
Raef Colesfe57e7d2021-10-15 11:07:09 +01002353 fill_rsp(state, rsp);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002354
Raef Colese8fe6cf2020-05-26 13:07:40 +01002355 fih_rc = FIH_SUCCESS;
Fabio Utzig298913b2019-08-28 11:22:45 -03002356out:
Dominik Ermel256bc372022-12-07 15:51:31 +00002357 /*
2358 * Since the boot_status struct stores plaintext encryption keys, reset
2359 * them here to avoid the possibility of jumping into an image that could
2360 * easily recover them.
2361 */
2362#if defined(MCUBOOT_ENC_IMAGES) || defined(MCUBOOT_SWAP_SAVE_ENCTLV)
2363 like_mbedtls_zeroize(&bs, sizeof(bs));
2364#else
2365 memset(&bs, 0, sizeof(struct boot_status));
2366#endif
2367
Mark Horvathccaf7f82021-01-04 18:16:42 +01002368 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002369 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002370}
2371
Michael Grand5047f032022-11-24 16:49:56 +01002372fih_ret
Christopher Collins92ea77f2016-12-12 15:59:26 -08002373split_go(int loader_slot, int split_slot, void **entry)
2374{
Marti Bolivarc50926f2017-06-14 09:35:40 -04002375 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08002376 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002377 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002378 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002379 int rc;
Michael Grand5047f032022-11-24 16:49:56 +01002380 FIH_DECLARE(fih_rc, FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002381
Christopher Collins92ea77f2016-12-12 15:59:26 -08002382 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
2383 if (sectors == NULL) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002384 FIH_RET(FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002385 }
David Vinczeba3bd602019-06-17 16:01:43 +02002386 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
2387 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002388
2389 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
2390 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07002391 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04002392 assert(rc == 0);
2393 split_flash_id = flash_area_id_from_image_slot(split_slot);
2394 rc = flash_area_open(split_flash_id,
2395 &BOOT_IMG_AREA(&boot_data, split_slot));
2396 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002397
2398 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002399 rc = boot_read_sectors(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002400 if (rc != 0) {
2401 rc = SPLIT_GO_ERR;
2402 goto done;
2403 }
2404
Fabio Utzig12d59162019-11-28 10:01:59 -03002405 rc = boot_read_image_headers(&boot_data, true, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002406 if (rc != 0) {
2407 goto done;
2408 }
2409
Christopher Collins92ea77f2016-12-12 15:59:26 -08002410 /* Don't check the bootable image flag because we could really call a
2411 * bootable or non-bootable image. Just validate that the image check
2412 * passes which is distinct from the normal check.
2413 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002414 FIH_CALL(split_image_check, fih_rc,
2415 boot_img_hdr(&boot_data, split_slot),
2416 BOOT_IMG_AREA(&boot_data, split_slot),
2417 boot_img_hdr(&boot_data, loader_slot),
2418 BOOT_IMG_AREA(&boot_data, loader_slot));
Michael Grand5047f032022-11-24 16:49:56 +01002419 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08002420 goto done;
2421 }
2422
Marti Bolivarea088872017-06-12 17:10:49 -04002423 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04002424 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08002425 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002426 rc = SPLIT_GO_OK;
2427
2428done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04002429 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
2430 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002431 free(sectors);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002432
2433 if (rc) {
Michael Grand5047f032022-11-24 16:49:56 +01002434 FIH_SET(fih_rc, FIH_FAILURE);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002435 }
2436
2437 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002438}
David Vinczee574f2d2020-07-10 11:42:03 +02002439
Tamas Banfe031092020-09-10 17:32:39 +02002440#else /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02002441
2442/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002443 * Opens all flash areas and checks which contain an image with a valid header.
David Vinczee574f2d2020-07-10 11:42:03 +02002444 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002445 * @param state Boot loader status information.
David Vinczee574f2d2020-07-10 11:42:03 +02002446 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002447 * @return 0 on success; nonzero on failure.
2448 */
2449static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002450boot_get_slot_usage(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002451{
2452 uint32_t slot;
2453 int fa_id;
2454 int rc;
2455 struct image_header *hdr = NULL;
2456
2457 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002458#if BOOT_IMAGE_NUMBER > 1
2459 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2460 continue;
2461 }
2462#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +01002463 /* Open all the slots */
2464 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2465 fa_id = flash_area_id_from_multi_image_slot(
2466 BOOT_CURR_IMG(state), slot);
2467 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
2468 assert(rc == 0);
2469 }
2470
2471 /* Attempt to read an image header from each slot. */
2472 rc = boot_read_image_headers(state, false, NULL);
2473 if (rc != 0) {
2474 BOOT_LOG_WRN("Failed reading image headers.");
2475 return rc;
2476 }
2477
2478 /* Check headers in all slots */
2479 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2480 hdr = boot_img_hdr(state, slot);
2481
2482 if (boot_is_header_valid(hdr, BOOT_IMG_AREA(state, slot))) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002483 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = true;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002484 BOOT_LOG_IMAGE_INFO(slot, hdr);
2485 } else {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002486 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = false;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002487 BOOT_LOG_INF("Image %d %s slot: Image not found",
2488 BOOT_CURR_IMG(state),
2489 (slot == BOOT_PRIMARY_SLOT)
2490 ? "Primary" : "Secondary");
2491 }
2492 }
2493
Raef Colesfe57e7d2021-10-15 11:07:09 +01002494 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002495 }
2496
2497 return 0;
2498}
2499
2500/**
2501 * Finds the slot containing the image with the highest version number for the
2502 * current image.
2503 *
2504 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002505 *
2506 * @return NO_ACTIVE_SLOT if no available slot found, number of
2507 * the found slot otherwise.
David Vinczee574f2d2020-07-10 11:42:03 +02002508 */
2509static uint32_t
Raef Colesfe57e7d2021-10-15 11:07:09 +01002510find_slot_with_highest_version(struct boot_loader_state *state)
David Vinczee574f2d2020-07-10 11:42:03 +02002511{
David Vinczee574f2d2020-07-10 11:42:03 +02002512 uint32_t slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002513 uint32_t candidate_slot = NO_ACTIVE_SLOT;
2514 int rc;
David Vinczee574f2d2020-07-10 11:42:03 +02002515
Mark Horvathccaf7f82021-01-04 18:16:42 +01002516 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002517 if (state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot]) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002518 if (candidate_slot == NO_ACTIVE_SLOT) {
2519 candidate_slot = slot;
2520 } else {
2521 rc = boot_version_cmp(
2522 &boot_img_hdr(state, slot)->ih_ver,
2523 &boot_img_hdr(state, candidate_slot)->ih_ver);
2524 if (rc == 1) {
2525 /* The version of the image being examined is greater than
2526 * the version of the current candidate.
2527 */
2528 candidate_slot = slot;
2529 }
2530 }
David Vinczee574f2d2020-07-10 11:42:03 +02002531 }
2532 }
2533
Mark Horvathccaf7f82021-01-04 18:16:42 +01002534 return candidate_slot;
David Vinczee574f2d2020-07-10 11:42:03 +02002535}
2536
Mark Horvathccaf7f82021-01-04 18:16:42 +01002537#ifdef MCUBOOT_HAVE_LOGGING
2538/**
2539 * Prints the state of the loaded images.
2540 *
2541 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002542 */
2543static void
Raef Colesfe57e7d2021-10-15 11:07:09 +01002544print_loaded_images(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002545{
2546 uint32_t active_slot;
2547
David Brown695e5912021-05-24 16:58:01 -06002548 (void)state;
2549
Mark Horvathccaf7f82021-01-04 18:16:42 +01002550 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002551#if BOOT_IMAGE_NUMBER > 1
2552 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2553 continue;
2554 }
2555#endif
Raef Colesfe57e7d2021-10-15 11:07:09 +01002556 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002557
2558 BOOT_LOG_INF("Image %d loaded from the %s slot",
2559 BOOT_CURR_IMG(state),
2560 (active_slot == BOOT_PRIMARY_SLOT) ?
2561 "primary" : "secondary");
2562 }
2563}
2564#endif
2565
David Vincze1c456242021-06-29 15:25:24 +02002566#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
David Vincze505fba22020-10-22 13:53:29 +02002567/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002568 * Checks whether the active slot of the current image was previously selected
2569 * to run. Erases the image if it was selected but its execution failed,
2570 * otherwise marks it as selected if it has not been before.
David Vincze505fba22020-10-22 13:53:29 +02002571 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002572 * @param state Boot loader status information.
David Vincze505fba22020-10-22 13:53:29 +02002573 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002574 * @return 0 on success; nonzero on failure.
David Vincze505fba22020-10-22 13:53:29 +02002575 */
2576static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002577boot_select_or_erase(struct boot_loader_state *state)
David Vincze505fba22020-10-22 13:53:29 +02002578{
2579 const struct flash_area *fap;
2580 int fa_id;
2581 int rc;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002582 uint32_t active_slot;
2583 struct boot_swap_state* active_swap_state;
David Vincze505fba22020-10-22 13:53:29 +02002584
Raef Colesfe57e7d2021-10-15 11:07:09 +01002585 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002586
2587 fa_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), active_slot);
David Vincze505fba22020-10-22 13:53:29 +02002588 rc = flash_area_open(fa_id, &fap);
2589 assert(rc == 0);
2590
Raef Colesfe57e7d2021-10-15 11:07:09 +01002591 active_swap_state = &(state->slot_usage[BOOT_CURR_IMG(state)].swap_state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002592
2593 memset(active_swap_state, 0, sizeof(struct boot_swap_state));
2594 rc = boot_read_swap_state(fap, active_swap_state);
David Vincze505fba22020-10-22 13:53:29 +02002595 assert(rc == 0);
2596
Mark Horvathccaf7f82021-01-04 18:16:42 +01002597 if (active_swap_state->magic != BOOT_MAGIC_GOOD ||
2598 (active_swap_state->copy_done == BOOT_FLAG_SET &&
2599 active_swap_state->image_ok != BOOT_FLAG_SET)) {
David Vincze505fba22020-10-22 13:53:29 +02002600 /*
2601 * A reboot happened without the image being confirmed at
2602 * runtime or its trailer is corrupted/invalid. Erase the image
2603 * to prevent it from being selected again on the next reboot.
2604 */
2605 BOOT_LOG_DBG("Erasing faulty image in the %s slot.",
Carlos Falgueras Garcíaae13c3c2021-06-21 17:20:31 +02002606 (active_slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
Dominik Ermel260ae092021-04-23 05:38:45 +00002607 rc = flash_area_erase(fap, 0, flash_area_get_size(fap));
David Vincze505fba22020-10-22 13:53:29 +02002608 assert(rc == 0);
2609
2610 flash_area_close(fap);
2611 rc = -1;
2612 } else {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002613 if (active_swap_state->copy_done != BOOT_FLAG_SET) {
2614 if (active_swap_state->copy_done == BOOT_FLAG_BAD) {
David Vincze505fba22020-10-22 13:53:29 +02002615 BOOT_LOG_DBG("The copy_done flag had an unexpected value. Its "
2616 "value was neither 'set' nor 'unset', but 'bad'.");
2617 }
2618 /*
2619 * Set the copy_done flag, indicating that the image has been
2620 * selected to boot. It can be set in advance, before even
2621 * validating the image, because in case the validation fails, the
2622 * entire image slot will be erased (including the trailer).
2623 */
2624 rc = boot_write_copy_done(fap);
2625 if (rc != 0) {
2626 BOOT_LOG_WRN("Failed to set copy_done flag of the image in "
Carlos Falgueras Garcíaae13c3c2021-06-21 17:20:31 +02002627 "the %s slot.", (active_slot == BOOT_PRIMARY_SLOT) ?
David Vincze505fba22020-10-22 13:53:29 +02002628 "primary" : "secondary");
2629 rc = 0;
2630 }
2631 }
2632 flash_area_close(fap);
2633 }
2634
2635 return rc;
2636}
David Vincze1c456242021-06-29 15:25:24 +02002637#endif /* MCUBOOT_DIRECT_XIP && MCUBOOT_DIRECT_XIP_REVERT */
David Vincze505fba22020-10-22 13:53:29 +02002638
Tamas Banfe031092020-09-10 17:32:39 +02002639#ifdef MCUBOOT_RAM_LOAD
2640
Mark Horvathccaf7f82021-01-04 18:16:42 +01002641#ifndef MULTIPLE_EXECUTABLE_RAM_REGIONS
Tamas Banfe031092020-09-10 17:32:39 +02002642#if !defined(IMAGE_EXECUTABLE_RAM_START) || !defined(IMAGE_EXECUTABLE_RAM_SIZE)
2643#error "Platform MUST define executable RAM bounds in case of RAM_LOAD"
2644#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +01002645#endif
Tamas Banfe031092020-09-10 17:32:39 +02002646
2647/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002648 * Verifies that the active slot of the current image can be loaded within the
2649 * predefined bounds that are allowed to be used by executable images.
Tamas Banfe031092020-09-10 17:32:39 +02002650 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002651 * @param state Boot loader status information.
Tamas Banfe031092020-09-10 17:32:39 +02002652 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002653 * @return 0 on success; nonzero on failure.
Tamas Banfe031092020-09-10 17:32:39 +02002654 */
2655static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002656boot_verify_ram_load_address(struct boot_loader_state *state)
Tamas Banfe031092020-09-10 17:32:39 +02002657{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002658 uint32_t img_dst;
2659 uint32_t img_sz;
Tamas Banfe031092020-09-10 17:32:39 +02002660 uint32_t img_end_addr;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002661 uint32_t exec_ram_start;
2662 uint32_t exec_ram_size;
David Brown695e5912021-05-24 16:58:01 -06002663
2664 (void)state;
2665
Mark Horvathccaf7f82021-01-04 18:16:42 +01002666#ifdef MULTIPLE_EXECUTABLE_RAM_REGIONS
2667 int rc;
Tamas Banfe031092020-09-10 17:32:39 +02002668
Mark Horvathccaf7f82021-01-04 18:16:42 +01002669 rc = boot_get_image_exec_ram_info(BOOT_CURR_IMG(state), &exec_ram_start,
2670 &exec_ram_size);
2671 if (rc != 0) {
2672 return BOOT_EBADSTATUS;
2673 }
2674#else
2675 exec_ram_start = IMAGE_EXECUTABLE_RAM_START;
2676 exec_ram_size = IMAGE_EXECUTABLE_RAM_SIZE;
2677#endif
2678
Raef Colesfe57e7d2021-10-15 11:07:09 +01002679 img_dst = state->slot_usage[BOOT_CURR_IMG(state)].img_dst;
2680 img_sz = state->slot_usage[BOOT_CURR_IMG(state)].img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002681
2682 if (img_dst < exec_ram_start) {
Tamas Banfe031092020-09-10 17:32:39 +02002683 return BOOT_EBADIMAGE;
2684 }
2685
2686 if (!boot_u32_safe_add(&img_end_addr, img_dst, img_sz)) {
2687 return BOOT_EBADIMAGE;
2688 }
2689
Mark Horvathccaf7f82021-01-04 18:16:42 +01002690 if (img_end_addr > (exec_ram_start + exec_ram_size)) {
Tamas Banfe031092020-09-10 17:32:39 +02002691 return BOOT_EBADIMAGE;
2692 }
2693
2694 return 0;
2695}
2696
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002697#ifdef MCUBOOT_ENC_IMAGES
2698
2699/**
2700 * Copies and decrypts an image from a slot in the flash to an SRAM address.
2701 *
2702 * @param state Boot loader status information.
2703 * @param slot The flash slot of the image to be copied to SRAM.
2704 * @param hdr The image header.
2705 * @param src_sz Size of the image.
2706 * @param img_dst Pointer to the address at which the image needs to be
2707 * copied to SRAM.
2708 *
2709 * @return 0 on success; nonzero on failure.
2710 */
2711static int
2712boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state,
2713 uint32_t slot, struct image_header *hdr,
2714 uint32_t src_sz, uint32_t img_dst)
2715{
2716 /* The flow for the decryption and copy of the image is as follows :
2717 * 1. The whole image is copied to the RAM (header + payload + TLV).
2718 * 2. The encryption key is loaded from the TLV in flash.
2719 * 3. The image is then decrypted chunk by chunk in RAM (1 chunk
2720 * is 1024 bytes). Only the payload section is decrypted.
2721 * 4. The image is authenticated in RAM.
2722 */
2723 const struct flash_area *fap_src = NULL;
2724 struct boot_status bs;
2725 uint32_t blk_off;
2726 uint32_t tlv_off;
2727 uint32_t blk_sz;
2728 uint32_t bytes_copied = hdr->ih_hdr_size;
2729 uint32_t chunk_sz;
2730 uint32_t max_sz = 1024;
2731 uint16_t idx;
2732 uint8_t image_index;
2733 uint8_t * cur_dst;
2734 int area_id;
2735 int rc;
2736 uint8_t * ram_dst = (void *)(IMAGE_RAM_BASE + img_dst);
2737
2738 image_index = BOOT_CURR_IMG(state);
2739 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2740 rc = flash_area_open(area_id, &fap_src);
2741 if (rc != 0){
2742 return BOOT_EFLASH;
2743 }
2744
2745 tlv_off = BOOT_TLV_OFF(hdr);
2746
2747 /* Copying the whole image in RAM */
2748 rc = flash_area_read(fap_src, 0, ram_dst, src_sz);
2749 if (rc != 0) {
2750 goto done;
2751 }
2752
2753 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap_src, &bs);
2754 if (rc < 0) {
2755 goto done;
2756 }
2757
2758 /* if rc > 0 then the key has already been loaded */
2759 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), slot, &bs)) {
2760 goto done;
2761 }
2762
2763 /* Starting at the end of the header as the header section is not encrypted */
2764 while (bytes_copied < tlv_off) { /* TLV section copied previously */
2765 if (src_sz - bytes_copied > max_sz) {
2766 chunk_sz = max_sz;
2767 } else {
2768 chunk_sz = src_sz - bytes_copied;
2769 }
2770
2771 cur_dst = ram_dst + bytes_copied;
2772 blk_sz = chunk_sz;
2773 idx = 0;
2774 if (bytes_copied + chunk_sz > tlv_off) {
2775 /* Going over TLV section
2776 * Part of the chunk is encrypted payload */
2777 blk_off = ((bytes_copied) - hdr->ih_hdr_size) & 0xf;
2778 blk_sz = tlv_off - (bytes_copied);
Thomas Altenbach08d2d942024-04-25 15:29:21 +02002779 boot_encrypt(BOOT_CURR_ENC(state), image_index, area_id,
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002780 (bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
2781 blk_off, cur_dst);
2782 } else {
2783 /* Image encrypted payload section */
2784 blk_off = ((bytes_copied) - hdr->ih_hdr_size) & 0xf;
Thomas Altenbach08d2d942024-04-25 15:29:21 +02002785 boot_encrypt(BOOT_CURR_ENC(state), image_index, area_id,
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002786 (bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
2787 blk_off, cur_dst);
2788 }
2789
2790 bytes_copied += chunk_sz;
2791 }
2792 rc = 0;
2793
2794done:
2795 flash_area_close(fap_src);
2796
2797 return rc;
2798}
2799
2800#endif /* MCUBOOT_ENC_IMAGES */
Tamas Banfe031092020-09-10 17:32:39 +02002801/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002802 * Copies a slot of the current image into SRAM.
Tamas Banfe031092020-09-10 17:32:39 +02002803 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002804 * @param state Boot loader status information.
Tamas Banfe031092020-09-10 17:32:39 +02002805 * @param slot The flash slot of the image to be copied to SRAM.
2806 * @param img_dst The address at which the image needs to be copied to
2807 * SRAM.
2808 * @param img_sz The size of the image that needs to be copied to SRAM.
2809 *
2810 * @return 0 on success; nonzero on failure.
2811 */
2812static int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002813boot_copy_image_to_sram(struct boot_loader_state *state, int slot,
2814 uint32_t img_dst, uint32_t img_sz)
Tamas Banfe031092020-09-10 17:32:39 +02002815{
2816 int rc;
2817 const struct flash_area *fap_src = NULL;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002818 int area_id;
Tamas Banfe031092020-09-10 17:32:39 +02002819
Mark Horvathccaf7f82021-01-04 18:16:42 +01002820#if (BOOT_IMAGE_NUMBER == 1)
2821 (void)state;
2822#endif
2823
2824 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2825
2826 rc = flash_area_open(area_id, &fap_src);
Tamas Banfe031092020-09-10 17:32:39 +02002827 if (rc != 0) {
2828 return BOOT_EFLASH;
2829 }
2830
2831 /* Direct copy from flash to its new location in SRAM. */
David Brown9bd7f902021-05-26 16:31:14 -06002832 rc = flash_area_read(fap_src, 0, (void *)(IMAGE_RAM_BASE + img_dst), img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002833 if (rc != 0) {
Antonio de Angelis48547002023-04-14 09:47:09 +01002834 BOOT_LOG_INF("Error whilst copying image %d from Flash to SRAM: %d",
2835 BOOT_CURR_IMG(state), rc);
Tamas Banfe031092020-09-10 17:32:39 +02002836 }
2837
2838 flash_area_close(fap_src);
2839
2840 return rc;
2841}
2842
Mark Horvathccaf7f82021-01-04 18:16:42 +01002843#if (BOOT_IMAGE_NUMBER > 1)
Tamas Banfe031092020-09-10 17:32:39 +02002844/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002845 * Checks if two memory regions (A and B) are overlap or not.
Tamas Banfe031092020-09-10 17:32:39 +02002846 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002847 * @param start_a Start of the A region.
2848 * @param end_a End of the A region.
2849 * @param start_b Start of the B region.
2850 * @param end_b End of the B region.
Tamas Banfe031092020-09-10 17:32:39 +02002851 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002852 * @return true if there is overlap; false otherwise.
2853 */
2854static bool
2855do_regions_overlap(uint32_t start_a, uint32_t end_a,
2856 uint32_t start_b, uint32_t end_b)
2857{
2858 if (start_b > end_a) {
2859 return false;
2860 } else if (start_b >= start_a) {
2861 return true;
2862 } else if (end_b > start_a) {
2863 return true;
2864 }
2865
2866 return false;
2867}
2868
2869/**
2870 * Checks if the image we want to load to memory overlap with an already
2871 * ramloaded image.
2872 *
Raef Colesfe57e7d2021-10-15 11:07:09 +01002873 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002874 *
2875 * @return 0 if there is no overlap; nonzero otherwise.
Tamas Banfe031092020-09-10 17:32:39 +02002876 */
2877static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002878boot_check_ram_load_overlapping(struct boot_loader_state *state)
Tamas Banfe031092020-09-10 17:32:39 +02002879{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002880 uint32_t i;
2881
2882 uint32_t start_a;
2883 uint32_t end_a;
2884 uint32_t start_b;
2885 uint32_t end_b;
Raef Colesfe57e7d2021-10-15 11:07:09 +01002886 uint32_t image_id_to_check = BOOT_CURR_IMG(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002887
Raef Colesfe57e7d2021-10-15 11:07:09 +01002888 start_a = state->slot_usage[image_id_to_check].img_dst;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002889 /* Safe to add here, values are already verified in
2890 * boot_verify_ram_load_address() */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002891 end_a = start_a + state->slot_usage[image_id_to_check].img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002892
2893 for (i = 0; i < BOOT_IMAGE_NUMBER; i++) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002894 if (state->slot_usage[i].active_slot == NO_ACTIVE_SLOT
Mark Horvathccaf7f82021-01-04 18:16:42 +01002895 || i == image_id_to_check) {
2896 continue;
2897 }
2898
Raef Colesfe57e7d2021-10-15 11:07:09 +01002899 start_b = state->slot_usage[i].img_dst;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002900 /* Safe to add here, values are already verified in
2901 * boot_verify_ram_load_address() */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002902 end_b = start_b + state->slot_usage[i].img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002903
2904 if (do_regions_overlap(start_a, end_a, start_b, end_b)) {
2905 return -1;
2906 }
2907 }
2908
2909 return 0;
2910}
2911#endif
2912
2913/**
2914 * Loads the active slot of the current image into SRAM. The load address and
2915 * image size is extracted from the image header.
2916 *
2917 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002918 *
2919 * @return 0 on success; nonzero on failure.
2920 */
2921static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002922boot_load_image_to_sram(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002923{
2924 uint32_t active_slot;
2925 struct image_header *hdr = NULL;
2926 uint32_t img_dst;
2927 uint32_t img_sz;
Tamas Banfe031092020-09-10 17:32:39 +02002928 int rc;
2929
Raef Colesfe57e7d2021-10-15 11:07:09 +01002930 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002931 hdr = boot_img_hdr(state, active_slot);
2932
Tamas Banfe031092020-09-10 17:32:39 +02002933 if (hdr->ih_flags & IMAGE_F_RAM_LOAD) {
2934
Mark Horvathccaf7f82021-01-04 18:16:42 +01002935 img_dst = hdr->ih_load_addr;
Tamas Banfe031092020-09-10 17:32:39 +02002936
Mark Horvathccaf7f82021-01-04 18:16:42 +01002937 rc = boot_read_image_size(state, active_slot, &img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002938 if (rc != 0) {
2939 return rc;
2940 }
2941
Raef Colesfe57e7d2021-10-15 11:07:09 +01002942 state->slot_usage[BOOT_CURR_IMG(state)].img_dst = img_dst;
2943 state->slot_usage[BOOT_CURR_IMG(state)].img_sz = img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002944
Raef Colesfe57e7d2021-10-15 11:07:09 +01002945 rc = boot_verify_ram_load_address(state);
Tamas Banfe031092020-09-10 17:32:39 +02002946 if (rc != 0) {
Antonio de Angelisba5fb1c2022-10-11 10:10:37 +01002947 BOOT_LOG_INF("Image %d RAM load address 0x%x is invalid.", BOOT_CURR_IMG(state), img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002948 return rc;
2949 }
2950
Mark Horvathccaf7f82021-01-04 18:16:42 +01002951#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesfe57e7d2021-10-15 11:07:09 +01002952 rc = boot_check_ram_load_overlapping(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002953 if (rc != 0) {
Antonio de Angelisba5fb1c2022-10-11 10:10:37 +01002954 BOOT_LOG_INF("Image %d RAM loading to address 0x%x would overlap with\
2955 another image.", BOOT_CURR_IMG(state), img_dst);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002956 return rc;
2957 }
2958#endif
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002959#ifdef MCUBOOT_ENC_IMAGES
2960 /* decrypt image if encrypted and copy it to RAM */
2961 if (IS_ENCRYPTED(hdr)) {
2962 rc = boot_decrypt_and_copy_image_to_sram(state, active_slot, hdr, img_sz, img_dst);
2963 } else {
2964 rc = boot_copy_image_to_sram(state, active_slot, img_dst, img_sz);
2965 }
2966#else
Tamas Banfe031092020-09-10 17:32:39 +02002967 /* Copy image to the load address from where it currently resides in
2968 * flash.
2969 */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002970 rc = boot_copy_image_to_sram(state, active_slot, img_dst, img_sz);
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002971#endif
Tamas Banfe031092020-09-10 17:32:39 +02002972 if (rc != 0) {
Antonio de Angelisba5fb1c2022-10-11 10:10:37 +01002973 BOOT_LOG_INF("Image %d RAM loading to 0x%x is failed.", BOOT_CURR_IMG(state), img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002974 } else {
Antonio de Angelisba5fb1c2022-10-11 10:10:37 +01002975 BOOT_LOG_INF("Image %d RAM loading to 0x%x is succeeded.", BOOT_CURR_IMG(state), img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002976 }
2977 } else {
2978 /* Only images that support IMAGE_F_RAM_LOAD are allowed if
2979 * MCUBOOT_RAM_LOAD is set.
2980 */
2981 rc = BOOT_EBADIMAGE;
2982 }
2983
Mark Horvathccaf7f82021-01-04 18:16:42 +01002984 if (rc != 0) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002985 state->slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
2986 state->slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002987 }
2988
Tamas Banfe031092020-09-10 17:32:39 +02002989 return rc;
2990}
2991
2992/**
2993 * Removes an image from SRAM, by overwriting it with zeros.
2994 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002995 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002996 *
2997 * @return 0 on success; nonzero on failure.
2998 */
2999static inline int
Raef Colesfe57e7d2021-10-15 11:07:09 +01003000boot_remove_image_from_sram(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003001{
David Brown695e5912021-05-24 16:58:01 -06003002 (void)state;
3003
Antonio de Angelisba5fb1c2022-10-11 10:10:37 +01003004 BOOT_LOG_INF("Removing image %d from SRAM at address 0x%x",
3005 BOOT_CURR_IMG(state),
Raef Colesfe57e7d2021-10-15 11:07:09 +01003006 state->slot_usage[BOOT_CURR_IMG(state)].img_dst);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003007
Raef Colesfe57e7d2021-10-15 11:07:09 +01003008 memset((void*)(IMAGE_RAM_BASE + state->slot_usage[BOOT_CURR_IMG(state)].img_dst),
3009 0, state->slot_usage[BOOT_CURR_IMG(state)].img_sz);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003010
Raef Colesfe57e7d2021-10-15 11:07:09 +01003011 state->slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
3012 state->slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003013
3014 return 0;
3015}
3016
3017/**
3018 * Removes an image from flash by erasing the corresponding flash area
3019 *
3020 * @param state Boot loader status information.
3021 * @param slot The flash slot of the image to be erased.
Tamas Banfe031092020-09-10 17:32:39 +02003022 *
3023 * @return 0 on success; nonzero on failure.
3024 */
3025static inline int
Mark Horvathccaf7f82021-01-04 18:16:42 +01003026boot_remove_image_from_flash(struct boot_loader_state *state, uint32_t slot)
Tamas Banfe031092020-09-10 17:32:39 +02003027{
Mark Horvathccaf7f82021-01-04 18:16:42 +01003028 int area_id;
3029 int rc;
3030 const struct flash_area *fap;
Tamas Banfe031092020-09-10 17:32:39 +02003031
David Brown695e5912021-05-24 16:58:01 -06003032 (void)state;
3033
Mark Horvathccaf7f82021-01-04 18:16:42 +01003034 BOOT_LOG_INF("Removing image %d slot %d from flash", BOOT_CURR_IMG(state),
3035 slot);
3036 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
3037 rc = flash_area_open(area_id, &fap);
3038 if (rc == 0) {
Dominik Ermel260ae092021-04-23 05:38:45 +00003039 flash_area_erase(fap, 0, flash_area_get_size(fap));
Dominik Ermel245de812022-09-02 13:39:23 +00003040 flash_area_close(fap);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003041 }
3042
3043 return rc;
Tamas Banfe031092020-09-10 17:32:39 +02003044}
3045#endif /* MCUBOOT_RAM_LOAD */
3046
Mark Horvathccaf7f82021-01-04 18:16:42 +01003047
3048/**
3049 * Tries to load a slot for all the images with validation.
3050 *
3051 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01003052 *
3053 * @return 0 on success; nonzero on failure.
3054 */
Michael Grand5047f032022-11-24 16:49:56 +01003055fih_ret
Raef Colesfe57e7d2021-10-15 11:07:09 +01003056boot_load_and_validate_images(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003057{
3058 uint32_t active_slot;
3059 int rc;
Michael Grand5047f032022-11-24 16:49:56 +01003060 fih_ret fih_rc;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003061
3062 /* Go over all the images and try to load one */
3063 IMAGES_ITER(BOOT_CURR_IMG(state)) {
3064 /* All slots tried until a valid image found. Breaking from this loop
3065 * means that a valid image found or already loaded. If no slot is
3066 * found the function returns with error code. */
3067 while (true) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01003068 /* Go over all the slots and try to load one */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003069 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003070 if (active_slot != NO_ACTIVE_SLOT){
3071 /* A slot is already active, go to next image. */
3072 break;
David Vinczee574f2d2020-07-10 11:42:03 +02003073 }
David Vincze505fba22020-10-22 13:53:29 +02003074
Raef Colesfe57e7d2021-10-15 11:07:09 +01003075 active_slot = find_slot_with_highest_version(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003076 if (active_slot == NO_ACTIVE_SLOT) {
3077 BOOT_LOG_INF("No slot to load for image %d",
3078 BOOT_CURR_IMG(state));
3079 FIH_RET(FIH_FAILURE);
3080 }
3081
3082 /* Save the number of the active slot. */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003083 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003084
Raef Colesf11de642021-10-15 11:11:56 +01003085#if BOOT_IMAGE_NUMBER > 1
3086 if (state->img_mask[BOOT_CURR_IMG(state)]) {
3087 continue;
3088 }
3089#endif
3090
Mark Horvathccaf7f82021-01-04 18:16:42 +01003091#ifdef MCUBOOT_DIRECT_XIP
Raef Colesfe57e7d2021-10-15 11:07:09 +01003092 rc = boot_rom_address_check(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003093 if (rc != 0) {
3094 /* The image is placed in an unsuitable slot. */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003095 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3096 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003097 continue;
3098 }
George Becksteind4d90f82021-05-11 02:00:00 -04003099
David Vincze505fba22020-10-22 13:53:29 +02003100#ifdef MCUBOOT_DIRECT_XIP_REVERT
Raef Colesfe57e7d2021-10-15 11:07:09 +01003101 rc = boot_select_or_erase(state);
David Vincze505fba22020-10-22 13:53:29 +02003102 if (rc != 0) {
3103 /* The selected image slot has been erased. */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003104 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3105 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
David Vincze505fba22020-10-22 13:53:29 +02003106 continue;
3107 }
3108#endif /* MCUBOOT_DIRECT_XIP_REVERT */
David Vincze1c456242021-06-29 15:25:24 +02003109#endif /* MCUBOOT_DIRECT_XIP */
David Vincze505fba22020-10-22 13:53:29 +02003110
Tamas Banfe031092020-09-10 17:32:39 +02003111#ifdef MCUBOOT_RAM_LOAD
3112 /* Image is first loaded to RAM and authenticated there in order to
3113 * prevent TOCTOU attack during image copy. This could be applied
3114 * when loading images from external (untrusted) flash to internal
3115 * (trusted) RAM and image is authenticated before copying.
3116 */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003117 rc = boot_load_image_to_sram(state);
Tamas Banfe031092020-09-10 17:32:39 +02003118 if (rc != 0 ) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01003119 /* Image cannot be ramloaded. */
3120 boot_remove_image_from_flash(state, active_slot);
Raef Colesfe57e7d2021-10-15 11:07:09 +01003121 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3122 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Tamas Banfe031092020-09-10 17:32:39 +02003123 continue;
Tamas Banfe031092020-09-10 17:32:39 +02003124 }
3125#endif /* MCUBOOT_RAM_LOAD */
Mark Horvathccaf7f82021-01-04 18:16:42 +01003126
3127 FIH_CALL(boot_validate_slot, fih_rc, state, active_slot, NULL);
Michael Grand5047f032022-11-24 16:49:56 +01003128 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01003129 /* Image is invalid. */
Tamas Banfe031092020-09-10 17:32:39 +02003130#ifdef MCUBOOT_RAM_LOAD
Raef Colesfe57e7d2021-10-15 11:07:09 +01003131 boot_remove_image_from_sram(state);
Tamas Banfe031092020-09-10 17:32:39 +02003132#endif /* MCUBOOT_RAM_LOAD */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003133 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3134 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003135 continue;
David Vincze505fba22020-10-22 13:53:29 +02003136 }
Mark Horvathccaf7f82021-01-04 18:16:42 +01003137
3138 /* Valid image loaded from a slot, go to next image. */
3139 break;
3140 }
3141 }
3142
3143 FIH_RET(FIH_SUCCESS);
3144}
3145
3146/**
3147 * Updates the security counter for the current image.
3148 *
3149 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01003150 *
3151 * @return 0 on success; nonzero on failure.
3152 */
3153static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01003154boot_update_hw_rollback_protection(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003155{
3156#ifdef MCUBOOT_HW_ROLLBACK_PROT
3157 int rc;
3158
3159 /* Update the stored security counter with the newer (active) image's
3160 * security counter value.
3161 */
David Vincze1c456242021-06-29 15:25:24 +02003162#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003163 /* When the 'revert' mechanism is enabled in direct-xip mode, the
3164 * security counter can be increased only after reboot, if the image
3165 * has been confirmed at runtime (the image_ok flag has been set).
3166 * This way a 'revert' can be performed when it's necessary.
3167 */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003168 if (state->slot_usage[BOOT_CURR_IMG(state)].swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze505fba22020-10-22 13:53:29 +02003169#endif
Sherry Zhang50b06ae2021-07-09 15:22:51 +08003170 rc = boot_update_security_counter(BOOT_CURR_IMG(state),
Raef Colesfe57e7d2021-10-15 11:07:09 +01003171 state->slot_usage[BOOT_CURR_IMG(state)].active_slot,
3172 boot_img_hdr(state, state->slot_usage[BOOT_CURR_IMG(state)].active_slot));
David Vinczee574f2d2020-07-10 11:42:03 +02003173 if (rc != 0) {
Antonio de Angelisba5fb1c2022-10-11 10:10:37 +01003174 BOOT_LOG_ERR("Security counter update failed after image %d validation.", BOOT_CURR_IMG(state));
Mark Horvathccaf7f82021-01-04 18:16:42 +01003175 return rc;
David Vinczee574f2d2020-07-10 11:42:03 +02003176 }
David Vincze1c456242021-06-29 15:25:24 +02003177#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003178 }
3179#endif
David Vinczee574f2d2020-07-10 11:42:03 +02003180
Mark Horvathccaf7f82021-01-04 18:16:42 +01003181 return 0;
David Vinczee574f2d2020-07-10 11:42:03 +02003182
Mark Horvathccaf7f82021-01-04 18:16:42 +01003183#else /* MCUBOOT_HW_ROLLBACK_PROT */
3184 (void) (state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003185 return 0;
3186#endif
3187}
3188
Michael Grand5047f032022-11-24 16:49:56 +01003189fih_ret
Mark Horvathccaf7f82021-01-04 18:16:42 +01003190context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
3191{
Mark Horvathccaf7f82021-01-04 18:16:42 +01003192 int rc;
Michael Grand5047f032022-11-24 16:49:56 +01003193 FIH_DECLARE(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003194
Raef Colesfe57e7d2021-10-15 11:07:09 +01003195 rc = boot_get_slot_usage(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003196 if (rc != 0) {
David Vinczee574f2d2020-07-10 11:42:03 +02003197 goto out;
3198 }
3199
Mark Horvathccaf7f82021-01-04 18:16:42 +01003200#if (BOOT_IMAGE_NUMBER > 1)
3201 while (true) {
3202#endif
Raef Colesfe57e7d2021-10-15 11:07:09 +01003203 FIH_CALL(boot_load_and_validate_images, fih_rc, state);
Michael Grand5047f032022-11-24 16:49:56 +01003204 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
3205 FIH_SET(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003206 goto out;
3207 }
3208
3209#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesfe57e7d2021-10-15 11:07:09 +01003210 rc = boot_verify_dependencies(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003211 if (rc != 0) {
3212 /* Dependency check failed for an image, it has been removed from
3213 * SRAM in case of MCUBOOT_RAM_LOAD strategy, and set to
3214 * unavailable. Try to load an image from another slot.
3215 */
3216 continue;
3217 }
3218 /* Dependency check was successful. */
3219 break;
3220 }
3221#endif
3222
3223 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01003224#if BOOT_IMAGE_NUMBER > 1
3225 if (state->img_mask[BOOT_CURR_IMG(state)]) {
3226 continue;
3227 }
3228#endif
Raef Colesfe57e7d2021-10-15 11:07:09 +01003229 rc = boot_update_hw_rollback_protection(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003230 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01003231 FIH_SET(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003232 goto out;
3233 }
3234
Jamie McCrae3016d002023-03-14 12:35:51 +00003235 rc = boot_add_shared_data(state, (uint8_t)state->slot_usage[BOOT_CURR_IMG(state)].active_slot);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003236 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01003237 FIH_SET(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003238 goto out;
3239 }
3240 }
3241
3242 /* All image loaded successfully. */
3243#ifdef MCUBOOT_HAVE_LOGGING
Raef Colesfe57e7d2021-10-15 11:07:09 +01003244 print_loaded_images(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003245#endif
3246
Raef Colesfe57e7d2021-10-15 11:07:09 +01003247 fill_rsp(state, rsp);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003248
David Vinczee574f2d2020-07-10 11:42:03 +02003249out:
Mark Horvathccaf7f82021-01-04 18:16:42 +01003250 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01003251
Michael Grand5047f032022-11-24 16:49:56 +01003252 if (rc != 0) {
3253 FIH_SET(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003254 }
Raef Colese8fe6cf2020-05-26 13:07:40 +01003255
Mark Horvathccaf7f82021-01-04 18:16:42 +01003256 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02003257}
Tamas Banfe031092020-09-10 17:32:39 +02003258#endif /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02003259
3260/**
Raef Colese8fe6cf2020-05-26 13:07:40 +01003261 * Prepares the booting process. This function moves images around in flash as
David Vinczee574f2d2020-07-10 11:42:03 +02003262 * appropriate, and tells you what address to boot from.
3263 *
3264 * @param rsp On success, indicates how booting should occur.
3265 *
Raef Colese8fe6cf2020-05-26 13:07:40 +01003266 * @return FIH_SUCCESS on success; nonzero on failure.
David Vinczee574f2d2020-07-10 11:42:03 +02003267 */
Michael Grand5047f032022-11-24 16:49:56 +01003268fih_ret
David Vinczee574f2d2020-07-10 11:42:03 +02003269boot_go(struct boot_rsp *rsp)
3270{
Michael Grand5047f032022-11-24 16:49:56 +01003271 FIH_DECLARE(fih_rc, FIH_FAILURE);
Raef Colesf11de642021-10-15 11:11:56 +01003272
3273 boot_state_clear(NULL);
3274
Raef Colese8fe6cf2020-05-26 13:07:40 +01003275 FIH_CALL(context_boot_go, fih_rc, &boot_data, rsp);
3276 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02003277}
Raef Colesf11de642021-10-15 11:11:56 +01003278
3279/**
3280 * Prepares the booting process, considering only a single image. This function
3281 * moves images around in flash as appropriate, and tells you what address to
3282 * boot from.
3283 *
3284 * @param rsp On success, indicates how booting should occur.
3285 *
3286 * @param image_id The image ID to prepare the boot process for.
3287 *
3288 * @return FIH_SUCCESS on success; nonzero on failure.
3289 */
Michael Grand5047f032022-11-24 16:49:56 +01003290fih_ret
Raef Colesf11de642021-10-15 11:11:56 +01003291boot_go_for_image_id(struct boot_rsp *rsp, uint32_t image_id)
3292{
Michael Grand5047f032022-11-24 16:49:56 +01003293 FIH_DECLARE(fih_rc, FIH_FAILURE);
Raef Colesf11de642021-10-15 11:11:56 +01003294
3295 if (image_id >= BOOT_IMAGE_NUMBER) {
3296 FIH_RET(FIH_FAILURE);
3297 }
3298
3299#if BOOT_IMAGE_NUMBER > 1
3300 memset(&boot_data.img_mask, 1, BOOT_IMAGE_NUMBER);
3301 boot_data.img_mask[image_id] = 0;
3302#endif
3303
3304 FIH_CALL(context_boot_go, fih_rc, &boot_data, rsp);
3305 FIH_RET(fih_rc);
3306}
3307
3308/**
3309 * Clears the boot state, so that previous operations have no effect on new
3310 * ones.
3311 *
3312 * @param state The state that should be cleared. If the value
3313 * is NULL, the default bootloader state will be
3314 * cleared.
3315 */
3316void boot_state_clear(struct boot_loader_state *state)
3317{
3318 if (state != NULL) {
3319 memset(state, 0, sizeof(struct boot_loader_state));
3320 } else {
3321 memset(&boot_data, 0, sizeof(struct boot_loader_state));
3322 }
3323}