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