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 | |
| 31 | struct flash_area; |
| 32 | |
| 33 | #define BOOT_EFLASH 1 |
| 34 | #define BOOT_EFILE 2 |
| 35 | #define BOOT_EBADIMAGE 3 |
| 36 | #define BOOT_EBADVECT 4 |
| 37 | #define BOOT_EBADSTATUS 5 |
| 38 | #define BOOT_ENOMEM 6 |
| 39 | #define BOOT_EBADARGS 7 |
| 40 | |
| 41 | #define BOOT_TMPBUF_SZ 256 |
| 42 | |
Fabio Utzig | 644b8d4 | 2017-04-20 07:56:05 -0300 | [diff] [blame] | 43 | #define BOOT_MAX_ALIGN 8 |
| 44 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 45 | /* |
| 46 | * Maintain state of copy progress. |
| 47 | */ |
| 48 | struct boot_status { |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 49 | uint32_t idx; /* Which area we're operating on */ |
| 50 | uint8_t state; /* Which part of the swapping process are we at */ |
| 51 | uint8_t use_scratch; /* Are status bytes ever written to scratch? */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | #define BOOT_MAGIC_GOOD 1 |
| 55 | #define BOOT_MAGIC_BAD 2 |
| 56 | #define BOOT_MAGIC_UNSET 3 |
| 57 | |
| 58 | /** |
| 59 | * End-of-image slot structure. |
| 60 | * |
| 61 | * 0 1 2 3 |
| 62 | * 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 |
| 63 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 64 | * ~ MAGIC (16 octets) ~ |
| 65 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 66 | * ~ ~ |
| 67 | * ~ Swap status (variable, aligned) ~ |
| 68 | * ~ ~ |
| 69 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 70 | * | Copy done | 0xff padding (up to min-write-sz - 1) ~ |
| 71 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 72 | * | Image OK | 0xff padding (up to min-write-sz - 1) ~ |
| 73 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 74 | */ |
| 75 | |
| 76 | extern const uint32_t boot_img_magic[4]; |
| 77 | |
| 78 | struct boot_swap_state { |
| 79 | uint8_t magic; /* One of the BOOT_MAGIC_[...] values. */ |
| 80 | uint8_t copy_done; |
| 81 | uint8_t image_ok; |
| 82 | }; |
| 83 | |
| 84 | #define BOOT_STATUS_STATE_COUNT 3 |
| 85 | #define BOOT_STATUS_MAX_ENTRIES 128 |
| 86 | |
| 87 | #define BOOT_STATUS_SOURCE_NONE 0 |
| 88 | #define BOOT_STATUS_SOURCE_SCRATCH 1 |
| 89 | #define BOOT_STATUS_SOURCE_SLOT0 2 |
| 90 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 91 | #define BOOT_FLAG_IMAGE_OK 0 |
| 92 | #define BOOT_FLAG_COPY_DONE 1 |
| 93 | |
Fabio Utzig | de8a38a | 2017-05-23 11:15:01 -0400 | [diff] [blame] | 94 | #define BOOT_FLAG_SET 0x01 |
| 95 | #define BOOT_FLAG_UNSET 0xff |
Fabio Utzig | ba49f84 | 2017-05-22 10:52:38 -0400 | [diff] [blame] | 96 | |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 97 | extern const uint32_t BOOT_MAGIC_SZ; |
| 98 | |
Marti Bolivar | 9b1f8bb | 2017-06-12 15:24:13 -0400 | [diff] [blame] | 99 | /** Number of image slots in flash; currently limited to two. */ |
| 100 | #define BOOT_NUM_SLOTS 2 |
| 101 | |
Marti Bolivar | cca28a9 | 2017-06-12 16:52:22 -0400 | [diff] [blame] | 102 | /** Maximum number of image sectors supported by the bootloader. */ |
| 103 | #define BOOT_MAX_IMG_SECTORS 120 |
| 104 | |
Marti Bolivar | 9b1f8bb | 2017-06-12 15:24:13 -0400 | [diff] [blame] | 105 | /** Private state maintained during boot. */ |
| 106 | struct boot_loader_state { |
| 107 | struct { |
| 108 | struct image_header hdr; |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 109 | const struct flash_area *area; |
Marti Bolivar | 9b1f8bb | 2017-06-12 15:24:13 -0400 | [diff] [blame] | 110 | struct flash_area *sectors; |
Marti Bolivar | 8489865 | 2017-06-13 17:20:22 -0400 | [diff] [blame] | 111 | size_t num_sectors; |
Marti Bolivar | 9b1f8bb | 2017-06-12 15:24:13 -0400 | [diff] [blame] | 112 | } imgs[BOOT_NUM_SLOTS]; |
| 113 | |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 114 | const struct flash_area *scratch_area; |
Marti Bolivar | 9b1f8bb | 2017-06-12 15:24:13 -0400 | [diff] [blame] | 115 | |
| 116 | uint8_t write_sz; |
| 117 | }; |
| 118 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 119 | int bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, int slen, |
| 120 | uint8_t key_id); |
| 121 | |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 122 | uint32_t boot_slots_trailer_sz(uint8_t min_write_sz); |
Fabio Utzig | 4cee4f7 | 2017-05-22 10:59:57 -0400 | [diff] [blame] | 123 | int boot_status_entries(const struct flash_area *fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 124 | uint32_t boot_status_off(const struct flash_area *fap); |
| 125 | int boot_read_swap_state(const struct flash_area *fap, |
| 126 | struct boot_swap_state *state); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 127 | int boot_read_swap_state_by_id(int flash_area_id, |
| 128 | struct boot_swap_state *state); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 129 | int boot_write_magic(const struct flash_area *fap); |
| 130 | int boot_write_status(struct boot_status *bs); |
| 131 | int boot_schedule_test_swap(void); |
| 132 | int boot_write_copy_done(const struct flash_area *fap); |
| 133 | int boot_write_image_ok(const struct flash_area *fap); |
| 134 | |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 135 | /* |
| 136 | * Accessors for the contents of struct boot_loader_state. |
| 137 | */ |
| 138 | |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 139 | /* These are macros so they can be used as lvalues. */ |
| 140 | #define BOOT_IMG_AREA(state, slot) ((state)->imgs[(slot)].area) |
| 141 | #define BOOT_SCRATCH_AREA(state) ((state)->scratch_area) |
Marti Bolivar | e10a739 | 2017-06-14 16:20:07 -0400 | [diff] [blame^] | 142 | #define BOOT_WRITE_SZ(state) ((state)->write_sz) |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 143 | |
Marti Bolivar | f804f62 | 2017-06-12 15:41:48 -0400 | [diff] [blame] | 144 | static inline struct image_header* |
| 145 | boot_img_hdr(struct boot_loader_state *state, size_t slot) |
| 146 | { |
| 147 | return &state->imgs[slot].hdr; |
| 148 | } |
| 149 | |
Marti Bolivar | e258715 | 2017-06-12 15:52:05 -0400 | [diff] [blame] | 150 | static inline uint8_t |
| 151 | boot_img_fa_device_id(struct boot_loader_state *state, size_t slot) |
| 152 | { |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 153 | return state->imgs[slot].area->fa_device_id; |
Marti Bolivar | e258715 | 2017-06-12 15:52:05 -0400 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | static inline uint8_t |
| 157 | boot_scratch_fa_device_id(struct boot_loader_state *state) |
| 158 | { |
Marti Bolivar | c0b4791 | 2017-06-13 17:18:09 -0400 | [diff] [blame] | 159 | return state->scratch_area->fa_device_id; |
Marti Bolivar | e258715 | 2017-06-12 15:52:05 -0400 | [diff] [blame] | 160 | } |
| 161 | |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 162 | static inline size_t |
| 163 | boot_img_num_sectors(struct boot_loader_state *state, size_t slot) |
| 164 | { |
| 165 | return state->imgs[slot].num_sectors; |
| 166 | } |
| 167 | |
Marti Bolivar | 135b8f6 | 2017-06-13 17:22:13 -0400 | [diff] [blame] | 168 | /* |
| 169 | * Offset of the slot from the beginning of the flash device. |
| 170 | */ |
| 171 | static inline uint32_t |
| 172 | boot_img_slot_off(struct boot_loader_state *state, size_t slot) |
| 173 | { |
| 174 | return state->imgs[slot].area->fa_off; |
| 175 | } |
| 176 | |
| 177 | static inline size_t boot_scratch_area_size(struct boot_loader_state *state) |
| 178 | { |
| 179 | return state->scratch_area->fa_size; |
| 180 | } |
| 181 | |
Marti Bolivar | d3269fd | 2017-06-12 16:31:12 -0400 | [diff] [blame] | 182 | static inline size_t |
| 183 | boot_img_sector_size(struct boot_loader_state *state, |
| 184 | size_t slot, size_t sector) |
| 185 | { |
| 186 | return state->imgs[slot].sectors[sector].fa_size; |
| 187 | } |
| 188 | |
Marti Bolivar | ea08887 | 2017-06-12 17:10:49 -0400 | [diff] [blame] | 189 | /* |
| 190 | * Offset of the sector from the beginning of the image, NOT the flash |
| 191 | * device. |
| 192 | */ |
| 193 | static inline uint32_t |
| 194 | boot_img_sector_off(struct boot_loader_state *state, size_t slot, |
| 195 | size_t sector) |
| 196 | { |
| 197 | return state->imgs[slot].sectors[sector].fa_off - |
| 198 | state->imgs[slot].sectors[0].fa_off; |
| 199 | } |
| 200 | |
Marti Bolivar | cca28a9 | 2017-06-12 16:52:22 -0400 | [diff] [blame] | 201 | static inline int |
| 202 | boot_initialize_area(struct boot_loader_state *state, int flash_area) |
| 203 | { |
Marti Bolivar | cca28a9 | 2017-06-12 16:52:22 -0400 | [diff] [blame] | 204 | int num_sectors = BOOT_MAX_IMG_SECTORS; |
| 205 | size_t slot; |
| 206 | int rc; |
| 207 | |
| 208 | switch (flash_area) { |
| 209 | case FLASH_AREA_IMAGE_0: |
| 210 | slot = 0; |
| 211 | break; |
| 212 | case FLASH_AREA_IMAGE_1: |
| 213 | slot = 1; |
| 214 | break; |
Marti Bolivar | cca28a9 | 2017-06-12 16:52:22 -0400 | [diff] [blame] | 215 | default: |
| 216 | return BOOT_EFLASH; |
| 217 | } |
| 218 | |
| 219 | rc = flash_area_to_sectors(flash_area, &num_sectors, |
| 220 | state->imgs[slot].sectors); |
| 221 | if (rc != 0) { |
| 222 | return rc; |
| 223 | } |
Marti Bolivar | 8489865 | 2017-06-13 17:20:22 -0400 | [diff] [blame] | 224 | state->imgs[slot].num_sectors = (size_t)num_sectors; |
Marti Bolivar | cca28a9 | 2017-06-12 16:52:22 -0400 | [diff] [blame] | 225 | return 0; |
| 226 | } |
| 227 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 228 | #ifdef __cplusplus |
| 229 | } |
| 230 | #endif |
| 231 | |
| 232 | #endif |