Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1 | /* |
David Brown | aac7111 | 2020-02-03 16:13:42 -0700 | [diff] [blame] | 2 | * SPDX-License-Identifier: Apache-2.0 |
| 3 | * |
| 4 | * Copyright (c) 2016-2020 Linaro LTD |
| 5 | * Copyright (c) 2016-2019 JUUL Labs |
| 6 | * Copyright (c) 2019-2020 Arm Limited |
| 7 | * |
| 8 | * Original license: |
| 9 | * |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 10 | * Licensed to the Apache Software Foundation (ASF) under one |
| 11 | * or more contributor license agreements. See the NOTICE file |
| 12 | * distributed with this work for additional information |
| 13 | * regarding copyright ownership. The ASF licenses this file |
| 14 | * to you under the Apache License, Version 2.0 (the |
| 15 | * "License"); you may not use this file except in compliance |
| 16 | * with the License. You may obtain a copy of the License at |
| 17 | * |
| 18 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 19 | * |
| 20 | * Unless required by applicable law or agreed to in writing, |
| 21 | * software distributed under the License is distributed on an |
| 22 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 23 | * KIND, either express or implied. See the License for the |
| 24 | * specific language governing permissions and limitations |
| 25 | * under the License. |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * This file provides an interface to the boot loader. Functions defined in |
| 30 | * this file should only be called while the boot loader is running. |
| 31 | */ |
| 32 | |
| 33 | #include <assert.h> |
| 34 | #include <stddef.h> |
David Brown | 52eee56 | 2017-07-05 11:25:09 -0600 | [diff] [blame] | 35 | #include <stdbool.h> |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 36 | #include <inttypes.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <string.h> |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 39 | #include <os/os_malloc.h> |
| 40 | #include "bootutil/bootutil.h" |
| 41 | #include "bootutil/image.h" |
| 42 | #include "bootutil_priv.h" |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 43 | #include "swap_priv.h" |
Marti Bolivar | fd20c76 | 2017-02-07 16:52:50 -0500 | [diff] [blame] | 44 | #include "bootutil/bootutil_log.h" |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 45 | #include "bootutil/security_cnt.h" |
David Vincze | 1cf11b5 | 2020-03-24 07:51:09 +0100 | [diff] [blame] | 46 | #include "bootutil/boot_record.h" |
Marti Bolivar | fd20c76 | 2017-02-07 16:52:50 -0500 | [diff] [blame] | 47 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 48 | #ifdef MCUBOOT_ENC_IMAGES |
| 49 | #include "bootutil/enc_key.h" |
| 50 | #endif |
| 51 | |
Fabio Utzig | ba1fbe6 | 2017-07-21 14:01:20 -0300 | [diff] [blame] | 52 | #include "mcuboot_config/mcuboot_config.h" |
Fabio Utzig | eed80b6 | 2017-06-10 08:03:05 -0300 | [diff] [blame] | 53 | |
Emanuele Di Santo | 9f1933d | 2018-11-20 10:59:59 +0100 | [diff] [blame] | 54 | MCUBOOT_LOG_MODULE_DECLARE(mcuboot); |
| 55 | |
Marti Bolivar | 9b1f8bb | 2017-06-12 15:24:13 -0400 | [diff] [blame] | 56 | static struct boot_loader_state boot_data; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 57 | |
Fabio Utzig | abec073 | 2019-07-31 08:40:22 -0300 | [diff] [blame] | 58 | #if (BOOT_IMAGE_NUMBER > 1) |
| 59 | #define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x)) |
| 60 | #else |
| 61 | #define IMAGES_ITER(x) |
| 62 | #endif |
| 63 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 64 | /* |
| 65 | * This macro allows some control on the allocation of local variables. |
| 66 | * When running natively on a target, we don't want to allocated huge |
| 67 | * variables on the stack, so make them global instead. For the simulator |
| 68 | * we want to run as many threads as there are tests, and it's safer |
| 69 | * to just make those variables stack allocated. |
| 70 | */ |
| 71 | #if !defined(__BOOTSIM__) |
| 72 | #define TARGET_STATIC static |
| 73 | #else |
| 74 | #define TARGET_STATIC |
| 75 | #endif |
| 76 | |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame^] | 77 | static int |
| 78 | boot_read_image_headers(struct boot_loader_state *state, bool require_all, |
| 79 | struct boot_status *bs) |
| 80 | { |
| 81 | int rc; |
| 82 | int i; |
| 83 | |
| 84 | for (i = 0; i < BOOT_NUM_SLOTS; i++) { |
| 85 | rc = boot_read_image_header(state, i, boot_img_hdr(state, i), bs); |
| 86 | if (rc != 0) { |
| 87 | /* If `require_all` is set, fail on any single fail, otherwise |
| 88 | * if at least the first slot's header was read successfully, |
| 89 | * then the boot loader can attempt a boot. |
| 90 | * |
| 91 | * Failure to read any headers is a fatal error. |
| 92 | */ |
| 93 | if (i > 0 && !require_all) { |
| 94 | return 0; |
| 95 | } else { |
| 96 | return rc; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | #ifndef MCUBOOT_DIRECT_XIP |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 105 | /* |
| 106 | * Compute the total size of the given image. Includes the size of |
| 107 | * the TLVs. |
| 108 | */ |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 109 | #if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST) |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 110 | static int |
Fabio Utzig | d638b17 | 2019-08-09 10:38:05 -0300 | [diff] [blame] | 111 | boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size) |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 112 | { |
| 113 | const struct flash_area *fap; |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 114 | struct image_tlv_info info; |
Fabio Utzig | 233af7d | 2019-08-26 12:06:16 -0300 | [diff] [blame] | 115 | uint32_t off; |
Fabio Utzig | e52c08e | 2019-09-11 19:32:00 -0300 | [diff] [blame] | 116 | uint32_t protect_tlv_size; |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 117 | int area_id; |
| 118 | int rc; |
| 119 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 120 | #if (BOOT_IMAGE_NUMBER == 1) |
| 121 | (void)state; |
| 122 | #endif |
| 123 | |
| 124 | area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot); |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 125 | rc = flash_area_open(area_id, &fap); |
| 126 | if (rc != 0) { |
| 127 | rc = BOOT_EFLASH; |
| 128 | goto done; |
| 129 | } |
| 130 | |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 131 | off = BOOT_TLV_OFF(boot_img_hdr(state, slot)); |
| 132 | |
| 133 | if (flash_area_read(fap, off, &info, sizeof(info))) { |
| 134 | rc = BOOT_EFLASH; |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 135 | goto done; |
| 136 | } |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 137 | |
Fabio Utzig | e52c08e | 2019-09-11 19:32:00 -0300 | [diff] [blame] | 138 | protect_tlv_size = boot_img_hdr(state, slot)->ih_protect_tlv_size; |
| 139 | if (info.it_magic == IMAGE_TLV_PROT_INFO_MAGIC) { |
| 140 | if (protect_tlv_size != info.it_tlv_tot) { |
| 141 | rc = BOOT_EBADIMAGE; |
| 142 | goto done; |
| 143 | } |
| 144 | |
| 145 | if (flash_area_read(fap, off + info.it_tlv_tot, &info, sizeof(info))) { |
| 146 | rc = BOOT_EFLASH; |
| 147 | goto done; |
| 148 | } |
| 149 | } else if (protect_tlv_size != 0) { |
| 150 | rc = BOOT_EBADIMAGE; |
| 151 | goto done; |
| 152 | } |
| 153 | |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 154 | if (info.it_magic != IMAGE_TLV_INFO_MAGIC) { |
| 155 | rc = BOOT_EBADIMAGE; |
| 156 | goto done; |
| 157 | } |
| 158 | |
Fabio Utzig | e52c08e | 2019-09-11 19:32:00 -0300 | [diff] [blame] | 159 | *size = off + protect_tlv_size + info.it_tlv_tot; |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 160 | rc = 0; |
| 161 | |
| 162 | done: |
| 163 | flash_area_close(fap); |
Fabio Utzig | 2eebf11 | 2017-09-04 15:25:08 -0300 | [diff] [blame] | 164 | return rc; |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 165 | } |
Fabio Utzig | 36ec0e7 | 2017-09-05 08:10:33 -0300 | [diff] [blame] | 166 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 167 | |
David Brown | ab44918 | 2019-11-15 09:32:52 -0700 | [diff] [blame] | 168 | static uint32_t |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 169 | boot_write_sz(struct boot_loader_state *state) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 170 | { |
David Brown | ab44918 | 2019-11-15 09:32:52 -0700 | [diff] [blame] | 171 | uint32_t elem_sz; |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 172 | #if MCUBOOT_SWAP_USING_SCRATCH |
David Brown | ab44918 | 2019-11-15 09:32:52 -0700 | [diff] [blame] | 173 | uint32_t align; |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 174 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 175 | |
| 176 | /* Figure out what size to write update status update as. The size depends |
| 177 | * on what the minimum write size is for scratch area, active image slot. |
| 178 | * We need to use the bigger of those 2 values. |
| 179 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 180 | elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)); |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 181 | #if MCUBOOT_SWAP_USING_SCRATCH |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 182 | align = flash_area_align(BOOT_SCRATCH_AREA(state)); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 183 | if (align > elem_sz) { |
| 184 | elem_sz = align; |
| 185 | } |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 186 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 187 | |
| 188 | return elem_sz; |
| 189 | } |
| 190 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 191 | #ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS |
| 192 | static int |
| 193 | boot_initialize_area(struct boot_loader_state *state, int flash_area) |
| 194 | { |
| 195 | int num_sectors = BOOT_MAX_IMG_SECTORS; |
| 196 | int rc; |
| 197 | |
| 198 | if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) { |
| 199 | rc = flash_area_to_sectors(flash_area, &num_sectors, |
| 200 | BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors); |
| 201 | BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors = (size_t)num_sectors; |
| 202 | |
| 203 | } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) { |
| 204 | rc = flash_area_to_sectors(flash_area, &num_sectors, |
| 205 | BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors); |
| 206 | BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors = (size_t)num_sectors; |
| 207 | |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 208 | #if MCUBOOT_SWAP_USING_SCRATCH |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 209 | } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) { |
| 210 | rc = flash_area_to_sectors(flash_area, &num_sectors, |
| 211 | state->scratch.sectors); |
| 212 | state->scratch.num_sectors = (size_t)num_sectors; |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 213 | #endif |
| 214 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 215 | } else { |
| 216 | return BOOT_EFLASH; |
| 217 | } |
| 218 | |
| 219 | return rc; |
| 220 | } |
| 221 | #else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */ |
| 222 | static int |
| 223 | boot_initialize_area(struct boot_loader_state *state, int flash_area) |
| 224 | { |
| 225 | uint32_t num_sectors; |
| 226 | struct flash_sector *out_sectors; |
| 227 | size_t *out_num_sectors; |
| 228 | int rc; |
| 229 | |
| 230 | num_sectors = BOOT_MAX_IMG_SECTORS; |
| 231 | |
| 232 | if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) { |
| 233 | out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors; |
| 234 | out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors; |
| 235 | } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) { |
| 236 | out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors; |
| 237 | out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors; |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 238 | #if MCUBOOT_SWAP_USING_SCRATCH |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 239 | } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) { |
| 240 | out_sectors = state->scratch.sectors; |
| 241 | out_num_sectors = &state->scratch.num_sectors; |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 242 | #endif |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 243 | } else { |
| 244 | return BOOT_EFLASH; |
| 245 | } |
| 246 | |
| 247 | rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors); |
| 248 | if (rc != 0) { |
| 249 | return rc; |
| 250 | } |
| 251 | *out_num_sectors = num_sectors; |
| 252 | return 0; |
| 253 | } |
| 254 | #endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */ |
| 255 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 256 | /** |
| 257 | * Determines the sector layout of both image slots and the scratch area. |
| 258 | * This information is necessary for calculating the number of bytes to erase |
| 259 | * and copy during an image swap. The information collected during this |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 260 | * function is used to populate the state. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 261 | */ |
| 262 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 263 | boot_read_sectors(struct boot_loader_state *state) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 264 | { |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 265 | uint8_t image_index; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 266 | int rc; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 267 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 268 | image_index = BOOT_CURR_IMG(state); |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 269 | |
| 270 | rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index)); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 271 | if (rc != 0) { |
| 272 | return BOOT_EFLASH; |
| 273 | } |
| 274 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 275 | rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index)); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 276 | if (rc != 0) { |
| 277 | return BOOT_EFLASH; |
| 278 | } |
| 279 | |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 280 | #if MCUBOOT_SWAP_USING_SCRATCH |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 281 | rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH); |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 282 | if (rc != 0) { |
| 283 | return BOOT_EFLASH; |
| 284 | } |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 285 | #endif |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 286 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 287 | BOOT_WRITE_SZ(state) = boot_write_sz(state); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 292 | void |
| 293 | boot_status_reset(struct boot_status *bs) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 294 | { |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 295 | #ifdef MCUBOOT_ENC_IMAGES |
| 296 | memset(&bs->enckey, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_KEY_SIZE); |
| 297 | #if MCUBOOT_SWAP_SAVE_ENCTLV |
| 298 | memset(&bs->enctlv, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_TLV_ALIGN_SIZE); |
| 299 | #endif |
| 300 | #endif /* MCUBOOT_ENC_IMAGES */ |
| 301 | |
| 302 | bs->use_scratch = 0; |
| 303 | bs->swap_size = 0; |
| 304 | bs->source = 0; |
| 305 | |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 306 | bs->op = BOOT_STATUS_OP_MOVE; |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 307 | bs->idx = BOOT_STATUS_IDX_0; |
| 308 | bs->state = BOOT_STATUS_STATE_0; |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 309 | bs->swap_type = BOOT_SWAP_TYPE_NONE; |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 310 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 311 | |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 312 | bool |
| 313 | boot_status_is_reset(const struct boot_status *bs) |
| 314 | { |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 315 | return (bs->op == BOOT_STATUS_OP_MOVE && |
| 316 | bs->idx == BOOT_STATUS_IDX_0 && |
| 317 | bs->state == BOOT_STATUS_STATE_0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Writes the supplied boot status to the flash file system. The boot status |
| 322 | * contains the current state of an in-progress image copy operation. |
| 323 | * |
| 324 | * @param bs The boot status to write. |
| 325 | * |
| 326 | * @return 0 on success; nonzero on failure. |
| 327 | */ |
| 328 | int |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 329 | boot_write_status(const struct boot_loader_state *state, struct boot_status *bs) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 330 | { |
| 331 | const struct flash_area *fap; |
| 332 | uint32_t off; |
| 333 | int area_id; |
| 334 | int rc; |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 335 | uint8_t buf[BOOT_MAX_ALIGN]; |
David Brown | 9d72546 | 2017-01-23 15:50:58 -0700 | [diff] [blame] | 336 | uint8_t align; |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 337 | uint8_t erased_val; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 338 | |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 339 | /* NOTE: The first sector copied (that is the last sector on slot) contains |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 340 | * the trailer. Since in the last step the primary slot is erased, the |
| 341 | * first two status writes go to the scratch which will be copied to |
| 342 | * the primary slot! |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 343 | */ |
| 344 | |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 345 | #if MCUBOOT_SWAP_USING_SCRATCH |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 346 | if (bs->use_scratch) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 347 | /* Write to scratch. */ |
| 348 | area_id = FLASH_AREA_IMAGE_SCRATCH; |
| 349 | } else { |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 350 | #endif |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 351 | /* Write to the primary slot. */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 352 | area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state)); |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 353 | #if MCUBOOT_SWAP_USING_SCRATCH |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 354 | } |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 355 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 356 | |
| 357 | rc = flash_area_open(area_id, &fap); |
| 358 | if (rc != 0) { |
| 359 | rc = BOOT_EFLASH; |
| 360 | goto done; |
| 361 | } |
| 362 | |
| 363 | off = boot_status_off(fap) + |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 364 | boot_status_internal_off(bs, BOOT_WRITE_SZ(state)); |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 365 | align = flash_area_align(fap); |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 366 | erased_val = flash_area_erased_val(fap); |
| 367 | memset(buf, erased_val, BOOT_MAX_ALIGN); |
David Brown | 9d72546 | 2017-01-23 15:50:58 -0700 | [diff] [blame] | 368 | buf[0] = bs->state; |
| 369 | |
| 370 | rc = flash_area_write(fap, off, buf, align); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 371 | if (rc != 0) { |
| 372 | rc = BOOT_EFLASH; |
| 373 | goto done; |
| 374 | } |
| 375 | |
| 376 | rc = 0; |
| 377 | |
| 378 | done: |
| 379 | flash_area_close(fap); |
| 380 | return rc; |
| 381 | } |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame^] | 382 | #endif /* !MCUBOOT_DIRECT_XIP */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 383 | |
| 384 | /* |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 385 | * Validate image hash/signature and optionally the security counter in a slot. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 386 | */ |
| 387 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 388 | boot_image_check(struct boot_loader_state *state, struct image_header *hdr, |
| 389 | const struct flash_area *fap, struct boot_status *bs) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 390 | { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 391 | TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ]; |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 392 | uint8_t image_index; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 393 | int rc; |
| 394 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 395 | #if (BOOT_IMAGE_NUMBER == 1) |
| 396 | (void)state; |
| 397 | #endif |
| 398 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 399 | (void)bs; |
| 400 | (void)rc; |
Fabio Utzig | bc07793 | 2019-08-26 11:16:34 -0300 | [diff] [blame] | 401 | |
| 402 | image_index = BOOT_CURR_IMG(state); |
| 403 | |
| 404 | #ifdef MCUBOOT_ENC_IMAGES |
| 405 | if (MUST_DECRYPT(fap, image_index, hdr)) { |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 406 | rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 407 | if (rc < 0) { |
| 408 | return BOOT_EBADIMAGE; |
| 409 | } |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 410 | if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 411 | return BOOT_EBADIMAGE; |
| 412 | } |
| 413 | } |
Fabio Utzig | bc07793 | 2019-08-26 11:16:34 -0300 | [diff] [blame] | 414 | #endif |
| 415 | |
Fabio Utzig | 1e4284b | 2019-08-23 11:55:27 -0300 | [diff] [blame] | 416 | if (bootutil_img_validate(BOOT_CURR_ENC(state), image_index, hdr, fap, tmpbuf, |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 417 | BOOT_TMPBUF_SZ, NULL, 0, NULL)) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 418 | return BOOT_EBADIMAGE; |
| 419 | } |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 420 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 421 | return 0; |
| 422 | } |
| 423 | |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame^] | 424 | #ifndef MCUBOOT_DIRECT_XIP |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 425 | static int |
| 426 | split_image_check(struct image_header *app_hdr, |
| 427 | const struct flash_area *app_fap, |
| 428 | struct image_header *loader_hdr, |
| 429 | const struct flash_area *loader_fap) |
| 430 | { |
| 431 | static void *tmpbuf; |
| 432 | uint8_t loader_hash[32]; |
| 433 | |
| 434 | if (!tmpbuf) { |
| 435 | tmpbuf = malloc(BOOT_TMPBUF_SZ); |
| 436 | if (!tmpbuf) { |
| 437 | return BOOT_ENOMEM; |
| 438 | } |
| 439 | } |
| 440 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 441 | if (bootutil_img_validate(NULL, 0, loader_hdr, loader_fap, tmpbuf, |
| 442 | BOOT_TMPBUF_SZ, NULL, 0, loader_hash)) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 443 | return BOOT_EBADIMAGE; |
| 444 | } |
| 445 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 446 | if (bootutil_img_validate(NULL, 0, app_hdr, app_fap, tmpbuf, |
| 447 | BOOT_TMPBUF_SZ, loader_hash, 32, NULL)) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 448 | return BOOT_EBADIMAGE; |
| 449 | } |
| 450 | |
| 451 | return 0; |
| 452 | } |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame^] | 453 | #endif /* !MCUBOOT_DIRECT_XIP */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 454 | |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 455 | /* |
David Brown | 9bf95af | 2019-10-10 15:36:36 -0600 | [diff] [blame] | 456 | * Check that this is a valid header. Valid means that the magic is |
| 457 | * correct, and that the sizes/offsets are "sane". Sane means that |
| 458 | * there is no overflow on the arithmetic, and that the result fits |
| 459 | * within the flash area we are in. |
| 460 | */ |
| 461 | static bool |
| 462 | boot_is_header_valid(const struct image_header *hdr, const struct flash_area *fap) |
| 463 | { |
| 464 | uint32_t size; |
| 465 | |
| 466 | if (hdr->ih_magic != IMAGE_MAGIC) { |
| 467 | return false; |
| 468 | } |
| 469 | |
| 470 | if (!boot_u32_safe_add(&size, hdr->ih_img_size, hdr->ih_hdr_size)) { |
| 471 | return false; |
| 472 | } |
| 473 | |
| 474 | if (size >= fap->fa_size) { |
| 475 | return false; |
| 476 | } |
| 477 | |
| 478 | return true; |
| 479 | } |
| 480 | |
| 481 | /* |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 482 | * Check that a memory area consists of a given value. |
| 483 | */ |
| 484 | static inline bool |
| 485 | boot_data_is_set_to(uint8_t val, void *data, size_t len) |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 486 | { |
| 487 | uint8_t i; |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 488 | uint8_t *p = (uint8_t *)data; |
| 489 | for (i = 0; i < len; i++) { |
| 490 | if (val != p[i]) { |
| 491 | return false; |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 492 | } |
| 493 | } |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 494 | return true; |
| 495 | } |
| 496 | |
| 497 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 498 | boot_check_header_erased(struct boot_loader_state *state, int slot) |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 499 | { |
| 500 | const struct flash_area *fap; |
| 501 | struct image_header *hdr; |
| 502 | uint8_t erased_val; |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 503 | int area_id; |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 504 | int rc; |
| 505 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 506 | area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot); |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 507 | rc = flash_area_open(area_id, &fap); |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 508 | if (rc != 0) { |
| 509 | return -1; |
| 510 | } |
| 511 | |
| 512 | erased_val = flash_area_erased_val(fap); |
| 513 | flash_area_close(fap); |
| 514 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 515 | hdr = boot_img_hdr(state, slot); |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 516 | if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) { |
| 517 | return -1; |
| 518 | } |
| 519 | |
| 520 | return 0; |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 521 | } |
| 522 | |
Håkon Øye Amundsen | 2d1bac1 | 2020-01-03 13:08:09 +0000 | [diff] [blame] | 523 | #if (BOOT_IMAGE_NUMBER > 1) || \ |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame^] | 524 | defined(MCUBOOT_DIRECT_XIP) || \ |
Håkon Øye Amundsen | 2d1bac1 | 2020-01-03 13:08:09 +0000 | [diff] [blame] | 525 | (defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION)) |
| 526 | /** |
David Vincze | 8b0b637 | 2020-05-20 19:54:44 +0200 | [diff] [blame] | 527 | * Compare image version numbers not including the build number |
Håkon Øye Amundsen | 2d1bac1 | 2020-01-03 13:08:09 +0000 | [diff] [blame] | 528 | * |
David Vincze | 8b0b637 | 2020-05-20 19:54:44 +0200 | [diff] [blame] | 529 | * @param ver1 Pointer to the first image version to compare. |
| 530 | * @param ver2 Pointer to the second image version to compare. |
Håkon Øye Amundsen | 2d1bac1 | 2020-01-03 13:08:09 +0000 | [diff] [blame] | 531 | * |
David Vincze | 8b0b637 | 2020-05-20 19:54:44 +0200 | [diff] [blame] | 532 | * @retval -1 If ver1 is strictly less than ver2. |
| 533 | * @retval 0 If the image version numbers are equal, |
| 534 | * (not including the build number). |
| 535 | * @retval 1 If ver1 is strictly greater than ver2. |
Håkon Øye Amundsen | 2d1bac1 | 2020-01-03 13:08:09 +0000 | [diff] [blame] | 536 | */ |
| 537 | static int |
David Vincze | 8b0b637 | 2020-05-20 19:54:44 +0200 | [diff] [blame] | 538 | boot_version_cmp(const struct image_version *ver1, |
| 539 | const struct image_version *ver2) |
Håkon Øye Amundsen | 2d1bac1 | 2020-01-03 13:08:09 +0000 | [diff] [blame] | 540 | { |
David Vincze | 8b0b637 | 2020-05-20 19:54:44 +0200 | [diff] [blame] | 541 | if (ver1->iv_major > ver2->iv_major) { |
| 542 | return 1; |
Håkon Øye Amundsen | 2d1bac1 | 2020-01-03 13:08:09 +0000 | [diff] [blame] | 543 | } |
David Vincze | 8b0b637 | 2020-05-20 19:54:44 +0200 | [diff] [blame] | 544 | if (ver1->iv_major < ver2->iv_major) { |
| 545 | return -1; |
Håkon Øye Amundsen | 2d1bac1 | 2020-01-03 13:08:09 +0000 | [diff] [blame] | 546 | } |
David Vincze | 8b0b637 | 2020-05-20 19:54:44 +0200 | [diff] [blame] | 547 | /* The major version numbers are equal, continue comparison. */ |
| 548 | if (ver1->iv_minor > ver2->iv_minor) { |
| 549 | return 1; |
Håkon Øye Amundsen | 2d1bac1 | 2020-01-03 13:08:09 +0000 | [diff] [blame] | 550 | } |
David Vincze | 8b0b637 | 2020-05-20 19:54:44 +0200 | [diff] [blame] | 551 | if (ver1->iv_minor < ver2->iv_minor) { |
| 552 | return -1; |
Håkon Øye Amundsen | 2d1bac1 | 2020-01-03 13:08:09 +0000 | [diff] [blame] | 553 | } |
David Vincze | 8b0b637 | 2020-05-20 19:54:44 +0200 | [diff] [blame] | 554 | /* The minor version numbers are equal, continue comparison. */ |
| 555 | if (ver1->iv_revision > ver2->iv_revision) { |
| 556 | return 1; |
| 557 | } |
| 558 | if (ver1->iv_revision < ver2->iv_revision) { |
| 559 | return -1; |
Håkon Øye Amundsen | 2d1bac1 | 2020-01-03 13:08:09 +0000 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | return 0; |
| 563 | } |
| 564 | #endif |
| 565 | |
Fabio Utzig | b1adb1e | 2019-09-11 11:42:53 -0300 | [diff] [blame] | 566 | /* |
| 567 | * Check that there is a valid image in a slot |
| 568 | * |
| 569 | * @returns |
Sam Bristow | d0ca0ff | 2019-10-30 20:51:35 +1300 | [diff] [blame] | 570 | * 0 if image was successfully validated |
Fabio Utzig | b1adb1e | 2019-09-11 11:42:53 -0300 | [diff] [blame] | 571 | * 1 if no bootloable image was found |
| 572 | * -1 on any errors |
| 573 | */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 574 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 575 | boot_validate_slot(struct boot_loader_state *state, int slot, |
| 576 | struct boot_status *bs) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 577 | { |
| 578 | const struct flash_area *fap; |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 579 | struct image_header *hdr; |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 580 | int area_id; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 581 | int rc; |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 582 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 583 | area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot); |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 584 | rc = flash_area_open(area_id, &fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 585 | if (rc != 0) { |
Fabio Utzig | b1adb1e | 2019-09-11 11:42:53 -0300 | [diff] [blame] | 586 | return -1; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 587 | } |
| 588 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 589 | hdr = boot_img_hdr(state, slot); |
| 590 | if (boot_check_header_erased(state, slot) == 0 || |
| 591 | (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) { |
Fabio Utzig | 260ec45 | 2020-07-09 18:40:07 -0300 | [diff] [blame] | 592 | |
| 593 | #if defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE) |
| 594 | /* |
| 595 | * This fixes an issue where an image might be erased, but a trailer |
| 596 | * be left behind. It can happen if the image is in the secondary slot |
| 597 | * and did not pass validation, in which case the whole slot is erased. |
| 598 | * If during the erase operation, a reset occurs, parts of the slot |
| 599 | * might have been erased while some did not. The concerning part is |
| 600 | * the trailer because it might disable a new image from being loaded |
| 601 | * through mcumgr; so we just get rid of the trailer here, if the header |
| 602 | * is erased. |
| 603 | */ |
| 604 | if (slot != BOOT_PRIMARY_SLOT) { |
| 605 | swap_erase_trailer_sectors(state, fap); |
| 606 | } |
| 607 | #endif |
| 608 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 609 | /* No bootable image in slot; continue booting from the primary slot. */ |
Fabio Utzig | b1adb1e | 2019-09-11 11:42:53 -0300 | [diff] [blame] | 610 | rc = 1; |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 611 | goto out; |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 612 | } |
| 613 | |
Håkon Øye Amundsen | 2d1bac1 | 2020-01-03 13:08:09 +0000 | [diff] [blame] | 614 | #if defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION) |
| 615 | if (slot != BOOT_PRIMARY_SLOT) { |
| 616 | /* Check if version of secondary slot is sufficient */ |
David Vincze | 8b0b637 | 2020-05-20 19:54:44 +0200 | [diff] [blame] | 617 | rc = boot_version_cmp( |
| 618 | &boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver, |
| 619 | &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver); |
| 620 | if (rc < 0 && boot_check_header_erased(state, BOOT_PRIMARY_SLOT)) { |
Håkon Øye Amundsen | 2d1bac1 | 2020-01-03 13:08:09 +0000 | [diff] [blame] | 621 | BOOT_LOG_ERR("insufficient version in secondary slot"); |
| 622 | flash_area_erase(fap, 0, fap->fa_size); |
| 623 | /* Image in the secondary slot does not satisfy version requirement. |
| 624 | * Erase the image and continue booting from the primary slot. |
| 625 | */ |
| 626 | rc = 1; |
| 627 | goto out; |
| 628 | } |
| 629 | } |
| 630 | #endif |
| 631 | |
David Brown | 9bf95af | 2019-10-10 15:36:36 -0600 | [diff] [blame] | 632 | if (!boot_is_header_valid(hdr, fap) || boot_image_check(state, hdr, fap, bs)) { |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame^] | 633 | if ((slot != BOOT_PRIMARY_SLOT) || IS_IN_XIP_MODE()) { |
David Brown | b38e044 | 2017-02-24 13:57:12 -0700 | [diff] [blame] | 634 | flash_area_erase(fap, 0, fap->fa_size); |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame^] | 635 | /* Image is invalid, erase it to prevent further unnecessary |
| 636 | * attempts to validate and boot it. |
David Brown | b38e044 | 2017-02-24 13:57:12 -0700 | [diff] [blame] | 637 | */ |
| 638 | } |
David Brown | 098de83 | 2019-12-10 11:58:01 -0700 | [diff] [blame] | 639 | #if !defined(__BOOTSIM__) |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 640 | BOOT_LOG_ERR("Image in the %s slot is not valid!", |
| 641 | (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary"); |
David Brown | 098de83 | 2019-12-10 11:58:01 -0700 | [diff] [blame] | 642 | #endif |
Håkon Øye Amundsen | 2d1bac1 | 2020-01-03 13:08:09 +0000 | [diff] [blame] | 643 | rc = 1; |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 644 | goto out; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 645 | } |
| 646 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 647 | /* Image in the secondary slot is valid. */ |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 648 | rc = 0; |
| 649 | |
| 650 | out: |
| 651 | flash_area_close(fap); |
| 652 | return rc; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 653 | } |
| 654 | |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 655 | #ifdef MCUBOOT_HW_ROLLBACK_PROT |
| 656 | /** |
| 657 | * Updates the stored security counter value with the image's security counter |
| 658 | * value which resides in the given slot, only if it's greater than the stored |
| 659 | * value. |
| 660 | * |
| 661 | * @param image_index Index of the image to determine which security |
| 662 | * counter to update. |
| 663 | * @param slot Slot number of the image. |
| 664 | * @param hdr Pointer to the image header structure of the image |
| 665 | * that is currently stored in the given slot. |
| 666 | * |
| 667 | * @return 0 on success; nonzero on failure. |
| 668 | */ |
| 669 | static int |
| 670 | boot_update_security_counter(uint8_t image_index, int slot, |
| 671 | struct image_header *hdr) |
| 672 | { |
| 673 | const struct flash_area *fap = NULL; |
| 674 | uint32_t img_security_cnt; |
| 675 | int rc; |
| 676 | |
| 677 | rc = flash_area_open(flash_area_id_from_multi_image_slot(image_index, slot), |
| 678 | &fap); |
| 679 | if (rc != 0) { |
| 680 | rc = BOOT_EFLASH; |
| 681 | goto done; |
| 682 | } |
| 683 | |
| 684 | rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt); |
| 685 | if (rc != 0) { |
| 686 | goto done; |
| 687 | } |
| 688 | |
| 689 | rc = boot_nv_security_counter_update(image_index, img_security_cnt); |
| 690 | if (rc != 0) { |
| 691 | goto done; |
| 692 | } |
| 693 | |
| 694 | done: |
| 695 | flash_area_close(fap); |
| 696 | return rc; |
| 697 | } |
| 698 | #endif /* MCUBOOT_HW_ROLLBACK_PROT */ |
| 699 | |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame^] | 700 | #ifndef MCUBOOT_DIRECT_XIP |
| 701 | /** |
| 702 | * Determines which swap operation to perform, if any. If it is determined |
| 703 | * that a swap operation is required, the image in the secondary slot is checked |
| 704 | * for validity. If the image in the secondary slot is invalid, it is erased, |
| 705 | * and a swap type of "none" is indicated. |
| 706 | * |
| 707 | * @return The type of swap to perform (BOOT_SWAP_TYPE...) |
| 708 | */ |
| 709 | static int |
| 710 | boot_validated_swap_type(struct boot_loader_state *state, |
| 711 | struct boot_status *bs) |
| 712 | { |
| 713 | int swap_type; |
| 714 | int rc; |
| 715 | |
| 716 | swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state)); |
| 717 | if (BOOT_IS_UPGRADE(swap_type)) { |
| 718 | /* Boot loader wants to switch to the secondary slot. |
| 719 | * Ensure image is valid. |
| 720 | */ |
| 721 | rc = boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs); |
| 722 | if (rc == 1) { |
| 723 | swap_type = BOOT_SWAP_TYPE_NONE; |
| 724 | } else if (rc != 0) { |
| 725 | swap_type = BOOT_SWAP_TYPE_FAIL; |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | return swap_type; |
| 730 | } |
| 731 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 732 | /** |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 733 | * Erases a region of flash. |
| 734 | * |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 735 | * @param flash_area The flash_area containing the region to erase. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 736 | * @param off The offset within the flash area to start the |
| 737 | * erase. |
| 738 | * @param sz The number of bytes to erase. |
| 739 | * |
| 740 | * @return 0 on success; nonzero on failure. |
| 741 | */ |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 742 | int |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame] | 743 | boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 744 | { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 745 | return flash_area_erase(fap, off, sz); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | /** |
| 749 | * Copies the contents of one flash region to another. You must erase the |
| 750 | * destination region prior to calling this function. |
| 751 | * |
| 752 | * @param flash_area_id_src The ID of the source flash area. |
| 753 | * @param flash_area_id_dst The ID of the destination flash area. |
| 754 | * @param off_src The offset within the source flash area to |
| 755 | * copy from. |
| 756 | * @param off_dst The offset within the destination flash area to |
| 757 | * copy to. |
| 758 | * @param sz The number of bytes to copy. |
| 759 | * |
| 760 | * @return 0 on success; nonzero on failure. |
| 761 | */ |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 762 | int |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame] | 763 | boot_copy_region(struct boot_loader_state *state, |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 764 | const struct flash_area *fap_src, |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 765 | const struct flash_area *fap_dst, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 766 | uint32_t off_src, uint32_t off_dst, uint32_t sz) |
| 767 | { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 768 | uint32_t bytes_copied; |
| 769 | int chunk_sz; |
| 770 | int rc; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 771 | #ifdef MCUBOOT_ENC_IMAGES |
| 772 | uint32_t off; |
Fabio Utzig | a87cc7d | 2019-08-26 11:21:45 -0300 | [diff] [blame] | 773 | uint32_t tlv_off; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 774 | size_t blk_off; |
| 775 | struct image_header *hdr; |
| 776 | uint16_t idx; |
| 777 | uint32_t blk_sz; |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 778 | uint8_t image_index; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 779 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 780 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 781 | TARGET_STATIC uint8_t buf[1024]; |
| 782 | |
| 783 | #if !defined(MCUBOOT_ENC_IMAGES) |
| 784 | (void)state; |
| 785 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 786 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 787 | bytes_copied = 0; |
| 788 | while (bytes_copied < sz) { |
| 789 | if (sz - bytes_copied > sizeof buf) { |
| 790 | chunk_sz = sizeof buf; |
| 791 | } else { |
| 792 | chunk_sz = sz - bytes_copied; |
| 793 | } |
| 794 | |
| 795 | rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz); |
| 796 | if (rc != 0) { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 797 | return BOOT_EFLASH; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 798 | } |
| 799 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 800 | #ifdef MCUBOOT_ENC_IMAGES |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 801 | image_index = BOOT_CURR_IMG(state); |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 802 | if ((fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) || |
| 803 | fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) && |
| 804 | !(fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) && |
| 805 | fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index))) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 806 | /* assume the secondary slot as src, needs decryption */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 807 | hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT); |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 808 | #if !defined(MCUBOOT_SWAP_USING_MOVE) |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 809 | off = off_src; |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 810 | if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 811 | /* might need encryption (metadata from the primary slot) */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 812 | hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 813 | off = off_dst; |
| 814 | } |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 815 | #else |
| 816 | off = off_dst; |
| 817 | if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) { |
| 818 | hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT); |
| 819 | } |
| 820 | #endif |
Fabio Utzig | 2fc80df | 2018-12-14 06:47:38 -0200 | [diff] [blame] | 821 | if (IS_ENCRYPTED(hdr)) { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 822 | blk_sz = chunk_sz; |
| 823 | idx = 0; |
| 824 | if (off + bytes_copied < hdr->ih_hdr_size) { |
| 825 | /* do not decrypt header */ |
| 826 | blk_off = 0; |
| 827 | blk_sz = chunk_sz - hdr->ih_hdr_size; |
| 828 | idx = hdr->ih_hdr_size; |
| 829 | } else { |
| 830 | blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf; |
| 831 | } |
Fabio Utzig | a87cc7d | 2019-08-26 11:21:45 -0300 | [diff] [blame] | 832 | tlv_off = BOOT_TLV_OFF(hdr); |
| 833 | if (off + bytes_copied + chunk_sz > tlv_off) { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 834 | /* do not decrypt TLVs */ |
Fabio Utzig | a87cc7d | 2019-08-26 11:21:45 -0300 | [diff] [blame] | 835 | if (off + bytes_copied >= tlv_off) { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 836 | blk_sz = 0; |
| 837 | } else { |
Fabio Utzig | a87cc7d | 2019-08-26 11:21:45 -0300 | [diff] [blame] | 838 | blk_sz = tlv_off - (off + bytes_copied); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 839 | } |
| 840 | } |
Fabio Utzig | 1e4284b | 2019-08-23 11:55:27 -0300 | [diff] [blame] | 841 | boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src, |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 842 | (off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz, |
| 843 | blk_off, &buf[idx]); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 844 | } |
| 845 | } |
| 846 | #endif |
| 847 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 848 | rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz); |
| 849 | if (rc != 0) { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 850 | return BOOT_EFLASH; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | bytes_copied += chunk_sz; |
Fabio Utzig | 853657c | 2019-05-07 08:06:07 -0300 | [diff] [blame] | 854 | |
| 855 | MCUBOOT_WATCHDOG_FEED(); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 856 | } |
| 857 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 858 | return 0; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 859 | } |
| 860 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 861 | /** |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 862 | * Overwrite primary slot with the image contained in the secondary slot. |
| 863 | * If a prior copy operation was interrupted by a system reset, this function |
| 864 | * redos the copy. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 865 | * |
| 866 | * @param bs The current boot status. This function reads |
| 867 | * this struct to determine if it is resuming |
| 868 | * an interrupted swap operation. This |
| 869 | * function writes the updated status to this |
| 870 | * function on return. |
| 871 | * |
| 872 | * @return 0 on success; nonzero on failure. |
| 873 | */ |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 874 | #if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP) |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 875 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 876 | boot_copy_image(struct boot_loader_state *state, struct boot_status *bs) |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 877 | { |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 878 | size_t sect_count; |
| 879 | size_t sect; |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 880 | int rc; |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 881 | size_t size; |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 882 | size_t this_size; |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 883 | size_t last_sector; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 884 | const struct flash_area *fap_primary_slot; |
| 885 | const struct flash_area *fap_secondary_slot; |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 886 | uint8_t image_index; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 887 | |
Fabio Utzig | aaf767c | 2017-12-05 10:22:46 -0200 | [diff] [blame] | 888 | (void)bs; |
| 889 | |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 890 | #if defined(MCUBOOT_OVERWRITE_ONLY_FAST) |
| 891 | uint32_t src_size = 0; |
Fabio Utzig | d638b17 | 2019-08-09 10:38:05 -0300 | [diff] [blame] | 892 | rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &src_size); |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 893 | assert(rc == 0); |
| 894 | #endif |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 895 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 896 | BOOT_LOG_INF("Image upgrade secondary slot -> primary slot"); |
| 897 | BOOT_LOG_INF("Erasing the primary slot"); |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 898 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 899 | image_index = BOOT_CURR_IMG(state); |
| 900 | |
| 901 | rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), |
| 902 | &fap_primary_slot); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 903 | assert (rc == 0); |
| 904 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 905 | rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index), |
| 906 | &fap_secondary_slot); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 907 | assert (rc == 0); |
| 908 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 909 | sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT); |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 910 | for (sect = 0, size = 0; sect < sect_count; sect++) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 911 | this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect); |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame] | 912 | rc = boot_erase_region(fap_primary_slot, size, this_size); |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 913 | assert(rc == 0); |
| 914 | |
| 915 | size += this_size; |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 916 | |
| 917 | #if defined(MCUBOOT_OVERWRITE_ONLY_FAST) |
| 918 | if (size >= src_size) { |
| 919 | break; |
| 920 | } |
| 921 | #endif |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 922 | } |
| 923 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 924 | #ifdef MCUBOOT_ENC_IMAGES |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 925 | if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) { |
Fabio Utzig | 1e4284b | 2019-08-23 11:55:27 -0300 | [diff] [blame] | 926 | rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 927 | boot_img_hdr(state, BOOT_SECONDARY_SLOT), |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 928 | fap_secondary_slot, bs); |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 929 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 930 | if (rc < 0) { |
| 931 | return BOOT_EBADIMAGE; |
| 932 | } |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 933 | if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 934 | return BOOT_EBADIMAGE; |
| 935 | } |
| 936 | } |
| 937 | #endif |
| 938 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 939 | BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes", |
| 940 | size); |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame] | 941 | rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot, 0, 0, size); |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 942 | |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 943 | #ifdef MCUBOOT_HW_ROLLBACK_PROT |
| 944 | /* Update the stored security counter with the new image's security counter |
| 945 | * value. Both slots hold the new image at this point, but the secondary |
| 946 | * slot's image header must be passed since the image headers in the |
| 947 | * boot_data structure have not been updated yet. |
| 948 | */ |
| 949 | rc = boot_update_security_counter(BOOT_CURR_IMG(state), BOOT_PRIMARY_SLOT, |
| 950 | boot_img_hdr(state, BOOT_SECONDARY_SLOT)); |
| 951 | if (rc != 0) { |
| 952 | BOOT_LOG_ERR("Security counter update failed after image upgrade."); |
| 953 | return rc; |
| 954 | } |
| 955 | #endif /* MCUBOOT_HW_ROLLBACK_PROT */ |
| 956 | |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 957 | /* |
| 958 | * Erases header and trailer. The trailer is erased because when a new |
| 959 | * image is written without a trailer as is the case when using newt, the |
| 960 | * trailer that was left might trigger a new upgrade. |
| 961 | */ |
Christopher Collins | 2c88e69 | 2019-05-22 15:10:14 -0700 | [diff] [blame] | 962 | BOOT_LOG_DBG("erasing secondary header"); |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame] | 963 | rc = boot_erase_region(fap_secondary_slot, |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 964 | boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0), |
| 965 | boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0)); |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 966 | assert(rc == 0); |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 967 | last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1; |
Christopher Collins | 2c88e69 | 2019-05-22 15:10:14 -0700 | [diff] [blame] | 968 | BOOT_LOG_DBG("erasing secondary trailer"); |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame] | 969 | rc = boot_erase_region(fap_secondary_slot, |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 970 | boot_img_sector_off(state, BOOT_SECONDARY_SLOT, |
| 971 | last_sector), |
| 972 | boot_img_sector_size(state, BOOT_SECONDARY_SLOT, |
| 973 | last_sector)); |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 974 | assert(rc == 0); |
| 975 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 976 | flash_area_close(fap_primary_slot); |
| 977 | flash_area_close(fap_secondary_slot); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 978 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 979 | /* TODO: Perhaps verify the primary slot's signature again? */ |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 980 | |
| 981 | return 0; |
| 982 | } |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 983 | #endif |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 984 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 985 | #if !defined(MCUBOOT_OVERWRITE_ONLY) |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 986 | /** |
| 987 | * Swaps the two images in flash. If a prior copy operation was interrupted |
| 988 | * by a system reset, this function completes that operation. |
| 989 | * |
| 990 | * @param bs The current boot status. This function reads |
| 991 | * this struct to determine if it is resuming |
| 992 | * an interrupted swap operation. This |
| 993 | * function writes the updated status to this |
| 994 | * function on return. |
| 995 | * |
| 996 | * @return 0 on success; nonzero on failure. |
| 997 | */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 998 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 999 | boot_swap_image(struct boot_loader_state *state, struct boot_status *bs) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1000 | { |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1001 | struct image_header *hdr; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1002 | #ifdef MCUBOOT_ENC_IMAGES |
| 1003 | const struct flash_area *fap; |
| 1004 | uint8_t slot; |
| 1005 | uint8_t i; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1006 | #endif |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1007 | uint32_t size; |
| 1008 | uint32_t copy_size; |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1009 | uint8_t image_index; |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1010 | int rc; |
| 1011 | |
| 1012 | /* FIXME: just do this if asked by user? */ |
| 1013 | |
| 1014 | size = copy_size = 0; |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1015 | image_index = BOOT_CURR_IMG(state); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1016 | |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1017 | if (boot_status_is_reset(bs)) { |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 1018 | /* |
| 1019 | * No swap ever happened, so need to find the largest image which |
| 1020 | * will be used to determine the amount of sectors to swap. |
| 1021 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1022 | hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1023 | if (hdr->ih_magic == IMAGE_MAGIC) { |
Fabio Utzig | d638b17 | 2019-08-09 10:38:05 -0300 | [diff] [blame] | 1024 | rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, ©_size); |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 1025 | assert(rc == 0); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1026 | } |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1027 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1028 | #ifdef MCUBOOT_ENC_IMAGES |
Fabio Utzig | 2fc80df | 2018-12-14 06:47:38 -0200 | [diff] [blame] | 1029 | if (IS_ENCRYPTED(hdr)) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1030 | fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT); |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 1031 | rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1032 | assert(rc >= 0); |
| 1033 | |
| 1034 | if (rc == 0) { |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 1035 | rc = boot_enc_set_key(BOOT_CURR_ENC(state), 0, bs); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1036 | assert(rc == 0); |
| 1037 | } else { |
| 1038 | rc = 0; |
| 1039 | } |
| 1040 | } else { |
| 1041 | memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE); |
| 1042 | } |
| 1043 | #endif |
| 1044 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1045 | hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT); |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 1046 | if (hdr->ih_magic == IMAGE_MAGIC) { |
Fabio Utzig | d638b17 | 2019-08-09 10:38:05 -0300 | [diff] [blame] | 1047 | rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size); |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 1048 | assert(rc == 0); |
| 1049 | } |
| 1050 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1051 | #ifdef MCUBOOT_ENC_IMAGES |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1052 | hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT); |
Fabio Utzig | 2fc80df | 2018-12-14 06:47:38 -0200 | [diff] [blame] | 1053 | if (IS_ENCRYPTED(hdr)) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1054 | fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT); |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 1055 | rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1056 | assert(rc >= 0); |
| 1057 | |
| 1058 | if (rc == 0) { |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 1059 | rc = boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1060 | assert(rc == 0); |
| 1061 | } else { |
| 1062 | rc = 0; |
| 1063 | } |
| 1064 | } else { |
| 1065 | memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE); |
| 1066 | } |
| 1067 | #endif |
| 1068 | |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 1069 | if (size > copy_size) { |
| 1070 | copy_size = size; |
| 1071 | } |
| 1072 | |
| 1073 | bs->swap_size = copy_size; |
| 1074 | } else { |
| 1075 | /* |
| 1076 | * If a swap was under way, the swap_size should already be present |
| 1077 | * in the trailer... |
| 1078 | */ |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1079 | rc = boot_read_swap_size(image_index, &bs->swap_size); |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 1080 | assert(rc == 0); |
| 1081 | |
| 1082 | copy_size = bs->swap_size; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1083 | |
| 1084 | #ifdef MCUBOOT_ENC_IMAGES |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 1085 | for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) { |
| 1086 | rc = boot_read_enc_key(image_index, slot, bs); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1087 | assert(rc == 0); |
| 1088 | |
| 1089 | for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) { |
Fabio Utzig | 1c7d959 | 2018-12-03 10:35:56 -0200 | [diff] [blame] | 1090 | if (bs->enckey[slot][i] != 0xff) { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1091 | break; |
| 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | if (i != BOOT_ENC_KEY_SIZE) { |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 1096 | boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1097 | } |
| 1098 | } |
| 1099 | #endif |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 1100 | } |
| 1101 | |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1102 | swap_run(state, bs, copy_size); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1103 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1104 | #ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1105 | extern int boot_status_fails; |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 1106 | if (boot_status_fails > 0) { |
Christopher Collins | 2c88e69 | 2019-05-22 15:10:14 -0700 | [diff] [blame] | 1107 | BOOT_LOG_WRN("%d status write fails performing the swap", |
| 1108 | boot_status_fails); |
Fabio Utzig | a0e1cce | 2017-11-23 20:04:01 -0200 | [diff] [blame] | 1109 | } |
| 1110 | #endif |
| 1111 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1112 | return 0; |
| 1113 | } |
David Brown | 17609d8 | 2017-05-05 09:41:34 -0600 | [diff] [blame] | 1114 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1115 | |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1116 | #if (BOOT_IMAGE_NUMBER > 1) |
| 1117 | /** |
| 1118 | * Check the image dependency whether it is satisfied and modify |
| 1119 | * the swap type if necessary. |
| 1120 | * |
| 1121 | * @param dep Image dependency which has to be verified. |
| 1122 | * |
| 1123 | * @return 0 on success; nonzero on failure. |
| 1124 | */ |
| 1125 | static int |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 1126 | boot_verify_slot_dependency(struct boot_loader_state *state, |
| 1127 | struct image_dependency *dep) |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1128 | { |
| 1129 | struct image_version *dep_version; |
| 1130 | size_t dep_slot; |
| 1131 | int rc; |
David Brown | e6ab34c | 2019-09-03 12:24:21 -0600 | [diff] [blame] | 1132 | uint8_t swap_type; |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1133 | |
| 1134 | /* Determine the source of the image which is the subject of |
| 1135 | * the dependency and get it's version. */ |
David Brown | e6ab34c | 2019-09-03 12:24:21 -0600 | [diff] [blame] | 1136 | swap_type = state->swap_type[dep->image_id]; |
Barry Solomon | 0407553 | 2020-03-18 09:33:32 -0400 | [diff] [blame] | 1137 | dep_slot = BOOT_IS_UPGRADE(swap_type) ? BOOT_SECONDARY_SLOT |
| 1138 | : BOOT_PRIMARY_SLOT; |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1139 | dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver; |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1140 | |
David Vincze | 8b0b637 | 2020-05-20 19:54:44 +0200 | [diff] [blame] | 1141 | rc = boot_version_cmp(dep_version, &dep->image_min_version); |
| 1142 | if (rc < 0) { |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1143 | /* Dependency not satisfied. |
| 1144 | * Modify the swap type to decrease the version number of the image |
| 1145 | * (which will be located in the primary slot after the boot process), |
| 1146 | * consequently the number of unsatisfied dependencies will be |
| 1147 | * decreased or remain the same. |
| 1148 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1149 | switch (BOOT_SWAP_TYPE(state)) { |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1150 | case BOOT_SWAP_TYPE_TEST: |
| 1151 | case BOOT_SWAP_TYPE_PERM: |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1152 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE; |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1153 | break; |
| 1154 | case BOOT_SWAP_TYPE_NONE: |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1155 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT; |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1156 | break; |
| 1157 | default: |
| 1158 | break; |
| 1159 | } |
David Vincze | 8b0b637 | 2020-05-20 19:54:44 +0200 | [diff] [blame] | 1160 | } else { |
| 1161 | /* Dependency satisfied. */ |
| 1162 | rc = 0; |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | return rc; |
| 1166 | } |
| 1167 | |
| 1168 | /** |
| 1169 | * Read all dependency TLVs of an image from the flash and verify |
| 1170 | * one after another to see if they are all satisfied. |
| 1171 | * |
| 1172 | * @param slot Image slot number. |
| 1173 | * |
| 1174 | * @return 0 on success; nonzero on failure. |
| 1175 | */ |
| 1176 | static int |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 1177 | boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot) |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1178 | { |
| 1179 | const struct flash_area *fap; |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 1180 | struct image_tlv_iter it; |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1181 | struct image_dependency dep; |
| 1182 | uint32_t off; |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 1183 | uint16_t len; |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1184 | int area_id; |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1185 | int rc; |
| 1186 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1187 | area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot); |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1188 | rc = flash_area_open(area_id, &fap); |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1189 | if (rc != 0) { |
| 1190 | rc = BOOT_EFLASH; |
| 1191 | goto done; |
| 1192 | } |
| 1193 | |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 1194 | rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap, |
| 1195 | IMAGE_TLV_DEPENDENCY, true); |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1196 | if (rc != 0) { |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1197 | goto done; |
| 1198 | } |
| 1199 | |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 1200 | while (true) { |
| 1201 | rc = bootutil_tlv_iter_next(&it, &off, &len, NULL); |
| 1202 | if (rc < 0) { |
| 1203 | return -1; |
| 1204 | } else if (rc > 0) { |
| 1205 | rc = 0; |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1206 | break; |
| 1207 | } |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 1208 | |
| 1209 | if (len != sizeof(dep)) { |
| 1210 | rc = BOOT_EBADIMAGE; |
| 1211 | goto done; |
| 1212 | } |
| 1213 | |
| 1214 | rc = flash_area_read(fap, off, &dep, len); |
| 1215 | if (rc != 0) { |
| 1216 | rc = BOOT_EFLASH; |
| 1217 | goto done; |
| 1218 | } |
| 1219 | |
| 1220 | if (dep.image_id >= BOOT_IMAGE_NUMBER) { |
| 1221 | rc = BOOT_EBADARGS; |
| 1222 | goto done; |
| 1223 | } |
| 1224 | |
| 1225 | /* Verify dependency and modify the swap type if not satisfied. */ |
| 1226 | rc = boot_verify_slot_dependency(state, &dep); |
| 1227 | if (rc != 0) { |
| 1228 | /* Dependency not satisfied. */ |
| 1229 | goto done; |
| 1230 | } |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1231 | } |
| 1232 | |
| 1233 | done: |
| 1234 | flash_area_close(fap); |
| 1235 | return rc; |
| 1236 | } |
| 1237 | |
| 1238 | /** |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1239 | * Iterate over all the images and verify whether the image dependencies in the |
| 1240 | * TLV area are all satisfied and update the related swap type if necessary. |
| 1241 | */ |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 1242 | static int |
| 1243 | boot_verify_dependencies(struct boot_loader_state *state) |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1244 | { |
Erik Johnson | 4906375 | 2020-02-06 09:59:31 -0600 | [diff] [blame] | 1245 | int rc = -1; |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 1246 | uint8_t slot; |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1247 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1248 | BOOT_CURR_IMG(state) = 0; |
| 1249 | while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) { |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 1250 | if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE && |
| 1251 | BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) { |
| 1252 | slot = BOOT_SECONDARY_SLOT; |
| 1253 | } else { |
| 1254 | slot = BOOT_PRIMARY_SLOT; |
| 1255 | } |
| 1256 | |
| 1257 | rc = boot_verify_slot_dependencies(state, slot); |
Fabio Utzig | abec073 | 2019-07-31 08:40:22 -0300 | [diff] [blame] | 1258 | if (rc == 0) { |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1259 | /* All dependencies've been satisfied, continue with next image. */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1260 | BOOT_CURR_IMG(state)++; |
David Vincze | 8b0b637 | 2020-05-20 19:54:44 +0200 | [diff] [blame] | 1261 | } else { |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 1262 | /* Cannot upgrade due to non-met dependencies, so disable all |
| 1263 | * image upgrades. |
| 1264 | */ |
| 1265 | for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) { |
| 1266 | BOOT_CURR_IMG(state) = idx; |
| 1267 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE; |
| 1268 | } |
| 1269 | break; |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1270 | } |
| 1271 | } |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 1272 | return rc; |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1273 | } |
| 1274 | #endif /* (BOOT_IMAGE_NUMBER > 1) */ |
| 1275 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1276 | /** |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1277 | * Performs a clean (not aborted) image update. |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1278 | * |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1279 | * @param bs The current boot status. |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1280 | * |
| 1281 | * @return 0 on success; nonzero on failure. |
| 1282 | */ |
| 1283 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1284 | boot_perform_update(struct boot_loader_state *state, struct boot_status *bs) |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1285 | { |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1286 | int rc; |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1287 | #ifndef MCUBOOT_OVERWRITE_ONLY |
| 1288 | uint8_t swap_type; |
| 1289 | #endif |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1290 | |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1291 | /* At this point there are no aborted swaps. */ |
| 1292 | #if defined(MCUBOOT_OVERWRITE_ONLY) |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1293 | rc = boot_copy_image(state, bs); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1294 | #elif defined(MCUBOOT_BOOTSTRAP) |
| 1295 | /* Check if the image update was triggered by a bad image in the |
| 1296 | * primary slot (the validity of the image in the secondary slot had |
| 1297 | * already been checked). |
| 1298 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1299 | if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 || |
| 1300 | boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) { |
| 1301 | rc = boot_copy_image(state, bs); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1302 | } else { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1303 | rc = boot_swap_image(state, bs); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1304 | } |
| 1305 | #else |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1306 | rc = boot_swap_image(state, bs); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1307 | #endif |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 1308 | assert(rc == 0); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1309 | |
| 1310 | #ifndef MCUBOOT_OVERWRITE_ONLY |
| 1311 | /* The following state needs image_ok be explicitly set after the |
| 1312 | * swap was finished to avoid a new revert. |
| 1313 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1314 | swap_type = BOOT_SWAP_TYPE(state); |
| 1315 | if (swap_type == BOOT_SWAP_TYPE_REVERT || |
| 1316 | swap_type == BOOT_SWAP_TYPE_PERM) { |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1317 | rc = swap_set_image_ok(BOOT_CURR_IMG(state)); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1318 | if (rc != 0) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1319 | BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1320 | } |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1321 | } |
| 1322 | |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 1323 | #ifdef MCUBOOT_HW_ROLLBACK_PROT |
| 1324 | if (swap_type == BOOT_SWAP_TYPE_PERM) { |
| 1325 | /* Update the stored security counter with the new image's security |
| 1326 | * counter value. The primary slot holds the new image at this point, |
| 1327 | * but the secondary slot's image header must be passed since image |
| 1328 | * headers in the boot_data structure have not been updated yet. |
| 1329 | * |
| 1330 | * In case of a permanent image swap mcuboot will never attempt to |
| 1331 | * revert the images on the next reboot. Therefore, the security |
| 1332 | * counter must be increased right after the image upgrade. |
| 1333 | */ |
| 1334 | rc = boot_update_security_counter( |
| 1335 | BOOT_CURR_IMG(state), |
| 1336 | BOOT_PRIMARY_SLOT, |
| 1337 | boot_img_hdr(state, BOOT_SECONDARY_SLOT)); |
| 1338 | if (rc != 0) { |
| 1339 | BOOT_LOG_ERR("Security counter update failed after " |
| 1340 | "image upgrade."); |
| 1341 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC; |
| 1342 | } |
| 1343 | } |
| 1344 | #endif /* MCUBOOT_HW_ROLLBACK_PROT */ |
| 1345 | |
Fabio Utzig | e575b0b | 2019-09-11 12:34:23 -0300 | [diff] [blame] | 1346 | if (BOOT_IS_UPGRADE(swap_type)) { |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1347 | rc = swap_set_copy_done(BOOT_CURR_IMG(state)); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1348 | if (rc != 0) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1349 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC; |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1350 | } |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1351 | } |
| 1352 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 1353 | |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1354 | return rc; |
| 1355 | } |
| 1356 | |
| 1357 | /** |
| 1358 | * Completes a previously aborted image swap. |
| 1359 | * |
| 1360 | * @param bs The current boot status. |
| 1361 | * |
| 1362 | * @return 0 on success; nonzero on failure. |
| 1363 | */ |
| 1364 | #if !defined(MCUBOOT_OVERWRITE_ONLY) |
| 1365 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1366 | boot_complete_partial_swap(struct boot_loader_state *state, |
| 1367 | struct boot_status *bs) |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1368 | { |
| 1369 | int rc; |
| 1370 | |
| 1371 | /* Determine the type of swap operation being resumed from the |
| 1372 | * `swap-type` trailer field. |
| 1373 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1374 | rc = boot_swap_image(state, bs); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1375 | assert(rc == 0); |
| 1376 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1377 | BOOT_SWAP_TYPE(state) = bs->swap_type; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1378 | |
| 1379 | /* The following states need image_ok be explicitly set after the |
| 1380 | * swap was finished to avoid a new revert. |
| 1381 | */ |
| 1382 | if (bs->swap_type == BOOT_SWAP_TYPE_REVERT || |
| 1383 | bs->swap_type == BOOT_SWAP_TYPE_PERM) { |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1384 | rc = swap_set_image_ok(BOOT_CURR_IMG(state)); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1385 | if (rc != 0) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1386 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1387 | } |
| 1388 | } |
| 1389 | |
Fabio Utzig | e575b0b | 2019-09-11 12:34:23 -0300 | [diff] [blame] | 1390 | if (BOOT_IS_UPGRADE(bs->swap_type)) { |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1391 | rc = swap_set_copy_done(BOOT_CURR_IMG(state)); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1392 | if (rc != 0) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1393 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1394 | } |
| 1395 | } |
| 1396 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1397 | if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1398 | BOOT_LOG_ERR("panic!"); |
| 1399 | assert(0); |
| 1400 | |
| 1401 | /* Loop forever... */ |
| 1402 | while (1) {} |
| 1403 | } |
| 1404 | |
| 1405 | return rc; |
| 1406 | } |
| 1407 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
| 1408 | |
| 1409 | #if (BOOT_IMAGE_NUMBER > 1) |
| 1410 | /** |
| 1411 | * Review the validity of previously determined swap types of other images. |
| 1412 | * |
| 1413 | * @param aborted_swap The current image upgrade is a |
| 1414 | * partial/aborted swap. |
| 1415 | */ |
| 1416 | static void |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1417 | boot_review_image_swap_types(struct boot_loader_state *state, |
| 1418 | bool aborted_swap) |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1419 | { |
| 1420 | /* In that case if we rebooted in the middle of an image upgrade process, we |
| 1421 | * must review the validity of swap types, that were previously determined |
| 1422 | * for other images. The image_ok flag had not been set before the reboot |
| 1423 | * for any of the updated images (only the copy_done flag) and thus falsely |
| 1424 | * the REVERT swap type has been determined for the previous images that had |
| 1425 | * been updated before the reboot. |
| 1426 | * |
| 1427 | * There are two separate scenarios that we have to deal with: |
| 1428 | * |
| 1429 | * 1. The reboot has happened during swapping an image: |
| 1430 | * The current image upgrade has been determined as a |
| 1431 | * partial/aborted swap. |
| 1432 | * 2. The reboot has happened between two separate image upgrades: |
| 1433 | * In this scenario we must check the swap type of the current image. |
| 1434 | * In those cases if it is NONE or REVERT we cannot certainly determine |
| 1435 | * the fact of a reboot. In a consistent state images must move in the |
| 1436 | * same direction or stay in place, e.g. in practice REVERT and TEST |
| 1437 | * swap types cannot be present at the same time. If the swap type of |
| 1438 | * the current image is either TEST, PERM or FAIL we must review the |
| 1439 | * already determined swap types of other images and set each false |
| 1440 | * REVERT swap types to NONE (these images had been successfully |
| 1441 | * updated before the system rebooted between two separate image |
| 1442 | * upgrades). |
| 1443 | */ |
| 1444 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1445 | if (BOOT_CURR_IMG(state) == 0) { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1446 | /* Nothing to do */ |
| 1447 | return; |
| 1448 | } |
| 1449 | |
| 1450 | if (!aborted_swap) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1451 | if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) || |
| 1452 | (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1453 | /* Nothing to do */ |
| 1454 | return; |
| 1455 | } |
| 1456 | } |
| 1457 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1458 | for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) { |
| 1459 | if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) { |
| 1460 | state->swap_type[i] = BOOT_SWAP_TYPE_NONE; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1461 | } |
| 1462 | } |
| 1463 | } |
| 1464 | #endif |
| 1465 | |
| 1466 | /** |
| 1467 | * Prepare image to be updated if required. |
| 1468 | * |
| 1469 | * Prepare image to be updated if required with completing an image swap |
| 1470 | * operation if one was aborted and/or determining the type of the |
| 1471 | * swap operation. In case of any error set the swap type to NONE. |
| 1472 | * |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1473 | * @param state TODO |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1474 | * @param bs Pointer where the read and possibly updated |
| 1475 | * boot status can be written to. |
| 1476 | */ |
| 1477 | static void |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1478 | boot_prepare_image_for_update(struct boot_loader_state *state, |
| 1479 | struct boot_status *bs) |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1480 | { |
| 1481 | int rc; |
| 1482 | |
| 1483 | /* Determine the sector layout of the image slots and scratch area. */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1484 | rc = boot_read_sectors(state); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1485 | if (rc != 0) { |
| 1486 | BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d" |
| 1487 | " - too small?", BOOT_MAX_IMG_SECTORS); |
| 1488 | /* Unable to determine sector layout, continue with next image |
| 1489 | * if there is one. |
| 1490 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1491 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1492 | return; |
| 1493 | } |
| 1494 | |
| 1495 | /* Attempt to read an image header from each slot. */ |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1496 | rc = boot_read_image_headers(state, false, NULL); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1497 | if (rc != 0) { |
| 1498 | /* Continue with next image if there is one. */ |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1499 | BOOT_LOG_WRN("Failed reading image headers; Image=%u", |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1500 | BOOT_CURR_IMG(state)); |
| 1501 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1502 | return; |
| 1503 | } |
| 1504 | |
| 1505 | /* If the current image's slots aren't compatible, no swap is possible. |
| 1506 | * Just boot into primary slot. |
| 1507 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1508 | if (boot_slots_compatible(state)) { |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1509 | boot_status_reset(bs); |
| 1510 | |
| 1511 | #ifndef MCUBOOT_OVERWRITE_ONLY |
| 1512 | rc = swap_read_status(state, bs); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1513 | if (rc != 0) { |
| 1514 | BOOT_LOG_WRN("Failed reading boot status; Image=%u", |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1515 | BOOT_CURR_IMG(state)); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1516 | /* Continue with next image if there is one. */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1517 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1518 | return; |
| 1519 | } |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1520 | #endif |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1521 | |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 1522 | #ifdef MCUBOOT_SWAP_USING_MOVE |
| 1523 | /* |
| 1524 | * Must re-read image headers because the boot status might |
| 1525 | * have been updated in the previous function call. |
| 1526 | */ |
| 1527 | rc = boot_read_image_headers(state, !boot_status_is_reset(bs), bs); |
| 1528 | if (rc != 0) { |
| 1529 | /* Continue with next image if there is one. */ |
| 1530 | BOOT_LOG_WRN("Failed reading image headers; Image=%u", |
| 1531 | BOOT_CURR_IMG(state)); |
| 1532 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE; |
| 1533 | return; |
| 1534 | } |
| 1535 | #endif |
| 1536 | |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1537 | /* Determine if we rebooted in the middle of an image swap |
| 1538 | * operation. If a partial swap was detected, complete it. |
| 1539 | */ |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1540 | if (!boot_status_is_reset(bs)) { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1541 | |
| 1542 | #if (BOOT_IMAGE_NUMBER > 1) |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1543 | boot_review_image_swap_types(state, true); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1544 | #endif |
| 1545 | |
| 1546 | #ifdef MCUBOOT_OVERWRITE_ONLY |
| 1547 | /* Should never arrive here, overwrite-only mode has |
| 1548 | * no swap state. |
| 1549 | */ |
| 1550 | assert(0); |
| 1551 | #else |
| 1552 | /* Determine the type of swap operation being resumed from the |
| 1553 | * `swap-type` trailer field. |
| 1554 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1555 | rc = boot_complete_partial_swap(state, bs); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1556 | assert(rc == 0); |
| 1557 | #endif |
| 1558 | /* Attempt to read an image header from each slot. Ensure that |
| 1559 | * image headers in slots are aligned with headers in boot_data. |
| 1560 | */ |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1561 | rc = boot_read_image_headers(state, false, bs); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1562 | assert(rc == 0); |
| 1563 | |
| 1564 | /* Swap has finished set to NONE */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1565 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1566 | } else { |
| 1567 | /* There was no partial swap, determine swap type. */ |
| 1568 | if (bs->swap_type == BOOT_SWAP_TYPE_NONE) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1569 | BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs); |
| 1570 | } else if (boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) != 0) { |
| 1571 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1572 | } else { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1573 | BOOT_SWAP_TYPE(state) = bs->swap_type; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | #if (BOOT_IMAGE_NUMBER > 1) |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1577 | boot_review_image_swap_types(state, false); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1578 | #endif |
| 1579 | |
| 1580 | #ifdef MCUBOOT_BOOTSTRAP |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1581 | if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1582 | /* Header checks are done first because they are |
| 1583 | * inexpensive. Since overwrite-only copies starting from |
| 1584 | * offset 0, if interrupted, it might leave a valid header |
| 1585 | * magic, so also run validation on the primary slot to be |
| 1586 | * sure it's not OK. |
| 1587 | */ |
Fabio Utzig | 59b63e5 | 2019-09-10 12:22:35 -0300 | [diff] [blame] | 1588 | if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 || |
| 1589 | boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) { |
| 1590 | if (boot_img_hdr(state, |
| 1591 | BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC && |
| 1592 | boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) == 0) { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1593 | /* Set swap type to REVERT to overwrite the primary |
| 1594 | * slot with the image contained in secondary slot |
| 1595 | * and to trigger the explicit setting of the |
| 1596 | * image_ok flag. |
| 1597 | */ |
Fabio Utzig | 59b63e5 | 2019-09-10 12:22:35 -0300 | [diff] [blame] | 1598 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1599 | } |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 1600 | } |
| 1601 | } |
Fabio Utzig | 338a19f | 2018-12-03 08:37:08 -0200 | [diff] [blame] | 1602 | #endif |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1603 | } |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1604 | } else { |
| 1605 | /* In that case if slots are not compatible. */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1606 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE; |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1607 | } |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1608 | } |
| 1609 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1610 | int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1611 | context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1612 | { |
Marti Bolivar | 8489865 | 2017-06-13 17:20:22 -0400 | [diff] [blame] | 1613 | size_t slot; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1614 | struct boot_status bs; |
David Vincze | 9015a5d | 2020-05-18 14:43:11 +0200 | [diff] [blame] | 1615 | int rc = -1; |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1616 | int fa_id; |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1617 | int image_index; |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 1618 | bool has_upgrade; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1619 | |
| 1620 | /* The array of slot sectors are defined here (as opposed to file scope) so |
| 1621 | * that they don't get allocated for non-boot-loader apps. This is |
| 1622 | * necessary because the gcc option "-fdata-sections" doesn't seem to have |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1623 | * any effect in older gcc versions (e.g., 4.8.4). |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1624 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1625 | TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS]; |
| 1626 | TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS]; |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1627 | #if MCUBOOT_SWAP_USING_SCRATCH |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1628 | TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS]; |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1629 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1630 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1631 | memset(state, 0, sizeof(struct boot_loader_state)); |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 1632 | has_upgrade = false; |
| 1633 | |
| 1634 | #if (BOOT_IMAGE_NUMBER == 1) |
| 1635 | (void)has_upgrade; |
| 1636 | #endif |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1637 | |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1638 | /* Iterate over all the images. By the end of the loop the swap type has |
| 1639 | * to be determined for each image and all aborted swaps have to be |
| 1640 | * completed. |
Christopher Collins | 0ff3c6c | 2016-12-21 12:04:17 -0800 | [diff] [blame] | 1641 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1642 | IMAGES_ITER(BOOT_CURR_IMG(state)) { |
Fabio Utzig | db5bd3c | 2017-07-13 22:20:22 -0300 | [diff] [blame] | 1643 | |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1644 | #if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1) |
| 1645 | /* The keys used for encryption may no longer be valid (could belong to |
| 1646 | * another images). Therefore, mark them as invalid to force their reload |
| 1647 | * by boot_enc_load(). |
Fabio Utzig | db5bd3c | 2017-07-13 22:20:22 -0300 | [diff] [blame] | 1648 | */ |
Fabio Utzig | 1e4284b | 2019-08-23 11:55:27 -0300 | [diff] [blame] | 1649 | boot_enc_zeroize(BOOT_CURR_ENC(state)); |
David Brown | 554c52e | 2017-06-30 16:01:07 -0600 | [diff] [blame] | 1650 | #endif |
| 1651 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1652 | image_index = BOOT_CURR_IMG(state); |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1653 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1654 | BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors = |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1655 | primary_slot_sectors[image_index]; |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1656 | BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors = |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1657 | secondary_slot_sectors[image_index]; |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1658 | #if MCUBOOT_SWAP_USING_SCRATCH |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1659 | state->scratch.sectors = scratch_sectors; |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1660 | #endif |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1661 | |
| 1662 | /* Open primary and secondary image areas for the duration |
| 1663 | * of this call. |
| 1664 | */ |
| 1665 | for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) { |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1666 | fa_id = flash_area_id_from_multi_image_slot(image_index, slot); |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1667 | rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot)); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1668 | assert(rc == 0); |
| 1669 | } |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1670 | #if MCUBOOT_SWAP_USING_SCRATCH |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1671 | rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1672 | &BOOT_SCRATCH_AREA(state)); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1673 | assert(rc == 0); |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1674 | #endif |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1675 | |
| 1676 | /* Determine swap type and complete swap if it has been aborted. */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1677 | boot_prepare_image_for_update(state, &bs); |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 1678 | |
Fabio Utzig | e575b0b | 2019-09-11 12:34:23 -0300 | [diff] [blame] | 1679 | if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) { |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 1680 | has_upgrade = true; |
| 1681 | } |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1682 | } |
| 1683 | |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1684 | #if (BOOT_IMAGE_NUMBER > 1) |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 1685 | if (has_upgrade) { |
| 1686 | /* Iterate over all the images and verify whether the image dependencies |
| 1687 | * are all satisfied and update swap type if necessary. |
| 1688 | */ |
| 1689 | rc = boot_verify_dependencies(state); |
David Vincze | 8b0b637 | 2020-05-20 19:54:44 +0200 | [diff] [blame] | 1690 | if (rc != 0) { |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 1691 | /* |
| 1692 | * It was impossible to upgrade because the expected dependency version |
| 1693 | * was not available. Here we already changed the swap_type so that |
| 1694 | * instead of asserting the bootloader, we continue and no upgrade is |
| 1695 | * performed. |
| 1696 | */ |
| 1697 | rc = 0; |
| 1698 | } |
| 1699 | } |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 1700 | #endif |
| 1701 | |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1702 | /* Iterate over all the images. At this point there are no aborted swaps |
| 1703 | * and the swap types are determined for each image. By the end of the loop |
| 1704 | * all required update operations will have been finished. |
| 1705 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1706 | IMAGES_ITER(BOOT_CURR_IMG(state)) { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1707 | |
| 1708 | #if (BOOT_IMAGE_NUMBER > 1) |
| 1709 | #ifdef MCUBOOT_ENC_IMAGES |
| 1710 | /* The keys used for encryption may no longer be valid (could belong to |
| 1711 | * another images). Therefore, mark them as invalid to force their reload |
| 1712 | * by boot_enc_load(). |
| 1713 | */ |
Fabio Utzig | 1e4284b | 2019-08-23 11:55:27 -0300 | [diff] [blame] | 1714 | boot_enc_zeroize(BOOT_CURR_ENC(state)); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1715 | #endif /* MCUBOOT_ENC_IMAGES */ |
| 1716 | |
| 1717 | /* Indicate that swap is not aborted */ |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1718 | boot_status_reset(&bs); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1719 | #endif /* (BOOT_IMAGE_NUMBER > 1) */ |
| 1720 | |
| 1721 | /* Set the previously determined swap type */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1722 | bs.swap_type = BOOT_SWAP_TYPE(state); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1723 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1724 | switch (BOOT_SWAP_TYPE(state)) { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1725 | case BOOT_SWAP_TYPE_NONE: |
| 1726 | break; |
| 1727 | |
| 1728 | case BOOT_SWAP_TYPE_TEST: /* fallthrough */ |
| 1729 | case BOOT_SWAP_TYPE_PERM: /* fallthrough */ |
| 1730 | case BOOT_SWAP_TYPE_REVERT: |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1731 | rc = boot_perform_update(state, &bs); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1732 | assert(rc == 0); |
| 1733 | break; |
| 1734 | |
| 1735 | case BOOT_SWAP_TYPE_FAIL: |
| 1736 | /* The image in secondary slot was invalid and is now erased. Ensure |
| 1737 | * we don't try to boot into it again on the next reboot. Do this by |
| 1738 | * pretending we just reverted back to primary slot. |
| 1739 | */ |
| 1740 | #ifndef MCUBOOT_OVERWRITE_ONLY |
| 1741 | /* image_ok needs to be explicitly set to avoid a new revert. */ |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1742 | rc = swap_set_image_ok(BOOT_CURR_IMG(state)); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1743 | if (rc != 0) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1744 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1745 | } |
| 1746 | #endif /* !MCUBOOT_OVERWRITE_ONLY */ |
| 1747 | break; |
| 1748 | |
| 1749 | default: |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1750 | BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1751 | } |
| 1752 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1753 | if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1754 | BOOT_LOG_ERR("panic!"); |
| 1755 | assert(0); |
| 1756 | |
| 1757 | /* Loop forever... */ |
| 1758 | while (1) {} |
| 1759 | } |
| 1760 | } |
| 1761 | |
| 1762 | /* Iterate over all the images. At this point all required update operations |
| 1763 | * have finished. By the end of the loop each image in the primary slot will |
| 1764 | * have been re-validated. |
| 1765 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1766 | IMAGES_ITER(BOOT_CURR_IMG(state)) { |
| 1767 | if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1768 | /* Attempt to read an image header from each slot. Ensure that image |
| 1769 | * headers in slots are aligned with headers in boot_data. |
| 1770 | */ |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1771 | rc = boot_read_image_headers(state, false, &bs); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1772 | if (rc != 0) { |
| 1773 | goto out; |
| 1774 | } |
| 1775 | /* Since headers were reloaded, it can be assumed we just performed |
| 1776 | * a swap or overwrite. Now the header info that should be used to |
| 1777 | * provide the data for the bootstrap, which previously was at |
| 1778 | * secondary slot, was updated to primary slot. |
| 1779 | */ |
| 1780 | } |
| 1781 | |
| 1782 | #ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1783 | rc = boot_validate_slot(state, BOOT_PRIMARY_SLOT, NULL); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1784 | if (rc != 0) { |
| 1785 | rc = BOOT_EBADIMAGE; |
| 1786 | goto out; |
| 1787 | } |
| 1788 | #else |
| 1789 | /* Even if we're not re-validating the primary slot, we could be booting |
| 1790 | * onto an empty flash chip. At least do a basic sanity check that |
| 1791 | * the magic number on the image is OK. |
| 1792 | */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1793 | if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1794 | BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long) |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1795 | &boot_img_hdr(state,BOOT_PRIMARY_SLOT)->ih_magic, |
| 1796 | BOOT_CURR_IMG(state)); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1797 | rc = BOOT_EBADIMAGE; |
| 1798 | goto out; |
| 1799 | } |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 1800 | #endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */ |
| 1801 | |
| 1802 | #ifdef MCUBOOT_HW_ROLLBACK_PROT |
| 1803 | /* Update the stored security counter with the active image's security |
| 1804 | * counter value. It will only be updated if the new security counter is |
| 1805 | * greater than the stored value. |
| 1806 | * |
| 1807 | * In case of a successful image swapping when the swap type is TEST the |
| 1808 | * security counter can be increased only after a reset, when the swap |
| 1809 | * type is NONE and the image has marked itself "OK" (the image_ok flag |
| 1810 | * has been set). This way a "revert" can be performed when it's |
| 1811 | * necessary. |
| 1812 | */ |
| 1813 | if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) { |
| 1814 | rc = boot_update_security_counter( |
| 1815 | BOOT_CURR_IMG(state), |
| 1816 | BOOT_PRIMARY_SLOT, |
| 1817 | boot_img_hdr(state, BOOT_PRIMARY_SLOT)); |
| 1818 | if (rc != 0) { |
| 1819 | BOOT_LOG_ERR("Security counter update failed after image " |
| 1820 | "validation."); |
| 1821 | goto out; |
| 1822 | } |
| 1823 | } |
| 1824 | #endif /* MCUBOOT_HW_ROLLBACK_PROT */ |
David Vincze | 1cf11b5 | 2020-03-24 07:51:09 +0100 | [diff] [blame] | 1825 | |
| 1826 | #ifdef MCUBOOT_MEASURED_BOOT |
| 1827 | rc = boot_save_boot_status(BOOT_CURR_IMG(state), |
| 1828 | boot_img_hdr(state, BOOT_PRIMARY_SLOT), |
| 1829 | BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)); |
| 1830 | if (rc != 0) { |
| 1831 | BOOT_LOG_ERR("Failed to add Image %u data to shared memory area", |
| 1832 | BOOT_CURR_IMG(state)); |
| 1833 | } |
| 1834 | #endif /* MCUBOOT_MEASURED_BOOT */ |
| 1835 | |
| 1836 | #ifdef MCUBOOT_DATA_SHARING |
| 1837 | rc = boot_save_shared_data(boot_img_hdr(state, BOOT_PRIMARY_SLOT), |
| 1838 | BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)); |
| 1839 | if (rc != 0) { |
| 1840 | BOOT_LOG_ERR("Failed to add data to shared memory area."); |
| 1841 | } |
| 1842 | #endif /* MCUBOOT_DATA_SHARING */ |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1843 | } |
| 1844 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1845 | #if (BOOT_IMAGE_NUMBER > 1) |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1846 | /* Always boot from the primary slot of Image 0. */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1847 | BOOT_CURR_IMG(state) = 0; |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 1848 | #endif |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 1849 | |
Fabio Utzig | f616c54 | 2019-12-19 15:23:32 -0300 | [diff] [blame] | 1850 | /* |
| 1851 | * Since the boot_status struct stores plaintext encryption keys, reset |
| 1852 | * them here to avoid the possibility of jumping into an image that could |
| 1853 | * easily recover them. |
| 1854 | */ |
| 1855 | memset(&bs, 0, sizeof(struct boot_status)); |
| 1856 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1857 | rsp->br_flash_dev_id = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)->fa_device_id; |
| 1858 | rsp->br_image_off = boot_img_slot_off(state, BOOT_PRIMARY_SLOT); |
| 1859 | rsp->br_hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1860 | |
Fabio Utzig | 298913b | 2019-08-28 11:22:45 -0300 | [diff] [blame] | 1861 | out: |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1862 | IMAGES_ITER(BOOT_CURR_IMG(state)) { |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1863 | #if MCUBOOT_SWAP_USING_SCRATCH |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1864 | flash_area_close(BOOT_SCRATCH_AREA(state)); |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1865 | #endif |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1866 | for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1867 | flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot)); |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1868 | } |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1869 | } |
| 1870 | return rc; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1871 | } |
| 1872 | |
| 1873 | int |
| 1874 | split_go(int loader_slot, int split_slot, void **entry) |
| 1875 | { |
Marti Bolivar | c50926f | 2017-06-14 09:35:40 -0400 | [diff] [blame] | 1876 | boot_sector_t *sectors; |
Christopher Collins | 034a620 | 2017-01-11 12:19:37 -0800 | [diff] [blame] | 1877 | uintptr_t entry_val; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1878 | int loader_flash_id; |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1879 | int split_flash_id; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1880 | int rc; |
| 1881 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1882 | sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors); |
| 1883 | if (sectors == NULL) { |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1884 | return SPLIT_GO_ERR; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1885 | } |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 1886 | BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0; |
| 1887 | BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS; |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1888 | |
| 1889 | loader_flash_id = flash_area_id_from_image_slot(loader_slot); |
| 1890 | rc = flash_area_open(loader_flash_id, |
Alvaro Prieto | 63a2bdb | 2019-07-04 12:18:49 -0700 | [diff] [blame] | 1891 | &BOOT_IMG_AREA(&boot_data, loader_slot)); |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1892 | assert(rc == 0); |
| 1893 | split_flash_id = flash_area_id_from_image_slot(split_slot); |
| 1894 | rc = flash_area_open(split_flash_id, |
| 1895 | &BOOT_IMG_AREA(&boot_data, split_slot)); |
| 1896 | assert(rc == 0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1897 | |
| 1898 | /* Determine the sector layout of the image slots and scratch area. */ |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 1899 | rc = boot_read_sectors(&boot_data); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1900 | if (rc != 0) { |
| 1901 | rc = SPLIT_GO_ERR; |
| 1902 | goto done; |
| 1903 | } |
| 1904 | |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1905 | rc = boot_read_image_headers(&boot_data, true, NULL); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1906 | if (rc != 0) { |
| 1907 | goto done; |
| 1908 | } |
| 1909 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1910 | /* Don't check the bootable image flag because we could really call a |
| 1911 | * bootable or non-bootable image. Just validate that the image check |
| 1912 | * passes which is distinct from the normal check. |
| 1913 | */ |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 1914 | rc = split_image_check(boot_img_hdr(&boot_data, split_slot), |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1915 | BOOT_IMG_AREA(&boot_data, split_slot), |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 1916 | boot_img_hdr(&boot_data, loader_slot), |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1917 | BOOT_IMG_AREA(&boot_data, loader_slot)); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1918 | if (rc != 0) { |
| 1919 | rc = SPLIT_GO_NON_MATCHING; |
| 1920 | goto done; |
| 1921 | } |
| 1922 | |
Marti Bolivar | ea08887 | 2017-06-12 17:10:49 -0400 | [diff] [blame] | 1923 | entry_val = boot_img_slot_off(&boot_data, split_slot) + |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 1924 | boot_img_hdr(&boot_data, split_slot)->ih_hdr_size; |
Christopher Collins | 034a620 | 2017-01-11 12:19:37 -0800 | [diff] [blame] | 1925 | *entry = (void *) entry_val; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1926 | rc = SPLIT_GO_OK; |
| 1927 | |
| 1928 | done: |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 1929 | flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot)); |
| 1930 | flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot)); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1931 | free(sectors); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1932 | return rc; |
| 1933 | } |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame^] | 1934 | |
| 1935 | #else /* MCUBOOT_DIRECT_XIP */ |
| 1936 | |
| 1937 | /** |
| 1938 | * Iterates over all slots and determines which contain a firmware image. |
| 1939 | * |
| 1940 | * @param state Boot loader status information. |
| 1941 | * @param slot_usage Pointer to an array, which aim is to carry information |
| 1942 | * about slots that contain an image. After return the |
| 1943 | * corresponding array elements are set to a non-zero |
| 1944 | * value if the given slots are in use (contain a firmware |
| 1945 | * image), otherwise they are set to zero. |
| 1946 | * @param slot_cnt The number of slots, which can contain firmware images. |
| 1947 | * (Equal to or smaller than the size of the |
| 1948 | * slot_usage array.) |
| 1949 | * |
| 1950 | * @return The number of found images. |
| 1951 | */ |
| 1952 | static uint32_t |
| 1953 | boot_get_slot_usage(struct boot_loader_state *state, uint8_t slot_usage[], |
| 1954 | uint32_t slot_cnt) |
| 1955 | { |
| 1956 | struct image_header *hdr = NULL; |
| 1957 | uint32_t image_cnt = 0; |
| 1958 | uint32_t slot; |
| 1959 | |
| 1960 | memset(slot_usage, 0, slot_cnt); |
| 1961 | |
| 1962 | for (slot = 0; slot < slot_cnt; slot++) { |
| 1963 | hdr = boot_img_hdr(state, slot); |
| 1964 | |
| 1965 | if (boot_is_header_valid(hdr, BOOT_IMG_AREA(state, slot))) { |
| 1966 | slot_usage[slot] = 1; |
| 1967 | image_cnt++; |
| 1968 | BOOT_LOG_IMAGE_INFO(slot, hdr); |
| 1969 | } else { |
| 1970 | BOOT_LOG_INF("%s slot: Image not found", (slot == BOOT_PRIMARY_SLOT) |
| 1971 | ? "Primary" : "Secondary"); |
| 1972 | } |
| 1973 | } |
| 1974 | |
| 1975 | return image_cnt; |
| 1976 | } |
| 1977 | |
| 1978 | int |
| 1979 | context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp) |
| 1980 | { |
| 1981 | struct image_header *hdr = NULL; |
| 1982 | struct image_header *selected_image_header = NULL; |
| 1983 | uint8_t slot_usage[BOOT_NUM_SLOTS]; |
| 1984 | uint32_t selected_slot; |
| 1985 | uint32_t slot; |
| 1986 | uint32_t img_cnt; |
| 1987 | uint32_t i; |
| 1988 | int fa_id; |
| 1989 | int rc; |
| 1990 | |
| 1991 | memset(state, 0, sizeof(struct boot_loader_state)); |
| 1992 | |
| 1993 | /* Open primary and secondary image areas for the duration |
| 1994 | * of this call. |
| 1995 | */ |
| 1996 | for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) { |
| 1997 | fa_id = flash_area_id_from_image_slot(slot); |
| 1998 | rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot)); |
| 1999 | assert(rc == 0); |
| 2000 | } |
| 2001 | |
| 2002 | /* Attempt to read an image header from each slot. */ |
| 2003 | rc = boot_read_image_headers(state, false, NULL); |
| 2004 | if (rc != 0) { |
| 2005 | BOOT_LOG_WRN("Failed reading image headers."); |
| 2006 | goto out; |
| 2007 | } |
| 2008 | |
| 2009 | img_cnt = boot_get_slot_usage(state, slot_usage, |
| 2010 | sizeof(slot_usage)/sizeof(slot_usage[0])); |
| 2011 | |
| 2012 | if (img_cnt) { |
| 2013 | /* Select the newest and valid image. */ |
| 2014 | for (i = 0; i < img_cnt; i++) { |
| 2015 | selected_slot = 0; |
| 2016 | selected_image_header = NULL; |
| 2017 | |
| 2018 | /* Iterate over all the slots that are in use (contain an image) |
| 2019 | * and select the one that holds the newest image. |
| 2020 | */ |
| 2021 | for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) { |
| 2022 | if (slot_usage[slot]) { |
| 2023 | hdr = boot_img_hdr(state, slot); |
| 2024 | if (selected_image_header != NULL) { |
| 2025 | rc = boot_version_cmp(&hdr->ih_ver, |
| 2026 | &selected_image_header->ih_ver); |
| 2027 | if (rc < 1) { |
| 2028 | /* The version of the image being examined wasn't |
| 2029 | * greater than the currently selected image's |
| 2030 | * version. |
| 2031 | */ |
| 2032 | continue; |
| 2033 | } |
| 2034 | } |
| 2035 | selected_slot = slot; |
| 2036 | selected_image_header = hdr; |
| 2037 | } |
| 2038 | } |
| 2039 | |
| 2040 | rc = boot_validate_slot(state, selected_slot, NULL); |
| 2041 | if (rc == 0) { |
| 2042 | /* If a valid image is found then there is no reason to check |
| 2043 | * the rest of the images, as each of them has a smaller version |
| 2044 | * number. |
| 2045 | */ |
| 2046 | break; |
| 2047 | } |
| 2048 | /* The selected image is invalid, mark its slot as "unused" |
| 2049 | * and start over. |
| 2050 | */ |
| 2051 | slot_usage[selected_slot] = 0; |
| 2052 | } |
| 2053 | |
| 2054 | if (rc || (selected_image_header == NULL)) { |
| 2055 | /* If there was no valid image at all */ |
| 2056 | rc = BOOT_EBADIMAGE; |
| 2057 | goto out; |
| 2058 | } |
| 2059 | |
| 2060 | #ifdef MCUBOOT_HW_ROLLBACK_PROT |
| 2061 | /* Update the stored security counter with the newer (active) image's |
| 2062 | * security counter value. |
| 2063 | */ |
| 2064 | rc = boot_update_security_counter(0, selected_slot, |
| 2065 | selected_image_header); |
| 2066 | if (rc != 0) { |
| 2067 | BOOT_LOG_ERR("Security counter update failed after image " |
| 2068 | "validation."); |
| 2069 | goto out; |
| 2070 | } |
| 2071 | #endif /* MCUBOOT_HW_ROLLBACK_PROT */ |
| 2072 | |
| 2073 | #ifdef MCUBOOT_MEASURED_BOOT |
| 2074 | rc = boot_save_boot_status(0, selected_image_header, |
| 2075 | BOOT_IMG_AREA(state, selected_slot)); |
| 2076 | if (rc != 0) { |
| 2077 | BOOT_LOG_ERR("Failed to add image data to shared area"); |
| 2078 | } |
| 2079 | #endif /* MCUBOOT_MEASURED_BOOT */ |
| 2080 | |
| 2081 | #ifdef MCUBOOT_DATA_SHARING |
| 2082 | rc = boot_save_shared_data(selected_image_header, |
| 2083 | BOOT_IMG_AREA(state, selected_slot)); |
| 2084 | if (rc != 0) { |
| 2085 | BOOT_LOG_ERR("Failed to add data to shared memory area."); |
| 2086 | } |
| 2087 | #endif /* MCUBOOT_DATA_SHARING */ |
| 2088 | |
| 2089 | BOOT_LOG_INF("Booting image from the %s slot", |
| 2090 | (selected_slot == BOOT_PRIMARY_SLOT) ? |
| 2091 | "primary" : "secondary"); |
| 2092 | |
| 2093 | rsp->br_flash_dev_id = |
| 2094 | BOOT_IMG_AREA(state, selected_slot)->fa_device_id; |
| 2095 | rsp->br_image_off = boot_img_slot_off(state, selected_slot); |
| 2096 | rsp->br_hdr = selected_image_header; |
| 2097 | } else { |
| 2098 | /* No candidate image available */ |
| 2099 | rc = BOOT_EBADIMAGE; |
| 2100 | goto out; |
| 2101 | } |
| 2102 | |
| 2103 | out: |
| 2104 | for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) { |
| 2105 | flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot)); |
| 2106 | } |
| 2107 | return rc; |
| 2108 | } |
| 2109 | #endif /* MCUBOOT_DIRECT_XIP */ |
| 2110 | |
| 2111 | /** |
| 2112 | * Prepares the booting process. This function moves images around in flash as |
| 2113 | * appropriate, and tells you what address to boot from. |
| 2114 | * |
| 2115 | * @param rsp On success, indicates how booting should occur. |
| 2116 | * |
| 2117 | * @return 0 on success; nonzero on failure. |
| 2118 | */ |
| 2119 | int |
| 2120 | boot_go(struct boot_rsp *rsp) |
| 2121 | { |
| 2122 | return context_boot_go(&boot_data, rsp); |
| 2123 | } |