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