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