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