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