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