Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [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 | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 20 | /* |
| 21 | * Original code taken from mcuboot project at: |
| 22 | * https://github.com/JuulLabs-OSS/mcuboot |
| 23 | * Git SHA of the original version: 178be54bd6e5f035cc60e98205535682acd26e64 |
| 24 | * Modifications are Copyright (c) 2018-2019 Arm Limited. |
| 25 | */ |
| 26 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 27 | #ifndef H_BOOTUTIL_PRIV_ |
| 28 | #define H_BOOTUTIL_PRIV_ |
| 29 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 30 | #include "flash_map/flash_map.h" |
| 31 | #include "bootutil/image.h" |
Mate Toth-Pal | a76e2ab | 2018-05-31 15:43:01 +0200 | [diff] [blame] | 32 | #include "flash_layout.h" |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 33 | |
| 34 | #ifdef __cplusplus |
| 35 | extern "C" { |
| 36 | #endif |
| 37 | |
| 38 | struct flash_area; |
| 39 | |
| 40 | #define BOOT_EFLASH 1 |
| 41 | #define BOOT_EFILE 2 |
| 42 | #define BOOT_EBADIMAGE 3 |
| 43 | #define BOOT_EBADVECT 4 |
| 44 | #define BOOT_EBADSTATUS 5 |
| 45 | #define BOOT_ENOMEM 6 |
| 46 | #define BOOT_EBADARGS 7 |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame^] | 47 | #define BOOT_EBADMAGIC 8 |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 48 | |
| 49 | #define BOOT_TMPBUF_SZ 256 |
| 50 | |
| 51 | /* |
| 52 | * Maintain state of copy progress. |
| 53 | */ |
| 54 | struct boot_status { |
| 55 | uint32_t idx; /* Which area we're operating on */ |
| 56 | uint8_t state; /* Which part of the swapping process are we at */ |
| 57 | uint8_t use_scratch; /* Are status bytes ever written to scratch? */ |
| 58 | uint32_t swap_size; /* Total size of swapped image */ |
| 59 | }; |
| 60 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 61 | #define BOOT_MAGIC_GOOD 1 |
| 62 | #define BOOT_MAGIC_BAD 2 |
| 63 | #define BOOT_MAGIC_UNSET 3 |
| 64 | #define BOOT_MAGIC_ANY 4 /* NOTE: control only, not dependent on sector */ |
| 65 | |
| 66 | /* |
| 67 | * NOTE: leave BOOT_FLAG_SET equal to one, this is written to flash! |
| 68 | */ |
| 69 | #define BOOT_FLAG_SET 1 |
| 70 | #define BOOT_FLAG_BAD 2 |
| 71 | #define BOOT_FLAG_UNSET 3 |
| 72 | #define BOOT_FLAG_ANY 4 /* NOTE: control only, not dependent on sector */ |
| 73 | |
| 74 | #define BOOT_STATUS_IDX_0 1 |
| 75 | |
| 76 | #define BOOT_STATUS_STATE_0 1 |
| 77 | #define BOOT_STATUS_STATE_1 2 |
| 78 | #define BOOT_STATUS_STATE_2 3 |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 79 | |
| 80 | /** |
| 81 | * End-of-image slot structure. |
| 82 | * |
| 83 | * 0 1 2 3 |
| 84 | * 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 |
| 85 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 86 | * ~ ~ |
| 87 | * ~ Swap status (variable, aligned) ~ |
| 88 | * ~ ~ |
| 89 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 90 | * | Swap size | |
| 91 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 92 | * ~ padding with erased val (MAX ALIGN - 4) ~ |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 93 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 94 | * | Copy done | padding with erased val (MAX ALIGN - 1) ~ |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 95 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 96 | * | Image OK | padding with erased val (MAX ALIGN - 1) ~ |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 97 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 98 | * ~ MAGIC (16 octets) ~ |
| 99 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 100 | */ |
| 101 | |
| 102 | extern const uint32_t boot_img_magic[4]; |
| 103 | |
| 104 | struct boot_swap_state { |
| 105 | uint8_t magic; /* One of the BOOT_MAGIC_[...] values. */ |
| 106 | uint8_t copy_done; |
| 107 | uint8_t image_ok; |
| 108 | }; |
| 109 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 110 | /* |
| 111 | * The current flashmap API does not check the amount of space allocated when |
| 112 | * loading sector data from the flash device, allowing for smaller counts here |
| 113 | * would most surely incur in overruns. |
| 114 | * |
| 115 | * TODO: make flashmap API receive the current sector array size. |
| 116 | */ |
| 117 | #if BOOT_MAX_IMG_SECTORS < 32 |
| 118 | #error "Too few sectors, please increase BOOT_MAX_IMG_SECTORS to at least 32" |
| 119 | #endif |
| 120 | |
| 121 | /** Number of image slots in flash; currently limited to two. */ |
| 122 | #define BOOT_NUM_SLOTS 2 |
| 123 | |
| 124 | /** Maximum number of image sectors supported by the bootloader. */ |
| 125 | #define BOOT_STATUS_STATE_COUNT 3 |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 126 | |
| 127 | #define BOOT_STATUS_SOURCE_NONE 0 |
| 128 | #define BOOT_STATUS_SOURCE_SCRATCH 1 |
| 129 | #define BOOT_STATUS_SOURCE_SLOT0 2 |
| 130 | |
| 131 | #define BOOT_FLAG_IMAGE_OK 0 |
| 132 | #define BOOT_FLAG_COPY_DONE 1 |
| 133 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 134 | extern const uint32_t BOOT_MAGIC_SZ; |
| 135 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 136 | /** |
| 137 | * Compatibility shim for flash sector type. |
| 138 | * |
| 139 | * This can be deleted when flash_area_to_sectors() is removed. |
| 140 | */ |
| 141 | #ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS |
| 142 | typedef struct flash_sector boot_sector_t; |
| 143 | #else |
| 144 | typedef struct flash_area boot_sector_t; |
| 145 | #endif |
| 146 | |
| 147 | /** Private state maintained during boot. */ |
| 148 | struct boot_loader_state { |
| 149 | struct { |
| 150 | struct image_header hdr; |
| 151 | const struct flash_area *area; |
| 152 | boot_sector_t *sectors; |
| 153 | size_t num_sectors; |
| 154 | } imgs[BOOT_NUM_SLOTS]; |
| 155 | |
| 156 | const struct flash_area *scratch_area; |
| 157 | |
| 158 | uint8_t write_sz; |
| 159 | }; |
| 160 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 161 | int bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, |
| 162 | size_t slen, uint8_t key_id); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 163 | |
| 164 | uint32_t boot_slots_trailer_sz(uint8_t min_write_sz); |
| 165 | int boot_status_entries(const struct flash_area *fap); |
| 166 | uint32_t boot_status_off(const struct flash_area *fap); |
| 167 | int boot_read_swap_state(const struct flash_area *fap, |
| 168 | struct boot_swap_state *state); |
| 169 | int boot_read_swap_state_by_id(int flash_area_id, |
| 170 | struct boot_swap_state *state); |
| 171 | int boot_write_magic(const struct flash_area *fap); |
| 172 | int boot_write_status(struct boot_status *bs); |
| 173 | int boot_schedule_test_swap(void); |
| 174 | int boot_write_copy_done(const struct flash_area *fap); |
| 175 | int boot_write_image_ok(const struct flash_area *fap); |
| 176 | int boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size); |
| 177 | int boot_read_swap_size(uint32_t *swap_size); |
| 178 | |
| 179 | /* |
| 180 | * Accessors for the contents of struct boot_loader_state. |
| 181 | */ |
| 182 | |
| 183 | /* These are macros so they can be used as lvalues. */ |
| 184 | #define BOOT_IMG_AREA(state, slot) ((state)->imgs[(slot)].area) |
| 185 | #define BOOT_SCRATCH_AREA(state) ((state)->scratch_area) |
| 186 | #define BOOT_WRITE_SZ(state) ((state)->write_sz) |
| 187 | |
| 188 | static inline struct image_header* |
| 189 | boot_img_hdr(struct boot_loader_state *state, size_t slot) |
| 190 | { |
| 191 | return &state->imgs[slot].hdr; |
| 192 | } |
| 193 | |
| 194 | static inline uint8_t |
| 195 | boot_img_fa_device_id(struct boot_loader_state *state, size_t slot) |
| 196 | { |
| 197 | return state->imgs[slot].area->fa_device_id; |
| 198 | } |
| 199 | |
| 200 | static inline uint8_t |
| 201 | boot_scratch_fa_device_id(struct boot_loader_state *state) |
| 202 | { |
| 203 | return state->scratch_area->fa_device_id; |
| 204 | } |
| 205 | |
| 206 | static inline size_t |
| 207 | boot_img_num_sectors(struct boot_loader_state *state, size_t slot) |
| 208 | { |
| 209 | return state->imgs[slot].num_sectors; |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | * Offset of the slot from the beginning of the flash device. |
| 214 | */ |
| 215 | static inline uint32_t |
| 216 | boot_img_slot_off(struct boot_loader_state *state, size_t slot) |
| 217 | { |
| 218 | return state->imgs[slot].area->fa_off; |
| 219 | } |
| 220 | |
| 221 | static inline size_t boot_scratch_area_size(struct boot_loader_state *state) |
| 222 | { |
| 223 | return state->scratch_area->fa_size; |
| 224 | } |
| 225 | |
| 226 | #ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS |
| 227 | |
| 228 | static inline size_t |
| 229 | boot_img_sector_size(struct boot_loader_state *state, |
| 230 | size_t slot, size_t sector) |
| 231 | { |
| 232 | return state->imgs[slot].sectors[sector].fa_size; |
| 233 | } |
| 234 | |
| 235 | /* |
| 236 | * Offset of the sector from the beginning of the image, NOT the flash |
| 237 | * device. |
| 238 | */ |
| 239 | static inline uint32_t |
| 240 | boot_img_sector_off(struct boot_loader_state *state, size_t slot, |
| 241 | size_t sector) |
| 242 | { |
| 243 | return state->imgs[slot].sectors[sector].fa_off - |
| 244 | state->imgs[slot].sectors[0].fa_off; |
| 245 | } |
| 246 | |
| 247 | static inline int |
| 248 | boot_initialize_area(struct boot_loader_state *state, int flash_area) |
| 249 | { |
| 250 | int num_sectors = BOOT_MAX_IMG_SECTORS; |
| 251 | size_t slot; |
| 252 | int rc; |
| 253 | |
| 254 | switch (flash_area) { |
| 255 | case FLASH_AREA_IMAGE_0: |
| 256 | slot = 0; |
| 257 | break; |
| 258 | case FLASH_AREA_IMAGE_1: |
| 259 | slot = 1; |
| 260 | break; |
| 261 | default: |
| 262 | return BOOT_EFLASH; |
| 263 | } |
| 264 | |
| 265 | rc = flash_area_to_sectors(flash_area, &num_sectors, |
| 266 | state->imgs[slot].sectors); |
| 267 | if (rc != 0) { |
| 268 | return rc; |
| 269 | } |
| 270 | state->imgs[slot].num_sectors = (size_t)num_sectors; |
| 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | #else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */ |
| 275 | |
| 276 | static inline size_t |
| 277 | boot_img_sector_size(struct boot_loader_state *state, |
| 278 | size_t slot, size_t sector) |
| 279 | { |
| 280 | return state->imgs[slot].sectors[sector].fs_size; |
| 281 | } |
| 282 | |
| 283 | static inline uint32_t |
| 284 | boot_img_sector_off(struct boot_loader_state *state, size_t slot, |
| 285 | size_t sector) |
| 286 | { |
| 287 | return state->imgs[slot].sectors[sector].fs_off - |
| 288 | state->imgs[slot].sectors[0].fs_off; |
| 289 | } |
| 290 | |
| 291 | static inline int |
| 292 | boot_initialize_area(struct boot_loader_state *state, int flash_area) |
| 293 | { |
| 294 | uint32_t num_sectors; |
| 295 | struct flash_sector *out_sectors; |
| 296 | size_t *out_num_sectors; |
| 297 | int rc; |
| 298 | |
| 299 | switch (flash_area) { |
| 300 | case FLASH_AREA_IMAGE_0: |
| 301 | num_sectors = BOOT_MAX_IMG_SECTORS; |
| 302 | out_sectors = state->imgs[0].sectors; |
| 303 | out_num_sectors = &state->imgs[0].num_sectors; |
| 304 | break; |
| 305 | case FLASH_AREA_IMAGE_1: |
| 306 | num_sectors = BOOT_MAX_IMG_SECTORS; |
| 307 | out_sectors = state->imgs[1].sectors; |
| 308 | out_num_sectors = &state->imgs[1].num_sectors; |
| 309 | break; |
| 310 | default: |
| 311 | return -1; |
| 312 | } |
| 313 | |
| 314 | rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors); |
| 315 | if (rc != 0) { |
| 316 | return rc; |
| 317 | } |
| 318 | *out_num_sectors = num_sectors; |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | #endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */ |
| 323 | |
| 324 | #ifdef __cplusplus |
| 325 | } |
| 326 | #endif |
| 327 | |
| 328 | #endif |