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