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