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