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