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 | |
| 20 | #ifndef H_BOOTUTIL_PRIV_ |
| 21 | #define H_BOOTUTIL_PRIV_ |
| 22 | |
Marti Bolivar | cca28a9 | 2017-06-12 16:52:22 -0400 | [diff] [blame] | 23 | #include "sysflash/sysflash.h" |
Marti Bolivar | 9b1f8bb | 2017-06-12 15:24:13 -0400 | [diff] [blame] | 24 | #include "flash_map/flash_map.h" |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 25 | #include "bootutil/image.h" |
| 26 | |
| 27 | #ifdef __cplusplus |
| 28 | extern "C" { |
| 29 | #endif |
| 30 | |
Fabio Utzig | 57c40f7 | 2017-12-12 21:48:30 -0200 | [diff] [blame^] | 31 | #ifdef __BOOTSIM__ |
| 32 | #include "bootsim.h" |
| 33 | #else |
| 34 | #define ASSERT assert |
| 35 | #endif |
| 36 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 37 | struct flash_area; |
| 38 | |
| 39 | #define BOOT_EFLASH 1 |
| 40 | #define BOOT_EFILE 2 |
| 41 | #define BOOT_EBADIMAGE 3 |
| 42 | #define BOOT_EBADVECT 4 |
| 43 | #define BOOT_EBADSTATUS 5 |
| 44 | #define BOOT_ENOMEM 6 |
| 45 | #define BOOT_EBADARGS 7 |
| 46 | |
| 47 | #define BOOT_TMPBUF_SZ 256 |
| 48 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 49 | /* |
| 50 | * Maintain state of copy progress. |
| 51 | */ |
| 52 | struct boot_status { |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 53 | uint32_t idx; /* Which area we're operating on */ |
| 54 | uint8_t state; /* Which part of the swapping process are we at */ |
| 55 | uint8_t use_scratch; /* Are status bytes ever written to scratch? */ |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 56 | uint32_t swap_size; /* Total size of swapped image */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | #define BOOT_MAGIC_GOOD 1 |
| 60 | #define BOOT_MAGIC_BAD 2 |
| 61 | #define BOOT_MAGIC_UNSET 3 |
| 62 | |
| 63 | /** |
| 64 | * End-of-image slot structure. |
| 65 | * |
| 66 | * 0 1 2 3 |
| 67 | * 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 |
| 68 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 69 | * ~ ~ |
| 70 | * ~ Swap status (variable, aligned) ~ |
| 71 | * ~ ~ |
| 72 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 73 | * | Swap size | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 74 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 75 | * ~ 0xff padding (MAX ALIGN - 4) ~ |
| 76 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 77 | * | Copy done | 0xff padding (MAX ALIGN - 1) ~ |
| 78 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 79 | * | Image OK | 0xff padding (MAX ALIGN - 1) ~ |
| 80 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 81 | * ~ MAGIC (16 octets) ~ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 82 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 83 | */ |
| 84 | |
| 85 | extern const uint32_t boot_img_magic[4]; |
| 86 | |
| 87 | struct boot_swap_state { |
| 88 | uint8_t magic; /* One of the BOOT_MAGIC_[...] values. */ |
| 89 | uint8_t copy_done; |
| 90 | uint8_t image_ok; |
| 91 | }; |
| 92 | |
| 93 | #define BOOT_STATUS_STATE_COUNT 3 |
| 94 | #define BOOT_STATUS_MAX_ENTRIES 128 |
| 95 | |
| 96 | #define BOOT_STATUS_SOURCE_NONE 0 |
| 97 | #define BOOT_STATUS_SOURCE_SCRATCH 1 |
| 98 | #define BOOT_STATUS_SOURCE_SLOT0 2 |
| 99 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 100 | #define BOOT_FLAG_IMAGE_OK 0 |
| 101 | #define BOOT_FLAG_COPY_DONE 1 |
| 102 | |
Fabio Utzig | de8a38a | 2017-05-23 11:15:01 -0400 | [diff] [blame] | 103 | #define BOOT_FLAG_SET 0x01 |
| 104 | #define BOOT_FLAG_UNSET 0xff |
Fabio Utzig | ba49f84 | 2017-05-22 10:52:38 -0400 | [diff] [blame] | 105 | |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 106 | extern const uint32_t BOOT_MAGIC_SZ; |
| 107 | |
Marti Bolivar | 9b1f8bb | 2017-06-12 15:24:13 -0400 | [diff] [blame] | 108 | /** Number of image slots in flash; currently limited to two. */ |
| 109 | #define BOOT_NUM_SLOTS 2 |
| 110 | |
Marti Bolivar | cca28a9 | 2017-06-12 16:52:22 -0400 | [diff] [blame] | 111 | /** Maximum number of image sectors supported by the bootloader. */ |
| 112 | #define BOOT_MAX_IMG_SECTORS 120 |
| 113 | |
Marti Bolivar | c50926f | 2017-06-14 09:35:40 -0400 | [diff] [blame] | 114 | /** |
| 115 | * Compatibility shim for flash sector type. |
| 116 | * |
| 117 | * This can be deleted when flash_area_to_sectors() is removed. |
| 118 | */ |
| 119 | #ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS |
| 120 | typedef struct flash_sector boot_sector_t; |
| 121 | #else |
| 122 | typedef struct flash_area boot_sector_t; |
| 123 | #endif |
| 124 | |
Marti Bolivar | 9b1f8bb | 2017-06-12 15:24:13 -0400 | [diff] [blame] | 125 | /** Private state maintained during boot. */ |
| 126 | struct boot_loader_state { |
| 127 | struct { |
| 128 | struct image_header hdr; |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 129 | const struct flash_area *area; |
Marti Bolivar | c50926f | 2017-06-14 09:35:40 -0400 | [diff] [blame] | 130 | boot_sector_t *sectors; |
Marti Bolivar | 8489865 | 2017-06-13 17:20:22 -0400 | [diff] [blame] | 131 | size_t num_sectors; |
Marti Bolivar | 9b1f8bb | 2017-06-12 15:24:13 -0400 | [diff] [blame] | 132 | } imgs[BOOT_NUM_SLOTS]; |
| 133 | |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 134 | const struct flash_area *scratch_area; |
Marti Bolivar | 9b1f8bb | 2017-06-12 15:24:13 -0400 | [diff] [blame] | 135 | |
| 136 | uint8_t write_sz; |
| 137 | }; |
| 138 | |
Fabio Utzig | 1a927dd | 2017-12-05 10:30:26 -0200 | [diff] [blame] | 139 | int bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, |
| 140 | size_t slen, uint8_t key_id); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 141 | |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 142 | uint32_t boot_slots_trailer_sz(uint8_t min_write_sz); |
Fabio Utzig | 4cee4f7 | 2017-05-22 10:59:57 -0400 | [diff] [blame] | 143 | int boot_status_entries(const struct flash_area *fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 144 | uint32_t boot_status_off(const struct flash_area *fap); |
| 145 | int boot_read_swap_state(const struct flash_area *fap, |
| 146 | struct boot_swap_state *state); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 147 | int boot_read_swap_state_by_id(int flash_area_id, |
| 148 | struct boot_swap_state *state); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 149 | int boot_write_magic(const struct flash_area *fap); |
| 150 | int boot_write_status(struct boot_status *bs); |
| 151 | int boot_schedule_test_swap(void); |
| 152 | int boot_write_copy_done(const struct flash_area *fap); |
| 153 | int boot_write_image_ok(const struct flash_area *fap); |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 154 | int boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size); |
| 155 | int boot_read_swap_size(uint32_t *swap_size); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 156 | |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 157 | /* |
| 158 | * Accessors for the contents of struct boot_loader_state. |
| 159 | */ |
| 160 | |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 161 | /* These are macros so they can be used as lvalues. */ |
| 162 | #define BOOT_IMG_AREA(state, slot) ((state)->imgs[(slot)].area) |
| 163 | #define BOOT_SCRATCH_AREA(state) ((state)->scratch_area) |
Marti Bolivar | e10a739 | 2017-06-14 16:20:07 -0400 | [diff] [blame] | 164 | #define BOOT_WRITE_SZ(state) ((state)->write_sz) |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 165 | |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 166 | static inline struct image_header* |
| 167 | boot_img_hdr(struct boot_loader_state *state, size_t slot) |
| 168 | { |
| 169 | return &state->imgs[slot].hdr; |
| 170 | } |
| 171 | |
Marti Bolivar | e258715 | 2017-06-12 15:52:05 -0400 | [diff] [blame] | 172 | static inline uint8_t |
| 173 | boot_img_fa_device_id(struct boot_loader_state *state, size_t slot) |
| 174 | { |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 175 | return state->imgs[slot].area->fa_device_id; |
Marti Bolivar | e258715 | 2017-06-12 15:52:05 -0400 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | static inline uint8_t |
| 179 | boot_scratch_fa_device_id(struct boot_loader_state *state) |
| 180 | { |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 181 | return state->scratch_area->fa_device_id; |
Marti Bolivar | e258715 | 2017-06-12 15:52:05 -0400 | [diff] [blame] | 182 | } |
| 183 | |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 184 | static inline size_t |
| 185 | boot_img_num_sectors(struct boot_loader_state *state, size_t slot) |
| 186 | { |
| 187 | return state->imgs[slot].num_sectors; |
| 188 | } |
| 189 | |
Marti Bolivar | 135b8f6 | 2017-06-13 17:22:13 -0400 | [diff] [blame] | 190 | /* |
| 191 | * Offset of the slot from the beginning of the flash device. |
| 192 | */ |
| 193 | static inline uint32_t |
| 194 | boot_img_slot_off(struct boot_loader_state *state, size_t slot) |
| 195 | { |
| 196 | return state->imgs[slot].area->fa_off; |
| 197 | } |
| 198 | |
| 199 | static inline size_t boot_scratch_area_size(struct boot_loader_state *state) |
| 200 | { |
| 201 | return state->scratch_area->fa_size; |
| 202 | } |
| 203 | |
Marti Bolivar | c50926f | 2017-06-14 09:35:40 -0400 | [diff] [blame] | 204 | #ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS |
| 205 | |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 206 | static inline size_t |
| 207 | boot_img_sector_size(struct boot_loader_state *state, |
| 208 | size_t slot, size_t sector) |
| 209 | { |
| 210 | return state->imgs[slot].sectors[sector].fa_size; |
| 211 | } |
| 212 | |
Marti Bolivar | ea08887 | 2017-06-12 17:10:49 -0400 | [diff] [blame] | 213 | /* |
| 214 | * Offset of the sector from the beginning of the image, NOT the flash |
| 215 | * device. |
| 216 | */ |
| 217 | static inline uint32_t |
| 218 | boot_img_sector_off(struct boot_loader_state *state, size_t slot, |
| 219 | size_t sector) |
| 220 | { |
| 221 | return state->imgs[slot].sectors[sector].fa_off - |
| 222 | state->imgs[slot].sectors[0].fa_off; |
| 223 | } |
| 224 | |
Marti Bolivar | cca28a9 | 2017-06-12 16:52:22 -0400 | [diff] [blame] | 225 | static inline int |
| 226 | boot_initialize_area(struct boot_loader_state *state, int flash_area) |
| 227 | { |
Marti Bolivar | cca28a9 | 2017-06-12 16:52:22 -0400 | [diff] [blame] | 228 | int num_sectors = BOOT_MAX_IMG_SECTORS; |
| 229 | size_t slot; |
| 230 | int rc; |
| 231 | |
| 232 | switch (flash_area) { |
| 233 | case FLASH_AREA_IMAGE_0: |
| 234 | slot = 0; |
| 235 | break; |
| 236 | case FLASH_AREA_IMAGE_1: |
| 237 | slot = 1; |
| 238 | break; |
Marti Bolivar | cca28a9 | 2017-06-12 16:52:22 -0400 | [diff] [blame] | 239 | default: |
| 240 | return BOOT_EFLASH; |
| 241 | } |
| 242 | |
| 243 | rc = flash_area_to_sectors(flash_area, &num_sectors, |
| 244 | state->imgs[slot].sectors); |
| 245 | if (rc != 0) { |
| 246 | return rc; |
| 247 | } |
Marti Bolivar | 8489865 | 2017-06-13 17:20:22 -0400 | [diff] [blame] | 248 | state->imgs[slot].num_sectors = (size_t)num_sectors; |
Marti Bolivar | cca28a9 | 2017-06-12 16:52:22 -0400 | [diff] [blame] | 249 | return 0; |
| 250 | } |
| 251 | |
Marti Bolivar | c50926f | 2017-06-14 09:35:40 -0400 | [diff] [blame] | 252 | #else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */ |
| 253 | |
| 254 | static inline size_t |
| 255 | boot_img_sector_size(struct boot_loader_state *state, |
| 256 | size_t slot, size_t sector) |
| 257 | { |
| 258 | return state->imgs[slot].sectors[sector].fs_size; |
| 259 | } |
| 260 | |
| 261 | static inline uint32_t |
| 262 | boot_img_sector_off(struct boot_loader_state *state, size_t slot, |
| 263 | size_t sector) |
| 264 | { |
| 265 | return state->imgs[slot].sectors[sector].fs_off - |
| 266 | state->imgs[slot].sectors[0].fs_off; |
| 267 | } |
| 268 | |
| 269 | static inline int |
| 270 | boot_initialize_area(struct boot_loader_state *state, int flash_area) |
| 271 | { |
| 272 | uint32_t num_sectors; |
| 273 | struct flash_sector *out_sectors; |
| 274 | size_t *out_num_sectors; |
| 275 | int rc; |
| 276 | |
| 277 | switch (flash_area) { |
| 278 | case FLASH_AREA_IMAGE_0: |
| 279 | num_sectors = BOOT_MAX_IMG_SECTORS; |
| 280 | out_sectors = state->imgs[0].sectors; |
| 281 | out_num_sectors = &state->imgs[0].num_sectors; |
| 282 | break; |
| 283 | case FLASH_AREA_IMAGE_1: |
| 284 | num_sectors = BOOT_MAX_IMG_SECTORS; |
| 285 | out_sectors = state->imgs[1].sectors; |
| 286 | out_num_sectors = &state->imgs[1].num_sectors; |
| 287 | break; |
| 288 | default: |
| 289 | return -1; |
| 290 | } |
| 291 | |
| 292 | rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors); |
| 293 | if (rc != 0) { |
| 294 | return rc; |
| 295 | } |
| 296 | *out_num_sectors = num_sectors; |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | #endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */ |
| 301 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 302 | #ifdef __cplusplus |
| 303 | } |
| 304 | #endif |
| 305 | |
| 306 | #endif |