-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcheck_sdk_mega.cpp
194 lines (179 loc) · 4.44 KB
/
check_sdk_mega.cpp
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
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <cstdlib>
#include <fstream>
#include "types.hpp"
#include "endian.hpp"
#include "dol.hpp"
#include "functions.hpp"
using namespace std;
int main(int argc, char **argv)
{
bool isDol = false;
bool createIDC = false;
bool ordered = false;
bool expanded = false;
std::vector<bool> arguments(argc, false);
if ( argc < 3 )
{
cout << "Usage: " << argv[0] <<
" <mega file> <dump or dol> [options]" << endl;
cout << "options:" << endl;
cout << "\t--dol using a dol" << endl;
cout << "\t--idc create idc file" << endl;
cout << "\t--ordered do functions in order" << endl;
cout << "\t--expanded add refs to list" << endl;
return EXIT_FAILURE;
}
if ( argc >= 4 )
{
for(int n = 1; n < argc; n++)
{
if ( !strncmp(argv[n], "--dol", 5) )
{
isDol = true;
arguments[n] = true;
}
if ( !strncmp(argv[n], "--idc", 5) )
{
createIDC = true;
arguments[n] = true;
}
if ( !strncmp(argv[n], "--ordered", 9) )
{
ordered = true;
arguments[n] = true;
}
if ( !strncmp(argv[n], "--expanded", 10) )
{
expanded = true;
arguments[n] = true;
}
}
}
int mega_index = 0;
int dol_index = 0;
for(int ii = 0; ii < argc; ii++)
{
if(arguments[ii] == false)
{
if(!mega_index)
mega_index = ii;
else if(!dol_index)
dol_index = ii;
}
}
ifstream myInputFile(argv[mega_index], ios::in);
if ( !myInputFile )
{
cout << "File ";
cout << argv[mega_index] << "could not be opened"
<< endl;
return EXIT_FAILURE;
}
vector<string> sigs;
string sLine;
while( getline(myInputFile, sLine) )
{
if ( !sLine.empty() )
sigs.push_back( sLine );
}
myInputFile.close();
ifstream memDump(argv[dol_index], ios::in);
if ( !memDump )
{
cout << "File ";
cout << argv[dol_index] << "could not be opened"
<< endl;
return EXIT_FAILURE;
}
memDump.seekg(0, ifstream::end);
u32 memDumpSize = memDump.tellg();
memDump.seekg(0);
char * buffer = new char[memDumpSize];
memDump.read(buffer, memDumpSize);
memDump.close();
// DO SOMETHING COOL //
if(createIDC)
{
cout << "#include <idc.idc>" << endl;
cout << "static main() {" << endl;
}
char* tmpBuf = buffer;
u32 size = memDumpSize;
u32 offset = 0;
for(u32 stri = 0; stri < sigs.size(); stri++)
{
//m_sig sig = ParseMegaLine(tmpBuf, size, sigs[stri], isDol);
m_sig sig = ParseMegaLine(sigs[stri]);
function_instance instance = FindMSig(tmpBuf, size, offset, sig, isDol);
if(instance.buffer_location)
{
if(createIDC)
{
cout << "\tMakeFunction(0x" << hex <<
instance.memory_address <<
", BADADDR); MakeName(0x" << hex <<
instance.memory_address << ", \"" <<
instance.sig.funcName << "\");" << endl;
if(expanded)
{
for(u32 ii=0; ii<instance.sig.refs.size(); ii++)
{
char* ref_offs = instance.buffer_location + instance.sig.refs[ii].first;
u32 insn = Big32(ref_offs);
u32 b_amt = insn ^ 0x48000000;
if ( b_amt & 0x2000000 )
b_amt = b_amt | 0xfd000000;
b_amt &= 0xfffffffe;
u32 ref_address = instance.memory_address + instance.sig.refs[ii].first;
ref_address += b_amt;
if ( ( insn & 0x48000000 ) == 0x48000000 )
{
cout << "\tMakeFunction(0x" << hex <<
ref_address <<
", BADADDR); MakeName(0x" << hex <<
ref_address << ", \"" <<
instance.sig.refs[ii].second << "\");" <<
endl;
}
}
}
} else {
cout << instance.sig.funcName << ": " << hex << instance.memory_address << endl;
if(expanded)
{
for(u32 ii=0; ii<instance.sig.refs.size(); ii++)
{
cout << "\t+" << hex << instance.sig.refs[ii].first << "\t" << instance.sig.refs[ii].second;
char* ref_offs = instance.buffer_location + instance.sig.refs[ii].first;
u32 insn = Big32(ref_offs);
u32 b_amt = insn ^ 0x48000000;
if ( b_amt & 0x2000000 )
b_amt = b_amt | 0xfd000000;
b_amt &= 0xfffffffe;
u32 ref_address = instance.memory_address + instance.sig.refs[ii].first;
ref_address += b_amt;
if ( ( insn & 0x48000000 ) == 0x48000000 )
cout << "\t" << hex << ref_address;
cout << endl;
}
}
}
}
if((instance.buffer_location) && (ordered))
{
tmpBuf = instance.buffer_location + 4;
offset = (u32)(instance.buffer_location - buffer) + 4;
size = memDumpSize - offset;
}
}
if(createIDC)
{
cout << "}" << endl;
}
delete [] buffer;
return EXIT_SUCCESS;
}