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