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