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