Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1 | /* |
David Brown | aac7111 | 2020-02-03 16:13:42 -0700 | [diff] [blame] | 2 | * SPDX-License-Identifier: Apache-2.0 |
| 3 | * |
| 4 | * Copyright (c) 2016-2020 Linaro LTD |
| 5 | * Copyright (c) 2016-2019 JUUL Labs |
Antonio de Angelis | ba5fb1c | 2022-10-11 10:10:37 +0100 | [diff] [blame] | 6 | * Copyright (c) 2019-2023 Arm Limited |
Dominik Ermel | 6f7f873 | 2024-02-01 11:59:24 +0000 | [diff] [blame] | 7 | * Copyright (c) 2024 Nordic Semiconductor ASA |
David Brown | aac7111 | 2020-02-03 16:13:42 -0700 | [diff] [blame] | 8 | * |
| 9 | * Original license: |
| 10 | * |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 11 | * 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 Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 34 | #include <stddef.h> |
David Brown | 52eee56 | 2017-07-05 11:25:09 -0600 | [diff] [blame] | 35 | #include <stdbool.h> |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 36 | #include <inttypes.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <string.h> |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 39 | #include "bootutil/bootutil.h" |
Kristine Jassmann | 73c38c6 | 2021-02-03 16:56:14 +0000 | [diff] [blame] | 40 | #include "bootutil/bootutil_public.h" |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 41 | #include "bootutil/image.h" |
| 42 | #include "bootutil_priv.h" |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 43 | #include "swap_priv.h" |
Marti Bolivar | fd20c76 | 2017-02-07 16:52:50 -0500 | [diff] [blame] | 44 | #include "bootutil/bootutil_log.h" |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 45 | #include "bootutil/security_cnt.h" |
David Vincze | 1cf11b5 | 2020-03-24 07:51:09 +0100 | [diff] [blame] | 46 | #include "bootutil/boot_record.h" |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 47 | #include "bootutil/fault_injection_hardening.h" |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 48 | #include "bootutil/ramload.h" |
Andrzej Puzdrowski | b8f3969 | 2021-07-02 15:05:37 +0200 | [diff] [blame] | 49 | #include "bootutil/boot_hooks.h" |
Jamie McCrae | 56cb610 | 2022-03-23 11:57:03 +0000 | [diff] [blame] | 50 | #include "bootutil/mcuboot_status.h" |
Marti Bolivar | fd20c76 | 2017-02-07 16:52:50 -0500 | [diff] [blame] | 51 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 52 | #ifdef MCUBOOT_ENC_IMAGES |
| 53 | #include "bootutil/enc_key.h" |
| 54 | #endif |
| 55 | |
Carlos Falgueras García | 391b197 | 2021-06-21 16:58:07 +0200 | [diff] [blame] | 56 | #if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD) |
| 57 | #include <os/os_malloc.h> |
| 58 | #endif |
| 59 | |
Fabio Utzig | ba1fbe6 | 2017-07-21 14:01:20 -0300 | [diff] [blame] | 60 | #include "mcuboot_config/mcuboot_config.h" |
Fabio Utzig | eed80b6 | 2017-06-10 08:03:05 -0300 | [diff] [blame] | 61 | |
Carlos Falgueras García | a4b4b0f | 2021-06-22 10:00:22 +0200 | [diff] [blame] | 62 | BOOT_LOG_MODULE_DECLARE(mcuboot); |
Emanuele Di Santo | 9f1933d | 2018-11-20 10:59:59 +0100 | [diff] [blame] | 63 | |
Marti Bolivar | 9b1f8bb | 2017-06-12 15:24:13 -0400 | [diff] [blame] | 64 | static struct boot_loader_state boot_data; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 65 | |
Jamie McCrae | e261b28 | 2024-07-26 11:48:19 +0100 | [diff] [blame] | 66 | #if defined(MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO) || defined(MCUBOOT_DATA_SHARING) |
Jamie McCrae | 4f1ab9e | 2024-07-26 12:29:24 +0100 | [diff] [blame] | 67 | static struct image_max_size image_max_sizes[BOOT_IMAGE_NUMBER] = {0}; |
| 68 | #endif |
| 69 | |
Jamie McCrae | e261b28 | 2024-07-26 11:48:19 +0100 | [diff] [blame] | 70 | #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 | */ |
| 74 | struct 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 Utzig | abec073 | 2019-07-31 08:40:22 -0300 | [diff] [blame] | 83 | #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 Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 89 | /* |
| 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 Jassmann | 73c38c6 | 2021-02-03 16:56:14 +0000 | [diff] [blame] | 102 | #if BOOT_MAX_ALIGN > 1024 |
| 103 | #define BUF_SZ BOOT_MAX_ALIGN |
| 104 | #else |
| 105 | #define BUF_SZ 1024 |
| 106 | #endif |
| 107 | |
Dominik Ermel | aafcbad | 2024-02-29 20:40:20 +0000 | [diff] [blame] | 108 | #define NO_ACTIVE_SLOT UINT32_MAX |
| 109 | |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 110 | static int |
| 111 | boot_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 Puzdrowski | b8f3969 | 2021-07-02 15:05:37 +0200 | [diff] [blame] | 118 | 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 Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 124 | 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 Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 142 | /** |
| 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 | */ |
| 150 | static int |
| 151 | boot_add_shared_data(struct boot_loader_state *state, |
Jamie McCrae | 3016d00 | 2023-03-14 12:35:51 +0000 | [diff] [blame] | 152 | uint8_t active_slot) |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 153 | { |
| 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 McCrae | 3016d00 | 2023-03-14 12:35:51 +0000 | [diff] [blame] | 169 | BOOT_IMG_AREA(state, active_slot), |
Jamie McCrae | 4f1ab9e | 2024-07-26 12:29:24 +0100 | [diff] [blame] | 170 | active_slot, image_max_sizes); |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 171 | 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 Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 191 | * @param rsp boot_rsp struct to fill. |
| 192 | */ |
| 193 | static void |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 194 | fill_rsp(struct boot_loader_state *state, struct boot_rsp *rsp) |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 195 | { |
| 196 | uint32_t active_slot; |
| 197 | |
| 198 | #if (BOOT_IMAGE_NUMBER > 1) |
Raef Coles | f11de64 | 2021-10-15 11:11:56 +0100 | [diff] [blame] | 199 | /* Always boot from the first enabled image. */ |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 200 | BOOT_CURR_IMG(state) = 0; |
Raef Coles | f11de64 | 2021-10-15 11:11:56 +0100 | [diff] [blame] | 201 | IMAGES_ITER(BOOT_CURR_IMG(state)) { |
| 202 | if (!state->img_mask[BOOT_CURR_IMG(state)]) { |
| 203 | break; |
| 204 | } |
| 205 | } |
INFINEON\DovhalA | 94360d5 | 2023-01-18 17:21:56 +0200 | [diff] [blame] | 206 | /* At least one image must be active, otherwise skip the execution */ |
| 207 | if (BOOT_CURR_IMG(state) >= BOOT_IMAGE_NUMBER) { |
| 208 | return; |
| 209 | } |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 210 | #endif |
| 211 | |
| 212 | #if defined(MCUBOOT_DIRECT_XIP) || defined(MCUBOOT_RAM_LOAD) |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 213 | active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot; |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 214 | #else |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 215 | active_slot = BOOT_PRIMARY_SLOT; |
| 216 | #endif |
| 217 | |
Dominik Ermel | 260ae09 | 2021-04-23 05:38:45 +0000 | [diff] [blame] | 218 | rsp->br_flash_dev_id = flash_area_get_device_id(BOOT_IMG_AREA(state, active_slot)); |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 219 | 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 | */ |
| 228 | static void |
| 229 | close_all_flash_areas(struct boot_loader_state *state) |
| 230 | { |
| 231 | uint32_t slot; |
| 232 | |
| 233 | IMAGES_ITER(BOOT_CURR_IMG(state)) { |
Raef Coles | f11de64 | 2021-10-15 11:11:56 +0100 | [diff] [blame] | 234 | #if BOOT_IMAGE_NUMBER > 1 |
| 235 | if (state->img_mask[BOOT_CURR_IMG(state)]) { |
| 236 | continue; |
| 237 | } |
| 238 | #endif |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 239 | #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 Ermel | aafcbad | 2024-02-29 20:40:20 +0000 | [diff] [blame] | 248 | #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 | */ |
| 265 | static int |
| 266 | boot_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 | |
| 307 | static int |
| 308 | boot_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 | */ |
| 318 | static int |
| 319 | boot_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 | */ |
| 377 | static int |
| 378 | boot_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 |
| 416 | static inline int |
| 417 | boot_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 | */ |
| 429 | static int |
| 430 | boot_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 | */ |
| 467 | static int |
| 468 | boot_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 | |
| 525 | done: |
| 526 | flash_area_close(fap); |
| 527 | return rc; |
| 528 | } |
| 529 | |
| 530 | #endif /* (BOOT_IMAGE_NUMBER > 1) */ |
| 531 | |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 532 | #if !defined(MCUBOOT_DIRECT_XIP) |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 533 | /* |
| 534 | * Compute the total size of the given image. Includes the size of |
| 535 | * the TLVs. |
| 536 | */ |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 537 | #if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST) |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 538 | static int |
Fabio Utzig | d638b17 | 2019-08-09 10:38:05 -0300 | [diff] [blame] | 539 | boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size) |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 540 | { |
| 541 | const struct flash_area *fap; |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 542 | struct image_tlv_info info; |
Fabio Utzig | 233af7d | 2019-08-26 12:06:16 -0300 | [diff] [blame] | 543 | uint32_t off; |
Fabio Utzig | e52c08e | 2019-09-11 19:32:00 -0300 | [diff] [blame] | 544 | uint32_t protect_tlv_size; |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 545 | int area_id; |
| 546 | int rc; |
| 547 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 548 | #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 Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 553 | rc = flash_area_open(area_id, &fap); |
| 554 | if (rc != 0) { |
| 555 | rc = BOOT_EFLASH; |
| 556 | goto done; |
| 557 | } |
| 558 | |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 559 | 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 Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 563 | goto done; |
| 564 | } |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 565 | |
Fabio Utzig | e52c08e | 2019-09-11 19:32:00 -0300 | [diff] [blame] | 566 | 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 Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 582 | if (info.it_magic != IMAGE_TLV_INFO_MAGIC) { |
| 583 | rc = BOOT_EBADIMAGE; |
| 584 | goto done; |
| 585 | } |
| 586 | |
Fabio Utzig | e52c08e | 2019-09-11 19:32:00 -0300 | [diff] [blame] | 587 | *size = off + protect_tlv_size + info.it_tlv_tot; |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 588 | rc = 0; |
| 589 | |
| 590 | done: |
| 591 | flash_area_close(fap); |
Fabio Utzig | 2eebf11 | 2017-09-04 15:25:08 -0300 | [diff] [blame] | 592 | return rc; |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 593 | } |
Fabio Utzig | 36ec0e7 | 2017-09-05 08:10:33 -0300 | [diff] [blame] | 594 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 595 | |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 596 | #if !defined(MCUBOOT_RAM_LOAD) |
David Brown | ab44918 | 2019-11-15 09:32:52 -0700 | [diff] [blame] | 597 | static uint32_t |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 598 | boot_write_sz(struct boot_loader_state *state) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 599 | { |
David Brown | ab44918 | 2019-11-15 09:32:52 -0700 | [diff] [blame] | 600 | uint32_t elem_sz; |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 601 | #if MCUBOOT_SWAP_USING_SCRATCH |
David Brown | ab44918 | 2019-11-15 09:32:52 -0700 | [diff] [blame] | 602 | uint32_t align; |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 603 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 604 | |
| 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 Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 609 | elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)); |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 610 | #if MCUBOOT_SWAP_USING_SCRATCH |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 611 | align = flash_area_align(BOOT_SCRATCH_AREA(state)); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 612 | if (align > elem_sz) { |
| 613 | elem_sz = align; |
| 614 | } |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 615 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 616 | |
| 617 | return elem_sz; |
| 618 | } |
| 619 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 620 | static int |
| 621 | boot_initialize_area(struct boot_loader_state *state, int flash_area) |
| 622 | { |
Dominik Ermel | 51c8d76 | 2021-05-24 15:34:01 +0000 | [diff] [blame] | 623 | uint32_t num_sectors = BOOT_MAX_IMG_SECTORS; |
| 624 | boot_sector_t *out_sectors; |
| 625 | uint32_t *out_num_sectors; |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 626 | 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 Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 636 | #if MCUBOOT_SWAP_USING_SCRATCH |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 637 | } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) { |
| 638 | out_sectors = state->scratch.sectors; |
| 639 | out_num_sectors = &state->scratch.num_sectors; |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 640 | #endif |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 641 | } else { |
| 642 | return BOOT_EFLASH; |
| 643 | } |
| 644 | |
Dominik Ermel | 51c8d76 | 2021-05-24 15:34:01 +0000 | [diff] [blame] | 645 | #ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 646 | rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors); |
Dominik Ermel | 51c8d76 | 2021-05-24 15:34:01 +0000 | [diff] [blame] | 647 | #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 Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 651 | if (rc != 0) { |
| 652 | return rc; |
| 653 | } |
| 654 | *out_num_sectors = num_sectors; |
| 655 | return 0; |
| 656 | } |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 657 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 658 | /** |
| 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 Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 662 | * function is used to populate the state. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 663 | */ |
| 664 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 665 | boot_read_sectors(struct boot_loader_state *state) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 666 | { |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 667 | uint8_t image_index; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 668 | int rc; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 669 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 670 | image_index = BOOT_CURR_IMG(state); |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 671 | |
| 672 | rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index)); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 673 | if (rc != 0) { |
| 674 | return BOOT_EFLASH; |
| 675 | } |
| 676 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 677 | rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index)); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 678 | if (rc != 0) { |
Andrzej Puzdrowski | 54b4ad9 | 2021-06-22 13:21:22 +0200 | [diff] [blame] | 679 | /* We need to differentiate from the primary image issue */ |
| 680 | return BOOT_EFLASH_SEC; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 681 | } |
| 682 | |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 683 | #if MCUBOOT_SWAP_USING_SCRATCH |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 684 | rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH); |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 685 | if (rc != 0) { |
| 686 | return BOOT_EFLASH; |
| 687 | } |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 688 | #endif |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 689 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 690 | BOOT_WRITE_SZ(state) = boot_write_sz(state); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 691 | |
| 692 | return 0; |
| 693 | } |
| 694 | |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 695 | void |
| 696 | boot_status_reset(struct boot_status *bs) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 697 | { |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 698 | #ifdef MCUBOOT_ENC_IMAGES |
Kristine Jassmann | 73c38c6 | 2021-02-03 16:56:14 +0000 | [diff] [blame] | 699 | memset(&bs->enckey, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_KEY_ALIGN_SIZE); |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 700 | #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 Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 709 | bs->op = BOOT_STATUS_OP_MOVE; |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 710 | bs->idx = BOOT_STATUS_IDX_0; |
| 711 | bs->state = BOOT_STATUS_STATE_0; |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 712 | bs->swap_type = BOOT_SWAP_TYPE_NONE; |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 713 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 714 | |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 715 | bool |
| 716 | boot_status_is_reset(const struct boot_status *bs) |
| 717 | { |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 718 | return (bs->op == BOOT_STATUS_OP_MOVE && |
| 719 | bs->idx == BOOT_STATUS_IDX_0 && |
| 720 | bs->state == BOOT_STATUS_STATE_0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 721 | } |
| 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 | */ |
| 731 | int |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 732 | boot_write_status(const struct boot_loader_state *state, struct boot_status *bs) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 733 | { |
| 734 | const struct flash_area *fap; |
| 735 | uint32_t off; |
| 736 | int area_id; |
Dominik Ermel | 9479af0 | 2021-10-19 10:06:44 +0000 | [diff] [blame] | 737 | int rc = 0; |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 738 | uint8_t buf[BOOT_MAX_ALIGN]; |
Gustavo Henrique Nihei | 4aa286d | 2021-11-24 14:54:56 -0300 | [diff] [blame] | 739 | uint32_t align; |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 740 | uint8_t erased_val; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 741 | |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 742 | /* NOTE: The first sector copied (that is the last sector on slot) contains |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 743 | * 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 Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 746 | */ |
| 747 | |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 748 | #if MCUBOOT_SWAP_USING_SCRATCH |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 749 | if (bs->use_scratch) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 750 | /* Write to scratch. */ |
| 751 | area_id = FLASH_AREA_IMAGE_SCRATCH; |
| 752 | } else { |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 753 | #endif |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 754 | /* Write to the primary slot. */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 755 | area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state)); |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 756 | #if MCUBOOT_SWAP_USING_SCRATCH |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 757 | } |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 758 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 759 | |
| 760 | rc = flash_area_open(area_id, &fap); |
| 761 | if (rc != 0) { |
Dominik Ermel | 9479af0 | 2021-10-19 10:06:44 +0000 | [diff] [blame] | 762 | return BOOT_EFLASH; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | off = boot_status_off(fap) + |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 766 | boot_status_internal_off(bs, BOOT_WRITE_SZ(state)); |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 767 | align = flash_area_align(fap); |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 768 | erased_val = flash_area_erased_val(fap); |
| 769 | memset(buf, erased_val, BOOT_MAX_ALIGN); |
David Brown | 9d72546 | 2017-01-23 15:50:58 -0700 | [diff] [blame] | 770 | buf[0] = bs->state; |
| 771 | |
Jamie McCrae | 35e9931 | 2024-01-03 07:37:55 +0000 | [diff] [blame] | 772 | 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 Brown | 9d72546 | 2017-01-23 15:50:58 -0700 | [diff] [blame] | 776 | rc = flash_area_write(fap, off, buf, align); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 777 | if (rc != 0) { |
| 778 | rc = BOOT_EFLASH; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 779 | } |
| 780 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 781 | flash_area_close(fap); |
Dominik Ermel | 9479af0 | 2021-10-19 10:06:44 +0000 | [diff] [blame] | 782 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 783 | return rc; |
| 784 | } |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 785 | #endif /* !MCUBOOT_RAM_LOAD */ |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 786 | #endif /* !MCUBOOT_DIRECT_XIP */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 787 | |
| 788 | /* |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 789 | * Validate image hash/signature and optionally the security counter in a slot. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 790 | */ |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 791 | static fih_ret |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 792 | boot_image_check(struct boot_loader_state *state, struct image_header *hdr, |
| 793 | const struct flash_area *fap, struct boot_status *bs) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 794 | { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 795 | TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ]; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 796 | int rc; |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 797 | FIH_DECLARE(fih_rc, FIH_FAILURE); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 798 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 799 | #if (BOOT_IMAGE_NUMBER == 1) |
| 800 | (void)state; |
| 801 | #endif |
| 802 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 803 | (void)bs; |
| 804 | (void)rc; |
Fabio Utzig | bc07793 | 2019-08-26 11:16:34 -0300 | [diff] [blame] | 805 | |
Hugo L'Hostis | db543e5 | 2021-03-09 18:00:31 +0000 | [diff] [blame] | 806 | /* 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 Ermel | 7f9ac97 | 2024-07-12 19:21:40 +0000 | [diff] [blame] | 809 | if (MUST_DECRYPT(fap, BOOT_CURR_IMG(state), hdr)) { |
| 810 | rc = boot_enc_load(BOOT_CURR_ENC(state), 1, hdr, fap, bs); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 811 | if (rc < 0) { |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 812 | FIH_RET(fih_rc); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 813 | } |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 814 | if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) { |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 815 | FIH_RET(fih_rc); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 816 | } |
| 817 | } |
Fabio Utzig | bc07793 | 2019-08-26 11:16:34 -0300 | [diff] [blame] | 818 | #endif |
| 819 | |
Dominik Ermel | 7f9ac97 | 2024-07-12 19:21:40 +0000 | [diff] [blame] | 820 | 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 Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 823 | |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 824 | FIH_RET(fih_rc); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 825 | } |
| 826 | |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 827 | #if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD) |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 828 | static fih_ret |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 829 | split_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 Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 836 | FIH_DECLARE(fih_rc, FIH_FAILURE); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 837 | |
| 838 | if (!tmpbuf) { |
| 839 | tmpbuf = malloc(BOOT_TMPBUF_SZ); |
| 840 | if (!tmpbuf) { |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 841 | goto out; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 842 | } |
| 843 | } |
| 844 | |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 845 | FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, loader_hdr, loader_fap, |
| 846 | tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, loader_hash); |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 847 | if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) { |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 848 | FIH_RET(fih_rc); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 849 | } |
| 850 | |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 851 | FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, app_hdr, app_fap, |
| 852 | tmpbuf, BOOT_TMPBUF_SZ, loader_hash, 32, NULL); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 853 | |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 854 | out: |
| 855 | FIH_RET(fih_rc); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 856 | } |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 857 | #endif /* !MCUBOOT_DIRECT_XIP && !MCUBOOT_RAM_LOAD */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 858 | |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 859 | /* |
David Brown | 9bf95af | 2019-10-10 15:36:36 -0600 | [diff] [blame] | 860 | * 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 | */ |
| 865 | static bool |
Jamie McCrae | dbb5c78 | 2024-08-22 10:43:14 +0100 | [diff] [blame] | 866 | boot_is_header_valid(const struct image_header *hdr, const struct flash_area *fap, |
| 867 | struct boot_loader_state *state) |
David Brown | 9bf95af | 2019-10-10 15:36:36 -0600 | [diff] [blame] | 868 | { |
| 869 | uint32_t size; |
| 870 | |
Jamie McCrae | dbb5c78 | 2024-08-22 10:43:14 +0100 | [diff] [blame] | 871 | (void)state; |
| 872 | |
David Brown | 9bf95af | 2019-10-10 15:36:36 -0600 | [diff] [blame] | 873 | 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 Ermel | 260ae09 | 2021-04-23 05:38:45 +0000 | [diff] [blame] | 881 | if (size >= flash_area_get_size(fap)) { |
David Brown | 9bf95af | 2019-10-10 15:36:36 -0600 | [diff] [blame] | 882 | return false; |
| 883 | } |
| 884 | |
| 885 | return true; |
| 886 | } |
| 887 | |
| 888 | /* |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 889 | * Check that a memory area consists of a given value. |
| 890 | */ |
| 891 | static inline bool |
| 892 | boot_data_is_set_to(uint8_t val, void *data, size_t len) |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 893 | { |
| 894 | uint8_t i; |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 895 | uint8_t *p = (uint8_t *)data; |
| 896 | for (i = 0; i < len; i++) { |
| 897 | if (val != p[i]) { |
| 898 | return false; |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 899 | } |
| 900 | } |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 901 | return true; |
| 902 | } |
| 903 | |
| 904 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 905 | boot_check_header_erased(struct boot_loader_state *state, int slot) |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 906 | { |
| 907 | const struct flash_area *fap; |
| 908 | struct image_header *hdr; |
| 909 | uint8_t erased_val; |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 910 | int area_id; |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 911 | int rc; |
| 912 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 913 | area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot); |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 914 | rc = flash_area_open(area_id, &fap); |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 915 | if (rc != 0) { |
| 916 | return -1; |
| 917 | } |
| 918 | |
| 919 | erased_val = flash_area_erased_val(fap); |
| 920 | flash_area_close(fap); |
| 921 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 922 | hdr = boot_img_hdr(state, slot); |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 923 | if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) { |
| 924 | return -1; |
| 925 | } |
| 926 | |
| 927 | return 0; |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 928 | } |
| 929 | |
Dominik Ermel | 0c8c8d5 | 2021-02-17 14:45:02 +0000 | [diff] [blame] | 930 | #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 | */ |
| 941 | static bool |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 942 | boot_rom_address_check(struct boot_loader_state *state) |
Dominik Ermel | 0c8c8d5 | 2021-02-17 14:45:02 +0000 | [diff] [blame] | 943 | { |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 944 | uint32_t active_slot; |
| 945 | const struct image_header *hdr; |
| 946 | uint32_t f_off; |
| 947 | |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 948 | active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot; |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 949 | hdr = boot_img_hdr(state, active_slot); |
| 950 | f_off = boot_img_slot_off(state, active_slot); |
| 951 | |
Dominik Ermel | 0c8c8d5 | 2021-02-17 14:45:02 +0000 | [diff] [blame] | 952 | 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 Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 954 | ", skipping", |
| 955 | active_slot == 0 ? "primary" : "secondary", f_off, |
Dominik Ermel | 0c8c8d5 | 2021-02-17 14:45:02 +0000 | [diff] [blame] | 956 | 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 Utzig | b1adb1e | 2019-09-11 11:42:53 -0300 | [diff] [blame] | 967 | /* |
| 968 | * Check that there is a valid image in a slot |
| 969 | * |
| 970 | * @returns |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 971 | * FIH_SUCCESS if image was successfully validated |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 972 | * FIH_NO_BOOTABLE_IMAGE if no bootloable image was found |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 973 | * FIH_FAILURE on any errors |
Fabio Utzig | b1adb1e | 2019-09-11 11:42:53 -0300 | [diff] [blame] | 974 | */ |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 975 | static fih_ret |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 976 | boot_validate_slot(struct boot_loader_state *state, int slot, |
| 977 | struct boot_status *bs) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 978 | { |
| 979 | const struct flash_area *fap; |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 980 | struct image_header *hdr; |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 981 | int area_id; |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 982 | FIH_DECLARE(fih_rc, FIH_FAILURE); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 983 | int rc; |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 984 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 985 | area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot); |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 986 | rc = flash_area_open(area_id, &fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 987 | if (rc != 0) { |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 988 | FIH_RET(fih_rc); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 989 | } |
| 990 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 991 | hdr = boot_img_hdr(state, slot); |
| 992 | if (boot_check_header_erased(state, slot) == 0 || |
| 993 | (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) { |
Fabio Utzig | 260ec45 | 2020-07-09 18:40:07 -0300 | [diff] [blame] | 994 | |
| 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 Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1011 | /* No bootable image in slot; continue booting from the primary slot. */ |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 1012 | fih_rc = FIH_NO_BOOTABLE_IMAGE; |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 1013 | goto out; |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 1014 | } |
| 1015 | |
Håkon Øye Amundsen | 2d1bac1 | 2020-01-03 13:08:09 +0000 | [diff] [blame] | 1016 | #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 Vincze | 8b0b637 | 2020-05-20 19:54:44 +0200 | [diff] [blame] | 1019 | 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 Amundsen | 2d1bac1 | 2020-01-03 13:08:09 +0000 | [diff] [blame] | 1023 | BOOT_LOG_ERR("insufficient version in secondary slot"); |
Dominik Ermel | 260ae09 | 2021-04-23 05:38:45 +0000 | [diff] [blame] | 1024 | flash_area_erase(fap, 0, flash_area_get_size(fap)); |
Håkon Øye Amundsen | 2d1bac1 | 2020-01-03 13:08:09 +0000 | [diff] [blame] | 1025 | /* Image in the secondary slot does not satisfy version requirement. |
| 1026 | * Erase the image and continue booting from the primary slot. |
| 1027 | */ |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 1028 | fih_rc = FIH_NO_BOOTABLE_IMAGE; |
Håkon Øye Amundsen | 2d1bac1 | 2020-01-03 13:08:09 +0000 | [diff] [blame] | 1029 | goto out; |
| 1030 | } |
| 1031 | } |
| 1032 | #endif |
Jamie McCrae | 2939d30 | 2024-08-22 10:44:46 +0100 | [diff] [blame] | 1033 | if (!boot_is_header_valid(hdr, fap, state)) { |
| 1034 | fih_rc = FIH_FAILURE; |
| 1035 | } else { |
| 1036 | BOOT_HOOK_CALL_FIH(boot_image_check_hook, FIH_BOOT_HOOK_REGULAR, |
| 1037 | fih_rc, BOOT_CURR_IMG(state), slot); |
| 1038 | if (FIH_EQ(fih_rc, FIH_BOOT_HOOK_REGULAR)) { |
| 1039 | FIH_CALL(boot_image_check, fih_rc, state, hdr, fap, bs); |
| 1040 | } |
Andrzej Puzdrowski | b8f3969 | 2021-07-02 15:05:37 +0200 | [diff] [blame] | 1041 | } |
Jamie McCrae | 2939d30 | 2024-08-22 10:44:46 +0100 | [diff] [blame] | 1042 | if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) { |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 1043 | if ((slot != BOOT_PRIMARY_SLOT) || ARE_SLOTS_EQUIVALENT()) { |
Dominik Ermel | 260ae09 | 2021-04-23 05:38:45 +0000 | [diff] [blame] | 1044 | flash_area_erase(fap, 0, flash_area_get_size(fap)); |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 1045 | /* Image is invalid, erase it to prevent further unnecessary |
| 1046 | * attempts to validate and boot it. |
David Brown | b38e044 | 2017-02-24 13:57:12 -0700 | [diff] [blame] | 1047 | */ |
| 1048 | } |
David Brown | 098de83 | 2019-12-10 11:58:01 -0700 | [diff] [blame] | 1049 | #if !defined(__BOOTSIM__) |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1050 | BOOT_LOG_ERR("Image in the %s slot is not valid!", |
| 1051 | (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary"); |
David Brown | 098de83 | 2019-12-10 11:58:01 -0700 | [diff] [blame] | 1052 | #endif |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 1053 | fih_rc = FIH_NO_BOOTABLE_IMAGE; |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 1054 | goto out; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1055 | } |
| 1056 | |
Håkon Øye Amundsen | e829e9d | 2021-11-12 14:01:01 +0000 | [diff] [blame] | 1057 | #if MCUBOOT_IMAGE_NUMBER > 1 && !defined(MCUBOOT_ENC_IMAGES) && defined(MCUBOOT_VERIFY_IMG_ADDRESS) |
| 1058 | /* Verify that the image in the secondary slot has a reset address |
| 1059 | * located in the primary slot. This is done to avoid users incorrectly |
| 1060 | * overwriting an application written to the incorrect slot. |
| 1061 | * This feature is only supported by ARM platforms. |
| 1062 | */ |
| 1063 | if (area_id == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) { |
| 1064 | const struct flash_area *pri_fa = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT); |
| 1065 | struct image_header *secondary_hdr = boot_img_hdr(state, slot); |
| 1066 | uint32_t reset_value = 0; |
| 1067 | uint32_t reset_addr = secondary_hdr->ih_hdr_size + sizeof(reset_value); |
| 1068 | |
| 1069 | rc = flash_area_read(fap, reset_addr, &reset_value, sizeof(reset_value)); |
| 1070 | if (rc != 0) { |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 1071 | fih_rc = FIH_NO_BOOTABLE_IMAGE; |
Håkon Øye Amundsen | e829e9d | 2021-11-12 14:01:01 +0000 | [diff] [blame] | 1072 | goto out; |
| 1073 | } |
| 1074 | |
| 1075 | if (reset_value < pri_fa->fa_off || reset_value> (pri_fa->fa_off + pri_fa->fa_size)) { |
| 1076 | BOOT_LOG_ERR("Reset address of image in secondary slot is not in the primary slot"); |
| 1077 | BOOT_LOG_ERR("Erasing image from secondary slot"); |
| 1078 | |
| 1079 | /* The vector table in the image located in the secondary |
| 1080 | * slot does not target the primary slot. This might |
| 1081 | * indicate that the image was loaded to the wrong slot. |
| 1082 | * |
| 1083 | * Erase the image and continue booting from the primary slot. |
| 1084 | */ |
| 1085 | flash_area_erase(fap, 0, fap->fa_size); |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 1086 | fih_rc = FIH_NO_BOOTABLE_IMAGE; |
Håkon Øye Amundsen | e829e9d | 2021-11-12 14:01:01 +0000 | [diff] [blame] | 1087 | goto out; |
| 1088 | } |
| 1089 | } |
| 1090 | #endif |
| 1091 | |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 1092 | out: |
| 1093 | flash_area_close(fap); |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 1094 | |
| 1095 | FIH_RET(fih_rc); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1096 | } |
| 1097 | |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 1098 | #ifdef MCUBOOT_HW_ROLLBACK_PROT |
| 1099 | /** |
| 1100 | * Updates the stored security counter value with the image's security counter |
| 1101 | * value which resides in the given slot, only if it's greater than the stored |
| 1102 | * value. |
| 1103 | * |
| 1104 | * @param image_index Index of the image to determine which security |
| 1105 | * counter to update. |
| 1106 | * @param slot Slot number of the image. |
| 1107 | * @param hdr Pointer to the image header structure of the image |
| 1108 | * that is currently stored in the given slot. |
| 1109 | * |
| 1110 | * @return 0 on success; nonzero on failure. |
| 1111 | */ |
| 1112 | static int |
| 1113 | boot_update_security_counter(uint8_t image_index, int slot, |
| 1114 | struct image_header *hdr) |
| 1115 | { |
| 1116 | const struct flash_area *fap = NULL; |
| 1117 | uint32_t img_security_cnt; |
| 1118 | int rc; |
| 1119 | |
| 1120 | rc = flash_area_open(flash_area_id_from_multi_image_slot(image_index, slot), |
| 1121 | &fap); |
| 1122 | if (rc != 0) { |
| 1123 | rc = BOOT_EFLASH; |
| 1124 | goto done; |
| 1125 | } |
| 1126 | |
| 1127 | rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt); |
| 1128 | if (rc != 0) { |
| 1129 | goto done; |
| 1130 | } |
| 1131 | |
| 1132 | rc = boot_nv_security_counter_update(image_index, img_security_cnt); |
| 1133 | if (rc != 0) { |
| 1134 | goto done; |
| 1135 | } |
| 1136 | |
| 1137 | done: |
| 1138 | flash_area_close(fap); |
| 1139 | return rc; |
| 1140 | } |
| 1141 | #endif /* MCUBOOT_HW_ROLLBACK_PROT */ |
| 1142 | |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 1143 | #if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD) |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 1144 | /** |
| 1145 | * Determines which swap operation to perform, if any. If it is determined |
| 1146 | * that a swap operation is required, the image in the secondary slot is checked |
| 1147 | * for validity. If the image in the secondary slot is invalid, it is erased, |
| 1148 | * and a swap type of "none" is indicated. |
| 1149 | * |
| 1150 | * @return The type of swap to perform (BOOT_SWAP_TYPE...) |
| 1151 | */ |
| 1152 | static int |
| 1153 | boot_validated_swap_type(struct boot_loader_state *state, |
| 1154 | struct boot_status *bs) |
| 1155 | { |
| 1156 | int swap_type; |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 1157 | FIH_DECLARE(fih_rc, FIH_FAILURE); |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 1158 | |
| 1159 | swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state)); |
| 1160 | if (BOOT_IS_UPGRADE(swap_type)) { |
| 1161 | /* Boot loader wants to switch to the secondary slot. |
| 1162 | * Ensure image is valid. |
| 1163 | */ |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 1164 | FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_SECONDARY_SLOT, bs); |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 1165 | if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) { |
| 1166 | if (FIH_EQ(fih_rc, FIH_NO_BOOTABLE_IMAGE)) { |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 1167 | swap_type = BOOT_SWAP_TYPE_NONE; |
| 1168 | } else { |
| 1169 | swap_type = BOOT_SWAP_TYPE_FAIL; |
| 1170 | } |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | return swap_type; |
| 1175 | } |
David Brown | 94ed12c | 2021-05-26 16:28:14 -0600 | [diff] [blame] | 1176 | #endif |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 1177 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1178 | /** |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1179 | * Erases a region of flash. |
| 1180 | * |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1181 | * @param flash_area The flash_area containing the region to erase. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1182 | * @param off The offset within the flash area to start the |
| 1183 | * erase. |
| 1184 | * @param sz The number of bytes to erase. |
| 1185 | * |
| 1186 | * @return 0 on success; nonzero on failure. |
| 1187 | */ |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1188 | int |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame] | 1189 | boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1190 | { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1191 | return flash_area_erase(fap, off, sz); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1192 | } |
| 1193 | |
David Brown | 94ed12c | 2021-05-26 16:28:14 -0600 | [diff] [blame] | 1194 | #if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD) |
Dominik Ermel | 256bc37 | 2022-12-07 15:51:31 +0000 | [diff] [blame] | 1195 | |
| 1196 | #if defined(MCUBOOT_ENC_IMAGES) || defined(MCUBOOT_SWAP_SAVE_ENCTLV) |
| 1197 | /* Replacement for memset(p, 0, sizeof(*p) that does not get |
| 1198 | * optimized out. |
| 1199 | */ |
| 1200 | static void like_mbedtls_zeroize(void *p, size_t n) |
| 1201 | { |
| 1202 | volatile unsigned char *v = (unsigned char *)p; |
| 1203 | |
| 1204 | for (size_t i = 0; i < n; i++) { |
| 1205 | v[i] = 0; |
| 1206 | } |
| 1207 | } |
| 1208 | #endif |
| 1209 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1210 | /** |
| 1211 | * Copies the contents of one flash region to another. You must erase the |
| 1212 | * destination region prior to calling this function. |
| 1213 | * |
| 1214 | * @param flash_area_id_src The ID of the source flash area. |
| 1215 | * @param flash_area_id_dst The ID of the destination flash area. |
| 1216 | * @param off_src The offset within the source flash area to |
| 1217 | * copy from. |
| 1218 | * @param off_dst The offset within the destination flash area to |
| 1219 | * copy to. |
| 1220 | * @param sz The number of bytes to copy. |
| 1221 | * |
| 1222 | * @return 0 on success; nonzero on failure. |
| 1223 | */ |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1224 | int |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame] | 1225 | boot_copy_region(struct boot_loader_state *state, |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1226 | const struct flash_area *fap_src, |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1227 | const struct flash_area *fap_dst, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1228 | uint32_t off_src, uint32_t off_dst, uint32_t sz) |
| 1229 | { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1230 | uint32_t bytes_copied; |
| 1231 | int chunk_sz; |
| 1232 | int rc; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1233 | #ifdef MCUBOOT_ENC_IMAGES |
Dominik Ermel | 6fe259b | 2024-07-18 11:40:53 +0000 | [diff] [blame] | 1234 | uint32_t off = off_dst; |
Fabio Utzig | a87cc7d | 2019-08-26 11:21:45 -0300 | [diff] [blame] | 1235 | uint32_t tlv_off; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1236 | size_t blk_off; |
| 1237 | struct image_header *hdr; |
| 1238 | uint16_t idx; |
| 1239 | uint32_t blk_sz; |
Dominik Ermel | 6fe259b | 2024-07-18 11:40:53 +0000 | [diff] [blame] | 1240 | uint8_t image_index = BOOT_CURR_IMG(state); |
Thomas Altenbach | 08d2d94 | 2024-04-25 15:29:21 +0200 | [diff] [blame] | 1241 | bool encrypted_src; |
| 1242 | bool encrypted_dst; |
Dominik Ermel | 6fe259b | 2024-07-18 11:40:53 +0000 | [diff] [blame] | 1243 | /* Assuming the secondary slot is source; note that 0 here not only |
| 1244 | * means that primary slot is source, but also that there will be |
| 1245 | * encryption happening, if it is 1 then there is decryption from |
| 1246 | * secondary slot. |
| 1247 | */ |
Dominik Ermel | 3f11286 | 2024-07-17 14:43:05 +0000 | [diff] [blame] | 1248 | int source_slot = 1; |
Dominik Ermel | 6fe259b | 2024-07-18 11:40:53 +0000 | [diff] [blame] | 1249 | /* In case of encryption enabled, we may have to do more work than |
| 1250 | * just copy bytes */ |
| 1251 | bool only_copy = false; |
| 1252 | #else |
| 1253 | (void)state; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1254 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1255 | |
Kristine Jassmann | 73c38c6 | 2021-02-03 16:56:14 +0000 | [diff] [blame] | 1256 | TARGET_STATIC uint8_t buf[BUF_SZ] __attribute__((aligned(4))); |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1257 | |
Dominik Ermel | 6fe259b | 2024-07-18 11:40:53 +0000 | [diff] [blame] | 1258 | #ifdef MCUBOOT_ENC_IMAGES |
| 1259 | encrypted_src = (flash_area_get_id(fap_src) != FLASH_AREA_IMAGE_PRIMARY(image_index)); |
| 1260 | encrypted_dst = (flash_area_get_id(fap_dst) != FLASH_AREA_IMAGE_PRIMARY(image_index)); |
| 1261 | |
| 1262 | if (encrypted_src != encrypted_dst) { |
| 1263 | if (encrypted_dst) { |
| 1264 | /* Need encryption, metadata from the primary slot */ |
| 1265 | hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT); |
| 1266 | source_slot = 0; |
| 1267 | } else { |
| 1268 | /* Need decryption, metadata from the secondary slot */ |
| 1269 | hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT); |
| 1270 | source_slot = 1; |
| 1271 | } |
| 1272 | } else { |
| 1273 | /* In case when source and targe is the same area, this means that we |
| 1274 | * only have to copy bytes, no encryption or decryption. |
| 1275 | */ |
| 1276 | only_copy = true; |
| 1277 | } |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1278 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1279 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1280 | bytes_copied = 0; |
| 1281 | while (bytes_copied < sz) { |
| 1282 | if (sz - bytes_copied > sizeof buf) { |
| 1283 | chunk_sz = sizeof buf; |
| 1284 | } else { |
| 1285 | chunk_sz = sz - bytes_copied; |
| 1286 | } |
| 1287 | |
| 1288 | rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz); |
| 1289 | if (rc != 0) { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1290 | return BOOT_EFLASH; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1291 | } |
| 1292 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1293 | #ifdef MCUBOOT_ENC_IMAGES |
Dominik Ermel | 6fe259b | 2024-07-18 11:40:53 +0000 | [diff] [blame] | 1294 | /* If only copy, then does not matter if header indicates need for |
| 1295 | * encryptio/decryptio, we just copy data. */ |
| 1296 | if (!only_copy && IS_ENCRYPTED(hdr)) { |
| 1297 | uint32_t abs_off = off + bytes_copied; |
| 1298 | if (abs_off < hdr->ih_hdr_size) { |
| 1299 | /* do not decrypt header */ |
| 1300 | if (abs_off + chunk_sz > hdr->ih_hdr_size) { |
| 1301 | /* The lower part of the chunk contains header data */ |
| 1302 | blk_off = 0; |
| 1303 | blk_sz = chunk_sz - (hdr->ih_hdr_size - abs_off); |
| 1304 | idx = hdr->ih_hdr_size - abs_off; |
| 1305 | } else { |
| 1306 | /* The chunk contains exclusively header data */ |
| 1307 | blk_sz = 0; /* nothing to decrypt */ |
| 1308 | } |
Thomas Altenbach | 08d2d94 | 2024-04-25 15:29:21 +0200 | [diff] [blame] | 1309 | } else { |
Dominik Ermel | 6fe259b | 2024-07-18 11:40:53 +0000 | [diff] [blame] | 1310 | idx = 0; |
| 1311 | blk_sz = chunk_sz; |
| 1312 | blk_off = (abs_off - hdr->ih_hdr_size) & 0xf; |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 1313 | } |
Thomas Altenbach | 08d2d94 | 2024-04-25 15:29:21 +0200 | [diff] [blame] | 1314 | |
Dominik Ermel | 6fe259b | 2024-07-18 11:40:53 +0000 | [diff] [blame] | 1315 | if (blk_sz > 0) |
| 1316 | { |
| 1317 | tlv_off = BOOT_TLV_OFF(hdr); |
| 1318 | if (abs_off + chunk_sz > tlv_off) { |
| 1319 | /* do not decrypt TLVs */ |
| 1320 | if (abs_off >= tlv_off) { |
| 1321 | blk_sz = 0; |
Andrzej Puzdrowski | e38b0af | 2021-11-04 13:34:11 +0100 | [diff] [blame] | 1322 | } else { |
Dominik Ermel | 6fe259b | 2024-07-18 11:40:53 +0000 | [diff] [blame] | 1323 | blk_sz = tlv_off - abs_off; |
Andrzej Puzdrowski | e38b0af | 2021-11-04 13:34:11 +0100 | [diff] [blame] | 1324 | } |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1325 | } |
Dominik Ermel | 6fe259b | 2024-07-18 11:40:53 +0000 | [diff] [blame] | 1326 | boot_encrypt(BOOT_CURR_ENC(state), source_slot, |
| 1327 | (abs_off + idx) - hdr->ih_hdr_size, blk_sz, |
| 1328 | blk_off, &buf[idx]); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1329 | } |
| 1330 | } |
| 1331 | #endif |
| 1332 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1333 | rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz); |
| 1334 | if (rc != 0) { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1335 | return BOOT_EFLASH; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1336 | } |
| 1337 | |
| 1338 | bytes_copied += chunk_sz; |
Fabio Utzig | 853657c | 2019-05-07 08:06:07 -0300 | [diff] [blame] | 1339 | |
| 1340 | MCUBOOT_WATCHDOG_FEED(); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1341 | } |
| 1342 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1343 | return 0; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1344 | } |
| 1345 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1346 | /** |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1347 | * Overwrite primary slot with the image contained in the secondary slot. |
| 1348 | * If a prior copy operation was interrupted by a system reset, this function |
| 1349 | * redos the copy. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1350 | * |
| 1351 | * @param bs The current boot status. This function reads |
| 1352 | * this struct to determine if it is resuming |
| 1353 | * an interrupted swap operation. This |
| 1354 | * function writes the updated status to this |
| 1355 | * function on return. |
| 1356 | * |
| 1357 | * @return 0 on success; nonzero on failure. |
| 1358 | */ |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 1359 | #if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP) |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 1360 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1361 | boot_copy_image(struct boot_loader_state *state, struct boot_status *bs) |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 1362 | { |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 1363 | size_t sect_count; |
| 1364 | size_t sect; |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 1365 | int rc; |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 1366 | size_t size; |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 1367 | size_t this_size; |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 1368 | size_t last_sector; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1369 | const struct flash_area *fap_primary_slot; |
| 1370 | const struct flash_area *fap_secondary_slot; |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1371 | uint8_t image_index; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1372 | |
Fabio Utzig | b4f8810 | 2020-10-04 10:16:24 -0300 | [diff] [blame] | 1373 | #if defined(MCUBOOT_OVERWRITE_ONLY_FAST) |
| 1374 | uint32_t sector; |
| 1375 | uint32_t trailer_sz; |
| 1376 | uint32_t off; |
| 1377 | uint32_t sz; |
| 1378 | #endif |
| 1379 | |
Fabio Utzig | aaf767c | 2017-12-05 10:22:46 -0200 | [diff] [blame] | 1380 | (void)bs; |
| 1381 | |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 1382 | #if defined(MCUBOOT_OVERWRITE_ONLY_FAST) |
| 1383 | uint32_t src_size = 0; |
Fabio Utzig | d638b17 | 2019-08-09 10:38:05 -0300 | [diff] [blame] | 1384 | rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &src_size); |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 1385 | assert(rc == 0); |
| 1386 | #endif |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 1387 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1388 | image_index = BOOT_CURR_IMG(state); |
| 1389 | |
Antonio de Angelis | ba5fb1c | 2022-10-11 10:10:37 +0100 | [diff] [blame] | 1390 | BOOT_LOG_INF("Image %d upgrade secondary slot -> primary slot", image_index); |
| 1391 | BOOT_LOG_INF("Erasing the primary slot"); |
| 1392 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1393 | rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), |
| 1394 | &fap_primary_slot); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1395 | assert (rc == 0); |
| 1396 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1397 | rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index), |
| 1398 | &fap_secondary_slot); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1399 | assert (rc == 0); |
| 1400 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1401 | sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT); |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 1402 | for (sect = 0, size = 0; sect < sect_count; sect++) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1403 | this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect); |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame] | 1404 | rc = boot_erase_region(fap_primary_slot, size, this_size); |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 1405 | assert(rc == 0); |
| 1406 | |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 1407 | #if defined(MCUBOOT_OVERWRITE_ONLY_FAST) |
Fabio Utzig | b4f8810 | 2020-10-04 10:16:24 -0300 | [diff] [blame] | 1408 | if ((size + this_size) >= src_size) { |
| 1409 | size += src_size - size; |
| 1410 | size += BOOT_WRITE_SZ(state) - (size % BOOT_WRITE_SZ(state)); |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 1411 | break; |
| 1412 | } |
| 1413 | #endif |
Fabio Utzig | b4f8810 | 2020-10-04 10:16:24 -0300 | [diff] [blame] | 1414 | |
| 1415 | size += this_size; |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 1416 | } |
| 1417 | |
Fabio Utzig | b4f8810 | 2020-10-04 10:16:24 -0300 | [diff] [blame] | 1418 | #if defined(MCUBOOT_OVERWRITE_ONLY_FAST) |
| 1419 | trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state)); |
| 1420 | sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1; |
| 1421 | sz = 0; |
| 1422 | do { |
| 1423 | sz += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sector); |
| 1424 | off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, sector); |
| 1425 | sector--; |
| 1426 | } while (sz < trailer_sz); |
| 1427 | |
| 1428 | rc = boot_erase_region(fap_primary_slot, off, sz); |
| 1429 | assert(rc == 0); |
| 1430 | #endif |
| 1431 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1432 | #ifdef MCUBOOT_ENC_IMAGES |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1433 | if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) { |
Dominik Ermel | 7f9ac97 | 2024-07-12 19:21:40 +0000 | [diff] [blame] | 1434 | rc = boot_enc_load(BOOT_CURR_ENC(state), BOOT_SECONDARY_SLOT, |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1435 | boot_img_hdr(state, BOOT_SECONDARY_SLOT), |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 1436 | fap_secondary_slot, bs); |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1437 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1438 | if (rc < 0) { |
| 1439 | return BOOT_EBADIMAGE; |
| 1440 | } |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 1441 | if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1442 | return BOOT_EBADIMAGE; |
| 1443 | } |
| 1444 | } |
| 1445 | #endif |
| 1446 | |
Antonio de Angelis | 4854700 | 2023-04-14 09:47:09 +0100 | [diff] [blame] | 1447 | BOOT_LOG_INF("Image %d copying the secondary slot to the primary slot: 0x%zx bytes", |
| 1448 | image_index, size); |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame] | 1449 | rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot, 0, 0, size); |
Fabio Utzig | b4f8810 | 2020-10-04 10:16:24 -0300 | [diff] [blame] | 1450 | if (rc != 0) { |
| 1451 | return rc; |
| 1452 | } |
| 1453 | |
| 1454 | #if defined(MCUBOOT_OVERWRITE_ONLY_FAST) |
| 1455 | rc = boot_write_magic(fap_primary_slot); |
| 1456 | if (rc != 0) { |
| 1457 | return rc; |
| 1458 | } |
| 1459 | #endif |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 1460 | |
Andrzej Puzdrowski | b8f3969 | 2021-07-02 15:05:37 +0200 | [diff] [blame] | 1461 | rc = BOOT_HOOK_CALL(boot_copy_region_post_hook, 0, BOOT_CURR_IMG(state), |
| 1462 | BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT), size); |
| 1463 | if (rc != 0) { |
| 1464 | return rc; |
| 1465 | } |
| 1466 | |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 1467 | #ifdef MCUBOOT_HW_ROLLBACK_PROT |
| 1468 | /* Update the stored security counter with the new image's security counter |
| 1469 | * value. Both slots hold the new image at this point, but the secondary |
| 1470 | * slot's image header must be passed since the image headers in the |
| 1471 | * boot_data structure have not been updated yet. |
| 1472 | */ |
| 1473 | rc = boot_update_security_counter(BOOT_CURR_IMG(state), BOOT_PRIMARY_SLOT, |
| 1474 | boot_img_hdr(state, BOOT_SECONDARY_SLOT)); |
| 1475 | if (rc != 0) { |
| 1476 | BOOT_LOG_ERR("Security counter update failed after image upgrade."); |
| 1477 | return rc; |
| 1478 | } |
| 1479 | #endif /* MCUBOOT_HW_ROLLBACK_PROT */ |
| 1480 | |
Petr Buchta | c5a528b | 2024-02-24 08:10:38 +0100 | [diff] [blame] | 1481 | #ifndef MCUBOOT_OVERWRITE_ONLY_KEEP_BACKUP |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 1482 | /* |
| 1483 | * Erases header and trailer. The trailer is erased because when a new |
| 1484 | * image is written without a trailer as is the case when using newt, the |
| 1485 | * trailer that was left might trigger a new upgrade. |
| 1486 | */ |
Christopher Collins | 2c88e69 | 2019-05-22 15:10:14 -0700 | [diff] [blame] | 1487 | BOOT_LOG_DBG("erasing secondary header"); |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame] | 1488 | rc = boot_erase_region(fap_secondary_slot, |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1489 | boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0), |
| 1490 | boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0)); |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 1491 | assert(rc == 0); |
Petr Buchta | c5a528b | 2024-02-24 08:10:38 +0100 | [diff] [blame] | 1492 | #endif |
| 1493 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1494 | last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1; |
Christopher Collins | 2c88e69 | 2019-05-22 15:10:14 -0700 | [diff] [blame] | 1495 | BOOT_LOG_DBG("erasing secondary trailer"); |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame] | 1496 | rc = boot_erase_region(fap_secondary_slot, |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1497 | boot_img_sector_off(state, BOOT_SECONDARY_SLOT, |
| 1498 | last_sector), |
| 1499 | boot_img_sector_size(state, BOOT_SECONDARY_SLOT, |
| 1500 | last_sector)); |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 1501 | assert(rc == 0); |
| 1502 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1503 | flash_area_close(fap_primary_slot); |
| 1504 | flash_area_close(fap_secondary_slot); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1505 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1506 | /* TODO: Perhaps verify the primary slot's signature again? */ |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 1507 | |
| 1508 | return 0; |
| 1509 | } |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 1510 | #endif |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1511 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1512 | #if !defined(MCUBOOT_OVERWRITE_ONLY) |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1513 | /** |
| 1514 | * Swaps the two images in flash. If a prior copy operation was interrupted |
| 1515 | * by a system reset, this function completes that operation. |
| 1516 | * |
| 1517 | * @param bs The current boot status. This function reads |
| 1518 | * this struct to determine if it is resuming |
| 1519 | * an interrupted swap operation. This |
| 1520 | * function writes the updated status to this |
| 1521 | * function on return. |
| 1522 | * |
| 1523 | * @return 0 on success; nonzero on failure. |
| 1524 | */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1525 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1526 | boot_swap_image(struct boot_loader_state *state, struct boot_status *bs) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1527 | { |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1528 | struct image_header *hdr; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1529 | const struct flash_area *fap; |
Dominik Ermel | 472d4c7 | 2023-02-10 13:29:58 +0000 | [diff] [blame] | 1530 | #ifdef MCUBOOT_ENC_IMAGES |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1531 | uint8_t slot; |
| 1532 | uint8_t i; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1533 | #endif |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1534 | uint32_t size; |
| 1535 | uint32_t copy_size; |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1536 | uint8_t image_index; |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1537 | int rc; |
| 1538 | |
| 1539 | /* FIXME: just do this if asked by user? */ |
| 1540 | |
| 1541 | size = copy_size = 0; |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1542 | image_index = BOOT_CURR_IMG(state); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1543 | |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1544 | if (boot_status_is_reset(bs)) { |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 1545 | /* |
| 1546 | * No swap ever happened, so need to find the largest image which |
| 1547 | * will be used to determine the amount of sectors to swap. |
| 1548 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1549 | hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1550 | if (hdr->ih_magic == IMAGE_MAGIC) { |
Fabio Utzig | d638b17 | 2019-08-09 10:38:05 -0300 | [diff] [blame] | 1551 | rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, ©_size); |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 1552 | assert(rc == 0); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1553 | } |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1554 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1555 | #ifdef MCUBOOT_ENC_IMAGES |
Fabio Utzig | 2fc80df | 2018-12-14 06:47:38 -0200 | [diff] [blame] | 1556 | if (IS_ENCRYPTED(hdr)) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1557 | fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT); |
Dominik Ermel | 7f9ac97 | 2024-07-12 19:21:40 +0000 | [diff] [blame] | 1558 | rc = boot_enc_load(BOOT_CURR_ENC(state), 0, hdr, fap, bs); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1559 | assert(rc >= 0); |
| 1560 | |
| 1561 | if (rc == 0) { |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 1562 | rc = boot_enc_set_key(BOOT_CURR_ENC(state), 0, bs); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1563 | assert(rc == 0); |
| 1564 | } else { |
| 1565 | rc = 0; |
| 1566 | } |
| 1567 | } else { |
Kristine Jassmann | 73c38c6 | 2021-02-03 16:56:14 +0000 | [diff] [blame] | 1568 | memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_ALIGN_SIZE); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1569 | } |
| 1570 | #endif |
| 1571 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1572 | hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT); |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 1573 | if (hdr->ih_magic == IMAGE_MAGIC) { |
Fabio Utzig | d638b17 | 2019-08-09 10:38:05 -0300 | [diff] [blame] | 1574 | rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size); |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 1575 | assert(rc == 0); |
| 1576 | } |
| 1577 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1578 | #ifdef MCUBOOT_ENC_IMAGES |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1579 | hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT); |
Fabio Utzig | 2fc80df | 2018-12-14 06:47:38 -0200 | [diff] [blame] | 1580 | if (IS_ENCRYPTED(hdr)) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1581 | fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT); |
Dominik Ermel | 7f9ac97 | 2024-07-12 19:21:40 +0000 | [diff] [blame] | 1582 | rc = boot_enc_load(BOOT_CURR_ENC(state), 1, hdr, fap, bs); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1583 | assert(rc >= 0); |
| 1584 | |
| 1585 | if (rc == 0) { |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 1586 | rc = boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1587 | assert(rc == 0); |
| 1588 | } else { |
| 1589 | rc = 0; |
| 1590 | } |
| 1591 | } else { |
Kristine Jassmann | 73c38c6 | 2021-02-03 16:56:14 +0000 | [diff] [blame] | 1592 | memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_ALIGN_SIZE); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1593 | } |
| 1594 | #endif |
| 1595 | |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 1596 | if (size > copy_size) { |
| 1597 | copy_size = size; |
| 1598 | } |
| 1599 | |
| 1600 | bs->swap_size = copy_size; |
| 1601 | } else { |
| 1602 | /* |
| 1603 | * If a swap was under way, the swap_size should already be present |
| 1604 | * in the trailer... |
| 1605 | */ |
Dominik Ermel | 472d4c7 | 2023-02-10 13:29:58 +0000 | [diff] [blame] | 1606 | |
| 1607 | rc = boot_find_status(image_index, &fap); |
| 1608 | assert(fap != NULL); |
| 1609 | rc = boot_read_swap_size(fap, &bs->swap_size); |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 1610 | assert(rc == 0); |
| 1611 | |
| 1612 | copy_size = bs->swap_size; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1613 | |
| 1614 | #ifdef MCUBOOT_ENC_IMAGES |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 1615 | for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) { |
Dominik Ermel | 472d4c7 | 2023-02-10 13:29:58 +0000 | [diff] [blame] | 1616 | rc = boot_read_enc_key(fap, slot, bs); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1617 | assert(rc == 0); |
| 1618 | |
| 1619 | for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) { |
Fabio Utzig | 1c7d959 | 2018-12-03 10:35:56 -0200 | [diff] [blame] | 1620 | if (bs->enckey[slot][i] != 0xff) { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1621 | break; |
| 1622 | } |
| 1623 | } |
| 1624 | |
Dominik Ermel | 7e3a1ce | 2024-07-12 17:19:17 +0000 | [diff] [blame] | 1625 | boot_enc_init(BOOT_CURR_ENC(state), slot); |
| 1626 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1627 | if (i != BOOT_ENC_KEY_SIZE) { |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 1628 | boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1629 | } |
| 1630 | } |
| 1631 | #endif |
Dominik Ermel | 472d4c7 | 2023-02-10 13:29:58 +0000 | [diff] [blame] | 1632 | flash_area_close(fap); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1633 | } |
| 1634 | |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1635 | swap_run(state, bs, copy_size); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1636 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1637 | #ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1638 | extern int boot_status_fails; |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 1639 | if (boot_status_fails > 0) { |
Christopher Collins | 2c88e69 | 2019-05-22 15:10:14 -0700 | [diff] [blame] | 1640 | BOOT_LOG_WRN("%d status write fails performing the swap", |
| 1641 | boot_status_fails); |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 1642 | } |
| 1643 | #endif |
Sigvart Hovland | 3fd4cd4 | 2022-09-09 13:21:18 +0200 | [diff] [blame] | 1644 | rc = BOOT_HOOK_CALL(boot_copy_region_post_hook, 0, BOOT_CURR_IMG(state), |
| 1645 | BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT), size); |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 1646 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1647 | return 0; |
| 1648 | } |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 1649 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1650 | |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1651 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1652 | /** |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1653 | * Performs a clean (not aborted) image update. |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1654 | * |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1655 | * @param bs The current boot status. |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1656 | * |
| 1657 | * @return 0 on success; nonzero on failure. |
| 1658 | */ |
| 1659 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1660 | boot_perform_update(struct boot_loader_state *state, struct boot_status *bs) |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1661 | { |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1662 | int rc; |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1663 | #ifndef MCUBOOT_OVERWRITE_ONLY |
| 1664 | uint8_t swap_type; |
| 1665 | #endif |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1666 | |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1667 | /* At this point there are no aborted swaps. */ |
| 1668 | #if defined(MCUBOOT_OVERWRITE_ONLY) |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1669 | rc = boot_copy_image(state, bs); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1670 | #elif defined(MCUBOOT_BOOTSTRAP) |
| 1671 | /* Check if the image update was triggered by a bad image in the |
| 1672 | * primary slot (the validity of the image in the secondary slot had |
| 1673 | * already been checked). |
| 1674 | */ |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 1675 | FIH_DECLARE(fih_rc, FIH_FAILURE); |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 1676 | rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT); |
| 1677 | FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, bs); |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 1678 | if (rc == 0 || FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1679 | rc = boot_copy_image(state, bs); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1680 | } else { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1681 | rc = boot_swap_image(state, bs); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1682 | } |
| 1683 | #else |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1684 | rc = boot_swap_image(state, bs); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1685 | #endif |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1686 | assert(rc == 0); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1687 | |
| 1688 | #ifndef MCUBOOT_OVERWRITE_ONLY |
| 1689 | /* The following state needs image_ok be explicitly set after the |
| 1690 | * swap was finished to avoid a new revert. |
| 1691 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1692 | swap_type = BOOT_SWAP_TYPE(state); |
| 1693 | if (swap_type == BOOT_SWAP_TYPE_REVERT || |
| 1694 | swap_type == BOOT_SWAP_TYPE_PERM) { |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1695 | rc = swap_set_image_ok(BOOT_CURR_IMG(state)); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1696 | if (rc != 0) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1697 | BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1698 | } |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1699 | } |
| 1700 | |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 1701 | #ifdef MCUBOOT_HW_ROLLBACK_PROT |
| 1702 | if (swap_type == BOOT_SWAP_TYPE_PERM) { |
| 1703 | /* Update the stored security counter with the new image's security |
| 1704 | * counter value. The primary slot holds the new image at this point, |
| 1705 | * but the secondary slot's image header must be passed since image |
| 1706 | * headers in the boot_data structure have not been updated yet. |
| 1707 | * |
| 1708 | * In case of a permanent image swap mcuboot will never attempt to |
| 1709 | * revert the images on the next reboot. Therefore, the security |
| 1710 | * counter must be increased right after the image upgrade. |
| 1711 | */ |
| 1712 | rc = boot_update_security_counter( |
| 1713 | BOOT_CURR_IMG(state), |
| 1714 | BOOT_PRIMARY_SLOT, |
| 1715 | boot_img_hdr(state, BOOT_SECONDARY_SLOT)); |
| 1716 | if (rc != 0) { |
| 1717 | BOOT_LOG_ERR("Security counter update failed after " |
| 1718 | "image upgrade."); |
| 1719 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC; |
| 1720 | } |
| 1721 | } |
| 1722 | #endif /* MCUBOOT_HW_ROLLBACK_PROT */ |
| 1723 | |
Fabio Utzig | e575b0b | 2019-09-11 12:34:23 -0300 | [diff] [blame] | 1724 | if (BOOT_IS_UPGRADE(swap_type)) { |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1725 | rc = swap_set_copy_done(BOOT_CURR_IMG(state)); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1726 | if (rc != 0) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1727 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC; |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1728 | } |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1729 | } |
| 1730 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 1731 | |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1732 | return rc; |
| 1733 | } |
| 1734 | |
| 1735 | /** |
| 1736 | * Completes a previously aborted image swap. |
| 1737 | * |
| 1738 | * @param bs The current boot status. |
| 1739 | * |
| 1740 | * @return 0 on success; nonzero on failure. |
| 1741 | */ |
| 1742 | #if !defined(MCUBOOT_OVERWRITE_ONLY) |
| 1743 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1744 | boot_complete_partial_swap(struct boot_loader_state *state, |
| 1745 | struct boot_status *bs) |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1746 | { |
| 1747 | int rc; |
| 1748 | |
| 1749 | /* Determine the type of swap operation being resumed from the |
| 1750 | * `swap-type` trailer field. |
| 1751 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1752 | rc = boot_swap_image(state, bs); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1753 | assert(rc == 0); |
| 1754 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1755 | BOOT_SWAP_TYPE(state) = bs->swap_type; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1756 | |
| 1757 | /* The following states need image_ok be explicitly set after the |
| 1758 | * swap was finished to avoid a new revert. |
| 1759 | */ |
| 1760 | if (bs->swap_type == BOOT_SWAP_TYPE_REVERT || |
| 1761 | bs->swap_type == BOOT_SWAP_TYPE_PERM) { |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1762 | rc = swap_set_image_ok(BOOT_CURR_IMG(state)); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1763 | if (rc != 0) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1764 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1765 | } |
| 1766 | } |
| 1767 | |
Fabio Utzig | e575b0b | 2019-09-11 12:34:23 -0300 | [diff] [blame] | 1768 | if (BOOT_IS_UPGRADE(bs->swap_type)) { |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1769 | rc = swap_set_copy_done(BOOT_CURR_IMG(state)); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1770 | if (rc != 0) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1771 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1772 | } |
| 1773 | } |
| 1774 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1775 | if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1776 | BOOT_LOG_ERR("panic!"); |
| 1777 | assert(0); |
| 1778 | |
| 1779 | /* Loop forever... */ |
| 1780 | while (1) {} |
| 1781 | } |
| 1782 | |
| 1783 | return rc; |
| 1784 | } |
| 1785 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
| 1786 | |
| 1787 | #if (BOOT_IMAGE_NUMBER > 1) |
| 1788 | /** |
| 1789 | * Review the validity of previously determined swap types of other images. |
| 1790 | * |
| 1791 | * @param aborted_swap The current image upgrade is a |
| 1792 | * partial/aborted swap. |
| 1793 | */ |
| 1794 | static void |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1795 | boot_review_image_swap_types(struct boot_loader_state *state, |
| 1796 | bool aborted_swap) |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1797 | { |
| 1798 | /* In that case if we rebooted in the middle of an image upgrade process, we |
| 1799 | * must review the validity of swap types, that were previously determined |
| 1800 | * for other images. The image_ok flag had not been set before the reboot |
| 1801 | * for any of the updated images (only the copy_done flag) and thus falsely |
| 1802 | * the REVERT swap type has been determined for the previous images that had |
| 1803 | * been updated before the reboot. |
| 1804 | * |
| 1805 | * There are two separate scenarios that we have to deal with: |
| 1806 | * |
| 1807 | * 1. The reboot has happened during swapping an image: |
| 1808 | * The current image upgrade has been determined as a |
| 1809 | * partial/aborted swap. |
| 1810 | * 2. The reboot has happened between two separate image upgrades: |
| 1811 | * In this scenario we must check the swap type of the current image. |
| 1812 | * In those cases if it is NONE or REVERT we cannot certainly determine |
| 1813 | * the fact of a reboot. In a consistent state images must move in the |
| 1814 | * same direction or stay in place, e.g. in practice REVERT and TEST |
| 1815 | * swap types cannot be present at the same time. If the swap type of |
| 1816 | * the current image is either TEST, PERM or FAIL we must review the |
| 1817 | * already determined swap types of other images and set each false |
| 1818 | * REVERT swap types to NONE (these images had been successfully |
| 1819 | * updated before the system rebooted between two separate image |
| 1820 | * upgrades). |
| 1821 | */ |
| 1822 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1823 | if (BOOT_CURR_IMG(state) == 0) { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1824 | /* Nothing to do */ |
| 1825 | return; |
| 1826 | } |
| 1827 | |
| 1828 | if (!aborted_swap) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1829 | if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) || |
| 1830 | (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1831 | /* Nothing to do */ |
| 1832 | return; |
| 1833 | } |
| 1834 | } |
| 1835 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1836 | for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) { |
| 1837 | if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) { |
| 1838 | state->swap_type[i] = BOOT_SWAP_TYPE_NONE; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1839 | } |
| 1840 | } |
| 1841 | } |
| 1842 | #endif |
| 1843 | |
| 1844 | /** |
| 1845 | * Prepare image to be updated if required. |
| 1846 | * |
| 1847 | * Prepare image to be updated if required with completing an image swap |
| 1848 | * operation if one was aborted and/or determining the type of the |
| 1849 | * swap operation. In case of any error set the swap type to NONE. |
| 1850 | * |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1851 | * @param state TODO |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1852 | * @param bs Pointer where the read and possibly updated |
| 1853 | * boot status can be written to. |
| 1854 | */ |
| 1855 | static void |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1856 | boot_prepare_image_for_update(struct boot_loader_state *state, |
| 1857 | struct boot_status *bs) |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1858 | { |
| 1859 | int rc; |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 1860 | FIH_DECLARE(fih_rc, FIH_FAILURE); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1861 | |
Jamie McCrae | e261b28 | 2024-07-26 11:48:19 +0100 | [diff] [blame] | 1862 | #if defined(MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO) || defined(MCUBOOT_DATA_SHARING) |
Jamie McCrae | 4f1ab9e | 2024-07-26 12:29:24 +0100 | [diff] [blame] | 1863 | int max_size; |
| 1864 | #endif |
| 1865 | |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1866 | /* Determine the sector layout of the image slots and scratch area. */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1867 | rc = boot_read_sectors(state); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1868 | if (rc != 0) { |
| 1869 | BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d" |
| 1870 | " - too small?", BOOT_MAX_IMG_SECTORS); |
| 1871 | /* Unable to determine sector layout, continue with next image |
| 1872 | * if there is one. |
| 1873 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1874 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE; |
Andrzej Puzdrowski | 54b4ad9 | 2021-06-22 13:21:22 +0200 | [diff] [blame] | 1875 | if (rc == BOOT_EFLASH) |
| 1876 | { |
| 1877 | /* Only return on error from the primary image flash */ |
| 1878 | return; |
| 1879 | } |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | /* Attempt to read an image header from each slot. */ |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1883 | rc = boot_read_image_headers(state, false, NULL); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1884 | if (rc != 0) { |
| 1885 | /* Continue with next image if there is one. */ |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1886 | BOOT_LOG_WRN("Failed reading image headers; Image=%u", |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1887 | BOOT_CURR_IMG(state)); |
| 1888 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1889 | return; |
| 1890 | } |
| 1891 | |
Jamie McCrae | e261b28 | 2024-07-26 11:48:19 +0100 | [diff] [blame] | 1892 | #if defined(MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO) || defined(MCUBOOT_DATA_SHARING) |
Jamie McCrae | 4f1ab9e | 2024-07-26 12:29:24 +0100 | [diff] [blame] | 1893 | /* Fetch information on maximum sizes for later usage, if needed */ |
| 1894 | max_size = app_max_size(state); |
| 1895 | |
| 1896 | if (max_size > 0) { |
| 1897 | image_max_sizes[BOOT_CURR_IMG(state)].calculated = true; |
| 1898 | image_max_sizes[BOOT_CURR_IMG(state)].max_size = max_size; |
| 1899 | } |
| 1900 | #endif |
| 1901 | |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1902 | /* If the current image's slots aren't compatible, no swap is possible. |
| 1903 | * Just boot into primary slot. |
| 1904 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1905 | if (boot_slots_compatible(state)) { |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1906 | boot_status_reset(bs); |
| 1907 | |
| 1908 | #ifndef MCUBOOT_OVERWRITE_ONLY |
| 1909 | rc = swap_read_status(state, bs); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1910 | if (rc != 0) { |
| 1911 | BOOT_LOG_WRN("Failed reading boot status; Image=%u", |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1912 | BOOT_CURR_IMG(state)); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1913 | /* Continue with next image if there is one. */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1914 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1915 | return; |
| 1916 | } |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1917 | #endif |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1918 | |
Thomas Altenbach | f515bb1 | 2024-04-26 18:31:57 +0200 | [diff] [blame] | 1919 | #if defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE) |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 1920 | /* |
| 1921 | * Must re-read image headers because the boot status might |
| 1922 | * have been updated in the previous function call. |
| 1923 | */ |
| 1924 | rc = boot_read_image_headers(state, !boot_status_is_reset(bs), bs); |
Fabio Utzig | 32afe85 | 2020-10-04 10:36:02 -0300 | [diff] [blame] | 1925 | #ifdef MCUBOOT_BOOTSTRAP |
| 1926 | /* When bootstrapping it's OK to not have image magic in the primary slot */ |
| 1927 | if (rc != 0 && (BOOT_CURR_IMG(state) != BOOT_PRIMARY_SLOT || |
| 1928 | boot_check_header_erased(state, BOOT_PRIMARY_SLOT) != 0)) { |
| 1929 | #else |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 1930 | if (rc != 0) { |
Fabio Utzig | 32afe85 | 2020-10-04 10:36:02 -0300 | [diff] [blame] | 1931 | #endif |
| 1932 | |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 1933 | /* Continue with next image if there is one. */ |
| 1934 | BOOT_LOG_WRN("Failed reading image headers; Image=%u", |
| 1935 | BOOT_CURR_IMG(state)); |
| 1936 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE; |
| 1937 | return; |
| 1938 | } |
| 1939 | #endif |
| 1940 | |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1941 | /* Determine if we rebooted in the middle of an image swap |
| 1942 | * operation. If a partial swap was detected, complete it. |
| 1943 | */ |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1944 | if (!boot_status_is_reset(bs)) { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1945 | |
| 1946 | #if (BOOT_IMAGE_NUMBER > 1) |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1947 | boot_review_image_swap_types(state, true); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1948 | #endif |
| 1949 | |
| 1950 | #ifdef MCUBOOT_OVERWRITE_ONLY |
| 1951 | /* Should never arrive here, overwrite-only mode has |
| 1952 | * no swap state. |
| 1953 | */ |
| 1954 | assert(0); |
| 1955 | #else |
| 1956 | /* Determine the type of swap operation being resumed from the |
| 1957 | * `swap-type` trailer field. |
| 1958 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1959 | rc = boot_complete_partial_swap(state, bs); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1960 | assert(rc == 0); |
| 1961 | #endif |
| 1962 | /* Attempt to read an image header from each slot. Ensure that |
| 1963 | * image headers in slots are aligned with headers in boot_data. |
| 1964 | */ |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1965 | rc = boot_read_image_headers(state, false, bs); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1966 | assert(rc == 0); |
| 1967 | |
| 1968 | /* Swap has finished set to NONE */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1969 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1970 | } else { |
| 1971 | /* There was no partial swap, determine swap type. */ |
| 1972 | if (bs->swap_type == BOOT_SWAP_TYPE_NONE) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1973 | BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1974 | } else { |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 1975 | FIH_CALL(boot_validate_slot, fih_rc, |
| 1976 | state, BOOT_SECONDARY_SLOT, bs); |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 1977 | if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) { |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 1978 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL; |
| 1979 | } else { |
| 1980 | BOOT_SWAP_TYPE(state) = bs->swap_type; |
| 1981 | } |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1982 | } |
| 1983 | |
| 1984 | #if (BOOT_IMAGE_NUMBER > 1) |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1985 | boot_review_image_swap_types(state, false); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1986 | #endif |
| 1987 | |
| 1988 | #ifdef MCUBOOT_BOOTSTRAP |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1989 | if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1990 | /* Header checks are done first because they are |
| 1991 | * inexpensive. Since overwrite-only copies starting from |
| 1992 | * offset 0, if interrupted, it might leave a valid header |
| 1993 | * magic, so also run validation on the primary slot to be |
| 1994 | * sure it's not OK. |
| 1995 | */ |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 1996 | rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT); |
| 1997 | FIH_CALL(boot_validate_slot, fih_rc, |
| 1998 | state, BOOT_PRIMARY_SLOT, bs); |
| 1999 | |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 2000 | if (rc == 0 || FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) { |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 2001 | |
Fabio Utzig | 3d77c95 | 2020-10-04 10:23:17 -0300 | [diff] [blame] | 2002 | rc = (boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC) ? 1: 0; |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 2003 | FIH_CALL(boot_validate_slot, fih_rc, |
| 2004 | state, BOOT_SECONDARY_SLOT, bs); |
| 2005 | |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 2006 | if (rc == 1 && FIH_EQ(fih_rc, FIH_SUCCESS)) { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2007 | /* Set swap type to REVERT to overwrite the primary |
| 2008 | * slot with the image contained in secondary slot |
| 2009 | * and to trigger the explicit setting of the |
| 2010 | * image_ok flag. |
| 2011 | */ |
Fabio Utzig | 59b63e5 | 2019-09-10 12:22:35 -0300 | [diff] [blame] | 2012 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2013 | } |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 2014 | } |
| 2015 | } |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 2016 | #endif |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 2017 | } |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2018 | } else { |
| 2019 | /* In that case if slots are not compatible. */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2020 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE; |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 2021 | } |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 2022 | } |
| 2023 | |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2024 | /** |
| 2025 | * Updates the security counter for the current image. |
| 2026 | * |
| 2027 | * @param state Boot loader status information. |
| 2028 | * |
| 2029 | * @return 0 on success; nonzero on failure. |
| 2030 | */ |
| 2031 | static int |
| 2032 | boot_update_hw_rollback_protection(struct boot_loader_state *state) |
| 2033 | { |
| 2034 | #ifdef MCUBOOT_HW_ROLLBACK_PROT |
| 2035 | int rc; |
| 2036 | |
| 2037 | /* Update the stored security counter with the active image's security |
| 2038 | * counter value. It will only be updated if the new security counter is |
| 2039 | * greater than the stored value. |
| 2040 | * |
| 2041 | * In case of a successful image swapping when the swap type is TEST the |
| 2042 | * security counter can be increased only after a reset, when the swap |
| 2043 | * type is NONE and the image has marked itself "OK" (the image_ok flag |
| 2044 | * has been set). This way a "revert" can be performed when it's |
| 2045 | * necessary. |
| 2046 | */ |
| 2047 | if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) { |
| 2048 | rc = boot_update_security_counter( |
| 2049 | BOOT_CURR_IMG(state), |
| 2050 | BOOT_PRIMARY_SLOT, |
| 2051 | boot_img_hdr(state, BOOT_PRIMARY_SLOT)); |
| 2052 | if (rc != 0) { |
| 2053 | BOOT_LOG_ERR("Security counter update failed after image " |
| 2054 | "validation."); |
| 2055 | return rc; |
| 2056 | } |
| 2057 | } |
| 2058 | |
| 2059 | return 0; |
| 2060 | |
| 2061 | #else /* MCUBOOT_HW_ROLLBACK_PROT */ |
| 2062 | (void) (state); |
| 2063 | |
| 2064 | return 0; |
| 2065 | #endif |
| 2066 | } |
| 2067 | |
Jerzy Kasenberg | e3f895d | 2022-04-12 15:05:33 +0200 | [diff] [blame] | 2068 | /** |
| 2069 | * Checks test swap downgrade prevention conditions. |
| 2070 | * |
| 2071 | * Function called only for swap upgrades test run. It may prevent |
| 2072 | * swap if slot 1 image has <= version number or < security counter |
| 2073 | * |
| 2074 | * @param state Boot loader status information. |
| 2075 | * |
| 2076 | * @return 0 - image can be swapped, -1 downgrade prevention |
| 2077 | */ |
| 2078 | static int |
| 2079 | check_downgrade_prevention(struct boot_loader_state *state) |
| 2080 | { |
| 2081 | #if defined(MCUBOOT_DOWNGRADE_PREVENTION) && \ |
| 2082 | (defined(MCUBOOT_SWAP_USING_MOVE) || defined(MCUBOOT_SWAP_USING_SCRATCH)) |
| 2083 | uint32_t security_counter[2]; |
| 2084 | int rc; |
| 2085 | |
| 2086 | if (MCUBOOT_DOWNGRADE_PREVENTION_SECURITY_COUNTER) { |
| 2087 | /* If there was security no counter in slot 0, allow swap */ |
| 2088 | rc = bootutil_get_img_security_cnt(&(BOOT_IMG(state, 0).hdr), |
| 2089 | BOOT_IMG(state, 0).area, |
| 2090 | &security_counter[0]); |
| 2091 | if (rc != 0) { |
| 2092 | return 0; |
| 2093 | } |
| 2094 | /* If there is no security counter in slot 1, or it's lower than |
| 2095 | * that of slot 0, prevent downgrade */ |
| 2096 | rc = bootutil_get_img_security_cnt(&(BOOT_IMG(state, 1).hdr), |
| 2097 | BOOT_IMG(state, 1).area, |
| 2098 | &security_counter[1]); |
| 2099 | if (rc != 0 || security_counter[0] > security_counter[1]) { |
| 2100 | rc = -1; |
| 2101 | } |
| 2102 | } |
| 2103 | else { |
| 2104 | rc = boot_version_cmp(&boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver, |
| 2105 | &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver); |
| 2106 | } |
| 2107 | if (rc < 0) { |
| 2108 | /* Image in slot 0 prevents downgrade, delete image in slot 1 */ |
Antonio de Angelis | 4854700 | 2023-04-14 09:47:09 +0100 | [diff] [blame] | 2109 | BOOT_LOG_INF("Image %d in slot 1 erased due to downgrade prevention", BOOT_CURR_IMG(state)); |
Jerzy Kasenberg | e3f895d | 2022-04-12 15:05:33 +0200 | [diff] [blame] | 2110 | flash_area_erase(BOOT_IMG(state, 1).area, 0, |
| 2111 | flash_area_get_size(BOOT_IMG(state, 1).area)); |
| 2112 | } else { |
| 2113 | rc = 0; |
| 2114 | } |
| 2115 | return rc; |
| 2116 | #else |
| 2117 | (void)state; |
| 2118 | return 0; |
| 2119 | #endif |
| 2120 | } |
| 2121 | |
Jamie McCrae | e261b28 | 2024-07-26 11:48:19 +0100 | [diff] [blame] | 2122 | #if !defined(__BOOTSIM__) |
| 2123 | static void boot_get_sector_buffers(struct sector_buffer_t *buffers) |
| 2124 | { |
| 2125 | /* The array of slot sectors are defined here (as opposed to file scope) so |
| 2126 | * that they don't get allocated for non-boot-loader apps. This is |
| 2127 | * necessary because the gcc option "-fdata-sections" doesn't seem to have |
| 2128 | * any effect in older gcc versions (e.g., 4.8.4). |
| 2129 | */ |
| 2130 | static boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS]; |
| 2131 | static boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS]; |
| 2132 | #if MCUBOOT_SWAP_USING_SCRATCH |
| 2133 | static boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS]; |
| 2134 | #endif |
| 2135 | |
| 2136 | buffers->primary = (boot_sector_t *)&primary_slot_sectors; |
| 2137 | buffers->secondary = (boot_sector_t *)&secondary_slot_sectors; |
| 2138 | #if MCUBOOT_SWAP_USING_SCRATCH |
| 2139 | buffers->scratch = (boot_sector_t *)&scratch_sectors; |
| 2140 | #endif |
| 2141 | } |
| 2142 | #endif |
| 2143 | |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 2144 | fih_ret |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2145 | context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2146 | { |
Marti Bolivar | 8489865 | 2017-06-13 17:20:22 -0400 | [diff] [blame] | 2147 | size_t slot; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2148 | struct boot_status bs; |
Jamie McCrae | e261b28 | 2024-07-26 11:48:19 +0100 | [diff] [blame] | 2149 | #if !defined(__BOOTSIM__) |
| 2150 | struct sector_buffer_t sector_buffers; |
| 2151 | #endif |
David Vincze | 9015a5d | 2020-05-18 14:43:11 +0200 | [diff] [blame] | 2152 | int rc = -1; |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 2153 | FIH_DECLARE(fih_rc, FIH_FAILURE); |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 2154 | int fa_id; |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 2155 | int image_index; |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 2156 | bool has_upgrade; |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 2157 | volatile int fih_cnt; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2158 | |
Jamie McCrae | e261b28 | 2024-07-26 11:48:19 +0100 | [diff] [blame] | 2159 | #if defined(__BOOTSIM__) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2160 | /* The array of slot sectors are defined here (as opposed to file scope) so |
| 2161 | * that they don't get allocated for non-boot-loader apps. This is |
| 2162 | * necessary because the gcc option "-fdata-sections" doesn't seem to have |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 2163 | * any effect in older gcc versions (e.g., 4.8.4). |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2164 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2165 | TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS]; |
| 2166 | TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS]; |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 2167 | #if MCUBOOT_SWAP_USING_SCRATCH |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2168 | TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS]; |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 2169 | #endif |
Jamie McCrae | e261b28 | 2024-07-26 11:48:19 +0100 | [diff] [blame] | 2170 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2171 | |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 2172 | has_upgrade = false; |
| 2173 | |
| 2174 | #if (BOOT_IMAGE_NUMBER == 1) |
| 2175 | (void)has_upgrade; |
| 2176 | #endif |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 2177 | |
Jamie McCrae | e261b28 | 2024-07-26 11:48:19 +0100 | [diff] [blame] | 2178 | #if !defined(__BOOTSIM__) |
| 2179 | boot_get_sector_buffers(§or_buffers); |
| 2180 | #endif |
| 2181 | |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2182 | /* Iterate over all the images. By the end of the loop the swap type has |
| 2183 | * to be determined for each image and all aborted swaps have to be |
| 2184 | * completed. |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 2185 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2186 | IMAGES_ITER(BOOT_CURR_IMG(state)) { |
Raef Coles | f11de64 | 2021-10-15 11:11:56 +0100 | [diff] [blame] | 2187 | #if BOOT_IMAGE_NUMBER > 1 |
| 2188 | if (state->img_mask[BOOT_CURR_IMG(state)]) { |
| 2189 | continue; |
| 2190 | } |
| 2191 | #endif |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2192 | #if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1) |
| 2193 | /* The keys used for encryption may no longer be valid (could belong to |
| 2194 | * another images). Therefore, mark them as invalid to force their reload |
| 2195 | * by boot_enc_load(). |
Fabio Utzig | db5bd3c | 2017-07-13 22:20:22 -0300 | [diff] [blame] | 2196 | */ |
Fabio Utzig | 1e4284b | 2019-08-23 11:55:27 -0300 | [diff] [blame] | 2197 | boot_enc_zeroize(BOOT_CURR_ENC(state)); |
David Brown | 554c52e | 2017-06-30 16:01:07 -0600 | [diff] [blame] | 2198 | #endif |
| 2199 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2200 | image_index = BOOT_CURR_IMG(state); |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 2201 | |
Jamie McCrae | e261b28 | 2024-07-26 11:48:19 +0100 | [diff] [blame] | 2202 | #if !defined(__BOOTSIM__) |
| 2203 | BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors = |
| 2204 | §or_buffers.primary[image_index]; |
| 2205 | BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors = |
| 2206 | §or_buffers.secondary[image_index]; |
| 2207 | #if MCUBOOT_SWAP_USING_SCRATCH |
| 2208 | state->scratch.sectors = sector_buffers.scratch; |
| 2209 | #endif |
| 2210 | #else |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2211 | BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors = |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 2212 | primary_slot_sectors[image_index]; |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2213 | BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors = |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 2214 | secondary_slot_sectors[image_index]; |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 2215 | #if MCUBOOT_SWAP_USING_SCRATCH |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2216 | state->scratch.sectors = scratch_sectors; |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 2217 | #endif |
Jamie McCrae | e261b28 | 2024-07-26 11:48:19 +0100 | [diff] [blame] | 2218 | #endif |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2219 | |
| 2220 | /* Open primary and secondary image areas for the duration |
| 2221 | * of this call. |
| 2222 | */ |
| 2223 | for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) { |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 2224 | fa_id = flash_area_id_from_multi_image_slot(image_index, slot); |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2225 | rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot)); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2226 | assert(rc == 0); |
Jamie McCrae | 2929a97 | 2023-09-25 11:12:20 +0100 | [diff] [blame] | 2227 | |
| 2228 | if (rc != 0) { |
| 2229 | BOOT_LOG_ERR("Failed to open flash area ID %d (image %d slot %d): %d, " |
| 2230 | "cannot continue", fa_id, image_index, (int8_t)slot, rc); |
| 2231 | FIH_PANIC; |
| 2232 | } |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2233 | } |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 2234 | #if MCUBOOT_SWAP_USING_SCRATCH |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2235 | rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2236 | &BOOT_SCRATCH_AREA(state)); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2237 | assert(rc == 0); |
Jamie McCrae | 2929a97 | 2023-09-25 11:12:20 +0100 | [diff] [blame] | 2238 | |
| 2239 | if (rc != 0) { |
| 2240 | BOOT_LOG_ERR("Failed to open scratch flash area: %d, cannot continue", rc); |
| 2241 | FIH_PANIC; |
| 2242 | } |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 2243 | #endif |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2244 | |
| 2245 | /* Determine swap type and complete swap if it has been aborted. */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2246 | boot_prepare_image_for_update(state, &bs); |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 2247 | |
Fabio Utzig | e575b0b | 2019-09-11 12:34:23 -0300 | [diff] [blame] | 2248 | if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) { |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 2249 | has_upgrade = true; |
| 2250 | } |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2251 | } |
| 2252 | |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 2253 | #if (BOOT_IMAGE_NUMBER > 1) |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 2254 | if (has_upgrade) { |
| 2255 | /* Iterate over all the images and verify whether the image dependencies |
| 2256 | * are all satisfied and update swap type if necessary. |
| 2257 | */ |
| 2258 | rc = boot_verify_dependencies(state); |
David Vincze | 8b0b637 | 2020-05-20 19:54:44 +0200 | [diff] [blame] | 2259 | if (rc != 0) { |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 2260 | /* |
| 2261 | * It was impossible to upgrade because the expected dependency version |
| 2262 | * was not available. Here we already changed the swap_type so that |
| 2263 | * instead of asserting the bootloader, we continue and no upgrade is |
| 2264 | * performed. |
| 2265 | */ |
| 2266 | rc = 0; |
| 2267 | } |
| 2268 | } |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 2269 | #endif |
| 2270 | |
Jamie McCrae | 56cb610 | 2022-03-23 11:57:03 +0000 | [diff] [blame] | 2271 | /* Trigger status change callback with upgrading status */ |
| 2272 | mcuboot_status_change(MCUBOOT_STATUS_UPGRADING); |
| 2273 | |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2274 | /* Iterate over all the images. At this point there are no aborted swaps |
| 2275 | * and the swap types are determined for each image. By the end of the loop |
| 2276 | * all required update operations will have been finished. |
| 2277 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2278 | IMAGES_ITER(BOOT_CURR_IMG(state)) { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2279 | #if (BOOT_IMAGE_NUMBER > 1) |
Raef Coles | f11de64 | 2021-10-15 11:11:56 +0100 | [diff] [blame] | 2280 | if (state->img_mask[BOOT_CURR_IMG(state)]) { |
| 2281 | continue; |
| 2282 | } |
| 2283 | |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2284 | #ifdef MCUBOOT_ENC_IMAGES |
| 2285 | /* The keys used for encryption may no longer be valid (could belong to |
| 2286 | * another images). Therefore, mark them as invalid to force their reload |
| 2287 | * by boot_enc_load(). |
| 2288 | */ |
Fabio Utzig | 1e4284b | 2019-08-23 11:55:27 -0300 | [diff] [blame] | 2289 | boot_enc_zeroize(BOOT_CURR_ENC(state)); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2290 | #endif /* MCUBOOT_ENC_IMAGES */ |
| 2291 | |
| 2292 | /* Indicate that swap is not aborted */ |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 2293 | boot_status_reset(&bs); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2294 | #endif /* (BOOT_IMAGE_NUMBER > 1) */ |
| 2295 | |
| 2296 | /* Set the previously determined swap type */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2297 | bs.swap_type = BOOT_SWAP_TYPE(state); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2298 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2299 | switch (BOOT_SWAP_TYPE(state)) { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2300 | case BOOT_SWAP_TYPE_NONE: |
| 2301 | break; |
| 2302 | |
Jerzy Kasenberg | e3f895d | 2022-04-12 15:05:33 +0200 | [diff] [blame] | 2303 | case BOOT_SWAP_TYPE_TEST: |
Michael Grand | 99613c6 | 2023-06-20 15:01:00 +0200 | [diff] [blame] | 2304 | /* fallthrough */ |
| 2305 | case BOOT_SWAP_TYPE_PERM: |
Jerzy Kasenberg | e3f895d | 2022-04-12 15:05:33 +0200 | [diff] [blame] | 2306 | if (check_downgrade_prevention(state) != 0) { |
| 2307 | /* Downgrade prevented */ |
| 2308 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE; |
| 2309 | break; |
| 2310 | } |
| 2311 | /* fallthrough */ |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2312 | case BOOT_SWAP_TYPE_REVERT: |
Andrzej Puzdrowski | b8f3969 | 2021-07-02 15:05:37 +0200 | [diff] [blame] | 2313 | rc = BOOT_HOOK_CALL(boot_perform_update_hook, BOOT_HOOK_REGULAR, |
| 2314 | BOOT_CURR_IMG(state), &(BOOT_IMG(state, 1).hdr), |
| 2315 | BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT)); |
| 2316 | if (rc == BOOT_HOOK_REGULAR) |
| 2317 | { |
| 2318 | rc = boot_perform_update(state, &bs); |
| 2319 | } |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2320 | assert(rc == 0); |
| 2321 | break; |
| 2322 | |
| 2323 | case BOOT_SWAP_TYPE_FAIL: |
| 2324 | /* The image in secondary slot was invalid and is now erased. Ensure |
| 2325 | * we don't try to boot into it again on the next reboot. Do this by |
| 2326 | * pretending we just reverted back to primary slot. |
| 2327 | */ |
| 2328 | #ifndef MCUBOOT_OVERWRITE_ONLY |
| 2329 | /* image_ok needs to be explicitly set to avoid a new revert. */ |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 2330 | rc = swap_set_image_ok(BOOT_CURR_IMG(state)); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2331 | if (rc != 0) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2332 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2333 | } |
| 2334 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
| 2335 | break; |
| 2336 | |
| 2337 | default: |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2338 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2339 | } |
| 2340 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2341 | if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2342 | BOOT_LOG_ERR("panic!"); |
| 2343 | assert(0); |
| 2344 | |
| 2345 | /* Loop forever... */ |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 2346 | FIH_PANIC; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2347 | } |
| 2348 | } |
| 2349 | |
| 2350 | /* Iterate over all the images. At this point all required update operations |
| 2351 | * have finished. By the end of the loop each image in the primary slot will |
| 2352 | * have been re-validated. |
| 2353 | */ |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 2354 | FIH_SET(fih_cnt, 0); |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2355 | IMAGES_ITER(BOOT_CURR_IMG(state)) { |
Raef Coles | f11de64 | 2021-10-15 11:11:56 +0100 | [diff] [blame] | 2356 | #if BOOT_IMAGE_NUMBER > 1 |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 2357 | /* Hardenned to prevent from skipping check of a given image, |
| 2358 | * tmp_img_mask is declared volatile |
| 2359 | */ |
| 2360 | volatile bool tmp_img_mask; |
| 2361 | FIH_SET(tmp_img_mask, state->img_mask[BOOT_CURR_IMG(state)]); |
| 2362 | if (FIH_EQ(tmp_img_mask, true)) { |
| 2363 | ++fih_cnt; |
Raef Coles | f11de64 | 2021-10-15 11:11:56 +0100 | [diff] [blame] | 2364 | continue; |
| 2365 | } |
| 2366 | #endif |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2367 | if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2368 | /* Attempt to read an image header from each slot. Ensure that image |
| 2369 | * headers in slots are aligned with headers in boot_data. |
Dominik Ermel | 6f7f873 | 2024-02-01 11:59:24 +0000 | [diff] [blame] | 2370 | * Note: Quite complicated internal logic of boot_read_image_headers |
| 2371 | * uses boot state, the last parm, to figure out in which slot which |
| 2372 | * header is located; when boot state is not provided, then it |
| 2373 | * is assumed that headers are at proper slots (we are not in |
| 2374 | * the middle of moving images, etc). |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2375 | */ |
Dominik Ermel | 6f7f873 | 2024-02-01 11:59:24 +0000 | [diff] [blame] | 2376 | rc = boot_read_image_headers(state, false, NULL); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2377 | if (rc != 0) { |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 2378 | FIH_SET(fih_rc, FIH_FAILURE); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2379 | goto out; |
| 2380 | } |
| 2381 | /* Since headers were reloaded, it can be assumed we just performed |
| 2382 | * a swap or overwrite. Now the header info that should be used to |
| 2383 | * provide the data for the bootstrap, which previously was at |
| 2384 | * secondary slot, was updated to primary slot. |
| 2385 | */ |
| 2386 | } |
| 2387 | |
| 2388 | #ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 2389 | FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, NULL); |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 2390 | /* Check for all possible values is redundant in normal operation it |
| 2391 | * is meant to prevent FI attack. |
| 2392 | */ |
| 2393 | if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS) || |
| 2394 | FIH_EQ(fih_rc, FIH_FAILURE) || |
| 2395 | FIH_EQ(fih_rc, FIH_NO_BOOTABLE_IMAGE)) { |
| 2396 | FIH_SET(fih_rc, FIH_FAILURE); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2397 | goto out; |
| 2398 | } |
| 2399 | #else |
| 2400 | /* Even if we're not re-validating the primary slot, we could be booting |
| 2401 | * onto an empty flash chip. At least do a basic sanity check that |
| 2402 | * the magic number on the image is OK. |
| 2403 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2404 | if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2405 | BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long) |
Dominik Ermel | 4a4d1ac | 2021-08-06 11:23:52 +0000 | [diff] [blame] | 2406 | BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic, |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2407 | BOOT_CURR_IMG(state)); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2408 | rc = BOOT_EBADIMAGE; |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 2409 | FIH_SET(fih_rc, FIH_FAILURE); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2410 | goto out; |
| 2411 | } |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 2412 | #endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */ |
| 2413 | |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2414 | rc = boot_update_hw_rollback_protection(state); |
David Vincze | 1cf11b5 | 2020-03-24 07:51:09 +0100 | [diff] [blame] | 2415 | if (rc != 0) { |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 2416 | FIH_SET(fih_rc, FIH_FAILURE); |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 2417 | goto out; |
David Vincze | 1cf11b5 | 2020-03-24 07:51:09 +0100 | [diff] [blame] | 2418 | } |
David Vincze | 1cf11b5 | 2020-03-24 07:51:09 +0100 | [diff] [blame] | 2419 | |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2420 | rc = boot_add_shared_data(state, BOOT_PRIMARY_SLOT); |
David Vincze | 1cf11b5 | 2020-03-24 07:51:09 +0100 | [diff] [blame] | 2421 | if (rc != 0) { |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 2422 | FIH_SET(fih_rc, FIH_FAILURE); |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 2423 | goto out; |
David Vincze | 1cf11b5 | 2020-03-24 07:51:09 +0100 | [diff] [blame] | 2424 | } |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 2425 | ++fih_cnt; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2426 | } |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 2427 | /* |
| 2428 | * fih_cnt should be equal to BOOT_IMAGE_NUMBER now. |
| 2429 | * If this is not the case, at least one iteration of the loop |
| 2430 | * has been skipped. |
| 2431 | */ |
| 2432 | if(FIH_NOT_EQ(fih_cnt, BOOT_IMAGE_NUMBER)) { |
| 2433 | FIH_PANIC; |
| 2434 | } |
Fabio Utzig | f616c54 | 2019-12-19 15:23:32 -0300 | [diff] [blame] | 2435 | |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 2436 | fill_rsp(state, rsp); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2437 | |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 2438 | fih_rc = FIH_SUCCESS; |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 2439 | out: |
Dominik Ermel | 256bc37 | 2022-12-07 15:51:31 +0000 | [diff] [blame] | 2440 | /* |
| 2441 | * Since the boot_status struct stores plaintext encryption keys, reset |
| 2442 | * them here to avoid the possibility of jumping into an image that could |
| 2443 | * easily recover them. |
| 2444 | */ |
| 2445 | #if defined(MCUBOOT_ENC_IMAGES) || defined(MCUBOOT_SWAP_SAVE_ENCTLV) |
| 2446 | like_mbedtls_zeroize(&bs, sizeof(bs)); |
| 2447 | #else |
| 2448 | memset(&bs, 0, sizeof(struct boot_status)); |
| 2449 | #endif |
| 2450 | |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2451 | close_all_flash_areas(state); |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 2452 | FIH_RET(fih_rc); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2453 | } |
| 2454 | |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 2455 | fih_ret |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2456 | split_go(int loader_slot, int split_slot, void **entry) |
| 2457 | { |
Marti Bolivar | c50926f | 2017-06-14 09:35:40 -0400 | [diff] [blame] | 2458 | boot_sector_t *sectors; |
Christopher Collins | 034a620 | 2017-01-11 12:19:37 -0800 | [diff] [blame] | 2459 | uintptr_t entry_val; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2460 | int loader_flash_id; |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 2461 | int split_flash_id; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2462 | int rc; |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 2463 | FIH_DECLARE(fih_rc, FIH_FAILURE); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2464 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2465 | sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors); |
| 2466 | if (sectors == NULL) { |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 2467 | FIH_RET(FIH_FAILURE); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2468 | } |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2469 | BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0; |
| 2470 | BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS; |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 2471 | |
| 2472 | loader_flash_id = flash_area_id_from_image_slot(loader_slot); |
| 2473 | rc = flash_area_open(loader_flash_id, |
Alvaro Prieto | 63a2bdb | 2019-07-04 12:18:49 -0700 | [diff] [blame] | 2474 | &BOOT_IMG_AREA(&boot_data, loader_slot)); |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 2475 | assert(rc == 0); |
| 2476 | split_flash_id = flash_area_id_from_image_slot(split_slot); |
| 2477 | rc = flash_area_open(split_flash_id, |
| 2478 | &BOOT_IMG_AREA(&boot_data, split_slot)); |
| 2479 | assert(rc == 0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2480 | |
| 2481 | /* Determine the sector layout of the image slots and scratch area. */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2482 | rc = boot_read_sectors(&boot_data); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2483 | if (rc != 0) { |
| 2484 | rc = SPLIT_GO_ERR; |
| 2485 | goto done; |
| 2486 | } |
| 2487 | |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 2488 | rc = boot_read_image_headers(&boot_data, true, NULL); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2489 | if (rc != 0) { |
| 2490 | goto done; |
| 2491 | } |
| 2492 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2493 | /* Don't check the bootable image flag because we could really call a |
| 2494 | * bootable or non-bootable image. Just validate that the image check |
| 2495 | * passes which is distinct from the normal check. |
| 2496 | */ |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 2497 | FIH_CALL(split_image_check, fih_rc, |
| 2498 | boot_img_hdr(&boot_data, split_slot), |
| 2499 | BOOT_IMG_AREA(&boot_data, split_slot), |
| 2500 | boot_img_hdr(&boot_data, loader_slot), |
| 2501 | BOOT_IMG_AREA(&boot_data, loader_slot)); |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 2502 | if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2503 | goto done; |
| 2504 | } |
| 2505 | |
Marti Bolivar | ea08887 | 2017-06-12 17:10:49 -0400 | [diff] [blame] | 2506 | entry_val = boot_img_slot_off(&boot_data, split_slot) + |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 2507 | boot_img_hdr(&boot_data, split_slot)->ih_hdr_size; |
Christopher Collins | 034a620 | 2017-01-11 12:19:37 -0800 | [diff] [blame] | 2508 | *entry = (void *) entry_val; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2509 | rc = SPLIT_GO_OK; |
| 2510 | |
| 2511 | done: |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 2512 | flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot)); |
| 2513 | flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot)); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2514 | free(sectors); |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 2515 | |
| 2516 | if (rc) { |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 2517 | FIH_SET(fih_rc, FIH_FAILURE); |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 2518 | } |
| 2519 | |
| 2520 | FIH_RET(fih_rc); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2521 | } |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 2522 | |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 2523 | #else /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */ |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 2524 | |
| 2525 | /** |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2526 | * Opens all flash areas and checks which contain an image with a valid header. |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 2527 | * |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2528 | * @param state Boot loader status information. |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 2529 | * |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2530 | * @return 0 on success; nonzero on failure. |
| 2531 | */ |
| 2532 | static int |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 2533 | boot_get_slot_usage(struct boot_loader_state *state) |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2534 | { |
| 2535 | uint32_t slot; |
| 2536 | int fa_id; |
| 2537 | int rc; |
| 2538 | struct image_header *hdr = NULL; |
| 2539 | |
| 2540 | IMAGES_ITER(BOOT_CURR_IMG(state)) { |
Raef Coles | f11de64 | 2021-10-15 11:11:56 +0100 | [diff] [blame] | 2541 | #if BOOT_IMAGE_NUMBER > 1 |
| 2542 | if (state->img_mask[BOOT_CURR_IMG(state)]) { |
| 2543 | continue; |
| 2544 | } |
| 2545 | #endif |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2546 | /* Open all the slots */ |
| 2547 | for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) { |
| 2548 | fa_id = flash_area_id_from_multi_image_slot( |
| 2549 | BOOT_CURR_IMG(state), slot); |
| 2550 | rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot)); |
| 2551 | assert(rc == 0); |
| 2552 | } |
| 2553 | |
| 2554 | /* Attempt to read an image header from each slot. */ |
| 2555 | rc = boot_read_image_headers(state, false, NULL); |
| 2556 | if (rc != 0) { |
| 2557 | BOOT_LOG_WRN("Failed reading image headers."); |
| 2558 | return rc; |
| 2559 | } |
| 2560 | |
| 2561 | /* Check headers in all slots */ |
| 2562 | for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) { |
| 2563 | hdr = boot_img_hdr(state, slot); |
| 2564 | |
Jamie McCrae | dbb5c78 | 2024-08-22 10:43:14 +0100 | [diff] [blame] | 2565 | if (boot_is_header_valid(hdr, BOOT_IMG_AREA(state, slot), state)) { |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 2566 | state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = true; |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2567 | BOOT_LOG_IMAGE_INFO(slot, hdr); |
| 2568 | } else { |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 2569 | state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = false; |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2570 | BOOT_LOG_INF("Image %d %s slot: Image not found", |
| 2571 | BOOT_CURR_IMG(state), |
| 2572 | (slot == BOOT_PRIMARY_SLOT) |
| 2573 | ? "Primary" : "Secondary"); |
| 2574 | } |
| 2575 | } |
| 2576 | |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 2577 | state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT; |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2578 | } |
| 2579 | |
| 2580 | return 0; |
| 2581 | } |
| 2582 | |
| 2583 | /** |
| 2584 | * Finds the slot containing the image with the highest version number for the |
| 2585 | * current image. |
| 2586 | * |
| 2587 | * @param state Boot loader status information. |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2588 | * |
| 2589 | * @return NO_ACTIVE_SLOT if no available slot found, number of |
| 2590 | * the found slot otherwise. |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 2591 | */ |
| 2592 | static uint32_t |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 2593 | find_slot_with_highest_version(struct boot_loader_state *state) |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 2594 | { |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 2595 | uint32_t slot; |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2596 | uint32_t candidate_slot = NO_ACTIVE_SLOT; |
| 2597 | int rc; |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 2598 | |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2599 | for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) { |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 2600 | if (state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot]) { |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2601 | if (candidate_slot == NO_ACTIVE_SLOT) { |
| 2602 | candidate_slot = slot; |
| 2603 | } else { |
| 2604 | rc = boot_version_cmp( |
| 2605 | &boot_img_hdr(state, slot)->ih_ver, |
| 2606 | &boot_img_hdr(state, candidate_slot)->ih_ver); |
| 2607 | if (rc == 1) { |
| 2608 | /* The version of the image being examined is greater than |
| 2609 | * the version of the current candidate. |
| 2610 | */ |
| 2611 | candidate_slot = slot; |
| 2612 | } |
| 2613 | } |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 2614 | } |
| 2615 | } |
| 2616 | |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2617 | return candidate_slot; |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 2618 | } |
| 2619 | |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2620 | #ifdef MCUBOOT_HAVE_LOGGING |
| 2621 | /** |
| 2622 | * Prints the state of the loaded images. |
| 2623 | * |
| 2624 | * @param state Boot loader status information. |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2625 | */ |
| 2626 | static void |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 2627 | print_loaded_images(struct boot_loader_state *state) |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2628 | { |
| 2629 | uint32_t active_slot; |
| 2630 | |
David Brown | 695e591 | 2021-05-24 16:58:01 -0600 | [diff] [blame] | 2631 | (void)state; |
| 2632 | |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2633 | IMAGES_ITER(BOOT_CURR_IMG(state)) { |
Raef Coles | f11de64 | 2021-10-15 11:11:56 +0100 | [diff] [blame] | 2634 | #if BOOT_IMAGE_NUMBER > 1 |
| 2635 | if (state->img_mask[BOOT_CURR_IMG(state)]) { |
| 2636 | continue; |
| 2637 | } |
| 2638 | #endif |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 2639 | active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot; |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2640 | |
| 2641 | BOOT_LOG_INF("Image %d loaded from the %s slot", |
| 2642 | BOOT_CURR_IMG(state), |
| 2643 | (active_slot == BOOT_PRIMARY_SLOT) ? |
| 2644 | "primary" : "secondary"); |
| 2645 | } |
| 2646 | } |
| 2647 | #endif |
| 2648 | |
David Vincze | 1c45624 | 2021-06-29 15:25:24 +0200 | [diff] [blame] | 2649 | #if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT) |
David Vincze | 505fba2 | 2020-10-22 13:53:29 +0200 | [diff] [blame] | 2650 | /** |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2651 | * Checks whether the active slot of the current image was previously selected |
| 2652 | * to run. Erases the image if it was selected but its execution failed, |
| 2653 | * otherwise marks it as selected if it has not been before. |
David Vincze | 505fba2 | 2020-10-22 13:53:29 +0200 | [diff] [blame] | 2654 | * |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2655 | * @param state Boot loader status information. |
David Vincze | 505fba2 | 2020-10-22 13:53:29 +0200 | [diff] [blame] | 2656 | * |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2657 | * @return 0 on success; nonzero on failure. |
David Vincze | 505fba2 | 2020-10-22 13:53:29 +0200 | [diff] [blame] | 2658 | */ |
| 2659 | static int |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 2660 | boot_select_or_erase(struct boot_loader_state *state) |
David Vincze | 505fba2 | 2020-10-22 13:53:29 +0200 | [diff] [blame] | 2661 | { |
| 2662 | const struct flash_area *fap; |
| 2663 | int fa_id; |
| 2664 | int rc; |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2665 | uint32_t active_slot; |
| 2666 | struct boot_swap_state* active_swap_state; |
David Vincze | 505fba2 | 2020-10-22 13:53:29 +0200 | [diff] [blame] | 2667 | |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 2668 | active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot; |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2669 | |
| 2670 | fa_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), active_slot); |
David Vincze | 505fba2 | 2020-10-22 13:53:29 +0200 | [diff] [blame] | 2671 | rc = flash_area_open(fa_id, &fap); |
| 2672 | assert(rc == 0); |
| 2673 | |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 2674 | active_swap_state = &(state->slot_usage[BOOT_CURR_IMG(state)].swap_state); |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2675 | |
| 2676 | memset(active_swap_state, 0, sizeof(struct boot_swap_state)); |
| 2677 | rc = boot_read_swap_state(fap, active_swap_state); |
David Vincze | 505fba2 | 2020-10-22 13:53:29 +0200 | [diff] [blame] | 2678 | assert(rc == 0); |
| 2679 | |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2680 | if (active_swap_state->magic != BOOT_MAGIC_GOOD || |
| 2681 | (active_swap_state->copy_done == BOOT_FLAG_SET && |
| 2682 | active_swap_state->image_ok != BOOT_FLAG_SET)) { |
David Vincze | 505fba2 | 2020-10-22 13:53:29 +0200 | [diff] [blame] | 2683 | /* |
| 2684 | * A reboot happened without the image being confirmed at |
| 2685 | * runtime or its trailer is corrupted/invalid. Erase the image |
| 2686 | * to prevent it from being selected again on the next reboot. |
| 2687 | */ |
| 2688 | BOOT_LOG_DBG("Erasing faulty image in the %s slot.", |
Carlos Falgueras García | ae13c3c | 2021-06-21 17:20:31 +0200 | [diff] [blame] | 2689 | (active_slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary"); |
Dominik Ermel | 260ae09 | 2021-04-23 05:38:45 +0000 | [diff] [blame] | 2690 | rc = flash_area_erase(fap, 0, flash_area_get_size(fap)); |
David Vincze | 505fba2 | 2020-10-22 13:53:29 +0200 | [diff] [blame] | 2691 | assert(rc == 0); |
| 2692 | |
| 2693 | flash_area_close(fap); |
| 2694 | rc = -1; |
| 2695 | } else { |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2696 | if (active_swap_state->copy_done != BOOT_FLAG_SET) { |
| 2697 | if (active_swap_state->copy_done == BOOT_FLAG_BAD) { |
David Vincze | 505fba2 | 2020-10-22 13:53:29 +0200 | [diff] [blame] | 2698 | BOOT_LOG_DBG("The copy_done flag had an unexpected value. Its " |
| 2699 | "value was neither 'set' nor 'unset', but 'bad'."); |
| 2700 | } |
| 2701 | /* |
| 2702 | * Set the copy_done flag, indicating that the image has been |
| 2703 | * selected to boot. It can be set in advance, before even |
| 2704 | * validating the image, because in case the validation fails, the |
| 2705 | * entire image slot will be erased (including the trailer). |
| 2706 | */ |
| 2707 | rc = boot_write_copy_done(fap); |
| 2708 | if (rc != 0) { |
| 2709 | BOOT_LOG_WRN("Failed to set copy_done flag of the image in " |
Carlos Falgueras García | ae13c3c | 2021-06-21 17:20:31 +0200 | [diff] [blame] | 2710 | "the %s slot.", (active_slot == BOOT_PRIMARY_SLOT) ? |
David Vincze | 505fba2 | 2020-10-22 13:53:29 +0200 | [diff] [blame] | 2711 | "primary" : "secondary"); |
| 2712 | rc = 0; |
| 2713 | } |
| 2714 | } |
| 2715 | flash_area_close(fap); |
| 2716 | } |
| 2717 | |
| 2718 | return rc; |
| 2719 | } |
David Vincze | 1c45624 | 2021-06-29 15:25:24 +0200 | [diff] [blame] | 2720 | #endif /* MCUBOOT_DIRECT_XIP && MCUBOOT_DIRECT_XIP_REVERT */ |
David Vincze | 505fba2 | 2020-10-22 13:53:29 +0200 | [diff] [blame] | 2721 | |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 2722 | #ifdef MCUBOOT_RAM_LOAD |
| 2723 | |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2724 | #ifndef MULTIPLE_EXECUTABLE_RAM_REGIONS |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 2725 | #if !defined(IMAGE_EXECUTABLE_RAM_START) || !defined(IMAGE_EXECUTABLE_RAM_SIZE) |
| 2726 | #error "Platform MUST define executable RAM bounds in case of RAM_LOAD" |
| 2727 | #endif |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2728 | #endif |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 2729 | |
| 2730 | /** |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2731 | * Verifies that the active slot of the current image can be loaded within the |
| 2732 | * predefined bounds that are allowed to be used by executable images. |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 2733 | * |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2734 | * @param state Boot loader status information. |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 2735 | * |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2736 | * @return 0 on success; nonzero on failure. |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 2737 | */ |
| 2738 | static int |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 2739 | boot_verify_ram_load_address(struct boot_loader_state *state) |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 2740 | { |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2741 | uint32_t img_dst; |
| 2742 | uint32_t img_sz; |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 2743 | uint32_t img_end_addr; |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2744 | uint32_t exec_ram_start; |
| 2745 | uint32_t exec_ram_size; |
David Brown | 695e591 | 2021-05-24 16:58:01 -0600 | [diff] [blame] | 2746 | |
| 2747 | (void)state; |
| 2748 | |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2749 | #ifdef MULTIPLE_EXECUTABLE_RAM_REGIONS |
| 2750 | int rc; |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 2751 | |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2752 | rc = boot_get_image_exec_ram_info(BOOT_CURR_IMG(state), &exec_ram_start, |
| 2753 | &exec_ram_size); |
| 2754 | if (rc != 0) { |
| 2755 | return BOOT_EBADSTATUS; |
| 2756 | } |
| 2757 | #else |
| 2758 | exec_ram_start = IMAGE_EXECUTABLE_RAM_START; |
| 2759 | exec_ram_size = IMAGE_EXECUTABLE_RAM_SIZE; |
| 2760 | #endif |
| 2761 | |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 2762 | img_dst = state->slot_usage[BOOT_CURR_IMG(state)].img_dst; |
| 2763 | img_sz = state->slot_usage[BOOT_CURR_IMG(state)].img_sz; |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2764 | |
| 2765 | if (img_dst < exec_ram_start) { |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 2766 | return BOOT_EBADIMAGE; |
| 2767 | } |
| 2768 | |
| 2769 | if (!boot_u32_safe_add(&img_end_addr, img_dst, img_sz)) { |
| 2770 | return BOOT_EBADIMAGE; |
| 2771 | } |
| 2772 | |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2773 | if (img_end_addr > (exec_ram_start + exec_ram_size)) { |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 2774 | return BOOT_EBADIMAGE; |
| 2775 | } |
| 2776 | |
| 2777 | return 0; |
| 2778 | } |
| 2779 | |
Hugo L'Hostis | db543e5 | 2021-03-09 18:00:31 +0000 | [diff] [blame] | 2780 | #ifdef MCUBOOT_ENC_IMAGES |
| 2781 | |
| 2782 | /** |
| 2783 | * Copies and decrypts an image from a slot in the flash to an SRAM address. |
| 2784 | * |
| 2785 | * @param state Boot loader status information. |
| 2786 | * @param slot The flash slot of the image to be copied to SRAM. |
| 2787 | * @param hdr The image header. |
| 2788 | * @param src_sz Size of the image. |
| 2789 | * @param img_dst Pointer to the address at which the image needs to be |
| 2790 | * copied to SRAM. |
| 2791 | * |
| 2792 | * @return 0 on success; nonzero on failure. |
| 2793 | */ |
| 2794 | static int |
| 2795 | boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state, |
| 2796 | uint32_t slot, struct image_header *hdr, |
| 2797 | uint32_t src_sz, uint32_t img_dst) |
| 2798 | { |
| 2799 | /* The flow for the decryption and copy of the image is as follows : |
| 2800 | * 1. The whole image is copied to the RAM (header + payload + TLV). |
| 2801 | * 2. The encryption key is loaded from the TLV in flash. |
| 2802 | * 3. The image is then decrypted chunk by chunk in RAM (1 chunk |
| 2803 | * is 1024 bytes). Only the payload section is decrypted. |
| 2804 | * 4. The image is authenticated in RAM. |
| 2805 | */ |
| 2806 | const struct flash_area *fap_src = NULL; |
| 2807 | struct boot_status bs; |
| 2808 | uint32_t blk_off; |
| 2809 | uint32_t tlv_off; |
| 2810 | uint32_t blk_sz; |
| 2811 | uint32_t bytes_copied = hdr->ih_hdr_size; |
| 2812 | uint32_t chunk_sz; |
| 2813 | uint32_t max_sz = 1024; |
| 2814 | uint16_t idx; |
Hugo L'Hostis | db543e5 | 2021-03-09 18:00:31 +0000 | [diff] [blame] | 2815 | uint8_t * cur_dst; |
| 2816 | int area_id; |
| 2817 | int rc; |
| 2818 | uint8_t * ram_dst = (void *)(IMAGE_RAM_BASE + img_dst); |
| 2819 | |
Hugo L'Hostis | db543e5 | 2021-03-09 18:00:31 +0000 | [diff] [blame] | 2820 | area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot); |
| 2821 | rc = flash_area_open(area_id, &fap_src); |
| 2822 | if (rc != 0){ |
| 2823 | return BOOT_EFLASH; |
| 2824 | } |
| 2825 | |
| 2826 | tlv_off = BOOT_TLV_OFF(hdr); |
| 2827 | |
| 2828 | /* Copying the whole image in RAM */ |
| 2829 | rc = flash_area_read(fap_src, 0, ram_dst, src_sz); |
| 2830 | if (rc != 0) { |
| 2831 | goto done; |
| 2832 | } |
| 2833 | |
Dominik Ermel | 7f9ac97 | 2024-07-12 19:21:40 +0000 | [diff] [blame] | 2834 | rc = boot_enc_load(BOOT_CURR_ENC(state), slot, hdr, fap_src, &bs); |
Hugo L'Hostis | db543e5 | 2021-03-09 18:00:31 +0000 | [diff] [blame] | 2835 | if (rc < 0) { |
| 2836 | goto done; |
| 2837 | } |
| 2838 | |
| 2839 | /* if rc > 0 then the key has already been loaded */ |
| 2840 | if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), slot, &bs)) { |
| 2841 | goto done; |
| 2842 | } |
| 2843 | |
| 2844 | /* Starting at the end of the header as the header section is not encrypted */ |
| 2845 | while (bytes_copied < tlv_off) { /* TLV section copied previously */ |
| 2846 | if (src_sz - bytes_copied > max_sz) { |
| 2847 | chunk_sz = max_sz; |
| 2848 | } else { |
| 2849 | chunk_sz = src_sz - bytes_copied; |
| 2850 | } |
| 2851 | |
| 2852 | cur_dst = ram_dst + bytes_copied; |
| 2853 | blk_sz = chunk_sz; |
| 2854 | idx = 0; |
Dominik Ermel | d09112a | 2024-07-18 16:43:57 +0000 | [diff] [blame] | 2855 | blk_off = ((bytes_copied) - hdr->ih_hdr_size) & 0xf; |
Hugo L'Hostis | db543e5 | 2021-03-09 18:00:31 +0000 | [diff] [blame] | 2856 | if (bytes_copied + chunk_sz > tlv_off) { |
| 2857 | /* Going over TLV section |
| 2858 | * Part of the chunk is encrypted payload */ |
Hugo L'Hostis | db543e5 | 2021-03-09 18:00:31 +0000 | [diff] [blame] | 2859 | blk_sz = tlv_off - (bytes_copied); |
Hugo L'Hostis | db543e5 | 2021-03-09 18:00:31 +0000 | [diff] [blame] | 2860 | } |
Dominik Ermel | 3f11286 | 2024-07-17 14:43:05 +0000 | [diff] [blame] | 2861 | boot_encrypt(BOOT_CURR_ENC(state), slot, |
Dominik Ermel | d09112a | 2024-07-18 16:43:57 +0000 | [diff] [blame] | 2862 | (bytes_copied + idx) - hdr->ih_hdr_size, blk_sz, |
| 2863 | blk_off, cur_dst); |
Hugo L'Hostis | db543e5 | 2021-03-09 18:00:31 +0000 | [diff] [blame] | 2864 | |
| 2865 | bytes_copied += chunk_sz; |
| 2866 | } |
| 2867 | rc = 0; |
| 2868 | |
| 2869 | done: |
| 2870 | flash_area_close(fap_src); |
| 2871 | |
| 2872 | return rc; |
| 2873 | } |
| 2874 | |
| 2875 | #endif /* MCUBOOT_ENC_IMAGES */ |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 2876 | /** |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2877 | * Copies a slot of the current image into SRAM. |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 2878 | * |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2879 | * @param state Boot loader status information. |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 2880 | * @param slot The flash slot of the image to be copied to SRAM. |
| 2881 | * @param img_dst The address at which the image needs to be copied to |
| 2882 | * SRAM. |
| 2883 | * @param img_sz The size of the image that needs to be copied to SRAM. |
| 2884 | * |
| 2885 | * @return 0 on success; nonzero on failure. |
| 2886 | */ |
| 2887 | static int |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2888 | boot_copy_image_to_sram(struct boot_loader_state *state, int slot, |
| 2889 | uint32_t img_dst, uint32_t img_sz) |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 2890 | { |
| 2891 | int rc; |
| 2892 | const struct flash_area *fap_src = NULL; |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2893 | int area_id; |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 2894 | |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2895 | #if (BOOT_IMAGE_NUMBER == 1) |
| 2896 | (void)state; |
| 2897 | #endif |
| 2898 | |
| 2899 | area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot); |
| 2900 | |
| 2901 | rc = flash_area_open(area_id, &fap_src); |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 2902 | if (rc != 0) { |
| 2903 | return BOOT_EFLASH; |
| 2904 | } |
| 2905 | |
| 2906 | /* Direct copy from flash to its new location in SRAM. */ |
David Brown | 9bd7f90 | 2021-05-26 16:31:14 -0600 | [diff] [blame] | 2907 | rc = flash_area_read(fap_src, 0, (void *)(IMAGE_RAM_BASE + img_dst), img_sz); |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 2908 | if (rc != 0) { |
Antonio de Angelis | 4854700 | 2023-04-14 09:47:09 +0100 | [diff] [blame] | 2909 | BOOT_LOG_INF("Error whilst copying image %d from Flash to SRAM: %d", |
| 2910 | BOOT_CURR_IMG(state), rc); |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 2911 | } |
| 2912 | |
| 2913 | flash_area_close(fap_src); |
| 2914 | |
| 2915 | return rc; |
| 2916 | } |
| 2917 | |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2918 | #if (BOOT_IMAGE_NUMBER > 1) |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 2919 | /** |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2920 | * Checks if two memory regions (A and B) are overlap or not. |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 2921 | * |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2922 | * @param start_a Start of the A region. |
| 2923 | * @param end_a End of the A region. |
| 2924 | * @param start_b Start of the B region. |
| 2925 | * @param end_b End of the B region. |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 2926 | * |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2927 | * @return true if there is overlap; false otherwise. |
| 2928 | */ |
| 2929 | static bool |
| 2930 | do_regions_overlap(uint32_t start_a, uint32_t end_a, |
| 2931 | uint32_t start_b, uint32_t end_b) |
| 2932 | { |
| 2933 | if (start_b > end_a) { |
| 2934 | return false; |
| 2935 | } else if (start_b >= start_a) { |
| 2936 | return true; |
| 2937 | } else if (end_b > start_a) { |
| 2938 | return true; |
| 2939 | } |
| 2940 | |
| 2941 | return false; |
| 2942 | } |
| 2943 | |
| 2944 | /** |
| 2945 | * Checks if the image we want to load to memory overlap with an already |
| 2946 | * ramloaded image. |
| 2947 | * |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 2948 | * @param state Boot loader status information. |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2949 | * |
| 2950 | * @return 0 if there is no overlap; nonzero otherwise. |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 2951 | */ |
| 2952 | static int |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 2953 | boot_check_ram_load_overlapping(struct boot_loader_state *state) |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 2954 | { |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2955 | uint32_t i; |
| 2956 | |
| 2957 | uint32_t start_a; |
| 2958 | uint32_t end_a; |
| 2959 | uint32_t start_b; |
| 2960 | uint32_t end_b; |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 2961 | uint32_t image_id_to_check = BOOT_CURR_IMG(state); |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2962 | |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 2963 | start_a = state->slot_usage[image_id_to_check].img_dst; |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2964 | /* Safe to add here, values are already verified in |
| 2965 | * boot_verify_ram_load_address() */ |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 2966 | end_a = start_a + state->slot_usage[image_id_to_check].img_sz; |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2967 | |
| 2968 | for (i = 0; i < BOOT_IMAGE_NUMBER; i++) { |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 2969 | if (state->slot_usage[i].active_slot == NO_ACTIVE_SLOT |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2970 | || i == image_id_to_check) { |
| 2971 | continue; |
| 2972 | } |
| 2973 | |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 2974 | start_b = state->slot_usage[i].img_dst; |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2975 | /* Safe to add here, values are already verified in |
| 2976 | * boot_verify_ram_load_address() */ |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 2977 | end_b = start_b + state->slot_usage[i].img_sz; |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2978 | |
| 2979 | if (do_regions_overlap(start_a, end_a, start_b, end_b)) { |
| 2980 | return -1; |
| 2981 | } |
| 2982 | } |
| 2983 | |
| 2984 | return 0; |
| 2985 | } |
| 2986 | #endif |
| 2987 | |
| 2988 | /** |
| 2989 | * Loads the active slot of the current image into SRAM. The load address and |
| 2990 | * image size is extracted from the image header. |
| 2991 | * |
| 2992 | * @param state Boot loader status information. |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2993 | * |
| 2994 | * @return 0 on success; nonzero on failure. |
| 2995 | */ |
| 2996 | static int |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 2997 | boot_load_image_to_sram(struct boot_loader_state *state) |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 2998 | { |
| 2999 | uint32_t active_slot; |
| 3000 | struct image_header *hdr = NULL; |
| 3001 | uint32_t img_dst; |
| 3002 | uint32_t img_sz; |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 3003 | int rc; |
| 3004 | |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 3005 | active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot; |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3006 | hdr = boot_img_hdr(state, active_slot); |
| 3007 | |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 3008 | if (hdr->ih_flags & IMAGE_F_RAM_LOAD) { |
| 3009 | |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3010 | img_dst = hdr->ih_load_addr; |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 3011 | |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3012 | rc = boot_read_image_size(state, active_slot, &img_sz); |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 3013 | if (rc != 0) { |
| 3014 | return rc; |
| 3015 | } |
| 3016 | |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 3017 | state->slot_usage[BOOT_CURR_IMG(state)].img_dst = img_dst; |
| 3018 | state->slot_usage[BOOT_CURR_IMG(state)].img_sz = img_sz; |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3019 | |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 3020 | rc = boot_verify_ram_load_address(state); |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 3021 | if (rc != 0) { |
Antonio de Angelis | ba5fb1c | 2022-10-11 10:10:37 +0100 | [diff] [blame] | 3022 | BOOT_LOG_INF("Image %d RAM load address 0x%x is invalid.", BOOT_CURR_IMG(state), img_dst); |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 3023 | return rc; |
| 3024 | } |
| 3025 | |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3026 | #if (BOOT_IMAGE_NUMBER > 1) |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 3027 | rc = boot_check_ram_load_overlapping(state); |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3028 | if (rc != 0) { |
Antonio de Angelis | ba5fb1c | 2022-10-11 10:10:37 +0100 | [diff] [blame] | 3029 | BOOT_LOG_INF("Image %d RAM loading to address 0x%x would overlap with\ |
| 3030 | another image.", BOOT_CURR_IMG(state), img_dst); |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3031 | return rc; |
| 3032 | } |
| 3033 | #endif |
Hugo L'Hostis | db543e5 | 2021-03-09 18:00:31 +0000 | [diff] [blame] | 3034 | #ifdef MCUBOOT_ENC_IMAGES |
| 3035 | /* decrypt image if encrypted and copy it to RAM */ |
| 3036 | if (IS_ENCRYPTED(hdr)) { |
| 3037 | rc = boot_decrypt_and_copy_image_to_sram(state, active_slot, hdr, img_sz, img_dst); |
| 3038 | } else { |
| 3039 | rc = boot_copy_image_to_sram(state, active_slot, img_dst, img_sz); |
| 3040 | } |
| 3041 | #else |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 3042 | /* Copy image to the load address from where it currently resides in |
| 3043 | * flash. |
| 3044 | */ |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3045 | rc = boot_copy_image_to_sram(state, active_slot, img_dst, img_sz); |
Hugo L'Hostis | db543e5 | 2021-03-09 18:00:31 +0000 | [diff] [blame] | 3046 | #endif |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 3047 | if (rc != 0) { |
Antonio de Angelis | ba5fb1c | 2022-10-11 10:10:37 +0100 | [diff] [blame] | 3048 | BOOT_LOG_INF("Image %d RAM loading to 0x%x is failed.", BOOT_CURR_IMG(state), img_dst); |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 3049 | } else { |
Antonio de Angelis | ba5fb1c | 2022-10-11 10:10:37 +0100 | [diff] [blame] | 3050 | BOOT_LOG_INF("Image %d RAM loading to 0x%x is succeeded.", BOOT_CURR_IMG(state), img_dst); |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 3051 | } |
| 3052 | } else { |
| 3053 | /* Only images that support IMAGE_F_RAM_LOAD are allowed if |
| 3054 | * MCUBOOT_RAM_LOAD is set. |
| 3055 | */ |
| 3056 | rc = BOOT_EBADIMAGE; |
| 3057 | } |
| 3058 | |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3059 | if (rc != 0) { |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 3060 | state->slot_usage[BOOT_CURR_IMG(state)].img_dst = 0; |
| 3061 | state->slot_usage[BOOT_CURR_IMG(state)].img_sz = 0; |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3062 | } |
| 3063 | |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 3064 | return rc; |
| 3065 | } |
| 3066 | |
| 3067 | /** |
| 3068 | * Removes an image from SRAM, by overwriting it with zeros. |
| 3069 | * |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3070 | * @param state Boot loader status information. |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3071 | * |
| 3072 | * @return 0 on success; nonzero on failure. |
| 3073 | */ |
| 3074 | static inline int |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 3075 | boot_remove_image_from_sram(struct boot_loader_state *state) |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3076 | { |
David Brown | 695e591 | 2021-05-24 16:58:01 -0600 | [diff] [blame] | 3077 | (void)state; |
| 3078 | |
Antonio de Angelis | ba5fb1c | 2022-10-11 10:10:37 +0100 | [diff] [blame] | 3079 | BOOT_LOG_INF("Removing image %d from SRAM at address 0x%x", |
| 3080 | BOOT_CURR_IMG(state), |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 3081 | state->slot_usage[BOOT_CURR_IMG(state)].img_dst); |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3082 | |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 3083 | memset((void*)(IMAGE_RAM_BASE + state->slot_usage[BOOT_CURR_IMG(state)].img_dst), |
| 3084 | 0, state->slot_usage[BOOT_CURR_IMG(state)].img_sz); |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3085 | |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 3086 | state->slot_usage[BOOT_CURR_IMG(state)].img_dst = 0; |
| 3087 | state->slot_usage[BOOT_CURR_IMG(state)].img_sz = 0; |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3088 | |
| 3089 | return 0; |
| 3090 | } |
| 3091 | |
| 3092 | /** |
| 3093 | * Removes an image from flash by erasing the corresponding flash area |
| 3094 | * |
| 3095 | * @param state Boot loader status information. |
| 3096 | * @param slot The flash slot of the image to be erased. |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 3097 | * |
| 3098 | * @return 0 on success; nonzero on failure. |
| 3099 | */ |
| 3100 | static inline int |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3101 | boot_remove_image_from_flash(struct boot_loader_state *state, uint32_t slot) |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 3102 | { |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3103 | int area_id; |
| 3104 | int rc; |
| 3105 | const struct flash_area *fap; |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 3106 | |
David Brown | 695e591 | 2021-05-24 16:58:01 -0600 | [diff] [blame] | 3107 | (void)state; |
| 3108 | |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3109 | BOOT_LOG_INF("Removing image %d slot %d from flash", BOOT_CURR_IMG(state), |
| 3110 | slot); |
| 3111 | area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot); |
| 3112 | rc = flash_area_open(area_id, &fap); |
| 3113 | if (rc == 0) { |
Dominik Ermel | 260ae09 | 2021-04-23 05:38:45 +0000 | [diff] [blame] | 3114 | flash_area_erase(fap, 0, flash_area_get_size(fap)); |
Dominik Ermel | 245de81 | 2022-09-02 13:39:23 +0000 | [diff] [blame] | 3115 | flash_area_close(fap); |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3116 | } |
| 3117 | |
| 3118 | return rc; |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 3119 | } |
| 3120 | #endif /* MCUBOOT_RAM_LOAD */ |
| 3121 | |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3122 | |
| 3123 | /** |
| 3124 | * Tries to load a slot for all the images with validation. |
| 3125 | * |
| 3126 | * @param state Boot loader status information. |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3127 | * |
| 3128 | * @return 0 on success; nonzero on failure. |
| 3129 | */ |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 3130 | fih_ret |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 3131 | boot_load_and_validate_images(struct boot_loader_state *state) |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3132 | { |
| 3133 | uint32_t active_slot; |
| 3134 | int rc; |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 3135 | fih_ret fih_rc; |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3136 | |
| 3137 | /* Go over all the images and try to load one */ |
| 3138 | IMAGES_ITER(BOOT_CURR_IMG(state)) { |
| 3139 | /* All slots tried until a valid image found. Breaking from this loop |
| 3140 | * means that a valid image found or already loaded. If no slot is |
| 3141 | * found the function returns with error code. */ |
| 3142 | while (true) { |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3143 | /* Go over all the slots and try to load one */ |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 3144 | active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot; |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3145 | if (active_slot != NO_ACTIVE_SLOT){ |
| 3146 | /* A slot is already active, go to next image. */ |
| 3147 | break; |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 3148 | } |
David Vincze | 505fba2 | 2020-10-22 13:53:29 +0200 | [diff] [blame] | 3149 | |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 3150 | active_slot = find_slot_with_highest_version(state); |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3151 | if (active_slot == NO_ACTIVE_SLOT) { |
| 3152 | BOOT_LOG_INF("No slot to load for image %d", |
| 3153 | BOOT_CURR_IMG(state)); |
| 3154 | FIH_RET(FIH_FAILURE); |
| 3155 | } |
| 3156 | |
| 3157 | /* Save the number of the active slot. */ |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 3158 | state->slot_usage[BOOT_CURR_IMG(state)].active_slot = active_slot; |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3159 | |
Raef Coles | f11de64 | 2021-10-15 11:11:56 +0100 | [diff] [blame] | 3160 | #if BOOT_IMAGE_NUMBER > 1 |
| 3161 | if (state->img_mask[BOOT_CURR_IMG(state)]) { |
| 3162 | continue; |
| 3163 | } |
| 3164 | #endif |
| 3165 | |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3166 | #ifdef MCUBOOT_DIRECT_XIP |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 3167 | rc = boot_rom_address_check(state); |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3168 | if (rc != 0) { |
| 3169 | /* The image is placed in an unsuitable slot. */ |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 3170 | state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false; |
| 3171 | state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT; |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3172 | continue; |
| 3173 | } |
George Beckstein | d4d90f8 | 2021-05-11 02:00:00 -0400 | [diff] [blame] | 3174 | |
David Vincze | 505fba2 | 2020-10-22 13:53:29 +0200 | [diff] [blame] | 3175 | #ifdef MCUBOOT_DIRECT_XIP_REVERT |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 3176 | rc = boot_select_or_erase(state); |
David Vincze | 505fba2 | 2020-10-22 13:53:29 +0200 | [diff] [blame] | 3177 | if (rc != 0) { |
| 3178 | /* The selected image slot has been erased. */ |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 3179 | state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false; |
| 3180 | state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT; |
David Vincze | 505fba2 | 2020-10-22 13:53:29 +0200 | [diff] [blame] | 3181 | continue; |
| 3182 | } |
| 3183 | #endif /* MCUBOOT_DIRECT_XIP_REVERT */ |
David Vincze | 1c45624 | 2021-06-29 15:25:24 +0200 | [diff] [blame] | 3184 | #endif /* MCUBOOT_DIRECT_XIP */ |
David Vincze | 505fba2 | 2020-10-22 13:53:29 +0200 | [diff] [blame] | 3185 | |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 3186 | #ifdef MCUBOOT_RAM_LOAD |
| 3187 | /* Image is first loaded to RAM and authenticated there in order to |
| 3188 | * prevent TOCTOU attack during image copy. This could be applied |
| 3189 | * when loading images from external (untrusted) flash to internal |
| 3190 | * (trusted) RAM and image is authenticated before copying. |
| 3191 | */ |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 3192 | rc = boot_load_image_to_sram(state); |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 3193 | if (rc != 0 ) { |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3194 | /* Image cannot be ramloaded. */ |
| 3195 | boot_remove_image_from_flash(state, active_slot); |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 3196 | state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false; |
| 3197 | state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT; |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 3198 | continue; |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 3199 | } |
| 3200 | #endif /* MCUBOOT_RAM_LOAD */ |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3201 | |
| 3202 | FIH_CALL(boot_validate_slot, fih_rc, state, active_slot, NULL); |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 3203 | if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) { |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3204 | /* Image is invalid. */ |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 3205 | #ifdef MCUBOOT_RAM_LOAD |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 3206 | boot_remove_image_from_sram(state); |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 3207 | #endif /* MCUBOOT_RAM_LOAD */ |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 3208 | state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false; |
| 3209 | state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT; |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3210 | continue; |
David Vincze | 505fba2 | 2020-10-22 13:53:29 +0200 | [diff] [blame] | 3211 | } |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3212 | |
| 3213 | /* Valid image loaded from a slot, go to next image. */ |
| 3214 | break; |
| 3215 | } |
| 3216 | } |
| 3217 | |
| 3218 | FIH_RET(FIH_SUCCESS); |
| 3219 | } |
| 3220 | |
| 3221 | /** |
| 3222 | * Updates the security counter for the current image. |
| 3223 | * |
| 3224 | * @param state Boot loader status information. |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3225 | * |
| 3226 | * @return 0 on success; nonzero on failure. |
| 3227 | */ |
| 3228 | static int |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 3229 | boot_update_hw_rollback_protection(struct boot_loader_state *state) |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3230 | { |
| 3231 | #ifdef MCUBOOT_HW_ROLLBACK_PROT |
| 3232 | int rc; |
| 3233 | |
| 3234 | /* Update the stored security counter with the newer (active) image's |
| 3235 | * security counter value. |
| 3236 | */ |
David Vincze | 1c45624 | 2021-06-29 15:25:24 +0200 | [diff] [blame] | 3237 | #if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT) |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3238 | /* When the 'revert' mechanism is enabled in direct-xip mode, the |
| 3239 | * security counter can be increased only after reboot, if the image |
| 3240 | * has been confirmed at runtime (the image_ok flag has been set). |
| 3241 | * This way a 'revert' can be performed when it's necessary. |
| 3242 | */ |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 3243 | if (state->slot_usage[BOOT_CURR_IMG(state)].swap_state.image_ok == BOOT_FLAG_SET) { |
David Vincze | 505fba2 | 2020-10-22 13:53:29 +0200 | [diff] [blame] | 3244 | #endif |
Sherry Zhang | 50b06ae | 2021-07-09 15:22:51 +0800 | [diff] [blame] | 3245 | rc = boot_update_security_counter(BOOT_CURR_IMG(state), |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 3246 | state->slot_usage[BOOT_CURR_IMG(state)].active_slot, |
| 3247 | boot_img_hdr(state, state->slot_usage[BOOT_CURR_IMG(state)].active_slot)); |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 3248 | if (rc != 0) { |
Antonio de Angelis | ba5fb1c | 2022-10-11 10:10:37 +0100 | [diff] [blame] | 3249 | BOOT_LOG_ERR("Security counter update failed after image %d validation.", BOOT_CURR_IMG(state)); |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3250 | return rc; |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 3251 | } |
David Vincze | 1c45624 | 2021-06-29 15:25:24 +0200 | [diff] [blame] | 3252 | #if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT) |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3253 | } |
| 3254 | #endif |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 3255 | |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3256 | return 0; |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 3257 | |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3258 | #else /* MCUBOOT_HW_ROLLBACK_PROT */ |
| 3259 | (void) (state); |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3260 | return 0; |
| 3261 | #endif |
| 3262 | } |
| 3263 | |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 3264 | fih_ret |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3265 | context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp) |
| 3266 | { |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3267 | int rc; |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 3268 | FIH_DECLARE(fih_rc, FIH_FAILURE); |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3269 | |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 3270 | rc = boot_get_slot_usage(state); |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3271 | if (rc != 0) { |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 3272 | goto out; |
| 3273 | } |
| 3274 | |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3275 | #if (BOOT_IMAGE_NUMBER > 1) |
| 3276 | while (true) { |
| 3277 | #endif |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 3278 | FIH_CALL(boot_load_and_validate_images, fih_rc, state); |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 3279 | if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) { |
| 3280 | FIH_SET(fih_rc, FIH_FAILURE); |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3281 | goto out; |
| 3282 | } |
| 3283 | |
| 3284 | #if (BOOT_IMAGE_NUMBER > 1) |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 3285 | rc = boot_verify_dependencies(state); |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3286 | if (rc != 0) { |
| 3287 | /* Dependency check failed for an image, it has been removed from |
| 3288 | * SRAM in case of MCUBOOT_RAM_LOAD strategy, and set to |
| 3289 | * unavailable. Try to load an image from another slot. |
| 3290 | */ |
| 3291 | continue; |
| 3292 | } |
| 3293 | /* Dependency check was successful. */ |
| 3294 | break; |
| 3295 | } |
| 3296 | #endif |
| 3297 | |
| 3298 | IMAGES_ITER(BOOT_CURR_IMG(state)) { |
Raef Coles | f11de64 | 2021-10-15 11:11:56 +0100 | [diff] [blame] | 3299 | #if BOOT_IMAGE_NUMBER > 1 |
| 3300 | if (state->img_mask[BOOT_CURR_IMG(state)]) { |
| 3301 | continue; |
| 3302 | } |
| 3303 | #endif |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 3304 | rc = boot_update_hw_rollback_protection(state); |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3305 | if (rc != 0) { |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 3306 | FIH_SET(fih_rc, FIH_FAILURE); |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3307 | goto out; |
| 3308 | } |
| 3309 | |
Jamie McCrae | 3016d00 | 2023-03-14 12:35:51 +0000 | [diff] [blame] | 3310 | rc = boot_add_shared_data(state, (uint8_t)state->slot_usage[BOOT_CURR_IMG(state)].active_slot); |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3311 | if (rc != 0) { |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 3312 | FIH_SET(fih_rc, FIH_FAILURE); |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3313 | goto out; |
| 3314 | } |
| 3315 | } |
| 3316 | |
| 3317 | /* All image loaded successfully. */ |
| 3318 | #ifdef MCUBOOT_HAVE_LOGGING |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 3319 | print_loaded_images(state); |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3320 | #endif |
| 3321 | |
Raef Coles | fe57e7d | 2021-10-15 11:07:09 +0100 | [diff] [blame] | 3322 | fill_rsp(state, rsp); |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3323 | |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 3324 | out: |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3325 | close_all_flash_areas(state); |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 3326 | |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 3327 | if (rc != 0) { |
| 3328 | FIH_SET(fih_rc, FIH_FAILURE); |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3329 | } |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 3330 | |
Mark Horvath | ccaf7f8 | 2021-01-04 18:16:42 +0100 | [diff] [blame] | 3331 | FIH_RET(fih_rc); |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 3332 | } |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 3333 | #endif /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */ |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 3334 | |
| 3335 | /** |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 3336 | * Prepares the booting process. This function moves images around in flash as |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 3337 | * appropriate, and tells you what address to boot from. |
| 3338 | * |
| 3339 | * @param rsp On success, indicates how booting should occur. |
| 3340 | * |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 3341 | * @return FIH_SUCCESS on success; nonzero on failure. |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 3342 | */ |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 3343 | fih_ret |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 3344 | boot_go(struct boot_rsp *rsp) |
| 3345 | { |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 3346 | FIH_DECLARE(fih_rc, FIH_FAILURE); |
Raef Coles | f11de64 | 2021-10-15 11:11:56 +0100 | [diff] [blame] | 3347 | |
| 3348 | boot_state_clear(NULL); |
| 3349 | |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 3350 | FIH_CALL(context_boot_go, fih_rc, &boot_data, rsp); |
| 3351 | FIH_RET(fih_rc); |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 3352 | } |
Raef Coles | f11de64 | 2021-10-15 11:11:56 +0100 | [diff] [blame] | 3353 | |
| 3354 | /** |
| 3355 | * Prepares the booting process, considering only a single image. This function |
| 3356 | * moves images around in flash as appropriate, and tells you what address to |
| 3357 | * boot from. |
| 3358 | * |
| 3359 | * @param rsp On success, indicates how booting should occur. |
| 3360 | * |
| 3361 | * @param image_id The image ID to prepare the boot process for. |
| 3362 | * |
| 3363 | * @return FIH_SUCCESS on success; nonzero on failure. |
| 3364 | */ |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 3365 | fih_ret |
Raef Coles | f11de64 | 2021-10-15 11:11:56 +0100 | [diff] [blame] | 3366 | boot_go_for_image_id(struct boot_rsp *rsp, uint32_t image_id) |
| 3367 | { |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 3368 | FIH_DECLARE(fih_rc, FIH_FAILURE); |
Raef Coles | f11de64 | 2021-10-15 11:11:56 +0100 | [diff] [blame] | 3369 | |
| 3370 | if (image_id >= BOOT_IMAGE_NUMBER) { |
| 3371 | FIH_RET(FIH_FAILURE); |
| 3372 | } |
| 3373 | |
| 3374 | #if BOOT_IMAGE_NUMBER > 1 |
| 3375 | memset(&boot_data.img_mask, 1, BOOT_IMAGE_NUMBER); |
| 3376 | boot_data.img_mask[image_id] = 0; |
| 3377 | #endif |
| 3378 | |
| 3379 | FIH_CALL(context_boot_go, fih_rc, &boot_data, rsp); |
| 3380 | FIH_RET(fih_rc); |
| 3381 | } |
| 3382 | |
| 3383 | /** |
| 3384 | * Clears the boot state, so that previous operations have no effect on new |
| 3385 | * ones. |
| 3386 | * |
| 3387 | * @param state The state that should be cleared. If the value |
| 3388 | * is NULL, the default bootloader state will be |
| 3389 | * cleared. |
| 3390 | */ |
| 3391 | void boot_state_clear(struct boot_loader_state *state) |
| 3392 | { |
| 3393 | if (state != NULL) { |
| 3394 | memset(state, 0, sizeof(struct boot_loader_state)); |
| 3395 | } else { |
| 3396 | memset(&boot_data, 0, sizeof(struct boot_loader_state)); |
| 3397 | } |
| 3398 | } |
Jamie McCrae | 4f1ab9e | 2024-07-26 12:29:24 +0100 | [diff] [blame] | 3399 | |
Jamie McCrae | e261b28 | 2024-07-26 11:48:19 +0100 | [diff] [blame] | 3400 | #if defined(MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO) |
Jamie McCrae | 4f1ab9e | 2024-07-26 12:29:24 +0100 | [diff] [blame] | 3401 | /** |
Jamie McCrae | e261b28 | 2024-07-26 11:48:19 +0100 | [diff] [blame] | 3402 | * Reads image data to find out the maximum application sizes. Only needs to |
| 3403 | * be called in serial recovery mode, as the state informatio is unpopulated |
| 3404 | * at that time |
| 3405 | */ |
| 3406 | static void boot_fetch_slot_state_sizes(void) |
| 3407 | { |
| 3408 | struct sector_buffer_t sector_buffers; |
| 3409 | size_t slot; |
| 3410 | int rc = -1; |
| 3411 | int fa_id; |
| 3412 | int image_index; |
| 3413 | |
| 3414 | boot_get_sector_buffers(§or_buffers); |
| 3415 | |
| 3416 | IMAGES_ITER(BOOT_CURR_IMG(&boot_data)) { |
| 3417 | int max_size = 0; |
| 3418 | |
| 3419 | image_index = BOOT_CURR_IMG(&boot_data); |
| 3420 | |
| 3421 | BOOT_IMG(&boot_data, BOOT_PRIMARY_SLOT).sectors = |
| 3422 | §or_buffers.primary[image_index]; |
| 3423 | BOOT_IMG(&boot_data, BOOT_SECONDARY_SLOT).sectors = |
| 3424 | §or_buffers.secondary[image_index]; |
| 3425 | #if MCUBOOT_SWAP_USING_SCRATCH |
| 3426 | boot_data.scratch.sectors = sector_buffers.scratch;; |
| 3427 | #endif |
| 3428 | |
| 3429 | /* Open primary and secondary image areas for the duration |
| 3430 | * of this call. |
| 3431 | */ |
| 3432 | for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) { |
| 3433 | fa_id = flash_area_id_from_multi_image_slot(image_index, slot); |
| 3434 | rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot)); |
| 3435 | assert(rc == 0); |
| 3436 | |
| 3437 | if (rc != 0) { |
| 3438 | goto finish; |
| 3439 | } |
| 3440 | } |
| 3441 | |
| 3442 | #if MCUBOOT_SWAP_USING_SCRATCH |
| 3443 | rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, |
| 3444 | &BOOT_SCRATCH_AREA(&boot_data)); |
| 3445 | assert(rc == 0); |
| 3446 | |
| 3447 | if (rc != 0) { |
| 3448 | goto finish; |
| 3449 | } |
| 3450 | #endif |
| 3451 | |
| 3452 | /* Determine the sector layout of the image slots and scratch area. */ |
| 3453 | rc = boot_read_sectors(&boot_data); |
| 3454 | |
| 3455 | if (rc == 0) { |
| 3456 | max_size = app_max_size(&boot_data); |
| 3457 | |
| 3458 | if (max_size > 0) { |
| 3459 | image_max_sizes[image_index].calculated = true; |
| 3460 | image_max_sizes[image_index].max_size = max_size; |
| 3461 | } |
| 3462 | } |
| 3463 | } |
| 3464 | |
| 3465 | finish: |
| 3466 | close_all_flash_areas(&boot_data); |
| 3467 | memset(&boot_data, 0x00, sizeof(boot_data)); |
| 3468 | } |
| 3469 | #endif |
| 3470 | |
| 3471 | #if defined(MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO) || defined(MCUBOOT_DATA_SHARING) |
| 3472 | /** |
| 3473 | * Fetches the maximum allowed size of the image |
| 3474 | */ |
Jamie McCrae | 4f1ab9e | 2024-07-26 12:29:24 +0100 | [diff] [blame] | 3475 | const struct image_max_size *boot_get_max_app_size(void) |
| 3476 | { |
Jamie McCrae | e261b28 | 2024-07-26 11:48:19 +0100 | [diff] [blame] | 3477 | #if defined(MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO) |
| 3478 | uint8_t i = 0; |
| 3479 | |
| 3480 | while (i < BOOT_IMAGE_NUMBER) { |
| 3481 | if (image_max_sizes[i].calculated == true) { |
| 3482 | break; |
| 3483 | } |
| 3484 | |
| 3485 | ++i; |
| 3486 | } |
| 3487 | |
| 3488 | if (i == BOOT_IMAGE_NUMBER) { |
| 3489 | /* Information not available, need to fetch it */ |
| 3490 | boot_fetch_slot_state_sizes(); |
| 3491 | } |
| 3492 | #endif |
| 3493 | |
Jamie McCrae | 4f1ab9e | 2024-07-26 12:29:24 +0100 | [diff] [blame] | 3494 | return image_max_sizes; |
| 3495 | } |
| 3496 | #endif |