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