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