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