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