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 | /** |
| 21 | * This file provides an interface to the boot loader. Functions defined in |
| 22 | * this file should only be called while the boot loader is running. |
| 23 | */ |
| 24 | |
| 25 | #include <assert.h> |
| 26 | #include <stddef.h> |
| 27 | #include <inttypes.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 30 | #include <hal/hal_flash.h> |
| 31 | #include <os/os_malloc.h> |
| 32 | #include "bootutil/bootutil.h" |
| 33 | #include "bootutil/image.h" |
| 34 | #include "bootutil_priv.h" |
| 35 | |
Marti Bolivar | fd20c76 | 2017-02-07 16:52:50 -0500 | [diff] [blame] | 36 | #define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO |
| 37 | #include "bootutil/bootutil_log.h" |
| 38 | |
Fabio Utzig | eed80b6 | 2017-06-10 08:03:05 -0300 | [diff] [blame] | 39 | #ifdef APP_mynewt |
| 40 | #include "mynewt/config.h" |
| 41 | #endif |
| 42 | |
Marti Bolivar | 9b1f8bb | 2017-06-12 15:24:13 -0400 | [diff] [blame] | 43 | static struct boot_loader_state boot_data; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 44 | |
| 45 | struct boot_status_table { |
| 46 | /** |
| 47 | * For each field, a value of 0 means "any". |
| 48 | */ |
| 49 | uint8_t bst_magic_slot0; |
| 50 | uint8_t bst_magic_scratch; |
| 51 | uint8_t bst_copy_done_slot0; |
| 52 | uint8_t bst_status_source; |
| 53 | }; |
| 54 | |
| 55 | /** |
| 56 | * This set of tables maps swap state contents to boot status location. |
| 57 | * When searching for a match, these tables must be iterated in order. |
| 58 | */ |
| 59 | static const struct boot_status_table boot_status_tables[] = { |
| 60 | { |
| 61 | /* | slot-0 | scratch | |
| 62 | * ----------+------------+------------| |
| 63 | * magic | Good | Any | |
| 64 | * copy-done | 0x01 | N/A | |
| 65 | * ----------+------------+------------' |
| 66 | * source: none | |
| 67 | * ------------------------------------' |
| 68 | */ |
| 69 | .bst_magic_slot0 = BOOT_MAGIC_GOOD, |
| 70 | .bst_magic_scratch = 0, |
| 71 | .bst_copy_done_slot0 = 0x01, |
| 72 | .bst_status_source = BOOT_STATUS_SOURCE_NONE, |
| 73 | }, |
| 74 | |
| 75 | { |
| 76 | /* | slot-0 | scratch | |
| 77 | * ----------+------------+------------| |
| 78 | * magic | Good | Any | |
| 79 | * copy-done | 0xff | N/A | |
| 80 | * ----------+------------+------------' |
| 81 | * source: slot 0 | |
| 82 | * ------------------------------------' |
| 83 | */ |
| 84 | .bst_magic_slot0 = BOOT_MAGIC_GOOD, |
| 85 | .bst_magic_scratch = 0, |
| 86 | .bst_copy_done_slot0 = 0xff, |
| 87 | .bst_status_source = BOOT_STATUS_SOURCE_SLOT0, |
| 88 | }, |
| 89 | |
| 90 | { |
| 91 | /* | slot-0 | scratch | |
| 92 | * ----------+------------+------------| |
| 93 | * magic | Any | Good | |
| 94 | * copy-done | Any | N/A | |
| 95 | * ----------+------------+------------' |
| 96 | * source: scratch | |
| 97 | * ------------------------------------' |
| 98 | */ |
| 99 | .bst_magic_slot0 = 0, |
| 100 | .bst_magic_scratch = BOOT_MAGIC_GOOD, |
| 101 | .bst_copy_done_slot0 = 0, |
| 102 | .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH, |
| 103 | }, |
| 104 | |
| 105 | { |
| 106 | /* | slot-0 | scratch | |
| 107 | * ----------+------------+------------| |
| 108 | * magic | Unset | Any | |
| 109 | * copy-done | 0xff | N/A | |
| 110 | * ----------+------------+------------| |
| 111 | * source: varies | |
| 112 | * ------------------------------------+------------------------------+ |
| 113 | * This represents one of two cases: | |
| 114 | * o No swaps ever (no status to read, so no harm in checking). | |
| 115 | * o Mid-revert; status in slot 0. | |
| 116 | * -------------------------------------------------------------------' |
| 117 | */ |
| 118 | .bst_magic_slot0 = BOOT_MAGIC_UNSET, |
| 119 | .bst_magic_scratch = 0, |
| 120 | .bst_copy_done_slot0 = 0xff, |
| 121 | .bst_status_source = BOOT_STATUS_SOURCE_SLOT0, |
| 122 | }, |
| 123 | }; |
| 124 | |
| 125 | #define BOOT_STATUS_TABLES_COUNT \ |
| 126 | (sizeof boot_status_tables / sizeof boot_status_tables[0]) |
| 127 | |
| 128 | /** |
| 129 | * This table indicates the next swap type that should be performed. The first |
| 130 | * column contains the current swap type. The second column contains the swap |
| 131 | * type that should be effected after the first completes. |
| 132 | */ |
| 133 | static const uint8_t boot_swap_trans_table[][2] = { |
| 134 | /* From To */ |
| 135 | { BOOT_SWAP_TYPE_REVERT, BOOT_SWAP_TYPE_NONE }, |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 136 | { BOOT_SWAP_TYPE_PERM, BOOT_SWAP_TYPE_NONE }, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 137 | { BOOT_SWAP_TYPE_TEST, BOOT_SWAP_TYPE_REVERT }, |
| 138 | }; |
| 139 | |
| 140 | #define BOOT_SWAP_TRANS_TABLE_SIZE \ |
| 141 | (sizeof boot_swap_trans_table / sizeof boot_swap_trans_table[0]) |
| 142 | |
Marti Bolivar | fd20c76 | 2017-02-07 16:52:50 -0500 | [diff] [blame] | 143 | #define BOOT_LOG_SWAP_STATE(area, state) \ |
| 144 | BOOT_LOG_INF("%s: magic=%s, copy_done=0x%x, image_ok=0x%x", \ |
| 145 | (area), \ |
| 146 | ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \ |
| 147 | (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \ |
| 148 | "bad"), \ |
| 149 | (state)->copy_done, \ |
| 150 | (state)->image_ok) |
| 151 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 152 | /** |
| 153 | * Determines where in flash the most recent boot status is stored. The boot |
| 154 | * status is necessary for completing a swap that was interrupted by a boot |
| 155 | * loader reset. |
| 156 | * |
| 157 | * @return A BOOT_STATUS_SOURCE_[...] code indicating where * status should be read from. |
| 158 | */ |
| 159 | static int |
| 160 | boot_status_source(void) |
| 161 | { |
| 162 | const struct boot_status_table *table; |
| 163 | struct boot_swap_state state_scratch; |
| 164 | struct boot_swap_state state_slot0; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 165 | int rc; |
| 166 | int i; |
Marti Bolivar | fd20c76 | 2017-02-07 16:52:50 -0500 | [diff] [blame] | 167 | uint8_t source; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 168 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 169 | 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] | 170 | assert(rc == 0); |
| 171 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 172 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 173 | assert(rc == 0); |
| 174 | |
Marti Bolivar | fd20c76 | 2017-02-07 16:52:50 -0500 | [diff] [blame] | 175 | BOOT_LOG_SWAP_STATE("Image 0", &state_slot0); |
Marti Bolivar | fd20c76 | 2017-02-07 16:52:50 -0500 | [diff] [blame] | 176 | BOOT_LOG_SWAP_STATE("Scratch", &state_scratch); |
| 177 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 178 | for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) { |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 179 | table = &boot_status_tables[i]; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 180 | |
| 181 | if ((table->bst_magic_slot0 == 0 || |
| 182 | table->bst_magic_slot0 == state_slot0.magic) && |
| 183 | (table->bst_magic_scratch == 0 || |
| 184 | table->bst_magic_scratch == state_scratch.magic) && |
| 185 | (table->bst_copy_done_slot0 == 0 || |
| 186 | table->bst_copy_done_slot0 == state_slot0.copy_done)) { |
Marti Bolivar | fd20c76 | 2017-02-07 16:52:50 -0500 | [diff] [blame] | 187 | source = table->bst_status_source; |
| 188 | BOOT_LOG_INF("Boot source: %s", |
| 189 | source == BOOT_STATUS_SOURCE_NONE ? "none" : |
| 190 | source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" : |
| 191 | source == BOOT_STATUS_SOURCE_SLOT0 ? "slot 0" : |
| 192 | "BUG; can't happen"); |
| 193 | return source; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Marti Bolivar | fd20c76 | 2017-02-07 16:52:50 -0500 | [diff] [blame] | 197 | BOOT_LOG_INF("Boot source: none"); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 198 | return BOOT_STATUS_SOURCE_NONE; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Calculates the type of swap that just completed. |
| 203 | */ |
| 204 | static int |
| 205 | boot_previous_swap_type(void) |
| 206 | { |
| 207 | int post_swap_type; |
| 208 | int i; |
| 209 | |
| 210 | post_swap_type = boot_swap_type(); |
| 211 | |
| 212 | for (i = 0; i < BOOT_SWAP_TRANS_TABLE_SIZE; i++){ |
| 213 | if (boot_swap_trans_table[i][1] == post_swap_type) { |
| 214 | return boot_swap_trans_table[i][0]; |
| 215 | } |
| 216 | } |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 217 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 218 | /* XXX: Temporary assert. */ |
| 219 | assert(0); |
| 220 | |
| 221 | return BOOT_SWAP_TYPE_REVERT; |
| 222 | } |
| 223 | |
| 224 | static int |
| 225 | boot_read_image_header(int slot, struct image_header *out_hdr) |
| 226 | { |
| 227 | const struct flash_area *fap; |
| 228 | int area_id; |
| 229 | int rc; |
| 230 | |
| 231 | area_id = flash_area_id_from_image_slot(slot); |
| 232 | rc = flash_area_open(area_id, &fap); |
| 233 | if (rc != 0) { |
| 234 | rc = BOOT_EFLASH; |
| 235 | goto done; |
| 236 | } |
| 237 | |
| 238 | rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr); |
| 239 | if (rc != 0) { |
| 240 | rc = BOOT_EFLASH; |
| 241 | goto done; |
| 242 | } |
| 243 | |
| 244 | rc = 0; |
| 245 | |
| 246 | done: |
| 247 | flash_area_close(fap); |
| 248 | return rc; |
| 249 | } |
| 250 | |
| 251 | static int |
| 252 | boot_read_image_headers(void) |
| 253 | { |
| 254 | int rc; |
| 255 | int i; |
| 256 | |
| 257 | for (i = 0; i < BOOT_NUM_SLOTS; i++) { |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 258 | rc = boot_read_image_header(i, boot_img_hdr(&boot_data, i)); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 259 | if (rc != 0) { |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 260 | /* If at least the first slot's header was read successfully, then |
| 261 | * the boot loader can attempt a boot. Failure to read any headers |
| 262 | * is a fatal error. |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 263 | */ |
| 264 | if (i > 0) { |
| 265 | return 0; |
| 266 | } else { |
| 267 | return rc; |
| 268 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | static uint8_t |
| 276 | boot_write_sz(void) |
| 277 | { |
| 278 | uint8_t elem_sz; |
| 279 | uint8_t align; |
| 280 | |
| 281 | /* Figure out what size to write update status update as. The size depends |
| 282 | * on what the minimum write size is for scratch area, active image slot. |
| 283 | * We need to use the bigger of those 2 values. |
| 284 | */ |
Marti Bolivar | e258715 | 2017-06-12 15:52:05 -0400 | [diff] [blame] | 285 | elem_sz = hal_flash_align(boot_img_fa_device_id(&boot_data, 0)); |
| 286 | align = hal_flash_align(boot_scratch_fa_device_id(&boot_data)); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 287 | if (align > elem_sz) { |
| 288 | elem_sz = align; |
| 289 | } |
| 290 | |
| 291 | return elem_sz; |
| 292 | } |
| 293 | |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 294 | static int |
| 295 | boot_slots_compatible(void) |
| 296 | { |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 297 | size_t num_sectors_0 = boot_img_num_sectors(&boot_data, 0); |
| 298 | size_t num_sectors_1 = boot_img_num_sectors(&boot_data, 1); |
| 299 | size_t size_0, size_1; |
| 300 | size_t i; |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 301 | |
| 302 | /* Ensure both image slots have identical sector layouts. */ |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 303 | if (num_sectors_0 != num_sectors_1) { |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 304 | return 0; |
| 305 | } |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 306 | for (i = 0; i < num_sectors_0; i++) { |
| 307 | size_0 = boot_img_sector_size(&boot_data, 0, i); |
| 308 | size_1 = boot_img_sector_size(&boot_data, 1, i); |
| 309 | if (size_0 != size_1) { |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 310 | return 0; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | return 1; |
| 315 | } |
| 316 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 317 | /** |
| 318 | * Determines the sector layout of both image slots and the scratch area. |
| 319 | * This information is necessary for calculating the number of bytes to erase |
| 320 | * and copy during an image swap. The information collected during this |
| 321 | * function is used to populate the boot_data global. |
| 322 | */ |
| 323 | static int |
| 324 | boot_read_sectors(void) |
| 325 | { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 326 | int rc; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 327 | |
Marti Bolivar | cca28a9 | 2017-06-12 16:52:22 -0400 | [diff] [blame] | 328 | rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 329 | if (rc != 0) { |
| 330 | return BOOT_EFLASH; |
| 331 | } |
| 332 | |
Marti Bolivar | cca28a9 | 2017-06-12 16:52:22 -0400 | [diff] [blame] | 333 | rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_1); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 334 | if (rc != 0) { |
| 335 | return BOOT_EFLASH; |
| 336 | } |
| 337 | |
Marti Bolivar | e10a739 | 2017-06-14 16:20:07 -0400 | [diff] [blame] | 338 | BOOT_WRITE_SZ(&boot_data) = boot_write_sz(); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | static uint32_t |
| 344 | boot_status_internal_off(int idx, int state, int elem_sz) |
| 345 | { |
| 346 | int idx_sz; |
| 347 | |
| 348 | idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT; |
| 349 | |
| 350 | return idx * idx_sz + state * elem_sz; |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Reads the status of a partially-completed swap, if any. This is necessary |
| 355 | * to recover in case the boot lodaer was reset in the middle of a swap |
| 356 | * operation. |
| 357 | */ |
| 358 | static int |
| 359 | boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs) |
| 360 | { |
| 361 | uint32_t off; |
| 362 | uint8_t status; |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 363 | int max_entries; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 364 | int found; |
| 365 | int rc; |
| 366 | int i; |
| 367 | |
| 368 | off = boot_status_off(fap); |
Fabio Utzig | 4cee4f7 | 2017-05-22 10:59:57 -0400 | [diff] [blame] | 369 | max_entries = boot_status_entries(fap); |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 370 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 371 | found = 0; |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 372 | for (i = 0; i < max_entries; i++) { |
Marti Bolivar | e10a739 | 2017-06-14 16:20:07 -0400 | [diff] [blame] | 373 | rc = flash_area_read(fap, off + i * BOOT_WRITE_SZ(&boot_data), |
| 374 | &status, 1); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 375 | if (rc != 0) { |
| 376 | return BOOT_EFLASH; |
| 377 | } |
| 378 | |
| 379 | if (status == 0xff) { |
| 380 | if (found) { |
| 381 | break; |
| 382 | } |
| 383 | } else if (!found) { |
| 384 | found = 1; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | if (found) { |
| 389 | i--; |
Fabio Utzig | 94d998c | 2017-05-22 11:02:41 -0400 | [diff] [blame] | 390 | bs->idx = i / BOOT_STATUS_STATE_COUNT; |
| 391 | bs->state = i % BOOT_STATUS_STATE_COUNT; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Reads the boot status from the flash. The boot status contains |
| 399 | * the current state of an interrupted image copy operation. If the boot |
| 400 | * status is not present, or it indicates that previous copy finished, |
| 401 | * there is no operation in progress. |
| 402 | */ |
| 403 | static int |
| 404 | boot_read_status(struct boot_status *bs) |
| 405 | { |
| 406 | const struct flash_area *fap; |
| 407 | int status_loc; |
| 408 | int area_id; |
| 409 | int rc; |
| 410 | |
| 411 | memset(bs, 0, sizeof *bs); |
| 412 | |
| 413 | status_loc = boot_status_source(); |
| 414 | switch (status_loc) { |
| 415 | case BOOT_STATUS_SOURCE_NONE: |
| 416 | return 0; |
| 417 | |
| 418 | case BOOT_STATUS_SOURCE_SCRATCH: |
| 419 | area_id = FLASH_AREA_IMAGE_SCRATCH; |
| 420 | break; |
| 421 | |
| 422 | case BOOT_STATUS_SOURCE_SLOT0: |
| 423 | area_id = FLASH_AREA_IMAGE_0; |
| 424 | break; |
| 425 | |
| 426 | default: |
| 427 | assert(0); |
| 428 | return BOOT_EBADARGS; |
| 429 | } |
| 430 | |
| 431 | rc = flash_area_open(area_id, &fap); |
| 432 | if (rc != 0) { |
| 433 | return BOOT_EFLASH; |
| 434 | } |
| 435 | |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 436 | return boot_read_status_bytes(fap, bs); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | /** |
| 440 | * Writes the supplied boot status to the flash file system. The boot status |
| 441 | * contains the current state of an in-progress image copy operation. |
| 442 | * |
| 443 | * @param bs The boot status to write. |
| 444 | * |
| 445 | * @return 0 on success; nonzero on failure. |
| 446 | */ |
| 447 | int |
| 448 | boot_write_status(struct boot_status *bs) |
| 449 | { |
| 450 | const struct flash_area *fap; |
| 451 | uint32_t off; |
| 452 | int area_id; |
| 453 | int rc; |
David Brown | 9d72546 | 2017-01-23 15:50:58 -0700 | [diff] [blame] | 454 | uint8_t buf[8]; |
| 455 | uint8_t align; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 456 | |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 457 | /* NOTE: The first sector copied (that is the last sector on slot) contains |
| 458 | * the trailer. Since in the last step SLOT 0 is erased, the first |
| 459 | * two status writes go to the scratch which will be copied to SLOT 0! |
| 460 | */ |
| 461 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 462 | if (bs->use_scratch) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 463 | /* Write to scratch. */ |
| 464 | area_id = FLASH_AREA_IMAGE_SCRATCH; |
| 465 | } else { |
| 466 | /* Write to slot 0. */ |
| 467 | area_id = FLASH_AREA_IMAGE_0; |
| 468 | } |
| 469 | |
| 470 | rc = flash_area_open(area_id, &fap); |
| 471 | if (rc != 0) { |
| 472 | rc = BOOT_EFLASH; |
| 473 | goto done; |
| 474 | } |
| 475 | |
| 476 | off = boot_status_off(fap) + |
Marti Bolivar | e10a739 | 2017-06-14 16:20:07 -0400 | [diff] [blame] | 477 | boot_status_internal_off(bs->idx, bs->state, |
| 478 | BOOT_WRITE_SZ(&boot_data)); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 479 | |
David Brown | 9d72546 | 2017-01-23 15:50:58 -0700 | [diff] [blame] | 480 | align = hal_flash_align(fap->fa_device_id); |
David Brown | 9d72546 | 2017-01-23 15:50:58 -0700 | [diff] [blame] | 481 | memset(buf, 0xFF, 8); |
| 482 | buf[0] = bs->state; |
| 483 | |
| 484 | rc = flash_area_write(fap, off, buf, align); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 485 | if (rc != 0) { |
| 486 | rc = BOOT_EFLASH; |
| 487 | goto done; |
| 488 | } |
| 489 | |
| 490 | rc = 0; |
| 491 | |
| 492 | done: |
| 493 | flash_area_close(fap); |
| 494 | return rc; |
| 495 | } |
| 496 | |
| 497 | /* |
| 498 | * Validate image hash/signature in a slot. |
| 499 | */ |
| 500 | static int |
| 501 | boot_image_check(struct image_header *hdr, const struct flash_area *fap) |
| 502 | { |
David Brown | db1d9d3 | 2017-01-06 11:07:54 -0700 | [diff] [blame] | 503 | static uint8_t tmpbuf[BOOT_TMPBUF_SZ]; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 504 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 505 | if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ, |
| 506 | NULL, 0, NULL)) { |
| 507 | return BOOT_EBADIMAGE; |
| 508 | } |
| 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | static int |
| 513 | split_image_check(struct image_header *app_hdr, |
| 514 | const struct flash_area *app_fap, |
| 515 | struct image_header *loader_hdr, |
| 516 | const struct flash_area *loader_fap) |
| 517 | { |
| 518 | static void *tmpbuf; |
| 519 | uint8_t loader_hash[32]; |
| 520 | |
| 521 | if (!tmpbuf) { |
| 522 | tmpbuf = malloc(BOOT_TMPBUF_SZ); |
| 523 | if (!tmpbuf) { |
| 524 | return BOOT_ENOMEM; |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | if (bootutil_img_validate(loader_hdr, loader_fap, tmpbuf, BOOT_TMPBUF_SZ, |
| 529 | NULL, 0, loader_hash)) { |
| 530 | return BOOT_EBADIMAGE; |
| 531 | } |
| 532 | |
| 533 | if (bootutil_img_validate(app_hdr, app_fap, tmpbuf, BOOT_TMPBUF_SZ, |
| 534 | loader_hash, 32, NULL)) { |
| 535 | return BOOT_EBADIMAGE; |
| 536 | } |
| 537 | |
| 538 | return 0; |
| 539 | } |
| 540 | |
| 541 | static int |
David Brown | d930ec6 | 2016-12-14 07:59:48 -0700 | [diff] [blame] | 542 | boot_validate_slot(int slot) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 543 | { |
| 544 | const struct flash_area *fap; |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 545 | struct image_header *hdr; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 546 | int rc; |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 547 | |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 548 | hdr = boot_img_hdr(&boot_data, slot); |
| 549 | if (hdr->ih_magic == 0xffffffff || hdr->ih_flags & IMAGE_F_NON_BOOTABLE) { |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 550 | /* No bootable image in slot; continue booting from slot 0. */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 551 | return -1; |
| 552 | } |
| 553 | |
David Brown | d930ec6 | 2016-12-14 07:59:48 -0700 | [diff] [blame] | 554 | rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 555 | if (rc != 0) { |
| 556 | return BOOT_EFLASH; |
| 557 | } |
| 558 | |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 559 | if ((hdr->ih_magic != IMAGE_MAGIC || boot_image_check(hdr, fap) != 0)) { |
David Brown | b38e044 | 2017-02-24 13:57:12 -0700 | [diff] [blame] | 560 | if (slot != 0) { |
| 561 | flash_area_erase(fap, 0, fap->fa_size); |
| 562 | /* Image in slot 1 is invalid. Erase the image and |
| 563 | * continue booting from slot 0. |
| 564 | */ |
| 565 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 566 | return -1; |
| 567 | } |
| 568 | |
| 569 | flash_area_close(fap); |
| 570 | |
| 571 | /* Image in slot 1 is valid. */ |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * Determines which swap operation to perform, if any. If it is determined |
| 577 | * that a swap operation is required, the image in the second slot is checked |
| 578 | * for validity. If the image in the second slot is invalid, it is erased, and |
| 579 | * a swap type of "none" is indicated. |
| 580 | * |
| 581 | * @return The type of swap to perform (BOOT_SWAP_TYPE...) |
| 582 | */ |
| 583 | static int |
| 584 | boot_validated_swap_type(void) |
| 585 | { |
| 586 | int swap_type; |
| 587 | int rc; |
| 588 | |
| 589 | swap_type = boot_swap_type(); |
| 590 | if (swap_type == BOOT_SWAP_TYPE_NONE) { |
| 591 | /* Continue using slot 0. */ |
| 592 | return BOOT_SWAP_TYPE_NONE; |
| 593 | } |
| 594 | |
| 595 | /* Boot loader wants to switch to slot 1. Ensure image is valid. */ |
David Brown | d930ec6 | 2016-12-14 07:59:48 -0700 | [diff] [blame] | 596 | rc = boot_validate_slot(1); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 597 | if (rc != 0) { |
| 598 | return BOOT_SWAP_TYPE_FAIL; |
| 599 | } |
| 600 | |
| 601 | return swap_type; |
| 602 | } |
| 603 | |
| 604 | /** |
| 605 | * Calculates the number of sectors the scratch area can contain. A "last" |
| 606 | * source sector is specified because images are copied backwards in flash |
| 607 | * (final index to index number 0). |
| 608 | * |
| 609 | * @param last_sector_idx The index of the last source sector |
| 610 | * (inclusive). |
| 611 | * @param out_first_sector_idx The index of the first source sector |
| 612 | * (inclusive) gets written here. |
| 613 | * |
| 614 | * @return The number of bytes comprised by the |
| 615 | * [first-sector, last-sector] range. |
| 616 | */ |
Fabio Utzig | 3488eef | 2017-06-12 10:25:43 -0300 | [diff] [blame] | 617 | #ifndef MCUBOOT_OVERWRITE_ONLY |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 618 | static uint32_t |
| 619 | boot_copy_sz(int last_sector_idx, int *out_first_sector_idx) |
| 620 | { |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 621 | size_t scratch_sz; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 622 | uint32_t new_sz; |
| 623 | uint32_t sz; |
| 624 | int i; |
| 625 | |
| 626 | sz = 0; |
| 627 | |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 628 | scratch_sz = boot_scratch_area_size(&boot_data); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 629 | for (i = last_sector_idx; i >= 0; i--) { |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 630 | new_sz = sz + boot_img_sector_size(&boot_data, 0, i); |
| 631 | if (new_sz > scratch_sz) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 632 | break; |
| 633 | } |
| 634 | sz = new_sz; |
| 635 | } |
| 636 | |
| 637 | /* i currently refers to a sector that doesn't fit or it is -1 because all |
| 638 | * sectors have been processed. In both cases, exclude sector i. |
| 639 | */ |
| 640 | *out_first_sector_idx = i + 1; |
| 641 | return sz; |
| 642 | } |
Fabio Utzig | 3488eef | 2017-06-12 10:25:43 -0300 | [diff] [blame] | 643 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 644 | |
| 645 | /** |
| 646 | * Erases a region of flash. |
| 647 | * |
| 648 | * @param flash_area_idx The ID of the flash area containing the region |
| 649 | * to erase. |
| 650 | * @param off The offset within the flash area to start the |
| 651 | * erase. |
| 652 | * @param sz The number of bytes to erase. |
| 653 | * |
| 654 | * @return 0 on success; nonzero on failure. |
| 655 | */ |
| 656 | static int |
| 657 | boot_erase_sector(int flash_area_id, uint32_t off, uint32_t sz) |
| 658 | { |
| 659 | const struct flash_area *fap; |
| 660 | int rc; |
| 661 | |
| 662 | rc = flash_area_open(flash_area_id, &fap); |
| 663 | if (rc != 0) { |
| 664 | rc = BOOT_EFLASH; |
| 665 | goto done; |
| 666 | } |
| 667 | |
| 668 | rc = flash_area_erase(fap, off, sz); |
| 669 | if (rc != 0) { |
| 670 | rc = BOOT_EFLASH; |
| 671 | goto done; |
| 672 | } |
| 673 | |
| 674 | rc = 0; |
| 675 | |
| 676 | done: |
| 677 | flash_area_close(fap); |
| 678 | return rc; |
| 679 | } |
| 680 | |
| 681 | /** |
| 682 | * Copies the contents of one flash region to another. You must erase the |
| 683 | * destination region prior to calling this function. |
| 684 | * |
| 685 | * @param flash_area_id_src The ID of the source flash area. |
| 686 | * @param flash_area_id_dst The ID of the destination flash area. |
| 687 | * @param off_src The offset within the source flash area to |
| 688 | * copy from. |
| 689 | * @param off_dst The offset within the destination flash area to |
| 690 | * copy to. |
| 691 | * @param sz The number of bytes to copy. |
| 692 | * |
| 693 | * @return 0 on success; nonzero on failure. |
| 694 | */ |
| 695 | static int |
| 696 | boot_copy_sector(int flash_area_id_src, int flash_area_id_dst, |
| 697 | uint32_t off_src, uint32_t off_dst, uint32_t sz) |
| 698 | { |
| 699 | const struct flash_area *fap_src; |
| 700 | const struct flash_area *fap_dst; |
| 701 | uint32_t bytes_copied; |
| 702 | int chunk_sz; |
| 703 | int rc; |
| 704 | |
| 705 | static uint8_t buf[1024]; |
| 706 | |
| 707 | fap_src = NULL; |
| 708 | fap_dst = NULL; |
| 709 | |
| 710 | rc = flash_area_open(flash_area_id_src, &fap_src); |
| 711 | if (rc != 0) { |
| 712 | rc = BOOT_EFLASH; |
| 713 | goto done; |
| 714 | } |
| 715 | |
| 716 | rc = flash_area_open(flash_area_id_dst, &fap_dst); |
| 717 | if (rc != 0) { |
| 718 | rc = BOOT_EFLASH; |
| 719 | goto done; |
| 720 | } |
| 721 | |
| 722 | bytes_copied = 0; |
| 723 | while (bytes_copied < sz) { |
| 724 | if (sz - bytes_copied > sizeof buf) { |
| 725 | chunk_sz = sizeof buf; |
| 726 | } else { |
| 727 | chunk_sz = sz - bytes_copied; |
| 728 | } |
| 729 | |
| 730 | rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz); |
| 731 | if (rc != 0) { |
| 732 | rc = BOOT_EFLASH; |
| 733 | goto done; |
| 734 | } |
| 735 | |
| 736 | rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz); |
| 737 | if (rc != 0) { |
| 738 | rc = BOOT_EFLASH; |
| 739 | goto done; |
| 740 | } |
| 741 | |
| 742 | bytes_copied += chunk_sz; |
| 743 | } |
| 744 | |
| 745 | rc = 0; |
| 746 | |
| 747 | done: |
Fabio Utzig | e768626 | 2017-06-28 09:26:54 -0300 | [diff] [blame^] | 748 | if (fap_src) { |
| 749 | flash_area_close(fap_src); |
| 750 | } |
| 751 | if (fap_dst) { |
| 752 | flash_area_close(fap_dst); |
| 753 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 754 | return rc; |
| 755 | } |
| 756 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 757 | static inline int |
| 758 | boot_status_init_by_id(int flash_area_id) |
| 759 | { |
| 760 | const struct flash_area *fap; |
| 761 | struct boot_swap_state swap_state; |
| 762 | int rc; |
| 763 | |
| 764 | rc = flash_area_open(flash_area_id, &fap); |
| 765 | assert(rc == 0); |
| 766 | |
| 767 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &swap_state); |
| 768 | assert(rc == 0); |
| 769 | |
Fabio Utzig | de8a38a | 2017-05-23 11:15:01 -0400 | [diff] [blame] | 770 | if (swap_state.image_ok == BOOT_FLAG_SET) { |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 771 | rc = boot_write_image_ok(fap); |
| 772 | assert(rc == 0); |
| 773 | } |
| 774 | |
| 775 | rc = boot_write_magic(fap); |
| 776 | assert(rc == 0); |
| 777 | |
| 778 | flash_area_close(fap); |
| 779 | |
| 780 | return 0; |
| 781 | } |
| 782 | |
| 783 | static int |
| 784 | boot_erase_last_sector_by_id(int flash_area_id) |
| 785 | { |
| 786 | uint8_t slot; |
| 787 | uint32_t last_sector; |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 788 | int rc; |
| 789 | |
| 790 | switch (flash_area_id) { |
| 791 | case FLASH_AREA_IMAGE_0: |
| 792 | slot = 0; |
| 793 | break; |
| 794 | case FLASH_AREA_IMAGE_1: |
| 795 | slot = 1; |
| 796 | break; |
| 797 | default: |
| 798 | return BOOT_EFLASH; |
| 799 | } |
| 800 | |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 801 | last_sector = boot_img_num_sectors(&boot_data, slot) - 1; |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 802 | rc = boot_erase_sector(flash_area_id, |
Marti Bolivar | ea08887 | 2017-06-12 17:10:49 -0400 | [diff] [blame] | 803 | boot_img_sector_off(&boot_data, slot, last_sector), |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 804 | boot_img_sector_size(&boot_data, slot, last_sector)); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 805 | assert(rc == 0); |
| 806 | |
| 807 | return rc; |
| 808 | } |
| 809 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 810 | /** |
| 811 | * Swaps the contents of two flash regions within the two image slots. |
| 812 | * |
| 813 | * @param idx The index of the first sector in the range of |
| 814 | * sectors being swapped. |
| 815 | * @param sz The number of bytes to swap. |
| 816 | * @param bs The current boot status. This struct gets |
| 817 | * updated according to the outcome. |
| 818 | * |
| 819 | * @return 0 on success; nonzero on failure. |
| 820 | */ |
Fabio Utzig | 3488eef | 2017-06-12 10:25:43 -0300 | [diff] [blame] | 821 | #ifndef MCUBOOT_OVERWRITE_ONLY |
Christopher Collins | 4772ac4 | 2017-02-27 20:08:01 -0800 | [diff] [blame] | 822 | static void |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 823 | boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs) |
| 824 | { |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 825 | const struct flash_area *fap; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 826 | uint32_t copy_sz; |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 827 | uint32_t trailer_sz; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 828 | uint32_t img_off; |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 829 | uint32_t scratch_trailer_off; |
| 830 | struct boot_swap_state swap_state; |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 831 | size_t last_sector; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 832 | int rc; |
| 833 | |
| 834 | /* Calculate offset from start of image area. */ |
Marti Bolivar | ea08887 | 2017-06-12 17:10:49 -0400 | [diff] [blame] | 835 | img_off = boot_img_sector_off(&boot_data, 0, idx); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 836 | |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 837 | copy_sz = sz; |
Marti Bolivar | e10a739 | 2017-06-14 16:20:07 -0400 | [diff] [blame] | 838 | trailer_sz = boot_slots_trailer_sz(BOOT_WRITE_SZ(&boot_data)); |
Fabio Utzig | 9678c97 | 2017-05-23 11:28:56 -0400 | [diff] [blame] | 839 | |
| 840 | /* sz in this function is always is always sized on a multiple of the |
Marti Bolivar | ea08887 | 2017-06-12 17:10:49 -0400 | [diff] [blame] | 841 | * sector size. The check against the start offset of the last sector |
Fabio Utzig | 9678c97 | 2017-05-23 11:28:56 -0400 | [diff] [blame] | 842 | * is to determine if we're swapping the last sector. The last sector |
| 843 | * needs special handling because it's where the trailer lives. If we're |
| 844 | * copying it, we need to use scratch to write the trailer temporarily. |
| 845 | * |
| 846 | * NOTE: `use_scratch` is a temporary flag (never written to flash) which |
| 847 | * controls if special handling is needed (swapping last sector). |
| 848 | */ |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 849 | last_sector = boot_img_num_sectors(&boot_data, 0) - 1; |
Marti Bolivar | ea08887 | 2017-06-12 17:10:49 -0400 | [diff] [blame] | 850 | if (img_off + sz > boot_img_sector_off(&boot_data, 0, last_sector)) { |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 851 | copy_sz -= trailer_sz; |
| 852 | } |
| 853 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 854 | bs->use_scratch = (bs->idx == 0 && copy_sz != sz); |
| 855 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 856 | if (bs->state == 0) { |
| 857 | rc = boot_erase_sector(FLASH_AREA_IMAGE_SCRATCH, 0, sz); |
Christopher Collins | 4772ac4 | 2017-02-27 20:08:01 -0800 | [diff] [blame] | 858 | assert(rc == 0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 859 | |
| 860 | rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_SCRATCH, |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 861 | img_off, 0, copy_sz); |
Christopher Collins | 4772ac4 | 2017-02-27 20:08:01 -0800 | [diff] [blame] | 862 | assert(rc == 0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 863 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 864 | if (bs->idx == 0) { |
| 865 | if (bs->use_scratch) { |
| 866 | boot_status_init_by_id(FLASH_AREA_IMAGE_SCRATCH); |
| 867 | } else { |
| 868 | /* Prepare the status area... here it is known that the |
| 869 | * last sector is not being used by the image data so it's |
| 870 | * safe to erase. |
| 871 | */ |
| 872 | rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_0); |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 873 | assert(rc == 0); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 874 | |
| 875 | boot_status_init_by_id(FLASH_AREA_IMAGE_0); |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 876 | } |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 877 | } |
| 878 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 879 | bs->state = 1; |
Christopher Collins | 4772ac4 | 2017-02-27 20:08:01 -0800 | [diff] [blame] | 880 | rc = boot_write_status(bs); |
| 881 | assert(rc == 0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 882 | } |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 883 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 884 | if (bs->state == 1) { |
| 885 | rc = boot_erase_sector(FLASH_AREA_IMAGE_1, img_off, sz); |
Christopher Collins | 4772ac4 | 2017-02-27 20:08:01 -0800 | [diff] [blame] | 886 | assert(rc == 0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 887 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 888 | rc = boot_copy_sector(FLASH_AREA_IMAGE_0, FLASH_AREA_IMAGE_1, |
| 889 | img_off, img_off, copy_sz); |
Christopher Collins | 4772ac4 | 2017-02-27 20:08:01 -0800 | [diff] [blame] | 890 | assert(rc == 0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 891 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 892 | if (bs->idx == 0 && !bs->use_scratch) { |
| 893 | /* If not all sectors of the slot are being swapped, |
| 894 | * guarantee here that only slot0 will have the state. |
| 895 | */ |
| 896 | rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_1); |
| 897 | assert(rc == 0); |
| 898 | } |
| 899 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 900 | bs->state = 2; |
Christopher Collins | 4772ac4 | 2017-02-27 20:08:01 -0800 | [diff] [blame] | 901 | rc = boot_write_status(bs); |
| 902 | assert(rc == 0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 903 | } |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 904 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 905 | if (bs->state == 2) { |
| 906 | rc = boot_erase_sector(FLASH_AREA_IMAGE_0, img_off, sz); |
Christopher Collins | 4772ac4 | 2017-02-27 20:08:01 -0800 | [diff] [blame] | 907 | assert(rc == 0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 908 | |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 909 | /* NOTE: also copy trailer from scratch (has status info) */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 910 | rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0, |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 911 | 0, img_off, copy_sz); |
Christopher Collins | 4772ac4 | 2017-02-27 20:08:01 -0800 | [diff] [blame] | 912 | assert(rc == 0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 913 | |
Fabio Utzig | 94d998c | 2017-05-22 11:02:41 -0400 | [diff] [blame] | 914 | if (bs->use_scratch) { |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 915 | rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap); |
| 916 | assert(rc == 0); |
| 917 | |
| 918 | scratch_trailer_off = boot_status_off(fap); |
| 919 | |
| 920 | flash_area_close(fap); |
| 921 | |
| 922 | rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap); |
| 923 | assert(rc == 0); |
| 924 | |
| 925 | /* copy current status that is being maintained in scratch */ |
| 926 | rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0, |
Marti Bolivar | e10a739 | 2017-06-14 16:20:07 -0400 | [diff] [blame] | 927 | scratch_trailer_off, |
| 928 | img_off + copy_sz + BOOT_MAGIC_SZ, |
| 929 | BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data)); |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 930 | assert(rc == 0); |
| 931 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 932 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, |
| 933 | &swap_state); |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 934 | assert(rc == 0); |
| 935 | |
Fabio Utzig | de8a38a | 2017-05-23 11:15:01 -0400 | [diff] [blame] | 936 | if (swap_state.image_ok == BOOT_FLAG_SET) { |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 937 | rc = boot_write_image_ok(fap); |
| 938 | assert(rc == 0); |
| 939 | } |
| 940 | |
| 941 | rc = boot_write_magic(fap); |
| 942 | assert(rc == 0); |
| 943 | |
| 944 | flash_area_close(fap); |
| 945 | } |
| 946 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 947 | bs->idx++; |
| 948 | bs->state = 0; |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 949 | bs->use_scratch = 0; |
Christopher Collins | 4772ac4 | 2017-02-27 20:08:01 -0800 | [diff] [blame] | 950 | rc = boot_write_status(bs); |
| 951 | assert(rc == 0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 952 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 953 | } |
Fabio Utzig | 3488eef | 2017-06-12 10:25:43 -0300 | [diff] [blame] | 954 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 955 | |
| 956 | /** |
| 957 | * Swaps the two images in flash. If a prior copy operation was interrupted |
| 958 | * by a system reset, this function completes that operation. |
| 959 | * |
| 960 | * @param bs The current boot status. This function reads |
| 961 | * this struct to determine if it is resuming |
| 962 | * an interrupted swap operation. This |
| 963 | * function writes the updated status to this |
| 964 | * function on return. |
| 965 | * |
| 966 | * @return 0 on success; nonzero on failure. |
| 967 | */ |
Fabio Utzig | 3488eef | 2017-06-12 10:25:43 -0300 | [diff] [blame] | 968 | #ifdef MCUBOOT_OVERWRITE_ONLY |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 969 | static int |
| 970 | boot_copy_image(struct boot_status *bs) |
| 971 | { |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 972 | size_t sect_count; |
| 973 | size_t sect; |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 974 | int rc; |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 975 | size_t size = 0; |
| 976 | size_t this_size; |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 977 | |
| 978 | BOOT_LOG_INF("Image upgrade slot1 -> slot0"); |
| 979 | BOOT_LOG_INF("Erasing slot0"); |
| 980 | |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 981 | sect_count = boot_img_num_sectors(&boot_data, 0); |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 982 | for (sect = 0; sect < sect_count; sect++) { |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 983 | this_size = boot_img_sector_size(&boot_data, 0, sect); |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 984 | rc = boot_erase_sector(FLASH_AREA_IMAGE_0, |
| 985 | size, |
| 986 | this_size); |
| 987 | assert(rc == 0); |
| 988 | |
| 989 | size += this_size; |
| 990 | } |
| 991 | |
| 992 | BOOT_LOG_INF("Copying slot 1 to slot 0: 0x%x bytes", |
| 993 | size); |
| 994 | rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_0, |
| 995 | 0, 0, size); |
| 996 | |
| 997 | /* Erase slot 1 so that we don't do the upgrade on every boot. |
| 998 | * TODO: Perhaps verify slot 0's signature again? */ |
| 999 | rc = boot_erase_sector(FLASH_AREA_IMAGE_1, |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 1000 | 0, boot_img_sector_size(&boot_data, 1, 0)); |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 1001 | assert(rc == 0); |
| 1002 | |
| 1003 | return 0; |
| 1004 | } |
| 1005 | #else |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1006 | static int |
| 1007 | boot_copy_image(struct boot_status *bs) |
| 1008 | { |
| 1009 | uint32_t sz; |
| 1010 | int first_sector_idx; |
| 1011 | int last_sector_idx; |
| 1012 | int swap_idx; |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1013 | struct image_header *hdr; |
| 1014 | uint32_t size; |
| 1015 | uint32_t copy_size; |
| 1016 | struct image_header tmp_hdr; |
| 1017 | int rc; |
| 1018 | |
| 1019 | /* FIXME: just do this if asked by user? */ |
| 1020 | |
| 1021 | size = copy_size = 0; |
| 1022 | |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 1023 | hdr = boot_img_hdr(&boot_data, 0); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1024 | if (hdr->ih_magic == IMAGE_MAGIC) { |
| 1025 | copy_size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size; |
| 1026 | } |
| 1027 | |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 1028 | hdr = boot_img_hdr(&boot_data, 1); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1029 | if (hdr->ih_magic == IMAGE_MAGIC) { |
| 1030 | size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size; |
| 1031 | } |
| 1032 | |
| 1033 | if (!size || !copy_size || size == copy_size) { |
| 1034 | rc = boot_read_image_header(2, &tmp_hdr); |
| 1035 | assert(rc == 0); |
| 1036 | |
| 1037 | hdr = &tmp_hdr; |
| 1038 | if (hdr->ih_magic == IMAGE_MAGIC) { |
| 1039 | if (!size) { |
| 1040 | size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size; |
| 1041 | } else { |
| 1042 | copy_size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size; |
| 1043 | } |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | if (size > copy_size) { |
| 1048 | copy_size = size; |
| 1049 | } |
| 1050 | |
| 1051 | size = 0; |
| 1052 | last_sector_idx = 0; |
| 1053 | while (1) { |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 1054 | size += boot_img_sector_size(&boot_data, 0, last_sector_idx); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1055 | if (size >= copy_size) { |
| 1056 | break; |
| 1057 | } |
| 1058 | last_sector_idx++; |
| 1059 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1060 | |
| 1061 | swap_idx = 0; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1062 | while (last_sector_idx >= 0) { |
| 1063 | sz = boot_copy_sz(last_sector_idx, &first_sector_idx); |
| 1064 | if (swap_idx >= bs->idx) { |
| 1065 | boot_swap_sectors(first_sector_idx, sz, bs); |
| 1066 | } |
| 1067 | |
| 1068 | last_sector_idx = first_sector_idx - 1; |
| 1069 | swap_idx++; |
| 1070 | } |
| 1071 | |
| 1072 | return 0; |
| 1073 | } |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 1074 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1075 | |
| 1076 | /** |
| 1077 | * Marks a test image in slot 0 as fully copied. |
| 1078 | */ |
| 1079 | static int |
| 1080 | boot_finalize_test_swap(void) |
| 1081 | { |
| 1082 | const struct flash_area *fap; |
| 1083 | int rc; |
| 1084 | |
| 1085 | rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap); |
| 1086 | if (rc != 0) { |
| 1087 | return BOOT_EFLASH; |
| 1088 | } |
| 1089 | |
| 1090 | rc = boot_write_copy_done(fap); |
| 1091 | if (rc != 0) { |
| 1092 | return rc; |
| 1093 | } |
| 1094 | |
| 1095 | return 0; |
| 1096 | } |
| 1097 | |
| 1098 | /** |
| 1099 | * Marks a reverted image in slot 0 as confirmed. This is necessary to ensure |
| 1100 | * the status bytes from the image revert operation don't get processed on a |
| 1101 | * subsequent boot. |
| 1102 | */ |
| 1103 | static int |
| 1104 | boot_finalize_revert_swap(void) |
| 1105 | { |
| 1106 | const struct flash_area *fap; |
| 1107 | struct boot_swap_state state_slot0; |
| 1108 | int rc; |
| 1109 | |
| 1110 | rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap); |
| 1111 | if (rc != 0) { |
| 1112 | return BOOT_EFLASH; |
| 1113 | } |
| 1114 | |
| 1115 | rc = boot_read_swap_state(fap, &state_slot0); |
| 1116 | if (rc != 0) { |
| 1117 | return BOOT_EFLASH; |
| 1118 | } |
| 1119 | |
| 1120 | if (state_slot0.magic == BOOT_MAGIC_UNSET) { |
| 1121 | rc = boot_write_magic(fap); |
| 1122 | if (rc != 0) { |
| 1123 | return rc; |
| 1124 | } |
| 1125 | } |
| 1126 | |
Fabio Utzig | de8a38a | 2017-05-23 11:15:01 -0400 | [diff] [blame] | 1127 | if (state_slot0.copy_done == BOOT_FLAG_UNSET) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1128 | rc = boot_write_copy_done(fap); |
| 1129 | if (rc != 0) { |
| 1130 | return rc; |
| 1131 | } |
| 1132 | } |
| 1133 | |
Fabio Utzig | de8a38a | 2017-05-23 11:15:01 -0400 | [diff] [blame] | 1134 | if (state_slot0.image_ok == BOOT_FLAG_UNSET) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1135 | rc = boot_write_image_ok(fap); |
| 1136 | if (rc != 0) { |
| 1137 | return rc; |
| 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | return 0; |
| 1142 | } |
| 1143 | |
| 1144 | /** |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1145 | * Performs an image swap if one is required. |
| 1146 | * |
| 1147 | * @param out_swap_type On success, the type of swap performed gets |
| 1148 | * written here. |
| 1149 | * |
| 1150 | * @return 0 on success; nonzero on failure. |
| 1151 | */ |
| 1152 | static int |
| 1153 | boot_swap_if_needed(int *out_swap_type) |
| 1154 | { |
| 1155 | struct boot_status bs; |
| 1156 | int swap_type; |
| 1157 | int rc; |
| 1158 | |
| 1159 | /* Determine if we rebooted in the middle of an image swap |
| 1160 | * operation. |
| 1161 | */ |
| 1162 | rc = boot_read_status(&bs); |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1163 | assert(rc == 0); |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1164 | if (rc != 0) { |
| 1165 | return rc; |
| 1166 | } |
| 1167 | |
| 1168 | /* If a partial swap was detected, complete it. */ |
| 1169 | if (bs.idx != 0 || bs.state != 0) { |
| 1170 | rc = boot_copy_image(&bs); |
| 1171 | assert(rc == 0); |
| 1172 | |
| 1173 | /* Extrapolate the type of the partial swap. We need this |
| 1174 | * information to know how to mark the swap complete in flash. |
| 1175 | */ |
| 1176 | swap_type = boot_previous_swap_type(); |
| 1177 | } else { |
| 1178 | swap_type = boot_validated_swap_type(); |
| 1179 | switch (swap_type) { |
| 1180 | case BOOT_SWAP_TYPE_TEST: |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 1181 | case BOOT_SWAP_TYPE_PERM: |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1182 | case BOOT_SWAP_TYPE_REVERT: |
| 1183 | rc = boot_copy_image(&bs); |
| 1184 | assert(rc == 0); |
| 1185 | break; |
| 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | *out_swap_type = swap_type; |
| 1190 | return 0; |
| 1191 | } |
| 1192 | |
| 1193 | /** |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1194 | * Prepares the booting process. This function moves images around in flash as |
| 1195 | * appropriate, and tells you what address to boot from. |
| 1196 | * |
| 1197 | * @param rsp On success, indicates how booting should occur. |
| 1198 | * |
| 1199 | * @return 0 on success; nonzero on failure. |
| 1200 | */ |
| 1201 | int |
| 1202 | boot_go(struct boot_rsp *rsp) |
| 1203 | { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1204 | int swap_type; |
Marti Bolivar | 8489865 | 2017-06-13 17:20:22 -0400 | [diff] [blame] | 1205 | size_t slot; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1206 | int rc; |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1207 | int fa_id; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1208 | |
| 1209 | /* The array of slot sectors are defined here (as opposed to file scope) so |
| 1210 | * that they don't get allocated for non-boot-loader apps. This is |
| 1211 | * necessary because the gcc option "-fdata-sections" doesn't seem to have |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1212 | * any effect in older gcc versions (e.g., 4.8.4). |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1213 | */ |
Marti Bolivar | c50926f | 2017-06-14 09:35:40 -0400 | [diff] [blame] | 1214 | static boot_sector_t slot0_sectors[BOOT_MAX_IMG_SECTORS]; |
| 1215 | static boot_sector_t slot1_sectors[BOOT_MAX_IMG_SECTORS]; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1216 | boot_data.imgs[0].sectors = slot0_sectors; |
| 1217 | boot_data.imgs[1].sectors = slot1_sectors; |
| 1218 | |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1219 | /* Open boot_data image areas for the duration of this call. */ |
| 1220 | for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) { |
| 1221 | fa_id = flash_area_id_from_image_slot(slot); |
| 1222 | rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot)); |
| 1223 | assert(rc == 0); |
| 1224 | } |
| 1225 | rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, |
| 1226 | &BOOT_SCRATCH_AREA(&boot_data)); |
| 1227 | assert(rc == 0); |
| 1228 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1229 | /* Determine the sector layout of the image slots and scratch area. */ |
| 1230 | rc = boot_read_sectors(); |
| 1231 | if (rc != 0) { |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1232 | goto out; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | /* Attempt to read an image header from each slot. */ |
| 1236 | rc = boot_read_image_headers(); |
| 1237 | if (rc != 0) { |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1238 | goto out; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1239 | } |
| 1240 | |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1241 | /* If the image slots aren't compatible, no swap is possible. Just boot |
| 1242 | * into slot 0. |
| 1243 | */ |
| 1244 | if (boot_slots_compatible()) { |
| 1245 | rc = boot_swap_if_needed(&swap_type); |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1246 | assert(rc == 0); |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1247 | if (rc != 0) { |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1248 | goto out; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1249 | } |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1250 | } else { |
| 1251 | swap_type = BOOT_SWAP_TYPE_NONE; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1252 | } |
| 1253 | |
| 1254 | switch (swap_type) { |
| 1255 | case BOOT_SWAP_TYPE_NONE: |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 1256 | #ifdef MCUBOOT_VALIDATE_SLOT0 |
David Brown | d930ec6 | 2016-12-14 07:59:48 -0700 | [diff] [blame] | 1257 | rc = boot_validate_slot(0); |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1258 | assert(rc == 0); |
David Brown | d930ec6 | 2016-12-14 07:59:48 -0700 | [diff] [blame] | 1259 | if (rc != 0) { |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1260 | rc = BOOT_EBADIMAGE; |
| 1261 | goto out; |
David Brown | d930ec6 | 2016-12-14 07:59:48 -0700 | [diff] [blame] | 1262 | } |
| 1263 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1264 | slot = 0; |
| 1265 | break; |
| 1266 | |
| 1267 | case BOOT_SWAP_TYPE_TEST: |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 1268 | case BOOT_SWAP_TYPE_PERM: |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1269 | slot = 1; |
| 1270 | boot_finalize_test_swap(); |
| 1271 | break; |
| 1272 | |
| 1273 | case BOOT_SWAP_TYPE_REVERT: |
| 1274 | slot = 1; |
| 1275 | boot_finalize_revert_swap(); |
| 1276 | break; |
| 1277 | |
| 1278 | case BOOT_SWAP_TYPE_FAIL: |
| 1279 | /* The image in slot 1 was invalid and is now erased. Ensure we don't |
| 1280 | * try to boot into it again on the next reboot. Do this by pretending |
| 1281 | * we just reverted back to slot 0. |
| 1282 | */ |
| 1283 | slot = 0; |
| 1284 | boot_finalize_revert_swap(); |
| 1285 | break; |
| 1286 | |
| 1287 | default: |
| 1288 | assert(0); |
| 1289 | slot = 0; |
| 1290 | break; |
| 1291 | } |
| 1292 | |
| 1293 | /* Always boot from the primary slot. */ |
Marti Bolivar | 428cdbf | 2017-05-01 22:32:42 -0400 | [diff] [blame] | 1294 | rsp->br_flash_dev_id = boot_img_fa_device_id(&boot_data, 0); |
Marti Bolivar | 88f48d9 | 2017-05-01 22:30:02 -0400 | [diff] [blame] | 1295 | rsp->br_image_off = boot_img_slot_off(&boot_data, 0); |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 1296 | rsp->br_hdr = boot_img_hdr(&boot_data, slot); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1297 | |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1298 | out: |
| 1299 | flash_area_close(BOOT_SCRATCH_AREA(&boot_data)); |
| 1300 | for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) { |
| 1301 | flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot)); |
| 1302 | } |
| 1303 | return rc; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | int |
| 1307 | split_go(int loader_slot, int split_slot, void **entry) |
| 1308 | { |
Marti Bolivar | c50926f | 2017-06-14 09:35:40 -0400 | [diff] [blame] | 1309 | boot_sector_t *sectors; |
Christopher Collins | 034a620 | 2017-01-11 12:19:37 -0800 | [diff] [blame] | 1310 | uintptr_t entry_val; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1311 | int loader_flash_id; |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1312 | int split_flash_id; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1313 | int rc; |
| 1314 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1315 | sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors); |
| 1316 | if (sectors == NULL) { |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1317 | return SPLIT_GO_ERR; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1318 | } |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1319 | boot_data.imgs[loader_slot].sectors = sectors + 0; |
| 1320 | boot_data.imgs[split_slot].sectors = sectors + BOOT_MAX_IMG_SECTORS; |
| 1321 | |
| 1322 | loader_flash_id = flash_area_id_from_image_slot(loader_slot); |
| 1323 | rc = flash_area_open(loader_flash_id, |
| 1324 | &BOOT_IMG_AREA(&boot_data, split_slot)); |
| 1325 | assert(rc == 0); |
| 1326 | split_flash_id = flash_area_id_from_image_slot(split_slot); |
| 1327 | rc = flash_area_open(split_flash_id, |
| 1328 | &BOOT_IMG_AREA(&boot_data, split_slot)); |
| 1329 | assert(rc == 0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1330 | |
| 1331 | /* Determine the sector layout of the image slots and scratch area. */ |
| 1332 | rc = boot_read_sectors(); |
| 1333 | if (rc != 0) { |
| 1334 | rc = SPLIT_GO_ERR; |
| 1335 | goto done; |
| 1336 | } |
| 1337 | |
| 1338 | rc = boot_read_image_headers(); |
| 1339 | if (rc != 0) { |
| 1340 | goto done; |
| 1341 | } |
| 1342 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1343 | /* Don't check the bootable image flag because we could really call a |
| 1344 | * bootable or non-bootable image. Just validate that the image check |
| 1345 | * passes which is distinct from the normal check. |
| 1346 | */ |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 1347 | rc = split_image_check(boot_img_hdr(&boot_data, split_slot), |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1348 | BOOT_IMG_AREA(&boot_data, split_slot), |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 1349 | boot_img_hdr(&boot_data, loader_slot), |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1350 | BOOT_IMG_AREA(&boot_data, loader_slot)); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1351 | if (rc != 0) { |
| 1352 | rc = SPLIT_GO_NON_MATCHING; |
| 1353 | goto done; |
| 1354 | } |
| 1355 | |
Marti Bolivar | ea08887 | 2017-06-12 17:10:49 -0400 | [diff] [blame] | 1356 | entry_val = boot_img_slot_off(&boot_data, split_slot) + |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 1357 | boot_img_hdr(&boot_data, split_slot)->ih_hdr_size; |
Christopher Collins | 034a620 | 2017-01-11 12:19:37 -0800 | [diff] [blame] | 1358 | *entry = (void *) entry_val; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1359 | rc = SPLIT_GO_OK; |
| 1360 | |
| 1361 | done: |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1362 | flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot)); |
| 1363 | flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot)); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1364 | free(sectors); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1365 | return rc; |
| 1366 | } |