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