forked from wolfSSL/wolfBoot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsim.c
More file actions
586 lines (497 loc) · 15.1 KB
/
sim.c
File metadata and controls
586 lines (497 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/* sim.c
*
* Copyright (C) 2022 wolfSSL Inc.
*
* This file is part of wolfBoot.
*
* wolfBoot is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfBoot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
/* Note: All logging must use stderr to avoid issue with scripts
* printing version information */
#define _GNU_SOURCE
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#ifdef __APPLE__
#include <mach-o/loader.h>
#include <mach-o/nlist.h>
#include <mach-o/dyld.h>
#endif
#include "wolfboot/wolfboot.h"
#include "target.h"
#include "printf.h"
#ifdef WOLFBOOT_ELF_FLASH_SCATTER
#include "elf.h"
#endif
#ifdef WOLFBOOT_ENABLE_WOLFHSM_CLIENT
#include "wolfhsm/wh_error.h"
#include "wolfhsm/wh_client.h"
#include "port/posix/posix_transport_tcp.h"
#elif defined(WOLFBOOT_ENABLE_WOLFHSM_SERVER) /*WOLFBOOT_ENABLE_WOLFHSM_CLIENT*/
#include "wolfhsm/wh_error.h"
#include "wolfhsm/wh_server.h"
#include "wolfhsm/wh_server_keystore.h"
#include "wolfhsm/wh_nvm.h"
#include "wolfhsm/wh_nvm_flash.h"
#include "wolfhsm/wh_transport_mem.h"
#include "port/posix/posix_flash_file.h"
#endif /* WOLFBOOT_ENABLE_WOLFHSM_SERVER */
/* Global pointer to the internal and external flash base */
uint8_t *sim_ram_base;
static uint8_t *flash_base;
int forceEmergency = 0;
uint32_t erasefail_address = 0xFFFFFFFF;
int flashLocked = 1;
int extFlashLocked = 1;
#define INTERNAL_FLASH_FILE "./internal_flash.dd"
#define EXTERNAL_FLASH_FILE "./external_flash.dd"
/* global used to store command line arguments to forward to the test
* application */
char **main_argv;
int main_argc;
#ifdef WOLFBOOT_ENABLE_WOLFHSM_CLIENT
/* Client configuration/contexts */
static whTransportClientCb pttccb[1] = {PTT_CLIENT_CB};
static posixTransportTcpClientContext tcc[1] = {};
static posixTransportTcpConfig mytcpconfig[1] = {{
.server_ip_string = "127.0.0.1",
.server_port = 23456,
}};
static whCommClientConfig cc_conf[1] = {{
.transport_cb = pttccb,
.transport_context = (void*)tcc,
.transport_config = (void*)mytcpconfig,
.client_id = 12,
}};
static whClientConfig c_conf[1] = {{
.comm = cc_conf,
}};
/* Globally exported HAL symbols */
whClientContext hsmClientCtx = {0};
const int hsmDevIdHash = WH_DEV_ID;
const int hsmDevIdPubKey = WH_DEV_ID;
const int hsmKeyIdPubKey = 0xFF;
#ifdef EXT_ENCRYPT
#error "Simulator does not support firmware encryption with wolfHSM(yet)"
const int hsmDevIdCrypt = WH_DEV_ID;
const int hsmKeyIdCrypt = 0xFF;
#endif
#ifdef WOLFBOOT_CERT_CHAIN_VERIFY
const whNvmId hsmNvmIdCertRootCA = 1;
#endif
int hal_hsm_init_connect(void);
int hal_hsm_disconnect(void);
#elif defined(WOLFBOOT_ENABLE_WOLFHSM_SERVER) /*WOLFBOOT_ENABLE_WOLFHSM_CLIENT*/
/* HAL Flash state and configuration */
const whFlashCb fcb[1] = {POSIX_FLASH_FILE_CB};
posixFlashFileContext fc[1] = {0};
posixFlashFileConfig fc_conf[1] = {{
.filename = "wolfBoot_wolfHSM_NVM.bin",
.partition_size = 16384,
.erased_byte = (uint8_t)0,
}};
/* NVM Configuration using PosixSim HAL Flash */
whNvmFlashConfig nf_conf[1] = {{
.cb = fcb,
.context = fc,
.config = fc_conf,
}};
whNvmFlashContext nfc[1] = {0};
whNvmCb nfcb[1] = {WH_NVM_FLASH_CB};
whNvmConfig n_conf[1] = {{
.cb = nfcb,
.context = nfc,
.config = nf_conf,
}};
whNvmContext nvm[1] = {{0}};
static uint8_t req[] = {0};
static uint8_t resp[] = {0};
whTransportMemConfig tmcf[1] = {{
.req = (whTransportMemCsr*)req,
.req_size = sizeof(req),
.resp = (whTransportMemCsr*)resp,
.resp_size = sizeof(resp),
}};
whTransportServerCb tscb[1] = {WH_TRANSPORT_MEM_SERVER_CB};
whTransportMemServerContext tmsc[1] = {0};
/* Dummy comm server config */
whCommServerConfig cs_conf[1] = {{
.transport_cb = tscb,
.transport_context = &tmsc,
.transport_config = &tmcf,
.server_id = 0,
}};
/* Crypto context */
whServerCryptoContext crypto[1] = {{
.devId = INVALID_DEVID,
}};
#if defined(WOLFHSM_CFG_SHE_EXTENSION)
whServerSheContext she[1] = {{0}};
#endif
whServerConfig s_conf[1] = {{
.comm_config = cs_conf,
.nvm = nvm,
.crypto = crypto,
}};
whServerContext hsmServerCtx = {0};
const int hsmDevIdHash = INVALID_DEVID;
const int hsmDevIdPubKey = INVALID_DEVID;
const whNvmId hsmNvmIdCertRootCA = 1;
#ifdef EXT_ENCRYPT
#error "Simulator does not support firmware encryption with wolfHSM(yet)"
const int hsmDevIdCrypt = WH_DEV_ID;
const int hsmKeyIdCrypt = 0xFF;
#endif
int hal_hsm_server_init(void);
int hal_hsm_server_cleanup(void);
#endif /* WOLFBOOT_ENABLE_WOLFHSM_SERVER*/
static int mmap_file(const char *path, uint8_t *address, uint8_t** ret_address)
{
struct stat st = { 0 };
uint8_t *mmaped_addr;
int ret;
int fd;
if (path == NULL)
return -1;
ret = stat(path, &st);
if (ret == -1)
return -1;
fd = open(path, O_RDWR);
if (fd == -1) {
wolfBoot_printf( "can't open %s\n", path);
return -1;
}
mmaped_addr = mmap(address, st.st_size, PROT_READ | PROT_WRITE,
MAP_SHARED, fd, 0);
if (mmaped_addr == MAP_FAILED)
return -1;
wolfBoot_printf( "Simulator assigned %s to base %p\n", path, mmaped_addr);
*ret_address = mmaped_addr;
close(fd);
return 0;
}
void hal_flash_unlock(void)
{
flashLocked = 0;
}
void hal_flash_lock(void)
{
flashLocked = 1;
}
void hal_prepare_boot(void)
{
/* no op */
}
int hal_flash_write(uintptr_t address, const uint8_t *data, int len)
{
int i;
if (flashLocked == 1) {
wolfBoot_printf("FLASH IS BEING WRITTEN TO WHILE LOCKED\n");
return -1;
}
if (forceEmergency == 1 && address == WOLFBOOT_PARTITION_BOOT_ADDRESS) {
/* implicit cast abide compiler warning */
memset((void*)address, 0, len);
/* let the rest of the writes work properly for the emergency update */
forceEmergency = 0;
}
else {
for (i = 0; i < len; i++) {
#ifdef NVM_FLASH_WRITEONCE
uint8_t *addr = (uint8_t *)address;
if (addr[i] != FLASH_BYTE_ERASED) {
/* no writing to non-erased page in NVM_FLASH_WRITEONCE */
wolfBoot_printf("NVM_FLASH_WRITEONCE non-erased write detected at address %p!\n", addr);
wolfBoot_printf("Address[%d] = %02x\n", i, addr[i]);
return -1;
}
#endif
#ifdef WOLFBOOT_FLAGS_INVERT
((uint8_t*)address)[i] |= data[i];
#else
((uint8_t*)address)[i] &= data[i];
#endif
}
}
return 0;
}
int hal_flash_erase(uintptr_t address, int len)
{
if (flashLocked == 1) {
wolfBoot_printf("FLASH IS BEING ERASED WHILE LOCKED\n");
return -1;
}
/* implicit cast abide compiler warning */
wolfBoot_printf( "hal_flash_erase addr %p len %d\n", (void*)address, len);
if (address == erasefail_address + WOLFBOOT_PARTITION_BOOT_ADDRESS) {
wolfBoot_printf( "POWER FAILURE\n");
/* Corrupt page */
memset((void*)address, 0xEE, len);
exit(0);
}
memset((void*)address, FLASH_BYTE_ERASED, len);
return 0;
}
void hal_init(void)
{
int ret;
int i;
ret = mmap_file(INTERNAL_FLASH_FILE,
(uint8_t*)ARCH_FLASH_OFFSET, &sim_ram_base);
if (ret != 0) {
wolfBoot_printf( "failed to load internal flash file\n");
exit(-1);
}
#ifdef EXT_FLASH
ret = mmap_file(EXTERNAL_FLASH_FILE,
(uint8_t*)ARCH_FLASH_OFFSET + 0x10000000, &flash_base);
if (ret != 0) {
wolfBoot_printf( "failed to load external flash file\n");
exit(-1);
}
#endif /* EXT_FLASH */
for (i = 1; i < main_argc; i++) {
if (strcmp(main_argv[i], "powerfail") == 0) {
erasefail_address = strtol(main_argv[++i], NULL, 16);
wolfBoot_printf( "Set power fail to erase at address %x\n",
erasefail_address);
}
/* force a bad write of the boot partition to trigger and test the
* emergency fallback feature */
else if (strcmp(main_argv[i], "emergency") == 0)
forceEmergency = 1;
}
}
void ext_flash_lock(void)
{
extFlashLocked = 1;
}
void ext_flash_unlock(void)
{
extFlashLocked = 0;
}
int ext_flash_write(uintptr_t address, const uint8_t *data, int len)
{
if (extFlashLocked == 1) {
wolfBoot_printf("EXT FLASH IS BEING WRITTEN TO WHILE LOCKED\n");
return -1;
}
memcpy(flash_base + address, data, len);
return 0;
}
int ext_flash_read(uintptr_t address, uint8_t *data, int len)
{
memcpy(data, flash_base + address, len);
return len;
}
int ext_flash_erase(uintptr_t address, int len)
{
if (extFlashLocked == 1) {
wolfBoot_printf("EXT FLASH IS BEING ERASED WHILE LOCKED\n");
return -1;
}
memset(flash_base + address, FLASH_BYTE_ERASED, len);
return 0;
}
#ifdef __APPLE__
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
/* Find the MachO entry point */
static int find_epc(void *base, struct entry_point_command **entry)
{
struct mach_header_64 *mh;
struct load_command *lc;
int i;
unsigned long text = 0;
*entry = NULL;
mh = (struct mach_header_64*)base;
lc = (struct load_command*)((uint8_t *)base + sizeof(struct mach_header_64));
for (i=0; i<(int)mh->ncmds; i++) {
if (lc->cmd == LC_MAIN) { /* 0x80000028 */
*entry = (struct entry_point_command *)lc;
return 1;
}
lc = (struct load_command*)((unsigned long)lc + lc->cmdsize);
}
return 0;
}
#endif
void do_boot(const uint32_t *app_offset)
{
int ret;
size_t app_size = WOLFBOOT_PARTITION_SIZE - IMAGE_HEADER_SIZE;
wolfBoot_printf("Simulator do_boot app_offset = %p\n", app_offset);
if (flashLocked == 0) {
wolfBoot_printf("WARNING FLASH IS UNLOCKED AT BOOT");
}
if (extFlashLocked == 0) {
wolfBoot_printf("WARNING EXT FLASH IS UNLOCKED AT BOOT");
}
#ifdef __APPLE__
typedef int (*main_entry)(int, char**, char**, char**);
NSObjectFileImage fileImage = NULL;
NSModule module = NULL;
NSSymbol symbol = NULL;
void *pSymbolAddress = NULL;
struct entry_point_command *epc;
main_entry main;
uint32_t *app_buf = (uint32_t*)app_offset;
uint32_t typeVal;
/* change to mh_bundle type - workaround to load object */
typeVal = app_buf[3];
if (typeVal != MH_BUNDLE)
app_buf[3] = MH_BUNDLE;
ret = NSCreateObjectFileImageFromMemory(app_buf, app_size, &fileImage);
if (ret != 1 || fileImage == NULL) {
wolfBoot_printf( "Error loading object memory!\n");
exit(-1);
}
module = NSLinkModule(fileImage, "module",
(NSLINKMODULE_OPTION_PRIVATE | NSLINKMODULE_OPTION_BINDNOW));
symbol = NSLookupSymbolInModule(module, "__mh_execute_header");
pSymbolAddress = NSAddressOfSymbol(symbol);
if (!find_epc(pSymbolAddress, &epc)) {
wolfBoot_printf( "Error finding entry point!\n");
exit(-1);
}
/* restore mh_bundle type to allow hash to remain valid */
app_buf[3] = typeVal;
main = (main_entry)((uint8_t*)pSymbolAddress + epc->entryoff);
main(main_argc, main_argv, NULL, NULL);
#elif defined (WOLFBOOT_ELF_FLASH_SCATTER)
uint8_t *entry_point = (sim_ram_base + (unsigned long)app_offset);
printf("entry point: %p\n", entry_point);
printf("app offset: %p\n", app_offset);
typedef int (*main_entry)(int, char**);
main_entry main;
main = (main_entry)(entry_point);
/* TODO: call main ! */
/* main(main_argc, main_argv); */
wolfBoot_printf("Simulator for ELF_FLASH_SCATTER image not implemented yet. Exiting...\n");
exit(0);
#else
char *envp[1] = {NULL};
int fd = memfd_create("test_app", 0);
size_t wret;
if (fd == -1) {
wolfBoot_printf( "memfd error\n");
exit(-1);
}
wret = write(fd, app_offset, app_size);
if (wret != app_size) {
wolfBoot_printf( "can't write test-app to memfd, address %p\n", app_offset);
exit(-1);
}
wolfBoot_printf("Stored test-app to memfd, address %p (%zu bytes)\n", app_offset, wret);
ret = fexecve(fd, main_argv, envp);
wolfBoot_printf( "fexecve error\n");
#endif
exit(1);
}
#ifdef __APPLE__
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
#endif
int wolfBoot_fallback_is_possible(void)
{
return 0;
}
int wolfBoot_dualboot_candidate(void)
{
return 0;
}
#ifdef WOLFBOOT_ENABLE_WOLFHSM_CLIENT
int hal_hsm_init_connect(void)
{
int rc = 0;
rc = wh_Client_Init(&hsmClientCtx, c_conf);
if (rc != WH_ERROR_OK) {
fprintf(stderr, "Failed to initialize HSM client\n");
exit(-1);
}
rc = wh_Client_CommInit(&hsmClientCtx, NULL, NULL);
if (rc != WH_ERROR_OK) {
fprintf(stderr, "Failed to initialize HSM client communication\n");
exit(-1);
}
return rc;
}
int hal_hsm_disconnect(void)
{
int rc = 0;
rc = wh_Client_CommClose(&hsmClientCtx);
if (rc != WH_ERROR_OK) {
fprintf(stderr, "Failed to close HSM client connection\n");
exit(-1);
}
rc = wh_Client_Cleanup(&hsmClientCtx);
if (rc != WH_ERROR_OK) {
fprintf(stderr, "Failed to cleanup HSM client\n");
exit(-1);
}
return rc;
}
#elif defined(WOLFBOOT_ENABLE_WOLFHSM_SERVER) /*WOLFBOOT_ENABLE_WOLFHSM_CLIENT*/
int hal_hsm_server_init(void)
{
int rc = 0;
rc = wh_Nvm_Init(nvm, n_conf);
if (rc != 0) {
fprintf(stderr, "Failed to initialize NVM: %d\n", rc);
exit(-1);
}
wolfCrypt_Init();
rc = wc_InitRng_ex(crypto->rng, NULL, INVALID_DEVID);
if (rc != 0) {
fprintf(stderr, "Failed to initialize RNG: %d\n", rc);
exit(-1);
}
rc = wh_Server_Init(&hsmServerCtx, s_conf);
if (rc != 0) {
fprintf(stderr, "Failed to initialize HSM server: %d\n", rc);
exit(-1);
}
return rc;
}
int hal_hsm_server_cleanup(void)
{
int rc = 0;
rc = wh_Server_Cleanup(&hsmServerCtx);
if (rc != 0) {
fprintf(stderr, "Failed to cleanup HSM server: %d\n", rc);
exit(-1);
}
rc = wc_FreeRng(crypto->rng);
if (rc != 0) {
fprintf(stderr, "Failed to cleanup RNG: %d\n", rc);
exit(-1);
}
rc = wolfCrypt_Cleanup();
if (rc != 0) {
fprintf(stderr, "Failed to cleanup wolfCrypt: %d\n", rc);
exit(-1);
}
return rc;
}
#endif /* WOLFBOOT_ENABLE_WOLFHSM_SERVER */