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