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