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 |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 23 | * Git SHA of the original version: 3c469bc698a9767859ed73cd0201c44161204d5c |
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; |
David Vincze | bb20798 | 2019-08-21 15:13:04 +0200 | [diff] [blame] | 50 | uint8_t current_image = 0; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 51 | |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 52 | #if !defined(MCUBOOT_NO_SWAP) && !defined(MCUBOOT_RAM_LOADING) |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 53 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 54 | #if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT) && !defined(MCUBOOT_OVERWRITE_ONLY) |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 55 | static int boot_status_fails = 0; |
| 56 | #define BOOT_STATUS_ASSERT(x) \ |
| 57 | do { \ |
| 58 | if (!(x)) { \ |
| 59 | boot_status_fails++; \ |
| 60 | } \ |
| 61 | } while (0) |
| 62 | #else |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 63 | #define BOOT_STATUS_ASSERT(x) ASSERT(x) |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 64 | #endif |
| 65 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 66 | struct boot_status_table { |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 67 | uint8_t bst_magic_primary_slot; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 68 | uint8_t bst_magic_scratch; |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 69 | uint8_t bst_copy_done_primary_slot; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 70 | uint8_t bst_status_source; |
| 71 | }; |
| 72 | |
| 73 | /** |
| 74 | * This set of tables maps swap state contents to boot status location. |
| 75 | * When searching for a match, these tables must be iterated in order. |
| 76 | */ |
| 77 | static const struct boot_status_table boot_status_tables[] = { |
| 78 | { |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 79 | /* | primary slot | scratch | |
| 80 | * ----------+--------------+--------------| |
| 81 | * magic | Good | Any | |
| 82 | * copy-done | Set | N/A | |
| 83 | * ----------+--------------+--------------' |
| 84 | * source: none | |
| 85 | * ----------------------------------------' |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 86 | */ |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 87 | .bst_magic_primary_slot = BOOT_MAGIC_GOOD, |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 88 | .bst_magic_scratch = BOOT_MAGIC_NOTGOOD, |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 89 | .bst_copy_done_primary_slot = BOOT_FLAG_SET, |
| 90 | .bst_status_source = BOOT_STATUS_SOURCE_NONE, |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 91 | }, |
| 92 | |
| 93 | { |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 94 | /* | primary slot | scratch | |
| 95 | * ----------+--------------+--------------| |
| 96 | * magic | Good | Any | |
| 97 | * copy-done | Unset | N/A | |
| 98 | * ----------+--------------+--------------' |
| 99 | * source: primary slot | |
| 100 | * ----------------------------------------' |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 101 | */ |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 102 | .bst_magic_primary_slot = BOOT_MAGIC_GOOD, |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 103 | .bst_magic_scratch = BOOT_MAGIC_NOTGOOD, |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 104 | .bst_copy_done_primary_slot = BOOT_FLAG_UNSET, |
| 105 | .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT, |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 106 | }, |
| 107 | |
| 108 | { |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 109 | /* | primary slot | scratch | |
| 110 | * ----------+--------------+--------------| |
| 111 | * magic | Any | Good | |
| 112 | * copy-done | Any | N/A | |
| 113 | * ----------+--------------+--------------' |
| 114 | * source: scratch | |
| 115 | * ----------------------------------------' |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 116 | */ |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 117 | .bst_magic_primary_slot = BOOT_MAGIC_ANY, |
| 118 | .bst_magic_scratch = BOOT_MAGIC_GOOD, |
| 119 | .bst_copy_done_primary_slot = BOOT_FLAG_ANY, |
| 120 | .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH, |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 121 | }, |
| 122 | |
| 123 | { |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 124 | /* | primary slot | scratch | |
| 125 | * ----------+--------------+--------------| |
| 126 | * magic | Unset | Any | |
| 127 | * copy-done | Unset | N/A | |
| 128 | * ----------+--------------+--------------| |
| 129 | * source: varies | |
| 130 | * ----------------------------------------+--------------------------+ |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 131 | * This represents one of two cases: | |
| 132 | * 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] | 133 | * o Mid-revert; status in the primary slot. | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 134 | * -------------------------------------------------------------------' |
| 135 | */ |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 136 | .bst_magic_primary_slot = BOOT_MAGIC_UNSET, |
| 137 | .bst_magic_scratch = BOOT_MAGIC_ANY, |
| 138 | .bst_copy_done_primary_slot = BOOT_FLAG_UNSET, |
| 139 | .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT, |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 140 | }, |
| 141 | }; |
| 142 | |
| 143 | #define BOOT_STATUS_TABLES_COUNT \ |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 144 | (sizeof(boot_status_tables) / sizeof(boot_status_tables[0])) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 145 | |
| 146 | #define BOOT_LOG_SWAP_STATE(area, state) \ |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 147 | BOOT_LOG_INF("%s: magic=%5s, swap_type=0x%x, copy_done=0x%x, " \ |
| 148 | "image_ok=0x%x", \ |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 149 | (area), \ |
| 150 | ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \ |
| 151 | (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \ |
| 152 | "bad"), \ |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 153 | (state)->swap_type, \ |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 154 | (state)->copy_done, \ |
| 155 | (state)->image_ok) |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 156 | #endif /* !MCUBOOT_NO_SWAP && !MCUBOOT_RAM_LOADING */ |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 157 | |
Tamas Ban | 056ed0b | 2019-09-16 12:48:32 +0100 | [diff] [blame] | 158 | /* |
| 159 | * \brief Verifies the image header: magic value, flags, integer overflow. |
| 160 | * |
| 161 | * \retval 0 |
| 162 | * \retval BOOT_EBADIMAGE |
| 163 | */ |
| 164 | static int |
| 165 | boot_verify_image_header(struct image_header *hdr) |
| 166 | { |
| 167 | uint32_t image_end; |
| 168 | |
| 169 | if (hdr->ih_magic != IMAGE_MAGIC) { |
| 170 | return BOOT_EBADIMAGE; |
| 171 | } |
| 172 | |
| 173 | /* Check input parameters against integer overflow */ |
| 174 | if (boot_add_uint32_overflow_check(hdr->ih_hdr_size, hdr->ih_img_size)) { |
| 175 | return BOOT_EBADIMAGE; |
| 176 | } |
| 177 | |
| 178 | image_end = hdr->ih_hdr_size + hdr->ih_img_size; |
| 179 | if (boot_add_uint32_overflow_check(image_end, hdr->ih_protect_tlv_size)) { |
| 180 | return BOOT_EBADIMAGE; |
| 181 | } |
| 182 | |
| 183 | |
| 184 | #if MCUBOOT_RAM_LOADING |
| 185 | if (!(hdr->ih_flags & IMAGE_F_RAM_LOAD)) { |
| 186 | return BOOT_EBADIMAGE; |
| 187 | } |
| 188 | |
| 189 | /* Check input parameters against integer overflow */ |
| 190 | if (boot_add_uint32_overflow_check(image_end, hdr->ih_load_addr)) { |
| 191 | return BOOT_EBADIMAGE; |
| 192 | } |
| 193 | #endif |
| 194 | |
| 195 | return 0; |
| 196 | } |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 197 | |
| 198 | static int |
| 199 | boot_read_image_header(int slot, struct image_header *out_hdr) |
| 200 | { |
| 201 | const struct flash_area *fap = NULL; |
| 202 | int area_id; |
| 203 | int rc; |
| 204 | |
| 205 | area_id = flash_area_id_from_image_slot(slot); |
| 206 | rc = flash_area_open(area_id, &fap); |
| 207 | if (rc != 0) { |
| 208 | rc = BOOT_EFLASH; |
| 209 | goto done; |
| 210 | } |
| 211 | |
| 212 | rc = flash_area_read(fap, 0, out_hdr, sizeof(*out_hdr)); |
| 213 | if (rc != 0) { |
| 214 | rc = BOOT_EFLASH; |
| 215 | goto done; |
| 216 | } |
| 217 | |
Tamas Ban | 056ed0b | 2019-09-16 12:48:32 +0100 | [diff] [blame] | 218 | rc = boot_verify_image_header(out_hdr); |
| 219 | BOOT_IMG_HDR_IS_VALID(&boot_data, slot) = (rc == 0); |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 220 | |
| 221 | done: |
| 222 | flash_area_close(fap); |
| 223 | return rc; |
| 224 | } |
| 225 | |
| 226 | static int |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 227 | boot_read_image_headers(bool require_all) |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 228 | { |
| 229 | int rc; |
| 230 | int i; |
| 231 | |
| 232 | for (i = 0; i < BOOT_NUM_SLOTS; i++) { |
| 233 | rc = boot_read_image_header(i, boot_img_hdr(&boot_data, i)); |
| 234 | if (rc != 0) { |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 235 | /* If `require_all` is set, fail on any single fail, otherwise |
| 236 | * if at least the first slot's header was read successfully, |
| 237 | * then the boot loader can attempt a boot. |
| 238 | * |
| 239 | * Failure to read any headers is a fatal error. |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 240 | */ |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 241 | if (i > 0 && !require_all) { |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 242 | return 0; |
| 243 | } else { |
| 244 | return rc; |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
Raef Coles | 204c5b4 | 2019-09-05 09:18:47 +0100 | [diff] [blame] | 252 | static uint32_t |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 253 | boot_write_sz(void) |
| 254 | { |
Raef Coles | 204c5b4 | 2019-09-05 09:18:47 +0100 | [diff] [blame] | 255 | uint32_t elem_sz; |
| 256 | uint32_t align; |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 257 | |
| 258 | /* Figure out what size to write update status update as. The size depends |
| 259 | * on what the minimum write size is for scratch area, active image slot. |
| 260 | * We need to use the bigger of those 2 values. |
| 261 | */ |
David Vincze | 7384ee7 | 2019-07-23 17:00:42 +0200 | [diff] [blame] | 262 | elem_sz = flash_area_align(BOOT_IMG_AREA(&boot_data, BOOT_PRIMARY_SLOT)); |
| 263 | align = flash_area_align(BOOT_SCRATCH_AREA(&boot_data)); |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 264 | if (align > elem_sz) { |
| 265 | elem_sz = align; |
| 266 | } |
| 267 | |
| 268 | return elem_sz; |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Determines the sector layout of both image slots and the scratch area. |
| 273 | * This information is necessary for calculating the number of bytes to erase |
| 274 | * and copy during an image swap. The information collected during this |
| 275 | * function is used to populate the boot_data global. |
| 276 | */ |
| 277 | static int |
| 278 | boot_read_sectors(void) |
| 279 | { |
| 280 | int rc; |
| 281 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 282 | rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_PRIMARY); |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 283 | if (rc != 0) { |
| 284 | return BOOT_EFLASH; |
| 285 | } |
| 286 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 287 | rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_SECONDARY); |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 288 | if (rc != 0) { |
| 289 | return BOOT_EFLASH; |
| 290 | } |
| 291 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 292 | rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_SCRATCH); |
| 293 | if (rc != 0) { |
| 294 | return BOOT_EFLASH; |
| 295 | } |
| 296 | |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 297 | BOOT_WRITE_SZ(&boot_data) = boot_write_sz(); |
| 298 | |
| 299 | return 0; |
| 300 | } |
| 301 | |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 302 | /** |
| 303 | * Validate image hash/signature and security counter in a slot. |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 304 | */ |
| 305 | static int |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 306 | boot_image_check(struct image_header *hdr, const struct flash_area *fap, |
| 307 | struct boot_status *bs) |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 308 | { |
| 309 | static uint8_t tmpbuf[BOOT_TMPBUF_SZ]; |
| 310 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 311 | (void)bs; |
| 312 | |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 313 | if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ, |
Tamas Ban | 0e8ab30 | 2019-01-17 11:45:31 +0000 | [diff] [blame] | 314 | NULL, 0, NULL)) { |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 315 | return BOOT_EBADIMAGE; |
| 316 | } |
| 317 | return 0; |
| 318 | } |
| 319 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 320 | /* |
| 321 | * Check that a memory area consists of a given value. |
| 322 | */ |
| 323 | static inline bool |
| 324 | boot_data_is_set_to(uint8_t val, void *data, size_t len) |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 325 | { |
| 326 | uint8_t i; |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 327 | uint8_t *p = (uint8_t *)data; |
| 328 | for (i = 0; i < len; i++) { |
| 329 | if (val != p[i]) { |
| 330 | return false; |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 331 | } |
| 332 | } |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 333 | return true; |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 334 | } |
| 335 | |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 336 | static int |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 337 | boot_check_header_erased(int slot) |
| 338 | { |
| 339 | const struct flash_area *fap; |
| 340 | struct image_header *hdr; |
| 341 | uint8_t erased_val; |
| 342 | int rc; |
| 343 | |
| 344 | rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap); |
| 345 | if (rc != 0) { |
| 346 | return -1; |
| 347 | } |
| 348 | |
| 349 | erased_val = flash_area_erased_val(fap); |
| 350 | flash_area_close(fap); |
| 351 | |
| 352 | hdr = boot_img_hdr(&boot_data, slot); |
| 353 | if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, |
| 354 | sizeof(hdr->ih_magic))) { |
| 355 | return -1; |
| 356 | } |
| 357 | |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | static int |
| 362 | boot_validate_slot(int slot, struct boot_status *bs) |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 363 | { |
| 364 | const struct flash_area *fap; |
| 365 | struct image_header *hdr; |
| 366 | int rc; |
| 367 | |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 368 | rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap); |
| 369 | if (rc != 0) { |
| 370 | return BOOT_EFLASH; |
| 371 | } |
| 372 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 373 | hdr = boot_img_hdr(&boot_data, slot); |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 374 | if ((boot_check_header_erased(slot) == 0) || |
| 375 | (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) { |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 376 | /* No bootable image in slot; continue booting from the primary slot. */ |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 377 | rc = -1; |
| 378 | goto out; |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 379 | } |
| 380 | |
Tamas Ban | 056ed0b | 2019-09-16 12:48:32 +0100 | [diff] [blame] | 381 | if ((!BOOT_IMG_HDR_IS_VALID(&boot_data, slot)) || |
| 382 | (boot_image_check(hdr, fap, bs) != 0)) { |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 383 | if (slot != BOOT_PRIMARY_SLOT) { |
David Vincze | 26e8c8a | 2018-08-28 16:59:41 +0200 | [diff] [blame] | 384 | rc = flash_area_erase(fap, 0, fap->fa_size); |
| 385 | if(rc != 0) { |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 386 | rc = BOOT_EFLASH; |
| 387 | goto out; |
David Vincze | 26e8c8a | 2018-08-28 16:59:41 +0200 | [diff] [blame] | 388 | } |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 389 | /* Image in the secondary slot is invalid. Erase the image and |
| 390 | * continue booting from the primary slot. |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 391 | */ |
| 392 | } |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 393 | BOOT_LOG_ERR("Authentication failed! Image in the %s slot is not valid." |
| 394 | , (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary"); |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 395 | rc = -1; |
| 396 | goto out; |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 397 | } |
| 398 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 399 | /* Image in the secondary slot is valid. */ |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 400 | rc = 0; |
| 401 | |
| 402 | out: |
| 403 | flash_area_close(fap); |
| 404 | return rc; |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 405 | } |
| 406 | |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 407 | /** |
| 408 | * Updates the stored security counter value with the image's security counter |
| 409 | * value which resides in the given slot if it's greater than the stored value. |
| 410 | * |
| 411 | * @param slot Slot number of the image. |
| 412 | * @param hdr Pointer to the image header structure of the image that is |
| 413 | * currently stored in the given slot. |
| 414 | * |
| 415 | * @return 0 on success; nonzero on failure. |
| 416 | */ |
| 417 | static int |
| 418 | boot_update_security_counter(int slot, struct image_header *hdr) |
| 419 | { |
| 420 | const struct flash_area *fap = NULL; |
| 421 | uint32_t img_security_cnt; |
| 422 | int rc; |
| 423 | |
| 424 | rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap); |
| 425 | if (rc != 0) { |
| 426 | rc = BOOT_EFLASH; |
| 427 | goto done; |
| 428 | } |
| 429 | |
| 430 | rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt); |
| 431 | if (rc != 0) { |
| 432 | goto done; |
| 433 | } |
| 434 | |
David Vincze | 0b41ee2 | 2019-06-28 14:28:20 +0200 | [diff] [blame] | 435 | rc = boot_nv_security_counter_update(current_image, img_security_cnt); |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 436 | if (rc != 0) { |
| 437 | goto done; |
| 438 | } |
| 439 | |
| 440 | done: |
| 441 | flash_area_close(fap); |
| 442 | return rc; |
| 443 | } |
| 444 | |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 445 | #if !defined(MCUBOOT_NO_SWAP) && !defined(MCUBOOT_OVERWRITE_ONLY) |
| 446 | /* |
| 447 | * Compute the total size of the given image. Includes the size of |
| 448 | * the TLVs. |
| 449 | */ |
| 450 | static int |
| 451 | boot_read_image_size(int slot, struct image_header *hdr, uint32_t *size) |
| 452 | { |
| 453 | const struct flash_area *fap = NULL; |
| 454 | struct image_tlv_info info; |
| 455 | int area_id; |
| 456 | int rc; |
| 457 | |
| 458 | area_id = flash_area_id_from_image_slot(slot); |
| 459 | rc = flash_area_open(area_id, &fap); |
| 460 | if (rc != 0) { |
| 461 | rc = BOOT_EFLASH; |
| 462 | goto done; |
| 463 | } |
| 464 | |
| 465 | rc = flash_area_read(fap, hdr->ih_hdr_size + hdr->ih_img_size, |
| 466 | &info, sizeof(info)); |
| 467 | if (rc != 0) { |
| 468 | rc = BOOT_EFLASH; |
| 469 | goto done; |
| 470 | } |
| 471 | if (info.it_magic != IMAGE_TLV_INFO_MAGIC) { |
| 472 | rc = BOOT_EBADIMAGE; |
| 473 | goto done; |
| 474 | } |
| 475 | *size = hdr->ih_hdr_size + hdr->ih_img_size + info.it_tlv_tot; |
| 476 | rc = 0; |
| 477 | |
| 478 | done: |
| 479 | flash_area_close(fap); |
| 480 | return rc; |
| 481 | } |
| 482 | #endif /* !MCUBOOT_NO_SWAP && !MCUBOOT_OVERWRITE_ONLY */ |
| 483 | |
| 484 | #if !defined(MCUBOOT_NO_SWAP) && !defined(MCUBOOT_RAM_LOADING) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 485 | /** |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 486 | * 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] | 487 | * status is necessary for completing a swap that was interrupted by a boot |
| 488 | * loader reset. |
| 489 | * |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 490 | * @return A BOOT_STATUS_SOURCE_[...] code indicating where status should |
| 491 | * be read from. |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 492 | */ |
| 493 | static int |
| 494 | boot_status_source(void) |
| 495 | { |
| 496 | const struct boot_status_table *table; |
| 497 | struct boot_swap_state state_scratch; |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 498 | struct boot_swap_state state_primary_slot; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 499 | int rc; |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 500 | size_t i; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 501 | uint8_t source; |
| 502 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 503 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY, |
| 504 | &state_primary_slot); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 505 | assert(rc == 0); |
| 506 | |
| 507 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch); |
| 508 | assert(rc == 0); |
| 509 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 510 | BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 511 | BOOT_LOG_SWAP_STATE("Scratch", &state_scratch); |
| 512 | |
| 513 | for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) { |
| 514 | table = &boot_status_tables[i]; |
| 515 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 516 | if (boot_magic_compatible_check(table->bst_magic_primary_slot, |
| 517 | state_primary_slot.magic) && |
| 518 | boot_magic_compatible_check(table->bst_magic_scratch, |
| 519 | state_scratch.magic) && |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 520 | (table->bst_copy_done_primary_slot == BOOT_FLAG_ANY || |
| 521 | table->bst_copy_done_primary_slot == state_primary_slot.copy_done)) |
| 522 | { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 523 | source = table->bst_status_source; |
David Vincze | 7384ee7 | 2019-07-23 17:00:42 +0200 | [diff] [blame] | 524 | |
| 525 | #if (BOOT_IMAGE_NUMBER > 1) |
| 526 | /* In case of multi-image boot it can happen that if boot status |
| 527 | * info is found on scratch area then it does not belong to the |
| 528 | * currently examined image. |
| 529 | */ |
| 530 | if (source == BOOT_STATUS_SOURCE_SCRATCH && |
| 531 | state_scratch.image_num != current_image) { |
| 532 | source = BOOT_STATUS_SOURCE_NONE; |
| 533 | } |
| 534 | #endif |
| 535 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 536 | BOOT_LOG_INF("Boot source: %s", |
| 537 | source == BOOT_STATUS_SOURCE_NONE ? "none" : |
| 538 | source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" : |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 539 | source == BOOT_STATUS_SOURCE_PRIMARY_SLOT ? |
| 540 | "primary slot" : "BUG; can't happen"); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 541 | return source; |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | BOOT_LOG_INF("Boot source: none"); |
| 546 | return BOOT_STATUS_SOURCE_NONE; |
| 547 | } |
| 548 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 549 | /* |
| 550 | * Slots are compatible when all sectors that store upto to size of the image |
| 551 | * round up to sector size, in both slot's are able to fit in the scratch |
| 552 | * area, and have sizes that are a multiple of each other (powers of two |
| 553 | * presumably!). |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 554 | */ |
| 555 | static int |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 556 | boot_slots_compatible(void) |
| 557 | { |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 558 | size_t num_sectors_primary; |
| 559 | size_t num_sectors_secondary; |
| 560 | size_t sz0, sz1; |
| 561 | size_t primary_slot_sz, secondary_slot_sz; |
| 562 | size_t scratch_sz; |
| 563 | size_t i, j; |
| 564 | int8_t smaller; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 565 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 566 | num_sectors_primary = |
| 567 | boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT); |
| 568 | num_sectors_secondary = |
| 569 | boot_img_num_sectors(&boot_data, BOOT_SECONDARY_SLOT); |
| 570 | if ((num_sectors_primary > BOOT_MAX_IMG_SECTORS) || |
| 571 | (num_sectors_secondary > BOOT_MAX_IMG_SECTORS)) { |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 572 | BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed"); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 573 | return 0; |
| 574 | } |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 575 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 576 | scratch_sz = boot_scratch_area_size(&boot_data); |
| 577 | |
| 578 | /* |
| 579 | * The following loop scans all sectors in a linear fashion, assuring that |
| 580 | * for each possible sector in each slot, it is able to fit in the other |
| 581 | * slot's sector or sectors. Slot's should be compatible as long as any |
| 582 | * number of a slot's sectors are able to fit into another, which only |
| 583 | * excludes cases where sector sizes are not a multiple of each other. |
| 584 | */ |
| 585 | i = sz0 = primary_slot_sz = 0; |
| 586 | j = sz1 = secondary_slot_sz = 0; |
| 587 | smaller = 0; |
| 588 | while (i < num_sectors_primary || j < num_sectors_secondary) { |
| 589 | if (sz0 == sz1) { |
| 590 | sz0 += boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i); |
| 591 | sz1 += boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT, j); |
| 592 | i++; |
| 593 | j++; |
| 594 | } else if (sz0 < sz1) { |
| 595 | sz0 += boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i); |
| 596 | /* Guarantee that multiple sectors of the secondary slot |
| 597 | * fit into the primary slot. |
| 598 | */ |
| 599 | if (smaller == 2) { |
| 600 | BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible" |
| 601 | " sectors"); |
| 602 | return 0; |
| 603 | } |
| 604 | smaller = 1; |
| 605 | i++; |
| 606 | } else { |
| 607 | sz1 += boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT, j); |
| 608 | /* Guarantee that multiple sectors of the primary slot |
| 609 | * fit into the secondary slot. |
| 610 | */ |
| 611 | if (smaller == 1) { |
| 612 | BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible" |
| 613 | " sectors"); |
| 614 | return 0; |
| 615 | } |
| 616 | smaller = 2; |
| 617 | j++; |
| 618 | } |
| 619 | if (sz0 == sz1) { |
| 620 | primary_slot_sz += sz0; |
| 621 | secondary_slot_sz += sz1; |
| 622 | /* Scratch has to fit each swap operation to the size of the larger |
| 623 | * sector among the primary slot and the secondary slot. |
| 624 | */ |
| 625 | if (sz0 > scratch_sz || sz1 > scratch_sz) { |
| 626 | BOOT_LOG_WRN("Cannot upgrade: not all sectors fit inside" |
| 627 | " scratch"); |
| 628 | return 0; |
| 629 | } |
| 630 | smaller = sz0 = sz1 = 0; |
| 631 | } |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 632 | } |
| 633 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 634 | if ((i != num_sectors_primary) || |
| 635 | (j != num_sectors_secondary) || |
| 636 | (primary_slot_sz != secondary_slot_sz)) { |
| 637 | BOOT_LOG_WRN("Cannot upgrade: slots are not compatible"); |
| 638 | return 0; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | return 1; |
| 642 | } |
| 643 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 644 | static uint32_t |
| 645 | boot_status_internal_off(int idx, int state, int elem_sz) |
| 646 | { |
| 647 | int idx_sz; |
| 648 | |
| 649 | idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT; |
| 650 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 651 | return (idx - BOOT_STATUS_IDX_0) * idx_sz + |
| 652 | (state - BOOT_STATUS_STATE_0) * elem_sz; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | /** |
| 656 | * Reads the status of a partially-completed swap, if any. This is necessary |
| 657 | * to recover in case the boot lodaer was reset in the middle of a swap |
| 658 | * operation. |
| 659 | */ |
| 660 | static int |
| 661 | boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs) |
| 662 | { |
| 663 | uint32_t off; |
| 664 | uint8_t status; |
| 665 | int max_entries; |
| 666 | int found; |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 667 | int found_idx; |
| 668 | int invalid; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 669 | int rc; |
| 670 | int i; |
| 671 | |
| 672 | off = boot_status_off(fap); |
| 673 | max_entries = boot_status_entries(fap); |
| 674 | |
| 675 | found = 0; |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 676 | found_idx = 0; |
| 677 | invalid = 0; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 678 | for (i = 0; i < max_entries; i++) { |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 679 | rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(&boot_data), |
| 680 | &status, 1); |
| 681 | if (rc < 0) { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 682 | return BOOT_EFLASH; |
| 683 | } |
| 684 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 685 | if (rc == 1) { |
| 686 | if (found && !found_idx) { |
| 687 | found_idx = i; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 688 | } |
| 689 | } else if (!found) { |
| 690 | found = 1; |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 691 | } else if (found_idx) { |
| 692 | invalid = 1; |
| 693 | break; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 697 | if (invalid) { |
| 698 | /* This means there was an error writing status on the last |
| 699 | * swap. Tell user and move on to validation! |
| 700 | */ |
| 701 | BOOT_LOG_ERR("Detected inconsistent status!"); |
| 702 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 703 | #if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT) |
| 704 | /* With validation of the primary slot disabled, there is no way |
| 705 | * to be sure the swapped primary slot is OK, so abort! |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 706 | */ |
| 707 | assert(0); |
| 708 | #endif |
| 709 | } |
| 710 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 711 | if (found) { |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 712 | if (!found_idx) { |
| 713 | found_idx = i; |
| 714 | } |
| 715 | found_idx--; |
| 716 | bs->idx = (found_idx / BOOT_STATUS_STATE_COUNT) + 1; |
| 717 | bs->state = (found_idx % BOOT_STATUS_STATE_COUNT) + 1; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | /** |
| 724 | * Reads the boot status from the flash. The boot status contains |
| 725 | * the current state of an interrupted image copy operation. If the boot |
| 726 | * status is not present, or it indicates that previous copy finished, |
| 727 | * there is no operation in progress. |
| 728 | */ |
| 729 | static int |
| 730 | boot_read_status(struct boot_status *bs) |
| 731 | { |
| 732 | const struct flash_area *fap; |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 733 | uint32_t off; |
David Vincze | 91b71ef | 2019-06-24 13:06:47 +0200 | [diff] [blame] | 734 | uint8_t swap_info; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 735 | int status_loc; |
| 736 | int area_id; |
| 737 | int rc; |
| 738 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 739 | memset(bs, 0, sizeof *bs); |
| 740 | bs->idx = BOOT_STATUS_IDX_0; |
| 741 | bs->state = BOOT_STATUS_STATE_0; |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 742 | bs->swap_type = BOOT_SWAP_TYPE_NONE; |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 743 | |
| 744 | #ifdef MCUBOOT_OVERWRITE_ONLY |
| 745 | /* Overwrite-only doesn't make use of the swap status area. */ |
| 746 | return 0; |
| 747 | #endif |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 748 | |
| 749 | status_loc = boot_status_source(); |
| 750 | switch (status_loc) { |
| 751 | case BOOT_STATUS_SOURCE_NONE: |
| 752 | return 0; |
| 753 | |
| 754 | case BOOT_STATUS_SOURCE_SCRATCH: |
| 755 | area_id = FLASH_AREA_IMAGE_SCRATCH; |
| 756 | break; |
| 757 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 758 | case BOOT_STATUS_SOURCE_PRIMARY_SLOT: |
| 759 | area_id = FLASH_AREA_IMAGE_PRIMARY; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 760 | break; |
| 761 | |
| 762 | default: |
| 763 | assert(0); |
| 764 | return BOOT_EBADARGS; |
| 765 | } |
| 766 | |
| 767 | rc = flash_area_open(area_id, &fap); |
| 768 | if (rc != 0) { |
| 769 | return BOOT_EFLASH; |
| 770 | } |
| 771 | |
| 772 | rc = boot_read_status_bytes(fap, bs); |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 773 | if (rc == 0) { |
David Vincze | 91b71ef | 2019-06-24 13:06:47 +0200 | [diff] [blame] | 774 | off = boot_swap_info_off(fap); |
| 775 | rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info); |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 776 | if (rc == 1) { |
David Vincze | 91b71ef | 2019-06-24 13:06:47 +0200 | [diff] [blame] | 777 | BOOT_SET_SWAP_INFO(swap_info, 0, BOOT_SWAP_TYPE_NONE); |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 778 | rc = 0; |
| 779 | } |
David Vincze | 91b71ef | 2019-06-24 13:06:47 +0200 | [diff] [blame] | 780 | |
| 781 | /* Extract the swap type info */ |
| 782 | bs->swap_type = BOOT_GET_SWAP_TYPE(swap_info); |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 783 | } |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 784 | |
| 785 | flash_area_close(fap); |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 786 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 787 | return rc; |
| 788 | } |
| 789 | |
| 790 | /** |
| 791 | * Writes the supplied boot status to the flash file system. The boot status |
| 792 | * contains the current state of an in-progress image copy operation. |
| 793 | * |
| 794 | * @param bs The boot status to write. |
| 795 | * |
| 796 | * @return 0 on success; nonzero on failure. |
| 797 | */ |
| 798 | int |
| 799 | boot_write_status(struct boot_status *bs) |
| 800 | { |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 801 | const struct flash_area *fap = NULL; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 802 | uint32_t off; |
| 803 | int area_id; |
| 804 | int rc; |
| 805 | uint8_t buf[BOOT_MAX_ALIGN]; |
Raef Coles | 204c5b4 | 2019-09-05 09:18:47 +0100 | [diff] [blame] | 806 | uint32_t align; |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 807 | uint8_t erased_val; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 808 | |
| 809 | /* 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] | 810 | * the trailer. Since in the last step the primary slot is erased, the |
| 811 | * first two status writes go to the scratch which will be copied to |
| 812 | * the primary slot! |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 813 | */ |
| 814 | |
| 815 | if (bs->use_scratch) { |
| 816 | /* Write to scratch. */ |
| 817 | area_id = FLASH_AREA_IMAGE_SCRATCH; |
| 818 | } else { |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 819 | /* Write to the primary slot. */ |
| 820 | area_id = FLASH_AREA_IMAGE_PRIMARY; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | rc = flash_area_open(area_id, &fap); |
| 824 | if (rc != 0) { |
| 825 | rc = BOOT_EFLASH; |
| 826 | goto done; |
| 827 | } |
| 828 | |
| 829 | off = boot_status_off(fap) + |
| 830 | boot_status_internal_off(bs->idx, bs->state, |
| 831 | BOOT_WRITE_SZ(&boot_data)); |
| 832 | |
Tamas Ban | c382885 | 2018-02-01 12:24:16 +0000 | [diff] [blame] | 833 | align = flash_area_align(fap); |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 834 | erased_val = flash_area_erased_val(fap); |
| 835 | memset(buf, erased_val, BOOT_MAX_ALIGN); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 836 | buf[0] = bs->state; |
| 837 | |
| 838 | rc = flash_area_write(fap, off, buf, align); |
| 839 | if (rc != 0) { |
| 840 | rc = BOOT_EFLASH; |
| 841 | goto done; |
| 842 | } |
| 843 | |
| 844 | rc = 0; |
| 845 | |
| 846 | done: |
| 847 | flash_area_close(fap); |
| 848 | return rc; |
| 849 | } |
| 850 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 851 | /** |
| 852 | * Determines which swap operation to perform, if any. If it is determined |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 853 | * that a swap operation is required, the image in the secondary slot is checked |
| 854 | * for validity. If the image in the secondary slot is invalid, it is erased, |
| 855 | * and a swap type of "none" is indicated. |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 856 | * |
| 857 | * @return The type of swap to perform (BOOT_SWAP_TYPE...) |
| 858 | */ |
| 859 | static int |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 860 | boot_validated_swap_type(struct boot_status *bs) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 861 | { |
| 862 | int swap_type; |
| 863 | |
| 864 | swap_type = boot_swap_type(); |
| 865 | switch (swap_type) { |
| 866 | case BOOT_SWAP_TYPE_TEST: |
| 867 | case BOOT_SWAP_TYPE_PERM: |
| 868 | case BOOT_SWAP_TYPE_REVERT: |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 869 | /* Boot loader wants to switch to the secondary slot. |
| 870 | * Ensure image is valid. |
| 871 | */ |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 872 | if (boot_validate_slot(BOOT_SECONDARY_SLOT, bs) != 0) { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 873 | swap_type = BOOT_SWAP_TYPE_FAIL; |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | return swap_type; |
| 878 | } |
| 879 | |
| 880 | /** |
| 881 | * Calculates the number of sectors the scratch area can contain. A "last" |
| 882 | * source sector is specified because images are copied backwards in flash |
| 883 | * (final index to index number 0). |
| 884 | * |
| 885 | * @param last_sector_idx The index of the last source sector |
| 886 | * (inclusive). |
| 887 | * @param out_first_sector_idx The index of the first source sector |
| 888 | * (inclusive) gets written here. |
| 889 | * |
| 890 | * @return The number of bytes comprised by the |
| 891 | * [first-sector, last-sector] range. |
| 892 | */ |
| 893 | #ifndef MCUBOOT_OVERWRITE_ONLY |
| 894 | static uint32_t |
| 895 | boot_copy_sz(int last_sector_idx, int *out_first_sector_idx) |
| 896 | { |
| 897 | size_t scratch_sz; |
| 898 | uint32_t new_sz; |
| 899 | uint32_t sz; |
| 900 | int i; |
| 901 | |
| 902 | sz = 0; |
| 903 | |
| 904 | scratch_sz = boot_scratch_area_size(&boot_data); |
| 905 | for (i = last_sector_idx; i >= 0; i--) { |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 906 | new_sz = sz + boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i); |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 907 | /* |
| 908 | * The secondary slot is not being checked here, because |
| 909 | * `boot_slots_compatible` already provides assurance that the copy size |
| 910 | * will be compatible with the primary slot and scratch. |
| 911 | */ |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 912 | if (new_sz > scratch_sz) { |
| 913 | break; |
| 914 | } |
| 915 | sz = new_sz; |
| 916 | } |
| 917 | |
| 918 | /* i currently refers to a sector that doesn't fit or it is -1 because all |
| 919 | * sectors have been processed. In both cases, exclude sector i. |
| 920 | */ |
| 921 | *out_first_sector_idx = i + 1; |
| 922 | return sz; |
| 923 | } |
| 924 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
| 925 | |
| 926 | /** |
David Vincze | f7641fa | 2018-09-04 18:29:46 +0200 | [diff] [blame] | 927 | * Erases a region of flash. |
| 928 | * |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 929 | * @param flash_area The flash_area containing the region to erase. |
David Vincze | f7641fa | 2018-09-04 18:29:46 +0200 | [diff] [blame] | 930 | * @param off The offset within the flash area to start the |
| 931 | * erase. |
| 932 | * @param sz The number of bytes to erase. |
| 933 | * |
| 934 | * @return 0 on success; nonzero on failure. |
| 935 | */ |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 936 | static inline int |
| 937 | boot_erase_sector(const struct flash_area *fap, uint32_t off, uint32_t sz) |
David Vincze | f7641fa | 2018-09-04 18:29:46 +0200 | [diff] [blame] | 938 | { |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 939 | return flash_area_erase(fap, off, sz); |
David Vincze | f7641fa | 2018-09-04 18:29:46 +0200 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | /** |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 943 | * Copies the contents of one flash region to another. You must erase the |
| 944 | * destination region prior to calling this function. |
| 945 | * |
| 946 | * @param flash_area_id_src The ID of the source flash area. |
| 947 | * @param flash_area_id_dst The ID of the destination flash area. |
| 948 | * @param off_src The offset within the source flash area to |
| 949 | * copy from. |
| 950 | * @param off_dst The offset within the destination flash area to |
| 951 | * copy to. |
| 952 | * @param sz The number of bytes to copy. |
| 953 | * |
| 954 | * @return 0 on success; nonzero on failure. |
| 955 | */ |
| 956 | static int |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 957 | boot_copy_sector(const struct flash_area *fap_src, |
| 958 | const struct flash_area *fap_dst, |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 959 | uint32_t off_src, uint32_t off_dst, uint32_t sz) |
| 960 | { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 961 | uint32_t bytes_copied; |
| 962 | int chunk_sz; |
| 963 | int rc; |
| 964 | |
| 965 | static uint8_t buf[1024]; |
| 966 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 967 | bytes_copied = 0; |
| 968 | while (bytes_copied < sz) { |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 969 | if (sz - bytes_copied > sizeof(buf)) { |
| 970 | chunk_sz = sizeof(buf); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 971 | } else { |
| 972 | chunk_sz = sz - bytes_copied; |
| 973 | } |
| 974 | |
| 975 | rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz); |
| 976 | if (rc != 0) { |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 977 | return BOOT_EFLASH; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz); |
| 981 | if (rc != 0) { |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 982 | return BOOT_EFLASH; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | bytes_copied += chunk_sz; |
| 986 | } |
| 987 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 988 | return 0; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | #ifndef MCUBOOT_OVERWRITE_ONLY |
| 992 | static inline int |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 993 | boot_status_init(const struct flash_area *fap, const struct boot_status *bs) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 994 | { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 995 | struct boot_swap_state swap_state; |
| 996 | int rc; |
| 997 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 998 | BOOT_LOG_DBG("initializing status; fa_id=%d", fap->fa_id); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 999 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1000 | 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] | 1001 | assert(rc == 0); |
| 1002 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1003 | if (bs->swap_type != BOOT_SWAP_TYPE_NONE) { |
David Vincze | 91b71ef | 2019-06-24 13:06:47 +0200 | [diff] [blame] | 1004 | rc = boot_write_swap_info(fap, |
| 1005 | bs->swap_type, |
David Vincze | 7384ee7 | 2019-07-23 17:00:42 +0200 | [diff] [blame] | 1006 | current_image); |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1007 | assert(rc == 0); |
| 1008 | } |
| 1009 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1010 | if (swap_state.image_ok == BOOT_FLAG_SET) { |
| 1011 | rc = boot_write_image_ok(fap); |
| 1012 | assert(rc == 0); |
| 1013 | } |
| 1014 | |
| 1015 | rc = boot_write_swap_size(fap, bs->swap_size); |
| 1016 | assert(rc == 0); |
| 1017 | |
| 1018 | rc = boot_write_magic(fap); |
| 1019 | assert(rc == 0); |
| 1020 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1021 | return 0; |
| 1022 | } |
David Vincze | f7641fa | 2018-09-04 18:29:46 +0200 | [diff] [blame] | 1023 | |
| 1024 | static int |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1025 | boot_erase_trailer_sectors(const struct flash_area *fap) |
David Vincze | f7641fa | 2018-09-04 18:29:46 +0200 | [diff] [blame] | 1026 | { |
| 1027 | uint8_t slot; |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1028 | uint32_t sector; |
| 1029 | uint32_t trailer_sz; |
| 1030 | uint32_t total_sz; |
| 1031 | uint32_t off; |
| 1032 | uint32_t sz; |
David Vincze | bb20798 | 2019-08-21 15:13:04 +0200 | [diff] [blame] | 1033 | int fa_id_primary; |
| 1034 | int fa_id_secondary; |
David Vincze | f7641fa | 2018-09-04 18:29:46 +0200 | [diff] [blame] | 1035 | int rc; |
| 1036 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1037 | BOOT_LOG_DBG("erasing trailer; fa_id=%d", fap->fa_id); |
| 1038 | |
David Vincze | bb20798 | 2019-08-21 15:13:04 +0200 | [diff] [blame] | 1039 | fa_id_primary = flash_area_id_from_image_slot(BOOT_PRIMARY_SLOT); |
| 1040 | fa_id_secondary = flash_area_id_from_image_slot(BOOT_SECONDARY_SLOT); |
| 1041 | |
| 1042 | if (fap->fa_id == fa_id_primary) { |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1043 | slot = BOOT_PRIMARY_SLOT; |
David Vincze | bb20798 | 2019-08-21 15:13:04 +0200 | [diff] [blame] | 1044 | } else if (fap->fa_id == fa_id_secondary) { |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1045 | slot = BOOT_SECONDARY_SLOT; |
David Vincze | bb20798 | 2019-08-21 15:13:04 +0200 | [diff] [blame] | 1046 | } else { |
David Vincze | f7641fa | 2018-09-04 18:29:46 +0200 | [diff] [blame] | 1047 | return BOOT_EFLASH; |
| 1048 | } |
| 1049 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1050 | /* delete starting from last sector and moving to beginning */ |
| 1051 | sector = boot_img_num_sectors(&boot_data, slot) - 1; |
| 1052 | trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(&boot_data)); |
| 1053 | total_sz = 0; |
| 1054 | do { |
| 1055 | sz = boot_img_sector_size(&boot_data, slot, sector); |
| 1056 | off = boot_img_sector_off(&boot_data, slot, sector); |
| 1057 | rc = boot_erase_sector(fap, off, sz); |
| 1058 | assert(rc == 0); |
| 1059 | |
| 1060 | sector--; |
| 1061 | total_sz += sz; |
| 1062 | } while (total_sz < trailer_sz); |
David Vincze | f7641fa | 2018-09-04 18:29:46 +0200 | [diff] [blame] | 1063 | |
| 1064 | return rc; |
| 1065 | } |
| 1066 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1067 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1068 | /** |
| 1069 | * Swaps the contents of two flash regions within the two image slots. |
| 1070 | * |
| 1071 | * @param idx The index of the first sector in the range of |
| 1072 | * sectors being swapped. |
| 1073 | * @param sz The number of bytes to swap. |
| 1074 | * @param bs The current boot status. This struct gets |
| 1075 | * updated according to the outcome. |
| 1076 | * |
| 1077 | * @return 0 on success; nonzero on failure. |
| 1078 | */ |
| 1079 | #ifndef MCUBOOT_OVERWRITE_ONLY |
| 1080 | static void |
| 1081 | boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs) |
| 1082 | { |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1083 | const struct flash_area *fap_primary_slot; |
| 1084 | const struct flash_area *fap_secondary_slot; |
| 1085 | const struct flash_area *fap_scratch; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1086 | uint32_t copy_sz; |
| 1087 | uint32_t trailer_sz; |
| 1088 | uint32_t img_off; |
| 1089 | uint32_t scratch_trailer_off; |
| 1090 | struct boot_swap_state swap_state; |
| 1091 | size_t last_sector; |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1092 | bool erase_scratch; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1093 | int rc; |
| 1094 | |
| 1095 | /* Calculate offset from start of image area. */ |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1096 | img_off = boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT, idx); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1097 | |
| 1098 | copy_sz = sz; |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1099 | trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(&boot_data)); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1100 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1101 | /* sz in this function is always sized on a multiple of the sector size. |
| 1102 | * The check against the start offset of the last sector |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1103 | * is to determine if we're swapping the last sector. The last sector |
| 1104 | * needs special handling because it's where the trailer lives. If we're |
| 1105 | * copying it, we need to use scratch to write the trailer temporarily. |
| 1106 | * |
| 1107 | * NOTE: `use_scratch` is a temporary flag (never written to flash) which |
| 1108 | * controls if special handling is needed (swapping last sector). |
| 1109 | */ |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1110 | last_sector = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT) - 1; |
David Vincze | 7384ee7 | 2019-07-23 17:00:42 +0200 | [diff] [blame] | 1111 | if ((img_off + sz) > |
| 1112 | boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT, last_sector)) { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1113 | copy_sz -= trailer_sz; |
| 1114 | } |
| 1115 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1116 | bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1117 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1118 | rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap_primary_slot); |
| 1119 | assert (rc == 0); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1120 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1121 | rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap_secondary_slot); |
| 1122 | assert (rc == 0); |
| 1123 | |
| 1124 | rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch); |
| 1125 | assert (rc == 0); |
| 1126 | |
| 1127 | if (bs->state == BOOT_STATUS_STATE_0) { |
| 1128 | BOOT_LOG_DBG("erasing scratch area"); |
| 1129 | rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1130 | assert(rc == 0); |
| 1131 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1132 | if (bs->idx == BOOT_STATUS_IDX_0) { |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1133 | /* Write a trailer to the scratch area, even if we don't need the |
| 1134 | * scratch area for status. We need a temporary place to store the |
| 1135 | * `swap-type` while we erase the primary trailer. |
| 1136 | */ |
| 1137 | rc = boot_status_init(fap_scratch, bs); |
| 1138 | assert(rc == 0); |
| 1139 | |
| 1140 | if (!bs->use_scratch) { |
| 1141 | /* Prepare the primary status area... here it is known that the |
| 1142 | * last sector is not being used by the image data so it's safe |
| 1143 | * to erase. |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1144 | */ |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1145 | rc = boot_erase_trailer_sectors(fap_primary_slot); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1146 | assert(rc == 0); |
| 1147 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1148 | rc = boot_status_init(fap_primary_slot, bs); |
| 1149 | assert(rc == 0); |
| 1150 | |
| 1151 | /* Erase the temporary trailer from the scratch area. */ |
| 1152 | rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size); |
| 1153 | assert(rc == 0); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1154 | } |
| 1155 | } |
| 1156 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1157 | rc = boot_copy_sector(fap_secondary_slot, fap_scratch, |
| 1158 | img_off, 0, copy_sz); |
| 1159 | assert(rc == 0); |
| 1160 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1161 | bs->state = BOOT_STATUS_STATE_1; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1162 | rc = boot_write_status(bs); |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1163 | BOOT_STATUS_ASSERT(rc == 0); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1166 | if (bs->state == BOOT_STATUS_STATE_1) { |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1167 | rc = boot_erase_sector(fap_secondary_slot, img_off, sz); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1168 | assert(rc == 0); |
| 1169 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1170 | rc = boot_copy_sector(fap_primary_slot, fap_secondary_slot, |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1171 | img_off, img_off, copy_sz); |
| 1172 | assert(rc == 0); |
| 1173 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1174 | if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1175 | /* If not all sectors of the slot are being swapped, |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1176 | * guarantee here that only the primary slot will have the state. |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1177 | */ |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1178 | rc = boot_erase_trailer_sectors(fap_secondary_slot); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1179 | assert(rc == 0); |
| 1180 | } |
| 1181 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1182 | bs->state = BOOT_STATUS_STATE_2; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1183 | rc = boot_write_status(bs); |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1184 | BOOT_STATUS_ASSERT(rc == 0); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1187 | if (bs->state == BOOT_STATUS_STATE_2) { |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1188 | rc = boot_erase_sector(fap_primary_slot, img_off, sz); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1189 | assert(rc == 0); |
| 1190 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1191 | /* NOTE: If this is the final sector, we exclude the image trailer from |
| 1192 | * this copy (copy_sz was truncated earlier). |
| 1193 | */ |
| 1194 | rc = boot_copy_sector(fap_scratch, fap_primary_slot, |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1195 | 0, img_off, copy_sz); |
| 1196 | assert(rc == 0); |
| 1197 | |
| 1198 | if (bs->use_scratch) { |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1199 | scratch_trailer_off = boot_status_off(fap_scratch); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1200 | |
| 1201 | /* copy current status that is being maintained in scratch */ |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1202 | rc = boot_copy_sector(fap_scratch, fap_primary_slot, |
| 1203 | scratch_trailer_off, img_off + copy_sz, |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1204 | BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data)); |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1205 | BOOT_STATUS_ASSERT(rc == 0); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1206 | |
| 1207 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, |
| 1208 | &swap_state); |
| 1209 | assert(rc == 0); |
| 1210 | |
| 1211 | if (swap_state.image_ok == BOOT_FLAG_SET) { |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1212 | rc = boot_write_image_ok(fap_primary_slot); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1213 | assert(rc == 0); |
| 1214 | } |
| 1215 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1216 | if (swap_state.swap_type != BOOT_SWAP_TYPE_NONE) { |
David Vincze | 91b71ef | 2019-06-24 13:06:47 +0200 | [diff] [blame] | 1217 | rc = boot_write_swap_info(fap_primary_slot, |
| 1218 | swap_state.swap_type, |
David Vincze | 7384ee7 | 2019-07-23 17:00:42 +0200 | [diff] [blame] | 1219 | current_image); |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1220 | assert(rc == 0); |
| 1221 | } |
| 1222 | |
| 1223 | rc = boot_write_swap_size(fap_primary_slot, bs->swap_size); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1224 | assert(rc == 0); |
| 1225 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1226 | rc = boot_write_magic(fap_primary_slot); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1227 | assert(rc == 0); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1230 | /* If we wrote a trailer to the scratch area, erase it after we persist |
| 1231 | * a trailer to the primary slot. We do this to prevent mcuboot from |
| 1232 | * reading a stale status from the scratch area in case of immediate |
| 1233 | * reset. |
| 1234 | */ |
| 1235 | erase_scratch = bs->use_scratch; |
| 1236 | bs->use_scratch = 0; |
| 1237 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1238 | bs->idx++; |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1239 | bs->state = BOOT_STATUS_STATE_0; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1240 | rc = boot_write_status(bs); |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1241 | BOOT_STATUS_ASSERT(rc == 0); |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1242 | |
| 1243 | if (erase_scratch) { |
| 1244 | rc = boot_erase_sector(fap_scratch, 0, sz); |
| 1245 | assert(rc == 0); |
| 1246 | } |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1247 | } |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1248 | |
| 1249 | flash_area_close(fap_primary_slot); |
| 1250 | flash_area_close(fap_secondary_slot); |
| 1251 | flash_area_close(fap_scratch); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1252 | } |
| 1253 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
| 1254 | |
| 1255 | /** |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1256 | * Overwrite primary slot with the image contained in the secondary slot. |
| 1257 | * If a prior copy operation was interrupted by a system reset, this function |
| 1258 | * redos the copy. |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1259 | * |
| 1260 | * @param bs The current boot status. This function reads |
| 1261 | * this struct to determine if it is resuming |
| 1262 | * an interrupted swap operation. This |
| 1263 | * function writes the updated status to this |
| 1264 | * function on return. |
| 1265 | * |
| 1266 | * @return 0 on success; nonzero on failure. |
| 1267 | */ |
| 1268 | #ifdef MCUBOOT_OVERWRITE_ONLY |
| 1269 | static int |
| 1270 | boot_copy_image(struct boot_status *bs) |
| 1271 | { |
| 1272 | size_t sect_count; |
| 1273 | size_t sect; |
| 1274 | int rc; |
| 1275 | size_t size = 0; |
| 1276 | size_t this_size; |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1277 | size_t last_sector; |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1278 | const struct flash_area *fap_primary_slot; |
| 1279 | const struct flash_area *fap_secondary_slot; |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1280 | |
| 1281 | (void)bs; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1282 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1283 | BOOT_LOG_INF("Image upgrade secondary slot -> primary slot"); |
| 1284 | BOOT_LOG_INF("Erasing the primary slot"); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1285 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1286 | rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap_primary_slot); |
| 1287 | assert (rc == 0); |
| 1288 | |
| 1289 | rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap_secondary_slot); |
| 1290 | assert (rc == 0); |
| 1291 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1292 | sect_count = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1293 | for (sect = 0; sect < sect_count; sect++) { |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1294 | this_size = boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, sect); |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1295 | rc = boot_erase_sector(fap_primary_slot, size, this_size); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1296 | assert(rc == 0); |
| 1297 | |
| 1298 | size += this_size; |
| 1299 | } |
| 1300 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1301 | BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes", |
| 1302 | size); |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1303 | rc = boot_copy_sector(fap_secondary_slot, fap_primary_slot, 0, 0, size); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1304 | |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 1305 | /* Update the stored security counter with the new image's security counter |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1306 | * value. Both slots hold the new image at this point, but the secondary |
| 1307 | * slot's image header must be passed because the read image headers in the |
| 1308 | * boot_data structure have not been updated yet. |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 1309 | */ |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1310 | rc = boot_update_security_counter(BOOT_PRIMARY_SLOT, |
| 1311 | boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT)); |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 1312 | if (rc != 0) { |
| 1313 | BOOT_LOG_ERR("Security counter update failed after image upgrade."); |
| 1314 | return rc; |
| 1315 | } |
| 1316 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1317 | /* |
| 1318 | * Erases header and trailer. The trailer is erased because when a new |
| 1319 | * image is written without a trailer as is the case when using newt, the |
| 1320 | * trailer that was left might trigger a new upgrade. |
| 1321 | */ |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1322 | BOOT_LOG_DBG("erasing secondary header"); |
| 1323 | rc = boot_erase_sector(fap_secondary_slot, |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1324 | boot_img_sector_off(&boot_data, |
| 1325 | BOOT_SECONDARY_SLOT, 0), |
| 1326 | boot_img_sector_size(&boot_data, |
| 1327 | BOOT_SECONDARY_SLOT, 0)); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1328 | assert(rc == 0); |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1329 | last_sector = boot_img_num_sectors(&boot_data, BOOT_SECONDARY_SLOT) - 1; |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1330 | BOOT_LOG_DBG("erasing secondary trailer"); |
| 1331 | rc = boot_erase_sector(fap_secondary_slot, |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1332 | boot_img_sector_off(&boot_data, BOOT_SECONDARY_SLOT, |
| 1333 | last_sector), |
| 1334 | boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT, |
| 1335 | last_sector)); |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1336 | assert(rc == 0); |
| 1337 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1338 | flash_area_close(fap_primary_slot); |
| 1339 | flash_area_close(fap_secondary_slot); |
| 1340 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1341 | /* TODO: Perhaps verify the primary slot's signature again? */ |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1342 | |
| 1343 | return 0; |
| 1344 | } |
| 1345 | #else |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1346 | /** |
| 1347 | * Swaps the two images in flash. If a prior copy operation was interrupted |
| 1348 | * by a system reset, this function completes that operation. |
| 1349 | * |
| 1350 | * @param bs The current boot status. This function reads |
| 1351 | * this struct to determine if it is resuming |
| 1352 | * an interrupted swap operation. This |
| 1353 | * function writes the updated status to this |
| 1354 | * function on return. |
| 1355 | * |
| 1356 | * @return 0 on success; nonzero on failure. |
| 1357 | */ |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1358 | static int |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1359 | boot_swap_image(struct boot_status *bs) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1360 | { |
| 1361 | uint32_t sz; |
| 1362 | int first_sector_idx; |
| 1363 | int last_sector_idx; |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1364 | int last_idx_secondary_slot; |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1365 | uint32_t swap_idx; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1366 | struct image_header *hdr; |
| 1367 | uint32_t size; |
| 1368 | uint32_t copy_size; |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1369 | uint32_t primary_slot_size; |
| 1370 | uint32_t secondary_slot_size; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1371 | int rc; |
| 1372 | |
| 1373 | /* FIXME: just do this if asked by user? */ |
| 1374 | |
| 1375 | size = copy_size = 0; |
| 1376 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1377 | 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] | 1378 | /* |
| 1379 | * No swap ever happened, so need to find the largest image which |
| 1380 | * will be used to determine the amount of sectors to swap. |
| 1381 | */ |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1382 | hdr = boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT); |
Tamas Ban | 056ed0b | 2019-09-16 12:48:32 +0100 | [diff] [blame] | 1383 | rc = boot_read_image_size(BOOT_PRIMARY_SLOT, hdr, ©_size); |
| 1384 | assert(rc == 0); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1385 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1386 | hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT); |
Tamas Ban | 056ed0b | 2019-09-16 12:48:32 +0100 | [diff] [blame] | 1387 | rc = boot_read_image_size(BOOT_SECONDARY_SLOT, hdr, &size); |
| 1388 | assert(rc == 0); |
| 1389 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1390 | |
| 1391 | if (size > copy_size) { |
| 1392 | copy_size = size; |
| 1393 | } |
| 1394 | |
| 1395 | bs->swap_size = copy_size; |
| 1396 | } else { |
| 1397 | /* |
| 1398 | * If a swap was under way, the swap_size should already be present |
| 1399 | * in the trailer... |
| 1400 | */ |
| 1401 | rc = boot_read_swap_size(&bs->swap_size); |
| 1402 | assert(rc == 0); |
| 1403 | |
| 1404 | copy_size = bs->swap_size; |
| 1405 | } |
| 1406 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1407 | primary_slot_size = 0; |
| 1408 | secondary_slot_size = 0; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1409 | last_sector_idx = 0; |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1410 | last_idx_secondary_slot = 0; |
| 1411 | |
| 1412 | /* |
| 1413 | * Knowing the size of the largest image between both slots, here we |
| 1414 | * find what is the last sector in the primary slot that needs swapping. |
| 1415 | * Since we already know that both slots are compatible, the secondary |
| 1416 | * slot's last sector is not really required after this check is finished. |
| 1417 | */ |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1418 | while (1) { |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1419 | if ((primary_slot_size < copy_size) || |
| 1420 | (primary_slot_size < secondary_slot_size)) { |
| 1421 | primary_slot_size += boot_img_sector_size(&boot_data, |
| 1422 | BOOT_PRIMARY_SLOT, |
| 1423 | last_sector_idx); |
| 1424 | } |
| 1425 | if ((secondary_slot_size < copy_size) || |
| 1426 | (secondary_slot_size < primary_slot_size)) { |
| 1427 | secondary_slot_size += boot_img_sector_size(&boot_data, |
| 1428 | BOOT_SECONDARY_SLOT, |
| 1429 | last_idx_secondary_slot); |
| 1430 | } |
| 1431 | if (primary_slot_size >= copy_size && |
| 1432 | secondary_slot_size >= copy_size && |
| 1433 | primary_slot_size == secondary_slot_size) { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1434 | break; |
| 1435 | } |
| 1436 | last_sector_idx++; |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1437 | last_idx_secondary_slot++; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | swap_idx = 0; |
| 1441 | while (last_sector_idx >= 0) { |
| 1442 | sz = boot_copy_sz(last_sector_idx, &first_sector_idx); |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1443 | if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1444 | boot_swap_sectors(first_sector_idx, sz, bs); |
| 1445 | } |
| 1446 | |
| 1447 | last_sector_idx = first_sector_idx - 1; |
| 1448 | swap_idx++; |
| 1449 | } |
| 1450 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1451 | #ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1452 | if (boot_status_fails > 0) { |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1453 | BOOT_LOG_WRN("%d status write fails performing the swap", |
| 1454 | boot_status_fails); |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1455 | } |
| 1456 | #endif |
| 1457 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1458 | return 0; |
| 1459 | } |
| 1460 | #endif |
| 1461 | |
| 1462 | /** |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1463 | * Marks the image in the primary slot as fully copied. |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1464 | */ |
| 1465 | #ifndef MCUBOOT_OVERWRITE_ONLY |
| 1466 | static int |
| 1467 | boot_set_copy_done(void) |
| 1468 | { |
| 1469 | const struct flash_area *fap; |
| 1470 | int rc; |
| 1471 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1472 | rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1473 | if (rc != 0) { |
| 1474 | return BOOT_EFLASH; |
| 1475 | } |
| 1476 | |
| 1477 | rc = boot_write_copy_done(fap); |
| 1478 | flash_area_close(fap); |
| 1479 | return rc; |
| 1480 | } |
| 1481 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
| 1482 | |
| 1483 | /** |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1484 | * Marks a reverted image in the primary slot as confirmed. This is necessary to |
| 1485 | * ensure the status bytes from the image revert operation don't get processed |
| 1486 | * on a subsequent boot. |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1487 | * |
| 1488 | * 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] | 1489 | * image installed on the primary slot and the new image to be upgrade to has a |
| 1490 | * bad sig, image_ok would be overwritten. |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1491 | */ |
| 1492 | #ifndef MCUBOOT_OVERWRITE_ONLY |
| 1493 | static int |
| 1494 | boot_set_image_ok(void) |
| 1495 | { |
| 1496 | const struct flash_area *fap; |
| 1497 | struct boot_swap_state state; |
| 1498 | int rc; |
| 1499 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1500 | rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1501 | if (rc != 0) { |
| 1502 | return BOOT_EFLASH; |
| 1503 | } |
| 1504 | |
| 1505 | rc = boot_read_swap_state(fap, &state); |
| 1506 | if (rc != 0) { |
| 1507 | rc = BOOT_EFLASH; |
| 1508 | goto out; |
| 1509 | } |
| 1510 | |
| 1511 | if (state.image_ok == BOOT_FLAG_UNSET) { |
| 1512 | rc = boot_write_image_ok(fap); |
| 1513 | } |
| 1514 | |
| 1515 | out: |
| 1516 | flash_area_close(fap); |
| 1517 | return rc; |
| 1518 | } |
| 1519 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
| 1520 | |
David Vincze | bb6e3b6 | 2019-07-31 14:24:05 +0200 | [diff] [blame] | 1521 | #if (BOOT_IMAGE_NUMBER > 1) |
| 1522 | /** |
| 1523 | * Check the image dependency whether it is satisfied and modify |
| 1524 | * the swap type if necessary. |
| 1525 | * |
| 1526 | * @param dep Image dependency which has to be verified. |
| 1527 | * |
| 1528 | * @return 0 on success; nonzero on failure. |
| 1529 | */ |
| 1530 | static int |
| 1531 | boot_verify_single_dependency(struct image_dependency *dep) |
| 1532 | { |
| 1533 | struct image_version *dep_version; |
| 1534 | size_t dep_slot; |
| 1535 | int rc; |
| 1536 | |
| 1537 | /* Determine the source of the image which is the subject of |
| 1538 | * the dependency and get it's version. */ |
| 1539 | dep_slot = (boot_data.swap_type[dep->image_id] != BOOT_SWAP_TYPE_NONE) ? |
| 1540 | BOOT_SECONDARY_SLOT : BOOT_PRIMARY_SLOT; |
| 1541 | dep_version = &boot_data.imgs[dep->image_id][dep_slot].hdr.ih_ver; |
| 1542 | |
| 1543 | rc = boot_is_version_sufficient(&dep->image_min_version, dep_version); |
| 1544 | if (rc != 0) { |
| 1545 | /* Dependency not satisfied. |
| 1546 | * Modify the swap type to decrease the version number of the image |
| 1547 | * (which will be located in the primary slot after the boot process), |
| 1548 | * consequently the number of unsatisfied dependencies will be |
| 1549 | * decreased or remain the same. |
| 1550 | */ |
| 1551 | switch (BOOT_SWAP_TYPE(&boot_data)) { |
| 1552 | case BOOT_SWAP_TYPE_TEST: |
| 1553 | case BOOT_SWAP_TYPE_PERM: |
| 1554 | BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE; |
| 1555 | break; |
| 1556 | case BOOT_SWAP_TYPE_NONE: |
| 1557 | BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_REVERT; |
| 1558 | break; |
| 1559 | default: |
| 1560 | break; |
| 1561 | } |
| 1562 | } |
| 1563 | |
| 1564 | return rc; |
| 1565 | } |
| 1566 | |
| 1567 | /** |
| 1568 | * Read all dependency TLVs of an image from the flash and verify |
| 1569 | * one after another to see if they are all satisfied. |
| 1570 | * |
| 1571 | * @param slot Image slot number. |
| 1572 | * |
| 1573 | * @return 0 on success; nonzero on failure. |
| 1574 | */ |
| 1575 | static int |
| 1576 | boot_verify_all_dependency(uint32_t slot) |
| 1577 | { |
| 1578 | const struct flash_area *fap; |
| 1579 | struct image_header *hdr; |
| 1580 | struct image_tlv_info info; |
| 1581 | struct image_tlv tlv; |
| 1582 | struct image_dependency dep; |
| 1583 | uint32_t off; |
| 1584 | uint32_t end; |
| 1585 | bool dep_tlvs_found = false; |
| 1586 | int rc; |
| 1587 | |
| 1588 | rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap); |
| 1589 | if (rc != 0) { |
| 1590 | rc = BOOT_EFLASH; |
| 1591 | goto done; |
| 1592 | } |
| 1593 | |
| 1594 | hdr = boot_img_hdr(&boot_data, slot); |
| 1595 | /* The TLVs come after the image. */ |
| 1596 | off = hdr->ih_hdr_size + hdr->ih_img_size; |
| 1597 | |
| 1598 | /* The TLV area always starts with an image_tlv_info structure. */ |
| 1599 | rc = flash_area_read(fap, off, &info, sizeof(info)); |
| 1600 | if (rc != 0) { |
| 1601 | rc = BOOT_EFLASH; |
| 1602 | goto done; |
| 1603 | } |
| 1604 | |
| 1605 | if (info.it_magic != IMAGE_TLV_INFO_MAGIC) { |
| 1606 | rc = BOOT_EBADIMAGE; |
| 1607 | goto done; |
| 1608 | } |
Tamas Ban | 056ed0b | 2019-09-16 12:48:32 +0100 | [diff] [blame] | 1609 | if (boot_add_uint32_overflow_check(off, (info.it_tlv_tot + sizeof(info)))) { |
| 1610 | return -1; |
| 1611 | } |
David Vincze | bb6e3b6 | 2019-07-31 14:24:05 +0200 | [diff] [blame] | 1612 | end = off + info.it_tlv_tot; |
| 1613 | off += sizeof(info); |
| 1614 | |
| 1615 | /* Traverse through all of the TLVs to find the dependency TLVs. */ |
Tamas Ban | 056ed0b | 2019-09-16 12:48:32 +0100 | [diff] [blame] | 1616 | while(off < end) { |
David Vincze | bb6e3b6 | 2019-07-31 14:24:05 +0200 | [diff] [blame] | 1617 | rc = flash_area_read(fap, off, &tlv, sizeof(tlv)); |
| 1618 | if (rc != 0) { |
| 1619 | rc = BOOT_EFLASH; |
| 1620 | goto done; |
| 1621 | } |
| 1622 | |
| 1623 | if (tlv.it_type == IMAGE_TLV_DEPENDENCY) { |
| 1624 | if (!dep_tlvs_found) { |
| 1625 | dep_tlvs_found = true; |
| 1626 | } |
| 1627 | |
| 1628 | if (tlv.it_len != sizeof(dep)) { |
| 1629 | rc = BOOT_EBADIMAGE; |
| 1630 | goto done; |
| 1631 | } |
| 1632 | |
| 1633 | rc = flash_area_read(fap, off + sizeof(tlv), &dep, tlv.it_len); |
| 1634 | if (rc != 0) { |
| 1635 | rc = BOOT_EFLASH; |
| 1636 | goto done; |
| 1637 | } |
| 1638 | |
| 1639 | /* Verify dependency and modify the swap type if not satisfied. */ |
| 1640 | rc = boot_verify_single_dependency(&dep); |
| 1641 | if (rc != 0) { |
| 1642 | /* Dependency not satisfied. */ |
| 1643 | goto done; |
| 1644 | } |
| 1645 | |
| 1646 | /* Dependency satisfied, no action needed. |
| 1647 | * Continue with the next TLV entry. |
| 1648 | */ |
| 1649 | } else if (dep_tlvs_found) { |
| 1650 | /* The dependency TLVs are contiguous in the TLV area. If a |
| 1651 | * dependency had already been found and the last read TLV |
| 1652 | * has a different type then there are no more dependency TLVs. |
| 1653 | * The search can be finished. |
| 1654 | */ |
| 1655 | break; |
| 1656 | } |
Tamas Ban | 056ed0b | 2019-09-16 12:48:32 +0100 | [diff] [blame] | 1657 | /* Avoid integer overflow. */ |
| 1658 | if (boot_add_uint32_overflow_check(off, (sizeof(tlv) + tlv.it_len))) { |
| 1659 | /* Potential overflow. */ |
| 1660 | return BOOT_EBADIMAGE; |
| 1661 | } else { |
| 1662 | off += sizeof(tlv) + tlv.it_len; |
| 1663 | } |
David Vincze | bb6e3b6 | 2019-07-31 14:24:05 +0200 | [diff] [blame] | 1664 | } |
| 1665 | |
| 1666 | done: |
| 1667 | flash_area_close(fap); |
| 1668 | return rc; |
| 1669 | } |
| 1670 | |
| 1671 | /** |
| 1672 | * Verify whether the image dependencies in the TLV area are |
| 1673 | * all satisfied and modify the swap type if necessary. |
| 1674 | * |
| 1675 | * @return 0 if all dependencies are satisfied, |
| 1676 | * nonzero otherwise. |
| 1677 | */ |
| 1678 | static int |
| 1679 | boot_verify_single_image_dependency(void) |
| 1680 | { |
| 1681 | size_t slot; |
| 1682 | |
| 1683 | /* Determine the source of the dependency TLVs. Those dependencies have to |
| 1684 | * be checked which belong to the image that will be located in the primary |
| 1685 | * slot after the firmware update process. |
| 1686 | */ |
| 1687 | if (BOOT_SWAP_TYPE(&boot_data) != BOOT_SWAP_TYPE_NONE && |
| 1688 | BOOT_SWAP_TYPE(&boot_data) != BOOT_SWAP_TYPE_FAIL) { |
| 1689 | slot = BOOT_SECONDARY_SLOT; |
| 1690 | } else { |
| 1691 | slot = BOOT_PRIMARY_SLOT; |
| 1692 | } |
| 1693 | |
| 1694 | return boot_verify_all_dependency(slot); |
| 1695 | } |
| 1696 | |
| 1697 | /** |
| 1698 | * Iterate over all the images and verify whether the image dependencies in the |
| 1699 | * TLV area are all satisfied and update the related swap type if necessary. |
| 1700 | */ |
| 1701 | static void |
| 1702 | boot_verify_all_image_dependency(void) |
| 1703 | { |
| 1704 | current_image = 0; |
| 1705 | int rc; |
| 1706 | |
| 1707 | while (current_image < BOOT_IMAGE_NUMBER) { |
| 1708 | rc = boot_verify_single_image_dependency(); |
| 1709 | if (rc == 0) { |
| 1710 | /* All dependencies've been satisfied, continue with next image. */ |
| 1711 | current_image++; |
| 1712 | } else if (rc == BOOT_EBADVERSION) { |
| 1713 | /* Dependency check needs to be restarted. */ |
| 1714 | current_image = 0; |
| 1715 | } else { |
| 1716 | /* Other error happened, images are inconsistent */ |
| 1717 | return; |
| 1718 | } |
| 1719 | } |
| 1720 | } |
| 1721 | #endif /* (BOOT_IMAGE_NUMBER > 1) */ |
| 1722 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1723 | /** |
David Vincze | 7384ee7 | 2019-07-23 17:00:42 +0200 | [diff] [blame] | 1724 | * Performs a clean (not aborted) image update. |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1725 | * |
David Vincze | 7384ee7 | 2019-07-23 17:00:42 +0200 | [diff] [blame] | 1726 | * @param bs The current boot status. |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1727 | * |
| 1728 | * @return 0 on success; nonzero on failure. |
| 1729 | */ |
| 1730 | static int |
David Vincze | 7384ee7 | 2019-07-23 17:00:42 +0200 | [diff] [blame] | 1731 | boot_perform_update(struct boot_status *bs) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1732 | { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1733 | int rc; |
| 1734 | |
David Vincze | 7384ee7 | 2019-07-23 17:00:42 +0200 | [diff] [blame] | 1735 | /* At this point there are no aborted swaps. */ |
| 1736 | #if defined(MCUBOOT_OVERWRITE_ONLY) |
| 1737 | rc = boot_copy_image(bs); |
| 1738 | #else |
| 1739 | rc = boot_swap_image(bs); |
| 1740 | #endif |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1741 | assert(rc == 0); |
David Vincze | 7384ee7 | 2019-07-23 17:00:42 +0200 | [diff] [blame] | 1742 | |
| 1743 | #ifndef MCUBOOT_OVERWRITE_ONLY |
| 1744 | /* The following state needs image_ok be explicitly set after the |
| 1745 | * swap was finished to avoid a new revert. |
| 1746 | */ |
| 1747 | if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_REVERT || |
| 1748 | BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_PERM) { |
| 1749 | rc = boot_set_image_ok(); |
| 1750 | if (rc != 0) { |
| 1751 | BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC; |
| 1752 | } |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
David Vincze | 7384ee7 | 2019-07-23 17:00:42 +0200 | [diff] [blame] | 1755 | if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_PERM) { |
| 1756 | /* Update the stored security counter with the new image's security |
| 1757 | * counter value. The primary slot holds the new image at this |
| 1758 | * point, but the secondary slot's image header must be passed |
| 1759 | * because the read image headers in the boot_data structure have |
| 1760 | * not been updated yet. |
| 1761 | * |
| 1762 | * In case of a permanent image swap mcuboot will never attempt to |
| 1763 | * revert the images on the next reboot. Therefore, the security |
| 1764 | * counter must be increased right after the image upgrade. |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1765 | */ |
David Vincze | 7384ee7 | 2019-07-23 17:00:42 +0200 | [diff] [blame] | 1766 | rc = boot_update_security_counter(BOOT_PRIMARY_SLOT, |
| 1767 | boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT)); |
| 1768 | if (rc != 0) { |
| 1769 | BOOT_LOG_ERR("Security counter update failed after " |
| 1770 | "image upgrade."); |
| 1771 | BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1772 | } |
| 1773 | } |
| 1774 | |
David Vincze | 7384ee7 | 2019-07-23 17:00:42 +0200 | [diff] [blame] | 1775 | if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_TEST || |
| 1776 | BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_PERM || |
| 1777 | BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_REVERT) { |
| 1778 | rc = boot_set_copy_done(); |
| 1779 | if (rc != 0) { |
| 1780 | BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC; |
| 1781 | } |
| 1782 | } |
| 1783 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
| 1784 | |
| 1785 | return rc; |
| 1786 | } |
| 1787 | |
| 1788 | /** |
| 1789 | * Completes a previously aborted image swap. |
| 1790 | * |
| 1791 | * @param bs The current boot status. |
| 1792 | * |
| 1793 | * @return 0 on success; nonzero on failure. |
| 1794 | */ |
| 1795 | #if !defined(MCUBOOT_OVERWRITE_ONLY) |
| 1796 | static int |
| 1797 | boot_complete_partial_swap(struct boot_status *bs) |
| 1798 | { |
| 1799 | int rc; |
| 1800 | |
| 1801 | /* Determine the type of swap operation being resumed from the |
| 1802 | * `swap-type` trailer field. |
| 1803 | */ |
| 1804 | rc = boot_swap_image(bs); |
| 1805 | assert(rc == 0); |
| 1806 | |
| 1807 | BOOT_SWAP_TYPE(&boot_data) = bs->swap_type; |
| 1808 | |
| 1809 | /* The following states need image_ok be explicitly set after the |
| 1810 | * swap was finished to avoid a new revert. |
| 1811 | */ |
| 1812 | if (bs->swap_type == BOOT_SWAP_TYPE_REVERT || |
| 1813 | bs->swap_type == BOOT_SWAP_TYPE_PERM) { |
| 1814 | rc = boot_set_image_ok(); |
| 1815 | if (rc != 0) { |
| 1816 | BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC; |
| 1817 | } |
| 1818 | } |
| 1819 | |
| 1820 | if (bs->swap_type == BOOT_SWAP_TYPE_TEST || |
| 1821 | bs->swap_type == BOOT_SWAP_TYPE_PERM || |
| 1822 | bs->swap_type == BOOT_SWAP_TYPE_REVERT) { |
| 1823 | rc = boot_set_copy_done(); |
| 1824 | if (rc != 0) { |
| 1825 | BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC; |
| 1826 | } |
| 1827 | } |
| 1828 | |
| 1829 | if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_PANIC) { |
| 1830 | BOOT_LOG_ERR("panic!"); |
| 1831 | assert(0); |
| 1832 | |
| 1833 | /* Loop forever... */ |
| 1834 | while (1) {} |
| 1835 | } |
| 1836 | |
| 1837 | return rc; |
| 1838 | } |
| 1839 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
| 1840 | |
| 1841 | #if (BOOT_IMAGE_NUMBER > 1) |
| 1842 | /** |
| 1843 | * Review the validity of previously determined swap types of other images. |
| 1844 | * |
| 1845 | * @param aborted_swap The current image upgrade is a |
| 1846 | * partial/aborted swap. |
| 1847 | */ |
| 1848 | static void |
| 1849 | boot_review_image_swap_types(bool aborted_swap) |
| 1850 | { |
| 1851 | /* In that case if we rebooted in the middle of an image upgrade process, we |
| 1852 | * must review the validity of swap types, that were previously determined |
| 1853 | * for other images. The image_ok flag had not been set before the reboot |
| 1854 | * for any of the updated images (only the copy_done flag) and thus falsely |
| 1855 | * the REVERT swap type has been determined for the previous images that had |
| 1856 | * been updated before the reboot. |
| 1857 | * |
| 1858 | * There are two separate scenarios that we have to deal with: |
| 1859 | * |
| 1860 | * 1. The reboot has happened during swapping an image: |
| 1861 | * The current image upgrade has been determined as a |
| 1862 | * partial/aborted swap. |
| 1863 | * 2. The reboot has happened between two separate image upgrades: |
| 1864 | * In this scenario we must check the swap type of the current image. |
| 1865 | * In those cases if it is NONE or REVERT we cannot certainly determine |
| 1866 | * the fact of a reboot. In a consistent state images must move in the |
| 1867 | * same direction or stay in place, e.g. in practice REVERT and TEST |
| 1868 | * swap types cannot be present at the same time. If the swap type of |
| 1869 | * the current image is either TEST, PERM or FAIL we must review the |
| 1870 | * already determined swap types of other images and set each false |
| 1871 | * REVERT swap types to NONE (these images had been successfully |
| 1872 | * updated before the system rebooted between two separate image |
| 1873 | * upgrades). |
| 1874 | */ |
| 1875 | |
| 1876 | if (current_image == 0) { |
| 1877 | /* Nothing to do */ |
| 1878 | return; |
| 1879 | } |
| 1880 | |
| 1881 | if (!aborted_swap) { |
| 1882 | if ((BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_NONE) || |
| 1883 | (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_REVERT)) { |
| 1884 | /* Nothing to do */ |
| 1885 | return; |
| 1886 | } |
| 1887 | } |
| 1888 | |
| 1889 | for (uint8_t i = 0; i < current_image; i++) { |
| 1890 | if (boot_data.swap_type[i] == BOOT_SWAP_TYPE_REVERT) { |
| 1891 | boot_data.swap_type[i] = BOOT_SWAP_TYPE_NONE; |
| 1892 | } |
| 1893 | } |
| 1894 | } |
| 1895 | #endif |
| 1896 | |
| 1897 | /** |
| 1898 | * Prepare image to be updated if required. |
| 1899 | * |
| 1900 | * Prepare image to be updated if required with completing an image swap |
| 1901 | * operation if one was aborted and/or determining the type of the |
| 1902 | * swap operation. In case of any error set the swap type to NONE. |
| 1903 | * |
| 1904 | * @param bs Pointer where the read and possibly updated |
| 1905 | * boot status can be written to. |
| 1906 | */ |
| 1907 | static void |
| 1908 | boot_prepare_image_for_update(struct boot_status *bs) |
| 1909 | { |
| 1910 | int rc; |
| 1911 | |
| 1912 | /* Determine the sector layout of the image slots and scratch area. */ |
| 1913 | rc = boot_read_sectors(); |
| 1914 | if (rc != 0) { |
| 1915 | BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d" |
| 1916 | " - too small?", BOOT_MAX_IMG_SECTORS); |
| 1917 | /* Unable to determine sector layout, continue with next image |
| 1918 | * if there is one. |
| 1919 | */ |
| 1920 | BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE; |
| 1921 | return; |
| 1922 | } |
| 1923 | |
| 1924 | /* Attempt to read an image header from each slot. */ |
| 1925 | rc = boot_read_image_headers(false); |
| 1926 | if (rc != 0) { |
| 1927 | /* Continue with next image if there is one. */ |
| 1928 | BOOT_LOG_WRN("Failed reading image headers; Image=%u", current_image); |
| 1929 | BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE; |
| 1930 | return; |
| 1931 | } |
| 1932 | |
| 1933 | /* If the current image's slots aren't compatible, no swap is possible. |
| 1934 | * Just boot into primary slot. |
| 1935 | */ |
| 1936 | if (boot_slots_compatible()) { |
| 1937 | |
| 1938 | rc = boot_read_status(bs); |
| 1939 | if (rc != 0) { |
| 1940 | BOOT_LOG_WRN("Failed reading boot status; Image=%u", |
| 1941 | current_image); |
| 1942 | /* Continue with next image if there is one. */ |
| 1943 | BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE; |
| 1944 | return; |
| 1945 | } |
| 1946 | |
| 1947 | /* Determine if we rebooted in the middle of an image swap |
| 1948 | * operation. If a partial swap was detected, complete it. |
| 1949 | */ |
| 1950 | if (bs->idx != BOOT_STATUS_IDX_0 || bs->state != BOOT_STATUS_STATE_0) { |
| 1951 | |
| 1952 | #if (BOOT_IMAGE_NUMBER > 1) |
| 1953 | boot_review_image_swap_types(true); |
| 1954 | #endif |
| 1955 | |
| 1956 | #ifdef MCUBOOT_OVERWRITE_ONLY |
| 1957 | /* Should never arrive here, overwrite-only mode has |
| 1958 | * no swap state. |
| 1959 | */ |
| 1960 | assert(0); |
| 1961 | #else |
| 1962 | /* Determine the type of swap operation being resumed from the |
| 1963 | * `swap-type` trailer field. |
| 1964 | */ |
| 1965 | rc = boot_complete_partial_swap(bs); |
| 1966 | assert(rc == 0); |
| 1967 | #endif |
| 1968 | /* Attempt to read an image header from each slot. Ensure that |
| 1969 | * image headers in slots are aligned with headers in boot_data. |
| 1970 | */ |
| 1971 | rc = boot_read_image_headers(false); |
| 1972 | assert(rc == 0); |
| 1973 | |
| 1974 | /* Swap has finished set to NONE */ |
| 1975 | BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE; |
| 1976 | } else { |
| 1977 | /* There was no partial swap, determine swap type. */ |
| 1978 | if (bs->swap_type == BOOT_SWAP_TYPE_NONE) { |
| 1979 | BOOT_SWAP_TYPE(&boot_data) = boot_validated_swap_type(bs); |
| 1980 | } else if (boot_validate_slot(BOOT_SECONDARY_SLOT, bs) != 0) { |
| 1981 | BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_FAIL; |
| 1982 | } else { |
| 1983 | BOOT_SWAP_TYPE(&boot_data) = bs->swap_type; |
| 1984 | } |
| 1985 | |
| 1986 | #if (BOOT_IMAGE_NUMBER > 1) |
| 1987 | boot_review_image_swap_types(false); |
| 1988 | #endif |
| 1989 | } |
| 1990 | } else { |
| 1991 | /* In that case if slots are not compatible. */ |
| 1992 | BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_NONE; |
| 1993 | } |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1994 | } |
| 1995 | |
| 1996 | /** |
| 1997 | * Prepares the booting process. This function moves images around in flash as |
| 1998 | * appropriate, and tells you what address to boot from. |
| 1999 | * |
| 2000 | * @param rsp On success, indicates how booting should occur. |
| 2001 | * |
| 2002 | * @return 0 on success; nonzero on failure. |
| 2003 | */ |
| 2004 | int |
| 2005 | boot_go(struct boot_rsp *rsp) |
| 2006 | { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 2007 | size_t slot; |
David Vincze | 7384ee7 | 2019-07-23 17:00:42 +0200 | [diff] [blame] | 2008 | struct boot_status bs; |
Tamas Ban | 4e5ed8d | 2019-09-17 09:31:11 +0100 | [diff] [blame] | 2009 | int rc = 0; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 2010 | int fa_id; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 2011 | |
| 2012 | /* The array of slot sectors are defined here (as opposed to file scope) so |
| 2013 | * that they don't get allocated for non-boot-loader apps. This is |
| 2014 | * necessary because the gcc option "-fdata-sections" doesn't seem to have |
| 2015 | * any effect in older gcc versions (e.g., 4.8.4). |
| 2016 | */ |
David Vincze | 7384ee7 | 2019-07-23 17:00:42 +0200 | [diff] [blame] | 2017 | static boot_sector_t |
| 2018 | primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS]; |
| 2019 | static boot_sector_t |
| 2020 | secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS]; |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 2021 | static boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS]; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 2022 | |
David Vincze | 7384ee7 | 2019-07-23 17:00:42 +0200 | [diff] [blame] | 2023 | /* Iterate over all the images. By the end of the loop the swap type has |
| 2024 | * to be determined for each image and all aborted swaps have to be |
| 2025 | * completed. |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 2026 | */ |
David Vincze | 7384ee7 | 2019-07-23 17:00:42 +0200 | [diff] [blame] | 2027 | for (current_image = 0; current_image < BOOT_IMAGE_NUMBER; ++current_image) |
| 2028 | { |
| 2029 | BOOT_IMG(&boot_data, BOOT_PRIMARY_SLOT).sectors = |
| 2030 | primary_slot_sectors[current_image]; |
| 2031 | BOOT_IMG(&boot_data, BOOT_SECONDARY_SLOT).sectors = |
| 2032 | secondary_slot_sectors[current_image]; |
| 2033 | boot_data.scratch.sectors = scratch_sectors; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 2034 | |
David Vincze | 7384ee7 | 2019-07-23 17:00:42 +0200 | [diff] [blame] | 2035 | /* Open primary and secondary image areas for the duration |
| 2036 | * of this call. |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 2037 | */ |
David Vincze | 7384ee7 | 2019-07-23 17:00:42 +0200 | [diff] [blame] | 2038 | for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) { |
| 2039 | fa_id = flash_area_id_from_image_slot(slot); |
| 2040 | rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot)); |
| 2041 | assert(rc == 0); |
| 2042 | } |
| 2043 | rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, |
| 2044 | &BOOT_SCRATCH_AREA(&boot_data)); |
| 2045 | assert(rc == 0); |
| 2046 | |
| 2047 | /* Determine swap type and complete swap if it has been aborted. */ |
| 2048 | boot_prepare_image_for_update(&bs); |
| 2049 | } |
| 2050 | |
David Vincze | bb6e3b6 | 2019-07-31 14:24:05 +0200 | [diff] [blame] | 2051 | #if (BOOT_IMAGE_NUMBER > 1) |
| 2052 | /* Iterate over all the images and verify whether the image dependencies |
| 2053 | * are all satisfied and update swap type if necessary. |
| 2054 | */ |
| 2055 | boot_verify_all_image_dependency(); |
| 2056 | #endif |
| 2057 | |
David Vincze | 7384ee7 | 2019-07-23 17:00:42 +0200 | [diff] [blame] | 2058 | /* Iterate over all the images. At this point there are no aborted swaps |
| 2059 | * and the swap types are determined for each image. By the end of the loop |
| 2060 | * all required update operations will have been finished. |
| 2061 | */ |
| 2062 | for (current_image = 0; current_image < BOOT_IMAGE_NUMBER; ++current_image) |
| 2063 | { |
| 2064 | |
| 2065 | #if (BOOT_IMAGE_NUMBER > 1) |
| 2066 | /* Indicate that swap is not aborted */ |
| 2067 | memset(&bs, 0, sizeof bs); |
| 2068 | bs.idx = BOOT_STATUS_IDX_0; |
| 2069 | bs.state = BOOT_STATUS_STATE_0; |
| 2070 | #endif /* (BOOT_IMAGE_NUMBER > 1) */ |
| 2071 | |
| 2072 | /* Set the previously determined swap type */ |
| 2073 | bs.swap_type = BOOT_SWAP_TYPE(&boot_data); |
| 2074 | |
| 2075 | switch (BOOT_SWAP_TYPE(&boot_data)) { |
| 2076 | case BOOT_SWAP_TYPE_NONE: |
| 2077 | break; |
| 2078 | |
| 2079 | case BOOT_SWAP_TYPE_TEST: /* fallthrough */ |
| 2080 | case BOOT_SWAP_TYPE_PERM: /* fallthrough */ |
| 2081 | case BOOT_SWAP_TYPE_REVERT: |
| 2082 | rc = boot_perform_update(&bs); |
| 2083 | assert(rc == 0); |
| 2084 | break; |
| 2085 | |
| 2086 | case BOOT_SWAP_TYPE_FAIL: |
| 2087 | /* The image in secondary slot was invalid and is now erased. Ensure |
| 2088 | * we don't try to boot into it again on the next reboot. Do this by |
| 2089 | * pretending we just reverted back to primary slot. |
| 2090 | */ |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 2091 | #ifndef MCUBOOT_OVERWRITE_ONLY |
David Vincze | 7384ee7 | 2019-07-23 17:00:42 +0200 | [diff] [blame] | 2092 | /* image_ok needs to be explicitly set to avoid a new revert. */ |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 2093 | rc = boot_set_image_ok(); |
| 2094 | if (rc != 0) { |
David Vincze | 7384ee7 | 2019-07-23 17:00:42 +0200 | [diff] [blame] | 2095 | BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 2096 | } |
| 2097 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
David Vincze | 7384ee7 | 2019-07-23 17:00:42 +0200 | [diff] [blame] | 2098 | break; |
| 2099 | |
| 2100 | default: |
| 2101 | BOOT_SWAP_TYPE(&boot_data) = BOOT_SWAP_TYPE_PANIC; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 2102 | } |
David Vincze | 7384ee7 | 2019-07-23 17:00:42 +0200 | [diff] [blame] | 2103 | |
| 2104 | if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_PANIC) { |
| 2105 | BOOT_LOG_ERR("panic!"); |
| 2106 | assert(0); |
| 2107 | |
| 2108 | /* Loop forever... */ |
| 2109 | while (1) {} |
| 2110 | } |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 2111 | } |
| 2112 | |
David Vincze | 7384ee7 | 2019-07-23 17:00:42 +0200 | [diff] [blame] | 2113 | /* Iterate over all the images. At this point all required update operations |
| 2114 | * have finished. By the end of the loop each image in the primary slot will |
| 2115 | * have been re-validated. |
| 2116 | */ |
| 2117 | for (current_image = 0; current_image < BOOT_IMAGE_NUMBER; ++current_image) |
| 2118 | { |
| 2119 | if (BOOT_SWAP_TYPE(&boot_data) != BOOT_SWAP_TYPE_NONE) { |
| 2120 | /* Attempt to read an image header from each slot. Ensure that image |
| 2121 | * headers in slots are aligned with headers in boot_data. |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 2122 | */ |
David Vincze | 7384ee7 | 2019-07-23 17:00:42 +0200 | [diff] [blame] | 2123 | rc = boot_read_image_headers(false); |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 2124 | if (rc != 0) { |
David Vincze | 7384ee7 | 2019-07-23 17:00:42 +0200 | [diff] [blame] | 2125 | goto out; |
| 2126 | } |
| 2127 | /* Since headers were reloaded, it can be assumed we just performed |
| 2128 | * a swap or overwrite. Now the header info that should be used to |
| 2129 | * provide the data for the bootstrap, which previously was at |
| 2130 | * secondary slot, was updated to primary slot. |
| 2131 | */ |
| 2132 | } |
| 2133 | |
| 2134 | #ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT |
| 2135 | rc = boot_validate_slot(BOOT_PRIMARY_SLOT, NULL); |
| 2136 | if (rc != 0) { |
| 2137 | rc = BOOT_EBADIMAGE; |
| 2138 | goto out; |
| 2139 | } |
| 2140 | #else |
| 2141 | /* Even if we're not re-validating the primary slot, we could be booting |
| 2142 | * onto an empty flash chip. At least do a basic sanity check that |
| 2143 | * the magic number on the image is OK. |
| 2144 | */ |
Tamas Ban | 056ed0b | 2019-09-16 12:48:32 +0100 | [diff] [blame] | 2145 | if (!BOOT_IMG_HDR_IS_VALID(&boot_data, slot)) { |
| 2146 | BOOT_LOG_ERR("Invalid image header Image=%u", current_image); |
David Vincze | 7384ee7 | 2019-07-23 17:00:42 +0200 | [diff] [blame] | 2147 | rc = BOOT_EBADIMAGE; |
| 2148 | goto out; |
| 2149 | } |
| 2150 | #endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */ |
| 2151 | |
| 2152 | /* Update the stored security counter with the active image's security |
| 2153 | * counter value. It will be updated only if the new security counter is |
| 2154 | * greater than the stored value. |
| 2155 | * |
| 2156 | * In case of a successful image swapping when the swap type is TEST the |
| 2157 | * security counter can be increased only after a reset, when the swap |
| 2158 | * type is NONE and the image has marked itself "OK" (the image_ok flag |
| 2159 | * has been set). This way a "revert" swap can be performed if it's |
| 2160 | * necessary. |
| 2161 | */ |
| 2162 | if (BOOT_SWAP_TYPE(&boot_data) == BOOT_SWAP_TYPE_NONE) { |
| 2163 | rc = boot_update_security_counter(BOOT_PRIMARY_SLOT, |
| 2164 | boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT)); |
| 2165 | if (rc != 0) { |
| 2166 | BOOT_LOG_ERR("Security counter update failed after image " |
| 2167 | "validation."); |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 2168 | goto out; |
| 2169 | } |
| 2170 | } |
| 2171 | |
David Vincze | 7384ee7 | 2019-07-23 17:00:42 +0200 | [diff] [blame] | 2172 | /* Save boot status to shared memory area */ |
| 2173 | #if (BOOT_IMAGE_NUMBER > 1) |
| 2174 | rc = boot_save_boot_status((current_image == 0) ? SW_SPE : SW_NSPE, |
| 2175 | boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT), |
| 2176 | BOOT_IMG_AREA(&boot_data, BOOT_PRIMARY_SLOT) |
| 2177 | ); |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 2178 | #else |
David Vincze | 7384ee7 | 2019-07-23 17:00:42 +0200 | [diff] [blame] | 2179 | rc = boot_save_boot_status(SW_S_NS, |
| 2180 | boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT), |
| 2181 | BOOT_IMG_AREA(&boot_data, BOOT_PRIMARY_SLOT) |
| 2182 | ); |
| 2183 | #endif |
| 2184 | if (rc) { |
| 2185 | BOOT_LOG_ERR("Failed to add Image %u data to shared area", |
| 2186 | current_image); |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 2187 | } |
| 2188 | } |
| 2189 | |
David Vincze | 7384ee7 | 2019-07-23 17:00:42 +0200 | [diff] [blame] | 2190 | /* Always boot from the primary slot of Image 0. */ |
| 2191 | current_image = 0; |
| 2192 | rsp->br_flash_dev_id = |
| 2193 | BOOT_IMG_AREA(&boot_data, BOOT_PRIMARY_SLOT)->fa_device_id; |
| 2194 | rsp->br_image_off = |
| 2195 | boot_img_slot_off(&boot_data, BOOT_PRIMARY_SLOT); |
| 2196 | rsp->br_hdr = |
| 2197 | boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT); |
Tamas Ban | 0e8ab30 | 2019-01-17 11:45:31 +0000 | [diff] [blame] | 2198 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 2199 | out: |
David Vincze | 7384ee7 | 2019-07-23 17:00:42 +0200 | [diff] [blame] | 2200 | for (current_image = 0; current_image < BOOT_IMAGE_NUMBER; ++current_image) |
| 2201 | { |
| 2202 | flash_area_close(BOOT_SCRATCH_AREA(&boot_data)); |
| 2203 | for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) { |
| 2204 | flash_area_close(BOOT_IMG_AREA(&boot_data, |
| 2205 | BOOT_NUM_SLOTS - 1 - slot)); |
| 2206 | } |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 2207 | } |
| 2208 | return rc; |
| 2209 | } |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 2210 | |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 2211 | #else /* MCUBOOT_NO_SWAP || MCUBOOT_RAM_LOADING */ |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 2212 | |
David Vincze | dcba70b | 2019-05-28 12:02:52 +0200 | [diff] [blame] | 2213 | #define BOOT_LOG_IMAGE_INFO(area, hdr, state) \ |
| 2214 | BOOT_LOG_INF("Image %u: version=%u.%u.%u+%u, magic=%5s, image_ok=0x%x", \ |
| 2215 | (area), \ |
| 2216 | (hdr)->ih_ver.iv_major, \ |
| 2217 | (hdr)->ih_ver.iv_minor, \ |
| 2218 | (hdr)->ih_ver.iv_revision, \ |
| 2219 | (hdr)->ih_ver.iv_build_num, \ |
| 2220 | ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \ |
| 2221 | (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \ |
| 2222 | "bad"), \ |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 2223 | (state)->image_ok) |
| 2224 | |
| 2225 | struct image_slot_version { |
| 2226 | uint64_t version; |
| 2227 | uint32_t slot_number; |
| 2228 | }; |
| 2229 | |
| 2230 | /** |
| 2231 | * Extract the version number from the image header. This function must be |
| 2232 | * ported if version number format has changed in the image header. |
| 2233 | * |
| 2234 | * @param hdr Pointer to an image header structure |
| 2235 | * |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 2236 | * @return Version number casted to uint64_t |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 2237 | */ |
| 2238 | static uint64_t |
| 2239 | boot_get_version_number(struct image_header *hdr) |
| 2240 | { |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 2241 | uint64_t version = 0; |
| 2242 | version |= (uint64_t)hdr->ih_ver.iv_major << (IMAGE_VER_MINOR_LENGTH |
| 2243 | + IMAGE_VER_REVISION_LENGTH |
| 2244 | + IMAGE_VER_BUILD_NUM_LENGTH); |
| 2245 | version |= (uint64_t)hdr->ih_ver.iv_minor << (IMAGE_VER_REVISION_LENGTH |
| 2246 | + IMAGE_VER_BUILD_NUM_LENGTH); |
| 2247 | version |= (uint64_t)hdr->ih_ver.iv_revision << IMAGE_VER_BUILD_NUM_LENGTH; |
| 2248 | version |= hdr->ih_ver.iv_build_num; |
| 2249 | return version; |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 2250 | } |
| 2251 | |
| 2252 | /** |
| 2253 | * Comparator function for `qsort` to compare version numbers. This function |
| 2254 | * must be ported if version number format has changed in the image header. |
| 2255 | * |
| 2256 | * @param ver1 Pointer to an array element which holds the version number |
| 2257 | * @param ver2 Pointer to another array element which holds the version |
| 2258 | * number |
| 2259 | * |
| 2260 | * @return if version1 > version2 -1 |
| 2261 | * if version1 == version2 0 |
| 2262 | * if version1 < version2 1 |
| 2263 | */ |
| 2264 | static int |
| 2265 | boot_compare_version_numbers(const void *ver1, const void *ver2) |
| 2266 | { |
| 2267 | if (((struct image_slot_version *)ver1)->version < |
| 2268 | ((struct image_slot_version *)ver2)->version) { |
| 2269 | return 1; |
| 2270 | } |
| 2271 | |
| 2272 | if (((struct image_slot_version *)ver1)->version == |
| 2273 | ((struct image_slot_version *)ver2)->version) { |
| 2274 | return 0; |
| 2275 | } |
| 2276 | |
| 2277 | return -1; |
| 2278 | } |
| 2279 | |
| 2280 | /** |
| 2281 | * Sort the available images based on the version number and puts them in |
| 2282 | * a list. |
| 2283 | * |
| 2284 | * @param boot_sequence A pointer to an array, whose aim is to carry |
| 2285 | * the boot order of candidate images. |
| 2286 | * @param slot_cnt The number of flash areas, which can contains firmware |
| 2287 | * images. |
| 2288 | * |
| 2289 | * @return The number of valid images. |
| 2290 | */ |
| 2291 | uint32_t |
| 2292 | boot_get_boot_sequence(uint32_t *boot_sequence, uint32_t slot_cnt) |
| 2293 | { |
| 2294 | struct boot_swap_state slot_state; |
| 2295 | struct image_header *hdr; |
| 2296 | struct image_slot_version image_versions[BOOT_NUM_SLOTS] = {{0}}; |
| 2297 | uint32_t image_cnt = 0; |
| 2298 | uint32_t slot; |
| 2299 | int32_t rc; |
| 2300 | int32_t fa_id; |
| 2301 | |
| 2302 | for (slot = 0; slot < slot_cnt; slot++) { |
| 2303 | hdr = boot_img_hdr(&boot_data, slot); |
| 2304 | fa_id = flash_area_id_from_image_slot(slot); |
| 2305 | rc = boot_read_swap_state_by_id(fa_id, &slot_state); |
| 2306 | if (rc != 0) { |
David Vincze | dcba70b | 2019-05-28 12:02:52 +0200 | [diff] [blame] | 2307 | BOOT_LOG_ERR("Error during reading image trailer from slot: %u", |
| 2308 | slot); |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 2309 | continue; |
| 2310 | } |
| 2311 | |
Tamas Ban | 056ed0b | 2019-09-16 12:48:32 +0100 | [diff] [blame] | 2312 | if (BOOT_IMG_HDR_IS_VALID(&boot_data, slot)) { |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 2313 | if (slot_state.magic == BOOT_MAGIC_GOOD || |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 2314 | slot_state.image_ok == BOOT_FLAG_SET) { |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 2315 | /* Valid cases: |
| 2316 | * - Test mode: magic is OK in image trailer |
| 2317 | * - Permanent mode: image_ok flag has previously set |
| 2318 | */ |
| 2319 | image_versions[slot].slot_number = slot; |
| 2320 | image_versions[slot].version = boot_get_version_number(hdr); |
| 2321 | image_cnt++; |
| 2322 | } |
| 2323 | |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 2324 | BOOT_LOG_IMAGE_INFO(slot, hdr, &slot_state); |
| 2325 | } else { |
David Vincze | dcba70b | 2019-05-28 12:02:52 +0200 | [diff] [blame] | 2326 | BOOT_LOG_INF("Image %u: No valid image", slot); |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 2327 | } |
| 2328 | } |
| 2329 | |
| 2330 | /* Sort the images based on version number */ |
| 2331 | qsort(&image_versions[0], |
| 2332 | slot_cnt, |
| 2333 | sizeof(struct image_slot_version), |
| 2334 | boot_compare_version_numbers); |
| 2335 | |
| 2336 | /* Copy the calculated boot sequence to boot_sequence array */ |
| 2337 | for (slot = 0; slot < slot_cnt; slot++) { |
| 2338 | boot_sequence[slot] = image_versions[slot].slot_number; |
| 2339 | } |
| 2340 | |
| 2341 | return image_cnt; |
| 2342 | } |
| 2343 | |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 2344 | #ifdef MCUBOOT_RAM_LOADING |
Raef Coles | af08238 | 2019-10-01 11:10:33 +0100 | [diff] [blame] | 2345 | |
| 2346 | /** |
| 2347 | * Verifies that the image in a slot lies within the predefined bounds that are |
| 2348 | * allowed to be used by executable images. |
| 2349 | * |
| 2350 | * @param img_dst The address to which the image is going to be copied. |
| 2351 | * |
| 2352 | * @param img_sz The size of the image. |
| 2353 | * |
| 2354 | * @return 0 on success; nonzero on failure. |
| 2355 | */ |
| 2356 | static int |
| 2357 | boot_verify_ram_loading_address(uint32_t img_dst, uint32_t img_sz) |
| 2358 | { |
| 2359 | if (img_dst < IMAGE_EXECUTABLE_RAM_START) { |
| 2360 | return BOOT_EBADIMAGE; |
| 2361 | } |
| 2362 | |
| 2363 | if (boot_add_uint32_overflow_check(img_dst, img_sz)) { |
| 2364 | return BOOT_EBADIMAGE; |
| 2365 | } |
| 2366 | |
| 2367 | if (img_dst + img_sz > IMAGE_EXECUTABLE_RAM_START + |
| 2368 | IMAGE_EXECUTABLE_RAM_SIZE) { |
| 2369 | return BOOT_EBADIMAGE; |
| 2370 | } |
| 2371 | |
| 2372 | return 0; |
| 2373 | } |
| 2374 | |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 2375 | /** |
| 2376 | * Copies an image from a slot in the flash to an SRAM address, where the load |
| 2377 | * address has already been inserted into the image header by this point and is |
| 2378 | * extracted from it within this method. The copying is done sector-by-sector. |
| 2379 | * |
| 2380 | * @param slot The flash slot of the image to be copied to SRAM. |
| 2381 | * |
| 2382 | * @param hdr Pointer to the image header structure of the image |
Raef Coles | af08238 | 2019-10-01 11:10:33 +0100 | [diff] [blame] | 2383 | * |
| 2384 | * @param img_dst The address at which the image needs to be copied to |
| 2385 | * SRAM. |
| 2386 | * |
| 2387 | * @param img_sz The size of the image that needs to be copied to SRAM. |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 2388 | * |
| 2389 | * @return 0 on success; nonzero on failure. |
| 2390 | */ |
| 2391 | static int |
Raef Coles | af08238 | 2019-10-01 11:10:33 +0100 | [diff] [blame] | 2392 | boot_copy_image_to_sram(int slot, struct image_header *hdr, |
| 2393 | uint32_t img_dst, uint32_t img_sz) |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 2394 | { |
| 2395 | int rc; |
| 2396 | uint32_t sect_sz; |
| 2397 | uint32_t sect = 0; |
| 2398 | uint32_t bytes_copied = 0; |
| 2399 | const struct flash_area *fap_src = NULL; |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 2400 | |
Raef Coles | af08238 | 2019-10-01 11:10:33 +0100 | [diff] [blame] | 2401 | if (img_dst % 4 != 0) { |
Tamas Ban | c27b5c3 | 2019-05-28 16:30:19 +0100 | [diff] [blame] | 2402 | 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] | 2403 | "- the load address must be aligned with 4 bytes due to SRAM " |
Raef Coles | af08238 | 2019-10-01 11:10:33 +0100 | [diff] [blame] | 2404 | "restrictions", img_dst); |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 2405 | return BOOT_EBADARGS; |
| 2406 | } |
| 2407 | |
| 2408 | rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap_src); |
| 2409 | if (rc != 0) { |
| 2410 | return BOOT_EFLASH; |
| 2411 | } |
| 2412 | |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 2413 | while (bytes_copied < img_sz) { |
| 2414 | sect_sz = boot_img_sector_size(&boot_data, slot, sect); |
| 2415 | /* |
| 2416 | * Direct copy from where the image sector resides in flash to its new |
| 2417 | * location in SRAM |
| 2418 | */ |
| 2419 | rc = flash_area_read(fap_src, |
| 2420 | bytes_copied, |
Raef Coles | af08238 | 2019-10-01 11:10:33 +0100 | [diff] [blame] | 2421 | (void *)(img_dst + bytes_copied), |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 2422 | sect_sz); |
| 2423 | if (rc != 0) { |
| 2424 | BOOT_LOG_INF("Error whilst copying image from Flash to SRAM"); |
| 2425 | break; |
| 2426 | } else { |
| 2427 | bytes_copied += sect_sz; |
| 2428 | } |
| 2429 | sect++; |
| 2430 | } |
| 2431 | |
| 2432 | if (fap_src) { |
| 2433 | flash_area_close(fap_src); |
| 2434 | } |
| 2435 | return rc; |
| 2436 | } |
Raef Coles | 27a6145 | 2019-09-25 15:32:25 +0100 | [diff] [blame] | 2437 | |
| 2438 | /** |
| 2439 | * Removes an image from SRAM, by overwriting it with zeros. |
| 2440 | * |
Raef Coles | af08238 | 2019-10-01 11:10:33 +0100 | [diff] [blame] | 2441 | * @param img_dst The address of the image that needs to be removed from |
| 2442 | * SRAM. |
Raef Coles | 27a6145 | 2019-09-25 15:32:25 +0100 | [diff] [blame] | 2443 | * |
Raef Coles | af08238 | 2019-10-01 11:10:33 +0100 | [diff] [blame] | 2444 | * @param img_sz The size of the image that needs to be removed from |
| 2445 | * SRAM. |
Raef Coles | 27a6145 | 2019-09-25 15:32:25 +0100 | [diff] [blame] | 2446 | * |
| 2447 | * @return 0 on success; nonzero on failure. |
| 2448 | */ |
| 2449 | static int |
Raef Coles | af08238 | 2019-10-01 11:10:33 +0100 | [diff] [blame] | 2450 | boot_remove_image_from_sram(uint32_t img_dst, uint32_t img_sz) |
Raef Coles | 27a6145 | 2019-09-25 15:32:25 +0100 | [diff] [blame] | 2451 | { |
Raef Coles | af08238 | 2019-10-01 11:10:33 +0100 | [diff] [blame] | 2452 | BOOT_LOG_INF("Removing image from SRAM at address 0x%x", img_dst); |
| 2453 | memset((void*)img_dst, 0, img_sz); |
Raef Coles | 27a6145 | 2019-09-25 15:32:25 +0100 | [diff] [blame] | 2454 | |
| 2455 | return 0; |
| 2456 | } |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 2457 | #endif /* MCUBOOT_RAM_LOADING */ |
| 2458 | |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 2459 | /** |
| 2460 | * Prepares the booting process. This function choose the newer image in flash |
| 2461 | * as appropriate, and returns the address to boot from. |
| 2462 | * |
| 2463 | * @param rsp On success, indicates how booting should occur. |
| 2464 | * |
| 2465 | * @return 0 on success; nonzero on failure. |
| 2466 | */ |
| 2467 | int |
| 2468 | boot_go(struct boot_rsp *rsp) |
| 2469 | { |
| 2470 | size_t slot = 0; |
| 2471 | int32_t i; |
| 2472 | int rc; |
| 2473 | int fa_id; |
| 2474 | uint32_t boot_sequence[BOOT_NUM_SLOTS]; |
| 2475 | uint32_t img_cnt; |
Raef Coles | 27a6145 | 2019-09-25 15:32:25 +0100 | [diff] [blame] | 2476 | struct image_header *selected_image_header; |
| 2477 | #ifdef MCUBOOT_RAM_LOADING |
| 2478 | int image_copied = 0; |
Raef Coles | af08238 | 2019-10-01 11:10:33 +0100 | [diff] [blame] | 2479 | uint32_t img_dst = 0; |
| 2480 | uint32_t img_sz = 0; |
Raef Coles | 27a6145 | 2019-09-25 15:32:25 +0100 | [diff] [blame] | 2481 | #endif /* MCUBOOT_RAM_LOADING */ |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 2482 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 2483 | static boot_sector_t primary_slot_sectors[BOOT_MAX_IMG_SECTORS]; |
| 2484 | static boot_sector_t secondary_slot_sectors[BOOT_MAX_IMG_SECTORS]; |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 2485 | |
David Vincze | 7384ee7 | 2019-07-23 17:00:42 +0200 | [diff] [blame] | 2486 | BOOT_IMG(&boot_data, BOOT_PRIMARY_SLOT).sectors = |
| 2487 | &primary_slot_sectors[0]; |
| 2488 | BOOT_IMG(&boot_data, BOOT_SECONDARY_SLOT).sectors = |
| 2489 | &secondary_slot_sectors[0]; |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 2490 | |
| 2491 | /* Open boot_data image areas for the duration of this call. */ |
| 2492 | for (i = 0; i < BOOT_NUM_SLOTS; i++) { |
| 2493 | fa_id = flash_area_id_from_image_slot(i); |
| 2494 | rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, i)); |
| 2495 | assert(rc == 0); |
| 2496 | } |
| 2497 | |
| 2498 | /* Determine the sector layout of the image slots. */ |
| 2499 | rc = boot_read_sectors(); |
| 2500 | if (rc != 0) { |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 2501 | BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - too small?", |
| 2502 | BOOT_MAX_IMG_SECTORS); |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 2503 | goto out; |
| 2504 | } |
| 2505 | |
| 2506 | /* Attempt to read an image header from each slot. */ |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 2507 | rc = boot_read_image_headers(false); |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 2508 | if (rc != 0) { |
| 2509 | goto out; |
| 2510 | } |
| 2511 | |
| 2512 | img_cnt = boot_get_boot_sequence(boot_sequence, BOOT_NUM_SLOTS); |
| 2513 | if (img_cnt) { |
| 2514 | /* Authenticate images */ |
| 2515 | for (i = 0; i < img_cnt; i++) { |
Raef Coles | 27a6145 | 2019-09-25 15:32:25 +0100 | [diff] [blame] | 2516 | |
| 2517 | slot = boot_sequence[i]; |
| 2518 | selected_image_header = boot_img_hdr(&boot_data, slot); |
| 2519 | |
| 2520 | #ifdef MCUBOOT_RAM_LOADING |
| 2521 | if (selected_image_header->ih_flags & IMAGE_F_RAM_LOAD) { |
Raef Coles | af08238 | 2019-10-01 11:10:33 +0100 | [diff] [blame] | 2522 | |
| 2523 | img_dst = selected_image_header->ih_load_addr; |
| 2524 | |
| 2525 | rc = boot_read_image_size(slot, selected_image_header, &img_sz); |
| 2526 | if (rc != 0) { |
| 2527 | rc = BOOT_EFLASH; |
| 2528 | BOOT_LOG_INF("Could not load image headers from the image" |
| 2529 | "in the %s slot.", |
| 2530 | (slot == BOOT_PRIMARY_SLOT) ? |
| 2531 | "primary" : "secondary"); |
| 2532 | continue; |
| 2533 | } |
| 2534 | |
| 2535 | rc = boot_verify_ram_loading_address(img_dst, img_sz); |
| 2536 | if (rc != 0) { |
| 2537 | BOOT_LOG_INF("Could not copy image from the %s slot in " |
| 2538 | "the Flash to load address 0x%x in SRAM as" |
| 2539 | " the image would overlap memory outside" |
| 2540 | " the defined executable region.", |
| 2541 | (slot == BOOT_PRIMARY_SLOT) ? |
| 2542 | "primary" : "secondary", |
| 2543 | selected_image_header->ih_load_addr); |
| 2544 | continue; |
| 2545 | } |
| 2546 | |
Raef Coles | 27a6145 | 2019-09-25 15:32:25 +0100 | [diff] [blame] | 2547 | /* Copy image to the load address from where it |
| 2548 | * currently resides in flash |
| 2549 | */ |
Raef Coles | af08238 | 2019-10-01 11:10:33 +0100 | [diff] [blame] | 2550 | rc = boot_copy_image_to_sram(slot, selected_image_header, |
| 2551 | img_dst, img_sz); |
Raef Coles | 27a6145 | 2019-09-25 15:32:25 +0100 | [diff] [blame] | 2552 | if (rc != 0) { |
| 2553 | rc = BOOT_EBADIMAGE; |
| 2554 | BOOT_LOG_INF("Could not copy image from the %s slot in " |
| 2555 | "the Flash to load address 0x%x in SRAM, " |
| 2556 | "aborting..", (slot == BOOT_PRIMARY_SLOT) ? |
| 2557 | "primary" : "secondary", |
| 2558 | selected_image_header->ih_load_addr); |
| 2559 | continue; |
| 2560 | } else { |
| 2561 | BOOT_LOG_INF("Image has been copied from the %s slot in " |
| 2562 | "the flash to SRAM address 0x%x", |
| 2563 | (slot == BOOT_PRIMARY_SLOT) ? |
| 2564 | "primary" : "secondary", |
| 2565 | selected_image_header->ih_load_addr); |
| 2566 | image_copied = 1; |
| 2567 | } |
| 2568 | } else { |
| 2569 | /* Only images that support IMAGE_F_RAM_LOAD are allowed if |
| 2570 | * MCUBOOT_RAM_LOADING is set. |
| 2571 | */ |
| 2572 | rc = BOOT_EBADIMAGE; |
| 2573 | continue; |
| 2574 | } |
| 2575 | #endif /* MCUBOOT_RAM_LOADING */ |
| 2576 | rc = boot_validate_slot(slot, NULL); |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 2577 | if (rc == 0) { |
Raef Coles | 27a6145 | 2019-09-25 15:32:25 +0100 | [diff] [blame] | 2578 | /* If a valid image is found then there is no reason to check |
| 2579 | * the rest of the images, as they were already ordered by |
| 2580 | * preference. |
| 2581 | */ |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 2582 | break; |
| 2583 | } |
Raef Coles | 27a6145 | 2019-09-25 15:32:25 +0100 | [diff] [blame] | 2584 | #ifdef MCUBOOT_RAM_LOADING |
| 2585 | else if (image_copied) { |
| 2586 | /* If an image is found to be invalid then it is removed from |
| 2587 | * RAM to prevent it being a shellcode vector. |
| 2588 | */ |
Raef Coles | af08238 | 2019-10-01 11:10:33 +0100 | [diff] [blame] | 2589 | boot_remove_image_from_sram(img_dst, img_sz); |
Raef Coles | 27a6145 | 2019-09-25 15:32:25 +0100 | [diff] [blame] | 2590 | image_copied = 0; |
| 2591 | } |
| 2592 | #endif /* MCUBOOT_RAM_LOADING */ |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 2593 | } |
| 2594 | if (rc) { |
| 2595 | /* If there was no valid image at all */ |
| 2596 | rc = BOOT_EBADIMAGE; |
| 2597 | goto out; |
| 2598 | } |
| 2599 | |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 2600 | /* Update the security counter with the newest image's security |
| 2601 | * counter value. |
| 2602 | */ |
Raef Coles | 27a6145 | 2019-09-25 15:32:25 +0100 | [diff] [blame] | 2603 | rc = boot_update_security_counter(slot, selected_image_header); |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 2604 | if (rc != 0) { |
| 2605 | BOOT_LOG_ERR("Security counter update failed after image " |
| 2606 | "validation."); |
| 2607 | goto out; |
| 2608 | } |
| 2609 | |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 2610 | |
David Vincze | 8a2a4e2 | 2019-05-24 10:14:23 +0200 | [diff] [blame] | 2611 | #ifdef MCUBOOT_RAM_LOADING |
Raef Coles | 27a6145 | 2019-09-25 15:32:25 +0100 | [diff] [blame] | 2612 | BOOT_LOG_INF("Booting image from SRAM at address 0x%x", |
| 2613 | selected_image_header->ih_load_addr); |
| 2614 | #else |
| 2615 | BOOT_LOG_INF("Booting image from the %s slot", |
| 2616 | (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary"); |
| 2617 | #endif /* MCUBOOT_RAM_LOADING */ |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 2618 | |
Raef Coles | 27a6145 | 2019-09-25 15:32:25 +0100 | [diff] [blame] | 2619 | rsp->br_hdr = selected_image_header; |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 2620 | rsp->br_image_off = boot_img_slot_off(&boot_data, slot); |
David Vincze | 7384ee7 | 2019-07-23 17:00:42 +0200 | [diff] [blame] | 2621 | rsp->br_flash_dev_id = BOOT_IMG_AREA(&boot_data, slot)->fa_device_id; |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 2622 | } else { |
| 2623 | /* No candidate image available */ |
| 2624 | rc = BOOT_EBADIMAGE; |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 2625 | goto out; |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 2626 | } |
| 2627 | |
Tamas Ban | 0e8ab30 | 2019-01-17 11:45:31 +0000 | [diff] [blame] | 2628 | /* Save boot status to shared memory area */ |
| 2629 | rc = boot_save_boot_status(SW_S_NS, |
| 2630 | rsp->br_hdr, |
| 2631 | BOOT_IMG_AREA(&boot_data, slot)); |
| 2632 | if (rc) { |
| 2633 | BOOT_LOG_ERR("Failed to add data to shared area"); |
| 2634 | } |
| 2635 | |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 2636 | out: |
| 2637 | for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) { |
| 2638 | flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot)); |
| 2639 | } |
| 2640 | return rc; |
| 2641 | } |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 2642 | #endif /* MCUBOOT_NO_SWAP || MCUBOOT_RAM_LOADING */ |