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