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