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