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