forked from wolfSSL/wolfBoot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsim.c
More file actions
194 lines (161 loc) · 3.99 KB
/
sim.c
File metadata and controls
194 lines (161 loc) · 3.99 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
/* 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 2 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
*/
#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 "wolfboot/wolfboot.h"
#include "target.h"
static uint8_t *ram_base;
static uint8_t *flash_base;
#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;
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) {
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;
if (address != NULL && mmaped_addr != address) {
munmap(address, st.st_size);
return -1;
}
*ret_address = mmaped_addr;
close(fd);
return 0;
}
void hal_flash_unlock(void)
{
/* no op */
}
void hal_flash_lock(void)
{
/* no op */
}
void hal_prepare_boot(void)
{
/* no op */
}
int hal_flash_write(uint32_t address, const uint8_t *data, int len)
{
uint8_t *ptr = 0;
/* implicit cast abide compiler warning */
memcpy(ptr + address, data, len);
return 0;
}
int hal_flash_erase(uint32_t address, int len)
{
uint8_t *ptr = 0;
/* implicit cast abide compiler warning */
memset(ptr + address, 0xff, len);
return 0;
}
void hal_init(void)
{
int ret;
uint8_t *p;
ret = mmap_file(INTERNAL_FLASH_FILE,
(uint8_t*)WOLFBOOT_PARTITION_BOOT_ADDRESS, &p);
if (ret != 0) {
printf("failed to load internal flash file\n");
exit(-1);
}
#ifdef EXT_FLASH
ret = mmap_file(EXTERNAL_FLASH_FILE, NULL, &flash_base);
if (ret != 0) {
printf("failed to load internal flash file\n");
exit(-1);
}
#endif /* EXT_FLASH */
}
void ext_flash_lock(void)
{
/* no op */
}
void ext_flash_unlock(void)
{
/* no op */
}
int ext_flash_write(uintptr_t address, const uint8_t *data, int len)
{
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)
{
memset(flash_base + address, 0xff, len);
return 0;
}
void do_boot(const uint32_t *app_offset)
{
char *envp[1] = {NULL};
int ret;
int fd;
fd = memfd_create("test_app", 0);
if (fd == -1) {
printf("memfd error\n");
exit(-1);
}
ret = write(fd, app_offset, WOLFBOOT_PARTITION_SIZE);
if (ret != WOLFBOOT_PARTITION_SIZE) {
printf("can't write test-app to memfd\n");
exit(-1);
}
ret = fexecve(fd, main_argv, envp);
printf("fexecve error\n");
exit(1);
}
int wolfBoot_fallback_is_possible(void)
{
return 0;
}
int wolfBoot_dualboot_candidate(void)
{
return 0;
}