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) 2017-2019 Linaro LTD |
| 5 | * Copyright (c) 2016-2019 JUUL Labs |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 6 | * Copyright (c) 2019-2020 Arm Limited |
David Brown | aac7111 | 2020-02-03 16:13:42 -0700 | [diff] [blame] | 7 | * |
| 8 | * Original license: |
| 9 | * |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 10 | * Licensed to the Apache Software Foundation (ASF) under one |
| 11 | * or more contributor license agreements. See the NOTICE file |
| 12 | * distributed with this work for additional information |
| 13 | * regarding copyright ownership. The ASF licenses this file |
| 14 | * to you under the Apache License, Version 2.0 (the |
| 15 | * "License"); you may not use this file except in compliance |
| 16 | * with the License. You may obtain a copy of the License at |
| 17 | * |
| 18 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 19 | * |
| 20 | * Unless required by applicable law or agreed to in writing, |
| 21 | * software distributed under the License is distributed on an |
| 22 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 23 | * KIND, either express or implied. See the License for the |
| 24 | * specific language governing permissions and limitations |
| 25 | * under the License. |
| 26 | */ |
| 27 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 28 | #include <string.h> |
| 29 | #include <inttypes.h> |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 30 | #include <stddef.h> |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 31 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 32 | #include "sysflash/sysflash.h" |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 33 | #include "flash_map_backend/flash_map_backend.h" |
| 34 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 35 | #include "bootutil/image.h" |
| 36 | #include "bootutil/bootutil.h" |
| 37 | #include "bootutil_priv.h" |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 38 | #include "bootutil/bootutil_log.h" |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 39 | #include "bootutil/fault_injection_hardening.h" |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 40 | #ifdef MCUBOOT_ENC_IMAGES |
| 41 | #include "bootutil/enc_key.h" |
| 42 | #endif |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 43 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 44 | #ifdef MCUBOOT_SWAP_USING_STATUS |
| 45 | #include "swap_status.h" |
| 46 | #endif |
| 47 | |
| 48 | #include "mcuboot_config/mcuboot_config.h" |
| 49 | |
Emanuele Di Santo | 9f1933d | 2018-11-20 10:59:59 +0100 | [diff] [blame] | 50 | MCUBOOT_LOG_MODULE_DECLARE(mcuboot); |
| 51 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 52 | /* Currently only used by imgmgr */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 53 | int boot_current_slot; |
| 54 | |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 55 | /** |
| 56 | * @brief Determine if the data at two memory addresses is equal |
| 57 | * |
| 58 | * @param s1 The first memory region to compare. |
| 59 | * @param s2 The second memory region to compare. |
| 60 | * @param n The amount of bytes to compare. |
| 61 | * |
| 62 | * @note This function does not comply with the specification of memcmp, |
| 63 | * so should not be considered a drop-in replacement. It has no |
| 64 | * constant time execution. The point is to make sure that all the |
| 65 | * bytes are compared and detect if loop was abused and some cycles |
| 66 | * was skipped due to fault injection. |
| 67 | * |
| 68 | * @return FIH_SUCCESS if memory regions are equal, otherwise FIH_FAILURE |
| 69 | */ |
| 70 | #ifdef MCUBOOT_FIH_PROFILE_OFF |
| 71 | inline |
| 72 | fih_int boot_fih_memequal(const void *s1, const void *s2, size_t n) |
| 73 | { |
| 74 | return memcmp(s1, s2, n); |
| 75 | } |
| 76 | #else |
| 77 | fih_int boot_fih_memequal(const void *s1, const void *s2, size_t n) |
| 78 | { |
| 79 | size_t i; |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 80 | const volatile uint8_t *s1_p = (const uint8_t*) s1; |
| 81 | const volatile uint8_t *s2_p = (const uint8_t*) s2; |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 82 | fih_int ret = FIH_FAILURE; |
| 83 | |
| 84 | for (i = 0; i < n; i++) { |
| 85 | if (s1_p[i] != s2_p[i]) { |
| 86 | goto out; |
| 87 | } |
| 88 | } |
| 89 | if (i == n) { |
| 90 | ret = FIH_SUCCESS; |
| 91 | } |
| 92 | |
| 93 | out: |
| 94 | FIH_RET(ret); |
| 95 | } |
| 96 | #endif |
| 97 | |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 98 | /* |
| 99 | * Amount of space used to save information required when doing a swap, |
| 100 | * or while a swap is under progress, but not the status of sector swap |
| 101 | * progress itself. |
| 102 | */ |
| 103 | static inline uint32_t |
| 104 | boot_trailer_info_sz(void) |
| 105 | { |
| 106 | return ( |
| 107 | #ifdef MCUBOOT_ENC_IMAGES |
| 108 | /* encryption keys */ |
| 109 | #ifdef MCUBOOT_SWAP_SAVE_ENCTLV |
| 110 | BOOT_ENC_TLV_ALIGN_SIZE * 2 + |
| 111 | # else |
| 112 | BOOT_ENC_KEY_ALIGN_SIZE * 2 + |
| 113 | # endif |
| 114 | #endif |
| 115 | /* swap_type + copy_done + image_ok + swap_size */ |
| 116 | BOOT_MAX_ALIGN * 4 + |
| 117 | BOOT_MAGIC_ALIGN_SIZE |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * Amount of space used to maintain progress information for a single swap |
| 123 | * operation. |
| 124 | */ |
| 125 | static inline uint32_t |
| 126 | boot_status_entry_sz(uint32_t min_write_sz) |
| 127 | { |
| 128 | return BOOT_STATUS_STATE_COUNT * min_write_sz; |
| 129 | } |
| 130 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 131 | uint32_t |
Fabio Utzig | 3fbbdac | 2019-12-19 15:18:23 -0300 | [diff] [blame] | 132 | boot_status_sz(uint32_t min_write_sz) |
| 133 | { |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 134 | return BOOT_STATUS_MAX_ENTRIES * boot_status_entry_sz(min_write_sz); |
Fabio Utzig | 3fbbdac | 2019-12-19 15:18:23 -0300 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | uint32_t |
David Brown | ab44918 | 2019-11-15 09:32:52 -0700 | [diff] [blame] | 138 | boot_trailer_sz(uint32_t min_write_sz) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 139 | { |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 140 | return boot_status_sz(min_write_sz) + boot_trailer_info_sz(); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 143 | #if !defined(MCUBOOT_SWAP_USING_STATUS) && defined(MCUBOOT_SWAP_USING_SCRATCH) |
| 144 | /* |
| 145 | * Similar to `boot_trailer_sz` but this function returns the space used to |
| 146 | * store status in the scratch partition. The scratch partition only stores |
| 147 | * status during the swap of the last sector from primary/secondary (which |
| 148 | * is the first swap operation) and thus only requires space for one swap. |
| 149 | */ |
| 150 | static uint32_t |
| 151 | boot_scratch_trailer_sz(uint32_t min_write_sz) |
| 152 | { |
| 153 | return boot_status_entry_sz(min_write_sz) + boot_trailer_info_sz(); |
| 154 | } |
| 155 | #endif |
| 156 | |
Fabio Utzig | 4cee4f7 | 2017-05-22 10:59:57 -0400 | [diff] [blame] | 157 | int |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 158 | boot_status_entries(int image_index, const struct flash_area *fap) |
Fabio Utzig | 4cee4f7 | 2017-05-22 10:59:57 -0400 | [diff] [blame] | 159 | { |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 160 | #if MCUBOOT_SWAP_USING_SCRATCH |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 161 | if (flash_area_get_id(fap) == FLASH_AREA_IMAGE_SCRATCH) { |
Fabio Utzig | 4cee4f7 | 2017-05-22 10:59:57 -0400 | [diff] [blame] | 162 | return BOOT_STATUS_STATE_COUNT; |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 163 | } else |
| 164 | #endif |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 165 | if (flash_area_get_id(fap) == FLASH_AREA_IMAGE_PRIMARY(image_index) || |
| 166 | flash_area_get_id(fap) == FLASH_AREA_IMAGE_SECONDARY(image_index)) { |
David Vincze | b75c12a | 2019-03-22 14:58:33 +0100 | [diff] [blame] | 167 | return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES; |
Fabio Utzig | 4cee4f7 | 2017-05-22 10:59:57 -0400 | [diff] [blame] | 168 | } |
Fabio Utzig | 9d16009 | 2019-08-09 07:46:34 -0300 | [diff] [blame] | 169 | return -1; |
Fabio Utzig | 4cee4f7 | 2017-05-22 10:59:57 -0400 | [diff] [blame] | 170 | } |
| 171 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 172 | #ifndef MCUBOOT_SWAP_USING_STATUS |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 173 | uint32_t |
| 174 | boot_status_off(const struct flash_area *fap) |
| 175 | { |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 176 | uint32_t off_from_end; |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 177 | uint32_t elem_sz; |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 178 | |
| 179 | elem_sz = flash_area_align(fap); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 180 | assert(elem_sz != 0u); |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 181 | |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 182 | #if MCUBOOT_SWAP_USING_SCRATCH |
| 183 | if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) { |
| 184 | off_from_end = boot_scratch_trailer_sz(elem_sz); |
| 185 | } else { |
| 186 | #endif |
| 187 | off_from_end = boot_trailer_sz(elem_sz); |
| 188 | #if MCUBOOT_SWAP_USING_SCRATCH |
| 189 | } |
| 190 | #endif |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 191 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 192 | assert(off_from_end <= flash_area_get_size(fap)); |
| 193 | return flash_area_get_size(fap) - off_from_end; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 196 | static uint32_t |
| 197 | boot_magic_decode(const uint8_t *magic) |
| 198 | { |
| 199 | if (memcmp(magic, BOOT_IMG_MAGIC, BOOT_MAGIC_SZ) == 0) { |
| 200 | return BOOT_MAGIC_GOOD; |
| 201 | } |
| 202 | return BOOT_MAGIC_BAD; |
| 203 | } |
Dovhal Artem (CSUKR CSS ICW SW FW 1) | f7a3d1b | 2022-04-01 15:07:37 +0000 | [diff] [blame] | 204 | |
Fabio Utzig | 4e8113b | 2019-08-09 09:11:18 -0300 | [diff] [blame] | 205 | static inline uint32_t |
| 206 | boot_magic_off(const struct flash_area *fap) |
| 207 | { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 208 | return flash_area_get_size(fap) - BOOT_MAGIC_SZ; |
Fabio Utzig | 4e8113b | 2019-08-09 09:11:18 -0300 | [diff] [blame] | 209 | } |
| 210 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 211 | |
Fabio Utzig | 4e8113b | 2019-08-09 09:11:18 -0300 | [diff] [blame] | 212 | static inline uint32_t |
| 213 | boot_image_ok_off(const struct flash_area *fap) |
| 214 | { |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 215 | return ALIGN_DOWN(boot_magic_off(fap) - BOOT_MAX_ALIGN, BOOT_MAX_ALIGN); |
Fabio Utzig | 4e8113b | 2019-08-09 09:11:18 -0300 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | static inline uint32_t |
| 219 | boot_copy_done_off(const struct flash_area *fap) |
| 220 | { |
| 221 | return boot_image_ok_off(fap) - BOOT_MAX_ALIGN; |
| 222 | } |
| 223 | |
Fabio Utzig | 4e8113b | 2019-08-09 09:11:18 -0300 | [diff] [blame] | 224 | static inline uint32_t |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 225 | boot_swap_size_off(const struct flash_area *fap) |
| 226 | { |
Fabio Utzig | 4e8113b | 2019-08-09 09:11:18 -0300 | [diff] [blame] | 227 | return boot_swap_info_off(fap) - BOOT_MAX_ALIGN; |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 228 | } |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 229 | #endif /* !MCUBOOT_SWAP_USING_STATUS */ |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 230 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 231 | #ifdef MCUBOOT_ENC_IMAGES |
Fabio Utzig | 4e8113b | 2019-08-09 09:11:18 -0300 | [diff] [blame] | 232 | static inline uint32_t |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 233 | boot_enc_key_off(const struct flash_area *fap, uint8_t slot) |
| 234 | { |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 235 | #if MCUBOOT_SWAP_SAVE_ENCTLV |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 236 | return boot_swap_size_off(fap) - (((uint32_t)slot + 1U) * BOOT_ENC_TLV_ALIGN_SIZE); |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 237 | #else |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 238 | return boot_swap_size_off(fap) - (((uint32_t)slot + 1U) * BOOT_ENC_KEY_ALIGN_SIZE); |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 239 | #endif |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 240 | } |
| 241 | #endif |
| 242 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 243 | #ifndef MCUBOOT_SWAP_USING_STATUS |
Fabio Utzig | df0cc50 | 2019-08-09 09:10:41 -0300 | [diff] [blame] | 244 | /** |
| 245 | * This functions tries to locate the status area after an aborted swap, |
| 246 | * by looking for the magic in the possible locations. |
| 247 | * |
Sam Bristow | d0ca0ff | 2019-10-30 20:51:35 +1300 | [diff] [blame] | 248 | * If the magic is successfully found, a flash_area * is returned and it |
Fabio Utzig | df0cc50 | 2019-08-09 09:10:41 -0300 | [diff] [blame] | 249 | * is the responsibility of the called to close it. |
| 250 | * |
| 251 | * @returns 0 on success, -1 on errors |
| 252 | */ |
| 253 | static int |
| 254 | boot_find_status(int image_index, const struct flash_area **fap) |
| 255 | { |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 256 | uint8_t magic[BOOT_MAGIC_SZ]; |
Fabio Utzig | df0cc50 | 2019-08-09 09:10:41 -0300 | [diff] [blame] | 257 | uint32_t off; |
| 258 | uint8_t areas[2] = { |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 259 | #if MCUBOOT_SWAP_USING_SCRATCH |
Fabio Utzig | df0cc50 | 2019-08-09 09:10:41 -0300 | [diff] [blame] | 260 | FLASH_AREA_IMAGE_SCRATCH, |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 261 | #endif |
Fabio Utzig | 3c44607 | 2019-11-22 12:48:26 -0300 | [diff] [blame] | 262 | FLASH_AREA_IMAGE_PRIMARY(image_index), |
Fabio Utzig | df0cc50 | 2019-08-09 09:10:41 -0300 | [diff] [blame] | 263 | }; |
| 264 | unsigned int i; |
| 265 | int rc; |
| 266 | |
| 267 | /* |
| 268 | * In the middle a swap, tries to locate the area that is currently |
| 269 | * storing a valid magic, first on the primary slot, then on scratch. |
| 270 | * Both "slots" can end up being temporary storage for a swap and it |
| 271 | * is assumed that if magic is valid then other metadata is too, |
| 272 | * because magic is always written in the last step. |
| 273 | */ |
| 274 | |
| 275 | for (i = 0; i < sizeof(areas) / sizeof(areas[0]); i++) { |
| 276 | rc = flash_area_open(areas[i], fap); |
| 277 | if (rc != 0) { |
| 278 | return rc; |
| 279 | } |
| 280 | |
| 281 | off = boot_magic_off(*fap); |
| 282 | rc = flash_area_read(*fap, off, magic, BOOT_MAGIC_SZ); |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 283 | flash_area_close(*fap); |
| 284 | |
Fabio Utzig | df0cc50 | 2019-08-09 09:10:41 -0300 | [diff] [blame] | 285 | if (rc != 0) { |
Fabio Utzig | df0cc50 | 2019-08-09 09:10:41 -0300 | [diff] [blame] | 286 | return rc; |
| 287 | } |
| 288 | |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 289 | if (BOOT_MAGIC_GOOD == boot_magic_decode(magic)) { |
Fabio Utzig | df0cc50 | 2019-08-09 09:10:41 -0300 | [diff] [blame] | 290 | return 0; |
| 291 | } |
| 292 | |
Fabio Utzig | df0cc50 | 2019-08-09 09:10:41 -0300 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | /* If we got here, no magic was found */ |
| 296 | return -1; |
| 297 | } |
| 298 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 299 | int |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 300 | boot_read_swap_size(int image_index, uint32_t *swap_size) |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 301 | { |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 302 | uint32_t off; |
| 303 | const struct flash_area *fap; |
| 304 | int rc; |
| 305 | |
Fabio Utzig | df0cc50 | 2019-08-09 09:10:41 -0300 | [diff] [blame] | 306 | rc = boot_find_status(image_index, &fap); |
| 307 | if (rc == 0) { |
| 308 | off = boot_swap_size_off(fap); |
| 309 | rc = flash_area_read(fap, off, swap_size, sizeof *swap_size); |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 310 | flash_area_close(fap); |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 311 | } |
| 312 | |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 313 | return rc; |
| 314 | } |
| 315 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 316 | #ifdef MCUBOOT_ENC_IMAGES |
| 317 | int |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 318 | boot_read_enc_key(int image_index, uint8_t slot, struct boot_status *bs) |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 319 | { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 320 | uint32_t off; |
| 321 | const struct flash_area *fap; |
| 322 | int rc; |
| 323 | |
Fabio Utzig | df0cc50 | 2019-08-09 09:10:41 -0300 | [diff] [blame] | 324 | rc = boot_find_status(image_index, &fap); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 325 | if (0 == rc) { |
Fabio Utzig | df0cc50 | 2019-08-09 09:10:41 -0300 | [diff] [blame] | 326 | off = boot_enc_key_off(fap, slot); |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 327 | #ifdef MCUBOOT_SWAP_SAVE_ENCTLV |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 328 | uint8_t aes_iv[BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE]; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 329 | |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 330 | rc = flash_area_read(fap, off, bs->enctlv[slot], BOOT_ENC_TLV_ALIGN_SIZE); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 331 | if (0 == rc) { |
| 332 | /* Only try to decrypt initialized TLV metadata */ |
| 333 | if (!bootutil_buffer_is_filled(bs->enctlv[slot], |
| 334 | BOOT_UNINITIALIZED_TLV_FILL, |
| 335 | BOOT_ENC_TLV_ALIGN_SIZE)) { |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 336 | rc = boot_enc_decrypt(bs->enctlv[slot], bs->enckey[slot], 0, aes_iv); |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | #else |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 340 | rc = flash_area_read(fap, off, bs->enckey[slot], BOOT_ENC_KEY_ALIGN_SIZE); |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 341 | #endif |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 342 | flash_area_close(fap); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 343 | } |
| 344 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 345 | return rc; |
| 346 | } |
| 347 | #endif |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 348 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 349 | #endif /* !MCUBOOT_SWAP_USING_STATUS */ |
| 350 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 351 | int |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 352 | boot_write_copy_done(const struct flash_area *fap) |
| 353 | { |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 354 | uint32_t off; |
| 355 | |
| 356 | off = boot_copy_done_off(fap); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 357 | BOOT_LOG_DBG("writing copy_done; fa_id=%u off=0x%" PRIx32 |
| 358 | " (0x%" PRIx32 ")", (unsigned)flash_area_get_id(fap), |
| 359 | off, flash_area_get_off(fap) + off); |
Fabio Utzig | 1a1ec17 | 2019-08-09 10:21:43 -0300 | [diff] [blame] | 360 | return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | int |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 364 | boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size) |
| 365 | { |
| 366 | uint32_t off; |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 367 | |
| 368 | off = boot_swap_size_off(fap); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 369 | BOOT_LOG_DBG("writing swap_size; fa_id=%u off=0x%" PRIx32 |
| 370 | " (0x%" PRIx32 ")", (unsigned)flash_area_get_id(fap), |
| 371 | off, flash_area_get_off(fap) + off); |
Fabio Utzig | 1a1ec17 | 2019-08-09 10:21:43 -0300 | [diff] [blame] | 372 | return boot_write_trailer(fap, off, (const uint8_t *) &swap_size, 4); |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 373 | } |
| 374 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 375 | #ifndef MCUBOOT_SWAP_USING_STATUS |
| 376 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 377 | #ifdef MCUBOOT_ENC_IMAGES |
| 378 | int |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 379 | boot_write_enc_key(const struct flash_area *fap, uint8_t slot, |
| 380 | const struct boot_status *bs) |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 381 | { |
| 382 | uint32_t off; |
| 383 | int rc; |
| 384 | |
| 385 | off = boot_enc_key_off(fap, slot); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 386 | BOOT_LOG_DBG("writing enc_key; fa_id=%u off=0x%" PRIx32 |
| 387 | " (0x%" PRIx32 ")", (unsigned)flash_area_get_id(fap), |
| 388 | off, flash_area_get_off(fap) + off); |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 389 | #ifdef MCUBOOT_SWAP_SAVE_ENCTLV |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 390 | rc = flash_area_write(fap, off, bs->enctlv[slot], BOOT_ENC_TLV_ALIGN_SIZE); |
| 391 | #else |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 392 | rc = flash_area_write(fap, off, bs->enckey[slot], BOOT_ENC_KEY_ALIGN_SIZE); |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 393 | #endif |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 394 | if (rc != 0) { |
| 395 | return BOOT_EFLASH; |
| 396 | } |
| 397 | |
| 398 | return 0; |
| 399 | } |
| 400 | #endif |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 401 | |
| 402 | #endif /* !MCUBOOT_SWAP_USING_STATUS */ |