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