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