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