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