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