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> |
| 23 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 24 | #include "sysflash/sysflash.h" |
| 25 | #include "hal/hal_bsp.h" |
| 26 | #include "hal/hal_flash.h" |
| 27 | #include "flash_map/flash_map.h" |
| 28 | #include "os/os.h" |
| 29 | #include "bootutil/image.h" |
| 30 | #include "bootutil/bootutil.h" |
| 31 | #include "bootutil_priv.h" |
| 32 | |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 33 | #define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO |
| 34 | #include "bootutil/bootutil_log.h" |
| 35 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 36 | int boot_current_slot; |
| 37 | |
Fabio Utzig | 24a273d | 2017-04-20 08:21:31 -0300 | [diff] [blame] | 38 | const uint32_t boot_img_magic[] = { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 39 | 0xf395c277, |
| 40 | 0x7fefd260, |
| 41 | 0x0f505235, |
| 42 | 0x8079b62c, |
| 43 | }; |
| 44 | |
Fabio Utzig | 24a273d | 2017-04-20 08:21:31 -0300 | [diff] [blame] | 45 | const uint32_t BOOT_MAGIC_SZ = sizeof boot_img_magic; |
| 46 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 47 | struct boot_swap_table { |
| 48 | /** * For each field, a value of 0 means "any". */ |
| 49 | uint8_t bsw_magic_slot0; |
| 50 | uint8_t bsw_magic_slot1; |
| 51 | uint8_t bsw_image_ok_slot0; |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 52 | uint8_t bsw_image_ok_slot1; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 53 | |
| 54 | uint8_t bsw_swap_type; |
| 55 | }; |
| 56 | |
| 57 | /** |
| 58 | * This set of tables maps image trailer contents to swap operation type. |
| 59 | * When searching for a match, these tables must be iterated sequentially. |
| 60 | */ |
| 61 | static const struct boot_swap_table boot_swap_tables[] = { |
| 62 | { |
| 63 | /* | slot-0 | slot-1 | |
| 64 | *----------+------------+------------| |
| 65 | * magic | Unset | Unset | |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 66 | * image-ok | Any | Any | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 67 | * ---------+------------+------------' |
| 68 | * swap: none | |
| 69 | * -----------------------------------' |
| 70 | */ |
| 71 | .bsw_magic_slot0 = BOOT_MAGIC_UNSET, |
| 72 | .bsw_magic_slot1 = BOOT_MAGIC_UNSET, |
| 73 | .bsw_image_ok_slot0 = 0, |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 74 | .bsw_image_ok_slot1 = 0, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 75 | .bsw_swap_type = BOOT_SWAP_TYPE_NONE, |
| 76 | }, |
| 77 | |
| 78 | { |
| 79 | /* | slot-0 | slot-1 | |
| 80 | *----------+------------+------------| |
| 81 | * magic | Any | Good | |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 82 | * image-ok | Any | Unset | |
| 83 | * ---------+------------+------------` |
| 84 | * swap: test | |
| 85 | * -----------------------------------' |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 86 | */ |
| 87 | .bsw_magic_slot0 = 0, |
| 88 | .bsw_magic_slot1 = BOOT_MAGIC_GOOD, |
| 89 | .bsw_image_ok_slot0 = 0, |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 90 | .bsw_image_ok_slot1 = 0xff, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 91 | .bsw_swap_type = BOOT_SWAP_TYPE_TEST, |
| 92 | }, |
| 93 | |
| 94 | { |
| 95 | /* | slot-0 | slot-1 | |
| 96 | *----------+------------+------------| |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 97 | * magic | Any | Good | |
| 98 | * image-ok | Any | 0x01 | |
| 99 | * ---------+------------+------------` |
| 100 | * swap: permanent | |
| 101 | * -----------------------------------' |
| 102 | */ |
| 103 | .bsw_magic_slot0 = 0, |
| 104 | .bsw_magic_slot1 = BOOT_MAGIC_GOOD, |
| 105 | .bsw_image_ok_slot0 = 0, |
| 106 | .bsw_image_ok_slot1 = 0x01, |
| 107 | .bsw_swap_type = BOOT_SWAP_TYPE_PERM, |
| 108 | }, |
| 109 | |
| 110 | { |
| 111 | /* | slot-0 | slot-1 | |
| 112 | *----------+------------+------------| |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 113 | * magic | Good | Unset | |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 114 | * image-ok | 0xff | Any | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 115 | * ---------+------------+------------' |
| 116 | * swap: revert (test image running) | |
| 117 | * -----------------------------------' |
| 118 | */ |
| 119 | .bsw_magic_slot0 = BOOT_MAGIC_GOOD, |
| 120 | .bsw_magic_slot1 = BOOT_MAGIC_UNSET, |
| 121 | .bsw_image_ok_slot0 = 0xff, |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 122 | .bsw_image_ok_slot1 = 0, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 123 | .bsw_swap_type = BOOT_SWAP_TYPE_REVERT, |
| 124 | }, |
| 125 | |
| 126 | { |
| 127 | /* | slot-0 | slot-1 | |
| 128 | *----------+------------+------------| |
| 129 | * magic | Good | Unset | |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 130 | * image-ok | 0x01 | Any | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 131 | * ---------+------------+------------' |
| 132 | * swap: none (confirmed test image) | |
| 133 | * -----------------------------------' |
| 134 | */ |
| 135 | .bsw_magic_slot0 = BOOT_MAGIC_GOOD, |
| 136 | .bsw_magic_slot1 = BOOT_MAGIC_UNSET, |
| 137 | .bsw_image_ok_slot0 = 0x01, |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 138 | .bsw_image_ok_slot1 = 0, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 139 | .bsw_swap_type = BOOT_SWAP_TYPE_NONE, |
| 140 | }, |
| 141 | }; |
| 142 | |
| 143 | #define BOOT_SWAP_TABLES_COUNT \ |
| 144 | (sizeof boot_swap_tables / sizeof boot_swap_tables[0]) |
| 145 | |
| 146 | int |
| 147 | boot_magic_code(const uint32_t *magic) |
| 148 | { |
| 149 | int i; |
| 150 | |
Fabio Utzig | 24a273d | 2017-04-20 08:21:31 -0300 | [diff] [blame] | 151 | if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 152 | return BOOT_MAGIC_GOOD; |
| 153 | } |
| 154 | |
Fabio Utzig | 24a273d | 2017-04-20 08:21:31 -0300 | [diff] [blame] | 155 | for (i = 0; i < BOOT_MAGIC_SZ / sizeof *magic; i++) { |
| 156 | if (magic[i] != 0xffffffff) { |
| 157 | return BOOT_MAGIC_BAD; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
Fabio Utzig | 24a273d | 2017-04-20 08:21:31 -0300 | [diff] [blame] | 161 | return BOOT_MAGIC_UNSET; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | uint32_t |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 165 | boot_slots_trailer_sz(uint8_t min_write_sz) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 166 | { |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 167 | return BOOT_MAGIC_SZ + |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 168 | /* state for all sectors */ |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 169 | BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz + |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 170 | /* copy_done + image_ok */ |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 171 | min_write_sz * 2; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 174 | static uint32_t |
| 175 | boot_scratch_trailer_sz(uint8_t min_write_sz) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 176 | { |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 177 | return BOOT_MAGIC_SZ + /* magic */ |
| 178 | BOOT_STATUS_STATE_COUNT * min_write_sz + /* state for one sector */ |
| 179 | min_write_sz; /* image_ok */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static uint32_t |
| 183 | boot_magic_off(const struct flash_area *fap) |
| 184 | { |
| 185 | uint32_t off_from_end; |
| 186 | uint8_t elem_sz; |
| 187 | |
| 188 | elem_sz = flash_area_align(fap); |
| 189 | |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 190 | if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) { |
| 191 | off_from_end = boot_scratch_trailer_sz(elem_sz); |
| 192 | } else { |
| 193 | off_from_end = boot_slots_trailer_sz(elem_sz); |
| 194 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 195 | |
| 196 | assert(off_from_end <= fap->fa_size); |
| 197 | return fap->fa_size - off_from_end; |
| 198 | } |
| 199 | |
Fabio Utzig | 4cee4f7 | 2017-05-22 10:59:57 -0400 | [diff] [blame] | 200 | int |
| 201 | boot_status_entries(const struct flash_area *fap) |
| 202 | { |
| 203 | switch (fap->fa_id) { |
| 204 | case FLASH_AREA_IMAGE_0: |
| 205 | case FLASH_AREA_IMAGE_1: |
| 206 | return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES; |
| 207 | case FLASH_AREA_IMAGE_SCRATCH: |
| 208 | return BOOT_STATUS_STATE_COUNT; |
| 209 | default: |
| 210 | return BOOT_EBADARGS; |
| 211 | } |
| 212 | } |
| 213 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 214 | uint32_t |
| 215 | boot_status_off(const struct flash_area *fap) |
| 216 | { |
Fabio Utzig | 24a273d | 2017-04-20 08:21:31 -0300 | [diff] [blame] | 217 | return boot_magic_off(fap) + BOOT_MAGIC_SZ; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | static uint32_t |
| 221 | boot_copy_done_off(const struct flash_area *fap) |
| 222 | { |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 223 | assert(fap->fa_id != FLASH_AREA_IMAGE_SCRATCH); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 224 | return fap->fa_size - flash_area_align(fap) * 2; |
| 225 | } |
| 226 | |
| 227 | static uint32_t |
| 228 | boot_image_ok_off(const struct flash_area *fap) |
| 229 | { |
| 230 | return fap->fa_size - flash_area_align(fap); |
| 231 | } |
| 232 | |
| 233 | int |
| 234 | boot_read_swap_state(const struct flash_area *fap, |
| 235 | struct boot_swap_state *state) |
| 236 | { |
Fabio Utzig | 24a273d | 2017-04-20 08:21:31 -0300 | [diff] [blame] | 237 | uint32_t magic[BOOT_MAGIC_SZ]; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 238 | uint32_t off; |
| 239 | int rc; |
| 240 | |
| 241 | off = boot_magic_off(fap); |
Fabio Utzig | 24a273d | 2017-04-20 08:21:31 -0300 | [diff] [blame] | 242 | rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 243 | if (rc != 0) { |
| 244 | return BOOT_EFLASH; |
| 245 | } |
| 246 | state->magic = boot_magic_code(magic); |
| 247 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 248 | if (fap->fa_id != FLASH_AREA_IMAGE_SCRATCH) { |
| 249 | off = boot_copy_done_off(fap); |
| 250 | rc = flash_area_read(fap, off, &state->copy_done, 1); |
| 251 | if (rc != 0) { |
| 252 | return BOOT_EFLASH; |
| 253 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | off = boot_image_ok_off(fap); |
| 257 | rc = flash_area_read(fap, off, &state->image_ok, 1); |
| 258 | if (rc != 0) { |
| 259 | return BOOT_EFLASH; |
| 260 | } |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Reads the image trailer from the scratch area. |
| 267 | */ |
| 268 | int |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 269 | 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] | 270 | { |
| 271 | const struct flash_area *fap; |
| 272 | int rc; |
| 273 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 274 | switch (flash_area_id) { |
| 275 | case FLASH_AREA_IMAGE_SCRATCH: |
| 276 | case FLASH_AREA_IMAGE_0: |
| 277 | case FLASH_AREA_IMAGE_1: |
| 278 | rc = flash_area_open(flash_area_id, &fap); |
| 279 | if (rc != 0) { |
| 280 | return BOOT_EFLASH; |
| 281 | } |
| 282 | break; |
| 283 | default: |
Fabio Utzig | 856f783 | 2017-05-22 11:04:44 -0400 | [diff] [blame] | 284 | return BOOT_EBADARGS; |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | rc = boot_read_swap_state(fap, state); |
Fabio Utzig | acfba2e | 2017-05-22 11:06:29 -0400 | [diff] [blame] | 288 | flash_area_close(fap); |
| 289 | return rc; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | int |
| 293 | boot_write_magic(const struct flash_area *fap) |
| 294 | { |
| 295 | uint32_t off; |
| 296 | int rc; |
| 297 | |
| 298 | off = boot_magic_off(fap); |
| 299 | |
Fabio Utzig | 24a273d | 2017-04-20 08:21:31 -0300 | [diff] [blame] | 300 | rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 301 | if (rc != 0) { |
| 302 | return BOOT_EFLASH; |
| 303 | } |
| 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 308 | static int |
| 309 | boot_write_flag(int flag, const struct flash_area *fap) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 310 | { |
| 311 | uint32_t off; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 312 | int rc; |
Fabio Utzig | 644b8d4 | 2017-04-20 07:56:05 -0300 | [diff] [blame] | 313 | uint8_t buf[BOOT_MAX_ALIGN]; |
David Brown | 9d72546 | 2017-01-23 15:50:58 -0700 | [diff] [blame] | 314 | uint8_t align; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 315 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 316 | switch (flag) { |
| 317 | case BOOT_FLAG_COPY_DONE: |
| 318 | off = boot_copy_done_off(fap); |
| 319 | break; |
| 320 | case BOOT_FLAG_IMAGE_OK: |
| 321 | off = boot_image_ok_off(fap); |
| 322 | break; |
| 323 | default: |
Fabio Utzig | 856f783 | 2017-05-22 11:04:44 -0400 | [diff] [blame] | 324 | return BOOT_EBADARGS; |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 325 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 326 | |
David Brown | 9d72546 | 2017-01-23 15:50:58 -0700 | [diff] [blame] | 327 | align = hal_flash_align(fap->fa_device_id); |
Fabio Utzig | 644b8d4 | 2017-04-20 07:56:05 -0300 | [diff] [blame] | 328 | assert(align <= BOOT_MAX_ALIGN); |
| 329 | memset(buf, 0xFF, BOOT_MAX_ALIGN); |
Fabio Utzig | de8a38a | 2017-05-23 11:15:01 -0400 | [diff] [blame] | 330 | buf[0] = BOOT_FLAG_SET; |
David Brown | 9d72546 | 2017-01-23 15:50:58 -0700 | [diff] [blame] | 331 | |
| 332 | rc = flash_area_write(fap, off, buf, align); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 333 | if (rc != 0) { |
| 334 | return BOOT_EFLASH; |
| 335 | } |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | int |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 341 | boot_write_copy_done(const struct flash_area *fap) |
| 342 | { |
| 343 | return boot_write_flag(BOOT_FLAG_COPY_DONE, fap); |
| 344 | } |
| 345 | |
| 346 | int |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 347 | boot_write_image_ok(const struct flash_area *fap) |
| 348 | { |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 349 | return boot_write_flag(BOOT_FLAG_IMAGE_OK, fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | int |
| 353 | boot_swap_type(void) |
| 354 | { |
| 355 | const struct boot_swap_table *table; |
| 356 | struct boot_swap_state state_slot0; |
| 357 | struct boot_swap_state state_slot1; |
| 358 | int rc; |
| 359 | int i; |
| 360 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 361 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &state_slot0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 362 | assert(rc == 0); |
| 363 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 364 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &state_slot1); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 365 | assert(rc == 0); |
| 366 | |
| 367 | for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) { |
| 368 | table = boot_swap_tables + i; |
| 369 | |
| 370 | if ((table->bsw_magic_slot0 == 0 || |
| 371 | table->bsw_magic_slot0 == state_slot0.magic) && |
| 372 | (table->bsw_magic_slot1 == 0 || |
| 373 | table->bsw_magic_slot1 == state_slot1.magic) && |
| 374 | (table->bsw_image_ok_slot0 == 0 || |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 375 | table->bsw_image_ok_slot0 == state_slot0.image_ok) && |
| 376 | (table->bsw_image_ok_slot1 == 0 || |
| 377 | table->bsw_image_ok_slot1 == state_slot1.image_ok)) { |
Fabio Utzig | 34e393e | 2017-05-22 11:07:07 -0400 | [diff] [blame] | 378 | BOOT_LOG_INF("Swap type: %s", |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 379 | table->bsw_swap_type == BOOT_SWAP_TYPE_NONE ? "none" : |
| 380 | table->bsw_swap_type == BOOT_SWAP_TYPE_TEST ? "test" : |
| 381 | table->bsw_swap_type == BOOT_SWAP_TYPE_PERM ? "perm" : |
| 382 | table->bsw_swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" : |
| 383 | table->bsw_swap_type == BOOT_SWAP_TYPE_FAIL ? "fail" : |
| 384 | "BUG; can't happen"); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 385 | return table->bsw_swap_type; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | assert(0); |
| 390 | return BOOT_SWAP_TYPE_NONE; |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Marks the image in slot 1 as pending. On the next reboot, the system will |
| 395 | * perform a one-time boot of the slot 1 image. |
| 396 | * |
Christopher Collins | 7835c1e | 2016-12-21 10:10:51 -0800 | [diff] [blame] | 397 | * @param permanent Whether the image should be used permanently or |
| 398 | * only tested once: |
| 399 | * 0=run image once, then confirm or revert. |
| 400 | * 1=run image forever. |
| 401 | * |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 402 | * @return 0 on success; nonzero on failure. |
| 403 | */ |
| 404 | int |
Christopher Collins | 7835c1e | 2016-12-21 10:10:51 -0800 | [diff] [blame] | 405 | boot_set_pending(int permanent) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 406 | { |
| 407 | const struct flash_area *fap; |
| 408 | struct boot_swap_state state_slot1; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 409 | int rc; |
| 410 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 411 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &state_slot1); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 412 | if (rc != 0) { |
| 413 | return rc; |
| 414 | } |
| 415 | |
| 416 | switch (state_slot1.magic) { |
| 417 | case BOOT_MAGIC_GOOD: |
| 418 | /* Swap already scheduled. */ |
| 419 | return 0; |
| 420 | |
| 421 | case BOOT_MAGIC_UNSET: |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 422 | rc = flash_area_open(FLASH_AREA_IMAGE_1, &fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 423 | if (rc != 0) { |
| 424 | rc = BOOT_EFLASH; |
| 425 | } else { |
| 426 | rc = boot_write_magic(fap); |
| 427 | } |
| 428 | |
Christopher Collins | 7835c1e | 2016-12-21 10:10:51 -0800 | [diff] [blame] | 429 | if (rc == 0 && permanent) { |
| 430 | rc = boot_write_image_ok(fap); |
| 431 | } |
| 432 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 433 | flash_area_close(fap); |
| 434 | return rc; |
| 435 | |
| 436 | default: |
| 437 | /* XXX: Temporary assert. */ |
| 438 | assert(0); |
| 439 | return -1; |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * Marks the image in slot 0 as confirmed. The system will continue booting into the image in slot 0 until told to boot from a different slot. |
| 445 | * |
| 446 | * @return 0 on success; nonzero on failure. |
| 447 | */ |
| 448 | int |
| 449 | boot_set_confirmed(void) |
| 450 | { |
| 451 | const struct flash_area *fap; |
| 452 | struct boot_swap_state state_slot0; |
| 453 | int rc; |
| 454 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 455 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &state_slot0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 456 | if (rc != 0) { |
| 457 | return rc; |
| 458 | } |
| 459 | |
| 460 | switch (state_slot0.magic) { |
| 461 | case BOOT_MAGIC_GOOD: |
| 462 | /* Confirm needed; proceed. */ |
| 463 | break; |
| 464 | |
| 465 | case BOOT_MAGIC_UNSET: |
| 466 | /* Already confirmed. */ |
| 467 | return 0; |
| 468 | |
| 469 | case BOOT_MAGIC_BAD: |
| 470 | /* Unexpected state. */ |
| 471 | return BOOT_EBADVECT; |
| 472 | } |
| 473 | |
Fabio Utzig | de8a38a | 2017-05-23 11:15:01 -0400 | [diff] [blame] | 474 | if (state_slot0.copy_done == BOOT_FLAG_UNSET) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 475 | /* Swap never completed. This is unexpected. */ |
| 476 | return BOOT_EBADVECT; |
| 477 | } |
| 478 | |
Fabio Utzig | de8a38a | 2017-05-23 11:15:01 -0400 | [diff] [blame] | 479 | if (state_slot0.image_ok != BOOT_FLAG_UNSET) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 480 | /* Already confirmed. */ |
| 481 | return 0; |
| 482 | } |
| 483 | |
| 484 | rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap); |
| 485 | if (rc) { |
| 486 | rc = BOOT_EFLASH; |
| 487 | goto done; |
| 488 | } |
| 489 | |
| 490 | rc = boot_write_image_ok(fap); |
| 491 | if (rc != 0) { |
| 492 | goto done; |
| 493 | } |
| 494 | |
| 495 | rc = 0; |
| 496 | |
| 497 | done: |
| 498 | flash_area_close(fap); |
| 499 | return rc; |
| 500 | } |