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