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 | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 16 | #ifdef MCUBOOT_ENCRYPT_RSA |
| 17 | #include "mbedtls/rsa.h" |
| 18 | #include "mbedtls/asn1.h" |
| 19 | #endif |
| 20 | |
| 21 | #ifdef MCUBOOT_ENCRYPT_KW |
| 22 | #include "mbedtls/nist_kw.h" |
| 23 | #endif |
| 24 | |
David Brown | 54b7779 | 2017-05-05 09:40:01 -0600 | [diff] [blame] | 25 | #define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_ERROR |
David Brown | 75fd5dc | 2017-05-04 09:04:47 -0600 | [diff] [blame] | 26 | #include <bootutil/bootutil_log.h> |
| 27 | |
David Vincze | 6c9b416 | 2019-03-21 19:18:08 +0100 | [diff] [blame] | 28 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
| 29 | |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 30 | struct area_desc; |
| 31 | extern struct area_desc *sim_get_flash_areas(void); |
| 32 | extern void sim_set_flash_areas(struct area_desc *areas); |
| 33 | extern void sim_reset_flash_areas(void); |
| 34 | |
| 35 | struct sim_context; |
| 36 | extern struct sim_context *sim_get_context(void); |
| 37 | extern void sim_set_context(struct sim_context *ctx); |
| 38 | extern void sim_reset_context(void); |
| 39 | |
Fabio Utzig | 99dfc78 | 2018-10-15 15:10:55 -0700 | [diff] [blame] | 40 | extern int sim_flash_erase(uint8_t flash_id, uint32_t offset, uint32_t size); |
| 41 | extern int sim_flash_read(uint8_t flash_id, uint32_t offset, uint8_t *dest, |
| 42 | uint32_t size); |
| 43 | extern int sim_flash_write(uint8_t flash_id, uint32_t offset, const uint8_t *src, |
| 44 | uint32_t size); |
Fabio Utzig | 73ffc44 | 2018-10-24 21:49:09 -0300 | [diff] [blame] | 45 | extern uint8_t sim_flash_align(uint8_t flash_id); |
| 46 | extern uint8_t sim_flash_erased_val(uint8_t flash_id); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 47 | |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 48 | struct sim_context { |
| 49 | int flash_counter; |
| 50 | int jumped; |
| 51 | uint8_t c_asserts; |
| 52 | uint8_t c_catch_asserts; |
| 53 | jmp_buf boot_jmpbuf; |
| 54 | }; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 55 | |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 56 | #ifdef MCUBOOT_ENCRYPT_RSA |
| 57 | static int |
| 58 | parse_pubkey(mbedtls_rsa_context *ctx, uint8_t **p, uint8_t *end) |
| 59 | { |
| 60 | int rc; |
| 61 | size_t len; |
| 62 | |
| 63 | if ((rc = mbedtls_asn1_get_tag(p, end, &len, |
| 64 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 65 | return -1; |
| 66 | } |
| 67 | |
| 68 | if (*p + len != end) { |
| 69 | return -2; |
| 70 | } |
| 71 | |
| 72 | if ((rc = mbedtls_asn1_get_tag(p, end, &len, |
| 73 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 74 | return -3; |
| 75 | } |
| 76 | |
| 77 | *p += len; |
| 78 | |
| 79 | if ((rc = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_BIT_STRING)) != 0) { |
| 80 | return -4; |
| 81 | } |
| 82 | |
| 83 | if (**p != MBEDTLS_ASN1_PRIMITIVE) { |
| 84 | return -5; |
| 85 | } |
| 86 | |
| 87 | *p += 1; |
| 88 | |
| 89 | if ((rc = mbedtls_asn1_get_tag(p, end, &len, |
| 90 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 91 | return -6; |
| 92 | } |
| 93 | |
| 94 | if (mbedtls_asn1_get_mpi(p, end, &ctx->N) != 0) { |
| 95 | return -7; |
| 96 | } |
| 97 | |
| 98 | if (mbedtls_asn1_get_mpi(p, end, &ctx->E) != 0) { |
| 99 | return -8; |
| 100 | } |
| 101 | |
| 102 | ctx->len = mbedtls_mpi_size(&ctx->N); |
| 103 | |
| 104 | if (*p != end) { |
| 105 | return -9; |
| 106 | } |
| 107 | |
| 108 | if (mbedtls_rsa_check_pubkey(ctx) != 0) { |
| 109 | return -10; |
| 110 | } |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | static int |
| 116 | fake_rng(void *p_rng, unsigned char *output, size_t len) |
| 117 | { |
| 118 | size_t i; |
| 119 | |
| 120 | (void)p_rng; |
| 121 | for (i = 0; i < len; i++) { |
| 122 | output[i] = (char)i; |
| 123 | } |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | #endif |
| 128 | |
| 129 | int mbedtls_platform_set_calloc_free(void * (*calloc_func)(size_t, size_t), |
| 130 | void (*free_func)(void *)); |
| 131 | |
| 132 | int rsa_oaep_encrypt_(const uint8_t *pubkey, unsigned pubkey_len, |
| 133 | const uint8_t *seckey, unsigned seckey_len, |
| 134 | uint8_t *encbuf) |
| 135 | { |
| 136 | #ifdef MCUBOOT_ENCRYPT_RSA |
| 137 | mbedtls_rsa_context ctx; |
| 138 | uint8_t *cp; |
| 139 | uint8_t *cpend; |
| 140 | int rc; |
| 141 | |
| 142 | mbedtls_platform_set_calloc_free(calloc, free); |
| 143 | |
| 144 | mbedtls_rsa_init(&ctx, MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256); |
| 145 | |
| 146 | cp = (uint8_t *)pubkey; |
| 147 | cpend = cp + pubkey_len; |
| 148 | |
| 149 | rc = parse_pubkey(&ctx, &cp, cpend); |
| 150 | if (rc) { |
| 151 | goto done; |
| 152 | } |
| 153 | |
| 154 | rc = mbedtls_rsa_rsaes_oaep_encrypt(&ctx, fake_rng, NULL, MBEDTLS_RSA_PUBLIC, |
| 155 | NULL, 0, seckey_len, seckey, encbuf); |
| 156 | if (rc) { |
| 157 | goto done; |
| 158 | } |
| 159 | |
| 160 | done: |
| 161 | mbedtls_rsa_free(&ctx); |
| 162 | return rc; |
| 163 | |
| 164 | #else |
| 165 | (void)pubkey; |
| 166 | (void)pubkey_len; |
| 167 | (void)seckey; |
| 168 | (void)seckey_len; |
| 169 | (void)encbuf; |
| 170 | return 0; |
| 171 | #endif |
| 172 | } |
| 173 | |
| 174 | int kw_encrypt_(const uint8_t *kek, const uint8_t *seckey, uint8_t *encbuf) |
| 175 | { |
| 176 | #ifdef MCUBOOT_ENCRYPT_KW |
| 177 | mbedtls_nist_kw_context kw; |
| 178 | size_t olen; |
| 179 | int rc; |
| 180 | |
| 181 | mbedtls_platform_set_calloc_free(calloc, free); |
| 182 | |
| 183 | mbedtls_nist_kw_init(&kw); |
| 184 | |
| 185 | rc = mbedtls_nist_kw_setkey(&kw, MBEDTLS_CIPHER_ID_AES, kek, 128, 1); |
| 186 | if (rc) { |
| 187 | goto done; |
| 188 | } |
| 189 | |
| 190 | rc = mbedtls_nist_kw_wrap(&kw, MBEDTLS_KW_MODE_KW, seckey, 16, encbuf, |
| 191 | &olen, 24); |
| 192 | |
| 193 | done: |
| 194 | mbedtls_nist_kw_free(&kw); |
| 195 | return rc; |
| 196 | |
| 197 | #else |
| 198 | (void)kek; |
| 199 | (void)seckey; |
| 200 | (void)encbuf; |
| 201 | return 0; |
| 202 | #endif |
| 203 | } |
| 204 | |
David Brown | 5acda26 | 2017-01-23 15:42:19 -0700 | [diff] [blame] | 205 | uint8_t flash_area_align(const struct flash_area *area) |
| 206 | { |
Fabio Utzig | 73ffc44 | 2018-10-24 21:49:09 -0300 | [diff] [blame] | 207 | return sim_flash_align(area->fa_device_id); |
David Brown | 5acda26 | 2017-01-23 15:42:19 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 210 | uint8_t flash_area_erased_val(const struct flash_area *area) |
| 211 | { |
Fabio Utzig | 73ffc44 | 2018-10-24 21:49:09 -0300 | [diff] [blame] | 212 | return sim_flash_erased_val(area->fa_device_id); |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 213 | } |
| 214 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 215 | struct area { |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 216 | struct flash_area whole; |
| 217 | struct flash_area *areas; |
| 218 | uint32_t num_areas; |
| 219 | uint8_t id; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 220 | }; |
| 221 | |
| 222 | struct area_desc { |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 223 | struct area slots[16]; |
| 224 | uint32_t num_slots; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 225 | }; |
| 226 | |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 227 | int invoke_boot_go(struct sim_context *ctx, struct area_desc *adesc) |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 228 | { |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 229 | int res; |
| 230 | struct boot_rsp rsp; |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 231 | struct boot_loader_state *state; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 232 | |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 233 | #if defined(MCUBOOT_SIGN_RSA) |
Fabio Utzig | b04afa9 | 2018-09-12 15:27:04 -0300 | [diff] [blame] | 234 | mbedtls_platform_set_calloc_free(calloc, free); |
| 235 | #endif |
David Brown | 7e701d8 | 2017-07-11 13:24:25 -0600 | [diff] [blame] | 236 | |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 237 | // NOTE: cleared internally by context_boot_go |
| 238 | state = malloc(sizeof(struct boot_loader_state)); |
| 239 | |
| 240 | sim_set_flash_areas(adesc); |
| 241 | sim_set_context(ctx); |
| 242 | |
| 243 | if (setjmp(ctx->boot_jmpbuf) == 0) { |
| 244 | res = context_boot_go(state, &rsp); |
| 245 | sim_reset_flash_areas(); |
| 246 | sim_reset_context(); |
| 247 | free(state); |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 248 | /* printf("boot_go off: %d (0x%08x)\n", res, rsp.br_image_off); */ |
| 249 | return res; |
| 250 | } else { |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 251 | sim_reset_flash_areas(); |
| 252 | sim_reset_context(); |
| 253 | free(state); |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 254 | return -0x13579; |
| 255 | } |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 256 | } |
| 257 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 258 | void *os_malloc(size_t size) |
| 259 | { |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 260 | // printf("os_malloc 0x%x bytes\n", size); |
| 261 | return malloc(size); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 264 | int flash_area_id_from_multi_image_slot(int image_index, int slot) |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 265 | { |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 266 | switch (slot) { |
| 267 | case 0: return FLASH_AREA_IMAGE_PRIMARY(image_index); |
| 268 | case 1: return FLASH_AREA_IMAGE_SECONDARY(image_index); |
| 269 | case 2: return FLASH_AREA_IMAGE_SCRATCH; |
David Vincze | 6c9b416 | 2019-03-21 19:18:08 +0100 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | printf("Image flash area ID not found\n"); |
| 273 | return -1; /* flash_area_open will fail on that */ |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | int flash_area_open(uint8_t id, const struct flash_area **area) |
| 277 | { |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 278 | uint32_t i; |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 279 | struct area_desc *flash_areas; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 280 | |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 281 | flash_areas = sim_get_flash_areas(); |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 282 | for (i = 0; i < flash_areas->num_slots; i++) { |
| 283 | if (flash_areas->slots[i].id == id) |
| 284 | break; |
| 285 | } |
| 286 | if (i == flash_areas->num_slots) { |
| 287 | printf("Unsupported area\n"); |
| 288 | abort(); |
| 289 | } |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 290 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 291 | /* Unsure if this is right, just returning the first area. */ |
| 292 | *area = &flash_areas->slots[i].whole; |
| 293 | return 0; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | void flash_area_close(const struct flash_area *area) |
| 297 | { |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 298 | (void)area; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /* |
| 302 | * Read/write/erase. Offset is relative from beginning of flash area. |
| 303 | */ |
| 304 | 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] | 305 | uint32_t len) |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 306 | { |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 307 | BOOT_LOG_DBG("%s: area=%d, off=%x, len=%x", |
| 308 | __func__, area->fa_id, off, len); |
Fabio Utzig | 99dfc78 | 2018-10-15 15:10:55 -0700 | [diff] [blame] | 309 | return sim_flash_read(area->fa_device_id, area->fa_off + off, dst, len); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | 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] | 313 | uint32_t len) |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 314 | { |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 315 | BOOT_LOG_DBG("%s: area=%d, off=%x, len=%x", __func__, |
| 316 | area->fa_id, off, len); |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 317 | struct sim_context *ctx = sim_get_context(); |
| 318 | if (--(ctx->flash_counter) == 0) { |
| 319 | ctx->jumped++; |
| 320 | longjmp(ctx->boot_jmpbuf, 1); |
Fabio Utzig | 99dfc78 | 2018-10-15 15:10:55 -0700 | [diff] [blame] | 321 | } |
| 322 | return sim_flash_write(area->fa_device_id, area->fa_off + off, src, len); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | int flash_area_erase(const struct flash_area *area, uint32_t off, uint32_t len) |
| 326 | { |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 327 | BOOT_LOG_DBG("%s: area=%d, off=%x, len=%x", __func__, |
| 328 | area->fa_id, off, len); |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 329 | struct sim_context *ctx = sim_get_context(); |
| 330 | if (--(ctx->flash_counter) == 0) { |
| 331 | ctx->jumped++; |
| 332 | longjmp(ctx->boot_jmpbuf, 1); |
Fabio Utzig | 99dfc78 | 2018-10-15 15:10:55 -0700 | [diff] [blame] | 333 | } |
| 334 | return sim_flash_erase(area->fa_device_id, area->fa_off + off, len); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Fabio Utzig | 35d31b4 | 2018-09-19 08:09:37 -0300 | [diff] [blame] | 337 | int flash_area_read_is_empty(const struct flash_area *area, uint32_t off, |
| 338 | void *dst, uint32_t len) |
| 339 | { |
| 340 | uint8_t i; |
| 341 | uint8_t *u8dst; |
| 342 | int rc; |
| 343 | |
| 344 | BOOT_LOG_DBG("%s: area=%d, off=%x, len=%x", __func__, area->fa_id, off, len); |
| 345 | |
Fabio Utzig | 99dfc78 | 2018-10-15 15:10:55 -0700 | [diff] [blame] | 346 | rc = sim_flash_read(area->fa_device_id, area->fa_off + off, dst, len); |
Fabio Utzig | 35d31b4 | 2018-09-19 08:09:37 -0300 | [diff] [blame] | 347 | if (rc) { |
| 348 | return -1; |
| 349 | } |
| 350 | |
| 351 | for (i = 0, u8dst = (uint8_t *)dst; i < len; i++) { |
Fabio Utzig | 73ffc44 | 2018-10-24 21:49:09 -0300 | [diff] [blame] | 352 | if (u8dst[i] != sim_flash_erased_val(area->fa_device_id)) { |
Fabio Utzig | 35d31b4 | 2018-09-19 08:09:37 -0300 | [diff] [blame] | 353 | return 0; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | return 1; |
| 358 | } |
| 359 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 360 | int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret) |
| 361 | { |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 362 | uint32_t i; |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 363 | struct area *slot; |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 364 | struct area_desc *flash_areas; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 365 | |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 366 | flash_areas = sim_get_flash_areas(); |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 367 | for (i = 0; i < flash_areas->num_slots; i++) { |
| 368 | if (flash_areas->slots[i].id == idx) |
| 369 | break; |
| 370 | } |
| 371 | if (i == flash_areas->num_slots) { |
| 372 | printf("Unsupported area\n"); |
| 373 | abort(); |
| 374 | } |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 375 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 376 | slot = &flash_areas->slots[i]; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 377 | |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 378 | if (slot->num_areas > (uint32_t)*cnt) { |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 379 | printf("Too many areas in slot\n"); |
| 380 | abort(); |
| 381 | } |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 382 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 383 | *cnt = slot->num_areas; |
| 384 | memcpy(ret, slot->areas, slot->num_areas * sizeof(struct flash_area)); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 385 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 386 | return 0; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 387 | } |
| 388 | |
David Brown | 60399f6 | 2017-05-11 10:20:34 -0600 | [diff] [blame] | 389 | int flash_area_get_sectors(int fa_id, uint32_t *count, |
| 390 | struct flash_sector *sectors) |
| 391 | { |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 392 | uint32_t i; |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 393 | struct area *slot; |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 394 | struct area_desc *flash_areas; |
David Brown | 60399f6 | 2017-05-11 10:20:34 -0600 | [diff] [blame] | 395 | |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 396 | flash_areas = sim_get_flash_areas(); |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 397 | for (i = 0; i < flash_areas->num_slots; i++) { |
| 398 | if (flash_areas->slots[i].id == fa_id) |
| 399 | break; |
| 400 | } |
| 401 | if (i == flash_areas->num_slots) { |
| 402 | printf("Unsupported area\n"); |
| 403 | abort(); |
| 404 | } |
David Brown | 60399f6 | 2017-05-11 10:20:34 -0600 | [diff] [blame] | 405 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 406 | slot = &flash_areas->slots[i]; |
David Brown | 60399f6 | 2017-05-11 10:20:34 -0600 | [diff] [blame] | 407 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 408 | if (slot->num_areas > *count) { |
| 409 | printf("Too many areas in slot\n"); |
| 410 | abort(); |
| 411 | } |
David Brown | 60399f6 | 2017-05-11 10:20:34 -0600 | [diff] [blame] | 412 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 413 | for (i = 0; i < slot->num_areas; i++) { |
| 414 | sectors[i].fs_off = slot->areas[i].fa_off - |
| 415 | slot->whole.fa_off; |
| 416 | sectors[i].fs_size = slot->areas[i].fa_size; |
| 417 | } |
| 418 | *count = slot->num_areas; |
David Brown | 60399f6 | 2017-05-11 10:20:34 -0600 | [diff] [blame] | 419 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 420 | return 0; |
David Brown | 60399f6 | 2017-05-11 10:20:34 -0600 | [diff] [blame] | 421 | } |
Fabio Utzig | 9b0ee90 | 2017-11-23 19:49:00 -0200 | [diff] [blame] | 422 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 423 | int flash_area_id_to_multi_image_slot(int image_index, int area_id) |
Andrzej Puzdrowski | e575fe9 | 2019-03-14 12:20:19 +0100 | [diff] [blame] | 424 | { |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 425 | if (area_id == FLASH_AREA_IMAGE_PRIMARY(image_index)) { |
Andrzej Puzdrowski | e575fe9 | 2019-03-14 12:20:19 +0100 | [diff] [blame] | 426 | return 0; |
Andrzej Puzdrowski | e575fe9 | 2019-03-14 12:20:19 +0100 | [diff] [blame] | 427 | } |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 428 | if (area_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) { |
David Vincze | b75c12a | 2019-03-22 14:58:33 +0100 | [diff] [blame] | 429 | return 1; |
| 430 | } |
| 431 | |
| 432 | printf("Unsupported image area ID\n"); |
| 433 | abort(); |
Andrzej Puzdrowski | e575fe9 | 2019-03-14 12:20:19 +0100 | [diff] [blame] | 434 | } |
| 435 | |
Fabio Utzig | 9b0ee90 | 2017-11-23 19:49:00 -0200 | [diff] [blame] | 436 | void sim_assert(int x, const char *assertion, const char *file, unsigned int line, const char *function) |
| 437 | { |
| 438 | if (!(x)) { |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 439 | struct sim_context *ctx = sim_get_context(); |
| 440 | if (ctx->c_catch_asserts) { |
| 441 | ctx->c_asserts++; |
Fabio Utzig | 9b0ee90 | 2017-11-23 19:49:00 -0200 | [diff] [blame] | 442 | } else { |
| 443 | BOOT_LOG_ERR("%s:%d: %s: Assertion `%s' failed.", file, line, function, assertion); |
| 444 | |
| 445 | /* NOTE: if the assert below is triggered, the place where it was originally |
| 446 | * asserted is printed by the message above... |
| 447 | */ |
| 448 | assert(x); |
| 449 | } |
| 450 | } |
| 451 | } |
David Brown | e0bb1f9 | 2019-10-01 15:57:01 -0600 | [diff] [blame] | 452 | |
| 453 | uint32_t boot_max_align(void) |
| 454 | { |
| 455 | return BOOT_MAX_ALIGN; |
| 456 | } |
David Brown | 2b8a695 | 2019-10-01 16:14:53 -0600 | [diff] [blame] | 457 | |
| 458 | uint32_t boot_magic_sz(void) |
| 459 | { |
| 460 | return BOOT_MAGIC_SZ; |
| 461 | } |