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