Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 20 | /* |
| 21 | * Modifications are Copyright (c) 2019 Arm Limited. |
| 22 | */ |
| 23 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 24 | /** |
| 25 | * This file provides an interface to the boot loader. Functions defined in |
| 26 | * this file should only be called while the boot loader is running. |
| 27 | */ |
| 28 | |
| 29 | #include <assert.h> |
| 30 | #include <stddef.h> |
David Brown | 52eee56 | 2017-07-05 11:25:09 -0600 | [diff] [blame] | 31 | #include <stdbool.h> |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 32 | #include <inttypes.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <string.h> |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 35 | #include <os/os_malloc.h> |
| 36 | #include "bootutil/bootutil.h" |
| 37 | #include "bootutil/image.h" |
| 38 | #include "bootutil_priv.h" |
Marti Bolivar | fd20c76 | 2017-02-07 16:52:50 -0500 | [diff] [blame] | 39 | #include "bootutil/bootutil_log.h" |
| 40 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 41 | #ifdef MCUBOOT_ENC_IMAGES |
| 42 | #include "bootutil/enc_key.h" |
| 43 | #endif |
| 44 | |
Fabio Utzig | ba1fbe6 | 2017-07-21 14:01:20 -0300 | [diff] [blame] | 45 | #include "mcuboot_config/mcuboot_config.h" |
Fabio Utzig | eed80b6 | 2017-06-10 08:03:05 -0300 | [diff] [blame] | 46 | |
Emanuele Di Santo | 9f1933d | 2018-11-20 10:59:59 +0100 | [diff] [blame] | 47 | MCUBOOT_LOG_MODULE_DECLARE(mcuboot); |
| 48 | |
Marti Bolivar | 9b1f8bb | 2017-06-12 15:24:13 -0400 | [diff] [blame] | 49 | static struct boot_loader_state boot_data; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 50 | |
Fabio Utzig | abec073 | 2019-07-31 08:40:22 -0300 | [diff] [blame] | 51 | #if (BOOT_IMAGE_NUMBER > 1) |
| 52 | #define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x)) |
| 53 | #else |
| 54 | #define IMAGES_ITER(x) |
| 55 | #endif |
| 56 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 57 | /* |
| 58 | * This macro allows some control on the allocation of local variables. |
| 59 | * When running natively on a target, we don't want to allocated huge |
| 60 | * variables on the stack, so make them global instead. For the simulator |
| 61 | * we want to run as many threads as there are tests, and it's safer |
| 62 | * to just make those variables stack allocated. |
| 63 | */ |
| 64 | #if !defined(__BOOTSIM__) |
| 65 | #define TARGET_STATIC static |
| 66 | #else |
| 67 | #define TARGET_STATIC |
| 68 | #endif |
| 69 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 70 | #if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT) && !defined(MCUBOOT_OVERWRITE_ONLY) |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 71 | /* |
| 72 | * FIXME: this might have to be updated for threaded sim |
| 73 | */ |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 74 | static int boot_status_fails = 0; |
| 75 | #define BOOT_STATUS_ASSERT(x) \ |
| 76 | do { \ |
Johann Fischer | ed8461b | 2018-02-15 16:50:31 +0100 | [diff] [blame] | 77 | if (!(x)) { \ |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 78 | boot_status_fails++; \ |
| 79 | } \ |
| 80 | } while (0) |
| 81 | #else |
Fabio Utzig | 57c40f7 | 2017-12-12 21:48:30 -0200 | [diff] [blame] | 82 | #define BOOT_STATUS_ASSERT(x) ASSERT(x) |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 83 | #endif |
| 84 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 85 | struct boot_status_table { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 86 | uint8_t bst_magic_primary_slot; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 87 | uint8_t bst_magic_scratch; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 88 | uint8_t bst_copy_done_primary_slot; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 89 | uint8_t bst_status_source; |
| 90 | }; |
| 91 | |
| 92 | /** |
| 93 | * This set of tables maps swap state contents to boot status location. |
| 94 | * When searching for a match, these tables must be iterated in order. |
| 95 | */ |
| 96 | static const struct boot_status_table boot_status_tables[] = { |
| 97 | { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 98 | /* | primary slot | scratch | |
| 99 | * ----------+--------------+--------------| |
| 100 | * magic | Good | Any | |
| 101 | * copy-done | Set | N/A | |
| 102 | * ----------+--------------+--------------' |
| 103 | * source: none | |
| 104 | * ----------------------------------------' |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 105 | */ |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 106 | .bst_magic_primary_slot = BOOT_MAGIC_GOOD, |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 107 | .bst_magic_scratch = BOOT_MAGIC_NOTGOOD, |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 108 | .bst_copy_done_primary_slot = BOOT_FLAG_SET, |
| 109 | .bst_status_source = BOOT_STATUS_SOURCE_NONE, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 110 | }, |
| 111 | |
| 112 | { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 113 | /* | primary slot | scratch | |
| 114 | * ----------+--------------+--------------| |
| 115 | * magic | Good | Any | |
| 116 | * copy-done | Unset | N/A | |
| 117 | * ----------+--------------+--------------' |
| 118 | * source: primary slot | |
| 119 | * ----------------------------------------' |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 120 | */ |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 121 | .bst_magic_primary_slot = BOOT_MAGIC_GOOD, |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 122 | .bst_magic_scratch = BOOT_MAGIC_NOTGOOD, |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 123 | .bst_copy_done_primary_slot = BOOT_FLAG_UNSET, |
| 124 | .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 125 | }, |
| 126 | |
| 127 | { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 128 | /* | primary slot | scratch | |
| 129 | * ----------+--------------+--------------| |
| 130 | * magic | Any | Good | |
| 131 | * copy-done | Any | N/A | |
| 132 | * ----------+--------------+--------------' |
| 133 | * source: scratch | |
| 134 | * ----------------------------------------' |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 135 | */ |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 136 | .bst_magic_primary_slot = BOOT_MAGIC_ANY, |
| 137 | .bst_magic_scratch = BOOT_MAGIC_GOOD, |
| 138 | .bst_copy_done_primary_slot = BOOT_FLAG_ANY, |
| 139 | .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 140 | }, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 141 | { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 142 | /* | primary slot | scratch | |
| 143 | * ----------+--------------+--------------| |
| 144 | * magic | Unset | Any | |
| 145 | * copy-done | Unset | N/A | |
| 146 | * ----------+--------------+--------------| |
| 147 | * source: varies | |
| 148 | * ----------------------------------------+--------------------------+ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 149 | * This represents one of two cases: | |
| 150 | * 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] | 151 | * o Mid-revert; status in primary slot. | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 152 | * -------------------------------------------------------------------' |
| 153 | */ |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 154 | .bst_magic_primary_slot = BOOT_MAGIC_UNSET, |
| 155 | .bst_magic_scratch = BOOT_MAGIC_ANY, |
| 156 | .bst_copy_done_primary_slot = BOOT_FLAG_UNSET, |
| 157 | .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 158 | }, |
| 159 | }; |
| 160 | |
| 161 | #define BOOT_STATUS_TABLES_COUNT \ |
| 162 | (sizeof boot_status_tables / sizeof boot_status_tables[0]) |
| 163 | |
Marti Bolivar | fd20c76 | 2017-02-07 16:52:50 -0500 | [diff] [blame] | 164 | #define BOOT_LOG_SWAP_STATE(area, state) \ |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 165 | BOOT_LOG_INF("%s: magic=%s, swap_type=0x%x, copy_done=0x%x, " \ |
| 166 | "image_ok=0x%x", \ |
Marti Bolivar | fd20c76 | 2017-02-07 16:52:50 -0500 | [diff] [blame] | 167 | (area), \ |
| 168 | ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \ |
| 169 | (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \ |
| 170 | "bad"), \ |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 171 | (state)->swap_type, \ |
Marti Bolivar | fd20c76 | 2017-02-07 16:52:50 -0500 | [diff] [blame] | 172 | (state)->copy_done, \ |
| 173 | (state)->image_ok) |
| 174 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 175 | /** |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 176 | * 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] | 177 | * status is necessary for completing a swap that was interrupted by a boot |
| 178 | * loader reset. |
| 179 | * |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 180 | * @return A BOOT_STATUS_SOURCE_[...] code indicating where status should |
| 181 | * be read from. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 182 | */ |
| 183 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 184 | boot_status_source(struct boot_loader_state *state) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 185 | { |
| 186 | const struct boot_status_table *table; |
| 187 | struct boot_swap_state state_scratch; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 188 | struct boot_swap_state state_primary_slot; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 189 | int rc; |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 190 | size_t i; |
Marti Bolivar | fd20c76 | 2017-02-07 16:52:50 -0500 | [diff] [blame] | 191 | uint8_t source; |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 192 | uint8_t image_index; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 193 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 194 | #if (BOOT_IMAGE_NUMBER == 1) |
| 195 | (void)state; |
| 196 | #endif |
| 197 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 198 | image_index = BOOT_CURR_IMG(state); |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 199 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index), |
| 200 | &state_primary_slot); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 201 | assert(rc == 0); |
| 202 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 203 | 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] | 204 | assert(rc == 0); |
| 205 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 206 | BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot); |
Marti Bolivar | fd20c76 | 2017-02-07 16:52:50 -0500 | [diff] [blame] | 207 | BOOT_LOG_SWAP_STATE("Scratch", &state_scratch); |
| 208 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 209 | for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) { |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 210 | table = &boot_status_tables[i]; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 211 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 212 | if (boot_magic_compatible_check(table->bst_magic_primary_slot, |
| 213 | state_primary_slot.magic) && |
| 214 | boot_magic_compatible_check(table->bst_magic_scratch, |
| 215 | state_scratch.magic) && |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 216 | (table->bst_copy_done_primary_slot == BOOT_FLAG_ANY || |
| 217 | table->bst_copy_done_primary_slot == state_primary_slot.copy_done)) |
| 218 | { |
Marti Bolivar | fd20c76 | 2017-02-07 16:52:50 -0500 | [diff] [blame] | 219 | source = table->bst_status_source; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 220 | |
| 221 | #if (BOOT_IMAGE_NUMBER > 1) |
| 222 | /* In case of multi-image boot it can happen that if boot status |
| 223 | * info is found on scratch area then it does not belong to the |
| 224 | * currently examined image. |
| 225 | */ |
| 226 | if (source == BOOT_STATUS_SOURCE_SCRATCH && |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 227 | state_scratch.image_num != BOOT_CURR_IMG(state)) { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 228 | source = BOOT_STATUS_SOURCE_NONE; |
| 229 | } |
| 230 | #endif |
| 231 | |
Marti Bolivar | fd20c76 | 2017-02-07 16:52:50 -0500 | [diff] [blame] | 232 | BOOT_LOG_INF("Boot source: %s", |
| 233 | source == BOOT_STATUS_SOURCE_NONE ? "none" : |
| 234 | source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" : |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 235 | source == BOOT_STATUS_SOURCE_PRIMARY_SLOT ? |
| 236 | "primary slot" : "BUG; can't happen"); |
Marti Bolivar | fd20c76 | 2017-02-07 16:52:50 -0500 | [diff] [blame] | 237 | return source; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
Marti Bolivar | fd20c76 | 2017-02-07 16:52:50 -0500 | [diff] [blame] | 241 | BOOT_LOG_INF("Boot source: none"); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 242 | return BOOT_STATUS_SOURCE_NONE; |
| 243 | } |
| 244 | |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 245 | /* |
Fabio Utzig | 233af7d | 2019-08-26 12:06:16 -0300 | [diff] [blame] | 246 | * Locate the TLVs in an image. |
| 247 | * |
| 248 | * @param hdr The image_header struct of the image being checked |
| 249 | * @param fap flash_area struct of the slot storing the image being checked |
| 250 | * @param off Address of the first TLV (after TLV info) |
| 251 | * @param end Address where TLV area ends |
| 252 | * |
| 253 | * Returns 0 on success. |
| 254 | */ |
| 255 | int |
| 256 | boot_find_tlv_offs(const struct image_header *hdr, const struct flash_area *fap, |
| 257 | uint32_t *off, uint32_t *end) |
| 258 | { |
| 259 | struct image_tlv_info info; |
| 260 | uint32_t off_; |
| 261 | |
| 262 | off_ = BOOT_TLV_OFF(hdr); |
| 263 | |
| 264 | if (flash_area_read(fap, off_, &info, sizeof(info))) { |
| 265 | return BOOT_EFLASH; |
| 266 | } |
| 267 | |
| 268 | if (info.it_magic != IMAGE_TLV_INFO_MAGIC) { |
| 269 | return BOOT_EBADIMAGE; |
| 270 | } |
| 271 | |
| 272 | *end = off_ + info.it_tlv_tot; |
| 273 | *off = off_ + sizeof(info); |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | /* |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 278 | * Compute the total size of the given image. Includes the size of |
| 279 | * the TLVs. |
| 280 | */ |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 281 | #if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST) |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 282 | static int |
Fabio Utzig | d638b17 | 2019-08-09 10:38:05 -0300 | [diff] [blame] | 283 | boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size) |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 284 | { |
| 285 | const struct flash_area *fap; |
Fabio Utzig | 233af7d | 2019-08-26 12:06:16 -0300 | [diff] [blame] | 286 | uint32_t off; |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 287 | int area_id; |
| 288 | int rc; |
| 289 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 290 | #if (BOOT_IMAGE_NUMBER == 1) |
| 291 | (void)state; |
| 292 | #endif |
| 293 | |
| 294 | area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot); |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 295 | rc = flash_area_open(area_id, &fap); |
| 296 | if (rc != 0) { |
| 297 | rc = BOOT_EFLASH; |
| 298 | goto done; |
| 299 | } |
| 300 | |
Fabio Utzig | 233af7d | 2019-08-26 12:06:16 -0300 | [diff] [blame] | 301 | rc = boot_find_tlv_offs(boot_img_hdr(state, slot), fap, &off, size); |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 302 | if (rc != 0) { |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 303 | goto done; |
| 304 | } |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 305 | rc = 0; |
| 306 | |
| 307 | done: |
| 308 | flash_area_close(fap); |
Fabio Utzig | 2eebf11 | 2017-09-04 15:25:08 -0300 | [diff] [blame] | 309 | return rc; |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 310 | } |
Fabio Utzig | 36ec0e7 | 2017-09-05 08:10:33 -0300 | [diff] [blame] | 311 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 312 | |
Fabio Utzig | c08ed21 | 2017-06-20 19:28:36 -0300 | [diff] [blame] | 313 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 314 | boot_read_image_header(struct boot_loader_state *state, int slot, |
| 315 | struct image_header *out_hdr) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 316 | { |
| 317 | const struct flash_area *fap; |
| 318 | int area_id; |
| 319 | int rc; |
| 320 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 321 | #if (BOOT_IMAGE_NUMBER == 1) |
| 322 | (void)state; |
| 323 | #endif |
| 324 | |
| 325 | area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 326 | rc = flash_area_open(area_id, &fap); |
| 327 | if (rc != 0) { |
| 328 | rc = BOOT_EFLASH; |
| 329 | goto done; |
| 330 | } |
| 331 | |
| 332 | rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr); |
| 333 | if (rc != 0) { |
| 334 | rc = BOOT_EFLASH; |
| 335 | goto done; |
| 336 | } |
| 337 | |
| 338 | rc = 0; |
| 339 | |
| 340 | done: |
| 341 | flash_area_close(fap); |
| 342 | return rc; |
| 343 | } |
| 344 | |
| 345 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 346 | boot_read_image_headers(struct boot_loader_state *state, bool require_all) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 347 | { |
| 348 | int rc; |
| 349 | int i; |
| 350 | |
| 351 | for (i = 0; i < BOOT_NUM_SLOTS; i++) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 352 | rc = boot_read_image_header(state, i, boot_img_hdr(state, i)); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 353 | if (rc != 0) { |
Fabio Utzig | 9c25fa7 | 2017-12-12 14:57:20 -0200 | [diff] [blame] | 354 | /* If `require_all` is set, fail on any single fail, otherwise |
| 355 | * if at least the first slot's header was read successfully, |
| 356 | * then the boot loader can attempt a boot. |
| 357 | * |
| 358 | * Failure to read any headers is a fatal error. |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 359 | */ |
Fabio Utzig | 9c25fa7 | 2017-12-12 14:57:20 -0200 | [diff] [blame] | 360 | if (i > 0 && !require_all) { |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 361 | return 0; |
| 362 | } else { |
| 363 | return rc; |
| 364 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | static uint8_t |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 372 | boot_write_sz(struct boot_loader_state *state) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 373 | { |
| 374 | uint8_t elem_sz; |
| 375 | uint8_t align; |
| 376 | |
| 377 | /* Figure out what size to write update status update as. The size depends |
| 378 | * on what the minimum write size is for scratch area, active image slot. |
| 379 | * We need to use the bigger of those 2 values. |
| 380 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 381 | elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)); |
| 382 | align = flash_area_align(BOOT_SCRATCH_AREA(state)); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 383 | if (align > elem_sz) { |
| 384 | elem_sz = align; |
| 385 | } |
| 386 | |
| 387 | return elem_sz; |
| 388 | } |
| 389 | |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 390 | /* |
| 391 | * Slots are compatible when all sectors that store upto to size of the image |
| 392 | * round up to sector size, in both slot's are able to fit in the scratch |
| 393 | * area, and have sizes that are a multiple of each other (powers of two |
| 394 | * presumably!). |
| 395 | */ |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 396 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 397 | boot_slots_compatible(struct boot_loader_state *state) |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 398 | { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 399 | size_t num_sectors_primary; |
| 400 | size_t num_sectors_secondary; |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 401 | size_t sz0, sz1; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 402 | size_t primary_slot_sz, secondary_slot_sz; |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 403 | size_t scratch_sz; |
| 404 | size_t i, j; |
| 405 | int8_t smaller; |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 406 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 407 | num_sectors_primary = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT); |
| 408 | num_sectors_secondary = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT); |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 409 | if ((num_sectors_primary > BOOT_MAX_IMG_SECTORS) || |
| 410 | (num_sectors_secondary > BOOT_MAX_IMG_SECTORS)) { |
Fabio Utzig | a1fae67 | 2018-03-30 10:52:38 -0300 | [diff] [blame] | 411 | BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed"); |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 412 | return 0; |
| 413 | } |
Fabio Utzig | a1fae67 | 2018-03-30 10:52:38 -0300 | [diff] [blame] | 414 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 415 | scratch_sz = boot_scratch_area_size(state); |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 416 | |
| 417 | /* |
| 418 | * The following loop scans all sectors in a linear fashion, assuring that |
| 419 | * for each possible sector in each slot, it is able to fit in the other |
| 420 | * slot's sector or sectors. Slot's should be compatible as long as any |
| 421 | * number of a slot's sectors are able to fit into another, which only |
| 422 | * excludes cases where sector sizes are not a multiple of each other. |
| 423 | */ |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 424 | i = sz0 = primary_slot_sz = 0; |
| 425 | j = sz1 = secondary_slot_sz = 0; |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 426 | smaller = 0; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 427 | while (i < num_sectors_primary || j < num_sectors_secondary) { |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 428 | if (sz0 == sz1) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 429 | sz0 += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i); |
| 430 | sz1 += boot_img_sector_size(state, BOOT_SECONDARY_SLOT, j); |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 431 | i++; |
| 432 | j++; |
| 433 | } else if (sz0 < sz1) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 434 | sz0 += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i); |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 435 | /* Guarantee that multiple sectors of the secondary slot |
| 436 | * fit into the primary slot. |
| 437 | */ |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 438 | if (smaller == 2) { |
| 439 | BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors"); |
| 440 | return 0; |
| 441 | } |
| 442 | smaller = 1; |
| 443 | i++; |
| 444 | } else { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 445 | sz1 += boot_img_sector_size(state, BOOT_SECONDARY_SLOT, j); |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 446 | /* Guarantee that multiple sectors of the primary slot |
| 447 | * fit into the secondary slot. |
| 448 | */ |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 449 | if (smaller == 1) { |
| 450 | BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors"); |
| 451 | return 0; |
| 452 | } |
| 453 | smaller = 2; |
| 454 | j++; |
| 455 | } |
| 456 | if (sz0 == sz1) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 457 | primary_slot_sz += sz0; |
| 458 | secondary_slot_sz += sz1; |
| 459 | /* Scratch has to fit each swap operation to the size of the larger |
| 460 | * sector among the primary slot and the secondary slot. |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 461 | */ |
| 462 | if (sz0 > scratch_sz || sz1 > scratch_sz) { |
| 463 | BOOT_LOG_WRN("Cannot upgrade: not all sectors fit inside scratch"); |
| 464 | return 0; |
| 465 | } |
| 466 | smaller = sz0 = sz1 = 0; |
| 467 | } |
Fabio Utzig | a1fae67 | 2018-03-30 10:52:38 -0300 | [diff] [blame] | 468 | } |
| 469 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 470 | if ((i != num_sectors_primary) || |
| 471 | (j != num_sectors_secondary) || |
| 472 | (primary_slot_sz != secondary_slot_sz)) { |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 473 | BOOT_LOG_WRN("Cannot upgrade: slots are not compatible"); |
| 474 | return 0; |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | return 1; |
| 478 | } |
| 479 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 480 | #ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS |
| 481 | static int |
| 482 | boot_initialize_area(struct boot_loader_state *state, int flash_area) |
| 483 | { |
| 484 | int num_sectors = BOOT_MAX_IMG_SECTORS; |
| 485 | int rc; |
| 486 | |
| 487 | if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) { |
| 488 | rc = flash_area_to_sectors(flash_area, &num_sectors, |
| 489 | BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors); |
| 490 | BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors = (size_t)num_sectors; |
| 491 | |
| 492 | } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) { |
| 493 | rc = flash_area_to_sectors(flash_area, &num_sectors, |
| 494 | BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors); |
| 495 | BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors = (size_t)num_sectors; |
| 496 | |
| 497 | } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) { |
| 498 | rc = flash_area_to_sectors(flash_area, &num_sectors, |
| 499 | state->scratch.sectors); |
| 500 | state->scratch.num_sectors = (size_t)num_sectors; |
| 501 | } else { |
| 502 | return BOOT_EFLASH; |
| 503 | } |
| 504 | |
| 505 | return rc; |
| 506 | } |
| 507 | #else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */ |
| 508 | static int |
| 509 | boot_initialize_area(struct boot_loader_state *state, int flash_area) |
| 510 | { |
| 511 | uint32_t num_sectors; |
| 512 | struct flash_sector *out_sectors; |
| 513 | size_t *out_num_sectors; |
| 514 | int rc; |
| 515 | |
| 516 | num_sectors = BOOT_MAX_IMG_SECTORS; |
| 517 | |
| 518 | if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) { |
| 519 | out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors; |
| 520 | out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors; |
| 521 | } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) { |
| 522 | out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors; |
| 523 | out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors; |
| 524 | } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) { |
| 525 | out_sectors = state->scratch.sectors; |
| 526 | out_num_sectors = &state->scratch.num_sectors; |
| 527 | } else { |
| 528 | return BOOT_EFLASH; |
| 529 | } |
| 530 | |
| 531 | rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors); |
| 532 | if (rc != 0) { |
| 533 | return rc; |
| 534 | } |
| 535 | *out_num_sectors = num_sectors; |
| 536 | return 0; |
| 537 | } |
| 538 | #endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */ |
| 539 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 540 | /** |
| 541 | * Determines the sector layout of both image slots and the scratch area. |
| 542 | * This information is necessary for calculating the number of bytes to erase |
| 543 | * and copy during an image swap. The information collected during this |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 544 | * function is used to populate the state. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 545 | */ |
| 546 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 547 | boot_read_sectors(struct boot_loader_state *state) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 548 | { |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 549 | uint8_t image_index; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 550 | int rc; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 551 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 552 | image_index = BOOT_CURR_IMG(state); |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 553 | |
| 554 | rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index)); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 555 | if (rc != 0) { |
| 556 | return BOOT_EFLASH; |
| 557 | } |
| 558 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 559 | rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index)); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 560 | if (rc != 0) { |
| 561 | return BOOT_EFLASH; |
| 562 | } |
| 563 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 564 | rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH); |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 565 | if (rc != 0) { |
| 566 | return BOOT_EFLASH; |
| 567 | } |
| 568 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 569 | BOOT_WRITE_SZ(state) = boot_write_sz(state); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 570 | |
| 571 | return 0; |
| 572 | } |
| 573 | |
| 574 | static uint32_t |
| 575 | boot_status_internal_off(int idx, int state, int elem_sz) |
| 576 | { |
| 577 | int idx_sz; |
| 578 | |
| 579 | idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT; |
| 580 | |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 581 | return (idx - BOOT_STATUS_IDX_0) * idx_sz + |
| 582 | (state - BOOT_STATUS_STATE_0) * elem_sz; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | /** |
| 586 | * Reads the status of a partially-completed swap, if any. This is necessary |
| 587 | * to recover in case the boot lodaer was reset in the middle of a swap |
| 588 | * operation. |
| 589 | */ |
| 590 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 591 | boot_read_status_bytes(const struct flash_area *fap, |
| 592 | struct boot_loader_state *state, struct boot_status *bs) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 593 | { |
| 594 | uint32_t off; |
| 595 | uint8_t status; |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 596 | int max_entries; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 597 | int found; |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 598 | int found_idx; |
| 599 | int invalid; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 600 | int rc; |
| 601 | int i; |
| 602 | |
| 603 | off = boot_status_off(fap); |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 604 | max_entries = boot_status_entries(BOOT_CURR_IMG(state), fap); |
Fabio Utzig | 9d16009 | 2019-08-09 07:46:34 -0300 | [diff] [blame] | 605 | if (max_entries < 0) { |
| 606 | return BOOT_EBADARGS; |
| 607 | } |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 608 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 609 | found = 0; |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 610 | found_idx = 0; |
| 611 | invalid = 0; |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 612 | for (i = 0; i < max_entries; i++) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 613 | rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(state), |
Fabio Utzig | 178be54 | 2018-09-19 08:12:56 -0300 | [diff] [blame] | 614 | &status, 1); |
| 615 | if (rc < 0) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 616 | return BOOT_EFLASH; |
| 617 | } |
| 618 | |
Fabio Utzig | 178be54 | 2018-09-19 08:12:56 -0300 | [diff] [blame] | 619 | if (rc == 1) { |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 620 | if (found && !found_idx) { |
| 621 | found_idx = i; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 622 | } |
| 623 | } else if (!found) { |
| 624 | found = 1; |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 625 | } else if (found_idx) { |
| 626 | invalid = 1; |
| 627 | break; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 631 | if (invalid) { |
| 632 | /* This means there was an error writing status on the last |
| 633 | * swap. Tell user and move on to validation! |
| 634 | */ |
| 635 | BOOT_LOG_ERR("Detected inconsistent status!"); |
| 636 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 637 | #if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT) |
| 638 | /* With validation of the primary slot disabled, there is no way |
| 639 | * to be sure the swapped primary slot is OK, so abort! |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 640 | */ |
| 641 | assert(0); |
| 642 | #endif |
| 643 | } |
| 644 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 645 | if (found) { |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 646 | if (!found_idx) { |
| 647 | found_idx = i; |
| 648 | } |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 649 | bs->idx = (found_idx / BOOT_STATUS_STATE_COUNT) + 1; |
| 650 | bs->state = (found_idx % BOOT_STATUS_STATE_COUNT) + 1; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | return 0; |
| 654 | } |
| 655 | |
| 656 | /** |
| 657 | * Reads the boot status from the flash. The boot status contains |
| 658 | * the current state of an interrupted image copy operation. If the boot |
| 659 | * status is not present, or it indicates that previous copy finished, |
| 660 | * there is no operation in progress. |
| 661 | */ |
| 662 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 663 | boot_read_status(struct boot_loader_state *state, struct boot_status *bs) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 664 | { |
| 665 | const struct flash_area *fap; |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 666 | uint32_t off; |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 667 | uint8_t swap_info; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 668 | int status_loc; |
| 669 | int area_id; |
| 670 | int rc; |
| 671 | |
| 672 | memset(bs, 0, sizeof *bs); |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 673 | bs->idx = BOOT_STATUS_IDX_0; |
| 674 | bs->state = BOOT_STATUS_STATE_0; |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 675 | bs->swap_type = BOOT_SWAP_TYPE_NONE; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 676 | |
Fabio Utzig | 03dc9a0 | 2018-06-11 12:24:07 -0700 | [diff] [blame] | 677 | #ifdef MCUBOOT_OVERWRITE_ONLY |
| 678 | /* Overwrite-only doesn't make use of the swap status area. */ |
| 679 | return 0; |
| 680 | #endif |
| 681 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 682 | status_loc = boot_status_source(state); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 683 | switch (status_loc) { |
| 684 | case BOOT_STATUS_SOURCE_NONE: |
| 685 | return 0; |
| 686 | |
| 687 | case BOOT_STATUS_SOURCE_SCRATCH: |
| 688 | area_id = FLASH_AREA_IMAGE_SCRATCH; |
| 689 | break; |
| 690 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 691 | case BOOT_STATUS_SOURCE_PRIMARY_SLOT: |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 692 | area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state)); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 693 | break; |
| 694 | |
| 695 | default: |
| 696 | assert(0); |
| 697 | return BOOT_EBADARGS; |
| 698 | } |
| 699 | |
| 700 | rc = flash_area_open(area_id, &fap); |
| 701 | if (rc != 0) { |
| 702 | return BOOT_EFLASH; |
| 703 | } |
| 704 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 705 | rc = boot_read_status_bytes(fap, state, bs); |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 706 | if (rc == 0) { |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 707 | off = boot_swap_info_off(fap); |
| 708 | rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info); |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 709 | if (rc == 1) { |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 710 | BOOT_SET_SWAP_INFO(swap_info, 0, BOOT_SWAP_TYPE_NONE); |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 711 | rc = 0; |
| 712 | } |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 713 | |
| 714 | /* Extract the swap type info */ |
| 715 | bs->swap_type = BOOT_GET_SWAP_TYPE(swap_info); |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 716 | } |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 717 | |
| 718 | flash_area_close(fap); |
Fabio Utzig | 03dc9a0 | 2018-06-11 12:24:07 -0700 | [diff] [blame] | 719 | |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 720 | return rc; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | /** |
| 724 | * Writes the supplied boot status to the flash file system. The boot status |
| 725 | * contains the current state of an in-progress image copy operation. |
| 726 | * |
| 727 | * @param bs The boot status to write. |
| 728 | * |
| 729 | * @return 0 on success; nonzero on failure. |
| 730 | */ |
| 731 | int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 732 | boot_write_status(struct boot_loader_state *state, struct boot_status *bs) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 733 | { |
| 734 | const struct flash_area *fap; |
| 735 | uint32_t off; |
| 736 | int area_id; |
| 737 | int rc; |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 738 | uint8_t buf[BOOT_MAX_ALIGN]; |
David Brown | 9d72546 | 2017-01-23 15:50:58 -0700 | [diff] [blame] | 739 | uint8_t align; |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 740 | uint8_t erased_val; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 741 | |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 742 | /* 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] | 743 | * the trailer. Since in the last step the primary slot is erased, the |
| 744 | * first two status writes go to the scratch which will be copied to |
| 745 | * the primary slot! |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 746 | */ |
| 747 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 748 | if (bs->use_scratch) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 749 | /* Write to scratch. */ |
| 750 | area_id = FLASH_AREA_IMAGE_SCRATCH; |
| 751 | } else { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 752 | /* Write to the primary slot. */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 753 | area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state)); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | rc = flash_area_open(area_id, &fap); |
| 757 | if (rc != 0) { |
| 758 | rc = BOOT_EFLASH; |
| 759 | goto done; |
| 760 | } |
| 761 | |
| 762 | off = boot_status_off(fap) + |
Fabio Utzig | d638b17 | 2019-08-09 10:38:05 -0300 | [diff] [blame] | 763 | boot_status_internal_off(bs->idx, bs->state, BOOT_WRITE_SZ(state)); |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 764 | align = flash_area_align(fap); |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 765 | erased_val = flash_area_erased_val(fap); |
| 766 | memset(buf, erased_val, BOOT_MAX_ALIGN); |
David Brown | 9d72546 | 2017-01-23 15:50:58 -0700 | [diff] [blame] | 767 | buf[0] = bs->state; |
| 768 | |
| 769 | rc = flash_area_write(fap, off, buf, align); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 770 | if (rc != 0) { |
| 771 | rc = BOOT_EFLASH; |
| 772 | goto done; |
| 773 | } |
| 774 | |
| 775 | rc = 0; |
| 776 | |
| 777 | done: |
| 778 | flash_area_close(fap); |
| 779 | return rc; |
| 780 | } |
| 781 | |
| 782 | /* |
| 783 | * Validate image hash/signature in a slot. |
| 784 | */ |
| 785 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 786 | boot_image_check(struct boot_loader_state *state, struct image_header *hdr, |
| 787 | const struct flash_area *fap, struct boot_status *bs) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 788 | { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 789 | TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ]; |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 790 | uint8_t image_index; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 791 | int rc; |
| 792 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 793 | #if (BOOT_IMAGE_NUMBER == 1) |
| 794 | (void)state; |
| 795 | #endif |
| 796 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 797 | (void)bs; |
| 798 | (void)rc; |
Fabio Utzig | bc07793 | 2019-08-26 11:16:34 -0300 | [diff] [blame] | 799 | |
| 800 | image_index = BOOT_CURR_IMG(state); |
| 801 | |
| 802 | #ifdef MCUBOOT_ENC_IMAGES |
| 803 | if (MUST_DECRYPT(fap, image_index, hdr)) { |
Fabio Utzig | 1e4284b | 2019-08-23 11:55:27 -0300 | [diff] [blame] | 804 | rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs->enckey[1]); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 805 | if (rc < 0) { |
| 806 | return BOOT_EBADIMAGE; |
| 807 | } |
Fabio Utzig | 1e4284b | 2019-08-23 11:55:27 -0300 | [diff] [blame] | 808 | if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs->enckey[1])) { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 809 | return BOOT_EBADIMAGE; |
| 810 | } |
| 811 | } |
Fabio Utzig | bc07793 | 2019-08-26 11:16:34 -0300 | [diff] [blame] | 812 | #endif |
| 813 | |
Fabio Utzig | 1e4284b | 2019-08-23 11:55:27 -0300 | [diff] [blame] | 814 | if (bootutil_img_validate(BOOT_CURR_ENC(state), image_index, hdr, fap, tmpbuf, |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 815 | BOOT_TMPBUF_SZ, NULL, 0, NULL)) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 816 | return BOOT_EBADIMAGE; |
| 817 | } |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 818 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 819 | return 0; |
| 820 | } |
| 821 | |
| 822 | static int |
| 823 | split_image_check(struct image_header *app_hdr, |
| 824 | const struct flash_area *app_fap, |
| 825 | struct image_header *loader_hdr, |
| 826 | const struct flash_area *loader_fap) |
| 827 | { |
| 828 | static void *tmpbuf; |
| 829 | uint8_t loader_hash[32]; |
| 830 | |
| 831 | if (!tmpbuf) { |
| 832 | tmpbuf = malloc(BOOT_TMPBUF_SZ); |
| 833 | if (!tmpbuf) { |
| 834 | return BOOT_ENOMEM; |
| 835 | } |
| 836 | } |
| 837 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 838 | if (bootutil_img_validate(NULL, 0, loader_hdr, loader_fap, tmpbuf, |
| 839 | BOOT_TMPBUF_SZ, NULL, 0, loader_hash)) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 840 | return BOOT_EBADIMAGE; |
| 841 | } |
| 842 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 843 | if (bootutil_img_validate(NULL, 0, app_hdr, app_fap, tmpbuf, |
| 844 | BOOT_TMPBUF_SZ, loader_hash, 32, NULL)) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 845 | return BOOT_EBADIMAGE; |
| 846 | } |
| 847 | |
| 848 | return 0; |
| 849 | } |
| 850 | |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 851 | /* |
| 852 | * Check that a memory area consists of a given value. |
| 853 | */ |
| 854 | static inline bool |
| 855 | 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] | 856 | { |
| 857 | uint8_t i; |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 858 | uint8_t *p = (uint8_t *)data; |
| 859 | for (i = 0; i < len; i++) { |
| 860 | if (val != p[i]) { |
| 861 | return false; |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 862 | } |
| 863 | } |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 864 | return true; |
| 865 | } |
| 866 | |
| 867 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 868 | boot_check_header_erased(struct boot_loader_state *state, int slot) |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 869 | { |
| 870 | const struct flash_area *fap; |
| 871 | struct image_header *hdr; |
| 872 | uint8_t erased_val; |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 873 | int area_id; |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 874 | int rc; |
| 875 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 876 | area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot); |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 877 | rc = flash_area_open(area_id, &fap); |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 878 | if (rc != 0) { |
| 879 | return -1; |
| 880 | } |
| 881 | |
| 882 | erased_val = flash_area_erased_val(fap); |
| 883 | flash_area_close(fap); |
| 884 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 885 | hdr = boot_img_hdr(state, slot); |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 886 | if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) { |
| 887 | return -1; |
| 888 | } |
| 889 | |
| 890 | return 0; |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 891 | } |
| 892 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 893 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 894 | boot_validate_slot(struct boot_loader_state *state, int slot, |
| 895 | struct boot_status *bs) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 896 | { |
| 897 | const struct flash_area *fap; |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 898 | struct image_header *hdr; |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 899 | int area_id; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 900 | int rc; |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 901 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 902 | area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot); |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 903 | rc = flash_area_open(area_id, &fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 904 | if (rc != 0) { |
| 905 | return BOOT_EFLASH; |
| 906 | } |
| 907 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 908 | hdr = boot_img_hdr(state, slot); |
| 909 | if (boot_check_header_erased(state, slot) == 0 || |
| 910 | (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 911 | /* No bootable image in slot; continue booting from the primary slot. */ |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 912 | rc = -1; |
| 913 | goto out; |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 914 | } |
| 915 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 916 | if (hdr->ih_magic != IMAGE_MAGIC || boot_image_check(state, hdr, fap, bs)) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 917 | if (slot != BOOT_PRIMARY_SLOT) { |
David Brown | b38e044 | 2017-02-24 13:57:12 -0700 | [diff] [blame] | 918 | flash_area_erase(fap, 0, fap->fa_size); |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 919 | /* Image in the secondary slot is invalid. Erase the image and |
| 920 | * continue booting from the primary slot. |
David Brown | b38e044 | 2017-02-24 13:57:12 -0700 | [diff] [blame] | 921 | */ |
| 922 | } |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 923 | BOOT_LOG_ERR("Image in the %s slot is not valid!", |
| 924 | (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary"); |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 925 | rc = -1; |
| 926 | goto out; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 927 | } |
| 928 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 929 | /* Image in the secondary slot is valid. */ |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 930 | rc = 0; |
| 931 | |
| 932 | out: |
| 933 | flash_area_close(fap); |
| 934 | return rc; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | /** |
| 938 | * Determines which swap operation to perform, if any. If it is determined |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 939 | * that a swap operation is required, the image in the secondary slot is checked |
| 940 | * for validity. If the image in the secondary slot is invalid, it is erased, |
| 941 | * and a swap type of "none" is indicated. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 942 | * |
| 943 | * @return The type of swap to perform (BOOT_SWAP_TYPE...) |
| 944 | */ |
| 945 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 946 | boot_validated_swap_type(struct boot_loader_state *state, |
| 947 | struct boot_status *bs) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 948 | { |
| 949 | int swap_type; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 950 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 951 | swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state)); |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 952 | switch (swap_type) { |
| 953 | case BOOT_SWAP_TYPE_TEST: |
| 954 | case BOOT_SWAP_TYPE_PERM: |
| 955 | case BOOT_SWAP_TYPE_REVERT: |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 956 | /* Boot loader wants to switch to the secondary slot. |
| 957 | * Ensure image is valid. |
| 958 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 959 | if (boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) != 0) { |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 960 | swap_type = BOOT_SWAP_TYPE_FAIL; |
| 961 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | return swap_type; |
| 965 | } |
| 966 | |
| 967 | /** |
| 968 | * Calculates the number of sectors the scratch area can contain. A "last" |
| 969 | * source sector is specified because images are copied backwards in flash |
| 970 | * (final index to index number 0). |
| 971 | * |
| 972 | * @param last_sector_idx The index of the last source sector |
| 973 | * (inclusive). |
| 974 | * @param out_first_sector_idx The index of the first source sector |
| 975 | * (inclusive) gets written here. |
| 976 | * |
| 977 | * @return The number of bytes comprised by the |
| 978 | * [first-sector, last-sector] range. |
| 979 | */ |
Fabio Utzig | 3488eef | 2017-06-12 10:25:43 -0300 | [diff] [blame] | 980 | #ifndef MCUBOOT_OVERWRITE_ONLY |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 981 | static uint32_t |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 982 | boot_copy_sz(struct boot_loader_state *state, int last_sector_idx, |
| 983 | int *out_first_sector_idx) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 984 | { |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 985 | size_t scratch_sz; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 986 | uint32_t new_sz; |
| 987 | uint32_t sz; |
| 988 | int i; |
| 989 | |
| 990 | sz = 0; |
| 991 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 992 | scratch_sz = boot_scratch_area_size(state); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 993 | for (i = last_sector_idx; i >= 0; i--) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 994 | new_sz = sz + boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i); |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 995 | /* |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 996 | * The secondary slot is not being checked here, because |
| 997 | * `boot_slots_compatible` already provides assurance that the copy size |
| 998 | * will be compatible with the primary slot and scratch. |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 999 | */ |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 1000 | if (new_sz > scratch_sz) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1001 | break; |
| 1002 | } |
| 1003 | sz = new_sz; |
| 1004 | } |
| 1005 | |
| 1006 | /* i currently refers to a sector that doesn't fit or it is -1 because all |
| 1007 | * sectors have been processed. In both cases, exclude sector i. |
| 1008 | */ |
| 1009 | *out_first_sector_idx = i + 1; |
| 1010 | return sz; |
| 1011 | } |
Fabio Utzig | 3488eef | 2017-06-12 10:25:43 -0300 | [diff] [blame] | 1012 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1013 | |
| 1014 | /** |
| 1015 | * Erases a region of flash. |
| 1016 | * |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1017 | * @param flash_area The flash_area containing the region to erase. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1018 | * @param off The offset within the flash area to start the |
| 1019 | * erase. |
| 1020 | * @param sz The number of bytes to erase. |
| 1021 | * |
| 1022 | * @return 0 on success; nonzero on failure. |
| 1023 | */ |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1024 | static inline int |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame^] | 1025 | boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1026 | { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1027 | return flash_area_erase(fap, off, sz); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | /** |
| 1031 | * Copies the contents of one flash region to another. You must erase the |
| 1032 | * destination region prior to calling this function. |
| 1033 | * |
| 1034 | * @param flash_area_id_src The ID of the source flash area. |
| 1035 | * @param flash_area_id_dst The ID of the destination flash area. |
| 1036 | * @param off_src The offset within the source flash area to |
| 1037 | * copy from. |
| 1038 | * @param off_dst The offset within the destination flash area to |
| 1039 | * copy to. |
| 1040 | * @param sz The number of bytes to copy. |
| 1041 | * |
| 1042 | * @return 0 on success; nonzero on failure. |
| 1043 | */ |
| 1044 | static int |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame^] | 1045 | boot_copy_region(struct boot_loader_state *state, |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1046 | const struct flash_area *fap_src, |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1047 | const struct flash_area *fap_dst, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1048 | uint32_t off_src, uint32_t off_dst, uint32_t sz) |
| 1049 | { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1050 | uint32_t bytes_copied; |
| 1051 | int chunk_sz; |
| 1052 | int rc; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1053 | #ifdef MCUBOOT_ENC_IMAGES |
| 1054 | uint32_t off; |
Fabio Utzig | a87cc7d | 2019-08-26 11:21:45 -0300 | [diff] [blame] | 1055 | uint32_t tlv_off; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1056 | size_t blk_off; |
| 1057 | struct image_header *hdr; |
| 1058 | uint16_t idx; |
| 1059 | uint32_t blk_sz; |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1060 | uint8_t image_index; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1061 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1062 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1063 | TARGET_STATIC uint8_t buf[1024]; |
| 1064 | |
| 1065 | #if !defined(MCUBOOT_ENC_IMAGES) |
| 1066 | (void)state; |
| 1067 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1068 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1069 | bytes_copied = 0; |
| 1070 | while (bytes_copied < sz) { |
| 1071 | if (sz - bytes_copied > sizeof buf) { |
| 1072 | chunk_sz = sizeof buf; |
| 1073 | } else { |
| 1074 | chunk_sz = sz - bytes_copied; |
| 1075 | } |
| 1076 | |
| 1077 | rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz); |
| 1078 | if (rc != 0) { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1079 | return BOOT_EFLASH; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1080 | } |
| 1081 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1082 | #ifdef MCUBOOT_ENC_IMAGES |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1083 | image_index = BOOT_CURR_IMG(state); |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1084 | if (fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) || |
| 1085 | fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1086 | /* assume the secondary slot as src, needs decryption */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1087 | hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1088 | off = off_src; |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1089 | if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1090 | /* might need encryption (metadata from the primary slot) */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1091 | hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1092 | off = off_dst; |
| 1093 | } |
Fabio Utzig | 2fc80df | 2018-12-14 06:47:38 -0200 | [diff] [blame] | 1094 | if (IS_ENCRYPTED(hdr)) { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1095 | blk_sz = chunk_sz; |
| 1096 | idx = 0; |
| 1097 | if (off + bytes_copied < hdr->ih_hdr_size) { |
| 1098 | /* do not decrypt header */ |
| 1099 | blk_off = 0; |
| 1100 | blk_sz = chunk_sz - hdr->ih_hdr_size; |
| 1101 | idx = hdr->ih_hdr_size; |
| 1102 | } else { |
| 1103 | blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf; |
| 1104 | } |
Fabio Utzig | a87cc7d | 2019-08-26 11:21:45 -0300 | [diff] [blame] | 1105 | tlv_off = BOOT_TLV_OFF(hdr); |
| 1106 | if (off + bytes_copied + chunk_sz > tlv_off) { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1107 | /* do not decrypt TLVs */ |
Fabio Utzig | a87cc7d | 2019-08-26 11:21:45 -0300 | [diff] [blame] | 1108 | if (off + bytes_copied >= tlv_off) { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1109 | blk_sz = 0; |
| 1110 | } else { |
Fabio Utzig | a87cc7d | 2019-08-26 11:21:45 -0300 | [diff] [blame] | 1111 | blk_sz = tlv_off - (off + bytes_copied); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1112 | } |
| 1113 | } |
Fabio Utzig | 1e4284b | 2019-08-23 11:55:27 -0300 | [diff] [blame] | 1114 | boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src, |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1115 | (off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz, |
| 1116 | blk_off, &buf[idx]); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1117 | } |
| 1118 | } |
| 1119 | #endif |
| 1120 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1121 | rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz); |
| 1122 | if (rc != 0) { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1123 | return BOOT_EFLASH; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | bytes_copied += chunk_sz; |
Fabio Utzig | 853657c | 2019-05-07 08:06:07 -0300 | [diff] [blame] | 1127 | |
| 1128 | MCUBOOT_WATCHDOG_FEED(); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1129 | } |
| 1130 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1131 | return 0; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1132 | } |
| 1133 | |
David Brown | 6b1b3b9 | 2017-09-19 08:59:10 -0600 | [diff] [blame] | 1134 | #ifndef MCUBOOT_OVERWRITE_ONLY |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1135 | static inline int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1136 | boot_status_init(const struct boot_loader_state *state, |
| 1137 | const struct flash_area *fap, |
| 1138 | const struct boot_status *bs) |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1139 | { |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1140 | struct boot_swap_state swap_state; |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1141 | uint8_t image_index; |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1142 | int rc; |
| 1143 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1144 | #if (BOOT_IMAGE_NUMBER == 1) |
| 1145 | (void)state; |
| 1146 | #endif |
| 1147 | |
| 1148 | image_index = BOOT_CURR_IMG(state); |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1149 | |
Christopher Collins | 2c88e69 | 2019-05-22 15:10:14 -0700 | [diff] [blame] | 1150 | BOOT_LOG_DBG("initializing status; fa_id=%d", fap->fa_id); |
| 1151 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1152 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index), |
| 1153 | &swap_state); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1154 | assert(rc == 0); |
| 1155 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1156 | if (bs->swap_type != BOOT_SWAP_TYPE_NONE) { |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1157 | rc = boot_write_swap_info(fap, bs->swap_type, image_index); |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1158 | assert(rc == 0); |
| 1159 | } |
| 1160 | |
Fabio Utzig | de8a38a | 2017-05-23 11:15:01 -0400 | [diff] [blame] | 1161 | if (swap_state.image_ok == BOOT_FLAG_SET) { |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1162 | rc = boot_write_image_ok(fap); |
| 1163 | assert(rc == 0); |
| 1164 | } |
| 1165 | |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 1166 | rc = boot_write_swap_size(fap, bs->swap_size); |
| 1167 | assert(rc == 0); |
| 1168 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1169 | #ifdef MCUBOOT_ENC_IMAGES |
| 1170 | rc = boot_write_enc_key(fap, 0, bs->enckey[0]); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1171 | assert(rc == 0); |
| 1172 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1173 | rc = boot_write_enc_key(fap, 1, bs->enckey[1]); |
| 1174 | assert(rc == 0); |
| 1175 | #endif |
| 1176 | |
| 1177 | rc = boot_write_magic(fap); |
| 1178 | assert(rc == 0); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1179 | |
| 1180 | return 0; |
| 1181 | } |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1182 | |
David Brown | 6b1b3b9 | 2017-09-19 08:59:10 -0600 | [diff] [blame] | 1183 | #endif |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1184 | |
Fabio Utzig | 358c935 | 2017-07-25 22:10:45 -0300 | [diff] [blame] | 1185 | #ifndef MCUBOOT_OVERWRITE_ONLY |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1186 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1187 | boot_erase_trailer_sectors(const struct boot_loader_state *state, |
| 1188 | const struct flash_area *fap) |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1189 | { |
| 1190 | uint8_t slot; |
Fabio Utzig | ed0ca43 | 2019-01-23 14:50:11 -0200 | [diff] [blame] | 1191 | uint32_t sector; |
| 1192 | uint32_t trailer_sz; |
| 1193 | uint32_t total_sz; |
| 1194 | uint32_t off; |
| 1195 | uint32_t sz; |
David Vincze | b75c12a | 2019-03-22 14:58:33 +0100 | [diff] [blame] | 1196 | int fa_id_primary; |
| 1197 | int fa_id_secondary; |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1198 | uint8_t image_index; |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1199 | int rc; |
| 1200 | |
Christopher Collins | 2c88e69 | 2019-05-22 15:10:14 -0700 | [diff] [blame] | 1201 | BOOT_LOG_DBG("erasing trailer; fa_id=%d", fap->fa_id); |
| 1202 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1203 | image_index = BOOT_CURR_IMG(state); |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1204 | fa_id_primary = flash_area_id_from_multi_image_slot(image_index, |
| 1205 | BOOT_PRIMARY_SLOT); |
| 1206 | fa_id_secondary = flash_area_id_from_multi_image_slot(image_index, |
| 1207 | BOOT_SECONDARY_SLOT); |
David Vincze | b75c12a | 2019-03-22 14:58:33 +0100 | [diff] [blame] | 1208 | |
| 1209 | if (fap->fa_id == fa_id_primary) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1210 | slot = BOOT_PRIMARY_SLOT; |
David Vincze | b75c12a | 2019-03-22 14:58:33 +0100 | [diff] [blame] | 1211 | } else if (fap->fa_id == fa_id_secondary) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1212 | slot = BOOT_SECONDARY_SLOT; |
David Vincze | b75c12a | 2019-03-22 14:58:33 +0100 | [diff] [blame] | 1213 | } else { |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1214 | return BOOT_EFLASH; |
| 1215 | } |
| 1216 | |
Fabio Utzig | ed0ca43 | 2019-01-23 14:50:11 -0200 | [diff] [blame] | 1217 | /* delete starting from last sector and moving to beginning */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1218 | sector = boot_img_num_sectors(state, slot) - 1; |
| 1219 | trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state)); |
Fabio Utzig | ed0ca43 | 2019-01-23 14:50:11 -0200 | [diff] [blame] | 1220 | total_sz = 0; |
| 1221 | do { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1222 | sz = boot_img_sector_size(state, slot, sector); |
| 1223 | off = boot_img_sector_off(state, slot, sector); |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame^] | 1224 | rc = boot_erase_region(fap, off, sz); |
Fabio Utzig | ed0ca43 | 2019-01-23 14:50:11 -0200 | [diff] [blame] | 1225 | assert(rc == 0); |
| 1226 | |
| 1227 | sector--; |
| 1228 | total_sz += sz; |
| 1229 | } while (total_sz < trailer_sz); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1230 | |
| 1231 | return rc; |
| 1232 | } |
Fabio Utzig | 358c935 | 2017-07-25 22:10:45 -0300 | [diff] [blame] | 1233 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1234 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1235 | /** |
| 1236 | * Swaps the contents of two flash regions within the two image slots. |
| 1237 | * |
| 1238 | * @param idx The index of the first sector in the range of |
| 1239 | * sectors being swapped. |
| 1240 | * @param sz The number of bytes to swap. |
| 1241 | * @param bs The current boot status. This struct gets |
| 1242 | * updated according to the outcome. |
| 1243 | * |
| 1244 | * @return 0 on success; nonzero on failure. |
| 1245 | */ |
Fabio Utzig | 3488eef | 2017-06-12 10:25:43 -0300 | [diff] [blame] | 1246 | #ifndef MCUBOOT_OVERWRITE_ONLY |
Christopher Collins | 4772ac4 | 2017-02-27 20:08:01 -0800 | [diff] [blame] | 1247 | static void |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1248 | boot_swap_sectors(int idx, uint32_t sz, struct boot_loader_state *state, |
| 1249 | struct boot_status *bs) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1250 | { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1251 | const struct flash_area *fap_primary_slot; |
| 1252 | const struct flash_area *fap_secondary_slot; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1253 | const struct flash_area *fap_scratch; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1254 | uint32_t copy_sz; |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1255 | uint32_t trailer_sz; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1256 | uint32_t img_off; |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1257 | uint32_t scratch_trailer_off; |
| 1258 | struct boot_swap_state swap_state; |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 1259 | size_t last_sector; |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1260 | bool erase_scratch; |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1261 | uint8_t image_index; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1262 | int rc; |
| 1263 | |
| 1264 | /* Calculate offset from start of image area. */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1265 | img_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1266 | |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1267 | copy_sz = sz; |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1268 | trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state)); |
Fabio Utzig | 9678c97 | 2017-05-23 11:28:56 -0400 | [diff] [blame] | 1269 | |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 1270 | /* sz in this function is always sized on a multiple of the sector size. |
| 1271 | * The check against the start offset of the last sector |
Fabio Utzig | 9678c97 | 2017-05-23 11:28:56 -0400 | [diff] [blame] | 1272 | * is to determine if we're swapping the last sector. The last sector |
| 1273 | * needs special handling because it's where the trailer lives. If we're |
| 1274 | * copying it, we need to use scratch to write the trailer temporarily. |
| 1275 | * |
| 1276 | * NOTE: `use_scratch` is a temporary flag (never written to flash) which |
| 1277 | * controls if special handling is needed (swapping last sector). |
| 1278 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1279 | last_sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1280 | if ((img_off + sz) > |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1281 | boot_img_sector_off(state, BOOT_PRIMARY_SLOT, last_sector)) { |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1282 | copy_sz -= trailer_sz; |
| 1283 | } |
| 1284 | |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 1285 | bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1286 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1287 | image_index = BOOT_CURR_IMG(state); |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1288 | |
| 1289 | rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), |
| 1290 | &fap_primary_slot); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1291 | assert (rc == 0); |
| 1292 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1293 | rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index), |
| 1294 | &fap_secondary_slot); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1295 | assert (rc == 0); |
| 1296 | |
| 1297 | rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch); |
| 1298 | assert (rc == 0); |
| 1299 | |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 1300 | if (bs->state == BOOT_STATUS_STATE_0) { |
Christopher Collins | 2c88e69 | 2019-05-22 15:10:14 -0700 | [diff] [blame] | 1301 | BOOT_LOG_DBG("erasing scratch area"); |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame^] | 1302 | rc = boot_erase_region(fap_scratch, 0, fap_scratch->fa_size); |
Christopher Collins | 4772ac4 | 2017-02-27 20:08:01 -0800 | [diff] [blame] | 1303 | assert(rc == 0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1304 | |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 1305 | if (bs->idx == BOOT_STATUS_IDX_0) { |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1306 | /* Write a trailer to the scratch area, even if we don't need the |
| 1307 | * scratch area for status. We need a temporary place to store the |
| 1308 | * `swap-type` while we erase the primary trailer. |
| 1309 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1310 | rc = boot_status_init(state, fap_scratch, bs); |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1311 | assert(rc == 0); |
| 1312 | |
| 1313 | if (!bs->use_scratch) { |
| 1314 | /* Prepare the primary status area... here it is known that the |
| 1315 | * last sector is not being used by the image data so it's safe |
| 1316 | * to erase. |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1317 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1318 | rc = boot_erase_trailer_sectors(state, fap_primary_slot); |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1319 | assert(rc == 0); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1320 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1321 | rc = boot_status_init(state, fap_primary_slot, bs); |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1322 | assert(rc == 0); |
| 1323 | |
| 1324 | /* Erase the temporary trailer from the scratch area. */ |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame^] | 1325 | rc = boot_erase_region(fap_scratch, 0, fap_scratch->fa_size); |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1326 | assert(rc == 0); |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1327 | } |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1328 | } |
| 1329 | |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame^] | 1330 | rc = boot_copy_region(state, fap_secondary_slot, fap_scratch, |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1331 | img_off, 0, copy_sz); |
| 1332 | assert(rc == 0); |
| 1333 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1334 | rc = boot_write_status(state, bs); |
Fabio Utzig | 4d7396d | 2019-09-05 18:38:05 -0300 | [diff] [blame] | 1335 | bs->state = BOOT_STATUS_STATE_1; |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 1336 | BOOT_STATUS_ASSERT(rc == 0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1337 | } |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1338 | |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 1339 | if (bs->state == BOOT_STATUS_STATE_1) { |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame^] | 1340 | rc = boot_erase_region(fap_secondary_slot, img_off, sz); |
Christopher Collins | 4772ac4 | 2017-02-27 20:08:01 -0800 | [diff] [blame] | 1341 | assert(rc == 0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1342 | |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame^] | 1343 | rc = boot_copy_region(state, fap_primary_slot, fap_secondary_slot, |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1344 | img_off, img_off, copy_sz); |
Christopher Collins | 4772ac4 | 2017-02-27 20:08:01 -0800 | [diff] [blame] | 1345 | assert(rc == 0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1346 | |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 1347 | if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) { |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1348 | /* If not all sectors of the slot are being swapped, |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1349 | * guarantee here that only the primary slot will have the state. |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1350 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1351 | rc = boot_erase_trailer_sectors(state, fap_secondary_slot); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1352 | assert(rc == 0); |
| 1353 | } |
| 1354 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1355 | rc = boot_write_status(state, bs); |
Fabio Utzig | 4d7396d | 2019-09-05 18:38:05 -0300 | [diff] [blame] | 1356 | bs->state = BOOT_STATUS_STATE_2; |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 1357 | BOOT_STATUS_ASSERT(rc == 0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1358 | } |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1359 | |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 1360 | if (bs->state == BOOT_STATUS_STATE_2) { |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame^] | 1361 | rc = boot_erase_region(fap_primary_slot, img_off, sz); |
Christopher Collins | 4772ac4 | 2017-02-27 20:08:01 -0800 | [diff] [blame] | 1362 | assert(rc == 0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1363 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1364 | /* NOTE: If this is the final sector, we exclude the image trailer from |
| 1365 | * this copy (copy_sz was truncated earlier). |
| 1366 | */ |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame^] | 1367 | rc = boot_copy_region(state, fap_scratch, fap_primary_slot, |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1368 | 0, img_off, copy_sz); |
Christopher Collins | 4772ac4 | 2017-02-27 20:08:01 -0800 | [diff] [blame] | 1369 | assert(rc == 0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1370 | |
Fabio Utzig | 94d998c | 2017-05-22 11:02:41 -0400 | [diff] [blame] | 1371 | if (bs->use_scratch) { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1372 | scratch_trailer_off = boot_status_off(fap_scratch); |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1373 | |
| 1374 | /* copy current status that is being maintained in scratch */ |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame^] | 1375 | rc = boot_copy_region(state, fap_scratch, fap_primary_slot, |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1376 | scratch_trailer_off, img_off + copy_sz, |
Fabio Utzig | 4d7396d | 2019-09-05 18:38:05 -0300 | [diff] [blame] | 1377 | (BOOT_STATUS_STATE_COUNT - 1) * BOOT_WRITE_SZ(state)); |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 1378 | BOOT_STATUS_ASSERT(rc == 0); |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1379 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1380 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, |
| 1381 | &swap_state); |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1382 | assert(rc == 0); |
| 1383 | |
Fabio Utzig | de8a38a | 2017-05-23 11:15:01 -0400 | [diff] [blame] | 1384 | if (swap_state.image_ok == BOOT_FLAG_SET) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1385 | rc = boot_write_image_ok(fap_primary_slot); |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1386 | assert(rc == 0); |
| 1387 | } |
| 1388 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1389 | if (swap_state.swap_type != BOOT_SWAP_TYPE_NONE) { |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 1390 | rc = boot_write_swap_info(fap_primary_slot, |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1391 | swap_state.swap_type, image_index); |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1392 | assert(rc == 0); |
| 1393 | } |
| 1394 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1395 | rc = boot_write_swap_size(fap_primary_slot, bs->swap_size); |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 1396 | assert(rc == 0); |
| 1397 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1398 | #ifdef MCUBOOT_ENC_IMAGES |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1399 | rc = boot_write_enc_key(fap_primary_slot, 0, bs->enckey[0]); |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1400 | assert(rc == 0); |
| 1401 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1402 | rc = boot_write_enc_key(fap_primary_slot, 1, bs->enckey[1]); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1403 | assert(rc == 0); |
| 1404 | #endif |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1405 | rc = boot_write_magic(fap_primary_slot); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1406 | assert(rc == 0); |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1407 | } |
| 1408 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1409 | /* If we wrote a trailer to the scratch area, erase it after we persist |
| 1410 | * a trailer to the primary slot. We do this to prevent mcuboot from |
| 1411 | * reading a stale status from the scratch area in case of immediate |
| 1412 | * reset. |
| 1413 | */ |
| 1414 | erase_scratch = bs->use_scratch; |
| 1415 | bs->use_scratch = 0; |
| 1416 | |
Fabio Utzig | 4d7396d | 2019-09-05 18:38:05 -0300 | [diff] [blame] | 1417 | rc = boot_write_status(state, bs); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1418 | bs->idx++; |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 1419 | bs->state = BOOT_STATUS_STATE_0; |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 1420 | BOOT_STATUS_ASSERT(rc == 0); |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1421 | |
| 1422 | if (erase_scratch) { |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame^] | 1423 | rc = boot_erase_region(fap_scratch, 0, sz); |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1424 | assert(rc == 0); |
| 1425 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1426 | } |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1427 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1428 | flash_area_close(fap_primary_slot); |
| 1429 | flash_area_close(fap_secondary_slot); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1430 | flash_area_close(fap_scratch); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1431 | } |
Fabio Utzig | 3488eef | 2017-06-12 10:25:43 -0300 | [diff] [blame] | 1432 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1433 | |
| 1434 | /** |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1435 | * Overwrite primary slot with the image contained in the secondary slot. |
| 1436 | * If a prior copy operation was interrupted by a system reset, this function |
| 1437 | * redos the copy. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1438 | * |
| 1439 | * @param bs The current boot status. This function reads |
| 1440 | * this struct to determine if it is resuming |
| 1441 | * an interrupted swap operation. This |
| 1442 | * function writes the updated status to this |
| 1443 | * function on return. |
| 1444 | * |
| 1445 | * @return 0 on success; nonzero on failure. |
| 1446 | */ |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 1447 | #if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP) |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 1448 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1449 | boot_copy_image(struct boot_loader_state *state, struct boot_status *bs) |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 1450 | { |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 1451 | size_t sect_count; |
| 1452 | size_t sect; |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 1453 | int rc; |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 1454 | size_t size; |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 1455 | size_t this_size; |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 1456 | size_t last_sector; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1457 | const struct flash_area *fap_primary_slot; |
| 1458 | const struct flash_area *fap_secondary_slot; |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1459 | uint8_t image_index; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1460 | |
Fabio Utzig | aaf767c | 2017-12-05 10:22:46 -0200 | [diff] [blame] | 1461 | (void)bs; |
| 1462 | |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 1463 | #if defined(MCUBOOT_OVERWRITE_ONLY_FAST) |
| 1464 | uint32_t src_size = 0; |
Fabio Utzig | d638b17 | 2019-08-09 10:38:05 -0300 | [diff] [blame] | 1465 | rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &src_size); |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 1466 | assert(rc == 0); |
| 1467 | #endif |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 1468 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1469 | BOOT_LOG_INF("Image upgrade secondary slot -> primary slot"); |
| 1470 | BOOT_LOG_INF("Erasing the primary slot"); |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 1471 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1472 | image_index = BOOT_CURR_IMG(state); |
| 1473 | |
| 1474 | rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), |
| 1475 | &fap_primary_slot); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1476 | assert (rc == 0); |
| 1477 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1478 | rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index), |
| 1479 | &fap_secondary_slot); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1480 | assert (rc == 0); |
| 1481 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1482 | sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT); |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 1483 | for (sect = 0, size = 0; sect < sect_count; sect++) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1484 | this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect); |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame^] | 1485 | rc = boot_erase_region(fap_primary_slot, size, this_size); |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 1486 | assert(rc == 0); |
| 1487 | |
| 1488 | size += this_size; |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 1489 | |
| 1490 | #if defined(MCUBOOT_OVERWRITE_ONLY_FAST) |
| 1491 | if (size >= src_size) { |
| 1492 | break; |
| 1493 | } |
| 1494 | #endif |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 1495 | } |
| 1496 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1497 | #ifdef MCUBOOT_ENC_IMAGES |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1498 | if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) { |
Fabio Utzig | 1e4284b | 2019-08-23 11:55:27 -0300 | [diff] [blame] | 1499 | rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1500 | boot_img_hdr(state, BOOT_SECONDARY_SLOT), |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1501 | fap_secondary_slot, bs->enckey[1]); |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1502 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1503 | if (rc < 0) { |
| 1504 | return BOOT_EBADIMAGE; |
| 1505 | } |
Fabio Utzig | 1e4284b | 2019-08-23 11:55:27 -0300 | [diff] [blame] | 1506 | if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs->enckey[1])) { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1507 | return BOOT_EBADIMAGE; |
| 1508 | } |
| 1509 | } |
| 1510 | #endif |
| 1511 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1512 | BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes", |
| 1513 | size); |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame^] | 1514 | rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot, 0, 0, size); |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 1515 | |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 1516 | /* |
| 1517 | * Erases header and trailer. The trailer is erased because when a new |
| 1518 | * image is written without a trailer as is the case when using newt, the |
| 1519 | * trailer that was left might trigger a new upgrade. |
| 1520 | */ |
Christopher Collins | 2c88e69 | 2019-05-22 15:10:14 -0700 | [diff] [blame] | 1521 | BOOT_LOG_DBG("erasing secondary header"); |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame^] | 1522 | rc = boot_erase_region(fap_secondary_slot, |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1523 | boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0), |
| 1524 | boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0)); |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 1525 | assert(rc == 0); |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1526 | last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1; |
Christopher Collins | 2c88e69 | 2019-05-22 15:10:14 -0700 | [diff] [blame] | 1527 | BOOT_LOG_DBG("erasing secondary trailer"); |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame^] | 1528 | rc = boot_erase_region(fap_secondary_slot, |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1529 | boot_img_sector_off(state, BOOT_SECONDARY_SLOT, |
| 1530 | last_sector), |
| 1531 | boot_img_sector_size(state, BOOT_SECONDARY_SLOT, |
| 1532 | last_sector)); |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 1533 | assert(rc == 0); |
| 1534 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1535 | flash_area_close(fap_primary_slot); |
| 1536 | flash_area_close(fap_secondary_slot); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1537 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1538 | /* TODO: Perhaps verify the primary slot's signature again? */ |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 1539 | |
| 1540 | return 0; |
| 1541 | } |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 1542 | #endif |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1543 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1544 | #if !defined(MCUBOOT_OVERWRITE_ONLY) |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1545 | /** |
| 1546 | * Swaps the two images in flash. If a prior copy operation was interrupted |
| 1547 | * by a system reset, this function completes that operation. |
| 1548 | * |
| 1549 | * @param bs The current boot status. This function reads |
| 1550 | * this struct to determine if it is resuming |
| 1551 | * an interrupted swap operation. This |
| 1552 | * function writes the updated status to this |
| 1553 | * function on return. |
| 1554 | * |
| 1555 | * @return 0 on success; nonzero on failure. |
| 1556 | */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1557 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1558 | boot_swap_image(struct boot_loader_state *state, struct boot_status *bs) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1559 | { |
| 1560 | uint32_t sz; |
| 1561 | int first_sector_idx; |
| 1562 | int last_sector_idx; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1563 | int last_idx_secondary_slot; |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 1564 | uint32_t swap_idx; |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1565 | struct image_header *hdr; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1566 | #ifdef MCUBOOT_ENC_IMAGES |
| 1567 | const struct flash_area *fap; |
| 1568 | uint8_t slot; |
| 1569 | uint8_t i; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1570 | #endif |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1571 | uint32_t size; |
| 1572 | uint32_t copy_size; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1573 | uint32_t primary_slot_size; |
| 1574 | uint32_t secondary_slot_size; |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1575 | uint8_t image_index; |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1576 | int rc; |
| 1577 | |
| 1578 | /* FIXME: just do this if asked by user? */ |
| 1579 | |
| 1580 | size = copy_size = 0; |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1581 | image_index = BOOT_CURR_IMG(state); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1582 | |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 1583 | 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] | 1584 | /* |
| 1585 | * No swap ever happened, so need to find the largest image which |
| 1586 | * will be used to determine the amount of sectors to swap. |
| 1587 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1588 | hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1589 | if (hdr->ih_magic == IMAGE_MAGIC) { |
Fabio Utzig | d638b17 | 2019-08-09 10:38:05 -0300 | [diff] [blame] | 1590 | rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, ©_size); |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 1591 | assert(rc == 0); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1592 | } |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1593 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1594 | #ifdef MCUBOOT_ENC_IMAGES |
Fabio Utzig | 2fc80df | 2018-12-14 06:47:38 -0200 | [diff] [blame] | 1595 | if (IS_ENCRYPTED(hdr)) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1596 | fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT); |
Fabio Utzig | 1e4284b | 2019-08-23 11:55:27 -0300 | [diff] [blame] | 1597 | rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs->enckey[0]); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1598 | assert(rc >= 0); |
| 1599 | |
| 1600 | if (rc == 0) { |
Fabio Utzig | 1e4284b | 2019-08-23 11:55:27 -0300 | [diff] [blame] | 1601 | rc = boot_enc_set_key(BOOT_CURR_ENC(state), 0, bs->enckey[0]); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1602 | assert(rc == 0); |
| 1603 | } else { |
| 1604 | rc = 0; |
| 1605 | } |
| 1606 | } else { |
| 1607 | memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE); |
| 1608 | } |
| 1609 | #endif |
| 1610 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1611 | hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT); |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 1612 | if (hdr->ih_magic == IMAGE_MAGIC) { |
Fabio Utzig | d638b17 | 2019-08-09 10:38:05 -0300 | [diff] [blame] | 1613 | rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size); |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 1614 | assert(rc == 0); |
| 1615 | } |
| 1616 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1617 | #ifdef MCUBOOT_ENC_IMAGES |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1618 | hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT); |
Fabio Utzig | 2fc80df | 2018-12-14 06:47:38 -0200 | [diff] [blame] | 1619 | if (IS_ENCRYPTED(hdr)) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1620 | fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT); |
Fabio Utzig | 1e4284b | 2019-08-23 11:55:27 -0300 | [diff] [blame] | 1621 | rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs->enckey[1]); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1622 | assert(rc >= 0); |
| 1623 | |
| 1624 | if (rc == 0) { |
Fabio Utzig | 1e4284b | 2019-08-23 11:55:27 -0300 | [diff] [blame] | 1625 | rc = boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs->enckey[1]); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1626 | assert(rc == 0); |
| 1627 | } else { |
| 1628 | rc = 0; |
| 1629 | } |
| 1630 | } else { |
| 1631 | memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE); |
| 1632 | } |
| 1633 | #endif |
| 1634 | |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 1635 | if (size > copy_size) { |
| 1636 | copy_size = size; |
| 1637 | } |
| 1638 | |
| 1639 | bs->swap_size = copy_size; |
| 1640 | } else { |
| 1641 | /* |
| 1642 | * If a swap was under way, the swap_size should already be present |
| 1643 | * in the trailer... |
| 1644 | */ |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1645 | rc = boot_read_swap_size(image_index, &bs->swap_size); |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 1646 | assert(rc == 0); |
| 1647 | |
| 1648 | copy_size = bs->swap_size; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1649 | |
| 1650 | #ifdef MCUBOOT_ENC_IMAGES |
| 1651 | for (slot = 0; slot <= 1; slot++) { |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1652 | rc = boot_read_enc_key(image_index, slot, bs->enckey[slot]); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1653 | assert(rc == 0); |
| 1654 | |
| 1655 | for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) { |
Fabio Utzig | 1c7d959 | 2018-12-03 10:35:56 -0200 | [diff] [blame] | 1656 | if (bs->enckey[slot][i] != 0xff) { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1657 | break; |
| 1658 | } |
| 1659 | } |
| 1660 | |
| 1661 | if (i != BOOT_ENC_KEY_SIZE) { |
Fabio Utzig | 1e4284b | 2019-08-23 11:55:27 -0300 | [diff] [blame] | 1662 | boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs->enckey[slot]); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1663 | } |
| 1664 | } |
| 1665 | #endif |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1666 | } |
| 1667 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1668 | primary_slot_size = 0; |
| 1669 | secondary_slot_size = 0; |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1670 | last_sector_idx = 0; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1671 | last_idx_secondary_slot = 0; |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 1672 | |
| 1673 | /* |
| 1674 | * Knowing the size of the largest image between both slots, here we |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1675 | * find what is the last sector in the primary slot that needs swapping. |
| 1676 | * Since we already know that both slots are compatible, the secondary |
| 1677 | * 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] | 1678 | */ |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1679 | while (1) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1680 | if ((primary_slot_size < copy_size) || |
| 1681 | (primary_slot_size < secondary_slot_size)) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1682 | primary_slot_size += boot_img_sector_size(state, |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1683 | BOOT_PRIMARY_SLOT, |
| 1684 | last_sector_idx); |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 1685 | } |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1686 | if ((secondary_slot_size < copy_size) || |
| 1687 | (secondary_slot_size < primary_slot_size)) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1688 | secondary_slot_size += boot_img_sector_size(state, |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1689 | BOOT_SECONDARY_SLOT, |
| 1690 | last_idx_secondary_slot); |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 1691 | } |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1692 | if (primary_slot_size >= copy_size && |
| 1693 | secondary_slot_size >= copy_size && |
| 1694 | primary_slot_size == secondary_slot_size) { |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1695 | break; |
| 1696 | } |
| 1697 | last_sector_idx++; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1698 | last_idx_secondary_slot++; |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1699 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1700 | |
| 1701 | swap_idx = 0; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1702 | while (last_sector_idx >= 0) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1703 | sz = boot_copy_sz(state, last_sector_idx, &first_sector_idx); |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 1704 | if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1705 | boot_swap_sectors(first_sector_idx, sz, state, bs); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | last_sector_idx = first_sector_idx - 1; |
| 1709 | swap_idx++; |
| 1710 | } |
| 1711 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1712 | #ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 1713 | if (boot_status_fails > 0) { |
Christopher Collins | 2c88e69 | 2019-05-22 15:10:14 -0700 | [diff] [blame] | 1714 | BOOT_LOG_WRN("%d status write fails performing the swap", |
| 1715 | boot_status_fails); |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 1716 | } |
| 1717 | #endif |
| 1718 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1719 | return 0; |
| 1720 | } |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 1721 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1722 | |
| 1723 | /** |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1724 | * Marks the image in the primary slot as fully copied. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1725 | */ |
Fabio Utzig | 8d0e588 | 2017-09-13 17:32:44 -0300 | [diff] [blame] | 1726 | #ifndef MCUBOOT_OVERWRITE_ONLY |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1727 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1728 | boot_set_copy_done(uint8_t image_index) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1729 | { |
| 1730 | const struct flash_area *fap; |
| 1731 | int rc; |
| 1732 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1733 | rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), |
| 1734 | &fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1735 | if (rc != 0) { |
| 1736 | return BOOT_EFLASH; |
| 1737 | } |
| 1738 | |
| 1739 | rc = boot_write_copy_done(fap); |
Fabio Utzig | db5bd3c | 2017-07-13 22:20:22 -0300 | [diff] [blame] | 1740 | flash_area_close(fap); |
| 1741 | return rc; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1742 | } |
Fabio Utzig | 8d0e588 | 2017-09-13 17:32:44 -0300 | [diff] [blame] | 1743 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1744 | |
| 1745 | /** |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1746 | * Marks a reverted image in the primary slot as confirmed. This is necessary to |
| 1747 | * ensure the status bytes from the image revert operation don't get processed |
| 1748 | * on a subsequent boot. |
Fabio Utzig | 1e56fcc | 2017-07-17 15:39:14 -0300 | [diff] [blame] | 1749 | * |
| 1750 | * 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] | 1751 | * image installed on the primary slot and the new image to be upgrade to has a |
| 1752 | * bad sig, image_ok would be overwritten. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1753 | */ |
Fabio Utzig | 8d0e588 | 2017-09-13 17:32:44 -0300 | [diff] [blame] | 1754 | #ifndef MCUBOOT_OVERWRITE_ONLY |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1755 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1756 | boot_set_image_ok(uint8_t image_index) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1757 | { |
| 1758 | const struct flash_area *fap; |
Fabio Utzig | 1e56fcc | 2017-07-17 15:39:14 -0300 | [diff] [blame] | 1759 | struct boot_swap_state state; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1760 | int rc; |
| 1761 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1762 | rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), |
| 1763 | &fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1764 | if (rc != 0) { |
| 1765 | return BOOT_EFLASH; |
| 1766 | } |
| 1767 | |
Fabio Utzig | 1e56fcc | 2017-07-17 15:39:14 -0300 | [diff] [blame] | 1768 | rc = boot_read_swap_state(fap, &state); |
| 1769 | if (rc != 0) { |
| 1770 | rc = BOOT_EFLASH; |
| 1771 | goto out; |
| 1772 | } |
| 1773 | |
| 1774 | if (state.image_ok == BOOT_FLAG_UNSET) { |
| 1775 | rc = boot_write_image_ok(fap); |
| 1776 | } |
| 1777 | |
| 1778 | out: |
Fabio Utzig | db5bd3c | 2017-07-13 22:20:22 -0300 | [diff] [blame] | 1779 | flash_area_close(fap); |
| 1780 | return rc; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1781 | } |
Fabio Utzig | 8d0e588 | 2017-09-13 17:32:44 -0300 | [diff] [blame] | 1782 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1783 | |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1784 | #if (BOOT_IMAGE_NUMBER > 1) |
| 1785 | /** |
Fabio Utzig | 019a81a | 2019-08-27 10:33:39 -0300 | [diff] [blame] | 1786 | * Check if the version of the image is not older than required. |
| 1787 | * |
| 1788 | * @param req Required minimal image version. |
| 1789 | * @param ver Version of the image to be checked. |
| 1790 | * |
| 1791 | * @return 0 if the version is sufficient, nonzero otherwise. |
| 1792 | */ |
| 1793 | static int |
| 1794 | boot_is_version_sufficient(struct image_version *req, |
| 1795 | struct image_version *ver) |
| 1796 | { |
| 1797 | if (ver->iv_major > req->iv_major) { |
| 1798 | return 0; |
| 1799 | } |
| 1800 | if (ver->iv_major < req->iv_major) { |
| 1801 | return BOOT_EBADVERSION; |
| 1802 | } |
| 1803 | /* The major version numbers are equal. */ |
| 1804 | if (ver->iv_minor > req->iv_minor) { |
| 1805 | return 0; |
| 1806 | } |
| 1807 | if (ver->iv_minor < req->iv_minor) { |
| 1808 | return BOOT_EBADVERSION; |
| 1809 | } |
| 1810 | /* The minor version numbers are equal. */ |
| 1811 | if (ver->iv_revision < req->iv_revision) { |
| 1812 | return BOOT_EBADVERSION; |
| 1813 | } |
| 1814 | |
| 1815 | return 0; |
| 1816 | } |
| 1817 | |
| 1818 | /** |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1819 | * Check the image dependency whether it is satisfied and modify |
| 1820 | * the swap type if necessary. |
| 1821 | * |
| 1822 | * @param dep Image dependency which has to be verified. |
| 1823 | * |
| 1824 | * @return 0 on success; nonzero on failure. |
| 1825 | */ |
| 1826 | static int |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 1827 | boot_verify_slot_dependency(struct boot_loader_state *state, |
| 1828 | struct image_dependency *dep) |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1829 | { |
| 1830 | struct image_version *dep_version; |
| 1831 | size_t dep_slot; |
| 1832 | int rc; |
David Brown | e6ab34c | 2019-09-03 12:24:21 -0600 | [diff] [blame] | 1833 | uint8_t swap_type; |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1834 | |
| 1835 | /* Determine the source of the image which is the subject of |
| 1836 | * the dependency and get it's version. */ |
David Brown | e6ab34c | 2019-09-03 12:24:21 -0600 | [diff] [blame] | 1837 | swap_type = state->swap_type[dep->image_id]; |
| 1838 | dep_slot = (swap_type != BOOT_SWAP_TYPE_NONE && swap_type != BOOT_SWAP_TYPE_FAIL) ? |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1839 | BOOT_SECONDARY_SLOT : BOOT_PRIMARY_SLOT; |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1840 | dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver; |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1841 | |
| 1842 | rc = boot_is_version_sufficient(&dep->image_min_version, dep_version); |
| 1843 | if (rc != 0) { |
| 1844 | /* Dependency not satisfied. |
| 1845 | * Modify the swap type to decrease the version number of the image |
| 1846 | * (which will be located in the primary slot after the boot process), |
| 1847 | * consequently the number of unsatisfied dependencies will be |
| 1848 | * decreased or remain the same. |
| 1849 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1850 | switch (BOOT_SWAP_TYPE(state)) { |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1851 | case BOOT_SWAP_TYPE_TEST: |
| 1852 | case BOOT_SWAP_TYPE_PERM: |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1853 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE; |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1854 | break; |
| 1855 | case BOOT_SWAP_TYPE_NONE: |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1856 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT; |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1857 | break; |
| 1858 | default: |
| 1859 | break; |
| 1860 | } |
| 1861 | } |
| 1862 | |
| 1863 | return rc; |
| 1864 | } |
| 1865 | |
| 1866 | /** |
| 1867 | * Read all dependency TLVs of an image from the flash and verify |
| 1868 | * one after another to see if they are all satisfied. |
| 1869 | * |
| 1870 | * @param slot Image slot number. |
| 1871 | * |
| 1872 | * @return 0 on success; nonzero on failure. |
| 1873 | */ |
| 1874 | static int |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 1875 | boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot) |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1876 | { |
| 1877 | const struct flash_area *fap; |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1878 | struct image_tlv tlv; |
| 1879 | struct image_dependency dep; |
| 1880 | uint32_t off; |
| 1881 | uint32_t end; |
| 1882 | bool dep_tlvs_found = false; |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1883 | int area_id; |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1884 | int rc; |
| 1885 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1886 | area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot); |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1887 | rc = flash_area_open(area_id, &fap); |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1888 | if (rc != 0) { |
| 1889 | rc = BOOT_EFLASH; |
| 1890 | goto done; |
| 1891 | } |
| 1892 | |
Fabio Utzig | 233af7d | 2019-08-26 12:06:16 -0300 | [diff] [blame] | 1893 | rc = boot_find_tlv_offs(boot_img_hdr(state, slot), fap, &off, &end); |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1894 | if (rc != 0) { |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1895 | goto done; |
| 1896 | } |
| 1897 | |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1898 | /* Traverse through all of the TLVs to find the dependency TLVs. */ |
| 1899 | for (; off < end; off += sizeof(tlv) + tlv.it_len) { |
| 1900 | rc = flash_area_read(fap, off, &tlv, sizeof(tlv)); |
| 1901 | if (rc != 0) { |
| 1902 | rc = BOOT_EFLASH; |
| 1903 | goto done; |
| 1904 | } |
| 1905 | |
| 1906 | if (tlv.it_type == IMAGE_TLV_DEPENDENCY) { |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 1907 | dep_tlvs_found = true; |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1908 | |
| 1909 | if (tlv.it_len != sizeof(dep)) { |
| 1910 | rc = BOOT_EBADIMAGE; |
| 1911 | goto done; |
| 1912 | } |
| 1913 | |
| 1914 | rc = flash_area_read(fap, off + sizeof(tlv), &dep, tlv.it_len); |
| 1915 | if (rc != 0) { |
| 1916 | rc = BOOT_EFLASH; |
| 1917 | goto done; |
| 1918 | } |
| 1919 | |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 1920 | if (dep.image_id >= BOOT_IMAGE_NUMBER) { |
| 1921 | rc = BOOT_EBADARGS; |
| 1922 | goto done; |
| 1923 | } |
| 1924 | |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1925 | /* Verify dependency and modify the swap type if not satisfied. */ |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 1926 | rc = boot_verify_slot_dependency(state, &dep); |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1927 | if (rc != 0) { |
| 1928 | /* Dependency not satisfied. */ |
| 1929 | goto done; |
| 1930 | } |
| 1931 | |
| 1932 | /* Dependency satisfied, no action needed. |
| 1933 | * Continue with the next TLV entry. |
| 1934 | */ |
| 1935 | } else if (dep_tlvs_found) { |
| 1936 | /* The dependency TLVs are contiguous in the TLV area. If a |
| 1937 | * dependency had already been found and the last read TLV |
| 1938 | * has a different type then there are no more dependency TLVs. |
| 1939 | * The search can be finished. |
| 1940 | */ |
| 1941 | break; |
| 1942 | } |
| 1943 | } |
| 1944 | |
| 1945 | done: |
| 1946 | flash_area_close(fap); |
| 1947 | return rc; |
| 1948 | } |
| 1949 | |
| 1950 | /** |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1951 | * Iterate over all the images and verify whether the image dependencies in the |
| 1952 | * TLV area are all satisfied and update the related swap type if necessary. |
| 1953 | */ |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 1954 | static int |
| 1955 | boot_verify_dependencies(struct boot_loader_state *state) |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1956 | { |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1957 | int rc; |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 1958 | uint8_t slot; |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1959 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1960 | BOOT_CURR_IMG(state) = 0; |
| 1961 | while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) { |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 1962 | if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE && |
| 1963 | BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) { |
| 1964 | slot = BOOT_SECONDARY_SLOT; |
| 1965 | } else { |
| 1966 | slot = BOOT_PRIMARY_SLOT; |
| 1967 | } |
| 1968 | |
| 1969 | rc = boot_verify_slot_dependencies(state, slot); |
Fabio Utzig | abec073 | 2019-07-31 08:40:22 -0300 | [diff] [blame] | 1970 | if (rc == 0) { |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1971 | /* All dependencies've been satisfied, continue with next image. */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1972 | BOOT_CURR_IMG(state)++; |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1973 | } else if (rc == BOOT_EBADVERSION) { |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 1974 | /* Cannot upgrade due to non-met dependencies, so disable all |
| 1975 | * image upgrades. |
| 1976 | */ |
| 1977 | for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) { |
| 1978 | BOOT_CURR_IMG(state) = idx; |
| 1979 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE; |
| 1980 | } |
| 1981 | break; |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1982 | } else { |
| 1983 | /* Other error happened, images are inconsistent */ |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 1984 | return rc; |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1985 | } |
| 1986 | } |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 1987 | return rc; |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1988 | } |
| 1989 | #endif /* (BOOT_IMAGE_NUMBER > 1) */ |
| 1990 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1991 | /** |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1992 | * Performs a clean (not aborted) image update. |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1993 | * |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1994 | * @param bs The current boot status. |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1995 | * |
| 1996 | * @return 0 on success; nonzero on failure. |
| 1997 | */ |
| 1998 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1999 | boot_perform_update(struct boot_loader_state *state, struct boot_status *bs) |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 2000 | { |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 2001 | int rc; |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2002 | #ifndef MCUBOOT_OVERWRITE_ONLY |
| 2003 | uint8_t swap_type; |
| 2004 | #endif |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 2005 | |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2006 | /* At this point there are no aborted swaps. */ |
| 2007 | #if defined(MCUBOOT_OVERWRITE_ONLY) |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2008 | rc = boot_copy_image(state, bs); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2009 | #elif defined(MCUBOOT_BOOTSTRAP) |
| 2010 | /* Check if the image update was triggered by a bad image in the |
| 2011 | * primary slot (the validity of the image in the secondary slot had |
| 2012 | * already been checked). |
| 2013 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2014 | if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 || |
| 2015 | boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) { |
| 2016 | rc = boot_copy_image(state, bs); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2017 | } else { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2018 | rc = boot_swap_image(state, bs); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2019 | } |
| 2020 | #else |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2021 | rc = boot_swap_image(state, bs); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2022 | #endif |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 2023 | assert(rc == 0); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2024 | |
| 2025 | #ifndef MCUBOOT_OVERWRITE_ONLY |
| 2026 | /* The following state needs image_ok be explicitly set after the |
| 2027 | * swap was finished to avoid a new revert. |
| 2028 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2029 | swap_type = BOOT_SWAP_TYPE(state); |
| 2030 | if (swap_type == BOOT_SWAP_TYPE_REVERT || |
| 2031 | swap_type == BOOT_SWAP_TYPE_PERM) { |
| 2032 | rc = boot_set_image_ok(BOOT_CURR_IMG(state)); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2033 | if (rc != 0) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2034 | BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2035 | } |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 2036 | } |
| 2037 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2038 | if (swap_type == BOOT_SWAP_TYPE_TEST || |
| 2039 | swap_type == BOOT_SWAP_TYPE_PERM || |
| 2040 | swap_type == BOOT_SWAP_TYPE_REVERT) { |
| 2041 | rc = boot_set_copy_done(BOOT_CURR_IMG(state)); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2042 | if (rc != 0) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2043 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC; |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 2044 | } |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2045 | } |
| 2046 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 2047 | |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2048 | return rc; |
| 2049 | } |
| 2050 | |
| 2051 | /** |
| 2052 | * Completes a previously aborted image swap. |
| 2053 | * |
| 2054 | * @param bs The current boot status. |
| 2055 | * |
| 2056 | * @return 0 on success; nonzero on failure. |
| 2057 | */ |
| 2058 | #if !defined(MCUBOOT_OVERWRITE_ONLY) |
| 2059 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2060 | boot_complete_partial_swap(struct boot_loader_state *state, |
| 2061 | struct boot_status *bs) |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2062 | { |
| 2063 | int rc; |
| 2064 | |
| 2065 | /* Determine the type of swap operation being resumed from the |
| 2066 | * `swap-type` trailer field. |
| 2067 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2068 | rc = boot_swap_image(state, bs); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2069 | assert(rc == 0); |
| 2070 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2071 | BOOT_SWAP_TYPE(state) = bs->swap_type; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2072 | |
| 2073 | /* The following states need image_ok be explicitly set after the |
| 2074 | * swap was finished to avoid a new revert. |
| 2075 | */ |
| 2076 | if (bs->swap_type == BOOT_SWAP_TYPE_REVERT || |
| 2077 | bs->swap_type == BOOT_SWAP_TYPE_PERM) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2078 | rc = boot_set_image_ok(BOOT_CURR_IMG(state)); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2079 | if (rc != 0) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2080 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2081 | } |
| 2082 | } |
| 2083 | |
| 2084 | if (bs->swap_type == BOOT_SWAP_TYPE_TEST || |
| 2085 | bs->swap_type == BOOT_SWAP_TYPE_PERM || |
| 2086 | bs->swap_type == BOOT_SWAP_TYPE_REVERT) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2087 | rc = boot_set_copy_done(BOOT_CURR_IMG(state)); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2088 | if (rc != 0) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2089 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2090 | } |
| 2091 | } |
| 2092 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2093 | if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2094 | BOOT_LOG_ERR("panic!"); |
| 2095 | assert(0); |
| 2096 | |
| 2097 | /* Loop forever... */ |
| 2098 | while (1) {} |
| 2099 | } |
| 2100 | |
| 2101 | return rc; |
| 2102 | } |
| 2103 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
| 2104 | |
| 2105 | #if (BOOT_IMAGE_NUMBER > 1) |
| 2106 | /** |
| 2107 | * Review the validity of previously determined swap types of other images. |
| 2108 | * |
| 2109 | * @param aborted_swap The current image upgrade is a |
| 2110 | * partial/aborted swap. |
| 2111 | */ |
| 2112 | static void |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2113 | boot_review_image_swap_types(struct boot_loader_state *state, |
| 2114 | bool aborted_swap) |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2115 | { |
| 2116 | /* In that case if we rebooted in the middle of an image upgrade process, we |
| 2117 | * must review the validity of swap types, that were previously determined |
| 2118 | * for other images. The image_ok flag had not been set before the reboot |
| 2119 | * for any of the updated images (only the copy_done flag) and thus falsely |
| 2120 | * the REVERT swap type has been determined for the previous images that had |
| 2121 | * been updated before the reboot. |
| 2122 | * |
| 2123 | * There are two separate scenarios that we have to deal with: |
| 2124 | * |
| 2125 | * 1. The reboot has happened during swapping an image: |
| 2126 | * The current image upgrade has been determined as a |
| 2127 | * partial/aborted swap. |
| 2128 | * 2. The reboot has happened between two separate image upgrades: |
| 2129 | * In this scenario we must check the swap type of the current image. |
| 2130 | * In those cases if it is NONE or REVERT we cannot certainly determine |
| 2131 | * the fact of a reboot. In a consistent state images must move in the |
| 2132 | * same direction or stay in place, e.g. in practice REVERT and TEST |
| 2133 | * swap types cannot be present at the same time. If the swap type of |
| 2134 | * the current image is either TEST, PERM or FAIL we must review the |
| 2135 | * already determined swap types of other images and set each false |
| 2136 | * REVERT swap types to NONE (these images had been successfully |
| 2137 | * updated before the system rebooted between two separate image |
| 2138 | * upgrades). |
| 2139 | */ |
| 2140 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2141 | if (BOOT_CURR_IMG(state) == 0) { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2142 | /* Nothing to do */ |
| 2143 | return; |
| 2144 | } |
| 2145 | |
| 2146 | if (!aborted_swap) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2147 | if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) || |
| 2148 | (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2149 | /* Nothing to do */ |
| 2150 | return; |
| 2151 | } |
| 2152 | } |
| 2153 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2154 | for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) { |
| 2155 | if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) { |
| 2156 | state->swap_type[i] = BOOT_SWAP_TYPE_NONE; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2157 | } |
| 2158 | } |
| 2159 | } |
| 2160 | #endif |
| 2161 | |
| 2162 | /** |
| 2163 | * Prepare image to be updated if required. |
| 2164 | * |
| 2165 | * Prepare image to be updated if required with completing an image swap |
| 2166 | * operation if one was aborted and/or determining the type of the |
| 2167 | * swap operation. In case of any error set the swap type to NONE. |
| 2168 | * |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2169 | * @param state TODO |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2170 | * @param bs Pointer where the read and possibly updated |
| 2171 | * boot status can be written to. |
| 2172 | */ |
| 2173 | static void |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2174 | boot_prepare_image_for_update(struct boot_loader_state *state, |
| 2175 | struct boot_status *bs) |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2176 | { |
| 2177 | int rc; |
| 2178 | |
| 2179 | /* Determine the sector layout of the image slots and scratch area. */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2180 | rc = boot_read_sectors(state); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2181 | if (rc != 0) { |
| 2182 | BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d" |
| 2183 | " - too small?", BOOT_MAX_IMG_SECTORS); |
| 2184 | /* Unable to determine sector layout, continue with next image |
| 2185 | * if there is one. |
| 2186 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2187 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2188 | return; |
| 2189 | } |
| 2190 | |
| 2191 | /* Attempt to read an image header from each slot. */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2192 | rc = boot_read_image_headers(state, false); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2193 | if (rc != 0) { |
| 2194 | /* Continue with next image if there is one. */ |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 2195 | BOOT_LOG_WRN("Failed reading image headers; Image=%u", |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2196 | BOOT_CURR_IMG(state)); |
| 2197 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2198 | return; |
| 2199 | } |
| 2200 | |
| 2201 | /* If the current image's slots aren't compatible, no swap is possible. |
| 2202 | * Just boot into primary slot. |
| 2203 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2204 | if (boot_slots_compatible(state)) { |
| 2205 | rc = boot_read_status(state, bs); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2206 | if (rc != 0) { |
| 2207 | BOOT_LOG_WRN("Failed reading boot status; Image=%u", |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2208 | BOOT_CURR_IMG(state)); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2209 | /* Continue with next image if there is one. */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2210 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2211 | return; |
| 2212 | } |
| 2213 | |
| 2214 | /* Determine if we rebooted in the middle of an image swap |
| 2215 | * operation. If a partial swap was detected, complete it. |
| 2216 | */ |
| 2217 | if (bs->idx != BOOT_STATUS_IDX_0 || bs->state != BOOT_STATUS_STATE_0) { |
| 2218 | |
| 2219 | #if (BOOT_IMAGE_NUMBER > 1) |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2220 | boot_review_image_swap_types(state, true); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2221 | #endif |
| 2222 | |
| 2223 | #ifdef MCUBOOT_OVERWRITE_ONLY |
| 2224 | /* Should never arrive here, overwrite-only mode has |
| 2225 | * no swap state. |
| 2226 | */ |
| 2227 | assert(0); |
| 2228 | #else |
| 2229 | /* Determine the type of swap operation being resumed from the |
| 2230 | * `swap-type` trailer field. |
| 2231 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2232 | rc = boot_complete_partial_swap(state, bs); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2233 | assert(rc == 0); |
| 2234 | #endif |
| 2235 | /* Attempt to read an image header from each slot. Ensure that |
| 2236 | * image headers in slots are aligned with headers in boot_data. |
| 2237 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2238 | rc = boot_read_image_headers(state, false); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2239 | assert(rc == 0); |
| 2240 | |
| 2241 | /* Swap has finished set to NONE */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2242 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2243 | } else { |
| 2244 | /* There was no partial swap, determine swap type. */ |
| 2245 | if (bs->swap_type == BOOT_SWAP_TYPE_NONE) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2246 | BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs); |
| 2247 | } else if (boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) != 0) { |
| 2248 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2249 | } else { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2250 | BOOT_SWAP_TYPE(state) = bs->swap_type; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2251 | } |
| 2252 | |
| 2253 | #if (BOOT_IMAGE_NUMBER > 1) |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2254 | boot_review_image_swap_types(state, false); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2255 | #endif |
| 2256 | |
| 2257 | #ifdef MCUBOOT_BOOTSTRAP |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2258 | if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2259 | /* Header checks are done first because they are |
| 2260 | * inexpensive. Since overwrite-only copies starting from |
| 2261 | * offset 0, if interrupted, it might leave a valid header |
| 2262 | * magic, so also run validation on the primary slot to be |
| 2263 | * sure it's not OK. |
| 2264 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2265 | if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 || |
| 2266 | boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) { |
| 2267 | if (boot_img_hdr(state, |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2268 | BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC && |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2269 | boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) == 0) |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2270 | { |
| 2271 | /* Set swap type to REVERT to overwrite the primary |
| 2272 | * slot with the image contained in secondary slot |
| 2273 | * and to trigger the explicit setting of the |
| 2274 | * image_ok flag. |
| 2275 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2276 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2277 | } |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 2278 | } |
| 2279 | } |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 2280 | #endif |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 2281 | } |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2282 | } else { |
| 2283 | /* In that case if slots are not compatible. */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2284 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE; |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 2285 | } |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 2286 | } |
| 2287 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2288 | int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2289 | context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2290 | { |
Marti Bolivar | 8489865 | 2017-06-13 17:20:22 -0400 | [diff] [blame] | 2291 | size_t slot; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2292 | struct boot_status bs; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2293 | int rc; |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 2294 | int fa_id; |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 2295 | int image_index; |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 2296 | bool has_upgrade; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2297 | |
| 2298 | /* The array of slot sectors are defined here (as opposed to file scope) so |
| 2299 | * that they don't get allocated for non-boot-loader apps. This is |
| 2300 | * necessary because the gcc option "-fdata-sections" doesn't seem to have |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 2301 | * any effect in older gcc versions (e.g., 4.8.4). |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2302 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2303 | TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS]; |
| 2304 | TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS]; |
| 2305 | TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS]; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2306 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2307 | memset(state, 0, sizeof(struct boot_loader_state)); |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 2308 | has_upgrade = false; |
| 2309 | |
| 2310 | #if (BOOT_IMAGE_NUMBER == 1) |
| 2311 | (void)has_upgrade; |
| 2312 | #endif |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 2313 | |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2314 | /* Iterate over all the images. By the end of the loop the swap type has |
| 2315 | * to be determined for each image and all aborted swaps have to be |
| 2316 | * completed. |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 2317 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2318 | IMAGES_ITER(BOOT_CURR_IMG(state)) { |
Fabio Utzig | db5bd3c | 2017-07-13 22:20:22 -0300 | [diff] [blame] | 2319 | |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2320 | #if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1) |
| 2321 | /* The keys used for encryption may no longer be valid (could belong to |
| 2322 | * another images). Therefore, mark them as invalid to force their reload |
| 2323 | * by boot_enc_load(). |
Fabio Utzig | db5bd3c | 2017-07-13 22:20:22 -0300 | [diff] [blame] | 2324 | */ |
Fabio Utzig | 1e4284b | 2019-08-23 11:55:27 -0300 | [diff] [blame] | 2325 | boot_enc_zeroize(BOOT_CURR_ENC(state)); |
David Brown | 554c52e | 2017-06-30 16:01:07 -0600 | [diff] [blame] | 2326 | #endif |
| 2327 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2328 | image_index = BOOT_CURR_IMG(state); |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 2329 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2330 | BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors = |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 2331 | primary_slot_sectors[image_index]; |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2332 | BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors = |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 2333 | secondary_slot_sectors[image_index]; |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2334 | state->scratch.sectors = scratch_sectors; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2335 | |
| 2336 | /* Open primary and secondary image areas for the duration |
| 2337 | * of this call. |
| 2338 | */ |
| 2339 | for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) { |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 2340 | fa_id = flash_area_id_from_multi_image_slot(image_index, slot); |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2341 | rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot)); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2342 | assert(rc == 0); |
| 2343 | } |
| 2344 | rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2345 | &BOOT_SCRATCH_AREA(state)); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2346 | assert(rc == 0); |
| 2347 | |
| 2348 | /* Determine swap type and complete swap if it has been aborted. */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2349 | boot_prepare_image_for_update(state, &bs); |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 2350 | |
| 2351 | if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) { |
| 2352 | has_upgrade = true; |
| 2353 | } |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2354 | } |
| 2355 | |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 2356 | #if (BOOT_IMAGE_NUMBER > 1) |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 2357 | if (has_upgrade) { |
| 2358 | /* Iterate over all the images and verify whether the image dependencies |
| 2359 | * are all satisfied and update swap type if necessary. |
| 2360 | */ |
| 2361 | rc = boot_verify_dependencies(state); |
| 2362 | if (rc == BOOT_EBADVERSION) { |
| 2363 | /* |
| 2364 | * It was impossible to upgrade because the expected dependency version |
| 2365 | * was not available. Here we already changed the swap_type so that |
| 2366 | * instead of asserting the bootloader, we continue and no upgrade is |
| 2367 | * performed. |
| 2368 | */ |
| 2369 | rc = 0; |
| 2370 | } |
| 2371 | } |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 2372 | #endif |
| 2373 | |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2374 | /* Iterate over all the images. At this point there are no aborted swaps |
| 2375 | * and the swap types are determined for each image. By the end of the loop |
| 2376 | * all required update operations will have been finished. |
| 2377 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2378 | IMAGES_ITER(BOOT_CURR_IMG(state)) { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2379 | |
| 2380 | #if (BOOT_IMAGE_NUMBER > 1) |
| 2381 | #ifdef MCUBOOT_ENC_IMAGES |
| 2382 | /* The keys used for encryption may no longer be valid (could belong to |
| 2383 | * another images). Therefore, mark them as invalid to force their reload |
| 2384 | * by boot_enc_load(). |
| 2385 | */ |
Fabio Utzig | 1e4284b | 2019-08-23 11:55:27 -0300 | [diff] [blame] | 2386 | boot_enc_zeroize(BOOT_CURR_ENC(state)); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2387 | #endif /* MCUBOOT_ENC_IMAGES */ |
| 2388 | |
| 2389 | /* Indicate that swap is not aborted */ |
| 2390 | memset(&bs, 0, sizeof bs); |
| 2391 | bs.idx = BOOT_STATUS_IDX_0; |
| 2392 | bs.state = BOOT_STATUS_STATE_0; |
| 2393 | #endif /* (BOOT_IMAGE_NUMBER > 1) */ |
| 2394 | |
| 2395 | /* Set the previously determined swap type */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2396 | bs.swap_type = BOOT_SWAP_TYPE(state); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2397 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2398 | switch (BOOT_SWAP_TYPE(state)) { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2399 | case BOOT_SWAP_TYPE_NONE: |
| 2400 | break; |
| 2401 | |
| 2402 | case BOOT_SWAP_TYPE_TEST: /* fallthrough */ |
| 2403 | case BOOT_SWAP_TYPE_PERM: /* fallthrough */ |
| 2404 | case BOOT_SWAP_TYPE_REVERT: |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2405 | rc = boot_perform_update(state, &bs); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2406 | assert(rc == 0); |
| 2407 | break; |
| 2408 | |
| 2409 | case BOOT_SWAP_TYPE_FAIL: |
| 2410 | /* The image in secondary slot was invalid and is now erased. Ensure |
| 2411 | * we don't try to boot into it again on the next reboot. Do this by |
| 2412 | * pretending we just reverted back to primary slot. |
| 2413 | */ |
| 2414 | #ifndef MCUBOOT_OVERWRITE_ONLY |
| 2415 | /* image_ok needs to be explicitly set to avoid a new revert. */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2416 | rc = boot_set_image_ok(BOOT_CURR_IMG(state)); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2417 | if (rc != 0) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2418 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2419 | } |
| 2420 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
| 2421 | break; |
| 2422 | |
| 2423 | default: |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2424 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2425 | } |
| 2426 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2427 | if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2428 | BOOT_LOG_ERR("panic!"); |
| 2429 | assert(0); |
| 2430 | |
| 2431 | /* Loop forever... */ |
| 2432 | while (1) {} |
| 2433 | } |
| 2434 | } |
| 2435 | |
| 2436 | /* Iterate over all the images. At this point all required update operations |
| 2437 | * have finished. By the end of the loop each image in the primary slot will |
| 2438 | * have been re-validated. |
| 2439 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2440 | IMAGES_ITER(BOOT_CURR_IMG(state)) { |
| 2441 | if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2442 | /* Attempt to read an image header from each slot. Ensure that image |
| 2443 | * headers in slots are aligned with headers in boot_data. |
| 2444 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2445 | rc = boot_read_image_headers(state, false); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2446 | if (rc != 0) { |
| 2447 | goto out; |
| 2448 | } |
| 2449 | /* Since headers were reloaded, it can be assumed we just performed |
| 2450 | * a swap or overwrite. Now the header info that should be used to |
| 2451 | * provide the data for the bootstrap, which previously was at |
| 2452 | * secondary slot, was updated to primary slot. |
| 2453 | */ |
| 2454 | } |
| 2455 | |
| 2456 | #ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2457 | rc = boot_validate_slot(state, BOOT_PRIMARY_SLOT, NULL); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2458 | if (rc != 0) { |
| 2459 | rc = BOOT_EBADIMAGE; |
| 2460 | goto out; |
| 2461 | } |
| 2462 | #else |
| 2463 | /* Even if we're not re-validating the primary slot, we could be booting |
| 2464 | * onto an empty flash chip. At least do a basic sanity check that |
| 2465 | * the magic number on the image is OK. |
| 2466 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2467 | if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2468 | BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long) |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2469 | &boot_img_hdr(state,BOOT_PRIMARY_SLOT)->ih_magic, |
| 2470 | BOOT_CURR_IMG(state)); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2471 | rc = BOOT_EBADIMAGE; |
| 2472 | goto out; |
| 2473 | } |
| 2474 | #endif |
| 2475 | } |
| 2476 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 2477 | #if (BOOT_IMAGE_NUMBER > 1) |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2478 | /* Always boot from the primary slot of Image 0. */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2479 | BOOT_CURR_IMG(state) = 0; |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 2480 | #endif |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 2481 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2482 | rsp->br_flash_dev_id = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)->fa_device_id; |
| 2483 | rsp->br_image_off = boot_img_slot_off(state, BOOT_PRIMARY_SLOT); |
| 2484 | rsp->br_hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2485 | |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 2486 | out: |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2487 | IMAGES_ITER(BOOT_CURR_IMG(state)) { |
| 2488 | flash_area_close(BOOT_SCRATCH_AREA(state)); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2489 | for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2490 | flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot)); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2491 | } |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 2492 | } |
| 2493 | return rc; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2494 | } |
| 2495 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2496 | /** |
| 2497 | * Prepares the booting process. This function moves images around in flash as |
| 2498 | * appropriate, and tells you what address to boot from. |
| 2499 | * |
| 2500 | * @param rsp On success, indicates how booting should occur. |
| 2501 | * |
| 2502 | * @return 0 on success; nonzero on failure. |
| 2503 | */ |
| 2504 | int |
| 2505 | boot_go(struct boot_rsp *rsp) |
| 2506 | { |
| 2507 | return context_boot_go(&boot_data, rsp); |
| 2508 | } |
| 2509 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2510 | int |
| 2511 | split_go(int loader_slot, int split_slot, void **entry) |
| 2512 | { |
Marti Bolivar | c50926f | 2017-06-14 09:35:40 -0400 | [diff] [blame] | 2513 | boot_sector_t *sectors; |
Christopher Collins | 034a620 | 2017-01-11 12:19:37 -0800 | [diff] [blame] | 2514 | uintptr_t entry_val; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2515 | int loader_flash_id; |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 2516 | int split_flash_id; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2517 | int rc; |
| 2518 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2519 | sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors); |
| 2520 | if (sectors == NULL) { |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 2521 | return SPLIT_GO_ERR; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2522 | } |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 2523 | BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0; |
| 2524 | BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS; |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 2525 | |
| 2526 | loader_flash_id = flash_area_id_from_image_slot(loader_slot); |
| 2527 | rc = flash_area_open(loader_flash_id, |
Alvaro Prieto | 63a2bdb | 2019-07-04 12:18:49 -0700 | [diff] [blame] | 2528 | &BOOT_IMG_AREA(&boot_data, loader_slot)); |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 2529 | assert(rc == 0); |
| 2530 | split_flash_id = flash_area_id_from_image_slot(split_slot); |
| 2531 | rc = flash_area_open(split_flash_id, |
| 2532 | &BOOT_IMG_AREA(&boot_data, split_slot)); |
| 2533 | assert(rc == 0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2534 | |
| 2535 | /* Determine the sector layout of the image slots and scratch area. */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2536 | rc = boot_read_sectors(&boot_data); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2537 | if (rc != 0) { |
| 2538 | rc = SPLIT_GO_ERR; |
| 2539 | goto done; |
| 2540 | } |
| 2541 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 2542 | rc = boot_read_image_headers(&boot_data, true); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2543 | if (rc != 0) { |
| 2544 | goto done; |
| 2545 | } |
| 2546 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2547 | /* Don't check the bootable image flag because we could really call a |
| 2548 | * bootable or non-bootable image. Just validate that the image check |
| 2549 | * passes which is distinct from the normal check. |
| 2550 | */ |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 2551 | rc = split_image_check(boot_img_hdr(&boot_data, split_slot), |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 2552 | BOOT_IMG_AREA(&boot_data, split_slot), |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 2553 | boot_img_hdr(&boot_data, loader_slot), |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 2554 | BOOT_IMG_AREA(&boot_data, loader_slot)); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2555 | if (rc != 0) { |
| 2556 | rc = SPLIT_GO_NON_MATCHING; |
| 2557 | goto done; |
| 2558 | } |
| 2559 | |
Marti Bolivar | ea08887 | 2017-06-12 17:10:49 -0400 | [diff] [blame] | 2560 | entry_val = boot_img_slot_off(&boot_data, split_slot) + |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 2561 | boot_img_hdr(&boot_data, split_slot)->ih_hdr_size; |
Christopher Collins | 034a620 | 2017-01-11 12:19:37 -0800 | [diff] [blame] | 2562 | *entry = (void *) entry_val; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2563 | rc = SPLIT_GO_OK; |
| 2564 | |
| 2565 | done: |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 2566 | flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot)); |
| 2567 | flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot)); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2568 | free(sectors); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 2569 | return rc; |
| 2570 | } |