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