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