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