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