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