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 | |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 31 | #include <string.h> |
| 32 | |
Marti Bolivar | cca28a9 | 2017-06-12 16:52:22 -0400 | [diff] [blame] | 33 | #include "sysflash/sysflash.h" |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 34 | |
| 35 | #include <flash_map_backend/flash_map_backend.h> |
| 36 | |
Christopher Collins | 01dfbb6 | 2019-03-21 07:28:37 -0700 | [diff] [blame] | 37 | #include "bootutil/bootutil.h" |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 38 | #include "bootutil/image.h" |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 39 | #include "bootutil/fault_injection_hardening.h" |
Marti Bolivar | f91bca5 | 2018-04-12 12:40:46 -0400 | [diff] [blame] | 40 | #include "mcuboot_config/mcuboot_config.h" |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 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 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 46 | #ifdef __cplusplus |
| 47 | extern "C" { |
| 48 | #endif |
| 49 | |
| 50 | struct flash_area; |
| 51 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 52 | #define BOOT_TMPBUF_SZ 256 |
| 53 | |
Fabio Utzig | 1e4284b | 2019-08-23 11:55:27 -0300 | [diff] [blame] | 54 | /** Number of image slots in flash; currently limited to two. */ |
| 55 | #define BOOT_NUM_SLOTS 2 |
| 56 | |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 57 | #if (defined(MCUBOOT_OVERWRITE_ONLY) + \ |
| 58 | defined(MCUBOOT_SWAP_USING_MOVE) + \ |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 59 | defined(MCUBOOT_DIRECT_XIP) + \ |
| 60 | defined(MCUBOOT_RAM_LOAD)) > 1 |
| 61 | #error "Please enable only one of MCUBOOT_OVERWRITE_ONLY, MCUBOOT_SWAP_USING_MOVE, MCUBOOT_DIRECT_XIP or MCUBOOT_RAM_LOAD" |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 62 | #endif |
| 63 | |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 64 | #if !defined(MCUBOOT_OVERWRITE_ONLY) && \ |
| 65 | !defined(MCUBOOT_SWAP_USING_MOVE) && \ |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 66 | !defined(MCUBOOT_DIRECT_XIP) && \ |
| 67 | !defined(MCUBOOT_RAM_LOAD) |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 68 | #define MCUBOOT_SWAP_USING_SCRATCH 1 |
| 69 | #endif |
| 70 | |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 71 | #define BOOT_STATUS_OP_MOVE 1 |
| 72 | #define BOOT_STATUS_OP_SWAP 2 |
| 73 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 74 | /* |
| 75 | * Maintain state of copy progress. |
| 76 | */ |
| 77 | struct boot_status { |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 78 | uint32_t idx; /* Which area we're operating on */ |
| 79 | uint8_t state; /* Which part of the swapping process are we at */ |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 80 | uint8_t op; /* What operation are we performing? */ |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 81 | uint8_t use_scratch; /* Are status bytes ever written to scratch? */ |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 82 | uint8_t swap_type; /* The type of swap in effect */ |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 83 | uint32_t swap_size; /* Total size of swapped image */ |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 84 | #ifdef MCUBOOT_ENC_IMAGES |
Fabio Utzig | 1e4284b | 2019-08-23 11:55:27 -0300 | [diff] [blame] | 85 | uint8_t enckey[BOOT_NUM_SLOTS][BOOT_ENC_KEY_SIZE]; |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 86 | #if MCUBOOT_SWAP_SAVE_ENCTLV |
| 87 | uint8_t enctlv[BOOT_NUM_SLOTS][BOOT_ENC_TLV_ALIGN_SIZE]; |
| 88 | #endif |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 89 | #endif |
Fabio Utzig | b86e688 | 2019-12-10 09:51:17 -0300 | [diff] [blame] | 90 | int source; /* Which slot contains swap status metadata */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 91 | }; |
| 92 | |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 93 | #define BOOT_STATUS_IDX_0 1 |
| 94 | |
| 95 | #define BOOT_STATUS_STATE_0 1 |
| 96 | #define BOOT_STATUS_STATE_1 2 |
| 97 | #define BOOT_STATUS_STATE_2 3 |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 98 | |
| 99 | /** |
| 100 | * End-of-image slot structure. |
| 101 | * |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 102 | * 0 1 2 3 |
| 103 | * 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 |
| 104 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 105 | * ~ ~ |
| 106 | * ~ Swap status (BOOT_MAX_IMG_SECTORS * min-write-size * 3) ~ |
| 107 | * ~ ~ |
| 108 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 109 | * | Encryption key 0 (16 octets) [*] | |
| 110 | * | | |
| 111 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 112 | * | Encryption key 1 (16 octets) [*] | |
| 113 | * | | |
| 114 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 115 | * | Swap size (4 octets) | |
| 116 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 117 | * | Swap info | 0xff padding (7 octets) | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 118 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 119 | * | Copy done | 0xff padding (7 octets) | |
| 120 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 121 | * | Image OK | 0xff padding (7 octets) | |
| 122 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 123 | * | MAGIC (16 octets) | |
| 124 | * | | |
| 125 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 126 | * |
| 127 | * [*]: Only present if the encryption option is enabled |
| 128 | * (`MCUBOOT_ENC_IMAGES`). |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 129 | */ |
| 130 | |
| 131 | extern const uint32_t boot_img_magic[4]; |
| 132 | |
David Vincze | b75c12a | 2019-03-22 14:58:33 +0100 | [diff] [blame] | 133 | #ifdef MCUBOOT_IMAGE_NUMBER |
| 134 | #define BOOT_IMAGE_NUMBER MCUBOOT_IMAGE_NUMBER |
| 135 | #else |
| 136 | #define BOOT_IMAGE_NUMBER 1 |
| 137 | #endif |
| 138 | |
Fabio Utzig | abec073 | 2019-07-31 08:40:22 -0300 | [diff] [blame] | 139 | _Static_assert(BOOT_IMAGE_NUMBER > 0, "Invalid value for BOOT_IMAGE_NUMBER"); |
| 140 | |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 141 | #if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD) |
| 142 | #define ARE_SLOTS_EQUIVALENT() 0 |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 143 | #else |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 144 | #define ARE_SLOTS_EQUIVALENT() 1 |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 145 | |
| 146 | #if (BOOT_IMAGE_NUMBER != 1) |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 147 | #error "The MCUBOOT_DIRECT_XIP and MCUBOOT_RAM_LOAD mode only supports single-image boot (MCUBOOT_IMAGE_NUMBER=1)." |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 148 | #endif |
| 149 | #ifdef MCUBOOT_ENC_IMAGES |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 150 | #error "Image encryption (MCUBOOT_ENC_IMAGES) is not supported when MCUBOOT_DIRECT_XIP or MCUBOOT_RAM_LOAD mode is selected." |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 151 | #endif |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 152 | #endif /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */ |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 153 | |
Marti Bolivar | f9bfddd | 2018-04-24 14:28:33 -0400 | [diff] [blame] | 154 | #define BOOT_MAX_IMG_SECTORS MCUBOOT_MAX_IMG_SECTORS |
Fabio Utzig | a1fae67 | 2018-03-30 10:52:38 -0300 | [diff] [blame] | 155 | |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 156 | #define BOOT_LOG_IMAGE_INFO(slot, hdr) \ |
| 157 | BOOT_LOG_INF("%-9s slot: version=%u.%u.%u+%u", \ |
| 158 | ((slot) == BOOT_PRIMARY_SLOT) ? "Primary" : "Secondary", \ |
| 159 | (hdr)->ih_ver.iv_major, \ |
| 160 | (hdr)->ih_ver.iv_minor, \ |
| 161 | (hdr)->ih_ver.iv_revision, \ |
| 162 | (hdr)->ih_ver.iv_build_num) |
| 163 | |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 164 | /* |
Fabio Utzig | a1fae67 | 2018-03-30 10:52:38 -0300 | [diff] [blame] | 165 | * The current flashmap API does not check the amount of space allocated when |
| 166 | * loading sector data from the flash device, allowing for smaller counts here |
| 167 | * would most surely incur in overruns. |
| 168 | * |
| 169 | * TODO: make flashmap API receive the current sector array size. |
| 170 | */ |
| 171 | #if BOOT_MAX_IMG_SECTORS < 32 |
| 172 | #error "Too few sectors, please increase BOOT_MAX_IMG_SECTORS to at least 32" |
| 173 | #endif |
| 174 | |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 175 | #if MCUBOOT_SWAP_USING_MOVE |
| 176 | #define BOOT_STATUS_MOVE_STATE_COUNT 1 |
| 177 | #define BOOT_STATUS_SWAP_STATE_COUNT 2 |
| 178 | #define BOOT_STATUS_STATE_COUNT (BOOT_STATUS_MOVE_STATE_COUNT + BOOT_STATUS_SWAP_STATE_COUNT) |
| 179 | #else |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 180 | #define BOOT_STATUS_STATE_COUNT 3 |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 181 | #endif |
| 182 | |
| 183 | /** Maximum number of image sectors supported by the bootloader. */ |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 184 | #define BOOT_STATUS_MAX_ENTRIES BOOT_MAX_IMG_SECTORS |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 185 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 186 | #define BOOT_PRIMARY_SLOT 0 |
| 187 | #define BOOT_SECONDARY_SLOT 1 |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 188 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 189 | #define BOOT_STATUS_SOURCE_NONE 0 |
| 190 | #define BOOT_STATUS_SOURCE_SCRATCH 1 |
| 191 | #define BOOT_STATUS_SOURCE_PRIMARY_SLOT 2 |
| 192 | |
Marti Bolivar | c50926f | 2017-06-14 09:35:40 -0400 | [diff] [blame] | 193 | /** |
| 194 | * Compatibility shim for flash sector type. |
| 195 | * |
| 196 | * This can be deleted when flash_area_to_sectors() is removed. |
| 197 | */ |
| 198 | #ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS |
| 199 | typedef struct flash_sector boot_sector_t; |
| 200 | #else |
| 201 | typedef struct flash_area boot_sector_t; |
| 202 | #endif |
| 203 | |
Marti Bolivar | 9b1f8bb | 2017-06-12 15:24:13 -0400 | [diff] [blame] | 204 | /** Private state maintained during boot. */ |
| 205 | struct boot_loader_state { |
| 206 | struct { |
| 207 | struct image_header hdr; |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 208 | const struct flash_area *area; |
Marti Bolivar | c50926f | 2017-06-14 09:35:40 -0400 | [diff] [blame] | 209 | boot_sector_t *sectors; |
Marti Bolivar | 8489865 | 2017-06-13 17:20:22 -0400 | [diff] [blame] | 210 | size_t num_sectors; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 211 | } imgs[BOOT_IMAGE_NUMBER][BOOT_NUM_SLOTS]; |
Marti Bolivar | 9b1f8bb | 2017-06-12 15:24:13 -0400 | [diff] [blame] | 212 | |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 213 | #if MCUBOOT_SWAP_USING_SCRATCH |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 214 | struct { |
| 215 | const struct flash_area *area; |
| 216 | boot_sector_t *sectors; |
| 217 | size_t num_sectors; |
| 218 | } scratch; |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 219 | #endif |
Marti Bolivar | 9b1f8bb | 2017-06-12 15:24:13 -0400 | [diff] [blame] | 220 | |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 221 | uint8_t swap_type[BOOT_IMAGE_NUMBER]; |
David Brown | ab44918 | 2019-11-15 09:32:52 -0700 | [diff] [blame] | 222 | uint32_t write_sz; |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 223 | |
| 224 | #if defined(MCUBOOT_ENC_IMAGES) |
Fabio Utzig | 1e4284b | 2019-08-23 11:55:27 -0300 | [diff] [blame] | 225 | struct enc_key_data enc[BOOT_IMAGE_NUMBER][BOOT_NUM_SLOTS]; |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 226 | #endif |
| 227 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 228 | #if (BOOT_IMAGE_NUMBER > 1) |
| 229 | uint8_t curr_img_idx; |
| 230 | #endif |
Marti Bolivar | 9b1f8bb | 2017-06-12 15:24:13 -0400 | [diff] [blame] | 231 | }; |
| 232 | |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 233 | fih_int bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, |
| 234 | size_t slen, uint8_t key_id); |
| 235 | |
| 236 | fih_int boot_fih_memequal(const void *s1, const void *s2, size_t n); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 237 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 238 | int boot_magic_compatible_check(uint8_t tbl_val, uint8_t val); |
Fabio Utzig | 3fbbdac | 2019-12-19 15:18:23 -0300 | [diff] [blame] | 239 | uint32_t boot_status_sz(uint32_t min_write_sz); |
David Brown | ab44918 | 2019-11-15 09:32:52 -0700 | [diff] [blame] | 240 | uint32_t boot_trailer_sz(uint32_t min_write_sz); |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 241 | int boot_status_entries(int image_index, const struct flash_area *fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 242 | uint32_t boot_status_off(const struct flash_area *fap); |
| 243 | int boot_read_swap_state(const struct flash_area *fap, |
| 244 | struct boot_swap_state *state); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 245 | int boot_read_swap_state_by_id(int flash_area_id, |
| 246 | struct boot_swap_state *state); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 247 | int boot_write_magic(const struct flash_area *fap); |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 248 | 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] | 249 | int boot_write_copy_done(const struct flash_area *fap); |
| 250 | int boot_write_image_ok(const struct flash_area *fap); |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 251 | int boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type, |
| 252 | uint8_t image_num); |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 253 | int boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size); |
Dominik Ermel | a7f9e9f | 2021-03-24 17:31:57 +0000 | [diff] [blame^] | 254 | int boot_write_trailer(const struct flash_area *fap, uint32_t off, |
| 255 | const uint8_t *inbuf, uint8_t inlen); |
| 256 | int boot_write_trailer_flag(const struct flash_area *fap, uint32_t off, |
| 257 | uint8_t flag_val); |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 258 | int boot_read_swap_size(int image_index, uint32_t *swap_size); |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 259 | int boot_slots_compatible(struct boot_loader_state *state); |
| 260 | uint32_t boot_status_internal_off(const struct boot_status *bs, int elem_sz); |
| 261 | int boot_read_image_header(struct boot_loader_state *state, int slot, |
| 262 | struct image_header *out_hdr, struct boot_status *bs); |
| 263 | int boot_copy_region(struct boot_loader_state *state, |
| 264 | const struct flash_area *fap_src, |
| 265 | const struct flash_area *fap_dst, |
| 266 | uint32_t off_src, uint32_t off_dst, uint32_t sz); |
| 267 | 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] | 268 | bool boot_status_is_reset(const struct boot_status *bs); |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 269 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 270 | #ifdef MCUBOOT_ENC_IMAGES |
| 271 | 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] | 272 | const struct boot_status *bs); |
| 273 | 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] | 274 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 275 | |
David Brown | effb06e | 2019-10-10 14:45:32 -0600 | [diff] [blame] | 276 | /** |
Fabio Utzig | 4b2e55f | 2020-09-24 11:49:20 -0300 | [diff] [blame] | 277 | * Checks that a buffer is erased according to what the erase value for the |
| 278 | * flash device provided in `flash_area` is. |
| 279 | * |
| 280 | * @returns true if the buffer is erased; false if any of the bytes is not |
| 281 | * erased, or when buffer is NULL, or when len == 0. |
| 282 | */ |
| 283 | bool bootutil_buffer_is_erased(const struct flash_area *area, |
| 284 | const void *buffer, size_t len); |
| 285 | |
| 286 | /** |
David Brown | effb06e | 2019-10-10 14:45:32 -0600 | [diff] [blame] | 287 | * Safe (non-overflowing) uint32_t addition. Returns true, and stores |
| 288 | * the result in *dest if it can be done without overflow. Otherwise, |
| 289 | * returns false. |
| 290 | */ |
| 291 | static inline bool boot_u32_safe_add(uint32_t *dest, uint32_t a, uint32_t b) |
| 292 | { |
| 293 | /* |
| 294 | * "a + b <= UINT32_MAX", subtract 'b' from both sides to avoid |
| 295 | * the overflow. |
| 296 | */ |
| 297 | if (a > UINT32_MAX - b) { |
| 298 | return false; |
| 299 | } else { |
| 300 | *dest = a + b; |
| 301 | return true; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Safe (non-overflowing) uint16_t addition. Returns true, and stores |
| 307 | * the result in *dest if it can be done without overflow. Otherwise, |
| 308 | * returns false. |
| 309 | */ |
| 310 | static inline bool boot_u16_safe_add(uint16_t *dest, uint16_t a, uint16_t b) |
| 311 | { |
| 312 | uint32_t tmp = a + b; |
| 313 | if (tmp > UINT16_MAX) { |
| 314 | return false; |
| 315 | } else { |
| 316 | *dest = tmp; |
| 317 | return true; |
| 318 | } |
| 319 | } |
| 320 | |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 321 | /* |
| 322 | * Accessors for the contents of struct boot_loader_state. |
| 323 | */ |
| 324 | |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 325 | /* These are macros so they can be used as lvalues. */ |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 326 | #if (BOOT_IMAGE_NUMBER > 1) |
| 327 | #define BOOT_CURR_IMG(state) ((state)->curr_img_idx) |
| 328 | #else |
| 329 | #define BOOT_CURR_IMG(state) 0 |
Fabio Utzig | bc07793 | 2019-08-26 11:16:34 -0300 | [diff] [blame] | 330 | #endif |
| 331 | #ifdef MCUBOOT_ENC_IMAGES |
| 332 | #define BOOT_CURR_ENC(state) ((state)->enc[BOOT_CURR_IMG(state)]) |
| 333 | #else |
| 334 | #define BOOT_CURR_ENC(state) NULL |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 335 | #endif |
| 336 | #define BOOT_IMG(state, slot) ((state)->imgs[BOOT_CURR_IMG(state)][(slot)]) |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 337 | #define BOOT_IMG_AREA(state, slot) (BOOT_IMG(state, slot).area) |
Marti Bolivar | e10a739 | 2017-06-14 16:20:07 -0400 | [diff] [blame] | 338 | #define BOOT_WRITE_SZ(state) ((state)->write_sz) |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 339 | #define BOOT_SWAP_TYPE(state) ((state)->swap_type[BOOT_CURR_IMG(state)]) |
Fabio Utzig | c962135 | 2019-08-08 12:15:51 -0300 | [diff] [blame] | 340 | #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] | 341 | |
Fabio Utzig | e575b0b | 2019-09-11 12:34:23 -0300 | [diff] [blame] | 342 | #define BOOT_IS_UPGRADE(swap_type) \ |
| 343 | (((swap_type) == BOOT_SWAP_TYPE_TEST) || \ |
| 344 | ((swap_type) == BOOT_SWAP_TYPE_REVERT) || \ |
| 345 | ((swap_type) == BOOT_SWAP_TYPE_PERM)) |
| 346 | |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 347 | static inline struct image_header* |
| 348 | boot_img_hdr(struct boot_loader_state *state, size_t slot) |
| 349 | { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 350 | return &BOOT_IMG(state, slot).hdr; |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 351 | } |
| 352 | |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 353 | static inline size_t |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 354 | 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] | 355 | { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 356 | return BOOT_IMG(state, slot).num_sectors; |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 357 | } |
| 358 | |
Marti Bolivar | 135b8f6 | 2017-06-13 17:22:13 -0400 | [diff] [blame] | 359 | /* |
| 360 | * Offset of the slot from the beginning of the flash device. |
| 361 | */ |
| 362 | static inline uint32_t |
| 363 | boot_img_slot_off(struct boot_loader_state *state, size_t slot) |
| 364 | { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 365 | return BOOT_IMG(state, slot).area->fa_off; |
Marti Bolivar | 135b8f6 | 2017-06-13 17:22:13 -0400 | [diff] [blame] | 366 | } |
| 367 | |
Marti Bolivar | c50926f | 2017-06-14 09:35:40 -0400 | [diff] [blame] | 368 | #ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS |
| 369 | |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 370 | static inline size_t |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 371 | boot_img_sector_size(const struct boot_loader_state *state, |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 372 | size_t slot, size_t sector) |
| 373 | { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 374 | return BOOT_IMG(state, slot).sectors[sector].fa_size; |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 375 | } |
| 376 | |
Marti Bolivar | ea08887 | 2017-06-12 17:10:49 -0400 | [diff] [blame] | 377 | /* |
| 378 | * Offset of the sector from the beginning of the image, NOT the flash |
| 379 | * device. |
| 380 | */ |
| 381 | static inline uint32_t |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 382 | 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] | 383 | size_t sector) |
| 384 | { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 385 | return BOOT_IMG(state, slot).sectors[sector].fa_off - |
| 386 | BOOT_IMG(state, slot).sectors[0].fa_off; |
Marti Bolivar | ea08887 | 2017-06-12 17:10:49 -0400 | [diff] [blame] | 387 | } |
| 388 | |
Marti Bolivar | c50926f | 2017-06-14 09:35:40 -0400 | [diff] [blame] | 389 | #else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */ |
| 390 | |
| 391 | static inline size_t |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 392 | boot_img_sector_size(const struct boot_loader_state *state, |
Marti Bolivar | c50926f | 2017-06-14 09:35:40 -0400 | [diff] [blame] | 393 | size_t slot, size_t sector) |
| 394 | { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 395 | return BOOT_IMG(state, slot).sectors[sector].fs_size; |
Marti Bolivar | c50926f | 2017-06-14 09:35:40 -0400 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | static inline uint32_t |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 399 | 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] | 400 | size_t sector) |
| 401 | { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 402 | return BOOT_IMG(state, slot).sectors[sector].fs_off - |
| 403 | BOOT_IMG(state, slot).sectors[0].fs_off; |
Marti Bolivar | c50926f | 2017-06-14 09:35:40 -0400 | [diff] [blame] | 404 | } |
| 405 | |
Marti Bolivar | c50926f | 2017-06-14 09:35:40 -0400 | [diff] [blame] | 406 | #endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */ |
| 407 | |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 408 | #ifdef MCUBOOT_RAM_LOAD |
| 409 | #define LOAD_IMAGE_DATA(hdr, fap, start, output, size) \ |
| 410 | (memcpy((output),(void*)((hdr)->ih_load_addr + (start)), \ |
Tamas Ban | e4885a6 | 2020-11-12 13:50:07 +0000 | [diff] [blame] | 411 | (size)), 0) |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 412 | #else |
| 413 | #define LOAD_IMAGE_DATA(hdr, fap, start, output, size) \ |
| 414 | (flash_area_read((fap), (start), (output), (size))) |
| 415 | #endif /* MCUBOOT_RAM_LOAD */ |
| 416 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 417 | #ifdef __cplusplus |
| 418 | } |
| 419 | #endif |
| 420 | |
| 421 | #endif |