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