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 | #ifndef H_BOOTUTIL_PRIV_ |
| 25 | #define H_BOOTUTIL_PRIV_ |
| 26 | |
Marti Bolivar | cca28a9 | 2017-06-12 16:52:22 -0400 | [diff] [blame] | 27 | #include "sysflash/sysflash.h" |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 28 | |
| 29 | #include <flash_map_backend/flash_map_backend.h> |
| 30 | |
Christopher Collins | 01dfbb6 | 2019-03-21 07:28:37 -0700 | [diff] [blame] | 31 | #include "bootutil/bootutil.h" |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 32 | #include "bootutil/image.h" |
Marti Bolivar | f91bca5 | 2018-04-12 12:40:46 -0400 | [diff] [blame] | 33 | #include "mcuboot_config/mcuboot_config.h" |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 34 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 35 | #ifdef MCUBOOT_ENC_IMAGES |
| 36 | #include "bootutil/enc_key.h" |
| 37 | #endif |
| 38 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 39 | #ifdef __cplusplus |
| 40 | extern "C" { |
| 41 | #endif |
| 42 | |
Marti Bolivar | 248da08 | 2018-04-24 15:11:39 -0400 | [diff] [blame] | 43 | #ifdef MCUBOOT_HAVE_ASSERT_H |
| 44 | #include "mcuboot_config/mcuboot_assert.h" |
Fabio Utzig | 57c40f7 | 2017-12-12 21:48:30 -0200 | [diff] [blame] | 45 | #else |
| 46 | #define ASSERT assert |
| 47 | #endif |
| 48 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 49 | struct flash_area; |
| 50 | |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 51 | #define BOOT_EFLASH 1 |
| 52 | #define BOOT_EFILE 2 |
| 53 | #define BOOT_EBADIMAGE 3 |
| 54 | #define BOOT_EBADVECT 4 |
| 55 | #define BOOT_EBADSTATUS 5 |
| 56 | #define BOOT_ENOMEM 6 |
| 57 | #define BOOT_EBADARGS 7 |
| 58 | #define BOOT_EBADVERSION 8 |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 59 | |
| 60 | #define BOOT_TMPBUF_SZ 256 |
| 61 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 62 | /* |
| 63 | * Maintain state of copy progress. |
| 64 | */ |
| 65 | struct boot_status { |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 66 | uint32_t idx; /* Which area we're operating on */ |
| 67 | uint8_t state; /* Which part of the swapping process are we at */ |
| 68 | uint8_t use_scratch; /* Are status bytes ever written to scratch? */ |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 69 | uint8_t swap_type; /* The type of swap in effect */ |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 70 | uint32_t swap_size; /* Total size of swapped image */ |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 71 | #ifdef MCUBOOT_ENC_IMAGES |
| 72 | uint8_t enckey[2][BOOT_ENC_KEY_SIZE]; |
| 73 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 74 | }; |
| 75 | |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 76 | #define BOOT_MAGIC_GOOD 1 |
| 77 | #define BOOT_MAGIC_BAD 2 |
| 78 | #define BOOT_MAGIC_UNSET 3 |
| 79 | #define BOOT_MAGIC_ANY 4 /* NOTE: control only, not dependent on sector */ |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 80 | #define BOOT_MAGIC_NOTGOOD 5 /* NOTE: control only, not dependent on sector */ |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 81 | |
| 82 | /* |
| 83 | * NOTE: leave BOOT_FLAG_SET equal to one, this is written to flash! |
| 84 | */ |
| 85 | #define BOOT_FLAG_SET 1 |
| 86 | #define BOOT_FLAG_BAD 2 |
| 87 | #define BOOT_FLAG_UNSET 3 |
| 88 | #define BOOT_FLAG_ANY 4 /* NOTE: control only, not dependent on sector */ |
| 89 | |
| 90 | #define BOOT_STATUS_IDX_0 1 |
| 91 | |
| 92 | #define BOOT_STATUS_STATE_0 1 |
| 93 | #define BOOT_STATUS_STATE_1 2 |
| 94 | #define BOOT_STATUS_STATE_2 3 |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 95 | |
| 96 | /** |
| 97 | * End-of-image slot structure. |
| 98 | * |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 99 | * 0 1 2 3 |
| 100 | * 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 |
| 101 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 102 | * ~ ~ |
| 103 | * ~ Swap status (BOOT_MAX_IMG_SECTORS * min-write-size * 3) ~ |
| 104 | * ~ ~ |
| 105 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 106 | * | Encryption key 0 (16 octets) [*] | |
| 107 | * | | |
| 108 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 109 | * | Encryption key 1 (16 octets) [*] | |
| 110 | * | | |
| 111 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 112 | * | Swap size (4 octets) | |
| 113 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 114 | * | Swap info | 0xff padding (7 octets) | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 115 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 116 | * | Copy done | 0xff padding (7 octets) | |
| 117 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 118 | * | Image OK | 0xff padding (7 octets) | |
| 119 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 120 | * | MAGIC (16 octets) | |
| 121 | * | | |
| 122 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 123 | * |
| 124 | * [*]: Only present if the encryption option is enabled |
| 125 | * (`MCUBOOT_ENC_IMAGES`). |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 126 | */ |
| 127 | |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 128 | extern uint8_t current_image; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 129 | extern const uint32_t boot_img_magic[4]; |
| 130 | |
| 131 | struct boot_swap_state { |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 132 | uint8_t magic; /* One of the BOOT_MAGIC_[...] values. */ |
| 133 | uint8_t swap_type; /* One of the BOOT_SWAP_TYPE_[...] values. */ |
| 134 | uint8_t copy_done; /* One of the BOOT_FLAG_[...] values. */ |
| 135 | uint8_t image_ok; /* One of the BOOT_FLAG_[...] values. */ |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 136 | uint8_t image_num; /* Boot status belongs to this image */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 137 | }; |
| 138 | |
David Vincze | b75c12a | 2019-03-22 14:58:33 +0100 | [diff] [blame] | 139 | #ifdef MCUBOOT_IMAGE_NUMBER |
| 140 | #define BOOT_IMAGE_NUMBER MCUBOOT_IMAGE_NUMBER |
| 141 | #else |
| 142 | #define BOOT_IMAGE_NUMBER 1 |
| 143 | #endif |
| 144 | |
Fabio Utzig | abec073 | 2019-07-31 08:40:22 -0300 | [diff] [blame^] | 145 | _Static_assert(BOOT_IMAGE_NUMBER > 0, "Invalid value for BOOT_IMAGE_NUMBER"); |
| 146 | |
Marti Bolivar | f9bfddd | 2018-04-24 14:28:33 -0400 | [diff] [blame] | 147 | #define BOOT_MAX_IMG_SECTORS MCUBOOT_MAX_IMG_SECTORS |
Fabio Utzig | a1fae67 | 2018-03-30 10:52:38 -0300 | [diff] [blame] | 148 | |
| 149 | /* |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 150 | * Extract the swap type and image number from image trailers's swap_info |
| 151 | * filed. |
| 152 | */ |
| 153 | #define BOOT_GET_SWAP_TYPE(swap_info) ((swap_info) & 0x0F) |
| 154 | #define BOOT_GET_IMAGE_NUM(swap_info) ((swap_info) >> 4) |
| 155 | |
| 156 | /* Construct the swap_info field from swap type and image number */ |
| 157 | #define BOOT_SET_SWAP_INFO(swap_info, image, type) { \ |
| 158 | assert((image) < 0xF); \ |
| 159 | assert((type) < 0xF); \ |
| 160 | (swap_info) = (image) << 4 \ |
| 161 | | (type); \ |
| 162 | } |
| 163 | |
| 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 | |
| 175 | /** Number of image slots in flash; currently limited to two. */ |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 176 | #define BOOT_NUM_SLOTS 2 |
Fabio Utzig | a1fae67 | 2018-03-30 10:52:38 -0300 | [diff] [blame] | 177 | |
| 178 | /** Maximum number of image sectors supported by the bootloader. */ |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 179 | #define BOOT_STATUS_STATE_COUNT 3 |
| 180 | #define BOOT_STATUS_MAX_ENTRIES BOOT_MAX_IMG_SECTORS |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 181 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 182 | #define BOOT_PRIMARY_SLOT 0 |
| 183 | #define BOOT_SECONDARY_SLOT 1 |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 184 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 185 | #define BOOT_STATUS_SOURCE_NONE 0 |
| 186 | #define BOOT_STATUS_SOURCE_SCRATCH 1 |
| 187 | #define BOOT_STATUS_SOURCE_PRIMARY_SLOT 2 |
| 188 | |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 189 | extern const uint32_t BOOT_MAGIC_SZ; |
| 190 | |
Marti Bolivar | c50926f | 2017-06-14 09:35:40 -0400 | [diff] [blame] | 191 | /** |
| 192 | * Compatibility shim for flash sector type. |
| 193 | * |
| 194 | * This can be deleted when flash_area_to_sectors() is removed. |
| 195 | */ |
| 196 | #ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS |
| 197 | typedef struct flash_sector boot_sector_t; |
| 198 | #else |
| 199 | typedef struct flash_area boot_sector_t; |
| 200 | #endif |
| 201 | |
Marti Bolivar | 9b1f8bb | 2017-06-12 15:24:13 -0400 | [diff] [blame] | 202 | /** Private state maintained during boot. */ |
| 203 | struct boot_loader_state { |
| 204 | struct { |
| 205 | struct image_header hdr; |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 206 | const struct flash_area *area; |
Marti Bolivar | c50926f | 2017-06-14 09:35:40 -0400 | [diff] [blame] | 207 | boot_sector_t *sectors; |
Marti Bolivar | 8489865 | 2017-06-13 17:20:22 -0400 | [diff] [blame] | 208 | size_t num_sectors; |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 209 | } imgs[BOOT_IMAGE_NUMBER][BOOT_NUM_SLOTS]; |
Marti Bolivar | 9b1f8bb | 2017-06-12 15:24:13 -0400 | [diff] [blame] | 210 | |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 211 | struct { |
| 212 | const struct flash_area *area; |
| 213 | boot_sector_t *sectors; |
| 214 | size_t num_sectors; |
| 215 | } scratch; |
Marti Bolivar | 9b1f8bb | 2017-06-12 15:24:13 -0400 | [diff] [blame] | 216 | |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 217 | uint8_t swap_type[BOOT_IMAGE_NUMBER]; |
Marti Bolivar | 9b1f8bb | 2017-06-12 15:24:13 -0400 | [diff] [blame] | 218 | uint8_t write_sz; |
| 219 | }; |
| 220 | |
Fabio Utzig | 1a927dd | 2017-12-05 10:30:26 -0200 | [diff] [blame] | 221 | int bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, |
| 222 | size_t slen, uint8_t key_id); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 223 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 224 | int boot_magic_compatible_check(uint8_t tbl_val, uint8_t val); |
Christopher Collins | 2adef70 | 2019-05-22 14:37:31 -0700 | [diff] [blame] | 225 | uint32_t boot_trailer_sz(uint8_t min_write_sz); |
Fabio Utzig | 4cee4f7 | 2017-05-22 10:59:57 -0400 | [diff] [blame] | 226 | int boot_status_entries(const struct flash_area *fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 227 | uint32_t boot_status_off(const struct flash_area *fap); |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 228 | uint32_t boot_swap_info_off(const struct flash_area *fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 229 | int boot_read_swap_state(const struct flash_area *fap, |
| 230 | struct boot_swap_state *state); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 231 | int boot_read_swap_state_by_id(int flash_area_id, |
| 232 | struct boot_swap_state *state); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 233 | int boot_write_magic(const struct flash_area *fap); |
| 234 | int boot_write_status(struct boot_status *bs); |
| 235 | int boot_schedule_test_swap(void); |
| 236 | int boot_write_copy_done(const struct flash_area *fap); |
| 237 | int boot_write_image_ok(const struct flash_area *fap); |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 238 | int boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type, |
| 239 | uint8_t image_num); |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 240 | int boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size); |
| 241 | int boot_read_swap_size(uint32_t *swap_size); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 242 | #ifdef MCUBOOT_ENC_IMAGES |
| 243 | int boot_write_enc_key(const struct flash_area *fap, uint8_t slot, |
| 244 | const uint8_t *enckey); |
| 245 | int boot_read_enc_key(uint8_t slot, uint8_t *enckey); |
| 246 | #endif |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 247 | #if (BOOT_IMAGE_NUMBER > 1) |
| 248 | int boot_is_version_sufficient(struct image_version *req, |
| 249 | struct image_version *ver); |
| 250 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 251 | |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 252 | /* |
| 253 | * Accessors for the contents of struct boot_loader_state. |
| 254 | */ |
| 255 | |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 256 | /* These are macros so they can be used as lvalues. */ |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 257 | #define BOOT_IMG(state, slot) ((state)->imgs[current_image][(slot)]) |
| 258 | #define BOOT_IMG_AREA(state, slot) (BOOT_IMG(state, slot).area) |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 259 | #define BOOT_SCRATCH_AREA(state) ((state)->scratch.area) |
Marti Bolivar | e10a739 | 2017-06-14 16:20:07 -0400 | [diff] [blame] | 260 | #define BOOT_WRITE_SZ(state) ((state)->write_sz) |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 261 | #define BOOT_SWAP_TYPE(state) ((state)->swap_type[current_image]) |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 262 | |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 263 | static inline struct image_header* |
| 264 | boot_img_hdr(struct boot_loader_state *state, size_t slot) |
| 265 | { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 266 | return &BOOT_IMG(state, slot).hdr; |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 267 | } |
| 268 | |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 269 | static inline size_t |
| 270 | boot_img_num_sectors(struct boot_loader_state *state, size_t slot) |
| 271 | { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 272 | return BOOT_IMG(state, slot).num_sectors; |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 273 | } |
| 274 | |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 275 | static inline size_t |
| 276 | boot_scratch_num_sectors(struct boot_loader_state *state) |
| 277 | { |
| 278 | return state->scratch.num_sectors; |
| 279 | } |
| 280 | |
Marti Bolivar | 135b8f6 | 2017-06-13 17:22:13 -0400 | [diff] [blame] | 281 | /* |
| 282 | * Offset of the slot from the beginning of the flash device. |
| 283 | */ |
| 284 | static inline uint32_t |
| 285 | boot_img_slot_off(struct boot_loader_state *state, size_t slot) |
| 286 | { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 287 | return BOOT_IMG(state, slot).area->fa_off; |
Marti Bolivar | 135b8f6 | 2017-06-13 17:22:13 -0400 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | static inline size_t boot_scratch_area_size(struct boot_loader_state *state) |
| 291 | { |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 292 | return BOOT_SCRATCH_AREA(state)->fa_size; |
Marti Bolivar | 135b8f6 | 2017-06-13 17:22:13 -0400 | [diff] [blame] | 293 | } |
| 294 | |
Marti Bolivar | c50926f | 2017-06-14 09:35:40 -0400 | [diff] [blame] | 295 | #ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS |
| 296 | |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 297 | static inline size_t |
| 298 | boot_img_sector_size(struct boot_loader_state *state, |
| 299 | size_t slot, size_t sector) |
| 300 | { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 301 | return BOOT_IMG(state, slot).sectors[sector].fa_size; |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 302 | } |
| 303 | |
Marti Bolivar | ea08887 | 2017-06-12 17:10:49 -0400 | [diff] [blame] | 304 | /* |
| 305 | * Offset of the sector from the beginning of the image, NOT the flash |
| 306 | * device. |
| 307 | */ |
| 308 | static inline uint32_t |
| 309 | boot_img_sector_off(struct boot_loader_state *state, size_t slot, |
| 310 | size_t sector) |
| 311 | { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 312 | return BOOT_IMG(state, slot).sectors[sector].fa_off - |
| 313 | BOOT_IMG(state, slot).sectors[0].fa_off; |
Marti Bolivar | ea08887 | 2017-06-12 17:10:49 -0400 | [diff] [blame] | 314 | } |
| 315 | |
Marti Bolivar | cca28a9 | 2017-06-12 16:52:22 -0400 | [diff] [blame] | 316 | static inline int |
| 317 | boot_initialize_area(struct boot_loader_state *state, int flash_area) |
| 318 | { |
Marti Bolivar | cca28a9 | 2017-06-12 16:52:22 -0400 | [diff] [blame] | 319 | int num_sectors = BOOT_MAX_IMG_SECTORS; |
Marti Bolivar | cca28a9 | 2017-06-12 16:52:22 -0400 | [diff] [blame] | 320 | int rc; |
| 321 | |
David Vincze | b75c12a | 2019-03-22 14:58:33 +0100 | [diff] [blame] | 322 | if (flash_area == FLASH_AREA_IMAGE_PRIMARY) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 323 | rc = flash_area_to_sectors(flash_area, &num_sectors, |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 324 | BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors); |
| 325 | BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors = (size_t)num_sectors; |
David Vincze | b75c12a | 2019-03-22 14:58:33 +0100 | [diff] [blame] | 326 | |
| 327 | } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 328 | rc = flash_area_to_sectors(flash_area, &num_sectors, |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 329 | BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors); |
| 330 | BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors = (size_t)num_sectors; |
David Vincze | b75c12a | 2019-03-22 14:58:33 +0100 | [diff] [blame] | 331 | |
| 332 | } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 333 | rc = flash_area_to_sectors(flash_area, &num_sectors, |
| 334 | state->scratch.sectors); |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 335 | state->scratch.num_sectors = (size_t)num_sectors; |
David Vincze | b75c12a | 2019-03-22 14:58:33 +0100 | [diff] [blame] | 336 | } else { |
Marti Bolivar | cca28a9 | 2017-06-12 16:52:22 -0400 | [diff] [blame] | 337 | return BOOT_EFLASH; |
| 338 | } |
| 339 | |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 340 | return rc; |
Marti Bolivar | cca28a9 | 2017-06-12 16:52:22 -0400 | [diff] [blame] | 341 | } |
| 342 | |
Marti Bolivar | c50926f | 2017-06-14 09:35:40 -0400 | [diff] [blame] | 343 | #else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */ |
| 344 | |
| 345 | static inline size_t |
| 346 | boot_img_sector_size(struct boot_loader_state *state, |
| 347 | size_t slot, size_t sector) |
| 348 | { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 349 | return BOOT_IMG(state, slot).sectors[sector].fs_size; |
Marti Bolivar | c50926f | 2017-06-14 09:35:40 -0400 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | static inline uint32_t |
| 353 | boot_img_sector_off(struct boot_loader_state *state, size_t slot, |
| 354 | size_t sector) |
| 355 | { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 356 | return BOOT_IMG(state, slot).sectors[sector].fs_off - |
| 357 | BOOT_IMG(state, slot).sectors[0].fs_off; |
Marti Bolivar | c50926f | 2017-06-14 09:35:40 -0400 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | static inline int |
| 361 | boot_initialize_area(struct boot_loader_state *state, int flash_area) |
| 362 | { |
| 363 | uint32_t num_sectors; |
| 364 | struct flash_sector *out_sectors; |
| 365 | size_t *out_num_sectors; |
| 366 | int rc; |
| 367 | |
David Vincze | b75c12a | 2019-03-22 14:58:33 +0100 | [diff] [blame] | 368 | num_sectors = BOOT_MAX_IMG_SECTORS; |
| 369 | |
| 370 | if (flash_area == FLASH_AREA_IMAGE_PRIMARY) { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 371 | out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors; |
| 372 | out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors; |
David Vincze | b75c12a | 2019-03-22 14:58:33 +0100 | [diff] [blame] | 373 | } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY) { |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 374 | out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors; |
| 375 | out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors; |
David Vincze | b75c12a | 2019-03-22 14:58:33 +0100 | [diff] [blame] | 376 | } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) { |
Fabio Utzig | 2bd980a | 2018-11-26 10:38:17 -0200 | [diff] [blame] | 377 | out_sectors = state->scratch.sectors; |
| 378 | out_num_sectors = &state->scratch.num_sectors; |
David Vincze | b75c12a | 2019-03-22 14:58:33 +0100 | [diff] [blame] | 379 | } else { |
| 380 | return BOOT_EFLASH; |
Marti Bolivar | c50926f | 2017-06-14 09:35:40 -0400 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors); |
| 384 | if (rc != 0) { |
| 385 | return rc; |
| 386 | } |
| 387 | *out_num_sectors = num_sectors; |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | #endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */ |
| 392 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 393 | #ifdef __cplusplus |
| 394 | } |
| 395 | #endif |
| 396 | |
| 397 | #endif |