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 | |
Bohdan Kovalchuk | 7460ae5 | 2020-07-21 17:03:57 +0300 | [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 | |
Fabio Utzig | 24a273d | 2017-04-20 08:21:31 -0300 | [diff] [blame] | 55 | const uint32_t boot_img_magic[] = { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 56 | 0xf395c277, |
| 57 | 0x7fefd260, |
| 58 | 0x0f505235, |
| 59 | 0x8079b62c, |
| 60 | }; |
| 61 | |
Hovland, Sigvart | 1d96f36 | 2018-09-25 13:23:42 +0200 | [diff] [blame] | 62 | #define BOOT_MAGIC_ARR_SZ \ |
| 63 | (sizeof boot_img_magic / sizeof boot_img_magic[0]) |
| 64 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 65 | struct boot_swap_table { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 66 | uint8_t magic_primary_slot; |
| 67 | uint8_t magic_secondary_slot; |
| 68 | uint8_t image_ok_primary_slot; |
| 69 | uint8_t image_ok_secondary_slot; |
| 70 | uint8_t copy_done_primary_slot; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 71 | |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 72 | uint8_t swap_type; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | /** |
| 76 | * This set of tables maps image trailer contents to swap operation type. |
| 77 | * When searching for a match, these tables must be iterated sequentially. |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 78 | * |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 79 | * NOTE: the table order is very important. The settings in the secondary |
| 80 | * slot always are priority to the primary slot and should be located |
| 81 | * earlier in the table. |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 82 | * |
| 83 | * The table lists only states where there is action needs to be taken by |
| 84 | * the bootloader, as in starting/finishing a swap operation. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 85 | */ |
| 86 | static const struct boot_swap_table boot_swap_tables[] = { |
| 87 | { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 88 | .magic_primary_slot = BOOT_MAGIC_ANY, |
| 89 | .magic_secondary_slot = BOOT_MAGIC_GOOD, |
| 90 | .image_ok_primary_slot = BOOT_FLAG_ANY, |
| 91 | .image_ok_secondary_slot = BOOT_FLAG_UNSET, |
| 92 | .copy_done_primary_slot = BOOT_FLAG_ANY, |
| 93 | .swap_type = BOOT_SWAP_TYPE_TEST, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 94 | }, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 95 | { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 96 | .magic_primary_slot = BOOT_MAGIC_ANY, |
| 97 | .magic_secondary_slot = BOOT_MAGIC_GOOD, |
| 98 | .image_ok_primary_slot = BOOT_FLAG_ANY, |
| 99 | .image_ok_secondary_slot = BOOT_FLAG_SET, |
| 100 | .copy_done_primary_slot = BOOT_FLAG_ANY, |
| 101 | .swap_type = BOOT_SWAP_TYPE_PERM, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 102 | }, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 103 | { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 104 | .magic_primary_slot = BOOT_MAGIC_GOOD, |
| 105 | .magic_secondary_slot = BOOT_MAGIC_UNSET, |
| 106 | .image_ok_primary_slot = BOOT_FLAG_UNSET, |
| 107 | .image_ok_secondary_slot = BOOT_FLAG_ANY, |
| 108 | .copy_done_primary_slot = BOOT_FLAG_SET, |
| 109 | .swap_type = BOOT_SWAP_TYPE_REVERT, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 110 | }, |
| 111 | }; |
| 112 | |
| 113 | #define BOOT_SWAP_TABLES_COUNT \ |
| 114 | (sizeof boot_swap_tables / sizeof boot_swap_tables[0]) |
| 115 | |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 116 | /** |
| 117 | * @brief Determine if the data at two memory addresses is equal |
| 118 | * |
| 119 | * @param s1 The first memory region to compare. |
| 120 | * @param s2 The second memory region to compare. |
| 121 | * @param n The amount of bytes to compare. |
| 122 | * |
| 123 | * @note This function does not comply with the specification of memcmp, |
| 124 | * so should not be considered a drop-in replacement. It has no |
| 125 | * constant time execution. The point is to make sure that all the |
| 126 | * bytes are compared and detect if loop was abused and some cycles |
| 127 | * was skipped due to fault injection. |
| 128 | * |
| 129 | * @return FIH_SUCCESS if memory regions are equal, otherwise FIH_FAILURE |
| 130 | */ |
| 131 | #ifdef MCUBOOT_FIH_PROFILE_OFF |
| 132 | inline |
| 133 | fih_int boot_fih_memequal(const void *s1, const void *s2, size_t n) |
| 134 | { |
| 135 | return memcmp(s1, s2, n); |
| 136 | } |
| 137 | #else |
| 138 | fih_int boot_fih_memequal(const void *s1, const void *s2, size_t n) |
| 139 | { |
| 140 | size_t i; |
| 141 | uint8_t *s1_p = (uint8_t*) s1; |
| 142 | uint8_t *s2_p = (uint8_t*) s2; |
| 143 | fih_int ret = FIH_FAILURE; |
| 144 | |
| 145 | for (i = 0; i < n; i++) { |
| 146 | if (s1_p[i] != s2_p[i]) { |
| 147 | goto out; |
| 148 | } |
| 149 | } |
| 150 | if (i == n) { |
| 151 | ret = FIH_SUCCESS; |
| 152 | } |
| 153 | |
| 154 | out: |
| 155 | FIH_RET(ret); |
| 156 | } |
| 157 | #endif |
| 158 | |
Bohdan Kovalchuk | 7460ae5 | 2020-07-21 17:03:57 +0300 | [diff] [blame^] | 159 | #ifndef MCUBOOT_SWAP_USING_STATUS |
| 160 | |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 161 | static int |
Fabio Utzig | 178be54 | 2018-09-19 08:12:56 -0300 | [diff] [blame] | 162 | boot_magic_decode(const uint32_t *magic) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 163 | { |
Fabio Utzig | 24a273d | 2017-04-20 08:21:31 -0300 | [diff] [blame] | 164 | if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 165 | return BOOT_MAGIC_GOOD; |
| 166 | } |
Fabio Utzig | 178be54 | 2018-09-19 08:12:56 -0300 | [diff] [blame] | 167 | return BOOT_MAGIC_BAD; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 170 | static int |
Fabio Utzig | 178be54 | 2018-09-19 08:12:56 -0300 | [diff] [blame] | 171 | boot_flag_decode(uint8_t flag) |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 172 | { |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 173 | if (flag != BOOT_FLAG_SET) { |
| 174 | return BOOT_FLAG_BAD; |
| 175 | } |
| 176 | return BOOT_FLAG_SET; |
| 177 | } |
Bohdan Kovalchuk | 7460ae5 | 2020-07-21 17:03:57 +0300 | [diff] [blame^] | 178 | #endif /* not defined MCUBOOT_SWAP_USING_STATUS */ |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 179 | /** |
| 180 | * Determines if a status source table is satisfied by the specified magic |
| 181 | * code. |
| 182 | * |
| 183 | * @param tbl_val A magic field from a status source table. |
| 184 | * @param val The magic value in a trailer, encoded as a |
| 185 | * BOOT_MAGIC_[...]. |
| 186 | * |
| 187 | * @return 1 if the two values are compatible; |
| 188 | * 0 otherwise. |
| 189 | */ |
| 190 | int |
| 191 | boot_magic_compatible_check(uint8_t tbl_val, uint8_t val) |
| 192 | { |
| 193 | switch (tbl_val) { |
| 194 | case BOOT_MAGIC_ANY: |
| 195 | return 1; |
| 196 | |
| 197 | case BOOT_MAGIC_NOTGOOD: |
| 198 | return val != BOOT_MAGIC_GOOD; |
| 199 | |
| 200 | default: |
| 201 | return tbl_val == val; |
| 202 | } |
| 203 | } |
| 204 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 205 | uint32_t |
Fabio Utzig | 3fbbdac | 2019-12-19 15:18:23 -0300 | [diff] [blame] | 206 | boot_status_sz(uint32_t min_write_sz) |
| 207 | { |
| 208 | return /* state for all sectors */ |
| 209 | BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz; |
| 210 | } |
| 211 | |
| 212 | uint32_t |
David Brown | ab44918 | 2019-11-15 09:32:52 -0700 | [diff] [blame] | 213 | boot_trailer_sz(uint32_t min_write_sz) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 214 | { |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 215 | return /* state for all sectors */ |
Fabio Utzig | 3fbbdac | 2019-12-19 15:18:23 -0300 | [diff] [blame] | 216 | boot_status_sz(min_write_sz) + |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 217 | #ifdef MCUBOOT_ENC_IMAGES |
| 218 | /* encryption keys */ |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 219 | # if MCUBOOT_SWAP_SAVE_ENCTLV |
| 220 | BOOT_ENC_TLV_ALIGN_SIZE * 2 + |
| 221 | # else |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 222 | BOOT_ENC_KEY_SIZE * 2 + |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 223 | # endif |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 224 | #endif |
Christopher Collins | 2adef70 | 2019-05-22 14:37:31 -0700 | [diff] [blame] | 225 | /* swap_type + copy_done + image_ok + swap_size */ |
| 226 | BOOT_MAX_ALIGN * 4 + |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 227 | BOOT_MAGIC_SZ; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 228 | } |
| 229 | |
Fabio Utzig | 4cee4f7 | 2017-05-22 10:59:57 -0400 | [diff] [blame] | 230 | int |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 231 | boot_status_entries(int image_index, const struct flash_area *fap) |
Fabio Utzig | 4cee4f7 | 2017-05-22 10:59:57 -0400 | [diff] [blame] | 232 | { |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 233 | #if MCUBOOT_SWAP_USING_SCRATCH |
David Vincze | b75c12a | 2019-03-22 14:58:33 +0100 | [diff] [blame] | 234 | if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) { |
Fabio Utzig | 4cee4f7 | 2017-05-22 10:59:57 -0400 | [diff] [blame] | 235 | return BOOT_STATUS_STATE_COUNT; |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 236 | } else |
| 237 | #endif |
| 238 | if (fap->fa_id == FLASH_AREA_IMAGE_PRIMARY(image_index) || |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 239 | fap->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) { |
David Vincze | b75c12a | 2019-03-22 14:58:33 +0100 | [diff] [blame] | 240 | return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES; |
Fabio Utzig | 4cee4f7 | 2017-05-22 10:59:57 -0400 | [diff] [blame] | 241 | } |
Fabio Utzig | 9d16009 | 2019-08-09 07:46:34 -0300 | [diff] [blame] | 242 | return -1; |
Fabio Utzig | 4cee4f7 | 2017-05-22 10:59:57 -0400 | [diff] [blame] | 243 | } |
| 244 | |
Bohdan Kovalchuk | 7460ae5 | 2020-07-21 17:03:57 +0300 | [diff] [blame^] | 245 | #ifndef MCUBOOT_SWAP_USING_STATUS |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 246 | uint32_t |
| 247 | boot_status_off(const struct flash_area *fap) |
| 248 | { |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 249 | uint32_t off_from_end; |
| 250 | uint8_t elem_sz; |
| 251 | |
| 252 | elem_sz = flash_area_align(fap); |
| 253 | |
Christopher Collins | 2adef70 | 2019-05-22 14:37:31 -0700 | [diff] [blame] | 254 | off_from_end = boot_trailer_sz(elem_sz); |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 255 | |
| 256 | assert(off_from_end <= fap->fa_size); |
| 257 | return fap->fa_size - off_from_end; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 258 | } |
Bohdan Kovalchuk | 7460ae5 | 2020-07-21 17:03:57 +0300 | [diff] [blame^] | 259 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 260 | |
Fabio Utzig | 4e8113b | 2019-08-09 09:11:18 -0300 | [diff] [blame] | 261 | static inline uint32_t |
| 262 | boot_magic_off(const struct flash_area *fap) |
| 263 | { |
| 264 | return fap->fa_size - BOOT_MAGIC_SZ; |
| 265 | } |
| 266 | |
Bohdan Kovalchuk | 7460ae5 | 2020-07-21 17:03:57 +0300 | [diff] [blame^] | 267 | #ifndef MCUBOOT_SWAP_USING_STATUS |
| 268 | |
Fabio Utzig | 4e8113b | 2019-08-09 09:11:18 -0300 | [diff] [blame] | 269 | static inline uint32_t |
| 270 | boot_image_ok_off(const struct flash_area *fap) |
| 271 | { |
| 272 | return boot_magic_off(fap) - BOOT_MAX_ALIGN; |
| 273 | } |
| 274 | |
| 275 | static inline uint32_t |
| 276 | boot_copy_done_off(const struct flash_area *fap) |
| 277 | { |
| 278 | return boot_image_ok_off(fap) - BOOT_MAX_ALIGN; |
| 279 | } |
| 280 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 281 | uint32_t |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 282 | boot_swap_info_off(const struct flash_area *fap) |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 283 | { |
Fabio Utzig | 4e8113b | 2019-08-09 09:11:18 -0300 | [diff] [blame] | 284 | return boot_copy_done_off(fap) - BOOT_MAX_ALIGN; |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Fabio Utzig | 4e8113b | 2019-08-09 09:11:18 -0300 | [diff] [blame] | 287 | static inline uint32_t |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 288 | boot_swap_size_off(const struct flash_area *fap) |
| 289 | { |
Fabio Utzig | 4e8113b | 2019-08-09 09:11:18 -0300 | [diff] [blame] | 290 | return boot_swap_info_off(fap) - BOOT_MAX_ALIGN; |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 291 | } |
Bohdan Kovalchuk | 7460ae5 | 2020-07-21 17:03:57 +0300 | [diff] [blame^] | 292 | #endif |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 293 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 294 | #ifdef MCUBOOT_ENC_IMAGES |
Fabio Utzig | 4e8113b | 2019-08-09 09:11:18 -0300 | [diff] [blame] | 295 | static inline uint32_t |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 296 | boot_enc_key_off(const struct flash_area *fap, uint8_t slot) |
| 297 | { |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 298 | #if MCUBOOT_SWAP_SAVE_ENCTLV |
| 299 | return boot_swap_size_off(fap) - ((slot + 1) * |
| 300 | ((((BOOT_ENC_TLV_SIZE - 1) / BOOT_MAX_ALIGN) + 1) * BOOT_MAX_ALIGN)); |
| 301 | #else |
Fabio Utzig | 4e8113b | 2019-08-09 09:11:18 -0300 | [diff] [blame] | 302 | return boot_swap_size_off(fap) - ((slot + 1) * BOOT_ENC_KEY_SIZE); |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 303 | #endif |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 304 | } |
| 305 | #endif |
| 306 | |
Fabio Utzig | 4b2e55f | 2020-09-24 11:49:20 -0300 | [diff] [blame] | 307 | bool bootutil_buffer_is_erased(const struct flash_area *area, |
| 308 | const void *buffer, size_t len) |
| 309 | { |
| 310 | size_t i; |
| 311 | uint8_t *u8b; |
| 312 | uint8_t erased_val; |
| 313 | |
| 314 | if (buffer == NULL || len == 0) { |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | erased_val = flash_area_erased_val(area); |
| 319 | for (i = 0, u8b = (uint8_t *)buffer; i < len; i++) { |
| 320 | if (u8b[i] != erased_val) { |
| 321 | return false; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | return true; |
| 326 | } |
| 327 | |
Bohdan Kovalchuk | 7460ae5 | 2020-07-21 17:03:57 +0300 | [diff] [blame^] | 328 | #ifndef MCUBOOT_SWAP_USING_STATUS |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 329 | int |
| 330 | boot_read_swap_state(const struct flash_area *fap, |
| 331 | struct boot_swap_state *state) |
| 332 | { |
Hovland, Sigvart | 1d96f36 | 2018-09-25 13:23:42 +0200 | [diff] [blame] | 333 | uint32_t magic[BOOT_MAGIC_ARR_SZ]; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 334 | uint32_t off; |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 335 | uint8_t swap_info; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 336 | int rc; |
| 337 | |
| 338 | off = boot_magic_off(fap); |
Fabio Utzig | 4b2e55f | 2020-09-24 11:49:20 -0300 | [diff] [blame] | 339 | rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ); |
Fabio Utzig | 178be54 | 2018-09-19 08:12:56 -0300 | [diff] [blame] | 340 | if (rc < 0) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 341 | return BOOT_EFLASH; |
| 342 | } |
Fabio Utzig | 4b2e55f | 2020-09-24 11:49:20 -0300 | [diff] [blame] | 343 | if (bootutil_buffer_is_erased(fap, magic, BOOT_MAGIC_SZ)) { |
Fabio Utzig | 178be54 | 2018-09-19 08:12:56 -0300 | [diff] [blame] | 344 | state->magic = BOOT_MAGIC_UNSET; |
| 345 | } else { |
| 346 | state->magic = boot_magic_decode(magic); |
| 347 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 348 | |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 349 | off = boot_swap_info_off(fap); |
Fabio Utzig | 4b2e55f | 2020-09-24 11:49:20 -0300 | [diff] [blame] | 350 | rc = flash_area_read(fap, off, &swap_info, sizeof swap_info); |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 351 | if (rc < 0) { |
| 352 | return BOOT_EFLASH; |
| 353 | } |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 354 | |
| 355 | /* Extract the swap type and image number */ |
| 356 | state->swap_type = BOOT_GET_SWAP_TYPE(swap_info); |
| 357 | state->image_num = BOOT_GET_IMAGE_NUM(swap_info); |
| 358 | |
Fabio Utzig | 4b2e55f | 2020-09-24 11:49:20 -0300 | [diff] [blame] | 359 | if (bootutil_buffer_is_erased(fap, &swap_info, sizeof swap_info) || |
| 360 | state->swap_type > BOOT_SWAP_TYPE_REVERT) { |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 361 | state->swap_type = BOOT_SWAP_TYPE_NONE; |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 362 | state->image_num = 0; |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Christopher Collins | 2adef70 | 2019-05-22 14:37:31 -0700 | [diff] [blame] | 365 | off = boot_copy_done_off(fap); |
Fabio Utzig | 4b2e55f | 2020-09-24 11:49:20 -0300 | [diff] [blame] | 366 | rc = flash_area_read(fap, off, &state->copy_done, sizeof state->copy_done); |
Christopher Collins | 2adef70 | 2019-05-22 14:37:31 -0700 | [diff] [blame] | 367 | if (rc < 0) { |
| 368 | return BOOT_EFLASH; |
| 369 | } |
Fabio Utzig | 4b2e55f | 2020-09-24 11:49:20 -0300 | [diff] [blame] | 370 | if (bootutil_buffer_is_erased(fap, &state->copy_done, |
| 371 | sizeof state->copy_done)) { |
Christopher Collins | 2adef70 | 2019-05-22 14:37:31 -0700 | [diff] [blame] | 372 | state->copy_done = BOOT_FLAG_UNSET; |
| 373 | } else { |
| 374 | state->copy_done = boot_flag_decode(state->copy_done); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | off = boot_image_ok_off(fap); |
Fabio Utzig | 4b2e55f | 2020-09-24 11:49:20 -0300 | [diff] [blame] | 378 | rc = flash_area_read(fap, off, &state->image_ok, sizeof state->image_ok); |
Fabio Utzig | 178be54 | 2018-09-19 08:12:56 -0300 | [diff] [blame] | 379 | if (rc < 0) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 380 | return BOOT_EFLASH; |
| 381 | } |
Fabio Utzig | 4b2e55f | 2020-09-24 11:49:20 -0300 | [diff] [blame] | 382 | if (bootutil_buffer_is_erased(fap, &state->image_ok, |
| 383 | sizeof state->image_ok)) { |
Fabio Utzig | 178be54 | 2018-09-19 08:12:56 -0300 | [diff] [blame] | 384 | state->image_ok = BOOT_FLAG_UNSET; |
| 385 | } else { |
| 386 | state->image_ok = boot_flag_decode(state->image_ok); |
| 387 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 388 | |
| 389 | return 0; |
| 390 | } |
Bohdan Kovalchuk | 7460ae5 | 2020-07-21 17:03:57 +0300 | [diff] [blame^] | 391 | #endif /* not MCUBOOT_SWAP_USING_STATUS */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 392 | |
| 393 | /** |
| 394 | * Reads the image trailer from the scratch area. |
| 395 | */ |
| 396 | int |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 397 | boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 398 | { |
| 399 | const struct flash_area *fap; |
| 400 | int rc; |
| 401 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 402 | rc = flash_area_open(flash_area_id, &fap); |
| 403 | if (rc != 0) { |
| 404 | return BOOT_EFLASH; |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | rc = boot_read_swap_state(fap, state); |
Fabio Utzig | acfba2e | 2017-05-22 11:06:29 -0400 | [diff] [blame] | 408 | flash_area_close(fap); |
| 409 | return rc; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 410 | } |
| 411 | |
Bohdan Kovalchuk | 7460ae5 | 2020-07-21 17:03:57 +0300 | [diff] [blame^] | 412 | #ifndef MCUBOOT_SWAP_USING_STATUS |
| 413 | |
Fabio Utzig | df0cc50 | 2019-08-09 09:10:41 -0300 | [diff] [blame] | 414 | /** |
| 415 | * This functions tries to locate the status area after an aborted swap, |
| 416 | * by looking for the magic in the possible locations. |
| 417 | * |
Sam Bristow | d0ca0ff | 2019-10-30 20:51:35 +1300 | [diff] [blame] | 418 | * 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] | 419 | * is the responsibility of the called to close it. |
| 420 | * |
| 421 | * @returns 0 on success, -1 on errors |
| 422 | */ |
| 423 | static int |
| 424 | boot_find_status(int image_index, const struct flash_area **fap) |
| 425 | { |
| 426 | uint32_t magic[BOOT_MAGIC_ARR_SZ]; |
| 427 | uint32_t off; |
| 428 | uint8_t areas[2] = { |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 429 | #if MCUBOOT_SWAP_USING_SCRATCH |
Fabio Utzig | df0cc50 | 2019-08-09 09:10:41 -0300 | [diff] [blame] | 430 | FLASH_AREA_IMAGE_SCRATCH, |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 431 | #endif |
Fabio Utzig | 3c44607 | 2019-11-22 12:48:26 -0300 | [diff] [blame] | 432 | FLASH_AREA_IMAGE_PRIMARY(image_index), |
Fabio Utzig | df0cc50 | 2019-08-09 09:10:41 -0300 | [diff] [blame] | 433 | }; |
| 434 | unsigned int i; |
| 435 | int rc; |
| 436 | |
| 437 | /* |
| 438 | * In the middle a swap, tries to locate the area that is currently |
| 439 | * storing a valid magic, first on the primary slot, then on scratch. |
| 440 | * Both "slots" can end up being temporary storage for a swap and it |
| 441 | * is assumed that if magic is valid then other metadata is too, |
| 442 | * because magic is always written in the last step. |
| 443 | */ |
| 444 | |
| 445 | for (i = 0; i < sizeof(areas) / sizeof(areas[0]); i++) { |
| 446 | rc = flash_area_open(areas[i], fap); |
| 447 | if (rc != 0) { |
| 448 | return rc; |
| 449 | } |
| 450 | |
| 451 | off = boot_magic_off(*fap); |
| 452 | rc = flash_area_read(*fap, off, magic, BOOT_MAGIC_SZ); |
| 453 | if (rc != 0) { |
| 454 | flash_area_close(*fap); |
| 455 | return rc; |
| 456 | } |
| 457 | |
| 458 | if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) { |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | flash_area_close(*fap); |
| 463 | } |
| 464 | |
| 465 | /* If we got here, no magic was found */ |
| 466 | return -1; |
| 467 | } |
| 468 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 469 | int |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 470 | boot_read_swap_size(int image_index, uint32_t *swap_size) |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 471 | { |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 472 | uint32_t off; |
| 473 | const struct flash_area *fap; |
| 474 | int rc; |
| 475 | |
Fabio Utzig | df0cc50 | 2019-08-09 09:10:41 -0300 | [diff] [blame] | 476 | rc = boot_find_status(image_index, &fap); |
| 477 | if (rc == 0) { |
| 478 | off = boot_swap_size_off(fap); |
| 479 | rc = flash_area_read(fap, off, swap_size, sizeof *swap_size); |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 480 | flash_area_close(fap); |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 481 | } |
| 482 | |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 483 | return rc; |
| 484 | } |
| 485 | |
Bohdan Kovalchuk | 7460ae5 | 2020-07-21 17:03:57 +0300 | [diff] [blame^] | 486 | #endif /* not MCUBOOT_SWAP_USING_STATUS */ |
| 487 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 488 | #ifdef MCUBOOT_ENC_IMAGES |
| 489 | int |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 490 | 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] | 491 | { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 492 | uint32_t off; |
| 493 | const struct flash_area *fap; |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 494 | #if MCUBOOT_SWAP_SAVE_ENCTLV |
| 495 | int i; |
| 496 | #endif |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 497 | int rc; |
| 498 | |
Fabio Utzig | df0cc50 | 2019-08-09 09:10:41 -0300 | [diff] [blame] | 499 | rc = boot_find_status(image_index, &fap); |
| 500 | if (rc == 0) { |
| 501 | off = boot_enc_key_off(fap, slot); |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 502 | #if MCUBOOT_SWAP_SAVE_ENCTLV |
| 503 | rc = flash_area_read(fap, off, bs->enctlv[slot], BOOT_ENC_TLV_ALIGN_SIZE); |
| 504 | if (rc == 0) { |
| 505 | for (i = 0; i < BOOT_ENC_TLV_ALIGN_SIZE; i++) { |
| 506 | if (bs->enctlv[slot][i] != 0xff) { |
| 507 | break; |
| 508 | } |
| 509 | } |
| 510 | /* Only try to decrypt non-erased TLV metadata */ |
| 511 | if (i != BOOT_ENC_TLV_ALIGN_SIZE) { |
| 512 | rc = boot_enc_decrypt(bs->enctlv[slot], bs->enckey[slot]); |
| 513 | } |
| 514 | } |
| 515 | #else |
| 516 | rc = flash_area_read(fap, off, bs->enckey[slot], BOOT_ENC_KEY_SIZE); |
| 517 | #endif |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 518 | flash_area_close(fap); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 519 | } |
| 520 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 521 | return rc; |
| 522 | } |
| 523 | #endif |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 524 | |
Bohdan Kovalchuk | 7460ae5 | 2020-07-21 17:03:57 +0300 | [diff] [blame^] | 525 | #ifndef MCUBOOT_SWAP_USING_STATUS |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 526 | int |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 527 | boot_write_magic(const struct flash_area *fap) |
| 528 | { |
| 529 | uint32_t off; |
| 530 | int rc; |
| 531 | |
| 532 | off = boot_magic_off(fap); |
| 533 | |
Ben McCrea | 4c0ee95 | 2019-08-28 09:13:24 -0700 | [diff] [blame] | 534 | BOOT_LOG_DBG("writing magic; fa_id=%d off=0x%lx (0x%lx)", |
Marti Bolivar | 99ec383 | 2019-09-12 21:21:26 -0600 | [diff] [blame] | 535 | fap->fa_id, (unsigned long)off, |
| 536 | (unsigned long)(fap->fa_off + off)); |
Fabio Utzig | 24a273d | 2017-04-20 08:21:31 -0300 | [diff] [blame] | 537 | rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 538 | if (rc != 0) { |
| 539 | return BOOT_EFLASH; |
| 540 | } |
| 541 | |
| 542 | return 0; |
| 543 | } |
| 544 | |
Fabio Utzig | 1a1ec17 | 2019-08-09 10:21:43 -0300 | [diff] [blame] | 545 | /** |
| 546 | * Write trailer data; status bytes, swap_size, etc |
| 547 | * |
| 548 | * @returns 0 on success, != 0 on error. |
| 549 | */ |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 550 | static int |
Fabio Utzig | 1a1ec17 | 2019-08-09 10:21:43 -0300 | [diff] [blame] | 551 | boot_write_trailer(const struct flash_area *fap, uint32_t off, |
| 552 | const uint8_t *inbuf, uint8_t inlen) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 553 | { |
Fabio Utzig | 644b8d4 | 2017-04-20 07:56:05 -0300 | [diff] [blame] | 554 | uint8_t buf[BOOT_MAX_ALIGN]; |
David Brown | 9d72546 | 2017-01-23 15:50:58 -0700 | [diff] [blame] | 555 | uint8_t align; |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 556 | uint8_t erased_val; |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 557 | int rc; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 558 | |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 559 | align = flash_area_align(fap); |
Fabio Utzig | 1a1ec17 | 2019-08-09 10:21:43 -0300 | [diff] [blame] | 560 | if (inlen > BOOT_MAX_ALIGN || align > BOOT_MAX_ALIGN) { |
| 561 | return -1; |
| 562 | } |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 563 | erased_val = flash_area_erased_val(fap); |
Fabio Utzig | 1a1ec17 | 2019-08-09 10:21:43 -0300 | [diff] [blame] | 564 | if (align < inlen) { |
| 565 | align = inlen; |
| 566 | } |
| 567 | memcpy(buf, inbuf, inlen); |
| 568 | memset(&buf[inlen], erased_val, align - inlen); |
David Brown | 9d72546 | 2017-01-23 15:50:58 -0700 | [diff] [blame] | 569 | |
| 570 | rc = flash_area_write(fap, off, buf, align); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 571 | if (rc != 0) { |
| 572 | return BOOT_EFLASH; |
| 573 | } |
| 574 | |
| 575 | return 0; |
| 576 | } |
| 577 | |
Bohdan Kovalchuk | 7460ae5 | 2020-07-21 17:03:57 +0300 | [diff] [blame^] | 578 | #endif /* MCUBOOT_SWAP_USING_STATUS */ |
| 579 | |
Fabio Utzig | 1a1ec17 | 2019-08-09 10:21:43 -0300 | [diff] [blame] | 580 | static int |
| 581 | boot_write_trailer_flag(const struct flash_area *fap, uint32_t off, |
| 582 | uint8_t flag_val) |
| 583 | { |
| 584 | const uint8_t buf[1] = { flag_val }; |
| 585 | return boot_write_trailer(fap, off, buf, 1); |
| 586 | } |
| 587 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 588 | int |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 589 | boot_write_copy_done(const struct flash_area *fap) |
| 590 | { |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 591 | uint32_t off; |
| 592 | |
| 593 | off = boot_copy_done_off(fap); |
Ben McCrea | 4c0ee95 | 2019-08-28 09:13:24 -0700 | [diff] [blame] | 594 | 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] | 595 | fap->fa_id, (unsigned long)off, |
| 596 | (unsigned long)(fap->fa_off + off)); |
Fabio Utzig | 1a1ec17 | 2019-08-09 10:21:43 -0300 | [diff] [blame] | 597 | return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | int |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 601 | boot_write_image_ok(const struct flash_area *fap) |
| 602 | { |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 603 | uint32_t off; |
| 604 | |
| 605 | off = boot_image_ok_off(fap); |
Ben McCrea | 4c0ee95 | 2019-08-28 09:13:24 -0700 | [diff] [blame] | 606 | BOOT_LOG_DBG("writing image_ok; fa_id=%d off=0x%lx (0x%lx)", |
Marti Bolivar | 99ec383 | 2019-09-12 21:21:26 -0600 | [diff] [blame] | 607 | fap->fa_id, (unsigned long)off, |
| 608 | (unsigned long)(fap->fa_off + off)); |
Fabio Utzig | 1a1ec17 | 2019-08-09 10:21:43 -0300 | [diff] [blame] | 609 | return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET); |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | /** |
| 613 | * Writes the specified value to the `swap-type` field of an image trailer. |
| 614 | * This value is persisted so that the boot loader knows what swap operation to |
| 615 | * resume in case of an unexpected reset. |
| 616 | */ |
| 617 | int |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 618 | boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type, |
| 619 | uint8_t image_num) |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 620 | { |
| 621 | uint32_t off; |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 622 | uint8_t swap_info; |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 623 | |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 624 | BOOT_SET_SWAP_INFO(swap_info, image_num, swap_type); |
| 625 | off = boot_swap_info_off(fap); |
Ben McCrea | 4c0ee95 | 2019-08-28 09:13:24 -0700 | [diff] [blame] | 626 | BOOT_LOG_DBG("writing swap_info; fa_id=%d off=0x%lx (0x%lx), swap_type=0x%x" |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 627 | " image_num=0x%x", |
Marti Bolivar | 99ec383 | 2019-09-12 21:21:26 -0600 | [diff] [blame] | 628 | fap->fa_id, (unsigned long)off, |
| 629 | (unsigned long)(fap->fa_off + off), swap_type, image_num); |
Fabio Utzig | 1a1ec17 | 2019-08-09 10:21:43 -0300 | [diff] [blame] | 630 | return boot_write_trailer(fap, off, (const uint8_t *) &swap_info, 1); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | int |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 634 | boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size) |
| 635 | { |
| 636 | uint32_t off; |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 637 | |
| 638 | off = boot_swap_size_off(fap); |
Ben McCrea | 4c0ee95 | 2019-08-28 09:13:24 -0700 | [diff] [blame] | 639 | 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] | 640 | fap->fa_id, (unsigned long)off, |
| 641 | (unsigned long)fap->fa_off + off); |
Fabio Utzig | 1a1ec17 | 2019-08-09 10:21:43 -0300 | [diff] [blame] | 642 | return boot_write_trailer(fap, off, (const uint8_t *) &swap_size, 4); |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 643 | } |
| 644 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 645 | #ifdef MCUBOOT_ENC_IMAGES |
| 646 | int |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 647 | boot_write_enc_key(const struct flash_area *fap, uint8_t slot, |
| 648 | const struct boot_status *bs) |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 649 | { |
| 650 | uint32_t off; |
| 651 | int rc; |
| 652 | |
| 653 | off = boot_enc_key_off(fap, slot); |
Fabio Utzig | 5e6ea22 | 2019-12-10 09:49:59 -0300 | [diff] [blame] | 654 | BOOT_LOG_DBG("writing enc_key; fa_id=%d off=0x%lx (0x%lx)", |
| 655 | fap->fa_id, (unsigned long)off, |
| 656 | (unsigned long)fap->fa_off + off); |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 657 | #if MCUBOOT_SWAP_SAVE_ENCTLV |
| 658 | rc = flash_area_write(fap, off, bs->enctlv[slot], BOOT_ENC_TLV_ALIGN_SIZE); |
| 659 | #else |
| 660 | rc = flash_area_write(fap, off, bs->enckey[slot], BOOT_ENC_KEY_SIZE); |
| 661 | #endif |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 662 | if (rc != 0) { |
| 663 | return BOOT_EFLASH; |
| 664 | } |
| 665 | |
| 666 | return 0; |
| 667 | } |
| 668 | #endif |
| 669 | |
Bohdan Kovalchuk | 7460ae5 | 2020-07-21 17:03:57 +0300 | [diff] [blame^] | 670 | #define BOOT_LOG_SWAP_STATE(area, state) \ |
| 671 | BOOT_LOG_INF("%s: magic=%s, swap_type=0x%x, copy_done=0x%x, " \ |
| 672 | "image_ok=0x%x", \ |
| 673 | (area), \ |
| 674 | ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \ |
| 675 | (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \ |
| 676 | "bad"), \ |
| 677 | (state)->swap_type, \ |
| 678 | (state)->copy_done, \ |
| 679 | (state)->image_ok) |
| 680 | |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 681 | int |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 682 | boot_swap_type_multi(int image_index) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 683 | { |
| 684 | const struct boot_swap_table *table; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 685 | struct boot_swap_state primary_slot; |
| 686 | struct boot_swap_state secondary_slot; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 687 | int rc; |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 688 | size_t i; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 689 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 690 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index), |
| 691 | &primary_slot); |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 692 | if (rc) { |
| 693 | return BOOT_SWAP_TYPE_PANIC; |
| 694 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 695 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 696 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index), |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 697 | &secondary_slot); |
Bohdan Kovalchuk | 7460ae5 | 2020-07-21 17:03:57 +0300 | [diff] [blame^] | 698 | |
| 699 | BOOT_LOG_SWAP_STATE("boot_swap_type_multi: Primary image", &primary_slot); |
| 700 | BOOT_LOG_SWAP_STATE("boot_swap_type_multi: Secondary image", &secondary_slot); |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 701 | if (rc) { |
| 702 | return BOOT_SWAP_TYPE_PANIC; |
| 703 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 704 | |
| 705 | for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) { |
| 706 | table = boot_swap_tables + i; |
| 707 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 708 | if (boot_magic_compatible_check(table->magic_primary_slot, |
| 709 | primary_slot.magic) && |
| 710 | boot_magic_compatible_check(table->magic_secondary_slot, |
| 711 | secondary_slot.magic) && |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 712 | (table->image_ok_primary_slot == BOOT_FLAG_ANY || |
| 713 | table->image_ok_primary_slot == primary_slot.image_ok) && |
| 714 | (table->image_ok_secondary_slot == BOOT_FLAG_ANY || |
| 715 | table->image_ok_secondary_slot == secondary_slot.image_ok) && |
| 716 | (table->copy_done_primary_slot == BOOT_FLAG_ANY || |
| 717 | table->copy_done_primary_slot == primary_slot.copy_done)) { |
Fabio Utzig | 34e393e | 2017-05-22 11:07:07 -0400 | [diff] [blame] | 718 | BOOT_LOG_INF("Swap type: %s", |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 719 | table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" : |
| 720 | table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" : |
| 721 | table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" : |
| 722 | "BUG; can't happen"); |
Fabio Utzig | f0dbd42 | 2019-08-09 10:22:05 -0300 | [diff] [blame] | 723 | if (table->swap_type != BOOT_SWAP_TYPE_TEST && |
| 724 | table->swap_type != BOOT_SWAP_TYPE_PERM && |
| 725 | table->swap_type != BOOT_SWAP_TYPE_REVERT) { |
| 726 | return BOOT_SWAP_TYPE_PANIC; |
| 727 | } |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 728 | return table->swap_type; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 732 | BOOT_LOG_INF("Swap type: none"); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 733 | return BOOT_SWAP_TYPE_NONE; |
| 734 | } |
| 735 | |
Fabio Utzig | 75e9a59 | 2019-08-27 09:10:36 -0300 | [diff] [blame] | 736 | /* |
| 737 | * This function is not used by the bootloader itself, but its required API |
| 738 | * by external tooling like mcumgr. |
| 739 | */ |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 740 | int |
| 741 | boot_swap_type(void) |
| 742 | { |
| 743 | return boot_swap_type_multi(0); |
| 744 | } |
| 745 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 746 | /** |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 747 | * Marks the image in the secondary slot as pending. On the next reboot, |
| 748 | * the system will perform a one-time boot of the the secondary slot image. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 749 | * |
Christopher Collins | 7835c1e | 2016-12-21 10:10:51 -0800 | [diff] [blame] | 750 | * @param permanent Whether the image should be used permanently or |
| 751 | * only tested once: |
| 752 | * 0=run image once, then confirm or revert. |
| 753 | * 1=run image forever. |
| 754 | * |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 755 | * @return 0 on success; nonzero on failure. |
| 756 | */ |
| 757 | int |
Christopher Collins | 7835c1e | 2016-12-21 10:10:51 -0800 | [diff] [blame] | 758 | boot_set_pending(int permanent) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 759 | { |
| 760 | const struct flash_area *fap; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 761 | struct boot_swap_state state_secondary_slot; |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 762 | uint8_t swap_type; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 763 | int rc; |
| 764 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 765 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(0), |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 766 | &state_secondary_slot); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 767 | if (rc != 0) { |
| 768 | return rc; |
| 769 | } |
| 770 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 771 | switch (state_secondary_slot.magic) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 772 | case BOOT_MAGIC_GOOD: |
| 773 | /* Swap already scheduled. */ |
| 774 | return 0; |
| 775 | |
| 776 | case BOOT_MAGIC_UNSET: |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 777 | rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(0), &fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 778 | if (rc != 0) { |
| 779 | rc = BOOT_EFLASH; |
| 780 | } else { |
| 781 | rc = boot_write_magic(fap); |
| 782 | } |
| 783 | |
Christopher Collins | 7835c1e | 2016-12-21 10:10:51 -0800 | [diff] [blame] | 784 | if (rc == 0 && permanent) { |
| 785 | rc = boot_write_image_ok(fap); |
| 786 | } |
| 787 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 788 | if (rc == 0) { |
| 789 | if (permanent) { |
| 790 | swap_type = BOOT_SWAP_TYPE_PERM; |
| 791 | } else { |
| 792 | swap_type = BOOT_SWAP_TYPE_TEST; |
| 793 | } |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 794 | rc = boot_write_swap_info(fap, swap_type, 0); |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 795 | } |
| 796 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 797 | flash_area_close(fap); |
| 798 | return rc; |
| 799 | |
Christopher Collins | ae01f15 | 2019-01-30 09:56:37 -0800 | [diff] [blame] | 800 | case BOOT_MAGIC_BAD: |
| 801 | /* The image slot is corrupt. There is no way to recover, so erase the |
| 802 | * slot to allow future upgrades. |
| 803 | */ |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 804 | rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(0), &fap); |
Christopher Collins | ae01f15 | 2019-01-30 09:56:37 -0800 | [diff] [blame] | 805 | if (rc != 0) { |
| 806 | return BOOT_EFLASH; |
| 807 | } |
| 808 | |
| 809 | flash_area_erase(fap, 0, fap->fa_size); |
| 810 | flash_area_close(fap); |
| 811 | return BOOT_EBADIMAGE; |
| 812 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 813 | default: |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 814 | assert(0); |
Christopher Collins | ae01f15 | 2019-01-30 09:56:37 -0800 | [diff] [blame] | 815 | return BOOT_EBADIMAGE; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 816 | } |
| 817 | } |
| 818 | |
| 819 | /** |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 820 | * Marks the image in the primary slot as confirmed. The system will continue |
| 821 | * booting into the image in the primary slot until told to boot from a |
| 822 | * different slot. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 823 | * |
| 824 | * @return 0 on success; nonzero on failure. |
| 825 | */ |
| 826 | int |
| 827 | boot_set_confirmed(void) |
| 828 | { |
| 829 | const struct flash_area *fap; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 830 | struct boot_swap_state state_primary_slot; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 831 | int rc; |
| 832 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 833 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(0), |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 834 | &state_primary_slot); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 835 | if (rc != 0) { |
| 836 | return rc; |
| 837 | } |
| 838 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 839 | switch (state_primary_slot.magic) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 840 | case BOOT_MAGIC_GOOD: |
| 841 | /* Confirm needed; proceed. */ |
| 842 | break; |
| 843 | |
| 844 | case BOOT_MAGIC_UNSET: |
| 845 | /* Already confirmed. */ |
| 846 | return 0; |
| 847 | |
| 848 | case BOOT_MAGIC_BAD: |
| 849 | /* Unexpected state. */ |
| 850 | return BOOT_EBADVECT; |
| 851 | } |
| 852 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 853 | rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(0), &fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 854 | if (rc) { |
| 855 | rc = BOOT_EFLASH; |
| 856 | goto done; |
| 857 | } |
| 858 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 859 | if (state_primary_slot.copy_done == BOOT_FLAG_UNSET) { |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 860 | /* Swap never completed. This is unexpected. */ |
| 861 | rc = BOOT_EBADVECT; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 862 | goto done; |
| 863 | } |
| 864 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 865 | if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) { |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 866 | /* Already confirmed. */ |
| 867 | goto done; |
| 868 | } |
| 869 | |
| 870 | rc = boot_write_image_ok(fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 871 | |
| 872 | done: |
| 873 | flash_area_close(fap); |
| 874 | return rc; |
| 875 | } |