David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 1 | /* Run the boot image. */ |
| 2 | |
| 3 | #include <setjmp.h> |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | #include <bootutil/bootutil.h> |
| 8 | #include <bootutil/image.h> |
| 9 | #include "flash_map/flash_map.h" |
| 10 | |
David Brown | d2b1853 | 2017-07-12 09:51:31 -0600 | [diff] [blame] | 11 | #include "../../../boot/bootutil/src/bootutil_priv.h" |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 12 | |
Fabio Utzig | 92be3fb | 2017-12-05 08:52:53 -0200 | [diff] [blame] | 13 | #ifdef MCUBOOT_SIGN_EC256 |
| 14 | #include "../../../ext/tinycrypt/lib/include/tinycrypt/ecc_dsa.h" |
| 15 | #endif |
| 16 | |
David Brown | 54b7779 | 2017-05-05 09:40:01 -0600 | [diff] [blame] | 17 | #define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_ERROR |
David Brown | 75fd5dc | 2017-05-04 09:04:47 -0600 | [diff] [blame] | 18 | #include <bootutil/bootutil_log.h> |
| 19 | |
David Brown | bdb6db7 | 2017-07-06 10:14:37 -0600 | [diff] [blame] | 20 | extern int sim_flash_erase(uint32_t offset, uint32_t size); |
| 21 | extern int sim_flash_read(uint32_t offset, uint8_t *dest, uint32_t size); |
| 22 | extern int sim_flash_write(uint32_t offset, const uint8_t *src, uint32_t size); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 23 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 24 | static jmp_buf boot_jmpbuf; |
| 25 | int flash_counter; |
| 26 | |
| 27 | int jumped = 0; |
| 28 | |
Fabio Utzig | 92be3fb | 2017-12-05 08:52:53 -0200 | [diff] [blame] | 29 | int ecdsa256_sign_(const uint8_t *privkey, const uint8_t *hash, |
| 30 | unsigned hash_len, uint8_t *signature) |
| 31 | { |
| 32 | #ifdef MCUBOOT_SIGN_EC256 |
| 33 | return uECC_sign(privkey, hash, hash_len, signature, uECC_secp256r1()); |
| 34 | #else |
| 35 | (void)privkey; |
| 36 | (void)hash; |
| 37 | (void)hash_len; |
| 38 | (void)signature; |
| 39 | return 0; |
| 40 | #endif |
| 41 | } |
| 42 | |
David Brown | 5acda26 | 2017-01-23 15:42:19 -0700 | [diff] [blame] | 43 | uint8_t sim_flash_align = 1; |
| 44 | uint8_t flash_area_align(const struct flash_area *area) |
| 45 | { |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 46 | (void)area; |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 47 | return sim_flash_align; |
David Brown | 5acda26 | 2017-01-23 15:42:19 -0700 | [diff] [blame] | 48 | } |
| 49 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 50 | struct area { |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 51 | struct flash_area whole; |
| 52 | struct flash_area *areas; |
| 53 | uint32_t num_areas; |
| 54 | uint8_t id; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | struct area_desc { |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 58 | struct area slots[16]; |
| 59 | uint32_t num_slots; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | static struct area_desc *flash_areas; |
| 63 | |
David Brown | 7e701d8 | 2017-07-11 13:24:25 -0600 | [diff] [blame] | 64 | void *(*mbedtls_calloc)(size_t n, size_t size); |
| 65 | void (*mbedtls_free)(void *ptr); |
| 66 | |
David Brown | bdb6db7 | 2017-07-06 10:14:37 -0600 | [diff] [blame] | 67 | int invoke_boot_go(struct area_desc *adesc) |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 68 | { |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 69 | int res; |
| 70 | struct boot_rsp rsp; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 71 | |
David Brown | 7e701d8 | 2017-07-11 13:24:25 -0600 | [diff] [blame] | 72 | mbedtls_calloc = calloc; |
| 73 | mbedtls_free = free; |
| 74 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 75 | flash_areas = adesc; |
| 76 | if (setjmp(boot_jmpbuf) == 0) { |
| 77 | res = boot_go(&rsp); |
David Brown | bdb6db7 | 2017-07-06 10:14:37 -0600 | [diff] [blame] | 78 | flash_areas = NULL; |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 79 | /* printf("boot_go off: %d (0x%08x)\n", res, rsp.br_image_off); */ |
| 80 | return res; |
| 81 | } else { |
David Brown | bdb6db7 | 2017-07-06 10:14:37 -0600 | [diff] [blame] | 82 | flash_areas = NULL; |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 83 | return -0x13579; |
| 84 | } |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | int hal_flash_read(uint8_t flash_id, uint32_t address, void *dst, |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 88 | uint32_t num_bytes) |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 89 | { |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 90 | (void)flash_id; |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 91 | // printf("hal_flash_read: %d, 0x%08x (0x%x)\n", |
| 92 | // flash_id, address, num_bytes); |
David Brown | bdb6db7 | 2017-07-06 10:14:37 -0600 | [diff] [blame] | 93 | return sim_flash_read(address, dst, num_bytes); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | int hal_flash_write(uint8_t flash_id, uint32_t address, |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 97 | const void *src, int32_t num_bytes) |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 98 | { |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 99 | (void)flash_id; |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 100 | // printf("hal_flash_write: 0x%08x (0x%x)\n", address, num_bytes); |
| 101 | // fflush(stdout); |
| 102 | if (--flash_counter == 0) { |
| 103 | jumped++; |
| 104 | longjmp(boot_jmpbuf, 1); |
| 105 | } |
David Brown | bdb6db7 | 2017-07-06 10:14:37 -0600 | [diff] [blame] | 106 | return sim_flash_write(address, src, num_bytes); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | int hal_flash_erase(uint8_t flash_id, uint32_t address, |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 110 | uint32_t num_bytes) |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 111 | { |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 112 | (void)flash_id; |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 113 | // printf("hal_flash_erase: 0x%08x, (0x%x)\n", address, num_bytes); |
| 114 | // fflush(stdout); |
| 115 | if (--flash_counter == 0) { |
| 116 | jumped++; |
| 117 | longjmp(boot_jmpbuf, 1); |
| 118 | } |
David Brown | bdb6db7 | 2017-07-06 10:14:37 -0600 | [diff] [blame] | 119 | return sim_flash_erase(address, num_bytes); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | uint8_t hal_flash_align(uint8_t flash_id) |
| 123 | { |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 124 | (void)flash_id; |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 125 | return sim_flash_align; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void *os_malloc(size_t size) |
| 129 | { |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 130 | // printf("os_malloc 0x%x bytes\n", size); |
| 131 | return malloc(size); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | int flash_area_id_from_image_slot(int slot) |
| 135 | { |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 136 | return slot + 1; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | int flash_area_open(uint8_t id, const struct flash_area **area) |
| 140 | { |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 141 | uint32_t i; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 142 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 143 | for (i = 0; i < flash_areas->num_slots; i++) { |
| 144 | if (flash_areas->slots[i].id == id) |
| 145 | break; |
| 146 | } |
| 147 | if (i == flash_areas->num_slots) { |
| 148 | printf("Unsupported area\n"); |
| 149 | abort(); |
| 150 | } |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 151 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 152 | /* Unsure if this is right, just returning the first area. */ |
| 153 | *area = &flash_areas->slots[i].whole; |
| 154 | return 0; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void flash_area_close(const struct flash_area *area) |
| 158 | { |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 159 | (void)area; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /* |
| 163 | * Read/write/erase. Offset is relative from beginning of flash area. |
| 164 | */ |
| 165 | int flash_area_read(const struct flash_area *area, uint32_t off, void *dst, |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 166 | uint32_t len) |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 167 | { |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 168 | BOOT_LOG_DBG("%s: area=%d, off=%x, len=%x", |
| 169 | __func__, area->fa_id, off, len); |
| 170 | return hal_flash_read(area->fa_id, |
| 171 | area->fa_off + off, |
| 172 | dst, len); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | int flash_area_write(const struct flash_area *area, uint32_t off, const void *src, |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 176 | uint32_t len) |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 177 | { |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 178 | BOOT_LOG_DBG("%s: area=%d, off=%x, len=%x", __func__, |
| 179 | area->fa_id, off, len); |
| 180 | return hal_flash_write(area->fa_id, |
| 181 | area->fa_off + off, |
| 182 | src, len); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | int flash_area_erase(const struct flash_area *area, uint32_t off, uint32_t len) |
| 186 | { |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 187 | BOOT_LOG_DBG("%s: area=%d, off=%x, len=%x", __func__, |
| 188 | area->fa_id, off, len); |
| 189 | return hal_flash_erase(area->fa_id, |
| 190 | area->fa_off + off, |
| 191 | len); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret) |
| 195 | { |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 196 | uint32_t i; |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 197 | struct area *slot; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 198 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 199 | for (i = 0; i < flash_areas->num_slots; i++) { |
| 200 | if (flash_areas->slots[i].id == idx) |
| 201 | break; |
| 202 | } |
| 203 | if (i == flash_areas->num_slots) { |
| 204 | printf("Unsupported area\n"); |
| 205 | abort(); |
| 206 | } |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 207 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 208 | slot = &flash_areas->slots[i]; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 209 | |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 210 | if (slot->num_areas > (uint32_t)*cnt) { |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 211 | printf("Too many areas in slot\n"); |
| 212 | abort(); |
| 213 | } |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 214 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 215 | *cnt = slot->num_areas; |
| 216 | memcpy(ret, slot->areas, slot->num_areas * sizeof(struct flash_area)); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 217 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 218 | return 0; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 219 | } |
| 220 | |
David Brown | 60399f6 | 2017-05-11 10:20:34 -0600 | [diff] [blame] | 221 | int flash_area_get_sectors(int fa_id, uint32_t *count, |
| 222 | struct flash_sector *sectors) |
| 223 | { |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 224 | uint32_t i; |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 225 | struct area *slot; |
David Brown | 60399f6 | 2017-05-11 10:20:34 -0600 | [diff] [blame] | 226 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 227 | for (i = 0; i < flash_areas->num_slots; i++) { |
| 228 | if (flash_areas->slots[i].id == fa_id) |
| 229 | break; |
| 230 | } |
| 231 | if (i == flash_areas->num_slots) { |
| 232 | printf("Unsupported area\n"); |
| 233 | abort(); |
| 234 | } |
David Brown | 60399f6 | 2017-05-11 10:20:34 -0600 | [diff] [blame] | 235 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 236 | slot = &flash_areas->slots[i]; |
David Brown | 60399f6 | 2017-05-11 10:20:34 -0600 | [diff] [blame] | 237 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 238 | if (slot->num_areas > *count) { |
| 239 | printf("Too many areas in slot\n"); |
| 240 | abort(); |
| 241 | } |
David Brown | 60399f6 | 2017-05-11 10:20:34 -0600 | [diff] [blame] | 242 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 243 | for (i = 0; i < slot->num_areas; i++) { |
| 244 | sectors[i].fs_off = slot->areas[i].fa_off - |
| 245 | slot->whole.fa_off; |
| 246 | sectors[i].fs_size = slot->areas[i].fa_size; |
| 247 | } |
| 248 | *count = slot->num_areas; |
David Brown | 60399f6 | 2017-05-11 10:20:34 -0600 | [diff] [blame] | 249 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 250 | return 0; |
David Brown | 60399f6 | 2017-05-11 10:20:34 -0600 | [diff] [blame] | 251 | } |