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