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