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