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