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