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