-
Notifications
You must be signed in to change notification settings - Fork 21
/
main.cc
279 lines (228 loc) · 9.48 KB
/
main.cc
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
// main.cc -- May 9, 2009
// by geohot
// part of "The Embedded Disassembler"
// released under GPLv3, see http://gplv3.fsf.org/
// This EDA is just focused on being a simulator first
// No threading, no servers, no bullshit
#include <iostream>
#include <iomanip>
#include <string>
#include "logic.h"
#include "data_memory.h"
#include "util.h"
#include "Servlet.h"
#include "FactoryOwner.h"
#include "debug.h"
#include "InstructionFactoryARM.h"
#include "InstructionFactoryISDF.h"
#include "JSON.h"
using namespace std;
using namespace eda;
void load_file(Memory *m, ChangelistFactory *cf, Address* me, const string& filename, uint32_t address) {
LOG(INFO) << "loading file, " << filename;
string data;
if(!file_to_string(filename, &data)) {
LOG(WARNING) << "File read error";
}
LOG(INFO) << "file read, " << data.size();
m->AllocateSegment(address, data.size());
LOG(INFO) << "segment allocated";
Changelist* c = cf->CreateFromInput(me, data, m->get_address_by_location(address));
LOG(INFO) << "changelist " << c->get_changelist_number() << " created, "<< c->get_size();
m->Commit(c);
LOG(INFO) << "committed";
}
Servlet<FactoryOwner> s;
void quitproc(int a) {
LOG(INFO) << "Exiting normally";
s.EndServer();
exit(0);
}
int main(int argc, char* argv[]) {
LOG(INFO) << "eda started";
/*JSON test;
test.add("hello", 1);
test.add("goodbye", "world");
vector<int> a;
a.push_back(1);
a.push_back(2);
test.add("arr", a);
LOG(INFO) << test.serialize();
return 0;*/
//LOG(INFO) << "Servlet constructed";
FactoryOwner f;
LOG(INFO) << "FactoryOwner constructed";
#ifndef WIN32
signal(SIGHUP, quitproc);
signal(SIGINT, quitproc);
signal(SIGQUIT, quitproc);
#endif
LOG(INFO) << "exit signals registered";
Address* me = f.memory_.AllocateSegment("me", 4); // Create the `me` address, 4 is just to prevent crashing
LOG(INFO) << "me address allocated";
/*Address* test = f.memory_.AllocateSegment("test", 8);
test->set32(1, 0x465E);
InstructionFactoryISDF a("thumb.isdf", &f.memory_);
a.Process(test);*/
load_file(&f.memory_, &f.changelist_factory_, me, "appldr.text", 0x12C00);
//load_file(&f.memory_, &f.changelist_factory_, me, "bootrom", 0x400000);
//load_file(&f.memory_, &f.changelist_factory_, me, "ibec.dfu", 0x18000000);
//f.memory_.ResolveToAddress(0,"`PC`")->set32(1, 0x1800F99C);
/*f.memory_.ResolveToAddress(0,"`PC`")->set32(1, 0x400024);
f.instruction_factory_->FastAnalyse(&f.memory_, f.memory_.ResolveToAddress(0, "[`PC`] - 8"));
f.memory_.ResolveToAddress(0,"`PC`")->set32(1, 0x400020);
f.instruction_factory_->FastAnalyse(&f.memory_, f.memory_.ResolveToAddress(0, "[`PC`] - 8"));*/
/*f.memory_.ResolveToAddress(0,"`PC`")->set32(1, 0x40001C);
f.instruction_factory_->FastAnalyse(&f.memory_, f.memory_.ResolveToAddress(0, "[`PC`] - 8"));*/
/*f.memory_.ResolveToAddress(0,"`PC`")->set32(1, 0x400018);
f.instruction_factory_->FastAnalyse(&f.memory_, f.memory_.ResolveToAddress(0, "[`PC`] - 8"));
f.memory_.ResolveToAddress(0,"`PC`")->set32(1, 0x400014);
f.instruction_factory_->FastAnalyse(&f.memory_, f.memory_.ResolveToAddress(0, "[`PC`] - 8"));
f.memory_.ResolveToAddress(0,"`PC`")->set32(1, 0x400010);
f.instruction_factory_->FastAnalyse(&f.memory_, f.memory_.ResolveToAddress(0, "[`PC`] - 8"));
f.memory_.ResolveToAddress(0,"`PC`")->set32(1, 0x40000C);
f.instruction_factory_->FastAnalyse(&f.memory_, f.memory_.ResolveToAddress(0, "[`PC`] - 8"));
f.memory_.ResolveToAddress(0,"`PC`")->set32(1, 0x400008);
f.instruction_factory_->FastAnalyse(&f.memory_, f.memory_.ResolveToAddress(0, "[`PC`] - 8"));*/
/*f.instruction_factory_->FastAnalyse(&f.memory_, f.memory_.ResolveToAddress(0, "0x400004"));
f.instruction_factory_->FastAnalyse(&f.memory_, f.memory_.ResolveToAddress(0, "0x400008"));
f.instruction_factory_->FastAnalyse(&f.memory_, f.memory_.ResolveToAddress(0, "0x40000C"));
f.instruction_factory_->FastAnalyse(&f.memory_, f.memory_.ResolveToAddress(0, "0x400010"));
f.instruction_factory_->FastAnalyse(&f.memory_, f.memory_.ResolveToAddress(0, "0x400014"));
f.instruction_factory_->FastAnalyse(&f.memory_, f.memory_.ResolveToAddress(0, "0x400018"));
f.instruction_factory_->FastAnalyse(&f.memory_, f.memory_.ResolveToAddress(0, "0x40001C"));*/
//f.instruction_factory_->FastAnalyse(&f.memory_, f.memory_.ResolveToAddress(0, "[`PC`] - 4"));
/*f.memory_.AllocateSegment(0, 0x40000);
f.memory_.ResolveToAddress(0,"`SP`")->set32(1, 0x10000);
f.memory_.AllocateSegment(0xf4300000, 0x100);
f.memory_.AllocateSegment(0xf4400000, 0x100);*/
f.memory_.ResolveToAddress(0,"`PC`")->set32(1, 0x12C00);
s.RegisterCommandHandler("GET", &f, &FactoryOwner::HandleGetRequest);
s.RegisterCommandHandler("EVAL", &f, &FactoryOwner::HandleEvalRequest);
s.RegisterCommandHandler("READ", &f, &FactoryOwner::HandleReadRequest);
s.RegisterCommandHandler("STEP", &f, &FactoryOwner::HandleStepRequest);
s.RegisterCommandHandler("RENAME", &f, &FactoryOwner::HandleRenameRequest);
s.RegisterCommandHandler("DISASSEMBLE", &f, &FactoryOwner::HandleDisassembleRequest);
s.StartServer(8080);
return 0;
}
int frontend_console() {
ChangelistFactory* cf;
Memory* m;
Address* me;
cf = new ChangelistFactory();
m = new Memory();
me = m->AllocateSegment("me", 4); // Create the `me` address, 4 is just to prevent crashing
load_file(m, cf, me, "bootrom", 0x400000);
m->AllocateSegment(0xf4300000, 0x100);
m->AllocateSegment(0xf4400000, 0x100);
InstructionFactoryARM iarm;
iarm.InitRegisters(m);
Address* PC = m->ResolveToAddress(0,"`PC`");
//INFO << "got PC" << endl;
PC->set32(1, 0x1800F99C);
Address* next_disassembly_address;
while (1) {
string cmd;
cout << "EDA> ";
getline(cin, cmd);
if(cmd.substr(0,2) == "cl") {
StatelessChangelist statelesscl;
while (1) {
cout << "cl> ";
getline(cin, cmd);
if(cmd == "") break;
string ls = cmd.substr(0, cmd.find_first_of('='));
string rs = cmd.substr(cmd.find_first_of('=')+1);
statelesscl.add_change(ls, "1", 4, rs);
}
if(statelesscl.get_size() > 0) {
DebugPrint(&statelesscl);
Changelist* c = cf->CreateFromStatelessChangelist(me, statelesscl, m);
//DebugPrint(c);
m->Commit(c);
}
} else if(cmd.substr(0,7) == "printcl") {
DebugPrint(m->history_.get_changelist(stoi(cmd.substr(8))));
} else if(cmd.substr(0,1) == "g") { // Go
Address* a;
while(1) {
a = m->ResolveToAddress(0, "[`PC`]-8"); // hardcoded for now
if(a->get_instruction() == NULL) {
next_disassembly_address = iarm.Process(a);
} //else
//break; //been here before
cout << "******* " << a->get_name() << endl;
cout << "******* ";
DebugPrint(a->get_instruction()->parsed_);
if (a->get_instruction()->change_->get_size() == 1) {
cout << "*******UNIMPLEMENTED INSTRUCTION" << endl;
}
DebugPrint(a->get_instruction()->change_);
cout << "*******" << endl;
Changelist* c = cf->CreateFromStatelessChangelist(a, *(a->get_instruction()->change_), m);
DebugPrint(c);
m->Commit(c);
}
} else if(cmd.substr(0,1) == "r") {
Address* a;
if(cmd.length() > 1)
a = m->ResolveToAddress(0, cmd.substr(cmd.find_first_of(' ')+1));
else
a = m->ResolveToAddress(0, "[`PC`]-8");
if(a->get_instruction() == NULL) {
next_disassembly_address = iarm.Process(a);
}
cout << "******* " << a->get_name() << endl;
cout << "******* ";
DebugPrint(a->get_instruction()->parsed_);
DebugPrint(a->get_instruction()->change_);
cout << "*******" << endl;
Changelist* c = cf->CreateFromStatelessChangelist(a, *(a->get_instruction()->change_), m);
DebugPrint(c);
m->Commit(c);
} else if(cmd.substr(0,1) == "d") {
Address* a;
if(cmd.length() > 1)
a = m->ResolveToAddress(0, cmd.substr(cmd.find_first_of(' ')+1));
else {
a = next_disassembly_address;
}
next_disassembly_address = iarm.Process(a);
cout << a->get_name() << endl;
DebugPrint(a->get_instruction()->parsed_);
DebugPrint(a->get_instruction()->change_);
} else if(cmd.substr(0,7) == "history") {
Address* a = m->ResolveToAddress(0, cmd.substr(8));
if (a == NULL) {
cout << "address not found" << endl;
continue;
}
vector<int>* changes = m->history_.get_xrefs(a);
if (changes == NULL) {
cout << "no history yet" << endl;
continue;
}
cout << "got modifiers " << changes->size() << endl;
for (vector<int>::iterator it = changes->begin(); it != changes->end(); ++it) {
uint32_t t;
a->get32(*it, &t);
cout << m->history_.get_changelist(*it)->get_owner()->get_name() << " ";
cout << setfill(' ') << dec << setw(3) << (*it) << ": " << hex << t << endl;
}
} else if(cmd.substr(0,3) == "new") {
string segname = cmd.substr(cmd.find_first_of(' ')+1);
m->AllocateSegment(segname, 4);
cout << "segment " << segname << " created" << endl;
} else if(cmd.substr(0,1) == "q") {
return 0;
} else if(cmd.substr(0,1) == "c") {
int clnum = stoi(cmd.substr(1));
uint32_t output = m->ResolveToNumber(clnum, cmd.substr(cmd.find(' ')+1));
cout << std::hex << "0x" << output << " at cl " << dec << clnum << endl;
} else {
uint32_t output = m->ResolveToNumber(0, cmd);
cout << std::hex << "0x" << output << endl;
}
}
}