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