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