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