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