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