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