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 | |
| 20 | #include <assert.h> |
| 21 | #include <string.h> |
| 22 | #include <inttypes.h> |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 23 | #include <stddef.h> |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 24 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 25 | #include "sysflash/sysflash.h" |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 26 | #include "flash_map_backend/flash_map_backend.h" |
| 27 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 28 | #include "bootutil/image.h" |
| 29 | #include "bootutil/bootutil.h" |
| 30 | #include "bootutil_priv.h" |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 31 | #include "bootutil/bootutil_log.h" |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 32 | #ifdef MCUBOOT_ENC_IMAGES |
| 33 | #include "bootutil/enc_key.h" |
| 34 | #endif |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 35 | |
Emanuele Di Santo | 9f1933d | 2018-11-20 10:59:59 +0100 | [diff] [blame] | 36 | MCUBOOT_LOG_MODULE_DECLARE(mcuboot); |
| 37 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 38 | int boot_current_slot; |
| 39 | |
Fabio Utzig | 24a273d | 2017-04-20 08:21:31 -0300 | [diff] [blame] | 40 | const uint32_t boot_img_magic[] = { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 41 | 0xf395c277, |
| 42 | 0x7fefd260, |
| 43 | 0x0f505235, |
| 44 | 0x8079b62c, |
| 45 | }; |
| 46 | |
Hovland, Sigvart | 1d96f36 | 2018-09-25 13:23:42 +0200 | [diff] [blame] | 47 | #define BOOT_MAGIC_ARR_SZ \ |
| 48 | (sizeof boot_img_magic / sizeof boot_img_magic[0]) |
| 49 | |
Fabio Utzig | 24a273d | 2017-04-20 08:21:31 -0300 | [diff] [blame] | 50 | const uint32_t BOOT_MAGIC_SZ = sizeof boot_img_magic; |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 51 | const uint32_t BOOT_MAX_ALIGN = MAX_FLASH_ALIGN; |
Fabio Utzig | 24a273d | 2017-04-20 08:21:31 -0300 | [diff] [blame] | 52 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 53 | struct boot_swap_table { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 54 | uint8_t magic_primary_slot; |
| 55 | uint8_t magic_secondary_slot; |
| 56 | uint8_t image_ok_primary_slot; |
| 57 | uint8_t image_ok_secondary_slot; |
| 58 | uint8_t copy_done_primary_slot; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 59 | |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 60 | uint8_t swap_type; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | /** |
| 64 | * This set of tables maps image trailer contents to swap operation type. |
| 65 | * When searching for a match, these tables must be iterated sequentially. |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 66 | * |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 67 | * NOTE: the table order is very important. The settings in the secondary |
| 68 | * slot always are priority to the primary slot and should be located |
| 69 | * earlier in the table. |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 70 | * |
| 71 | * The table lists only states where there is action needs to be taken by |
| 72 | * the bootloader, as in starting/finishing a swap operation. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 73 | */ |
| 74 | static const struct boot_swap_table boot_swap_tables[] = { |
| 75 | { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 76 | .magic_primary_slot = BOOT_MAGIC_ANY, |
| 77 | .magic_secondary_slot = BOOT_MAGIC_GOOD, |
| 78 | .image_ok_primary_slot = BOOT_FLAG_ANY, |
| 79 | .image_ok_secondary_slot = BOOT_FLAG_UNSET, |
| 80 | .copy_done_primary_slot = BOOT_FLAG_ANY, |
| 81 | .swap_type = BOOT_SWAP_TYPE_TEST, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 82 | }, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 83 | { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 84 | .magic_primary_slot = BOOT_MAGIC_ANY, |
| 85 | .magic_secondary_slot = BOOT_MAGIC_GOOD, |
| 86 | .image_ok_primary_slot = BOOT_FLAG_ANY, |
| 87 | .image_ok_secondary_slot = BOOT_FLAG_SET, |
| 88 | .copy_done_primary_slot = BOOT_FLAG_ANY, |
| 89 | .swap_type = BOOT_SWAP_TYPE_PERM, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 90 | }, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 91 | { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 92 | .magic_primary_slot = BOOT_MAGIC_GOOD, |
| 93 | .magic_secondary_slot = BOOT_MAGIC_UNSET, |
| 94 | .image_ok_primary_slot = BOOT_FLAG_UNSET, |
| 95 | .image_ok_secondary_slot = BOOT_FLAG_ANY, |
| 96 | .copy_done_primary_slot = BOOT_FLAG_SET, |
| 97 | .swap_type = BOOT_SWAP_TYPE_REVERT, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 98 | }, |
| 99 | }; |
| 100 | |
| 101 | #define BOOT_SWAP_TABLES_COUNT \ |
| 102 | (sizeof boot_swap_tables / sizeof boot_swap_tables[0]) |
| 103 | |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 104 | static int |
Fabio Utzig | 178be54 | 2018-09-19 08:12:56 -0300 | [diff] [blame] | 105 | boot_magic_decode(const uint32_t *magic) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 106 | { |
Fabio Utzig | 24a273d | 2017-04-20 08:21:31 -0300 | [diff] [blame] | 107 | if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 108 | return BOOT_MAGIC_GOOD; |
| 109 | } |
Fabio Utzig | 178be54 | 2018-09-19 08:12:56 -0300 | [diff] [blame] | 110 | return BOOT_MAGIC_BAD; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 113 | static int |
Fabio Utzig | 178be54 | 2018-09-19 08:12:56 -0300 | [diff] [blame] | 114 | boot_flag_decode(uint8_t flag) |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 115 | { |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 116 | if (flag != BOOT_FLAG_SET) { |
| 117 | return BOOT_FLAG_BAD; |
| 118 | } |
| 119 | return BOOT_FLAG_SET; |
| 120 | } |
| 121 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 122 | uint32_t |
Christopher Collins | 2adef70 | 2019-05-22 14:37:31 -0700 | [diff] [blame] | 123 | boot_trailer_sz(uint8_t min_write_sz) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 124 | { |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 125 | return /* state for all sectors */ |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 126 | BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz + |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 127 | #ifdef MCUBOOT_ENC_IMAGES |
| 128 | /* encryption keys */ |
| 129 | BOOT_ENC_KEY_SIZE * 2 + |
| 130 | #endif |
Christopher Collins | 2adef70 | 2019-05-22 14:37:31 -0700 | [diff] [blame] | 131 | /* swap_type + copy_done + image_ok + swap_size */ |
| 132 | BOOT_MAX_ALIGN * 4 + |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 133 | BOOT_MAGIC_SZ; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | static uint32_t |
| 137 | boot_magic_off(const struct flash_area *fap) |
| 138 | { |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 139 | assert(offsetof(struct image_trailer, magic) == 16); |
| 140 | return fap->fa_size - BOOT_MAGIC_SZ; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Fabio Utzig | 4cee4f7 | 2017-05-22 10:59:57 -0400 | [diff] [blame] | 143 | int |
| 144 | boot_status_entries(const struct flash_area *fap) |
| 145 | { |
| 146 | switch (fap->fa_id) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 147 | case FLASH_AREA_IMAGE_PRIMARY: |
| 148 | case FLASH_AREA_IMAGE_SECONDARY: |
Fabio Utzig | 4cee4f7 | 2017-05-22 10:59:57 -0400 | [diff] [blame] | 149 | return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES; |
| 150 | case FLASH_AREA_IMAGE_SCRATCH: |
| 151 | return BOOT_STATUS_STATE_COUNT; |
| 152 | default: |
| 153 | return BOOT_EBADARGS; |
| 154 | } |
| 155 | } |
| 156 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 157 | uint32_t |
| 158 | boot_status_off(const struct flash_area *fap) |
| 159 | { |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 160 | uint32_t off_from_end; |
| 161 | uint8_t elem_sz; |
| 162 | |
| 163 | elem_sz = flash_area_align(fap); |
| 164 | |
Christopher Collins | 2adef70 | 2019-05-22 14:37:31 -0700 | [diff] [blame] | 165 | off_from_end = boot_trailer_sz(elem_sz); |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 166 | |
| 167 | assert(off_from_end <= fap->fa_size); |
| 168 | return fap->fa_size - off_from_end; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | static uint32_t |
| 172 | boot_copy_done_off(const struct flash_area *fap) |
| 173 | { |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 174 | assert(offsetof(struct image_trailer, copy_done) == 0); |
| 175 | return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | static uint32_t |
| 179 | boot_image_ok_off(const struct flash_area *fap) |
| 180 | { |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 181 | assert(offsetof(struct image_trailer, image_ok) == 8); |
| 182 | return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 185 | static uint32_t |
| 186 | boot_swap_size_off(const struct flash_area *fap) |
| 187 | { |
Christopher Collins | 2adef70 | 2019-05-22 14:37:31 -0700 | [diff] [blame] | 188 | return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 4; |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 189 | } |
| 190 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 191 | #ifdef MCUBOOT_ENC_IMAGES |
| 192 | static uint32_t |
| 193 | boot_enc_key_off(const struct flash_area *fap, uint8_t slot) |
| 194 | { |
Christopher Collins | 2adef70 | 2019-05-22 14:37:31 -0700 | [diff] [blame] | 195 | return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 4 - |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 196 | ((slot + 1) * BOOT_ENC_KEY_SIZE); |
| 197 | } |
| 198 | #endif |
| 199 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 200 | int |
| 201 | boot_read_swap_state(const struct flash_area *fap, |
| 202 | struct boot_swap_state *state) |
| 203 | { |
Hovland, Sigvart | 1d96f36 | 2018-09-25 13:23:42 +0200 | [diff] [blame] | 204 | uint32_t magic[BOOT_MAGIC_ARR_SZ]; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 205 | uint32_t off; |
| 206 | int rc; |
| 207 | |
| 208 | off = boot_magic_off(fap); |
Fabio Utzig | 178be54 | 2018-09-19 08:12:56 -0300 | [diff] [blame] | 209 | rc = flash_area_read_is_empty(fap, off, magic, BOOT_MAGIC_SZ); |
| 210 | if (rc < 0) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 211 | return BOOT_EFLASH; |
| 212 | } |
Fabio Utzig | 178be54 | 2018-09-19 08:12:56 -0300 | [diff] [blame] | 213 | if (rc == 1) { |
| 214 | state->magic = BOOT_MAGIC_UNSET; |
| 215 | } else { |
| 216 | state->magic = boot_magic_decode(magic); |
| 217 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 218 | |
Christopher Collins | 2adef70 | 2019-05-22 14:37:31 -0700 | [diff] [blame] | 219 | off = boot_copy_done_off(fap); |
| 220 | rc = flash_area_read_is_empty(fap, off, &state->copy_done, |
| 221 | sizeof state->copy_done); |
| 222 | if (rc < 0) { |
| 223 | return BOOT_EFLASH; |
| 224 | } |
| 225 | if (rc == 1) { |
| 226 | state->copy_done = BOOT_FLAG_UNSET; |
| 227 | } else { |
| 228 | state->copy_done = boot_flag_decode(state->copy_done); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | off = boot_image_ok_off(fap); |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 232 | rc = flash_area_read_is_empty(fap, off, &state->image_ok, |
| 233 | sizeof state->image_ok); |
Fabio Utzig | 178be54 | 2018-09-19 08:12:56 -0300 | [diff] [blame] | 234 | if (rc < 0) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 235 | return BOOT_EFLASH; |
| 236 | } |
Fabio Utzig | 178be54 | 2018-09-19 08:12:56 -0300 | [diff] [blame] | 237 | if (rc == 1) { |
| 238 | state->image_ok = BOOT_FLAG_UNSET; |
| 239 | } else { |
| 240 | state->image_ok = boot_flag_decode(state->image_ok); |
| 241 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Reads the image trailer from the scratch area. |
| 248 | */ |
| 249 | int |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 250 | 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] | 251 | { |
| 252 | const struct flash_area *fap; |
| 253 | int rc; |
| 254 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 255 | switch (flash_area_id) { |
| 256 | case FLASH_AREA_IMAGE_SCRATCH: |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 257 | case FLASH_AREA_IMAGE_PRIMARY: |
| 258 | case FLASH_AREA_IMAGE_SECONDARY: |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 259 | rc = flash_area_open(flash_area_id, &fap); |
| 260 | if (rc != 0) { |
| 261 | return BOOT_EFLASH; |
| 262 | } |
| 263 | break; |
| 264 | default: |
Fabio Utzig | 856f783 | 2017-05-22 11:04:44 -0400 | [diff] [blame] | 265 | return BOOT_EBADARGS; |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | rc = boot_read_swap_state(fap, state); |
Fabio Utzig | acfba2e | 2017-05-22 11:06:29 -0400 | [diff] [blame] | 269 | flash_area_close(fap); |
| 270 | return rc; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | int |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 274 | boot_read_swap_size(uint32_t *swap_size) |
| 275 | { |
Hovland, Sigvart | 1d96f36 | 2018-09-25 13:23:42 +0200 | [diff] [blame] | 276 | uint32_t magic[BOOT_MAGIC_ARR_SZ]; |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 277 | uint32_t off; |
| 278 | const struct flash_area *fap; |
| 279 | int rc; |
| 280 | |
| 281 | /* |
| 282 | * In the middle a swap, tries to locate the saved swap size. Looks |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 283 | * for a valid magic, first on the primary slot, then on scratch. |
| 284 | * Both "slots" can end up being temporary storage for a swap and it |
| 285 | * is assumed that if magic is valid then swap size is too, because |
| 286 | * magic is always written in the last step. |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 287 | */ |
| 288 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 289 | rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap); |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 290 | if (rc != 0) { |
| 291 | return BOOT_EFLASH; |
| 292 | } |
| 293 | |
| 294 | off = boot_magic_off(fap); |
| 295 | rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ); |
| 296 | if (rc != 0) { |
| 297 | rc = BOOT_EFLASH; |
| 298 | goto out; |
| 299 | } |
| 300 | |
| 301 | if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) != 0) { |
| 302 | /* |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 303 | * If the primary slot's magic is not valid, try scratch... |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 304 | */ |
| 305 | |
| 306 | flash_area_close(fap); |
| 307 | |
| 308 | rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap); |
| 309 | if (rc != 0) { |
| 310 | return BOOT_EFLASH; |
| 311 | } |
| 312 | |
| 313 | off = boot_magic_off(fap); |
| 314 | rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ); |
| 315 | if (rc != 0) { |
| 316 | rc = BOOT_EFLASH; |
| 317 | goto out; |
| 318 | } |
| 319 | |
| 320 | assert(memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0); |
| 321 | } |
| 322 | |
| 323 | off = boot_swap_size_off(fap); |
| 324 | rc = flash_area_read(fap, off, swap_size, sizeof *swap_size); |
| 325 | if (rc != 0) { |
| 326 | rc = BOOT_EFLASH; |
| 327 | } |
| 328 | |
| 329 | out: |
| 330 | flash_area_close(fap); |
| 331 | return rc; |
| 332 | } |
| 333 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 334 | #ifdef MCUBOOT_ENC_IMAGES |
| 335 | int |
| 336 | boot_read_enc_key(uint8_t slot, uint8_t *enckey) |
| 337 | { |
| 338 | uint32_t magic[BOOT_MAGIC_SZ]; |
| 339 | uint32_t off; |
| 340 | const struct flash_area *fap; |
| 341 | int rc; |
| 342 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 343 | rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 344 | if (rc != 0) { |
| 345 | return BOOT_EFLASH; |
| 346 | } |
| 347 | |
| 348 | off = boot_magic_off(fap); |
| 349 | rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ); |
| 350 | if (rc != 0) { |
| 351 | rc = BOOT_EFLASH; |
| 352 | goto out; |
| 353 | } |
| 354 | |
| 355 | if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) != 0) { |
| 356 | /* |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 357 | * If the primary slot's magic is not valid, try scratch... |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 358 | */ |
| 359 | |
| 360 | flash_area_close(fap); |
| 361 | |
| 362 | rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap); |
| 363 | if (rc != 0) { |
| 364 | return BOOT_EFLASH; |
| 365 | } |
| 366 | |
| 367 | off = boot_magic_off(fap); |
| 368 | rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ); |
| 369 | if (rc != 0) { |
| 370 | rc = BOOT_EFLASH; |
| 371 | goto out; |
| 372 | } |
| 373 | |
| 374 | assert(memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0); |
| 375 | } |
| 376 | |
| 377 | off = boot_enc_key_off(fap, slot); |
| 378 | rc = flash_area_read(fap, off, enckey, BOOT_ENC_KEY_SIZE); |
| 379 | if (rc != 0) { |
| 380 | rc = BOOT_EFLASH; |
| 381 | } |
| 382 | |
| 383 | out: |
| 384 | flash_area_close(fap); |
| 385 | return rc; |
| 386 | } |
| 387 | #endif |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 388 | |
| 389 | int |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 390 | boot_write_magic(const struct flash_area *fap) |
| 391 | { |
| 392 | uint32_t off; |
| 393 | int rc; |
| 394 | |
| 395 | off = boot_magic_off(fap); |
| 396 | |
Christopher Collins | 2c88e69 | 2019-05-22 15:10:14 -0700 | [diff] [blame^] | 397 | BOOT_LOG_DBG("writing magic; fa_id=%d off=0x%x (0x%x)", |
| 398 | fap->fa_id, off, fap->fa_off + off); |
Fabio Utzig | 24a273d | 2017-04-20 08:21:31 -0300 | [diff] [blame] | 399 | rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 400 | if (rc != 0) { |
| 401 | return BOOT_EFLASH; |
| 402 | } |
| 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 407 | static int |
| 408 | boot_write_flag(int flag, const struct flash_area *fap) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 409 | { |
| 410 | uint32_t off; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 411 | int rc; |
Fabio Utzig | 644b8d4 | 2017-04-20 07:56:05 -0300 | [diff] [blame] | 412 | uint8_t buf[BOOT_MAX_ALIGN]; |
David Brown | 9d72546 | 2017-01-23 15:50:58 -0700 | [diff] [blame] | 413 | uint8_t align; |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 414 | uint8_t erased_val; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 415 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 416 | switch (flag) { |
| 417 | case BOOT_FLAG_COPY_DONE: |
| 418 | off = boot_copy_done_off(fap); |
Christopher Collins | 2c88e69 | 2019-05-22 15:10:14 -0700 | [diff] [blame^] | 419 | BOOT_LOG_DBG("writing copy_done; fa_id=%d off=0x%x (0x%x)", |
| 420 | fap->fa_id, off, fap->fa_off + off); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 421 | break; |
| 422 | case BOOT_FLAG_IMAGE_OK: |
| 423 | off = boot_image_ok_off(fap); |
Christopher Collins | 2c88e69 | 2019-05-22 15:10:14 -0700 | [diff] [blame^] | 424 | BOOT_LOG_DBG("writing image_ok; fa_id=%d off=0x%x (0x%x)", |
| 425 | fap->fa_id, off, fap->fa_off + off); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 426 | break; |
| 427 | default: |
Fabio Utzig | 856f783 | 2017-05-22 11:04:44 -0400 | [diff] [blame] | 428 | return BOOT_EBADARGS; |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 429 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 430 | |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 431 | align = flash_area_align(fap); |
Fabio Utzig | 644b8d4 | 2017-04-20 07:56:05 -0300 | [diff] [blame] | 432 | assert(align <= BOOT_MAX_ALIGN); |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 433 | erased_val = flash_area_erased_val(fap); |
| 434 | memset(buf, erased_val, BOOT_MAX_ALIGN); |
Fabio Utzig | de8a38a | 2017-05-23 11:15:01 -0400 | [diff] [blame] | 435 | buf[0] = BOOT_FLAG_SET; |
David Brown | 9d72546 | 2017-01-23 15:50:58 -0700 | [diff] [blame] | 436 | |
| 437 | rc = flash_area_write(fap, off, buf, align); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 438 | if (rc != 0) { |
| 439 | return BOOT_EFLASH; |
| 440 | } |
| 441 | |
| 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | int |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 446 | boot_write_copy_done(const struct flash_area *fap) |
| 447 | { |
| 448 | return boot_write_flag(BOOT_FLAG_COPY_DONE, fap); |
| 449 | } |
| 450 | |
| 451 | int |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 452 | boot_write_image_ok(const struct flash_area *fap) |
| 453 | { |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 454 | return boot_write_flag(BOOT_FLAG_IMAGE_OK, fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | int |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 458 | boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size) |
| 459 | { |
| 460 | uint32_t off; |
| 461 | int rc; |
| 462 | uint8_t buf[BOOT_MAX_ALIGN]; |
| 463 | uint8_t align; |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 464 | uint8_t erased_val; |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 465 | |
| 466 | off = boot_swap_size_off(fap); |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 467 | align = flash_area_align(fap); |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 468 | assert(align <= BOOT_MAX_ALIGN); |
| 469 | if (align < sizeof swap_size) { |
| 470 | align = sizeof swap_size; |
| 471 | } |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 472 | erased_val = flash_area_erased_val(fap); |
| 473 | memset(buf, erased_val, BOOT_MAX_ALIGN); |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 474 | memcpy(buf, (uint8_t *)&swap_size, sizeof swap_size); |
| 475 | |
Christopher Collins | 2c88e69 | 2019-05-22 15:10:14 -0700 | [diff] [blame^] | 476 | BOOT_LOG_DBG("writing swap_size; fa_id=%d off=0x%x (0x%x)", |
| 477 | fap->fa_id, off, fap->fa_off + off); |
| 478 | |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 479 | rc = flash_area_write(fap, off, buf, align); |
| 480 | if (rc != 0) { |
| 481 | return BOOT_EFLASH; |
| 482 | } |
| 483 | |
| 484 | return 0; |
| 485 | } |
| 486 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 487 | #ifdef MCUBOOT_ENC_IMAGES |
| 488 | int |
| 489 | boot_write_enc_key(const struct flash_area *fap, uint8_t slot, const uint8_t *enckey) |
| 490 | { |
| 491 | uint32_t off; |
| 492 | int rc; |
| 493 | |
| 494 | off = boot_enc_key_off(fap, slot); |
| 495 | rc = flash_area_write(fap, off, enckey, BOOT_ENC_KEY_SIZE); |
| 496 | if (rc != 0) { |
| 497 | return BOOT_EFLASH; |
| 498 | } |
| 499 | |
| 500 | return 0; |
| 501 | } |
| 502 | #endif |
| 503 | |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 504 | int |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 505 | boot_swap_type(void) |
| 506 | { |
| 507 | const struct boot_swap_table *table; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 508 | struct boot_swap_state primary_slot; |
| 509 | struct boot_swap_state secondary_slot; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 510 | int rc; |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 511 | size_t i; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 512 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 513 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY, &primary_slot); |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 514 | if (rc) { |
| 515 | return BOOT_SWAP_TYPE_PANIC; |
| 516 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 517 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 518 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY, |
| 519 | &secondary_slot); |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 520 | if (rc) { |
| 521 | return BOOT_SWAP_TYPE_PANIC; |
| 522 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 523 | |
| 524 | for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) { |
| 525 | table = boot_swap_tables + i; |
| 526 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 527 | if ((table->magic_primary_slot == BOOT_MAGIC_ANY || |
| 528 | table->magic_primary_slot == primary_slot.magic) && |
| 529 | (table->magic_secondary_slot == BOOT_MAGIC_ANY || |
| 530 | table->magic_secondary_slot == secondary_slot.magic) && |
| 531 | (table->image_ok_primary_slot == BOOT_FLAG_ANY || |
| 532 | table->image_ok_primary_slot == primary_slot.image_ok) && |
| 533 | (table->image_ok_secondary_slot == BOOT_FLAG_ANY || |
| 534 | table->image_ok_secondary_slot == secondary_slot.image_ok) && |
| 535 | (table->copy_done_primary_slot == BOOT_FLAG_ANY || |
| 536 | table->copy_done_primary_slot == primary_slot.copy_done)) { |
Fabio Utzig | 34e393e | 2017-05-22 11:07:07 -0400 | [diff] [blame] | 537 | BOOT_LOG_INF("Swap type: %s", |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 538 | table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" : |
| 539 | table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" : |
| 540 | table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" : |
| 541 | "BUG; can't happen"); |
| 542 | assert(table->swap_type == BOOT_SWAP_TYPE_TEST || |
| 543 | table->swap_type == BOOT_SWAP_TYPE_PERM || |
| 544 | table->swap_type == BOOT_SWAP_TYPE_REVERT); |
| 545 | return table->swap_type; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 549 | BOOT_LOG_INF("Swap type: none"); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 550 | return BOOT_SWAP_TYPE_NONE; |
| 551 | } |
| 552 | |
| 553 | /** |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 554 | * Marks the image in the secondary slot as pending. On the next reboot, |
| 555 | * 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] | 556 | * |
Christopher Collins | 7835c1e | 2016-12-21 10:10:51 -0800 | [diff] [blame] | 557 | * @param permanent Whether the image should be used permanently or |
| 558 | * only tested once: |
| 559 | * 0=run image once, then confirm or revert. |
| 560 | * 1=run image forever. |
| 561 | * |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 562 | * @return 0 on success; nonzero on failure. |
| 563 | */ |
| 564 | int |
Christopher Collins | 7835c1e | 2016-12-21 10:10:51 -0800 | [diff] [blame] | 565 | boot_set_pending(int permanent) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 566 | { |
| 567 | const struct flash_area *fap; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 568 | struct boot_swap_state state_secondary_slot; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 569 | int rc; |
| 570 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 571 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY, |
| 572 | &state_secondary_slot); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 573 | if (rc != 0) { |
| 574 | return rc; |
| 575 | } |
| 576 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 577 | switch (state_secondary_slot.magic) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 578 | case BOOT_MAGIC_GOOD: |
| 579 | /* Swap already scheduled. */ |
| 580 | return 0; |
| 581 | |
| 582 | case BOOT_MAGIC_UNSET: |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 583 | rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 584 | if (rc != 0) { |
| 585 | rc = BOOT_EFLASH; |
| 586 | } else { |
| 587 | rc = boot_write_magic(fap); |
| 588 | } |
| 589 | |
Christopher Collins | 7835c1e | 2016-12-21 10:10:51 -0800 | [diff] [blame] | 590 | if (rc == 0 && permanent) { |
| 591 | rc = boot_write_image_ok(fap); |
| 592 | } |
| 593 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 594 | flash_area_close(fap); |
| 595 | return rc; |
| 596 | |
Christopher Collins | ae01f15 | 2019-01-30 09:56:37 -0800 | [diff] [blame] | 597 | case BOOT_MAGIC_BAD: |
| 598 | /* The image slot is corrupt. There is no way to recover, so erase the |
| 599 | * slot to allow future upgrades. |
| 600 | */ |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 601 | rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap); |
Christopher Collins | ae01f15 | 2019-01-30 09:56:37 -0800 | [diff] [blame] | 602 | if (rc != 0) { |
| 603 | return BOOT_EFLASH; |
| 604 | } |
| 605 | |
| 606 | flash_area_erase(fap, 0, fap->fa_size); |
| 607 | flash_area_close(fap); |
| 608 | return BOOT_EBADIMAGE; |
| 609 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 610 | default: |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 611 | assert(0); |
Christopher Collins | ae01f15 | 2019-01-30 09:56:37 -0800 | [diff] [blame] | 612 | return BOOT_EBADIMAGE; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | |
| 616 | /** |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 617 | * Marks the image in the primary slot as confirmed. The system will continue |
| 618 | * booting into the image in the primary slot until told to boot from a |
| 619 | * different slot. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 620 | * |
| 621 | * @return 0 on success; nonzero on failure. |
| 622 | */ |
| 623 | int |
| 624 | boot_set_confirmed(void) |
| 625 | { |
| 626 | const struct flash_area *fap; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 627 | struct boot_swap_state state_primary_slot; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 628 | int rc; |
| 629 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 630 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY, |
| 631 | &state_primary_slot); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 632 | if (rc != 0) { |
| 633 | return rc; |
| 634 | } |
| 635 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 636 | switch (state_primary_slot.magic) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 637 | case BOOT_MAGIC_GOOD: |
| 638 | /* Confirm needed; proceed. */ |
| 639 | break; |
| 640 | |
| 641 | case BOOT_MAGIC_UNSET: |
| 642 | /* Already confirmed. */ |
| 643 | return 0; |
| 644 | |
| 645 | case BOOT_MAGIC_BAD: |
| 646 | /* Unexpected state. */ |
| 647 | return BOOT_EBADVECT; |
| 648 | } |
| 649 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 650 | rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 651 | if (rc) { |
| 652 | rc = BOOT_EFLASH; |
| 653 | goto done; |
| 654 | } |
| 655 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 656 | if (state_primary_slot.copy_done == BOOT_FLAG_UNSET) { |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 657 | /* Swap never completed. This is unexpected. */ |
| 658 | rc = BOOT_EBADVECT; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 659 | goto done; |
| 660 | } |
| 661 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 662 | if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) { |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 663 | /* Already confirmed. */ |
| 664 | goto done; |
| 665 | } |
| 666 | |
| 667 | rc = boot_write_image_ok(fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 668 | |
| 669 | done: |
| 670 | flash_area_close(fap); |
| 671 | return rc; |
| 672 | } |