Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1 | /* |
David Brown | aac7111 | 2020-02-03 16:13:42 -0700 | [diff] [blame] | 2 | * SPDX-License-Identifier: Apache-2.0 |
| 3 | * |
| 4 | * Copyright (c) 2017-2020 Linaro LTD |
| 5 | * Copyright (c) 2017-2019 JUUL Labs |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 6 | * Copyright (c) 2019-2020 Arm Limited |
David Brown | aac7111 | 2020-02-03 16:13:42 -0700 | [diff] [blame] | 7 | * |
| 8 | * Original license: |
| 9 | * |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 10 | * Licensed to the Apache Software Foundation (ASF) under one |
| 11 | * or more contributor license agreements. See the NOTICE file |
| 12 | * distributed with this work for additional information |
| 13 | * regarding copyright ownership. The ASF licenses this file |
| 14 | * to you under the Apache License, Version 2.0 (the |
| 15 | * "License"); you may not use this file except in compliance |
| 16 | * with the License. You may obtain a copy of the License at |
| 17 | * |
| 18 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 19 | * |
| 20 | * Unless required by applicable law or agreed to in writing, |
| 21 | * software distributed under the License is distributed on an |
| 22 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 23 | * KIND, either express or implied. See the License for the |
| 24 | * specific language governing permissions and limitations |
| 25 | * under the License. |
| 26 | */ |
| 27 | |
| 28 | #ifndef H_BOOTUTIL_PRIV_ |
| 29 | #define H_BOOTUTIL_PRIV_ |
| 30 | |
Marti Bolivar | cca28a9 | 2017-06-12 16:52:22 -0400 | [diff] [blame] | 31 | #include "sysflash/sysflash.h" |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 32 | |
| 33 | #include <flash_map_backend/flash_map_backend.h> |
| 34 | |
Christopher Collins | 01dfbb6 | 2019-03-21 07:28:37 -0700 | [diff] [blame] | 35 | #include "bootutil/bootutil.h" |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 36 | #include "bootutil/image.h" |
Marti Bolivar | f91bca5 | 2018-04-12 12:40:46 -0400 | [diff] [blame] | 37 | #include "mcuboot_config/mcuboot_config.h" |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 38 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 39 | #ifdef MCUBOOT_ENC_IMAGES |
| 40 | #include "bootutil/enc_key.h" |
| 41 | #endif |
| 42 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 43 | #ifdef __cplusplus |
| 44 | extern "C" { |
| 45 | #endif |
| 46 | |
Marti Bolivar | 248da08 | 2018-04-24 15:11:39 -0400 | [diff] [blame] | 47 | #ifdef MCUBOOT_HAVE_ASSERT_H |
| 48 | #include "mcuboot_config/mcuboot_assert.h" |
Fabio Utzig | 57c40f7 | 2017-12-12 21:48:30 -0200 | [diff] [blame] | 49 | #else |
| 50 | #define ASSERT assert |
| 51 | #endif |
| 52 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 53 | struct flash_area; |
| 54 | |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 55 | #define BOOT_EFLASH 1 |
| 56 | #define BOOT_EFILE 2 |
| 57 | #define BOOT_EBADIMAGE 3 |
| 58 | #define BOOT_EBADVECT 4 |
| 59 | #define BOOT_EBADSTATUS 5 |
| 60 | #define BOOT_ENOMEM 6 |
| 61 | #define BOOT_EBADARGS 7 |
| 62 | #define BOOT_EBADVERSION 8 |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 63 | |
| 64 | #define BOOT_TMPBUF_SZ 256 |
| 65 | |
Fabio Utzig | 1e4284b | 2019-08-23 11:55:27 -0300 | [diff] [blame] | 66 | /** Number of image slots in flash; currently limited to two. */ |
| 67 | #define BOOT_NUM_SLOTS 2 |
| 68 | |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 69 | #if (defined(MCUBOOT_OVERWRITE_ONLY) + \ |
| 70 | defined(MCUBOOT_SWAP_USING_MOVE) + \ |
| 71 | defined(MCUBOOT_DIRECT_XIP)) > 1 |
| 72 | #error "Please enable only one of MCUBOOT_OVERWRITE_ONLY, MCUBOOT_SWAP_USING_MOVE or MCUBOOT_DIRECT_XIP" |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 73 | #endif |
| 74 | |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 75 | #if !defined(MCUBOOT_OVERWRITE_ONLY) && \ |
| 76 | !defined(MCUBOOT_SWAP_USING_MOVE) && \ |
| 77 | !defined(MCUBOOT_DIRECT_XIP) |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 78 | #define MCUBOOT_SWAP_USING_SCRATCH 1 |
| 79 | #endif |
| 80 | |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 81 | #define BOOT_STATUS_OP_MOVE 1 |
| 82 | #define BOOT_STATUS_OP_SWAP 2 |
| 83 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 84 | /* |
| 85 | * Maintain state of copy progress. |
| 86 | */ |
| 87 | struct boot_status { |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 88 | uint32_t idx; /* Which area we're operating on */ |
| 89 | uint8_t state; /* Which part of the swapping process are we at */ |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 90 | uint8_t op; /* What operation are we performing? */ |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 91 | uint8_t use_scratch; /* Are status bytes ever written to scratch? */ |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 92 | uint8_t swap_type; /* The type of swap in effect */ |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 93 | uint32_t swap_size; /* Total size of swapped image */ |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 94 | #ifdef MCUBOOT_ENC_IMAGES |
Fabio Utzig | 1e4284b | 2019-08-23 11:55:27 -0300 | [diff] [blame] | 95 | uint8_t enckey[BOOT_NUM_SLOTS][BOOT_ENC_KEY_SIZE]; |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 96 | #if MCUBOOT_SWAP_SAVE_ENCTLV |
| 97 | uint8_t enctlv[BOOT_NUM_SLOTS][BOOT_ENC_TLV_ALIGN_SIZE]; |
| 98 | #endif |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 99 | #endif |
Fabio Utzig | b86e688 | 2019-12-10 09:51:17 -0300 | [diff] [blame] | 100 | int source; /* Which slot contains swap status metadata */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 101 | }; |
| 102 | |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 103 | #define BOOT_MAGIC_GOOD 1 |
| 104 | #define BOOT_MAGIC_BAD 2 |
| 105 | #define BOOT_MAGIC_UNSET 3 |
| 106 | #define BOOT_MAGIC_ANY 4 /* NOTE: control only, not dependent on sector */ |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 107 | #define BOOT_MAGIC_NOTGOOD 5 /* NOTE: control only, not dependent on sector */ |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 108 | |
| 109 | /* |
| 110 | * NOTE: leave BOOT_FLAG_SET equal to one, this is written to flash! |
| 111 | */ |
| 112 | #define BOOT_FLAG_SET 1 |
| 113 | #define BOOT_FLAG_BAD 2 |
| 114 | #define BOOT_FLAG_UNSET 3 |
| 115 | #define BOOT_FLAG_ANY 4 /* NOTE: control only, not dependent on sector */ |
| 116 | |
| 117 | #define BOOT_STATUS_IDX_0 1 |
| 118 | |
| 119 | #define BOOT_STATUS_STATE_0 1 |
| 120 | #define BOOT_STATUS_STATE_1 2 |
| 121 | #define BOOT_STATUS_STATE_2 3 |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 122 | |
| 123 | /** |
| 124 | * End-of-image slot structure. |
| 125 | * |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 126 | * 0 1 2 3 |
| 127 | * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 128 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 129 | * ~ ~ |
| 130 | * ~ Swap status (BOOT_MAX_IMG_SECTORS * min-write-size * 3) ~ |
| 131 | * ~ ~ |
| 132 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 133 | * | Encryption key 0 (16 octets) [*] | |
| 134 | * | | |
| 135 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 136 | * | Encryption key 1 (16 octets) [*] | |
| 137 | * | | |
| 138 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 139 | * | Swap size (4 octets) | |
| 140 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 141 | * | Swap info | 0xff padding (7 octets) | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 142 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 143 | * | Copy done | 0xff padding (7 octets) | |
| 144 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 145 | * | Image OK | 0xff padding (7 octets) | |
| 146 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 147 | * | MAGIC (16 octets) | |
| 148 | * | | |
| 149 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 150 | * |
| 151 | * [*]: Only present if the encryption option is enabled |
| 152 | * (`MCUBOOT_ENC_IMAGES`). |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 153 | */ |
| 154 | |
| 155 | extern const uint32_t boot_img_magic[4]; |
| 156 | |
| 157 | struct boot_swap_state { |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 158 | uint8_t magic; /* One of the BOOT_MAGIC_[...] values. */ |
| 159 | uint8_t swap_type; /* One of the BOOT_SWAP_TYPE_[...] values. */ |
| 160 | uint8_t copy_done; /* One of the BOOT_FLAG_[...] values. */ |
| 161 | uint8_t image_ok; /* One of the BOOT_FLAG_[...] values. */ |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 162 | uint8_t image_num; /* Boot status belongs to this image */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 163 | }; |
| 164 | |
David Vincze | b75c12a | 2019-03-22 14:58:33 +0100 | [diff] [blame] | 165 | #ifdef MCUBOOT_IMAGE_NUMBER |
| 166 | #define BOOT_IMAGE_NUMBER MCUBOOT_IMAGE_NUMBER |
| 167 | #else |
| 168 | #define BOOT_IMAGE_NUMBER 1 |
| 169 | #endif |
| 170 | |
Fabio Utzig | abec073 | 2019-07-31 08:40:22 -0300 | [diff] [blame] | 171 | _Static_assert(BOOT_IMAGE_NUMBER > 0, "Invalid value for BOOT_IMAGE_NUMBER"); |
| 172 | |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 173 | #if !defined(MCUBOOT_DIRECT_XIP) |
| 174 | #define IS_IN_XIP_MODE() 0 |
| 175 | #else |
| 176 | #define IS_IN_XIP_MODE() 1 |
| 177 | |
| 178 | #if (BOOT_IMAGE_NUMBER != 1) |
| 179 | #error "The MCUBOOT_DIRECT_XIP mode only supports single-image boot (MCUBOOT_IMAGE_NUMBER=1)." |
| 180 | #endif |
| 181 | #ifdef MCUBOOT_ENC_IMAGES |
| 182 | #error "Image encryption (MCUBOOT_ENC_IMAGES) is not supported when MCUBOOT_DIRECT_XIP mode is selected." |
| 183 | #endif |
| 184 | #endif /* MCUBOOT_DIRECT_XIP */ |
| 185 | |
Marti Bolivar | f9bfddd | 2018-04-24 14:28:33 -0400 | [diff] [blame] | 186 | #define BOOT_MAX_IMG_SECTORS MCUBOOT_MAX_IMG_SECTORS |
Fabio Utzig | a1fae67 | 2018-03-30 10:52:38 -0300 | [diff] [blame] | 187 | |
| 188 | /* |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 189 | * Extract the swap type and image number from image trailers's swap_info |
| 190 | * filed. |
| 191 | */ |
| 192 | #define BOOT_GET_SWAP_TYPE(swap_info) ((swap_info) & 0x0F) |
| 193 | #define BOOT_GET_IMAGE_NUM(swap_info) ((swap_info) >> 4) |
| 194 | |
| 195 | /* Construct the swap_info field from swap type and image number */ |
| 196 | #define BOOT_SET_SWAP_INFO(swap_info, image, type) { \ |
| 197 | assert((image) < 0xF); \ |
| 198 | assert((type) < 0xF); \ |
| 199 | (swap_info) = (image) << 4 \ |
| 200 | | (type); \ |
| 201 | } |
| 202 | |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 203 | #define BOOT_LOG_IMAGE_INFO(slot, hdr) \ |
| 204 | BOOT_LOG_INF("%-9s slot: version=%u.%u.%u+%u", \ |
| 205 | ((slot) == BOOT_PRIMARY_SLOT) ? "Primary" : "Secondary", \ |
| 206 | (hdr)->ih_ver.iv_major, \ |
| 207 | (hdr)->ih_ver.iv_minor, \ |
| 208 | (hdr)->ih_ver.iv_revision, \ |
| 209 | (hdr)->ih_ver.iv_build_num) |
| 210 | |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 211 | /* |
Fabio Utzig | a1fae67 | 2018-03-30 10:52:38 -0300 | [diff] [blame] | 212 | * The current flashmap API does not check the amount of space allocated when |
| 213 | * loading sector data from the flash device, allowing for smaller counts here |
| 214 | * would most surely incur in overruns. |
| 215 | * |
| 216 | * TODO: make flashmap API receive the current sector array size. |
| 217 | */ |
| 218 | #if BOOT_MAX_IMG_SECTORS < 32 |
| 219 | #error "Too few sectors, please increase BOOT_MAX_IMG_SECTORS to at least 32" |
| 220 | #endif |
| 221 | |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 222 | #if MCUBOOT_SWAP_USING_MOVE |
| 223 | #define BOOT_STATUS_MOVE_STATE_COUNT 1 |
| 224 | #define BOOT_STATUS_SWAP_STATE_COUNT 2 |
| 225 | #define BOOT_STATUS_STATE_COUNT (BOOT_STATUS_MOVE_STATE_COUNT + BOOT_STATUS_SWAP_STATE_COUNT) |
| 226 | #else |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 227 | #define BOOT_STATUS_STATE_COUNT 3 |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 228 | #endif |
| 229 | |
| 230 | /** Maximum number of image sectors supported by the bootloader. */ |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 231 | #define BOOT_STATUS_MAX_ENTRIES BOOT_MAX_IMG_SECTORS |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 232 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 233 | #define BOOT_PRIMARY_SLOT 0 |
| 234 | #define BOOT_SECONDARY_SLOT 1 |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 235 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 236 | #define BOOT_STATUS_SOURCE_NONE 0 |
| 237 | #define BOOT_STATUS_SOURCE_SCRATCH 1 |
| 238 | #define BOOT_STATUS_SOURCE_PRIMARY_SLOT 2 |
| 239 | |
David Brown | 2b8a695 | 2019-10-01 16:14:53 -0600 | [diff] [blame] | 240 | #define BOOT_MAGIC_SZ (sizeof boot_img_magic) |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 241 | |
Marti Bolivar | c50926f | 2017-06-14 09:35:40 -0400 | [diff] [blame] | 242 | /** |
| 243 | * Compatibility shim for flash sector type. |
| 244 | * |
| 245 | * This can be deleted when flash_area_to_sectors() is removed. |
| 246 | */ |
| 247 | #ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS |
| 248 | typedef struct flash_sector boot_sector_t; |
| 249 | #else |
| 250 | typedef struct flash_area boot_sector_t; |
| 251 | #endif |
| 252 | |
Marti Bolivar | 9b1f8bb | 2017-06-12 15:24:13 -0400 | [diff] [blame] | 253 | /** Private state maintained during boot. */ |
| 254 | struct boot_loader_state { |
| 255 | struct { |
| 256 | struct image_header hdr; |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 257 | const struct flash_area *area; |
Marti Bolivar | c50926f | 2017-06-14 09:35:40 -0400 | [diff] [blame] | 258 | boot_sector_t *sectors; |
Marti Bolivar | 8489865 | 2017-06-13 17:20:22 -0400 | [diff] [blame] | 259 | size_t num_sectors; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 260 | } imgs[BOOT_IMAGE_NUMBER][BOOT_NUM_SLOTS]; |
Marti Bolivar | 9b1f8bb | 2017-06-12 15:24:13 -0400 | [diff] [blame] | 261 | |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 262 | #if MCUBOOT_SWAP_USING_SCRATCH |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 263 | struct { |
| 264 | const struct flash_area *area; |
| 265 | boot_sector_t *sectors; |
| 266 | size_t num_sectors; |
| 267 | } scratch; |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 268 | #endif |
Marti Bolivar | 9b1f8bb | 2017-06-12 15:24:13 -0400 | [diff] [blame] | 269 | |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 270 | uint8_t swap_type[BOOT_IMAGE_NUMBER]; |
David Brown | ab44918 | 2019-11-15 09:32:52 -0700 | [diff] [blame] | 271 | uint32_t write_sz; |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 272 | |
| 273 | #if defined(MCUBOOT_ENC_IMAGES) |
Fabio Utzig | 1e4284b | 2019-08-23 11:55:27 -0300 | [diff] [blame] | 274 | struct enc_key_data enc[BOOT_IMAGE_NUMBER][BOOT_NUM_SLOTS]; |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 275 | #endif |
| 276 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 277 | #if (BOOT_IMAGE_NUMBER > 1) |
| 278 | uint8_t curr_img_idx; |
| 279 | #endif |
Marti Bolivar | 9b1f8bb | 2017-06-12 15:24:13 -0400 | [diff] [blame] | 280 | }; |
| 281 | |
Fabio Utzig | 1a927dd | 2017-12-05 10:30:26 -0200 | [diff] [blame] | 282 | int bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, |
| 283 | size_t slen, uint8_t key_id); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 284 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 285 | int boot_magic_compatible_check(uint8_t tbl_val, uint8_t val); |
Fabio Utzig | 3fbbdac | 2019-12-19 15:18:23 -0300 | [diff] [blame] | 286 | uint32_t boot_status_sz(uint32_t min_write_sz); |
David Brown | ab44918 | 2019-11-15 09:32:52 -0700 | [diff] [blame] | 287 | uint32_t boot_trailer_sz(uint32_t min_write_sz); |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 288 | int boot_status_entries(int image_index, const struct flash_area *fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 289 | uint32_t boot_status_off(const struct flash_area *fap); |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 290 | uint32_t boot_swap_info_off(const struct flash_area *fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 291 | int boot_read_swap_state(const struct flash_area *fap, |
| 292 | struct boot_swap_state *state); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 293 | int boot_read_swap_state_by_id(int flash_area_id, |
| 294 | struct boot_swap_state *state); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 295 | int boot_write_magic(const struct flash_area *fap); |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 296 | int 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] | 297 | int boot_write_copy_done(const struct flash_area *fap); |
| 298 | int boot_write_image_ok(const struct flash_area *fap); |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 299 | int boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type, |
| 300 | uint8_t image_num); |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 301 | int boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size); |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 302 | int boot_read_swap_size(int image_index, uint32_t *swap_size); |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 303 | int boot_slots_compatible(struct boot_loader_state *state); |
| 304 | uint32_t boot_status_internal_off(const struct boot_status *bs, int elem_sz); |
| 305 | int boot_read_image_header(struct boot_loader_state *state, int slot, |
| 306 | struct image_header *out_hdr, struct boot_status *bs); |
| 307 | int boot_copy_region(struct boot_loader_state *state, |
| 308 | const struct flash_area *fap_src, |
| 309 | const struct flash_area *fap_dst, |
| 310 | uint32_t off_src, uint32_t off_dst, uint32_t sz); |
| 311 | int boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz); |
Fabio Utzig | 9e1db9a | 2020-01-02 21:14:22 -0300 | [diff] [blame] | 312 | bool boot_status_is_reset(const struct boot_status *bs); |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 313 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 314 | #ifdef MCUBOOT_ENC_IMAGES |
| 315 | int boot_write_enc_key(const struct flash_area *fap, uint8_t slot, |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 316 | const struct boot_status *bs); |
| 317 | int boot_read_enc_key(int image_index, uint8_t slot, struct boot_status *bs); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 318 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 319 | |
David Brown | effb06e | 2019-10-10 14:45:32 -0600 | [diff] [blame] | 320 | /** |
| 321 | * Safe (non-overflowing) uint32_t addition. Returns true, and stores |
| 322 | * the result in *dest if it can be done without overflow. Otherwise, |
| 323 | * returns false. |
| 324 | */ |
| 325 | static inline bool boot_u32_safe_add(uint32_t *dest, uint32_t a, uint32_t b) |
| 326 | { |
| 327 | /* |
| 328 | * "a + b <= UINT32_MAX", subtract 'b' from both sides to avoid |
| 329 | * the overflow. |
| 330 | */ |
| 331 | if (a > UINT32_MAX - b) { |
| 332 | return false; |
| 333 | } else { |
| 334 | *dest = a + b; |
| 335 | return true; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Safe (non-overflowing) uint16_t addition. Returns true, and stores |
| 341 | * the result in *dest if it can be done without overflow. Otherwise, |
| 342 | * returns false. |
| 343 | */ |
| 344 | static inline bool boot_u16_safe_add(uint16_t *dest, uint16_t a, uint16_t b) |
| 345 | { |
| 346 | uint32_t tmp = a + b; |
| 347 | if (tmp > UINT16_MAX) { |
| 348 | return false; |
| 349 | } else { |
| 350 | *dest = tmp; |
| 351 | return true; |
| 352 | } |
| 353 | } |
| 354 | |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 355 | /* |
| 356 | * Accessors for the contents of struct boot_loader_state. |
| 357 | */ |
| 358 | |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 359 | /* These are macros so they can be used as lvalues. */ |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 360 | #if (BOOT_IMAGE_NUMBER > 1) |
| 361 | #define BOOT_CURR_IMG(state) ((state)->curr_img_idx) |
| 362 | #else |
| 363 | #define BOOT_CURR_IMG(state) 0 |
Fabio Utzig | bc07793 | 2019-08-26 11:16:34 -0300 | [diff] [blame] | 364 | #endif |
| 365 | #ifdef MCUBOOT_ENC_IMAGES |
| 366 | #define BOOT_CURR_ENC(state) ((state)->enc[BOOT_CURR_IMG(state)]) |
| 367 | #else |
| 368 | #define BOOT_CURR_ENC(state) NULL |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 369 | #endif |
| 370 | #define BOOT_IMG(state, slot) ((state)->imgs[BOOT_CURR_IMG(state)][(slot)]) |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 371 | #define BOOT_IMG_AREA(state, slot) (BOOT_IMG(state, slot).area) |
Marti Bolivar | e10a739 | 2017-06-14 16:20:07 -0400 | [diff] [blame] | 372 | #define BOOT_WRITE_SZ(state) ((state)->write_sz) |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 373 | #define BOOT_SWAP_TYPE(state) ((state)->swap_type[BOOT_CURR_IMG(state)]) |
Fabio Utzig | c962135 | 2019-08-08 12:15:51 -0300 | [diff] [blame] | 374 | #define BOOT_TLV_OFF(hdr) ((hdr)->ih_hdr_size + (hdr)->ih_img_size) |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 375 | |
Fabio Utzig | e575b0b | 2019-09-11 12:34:23 -0300 | [diff] [blame] | 376 | #define BOOT_IS_UPGRADE(swap_type) \ |
| 377 | (((swap_type) == BOOT_SWAP_TYPE_TEST) || \ |
| 378 | ((swap_type) == BOOT_SWAP_TYPE_REVERT) || \ |
| 379 | ((swap_type) == BOOT_SWAP_TYPE_PERM)) |
| 380 | |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 381 | static inline struct image_header* |
| 382 | boot_img_hdr(struct boot_loader_state *state, size_t slot) |
| 383 | { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 384 | return &BOOT_IMG(state, slot).hdr; |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 385 | } |
| 386 | |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 387 | static inline size_t |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 388 | boot_img_num_sectors(const struct boot_loader_state *state, size_t slot) |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 389 | { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 390 | return BOOT_IMG(state, slot).num_sectors; |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 391 | } |
| 392 | |
Marti Bolivar | 135b8f6 | 2017-06-13 17:22:13 -0400 | [diff] [blame] | 393 | /* |
| 394 | * Offset of the slot from the beginning of the flash device. |
| 395 | */ |
| 396 | static inline uint32_t |
| 397 | boot_img_slot_off(struct boot_loader_state *state, size_t slot) |
| 398 | { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 399 | return BOOT_IMG(state, slot).area->fa_off; |
Marti Bolivar | 135b8f6 | 2017-06-13 17:22:13 -0400 | [diff] [blame] | 400 | } |
| 401 | |
Marti Bolivar | c50926f | 2017-06-14 09:35:40 -0400 | [diff] [blame] | 402 | #ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS |
| 403 | |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 404 | static inline size_t |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 405 | boot_img_sector_size(const struct boot_loader_state *state, |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 406 | size_t slot, size_t sector) |
| 407 | { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 408 | return BOOT_IMG(state, slot).sectors[sector].fa_size; |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 409 | } |
| 410 | |
Marti Bolivar | ea08887 | 2017-06-12 17:10:49 -0400 | [diff] [blame] | 411 | /* |
| 412 | * Offset of the sector from the beginning of the image, NOT the flash |
| 413 | * device. |
| 414 | */ |
| 415 | static inline uint32_t |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 416 | boot_img_sector_off(const struct boot_loader_state *state, size_t slot, |
Marti Bolivar | ea08887 | 2017-06-12 17:10:49 -0400 | [diff] [blame] | 417 | size_t sector) |
| 418 | { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 419 | return BOOT_IMG(state, slot).sectors[sector].fa_off - |
| 420 | BOOT_IMG(state, slot).sectors[0].fa_off; |
Marti Bolivar | ea08887 | 2017-06-12 17:10:49 -0400 | [diff] [blame] | 421 | } |
| 422 | |
Marti Bolivar | c50926f | 2017-06-14 09:35:40 -0400 | [diff] [blame] | 423 | #else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */ |
| 424 | |
| 425 | static inline size_t |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 426 | boot_img_sector_size(const struct boot_loader_state *state, |
Marti Bolivar | c50926f | 2017-06-14 09:35:40 -0400 | [diff] [blame] | 427 | size_t slot, size_t sector) |
| 428 | { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 429 | return BOOT_IMG(state, slot).sectors[sector].fs_size; |
Marti Bolivar | c50926f | 2017-06-14 09:35:40 -0400 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | static inline uint32_t |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 433 | boot_img_sector_off(const struct boot_loader_state *state, size_t slot, |
Marti Bolivar | c50926f | 2017-06-14 09:35:40 -0400 | [diff] [blame] | 434 | size_t sector) |
| 435 | { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 436 | return BOOT_IMG(state, slot).sectors[sector].fs_off - |
| 437 | BOOT_IMG(state, slot).sectors[0].fs_off; |
Marti Bolivar | c50926f | 2017-06-14 09:35:40 -0400 | [diff] [blame] | 438 | } |
| 439 | |
Marti Bolivar | c50926f | 2017-06-14 09:35:40 -0400 | [diff] [blame] | 440 | #endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */ |
| 441 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 442 | #ifdef __cplusplus |
| 443 | } |
| 444 | #endif |
| 445 | |
| 446 | #endif |