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