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