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 | |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 20 | /* |
| 21 | * Modifications are Copyright (c) 2019 Arm Limited. |
| 22 | */ |
| 23 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 24 | /** |
| 25 | * This file provides an interface to the boot loader. Functions defined in |
| 26 | * this file should only be called while the boot loader is running. |
| 27 | */ |
| 28 | |
| 29 | #include <assert.h> |
| 30 | #include <stddef.h> |
David Brown | 52eee56 | 2017-07-05 11:25:09 -0600 | [diff] [blame] | 31 | #include <stdbool.h> |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 32 | #include <inttypes.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <string.h> |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 35 | #include <os/os_malloc.h> |
| 36 | #include "bootutil/bootutil.h" |
| 37 | #include "bootutil/image.h" |
| 38 | #include "bootutil_priv.h" |
Marti Bolivar | fd20c76 | 2017-02-07 16:52:50 -0500 | [diff] [blame] | 39 | #include "bootutil/bootutil_log.h" |
| 40 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 41 | #ifdef MCUBOOT_ENC_IMAGES |
| 42 | #include "bootutil/enc_key.h" |
| 43 | #endif |
| 44 | |
Fabio Utzig | ba1fbe6 | 2017-07-21 14:01:20 -0300 | [diff] [blame] | 45 | #include "mcuboot_config/mcuboot_config.h" |
Fabio Utzig | eed80b6 | 2017-06-10 08:03:05 -0300 | [diff] [blame] | 46 | |
Emanuele Di Santo | 9f1933d | 2018-11-20 10:59:59 +0100 | [diff] [blame] | 47 | MCUBOOT_LOG_MODULE_DECLARE(mcuboot); |
| 48 | |
Marti Bolivar | 9b1f8bb | 2017-06-12 15:24:13 -0400 | [diff] [blame] | 49 | static struct boot_loader_state boot_data; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 50 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 51 | #if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT) && !defined(MCUBOOT_OVERWRITE_ONLY) |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 52 | static int boot_status_fails = 0; |
| 53 | #define BOOT_STATUS_ASSERT(x) \ |
| 54 | do { \ |
Johann Fischer | ed8461b | 2018-02-15 16:50:31 +0100 | [diff] [blame] | 55 | if (!(x)) { \ |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 56 | boot_status_fails++; \ |
| 57 | } \ |
| 58 | } while (0) |
| 59 | #else |
Fabio Utzig | 57c40f7 | 2017-12-12 21:48:30 -0200 | [diff] [blame] | 60 | #define BOOT_STATUS_ASSERT(x) ASSERT(x) |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 61 | #endif |
| 62 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 63 | struct boot_status_table { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 64 | uint8_t bst_magic_primary_slot; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 65 | uint8_t bst_magic_scratch; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 66 | uint8_t bst_copy_done_primary_slot; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 67 | uint8_t bst_status_source; |
| 68 | }; |
| 69 | |
| 70 | /** |
| 71 | * This set of tables maps swap state contents to boot status location. |
| 72 | * When searching for a match, these tables must be iterated in order. |
| 73 | */ |
| 74 | static const struct boot_status_table boot_status_tables[] = { |
| 75 | { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 76 | /* | primary slot | scratch | |
| 77 | * ----------+--------------+--------------| |
| 78 | * magic | Good | Any | |
| 79 | * copy-done | Set | N/A | |
| 80 | * ----------+--------------+--------------' |
| 81 | * source: none | |
| 82 | * ----------------------------------------' |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 83 | */ |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 84 | .bst_magic_primary_slot = BOOT_MAGIC_GOOD, |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 85 | .bst_magic_scratch = BOOT_MAGIC_NOTGOOD, |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 86 | .bst_copy_done_primary_slot = BOOT_FLAG_SET, |
| 87 | .bst_status_source = BOOT_STATUS_SOURCE_NONE, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 88 | }, |
| 89 | |
| 90 | { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 91 | /* | primary slot | scratch | |
| 92 | * ----------+--------------+--------------| |
| 93 | * magic | Good | Any | |
| 94 | * copy-done | Unset | N/A | |
| 95 | * ----------+--------------+--------------' |
| 96 | * source: primary slot | |
| 97 | * ----------------------------------------' |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 98 | */ |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 99 | .bst_magic_primary_slot = BOOT_MAGIC_GOOD, |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 100 | .bst_magic_scratch = BOOT_MAGIC_NOTGOOD, |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 101 | .bst_copy_done_primary_slot = BOOT_FLAG_UNSET, |
| 102 | .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 103 | }, |
| 104 | |
| 105 | { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 106 | /* | primary slot | scratch | |
| 107 | * ----------+--------------+--------------| |
| 108 | * magic | Any | Good | |
| 109 | * copy-done | Any | N/A | |
| 110 | * ----------+--------------+--------------' |
| 111 | * source: scratch | |
| 112 | * ----------------------------------------' |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 113 | */ |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 114 | .bst_magic_primary_slot = BOOT_MAGIC_ANY, |
| 115 | .bst_magic_scratch = BOOT_MAGIC_GOOD, |
| 116 | .bst_copy_done_primary_slot = BOOT_FLAG_ANY, |
| 117 | .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 118 | }, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 119 | { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 120 | /* | primary slot | scratch | |
| 121 | * ----------+--------------+--------------| |
| 122 | * magic | Unset | Any | |
| 123 | * copy-done | Unset | N/A | |
| 124 | * ----------+--------------+--------------| |
| 125 | * source: varies | |
| 126 | * ----------------------------------------+--------------------------+ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 127 | * This represents one of two cases: | |
| 128 | * o No swaps ever (no status to read, so no harm in checking). | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 129 | * o Mid-revert; status in primary slot. | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 130 | * -------------------------------------------------------------------' |
| 131 | */ |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 132 | .bst_magic_primary_slot = BOOT_MAGIC_UNSET, |
| 133 | .bst_magic_scratch = BOOT_MAGIC_ANY, |
| 134 | .bst_copy_done_primary_slot = BOOT_FLAG_UNSET, |
| 135 | .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 136 | }, |
| 137 | }; |
| 138 | |
| 139 | #define BOOT_STATUS_TABLES_COUNT \ |
| 140 | (sizeof boot_status_tables / sizeof boot_status_tables[0]) |
| 141 | |
Marti Bolivar | fd20c76 | 2017-02-07 16:52:50 -0500 | [diff] [blame] | 142 | #define BOOT_LOG_SWAP_STATE(area, state) \ |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 143 | BOOT_LOG_INF("%s: magic=%s, swap_type=0x%x, copy_done=0x%x, " \ |
| 144 | "image_ok=0x%x", \ |
Marti Bolivar | fd20c76 | 2017-02-07 16:52:50 -0500 | [diff] [blame] | 145 | (area), \ |
| 146 | ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \ |
| 147 | (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \ |
| 148 | "bad"), \ |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 149 | (state)->swap_type, \ |
Marti Bolivar | fd20c76 | 2017-02-07 16:52:50 -0500 | [diff] [blame] | 150 | (state)->copy_done, \ |
| 151 | (state)->image_ok) |
| 152 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 153 | /** |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 154 | * Determines where in flash the most recent boot status is stored. The boot |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 155 | * status is necessary for completing a swap that was interrupted by a boot |
| 156 | * loader reset. |
| 157 | * |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 158 | * @return A BOOT_STATUS_SOURCE_[...] code indicating where status should |
| 159 | * be read from. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 160 | */ |
| 161 | static int |
| 162 | boot_status_source(void) |
| 163 | { |
| 164 | const struct boot_status_table *table; |
| 165 | struct boot_swap_state state_scratch; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 166 | struct boot_swap_state state_primary_slot; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 167 | int rc; |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 168 | size_t i; |
Marti Bolivar | fd20c76 | 2017-02-07 16:52:50 -0500 | [diff] [blame] | 169 | uint8_t source; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 170 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 171 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY, |
| 172 | &state_primary_slot); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 173 | assert(rc == 0); |
| 174 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 175 | 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] | 176 | assert(rc == 0); |
| 177 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 178 | BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot); |
Marti Bolivar | fd20c76 | 2017-02-07 16:52:50 -0500 | [diff] [blame] | 179 | BOOT_LOG_SWAP_STATE("Scratch", &state_scratch); |
| 180 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 181 | for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) { |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 182 | table = &boot_status_tables[i]; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 183 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 184 | if (boot_magic_compatible_check(table->bst_magic_primary_slot, |
| 185 | state_primary_slot.magic) && |
| 186 | boot_magic_compatible_check(table->bst_magic_scratch, |
| 187 | state_scratch.magic) && |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 188 | (table->bst_copy_done_primary_slot == BOOT_FLAG_ANY || |
| 189 | table->bst_copy_done_primary_slot == state_primary_slot.copy_done)) |
| 190 | { |
Marti Bolivar | fd20c76 | 2017-02-07 16:52:50 -0500 | [diff] [blame] | 191 | source = table->bst_status_source; |
| 192 | BOOT_LOG_INF("Boot source: %s", |
| 193 | source == BOOT_STATUS_SOURCE_NONE ? "none" : |
| 194 | source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" : |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 195 | source == BOOT_STATUS_SOURCE_PRIMARY_SLOT ? |
| 196 | "primary slot" : "BUG; can't happen"); |
Marti Bolivar | fd20c76 | 2017-02-07 16:52:50 -0500 | [diff] [blame] | 197 | return source; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
Marti Bolivar | fd20c76 | 2017-02-07 16:52:50 -0500 | [diff] [blame] | 201 | BOOT_LOG_INF("Boot source: none"); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 202 | return BOOT_STATUS_SOURCE_NONE; |
| 203 | } |
| 204 | |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 205 | /* |
| 206 | * Compute the total size of the given image. Includes the size of |
| 207 | * the TLVs. |
| 208 | */ |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 209 | #if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST) |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 210 | static int |
| 211 | boot_read_image_size(int slot, struct image_header *hdr, uint32_t *size) |
| 212 | { |
| 213 | const struct flash_area *fap; |
| 214 | struct image_tlv_info info; |
| 215 | int area_id; |
| 216 | int rc; |
| 217 | |
| 218 | area_id = flash_area_id_from_image_slot(slot); |
| 219 | rc = flash_area_open(area_id, &fap); |
| 220 | if (rc != 0) { |
| 221 | rc = BOOT_EFLASH; |
| 222 | goto done; |
| 223 | } |
| 224 | |
| 225 | rc = flash_area_read(fap, hdr->ih_hdr_size + hdr->ih_img_size, |
| 226 | &info, sizeof(info)); |
| 227 | if (rc != 0) { |
| 228 | rc = BOOT_EFLASH; |
| 229 | goto done; |
| 230 | } |
| 231 | if (info.it_magic != IMAGE_TLV_INFO_MAGIC) { |
| 232 | rc = BOOT_EBADIMAGE; |
| 233 | goto done; |
| 234 | } |
| 235 | *size = hdr->ih_hdr_size + hdr->ih_img_size + info.it_tlv_tot; |
| 236 | rc = 0; |
| 237 | |
| 238 | done: |
| 239 | flash_area_close(fap); |
Fabio Utzig | 2eebf11 | 2017-09-04 15:25:08 -0300 | [diff] [blame] | 240 | return rc; |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 241 | } |
Fabio Utzig | 36ec0e7 | 2017-09-05 08:10:33 -0300 | [diff] [blame] | 242 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 243 | |
Fabio Utzig | c08ed21 | 2017-06-20 19:28:36 -0300 | [diff] [blame] | 244 | static int |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 245 | boot_read_image_header(int slot, struct image_header *out_hdr) |
| 246 | { |
| 247 | const struct flash_area *fap; |
| 248 | int area_id; |
| 249 | int rc; |
| 250 | |
| 251 | area_id = flash_area_id_from_image_slot(slot); |
| 252 | rc = flash_area_open(area_id, &fap); |
| 253 | if (rc != 0) { |
| 254 | rc = BOOT_EFLASH; |
| 255 | goto done; |
| 256 | } |
| 257 | |
| 258 | rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr); |
| 259 | if (rc != 0) { |
| 260 | rc = BOOT_EFLASH; |
| 261 | goto done; |
| 262 | } |
| 263 | |
| 264 | rc = 0; |
| 265 | |
| 266 | done: |
| 267 | flash_area_close(fap); |
| 268 | return rc; |
| 269 | } |
| 270 | |
| 271 | static int |
Fabio Utzig | 9c25fa7 | 2017-12-12 14:57:20 -0200 | [diff] [blame] | 272 | boot_read_image_headers(bool require_all) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 273 | { |
| 274 | int rc; |
| 275 | int i; |
| 276 | |
| 277 | for (i = 0; i < BOOT_NUM_SLOTS; i++) { |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 278 | rc = boot_read_image_header(i, boot_img_hdr(&boot_data, i)); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 279 | if (rc != 0) { |
Fabio Utzig | 9c25fa7 | 2017-12-12 14:57:20 -0200 | [diff] [blame] | 280 | /* If `require_all` is set, fail on any single fail, otherwise |
| 281 | * if at least the first slot's header was read successfully, |
| 282 | * then the boot loader can attempt a boot. |
| 283 | * |
| 284 | * Failure to read any headers is a fatal error. |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 285 | */ |
Fabio Utzig | 9c25fa7 | 2017-12-12 14:57:20 -0200 | [diff] [blame] | 286 | if (i > 0 && !require_all) { |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 287 | return 0; |
| 288 | } else { |
| 289 | return rc; |
| 290 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static uint8_t |
| 298 | boot_write_sz(void) |
| 299 | { |
| 300 | uint8_t elem_sz; |
| 301 | uint8_t align; |
| 302 | |
| 303 | /* Figure out what size to write update status update as. The size depends |
| 304 | * on what the minimum write size is for scratch area, active image slot. |
| 305 | * We need to use the bigger of those 2 values. |
| 306 | */ |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 307 | elem_sz = flash_area_align(boot_data.imgs[BOOT_PRIMARY_SLOT].area); |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 308 | align = flash_area_align(boot_data.scratch.area); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 309 | if (align > elem_sz) { |
| 310 | elem_sz = align; |
| 311 | } |
| 312 | |
| 313 | return elem_sz; |
| 314 | } |
| 315 | |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 316 | /* |
| 317 | * Slots are compatible when all sectors that store upto to size of the image |
| 318 | * round up to sector size, in both slot's are able to fit in the scratch |
| 319 | * area, and have sizes that are a multiple of each other (powers of two |
| 320 | * presumably!). |
| 321 | */ |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 322 | static int |
| 323 | boot_slots_compatible(void) |
| 324 | { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 325 | size_t num_sectors_primary; |
| 326 | size_t num_sectors_secondary; |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 327 | size_t sz0, sz1; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 328 | size_t primary_slot_sz, secondary_slot_sz; |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 329 | size_t scratch_sz; |
| 330 | size_t i, j; |
| 331 | int8_t smaller; |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 332 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 333 | num_sectors_primary = |
| 334 | boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT); |
| 335 | num_sectors_secondary = |
| 336 | boot_img_num_sectors(&boot_data, BOOT_SECONDARY_SLOT); |
| 337 | if ((num_sectors_primary > BOOT_MAX_IMG_SECTORS) || |
| 338 | (num_sectors_secondary > BOOT_MAX_IMG_SECTORS)) { |
Fabio Utzig | a1fae67 | 2018-03-30 10:52:38 -0300 | [diff] [blame] | 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 | |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 343 | scratch_sz = boot_scratch_area_size(&boot_data); |
| 344 | |
| 345 | /* |
| 346 | * The following loop scans all sectors in a linear fashion, assuring that |
| 347 | * for each possible sector in each slot, it is able to fit in the other |
| 348 | * slot's sector or sectors. Slot's should be compatible as long as any |
| 349 | * number of a slot's sectors are able to fit into another, which only |
| 350 | * excludes cases where sector sizes are not a multiple of each other. |
| 351 | */ |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 352 | i = sz0 = primary_slot_sz = 0; |
| 353 | j = sz1 = secondary_slot_sz = 0; |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 354 | smaller = 0; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 355 | while (i < num_sectors_primary || j < num_sectors_secondary) { |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 356 | if (sz0 == sz1) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 357 | sz0 += boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i); |
| 358 | sz1 += boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT, j); |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 359 | i++; |
| 360 | j++; |
| 361 | } else if (sz0 < sz1) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 362 | sz0 += boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i); |
| 363 | /* Guarantee that multiple sectors of the secondary slot |
| 364 | * fit into the primary slot. |
| 365 | */ |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 366 | if (smaller == 2) { |
| 367 | BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors"); |
| 368 | return 0; |
| 369 | } |
| 370 | smaller = 1; |
| 371 | i++; |
| 372 | } else { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 373 | sz1 += boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT, j); |
| 374 | /* Guarantee that multiple sectors of the primary slot |
| 375 | * fit into the secondary slot. |
| 376 | */ |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 377 | if (smaller == 1) { |
| 378 | BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors"); |
| 379 | return 0; |
| 380 | } |
| 381 | smaller = 2; |
| 382 | j++; |
| 383 | } |
| 384 | if (sz0 == sz1) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 385 | primary_slot_sz += sz0; |
| 386 | secondary_slot_sz += sz1; |
| 387 | /* Scratch has to fit each swap operation to the size of the larger |
| 388 | * sector among the primary slot and the secondary slot. |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 389 | */ |
| 390 | if (sz0 > scratch_sz || sz1 > scratch_sz) { |
| 391 | BOOT_LOG_WRN("Cannot upgrade: not all sectors fit inside scratch"); |
| 392 | return 0; |
| 393 | } |
| 394 | smaller = sz0 = sz1 = 0; |
| 395 | } |
Fabio Utzig | a1fae67 | 2018-03-30 10:52:38 -0300 | [diff] [blame] | 396 | } |
| 397 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 398 | if ((i != num_sectors_primary) || |
| 399 | (j != num_sectors_secondary) || |
| 400 | (primary_slot_sz != secondary_slot_sz)) { |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 401 | BOOT_LOG_WRN("Cannot upgrade: slots are not compatible"); |
| 402 | return 0; |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | return 1; |
| 406 | } |
| 407 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 408 | /** |
| 409 | * Determines the sector layout of both image slots and the scratch area. |
| 410 | * This information is necessary for calculating the number of bytes to erase |
| 411 | * and copy during an image swap. The information collected during this |
| 412 | * function is used to populate the boot_data global. |
| 413 | */ |
| 414 | static int |
| 415 | boot_read_sectors(void) |
| 416 | { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 417 | int rc; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 418 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 419 | rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_PRIMARY); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 420 | if (rc != 0) { |
| 421 | return BOOT_EFLASH; |
| 422 | } |
| 423 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 424 | rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_SECONDARY); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 425 | if (rc != 0) { |
| 426 | return BOOT_EFLASH; |
| 427 | } |
| 428 | |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 429 | rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_SCRATCH); |
| 430 | if (rc != 0) { |
| 431 | return BOOT_EFLASH; |
| 432 | } |
| 433 | |
Marti Bolivar | e10a739 | 2017-06-14 16:20:07 -0400 | [diff] [blame] | 434 | BOOT_WRITE_SZ(&boot_data) = boot_write_sz(); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 435 | |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | static uint32_t |
| 440 | boot_status_internal_off(int idx, int state, int elem_sz) |
| 441 | { |
| 442 | int idx_sz; |
| 443 | |
| 444 | idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT; |
| 445 | |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 446 | return (idx - BOOT_STATUS_IDX_0) * idx_sz + |
| 447 | (state - BOOT_STATUS_STATE_0) * elem_sz; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | /** |
| 451 | * Reads the status of a partially-completed swap, if any. This is necessary |
| 452 | * to recover in case the boot lodaer was reset in the middle of a swap |
| 453 | * operation. |
| 454 | */ |
| 455 | static int |
| 456 | boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs) |
| 457 | { |
| 458 | uint32_t off; |
| 459 | uint8_t status; |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 460 | int max_entries; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 461 | int found; |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 462 | int found_idx; |
| 463 | int invalid; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 464 | int rc; |
| 465 | int i; |
| 466 | |
| 467 | off = boot_status_off(fap); |
Fabio Utzig | 4cee4f7 | 2017-05-22 10:59:57 -0400 | [diff] [blame] | 468 | max_entries = boot_status_entries(fap); |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 469 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 470 | found = 0; |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 471 | found_idx = 0; |
| 472 | invalid = 0; |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 473 | for (i = 0; i < max_entries; i++) { |
Fabio Utzig | 178be54 | 2018-09-19 08:12:56 -0300 | [diff] [blame] | 474 | rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(&boot_data), |
| 475 | &status, 1); |
| 476 | if (rc < 0) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 477 | return BOOT_EFLASH; |
| 478 | } |
| 479 | |
Fabio Utzig | 178be54 | 2018-09-19 08:12:56 -0300 | [diff] [blame] | 480 | if (rc == 1) { |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 481 | if (found && !found_idx) { |
| 482 | found_idx = i; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 483 | } |
| 484 | } else if (!found) { |
| 485 | found = 1; |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 486 | } else if (found_idx) { |
| 487 | invalid = 1; |
| 488 | break; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 492 | if (invalid) { |
| 493 | /* This means there was an error writing status on the last |
| 494 | * swap. Tell user and move on to validation! |
| 495 | */ |
| 496 | BOOT_LOG_ERR("Detected inconsistent status!"); |
| 497 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 498 | #if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT) |
| 499 | /* With validation of the primary slot disabled, there is no way |
| 500 | * to be sure the swapped primary slot is OK, so abort! |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 501 | */ |
| 502 | assert(0); |
| 503 | #endif |
| 504 | } |
| 505 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 506 | if (found) { |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 507 | if (!found_idx) { |
| 508 | found_idx = i; |
| 509 | } |
| 510 | found_idx--; |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 511 | bs->idx = (found_idx / BOOT_STATUS_STATE_COUNT) + 1; |
| 512 | bs->state = (found_idx % BOOT_STATUS_STATE_COUNT) + 1; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | /** |
| 519 | * Reads the boot status from the flash. The boot status contains |
| 520 | * the current state of an interrupted image copy operation. If the boot |
| 521 | * status is not present, or it indicates that previous copy finished, |
| 522 | * there is no operation in progress. |
| 523 | */ |
| 524 | static int |
| 525 | boot_read_status(struct boot_status *bs) |
| 526 | { |
| 527 | const struct flash_area *fap; |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 528 | uint32_t off; |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 529 | uint8_t swap_info; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 530 | int status_loc; |
| 531 | int area_id; |
| 532 | int rc; |
| 533 | |
| 534 | memset(bs, 0, sizeof *bs); |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 535 | bs->idx = BOOT_STATUS_IDX_0; |
| 536 | bs->state = BOOT_STATUS_STATE_0; |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 537 | bs->swap_type = BOOT_SWAP_TYPE_NONE; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 538 | |
Fabio Utzig | 03dc9a0 | 2018-06-11 12:24:07 -0700 | [diff] [blame] | 539 | #ifdef MCUBOOT_OVERWRITE_ONLY |
| 540 | /* Overwrite-only doesn't make use of the swap status area. */ |
| 541 | return 0; |
| 542 | #endif |
| 543 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 544 | status_loc = boot_status_source(); |
| 545 | switch (status_loc) { |
| 546 | case BOOT_STATUS_SOURCE_NONE: |
| 547 | return 0; |
| 548 | |
| 549 | case BOOT_STATUS_SOURCE_SCRATCH: |
| 550 | area_id = FLASH_AREA_IMAGE_SCRATCH; |
| 551 | break; |
| 552 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 553 | case BOOT_STATUS_SOURCE_PRIMARY_SLOT: |
| 554 | area_id = FLASH_AREA_IMAGE_PRIMARY; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 555 | break; |
| 556 | |
| 557 | default: |
| 558 | assert(0); |
| 559 | return BOOT_EBADARGS; |
| 560 | } |
| 561 | |
| 562 | rc = flash_area_open(area_id, &fap); |
| 563 | if (rc != 0) { |
| 564 | return BOOT_EFLASH; |
| 565 | } |
| 566 | |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 567 | rc = boot_read_status_bytes(fap, bs); |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 568 | if (rc == 0) { |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 569 | off = boot_swap_info_off(fap); |
| 570 | rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info); |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 571 | if (rc == 1) { |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 572 | BOOT_SET_SWAP_INFO(swap_info, 0, BOOT_SWAP_TYPE_NONE); |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 573 | rc = 0; |
| 574 | } |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 575 | |
| 576 | /* Extract the swap type info */ |
| 577 | bs->swap_type = BOOT_GET_SWAP_TYPE(swap_info); |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 578 | } |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 579 | |
| 580 | flash_area_close(fap); |
Fabio Utzig | 03dc9a0 | 2018-06-11 12:24:07 -0700 | [diff] [blame] | 581 | |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 582 | return rc; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | /** |
| 586 | * Writes the supplied boot status to the flash file system. The boot status |
| 587 | * contains the current state of an in-progress image copy operation. |
| 588 | * |
| 589 | * @param bs The boot status to write. |
| 590 | * |
| 591 | * @return 0 on success; nonzero on failure. |
| 592 | */ |
| 593 | int |
| 594 | boot_write_status(struct boot_status *bs) |
| 595 | { |
| 596 | const struct flash_area *fap; |
| 597 | uint32_t off; |
| 598 | int area_id; |
| 599 | int rc; |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 600 | uint8_t buf[BOOT_MAX_ALIGN]; |
David Brown | 9d72546 | 2017-01-23 15:50:58 -0700 | [diff] [blame] | 601 | uint8_t align; |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 602 | uint8_t erased_val; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 603 | |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 604 | /* NOTE: The first sector copied (that is the last sector on slot) contains |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 605 | * the trailer. Since in the last step the primary slot is erased, the |
| 606 | * first two status writes go to the scratch which will be copied to |
| 607 | * the primary slot! |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 608 | */ |
| 609 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 610 | if (bs->use_scratch) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 611 | /* Write to scratch. */ |
| 612 | area_id = FLASH_AREA_IMAGE_SCRATCH; |
| 613 | } else { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 614 | /* Write to the primary slot. */ |
| 615 | area_id = FLASH_AREA_IMAGE_PRIMARY; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | rc = flash_area_open(area_id, &fap); |
| 619 | if (rc != 0) { |
| 620 | rc = BOOT_EFLASH; |
| 621 | goto done; |
| 622 | } |
| 623 | |
| 624 | off = boot_status_off(fap) + |
Marti Bolivar | e10a739 | 2017-06-14 16:20:07 -0400 | [diff] [blame] | 625 | boot_status_internal_off(bs->idx, bs->state, |
| 626 | BOOT_WRITE_SZ(&boot_data)); |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 627 | align = flash_area_align(fap); |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 628 | erased_val = flash_area_erased_val(fap); |
| 629 | memset(buf, erased_val, BOOT_MAX_ALIGN); |
David Brown | 9d72546 | 2017-01-23 15:50:58 -0700 | [diff] [blame] | 630 | buf[0] = bs->state; |
| 631 | |
| 632 | rc = flash_area_write(fap, off, buf, align); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 633 | if (rc != 0) { |
| 634 | rc = BOOT_EFLASH; |
| 635 | goto done; |
| 636 | } |
| 637 | |
| 638 | rc = 0; |
| 639 | |
| 640 | done: |
| 641 | flash_area_close(fap); |
| 642 | return rc; |
| 643 | } |
| 644 | |
| 645 | /* |
| 646 | * Validate image hash/signature in a slot. |
| 647 | */ |
| 648 | static int |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 649 | boot_image_check(struct image_header *hdr, const struct flash_area *fap, |
| 650 | struct boot_status *bs) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 651 | { |
David Brown | db1d9d3 | 2017-01-06 11:07:54 -0700 | [diff] [blame] | 652 | static uint8_t tmpbuf[BOOT_TMPBUF_SZ]; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 653 | int rc; |
| 654 | |
| 655 | #ifndef MCUBOOT_ENC_IMAGES |
| 656 | (void)bs; |
| 657 | (void)rc; |
| 658 | #else |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 659 | if ((fap->fa_id == FLASH_AREA_IMAGE_SECONDARY) && IS_ENCRYPTED(hdr)) { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 660 | rc = boot_enc_load(hdr, fap, bs->enckey[1]); |
| 661 | if (rc < 0) { |
| 662 | return BOOT_EBADIMAGE; |
| 663 | } |
| 664 | if (rc == 0 && boot_enc_set_key(1, bs->enckey[1])) { |
| 665 | return BOOT_EBADIMAGE; |
| 666 | } |
| 667 | } |
| 668 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 669 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 670 | if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ, |
| 671 | NULL, 0, NULL)) { |
| 672 | return BOOT_EBADIMAGE; |
| 673 | } |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | static int |
| 678 | split_image_check(struct image_header *app_hdr, |
| 679 | const struct flash_area *app_fap, |
| 680 | struct image_header *loader_hdr, |
| 681 | const struct flash_area *loader_fap) |
| 682 | { |
| 683 | static void *tmpbuf; |
| 684 | uint8_t loader_hash[32]; |
| 685 | |
| 686 | if (!tmpbuf) { |
| 687 | tmpbuf = malloc(BOOT_TMPBUF_SZ); |
| 688 | if (!tmpbuf) { |
| 689 | return BOOT_ENOMEM; |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | if (bootutil_img_validate(loader_hdr, loader_fap, tmpbuf, BOOT_TMPBUF_SZ, |
| 694 | NULL, 0, loader_hash)) { |
| 695 | return BOOT_EBADIMAGE; |
| 696 | } |
| 697 | |
| 698 | if (bootutil_img_validate(app_hdr, app_fap, tmpbuf, BOOT_TMPBUF_SZ, |
| 699 | loader_hash, 32, NULL)) { |
| 700 | return BOOT_EBADIMAGE; |
| 701 | } |
| 702 | |
| 703 | return 0; |
| 704 | } |
| 705 | |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 706 | /* |
| 707 | * Check that a memory area consists of a given value. |
| 708 | */ |
| 709 | static inline bool |
| 710 | boot_data_is_set_to(uint8_t val, void *data, size_t len) |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 711 | { |
| 712 | uint8_t i; |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 713 | uint8_t *p = (uint8_t *)data; |
| 714 | for (i = 0; i < len; i++) { |
| 715 | if (val != p[i]) { |
| 716 | return false; |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 717 | } |
| 718 | } |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 719 | return true; |
| 720 | } |
| 721 | |
| 722 | static int |
| 723 | boot_check_header_erased(int slot) |
| 724 | { |
| 725 | const struct flash_area *fap; |
| 726 | struct image_header *hdr; |
| 727 | uint8_t erased_val; |
| 728 | int rc; |
| 729 | |
| 730 | rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap); |
| 731 | if (rc != 0) { |
| 732 | return -1; |
| 733 | } |
| 734 | |
| 735 | erased_val = flash_area_erased_val(fap); |
| 736 | flash_area_close(fap); |
| 737 | |
| 738 | hdr = boot_img_hdr(&boot_data, slot); |
| 739 | if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) { |
| 740 | return -1; |
| 741 | } |
| 742 | |
| 743 | return 0; |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 744 | } |
| 745 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 746 | static int |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 747 | boot_validate_slot(int slot, struct boot_status *bs) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 748 | { |
| 749 | const struct flash_area *fap; |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 750 | struct image_header *hdr; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 751 | int rc; |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 752 | |
David Brown | d930ec6 | 2016-12-14 07:59:48 -0700 | [diff] [blame] | 753 | rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 754 | if (rc != 0) { |
| 755 | return BOOT_EFLASH; |
| 756 | } |
| 757 | |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 758 | hdr = boot_img_hdr(&boot_data, slot); |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 759 | if (boot_check_header_erased(slot) == 0 || (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 760 | /* No bootable image in slot; continue booting from the primary slot. */ |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 761 | rc = -1; |
| 762 | goto out; |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 763 | } |
| 764 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 765 | if ((hdr->ih_magic != IMAGE_MAGIC || boot_image_check(hdr, fap, bs) != 0)) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 766 | if (slot != BOOT_PRIMARY_SLOT) { |
David Brown | b38e044 | 2017-02-24 13:57:12 -0700 | [diff] [blame] | 767 | flash_area_erase(fap, 0, fap->fa_size); |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 768 | /* Image in the secondary slot is invalid. Erase the image and |
| 769 | * continue booting from the primary slot. |
David Brown | b38e044 | 2017-02-24 13:57:12 -0700 | [diff] [blame] | 770 | */ |
| 771 | } |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 772 | BOOT_LOG_ERR("Image in the %s slot is not valid!", |
| 773 | (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary"); |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 774 | rc = -1; |
| 775 | goto out; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 776 | } |
| 777 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 778 | /* Image in the secondary slot is valid. */ |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 779 | rc = 0; |
| 780 | |
| 781 | out: |
| 782 | flash_area_close(fap); |
| 783 | return rc; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | /** |
| 787 | * Determines which swap operation to perform, if any. If it is determined |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 788 | * that a swap operation is required, the image in the secondary slot is checked |
| 789 | * for validity. If the image in the secondary slot is invalid, it is erased, |
| 790 | * and a swap type of "none" is indicated. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 791 | * |
| 792 | * @return The type of swap to perform (BOOT_SWAP_TYPE...) |
| 793 | */ |
| 794 | static int |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 795 | boot_validated_swap_type(struct boot_status *bs) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 796 | { |
| 797 | int swap_type; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 798 | |
| 799 | swap_type = boot_swap_type(); |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 800 | switch (swap_type) { |
| 801 | case BOOT_SWAP_TYPE_TEST: |
| 802 | case BOOT_SWAP_TYPE_PERM: |
| 803 | case BOOT_SWAP_TYPE_REVERT: |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 804 | /* Boot loader wants to switch to the secondary slot. |
| 805 | * Ensure image is valid. |
| 806 | */ |
| 807 | if (boot_validate_slot(BOOT_SECONDARY_SLOT, bs) != 0) { |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 808 | swap_type = BOOT_SWAP_TYPE_FAIL; |
| 809 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | return swap_type; |
| 813 | } |
| 814 | |
| 815 | /** |
| 816 | * Calculates the number of sectors the scratch area can contain. A "last" |
| 817 | * source sector is specified because images are copied backwards in flash |
| 818 | * (final index to index number 0). |
| 819 | * |
| 820 | * @param last_sector_idx The index of the last source sector |
| 821 | * (inclusive). |
| 822 | * @param out_first_sector_idx The index of the first source sector |
| 823 | * (inclusive) gets written here. |
| 824 | * |
| 825 | * @return The number of bytes comprised by the |
| 826 | * [first-sector, last-sector] range. |
| 827 | */ |
Fabio Utzig | 3488eef | 2017-06-12 10:25:43 -0300 | [diff] [blame] | 828 | #ifndef MCUBOOT_OVERWRITE_ONLY |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 829 | static uint32_t |
| 830 | boot_copy_sz(int last_sector_idx, int *out_first_sector_idx) |
| 831 | { |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 832 | size_t scratch_sz; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 833 | uint32_t new_sz; |
| 834 | uint32_t sz; |
| 835 | int i; |
| 836 | |
| 837 | sz = 0; |
| 838 | |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 839 | scratch_sz = boot_scratch_area_size(&boot_data); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 840 | for (i = last_sector_idx; i >= 0; i--) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 841 | new_sz = sz + boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i); |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 842 | /* |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 843 | * The secondary slot is not being checked here, because |
| 844 | * `boot_slots_compatible` already provides assurance that the copy size |
| 845 | * will be compatible with the primary slot and scratch. |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 846 | */ |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 847 | if (new_sz > scratch_sz) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 848 | break; |
| 849 | } |
| 850 | sz = new_sz; |
| 851 | } |
| 852 | |
| 853 | /* i currently refers to a sector that doesn't fit or it is -1 because all |
| 854 | * sectors have been processed. In both cases, exclude sector i. |
| 855 | */ |
| 856 | *out_first_sector_idx = i + 1; |
| 857 | return sz; |
| 858 | } |
Fabio Utzig | 3488eef | 2017-06-12 10:25:43 -0300 | [diff] [blame] | 859 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 860 | |
| 861 | /** |
| 862 | * Erases a region of flash. |
| 863 | * |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 864 | * @param flash_area The flash_area containing the region to erase. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 865 | * @param off The offset within the flash area to start the |
| 866 | * erase. |
| 867 | * @param sz The number of bytes to erase. |
| 868 | * |
| 869 | * @return 0 on success; nonzero on failure. |
| 870 | */ |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 871 | static inline int |
| 872 | boot_erase_sector(const struct flash_area *fap, uint32_t off, uint32_t sz) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 873 | { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 874 | return flash_area_erase(fap, off, sz); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | /** |
| 878 | * Copies the contents of one flash region to another. You must erase the |
| 879 | * destination region prior to calling this function. |
| 880 | * |
| 881 | * @param flash_area_id_src The ID of the source flash area. |
| 882 | * @param flash_area_id_dst The ID of the destination flash area. |
| 883 | * @param off_src The offset within the source flash area to |
| 884 | * copy from. |
| 885 | * @param off_dst The offset within the destination flash area to |
| 886 | * copy to. |
| 887 | * @param sz The number of bytes to copy. |
| 888 | * |
| 889 | * @return 0 on success; nonzero on failure. |
| 890 | */ |
| 891 | static int |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 892 | boot_copy_sector(const struct flash_area *fap_src, |
| 893 | const struct flash_area *fap_dst, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 894 | uint32_t off_src, uint32_t off_dst, uint32_t sz) |
| 895 | { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 896 | uint32_t bytes_copied; |
| 897 | int chunk_sz; |
| 898 | int rc; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 899 | #ifdef MCUBOOT_ENC_IMAGES |
| 900 | uint32_t off; |
| 901 | size_t blk_off; |
| 902 | struct image_header *hdr; |
| 903 | uint16_t idx; |
| 904 | uint32_t blk_sz; |
| 905 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 906 | |
| 907 | static uint8_t buf[1024]; |
| 908 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 909 | bytes_copied = 0; |
| 910 | while (bytes_copied < sz) { |
| 911 | if (sz - bytes_copied > sizeof buf) { |
| 912 | chunk_sz = sizeof buf; |
| 913 | } else { |
| 914 | chunk_sz = sz - bytes_copied; |
| 915 | } |
| 916 | |
| 917 | rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz); |
| 918 | if (rc != 0) { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 919 | return BOOT_EFLASH; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 920 | } |
| 921 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 922 | #ifdef MCUBOOT_ENC_IMAGES |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 923 | if ((fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY) || |
| 924 | (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY)) { |
| 925 | /* assume the secondary slot as src, needs decryption */ |
| 926 | hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 927 | off = off_src; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 928 | if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY) { |
| 929 | /* might need encryption (metadata from the primary slot) */ |
| 930 | hdr = boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 931 | off = off_dst; |
| 932 | } |
Fabio Utzig | 2fc80df | 2018-12-14 06:47:38 -0200 | [diff] [blame] | 933 | if (IS_ENCRYPTED(hdr)) { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 934 | blk_sz = chunk_sz; |
| 935 | idx = 0; |
| 936 | if (off + bytes_copied < hdr->ih_hdr_size) { |
| 937 | /* do not decrypt header */ |
| 938 | blk_off = 0; |
| 939 | blk_sz = chunk_sz - hdr->ih_hdr_size; |
| 940 | idx = hdr->ih_hdr_size; |
| 941 | } else { |
| 942 | blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf; |
| 943 | } |
| 944 | if (off + bytes_copied + chunk_sz > hdr->ih_hdr_size + hdr->ih_img_size) { |
| 945 | /* do not decrypt TLVs */ |
| 946 | if (off + bytes_copied >= hdr->ih_hdr_size + hdr->ih_img_size) { |
| 947 | blk_sz = 0; |
| 948 | } else { |
| 949 | blk_sz = (hdr->ih_hdr_size + hdr->ih_img_size) - (off + bytes_copied); |
| 950 | } |
| 951 | } |
| 952 | boot_encrypt(fap_src, (off + bytes_copied + idx) - hdr->ih_hdr_size, |
| 953 | blk_sz, blk_off, &buf[idx]); |
| 954 | } |
| 955 | } |
| 956 | #endif |
| 957 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 958 | rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz); |
| 959 | if (rc != 0) { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 960 | return BOOT_EFLASH; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | bytes_copied += chunk_sz; |
Fabio Utzig | 853657c | 2019-05-07 08:06:07 -0300 | [diff] [blame] | 964 | |
| 965 | MCUBOOT_WATCHDOG_FEED(); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 966 | } |
| 967 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 968 | return 0; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 969 | } |
| 970 | |
David Brown | 6b1b3b9 | 2017-09-19 08:59:10 -0600 | [diff] [blame] | 971 | #ifndef MCUBOOT_OVERWRITE_ONLY |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 972 | static inline int |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 973 | boot_status_init(const struct flash_area *fap, const struct boot_status *bs) |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 974 | { |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 975 | struct boot_swap_state swap_state; |
| 976 | int rc; |
| 977 | |
Christopher Collins | 2c88e69 | 2019-05-22 15:10:14 -0700 | [diff] [blame] | 978 | BOOT_LOG_DBG("initializing status; fa_id=%d", fap->fa_id); |
| 979 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 980 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY, &swap_state); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 981 | assert(rc == 0); |
| 982 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 983 | if (bs->swap_type != BOOT_SWAP_TYPE_NONE) { |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 984 | rc = boot_write_swap_info(fap, |
| 985 | bs->swap_type, |
| 986 | 0); |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 987 | assert(rc == 0); |
| 988 | } |
| 989 | |
Fabio Utzig | de8a38a | 2017-05-23 11:15:01 -0400 | [diff] [blame] | 990 | if (swap_state.image_ok == BOOT_FLAG_SET) { |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 991 | rc = boot_write_image_ok(fap); |
| 992 | assert(rc == 0); |
| 993 | } |
| 994 | |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 995 | rc = boot_write_swap_size(fap, bs->swap_size); |
| 996 | assert(rc == 0); |
| 997 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 998 | #ifdef MCUBOOT_ENC_IMAGES |
| 999 | rc = boot_write_enc_key(fap, 0, bs->enckey[0]); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1000 | assert(rc == 0); |
| 1001 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1002 | rc = boot_write_enc_key(fap, 1, bs->enckey[1]); |
| 1003 | assert(rc == 0); |
| 1004 | #endif |
| 1005 | |
| 1006 | rc = boot_write_magic(fap); |
| 1007 | assert(rc == 0); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1008 | |
| 1009 | return 0; |
| 1010 | } |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1011 | |
David Brown | 6b1b3b9 | 2017-09-19 08:59:10 -0600 | [diff] [blame] | 1012 | #endif |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1013 | |
Fabio Utzig | 358c935 | 2017-07-25 22:10:45 -0300 | [diff] [blame] | 1014 | #ifndef MCUBOOT_OVERWRITE_ONLY |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1015 | static int |
Fabio Utzig | ed0ca43 | 2019-01-23 14:50:11 -0200 | [diff] [blame] | 1016 | boot_erase_trailer_sectors(const struct flash_area *fap) |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1017 | { |
| 1018 | uint8_t slot; |
Fabio Utzig | ed0ca43 | 2019-01-23 14:50:11 -0200 | [diff] [blame] | 1019 | uint32_t sector; |
| 1020 | uint32_t trailer_sz; |
| 1021 | uint32_t total_sz; |
| 1022 | uint32_t off; |
| 1023 | uint32_t sz; |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1024 | int rc; |
| 1025 | |
Christopher Collins | 2c88e69 | 2019-05-22 15:10:14 -0700 | [diff] [blame] | 1026 | BOOT_LOG_DBG("erasing trailer; fa_id=%d", fap->fa_id); |
| 1027 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1028 | switch (fap->fa_id) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1029 | case FLASH_AREA_IMAGE_PRIMARY: |
| 1030 | slot = BOOT_PRIMARY_SLOT; |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1031 | break; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1032 | case FLASH_AREA_IMAGE_SECONDARY: |
| 1033 | slot = BOOT_SECONDARY_SLOT; |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1034 | break; |
| 1035 | default: |
| 1036 | return BOOT_EFLASH; |
| 1037 | } |
| 1038 | |
Fabio Utzig | ed0ca43 | 2019-01-23 14:50:11 -0200 | [diff] [blame] | 1039 | /* delete starting from last sector and moving to beginning */ |
| 1040 | sector = boot_img_num_sectors(&boot_data, slot) - 1; |
Christopher Collins | 2adef70 | 2019-05-22 14:37:31 -0700 | [diff] [blame] | 1041 | trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(&boot_data)); |
Fabio Utzig | ed0ca43 | 2019-01-23 14:50:11 -0200 | [diff] [blame] | 1042 | total_sz = 0; |
| 1043 | do { |
| 1044 | sz = boot_img_sector_size(&boot_data, slot, sector); |
| 1045 | off = boot_img_sector_off(&boot_data, slot, sector); |
| 1046 | rc = boot_erase_sector(fap, off, sz); |
| 1047 | assert(rc == 0); |
| 1048 | |
| 1049 | sector--; |
| 1050 | total_sz += sz; |
| 1051 | } while (total_sz < trailer_sz); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1052 | |
| 1053 | return rc; |
| 1054 | } |
Fabio Utzig | 358c935 | 2017-07-25 22:10:45 -0300 | [diff] [blame] | 1055 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1056 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1057 | /** |
| 1058 | * Swaps the contents of two flash regions within the two image slots. |
| 1059 | * |
| 1060 | * @param idx The index of the first sector in the range of |
| 1061 | * sectors being swapped. |
| 1062 | * @param sz The number of bytes to swap. |
| 1063 | * @param bs The current boot status. This struct gets |
| 1064 | * updated according to the outcome. |
| 1065 | * |
| 1066 | * @return 0 on success; nonzero on failure. |
| 1067 | */ |
Fabio Utzig | 3488eef | 2017-06-12 10:25:43 -0300 | [diff] [blame] | 1068 | #ifndef MCUBOOT_OVERWRITE_ONLY |
Christopher Collins | 4772ac4 | 2017-02-27 20:08:01 -0800 | [diff] [blame] | 1069 | static void |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1070 | boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs) |
| 1071 | { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1072 | const struct flash_area *fap_primary_slot; |
| 1073 | const struct flash_area *fap_secondary_slot; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1074 | const struct flash_area *fap_scratch; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1075 | uint32_t copy_sz; |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1076 | uint32_t trailer_sz; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1077 | uint32_t img_off; |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1078 | uint32_t scratch_trailer_off; |
| 1079 | struct boot_swap_state swap_state; |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 1080 | size_t last_sector; |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1081 | bool erase_scratch; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1082 | int rc; |
| 1083 | |
| 1084 | /* Calculate offset from start of image area. */ |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1085 | img_off = boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT, idx); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1086 | |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1087 | copy_sz = sz; |
Christopher Collins | 2adef70 | 2019-05-22 14:37:31 -0700 | [diff] [blame] | 1088 | trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(&boot_data)); |
Fabio Utzig | 9678c97 | 2017-05-23 11:28:56 -0400 | [diff] [blame] | 1089 | |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 1090 | /* sz in this function is always sized on a multiple of the sector size. |
| 1091 | * The check against the start offset of the last sector |
Fabio Utzig | 9678c97 | 2017-05-23 11:28:56 -0400 | [diff] [blame] | 1092 | * is to determine if we're swapping the last sector. The last sector |
| 1093 | * needs special handling because it's where the trailer lives. If we're |
| 1094 | * copying it, we need to use scratch to write the trailer temporarily. |
| 1095 | * |
| 1096 | * NOTE: `use_scratch` is a temporary flag (never written to flash) which |
| 1097 | * controls if special handling is needed (swapping last sector). |
| 1098 | */ |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1099 | last_sector = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT) - 1; |
| 1100 | if (img_off + sz > boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT, |
| 1101 | last_sector)) { |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1102 | copy_sz -= trailer_sz; |
| 1103 | } |
| 1104 | |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 1105 | bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1106 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1107 | rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap_primary_slot); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1108 | assert (rc == 0); |
| 1109 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1110 | rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap_secondary_slot); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1111 | assert (rc == 0); |
| 1112 | |
| 1113 | rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch); |
| 1114 | assert (rc == 0); |
| 1115 | |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 1116 | if (bs->state == BOOT_STATUS_STATE_0) { |
Christopher Collins | 2c88e69 | 2019-05-22 15:10:14 -0700 | [diff] [blame] | 1117 | BOOT_LOG_DBG("erasing scratch area"); |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1118 | rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size); |
Christopher Collins | 4772ac4 | 2017-02-27 20:08:01 -0800 | [diff] [blame] | 1119 | assert(rc == 0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1120 | |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 1121 | if (bs->idx == BOOT_STATUS_IDX_0) { |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1122 | /* Write a trailer to the scratch area, even if we don't need the |
| 1123 | * scratch area for status. We need a temporary place to store the |
| 1124 | * `swap-type` while we erase the primary trailer. |
| 1125 | */ |
| 1126 | rc = boot_status_init(fap_scratch, bs); |
| 1127 | assert(rc == 0); |
| 1128 | |
| 1129 | if (!bs->use_scratch) { |
| 1130 | /* Prepare the primary status area... here it is known that the |
| 1131 | * last sector is not being used by the image data so it's safe |
| 1132 | * to erase. |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1133 | */ |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1134 | rc = boot_erase_trailer_sectors(fap_primary_slot); |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1135 | assert(rc == 0); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1136 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1137 | rc = boot_status_init(fap_primary_slot, bs); |
| 1138 | assert(rc == 0); |
| 1139 | |
| 1140 | /* Erase the temporary trailer from the scratch area. */ |
| 1141 | rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size); |
| 1142 | assert(rc == 0); |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1143 | } |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1144 | } |
| 1145 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1146 | rc = boot_copy_sector(fap_secondary_slot, fap_scratch, |
| 1147 | img_off, 0, copy_sz); |
| 1148 | assert(rc == 0); |
| 1149 | |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 1150 | bs->state = BOOT_STATUS_STATE_1; |
Christopher Collins | 4772ac4 | 2017-02-27 20:08:01 -0800 | [diff] [blame] | 1151 | rc = boot_write_status(bs); |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 1152 | BOOT_STATUS_ASSERT(rc == 0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1153 | } |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1154 | |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 1155 | if (bs->state == BOOT_STATUS_STATE_1) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1156 | rc = boot_erase_sector(fap_secondary_slot, img_off, sz); |
Christopher Collins | 4772ac4 | 2017-02-27 20:08:01 -0800 | [diff] [blame] | 1157 | assert(rc == 0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1158 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1159 | rc = boot_copy_sector(fap_primary_slot, fap_secondary_slot, |
| 1160 | img_off, img_off, copy_sz); |
Christopher Collins | 4772ac4 | 2017-02-27 20:08:01 -0800 | [diff] [blame] | 1161 | assert(rc == 0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1162 | |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 1163 | if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) { |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1164 | /* If not all sectors of the slot are being swapped, |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1165 | * guarantee here that only the primary slot will have the state. |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1166 | */ |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1167 | rc = boot_erase_trailer_sectors(fap_secondary_slot); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1168 | assert(rc == 0); |
| 1169 | } |
| 1170 | |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 1171 | bs->state = BOOT_STATUS_STATE_2; |
Christopher Collins | 4772ac4 | 2017-02-27 20:08:01 -0800 | [diff] [blame] | 1172 | rc = boot_write_status(bs); |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 1173 | BOOT_STATUS_ASSERT(rc == 0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1174 | } |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1175 | |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 1176 | if (bs->state == BOOT_STATUS_STATE_2) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1177 | rc = boot_erase_sector(fap_primary_slot, img_off, sz); |
Christopher Collins | 4772ac4 | 2017-02-27 20:08:01 -0800 | [diff] [blame] | 1178 | assert(rc == 0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1179 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1180 | /* NOTE: If this is the final sector, we exclude the image trailer from |
| 1181 | * this copy (copy_sz was truncated earlier). |
| 1182 | */ |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1183 | rc = boot_copy_sector(fap_scratch, fap_primary_slot, |
| 1184 | 0, img_off, copy_sz); |
Christopher Collins | 4772ac4 | 2017-02-27 20:08:01 -0800 | [diff] [blame] | 1185 | assert(rc == 0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1186 | |
Fabio Utzig | 94d998c | 2017-05-22 11:02:41 -0400 | [diff] [blame] | 1187 | if (bs->use_scratch) { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1188 | scratch_trailer_off = boot_status_off(fap_scratch); |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1189 | |
| 1190 | /* copy current status that is being maintained in scratch */ |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1191 | rc = boot_copy_sector(fap_scratch, fap_primary_slot, |
| 1192 | scratch_trailer_off, img_off + copy_sz, |
Marti Bolivar | e10a739 | 2017-06-14 16:20:07 -0400 | [diff] [blame] | 1193 | BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data)); |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 1194 | BOOT_STATUS_ASSERT(rc == 0); |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1195 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1196 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, |
| 1197 | &swap_state); |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1198 | assert(rc == 0); |
| 1199 | |
Fabio Utzig | de8a38a | 2017-05-23 11:15:01 -0400 | [diff] [blame] | 1200 | if (swap_state.image_ok == BOOT_FLAG_SET) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1201 | rc = boot_write_image_ok(fap_primary_slot); |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1202 | assert(rc == 0); |
| 1203 | } |
| 1204 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1205 | if (swap_state.swap_type != BOOT_SWAP_TYPE_NONE) { |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 1206 | rc = boot_write_swap_info(fap_primary_slot, |
| 1207 | swap_state.swap_type, |
| 1208 | 0); |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1209 | assert(rc == 0); |
| 1210 | } |
| 1211 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1212 | rc = boot_write_swap_size(fap_primary_slot, bs->swap_size); |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 1213 | assert(rc == 0); |
| 1214 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1215 | #ifdef MCUBOOT_ENC_IMAGES |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1216 | rc = boot_write_enc_key(fap_primary_slot, 0, bs->enckey[0]); |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1217 | assert(rc == 0); |
| 1218 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1219 | rc = boot_write_enc_key(fap_primary_slot, 1, bs->enckey[1]); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1220 | assert(rc == 0); |
| 1221 | #endif |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1222 | rc = boot_write_magic(fap_primary_slot); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1223 | assert(rc == 0); |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1224 | } |
| 1225 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1226 | /* If we wrote a trailer to the scratch area, erase it after we persist |
| 1227 | * a trailer to the primary slot. We do this to prevent mcuboot from |
| 1228 | * reading a stale status from the scratch area in case of immediate |
| 1229 | * reset. |
| 1230 | */ |
| 1231 | erase_scratch = bs->use_scratch; |
| 1232 | bs->use_scratch = 0; |
| 1233 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1234 | bs->idx++; |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 1235 | bs->state = BOOT_STATUS_STATE_0; |
Christopher Collins | 4772ac4 | 2017-02-27 20:08:01 -0800 | [diff] [blame] | 1236 | rc = boot_write_status(bs); |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 1237 | BOOT_STATUS_ASSERT(rc == 0); |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1238 | |
| 1239 | if (erase_scratch) { |
| 1240 | rc = boot_erase_sector(fap_scratch, 0, sz); |
| 1241 | assert(rc == 0); |
| 1242 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1243 | } |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1244 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1245 | flash_area_close(fap_primary_slot); |
| 1246 | flash_area_close(fap_secondary_slot); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1247 | flash_area_close(fap_scratch); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1248 | } |
Fabio Utzig | 3488eef | 2017-06-12 10:25:43 -0300 | [diff] [blame] | 1249 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1250 | |
| 1251 | /** |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1252 | * Overwrite primary slot with the image contained in the secondary slot. |
| 1253 | * If a prior copy operation was interrupted by a system reset, this function |
| 1254 | * redos the copy. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1255 | * |
| 1256 | * @param bs The current boot status. This function reads |
| 1257 | * this struct to determine if it is resuming |
| 1258 | * an interrupted swap operation. This |
| 1259 | * function writes the updated status to this |
| 1260 | * function on return. |
| 1261 | * |
| 1262 | * @return 0 on success; nonzero on failure. |
| 1263 | */ |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 1264 | #if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP) |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 1265 | static int |
| 1266 | boot_copy_image(struct boot_status *bs) |
| 1267 | { |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 1268 | size_t sect_count; |
| 1269 | size_t sect; |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 1270 | int rc; |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 1271 | size_t size; |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 1272 | size_t this_size; |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 1273 | size_t last_sector; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1274 | const struct flash_area *fap_primary_slot; |
| 1275 | const struct flash_area *fap_secondary_slot; |
| 1276 | |
Fabio Utzig | aaf767c | 2017-12-05 10:22:46 -0200 | [diff] [blame] | 1277 | (void)bs; |
| 1278 | |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 1279 | #if defined(MCUBOOT_OVERWRITE_ONLY_FAST) |
| 1280 | uint32_t src_size = 0; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1281 | rc = boot_read_image_size(BOOT_SECONDARY_SLOT, |
| 1282 | boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT), |
| 1283 | &src_size); |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 1284 | assert(rc == 0); |
| 1285 | #endif |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 1286 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1287 | BOOT_LOG_INF("Image upgrade secondary slot -> primary slot"); |
| 1288 | BOOT_LOG_INF("Erasing the primary slot"); |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 1289 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1290 | rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap_primary_slot); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1291 | assert (rc == 0); |
| 1292 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1293 | rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap_secondary_slot); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1294 | assert (rc == 0); |
| 1295 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1296 | sect_count = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT); |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 1297 | for (sect = 0, size = 0; sect < sect_count; sect++) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1298 | this_size = boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, sect); |
| 1299 | rc = boot_erase_sector(fap_primary_slot, size, this_size); |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 1300 | assert(rc == 0); |
| 1301 | |
| 1302 | size += this_size; |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 1303 | |
| 1304 | #if defined(MCUBOOT_OVERWRITE_ONLY_FAST) |
| 1305 | if (size >= src_size) { |
| 1306 | break; |
| 1307 | } |
| 1308 | #endif |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 1309 | } |
| 1310 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1311 | #ifdef MCUBOOT_ENC_IMAGES |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1312 | if (IS_ENCRYPTED(boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT))) { |
| 1313 | rc = boot_enc_load(boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT), |
| 1314 | fap_secondary_slot, |
| 1315 | bs->enckey[1]); |
| 1316 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1317 | if (rc < 0) { |
| 1318 | return BOOT_EBADIMAGE; |
| 1319 | } |
Fabio Utzig | e641ea5 | 2018-12-03 10:37:53 -0200 | [diff] [blame] | 1320 | if (rc == 0 && boot_enc_set_key(1, bs->enckey[1])) { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1321 | return BOOT_EBADIMAGE; |
| 1322 | } |
| 1323 | } |
| 1324 | #endif |
| 1325 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1326 | BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes", |
| 1327 | size); |
| 1328 | rc = boot_copy_sector(fap_secondary_slot, fap_primary_slot, 0, 0, size); |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 1329 | |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 1330 | /* |
| 1331 | * Erases header and trailer. The trailer is erased because when a new |
| 1332 | * image is written without a trailer as is the case when using newt, the |
| 1333 | * trailer that was left might trigger a new upgrade. |
| 1334 | */ |
Christopher Collins | 2c88e69 | 2019-05-22 15:10:14 -0700 | [diff] [blame] | 1335 | BOOT_LOG_DBG("erasing secondary header"); |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1336 | rc = boot_erase_sector(fap_secondary_slot, |
| 1337 | boot_img_sector_off(&boot_data, |
| 1338 | BOOT_SECONDARY_SLOT, 0), |
| 1339 | boot_img_sector_size(&boot_data, |
| 1340 | BOOT_SECONDARY_SLOT, 0)); |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 1341 | assert(rc == 0); |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1342 | last_sector = boot_img_num_sectors(&boot_data, BOOT_SECONDARY_SLOT) - 1; |
Christopher Collins | 2c88e69 | 2019-05-22 15:10:14 -0700 | [diff] [blame] | 1343 | BOOT_LOG_DBG("erasing secondary trailer"); |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1344 | rc = boot_erase_sector(fap_secondary_slot, |
| 1345 | boot_img_sector_off(&boot_data, |
| 1346 | BOOT_SECONDARY_SLOT, last_sector), |
| 1347 | boot_img_sector_size(&boot_data, |
| 1348 | BOOT_SECONDARY_SLOT, last_sector)); |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 1349 | assert(rc == 0); |
| 1350 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1351 | flash_area_close(fap_primary_slot); |
| 1352 | flash_area_close(fap_secondary_slot); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1353 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1354 | /* TODO: Perhaps verify the primary slot's signature again? */ |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 1355 | |
| 1356 | return 0; |
| 1357 | } |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 1358 | #endif |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1359 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1360 | #if !defined(MCUBOOT_OVERWRITE_ONLY) |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1361 | /** |
| 1362 | * Swaps the two images in flash. If a prior copy operation was interrupted |
| 1363 | * by a system reset, this function completes that operation. |
| 1364 | * |
| 1365 | * @param bs The current boot status. This function reads |
| 1366 | * this struct to determine if it is resuming |
| 1367 | * an interrupted swap operation. This |
| 1368 | * function writes the updated status to this |
| 1369 | * function on return. |
| 1370 | * |
| 1371 | * @return 0 on success; nonzero on failure. |
| 1372 | */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1373 | static int |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 1374 | boot_swap_image(struct boot_status *bs) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1375 | { |
| 1376 | uint32_t sz; |
| 1377 | int first_sector_idx; |
| 1378 | int last_sector_idx; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1379 | int last_idx_secondary_slot; |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 1380 | uint32_t swap_idx; |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1381 | struct image_header *hdr; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1382 | #ifdef MCUBOOT_ENC_IMAGES |
| 1383 | const struct flash_area *fap; |
| 1384 | uint8_t slot; |
| 1385 | uint8_t i; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1386 | #endif |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1387 | uint32_t size; |
| 1388 | uint32_t copy_size; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1389 | uint32_t primary_slot_size; |
| 1390 | uint32_t secondary_slot_size; |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1391 | int rc; |
| 1392 | |
| 1393 | /* FIXME: just do this if asked by user? */ |
| 1394 | |
| 1395 | size = copy_size = 0; |
| 1396 | |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 1397 | if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) { |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 1398 | /* |
| 1399 | * No swap ever happened, so need to find the largest image which |
| 1400 | * will be used to determine the amount of sectors to swap. |
| 1401 | */ |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1402 | hdr = boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1403 | if (hdr->ih_magic == IMAGE_MAGIC) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1404 | rc = boot_read_image_size(BOOT_PRIMARY_SLOT, hdr, ©_size); |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 1405 | assert(rc == 0); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1406 | } |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1407 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1408 | #ifdef MCUBOOT_ENC_IMAGES |
Fabio Utzig | 2fc80df | 2018-12-14 06:47:38 -0200 | [diff] [blame] | 1409 | if (IS_ENCRYPTED(hdr)) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1410 | fap = BOOT_IMG_AREA(&boot_data, BOOT_PRIMARY_SLOT); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1411 | rc = boot_enc_load(hdr, fap, bs->enckey[0]); |
| 1412 | assert(rc >= 0); |
| 1413 | |
| 1414 | if (rc == 0) { |
| 1415 | rc = boot_enc_set_key(0, bs->enckey[0]); |
| 1416 | assert(rc == 0); |
| 1417 | } else { |
| 1418 | rc = 0; |
| 1419 | } |
| 1420 | } else { |
| 1421 | memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE); |
| 1422 | } |
| 1423 | #endif |
| 1424 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1425 | hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT); |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 1426 | if (hdr->ih_magic == IMAGE_MAGIC) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1427 | rc = boot_read_image_size(BOOT_SECONDARY_SLOT, hdr, &size); |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 1428 | assert(rc == 0); |
| 1429 | } |
| 1430 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1431 | #ifdef MCUBOOT_ENC_IMAGES |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1432 | hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT); |
Fabio Utzig | 2fc80df | 2018-12-14 06:47:38 -0200 | [diff] [blame] | 1433 | if (IS_ENCRYPTED(hdr)) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1434 | fap = BOOT_IMG_AREA(&boot_data, BOOT_SECONDARY_SLOT); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1435 | rc = boot_enc_load(hdr, fap, bs->enckey[1]); |
| 1436 | assert(rc >= 0); |
| 1437 | |
| 1438 | if (rc == 0) { |
| 1439 | rc = boot_enc_set_key(1, bs->enckey[1]); |
| 1440 | assert(rc == 0); |
| 1441 | } else { |
| 1442 | rc = 0; |
| 1443 | } |
| 1444 | } else { |
| 1445 | memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE); |
| 1446 | } |
| 1447 | #endif |
| 1448 | |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 1449 | if (size > copy_size) { |
| 1450 | copy_size = size; |
| 1451 | } |
| 1452 | |
| 1453 | bs->swap_size = copy_size; |
| 1454 | } else { |
| 1455 | /* |
| 1456 | * If a swap was under way, the swap_size should already be present |
| 1457 | * in the trailer... |
| 1458 | */ |
| 1459 | rc = boot_read_swap_size(&bs->swap_size); |
| 1460 | assert(rc == 0); |
| 1461 | |
| 1462 | copy_size = bs->swap_size; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1463 | |
| 1464 | #ifdef MCUBOOT_ENC_IMAGES |
| 1465 | for (slot = 0; slot <= 1; slot++) { |
| 1466 | rc = boot_read_enc_key(slot, bs->enckey[slot]); |
| 1467 | assert(rc == 0); |
| 1468 | |
| 1469 | for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) { |
Fabio Utzig | 1c7d959 | 2018-12-03 10:35:56 -0200 | [diff] [blame] | 1470 | if (bs->enckey[slot][i] != 0xff) { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1471 | break; |
| 1472 | } |
| 1473 | } |
| 1474 | |
| 1475 | if (i != BOOT_ENC_KEY_SIZE) { |
| 1476 | boot_enc_set_key(slot, bs->enckey[slot]); |
| 1477 | } |
| 1478 | } |
| 1479 | #endif |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1480 | } |
| 1481 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1482 | primary_slot_size = 0; |
| 1483 | secondary_slot_size = 0; |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1484 | last_sector_idx = 0; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1485 | last_idx_secondary_slot = 0; |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 1486 | |
| 1487 | /* |
| 1488 | * Knowing the size of the largest image between both slots, here we |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1489 | * find what is the last sector in the primary slot that needs swapping. |
| 1490 | * Since we already know that both slots are compatible, the secondary |
| 1491 | * slot's last sector is not really required after this check is finished. |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 1492 | */ |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1493 | while (1) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1494 | if ((primary_slot_size < copy_size) || |
| 1495 | (primary_slot_size < secondary_slot_size)) { |
| 1496 | primary_slot_size += boot_img_sector_size(&boot_data, |
| 1497 | BOOT_PRIMARY_SLOT, |
| 1498 | last_sector_idx); |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 1499 | } |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1500 | if ((secondary_slot_size < copy_size) || |
| 1501 | (secondary_slot_size < primary_slot_size)) { |
| 1502 | secondary_slot_size += boot_img_sector_size(&boot_data, |
| 1503 | BOOT_SECONDARY_SLOT, |
| 1504 | last_idx_secondary_slot); |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 1505 | } |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1506 | if (primary_slot_size >= copy_size && |
| 1507 | secondary_slot_size >= copy_size && |
| 1508 | primary_slot_size == secondary_slot_size) { |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1509 | break; |
| 1510 | } |
| 1511 | last_sector_idx++; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1512 | last_idx_secondary_slot++; |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1513 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1514 | |
| 1515 | swap_idx = 0; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1516 | while (last_sector_idx >= 0) { |
| 1517 | sz = boot_copy_sz(last_sector_idx, &first_sector_idx); |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 1518 | if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1519 | boot_swap_sectors(first_sector_idx, sz, bs); |
| 1520 | } |
| 1521 | |
| 1522 | last_sector_idx = first_sector_idx - 1; |
| 1523 | swap_idx++; |
| 1524 | } |
| 1525 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1526 | #ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 1527 | if (boot_status_fails > 0) { |
Christopher Collins | 2c88e69 | 2019-05-22 15:10:14 -0700 | [diff] [blame] | 1528 | BOOT_LOG_WRN("%d status write fails performing the swap", |
| 1529 | boot_status_fails); |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 1530 | } |
| 1531 | #endif |
| 1532 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1533 | return 0; |
| 1534 | } |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 1535 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1536 | |
| 1537 | /** |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1538 | * Marks the image in the primary slot as fully copied. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1539 | */ |
Fabio Utzig | 8d0e588 | 2017-09-13 17:32:44 -0300 | [diff] [blame] | 1540 | #ifndef MCUBOOT_OVERWRITE_ONLY |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1541 | static int |
Fabio Utzig | db5bd3c | 2017-07-13 22:20:22 -0300 | [diff] [blame] | 1542 | boot_set_copy_done(void) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1543 | { |
| 1544 | const struct flash_area *fap; |
| 1545 | int rc; |
| 1546 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1547 | rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1548 | if (rc != 0) { |
| 1549 | return BOOT_EFLASH; |
| 1550 | } |
| 1551 | |
| 1552 | rc = boot_write_copy_done(fap); |
Fabio Utzig | db5bd3c | 2017-07-13 22:20:22 -0300 | [diff] [blame] | 1553 | flash_area_close(fap); |
| 1554 | return rc; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1555 | } |
Fabio Utzig | 8d0e588 | 2017-09-13 17:32:44 -0300 | [diff] [blame] | 1556 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1557 | |
| 1558 | /** |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1559 | * Marks a reverted image in the primary slot as confirmed. This is necessary to |
| 1560 | * ensure the status bytes from the image revert operation don't get processed |
| 1561 | * on a subsequent boot. |
Fabio Utzig | 1e56fcc | 2017-07-17 15:39:14 -0300 | [diff] [blame] | 1562 | * |
| 1563 | * NOTE: image_ok is tested before writing because if there's a valid permanent |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1564 | * image installed on the primary slot and the new image to be upgrade to has a |
| 1565 | * bad sig, image_ok would be overwritten. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1566 | */ |
Fabio Utzig | 8d0e588 | 2017-09-13 17:32:44 -0300 | [diff] [blame] | 1567 | #ifndef MCUBOOT_OVERWRITE_ONLY |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1568 | static int |
Fabio Utzig | db5bd3c | 2017-07-13 22:20:22 -0300 | [diff] [blame] | 1569 | boot_set_image_ok(void) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1570 | { |
| 1571 | const struct flash_area *fap; |
Fabio Utzig | 1e56fcc | 2017-07-17 15:39:14 -0300 | [diff] [blame] | 1572 | struct boot_swap_state state; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1573 | int rc; |
| 1574 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1575 | rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1576 | if (rc != 0) { |
| 1577 | return BOOT_EFLASH; |
| 1578 | } |
| 1579 | |
Fabio Utzig | 1e56fcc | 2017-07-17 15:39:14 -0300 | [diff] [blame] | 1580 | rc = boot_read_swap_state(fap, &state); |
| 1581 | if (rc != 0) { |
| 1582 | rc = BOOT_EFLASH; |
| 1583 | goto out; |
| 1584 | } |
| 1585 | |
| 1586 | if (state.image_ok == BOOT_FLAG_UNSET) { |
| 1587 | rc = boot_write_image_ok(fap); |
| 1588 | } |
| 1589 | |
| 1590 | out: |
Fabio Utzig | db5bd3c | 2017-07-13 22:20:22 -0300 | [diff] [blame] | 1591 | flash_area_close(fap); |
| 1592 | return rc; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1593 | } |
Fabio Utzig | 8d0e588 | 2017-09-13 17:32:44 -0300 | [diff] [blame] | 1594 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1595 | |
| 1596 | /** |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1597 | * Performs an image swap if one is required. |
| 1598 | * |
| 1599 | * @param out_swap_type On success, the type of swap performed gets |
| 1600 | * written here. |
| 1601 | * |
| 1602 | * @return 0 on success; nonzero on failure. |
| 1603 | */ |
| 1604 | static int |
| 1605 | boot_swap_if_needed(int *out_swap_type) |
| 1606 | { |
| 1607 | struct boot_status bs; |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1608 | int rc; |
| 1609 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1610 | /* Determine if we rebooted in the middle of an image swap operation. */ |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1611 | rc = boot_read_status(&bs); |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1612 | assert(rc == 0); |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1613 | if (rc != 0) { |
| 1614 | return rc; |
| 1615 | } |
| 1616 | |
| 1617 | /* If a partial swap was detected, complete it. */ |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 1618 | if (bs.idx != BOOT_STATUS_IDX_0 || bs.state != BOOT_STATUS_STATE_0) { |
Fabio Utzig | a32f1af | 2019-01-07 06:50:58 -0200 | [diff] [blame] | 1619 | #ifdef MCUBOOT_OVERWRITE_ONLY |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 1620 | /* Should never arrive here, overwrite-only mode has no swap state. */ |
| 1621 | assert(0); |
| 1622 | #else |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1623 | /* Determine the type of swap operation being resumed from the |
| 1624 | * `swap-type` trailer field. |
| 1625 | */ |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 1626 | rc = boot_swap_image(&bs); |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1627 | assert(rc == 0); |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1628 | #endif |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1629 | |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1630 | } else { |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1631 | if (bs.swap_type == BOOT_SWAP_TYPE_NONE) { |
| 1632 | bs.swap_type = boot_validated_swap_type(&bs); |
| 1633 | } else if (boot_validate_slot(BOOT_SECONDARY_SLOT, &bs) != 0) { |
| 1634 | bs.swap_type = BOOT_SWAP_TYPE_FAIL; |
| 1635 | } |
| 1636 | switch (bs.swap_type) { |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1637 | case BOOT_SWAP_TYPE_TEST: |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 1638 | case BOOT_SWAP_TYPE_PERM: |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1639 | case BOOT_SWAP_TYPE_REVERT: |
Fabio Utzig | a32f1af | 2019-01-07 06:50:58 -0200 | [diff] [blame] | 1640 | #ifdef MCUBOOT_OVERWRITE_ONLY |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1641 | rc = boot_copy_image(&bs); |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 1642 | #else |
| 1643 | rc = boot_swap_image(&bs); |
| 1644 | #endif |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1645 | assert(rc == 0); |
| 1646 | break; |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 1647 | #ifdef MCUBOOT_BOOTSTRAP |
| 1648 | case BOOT_SWAP_TYPE_NONE: |
| 1649 | /* |
| 1650 | * Header checks are done first because they are inexpensive. |
| 1651 | * Since overwrite-only copies starting from offset 0, if |
| 1652 | * interrupted, it might leave a valid header magic, so also |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1653 | * run validation on the primary slot to be sure it's not OK. |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 1654 | */ |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1655 | if (boot_check_header_erased(BOOT_PRIMARY_SLOT) == 0 || |
| 1656 | boot_validate_slot(BOOT_PRIMARY_SLOT, &bs) != 0) { |
| 1657 | if ((boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT)->ih_magic |
| 1658 | == IMAGE_MAGIC ) && |
| 1659 | (boot_validate_slot(BOOT_SECONDARY_SLOT, &bs) == 0)) { |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 1660 | rc = boot_copy_image(&bs); |
| 1661 | assert(rc == 0); |
| 1662 | |
| 1663 | /* Returns fail here to trigger a re-read of the headers. */ |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1664 | bs.swap_type = BOOT_SWAP_TYPE_FAIL; |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 1665 | } |
| 1666 | } |
| 1667 | break; |
| 1668 | #endif |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1669 | } |
| 1670 | } |
| 1671 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1672 | *out_swap_type = bs.swap_type; |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1673 | return 0; |
| 1674 | } |
| 1675 | |
| 1676 | /** |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1677 | * Prepares the booting process. This function moves images around in flash as |
| 1678 | * appropriate, and tells you what address to boot from. |
| 1679 | * |
| 1680 | * @param rsp On success, indicates how booting should occur. |
| 1681 | * |
| 1682 | * @return 0 on success; nonzero on failure. |
| 1683 | */ |
| 1684 | int |
| 1685 | boot_go(struct boot_rsp *rsp) |
| 1686 | { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1687 | int swap_type; |
Marti Bolivar | 8489865 | 2017-06-13 17:20:22 -0400 | [diff] [blame] | 1688 | size_t slot; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1689 | int rc; |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1690 | int fa_id; |
David Brown | 52eee56 | 2017-07-05 11:25:09 -0600 | [diff] [blame] | 1691 | bool reload_headers = false; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1692 | |
| 1693 | /* The array of slot sectors are defined here (as opposed to file scope) so |
| 1694 | * that they don't get allocated for non-boot-loader apps. This is |
| 1695 | * necessary because the gcc option "-fdata-sections" doesn't seem to have |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1696 | * any effect in older gcc versions (e.g., 4.8.4). |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1697 | */ |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1698 | static boot_sector_t primary_slot_sectors[BOOT_MAX_IMG_SECTORS]; |
| 1699 | static boot_sector_t secondary_slot_sectors[BOOT_MAX_IMG_SECTORS]; |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 1700 | static boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS]; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1701 | boot_data.imgs[BOOT_PRIMARY_SLOT].sectors = primary_slot_sectors; |
| 1702 | boot_data.imgs[BOOT_SECONDARY_SLOT].sectors = secondary_slot_sectors; |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 1703 | boot_data.scratch.sectors = scratch_sectors; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1704 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1705 | #ifdef MCUBOOT_ENC_IMAGES |
| 1706 | /* FIXME: remove this after RAM is cleared by sim */ |
| 1707 | boot_enc_zeroize(); |
| 1708 | #endif |
| 1709 | |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1710 | /* Open boot_data image areas for the duration of this call. */ |
| 1711 | for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) { |
| 1712 | fa_id = flash_area_id_from_image_slot(slot); |
| 1713 | rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot)); |
| 1714 | assert(rc == 0); |
| 1715 | } |
| 1716 | rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, |
| 1717 | &BOOT_SCRATCH_AREA(&boot_data)); |
| 1718 | assert(rc == 0); |
| 1719 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1720 | /* Determine the sector layout of the image slots and scratch area. */ |
| 1721 | rc = boot_read_sectors(); |
| 1722 | if (rc != 0) { |
Fabio Utzig | a1fae67 | 2018-03-30 10:52:38 -0300 | [diff] [blame] | 1723 | BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - too small?", |
| 1724 | BOOT_MAX_IMG_SECTORS); |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1725 | goto out; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1726 | } |
| 1727 | |
| 1728 | /* Attempt to read an image header from each slot. */ |
Fabio Utzig | 9c25fa7 | 2017-12-12 14:57:20 -0200 | [diff] [blame] | 1729 | rc = boot_read_image_headers(false); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1730 | if (rc != 0) { |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1731 | goto out; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1732 | } |
| 1733 | |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1734 | /* If the image slots aren't compatible, no swap is possible. Just boot |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1735 | * into the primary slot. |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1736 | */ |
| 1737 | if (boot_slots_compatible()) { |
| 1738 | rc = boot_swap_if_needed(&swap_type); |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1739 | assert(rc == 0); |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1740 | if (rc != 0) { |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1741 | goto out; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1742 | } |
Fabio Utzig | db5bd3c | 2017-07-13 22:20:22 -0300 | [diff] [blame] | 1743 | |
| 1744 | /* |
| 1745 | * The following states need image_ok be explicitly set after the |
| 1746 | * swap was finished to avoid a new revert. |
| 1747 | */ |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1748 | if (swap_type == BOOT_SWAP_TYPE_REVERT || |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1749 | swap_type == BOOT_SWAP_TYPE_FAIL || |
| 1750 | swap_type == BOOT_SWAP_TYPE_PERM) { |
Fabio Utzig | 8d0e588 | 2017-09-13 17:32:44 -0300 | [diff] [blame] | 1751 | #ifndef MCUBOOT_OVERWRITE_ONLY |
Fabio Utzig | 695d564 | 2017-07-20 09:47:16 -0300 | [diff] [blame] | 1752 | rc = boot_set_image_ok(); |
| 1753 | if (rc != 0) { |
| 1754 | swap_type = BOOT_SWAP_TYPE_PANIC; |
| 1755 | } |
Fabio Utzig | 8d0e588 | 2017-09-13 17:32:44 -0300 | [diff] [blame] | 1756 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
Fabio Utzig | db5bd3c | 2017-07-13 22:20:22 -0300 | [diff] [blame] | 1757 | } |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1758 | } else { |
| 1759 | swap_type = BOOT_SWAP_TYPE_NONE; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1760 | } |
| 1761 | |
| 1762 | switch (swap_type) { |
| 1763 | case BOOT_SWAP_TYPE_NONE: |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1764 | slot = BOOT_PRIMARY_SLOT; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1765 | break; |
| 1766 | |
Fabio Utzig | 695d564 | 2017-07-20 09:47:16 -0300 | [diff] [blame] | 1767 | case BOOT_SWAP_TYPE_TEST: /* fallthrough */ |
| 1768 | case BOOT_SWAP_TYPE_PERM: /* fallthrough */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1769 | case BOOT_SWAP_TYPE_REVERT: |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1770 | slot = BOOT_SECONDARY_SLOT; |
David Brown | 52eee56 | 2017-07-05 11:25:09 -0600 | [diff] [blame] | 1771 | reload_headers = true; |
Fabio Utzig | 8d0e588 | 2017-09-13 17:32:44 -0300 | [diff] [blame] | 1772 | #ifndef MCUBOOT_OVERWRITE_ONLY |
Fabio Utzig | 695d564 | 2017-07-20 09:47:16 -0300 | [diff] [blame] | 1773 | rc = boot_set_copy_done(); |
| 1774 | if (rc != 0) { |
| 1775 | swap_type = BOOT_SWAP_TYPE_PANIC; |
| 1776 | } |
Fabio Utzig | 8d0e588 | 2017-09-13 17:32:44 -0300 | [diff] [blame] | 1777 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1778 | break; |
| 1779 | |
| 1780 | case BOOT_SWAP_TYPE_FAIL: |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1781 | /* The image in the secondary slot was invalid and is now erased. |
| 1782 | * Ensure we don't try to boot into it again on the next reboot. |
| 1783 | * Do this by pretending we just reverted back to the primary slot. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1784 | */ |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1785 | slot = BOOT_PRIMARY_SLOT; |
David Brown | 52eee56 | 2017-07-05 11:25:09 -0600 | [diff] [blame] | 1786 | reload_headers = true; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1787 | break; |
| 1788 | |
Fabio Utzig | db5bd3c | 2017-07-13 22:20:22 -0300 | [diff] [blame] | 1789 | default: |
Fabio Utzig | 695d564 | 2017-07-20 09:47:16 -0300 | [diff] [blame] | 1790 | swap_type = BOOT_SWAP_TYPE_PANIC; |
| 1791 | } |
| 1792 | |
| 1793 | if (swap_type == BOOT_SWAP_TYPE_PANIC) { |
| 1794 | BOOT_LOG_ERR("panic!"); |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 1795 | assert(0); |
Fabio Utzig | 695d564 | 2017-07-20 09:47:16 -0300 | [diff] [blame] | 1796 | |
| 1797 | /* Loop forever... */ |
| 1798 | while (1) {} |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1799 | } |
| 1800 | |
David Brown | 52eee56 | 2017-07-05 11:25:09 -0600 | [diff] [blame] | 1801 | if (reload_headers) { |
Fabio Utzig | 9c25fa7 | 2017-12-12 14:57:20 -0200 | [diff] [blame] | 1802 | rc = boot_read_image_headers(false); |
Fabio Utzig | c6a7b0c | 2017-09-13 19:01:15 -0300 | [diff] [blame] | 1803 | if (rc != 0) { |
| 1804 | goto out; |
| 1805 | } |
| 1806 | /* Since headers were reloaded, it can be assumed we just performed a |
| 1807 | * swap or overwrite. Now the header info that should be used to |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1808 | * provide the data for the bootstrap, which previously was at the |
| 1809 | * secondary slot, was updated to the primary slot. |
Fabio Utzig | c6a7b0c | 2017-09-13 19:01:15 -0300 | [diff] [blame] | 1810 | */ |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1811 | slot = BOOT_PRIMARY_SLOT; |
David Brown | 52eee56 | 2017-07-05 11:25:09 -0600 | [diff] [blame] | 1812 | } |
| 1813 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1814 | #ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT |
| 1815 | rc = boot_validate_slot(BOOT_PRIMARY_SLOT, NULL); |
David Brown | 554c52e | 2017-06-30 16:01:07 -0600 | [diff] [blame] | 1816 | if (rc != 0) { |
| 1817 | rc = BOOT_EBADIMAGE; |
| 1818 | goto out; |
| 1819 | } |
Fabio Utzig | 1e56fcc | 2017-07-17 15:39:14 -0300 | [diff] [blame] | 1820 | #else |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1821 | /* Even if we're not re-validating the primary slot, we could be booting |
Marti Bolivar | c1f939d | 2017-11-14 20:04:51 -0500 | [diff] [blame] | 1822 | * onto an empty flash chip. At least do a basic sanity check that |
| 1823 | * the magic number on the image is OK. |
| 1824 | */ |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1825 | if (boot_data.imgs[BOOT_PRIMARY_SLOT].hdr.ih_magic != IMAGE_MAGIC) { |
| 1826 | BOOT_LOG_ERR("bad image magic 0x%lx", |
| 1827 | (unsigned long)boot_data.imgs[BOOT_PRIMARY_SLOT].hdr.ih_magic); |
Marti Bolivar | c1f939d | 2017-11-14 20:04:51 -0500 | [diff] [blame] | 1828 | rc = BOOT_EBADIMAGE; |
| 1829 | goto out; |
| 1830 | } |
David Brown | 554c52e | 2017-06-30 16:01:07 -0600 | [diff] [blame] | 1831 | #endif |
| 1832 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1833 | /* Always boot from the primary slot. */ |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1834 | rsp->br_flash_dev_id = boot_data.imgs[BOOT_PRIMARY_SLOT].area->fa_device_id; |
| 1835 | rsp->br_image_off = boot_img_slot_off(&boot_data, BOOT_PRIMARY_SLOT); |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 1836 | rsp->br_hdr = boot_img_hdr(&boot_data, slot); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1837 | |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1838 | out: |
| 1839 | flash_area_close(BOOT_SCRATCH_AREA(&boot_data)); |
| 1840 | for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) { |
| 1841 | flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot)); |
| 1842 | } |
| 1843 | return rc; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1844 | } |
| 1845 | |
| 1846 | int |
| 1847 | split_go(int loader_slot, int split_slot, void **entry) |
| 1848 | { |
Marti Bolivar | c50926f | 2017-06-14 09:35:40 -0400 | [diff] [blame] | 1849 | boot_sector_t *sectors; |
Christopher Collins | 034a620 | 2017-01-11 12:19:37 -0800 | [diff] [blame] | 1850 | uintptr_t entry_val; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1851 | int loader_flash_id; |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1852 | int split_flash_id; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1853 | int rc; |
| 1854 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1855 | sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors); |
| 1856 | if (sectors == NULL) { |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1857 | return SPLIT_GO_ERR; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1858 | } |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1859 | boot_data.imgs[loader_slot].sectors = sectors + 0; |
| 1860 | boot_data.imgs[split_slot].sectors = sectors + BOOT_MAX_IMG_SECTORS; |
| 1861 | |
| 1862 | loader_flash_id = flash_area_id_from_image_slot(loader_slot); |
| 1863 | rc = flash_area_open(loader_flash_id, |
Alvaro Prieto | 63a2bdb | 2019-07-04 12:18:49 -0700 | [diff] [blame] | 1864 | &BOOT_IMG_AREA(&boot_data, loader_slot)); |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1865 | assert(rc == 0); |
| 1866 | split_flash_id = flash_area_id_from_image_slot(split_slot); |
| 1867 | rc = flash_area_open(split_flash_id, |
| 1868 | &BOOT_IMG_AREA(&boot_data, split_slot)); |
| 1869 | assert(rc == 0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1870 | |
| 1871 | /* Determine the sector layout of the image slots and scratch area. */ |
| 1872 | rc = boot_read_sectors(); |
| 1873 | if (rc != 0) { |
| 1874 | rc = SPLIT_GO_ERR; |
| 1875 | goto done; |
| 1876 | } |
| 1877 | |
Fabio Utzig | 9c25fa7 | 2017-12-12 14:57:20 -0200 | [diff] [blame] | 1878 | rc = boot_read_image_headers(true); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1879 | if (rc != 0) { |
| 1880 | goto done; |
| 1881 | } |
| 1882 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1883 | /* Don't check the bootable image flag because we could really call a |
| 1884 | * bootable or non-bootable image. Just validate that the image check |
| 1885 | * passes which is distinct from the normal check. |
| 1886 | */ |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 1887 | rc = split_image_check(boot_img_hdr(&boot_data, split_slot), |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1888 | BOOT_IMG_AREA(&boot_data, split_slot), |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 1889 | boot_img_hdr(&boot_data, loader_slot), |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1890 | BOOT_IMG_AREA(&boot_data, loader_slot)); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1891 | if (rc != 0) { |
| 1892 | rc = SPLIT_GO_NON_MATCHING; |
| 1893 | goto done; |
| 1894 | } |
| 1895 | |
Marti Bolivar | ea08887 | 2017-06-12 17:10:49 -0400 | [diff] [blame] | 1896 | entry_val = boot_img_slot_off(&boot_data, split_slot) + |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 1897 | boot_img_hdr(&boot_data, split_slot)->ih_hdr_size; |
Christopher Collins | 034a620 | 2017-01-11 12:19:37 -0800 | [diff] [blame] | 1898 | *entry = (void *) entry_val; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1899 | rc = SPLIT_GO_OK; |
| 1900 | |
| 1901 | done: |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1902 | flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot)); |
| 1903 | flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot)); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1904 | free(sectors); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1905 | return rc; |
| 1906 | } |