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