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