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