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