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