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