forked from techniker/uARM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_pc.c
237 lines (168 loc) · 3.98 KB
/
main_pc.c
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
#include "SoC.h"
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/select.h>
#include <signal.h>
#include <termios.h>
#define off64_t __off64_t
unsigned char* readFile(const char* name, UInt32* lenP){
long len = 0;
unsigned char *r = NULL;
int i;
FILE* f;
f = fopen(name, "r");
if(!f){
perror("cannot open file");
return NULL;
}
i = fseek(f, 0, SEEK_END);
if(i){
return NULL;
perror("cannot seek to end");
}
len = ftell(f);
if(len < 0){
perror("cannot get position");
return NULL;
}
i = fseek(f, 0, SEEK_SET);
if(i){
return NULL;
perror("cannot seek to start");
}
r = malloc(len);
if(!r){
perror("cannot alloc memory");
return NULL;
}
if(len != (long)fread(r, 1, len, f)){
perror("canot read file");
free(r);
return NULL;
}
*lenP = len;
return r;
}
static int ctlCSeen = 0;
static int readchar(void){
struct timeval tv;
fd_set set;
char c;
int i, ret = CHAR_NONE;
if(ctlCSeen){
ctlCSeen = 0;
return 0x03;
}
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&set);
FD_SET(0, &set);
i = select(1, &set, NULL, NULL, &tv);
if(i == 1 && 1 == read(0, &c, 1)){
ret = c;
}
return ret;
}
static void writechar(int chr){
if(!(chr & 0xFF00)){
printf("%c", chr);
}
else{
printf("<<~~ EC_0x%x ~~>>", chr);
}
fflush(stdout);
}
void ctl_cHandler(_UNUSED_ int v){ //handle SIGTERM
// exit(-1);
ctlCSeen = 1;
}
int rootOps(void* userData, UInt32 sector, void* buf, UInt8 op){
FILE* root = userData;
int i;
switch(op){
case BLK_OP_SIZE:
if(sector == 0){ //num blocks
if(root){
i = fseeko64(root, 0, SEEK_END);
if(i) return false;
*(unsigned long*)buf = (off64_t)ftello64(root) / (off64_t)BLK_DEV_BLK_SZ;
}
else{
*(unsigned long*)buf = 0;
}
}
else if(sector == 1){ //block size
*(unsigned long*)buf = BLK_DEV_BLK_SZ;
}
else return 0;
return 1;
case BLK_OP_READ:
i = fseeko64(root, (off64_t)sector * (off64_t)BLK_DEV_BLK_SZ, SEEK_SET);
if(i) return false;
return fread(buf, 1, BLK_DEV_BLK_SZ, root) == BLK_DEV_BLK_SZ;
case BLK_OP_WRITE:
i = fseeko64(root, (off64_t)sector * (off64_t)BLK_DEV_BLK_SZ, SEEK_SET);
if(i) return false;
return fwrite(buf, 1, BLK_DEV_BLK_SZ, root) == BLK_DEV_BLK_SZ;
}
return 0;
}
SoC soc;
int main(int argc, char** argv){
struct termios cfg, old;
FILE* root = NULL;
int gdbPort = 0;
if(argc != 3 && argc != 2){
fprintf(stderr,"usage: %s path_to_disk [gdbPort]\n", argv[0]);
return -1;
}
//setup the terminal
{
int ret;
ret = tcgetattr(0, &old);
cfg = old;
if(ret) perror("cannot get term attrs");
#ifndef _DARWIN_
cfg.c_iflag &=~ (INLCR | INPCK | ISTRIP | IUCLC | IXANY | IXOFF | IXON);
cfg.c_oflag &=~ (OPOST | OLCUC | ONLCR | OCRNL | ONOCR | ONLRET);
cfg.c_lflag &=~ (ECHO | ECHOE | ECHONL | ICANON | IEXTEN | XCASE);
#else
cfmakeraw(&cfg);
#endif
ret = tcsetattr(0, TCSANOW, &cfg);
if(ret) perror("cannot set term attrs");
}
root = fopen64(argv[1], "r+b");
if(!root){
fprintf(stderr,"Failed to open root device\n");
exit(-1);
}
if(argc >= 3) gdbPort = atoi(argv[2]);
socInit(&soc, socRamModeAlloc, NULL, readchar, writechar, rootOps, root);
signal(SIGINT, &ctl_cHandler);
socRun(&soc, gdbPort);
fclose(root);
tcsetattr(0, TCSANOW, &old);
return 0;
}
//////// runtime things
void* emu_alloc(UInt32 size){
return calloc(size,1);
}
void emu_free(void* ptr){
free(ptr);
}
UInt32 rtcCurTime(void){
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec;
}
void err_str(const char* str){
fprintf(stderr, "%s", str);
}