blob: 491c83c1fc13e046f8af5e04ae9c911e968b1188 [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
Jamie McCraee261b282024-07-26 11:48:19 +010066#if defined(MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO) || defined(MCUBOOT_DATA_SHARING)
Jamie McCrae4f1ab9e2024-07-26 12:29:24 +010067static struct image_max_size image_max_sizes[BOOT_IMAGE_NUMBER] = {0};
68#endif
69
Jamie McCraee261b282024-07-26 11:48:19 +010070#if !defined(__BOOTSIM__)
71/* Used for holding static buffers in multiple functions to work around issues
72 * in older versions of gcc (e.g. 4.8.4)
73 */
74struct sector_buffer_t {
75 boot_sector_t *primary;
76 boot_sector_t *secondary;
77#if MCUBOOT_SWAP_USING_SCRATCH
78 boot_sector_t *scratch;
79#endif
80};
81#endif
82
Fabio Utzigabec0732019-07-31 08:40:22 -030083#if (BOOT_IMAGE_NUMBER > 1)
84#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
85#else
86#define IMAGES_ITER(x)
87#endif
88
Fabio Utzig10ee6482019-08-01 12:04:52 -030089/*
90 * This macro allows some control on the allocation of local variables.
91 * When running natively on a target, we don't want to allocated huge
92 * variables on the stack, so make them global instead. For the simulator
93 * we want to run as many threads as there are tests, and it's safer
94 * to just make those variables stack allocated.
95 */
96#if !defined(__BOOTSIM__)
97#define TARGET_STATIC static
98#else
99#define TARGET_STATIC
100#endif
101
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000102#if BOOT_MAX_ALIGN > 1024
103#define BUF_SZ BOOT_MAX_ALIGN
104#else
105#define BUF_SZ 1024
106#endif
107
Dominik Ermelaafcbad2024-02-29 20:40:20 +0000108#define NO_ACTIVE_SLOT UINT32_MAX
109
David Vinczee574f2d2020-07-10 11:42:03 +0200110static int
111boot_read_image_headers(struct boot_loader_state *state, bool require_all,
112 struct boot_status *bs)
113{
114 int rc;
115 int i;
116
117 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +0200118 rc = BOOT_HOOK_CALL(boot_read_image_header_hook, BOOT_HOOK_REGULAR,
119 BOOT_CURR_IMG(state), i, boot_img_hdr(state, i));
120 if (rc == BOOT_HOOK_REGULAR)
121 {
122 rc = boot_read_image_header(state, i, boot_img_hdr(state, i), bs);
123 }
David Vinczee574f2d2020-07-10 11:42:03 +0200124 if (rc != 0) {
125 /* If `require_all` is set, fail on any single fail, otherwise
126 * if at least the first slot's header was read successfully,
127 * then the boot loader can attempt a boot.
128 *
129 * Failure to read any headers is a fatal error.
130 */
131 if (i > 0 && !require_all) {
132 return 0;
133 } else {
134 return rc;
135 }
136 }
137 }
138
139 return 0;
140}
141
Mark Horvathccaf7f82021-01-04 18:16:42 +0100142/**
143 * Saves boot status and shared data for current image.
144 *
145 * @param state Boot loader status information.
146 * @param active_slot Index of the slot will be loaded for current image.
147 *
148 * @return 0 on success; nonzero on failure.
149 */
150static int
151boot_add_shared_data(struct boot_loader_state *state,
Jamie McCrae3016d002023-03-14 12:35:51 +0000152 uint8_t active_slot)
Mark Horvathccaf7f82021-01-04 18:16:42 +0100153{
154#if defined(MCUBOOT_MEASURED_BOOT) || defined(MCUBOOT_DATA_SHARING)
155 int rc;
156
157#ifdef MCUBOOT_MEASURED_BOOT
158 rc = boot_save_boot_status(BOOT_CURR_IMG(state),
159 boot_img_hdr(state, active_slot),
160 BOOT_IMG_AREA(state, active_slot));
161 if (rc != 0) {
162 BOOT_LOG_ERR("Failed to add image data to shared area");
163 return rc;
164 }
165#endif /* MCUBOOT_MEASURED_BOOT */
166
167#ifdef MCUBOOT_DATA_SHARING
168 rc = boot_save_shared_data(boot_img_hdr(state, active_slot),
Jamie McCrae3016d002023-03-14 12:35:51 +0000169 BOOT_IMG_AREA(state, active_slot),
Jamie McCrae4f1ab9e2024-07-26 12:29:24 +0100170 active_slot, image_max_sizes);
Mark Horvathccaf7f82021-01-04 18:16:42 +0100171 if (rc != 0) {
172 BOOT_LOG_ERR("Failed to add data to shared memory area.");
173 return rc;
174 }
175#endif /* MCUBOOT_DATA_SHARING */
176
177 return 0;
178
179#else /* MCUBOOT_MEASURED_BOOT || MCUBOOT_DATA_SHARING */
180 (void) (state);
181 (void) (active_slot);
182
183 return 0;
184#endif
185}
186
187/**
188 * Fills rsp to indicate how booting should occur.
189 *
190 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +0100191 * @param rsp boot_rsp struct to fill.
192 */
193static void
Raef Colesfe57e7d2021-10-15 11:07:09 +0100194fill_rsp(struct boot_loader_state *state, struct boot_rsp *rsp)
Mark Horvathccaf7f82021-01-04 18:16:42 +0100195{
196 uint32_t active_slot;
197
198#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesf11de642021-10-15 11:11:56 +0100199 /* Always boot from the first enabled image. */
Mark Horvathccaf7f82021-01-04 18:16:42 +0100200 BOOT_CURR_IMG(state) = 0;
Raef Colesf11de642021-10-15 11:11:56 +0100201 IMAGES_ITER(BOOT_CURR_IMG(state)) {
202 if (!state->img_mask[BOOT_CURR_IMG(state)]) {
203 break;
204 }
205 }
INFINEON\DovhalA94360d52023-01-18 17:21:56 +0200206 /* At least one image must be active, otherwise skip the execution */
207 if (BOOT_CURR_IMG(state) >= BOOT_IMAGE_NUMBER) {
208 return;
209 }
Mark Horvathccaf7f82021-01-04 18:16:42 +0100210#endif
211
212#if defined(MCUBOOT_DIRECT_XIP) || defined(MCUBOOT_RAM_LOAD)
Raef Colesfe57e7d2021-10-15 11:07:09 +0100213 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +0100214#else
Mark Horvathccaf7f82021-01-04 18:16:42 +0100215 active_slot = BOOT_PRIMARY_SLOT;
216#endif
217
Dominik Ermel260ae092021-04-23 05:38:45 +0000218 rsp->br_flash_dev_id = flash_area_get_device_id(BOOT_IMG_AREA(state, active_slot));
Mark Horvathccaf7f82021-01-04 18:16:42 +0100219 rsp->br_image_off = boot_img_slot_off(state, active_slot);
220 rsp->br_hdr = boot_img_hdr(state, active_slot);
221}
222
223/**
224 * Closes all flash areas.
225 *
226 * @param state Boot loader status information.
227 */
228static void
229close_all_flash_areas(struct boot_loader_state *state)
230{
231 uint32_t slot;
232
233 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +0100234#if BOOT_IMAGE_NUMBER > 1
235 if (state->img_mask[BOOT_CURR_IMG(state)]) {
236 continue;
237 }
238#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +0100239#if MCUBOOT_SWAP_USING_SCRATCH
240 flash_area_close(BOOT_SCRATCH_AREA(state));
241#endif
242 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
243 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
244 }
245 }
246}
247
Dominik Ermelaafcbad2024-02-29 20:40:20 +0000248#if (BOOT_IMAGE_NUMBER > 1) || \
249 defined(MCUBOOT_DIRECT_XIP) || \
250 defined(MCUBOOT_RAM_LOAD) || \
251 defined(MCUBOOT_DOWNGRADE_PREVENTION)
252/**
253 * Compare image version numbers
254 *
255 * By default, the comparison does not take build number into account.
256 * Enable MCUBOOT_VERSION_CMP_USE_BUILD_NUMBER to take the build number into account.
257 *
258 * @param ver1 Pointer to the first image version to compare.
259 * @param ver2 Pointer to the second image version to compare.
260 *
261 * @retval -1 If ver1 is less than ver2.
262 * @retval 0 If the image version numbers are equal.
263 * @retval 1 If ver1 is greater than ver2.
264 */
265static int
266boot_version_cmp(const struct image_version *ver1,
267 const struct image_version *ver2)
268{
269 if (ver1->iv_major > ver2->iv_major) {
270 return 1;
271 }
272 if (ver1->iv_major < ver2->iv_major) {
273 return -1;
274 }
275 /* The major version numbers are equal, continue comparison. */
276 if (ver1->iv_minor > ver2->iv_minor) {
277 return 1;
278 }
279 if (ver1->iv_minor < ver2->iv_minor) {
280 return -1;
281 }
282 /* The minor version numbers are equal, continue comparison. */
283 if (ver1->iv_revision > ver2->iv_revision) {
284 return 1;
285 }
286 if (ver1->iv_revision < ver2->iv_revision) {
287 return -1;
288 }
289
290#if defined(MCUBOOT_VERSION_CMP_USE_BUILD_NUMBER)
291 /* The revisions are equal, continue comparison. */
292 if (ver1->iv_build_num > ver2->iv_build_num) {
293 return 1;
294 }
295 if (ver1->iv_build_num < ver2->iv_build_num) {
296 return -1;
297 }
298#endif
299
300 return 0;
301}
302#endif
303
304
305#if (BOOT_IMAGE_NUMBER > 1)
306
307static int
308boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot);
309
310/**
311 * Check the image dependency whether it is satisfied and modify
312 * the swap type if necessary.
313 *
314 * @param dep Image dependency which has to be verified.
315 *
316 * @return 0 on success; nonzero on failure.
317 */
318static int
319boot_verify_slot_dependency(struct boot_loader_state *state,
320 struct image_dependency *dep)
321{
322 struct image_version *dep_version;
323 size_t dep_slot;
324 int rc;
325
326 /* Determine the source of the image which is the subject of
327 * the dependency and get it's version. */
328#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
329 uint8_t swap_type = state->swap_type[dep->image_id];
330 dep_slot = BOOT_IS_UPGRADE(swap_type) ? BOOT_SECONDARY_SLOT
331 : BOOT_PRIMARY_SLOT;
332#else
333 dep_slot = state->slot_usage[dep->image_id].active_slot;
334#endif
335
336 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
337
338 rc = boot_version_cmp(dep_version, &dep->image_min_version);
339#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
340 if (rc < 0) {
341 /* Dependency not satisfied.
342 * Modify the swap type to decrease the version number of the image
343 * (which will be located in the primary slot after the boot process),
344 * consequently the number of unsatisfied dependencies will be
345 * decreased or remain the same.
346 */
347 switch (BOOT_SWAP_TYPE(state)) {
348 case BOOT_SWAP_TYPE_TEST:
349 case BOOT_SWAP_TYPE_PERM:
350 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
351 break;
352 case BOOT_SWAP_TYPE_NONE:
353 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
354 break;
355 default:
356 break;
357 }
358 } else {
359 /* Dependency satisfied. */
360 rc = 0;
361 }
362#else
363 if (rc >= 0) {
364 /* Dependency satisfied. */
365 rc = 0;
366 }
367#endif
368
369 return rc;
370}
371
372#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
373/**
374 * Iterate over all the images and verify whether the image dependencies in the
375 * TLV area are all satisfied and update the related swap type if necessary.
376 */
377static int
378boot_verify_dependencies(struct boot_loader_state *state)
379{
380 int rc = -1;
381 uint8_t slot;
382
383 BOOT_CURR_IMG(state) = 0;
384 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
385 if (state->img_mask[BOOT_CURR_IMG(state)]) {
386 BOOT_CURR_IMG(state)++;
387 continue;
388 }
389 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
390 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
391 slot = BOOT_SECONDARY_SLOT;
392 } else {
393 slot = BOOT_PRIMARY_SLOT;
394 }
395
396 rc = boot_verify_slot_dependencies(state, slot);
397 if (rc == 0) {
398 /* All dependencies've been satisfied, continue with next image. */
399 BOOT_CURR_IMG(state)++;
400 } else {
401 /* Cannot upgrade due to non-met dependencies, so disable all
402 * image upgrades.
403 */
404 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
405 BOOT_CURR_IMG(state) = idx;
406 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
407 }
408 break;
409 }
410 }
411 return rc;
412}
413#else
414
415#if defined MCUBOOT_RAM_LOAD
416static inline int
417boot_remove_image_from_sram(struct boot_loader_state *state);
418#endif
419
420/**
421 * Checks the dependency of all the active slots. If an image found with
422 * invalid or not satisfied dependencies the image is removed from SRAM (in
423 * case of MCUBOOT_RAM_LOAD strategy) and its slot is set to unavailable.
424 *
425 * @param state Boot loader status information.
426 *
427 * @return 0 if dependencies are met; nonzero otherwise.
428 */
429static int
430boot_verify_dependencies(struct boot_loader_state *state)
431{
432 int rc = -1;
433 uint32_t active_slot;
434
435 IMAGES_ITER(BOOT_CURR_IMG(state)) {
436 if (state->img_mask[BOOT_CURR_IMG(state)]) {
437 continue;
438 }
439 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
440 rc = boot_verify_slot_dependencies(state, active_slot);
441 if (rc != 0) {
442 /* Dependencies not met or invalid dependencies. */
443
444#ifdef MCUBOOT_RAM_LOAD
445 boot_remove_image_from_sram(state);
446#endif /* MCUBOOT_RAM_LOAD */
447
448 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
449 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
450
451 return rc;
452 }
453 }
454
455 return rc;
456}
457#endif
458
459/**
460 * Read all dependency TLVs of an image from the flash and verify
461 * one after another to see if they are all satisfied.
462 *
463 * @param slot Image slot number.
464 *
465 * @return 0 on success; nonzero on failure.
466 */
467static int
468boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
469{
470 const struct flash_area *fap;
471 struct image_tlv_iter it;
472 struct image_dependency dep;
473 uint32_t off;
474 uint16_t len;
475 int area_id;
476 int rc;
477
478 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
479 rc = flash_area_open(area_id, &fap);
480 if (rc != 0) {
481 rc = BOOT_EFLASH;
482 goto done;
483 }
484
485 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
486 IMAGE_TLV_DEPENDENCY, true);
487 if (rc != 0) {
488 goto done;
489 }
490
491 while (true) {
492 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
493 if (rc < 0) {
494 return -1;
495 } else if (rc > 0) {
496 rc = 0;
497 break;
498 }
499
500 if (len != sizeof(dep)) {
501 rc = BOOT_EBADIMAGE;
502 goto done;
503 }
504
505 rc = LOAD_IMAGE_DATA(boot_img_hdr(state, slot),
506 fap, off, &dep, len);
507 if (rc != 0) {
508 rc = BOOT_EFLASH;
509 goto done;
510 }
511
512 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
513 rc = BOOT_EBADARGS;
514 goto done;
515 }
516
517 /* Verify dependency and modify the swap type if not satisfied. */
518 rc = boot_verify_slot_dependency(state, &dep);
519 if (rc != 0) {
520 /* Dependency not satisfied */
521 goto done;
522 }
523 }
524
525done:
526 flash_area_close(fap);
527 return rc;
528}
529
530#endif /* (BOOT_IMAGE_NUMBER > 1) */
531
Tamas Banfe031092020-09-10 17:32:39 +0200532#if !defined(MCUBOOT_DIRECT_XIP)
David Brownf5b33d82017-09-01 10:58:27 -0600533/*
534 * Compute the total size of the given image. Includes the size of
535 * the TLVs.
536 */
Tamas Banfe031092020-09-10 17:32:39 +0200537#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600538static int
Fabio Utzigd638b172019-08-09 10:38:05 -0300539boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
David Brownf5b33d82017-09-01 10:58:27 -0600540{
541 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300542 struct image_tlv_info info;
Fabio Utzig233af7d2019-08-26 12:06:16 -0300543 uint32_t off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300544 uint32_t protect_tlv_size;
David Brownf5b33d82017-09-01 10:58:27 -0600545 int area_id;
546 int rc;
547
Fabio Utzig10ee6482019-08-01 12:04:52 -0300548#if (BOOT_IMAGE_NUMBER == 1)
549 (void)state;
550#endif
551
552 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
David Brownf5b33d82017-09-01 10:58:27 -0600553 rc = flash_area_open(area_id, &fap);
554 if (rc != 0) {
555 rc = BOOT_EFLASH;
556 goto done;
557 }
558
Fabio Utzig61fd8882019-09-14 20:00:20 -0300559 off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
560
561 if (flash_area_read(fap, off, &info, sizeof(info))) {
562 rc = BOOT_EFLASH;
David Brownf5b33d82017-09-01 10:58:27 -0600563 goto done;
564 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300565
Fabio Utzige52c08e2019-09-11 19:32:00 -0300566 protect_tlv_size = boot_img_hdr(state, slot)->ih_protect_tlv_size;
567 if (info.it_magic == IMAGE_TLV_PROT_INFO_MAGIC) {
568 if (protect_tlv_size != info.it_tlv_tot) {
569 rc = BOOT_EBADIMAGE;
570 goto done;
571 }
572
573 if (flash_area_read(fap, off + info.it_tlv_tot, &info, sizeof(info))) {
574 rc = BOOT_EFLASH;
575 goto done;
576 }
577 } else if (protect_tlv_size != 0) {
578 rc = BOOT_EBADIMAGE;
579 goto done;
580 }
581
Fabio Utzig61fd8882019-09-14 20:00:20 -0300582 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
583 rc = BOOT_EBADIMAGE;
584 goto done;
585 }
586
Fabio Utzige52c08e2019-09-11 19:32:00 -0300587 *size = off + protect_tlv_size + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600588 rc = 0;
589
590done:
591 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300592 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600593}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300594#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600595
Tamas Banfe031092020-09-10 17:32:39 +0200596#if !defined(MCUBOOT_RAM_LOAD)
David Brownab449182019-11-15 09:32:52 -0700597static uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300598boot_write_sz(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800599{
David Brownab449182019-11-15 09:32:52 -0700600 uint32_t elem_sz;
Fabio Utzig12d59162019-11-28 10:01:59 -0300601#if MCUBOOT_SWAP_USING_SCRATCH
David Brownab449182019-11-15 09:32:52 -0700602 uint32_t align;
Fabio Utzig12d59162019-11-28 10:01:59 -0300603#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800604
605 /* Figure out what size to write update status update as. The size depends
606 * on what the minimum write size is for scratch area, active image slot.
607 * We need to use the bigger of those 2 values.
608 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300609 elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
Fabio Utzig12d59162019-11-28 10:01:59 -0300610#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300611 align = flash_area_align(BOOT_SCRATCH_AREA(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800612 if (align > elem_sz) {
613 elem_sz = align;
614 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300615#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800616
617 return elem_sz;
618}
619
Fabio Utzig10ee6482019-08-01 12:04:52 -0300620static int
621boot_initialize_area(struct boot_loader_state *state, int flash_area)
622{
Dominik Ermel51c8d762021-05-24 15:34:01 +0000623 uint32_t num_sectors = BOOT_MAX_IMG_SECTORS;
624 boot_sector_t *out_sectors;
625 uint32_t *out_num_sectors;
Fabio Utzig10ee6482019-08-01 12:04:52 -0300626 int rc;
627
628 num_sectors = BOOT_MAX_IMG_SECTORS;
629
630 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
631 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
632 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
633 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
634 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
635 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300636#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300637 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
638 out_sectors = state->scratch.sectors;
639 out_num_sectors = &state->scratch.num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300640#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -0300641 } else {
642 return BOOT_EFLASH;
643 }
644
Dominik Ermel51c8d762021-05-24 15:34:01 +0000645#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
Fabio Utzig10ee6482019-08-01 12:04:52 -0300646 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
Dominik Ermel51c8d762021-05-24 15:34:01 +0000647#else
648 _Static_assert(sizeof(int) <= sizeof(uint32_t), "Fix needed");
649 rc = flash_area_to_sectors(flash_area, (int *)&num_sectors, out_sectors);
650#endif /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300651 if (rc != 0) {
652 return rc;
653 }
654 *out_num_sectors = num_sectors;
655 return 0;
656}
Fabio Utzig10ee6482019-08-01 12:04:52 -0300657
Christopher Collins92ea77f2016-12-12 15:59:26 -0800658/**
659 * Determines the sector layout of both image slots and the scratch area.
660 * This information is necessary for calculating the number of bytes to erase
661 * and copy during an image swap. The information collected during this
Fabio Utzig10ee6482019-08-01 12:04:52 -0300662 * function is used to populate the state.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800663 */
664static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300665boot_read_sectors(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800666{
Fabio Utzigb0f04732019-07-31 09:49:19 -0300667 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800668 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800669
Fabio Utzig10ee6482019-08-01 12:04:52 -0300670 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300671
672 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800673 if (rc != 0) {
674 return BOOT_EFLASH;
675 }
676
Fabio Utzig10ee6482019-08-01 12:04:52 -0300677 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800678 if (rc != 0) {
Andrzej Puzdrowski54b4ad92021-06-22 13:21:22 +0200679 /* We need to differentiate from the primary image issue */
680 return BOOT_EFLASH_SEC;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800681 }
682
Fabio Utzig12d59162019-11-28 10:01:59 -0300683#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300684 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200685 if (rc != 0) {
686 return BOOT_EFLASH;
687 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300688#endif
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200689
Fabio Utzig10ee6482019-08-01 12:04:52 -0300690 BOOT_WRITE_SZ(state) = boot_write_sz(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800691
692 return 0;
693}
694
Fabio Utzig12d59162019-11-28 10:01:59 -0300695void
696boot_status_reset(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800697{
Fabio Utzig4741c452019-12-19 15:32:41 -0300698#ifdef MCUBOOT_ENC_IMAGES
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000699 memset(&bs->enckey, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_KEY_ALIGN_SIZE);
Fabio Utzig4741c452019-12-19 15:32:41 -0300700#if MCUBOOT_SWAP_SAVE_ENCTLV
701 memset(&bs->enctlv, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_TLV_ALIGN_SIZE);
702#endif
703#endif /* MCUBOOT_ENC_IMAGES */
704
705 bs->use_scratch = 0;
706 bs->swap_size = 0;
707 bs->source = 0;
708
Fabio Utzig74aef312019-11-28 11:05:34 -0300709 bs->op = BOOT_STATUS_OP_MOVE;
Fabio Utzig39000012018-07-30 12:40:20 -0300710 bs->idx = BOOT_STATUS_IDX_0;
711 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700712 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Fabio Utzig12d59162019-11-28 10:01:59 -0300713}
Christopher Collins92ea77f2016-12-12 15:59:26 -0800714
Fabio Utzig12d59162019-11-28 10:01:59 -0300715bool
716boot_status_is_reset(const struct boot_status *bs)
717{
Fabio Utzig74aef312019-11-28 11:05:34 -0300718 return (bs->op == BOOT_STATUS_OP_MOVE &&
719 bs->idx == BOOT_STATUS_IDX_0 &&
720 bs->state == BOOT_STATUS_STATE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800721}
722
723/**
724 * Writes the supplied boot status to the flash file system. The boot status
725 * contains the current state of an in-progress image copy operation.
726 *
727 * @param bs The boot status to write.
728 *
729 * @return 0 on success; nonzero on failure.
730 */
731int
Fabio Utzig12d59162019-11-28 10:01:59 -0300732boot_write_status(const struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800733{
734 const struct flash_area *fap;
735 uint32_t off;
736 int area_id;
Dominik Ermel9479af02021-10-19 10:06:44 +0000737 int rc = 0;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300738 uint8_t buf[BOOT_MAX_ALIGN];
Gustavo Henrique Nihei4aa286d2021-11-24 14:54:56 -0300739 uint32_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300740 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800741
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300742 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100743 * the trailer. Since in the last step the primary slot is erased, the
744 * first two status writes go to the scratch which will be copied to
745 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300746 */
747
Fabio Utzig12d59162019-11-28 10:01:59 -0300748#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig2473ac02017-05-02 12:45:02 -0300749 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800750 /* Write to scratch. */
751 area_id = FLASH_AREA_IMAGE_SCRATCH;
752 } else {
Fabio Utzig12d59162019-11-28 10:01:59 -0300753#endif
David Vincze2d736ad2019-02-18 11:50:22 +0100754 /* Write to the primary slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300755 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Fabio Utzig12d59162019-11-28 10:01:59 -0300756#if MCUBOOT_SWAP_USING_SCRATCH
Christopher Collins92ea77f2016-12-12 15:59:26 -0800757 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300758#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800759
760 rc = flash_area_open(area_id, &fap);
761 if (rc != 0) {
Dominik Ermel9479af02021-10-19 10:06:44 +0000762 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800763 }
764
765 off = boot_status_off(fap) +
Fabio Utzig12d59162019-11-28 10:01:59 -0300766 boot_status_internal_off(bs, BOOT_WRITE_SZ(state));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200767 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300768 erased_val = flash_area_erased_val(fap);
769 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700770 buf[0] = bs->state;
771
Jamie McCrae35e99312024-01-03 07:37:55 +0000772 BOOT_LOG_DBG("writing swap status; fa_id=%d off=0x%lx (0x%lx)",
773 flash_area_get_id(fap), (unsigned long)off,
774 (unsigned long)flash_area_get_off(fap) + off);
775
David Brown9d725462017-01-23 15:50:58 -0700776 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800777 if (rc != 0) {
778 rc = BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800779 }
780
Christopher Collins92ea77f2016-12-12 15:59:26 -0800781 flash_area_close(fap);
Dominik Ermel9479af02021-10-19 10:06:44 +0000782
Christopher Collins92ea77f2016-12-12 15:59:26 -0800783 return rc;
784}
Tamas Banfe031092020-09-10 17:32:39 +0200785#endif /* !MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +0200786#endif /* !MCUBOOT_DIRECT_XIP */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800787
788/*
David Vinczec3084132020-02-18 14:50:47 +0100789 * Validate image hash/signature and optionally the security counter in a slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800790 */
Michael Grand5047f032022-11-24 16:49:56 +0100791static fih_ret
Fabio Utzig10ee6482019-08-01 12:04:52 -0300792boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
793 const struct flash_area *fap, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800794{
Fabio Utzig10ee6482019-08-01 12:04:52 -0300795 TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigba829042018-09-18 08:29:34 -0300796 int rc;
Michael Grand5047f032022-11-24 16:49:56 +0100797 FIH_DECLARE(fih_rc, FIH_FAILURE);
Fabio Utzigba829042018-09-18 08:29:34 -0300798
Fabio Utzig10ee6482019-08-01 12:04:52 -0300799#if (BOOT_IMAGE_NUMBER == 1)
800 (void)state;
801#endif
802
Fabio Utzigba829042018-09-18 08:29:34 -0300803 (void)bs;
804 (void)rc;
Fabio Utzigbc077932019-08-26 11:16:34 -0300805
Hugo L'Hostisdb543e52021-03-09 18:00:31 +0000806/* In the case of ram loading the image has already been decrypted as it is
807 * decrypted when copied in ram */
808#if defined(MCUBOOT_ENC_IMAGES) && !defined(MCUBOOT_RAM_LOAD)
Dominik Ermel7f9ac972024-07-12 19:21:40 +0000809 if (MUST_DECRYPT(fap, BOOT_CURR_IMG(state), hdr)) {
810 rc = boot_enc_load(BOOT_CURR_ENC(state), 1, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300811 if (rc < 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100812 FIH_RET(fih_rc);
Fabio Utzigba829042018-09-18 08:29:34 -0300813 }
Fabio Utzig4741c452019-12-19 15:32:41 -0300814 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100815 FIH_RET(fih_rc);
Fabio Utzigba829042018-09-18 08:29:34 -0300816 }
817 }
Fabio Utzigbc077932019-08-26 11:16:34 -0300818#endif
819
Dominik Ermel7f9ac972024-07-12 19:21:40 +0000820 FIH_CALL(bootutil_img_validate, fih_rc, BOOT_CURR_ENC(state),
821 BOOT_CURR_IMG(state), hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
822 NULL, 0, NULL);
Fabio Utzig10ee6482019-08-01 12:04:52 -0300823
Raef Colese8fe6cf2020-05-26 13:07:40 +0100824 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800825}
826
Tamas Banfe031092020-09-10 17:32:39 +0200827#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
Michael Grand5047f032022-11-24 16:49:56 +0100828static fih_ret
Christopher Collins92ea77f2016-12-12 15:59:26 -0800829split_image_check(struct image_header *app_hdr,
830 const struct flash_area *app_fap,
831 struct image_header *loader_hdr,
832 const struct flash_area *loader_fap)
833{
834 static void *tmpbuf;
835 uint8_t loader_hash[32];
Michael Grand5047f032022-11-24 16:49:56 +0100836 FIH_DECLARE(fih_rc, FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800837
838 if (!tmpbuf) {
839 tmpbuf = malloc(BOOT_TMPBUF_SZ);
840 if (!tmpbuf) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100841 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800842 }
843 }
844
Raef Colese8fe6cf2020-05-26 13:07:40 +0100845 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, loader_hdr, loader_fap,
846 tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, loader_hash);
Michael Grand5047f032022-11-24 16:49:56 +0100847 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100848 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800849 }
850
Raef Colese8fe6cf2020-05-26 13:07:40 +0100851 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, app_hdr, app_fap,
852 tmpbuf, BOOT_TMPBUF_SZ, loader_hash, 32, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800853
Raef Colese8fe6cf2020-05-26 13:07:40 +0100854out:
855 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800856}
Tamas Banfe031092020-09-10 17:32:39 +0200857#endif /* !MCUBOOT_DIRECT_XIP && !MCUBOOT_RAM_LOAD */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800858
Fabio Utzig338a19f2018-12-03 08:37:08 -0200859/*
David Brown9bf95af2019-10-10 15:36:36 -0600860 * Check that this is a valid header. Valid means that the magic is
861 * correct, and that the sizes/offsets are "sane". Sane means that
862 * there is no overflow on the arithmetic, and that the result fits
863 * within the flash area we are in.
864 */
865static bool
Jamie McCraedbb5c782024-08-22 10:43:14 +0100866boot_is_header_valid(const struct image_header *hdr, const struct flash_area *fap,
867 struct boot_loader_state *state)
David Brown9bf95af2019-10-10 15:36:36 -0600868{
869 uint32_t size;
870
Jamie McCraedbb5c782024-08-22 10:43:14 +0100871 (void)state;
872
David Brown9bf95af2019-10-10 15:36:36 -0600873 if (hdr->ih_magic != IMAGE_MAGIC) {
874 return false;
875 }
876
877 if (!boot_u32_safe_add(&size, hdr->ih_img_size, hdr->ih_hdr_size)) {
878 return false;
879 }
880
Dominik Ermel260ae092021-04-23 05:38:45 +0000881 if (size >= flash_area_get_size(fap)) {
David Brown9bf95af2019-10-10 15:36:36 -0600882 return false;
883 }
884
885 return true;
886}
887
888/*
Fabio Utzig338a19f2018-12-03 08:37:08 -0200889 * Check that a memory area consists of a given value.
890 */
891static inline bool
892boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300893{
894 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200895 uint8_t *p = (uint8_t *)data;
896 for (i = 0; i < len; i++) {
897 if (val != p[i]) {
898 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300899 }
900 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200901 return true;
902}
903
904static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300905boot_check_header_erased(struct boot_loader_state *state, int slot)
Fabio Utzig338a19f2018-12-03 08:37:08 -0200906{
907 const struct flash_area *fap;
908 struct image_header *hdr;
909 uint8_t erased_val;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300910 int area_id;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200911 int rc;
912
Fabio Utzig10ee6482019-08-01 12:04:52 -0300913 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300914 rc = flash_area_open(area_id, &fap);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200915 if (rc != 0) {
916 return -1;
917 }
918
919 erased_val = flash_area_erased_val(fap);
920 flash_area_close(fap);
921
Fabio Utzig10ee6482019-08-01 12:04:52 -0300922 hdr = boot_img_hdr(state, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200923 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
924 return -1;
925 }
926
927 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300928}
929
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000930#if defined(MCUBOOT_DIRECT_XIP)
931/**
932 * Check if image in slot has been set with specific ROM address to run from
933 * and whether the slot starts at that address.
934 *
935 * @returns 0 if IMAGE_F_ROM_FIXED flag is not set;
936 * 0 if IMAGE_F_ROM_FIXED flag is set and ROM address specified in
937 * header matches the slot address;
938 * 1 if IMF_F_ROM_FIXED flag is set but ROM address specified in header
939 * does not match the slot address.
940 */
941static bool
Raef Colesfe57e7d2021-10-15 11:07:09 +0100942boot_rom_address_check(struct boot_loader_state *state)
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000943{
Mark Horvathccaf7f82021-01-04 18:16:42 +0100944 uint32_t active_slot;
945 const struct image_header *hdr;
946 uint32_t f_off;
947
Raef Colesfe57e7d2021-10-15 11:07:09 +0100948 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +0100949 hdr = boot_img_hdr(state, active_slot);
950 f_off = boot_img_slot_off(state, active_slot);
951
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000952 if (hdr->ih_flags & IMAGE_F_ROM_FIXED && hdr->ih_load_addr != f_off) {
953 BOOT_LOG_WRN("Image in %s slot at 0x%x has been built for offset 0x%x"\
Mark Horvathccaf7f82021-01-04 18:16:42 +0100954 ", skipping",
955 active_slot == 0 ? "primary" : "secondary", f_off,
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000956 hdr->ih_load_addr);
957
958 /* If there is address mismatch, the image is not bootable from this
959 * slot.
960 */
961 return 1;
962 }
963 return 0;
964}
965#endif
966
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300967/*
968 * Check that there is a valid image in a slot
969 *
970 * @returns
Raef Colese8fe6cf2020-05-26 13:07:40 +0100971 * FIH_SUCCESS if image was successfully validated
Michael Grand5047f032022-11-24 16:49:56 +0100972 * FIH_NO_BOOTABLE_IMAGE if no bootloable image was found
Raef Colese8fe6cf2020-05-26 13:07:40 +0100973 * FIH_FAILURE on any errors
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300974 */
Michael Grand5047f032022-11-24 16:49:56 +0100975static fih_ret
Fabio Utzig10ee6482019-08-01 12:04:52 -0300976boot_validate_slot(struct boot_loader_state *state, int slot,
977 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800978{
979 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400980 struct image_header *hdr;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300981 int area_id;
Michael Grand5047f032022-11-24 16:49:56 +0100982 FIH_DECLARE(fih_rc, FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800983 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300984
Fabio Utzig10ee6482019-08-01 12:04:52 -0300985 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300986 rc = flash_area_open(area_id, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800987 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100988 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800989 }
990
Fabio Utzig10ee6482019-08-01 12:04:52 -0300991 hdr = boot_img_hdr(state, slot);
992 if (boot_check_header_erased(state, slot) == 0 ||
993 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
Fabio Utzig260ec452020-07-09 18:40:07 -0300994
995#if defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE)
996 /*
997 * This fixes an issue where an image might be erased, but a trailer
998 * be left behind. It can happen if the image is in the secondary slot
999 * and did not pass validation, in which case the whole slot is erased.
1000 * If during the erase operation, a reset occurs, parts of the slot
1001 * might have been erased while some did not. The concerning part is
1002 * the trailer because it might disable a new image from being loaded
1003 * through mcumgr; so we just get rid of the trailer here, if the header
1004 * is erased.
1005 */
1006 if (slot != BOOT_PRIMARY_SLOT) {
1007 swap_erase_trailer_sectors(state, fap);
1008 }
1009#endif
1010
David Vincze2d736ad2019-02-18 11:50:22 +01001011 /* No bootable image in slot; continue booting from the primary slot. */
Michael Grand5047f032022-11-24 16:49:56 +01001012 fih_rc = FIH_NO_BOOTABLE_IMAGE;
Fabio Utzig338a19f2018-12-03 08:37:08 -02001013 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -03001014 }
1015
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +00001016#if defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION)
1017 if (slot != BOOT_PRIMARY_SLOT) {
1018 /* Check if version of secondary slot is sufficient */
David Vincze8b0b6372020-05-20 19:54:44 +02001019 rc = boot_version_cmp(
1020 &boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
1021 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
1022 if (rc < 0 && boot_check_header_erased(state, BOOT_PRIMARY_SLOT)) {
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +00001023 BOOT_LOG_ERR("insufficient version in secondary slot");
Dominik Ermel260ae092021-04-23 05:38:45 +00001024 flash_area_erase(fap, 0, flash_area_get_size(fap));
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +00001025 /* Image in the secondary slot does not satisfy version requirement.
1026 * Erase the image and continue booting from the primary slot.
1027 */
Michael Grand5047f032022-11-24 16:49:56 +01001028 fih_rc = FIH_NO_BOOTABLE_IMAGE;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +00001029 goto out;
1030 }
1031 }
1032#endif
Michael Grand5047f032022-11-24 16:49:56 +01001033 BOOT_HOOK_CALL_FIH(boot_image_check_hook, FIH_BOOT_HOOK_REGULAR,
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +02001034 fih_rc, BOOT_CURR_IMG(state), slot);
Michael Grand5047f032022-11-24 16:49:56 +01001035 if (FIH_EQ(fih_rc, FIH_BOOT_HOOK_REGULAR))
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +02001036 {
1037 FIH_CALL(boot_image_check, fih_rc, state, hdr, fap, bs);
1038 }
Jamie McCraedbb5c782024-08-22 10:43:14 +01001039 if (!boot_is_header_valid(hdr, fap, state) || FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Tamas Banfe031092020-09-10 17:32:39 +02001040 if ((slot != BOOT_PRIMARY_SLOT) || ARE_SLOTS_EQUIVALENT()) {
Dominik Ermel260ae092021-04-23 05:38:45 +00001041 flash_area_erase(fap, 0, flash_area_get_size(fap));
David Vinczee574f2d2020-07-10 11:42:03 +02001042 /* Image is invalid, erase it to prevent further unnecessary
1043 * attempts to validate and boot it.
David Brownb38e0442017-02-24 13:57:12 -07001044 */
1045 }
David Brown098de832019-12-10 11:58:01 -07001046#if !defined(__BOOTSIM__)
David Vincze2d736ad2019-02-18 11:50:22 +01001047 BOOT_LOG_ERR("Image in the %s slot is not valid!",
1048 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Brown098de832019-12-10 11:58:01 -07001049#endif
Michael Grand5047f032022-11-24 16:49:56 +01001050 fih_rc = FIH_NO_BOOTABLE_IMAGE;
Fabio Utzig338a19f2018-12-03 08:37:08 -02001051 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001052 }
1053
Håkon Øye Amundsene829e9d2021-11-12 14:01:01 +00001054#if MCUBOOT_IMAGE_NUMBER > 1 && !defined(MCUBOOT_ENC_IMAGES) && defined(MCUBOOT_VERIFY_IMG_ADDRESS)
1055 /* Verify that the image in the secondary slot has a reset address
1056 * located in the primary slot. This is done to avoid users incorrectly
1057 * overwriting an application written to the incorrect slot.
1058 * This feature is only supported by ARM platforms.
1059 */
1060 if (area_id == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
1061 const struct flash_area *pri_fa = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
1062 struct image_header *secondary_hdr = boot_img_hdr(state, slot);
1063 uint32_t reset_value = 0;
1064 uint32_t reset_addr = secondary_hdr->ih_hdr_size + sizeof(reset_value);
1065
1066 rc = flash_area_read(fap, reset_addr, &reset_value, sizeof(reset_value));
1067 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01001068 fih_rc = FIH_NO_BOOTABLE_IMAGE;
Håkon Øye Amundsene829e9d2021-11-12 14:01:01 +00001069 goto out;
1070 }
1071
1072 if (reset_value < pri_fa->fa_off || reset_value> (pri_fa->fa_off + pri_fa->fa_size)) {
1073 BOOT_LOG_ERR("Reset address of image in secondary slot is not in the primary slot");
1074 BOOT_LOG_ERR("Erasing image from secondary slot");
1075
1076 /* The vector table in the image located in the secondary
1077 * slot does not target the primary slot. This might
1078 * indicate that the image was loaded to the wrong slot.
1079 *
1080 * Erase the image and continue booting from the primary slot.
1081 */
1082 flash_area_erase(fap, 0, fap->fa_size);
Michael Grand5047f032022-11-24 16:49:56 +01001083 fih_rc = FIH_NO_BOOTABLE_IMAGE;
Håkon Øye Amundsene829e9d2021-11-12 14:01:01 +00001084 goto out;
1085 }
1086 }
1087#endif
1088
Fabio Utzig338a19f2018-12-03 08:37:08 -02001089out:
1090 flash_area_close(fap);
Raef Colese8fe6cf2020-05-26 13:07:40 +01001091
1092 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001093}
1094
David Vinczec3084132020-02-18 14:50:47 +01001095#ifdef MCUBOOT_HW_ROLLBACK_PROT
1096/**
1097 * Updates the stored security counter value with the image's security counter
1098 * value which resides in the given slot, only if it's greater than the stored
1099 * value.
1100 *
1101 * @param image_index Index of the image to determine which security
1102 * counter to update.
1103 * @param slot Slot number of the image.
1104 * @param hdr Pointer to the image header structure of the image
1105 * that is currently stored in the given slot.
1106 *
1107 * @return 0 on success; nonzero on failure.
1108 */
1109static int
1110boot_update_security_counter(uint8_t image_index, int slot,
1111 struct image_header *hdr)
1112{
1113 const struct flash_area *fap = NULL;
1114 uint32_t img_security_cnt;
1115 int rc;
1116
1117 rc = flash_area_open(flash_area_id_from_multi_image_slot(image_index, slot),
1118 &fap);
1119 if (rc != 0) {
1120 rc = BOOT_EFLASH;
1121 goto done;
1122 }
1123
1124 rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt);
1125 if (rc != 0) {
1126 goto done;
1127 }
1128
1129 rc = boot_nv_security_counter_update(image_index, img_security_cnt);
1130 if (rc != 0) {
1131 goto done;
1132 }
1133
1134done:
1135 flash_area_close(fap);
1136 return rc;
1137}
1138#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1139
Tamas Banfe031092020-09-10 17:32:39 +02001140#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
David Vinczee574f2d2020-07-10 11:42:03 +02001141/**
1142 * Determines which swap operation to perform, if any. If it is determined
1143 * that a swap operation is required, the image in the secondary slot is checked
1144 * for validity. If the image in the secondary slot is invalid, it is erased,
1145 * and a swap type of "none" is indicated.
1146 *
1147 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
1148 */
1149static int
1150boot_validated_swap_type(struct boot_loader_state *state,
1151 struct boot_status *bs)
1152{
1153 int swap_type;
Michael Grand5047f032022-11-24 16:49:56 +01001154 FIH_DECLARE(fih_rc, FIH_FAILURE);
David Vinczee574f2d2020-07-10 11:42:03 +02001155
1156 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
1157 if (BOOT_IS_UPGRADE(swap_type)) {
1158 /* Boot loader wants to switch to the secondary slot.
1159 * Ensure image is valid.
1160 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001161 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_SECONDARY_SLOT, bs);
Michael Grand5047f032022-11-24 16:49:56 +01001162 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
1163 if (FIH_EQ(fih_rc, FIH_NO_BOOTABLE_IMAGE)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001164 swap_type = BOOT_SWAP_TYPE_NONE;
1165 } else {
1166 swap_type = BOOT_SWAP_TYPE_FAIL;
1167 }
David Vinczee574f2d2020-07-10 11:42:03 +02001168 }
1169 }
1170
1171 return swap_type;
1172}
David Brown94ed12c2021-05-26 16:28:14 -06001173#endif
David Vinczee574f2d2020-07-10 11:42:03 +02001174
Christopher Collins92ea77f2016-12-12 15:59:26 -08001175/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08001176 * Erases a region of flash.
1177 *
Fabio Utzigba829042018-09-18 08:29:34 -03001178 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001179 * @param off The offset within the flash area to start the
1180 * erase.
1181 * @param sz The number of bytes to erase.
1182 *
1183 * @return 0 on success; nonzero on failure.
1184 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001185int
Fabio Utzigc28005b2019-09-10 12:18:29 -03001186boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001187{
Fabio Utzigba829042018-09-18 08:29:34 -03001188 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001189}
1190
David Brown94ed12c2021-05-26 16:28:14 -06001191#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
Dominik Ermel256bc372022-12-07 15:51:31 +00001192
1193#if defined(MCUBOOT_ENC_IMAGES) || defined(MCUBOOT_SWAP_SAVE_ENCTLV)
1194/* Replacement for memset(p, 0, sizeof(*p) that does not get
1195 * optimized out.
1196 */
1197static void like_mbedtls_zeroize(void *p, size_t n)
1198{
1199 volatile unsigned char *v = (unsigned char *)p;
1200
1201 for (size_t i = 0; i < n; i++) {
1202 v[i] = 0;
1203 }
1204}
1205#endif
1206
Christopher Collins92ea77f2016-12-12 15:59:26 -08001207/**
1208 * Copies the contents of one flash region to another. You must erase the
1209 * destination region prior to calling this function.
1210 *
1211 * @param flash_area_id_src The ID of the source flash area.
1212 * @param flash_area_id_dst The ID of the destination flash area.
1213 * @param off_src The offset within the source flash area to
1214 * copy from.
1215 * @param off_dst The offset within the destination flash area to
1216 * copy to.
1217 * @param sz The number of bytes to copy.
1218 *
1219 * @return 0 on success; nonzero on failure.
1220 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001221int
Fabio Utzigc28005b2019-09-10 12:18:29 -03001222boot_copy_region(struct boot_loader_state *state,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001223 const struct flash_area *fap_src,
Fabio Utzigba829042018-09-18 08:29:34 -03001224 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -08001225 uint32_t off_src, uint32_t off_dst, uint32_t sz)
1226{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001227 uint32_t bytes_copied;
1228 int chunk_sz;
1229 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -03001230#ifdef MCUBOOT_ENC_IMAGES
Dominik Ermel6fe259b2024-07-18 11:40:53 +00001231 uint32_t off = off_dst;
Fabio Utziga87cc7d2019-08-26 11:21:45 -03001232 uint32_t tlv_off;
Fabio Utzigba829042018-09-18 08:29:34 -03001233 size_t blk_off;
1234 struct image_header *hdr;
1235 uint16_t idx;
1236 uint32_t blk_sz;
Dominik Ermel6fe259b2024-07-18 11:40:53 +00001237 uint8_t image_index = BOOT_CURR_IMG(state);
Thomas Altenbach08d2d942024-04-25 15:29:21 +02001238 bool encrypted_src;
1239 bool encrypted_dst;
Dominik Ermel6fe259b2024-07-18 11:40:53 +00001240 /* Assuming the secondary slot is source; note that 0 here not only
1241 * means that primary slot is source, but also that there will be
1242 * encryption happening, if it is 1 then there is decryption from
1243 * secondary slot.
1244 */
Dominik Ermel3f112862024-07-17 14:43:05 +00001245 int source_slot = 1;
Dominik Ermel6fe259b2024-07-18 11:40:53 +00001246 /* In case of encryption enabled, we may have to do more work than
1247 * just copy bytes */
1248 bool only_copy = false;
1249#else
1250 (void)state;
Fabio Utzigba829042018-09-18 08:29:34 -03001251#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001252
Kristine Jassmann73c38c62021-02-03 16:56:14 +00001253 TARGET_STATIC uint8_t buf[BUF_SZ] __attribute__((aligned(4)));
Fabio Utzig10ee6482019-08-01 12:04:52 -03001254
Dominik Ermel6fe259b2024-07-18 11:40:53 +00001255#ifdef MCUBOOT_ENC_IMAGES
1256 encrypted_src = (flash_area_get_id(fap_src) != FLASH_AREA_IMAGE_PRIMARY(image_index));
1257 encrypted_dst = (flash_area_get_id(fap_dst) != FLASH_AREA_IMAGE_PRIMARY(image_index));
1258
1259 if (encrypted_src != encrypted_dst) {
1260 if (encrypted_dst) {
1261 /* Need encryption, metadata from the primary slot */
1262 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
1263 source_slot = 0;
1264 } else {
1265 /* Need decryption, metadata from the secondary slot */
1266 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
1267 source_slot = 1;
1268 }
1269 } else {
1270 /* In case when source and targe is the same area, this means that we
1271 * only have to copy bytes, no encryption or decryption.
1272 */
1273 only_copy = true;
1274 }
Fabio Utzig10ee6482019-08-01 12:04:52 -03001275#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001276
Christopher Collins92ea77f2016-12-12 15:59:26 -08001277 bytes_copied = 0;
1278 while (bytes_copied < sz) {
1279 if (sz - bytes_copied > sizeof buf) {
1280 chunk_sz = sizeof buf;
1281 } else {
1282 chunk_sz = sz - bytes_copied;
1283 }
1284
1285 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
1286 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001287 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001288 }
1289
Fabio Utzigba829042018-09-18 08:29:34 -03001290#ifdef MCUBOOT_ENC_IMAGES
Dominik Ermel6fe259b2024-07-18 11:40:53 +00001291 /* If only copy, then does not matter if header indicates need for
1292 * encryptio/decryptio, we just copy data. */
1293 if (!only_copy && IS_ENCRYPTED(hdr)) {
1294 uint32_t abs_off = off + bytes_copied;
1295 if (abs_off < hdr->ih_hdr_size) {
1296 /* do not decrypt header */
1297 if (abs_off + chunk_sz > hdr->ih_hdr_size) {
1298 /* The lower part of the chunk contains header data */
1299 blk_off = 0;
1300 blk_sz = chunk_sz - (hdr->ih_hdr_size - abs_off);
1301 idx = hdr->ih_hdr_size - abs_off;
1302 } else {
1303 /* The chunk contains exclusively header data */
1304 blk_sz = 0; /* nothing to decrypt */
1305 }
Thomas Altenbach08d2d942024-04-25 15:29:21 +02001306 } else {
Dominik Ermel6fe259b2024-07-18 11:40:53 +00001307 idx = 0;
1308 blk_sz = chunk_sz;
1309 blk_off = (abs_off - hdr->ih_hdr_size) & 0xf;
Fabio Utzig74aef312019-11-28 11:05:34 -03001310 }
Thomas Altenbach08d2d942024-04-25 15:29:21 +02001311
Dominik Ermel6fe259b2024-07-18 11:40:53 +00001312 if (blk_sz > 0)
1313 {
1314 tlv_off = BOOT_TLV_OFF(hdr);
1315 if (abs_off + chunk_sz > tlv_off) {
1316 /* do not decrypt TLVs */
1317 if (abs_off >= tlv_off) {
1318 blk_sz = 0;
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001319 } else {
Dominik Ermel6fe259b2024-07-18 11:40:53 +00001320 blk_sz = tlv_off - abs_off;
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001321 }
Fabio Utzigba829042018-09-18 08:29:34 -03001322 }
Dominik Ermel6fe259b2024-07-18 11:40:53 +00001323 boot_encrypt(BOOT_CURR_ENC(state), source_slot,
1324 (abs_off + idx) - hdr->ih_hdr_size, blk_sz,
1325 blk_off, &buf[idx]);
Fabio Utzigba829042018-09-18 08:29:34 -03001326 }
1327 }
1328#endif
1329
Christopher Collins92ea77f2016-12-12 15:59:26 -08001330 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
1331 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001332 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001333 }
1334
1335 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -03001336
1337 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -08001338 }
1339
Fabio Utzigba829042018-09-18 08:29:34 -03001340 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001341}
1342
Christopher Collins92ea77f2016-12-12 15:59:26 -08001343/**
David Vincze2d736ad2019-02-18 11:50:22 +01001344 * Overwrite primary slot with the image contained in the secondary slot.
1345 * If a prior copy operation was interrupted by a system reset, this function
1346 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001347 *
1348 * @param bs The current boot status. This function reads
1349 * this struct to determine if it is resuming
1350 * an interrupted swap operation. This
1351 * function writes the updated status to this
1352 * function on return.
1353 *
1354 * @return 0 on success; nonzero on failure.
1355 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001356#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001357static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001358boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
David Brown17609d82017-05-05 09:41:34 -06001359{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001360 size_t sect_count;
1361 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001362 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001363 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001364 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001365 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +01001366 const struct flash_area *fap_primary_slot;
1367 const struct flash_area *fap_secondary_slot;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001368 uint8_t image_index;
David Vincze2d736ad2019-02-18 11:50:22 +01001369
Fabio Utzigb4f88102020-10-04 10:16:24 -03001370#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1371 uint32_t sector;
1372 uint32_t trailer_sz;
1373 uint32_t off;
1374 uint32_t sz;
1375#endif
1376
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001377 (void)bs;
1378
Fabio Utzig13d9e352017-10-05 20:32:31 -03001379#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1380 uint32_t src_size = 0;
Fabio Utzigd638b172019-08-09 10:38:05 -03001381 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001382 assert(rc == 0);
1383#endif
David Brown17609d82017-05-05 09:41:34 -06001384
Fabio Utzigb0f04732019-07-31 09:49:19 -03001385 image_index = BOOT_CURR_IMG(state);
1386
Antonio de Angelisba5fb1c2022-10-11 10:10:37 +01001387 BOOT_LOG_INF("Image %d upgrade secondary slot -> primary slot", image_index);
1388 BOOT_LOG_INF("Erasing the primary slot");
1389
Fabio Utzigb0f04732019-07-31 09:49:19 -03001390 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1391 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001392 assert (rc == 0);
1393
Fabio Utzigb0f04732019-07-31 09:49:19 -03001394 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1395 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001396 assert (rc == 0);
1397
Fabio Utzig10ee6482019-08-01 12:04:52 -03001398 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001399 for (sect = 0, size = 0; sect < sect_count; sect++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001400 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001401 rc = boot_erase_region(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001402 assert(rc == 0);
1403
Fabio Utzig13d9e352017-10-05 20:32:31 -03001404#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
Fabio Utzigb4f88102020-10-04 10:16:24 -03001405 if ((size + this_size) >= src_size) {
1406 size += src_size - size;
1407 size += BOOT_WRITE_SZ(state) - (size % BOOT_WRITE_SZ(state));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001408 break;
1409 }
1410#endif
Fabio Utzigb4f88102020-10-04 10:16:24 -03001411
1412 size += this_size;
David Brown17609d82017-05-05 09:41:34 -06001413 }
1414
Fabio Utzigb4f88102020-10-04 10:16:24 -03001415#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1416 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
1417 sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
1418 sz = 0;
1419 do {
1420 sz += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sector);
1421 off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, sector);
1422 sector--;
1423 } while (sz < trailer_sz);
1424
1425 rc = boot_erase_region(fap_primary_slot, off, sz);
1426 assert(rc == 0);
1427#endif
1428
Fabio Utzigba829042018-09-18 08:29:34 -03001429#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001430 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
Dominik Ermel7f9ac972024-07-12 19:21:40 +00001431 rc = boot_enc_load(BOOT_CURR_ENC(state), BOOT_SECONDARY_SLOT,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001432 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
Fabio Utzig4741c452019-12-19 15:32:41 -03001433 fap_secondary_slot, bs);
David Vincze2d736ad2019-02-18 11:50:22 +01001434
Fabio Utzigba829042018-09-18 08:29:34 -03001435 if (rc < 0) {
1436 return BOOT_EBADIMAGE;
1437 }
Fabio Utzig4741c452019-12-19 15:32:41 -03001438 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001439 return BOOT_EBADIMAGE;
1440 }
1441 }
1442#endif
1443
Antonio de Angelis48547002023-04-14 09:47:09 +01001444 BOOT_LOG_INF("Image %d copying the secondary slot to the primary slot: 0x%zx bytes",
1445 image_index, size);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001446 rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot, 0, 0, size);
Fabio Utzigb4f88102020-10-04 10:16:24 -03001447 if (rc != 0) {
1448 return rc;
1449 }
1450
1451#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1452 rc = boot_write_magic(fap_primary_slot);
1453 if (rc != 0) {
1454 return rc;
1455 }
1456#endif
David Brown17609d82017-05-05 09:41:34 -06001457
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +02001458 rc = BOOT_HOOK_CALL(boot_copy_region_post_hook, 0, BOOT_CURR_IMG(state),
1459 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT), size);
1460 if (rc != 0) {
1461 return rc;
1462 }
1463
David Vinczec3084132020-02-18 14:50:47 +01001464#ifdef MCUBOOT_HW_ROLLBACK_PROT
1465 /* Update the stored security counter with the new image's security counter
1466 * value. Both slots hold the new image at this point, but the secondary
1467 * slot's image header must be passed since the image headers in the
1468 * boot_data structure have not been updated yet.
1469 */
1470 rc = boot_update_security_counter(BOOT_CURR_IMG(state), BOOT_PRIMARY_SLOT,
1471 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1472 if (rc != 0) {
1473 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
1474 return rc;
1475 }
1476#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1477
Petr Buchtac5a528b2024-02-24 08:10:38 +01001478#ifndef MCUBOOT_OVERWRITE_ONLY_KEEP_BACKUP
Fabio Utzig13d9e352017-10-05 20:32:31 -03001479 /*
1480 * Erases header and trailer. The trailer is erased because when a new
1481 * image is written without a trailer as is the case when using newt, the
1482 * trailer that was left might trigger a new upgrade.
1483 */
Christopher Collins2c88e692019-05-22 15:10:14 -07001484 BOOT_LOG_DBG("erasing secondary header");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001485 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001486 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
1487 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001488 assert(rc == 0);
Petr Buchtac5a528b2024-02-24 08:10:38 +01001489#endif
1490
Fabio Utzig10ee6482019-08-01 12:04:52 -03001491 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -07001492 BOOT_LOG_DBG("erasing secondary trailer");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001493 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001494 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
1495 last_sector),
1496 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
1497 last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001498 assert(rc == 0);
1499
David Vincze2d736ad2019-02-18 11:50:22 +01001500 flash_area_close(fap_primary_slot);
1501 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001502
David Vincze2d736ad2019-02-18 11:50:22 +01001503 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001504
1505 return 0;
1506}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001507#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001508
Christopher Collinsa1c12042019-05-23 14:00:28 -07001509#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -03001510/**
1511 * Swaps the two images in flash. If a prior copy operation was interrupted
1512 * by a system reset, this function completes that operation.
1513 *
1514 * @param bs The current boot status. This function reads
1515 * this struct to determine if it is resuming
1516 * an interrupted swap operation. This
1517 * function writes the updated status to this
1518 * function on return.
1519 *
1520 * @return 0 on success; nonzero on failure.
1521 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001522static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001523boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001524{
Fabio Utzig2473ac02017-05-02 12:45:02 -03001525 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001526 const struct flash_area *fap;
Dominik Ermel472d4c72023-02-10 13:29:58 +00001527#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzigba829042018-09-18 08:29:34 -03001528 uint8_t slot;
1529 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001530#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001531 uint32_t size;
1532 uint32_t copy_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001533 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001534 int rc;
1535
1536 /* FIXME: just do this if asked by user? */
1537
1538 size = copy_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001539 image_index = BOOT_CURR_IMG(state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001540
Fabio Utzig12d59162019-11-28 10:01:59 -03001541 if (boot_status_is_reset(bs)) {
Fabio Utzig46490722017-09-04 15:34:32 -03001542 /*
1543 * No swap ever happened, so need to find the largest image which
1544 * will be used to determine the amount of sectors to swap.
1545 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001546 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001547 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001548 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001549 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001550 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001551
Fabio Utzigba829042018-09-18 08:29:34 -03001552#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001553 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001554 fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
Dominik Ermel7f9ac972024-07-12 19:21:40 +00001555 rc = boot_enc_load(BOOT_CURR_ENC(state), 0, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001556 assert(rc >= 0);
1557
1558 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001559 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 0, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001560 assert(rc == 0);
1561 } else {
1562 rc = 0;
1563 }
1564 } else {
Kristine Jassmann73c38c62021-02-03 16:56:14 +00001565 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_ALIGN_SIZE);
Fabio Utzigba829042018-09-18 08:29:34 -03001566 }
1567#endif
1568
Fabio Utzig10ee6482019-08-01 12:04:52 -03001569 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001570 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001571 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001572 assert(rc == 0);
1573 }
1574
Fabio Utzigba829042018-09-18 08:29:34 -03001575#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001576 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001577 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001578 fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
Dominik Ermel7f9ac972024-07-12 19:21:40 +00001579 rc = boot_enc_load(BOOT_CURR_ENC(state), 1, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001580 assert(rc >= 0);
1581
1582 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001583 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001584 assert(rc == 0);
1585 } else {
1586 rc = 0;
1587 }
1588 } else {
Kristine Jassmann73c38c62021-02-03 16:56:14 +00001589 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_ALIGN_SIZE);
Fabio Utzigba829042018-09-18 08:29:34 -03001590 }
1591#endif
1592
Fabio Utzig46490722017-09-04 15:34:32 -03001593 if (size > copy_size) {
1594 copy_size = size;
1595 }
1596
1597 bs->swap_size = copy_size;
1598 } else {
1599 /*
1600 * If a swap was under way, the swap_size should already be present
1601 * in the trailer...
1602 */
Dominik Ermel472d4c72023-02-10 13:29:58 +00001603
1604 rc = boot_find_status(image_index, &fap);
1605 assert(fap != NULL);
1606 rc = boot_read_swap_size(fap, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001607 assert(rc == 0);
1608
1609 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001610
1611#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4741c452019-12-19 15:32:41 -03001612 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Dominik Ermel472d4c72023-02-10 13:29:58 +00001613 rc = boot_read_enc_key(fap, slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001614 assert(rc == 0);
1615
1616 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001617 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001618 break;
1619 }
1620 }
1621
Dominik Ermel7e3a1ce2024-07-12 17:19:17 +00001622 boot_enc_init(BOOT_CURR_ENC(state), slot);
1623
Fabio Utzigba829042018-09-18 08:29:34 -03001624 if (i != BOOT_ENC_KEY_SIZE) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001625 boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001626 }
1627 }
1628#endif
Dominik Ermel472d4c72023-02-10 13:29:58 +00001629 flash_area_close(fap);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001630 }
1631
Fabio Utzig12d59162019-11-28 10:01:59 -03001632 swap_run(state, bs, copy_size);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001633
David Vincze2d736ad2019-02-18 11:50:22 +01001634#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig12d59162019-11-28 10:01:59 -03001635 extern int boot_status_fails;
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001636 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001637 BOOT_LOG_WRN("%d status write fails performing the swap",
1638 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001639 }
1640#endif
Sigvart Hovland3fd4cd42022-09-09 13:21:18 +02001641 rc = BOOT_HOOK_CALL(boot_copy_region_post_hook, 0, BOOT_CURR_IMG(state),
1642 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT), size);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001643
Christopher Collins92ea77f2016-12-12 15:59:26 -08001644 return 0;
1645}
David Brown17609d82017-05-05 09:41:34 -06001646#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001647
David Vinczee32483f2019-06-13 10:46:24 +02001648
Christopher Collins92ea77f2016-12-12 15:59:26 -08001649/**
David Vinczeba3bd602019-06-17 16:01:43 +02001650 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001651 *
David Vinczeba3bd602019-06-17 16:01:43 +02001652 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001653 *
1654 * @return 0 on success; nonzero on failure.
1655 */
1656static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001657boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001658{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001659 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001660#ifndef MCUBOOT_OVERWRITE_ONLY
1661 uint8_t swap_type;
1662#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001663
David Vinczeba3bd602019-06-17 16:01:43 +02001664 /* At this point there are no aborted swaps. */
1665#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001666 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001667#elif defined(MCUBOOT_BOOTSTRAP)
1668 /* Check if the image update was triggered by a bad image in the
1669 * primary slot (the validity of the image in the secondary slot had
1670 * already been checked).
1671 */
Michael Grand5047f032022-11-24 16:49:56 +01001672 FIH_DECLARE(fih_rc, FIH_FAILURE);
Raef Colese8fe6cf2020-05-26 13:07:40 +01001673 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1674 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, bs);
Michael Grand5047f032022-11-24 16:49:56 +01001675 if (rc == 0 || FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001676 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001677 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001678 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001679 }
1680#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03001681 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001682#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001683 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001684
1685#ifndef MCUBOOT_OVERWRITE_ONLY
1686 /* The following state needs image_ok be explicitly set after the
1687 * swap was finished to avoid a new revert.
1688 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001689 swap_type = BOOT_SWAP_TYPE(state);
1690 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1691 swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001692 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001693 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001694 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001695 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001696 }
1697
David Vinczec3084132020-02-18 14:50:47 +01001698#ifdef MCUBOOT_HW_ROLLBACK_PROT
1699 if (swap_type == BOOT_SWAP_TYPE_PERM) {
1700 /* Update the stored security counter with the new image's security
1701 * counter value. The primary slot holds the new image at this point,
1702 * but the secondary slot's image header must be passed since image
1703 * headers in the boot_data structure have not been updated yet.
1704 *
1705 * In case of a permanent image swap mcuboot will never attempt to
1706 * revert the images on the next reboot. Therefore, the security
1707 * counter must be increased right after the image upgrade.
1708 */
1709 rc = boot_update_security_counter(
1710 BOOT_CURR_IMG(state),
1711 BOOT_PRIMARY_SLOT,
1712 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1713 if (rc != 0) {
1714 BOOT_LOG_ERR("Security counter update failed after "
1715 "image upgrade.");
1716 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
1717 }
1718 }
1719#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1720
Fabio Utzige575b0b2019-09-11 12:34:23 -03001721 if (BOOT_IS_UPGRADE(swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001722 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001723 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001724 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001725 }
David Vinczeba3bd602019-06-17 16:01:43 +02001726 }
1727#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001728
David Vinczeba3bd602019-06-17 16:01:43 +02001729 return rc;
1730}
1731
1732/**
1733 * Completes a previously aborted image swap.
1734 *
1735 * @param bs The current boot status.
1736 *
1737 * @return 0 on success; nonzero on failure.
1738 */
1739#if !defined(MCUBOOT_OVERWRITE_ONLY)
1740static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001741boot_complete_partial_swap(struct boot_loader_state *state,
1742 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001743{
1744 int rc;
1745
1746 /* Determine the type of swap operation being resumed from the
1747 * `swap-type` trailer field.
1748 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001749 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001750 assert(rc == 0);
1751
Fabio Utzig10ee6482019-08-01 12:04:52 -03001752 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001753
1754 /* The following states need image_ok be explicitly set after the
1755 * swap was finished to avoid a new revert.
1756 */
1757 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1758 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001759 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001760 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001761 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001762 }
1763 }
1764
Fabio Utzige575b0b2019-09-11 12:34:23 -03001765 if (BOOT_IS_UPGRADE(bs->swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001766 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001767 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001768 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001769 }
1770 }
1771
Fabio Utzig10ee6482019-08-01 12:04:52 -03001772 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001773 BOOT_LOG_ERR("panic!");
1774 assert(0);
1775
1776 /* Loop forever... */
1777 while (1) {}
1778 }
1779
1780 return rc;
1781}
1782#endif /* !MCUBOOT_OVERWRITE_ONLY */
1783
1784#if (BOOT_IMAGE_NUMBER > 1)
1785/**
1786 * Review the validity of previously determined swap types of other images.
1787 *
1788 * @param aborted_swap The current image upgrade is a
1789 * partial/aborted swap.
1790 */
1791static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001792boot_review_image_swap_types(struct boot_loader_state *state,
1793 bool aborted_swap)
David Vinczeba3bd602019-06-17 16:01:43 +02001794{
1795 /* In that case if we rebooted in the middle of an image upgrade process, we
1796 * must review the validity of swap types, that were previously determined
1797 * for other images. The image_ok flag had not been set before the reboot
1798 * for any of the updated images (only the copy_done flag) and thus falsely
1799 * the REVERT swap type has been determined for the previous images that had
1800 * been updated before the reboot.
1801 *
1802 * There are two separate scenarios that we have to deal with:
1803 *
1804 * 1. The reboot has happened during swapping an image:
1805 * The current image upgrade has been determined as a
1806 * partial/aborted swap.
1807 * 2. The reboot has happened between two separate image upgrades:
1808 * In this scenario we must check the swap type of the current image.
1809 * In those cases if it is NONE or REVERT we cannot certainly determine
1810 * the fact of a reboot. In a consistent state images must move in the
1811 * same direction or stay in place, e.g. in practice REVERT and TEST
1812 * swap types cannot be present at the same time. If the swap type of
1813 * the current image is either TEST, PERM or FAIL we must review the
1814 * already determined swap types of other images and set each false
1815 * REVERT swap types to NONE (these images had been successfully
1816 * updated before the system rebooted between two separate image
1817 * upgrades).
1818 */
1819
Fabio Utzig10ee6482019-08-01 12:04:52 -03001820 if (BOOT_CURR_IMG(state) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001821 /* Nothing to do */
1822 return;
1823 }
1824
1825 if (!aborted_swap) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001826 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
1827 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001828 /* Nothing to do */
1829 return;
1830 }
1831 }
1832
Fabio Utzig10ee6482019-08-01 12:04:52 -03001833 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
1834 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
1835 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001836 }
1837 }
1838}
1839#endif
1840
1841/**
1842 * Prepare image to be updated if required.
1843 *
1844 * Prepare image to be updated if required with completing an image swap
1845 * operation if one was aborted and/or determining the type of the
1846 * swap operation. In case of any error set the swap type to NONE.
1847 *
Fabio Utzig10ee6482019-08-01 12:04:52 -03001848 * @param state TODO
David Vinczeba3bd602019-06-17 16:01:43 +02001849 * @param bs Pointer where the read and possibly updated
1850 * boot status can be written to.
1851 */
1852static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001853boot_prepare_image_for_update(struct boot_loader_state *state,
1854 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001855{
1856 int rc;
Michael Grand5047f032022-11-24 16:49:56 +01001857 FIH_DECLARE(fih_rc, FIH_FAILURE);
David Vinczeba3bd602019-06-17 16:01:43 +02001858
Jamie McCraee261b282024-07-26 11:48:19 +01001859#if defined(MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO) || defined(MCUBOOT_DATA_SHARING)
Jamie McCrae4f1ab9e2024-07-26 12:29:24 +01001860 int max_size;
1861#endif
1862
David Vinczeba3bd602019-06-17 16:01:43 +02001863 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001864 rc = boot_read_sectors(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001865 if (rc != 0) {
1866 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
1867 " - too small?", BOOT_MAX_IMG_SECTORS);
1868 /* Unable to determine sector layout, continue with next image
1869 * if there is one.
1870 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001871 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Andrzej Puzdrowski54b4ad92021-06-22 13:21:22 +02001872 if (rc == BOOT_EFLASH)
1873 {
1874 /* Only return on error from the primary image flash */
1875 return;
1876 }
David Vinczeba3bd602019-06-17 16:01:43 +02001877 }
1878
1879 /* Attempt to read an image header from each slot. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001880 rc = boot_read_image_headers(state, false, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02001881 if (rc != 0) {
1882 /* Continue with next image if there is one. */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001883 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001884 BOOT_CURR_IMG(state));
1885 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001886 return;
1887 }
1888
Jamie McCraee261b282024-07-26 11:48:19 +01001889#if defined(MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO) || defined(MCUBOOT_DATA_SHARING)
Jamie McCrae4f1ab9e2024-07-26 12:29:24 +01001890 /* Fetch information on maximum sizes for later usage, if needed */
1891 max_size = app_max_size(state);
1892
1893 if (max_size > 0) {
1894 image_max_sizes[BOOT_CURR_IMG(state)].calculated = true;
1895 image_max_sizes[BOOT_CURR_IMG(state)].max_size = max_size;
1896 }
1897#endif
1898
David Vinczeba3bd602019-06-17 16:01:43 +02001899 /* If the current image's slots aren't compatible, no swap is possible.
1900 * Just boot into primary slot.
1901 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001902 if (boot_slots_compatible(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001903 boot_status_reset(bs);
1904
1905#ifndef MCUBOOT_OVERWRITE_ONLY
1906 rc = swap_read_status(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001907 if (rc != 0) {
1908 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001909 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001910 /* Continue with next image if there is one. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001911 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001912 return;
1913 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001914#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001915
Thomas Altenbachf515bb12024-04-26 18:31:57 +02001916#if defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE)
Fabio Utzig74aef312019-11-28 11:05:34 -03001917 /*
1918 * Must re-read image headers because the boot status might
1919 * have been updated in the previous function call.
1920 */
1921 rc = boot_read_image_headers(state, !boot_status_is_reset(bs), bs);
Fabio Utzig32afe852020-10-04 10:36:02 -03001922#ifdef MCUBOOT_BOOTSTRAP
1923 /* When bootstrapping it's OK to not have image magic in the primary slot */
1924 if (rc != 0 && (BOOT_CURR_IMG(state) != BOOT_PRIMARY_SLOT ||
1925 boot_check_header_erased(state, BOOT_PRIMARY_SLOT) != 0)) {
1926#else
Fabio Utzig74aef312019-11-28 11:05:34 -03001927 if (rc != 0) {
Fabio Utzig32afe852020-10-04 10:36:02 -03001928#endif
1929
Fabio Utzig74aef312019-11-28 11:05:34 -03001930 /* Continue with next image if there is one. */
1931 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
1932 BOOT_CURR_IMG(state));
1933 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1934 return;
1935 }
1936#endif
1937
David Vinczeba3bd602019-06-17 16:01:43 +02001938 /* Determine if we rebooted in the middle of an image swap
1939 * operation. If a partial swap was detected, complete it.
1940 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001941 if (!boot_status_is_reset(bs)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001942
1943#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001944 boot_review_image_swap_types(state, true);
David Vinczeba3bd602019-06-17 16:01:43 +02001945#endif
1946
1947#ifdef MCUBOOT_OVERWRITE_ONLY
1948 /* Should never arrive here, overwrite-only mode has
1949 * no swap state.
1950 */
1951 assert(0);
1952#else
1953 /* Determine the type of swap operation being resumed from the
1954 * `swap-type` trailer field.
1955 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001956 rc = boot_complete_partial_swap(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001957 assert(rc == 0);
1958#endif
1959 /* Attempt to read an image header from each slot. Ensure that
1960 * image headers in slots are aligned with headers in boot_data.
1961 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001962 rc = boot_read_image_headers(state, false, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001963 assert(rc == 0);
1964
1965 /* Swap has finished set to NONE */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001966 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001967 } else {
1968 /* There was no partial swap, determine swap type. */
1969 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001970 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001971 } else {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001972 FIH_CALL(boot_validate_slot, fih_rc,
1973 state, BOOT_SECONDARY_SLOT, bs);
Michael Grand5047f032022-11-24 16:49:56 +01001974 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001975 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
1976 } else {
1977 BOOT_SWAP_TYPE(state) = bs->swap_type;
1978 }
David Vinczeba3bd602019-06-17 16:01:43 +02001979 }
1980
1981#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001982 boot_review_image_swap_types(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02001983#endif
1984
1985#ifdef MCUBOOT_BOOTSTRAP
Fabio Utzig10ee6482019-08-01 12:04:52 -03001986 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001987 /* Header checks are done first because they are
1988 * inexpensive. Since overwrite-only copies starting from
1989 * offset 0, if interrupted, it might leave a valid header
1990 * magic, so also run validation on the primary slot to be
1991 * sure it's not OK.
1992 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001993 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1994 FIH_CALL(boot_validate_slot, fih_rc,
1995 state, BOOT_PRIMARY_SLOT, bs);
1996
Michael Grand5047f032022-11-24 16:49:56 +01001997 if (rc == 0 || FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001998
Fabio Utzig3d77c952020-10-04 10:23:17 -03001999 rc = (boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC) ? 1: 0;
Raef Colese8fe6cf2020-05-26 13:07:40 +01002000 FIH_CALL(boot_validate_slot, fih_rc,
2001 state, BOOT_SECONDARY_SLOT, bs);
2002
Michael Grand5047f032022-11-24 16:49:56 +01002003 if (rc == 1 && FIH_EQ(fih_rc, FIH_SUCCESS)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002004 /* Set swap type to REVERT to overwrite the primary
2005 * slot with the image contained in secondary slot
2006 * and to trigger the explicit setting of the
2007 * image_ok flag.
2008 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03002009 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02002010 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02002011 }
2012 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02002013#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002014 }
David Vinczeba3bd602019-06-17 16:01:43 +02002015 } else {
2016 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002017 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002018 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002019}
2020
Mark Horvathccaf7f82021-01-04 18:16:42 +01002021/**
2022 * Updates the security counter for the current image.
2023 *
2024 * @param state Boot loader status information.
2025 *
2026 * @return 0 on success; nonzero on failure.
2027 */
2028static int
2029boot_update_hw_rollback_protection(struct boot_loader_state *state)
2030{
2031#ifdef MCUBOOT_HW_ROLLBACK_PROT
2032 int rc;
2033
2034 /* Update the stored security counter with the active image's security
2035 * counter value. It will only be updated if the new security counter is
2036 * greater than the stored value.
2037 *
2038 * In case of a successful image swapping when the swap type is TEST the
2039 * security counter can be increased only after a reset, when the swap
2040 * type is NONE and the image has marked itself "OK" (the image_ok flag
2041 * has been set). This way a "revert" can be performed when it's
2042 * necessary.
2043 */
2044 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
2045 rc = boot_update_security_counter(
2046 BOOT_CURR_IMG(state),
2047 BOOT_PRIMARY_SLOT,
2048 boot_img_hdr(state, BOOT_PRIMARY_SLOT));
2049 if (rc != 0) {
2050 BOOT_LOG_ERR("Security counter update failed after image "
2051 "validation.");
2052 return rc;
2053 }
2054 }
2055
2056 return 0;
2057
2058#else /* MCUBOOT_HW_ROLLBACK_PROT */
2059 (void) (state);
2060
2061 return 0;
2062#endif
2063}
2064
Jerzy Kasenberge3f895d2022-04-12 15:05:33 +02002065/**
2066 * Checks test swap downgrade prevention conditions.
2067 *
2068 * Function called only for swap upgrades test run. It may prevent
2069 * swap if slot 1 image has <= version number or < security counter
2070 *
2071 * @param state Boot loader status information.
2072 *
2073 * @return 0 - image can be swapped, -1 downgrade prevention
2074 */
2075static int
2076check_downgrade_prevention(struct boot_loader_state *state)
2077{
2078#if defined(MCUBOOT_DOWNGRADE_PREVENTION) && \
2079 (defined(MCUBOOT_SWAP_USING_MOVE) || defined(MCUBOOT_SWAP_USING_SCRATCH))
2080 uint32_t security_counter[2];
2081 int rc;
2082
2083 if (MCUBOOT_DOWNGRADE_PREVENTION_SECURITY_COUNTER) {
2084 /* If there was security no counter in slot 0, allow swap */
2085 rc = bootutil_get_img_security_cnt(&(BOOT_IMG(state, 0).hdr),
2086 BOOT_IMG(state, 0).area,
2087 &security_counter[0]);
2088 if (rc != 0) {
2089 return 0;
2090 }
2091 /* If there is no security counter in slot 1, or it's lower than
2092 * that of slot 0, prevent downgrade */
2093 rc = bootutil_get_img_security_cnt(&(BOOT_IMG(state, 1).hdr),
2094 BOOT_IMG(state, 1).area,
2095 &security_counter[1]);
2096 if (rc != 0 || security_counter[0] > security_counter[1]) {
2097 rc = -1;
2098 }
2099 }
2100 else {
2101 rc = boot_version_cmp(&boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
2102 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
2103 }
2104 if (rc < 0) {
2105 /* Image in slot 0 prevents downgrade, delete image in slot 1 */
Antonio de Angelis48547002023-04-14 09:47:09 +01002106 BOOT_LOG_INF("Image %d in slot 1 erased due to downgrade prevention", BOOT_CURR_IMG(state));
Jerzy Kasenberge3f895d2022-04-12 15:05:33 +02002107 flash_area_erase(BOOT_IMG(state, 1).area, 0,
2108 flash_area_get_size(BOOT_IMG(state, 1).area));
2109 } else {
2110 rc = 0;
2111 }
2112 return rc;
2113#else
2114 (void)state;
2115 return 0;
2116#endif
2117}
2118
Jamie McCraee261b282024-07-26 11:48:19 +01002119#if !defined(__BOOTSIM__)
2120static void boot_get_sector_buffers(struct sector_buffer_t *buffers)
2121{
2122 /* The array of slot sectors are defined here (as opposed to file scope) so
2123 * that they don't get allocated for non-boot-loader apps. This is
2124 * necessary because the gcc option "-fdata-sections" doesn't seem to have
2125 * any effect in older gcc versions (e.g., 4.8.4).
2126 */
2127 static boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
2128 static boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
2129#if MCUBOOT_SWAP_USING_SCRATCH
2130 static boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
2131#endif
2132
2133 buffers->primary = (boot_sector_t *)&primary_slot_sectors;
2134 buffers->secondary = (boot_sector_t *)&secondary_slot_sectors;
2135#if MCUBOOT_SWAP_USING_SCRATCH
2136 buffers->scratch = (boot_sector_t *)&scratch_sectors;
2137#endif
2138}
2139#endif
2140
Michael Grand5047f032022-11-24 16:49:56 +01002141fih_ret
Fabio Utzig10ee6482019-08-01 12:04:52 -03002142context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08002143{
Marti Bolivar84898652017-06-13 17:20:22 -04002144 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02002145 struct boot_status bs;
Jamie McCraee261b282024-07-26 11:48:19 +01002146#if !defined(__BOOTSIM__)
2147 struct sector_buffer_t sector_buffers;
2148#endif
David Vincze9015a5d2020-05-18 14:43:11 +02002149 int rc = -1;
Michael Grand5047f032022-11-24 16:49:56 +01002150 FIH_DECLARE(fih_rc, FIH_FAILURE);
Marti Bolivarc0b47912017-06-13 17:18:09 -04002151 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03002152 int image_index;
Fabio Utzig298913b2019-08-28 11:22:45 -03002153 bool has_upgrade;
Michael Grand5047f032022-11-24 16:49:56 +01002154 volatile int fih_cnt;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002155
Jamie McCraee261b282024-07-26 11:48:19 +01002156#if defined(__BOOTSIM__)
Christopher Collins92ea77f2016-12-12 15:59:26 -08002157 /* The array of slot sectors are defined here (as opposed to file scope) so
2158 * that they don't get allocated for non-boot-loader apps. This is
2159 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002160 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08002161 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002162 TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
2163 TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03002164#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03002165 TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03002166#endif
Jamie McCraee261b282024-07-26 11:48:19 +01002167#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08002168
Fabio Utzig298913b2019-08-28 11:22:45 -03002169 has_upgrade = false;
2170
2171#if (BOOT_IMAGE_NUMBER == 1)
2172 (void)has_upgrade;
2173#endif
Fabio Utzigba829042018-09-18 08:29:34 -03002174
Jamie McCraee261b282024-07-26 11:48:19 +01002175#if !defined(__BOOTSIM__)
2176 boot_get_sector_buffers(&sector_buffers);
2177#endif
2178
David Vinczeba3bd602019-06-17 16:01:43 +02002179 /* Iterate over all the images. By the end of the loop the swap type has
2180 * to be determined for each image and all aborted swaps have to be
2181 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002182 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002183 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002184#if BOOT_IMAGE_NUMBER > 1
2185 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2186 continue;
2187 }
2188#endif
David Vinczeba3bd602019-06-17 16:01:43 +02002189#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
2190 /* The keys used for encryption may no longer be valid (could belong to
2191 * another images). Therefore, mark them as invalid to force their reload
2192 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03002193 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03002194 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Brown554c52e2017-06-30 16:01:07 -06002195#endif
2196
Fabio Utzig10ee6482019-08-01 12:04:52 -03002197 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03002198
Jamie McCraee261b282024-07-26 11:48:19 +01002199#if !defined(__BOOTSIM__)
2200 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
2201 &sector_buffers.primary[image_index];
2202 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
2203 &sector_buffers.secondary[image_index];
2204#if MCUBOOT_SWAP_USING_SCRATCH
2205 state->scratch.sectors = sector_buffers.scratch;
2206#endif
2207#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03002208 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002209 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03002210 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002211 secondary_slot_sectors[image_index];
Fabio Utzig12d59162019-11-28 10:01:59 -03002212#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03002213 state->scratch.sectors = scratch_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -03002214#endif
Jamie McCraee261b282024-07-26 11:48:19 +01002215#endif
David Vinczeba3bd602019-06-17 16:01:43 +02002216
2217 /* Open primary and secondary image areas for the duration
2218 * of this call.
2219 */
2220 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03002221 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03002222 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02002223 assert(rc == 0);
Jamie McCrae2929a972023-09-25 11:12:20 +01002224
2225 if (rc != 0) {
2226 BOOT_LOG_ERR("Failed to open flash area ID %d (image %d slot %d): %d, "
2227 "cannot continue", fa_id, image_index, (int8_t)slot, rc);
2228 FIH_PANIC;
2229 }
David Vinczeba3bd602019-06-17 16:01:43 +02002230 }
Fabio Utzig12d59162019-11-28 10:01:59 -03002231#if MCUBOOT_SWAP_USING_SCRATCH
David Vinczeba3bd602019-06-17 16:01:43 +02002232 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03002233 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002234 assert(rc == 0);
Jamie McCrae2929a972023-09-25 11:12:20 +01002235
2236 if (rc != 0) {
2237 BOOT_LOG_ERR("Failed to open scratch flash area: %d, cannot continue", rc);
2238 FIH_PANIC;
2239 }
Fabio Utzig12d59162019-11-28 10:01:59 -03002240#endif
David Vinczeba3bd602019-06-17 16:01:43 +02002241
2242 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002243 boot_prepare_image_for_update(state, &bs);
Fabio Utzig298913b2019-08-28 11:22:45 -03002244
Fabio Utzige575b0b2019-09-11 12:34:23 -03002245 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
Fabio Utzig298913b2019-08-28 11:22:45 -03002246 has_upgrade = true;
2247 }
David Vinczeba3bd602019-06-17 16:01:43 +02002248 }
2249
David Vinczee32483f2019-06-13 10:46:24 +02002250#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig298913b2019-08-28 11:22:45 -03002251 if (has_upgrade) {
2252 /* Iterate over all the images and verify whether the image dependencies
2253 * are all satisfied and update swap type if necessary.
2254 */
2255 rc = boot_verify_dependencies(state);
David Vincze8b0b6372020-05-20 19:54:44 +02002256 if (rc != 0) {
Fabio Utzig298913b2019-08-28 11:22:45 -03002257 /*
2258 * It was impossible to upgrade because the expected dependency version
2259 * was not available. Here we already changed the swap_type so that
2260 * instead of asserting the bootloader, we continue and no upgrade is
2261 * performed.
2262 */
2263 rc = 0;
2264 }
2265 }
David Vinczee32483f2019-06-13 10:46:24 +02002266#endif
2267
Jamie McCrae56cb6102022-03-23 11:57:03 +00002268 /* Trigger status change callback with upgrading status */
2269 mcuboot_status_change(MCUBOOT_STATUS_UPGRADING);
2270
David Vinczeba3bd602019-06-17 16:01:43 +02002271 /* Iterate over all the images. At this point there are no aborted swaps
2272 * and the swap types are determined for each image. By the end of the loop
2273 * all required update operations will have been finished.
2274 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002275 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002276#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesf11de642021-10-15 11:11:56 +01002277 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2278 continue;
2279 }
2280
David Vinczeba3bd602019-06-17 16:01:43 +02002281#ifdef MCUBOOT_ENC_IMAGES
2282 /* The keys used for encryption may no longer be valid (could belong to
2283 * another images). Therefore, mark them as invalid to force their reload
2284 * by boot_enc_load().
2285 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03002286 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002287#endif /* MCUBOOT_ENC_IMAGES */
2288
2289 /* Indicate that swap is not aborted */
Fabio Utzig12d59162019-11-28 10:01:59 -03002290 boot_status_reset(&bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002291#endif /* (BOOT_IMAGE_NUMBER > 1) */
2292
2293 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002294 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02002295
Fabio Utzig10ee6482019-08-01 12:04:52 -03002296 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002297 case BOOT_SWAP_TYPE_NONE:
2298 break;
2299
Jerzy Kasenberge3f895d2022-04-12 15:05:33 +02002300 case BOOT_SWAP_TYPE_TEST:
Michael Grand99613c62023-06-20 15:01:00 +02002301 /* fallthrough */
2302 case BOOT_SWAP_TYPE_PERM:
Jerzy Kasenberge3f895d2022-04-12 15:05:33 +02002303 if (check_downgrade_prevention(state) != 0) {
2304 /* Downgrade prevented */
2305 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
2306 break;
2307 }
2308 /* fallthrough */
David Vinczeba3bd602019-06-17 16:01:43 +02002309 case BOOT_SWAP_TYPE_REVERT:
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +02002310 rc = BOOT_HOOK_CALL(boot_perform_update_hook, BOOT_HOOK_REGULAR,
2311 BOOT_CURR_IMG(state), &(BOOT_IMG(state, 1).hdr),
2312 BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT));
2313 if (rc == BOOT_HOOK_REGULAR)
2314 {
2315 rc = boot_perform_update(state, &bs);
2316 }
David Vinczeba3bd602019-06-17 16:01:43 +02002317 assert(rc == 0);
2318 break;
2319
2320 case BOOT_SWAP_TYPE_FAIL:
2321 /* The image in secondary slot was invalid and is now erased. Ensure
2322 * we don't try to boot into it again on the next reboot. Do this by
2323 * pretending we just reverted back to primary slot.
2324 */
2325#ifndef MCUBOOT_OVERWRITE_ONLY
2326 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig12d59162019-11-28 10:01:59 -03002327 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002328 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002329 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002330 }
2331#endif /* !MCUBOOT_OVERWRITE_ONLY */
2332 break;
2333
2334 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002335 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002336 }
2337
Fabio Utzig10ee6482019-08-01 12:04:52 -03002338 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002339 BOOT_LOG_ERR("panic!");
2340 assert(0);
2341
2342 /* Loop forever... */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002343 FIH_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002344 }
2345 }
2346
2347 /* Iterate over all the images. At this point all required update operations
2348 * have finished. By the end of the loop each image in the primary slot will
2349 * have been re-validated.
2350 */
Michael Grand5047f032022-11-24 16:49:56 +01002351 FIH_SET(fih_cnt, 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -03002352 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002353#if BOOT_IMAGE_NUMBER > 1
Michael Grand5047f032022-11-24 16:49:56 +01002354 /* Hardenned to prevent from skipping check of a given image,
2355 * tmp_img_mask is declared volatile
2356 */
2357 volatile bool tmp_img_mask;
2358 FIH_SET(tmp_img_mask, state->img_mask[BOOT_CURR_IMG(state)]);
2359 if (FIH_EQ(tmp_img_mask, true)) {
2360 ++fih_cnt;
Raef Colesf11de642021-10-15 11:11:56 +01002361 continue;
2362 }
2363#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -03002364 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02002365 /* Attempt to read an image header from each slot. Ensure that image
2366 * headers in slots are aligned with headers in boot_data.
Dominik Ermel6f7f8732024-02-01 11:59:24 +00002367 * Note: Quite complicated internal logic of boot_read_image_headers
2368 * uses boot state, the last parm, to figure out in which slot which
2369 * header is located; when boot state is not provided, then it
2370 * is assumed that headers are at proper slots (we are not in
2371 * the middle of moving images, etc).
David Vinczeba3bd602019-06-17 16:01:43 +02002372 */
Dominik Ermel6f7f8732024-02-01 11:59:24 +00002373 rc = boot_read_image_headers(state, false, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02002374 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01002375 FIH_SET(fih_rc, FIH_FAILURE);
David Vinczeba3bd602019-06-17 16:01:43 +02002376 goto out;
2377 }
2378 /* Since headers were reloaded, it can be assumed we just performed
2379 * a swap or overwrite. Now the header info that should be used to
2380 * provide the data for the bootstrap, which previously was at
2381 * secondary slot, was updated to primary slot.
2382 */
2383 }
2384
2385#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Raef Colese8fe6cf2020-05-26 13:07:40 +01002386 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, NULL);
Michael Grand5047f032022-11-24 16:49:56 +01002387 /* Check for all possible values is redundant in normal operation it
2388 * is meant to prevent FI attack.
2389 */
2390 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS) ||
2391 FIH_EQ(fih_rc, FIH_FAILURE) ||
2392 FIH_EQ(fih_rc, FIH_NO_BOOTABLE_IMAGE)) {
2393 FIH_SET(fih_rc, FIH_FAILURE);
David Vinczeba3bd602019-06-17 16:01:43 +02002394 goto out;
2395 }
2396#else
2397 /* Even if we're not re-validating the primary slot, we could be booting
2398 * onto an empty flash chip. At least do a basic sanity check that
2399 * the magic number on the image is OK.
2400 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002401 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002402 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
Dominik Ermel4a4d1ac2021-08-06 11:23:52 +00002403 BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic,
Fabio Utzig10ee6482019-08-01 12:04:52 -03002404 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002405 rc = BOOT_EBADIMAGE;
Michael Grand5047f032022-11-24 16:49:56 +01002406 FIH_SET(fih_rc, FIH_FAILURE);
David Vinczeba3bd602019-06-17 16:01:43 +02002407 goto out;
2408 }
David Vinczec3084132020-02-18 14:50:47 +01002409#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
2410
Mark Horvathccaf7f82021-01-04 18:16:42 +01002411 rc = boot_update_hw_rollback_protection(state);
David Vincze1cf11b52020-03-24 07:51:09 +01002412 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01002413 FIH_SET(fih_rc, FIH_FAILURE);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002414 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002415 }
David Vincze1cf11b52020-03-24 07:51:09 +01002416
Mark Horvathccaf7f82021-01-04 18:16:42 +01002417 rc = boot_add_shared_data(state, BOOT_PRIMARY_SLOT);
David Vincze1cf11b52020-03-24 07:51:09 +01002418 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01002419 FIH_SET(fih_rc, FIH_FAILURE);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002420 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002421 }
Michael Grand5047f032022-11-24 16:49:56 +01002422 ++fih_cnt;
David Vinczeba3bd602019-06-17 16:01:43 +02002423 }
Michael Grand5047f032022-11-24 16:49:56 +01002424 /*
2425 * fih_cnt should be equal to BOOT_IMAGE_NUMBER now.
2426 * If this is not the case, at least one iteration of the loop
2427 * has been skipped.
2428 */
2429 if(FIH_NOT_EQ(fih_cnt, BOOT_IMAGE_NUMBER)) {
2430 FIH_PANIC;
2431 }
Fabio Utzigf616c542019-12-19 15:23:32 -03002432
Raef Colesfe57e7d2021-10-15 11:07:09 +01002433 fill_rsp(state, rsp);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002434
Raef Colese8fe6cf2020-05-26 13:07:40 +01002435 fih_rc = FIH_SUCCESS;
Fabio Utzig298913b2019-08-28 11:22:45 -03002436out:
Dominik Ermel256bc372022-12-07 15:51:31 +00002437 /*
2438 * Since the boot_status struct stores plaintext encryption keys, reset
2439 * them here to avoid the possibility of jumping into an image that could
2440 * easily recover them.
2441 */
2442#if defined(MCUBOOT_ENC_IMAGES) || defined(MCUBOOT_SWAP_SAVE_ENCTLV)
2443 like_mbedtls_zeroize(&bs, sizeof(bs));
2444#else
2445 memset(&bs, 0, sizeof(struct boot_status));
2446#endif
2447
Mark Horvathccaf7f82021-01-04 18:16:42 +01002448 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002449 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002450}
2451
Michael Grand5047f032022-11-24 16:49:56 +01002452fih_ret
Christopher Collins92ea77f2016-12-12 15:59:26 -08002453split_go(int loader_slot, int split_slot, void **entry)
2454{
Marti Bolivarc50926f2017-06-14 09:35:40 -04002455 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08002456 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002457 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002458 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002459 int rc;
Michael Grand5047f032022-11-24 16:49:56 +01002460 FIH_DECLARE(fih_rc, FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002461
Christopher Collins92ea77f2016-12-12 15:59:26 -08002462 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
2463 if (sectors == NULL) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002464 FIH_RET(FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002465 }
David Vinczeba3bd602019-06-17 16:01:43 +02002466 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
2467 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002468
2469 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
2470 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07002471 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04002472 assert(rc == 0);
2473 split_flash_id = flash_area_id_from_image_slot(split_slot);
2474 rc = flash_area_open(split_flash_id,
2475 &BOOT_IMG_AREA(&boot_data, split_slot));
2476 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002477
2478 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002479 rc = boot_read_sectors(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002480 if (rc != 0) {
2481 rc = SPLIT_GO_ERR;
2482 goto done;
2483 }
2484
Fabio Utzig12d59162019-11-28 10:01:59 -03002485 rc = boot_read_image_headers(&boot_data, true, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002486 if (rc != 0) {
2487 goto done;
2488 }
2489
Christopher Collins92ea77f2016-12-12 15:59:26 -08002490 /* Don't check the bootable image flag because we could really call a
2491 * bootable or non-bootable image. Just validate that the image check
2492 * passes which is distinct from the normal check.
2493 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002494 FIH_CALL(split_image_check, fih_rc,
2495 boot_img_hdr(&boot_data, split_slot),
2496 BOOT_IMG_AREA(&boot_data, split_slot),
2497 boot_img_hdr(&boot_data, loader_slot),
2498 BOOT_IMG_AREA(&boot_data, loader_slot));
Michael Grand5047f032022-11-24 16:49:56 +01002499 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08002500 goto done;
2501 }
2502
Marti Bolivarea088872017-06-12 17:10:49 -04002503 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04002504 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08002505 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002506 rc = SPLIT_GO_OK;
2507
2508done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04002509 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
2510 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002511 free(sectors);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002512
2513 if (rc) {
Michael Grand5047f032022-11-24 16:49:56 +01002514 FIH_SET(fih_rc, FIH_FAILURE);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002515 }
2516
2517 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002518}
David Vinczee574f2d2020-07-10 11:42:03 +02002519
Tamas Banfe031092020-09-10 17:32:39 +02002520#else /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02002521
2522/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002523 * Opens all flash areas and checks which contain an image with a valid header.
David Vinczee574f2d2020-07-10 11:42:03 +02002524 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002525 * @param state Boot loader status information.
David Vinczee574f2d2020-07-10 11:42:03 +02002526 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002527 * @return 0 on success; nonzero on failure.
2528 */
2529static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002530boot_get_slot_usage(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002531{
2532 uint32_t slot;
2533 int fa_id;
2534 int rc;
2535 struct image_header *hdr = NULL;
2536
2537 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002538#if BOOT_IMAGE_NUMBER > 1
2539 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2540 continue;
2541 }
2542#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +01002543 /* Open all the slots */
2544 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2545 fa_id = flash_area_id_from_multi_image_slot(
2546 BOOT_CURR_IMG(state), slot);
2547 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
2548 assert(rc == 0);
2549 }
2550
2551 /* Attempt to read an image header from each slot. */
2552 rc = boot_read_image_headers(state, false, NULL);
2553 if (rc != 0) {
2554 BOOT_LOG_WRN("Failed reading image headers.");
2555 return rc;
2556 }
2557
2558 /* Check headers in all slots */
2559 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2560 hdr = boot_img_hdr(state, slot);
2561
Jamie McCraedbb5c782024-08-22 10:43:14 +01002562 if (boot_is_header_valid(hdr, BOOT_IMG_AREA(state, slot), state)) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002563 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = true;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002564 BOOT_LOG_IMAGE_INFO(slot, hdr);
2565 } else {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002566 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = false;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002567 BOOT_LOG_INF("Image %d %s slot: Image not found",
2568 BOOT_CURR_IMG(state),
2569 (slot == BOOT_PRIMARY_SLOT)
2570 ? "Primary" : "Secondary");
2571 }
2572 }
2573
Raef Colesfe57e7d2021-10-15 11:07:09 +01002574 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002575 }
2576
2577 return 0;
2578}
2579
2580/**
2581 * Finds the slot containing the image with the highest version number for the
2582 * current image.
2583 *
2584 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002585 *
2586 * @return NO_ACTIVE_SLOT if no available slot found, number of
2587 * the found slot otherwise.
David Vinczee574f2d2020-07-10 11:42:03 +02002588 */
2589static uint32_t
Raef Colesfe57e7d2021-10-15 11:07:09 +01002590find_slot_with_highest_version(struct boot_loader_state *state)
David Vinczee574f2d2020-07-10 11:42:03 +02002591{
David Vinczee574f2d2020-07-10 11:42:03 +02002592 uint32_t slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002593 uint32_t candidate_slot = NO_ACTIVE_SLOT;
2594 int rc;
David Vinczee574f2d2020-07-10 11:42:03 +02002595
Mark Horvathccaf7f82021-01-04 18:16:42 +01002596 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002597 if (state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot]) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002598 if (candidate_slot == NO_ACTIVE_SLOT) {
2599 candidate_slot = slot;
2600 } else {
2601 rc = boot_version_cmp(
2602 &boot_img_hdr(state, slot)->ih_ver,
2603 &boot_img_hdr(state, candidate_slot)->ih_ver);
2604 if (rc == 1) {
2605 /* The version of the image being examined is greater than
2606 * the version of the current candidate.
2607 */
2608 candidate_slot = slot;
2609 }
2610 }
David Vinczee574f2d2020-07-10 11:42:03 +02002611 }
2612 }
2613
Mark Horvathccaf7f82021-01-04 18:16:42 +01002614 return candidate_slot;
David Vinczee574f2d2020-07-10 11:42:03 +02002615}
2616
Mark Horvathccaf7f82021-01-04 18:16:42 +01002617#ifdef MCUBOOT_HAVE_LOGGING
2618/**
2619 * Prints the state of the loaded images.
2620 *
2621 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002622 */
2623static void
Raef Colesfe57e7d2021-10-15 11:07:09 +01002624print_loaded_images(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002625{
2626 uint32_t active_slot;
2627
David Brown695e5912021-05-24 16:58:01 -06002628 (void)state;
2629
Mark Horvathccaf7f82021-01-04 18:16:42 +01002630 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002631#if BOOT_IMAGE_NUMBER > 1
2632 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2633 continue;
2634 }
2635#endif
Raef Colesfe57e7d2021-10-15 11:07:09 +01002636 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002637
2638 BOOT_LOG_INF("Image %d loaded from the %s slot",
2639 BOOT_CURR_IMG(state),
2640 (active_slot == BOOT_PRIMARY_SLOT) ?
2641 "primary" : "secondary");
2642 }
2643}
2644#endif
2645
David Vincze1c456242021-06-29 15:25:24 +02002646#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
David Vincze505fba22020-10-22 13:53:29 +02002647/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002648 * Checks whether the active slot of the current image was previously selected
2649 * to run. Erases the image if it was selected but its execution failed,
2650 * otherwise marks it as selected if it has not been before.
David Vincze505fba22020-10-22 13:53:29 +02002651 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002652 * @param state Boot loader status information.
David Vincze505fba22020-10-22 13:53:29 +02002653 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002654 * @return 0 on success; nonzero on failure.
David Vincze505fba22020-10-22 13:53:29 +02002655 */
2656static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002657boot_select_or_erase(struct boot_loader_state *state)
David Vincze505fba22020-10-22 13:53:29 +02002658{
2659 const struct flash_area *fap;
2660 int fa_id;
2661 int rc;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002662 uint32_t active_slot;
2663 struct boot_swap_state* active_swap_state;
David Vincze505fba22020-10-22 13:53:29 +02002664
Raef Colesfe57e7d2021-10-15 11:07:09 +01002665 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002666
2667 fa_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), active_slot);
David Vincze505fba22020-10-22 13:53:29 +02002668 rc = flash_area_open(fa_id, &fap);
2669 assert(rc == 0);
2670
Raef Colesfe57e7d2021-10-15 11:07:09 +01002671 active_swap_state = &(state->slot_usage[BOOT_CURR_IMG(state)].swap_state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002672
2673 memset(active_swap_state, 0, sizeof(struct boot_swap_state));
2674 rc = boot_read_swap_state(fap, active_swap_state);
David Vincze505fba22020-10-22 13:53:29 +02002675 assert(rc == 0);
2676
Mark Horvathccaf7f82021-01-04 18:16:42 +01002677 if (active_swap_state->magic != BOOT_MAGIC_GOOD ||
2678 (active_swap_state->copy_done == BOOT_FLAG_SET &&
2679 active_swap_state->image_ok != BOOT_FLAG_SET)) {
David Vincze505fba22020-10-22 13:53:29 +02002680 /*
2681 * A reboot happened without the image being confirmed at
2682 * runtime or its trailer is corrupted/invalid. Erase the image
2683 * to prevent it from being selected again on the next reboot.
2684 */
2685 BOOT_LOG_DBG("Erasing faulty image in the %s slot.",
Carlos Falgueras Garcíaae13c3c2021-06-21 17:20:31 +02002686 (active_slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
Dominik Ermel260ae092021-04-23 05:38:45 +00002687 rc = flash_area_erase(fap, 0, flash_area_get_size(fap));
David Vincze505fba22020-10-22 13:53:29 +02002688 assert(rc == 0);
2689
2690 flash_area_close(fap);
2691 rc = -1;
2692 } else {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002693 if (active_swap_state->copy_done != BOOT_FLAG_SET) {
2694 if (active_swap_state->copy_done == BOOT_FLAG_BAD) {
David Vincze505fba22020-10-22 13:53:29 +02002695 BOOT_LOG_DBG("The copy_done flag had an unexpected value. Its "
2696 "value was neither 'set' nor 'unset', but 'bad'.");
2697 }
2698 /*
2699 * Set the copy_done flag, indicating that the image has been
2700 * selected to boot. It can be set in advance, before even
2701 * validating the image, because in case the validation fails, the
2702 * entire image slot will be erased (including the trailer).
2703 */
2704 rc = boot_write_copy_done(fap);
2705 if (rc != 0) {
2706 BOOT_LOG_WRN("Failed to set copy_done flag of the image in "
Carlos Falgueras Garcíaae13c3c2021-06-21 17:20:31 +02002707 "the %s slot.", (active_slot == BOOT_PRIMARY_SLOT) ?
David Vincze505fba22020-10-22 13:53:29 +02002708 "primary" : "secondary");
2709 rc = 0;
2710 }
2711 }
2712 flash_area_close(fap);
2713 }
2714
2715 return rc;
2716}
David Vincze1c456242021-06-29 15:25:24 +02002717#endif /* MCUBOOT_DIRECT_XIP && MCUBOOT_DIRECT_XIP_REVERT */
David Vincze505fba22020-10-22 13:53:29 +02002718
Tamas Banfe031092020-09-10 17:32:39 +02002719#ifdef MCUBOOT_RAM_LOAD
2720
Mark Horvathccaf7f82021-01-04 18:16:42 +01002721#ifndef MULTIPLE_EXECUTABLE_RAM_REGIONS
Tamas Banfe031092020-09-10 17:32:39 +02002722#if !defined(IMAGE_EXECUTABLE_RAM_START) || !defined(IMAGE_EXECUTABLE_RAM_SIZE)
2723#error "Platform MUST define executable RAM bounds in case of RAM_LOAD"
2724#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +01002725#endif
Tamas Banfe031092020-09-10 17:32:39 +02002726
2727/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002728 * Verifies that the active slot of the current image can be loaded within the
2729 * predefined bounds that are allowed to be used by executable images.
Tamas Banfe031092020-09-10 17:32:39 +02002730 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002731 * @param state Boot loader status information.
Tamas Banfe031092020-09-10 17:32:39 +02002732 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002733 * @return 0 on success; nonzero on failure.
Tamas Banfe031092020-09-10 17:32:39 +02002734 */
2735static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002736boot_verify_ram_load_address(struct boot_loader_state *state)
Tamas Banfe031092020-09-10 17:32:39 +02002737{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002738 uint32_t img_dst;
2739 uint32_t img_sz;
Tamas Banfe031092020-09-10 17:32:39 +02002740 uint32_t img_end_addr;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002741 uint32_t exec_ram_start;
2742 uint32_t exec_ram_size;
David Brown695e5912021-05-24 16:58:01 -06002743
2744 (void)state;
2745
Mark Horvathccaf7f82021-01-04 18:16:42 +01002746#ifdef MULTIPLE_EXECUTABLE_RAM_REGIONS
2747 int rc;
Tamas Banfe031092020-09-10 17:32:39 +02002748
Mark Horvathccaf7f82021-01-04 18:16:42 +01002749 rc = boot_get_image_exec_ram_info(BOOT_CURR_IMG(state), &exec_ram_start,
2750 &exec_ram_size);
2751 if (rc != 0) {
2752 return BOOT_EBADSTATUS;
2753 }
2754#else
2755 exec_ram_start = IMAGE_EXECUTABLE_RAM_START;
2756 exec_ram_size = IMAGE_EXECUTABLE_RAM_SIZE;
2757#endif
2758
Raef Colesfe57e7d2021-10-15 11:07:09 +01002759 img_dst = state->slot_usage[BOOT_CURR_IMG(state)].img_dst;
2760 img_sz = state->slot_usage[BOOT_CURR_IMG(state)].img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002761
2762 if (img_dst < exec_ram_start) {
Tamas Banfe031092020-09-10 17:32:39 +02002763 return BOOT_EBADIMAGE;
2764 }
2765
2766 if (!boot_u32_safe_add(&img_end_addr, img_dst, img_sz)) {
2767 return BOOT_EBADIMAGE;
2768 }
2769
Mark Horvathccaf7f82021-01-04 18:16:42 +01002770 if (img_end_addr > (exec_ram_start + exec_ram_size)) {
Tamas Banfe031092020-09-10 17:32:39 +02002771 return BOOT_EBADIMAGE;
2772 }
2773
2774 return 0;
2775}
2776
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002777#ifdef MCUBOOT_ENC_IMAGES
2778
2779/**
2780 * Copies and decrypts an image from a slot in the flash to an SRAM address.
2781 *
2782 * @param state Boot loader status information.
2783 * @param slot The flash slot of the image to be copied to SRAM.
2784 * @param hdr The image header.
2785 * @param src_sz Size of the image.
2786 * @param img_dst Pointer to the address at which the image needs to be
2787 * copied to SRAM.
2788 *
2789 * @return 0 on success; nonzero on failure.
2790 */
2791static int
2792boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state,
2793 uint32_t slot, struct image_header *hdr,
2794 uint32_t src_sz, uint32_t img_dst)
2795{
2796 /* The flow for the decryption and copy of the image is as follows :
2797 * 1. The whole image is copied to the RAM (header + payload + TLV).
2798 * 2. The encryption key is loaded from the TLV in flash.
2799 * 3. The image is then decrypted chunk by chunk in RAM (1 chunk
2800 * is 1024 bytes). Only the payload section is decrypted.
2801 * 4. The image is authenticated in RAM.
2802 */
2803 const struct flash_area *fap_src = NULL;
2804 struct boot_status bs;
2805 uint32_t blk_off;
2806 uint32_t tlv_off;
2807 uint32_t blk_sz;
2808 uint32_t bytes_copied = hdr->ih_hdr_size;
2809 uint32_t chunk_sz;
2810 uint32_t max_sz = 1024;
2811 uint16_t idx;
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002812 uint8_t * cur_dst;
2813 int area_id;
2814 int rc;
2815 uint8_t * ram_dst = (void *)(IMAGE_RAM_BASE + img_dst);
2816
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002817 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2818 rc = flash_area_open(area_id, &fap_src);
2819 if (rc != 0){
2820 return BOOT_EFLASH;
2821 }
2822
2823 tlv_off = BOOT_TLV_OFF(hdr);
2824
2825 /* Copying the whole image in RAM */
2826 rc = flash_area_read(fap_src, 0, ram_dst, src_sz);
2827 if (rc != 0) {
2828 goto done;
2829 }
2830
Dominik Ermel7f9ac972024-07-12 19:21:40 +00002831 rc = boot_enc_load(BOOT_CURR_ENC(state), slot, hdr, fap_src, &bs);
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002832 if (rc < 0) {
2833 goto done;
2834 }
2835
2836 /* if rc > 0 then the key has already been loaded */
2837 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), slot, &bs)) {
2838 goto done;
2839 }
2840
2841 /* Starting at the end of the header as the header section is not encrypted */
2842 while (bytes_copied < tlv_off) { /* TLV section copied previously */
2843 if (src_sz - bytes_copied > max_sz) {
2844 chunk_sz = max_sz;
2845 } else {
2846 chunk_sz = src_sz - bytes_copied;
2847 }
2848
2849 cur_dst = ram_dst + bytes_copied;
2850 blk_sz = chunk_sz;
2851 idx = 0;
Dominik Ermeld09112a2024-07-18 16:43:57 +00002852 blk_off = ((bytes_copied) - hdr->ih_hdr_size) & 0xf;
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002853 if (bytes_copied + chunk_sz > tlv_off) {
2854 /* Going over TLV section
2855 * Part of the chunk is encrypted payload */
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002856 blk_sz = tlv_off - (bytes_copied);
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002857 }
Dominik Ermel3f112862024-07-17 14:43:05 +00002858 boot_encrypt(BOOT_CURR_ENC(state), slot,
Dominik Ermeld09112a2024-07-18 16:43:57 +00002859 (bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
2860 blk_off, cur_dst);
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002861
2862 bytes_copied += chunk_sz;
2863 }
2864 rc = 0;
2865
2866done:
2867 flash_area_close(fap_src);
2868
2869 return rc;
2870}
2871
2872#endif /* MCUBOOT_ENC_IMAGES */
Tamas Banfe031092020-09-10 17:32:39 +02002873/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002874 * Copies a slot of the current image into SRAM.
Tamas Banfe031092020-09-10 17:32:39 +02002875 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002876 * @param state Boot loader status information.
Tamas Banfe031092020-09-10 17:32:39 +02002877 * @param slot The flash slot of the image to be copied to SRAM.
2878 * @param img_dst The address at which the image needs to be copied to
2879 * SRAM.
2880 * @param img_sz The size of the image that needs to be copied to SRAM.
2881 *
2882 * @return 0 on success; nonzero on failure.
2883 */
2884static int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002885boot_copy_image_to_sram(struct boot_loader_state *state, int slot,
2886 uint32_t img_dst, uint32_t img_sz)
Tamas Banfe031092020-09-10 17:32:39 +02002887{
2888 int rc;
2889 const struct flash_area *fap_src = NULL;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002890 int area_id;
Tamas Banfe031092020-09-10 17:32:39 +02002891
Mark Horvathccaf7f82021-01-04 18:16:42 +01002892#if (BOOT_IMAGE_NUMBER == 1)
2893 (void)state;
2894#endif
2895
2896 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2897
2898 rc = flash_area_open(area_id, &fap_src);
Tamas Banfe031092020-09-10 17:32:39 +02002899 if (rc != 0) {
2900 return BOOT_EFLASH;
2901 }
2902
2903 /* Direct copy from flash to its new location in SRAM. */
David Brown9bd7f902021-05-26 16:31:14 -06002904 rc = flash_area_read(fap_src, 0, (void *)(IMAGE_RAM_BASE + img_dst), img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002905 if (rc != 0) {
Antonio de Angelis48547002023-04-14 09:47:09 +01002906 BOOT_LOG_INF("Error whilst copying image %d from Flash to SRAM: %d",
2907 BOOT_CURR_IMG(state), rc);
Tamas Banfe031092020-09-10 17:32:39 +02002908 }
2909
2910 flash_area_close(fap_src);
2911
2912 return rc;
2913}
2914
Mark Horvathccaf7f82021-01-04 18:16:42 +01002915#if (BOOT_IMAGE_NUMBER > 1)
Tamas Banfe031092020-09-10 17:32:39 +02002916/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002917 * Checks if two memory regions (A and B) are overlap or not.
Tamas Banfe031092020-09-10 17:32:39 +02002918 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002919 * @param start_a Start of the A region.
2920 * @param end_a End of the A region.
2921 * @param start_b Start of the B region.
2922 * @param end_b End of the B region.
Tamas Banfe031092020-09-10 17:32:39 +02002923 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002924 * @return true if there is overlap; false otherwise.
2925 */
2926static bool
2927do_regions_overlap(uint32_t start_a, uint32_t end_a,
2928 uint32_t start_b, uint32_t end_b)
2929{
2930 if (start_b > end_a) {
2931 return false;
2932 } else if (start_b >= start_a) {
2933 return true;
2934 } else if (end_b > start_a) {
2935 return true;
2936 }
2937
2938 return false;
2939}
2940
2941/**
2942 * Checks if the image we want to load to memory overlap with an already
2943 * ramloaded image.
2944 *
Raef Colesfe57e7d2021-10-15 11:07:09 +01002945 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002946 *
2947 * @return 0 if there is no overlap; nonzero otherwise.
Tamas Banfe031092020-09-10 17:32:39 +02002948 */
2949static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002950boot_check_ram_load_overlapping(struct boot_loader_state *state)
Tamas Banfe031092020-09-10 17:32:39 +02002951{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002952 uint32_t i;
2953
2954 uint32_t start_a;
2955 uint32_t end_a;
2956 uint32_t start_b;
2957 uint32_t end_b;
Raef Colesfe57e7d2021-10-15 11:07:09 +01002958 uint32_t image_id_to_check = BOOT_CURR_IMG(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002959
Raef Colesfe57e7d2021-10-15 11:07:09 +01002960 start_a = state->slot_usage[image_id_to_check].img_dst;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002961 /* Safe to add here, values are already verified in
2962 * boot_verify_ram_load_address() */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002963 end_a = start_a + state->slot_usage[image_id_to_check].img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002964
2965 for (i = 0; i < BOOT_IMAGE_NUMBER; i++) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002966 if (state->slot_usage[i].active_slot == NO_ACTIVE_SLOT
Mark Horvathccaf7f82021-01-04 18:16:42 +01002967 || i == image_id_to_check) {
2968 continue;
2969 }
2970
Raef Colesfe57e7d2021-10-15 11:07:09 +01002971 start_b = state->slot_usage[i].img_dst;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002972 /* Safe to add here, values are already verified in
2973 * boot_verify_ram_load_address() */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002974 end_b = start_b + state->slot_usage[i].img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002975
2976 if (do_regions_overlap(start_a, end_a, start_b, end_b)) {
2977 return -1;
2978 }
2979 }
2980
2981 return 0;
2982}
2983#endif
2984
2985/**
2986 * Loads the active slot of the current image into SRAM. The load address and
2987 * image size is extracted from the image header.
2988 *
2989 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002990 *
2991 * @return 0 on success; nonzero on failure.
2992 */
2993static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002994boot_load_image_to_sram(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002995{
2996 uint32_t active_slot;
2997 struct image_header *hdr = NULL;
2998 uint32_t img_dst;
2999 uint32_t img_sz;
Tamas Banfe031092020-09-10 17:32:39 +02003000 int rc;
3001
Raef Colesfe57e7d2021-10-15 11:07:09 +01003002 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003003 hdr = boot_img_hdr(state, active_slot);
3004
Tamas Banfe031092020-09-10 17:32:39 +02003005 if (hdr->ih_flags & IMAGE_F_RAM_LOAD) {
3006
Mark Horvathccaf7f82021-01-04 18:16:42 +01003007 img_dst = hdr->ih_load_addr;
Tamas Banfe031092020-09-10 17:32:39 +02003008
Mark Horvathccaf7f82021-01-04 18:16:42 +01003009 rc = boot_read_image_size(state, active_slot, &img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02003010 if (rc != 0) {
3011 return rc;
3012 }
3013
Raef Colesfe57e7d2021-10-15 11:07:09 +01003014 state->slot_usage[BOOT_CURR_IMG(state)].img_dst = img_dst;
3015 state->slot_usage[BOOT_CURR_IMG(state)].img_sz = img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003016
Raef Colesfe57e7d2021-10-15 11:07:09 +01003017 rc = boot_verify_ram_load_address(state);
Tamas Banfe031092020-09-10 17:32:39 +02003018 if (rc != 0) {
Antonio de Angelisba5fb1c2022-10-11 10:10:37 +01003019 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 +02003020 return rc;
3021 }
3022
Mark Horvathccaf7f82021-01-04 18:16:42 +01003023#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesfe57e7d2021-10-15 11:07:09 +01003024 rc = boot_check_ram_load_overlapping(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003025 if (rc != 0) {
Antonio de Angelisba5fb1c2022-10-11 10:10:37 +01003026 BOOT_LOG_INF("Image %d RAM loading to address 0x%x would overlap with\
3027 another image.", BOOT_CURR_IMG(state), img_dst);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003028 return rc;
3029 }
3030#endif
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00003031#ifdef MCUBOOT_ENC_IMAGES
3032 /* decrypt image if encrypted and copy it to RAM */
3033 if (IS_ENCRYPTED(hdr)) {
3034 rc = boot_decrypt_and_copy_image_to_sram(state, active_slot, hdr, img_sz, img_dst);
3035 } else {
3036 rc = boot_copy_image_to_sram(state, active_slot, img_dst, img_sz);
3037 }
3038#else
Tamas Banfe031092020-09-10 17:32:39 +02003039 /* Copy image to the load address from where it currently resides in
3040 * flash.
3041 */
Mark Horvathccaf7f82021-01-04 18:16:42 +01003042 rc = boot_copy_image_to_sram(state, active_slot, img_dst, img_sz);
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00003043#endif
Tamas Banfe031092020-09-10 17:32:39 +02003044 if (rc != 0) {
Antonio de Angelisba5fb1c2022-10-11 10:10:37 +01003045 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 +02003046 } else {
Antonio de Angelisba5fb1c2022-10-11 10:10:37 +01003047 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 +02003048 }
3049 } else {
3050 /* Only images that support IMAGE_F_RAM_LOAD are allowed if
3051 * MCUBOOT_RAM_LOAD is set.
3052 */
3053 rc = BOOT_EBADIMAGE;
3054 }
3055
Mark Horvathccaf7f82021-01-04 18:16:42 +01003056 if (rc != 0) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01003057 state->slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
3058 state->slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003059 }
3060
Tamas Banfe031092020-09-10 17:32:39 +02003061 return rc;
3062}
3063
3064/**
3065 * Removes an image from SRAM, by overwriting it with zeros.
3066 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01003067 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01003068 *
3069 * @return 0 on success; nonzero on failure.
3070 */
3071static inline int
Raef Colesfe57e7d2021-10-15 11:07:09 +01003072boot_remove_image_from_sram(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003073{
David Brown695e5912021-05-24 16:58:01 -06003074 (void)state;
3075
Antonio de Angelisba5fb1c2022-10-11 10:10:37 +01003076 BOOT_LOG_INF("Removing image %d from SRAM at address 0x%x",
3077 BOOT_CURR_IMG(state),
Raef Colesfe57e7d2021-10-15 11:07:09 +01003078 state->slot_usage[BOOT_CURR_IMG(state)].img_dst);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003079
Raef Colesfe57e7d2021-10-15 11:07:09 +01003080 memset((void*)(IMAGE_RAM_BASE + state->slot_usage[BOOT_CURR_IMG(state)].img_dst),
3081 0, state->slot_usage[BOOT_CURR_IMG(state)].img_sz);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003082
Raef Colesfe57e7d2021-10-15 11:07:09 +01003083 state->slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
3084 state->slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003085
3086 return 0;
3087}
3088
3089/**
3090 * Removes an image from flash by erasing the corresponding flash area
3091 *
3092 * @param state Boot loader status information.
3093 * @param slot The flash slot of the image to be erased.
Tamas Banfe031092020-09-10 17:32:39 +02003094 *
3095 * @return 0 on success; nonzero on failure.
3096 */
3097static inline int
Mark Horvathccaf7f82021-01-04 18:16:42 +01003098boot_remove_image_from_flash(struct boot_loader_state *state, uint32_t slot)
Tamas Banfe031092020-09-10 17:32:39 +02003099{
Mark Horvathccaf7f82021-01-04 18:16:42 +01003100 int area_id;
3101 int rc;
3102 const struct flash_area *fap;
Tamas Banfe031092020-09-10 17:32:39 +02003103
David Brown695e5912021-05-24 16:58:01 -06003104 (void)state;
3105
Mark Horvathccaf7f82021-01-04 18:16:42 +01003106 BOOT_LOG_INF("Removing image %d slot %d from flash", BOOT_CURR_IMG(state),
3107 slot);
3108 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
3109 rc = flash_area_open(area_id, &fap);
3110 if (rc == 0) {
Dominik Ermel260ae092021-04-23 05:38:45 +00003111 flash_area_erase(fap, 0, flash_area_get_size(fap));
Dominik Ermel245de812022-09-02 13:39:23 +00003112 flash_area_close(fap);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003113 }
3114
3115 return rc;
Tamas Banfe031092020-09-10 17:32:39 +02003116}
3117#endif /* MCUBOOT_RAM_LOAD */
3118
Mark Horvathccaf7f82021-01-04 18:16:42 +01003119
3120/**
3121 * Tries to load a slot for all the images with validation.
3122 *
3123 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01003124 *
3125 * @return 0 on success; nonzero on failure.
3126 */
Michael Grand5047f032022-11-24 16:49:56 +01003127fih_ret
Raef Colesfe57e7d2021-10-15 11:07:09 +01003128boot_load_and_validate_images(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003129{
3130 uint32_t active_slot;
3131 int rc;
Michael Grand5047f032022-11-24 16:49:56 +01003132 fih_ret fih_rc;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003133
3134 /* Go over all the images and try to load one */
3135 IMAGES_ITER(BOOT_CURR_IMG(state)) {
3136 /* All slots tried until a valid image found. Breaking from this loop
3137 * means that a valid image found or already loaded. If no slot is
3138 * found the function returns with error code. */
3139 while (true) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01003140 /* Go over all the slots and try to load one */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003141 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003142 if (active_slot != NO_ACTIVE_SLOT){
3143 /* A slot is already active, go to next image. */
3144 break;
David Vinczee574f2d2020-07-10 11:42:03 +02003145 }
David Vincze505fba22020-10-22 13:53:29 +02003146
Raef Colesfe57e7d2021-10-15 11:07:09 +01003147 active_slot = find_slot_with_highest_version(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003148 if (active_slot == NO_ACTIVE_SLOT) {
3149 BOOT_LOG_INF("No slot to load for image %d",
3150 BOOT_CURR_IMG(state));
3151 FIH_RET(FIH_FAILURE);
3152 }
3153
3154 /* Save the number of the active slot. */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003155 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003156
Raef Colesf11de642021-10-15 11:11:56 +01003157#if BOOT_IMAGE_NUMBER > 1
3158 if (state->img_mask[BOOT_CURR_IMG(state)]) {
3159 continue;
3160 }
3161#endif
3162
Mark Horvathccaf7f82021-01-04 18:16:42 +01003163#ifdef MCUBOOT_DIRECT_XIP
Raef Colesfe57e7d2021-10-15 11:07:09 +01003164 rc = boot_rom_address_check(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003165 if (rc != 0) {
3166 /* The image is placed in an unsuitable slot. */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003167 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3168 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003169 continue;
3170 }
George Becksteind4d90f82021-05-11 02:00:00 -04003171
David Vincze505fba22020-10-22 13:53:29 +02003172#ifdef MCUBOOT_DIRECT_XIP_REVERT
Raef Colesfe57e7d2021-10-15 11:07:09 +01003173 rc = boot_select_or_erase(state);
David Vincze505fba22020-10-22 13:53:29 +02003174 if (rc != 0) {
3175 /* The selected image slot has been erased. */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003176 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3177 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
David Vincze505fba22020-10-22 13:53:29 +02003178 continue;
3179 }
3180#endif /* MCUBOOT_DIRECT_XIP_REVERT */
David Vincze1c456242021-06-29 15:25:24 +02003181#endif /* MCUBOOT_DIRECT_XIP */
David Vincze505fba22020-10-22 13:53:29 +02003182
Tamas Banfe031092020-09-10 17:32:39 +02003183#ifdef MCUBOOT_RAM_LOAD
3184 /* Image is first loaded to RAM and authenticated there in order to
3185 * prevent TOCTOU attack during image copy. This could be applied
3186 * when loading images from external (untrusted) flash to internal
3187 * (trusted) RAM and image is authenticated before copying.
3188 */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003189 rc = boot_load_image_to_sram(state);
Tamas Banfe031092020-09-10 17:32:39 +02003190 if (rc != 0 ) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01003191 /* Image cannot be ramloaded. */
3192 boot_remove_image_from_flash(state, active_slot);
Raef Colesfe57e7d2021-10-15 11:07:09 +01003193 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3194 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Tamas Banfe031092020-09-10 17:32:39 +02003195 continue;
Tamas Banfe031092020-09-10 17:32:39 +02003196 }
3197#endif /* MCUBOOT_RAM_LOAD */
Mark Horvathccaf7f82021-01-04 18:16:42 +01003198
3199 FIH_CALL(boot_validate_slot, fih_rc, state, active_slot, NULL);
Michael Grand5047f032022-11-24 16:49:56 +01003200 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01003201 /* Image is invalid. */
Tamas Banfe031092020-09-10 17:32:39 +02003202#ifdef MCUBOOT_RAM_LOAD
Raef Colesfe57e7d2021-10-15 11:07:09 +01003203 boot_remove_image_from_sram(state);
Tamas Banfe031092020-09-10 17:32:39 +02003204#endif /* MCUBOOT_RAM_LOAD */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003205 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3206 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003207 continue;
David Vincze505fba22020-10-22 13:53:29 +02003208 }
Mark Horvathccaf7f82021-01-04 18:16:42 +01003209
3210 /* Valid image loaded from a slot, go to next image. */
3211 break;
3212 }
3213 }
3214
3215 FIH_RET(FIH_SUCCESS);
3216}
3217
3218/**
3219 * Updates the security counter for the current image.
3220 *
3221 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01003222 *
3223 * @return 0 on success; nonzero on failure.
3224 */
3225static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01003226boot_update_hw_rollback_protection(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003227{
3228#ifdef MCUBOOT_HW_ROLLBACK_PROT
3229 int rc;
3230
3231 /* Update the stored security counter with the newer (active) image's
3232 * security counter value.
3233 */
David Vincze1c456242021-06-29 15:25:24 +02003234#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003235 /* When the 'revert' mechanism is enabled in direct-xip mode, the
3236 * security counter can be increased only after reboot, if the image
3237 * has been confirmed at runtime (the image_ok flag has been set).
3238 * This way a 'revert' can be performed when it's necessary.
3239 */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003240 if (state->slot_usage[BOOT_CURR_IMG(state)].swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze505fba22020-10-22 13:53:29 +02003241#endif
Sherry Zhang50b06ae2021-07-09 15:22:51 +08003242 rc = boot_update_security_counter(BOOT_CURR_IMG(state),
Raef Colesfe57e7d2021-10-15 11:07:09 +01003243 state->slot_usage[BOOT_CURR_IMG(state)].active_slot,
3244 boot_img_hdr(state, state->slot_usage[BOOT_CURR_IMG(state)].active_slot));
David Vinczee574f2d2020-07-10 11:42:03 +02003245 if (rc != 0) {
Antonio de Angelisba5fb1c2022-10-11 10:10:37 +01003246 BOOT_LOG_ERR("Security counter update failed after image %d validation.", BOOT_CURR_IMG(state));
Mark Horvathccaf7f82021-01-04 18:16:42 +01003247 return rc;
David Vinczee574f2d2020-07-10 11:42:03 +02003248 }
David Vincze1c456242021-06-29 15:25:24 +02003249#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003250 }
3251#endif
David Vinczee574f2d2020-07-10 11:42:03 +02003252
Mark Horvathccaf7f82021-01-04 18:16:42 +01003253 return 0;
David Vinczee574f2d2020-07-10 11:42:03 +02003254
Mark Horvathccaf7f82021-01-04 18:16:42 +01003255#else /* MCUBOOT_HW_ROLLBACK_PROT */
3256 (void) (state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003257 return 0;
3258#endif
3259}
3260
Michael Grand5047f032022-11-24 16:49:56 +01003261fih_ret
Mark Horvathccaf7f82021-01-04 18:16:42 +01003262context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
3263{
Mark Horvathccaf7f82021-01-04 18:16:42 +01003264 int rc;
Michael Grand5047f032022-11-24 16:49:56 +01003265 FIH_DECLARE(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003266
Raef Colesfe57e7d2021-10-15 11:07:09 +01003267 rc = boot_get_slot_usage(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003268 if (rc != 0) {
David Vinczee574f2d2020-07-10 11:42:03 +02003269 goto out;
3270 }
3271
Mark Horvathccaf7f82021-01-04 18:16:42 +01003272#if (BOOT_IMAGE_NUMBER > 1)
3273 while (true) {
3274#endif
Raef Colesfe57e7d2021-10-15 11:07:09 +01003275 FIH_CALL(boot_load_and_validate_images, fih_rc, state);
Michael Grand5047f032022-11-24 16:49:56 +01003276 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
3277 FIH_SET(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003278 goto out;
3279 }
3280
3281#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesfe57e7d2021-10-15 11:07:09 +01003282 rc = boot_verify_dependencies(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003283 if (rc != 0) {
3284 /* Dependency check failed for an image, it has been removed from
3285 * SRAM in case of MCUBOOT_RAM_LOAD strategy, and set to
3286 * unavailable. Try to load an image from another slot.
3287 */
3288 continue;
3289 }
3290 /* Dependency check was successful. */
3291 break;
3292 }
3293#endif
3294
3295 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01003296#if BOOT_IMAGE_NUMBER > 1
3297 if (state->img_mask[BOOT_CURR_IMG(state)]) {
3298 continue;
3299 }
3300#endif
Raef Colesfe57e7d2021-10-15 11:07:09 +01003301 rc = boot_update_hw_rollback_protection(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003302 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01003303 FIH_SET(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003304 goto out;
3305 }
3306
Jamie McCrae3016d002023-03-14 12:35:51 +00003307 rc = boot_add_shared_data(state, (uint8_t)state->slot_usage[BOOT_CURR_IMG(state)].active_slot);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003308 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01003309 FIH_SET(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003310 goto out;
3311 }
3312 }
3313
3314 /* All image loaded successfully. */
3315#ifdef MCUBOOT_HAVE_LOGGING
Raef Colesfe57e7d2021-10-15 11:07:09 +01003316 print_loaded_images(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003317#endif
3318
Raef Colesfe57e7d2021-10-15 11:07:09 +01003319 fill_rsp(state, rsp);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003320
David Vinczee574f2d2020-07-10 11:42:03 +02003321out:
Mark Horvathccaf7f82021-01-04 18:16:42 +01003322 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01003323
Michael Grand5047f032022-11-24 16:49:56 +01003324 if (rc != 0) {
3325 FIH_SET(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003326 }
Raef Colese8fe6cf2020-05-26 13:07:40 +01003327
Mark Horvathccaf7f82021-01-04 18:16:42 +01003328 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02003329}
Tamas Banfe031092020-09-10 17:32:39 +02003330#endif /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02003331
3332/**
Raef Colese8fe6cf2020-05-26 13:07:40 +01003333 * Prepares the booting process. This function moves images around in flash as
David Vinczee574f2d2020-07-10 11:42:03 +02003334 * appropriate, and tells you what address to boot from.
3335 *
3336 * @param rsp On success, indicates how booting should occur.
3337 *
Raef Colese8fe6cf2020-05-26 13:07:40 +01003338 * @return FIH_SUCCESS on success; nonzero on failure.
David Vinczee574f2d2020-07-10 11:42:03 +02003339 */
Michael Grand5047f032022-11-24 16:49:56 +01003340fih_ret
David Vinczee574f2d2020-07-10 11:42:03 +02003341boot_go(struct boot_rsp *rsp)
3342{
Michael Grand5047f032022-11-24 16:49:56 +01003343 FIH_DECLARE(fih_rc, FIH_FAILURE);
Raef Colesf11de642021-10-15 11:11:56 +01003344
3345 boot_state_clear(NULL);
3346
Raef Colese8fe6cf2020-05-26 13:07:40 +01003347 FIH_CALL(context_boot_go, fih_rc, &boot_data, rsp);
3348 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02003349}
Raef Colesf11de642021-10-15 11:11:56 +01003350
3351/**
3352 * Prepares the booting process, considering only a single image. This function
3353 * moves images around in flash as appropriate, and tells you what address to
3354 * boot from.
3355 *
3356 * @param rsp On success, indicates how booting should occur.
3357 *
3358 * @param image_id The image ID to prepare the boot process for.
3359 *
3360 * @return FIH_SUCCESS on success; nonzero on failure.
3361 */
Michael Grand5047f032022-11-24 16:49:56 +01003362fih_ret
Raef Colesf11de642021-10-15 11:11:56 +01003363boot_go_for_image_id(struct boot_rsp *rsp, uint32_t image_id)
3364{
Michael Grand5047f032022-11-24 16:49:56 +01003365 FIH_DECLARE(fih_rc, FIH_FAILURE);
Raef Colesf11de642021-10-15 11:11:56 +01003366
3367 if (image_id >= BOOT_IMAGE_NUMBER) {
3368 FIH_RET(FIH_FAILURE);
3369 }
3370
3371#if BOOT_IMAGE_NUMBER > 1
3372 memset(&boot_data.img_mask, 1, BOOT_IMAGE_NUMBER);
3373 boot_data.img_mask[image_id] = 0;
3374#endif
3375
3376 FIH_CALL(context_boot_go, fih_rc, &boot_data, rsp);
3377 FIH_RET(fih_rc);
3378}
3379
3380/**
3381 * Clears the boot state, so that previous operations have no effect on new
3382 * ones.
3383 *
3384 * @param state The state that should be cleared. If the value
3385 * is NULL, the default bootloader state will be
3386 * cleared.
3387 */
3388void boot_state_clear(struct boot_loader_state *state)
3389{
3390 if (state != NULL) {
3391 memset(state, 0, sizeof(struct boot_loader_state));
3392 } else {
3393 memset(&boot_data, 0, sizeof(struct boot_loader_state));
3394 }
3395}
Jamie McCrae4f1ab9e2024-07-26 12:29:24 +01003396
Jamie McCraee261b282024-07-26 11:48:19 +01003397#if defined(MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO)
Jamie McCrae4f1ab9e2024-07-26 12:29:24 +01003398/**
Jamie McCraee261b282024-07-26 11:48:19 +01003399 * Reads image data to find out the maximum application sizes. Only needs to
3400 * be called in serial recovery mode, as the state informatio is unpopulated
3401 * at that time
3402 */
3403static void boot_fetch_slot_state_sizes(void)
3404{
3405 struct sector_buffer_t sector_buffers;
3406 size_t slot;
3407 int rc = -1;
3408 int fa_id;
3409 int image_index;
3410
3411 boot_get_sector_buffers(&sector_buffers);
3412
3413 IMAGES_ITER(BOOT_CURR_IMG(&boot_data)) {
3414 int max_size = 0;
3415
3416 image_index = BOOT_CURR_IMG(&boot_data);
3417
3418 BOOT_IMG(&boot_data, BOOT_PRIMARY_SLOT).sectors =
3419 &sector_buffers.primary[image_index];
3420 BOOT_IMG(&boot_data, BOOT_SECONDARY_SLOT).sectors =
3421 &sector_buffers.secondary[image_index];
3422#if MCUBOOT_SWAP_USING_SCRATCH
3423 boot_data.scratch.sectors = sector_buffers.scratch;;
3424#endif
3425
3426 /* Open primary and secondary image areas for the duration
3427 * of this call.
3428 */
3429 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
3430 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
3431 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot));
3432 assert(rc == 0);
3433
3434 if (rc != 0) {
3435 goto finish;
3436 }
3437 }
3438
3439#if MCUBOOT_SWAP_USING_SCRATCH
3440 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
3441 &BOOT_SCRATCH_AREA(&boot_data));
3442 assert(rc == 0);
3443
3444 if (rc != 0) {
3445 goto finish;
3446 }
3447#endif
3448
3449 /* Determine the sector layout of the image slots and scratch area. */
3450 rc = boot_read_sectors(&boot_data);
3451
3452 if (rc == 0) {
3453 max_size = app_max_size(&boot_data);
3454
3455 if (max_size > 0) {
3456 image_max_sizes[image_index].calculated = true;
3457 image_max_sizes[image_index].max_size = max_size;
3458 }
3459 }
3460 }
3461
3462finish:
3463 close_all_flash_areas(&boot_data);
3464 memset(&boot_data, 0x00, sizeof(boot_data));
3465}
3466#endif
3467
3468#if defined(MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO) || defined(MCUBOOT_DATA_SHARING)
3469/**
3470 * Fetches the maximum allowed size of the image
3471 */
Jamie McCrae4f1ab9e2024-07-26 12:29:24 +01003472const struct image_max_size *boot_get_max_app_size(void)
3473{
Jamie McCraee261b282024-07-26 11:48:19 +01003474#if defined(MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO)
3475 uint8_t i = 0;
3476
3477 while (i < BOOT_IMAGE_NUMBER) {
3478 if (image_max_sizes[i].calculated == true) {
3479 break;
3480 }
3481
3482 ++i;
3483 }
3484
3485 if (i == BOOT_IMAGE_NUMBER) {
3486 /* Information not available, need to fetch it */
3487 boot_fetch_slot_state_sizes();
3488 }
3489#endif
3490
Jamie McCrae4f1ab9e2024-07-26 12:29:24 +01003491 return image_max_sizes;
3492}
3493#endif