Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-License-Identifier: Apache-2.0 |
| 3 | * |
| 4 | * Copyright (c) 2017-2019 Linaro LTD |
| 5 | * Copyright (c) 2016-2019 JUUL Labs |
| 6 | * Copyright (c) 2019-2020 Arm Limited |
| 7 | * Copyright (c) 2020 Cypress Semiconductors |
| 8 | * |
| 9 | * Original license: |
| 10 | * |
| 11 | * Licensed to the Apache Software Foundation (ASF) under one |
| 12 | * or more contributor license agreements. See the NOTICE file |
| 13 | * distributed with this work for additional information |
| 14 | * regarding copyright ownership. The ASF licenses this file |
| 15 | * to you under the Apache License, Version 2.0 (the |
| 16 | * "License"); you may not use this file except in compliance |
| 17 | * with the License. You may obtain a copy of the License at |
| 18 | * |
| 19 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 20 | * |
| 21 | * Unless required by applicable law or agreed to in writing, |
| 22 | * software distributed under the License is distributed on an |
| 23 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 24 | * KIND, either express or implied. See the License for the |
| 25 | * specific language governing permissions and limitations |
| 26 | * under the License. |
| 27 | */ |
| 28 | |
| 29 | #include <assert.h> |
| 30 | #include <stddef.h> |
| 31 | #include <stdbool.h> |
| 32 | #include <inttypes.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <string.h> |
| 35 | #include "bootutil/bootutil.h" |
| 36 | #include "bootutil_priv.h" |
| 37 | #include "swap_priv.h" |
| 38 | #include "bootutil/bootutil_log.h" |
| 39 | |
| 40 | #include "swap_status.h" |
| 41 | |
| 42 | #include "mcuboot_config/mcuboot_config.h" |
| 43 | |
| 44 | MCUBOOT_LOG_MODULE_DECLARE(mcuboot); |
| 45 | |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 46 | #define ERROR_VALUE (UINT32_MAX) |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 47 | |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 48 | #if defined(MCUBOOT_SWAP_USING_STATUS) |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 49 | |
| 50 | static int |
| 51 | boot_find_status(int image_index, const struct flash_area **fap); |
| 52 | |
| 53 | static int |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 54 | boot_magic_decode(const union boot_img_magic_t *magic_p) |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 55 | { |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 56 | if (memcmp((const void*)magic_p->val, (const void *)&boot_img_magic.val, BOOT_MAGIC_SZ) == 0) { |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 57 | return BOOT_MAGIC_GOOD; |
| 58 | } |
| 59 | return BOOT_MAGIC_BAD; |
| 60 | } |
| 61 | |
| 62 | static int |
| 63 | boot_flag_decode(uint8_t flag) |
| 64 | { |
| 65 | if (flag != (uint8_t)BOOT_FLAG_SET) { |
| 66 | return BOOT_FLAG_BAD; |
| 67 | } |
| 68 | return BOOT_FLAG_SET; |
| 69 | } |
| 70 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 71 | /* Offset Section */ |
| 72 | static inline uint32_t |
| 73 | boot_magic_off(const struct flash_area *fap) |
| 74 | { |
| 75 | (void)fap; |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 76 | return (uint32_t)BOOT_SWAP_STATUS_D_SIZE_RAW - (uint32_t)BOOT_MAGIC_SZ; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | uint32_t |
| 80 | boot_image_ok_off(const struct flash_area *fap) |
| 81 | { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 82 | return boot_magic_off(fap) - BOOT_SWAP_STATUS_IMG_OK_SZ; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | uint32_t |
| 86 | boot_copy_done_off(const struct flash_area *fap) |
| 87 | { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 88 | return boot_image_ok_off(fap) - BOOT_SWAP_STATUS_COPY_DONE_SZ; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | uint32_t |
| 92 | boot_swap_info_off(const struct flash_area *fap) |
| 93 | { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 94 | return boot_copy_done_off(fap) - BOOT_SWAP_STATUS_SWAPINF_SZ; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | uint32_t |
| 98 | boot_swap_size_off(const struct flash_area *fap) |
| 99 | { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 100 | return boot_swap_info_off(fap) - BOOT_SWAP_STATUS_SWAPSZ_SZ; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | uint32_t |
| 104 | boot_status_off(const struct flash_area *fap) |
| 105 | { |
| 106 | (void)fap; |
| 107 | /* this offset is equal to 0, because swap status fields |
| 108 | in this implementation count from the start of partition */ |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | #ifdef MCUBOOT_ENC_IMAGES |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 113 | /** |
| 114 | * @returns ERROR_VALUE on error, otherwise result. |
| 115 | */ |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 116 | static inline uint32_t |
| 117 | boot_enc_key_off(const struct flash_area *fap, uint8_t slot) |
| 118 | { |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 119 | uint32_t slot_offset; |
| 120 | uint32_t res = ERROR_VALUE; |
| 121 | uint32_t boot_swap_size_offset = boot_swap_size_off(fap); |
| 122 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 123 | #ifdef MCUBOOT_SWAP_SAVE_ENCTLV |
| 124 | /* suggest encryption key is also stored in status partition */ |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 125 | slot_offset = ((uint32_t)slot + 1UL) * (uint32_t)BOOT_ENC_TLV_SIZE; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 126 | #else |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 127 | slot_offset = ((uint32_t)slot + 1UL) * (uint32_t)BOOT_ENC_KEY_SIZE; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 128 | #endif |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 129 | |
| 130 | if (boot_swap_size_offset >= slot_offset) |
| 131 | { |
| 132 | res = boot_swap_size_offset - slot_offset; |
| 133 | } |
| 134 | return res; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 135 | } |
| 136 | #endif |
| 137 | |
| 138 | /** |
| 139 | * Write trailer data; status bytes, swap_size, etc |
| 140 | * |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 141 | * @returns 0 on success, -1 on error. |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 142 | */ |
| 143 | int |
| 144 | boot_write_trailer(const struct flash_area *fap, uint32_t off, |
| 145 | const uint8_t *inbuf, uint8_t inlen) |
| 146 | { |
| 147 | int rc; |
| 148 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 149 | /* copy status part trailer to primary image before set copy_done flag */ |
| 150 | if (boot_copy_done_off(fap) == off && |
| 151 | fap->fa_id == FLASH_AREA_IMAGE_PRIMARY(0u) && |
| 152 | BOOT_SWAP_STATUS_COPY_DONE_SZ == inlen) { |
| 153 | |
| 154 | BOOT_LOG_DBG("copy status part trailer to primary image slot"); |
| 155 | rc = swap_status_to_image_trailer(fap); |
| 156 | if (rc != 0) { |
| 157 | BOOT_LOG_ERR("trailer copy failed"); |
| 158 | return -1; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | rc = swap_status_update(fap->fa_id, off, inbuf, inlen); |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 163 | |
| 164 | if (rc != 0) { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 165 | return -1; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 166 | } |
| 167 | return rc; |
| 168 | } |
| 169 | |
| 170 | #ifdef MCUBOOT_ENC_IMAGES |
| 171 | int |
| 172 | boot_write_enc_key(const struct flash_area *fap, uint8_t slot, |
| 173 | const struct boot_status *bs) |
| 174 | { |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 175 | int rc; |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 176 | uint32_t off = boot_enc_key_off(fap, slot); |
| 177 | if (ERROR_VALUE == off) |
| 178 | { |
| 179 | return -1; |
| 180 | } |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 181 | #ifdef MCUBOOT_SWAP_SAVE_ENCTLV |
| 182 | rc = swap_status_update(fap->fa_id, off, |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 183 | bs->enctlv[slot], BOOT_ENC_TLV_ALIGN_SIZE); |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 184 | #else |
| 185 | rc = swap_status_update(fap->fa_id, off, |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 186 | bs->enckey[slot], BOOT_ENC_KEY_SIZE); |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 187 | #endif |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 188 | if (rc != 0) { |
| 189 | return -1; |
| 190 | } |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | int |
| 196 | boot_read_enc_key(int image_index, uint8_t slot, struct boot_status *bs) |
| 197 | { |
| 198 | uint32_t off; |
| 199 | const struct flash_area *fap; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 200 | int rc; |
| 201 | |
| 202 | rc = boot_find_status(image_index, &fap); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 203 | if (0 == rc) { |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 204 | off = boot_enc_key_off(fap, slot); |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 205 | if (ERROR_VALUE == off) |
| 206 | { |
| 207 | return -1; |
| 208 | } |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 209 | #ifdef MCUBOOT_SWAP_SAVE_ENCTLV |
| 210 | rc = swap_status_retrieve(fap->fa_id, off, bs->enctlv[slot], BOOT_ENC_TLV_ALIGN_SIZE); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 211 | if (0 == rc) { |
| 212 | uint8_t aes_iv[BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE]; |
| 213 | |
| 214 | /* Only try to decrypt initialized TLV metadata */ |
| 215 | if (!bootutil_buffer_is_filled(bs->enctlv[slot], |
| 216 | BOOT_UNINITIALIZED_TLV_FILL, |
| 217 | BOOT_ENC_TLV_ALIGN_SIZE)) { |
| 218 | rc = boot_enc_decrypt(bs->enctlv[slot], bs->enckey[slot], 0, aes_iv); |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | #else |
| 222 | rc = swap_status_retrieve(fap->fa_id, off, bs->enckey[slot], BOOT_ENC_KEY_SIZE); |
| 223 | #endif |
| 224 | flash_area_close(fap); |
| 225 | } |
| 226 | |
| 227 | return rc; |
| 228 | } |
| 229 | #endif /* MCUBOOT_ENC_IMAGES */ |
| 230 | |
| 231 | /* Write Section */ |
| 232 | int |
| 233 | boot_write_magic(const struct flash_area *fap) |
| 234 | { |
| 235 | uint32_t off; |
| 236 | int rc; |
| 237 | |
| 238 | off = boot_magic_off(fap); |
| 239 | |
| 240 | rc = swap_status_update(fap->fa_id, off, |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 241 | (const uint8_t *)&boot_img_magic, BOOT_MAGIC_SZ); |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 242 | |
| 243 | if (rc != 0) { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 244 | return -1; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 245 | } |
| 246 | return 0; |
| 247 | } |
| 248 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 249 | /** |
| 250 | * Writes the supplied boot status to the flash file system. The boot status |
| 251 | * contains the current state of an in-progress image copy operation. |
| 252 | * |
| 253 | * @param bs The boot status to write. |
| 254 | * |
| 255 | * @return 0 on success; nonzero on failure. |
| 256 | */ |
| 257 | int |
| 258 | boot_write_status(const struct boot_loader_state *state, struct boot_status *bs) |
| 259 | { |
| 260 | const struct flash_area *fap = NULL; |
| 261 | uint32_t off; |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 262 | uint8_t area_id; |
Dovhal Artem (CSUKR CSS ICW SW FW 1) | f7a3d1b | 2022-04-01 15:07:37 +0000 | [diff] [blame] | 263 | uint8_t tmp_state; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 264 | int rc; |
| 265 | (void)state; |
| 266 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 267 | if (bs->idx < BOOT_STATUS_IDX_0) { |
| 268 | return BOOT_EFLASH; |
| 269 | } |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 270 | /* NOTE: The first sector copied (that is the last sector on slot) contains |
| 271 | * the trailer. Since in the last step the primary slot is erased, the |
| 272 | * first two status writes go to the scratch which will be copied to |
| 273 | * the primary slot! |
| 274 | */ |
| 275 | |
| 276 | #ifdef MCUBOOT_SWAP_USING_SCRATCH |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 277 | if (bs->use_scratch != 0U) { |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 278 | /* Write to scratch status. */ |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 279 | area_id = (uint8_t)FLASH_AREA_IMAGE_SCRATCH; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 280 | } else |
| 281 | #endif |
| 282 | { |
| 283 | /* Write to the primary slot. */ |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 284 | area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state)); |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 285 | } |
| 286 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 287 | rc = flash_area_open(area_id, &fap); |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 288 | if (rc != 0) { |
| 289 | rc = BOOT_EFLASH; |
| 290 | goto done; |
| 291 | } |
| 292 | off = boot_status_off(fap) + boot_status_internal_off(bs, 1); |
| 293 | |
Dovhal Artem (CSUKR CSS ICW SW FW 1) | f7a3d1b | 2022-04-01 15:07:37 +0000 | [diff] [blame] | 294 | tmp_state = bs->state; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 295 | |
| 296 | rc = swap_status_update(fap->fa_id, off, &tmp_state, 1); |
| 297 | if (rc != 0) { |
| 298 | rc = BOOT_EFLASH; |
| 299 | goto done; |
| 300 | } |
| 301 | |
| 302 | done: |
| 303 | flash_area_close(fap); |
| 304 | |
| 305 | return rc; |
| 306 | } |
| 307 | |
INFINEON\DovhalA | 3d3b51d | 2024-06-12 17:01:18 +0300 | [diff] [blame^] | 308 | #if defined (MCUBOOT_SWAP_STATUS_FAST_BOOT) |
Roman Okhrimenko | 883cb5b | 2024-03-28 17:22:33 +0200 | [diff] [blame] | 309 | static inline int |
| 310 | boot_read_flag(const struct flash_area *fap, uint8_t *flag, uint32_t off) |
| 311 | { |
| 312 | int rc; |
| 313 | |
| 314 | do { |
| 315 | rc = flash_area_read(fap, off, flag, sizeof *flag); |
| 316 | |
| 317 | if (rc != 0) { |
| 318 | break; |
| 319 | } |
| 320 | |
| 321 | if (*flag == flash_area_erased_val(fap)) { |
| 322 | *flag = BOOT_FLAG_UNSET; |
| 323 | } else { |
| 324 | *flag = boot_flag_decode(*flag); |
| 325 | } |
| 326 | |
| 327 | } while (false); |
| 328 | |
| 329 | return rc; |
| 330 | } |
| 331 | |
| 332 | static inline uint32_t |
| 333 | boot_magic_off_trailer(const struct flash_area *fap) |
| 334 | { |
| 335 | return flash_area_get_size(fap) - BOOT_MAGIC_SZ; |
| 336 | } |
| 337 | |
| 338 | static inline uint32_t |
| 339 | boot_image_ok_off_trailer(const struct flash_area *fap) |
| 340 | { |
| 341 | return ALIGN_DOWN(boot_magic_off_trailer(fap) - BOOT_MAX_ALIGN, BOOT_MAX_ALIGN); |
| 342 | } |
| 343 | |
| 344 | static inline uint32_t |
| 345 | boot_copy_done_off_trailer(const struct flash_area *fap) |
| 346 | { |
| 347 | return boot_image_ok_off_trailer(fap) - BOOT_MAX_ALIGN; |
| 348 | } |
| 349 | |
| 350 | static inline uint32_t |
| 351 | boot_swap_info_off_trailer(const struct flash_area *fap) |
| 352 | { |
| 353 | return boot_copy_done_off_trailer(fap) - BOOT_MAX_ALIGN; |
| 354 | } |
| 355 | |
| 356 | int |
| 357 | boot_read_swap_state_trailer(const struct flash_area *fap, |
| 358 | struct boot_swap_state *state) |
| 359 | { |
| 360 | union boot_img_magic_t magic = {0U}; |
| 361 | uint32_t off; |
| 362 | uint8_t swap_info; |
| 363 | int rc; |
| 364 | |
| 365 | do { |
| 366 | off = boot_magic_off_trailer(fap); |
| 367 | rc = flash_area_read(fap, off, &magic, BOOT_MAGIC_SZ); |
| 368 | |
| 369 | if (rc != 0) { |
| 370 | break; |
| 371 | } |
| 372 | |
| 373 | if (bootutil_buffer_is_erased(fap, &magic, BOOT_MAGIC_SZ)) { |
| 374 | state->magic = BOOT_MAGIC_UNSET; |
| 375 | } else { |
| 376 | state->magic = boot_magic_decode(&magic); |
| 377 | } |
| 378 | |
| 379 | off = boot_swap_info_off_trailer(fap); |
| 380 | rc = flash_area_read(fap, off, &swap_info, sizeof swap_info); |
| 381 | |
| 382 | if (rc != 0) { |
| 383 | break; |
| 384 | } |
| 385 | |
| 386 | /* Extract the swap type and image number */ |
| 387 | state->swap_type = BOOT_GET_SWAP_TYPE(swap_info); |
| 388 | state->image_num = BOOT_GET_IMAGE_NUM(swap_info); |
| 389 | |
| 390 | if (swap_info == flash_area_erased_val(fap) || |
| 391 | state->swap_type > BOOT_SWAP_TYPE_REVERT) { |
| 392 | state->swap_type = BOOT_SWAP_TYPE_NONE; |
| 393 | state->image_num = 0; |
| 394 | } |
| 395 | |
| 396 | off = boot_copy_done_off_trailer(fap); |
| 397 | rc = boot_read_flag(fap, &state->copy_done, off); |
| 398 | |
| 399 | if (rc != 0) { |
| 400 | break; |
| 401 | } |
| 402 | |
| 403 | off = boot_image_ok_off_trailer(fap); |
| 404 | rc = boot_read_flag(fap, &state->image_ok, off); |
| 405 | |
| 406 | } while (false); |
| 407 | |
| 408 | return rc; |
| 409 | } |
INFINEON\DovhalA | 3d3b51d | 2024-06-12 17:01:18 +0300 | [diff] [blame^] | 410 | #endif |
Roman Okhrimenko | 883cb5b | 2024-03-28 17:22:33 +0200 | [diff] [blame] | 411 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 412 | int |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 413 | boot_read_swap_state(const struct flash_area *fap, |
| 414 | struct boot_swap_state *state) |
| 415 | { |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 416 | union boot_img_magic_t magic = {0U}; |
| 417 | uint32_t off = 0U; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 418 | uint32_t trailer_off = 0U; |
| 419 | uint8_t swap_info = 0U; |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 420 | int rc = 0; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 421 | uint32_t erase_trailer = 0; |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 422 | bool buf_is_clean = false; |
| 423 | bool is_primary = false; |
| 424 | bool is_secondary = false; |
| 425 | uint32_t i; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 426 | |
| 427 | const struct flash_area *fap_stat = NULL; |
| 428 | |
Roman Okhrimenko | 883cb5b | 2024-03-28 17:22:33 +0200 | [diff] [blame] | 429 | for (i = 0u; i < (uint32_t)BOOT_IMAGE_NUMBER; i++) { |
| 430 | if (fap->fa_id == FLASH_AREA_IMAGE_PRIMARY(i)) { |
| 431 | is_primary = true; |
| 432 | break; |
| 433 | } |
| 434 | if (fap->fa_id == FLASH_AREA_IMAGE_SECONDARY(i)) { |
| 435 | is_secondary = true; |
| 436 | break; |
| 437 | } |
| 438 | } |
| 439 | |
INFINEON\DovhalA | 3d3b51d | 2024-06-12 17:01:18 +0300 | [diff] [blame^] | 440 | #if defined(MCUBOOT_SWAP_STATUS_FAST_BOOT) |
Roman Okhrimenko | 883cb5b | 2024-03-28 17:22:33 +0200 | [diff] [blame] | 441 | { |
INFINEON\DovhalA | 3d3b51d | 2024-06-12 17:01:18 +0300 | [diff] [blame^] | 442 | bool is_scratch = fap->fa_id == FLASH_AREA_IMAGE_SCRATCH; |
| 443 | boot_read_swap_state_trailer(fap, state); |
| 444 | |
| 445 | if (is_primary) { |
| 446 | if (state->image_ok == BOOT_FLAG_SET && state->copy_done == BOOT_FLAG_SET && state->magic == BOOT_MAGIC_GOOD) { |
| 447 | return 0; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | if (is_secondary || is_scratch) { |
| 452 | if (state->image_ok == BOOT_FLAG_UNSET && state->copy_done == BOOT_FLAG_UNSET && state->magic == BOOT_MAGIC_UNSET) { |
| 453 | return 0; |
| 454 | } |
Roman Okhrimenko | 883cb5b | 2024-03-28 17:22:33 +0200 | [diff] [blame] | 455 | } |
| 456 | } |
INFINEON\DovhalA | 3d3b51d | 2024-06-12 17:01:18 +0300 | [diff] [blame^] | 457 | #endif |
Roman Okhrimenko | 883cb5b | 2024-03-28 17:22:33 +0200 | [diff] [blame] | 458 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 459 | rc = flash_area_open(FLASH_AREA_IMAGE_SWAP_STATUS, &fap_stat); |
| 460 | if (rc != 0) { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 461 | return -1; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | off = boot_magic_off(fap); |
| 465 | /* retrieve value for magic field from status partition area */ |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 466 | rc = swap_status_retrieve(fap->fa_id, off, &magic, BOOT_MAGIC_SZ); |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 467 | if (rc < 0) { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 468 | return -1; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 469 | } |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 470 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 471 | /* fill magic number value if equal to expected */ |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 472 | if (bootutil_buffer_is_erased(fap_stat, &magic, BOOT_MAGIC_SZ)) { |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 473 | state->magic = BOOT_MAGIC_UNSET; |
| 474 | |
| 475 | /* attempt to find magic in upgrade img slot trailer */ |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 476 | if (is_secondary) { |
| 477 | trailer_off = fap->fa_size - BOOT_MAGIC_SZ; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 478 | |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 479 | rc = flash_area_read(fap, trailer_off, &magic, BOOT_MAGIC_SZ); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 480 | if (rc != 0) { |
| 481 | return -1; |
| 482 | } |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 483 | buf_is_clean = bootutil_buffer_is_erased(fap, &magic, BOOT_MAGIC_SZ); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 484 | if (buf_is_clean) { |
| 485 | state->magic = BOOT_MAGIC_UNSET; |
| 486 | } else { |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 487 | state->magic = (uint8_t)boot_magic_decode(&magic); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 488 | /* put magic to status partition for upgrade slot*/ |
| 489 | if ((uint32_t)BOOT_MAGIC_GOOD == state->magic) { |
| 490 | rc = swap_status_update(fap->fa_id, off, |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 491 | (uint8_t *)&magic, BOOT_MAGIC_SZ); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 492 | } |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 493 | if (rc < 0) { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 494 | return -1; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 495 | } else { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 496 | erase_trailer = 1; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 497 | } |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 498 | } |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 499 | } |
| 500 | } else { |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 501 | state->magic = (uint8_t)boot_magic_decode(&magic); |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | off = boot_swap_info_off(fap); |
| 505 | rc = swap_status_retrieve(fap->fa_id, off, &swap_info, sizeof swap_info); |
| 506 | if (rc < 0) { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 507 | return -1; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 508 | } |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 509 | if (bootutil_buffer_is_erased(fap_stat, &swap_info, sizeof swap_info) || |
| 510 | state->swap_type >= (uint8_t)BOOT_SWAP_TYPE_FAIL) { |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 511 | state->swap_type = (uint8_t)BOOT_SWAP_TYPE_NONE; |
| 512 | state->image_num = 0; |
| 513 | } |
| 514 | else { |
| 515 | /* Extract the swap type and image number */ |
| 516 | state->swap_type = (uint8_t)BOOT_GET_SWAP_TYPE_M(swap_info); |
| 517 | state->image_num = (uint8_t)BOOT_GET_IMAGE_NUM_M(swap_info); |
| 518 | } |
| 519 | |
| 520 | off = boot_copy_done_off(fap); |
| 521 | rc = swap_status_retrieve(fap->fa_id, off, &state->copy_done, sizeof state->copy_done); |
| 522 | if (rc < 0) { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 523 | return -1; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 524 | } |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 525 | /* need to check swap_info was empty */ |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 526 | if (bootutil_buffer_is_erased(fap_stat, &state->copy_done, sizeof state->copy_done)) { |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 527 | state->copy_done = BOOT_FLAG_UNSET; |
| 528 | } else { |
| 529 | state->copy_done = (uint8_t)boot_flag_decode(state->copy_done); |
| 530 | } |
| 531 | |
| 532 | off = boot_image_ok_off(fap); |
| 533 | rc = swap_status_retrieve(fap->fa_id, off, &state->image_ok, sizeof state->image_ok); |
| 534 | if (rc < 0) { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 535 | return -1; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 536 | } |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 537 | /* need to check swap_info was empty */ |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 538 | if (bootutil_buffer_is_erased(fap_stat, &state->image_ok, sizeof state->image_ok)) { |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 539 | /* assign img_ok unset */ |
| 540 | state->image_ok = BOOT_FLAG_UNSET; |
| 541 | |
| 542 | /* attempt to read img_ok value in upgrade img slots trailer area |
| 543 | * it is set when image in slot for upgrade is signed for swap_type permanent |
| 544 | */ |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 545 | bool process_image_ok = (uint8_t)BOOT_FLAG_SET == state->copy_done; |
| 546 | if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) { |
| 547 | BOOT_LOG_DBG(" * selected SCRATCH area, copy_done = %u", (unsigned)state->copy_done); |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 548 | } |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 549 | else if (is_secondary) { |
| 550 | process_image_ok = true; |
| 551 | } |
| 552 | else if (!is_primary) { |
| 553 | process_image_ok = false; |
| 554 | rc = -1; |
| 555 | } |
| 556 | else { |
| 557 | /* Fix MISRA Rule 15.7 */ |
| 558 | } |
| 559 | if (process_image_ok) { |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 560 | trailer_off = fap->fa_size - (uint8_t)BOOT_MAGIC_SZ - (uint8_t)BOOT_MAX_ALIGN; |
| 561 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 562 | rc = flash_area_read(fap, trailer_off, &state->image_ok, sizeof state->image_ok); |
| 563 | if (rc != 0) { |
| 564 | return -1; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 565 | } |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 566 | |
| 567 | buf_is_clean = bootutil_buffer_is_erased(fap, &state->image_ok, sizeof state->image_ok); |
| 568 | if (buf_is_clean) { |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 569 | state->image_ok = BOOT_FLAG_UNSET; |
| 570 | } else { |
| 571 | state->image_ok = (uint8_t)boot_flag_decode(state->image_ok); |
| 572 | |
| 573 | /* put img_ok to status partition for upgrade slot */ |
| 574 | if (state->image_ok != (uint8_t)BOOT_FLAG_BAD) { |
| 575 | rc = swap_status_update(fap->fa_id, off, |
| 576 | &state->image_ok, sizeof state->image_ok); |
| 577 | } |
| 578 | if (rc < 0) { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 579 | return -1; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 580 | } |
| 581 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 582 | /* don't erase trailer, just move img_ok to status part */ |
| 583 | erase_trailer = 0; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | } else { |
| 587 | state->image_ok = (uint8_t)boot_flag_decode(state->image_ok); |
| 588 | } |
| 589 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 590 | if ((erase_trailer != 0u) && (fap->fa_id != FLASH_AREA_IMAGE_SCRATCH) && (0 == rc)) { |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 591 | /* erase magic from upgrade img trailer */ |
| 592 | rc = flash_area_erase(fap, trailer_off, BOOT_MAGIC_SZ); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 593 | if (rc != 0) { |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 594 | return rc; |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 595 | } |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 596 | } |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 597 | return rc; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | /** |
| 601 | * This functions tries to locate the status area after an aborted swap, |
| 602 | * by looking for the magic in the possible locations. |
| 603 | * |
| 604 | * If the magic is successfully found, a flash_area * is returned and it |
| 605 | * is the responsibility of the called to close it. |
| 606 | * |
| 607 | * @returns 0 on success, -1 on errors |
| 608 | */ |
| 609 | static int |
| 610 | boot_find_status(int image_index, const struct flash_area **fap) |
| 611 | { |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 612 | union boot_img_magic_t magic = {0}; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 613 | uint32_t off; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 614 | int rc = -1; |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 615 | uint8_t area = FLASH_AREA_ERROR; |
| 616 | |
| 617 | if ((image_index < 0) || (image_index >= MCUBOOT_IMAGE_NUMBER)) { |
| 618 | return rc; |
| 619 | } |
| 620 | /* the status is always in status partition */ |
| 621 | area = FLASH_AREA_IMAGE_PRIMARY((uint32_t)image_index); |
| 622 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 623 | |
| 624 | /* |
| 625 | * In the middle a swap, tries to locate the area that is currently |
| 626 | * storing a valid magic, first on the primary slot, then on scratch. |
| 627 | * Both "slots" can end up being temporary storage for a swap and it |
| 628 | * is assumed that if magic is valid then other metadata is too, |
| 629 | * because magic is always written in the last step. |
| 630 | */ |
| 631 | rc = flash_area_open(area, fap); |
| 632 | if (rc != 0) { |
| 633 | return rc; |
| 634 | } |
| 635 | off = boot_magic_off(*fap); |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 636 | rc = swap_status_retrieve(area, off, &magic, BOOT_MAGIC_SZ); |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 637 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 638 | if (0 == rc) { |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 639 | rc = memcmp((const void*)&magic.val, (const void *)&boot_img_magic.val, BOOT_MAGIC_SZ); |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | flash_area_close(*fap); |
| 643 | return rc; |
| 644 | } |
| 645 | |
| 646 | int |
| 647 | boot_read_swap_size(int image_index, uint32_t *swap_size) |
| 648 | { |
| 649 | uint32_t off; |
| 650 | const struct flash_area *fap; |
| 651 | int rc; |
| 652 | |
| 653 | rc = boot_find_status(image_index, &fap); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 654 | if (0 == rc) { |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 655 | off = boot_swap_size_off(fap); |
| 656 | |
| 657 | rc = swap_status_retrieve(fap->fa_id, off, swap_size, sizeof *swap_size); |
| 658 | } |
| 659 | return rc; |
| 660 | } |
| 661 | |
| 662 | int |
| 663 | swap_erase_trailer_sectors(const struct boot_loader_state *state, |
| 664 | const struct flash_area *fap) |
| 665 | { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 666 | int32_t sub_offs; |
| 667 | uint32_t trailer_offs; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 668 | uint8_t fa_id_primary; |
| 669 | uint8_t fa_id_secondary; |
| 670 | uint8_t image_index; |
| 671 | int rc; |
| 672 | (void)state; |
| 673 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 674 | BOOT_LOG_INF("Erasing trailer; fa_id=%u", (unsigned)fap->fa_id); |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 675 | /* trailer is located in status-partition */ |
| 676 | const struct flash_area *fap_stat = NULL; |
| 677 | |
| 678 | rc = flash_area_open(FLASH_AREA_IMAGE_SWAP_STATUS, &fap_stat); |
| 679 | if (rc != 0) { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 680 | return -1; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 681 | } |
| 682 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 683 | if (fap->fa_id != FLASH_AREA_IMAGE_SCRATCH) { |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 684 | image_index = BOOT_CURR_IMG(state); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 685 | rc = flash_area_id_from_multi_image_slot((int32_t)image_index, |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 686 | BOOT_PRIMARY_SLOT); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 687 | if (rc < 0) { |
| 688 | return -1; |
| 689 | } |
| 690 | fa_id_primary = (uint8_t)rc; |
| 691 | |
| 692 | rc = flash_area_id_from_multi_image_slot((int32_t)image_index, |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 693 | BOOT_SECONDARY_SLOT); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 694 | if (rc < 0) { |
| 695 | return -1; |
| 696 | } |
| 697 | fa_id_secondary = (uint8_t)rc; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 698 | |
| 699 | /* skip if Flash Area is not recognizable */ |
| 700 | if ((fap->fa_id != fa_id_primary) && (fap->fa_id != fa_id_secondary)) { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 701 | return -1; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 702 | } |
| 703 | } |
| 704 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 705 | sub_offs = swap_status_init_offset(fap->fa_id); |
| 706 | if (sub_offs < 0) { |
| 707 | return -1; |
| 708 | } |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 709 | |
| 710 | /* delete starting from last sector and moving to beginning */ |
| 711 | /* calculate last sector of status sub-area */ |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 712 | rc = flash_area_erase(fap_stat, (uint32_t)sub_offs, (uint32_t)BOOT_SWAP_STATUS_SIZE); |
| 713 | if (rc != 0) { |
| 714 | return -1; |
| 715 | } |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 716 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 717 | if (fap->fa_id != FLASH_AREA_IMAGE_SCRATCH) { |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 718 | /* |
| 719 | * it is also needed to erase trailer area in slots since they may contain |
| 720 | * data, which is already cleared in corresponding status partition |
| 721 | */ |
| 722 | trailer_offs = fap->fa_size - BOOT_SWAP_STATUS_TRAILER_SIZE; |
| 723 | rc = flash_area_erase(fap, trailer_offs, BOOT_SWAP_STATUS_TRAILER_SIZE); |
| 724 | } |
| 725 | |
| 726 | flash_area_close(fap_stat); |
| 727 | |
| 728 | return rc; |
| 729 | } |
| 730 | |
| 731 | int |
| 732 | swap_status_init(const struct boot_loader_state *state, |
| 733 | const struct flash_area *fap, |
| 734 | const struct boot_status *bs) |
| 735 | { |
Dovhal Artem (CSUKR CSS ICW SW FW 1) | f7a3d1b | 2022-04-01 15:07:37 +0000 | [diff] [blame] | 736 | struct boot_swap_state swap_state = {0}; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 737 | uint8_t image_index; |
| 738 | int rc; |
| 739 | |
| 740 | #if (BOOT_IMAGE_NUMBER == 1) |
| 741 | (void)state; |
| 742 | #endif |
| 743 | |
| 744 | image_index = BOOT_CURR_IMG(state); |
| 745 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 746 | BOOT_LOG_DBG("initializing status; fa_id=%u", (unsigned)fap->fa_id); |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 747 | |
| 748 | rc = boot_read_swap_state_by_id((int32_t)FLASH_AREA_IMAGE_SECONDARY(image_index), |
| 749 | &swap_state); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 750 | assert(0 == rc); |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 751 | |
| 752 | if (bs->swap_type != (uint8_t)BOOT_SWAP_TYPE_NONE) { |
| 753 | rc = boot_write_swap_info(fap, bs->swap_type, image_index); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 754 | assert(0 == rc); |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 755 | } |
| 756 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 757 | if ((uint8_t)BOOT_FLAG_SET == swap_state.image_ok) { |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 758 | rc = boot_write_image_ok(fap); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 759 | assert(0 == rc); |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | rc = boot_write_swap_size(fap, bs->swap_size); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 763 | assert(0 == rc); |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 764 | |
| 765 | #ifdef MCUBOOT_ENC_IMAGES |
| 766 | rc = boot_write_enc_key(fap, 0, bs); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 767 | assert(0 == rc); |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 768 | |
| 769 | rc = boot_write_enc_key(fap, 1, bs); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 770 | assert(0 == rc); |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 771 | #endif |
| 772 | |
| 773 | rc = boot_write_magic(fap); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 774 | assert(0 == rc); |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 775 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 776 | return rc; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | int |
| 780 | swap_read_status(struct boot_loader_state *state, struct boot_status *bs) |
| 781 | { |
| 782 | const struct flash_area *fap = NULL; |
| 783 | const struct flash_area *fap_stat = NULL; |
| 784 | uint32_t off; |
| 785 | uint8_t swap_info = 0; |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 786 | uint8_t area_id; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 787 | int rc = 0; |
| 788 | |
| 789 | bs->source = swap_status_source(state); |
| 790 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 791 | if (BOOT_STATUS_SOURCE_NONE == bs->source) { |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 792 | return 0; |
| 793 | } |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 794 | else if (BOOT_STATUS_SOURCE_PRIMARY_SLOT == bs->source) { |
| 795 | area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state)); |
| 796 | } |
| 797 | else if (BOOT_STATUS_SOURCE_SCRATCH == bs->source) { |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 798 | area_id = FLASH_AREA_IMAGE_SCRATCH; |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 799 | } |
| 800 | else { |
| 801 | return -1; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 802 | } |
| 803 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 804 | rc = flash_area_open(area_id, &fap); |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 805 | if (rc != 0) { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 806 | return -1; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | rc = flash_area_open(FLASH_AREA_IMAGE_SWAP_STATUS, &fap_stat); |
| 810 | if (rc != 0) { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 811 | return -1; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | rc = swap_read_status_bytes(fap, state, bs); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 815 | if (0 == rc) { |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 816 | off = boot_swap_info_off(fap); |
| 817 | rc = swap_status_retrieve((uint8_t)area_id, off, &swap_info, sizeof swap_info); |
| 818 | if (rc < 0) { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 819 | return -1; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 820 | } |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 821 | if (bootutil_buffer_is_erased(fap_stat, &swap_info, sizeof swap_info)) { |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 822 | BOOT_SET_SWAP_INFO_M(swap_info, 0u, (uint8_t)BOOT_SWAP_TYPE_NONE); |
| 823 | rc = 0; |
| 824 | } |
| 825 | |
| 826 | /* Extract the swap type info */ |
| 827 | bs->swap_type = BOOT_GET_SWAP_TYPE_M(swap_info); |
| 828 | } |
| 829 | |
| 830 | flash_area_close(fap); |
| 831 | flash_area_close(fap_stat); |
| 832 | |
| 833 | return rc; |
| 834 | } |
| 835 | |
| 836 | #endif /* MCUBOOT_SWAP_USING_STATUS */ |