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 | |
| 212 | static uint8_t |
| 213 | boot_write_sz(void) |
| 214 | { |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 215 | uint8_t elem_sz; |
| 216 | uint8_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 | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 222 | elem_sz = flash_area_align(boot_data.imgs[BOOT_PRIMARY_SLOT].area); |
| 223 | align = flash_area_align(boot_data.scratch.area); |
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 | |
| 395 | rc = boot_nv_security_counter_update(0, img_security_cnt); |
| 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; |
| 484 | BOOT_LOG_INF("Boot source: %s", |
| 485 | source == BOOT_STATUS_SOURCE_NONE ? "none" : |
| 486 | source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" : |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 487 | source == BOOT_STATUS_SOURCE_PRIMARY_SLOT ? |
| 488 | "primary slot" : "BUG; can't happen"); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 489 | return source; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | BOOT_LOG_INF("Boot source: none"); |
| 494 | return BOOT_STATUS_SOURCE_NONE; |
| 495 | } |
| 496 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 497 | /* |
| 498 | * Slots are compatible when all sectors that store upto to size of the image |
| 499 | * round up to sector size, in both slot's are able to fit in the scratch |
| 500 | * area, and have sizes that are a multiple of each other (powers of two |
| 501 | * presumably!). |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 502 | */ |
| 503 | static int |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 504 | boot_slots_compatible(void) |
| 505 | { |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 506 | size_t num_sectors_primary; |
| 507 | size_t num_sectors_secondary; |
| 508 | size_t sz0, sz1; |
| 509 | size_t primary_slot_sz, secondary_slot_sz; |
| 510 | size_t scratch_sz; |
| 511 | size_t i, j; |
| 512 | int8_t smaller; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 513 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 514 | num_sectors_primary = |
| 515 | boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT); |
| 516 | num_sectors_secondary = |
| 517 | boot_img_num_sectors(&boot_data, BOOT_SECONDARY_SLOT); |
| 518 | if ((num_sectors_primary > BOOT_MAX_IMG_SECTORS) || |
| 519 | (num_sectors_secondary > BOOT_MAX_IMG_SECTORS)) { |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 520 | BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed"); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 521 | return 0; |
| 522 | } |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 523 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 524 | scratch_sz = boot_scratch_area_size(&boot_data); |
| 525 | |
| 526 | /* |
| 527 | * The following loop scans all sectors in a linear fashion, assuring that |
| 528 | * for each possible sector in each slot, it is able to fit in the other |
| 529 | * slot's sector or sectors. Slot's should be compatible as long as any |
| 530 | * number of a slot's sectors are able to fit into another, which only |
| 531 | * excludes cases where sector sizes are not a multiple of each other. |
| 532 | */ |
| 533 | i = sz0 = primary_slot_sz = 0; |
| 534 | j = sz1 = secondary_slot_sz = 0; |
| 535 | smaller = 0; |
| 536 | while (i < num_sectors_primary || j < num_sectors_secondary) { |
| 537 | if (sz0 == sz1) { |
| 538 | sz0 += boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i); |
| 539 | sz1 += boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT, j); |
| 540 | i++; |
| 541 | j++; |
| 542 | } else if (sz0 < sz1) { |
| 543 | sz0 += boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i); |
| 544 | /* Guarantee that multiple sectors of the secondary slot |
| 545 | * fit into the primary slot. |
| 546 | */ |
| 547 | if (smaller == 2) { |
| 548 | BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible" |
| 549 | " sectors"); |
| 550 | return 0; |
| 551 | } |
| 552 | smaller = 1; |
| 553 | i++; |
| 554 | } else { |
| 555 | sz1 += boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT, j); |
| 556 | /* Guarantee that multiple sectors of the primary slot |
| 557 | * fit into the secondary slot. |
| 558 | */ |
| 559 | if (smaller == 1) { |
| 560 | BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible" |
| 561 | " sectors"); |
| 562 | return 0; |
| 563 | } |
| 564 | smaller = 2; |
| 565 | j++; |
| 566 | } |
| 567 | if (sz0 == sz1) { |
| 568 | primary_slot_sz += sz0; |
| 569 | secondary_slot_sz += sz1; |
| 570 | /* Scratch has to fit each swap operation to the size of the larger |
| 571 | * sector among the primary slot and the secondary slot. |
| 572 | */ |
| 573 | if (sz0 > scratch_sz || sz1 > scratch_sz) { |
| 574 | BOOT_LOG_WRN("Cannot upgrade: not all sectors fit inside" |
| 575 | " scratch"); |
| 576 | return 0; |
| 577 | } |
| 578 | smaller = sz0 = sz1 = 0; |
| 579 | } |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 580 | } |
| 581 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 582 | if ((i != num_sectors_primary) || |
| 583 | (j != num_sectors_secondary) || |
| 584 | (primary_slot_sz != secondary_slot_sz)) { |
| 585 | BOOT_LOG_WRN("Cannot upgrade: slots are not compatible"); |
| 586 | return 0; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | return 1; |
| 590 | } |
| 591 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 592 | static uint32_t |
| 593 | boot_status_internal_off(int idx, int state, int elem_sz) |
| 594 | { |
| 595 | int idx_sz; |
| 596 | |
| 597 | idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT; |
| 598 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 599 | return (idx - BOOT_STATUS_IDX_0) * idx_sz + |
| 600 | (state - BOOT_STATUS_STATE_0) * elem_sz; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | /** |
| 604 | * Reads the status of a partially-completed swap, if any. This is necessary |
| 605 | * to recover in case the boot lodaer was reset in the middle of a swap |
| 606 | * operation. |
| 607 | */ |
| 608 | static int |
| 609 | boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs) |
| 610 | { |
| 611 | uint32_t off; |
| 612 | uint8_t status; |
| 613 | int max_entries; |
| 614 | int found; |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 615 | int found_idx; |
| 616 | int invalid; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 617 | int rc; |
| 618 | int i; |
| 619 | |
| 620 | off = boot_status_off(fap); |
| 621 | max_entries = boot_status_entries(fap); |
| 622 | |
| 623 | found = 0; |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 624 | found_idx = 0; |
| 625 | invalid = 0; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 626 | for (i = 0; i < max_entries; i++) { |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 627 | rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(&boot_data), |
| 628 | &status, 1); |
| 629 | if (rc < 0) { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 630 | return BOOT_EFLASH; |
| 631 | } |
| 632 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 633 | if (rc == 1) { |
| 634 | if (found && !found_idx) { |
| 635 | found_idx = i; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 636 | } |
| 637 | } else if (!found) { |
| 638 | found = 1; |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 639 | } else if (found_idx) { |
| 640 | invalid = 1; |
| 641 | break; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 645 | if (invalid) { |
| 646 | /* This means there was an error writing status on the last |
| 647 | * swap. Tell user and move on to validation! |
| 648 | */ |
| 649 | BOOT_LOG_ERR("Detected inconsistent status!"); |
| 650 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 651 | #if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT) |
| 652 | /* With validation of the primary slot disabled, there is no way |
| 653 | * to be sure the swapped primary slot is OK, so abort! |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 654 | */ |
| 655 | assert(0); |
| 656 | #endif |
| 657 | } |
| 658 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 659 | if (found) { |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 660 | if (!found_idx) { |
| 661 | found_idx = i; |
| 662 | } |
| 663 | found_idx--; |
| 664 | bs->idx = (found_idx / BOOT_STATUS_STATE_COUNT) + 1; |
| 665 | bs->state = (found_idx % BOOT_STATUS_STATE_COUNT) + 1; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | return 0; |
| 669 | } |
| 670 | |
| 671 | /** |
| 672 | * Reads the boot status from the flash. The boot status contains |
| 673 | * the current state of an interrupted image copy operation. If the boot |
| 674 | * status is not present, or it indicates that previous copy finished, |
| 675 | * there is no operation in progress. |
| 676 | */ |
| 677 | static int |
| 678 | boot_read_status(struct boot_status *bs) |
| 679 | { |
| 680 | const struct flash_area *fap; |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 681 | uint32_t off; |
David Vincze | 91b71ef | 2019-06-24 13:06:47 +0200 | [diff] [blame] | 682 | uint8_t swap_info; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 683 | int status_loc; |
| 684 | int area_id; |
| 685 | int rc; |
| 686 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 687 | memset(bs, 0, sizeof *bs); |
| 688 | bs->idx = BOOT_STATUS_IDX_0; |
| 689 | bs->state = BOOT_STATUS_STATE_0; |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 690 | bs->swap_type = BOOT_SWAP_TYPE_NONE; |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 691 | |
| 692 | #ifdef MCUBOOT_OVERWRITE_ONLY |
| 693 | /* Overwrite-only doesn't make use of the swap status area. */ |
| 694 | return 0; |
| 695 | #endif |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 696 | |
| 697 | status_loc = boot_status_source(); |
| 698 | switch (status_loc) { |
| 699 | case BOOT_STATUS_SOURCE_NONE: |
| 700 | return 0; |
| 701 | |
| 702 | case BOOT_STATUS_SOURCE_SCRATCH: |
| 703 | area_id = FLASH_AREA_IMAGE_SCRATCH; |
| 704 | break; |
| 705 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 706 | case BOOT_STATUS_SOURCE_PRIMARY_SLOT: |
| 707 | area_id = FLASH_AREA_IMAGE_PRIMARY; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 708 | break; |
| 709 | |
| 710 | default: |
| 711 | assert(0); |
| 712 | return BOOT_EBADARGS; |
| 713 | } |
| 714 | |
| 715 | rc = flash_area_open(area_id, &fap); |
| 716 | if (rc != 0) { |
| 717 | return BOOT_EFLASH; |
| 718 | } |
| 719 | |
| 720 | rc = boot_read_status_bytes(fap, bs); |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 721 | if (rc == 0) { |
David Vincze | 91b71ef | 2019-06-24 13:06:47 +0200 | [diff] [blame] | 722 | off = boot_swap_info_off(fap); |
| 723 | 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] | 724 | if (rc == 1) { |
David Vincze | 91b71ef | 2019-06-24 13:06:47 +0200 | [diff] [blame] | 725 | BOOT_SET_SWAP_INFO(swap_info, 0, BOOT_SWAP_TYPE_NONE); |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 726 | rc = 0; |
| 727 | } |
David Vincze | 91b71ef | 2019-06-24 13:06:47 +0200 | [diff] [blame] | 728 | |
| 729 | /* Extract the swap type info */ |
| 730 | bs->swap_type = BOOT_GET_SWAP_TYPE(swap_info); |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 731 | } |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 732 | |
| 733 | flash_area_close(fap); |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 734 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 735 | return rc; |
| 736 | } |
| 737 | |
| 738 | /** |
| 739 | * Writes the supplied boot status to the flash file system. The boot status |
| 740 | * contains the current state of an in-progress image copy operation. |
| 741 | * |
| 742 | * @param bs The boot status to write. |
| 743 | * |
| 744 | * @return 0 on success; nonzero on failure. |
| 745 | */ |
| 746 | int |
| 747 | boot_write_status(struct boot_status *bs) |
| 748 | { |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 749 | const struct flash_area *fap = NULL; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 750 | uint32_t off; |
| 751 | int area_id; |
| 752 | int rc; |
| 753 | uint8_t buf[BOOT_MAX_ALIGN]; |
| 754 | uint8_t align; |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 755 | uint8_t erased_val; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 756 | |
| 757 | /* 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] | 758 | * the trailer. Since in the last step the primary slot is erased, the |
| 759 | * first two status writes go to the scratch which will be copied to |
| 760 | * the primary slot! |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 761 | */ |
| 762 | |
| 763 | if (bs->use_scratch) { |
| 764 | /* Write to scratch. */ |
| 765 | area_id = FLASH_AREA_IMAGE_SCRATCH; |
| 766 | } else { |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 767 | /* Write to the primary slot. */ |
| 768 | area_id = FLASH_AREA_IMAGE_PRIMARY; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | rc = flash_area_open(area_id, &fap); |
| 772 | if (rc != 0) { |
| 773 | rc = BOOT_EFLASH; |
| 774 | goto done; |
| 775 | } |
| 776 | |
| 777 | off = boot_status_off(fap) + |
| 778 | boot_status_internal_off(bs->idx, bs->state, |
| 779 | BOOT_WRITE_SZ(&boot_data)); |
| 780 | |
Tamas Ban | c382885 | 2018-02-01 12:24:16 +0000 | [diff] [blame] | 781 | align = flash_area_align(fap); |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 782 | erased_val = flash_area_erased_val(fap); |
| 783 | memset(buf, erased_val, BOOT_MAX_ALIGN); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 784 | buf[0] = bs->state; |
| 785 | |
| 786 | rc = flash_area_write(fap, off, buf, align); |
| 787 | if (rc != 0) { |
| 788 | rc = BOOT_EFLASH; |
| 789 | goto done; |
| 790 | } |
| 791 | |
| 792 | rc = 0; |
| 793 | |
| 794 | done: |
| 795 | flash_area_close(fap); |
| 796 | return rc; |
| 797 | } |
| 798 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 799 | /** |
| 800 | * Determines which swap operation to perform, if any. If it is determined |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 801 | * that a swap operation is required, the image in the secondary slot is checked |
| 802 | * for validity. If the image in the secondary slot is invalid, it is erased, |
| 803 | * and a swap type of "none" is indicated. |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 804 | * |
| 805 | * @return The type of swap to perform (BOOT_SWAP_TYPE...) |
| 806 | */ |
| 807 | static int |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 808 | boot_validated_swap_type(struct boot_status *bs) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 809 | { |
| 810 | int swap_type; |
| 811 | |
| 812 | swap_type = boot_swap_type(); |
| 813 | switch (swap_type) { |
| 814 | case BOOT_SWAP_TYPE_TEST: |
| 815 | case BOOT_SWAP_TYPE_PERM: |
| 816 | case BOOT_SWAP_TYPE_REVERT: |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 817 | /* Boot loader wants to switch to the secondary slot. |
| 818 | * Ensure image is valid. |
| 819 | */ |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 820 | if (boot_validate_slot(BOOT_SECONDARY_SLOT, bs) != 0) { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 821 | swap_type = BOOT_SWAP_TYPE_FAIL; |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | return swap_type; |
| 826 | } |
| 827 | |
| 828 | /** |
| 829 | * Calculates the number of sectors the scratch area can contain. A "last" |
| 830 | * source sector is specified because images are copied backwards in flash |
| 831 | * (final index to index number 0). |
| 832 | * |
| 833 | * @param last_sector_idx The index of the last source sector |
| 834 | * (inclusive). |
| 835 | * @param out_first_sector_idx The index of the first source sector |
| 836 | * (inclusive) gets written here. |
| 837 | * |
| 838 | * @return The number of bytes comprised by the |
| 839 | * [first-sector, last-sector] range. |
| 840 | */ |
| 841 | #ifndef MCUBOOT_OVERWRITE_ONLY |
| 842 | static uint32_t |
| 843 | boot_copy_sz(int last_sector_idx, int *out_first_sector_idx) |
| 844 | { |
| 845 | size_t scratch_sz; |
| 846 | uint32_t new_sz; |
| 847 | uint32_t sz; |
| 848 | int i; |
| 849 | |
| 850 | sz = 0; |
| 851 | |
| 852 | scratch_sz = boot_scratch_area_size(&boot_data); |
| 853 | for (i = last_sector_idx; i >= 0; i--) { |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 854 | 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] | 855 | /* |
| 856 | * The secondary slot is not being checked here, because |
| 857 | * `boot_slots_compatible` already provides assurance that the copy size |
| 858 | * will be compatible with the primary slot and scratch. |
| 859 | */ |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 860 | if (new_sz > scratch_sz) { |
| 861 | break; |
| 862 | } |
| 863 | sz = new_sz; |
| 864 | } |
| 865 | |
| 866 | /* i currently refers to a sector that doesn't fit or it is -1 because all |
| 867 | * sectors have been processed. In both cases, exclude sector i. |
| 868 | */ |
| 869 | *out_first_sector_idx = i + 1; |
| 870 | return sz; |
| 871 | } |
| 872 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
| 873 | |
| 874 | /** |
David Vincze | f7641fa | 2018-09-04 18:29:46 +0200 | [diff] [blame] | 875 | * Erases a region of flash. |
| 876 | * |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 877 | * @param flash_area The flash_area containing the region to erase. |
David Vincze | f7641fa | 2018-09-04 18:29:46 +0200 | [diff] [blame] | 878 | * @param off The offset within the flash area to start the |
| 879 | * erase. |
| 880 | * @param sz The number of bytes to erase. |
| 881 | * |
| 882 | * @return 0 on success; nonzero on failure. |
| 883 | */ |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 884 | static inline int |
| 885 | 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] | 886 | { |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 887 | return flash_area_erase(fap, off, sz); |
David Vincze | f7641fa | 2018-09-04 18:29:46 +0200 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | /** |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 891 | * Copies the contents of one flash region to another. You must erase the |
| 892 | * destination region prior to calling this function. |
| 893 | * |
| 894 | * @param flash_area_id_src The ID of the source flash area. |
| 895 | * @param flash_area_id_dst The ID of the destination flash area. |
| 896 | * @param off_src The offset within the source flash area to |
| 897 | * copy from. |
| 898 | * @param off_dst The offset within the destination flash area to |
| 899 | * copy to. |
| 900 | * @param sz The number of bytes to copy. |
| 901 | * |
| 902 | * @return 0 on success; nonzero on failure. |
| 903 | */ |
| 904 | static int |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 905 | boot_copy_sector(const struct flash_area *fap_src, |
| 906 | const struct flash_area *fap_dst, |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 907 | uint32_t off_src, uint32_t off_dst, uint32_t sz) |
| 908 | { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 909 | uint32_t bytes_copied; |
| 910 | int chunk_sz; |
| 911 | int rc; |
| 912 | |
| 913 | static uint8_t buf[1024]; |
| 914 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 915 | bytes_copied = 0; |
| 916 | while (bytes_copied < sz) { |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 917 | if (sz - bytes_copied > sizeof(buf)) { |
| 918 | chunk_sz = sizeof(buf); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 919 | } else { |
| 920 | chunk_sz = sz - bytes_copied; |
| 921 | } |
| 922 | |
| 923 | rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz); |
| 924 | if (rc != 0) { |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 925 | return BOOT_EFLASH; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz); |
| 929 | if (rc != 0) { |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 930 | return BOOT_EFLASH; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | bytes_copied += chunk_sz; |
| 934 | } |
| 935 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 936 | return 0; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | #ifndef MCUBOOT_OVERWRITE_ONLY |
| 940 | static inline int |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 941 | 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] | 942 | { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 943 | struct boot_swap_state swap_state; |
| 944 | int rc; |
| 945 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 946 | BOOT_LOG_DBG("initializing status; fa_id=%d", fap->fa_id); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 947 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 948 | 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] | 949 | assert(rc == 0); |
| 950 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 951 | if (bs->swap_type != BOOT_SWAP_TYPE_NONE) { |
David Vincze | 91b71ef | 2019-06-24 13:06:47 +0200 | [diff] [blame] | 952 | rc = boot_write_swap_info(fap, |
| 953 | bs->swap_type, |
| 954 | 0); |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 955 | assert(rc == 0); |
| 956 | } |
| 957 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 958 | if (swap_state.image_ok == BOOT_FLAG_SET) { |
| 959 | rc = boot_write_image_ok(fap); |
| 960 | assert(rc == 0); |
| 961 | } |
| 962 | |
| 963 | rc = boot_write_swap_size(fap, bs->swap_size); |
| 964 | assert(rc == 0); |
| 965 | |
| 966 | rc = boot_write_magic(fap); |
| 967 | assert(rc == 0); |
| 968 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 969 | return 0; |
| 970 | } |
David Vincze | f7641fa | 2018-09-04 18:29:46 +0200 | [diff] [blame] | 971 | |
| 972 | static int |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 973 | boot_erase_trailer_sectors(const struct flash_area *fap) |
David Vincze | f7641fa | 2018-09-04 18:29:46 +0200 | [diff] [blame] | 974 | { |
| 975 | uint8_t slot; |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 976 | uint32_t sector; |
| 977 | uint32_t trailer_sz; |
| 978 | uint32_t total_sz; |
| 979 | uint32_t off; |
| 980 | uint32_t sz; |
David Vincze | bb20798 | 2019-08-21 15:13:04 +0200 | [diff] [blame] | 981 | int fa_id_primary; |
| 982 | int fa_id_secondary; |
David Vincze | f7641fa | 2018-09-04 18:29:46 +0200 | [diff] [blame] | 983 | int rc; |
| 984 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 985 | BOOT_LOG_DBG("erasing trailer; fa_id=%d", fap->fa_id); |
| 986 | |
David Vincze | bb20798 | 2019-08-21 15:13:04 +0200 | [diff] [blame] | 987 | fa_id_primary = flash_area_id_from_image_slot(BOOT_PRIMARY_SLOT); |
| 988 | fa_id_secondary = flash_area_id_from_image_slot(BOOT_SECONDARY_SLOT); |
| 989 | |
| 990 | if (fap->fa_id == fa_id_primary) { |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 991 | slot = BOOT_PRIMARY_SLOT; |
David Vincze | bb20798 | 2019-08-21 15:13:04 +0200 | [diff] [blame] | 992 | } else if (fap->fa_id == fa_id_secondary) { |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 993 | slot = BOOT_SECONDARY_SLOT; |
David Vincze | bb20798 | 2019-08-21 15:13:04 +0200 | [diff] [blame] | 994 | } else { |
David Vincze | f7641fa | 2018-09-04 18:29:46 +0200 | [diff] [blame] | 995 | return BOOT_EFLASH; |
| 996 | } |
| 997 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 998 | /* delete starting from last sector and moving to beginning */ |
| 999 | sector = boot_img_num_sectors(&boot_data, slot) - 1; |
| 1000 | trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(&boot_data)); |
| 1001 | total_sz = 0; |
| 1002 | do { |
| 1003 | sz = boot_img_sector_size(&boot_data, slot, sector); |
| 1004 | off = boot_img_sector_off(&boot_data, slot, sector); |
| 1005 | rc = boot_erase_sector(fap, off, sz); |
| 1006 | assert(rc == 0); |
| 1007 | |
| 1008 | sector--; |
| 1009 | total_sz += sz; |
| 1010 | } while (total_sz < trailer_sz); |
David Vincze | f7641fa | 2018-09-04 18:29:46 +0200 | [diff] [blame] | 1011 | |
| 1012 | return rc; |
| 1013 | } |
| 1014 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1015 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1016 | /** |
| 1017 | * Swaps the contents of two flash regions within the two image slots. |
| 1018 | * |
| 1019 | * @param idx The index of the first sector in the range of |
| 1020 | * sectors being swapped. |
| 1021 | * @param sz The number of bytes to swap. |
| 1022 | * @param bs The current boot status. This struct gets |
| 1023 | * updated according to the outcome. |
| 1024 | * |
| 1025 | * @return 0 on success; nonzero on failure. |
| 1026 | */ |
| 1027 | #ifndef MCUBOOT_OVERWRITE_ONLY |
| 1028 | static void |
| 1029 | boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs) |
| 1030 | { |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1031 | const struct flash_area *fap_primary_slot; |
| 1032 | const struct flash_area *fap_secondary_slot; |
| 1033 | const struct flash_area *fap_scratch; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1034 | uint32_t copy_sz; |
| 1035 | uint32_t trailer_sz; |
| 1036 | uint32_t img_off; |
| 1037 | uint32_t scratch_trailer_off; |
| 1038 | struct boot_swap_state swap_state; |
| 1039 | size_t last_sector; |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1040 | bool erase_scratch; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1041 | int rc; |
| 1042 | |
| 1043 | /* Calculate offset from start of image area. */ |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1044 | img_off = boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT, idx); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1045 | |
| 1046 | copy_sz = sz; |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1047 | trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(&boot_data)); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1048 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1049 | /* sz in this function is always sized on a multiple of the sector size. |
| 1050 | * The check against the start offset of the last sector |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1051 | * is to determine if we're swapping the last sector. The last sector |
| 1052 | * needs special handling because it's where the trailer lives. If we're |
| 1053 | * copying it, we need to use scratch to write the trailer temporarily. |
| 1054 | * |
| 1055 | * NOTE: `use_scratch` is a temporary flag (never written to flash) which |
| 1056 | * controls if special handling is needed (swapping last sector). |
| 1057 | */ |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1058 | last_sector = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT) - 1; |
| 1059 | if (img_off + sz > boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT, |
| 1060 | last_sector)) { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1061 | copy_sz -= trailer_sz; |
| 1062 | } |
| 1063 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1064 | bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1065 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1066 | rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap_primary_slot); |
| 1067 | assert (rc == 0); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1068 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1069 | rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap_secondary_slot); |
| 1070 | assert (rc == 0); |
| 1071 | |
| 1072 | rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch); |
| 1073 | assert (rc == 0); |
| 1074 | |
| 1075 | if (bs->state == BOOT_STATUS_STATE_0) { |
| 1076 | BOOT_LOG_DBG("erasing scratch area"); |
| 1077 | rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1078 | assert(rc == 0); |
| 1079 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1080 | if (bs->idx == BOOT_STATUS_IDX_0) { |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1081 | /* Write a trailer to the scratch area, even if we don't need the |
| 1082 | * scratch area for status. We need a temporary place to store the |
| 1083 | * `swap-type` while we erase the primary trailer. |
| 1084 | */ |
| 1085 | rc = boot_status_init(fap_scratch, bs); |
| 1086 | assert(rc == 0); |
| 1087 | |
| 1088 | if (!bs->use_scratch) { |
| 1089 | /* Prepare the primary status area... here it is known that the |
| 1090 | * last sector is not being used by the image data so it's safe |
| 1091 | * to erase. |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1092 | */ |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1093 | rc = boot_erase_trailer_sectors(fap_primary_slot); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1094 | assert(rc == 0); |
| 1095 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1096 | rc = boot_status_init(fap_primary_slot, bs); |
| 1097 | assert(rc == 0); |
| 1098 | |
| 1099 | /* Erase the temporary trailer from the scratch area. */ |
| 1100 | rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size); |
| 1101 | assert(rc == 0); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1102 | } |
| 1103 | } |
| 1104 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1105 | rc = boot_copy_sector(fap_secondary_slot, fap_scratch, |
| 1106 | img_off, 0, copy_sz); |
| 1107 | assert(rc == 0); |
| 1108 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1109 | bs->state = BOOT_STATUS_STATE_1; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1110 | rc = boot_write_status(bs); |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1111 | BOOT_STATUS_ASSERT(rc == 0); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1114 | if (bs->state == BOOT_STATUS_STATE_1) { |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1115 | rc = boot_erase_sector(fap_secondary_slot, img_off, sz); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1116 | assert(rc == 0); |
| 1117 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1118 | rc = boot_copy_sector(fap_primary_slot, fap_secondary_slot, |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1119 | img_off, img_off, copy_sz); |
| 1120 | assert(rc == 0); |
| 1121 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1122 | if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1123 | /* If not all sectors of the slot are being swapped, |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1124 | * guarantee here that only the primary slot will have the state. |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1125 | */ |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1126 | rc = boot_erase_trailer_sectors(fap_secondary_slot); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1127 | assert(rc == 0); |
| 1128 | } |
| 1129 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1130 | bs->state = BOOT_STATUS_STATE_2; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1131 | rc = boot_write_status(bs); |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1132 | BOOT_STATUS_ASSERT(rc == 0); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1135 | if (bs->state == BOOT_STATUS_STATE_2) { |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1136 | rc = boot_erase_sector(fap_primary_slot, img_off, sz); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1137 | assert(rc == 0); |
| 1138 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1139 | /* NOTE: If this is the final sector, we exclude the image trailer from |
| 1140 | * this copy (copy_sz was truncated earlier). |
| 1141 | */ |
| 1142 | rc = boot_copy_sector(fap_scratch, fap_primary_slot, |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1143 | 0, img_off, copy_sz); |
| 1144 | assert(rc == 0); |
| 1145 | |
| 1146 | if (bs->use_scratch) { |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1147 | scratch_trailer_off = boot_status_off(fap_scratch); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1148 | |
| 1149 | /* copy current status that is being maintained in scratch */ |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1150 | rc = boot_copy_sector(fap_scratch, fap_primary_slot, |
| 1151 | scratch_trailer_off, img_off + copy_sz, |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1152 | BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data)); |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1153 | BOOT_STATUS_ASSERT(rc == 0); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1154 | |
| 1155 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, |
| 1156 | &swap_state); |
| 1157 | assert(rc == 0); |
| 1158 | |
| 1159 | if (swap_state.image_ok == BOOT_FLAG_SET) { |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1160 | rc = boot_write_image_ok(fap_primary_slot); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1161 | assert(rc == 0); |
| 1162 | } |
| 1163 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1164 | if (swap_state.swap_type != BOOT_SWAP_TYPE_NONE) { |
David Vincze | 91b71ef | 2019-06-24 13:06:47 +0200 | [diff] [blame] | 1165 | rc = boot_write_swap_info(fap_primary_slot, |
| 1166 | swap_state.swap_type, |
| 1167 | 0); |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1168 | assert(rc == 0); |
| 1169 | } |
| 1170 | |
| 1171 | rc = boot_write_swap_size(fap_primary_slot, bs->swap_size); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1172 | assert(rc == 0); |
| 1173 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1174 | rc = boot_write_magic(fap_primary_slot); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1175 | assert(rc == 0); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1178 | /* If we wrote a trailer to the scratch area, erase it after we persist |
| 1179 | * a trailer to the primary slot. We do this to prevent mcuboot from |
| 1180 | * reading a stale status from the scratch area in case of immediate |
| 1181 | * reset. |
| 1182 | */ |
| 1183 | erase_scratch = bs->use_scratch; |
| 1184 | bs->use_scratch = 0; |
| 1185 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1186 | bs->idx++; |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1187 | bs->state = BOOT_STATUS_STATE_0; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1188 | rc = boot_write_status(bs); |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1189 | BOOT_STATUS_ASSERT(rc == 0); |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1190 | |
| 1191 | if (erase_scratch) { |
| 1192 | rc = boot_erase_sector(fap_scratch, 0, sz); |
| 1193 | assert(rc == 0); |
| 1194 | } |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1195 | } |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1196 | |
| 1197 | flash_area_close(fap_primary_slot); |
| 1198 | flash_area_close(fap_secondary_slot); |
| 1199 | flash_area_close(fap_scratch); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1200 | } |
| 1201 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
| 1202 | |
| 1203 | /** |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1204 | * Overwrite primary slot with the image contained in the secondary slot. |
| 1205 | * If a prior copy operation was interrupted by a system reset, this function |
| 1206 | * redos the copy. |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1207 | * |
| 1208 | * @param bs The current boot status. This function reads |
| 1209 | * this struct to determine if it is resuming |
| 1210 | * an interrupted swap operation. This |
| 1211 | * function writes the updated status to this |
| 1212 | * function on return. |
| 1213 | * |
| 1214 | * @return 0 on success; nonzero on failure. |
| 1215 | */ |
| 1216 | #ifdef MCUBOOT_OVERWRITE_ONLY |
| 1217 | static int |
| 1218 | boot_copy_image(struct boot_status *bs) |
| 1219 | { |
| 1220 | size_t sect_count; |
| 1221 | size_t sect; |
| 1222 | int rc; |
| 1223 | size_t size = 0; |
| 1224 | size_t this_size; |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1225 | size_t last_sector; |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1226 | const struct flash_area *fap_primary_slot; |
| 1227 | const struct flash_area *fap_secondary_slot; |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1228 | |
| 1229 | (void)bs; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1230 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1231 | BOOT_LOG_INF("Image upgrade secondary slot -> primary slot"); |
| 1232 | BOOT_LOG_INF("Erasing the primary slot"); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1233 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1234 | rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap_primary_slot); |
| 1235 | assert (rc == 0); |
| 1236 | |
| 1237 | rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap_secondary_slot); |
| 1238 | assert (rc == 0); |
| 1239 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1240 | sect_count = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1241 | for (sect = 0; sect < sect_count; sect++) { |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1242 | this_size = boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, sect); |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1243 | rc = boot_erase_sector(fap_primary_slot, size, this_size); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1244 | assert(rc == 0); |
| 1245 | |
| 1246 | size += this_size; |
| 1247 | } |
| 1248 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1249 | BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes", |
| 1250 | size); |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1251 | 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] | 1252 | |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 1253 | /* Update the stored security counter with the new image's security counter |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1254 | * value. Both slots hold the new image at this point, but the secondary |
| 1255 | * slot's image header must be passed because the read image headers in the |
| 1256 | * boot_data structure have not been updated yet. |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 1257 | */ |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1258 | rc = boot_update_security_counter(BOOT_PRIMARY_SLOT, |
| 1259 | boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT)); |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 1260 | if (rc != 0) { |
| 1261 | BOOT_LOG_ERR("Security counter update failed after image upgrade."); |
| 1262 | return rc; |
| 1263 | } |
| 1264 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1265 | /* |
| 1266 | * Erases header and trailer. The trailer is erased because when a new |
| 1267 | * image is written without a trailer as is the case when using newt, the |
| 1268 | * trailer that was left might trigger a new upgrade. |
| 1269 | */ |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1270 | BOOT_LOG_DBG("erasing secondary header"); |
| 1271 | rc = boot_erase_sector(fap_secondary_slot, |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1272 | boot_img_sector_off(&boot_data, |
| 1273 | BOOT_SECONDARY_SLOT, 0), |
| 1274 | boot_img_sector_size(&boot_data, |
| 1275 | BOOT_SECONDARY_SLOT, 0)); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1276 | assert(rc == 0); |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1277 | last_sector = boot_img_num_sectors(&boot_data, BOOT_SECONDARY_SLOT) - 1; |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1278 | BOOT_LOG_DBG("erasing secondary trailer"); |
| 1279 | rc = boot_erase_sector(fap_secondary_slot, |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1280 | boot_img_sector_off(&boot_data, BOOT_SECONDARY_SLOT, |
| 1281 | last_sector), |
| 1282 | boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT, |
| 1283 | last_sector)); |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1284 | assert(rc == 0); |
| 1285 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1286 | flash_area_close(fap_primary_slot); |
| 1287 | flash_area_close(fap_secondary_slot); |
| 1288 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1289 | /* TODO: Perhaps verify the primary slot's signature again? */ |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1290 | |
| 1291 | return 0; |
| 1292 | } |
| 1293 | #else |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1294 | /** |
| 1295 | * Swaps the two images in flash. If a prior copy operation was interrupted |
| 1296 | * by a system reset, this function completes that operation. |
| 1297 | * |
| 1298 | * @param bs The current boot status. This function reads |
| 1299 | * this struct to determine if it is resuming |
| 1300 | * an interrupted swap operation. This |
| 1301 | * function writes the updated status to this |
| 1302 | * function on return. |
| 1303 | * |
| 1304 | * @return 0 on success; nonzero on failure. |
| 1305 | */ |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1306 | static int |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1307 | boot_swap_image(struct boot_status *bs) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1308 | { |
| 1309 | uint32_t sz; |
| 1310 | int first_sector_idx; |
| 1311 | int last_sector_idx; |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1312 | int last_idx_secondary_slot; |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1313 | uint32_t swap_idx; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1314 | struct image_header *hdr; |
| 1315 | uint32_t size; |
| 1316 | uint32_t copy_size; |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1317 | uint32_t primary_slot_size; |
| 1318 | uint32_t secondary_slot_size; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1319 | int rc; |
| 1320 | |
| 1321 | /* FIXME: just do this if asked by user? */ |
| 1322 | |
| 1323 | size = copy_size = 0; |
| 1324 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1325 | 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] | 1326 | /* |
| 1327 | * No swap ever happened, so need to find the largest image which |
| 1328 | * will be used to determine the amount of sectors to swap. |
| 1329 | */ |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1330 | hdr = boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1331 | if (hdr->ih_magic == IMAGE_MAGIC) { |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1332 | rc = boot_read_image_size(BOOT_PRIMARY_SLOT, hdr, ©_size); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1333 | assert(rc == 0); |
| 1334 | } |
| 1335 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1336 | hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1337 | if (hdr->ih_magic == IMAGE_MAGIC) { |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1338 | rc = boot_read_image_size(BOOT_SECONDARY_SLOT, hdr, &size); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1339 | assert(rc == 0); |
| 1340 | } |
| 1341 | |
| 1342 | if (size > copy_size) { |
| 1343 | copy_size = size; |
| 1344 | } |
| 1345 | |
| 1346 | bs->swap_size = copy_size; |
| 1347 | } else { |
| 1348 | /* |
| 1349 | * If a swap was under way, the swap_size should already be present |
| 1350 | * in the trailer... |
| 1351 | */ |
| 1352 | rc = boot_read_swap_size(&bs->swap_size); |
| 1353 | assert(rc == 0); |
| 1354 | |
| 1355 | copy_size = bs->swap_size; |
| 1356 | } |
| 1357 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1358 | primary_slot_size = 0; |
| 1359 | secondary_slot_size = 0; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1360 | last_sector_idx = 0; |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1361 | last_idx_secondary_slot = 0; |
| 1362 | |
| 1363 | /* |
| 1364 | * Knowing the size of the largest image between both slots, here we |
| 1365 | * find what is the last sector in the primary slot that needs swapping. |
| 1366 | * Since we already know that both slots are compatible, the secondary |
| 1367 | * slot's last sector is not really required after this check is finished. |
| 1368 | */ |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1369 | while (1) { |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1370 | if ((primary_slot_size < copy_size) || |
| 1371 | (primary_slot_size < secondary_slot_size)) { |
| 1372 | primary_slot_size += boot_img_sector_size(&boot_data, |
| 1373 | BOOT_PRIMARY_SLOT, |
| 1374 | last_sector_idx); |
| 1375 | } |
| 1376 | if ((secondary_slot_size < copy_size) || |
| 1377 | (secondary_slot_size < primary_slot_size)) { |
| 1378 | secondary_slot_size += boot_img_sector_size(&boot_data, |
| 1379 | BOOT_SECONDARY_SLOT, |
| 1380 | last_idx_secondary_slot); |
| 1381 | } |
| 1382 | if (primary_slot_size >= copy_size && |
| 1383 | secondary_slot_size >= copy_size && |
| 1384 | primary_slot_size == secondary_slot_size) { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1385 | break; |
| 1386 | } |
| 1387 | last_sector_idx++; |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1388 | last_idx_secondary_slot++; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | swap_idx = 0; |
| 1392 | while (last_sector_idx >= 0) { |
| 1393 | sz = boot_copy_sz(last_sector_idx, &first_sector_idx); |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1394 | if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1395 | boot_swap_sectors(first_sector_idx, sz, bs); |
| 1396 | } |
| 1397 | |
| 1398 | last_sector_idx = first_sector_idx - 1; |
| 1399 | swap_idx++; |
| 1400 | } |
| 1401 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1402 | #ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1403 | if (boot_status_fails > 0) { |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1404 | BOOT_LOG_WRN("%d status write fails performing the swap", |
| 1405 | boot_status_fails); |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1406 | } |
| 1407 | #endif |
| 1408 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1409 | return 0; |
| 1410 | } |
| 1411 | #endif |
| 1412 | |
| 1413 | /** |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1414 | * Marks the image in the primary slot as fully copied. |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1415 | */ |
| 1416 | #ifndef MCUBOOT_OVERWRITE_ONLY |
| 1417 | static int |
| 1418 | boot_set_copy_done(void) |
| 1419 | { |
| 1420 | const struct flash_area *fap; |
| 1421 | int rc; |
| 1422 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1423 | rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1424 | if (rc != 0) { |
| 1425 | return BOOT_EFLASH; |
| 1426 | } |
| 1427 | |
| 1428 | rc = boot_write_copy_done(fap); |
| 1429 | flash_area_close(fap); |
| 1430 | return rc; |
| 1431 | } |
| 1432 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
| 1433 | |
| 1434 | /** |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1435 | * Marks a reverted image in the primary slot as confirmed. This is necessary to |
| 1436 | * ensure the status bytes from the image revert operation don't get processed |
| 1437 | * on a subsequent boot. |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1438 | * |
| 1439 | * 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] | 1440 | * image installed on the primary slot and the new image to be upgrade to has a |
| 1441 | * bad sig, image_ok would be overwritten. |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1442 | */ |
| 1443 | #ifndef MCUBOOT_OVERWRITE_ONLY |
| 1444 | static int |
| 1445 | boot_set_image_ok(void) |
| 1446 | { |
| 1447 | const struct flash_area *fap; |
| 1448 | struct boot_swap_state state; |
| 1449 | int rc; |
| 1450 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1451 | rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1452 | if (rc != 0) { |
| 1453 | return BOOT_EFLASH; |
| 1454 | } |
| 1455 | |
| 1456 | rc = boot_read_swap_state(fap, &state); |
| 1457 | if (rc != 0) { |
| 1458 | rc = BOOT_EFLASH; |
| 1459 | goto out; |
| 1460 | } |
| 1461 | |
| 1462 | if (state.image_ok == BOOT_FLAG_UNSET) { |
| 1463 | rc = boot_write_image_ok(fap); |
| 1464 | } |
| 1465 | |
| 1466 | out: |
| 1467 | flash_area_close(fap); |
| 1468 | return rc; |
| 1469 | } |
| 1470 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
| 1471 | |
| 1472 | /** |
| 1473 | * Performs an image swap if one is required. |
| 1474 | * |
| 1475 | * @param out_swap_type On success, the type of swap performed gets |
| 1476 | * written here. |
| 1477 | * |
| 1478 | * @return 0 on success; nonzero on failure. |
| 1479 | */ |
| 1480 | static int |
| 1481 | boot_swap_if_needed(int *out_swap_type) |
| 1482 | { |
| 1483 | struct boot_status bs; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1484 | int rc; |
| 1485 | |
| 1486 | /* Determine if we rebooted in the middle of an image swap |
| 1487 | * operation. |
| 1488 | */ |
| 1489 | rc = boot_read_status(&bs); |
| 1490 | assert(rc == 0); |
| 1491 | if (rc != 0) { |
| 1492 | return rc; |
| 1493 | } |
| 1494 | |
| 1495 | /* If a partial swap was detected, complete it. */ |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1496 | if (bs.idx != BOOT_STATUS_IDX_0 || bs.state != BOOT_STATUS_STATE_0) { |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1497 | #ifdef MCUBOOT_OVERWRITE_ONLY |
| 1498 | /* Should never arrive here, overwrite-only mode has no swap state. */ |
| 1499 | assert(0); |
| 1500 | #else |
| 1501 | /* Determine the type of swap operation being resumed from the |
| 1502 | * `swap-type` trailer field. |
| 1503 | */ |
| 1504 | rc = boot_swap_image(&bs); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1505 | assert(rc == 0); |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1506 | #endif |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1507 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1508 | } else { |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1509 | if (bs.swap_type == BOOT_SWAP_TYPE_NONE) { |
| 1510 | bs.swap_type = boot_validated_swap_type(&bs); |
| 1511 | } else if (boot_validate_slot(BOOT_SECONDARY_SLOT, &bs) != 0) { |
| 1512 | bs.swap_type = BOOT_SWAP_TYPE_FAIL; |
| 1513 | } |
| 1514 | switch (bs.swap_type) { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1515 | case BOOT_SWAP_TYPE_TEST: |
| 1516 | case BOOT_SWAP_TYPE_PERM: |
| 1517 | case BOOT_SWAP_TYPE_REVERT: |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1518 | #ifdef MCUBOOT_OVERWRITE_ONLY |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1519 | rc = boot_copy_image(&bs); |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1520 | #else |
| 1521 | rc = boot_swap_image(&bs); |
| 1522 | #endif |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1523 | assert(rc == 0); |
| 1524 | break; |
| 1525 | } |
| 1526 | } |
| 1527 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1528 | *out_swap_type = bs.swap_type; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1529 | return 0; |
| 1530 | } |
| 1531 | |
| 1532 | /** |
| 1533 | * Prepares the booting process. This function moves images around in flash as |
| 1534 | * appropriate, and tells you what address to boot from. |
| 1535 | * |
| 1536 | * @param rsp On success, indicates how booting should occur. |
| 1537 | * |
| 1538 | * @return 0 on success; nonzero on failure. |
| 1539 | */ |
| 1540 | int |
| 1541 | boot_go(struct boot_rsp *rsp) |
| 1542 | { |
| 1543 | int swap_type; |
| 1544 | size_t slot; |
| 1545 | int rc; |
| 1546 | int fa_id; |
| 1547 | bool reload_headers = false; |
| 1548 | |
| 1549 | /* The array of slot sectors are defined here (as opposed to file scope) so |
| 1550 | * that they don't get allocated for non-boot-loader apps. This is |
| 1551 | * necessary because the gcc option "-fdata-sections" doesn't seem to have |
| 1552 | * any effect in older gcc versions (e.g., 4.8.4). |
| 1553 | */ |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1554 | static boot_sector_t primary_slot_sectors[BOOT_MAX_IMG_SECTORS]; |
| 1555 | static boot_sector_t secondary_slot_sectors[BOOT_MAX_IMG_SECTORS]; |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1556 | static boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS]; |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1557 | boot_data.imgs[BOOT_PRIMARY_SLOT].sectors = primary_slot_sectors; |
| 1558 | boot_data.imgs[BOOT_SECONDARY_SLOT].sectors = secondary_slot_sectors; |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1559 | boot_data.scratch.sectors = scratch_sectors; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1560 | |
| 1561 | /* Open boot_data image areas for the duration of this call. */ |
| 1562 | for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) { |
| 1563 | fa_id = flash_area_id_from_image_slot(slot); |
| 1564 | rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot)); |
| 1565 | assert(rc == 0); |
| 1566 | } |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 1567 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1568 | rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, |
| 1569 | &BOOT_SCRATCH_AREA(&boot_data)); |
| 1570 | assert(rc == 0); |
| 1571 | |
| 1572 | /* Determine the sector layout of the image slots and scratch area. */ |
| 1573 | rc = boot_read_sectors(); |
| 1574 | if (rc != 0) { |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1575 | BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - too small?", |
| 1576 | BOOT_MAX_IMG_SECTORS); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1577 | goto out; |
| 1578 | } |
| 1579 | |
| 1580 | /* Attempt to read an image header from each slot. */ |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1581 | rc = boot_read_image_headers(false); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1582 | if (rc != 0) { |
| 1583 | goto out; |
| 1584 | } |
| 1585 | |
| 1586 | /* If the image slots aren't compatible, no swap is possible. Just boot |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1587 | * into the primary slot. |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1588 | */ |
| 1589 | if (boot_slots_compatible()) { |
| 1590 | rc = boot_swap_if_needed(&swap_type); |
| 1591 | assert(rc == 0); |
| 1592 | if (rc != 0) { |
| 1593 | goto out; |
| 1594 | } |
| 1595 | |
| 1596 | /* |
| 1597 | * The following states need image_ok be explicitly set after the |
| 1598 | * swap was finished to avoid a new revert. |
| 1599 | */ |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 1600 | if (swap_type == BOOT_SWAP_TYPE_REVERT || |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1601 | swap_type == BOOT_SWAP_TYPE_FAIL || |
| 1602 | swap_type == BOOT_SWAP_TYPE_PERM) { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1603 | #ifndef MCUBOOT_OVERWRITE_ONLY |
| 1604 | rc = boot_set_image_ok(); |
| 1605 | if (rc != 0) { |
| 1606 | swap_type = BOOT_SWAP_TYPE_PANIC; |
| 1607 | } |
| 1608 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
| 1609 | } |
| 1610 | } else { |
| 1611 | swap_type = BOOT_SWAP_TYPE_NONE; |
| 1612 | } |
| 1613 | |
| 1614 | switch (swap_type) { |
| 1615 | case BOOT_SWAP_TYPE_NONE: |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1616 | slot = BOOT_PRIMARY_SLOT; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1617 | break; |
| 1618 | |
| 1619 | case BOOT_SWAP_TYPE_TEST: /* fallthrough */ |
| 1620 | case BOOT_SWAP_TYPE_PERM: /* fallthrough */ |
| 1621 | case BOOT_SWAP_TYPE_REVERT: |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1622 | slot = BOOT_SECONDARY_SLOT; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1623 | reload_headers = true; |
| 1624 | #ifndef MCUBOOT_OVERWRITE_ONLY |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 1625 | if (swap_type == BOOT_SWAP_TYPE_PERM) { |
| 1626 | /* Update the stored security counter with the new image's security |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1627 | * counter value. The primary slot holds the new image at this |
| 1628 | * point, but the secondary slot's image header must be passed |
| 1629 | * because the read image headers in the boot_data structure have |
| 1630 | * not been updated yet. |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 1631 | * |
| 1632 | * In case of a permanent image swap mcuboot will never attempt to |
| 1633 | * revert the images on the next reboot. Therefore, the security |
| 1634 | * counter must be increased right after the image upgrade. |
| 1635 | */ |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1636 | rc = boot_update_security_counter(BOOT_PRIMARY_SLOT, |
| 1637 | boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT)); |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 1638 | if (rc != 0) { |
| 1639 | BOOT_LOG_ERR("Security counter update failed after " |
| 1640 | "image upgrade."); |
| 1641 | goto out; |
| 1642 | } |
| 1643 | } |
| 1644 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1645 | rc = boot_set_copy_done(); |
| 1646 | if (rc != 0) { |
| 1647 | swap_type = BOOT_SWAP_TYPE_PANIC; |
| 1648 | } |
| 1649 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
| 1650 | break; |
| 1651 | |
| 1652 | case BOOT_SWAP_TYPE_FAIL: |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1653 | /* The image in the secondary slot was invalid and is now erased. |
| 1654 | * Ensure we don't try to boot into it again on the next reboot. |
| 1655 | * Do this by pretending we just reverted back to the primary slot. |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1656 | */ |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1657 | slot = BOOT_PRIMARY_SLOT; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1658 | reload_headers = true; |
| 1659 | break; |
| 1660 | |
| 1661 | default: |
| 1662 | swap_type = BOOT_SWAP_TYPE_PANIC; |
| 1663 | } |
| 1664 | |
| 1665 | if (swap_type == BOOT_SWAP_TYPE_PANIC) { |
| 1666 | BOOT_LOG_ERR("panic!"); |
| 1667 | assert(0); |
| 1668 | |
| 1669 | /* Loop forever... */ |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1670 | while (1) {} |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1671 | } |
| 1672 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1673 | if (reload_headers) { |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1674 | rc = boot_read_image_headers(false); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1675 | if (rc != 0) { |
| 1676 | goto out; |
| 1677 | } |
| 1678 | /* Since headers were reloaded, it can be assumed we just performed a |
| 1679 | * swap or overwrite. Now the header info that should be used to |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1680 | * provide the data for the bootstrap, which previously was at the |
| 1681 | * secondary slot, was updated to the primary slot. |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1682 | */ |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1683 | slot = BOOT_PRIMARY_SLOT; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1686 | #ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1687 | rc = boot_validate_slot(BOOT_PRIMARY_SLOT, NULL); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1688 | if (rc != 0) { |
| 1689 | rc = BOOT_EBADIMAGE; |
| 1690 | goto out; |
| 1691 | } |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1692 | #else |
| 1693 | /* Even if we're not re-validating the primary slot, we could be booting |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1694 | * onto an empty flash chip. At least do a basic sanity check that |
| 1695 | * the magic number on the image is OK. |
| 1696 | */ |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1697 | if (boot_data.imgs[BOOT_PRIMARY_SLOT].hdr.ih_magic != IMAGE_MAGIC) { |
| 1698 | BOOT_LOG_ERR("bad image magic 0x%lx", |
| 1699 | (unsigned long)boot_data.imgs[BOOT_PRIMARY_SLOT].hdr.ih_magic); |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1700 | rc = BOOT_EBADIMAGE; |
| 1701 | goto out; |
| 1702 | } |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1703 | #endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */ |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1704 | |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 1705 | /* Update the stored security counter with the active image's security |
| 1706 | * counter value. It will be updated only if the new security counter is |
| 1707 | * greater than the stored value. |
| 1708 | * |
| 1709 | * In case of a successful image swapping when the swap type is TEST the |
| 1710 | * security counter can be increased only after a reset, when the swap type |
| 1711 | * is NONE and the image has marked itself "OK" (the image_ok flag has been |
| 1712 | * set). This way a "revert" swap can be performed if it's necessary. |
| 1713 | */ |
| 1714 | if (swap_type == BOOT_SWAP_TYPE_NONE) { |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1715 | rc = boot_update_security_counter(BOOT_PRIMARY_SLOT, |
| 1716 | boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT)); |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 1717 | if (rc != 0) { |
| 1718 | BOOT_LOG_ERR("Security counter update failed after image " |
| 1719 | "validation."); |
| 1720 | goto out; |
| 1721 | } |
| 1722 | } |
| 1723 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1724 | /* Always boot from the primary slot. */ |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1725 | rsp->br_flash_dev_id = boot_data.imgs[BOOT_PRIMARY_SLOT].area->fa_device_id; |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1726 | rsp->br_image_off = boot_img_slot_off(&boot_data, BOOT_PRIMARY_SLOT); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1727 | rsp->br_hdr = boot_img_hdr(&boot_data, slot); |
| 1728 | |
Tamas Ban | 0e8ab30 | 2019-01-17 11:45:31 +0000 | [diff] [blame] | 1729 | /* Save boot status to shared memory area */ |
| 1730 | rc = boot_save_boot_status(SW_S_NS, |
| 1731 | rsp->br_hdr, |
| 1732 | BOOT_IMG_AREA(&boot_data, slot)); |
| 1733 | if (rc) { |
| 1734 | BOOT_LOG_ERR("Failed to add data to shared area"); |
| 1735 | } |
| 1736 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1737 | out: |
| 1738 | flash_area_close(BOOT_SCRATCH_AREA(&boot_data)); |
| 1739 | for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) { |
| 1740 | flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot)); |
| 1741 | } |
| 1742 | return rc; |
| 1743 | } |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 1744 | |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 1745 | #else /* MCUBOOT_NO_SWAP || MCUBOOT_RAM_LOADING */ |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 1746 | |
David Vincze | dcba70b | 2019-05-28 12:02:52 +0200 | [diff] [blame] | 1747 | #define BOOT_LOG_IMAGE_INFO(area, hdr, state) \ |
| 1748 | BOOT_LOG_INF("Image %u: version=%u.%u.%u+%u, magic=%5s, image_ok=0x%x", \ |
| 1749 | (area), \ |
| 1750 | (hdr)->ih_ver.iv_major, \ |
| 1751 | (hdr)->ih_ver.iv_minor, \ |
| 1752 | (hdr)->ih_ver.iv_revision, \ |
| 1753 | (hdr)->ih_ver.iv_build_num, \ |
| 1754 | ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \ |
| 1755 | (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \ |
| 1756 | "bad"), \ |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 1757 | (state)->image_ok) |
| 1758 | |
| 1759 | struct image_slot_version { |
| 1760 | uint64_t version; |
| 1761 | uint32_t slot_number; |
| 1762 | }; |
| 1763 | |
| 1764 | /** |
| 1765 | * Extract the version number from the image header. This function must be |
| 1766 | * ported if version number format has changed in the image header. |
| 1767 | * |
| 1768 | * @param hdr Pointer to an image header structure |
| 1769 | * |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 1770 | * @return Version number casted to uint64_t |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 1771 | */ |
| 1772 | static uint64_t |
| 1773 | boot_get_version_number(struct image_header *hdr) |
| 1774 | { |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 1775 | uint64_t version = 0; |
| 1776 | version |= (uint64_t)hdr->ih_ver.iv_major << (IMAGE_VER_MINOR_LENGTH |
| 1777 | + IMAGE_VER_REVISION_LENGTH |
| 1778 | + IMAGE_VER_BUILD_NUM_LENGTH); |
| 1779 | version |= (uint64_t)hdr->ih_ver.iv_minor << (IMAGE_VER_REVISION_LENGTH |
| 1780 | + IMAGE_VER_BUILD_NUM_LENGTH); |
| 1781 | version |= (uint64_t)hdr->ih_ver.iv_revision << IMAGE_VER_BUILD_NUM_LENGTH; |
| 1782 | version |= hdr->ih_ver.iv_build_num; |
| 1783 | return version; |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 1784 | } |
| 1785 | |
| 1786 | /** |
| 1787 | * Comparator function for `qsort` to compare version numbers. This function |
| 1788 | * must be ported if version number format has changed in the image header. |
| 1789 | * |
| 1790 | * @param ver1 Pointer to an array element which holds the version number |
| 1791 | * @param ver2 Pointer to another array element which holds the version |
| 1792 | * number |
| 1793 | * |
| 1794 | * @return if version1 > version2 -1 |
| 1795 | * if version1 == version2 0 |
| 1796 | * if version1 < version2 1 |
| 1797 | */ |
| 1798 | static int |
| 1799 | boot_compare_version_numbers(const void *ver1, const void *ver2) |
| 1800 | { |
| 1801 | if (((struct image_slot_version *)ver1)->version < |
| 1802 | ((struct image_slot_version *)ver2)->version) { |
| 1803 | return 1; |
| 1804 | } |
| 1805 | |
| 1806 | if (((struct image_slot_version *)ver1)->version == |
| 1807 | ((struct image_slot_version *)ver2)->version) { |
| 1808 | return 0; |
| 1809 | } |
| 1810 | |
| 1811 | return -1; |
| 1812 | } |
| 1813 | |
| 1814 | /** |
| 1815 | * Sort the available images based on the version number and puts them in |
| 1816 | * a list. |
| 1817 | * |
| 1818 | * @param boot_sequence A pointer to an array, whose aim is to carry |
| 1819 | * the boot order of candidate images. |
| 1820 | * @param slot_cnt The number of flash areas, which can contains firmware |
| 1821 | * images. |
| 1822 | * |
| 1823 | * @return The number of valid images. |
| 1824 | */ |
| 1825 | uint32_t |
| 1826 | boot_get_boot_sequence(uint32_t *boot_sequence, uint32_t slot_cnt) |
| 1827 | { |
| 1828 | struct boot_swap_state slot_state; |
| 1829 | struct image_header *hdr; |
| 1830 | struct image_slot_version image_versions[BOOT_NUM_SLOTS] = {{0}}; |
| 1831 | uint32_t image_cnt = 0; |
| 1832 | uint32_t slot; |
| 1833 | int32_t rc; |
| 1834 | int32_t fa_id; |
| 1835 | |
| 1836 | for (slot = 0; slot < slot_cnt; slot++) { |
| 1837 | hdr = boot_img_hdr(&boot_data, slot); |
| 1838 | fa_id = flash_area_id_from_image_slot(slot); |
| 1839 | rc = boot_read_swap_state_by_id(fa_id, &slot_state); |
| 1840 | if (rc != 0) { |
David Vincze | dcba70b | 2019-05-28 12:02:52 +0200 | [diff] [blame] | 1841 | BOOT_LOG_ERR("Error during reading image trailer from slot: %u", |
| 1842 | slot); |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 1843 | continue; |
| 1844 | } |
| 1845 | |
| 1846 | if (hdr->ih_magic == IMAGE_MAGIC) { |
| 1847 | if (slot_state.magic == BOOT_MAGIC_GOOD || |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1848 | slot_state.image_ok == BOOT_FLAG_SET) { |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 1849 | /* Valid cases: |
| 1850 | * - Test mode: magic is OK in image trailer |
| 1851 | * - Permanent mode: image_ok flag has previously set |
| 1852 | */ |
| 1853 | image_versions[slot].slot_number = slot; |
| 1854 | image_versions[slot].version = boot_get_version_number(hdr); |
| 1855 | image_cnt++; |
| 1856 | } |
| 1857 | |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 1858 | BOOT_LOG_IMAGE_INFO(slot, hdr, &slot_state); |
| 1859 | } else { |
David Vincze | dcba70b | 2019-05-28 12:02:52 +0200 | [diff] [blame] | 1860 | BOOT_LOG_INF("Image %u: No valid image", slot); |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 1861 | } |
| 1862 | } |
| 1863 | |
| 1864 | /* Sort the images based on version number */ |
| 1865 | qsort(&image_versions[0], |
| 1866 | slot_cnt, |
| 1867 | sizeof(struct image_slot_version), |
| 1868 | boot_compare_version_numbers); |
| 1869 | |
| 1870 | /* Copy the calculated boot sequence to boot_sequence array */ |
| 1871 | for (slot = 0; slot < slot_cnt; slot++) { |
| 1872 | boot_sequence[slot] = image_versions[slot].slot_number; |
| 1873 | } |
| 1874 | |
| 1875 | return image_cnt; |
| 1876 | } |
| 1877 | |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 1878 | #ifdef MCUBOOT_RAM_LOADING |
| 1879 | /** |
| 1880 | * Copies an image from a slot in the flash to an SRAM address, where the load |
| 1881 | * address has already been inserted into the image header by this point and is |
| 1882 | * extracted from it within this method. The copying is done sector-by-sector. |
| 1883 | * |
| 1884 | * @param slot The flash slot of the image to be copied to SRAM. |
| 1885 | * |
| 1886 | * @param hdr Pointer to the image header structure of the image |
| 1887 | * that needs to be copid to SRAM |
| 1888 | * |
| 1889 | * @return 0 on success; nonzero on failure. |
| 1890 | */ |
| 1891 | static int |
| 1892 | boot_copy_image_to_sram(int slot, struct image_header *hdr) |
| 1893 | { |
| 1894 | int rc; |
| 1895 | uint32_t sect_sz; |
| 1896 | uint32_t sect = 0; |
| 1897 | uint32_t bytes_copied = 0; |
| 1898 | const struct flash_area *fap_src = NULL; |
| 1899 | uint32_t dst = (uint32_t) hdr->ih_load_addr; |
| 1900 | uint32_t img_sz; |
| 1901 | |
| 1902 | if (dst % 4 != 0) { |
Tamas Ban | c27b5c3 | 2019-05-28 16:30:19 +0100 | [diff] [blame] | 1903 | 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] | 1904 | "- the load address must be aligned with 4 bytes due to SRAM " |
| 1905 | "restrictions", dst); |
| 1906 | return BOOT_EBADARGS; |
| 1907 | } |
| 1908 | |
| 1909 | rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap_src); |
| 1910 | if (rc != 0) { |
| 1911 | return BOOT_EFLASH; |
| 1912 | } |
| 1913 | |
| 1914 | rc = boot_read_image_size(slot, hdr, &img_sz); |
| 1915 | if (rc != 0) { |
| 1916 | return BOOT_EFLASH; |
| 1917 | } |
| 1918 | |
| 1919 | while (bytes_copied < img_sz) { |
| 1920 | sect_sz = boot_img_sector_size(&boot_data, slot, sect); |
| 1921 | /* |
| 1922 | * Direct copy from where the image sector resides in flash to its new |
| 1923 | * location in SRAM |
| 1924 | */ |
| 1925 | rc = flash_area_read(fap_src, |
| 1926 | bytes_copied, |
| 1927 | (void *)(dst + bytes_copied), |
| 1928 | sect_sz); |
| 1929 | if (rc != 0) { |
| 1930 | BOOT_LOG_INF("Error whilst copying image from Flash to SRAM"); |
| 1931 | break; |
| 1932 | } else { |
| 1933 | bytes_copied += sect_sz; |
| 1934 | } |
| 1935 | sect++; |
| 1936 | } |
| 1937 | |
| 1938 | if (fap_src) { |
| 1939 | flash_area_close(fap_src); |
| 1940 | } |
| 1941 | return rc; |
| 1942 | } |
| 1943 | #endif /* MCUBOOT_RAM_LOADING */ |
| 1944 | |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 1945 | /** |
| 1946 | * Prepares the booting process. This function choose the newer image in flash |
| 1947 | * as appropriate, and returns the address to boot from. |
| 1948 | * |
| 1949 | * @param rsp On success, indicates how booting should occur. |
| 1950 | * |
| 1951 | * @return 0 on success; nonzero on failure. |
| 1952 | */ |
| 1953 | int |
| 1954 | boot_go(struct boot_rsp *rsp) |
| 1955 | { |
| 1956 | size_t slot = 0; |
| 1957 | int32_t i; |
| 1958 | int rc; |
| 1959 | int fa_id; |
| 1960 | uint32_t boot_sequence[BOOT_NUM_SLOTS]; |
| 1961 | uint32_t img_cnt; |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 1962 | struct image_header *newest_image_header; |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 1963 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1964 | static boot_sector_t primary_slot_sectors[BOOT_MAX_IMG_SECTORS]; |
| 1965 | static boot_sector_t secondary_slot_sectors[BOOT_MAX_IMG_SECTORS]; |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 1966 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 1967 | boot_data.imgs[BOOT_PRIMARY_SLOT].sectors = &primary_slot_sectors[0]; |
| 1968 | boot_data.imgs[BOOT_SECONDARY_SLOT].sectors = &secondary_slot_sectors[0]; |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 1969 | |
| 1970 | /* Open boot_data image areas for the duration of this call. */ |
| 1971 | for (i = 0; i < BOOT_NUM_SLOTS; i++) { |
| 1972 | fa_id = flash_area_id_from_image_slot(i); |
| 1973 | rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, i)); |
| 1974 | assert(rc == 0); |
| 1975 | } |
| 1976 | |
| 1977 | /* Determine the sector layout of the image slots. */ |
| 1978 | rc = boot_read_sectors(); |
| 1979 | if (rc != 0) { |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1980 | BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - too small?", |
| 1981 | BOOT_MAX_IMG_SECTORS); |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 1982 | goto out; |
| 1983 | } |
| 1984 | |
| 1985 | /* Attempt to read an image header from each slot. */ |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 1986 | rc = boot_read_image_headers(false); |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 1987 | if (rc != 0) { |
| 1988 | goto out; |
| 1989 | } |
| 1990 | |
| 1991 | img_cnt = boot_get_boot_sequence(boot_sequence, BOOT_NUM_SLOTS); |
| 1992 | if (img_cnt) { |
| 1993 | /* Authenticate images */ |
| 1994 | for (i = 0; i < img_cnt; i++) { |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 1995 | rc = boot_validate_slot(boot_sequence[i], NULL); |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 1996 | if (rc == 0) { |
| 1997 | slot = boot_sequence[i]; |
| 1998 | break; |
| 1999 | } |
| 2000 | } |
| 2001 | if (rc) { |
| 2002 | /* If there was no valid image at all */ |
| 2003 | rc = BOOT_EBADIMAGE; |
| 2004 | goto out; |
| 2005 | } |
| 2006 | |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 2007 | /* The slot variable now refers to the newest image's slot in flash */ |
| 2008 | newest_image_header = boot_img_hdr(&boot_data, slot); |
| 2009 | |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 2010 | /* Update the security counter with the newest image's security |
| 2011 | * counter value. |
| 2012 | */ |
| 2013 | rc = boot_update_security_counter(slot, newest_image_header); |
| 2014 | if (rc != 0) { |
| 2015 | BOOT_LOG_ERR("Security counter update failed after image " |
| 2016 | "validation."); |
| 2017 | goto out; |
| 2018 | } |
| 2019 | |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 2020 | #ifdef MCUBOOT_RAM_LOADING |
| 2021 | if (newest_image_header->ih_flags & IMAGE_F_RAM_LOAD) { |
| 2022 | /* Copy image to the load address from where it |
| 2023 | * currently resides in flash */ |
| 2024 | rc = boot_copy_image_to_sram(slot, newest_image_header); |
| 2025 | if (rc != 0) { |
| 2026 | rc = BOOT_EBADIMAGE; |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 2027 | BOOT_LOG_INF("Could not copy image from the %s slot in " |
Tamas Ban | c27b5c3 | 2019-05-28 16:30:19 +0100 | [diff] [blame] | 2028 | "the Flash to load address 0x%x in SRAM, " |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 2029 | "aborting..", (slot == BOOT_PRIMARY_SLOT) ? |
| 2030 | "primary" : "secondary", |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 2031 | newest_image_header->ih_load_addr); |
| 2032 | goto out; |
| 2033 | } else { |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 2034 | BOOT_LOG_INF("Image has been copied from the %s slot in " |
| 2035 | "the flash to SRAM address 0x%x", |
| 2036 | (slot == BOOT_PRIMARY_SLOT) ? |
| 2037 | "primary" : "secondary", |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 2038 | newest_image_header->ih_load_addr); |
| 2039 | } |
| 2040 | |
| 2041 | /* Validate the image hash in SRAM after the copy was successful */ |
| 2042 | rc = bootutil_check_hash_after_loading(newest_image_header); |
| 2043 | if (rc != 0) { |
| 2044 | rc = BOOT_EBADIMAGE; |
| 2045 | BOOT_LOG_INF("Cannot validate the hash of the image that was " |
| 2046 | "copied to SRAM, aborting.."); |
| 2047 | goto out; |
| 2048 | } |
| 2049 | |
Tamas Ban | c27b5c3 | 2019-05-28 16:30:19 +0100 | [diff] [blame] | 2050 | BOOT_LOG_INF("Booting image from SRAM at address 0x%x", |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 2051 | newest_image_header->ih_load_addr); |
| 2052 | } else { |
David Vincze | 8a2a4e2 | 2019-05-24 10:14:23 +0200 | [diff] [blame] | 2053 | #endif /* MCUBOOT_RAM_LOADING */ |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 2054 | BOOT_LOG_INF("Booting image from the %s slot", |
| 2055 | (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary"); |
David Vincze | 8a2a4e2 | 2019-05-24 10:14:23 +0200 | [diff] [blame] | 2056 | #ifdef MCUBOOT_RAM_LOADING |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 2057 | } |
David Vincze | 8a2a4e2 | 2019-05-24 10:14:23 +0200 | [diff] [blame] | 2058 | #endif |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 2059 | |
| 2060 | rsp->br_hdr = newest_image_header; |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 2061 | rsp->br_image_off = boot_img_slot_off(&boot_data, slot); |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 2062 | rsp->br_flash_dev_id = boot_data.imgs[slot].area->fa_device_id; |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 2063 | } else { |
| 2064 | /* No candidate image available */ |
| 2065 | rc = BOOT_EBADIMAGE; |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 2066 | goto out; |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
Tamas Ban | 0e8ab30 | 2019-01-17 11:45:31 +0000 | [diff] [blame] | 2069 | /* Save boot status to shared memory area */ |
| 2070 | rc = boot_save_boot_status(SW_S_NS, |
| 2071 | rsp->br_hdr, |
| 2072 | BOOT_IMG_AREA(&boot_data, slot)); |
| 2073 | if (rc) { |
| 2074 | BOOT_LOG_ERR("Failed to add data to shared area"); |
| 2075 | } |
| 2076 | |
Tamas Ban | 4fb8e9d | 2018-02-23 14:22:03 +0000 | [diff] [blame] | 2077 | out: |
| 2078 | for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) { |
| 2079 | flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot)); |
| 2080 | } |
| 2081 | return rc; |
| 2082 | } |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 2083 | #endif /* MCUBOOT_NO_SWAP || MCUBOOT_RAM_LOADING */ |