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