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