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