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