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