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