blob: 201b50bab67856b5f0cfd6018ba941cb1a410403 [file] [log] [blame]
David Brownde7729e2017-01-09 10:41:35 -07001/* Run the boot image. */
2
Fabio Utzig9b0ee902017-11-23 19:49:00 -02003#include <assert.h>
David Brownde7729e2017-01-09 10:41:35 -07004#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 Puzdrowskib788c712018-04-12 12:42:49 +020010
11#include <flash_map_backend/flash_map_backend.h>
David Brownde7729e2017-01-09 10:41:35 -070012
David Brownd2b18532017-07-12 09:51:31 -060013#include "../../../boot/bootutil/src/bootutil_priv.h"
Fabio Utzig57c40f72017-12-12 21:48:30 -020014#include "bootsim.h"
David Brownde7729e2017-01-09 10:41:35 -070015
Fabio Utzig1e48b912018-09-18 09:04:18 -030016#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 Brown54b77792017-05-05 09:40:01 -060025#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_ERROR
David Brown75fd5dc2017-05-04 09:04:47 -060026#include <bootutil/bootutil_log.h>
27
David Vincze6c9b4162019-03-21 19:18:08 +010028#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
29
Fabio Utzig8000e322019-08-05 08:14:32 -030030struct area_desc;
31extern struct area_desc *sim_get_flash_areas(void);
32extern void sim_set_flash_areas(struct area_desc *areas);
33extern void sim_reset_flash_areas(void);
34
35struct sim_context;
36extern struct sim_context *sim_get_context(void);
37extern void sim_set_context(struct sim_context *ctx);
38extern void sim_reset_context(void);
39
Fabio Utzig99dfc782018-10-15 15:10:55 -070040extern int sim_flash_erase(uint8_t flash_id, uint32_t offset, uint32_t size);
41extern int sim_flash_read(uint8_t flash_id, uint32_t offset, uint8_t *dest,
42 uint32_t size);
43extern int sim_flash_write(uint8_t flash_id, uint32_t offset, const uint8_t *src,
44 uint32_t size);
Fabio Utzig1edb7882020-10-04 11:51:53 -030045extern uint16_t sim_flash_align(uint8_t flash_id);
Fabio Utzig73ffc442018-10-24 21:49:09 -030046extern uint8_t sim_flash_erased_val(uint8_t flash_id);
David Brownde7729e2017-01-09 10:41:35 -070047
Fabio Utzig8000e322019-08-05 08:14:32 -030048struct sim_context {
49 int flash_counter;
50 int jumped;
51 uint8_t c_asserts;
52 uint8_t c_catch_asserts;
53 jmp_buf boot_jmpbuf;
54};
David Brownde7729e2017-01-09 10:41:35 -070055
Fabio Utzig1e48b912018-09-18 09:04:18 -030056#ifdef MCUBOOT_ENCRYPT_RSA
57static int
58parse_pubkey(mbedtls_rsa_context *ctx, uint8_t **p, uint8_t *end)
59{
60 int rc;
61 size_t len;
62
63 if ((rc = mbedtls_asn1_get_tag(p, end, &len,
64 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
65 return -1;
66 }
67
68 if (*p + len != end) {
69 return -2;
70 }
71
72 if ((rc = mbedtls_asn1_get_tag(p, end, &len,
73 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
74 return -3;
75 }
76
77 *p += len;
78
79 if ((rc = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_BIT_STRING)) != 0) {
80 return -4;
81 }
82
83 if (**p != MBEDTLS_ASN1_PRIMITIVE) {
84 return -5;
85 }
86
87 *p += 1;
88
89 if ((rc = mbedtls_asn1_get_tag(p, end, &len,
90 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
91 return -6;
92 }
93
94 if (mbedtls_asn1_get_mpi(p, end, &ctx->N) != 0) {
95 return -7;
96 }
97
98 if (mbedtls_asn1_get_mpi(p, end, &ctx->E) != 0) {
99 return -8;
100 }
101
102 ctx->len = mbedtls_mpi_size(&ctx->N);
103
104 if (*p != end) {
105 return -9;
106 }
107
108 if (mbedtls_rsa_check_pubkey(ctx) != 0) {
109 return -10;
110 }
111
112 return 0;
113}
114
115static int
116fake_rng(void *p_rng, unsigned char *output, size_t len)
117{
118 size_t i;
119
120 (void)p_rng;
121 for (i = 0; i < len; i++) {
122 output[i] = (char)i;
123 }
124
125 return 0;
126}
127#endif
128
129int mbedtls_platform_set_calloc_free(void * (*calloc_func)(size_t, size_t),
130 void (*free_func)(void *));
131
132int rsa_oaep_encrypt_(const uint8_t *pubkey, unsigned pubkey_len,
133 const uint8_t *seckey, unsigned seckey_len,
134 uint8_t *encbuf)
135{
136#ifdef MCUBOOT_ENCRYPT_RSA
137 mbedtls_rsa_context ctx;
138 uint8_t *cp;
139 uint8_t *cpend;
140 int rc;
141
142 mbedtls_platform_set_calloc_free(calloc, free);
143
144 mbedtls_rsa_init(&ctx, MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256);
145
146 cp = (uint8_t *)pubkey;
147 cpend = cp + pubkey_len;
148
149 rc = parse_pubkey(&ctx, &cp, cpend);
150 if (rc) {
151 goto done;
152 }
153
154 rc = mbedtls_rsa_rsaes_oaep_encrypt(&ctx, fake_rng, NULL, MBEDTLS_RSA_PUBLIC,
155 NULL, 0, seckey_len, seckey, encbuf);
156 if (rc) {
157 goto done;
158 }
159
160done:
161 mbedtls_rsa_free(&ctx);
162 return rc;
163
164#else
165 (void)pubkey;
166 (void)pubkey_len;
167 (void)seckey;
168 (void)seckey_len;
169 (void)encbuf;
170 return 0;
171#endif
172}
173
174int kw_encrypt_(const uint8_t *kek, const uint8_t *seckey, uint8_t *encbuf)
175{
176#ifdef MCUBOOT_ENCRYPT_KW
Salome Thirot6fdbf552021-05-14 16:46:14 +0100177#ifdef MCUBOOT_AES_256
178 int key_len = 256;
179 int out_size = 40;
180 int in_len = 32;
181#else
182 int key_len = 128;
183 int out_size = 24;
184 int in_len = 16;
185#endif
Fabio Utzig1e48b912018-09-18 09:04:18 -0300186 mbedtls_nist_kw_context kw;
187 size_t olen;
188 int rc;
189
190 mbedtls_platform_set_calloc_free(calloc, free);
191
192 mbedtls_nist_kw_init(&kw);
193
Salome Thirot6fdbf552021-05-14 16:46:14 +0100194 rc = mbedtls_nist_kw_setkey(&kw, MBEDTLS_CIPHER_ID_AES, kek, key_len, 1);
Fabio Utzig1e48b912018-09-18 09:04:18 -0300195 if (rc) {
196 goto done;
197 }
198
Salome Thirot6fdbf552021-05-14 16:46:14 +0100199 rc = mbedtls_nist_kw_wrap(&kw, MBEDTLS_KW_MODE_KW, seckey, in_len, encbuf,
200 &olen, out_size);
Fabio Utzig1e48b912018-09-18 09:04:18 -0300201
202done:
203 mbedtls_nist_kw_free(&kw);
204 return rc;
205
206#else
207 (void)kek;
208 (void)seckey;
209 (void)encbuf;
210 return 0;
211#endif
212}
213
Fabio Utzig1edb7882020-10-04 11:51:53 -0300214uint16_t flash_area_align(const struct flash_area *area)
David Brown5acda262017-01-23 15:42:19 -0700215{
Fabio Utzig73ffc442018-10-24 21:49:09 -0300216 return sim_flash_align(area->fa_device_id);
David Brown5acda262017-01-23 15:42:19 -0700217}
218
Fabio Utzigea0290b2018-08-09 14:23:01 -0300219uint8_t flash_area_erased_val(const struct flash_area *area)
220{
Fabio Utzig73ffc442018-10-24 21:49:09 -0300221 return sim_flash_erased_val(area->fa_device_id);
Fabio Utzigea0290b2018-08-09 14:23:01 -0300222}
223
David Brownde7729e2017-01-09 10:41:35 -0700224struct area {
David Brown7ad80882017-06-20 15:30:36 -0600225 struct flash_area whole;
226 struct flash_area *areas;
227 uint32_t num_areas;
228 uint8_t id;
David Brownde7729e2017-01-09 10:41:35 -0700229};
230
231struct area_desc {
David Brown7ad80882017-06-20 15:30:36 -0600232 struct area slots[16];
233 uint32_t num_slots;
David Brownde7729e2017-01-09 10:41:35 -0700234};
235
Fabio Utzig8000e322019-08-05 08:14:32 -0300236int invoke_boot_go(struct sim_context *ctx, struct area_desc *adesc)
David Brownde7729e2017-01-09 10:41:35 -0700237{
David Brown7ad80882017-06-20 15:30:36 -0600238 int res;
239 struct boot_rsp rsp;
Fabio Utzig8000e322019-08-05 08:14:32 -0300240 struct boot_loader_state *state;
David Brownde7729e2017-01-09 10:41:35 -0700241
David Brown641af452021-02-19 12:16:48 -0700242#if defined(MCUBOOT_SIGN_RSA) || \
Fabio Utzig6c553d62021-05-06 19:56:18 -0300243 (defined(MCUBOOT_SIGN_EC256) && defined(MCUBOOT_USE_MBED_TLS)) ||\
Salome Thirot6fdbf552021-05-14 16:46:14 +0100244 (defined(MCUBOOT_ENCRYPT_EC256) && defined(MCUBOOT_USE_MBED_TLS)) ||\
245 (defined(MCUBOOT_ENCRYPT_X25519) && defined(MCUBOOT_USE_MBED_TLS))
Fabio Utzigb04afa92018-09-12 15:27:04 -0300246 mbedtls_platform_set_calloc_free(calloc, free);
247#endif
David Brown7e701d82017-07-11 13:24:25 -0600248
Fabio Utzig8000e322019-08-05 08:14:32 -0300249 // NOTE: cleared internally by context_boot_go
250 state = malloc(sizeof(struct boot_loader_state));
251
252 sim_set_flash_areas(adesc);
253 sim_set_context(ctx);
254
255 if (setjmp(ctx->boot_jmpbuf) == 0) {
256 res = context_boot_go(state, &rsp);
257 sim_reset_flash_areas();
258 sim_reset_context();
259 free(state);
David Brown7ad80882017-06-20 15:30:36 -0600260 /* printf("boot_go off: %d (0x%08x)\n", res, rsp.br_image_off); */
261 return res;
262 } else {
Fabio Utzig8000e322019-08-05 08:14:32 -0300263 sim_reset_flash_areas();
264 sim_reset_context();
265 free(state);
David Brown7ad80882017-06-20 15:30:36 -0600266 return -0x13579;
267 }
David Brownde7729e2017-01-09 10:41:35 -0700268}
269
David Brownde7729e2017-01-09 10:41:35 -0700270void *os_malloc(size_t size)
271{
David Brown7ad80882017-06-20 15:30:36 -0600272 // printf("os_malloc 0x%x bytes\n", size);
273 return malloc(size);
David Brownde7729e2017-01-09 10:41:35 -0700274}
275
Fabio Utzigb0f04732019-07-31 09:49:19 -0300276int flash_area_id_from_multi_image_slot(int image_index, int slot)
David Brownde7729e2017-01-09 10:41:35 -0700277{
Fabio Utzigb0f04732019-07-31 09:49:19 -0300278 switch (slot) {
279 case 0: return FLASH_AREA_IMAGE_PRIMARY(image_index);
280 case 1: return FLASH_AREA_IMAGE_SECONDARY(image_index);
281 case 2: return FLASH_AREA_IMAGE_SCRATCH;
David Vincze6c9b4162019-03-21 19:18:08 +0100282 }
283
284 printf("Image flash area ID not found\n");
285 return -1; /* flash_area_open will fail on that */
David Brownde7729e2017-01-09 10:41:35 -0700286}
287
288int flash_area_open(uint8_t id, const struct flash_area **area)
289{
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200290 uint32_t i;
Fabio Utzig8000e322019-08-05 08:14:32 -0300291 struct area_desc *flash_areas;
David Brownde7729e2017-01-09 10:41:35 -0700292
Fabio Utzig8000e322019-08-05 08:14:32 -0300293 flash_areas = sim_get_flash_areas();
David Brown7ad80882017-06-20 15:30:36 -0600294 for (i = 0; i < flash_areas->num_slots; i++) {
295 if (flash_areas->slots[i].id == id)
296 break;
297 }
298 if (i == flash_areas->num_slots) {
299 printf("Unsupported area\n");
300 abort();
301 }
David Brownde7729e2017-01-09 10:41:35 -0700302
David Brown7ad80882017-06-20 15:30:36 -0600303 /* Unsure if this is right, just returning the first area. */
304 *area = &flash_areas->slots[i].whole;
305 return 0;
David Brownde7729e2017-01-09 10:41:35 -0700306}
307
308void flash_area_close(const struct flash_area *area)
309{
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200310 (void)area;
David Brownde7729e2017-01-09 10:41:35 -0700311}
312
313/*
314 * Read/write/erase. Offset is relative from beginning of flash area.
315 */
316int flash_area_read(const struct flash_area *area, uint32_t off, void *dst,
David Brown7ad80882017-06-20 15:30:36 -0600317 uint32_t len)
David Brownde7729e2017-01-09 10:41:35 -0700318{
Fabio Utzig6fa2d402019-12-10 14:34:18 -0300319 BOOT_LOG_SIM("%s: area=%d, off=%x, len=%x",
David Brown7ad80882017-06-20 15:30:36 -0600320 __func__, area->fa_id, off, len);
Fabio Utzig99dfc782018-10-15 15:10:55 -0700321 return sim_flash_read(area->fa_device_id, area->fa_off + off, dst, len);
David Brownde7729e2017-01-09 10:41:35 -0700322}
323
324int flash_area_write(const struct flash_area *area, uint32_t off, const void *src,
David Brown7ad80882017-06-20 15:30:36 -0600325 uint32_t len)
David Brownde7729e2017-01-09 10:41:35 -0700326{
Fabio Utzig6fa2d402019-12-10 14:34:18 -0300327 BOOT_LOG_SIM("%s: area=%d, off=%x, len=%x", __func__,
David Brown7ad80882017-06-20 15:30:36 -0600328 area->fa_id, off, len);
Fabio Utzig8000e322019-08-05 08:14:32 -0300329 struct sim_context *ctx = sim_get_context();
330 if (--(ctx->flash_counter) == 0) {
331 ctx->jumped++;
332 longjmp(ctx->boot_jmpbuf, 1);
Fabio Utzig99dfc782018-10-15 15:10:55 -0700333 }
334 return sim_flash_write(area->fa_device_id, area->fa_off + off, src, len);
David Brownde7729e2017-01-09 10:41:35 -0700335}
336
337int flash_area_erase(const struct flash_area *area, uint32_t off, uint32_t len)
338{
Fabio Utzig6fa2d402019-12-10 14:34:18 -0300339 BOOT_LOG_SIM("%s: area=%d, off=%x, len=%x", __func__,
David Brown7ad80882017-06-20 15:30:36 -0600340 area->fa_id, off, len);
Fabio Utzig8000e322019-08-05 08:14:32 -0300341 struct sim_context *ctx = sim_get_context();
342 if (--(ctx->flash_counter) == 0) {
343 ctx->jumped++;
344 longjmp(ctx->boot_jmpbuf, 1);
Fabio Utzig99dfc782018-10-15 15:10:55 -0700345 }
346 return sim_flash_erase(area->fa_device_id, area->fa_off + off, len);
David Brownde7729e2017-01-09 10:41:35 -0700347}
348
349int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret)
350{
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200351 uint32_t i;
David Brown7ad80882017-06-20 15:30:36 -0600352 struct area *slot;
Fabio Utzig8000e322019-08-05 08:14:32 -0300353 struct area_desc *flash_areas;
David Brownde7729e2017-01-09 10:41:35 -0700354
Fabio Utzig8000e322019-08-05 08:14:32 -0300355 flash_areas = sim_get_flash_areas();
David Brown7ad80882017-06-20 15:30:36 -0600356 for (i = 0; i < flash_areas->num_slots; i++) {
357 if (flash_areas->slots[i].id == idx)
358 break;
359 }
360 if (i == flash_areas->num_slots) {
361 printf("Unsupported area\n");
362 abort();
363 }
David Brownde7729e2017-01-09 10:41:35 -0700364
David Brown7ad80882017-06-20 15:30:36 -0600365 slot = &flash_areas->slots[i];
David Brownde7729e2017-01-09 10:41:35 -0700366
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200367 if (slot->num_areas > (uint32_t)*cnt) {
David Brown7ad80882017-06-20 15:30:36 -0600368 printf("Too many areas in slot\n");
369 abort();
370 }
David Brownde7729e2017-01-09 10:41:35 -0700371
David Brown7ad80882017-06-20 15:30:36 -0600372 *cnt = slot->num_areas;
373 memcpy(ret, slot->areas, slot->num_areas * sizeof(struct flash_area));
David Brownde7729e2017-01-09 10:41:35 -0700374
David Brown7ad80882017-06-20 15:30:36 -0600375 return 0;
David Brownde7729e2017-01-09 10:41:35 -0700376}
377
David Brown60399f62017-05-11 10:20:34 -0600378int flash_area_get_sectors(int fa_id, uint32_t *count,
379 struct flash_sector *sectors)
380{
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200381 uint32_t i;
David Brown7ad80882017-06-20 15:30:36 -0600382 struct area *slot;
Fabio Utzig8000e322019-08-05 08:14:32 -0300383 struct area_desc *flash_areas;
David Brown60399f62017-05-11 10:20:34 -0600384
Fabio Utzig8000e322019-08-05 08:14:32 -0300385 flash_areas = sim_get_flash_areas();
David Brown7ad80882017-06-20 15:30:36 -0600386 for (i = 0; i < flash_areas->num_slots; i++) {
387 if (flash_areas->slots[i].id == fa_id)
388 break;
389 }
390 if (i == flash_areas->num_slots) {
391 printf("Unsupported area\n");
392 abort();
393 }
David Brown60399f62017-05-11 10:20:34 -0600394
David Brown7ad80882017-06-20 15:30:36 -0600395 slot = &flash_areas->slots[i];
David Brown60399f62017-05-11 10:20:34 -0600396
David Brown7ad80882017-06-20 15:30:36 -0600397 if (slot->num_areas > *count) {
398 printf("Too many areas in slot\n");
399 abort();
400 }
David Brown60399f62017-05-11 10:20:34 -0600401
David Brown7ad80882017-06-20 15:30:36 -0600402 for (i = 0; i < slot->num_areas; i++) {
403 sectors[i].fs_off = slot->areas[i].fa_off -
404 slot->whole.fa_off;
405 sectors[i].fs_size = slot->areas[i].fa_size;
406 }
407 *count = slot->num_areas;
David Brown60399f62017-05-11 10:20:34 -0600408
David Brown7ad80882017-06-20 15:30:36 -0600409 return 0;
David Brown60399f62017-05-11 10:20:34 -0600410}
Fabio Utzig9b0ee902017-11-23 19:49:00 -0200411
Fabio Utzigb0f04732019-07-31 09:49:19 -0300412int flash_area_id_to_multi_image_slot(int image_index, int area_id)
Andrzej Puzdrowskie575fe92019-03-14 12:20:19 +0100413{
Fabio Utzigb0f04732019-07-31 09:49:19 -0300414 if (area_id == FLASH_AREA_IMAGE_PRIMARY(image_index)) {
Andrzej Puzdrowskie575fe92019-03-14 12:20:19 +0100415 return 0;
Andrzej Puzdrowskie575fe92019-03-14 12:20:19 +0100416 }
Fabio Utzigb0f04732019-07-31 09:49:19 -0300417 if (area_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vinczeb75c12a2019-03-22 14:58:33 +0100418 return 1;
419 }
420
421 printf("Unsupported image area ID\n");
422 abort();
Andrzej Puzdrowskie575fe92019-03-14 12:20:19 +0100423}
424
Dominik Ermelc304a7f2021-06-14 16:08:42 +0000425uint8_t flash_area_get_device_id(const struct flash_area *fa)
426{
427 (void)fa;
428 return 0;
429}
430
Fabio Utzig9b0ee902017-11-23 19:49:00 -0200431void sim_assert(int x, const char *assertion, const char *file, unsigned int line, const char *function)
432{
433 if (!(x)) {
Fabio Utzig8000e322019-08-05 08:14:32 -0300434 struct sim_context *ctx = sim_get_context();
435 if (ctx->c_catch_asserts) {
436 ctx->c_asserts++;
Fabio Utzig9b0ee902017-11-23 19:49:00 -0200437 } else {
438 BOOT_LOG_ERR("%s:%d: %s: Assertion `%s' failed.", file, line, function, assertion);
439
440 /* NOTE: if the assert below is triggered, the place where it was originally
441 * asserted is printed by the message above...
442 */
443 assert(x);
444 }
445 }
446}
David Browne0bb1f92019-10-01 15:57:01 -0600447
448uint32_t boot_max_align(void)
449{
450 return BOOT_MAX_ALIGN;
451}
David Brown2b8a6952019-10-01 16:14:53 -0600452
453uint32_t boot_magic_sz(void)
454{
455 return BOOT_MAGIC_SZ;
456}