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 | |
| 245 | int flash_area_id_from_image_slot(int slot) |
| 246 | { |
David Vincze | 6c9b416 | 2019-03-21 19:18:08 +0100 | [diff] [blame^] | 247 | const int area_id_tab[] = {FLASH_AREA_IMAGE_PRIMARY, |
| 248 | FLASH_AREA_IMAGE_SECONDARY, |
| 249 | FLASH_AREA_IMAGE_SCRATCH}; |
| 250 | |
| 251 | if (slot >= 0 && (unsigned)slot < ARRAY_SIZE(area_id_tab)) { |
| 252 | return area_id_tab[slot]; |
| 253 | } |
| 254 | |
| 255 | printf("Image flash area ID not found\n"); |
| 256 | return -1; /* flash_area_open will fail on that */ |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | int flash_area_open(uint8_t id, const struct flash_area **area) |
| 260 | { |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 261 | uint32_t i; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 262 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 263 | for (i = 0; i < flash_areas->num_slots; i++) { |
| 264 | if (flash_areas->slots[i].id == id) |
| 265 | break; |
| 266 | } |
| 267 | if (i == flash_areas->num_slots) { |
| 268 | printf("Unsupported area\n"); |
| 269 | abort(); |
| 270 | } |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 271 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 272 | /* Unsure if this is right, just returning the first area. */ |
| 273 | *area = &flash_areas->slots[i].whole; |
| 274 | return 0; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | void flash_area_close(const struct flash_area *area) |
| 278 | { |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 279 | (void)area; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | /* |
| 283 | * Read/write/erase. Offset is relative from beginning of flash area. |
| 284 | */ |
| 285 | 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] | 286 | uint32_t len) |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 287 | { |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 288 | BOOT_LOG_DBG("%s: area=%d, off=%x, len=%x", |
| 289 | __func__, area->fa_id, off, len); |
Fabio Utzig | 99dfc78 | 2018-10-15 15:10:55 -0700 | [diff] [blame] | 290 | 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] | 291 | } |
| 292 | |
| 293 | 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] | 294 | uint32_t len) |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 295 | { |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 296 | BOOT_LOG_DBG("%s: area=%d, off=%x, len=%x", __func__, |
| 297 | area->fa_id, off, len); |
Fabio Utzig | 99dfc78 | 2018-10-15 15:10:55 -0700 | [diff] [blame] | 298 | if (--flash_counter == 0) { |
| 299 | jumped++; |
| 300 | longjmp(boot_jmpbuf, 1); |
| 301 | } |
| 302 | 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] | 303 | } |
| 304 | |
| 305 | int flash_area_erase(const struct flash_area *area, uint32_t off, uint32_t len) |
| 306 | { |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 307 | BOOT_LOG_DBG("%s: area=%d, off=%x, len=%x", __func__, |
| 308 | area->fa_id, off, len); |
Fabio Utzig | 99dfc78 | 2018-10-15 15:10:55 -0700 | [diff] [blame] | 309 | if (--flash_counter == 0) { |
| 310 | jumped++; |
| 311 | longjmp(boot_jmpbuf, 1); |
| 312 | } |
| 313 | 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] | 314 | } |
| 315 | |
Fabio Utzig | 35d31b4 | 2018-09-19 08:09:37 -0300 | [diff] [blame] | 316 | int flash_area_read_is_empty(const struct flash_area *area, uint32_t off, |
| 317 | void *dst, uint32_t len) |
| 318 | { |
| 319 | uint8_t i; |
| 320 | uint8_t *u8dst; |
| 321 | int rc; |
| 322 | |
| 323 | BOOT_LOG_DBG("%s: area=%d, off=%x, len=%x", __func__, area->fa_id, off, len); |
| 324 | |
Fabio Utzig | 99dfc78 | 2018-10-15 15:10:55 -0700 | [diff] [blame] | 325 | 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] | 326 | if (rc) { |
| 327 | return -1; |
| 328 | } |
| 329 | |
| 330 | for (i = 0, u8dst = (uint8_t *)dst; i < len; i++) { |
Fabio Utzig | 73ffc44 | 2018-10-24 21:49:09 -0300 | [diff] [blame] | 331 | if (u8dst[i] != sim_flash_erased_val(area->fa_device_id)) { |
Fabio Utzig | 35d31b4 | 2018-09-19 08:09:37 -0300 | [diff] [blame] | 332 | return 0; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | return 1; |
| 337 | } |
| 338 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 339 | int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret) |
| 340 | { |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 341 | uint32_t i; |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 342 | struct area *slot; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 343 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 344 | for (i = 0; i < flash_areas->num_slots; i++) { |
| 345 | if (flash_areas->slots[i].id == idx) |
| 346 | break; |
| 347 | } |
| 348 | if (i == flash_areas->num_slots) { |
| 349 | printf("Unsupported area\n"); |
| 350 | abort(); |
| 351 | } |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 352 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 353 | slot = &flash_areas->slots[i]; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 354 | |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 355 | if (slot->num_areas > (uint32_t)*cnt) { |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 356 | printf("Too many areas in slot\n"); |
| 357 | abort(); |
| 358 | } |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 359 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 360 | *cnt = slot->num_areas; |
| 361 | memcpy(ret, slot->areas, slot->num_areas * sizeof(struct flash_area)); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 362 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 363 | return 0; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 364 | } |
| 365 | |
David Brown | 60399f6 | 2017-05-11 10:20:34 -0600 | [diff] [blame] | 366 | int flash_area_get_sectors(int fa_id, uint32_t *count, |
| 367 | struct flash_sector *sectors) |
| 368 | { |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 369 | uint32_t i; |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 370 | struct area *slot; |
David Brown | 60399f6 | 2017-05-11 10:20:34 -0600 | [diff] [blame] | 371 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 372 | for (i = 0; i < flash_areas->num_slots; i++) { |
| 373 | if (flash_areas->slots[i].id == fa_id) |
| 374 | break; |
| 375 | } |
| 376 | if (i == flash_areas->num_slots) { |
| 377 | printf("Unsupported area\n"); |
| 378 | abort(); |
| 379 | } |
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 | slot = &flash_areas->slots[i]; |
David Brown | 60399f6 | 2017-05-11 10:20:34 -0600 | [diff] [blame] | 382 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 383 | if (slot->num_areas > *count) { |
| 384 | printf("Too many areas in slot\n"); |
| 385 | abort(); |
| 386 | } |
David Brown | 60399f6 | 2017-05-11 10:20:34 -0600 | [diff] [blame] | 387 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 388 | for (i = 0; i < slot->num_areas; i++) { |
| 389 | sectors[i].fs_off = slot->areas[i].fa_off - |
| 390 | slot->whole.fa_off; |
| 391 | sectors[i].fs_size = slot->areas[i].fa_size; |
| 392 | } |
| 393 | *count = slot->num_areas; |
David Brown | 60399f6 | 2017-05-11 10:20:34 -0600 | [diff] [blame] | 394 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 395 | return 0; |
David Brown | 60399f6 | 2017-05-11 10:20:34 -0600 | [diff] [blame] | 396 | } |
Fabio Utzig | 9b0ee90 | 2017-11-23 19:49:00 -0200 | [diff] [blame] | 397 | |
Andrzej Puzdrowski | e575fe9 | 2019-03-14 12:20:19 +0100 | [diff] [blame] | 398 | int flash_area_id_to_image_slot(int area_id) |
| 399 | { |
| 400 | switch (area_id) { |
| 401 | case FLASH_AREA_IMAGE_PRIMARY: |
| 402 | return 0; |
| 403 | case FLASH_AREA_IMAGE_SECONDARY: |
| 404 | return 1; |
| 405 | default: |
| 406 | printf("Unsupported image area ID\n"); |
| 407 | abort(); |
| 408 | } |
| 409 | } |
| 410 | |
Fabio Utzig | 9b0ee90 | 2017-11-23 19:49:00 -0200 | [diff] [blame] | 411 | void sim_assert(int x, const char *assertion, const char *file, unsigned int line, const char *function) |
| 412 | { |
| 413 | if (!(x)) { |
| 414 | if (c_catch_asserts) { |
| 415 | c_asserts++; |
| 416 | } else { |
| 417 | BOOT_LOG_ERR("%s:%d: %s: Assertion `%s' failed.", file, line, function, assertion); |
| 418 | |
| 419 | /* NOTE: if the assert below is triggered, the place where it was originally |
| 420 | * asserted is printed by the message above... |
| 421 | */ |
| 422 | assert(x); |
| 423 | } |
| 424 | } |
| 425 | } |