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