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