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