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