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