-
Notifications
You must be signed in to change notification settings - Fork 1
/
Patcher.cpp
306 lines (260 loc) · 8.75 KB
/
Patcher.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
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
#include "Patcher.h"
#include <QDir>
#include <QDateTime>
#define COUNT_OF_PATCHES 8
#define DELAY 50
Patcher::Patcher() :
foundPatches(0), appliedPatches(0), countOfPatches(0),
progressValue(0)
{
fileNameCG45 = dirNameCG45 = patchedFileNameCG45 = "";
moveToThread(this);
}
void Patcher::run()
{
foundPatches = appliedPatches = countOfPatches = 0;
emit clearLogArea();
emit toProgressBar(0);
emit toLogArea(Message, tr("Patching started... %1").arg(QDateTime::currentDateTime().toString("hh:mm:ss")));
if(createPatchFile()) {
emit toLogArea(Success, tr("Done: %1/%2 applied patches").arg(appliedPatches).arg(countOfPatches));
emit toLogArea(Message, tr("Patching ended. %1").arg(QDateTime::currentDateTime().toString("hh:mm:ss")));
}
emit done();
}
bool Patcher::createPatchFile()
{
QFile fileCG45(dirNameCG45 + QDir::separator() + fileNameCG45);
if (fileCG45.open(QIODevice::ReadOnly)) {
fileCG45.seek(0);
QByteArray header = fileCG45.read(16);
EPhoneModels phone = determinePhoneModel(header);
fileCG45.seek(0);
switch (phone) {
case ZN5: {
emit toLogArea(Message, tr("CG45 from <strong>Motorola ZN5</strong> (64 MB RAM)"));
patchCG45to(fileCG45, ZN5);
break;
}
case ZN5Tmobile: {
emit toLogArea(Message, tr("CG45 from <strong>Motorola ZN5 T-Mobile</strong> (128 MB RAM)"));
patchCG45to(fileCG45, ZN5Tmobile);
break;
}
case UnknownModel:
default: {
emit toLogArea(Error, tr("File isn't recognize! This is CG45.smg?"));
fileCG45.close();
return false;
}
}
} else {
emit toLogArea(Error, tr("Can't open file: %1").arg(fileCG45.errorString()));
return false;
}
fileCG45.close();
return true;
}
Patcher::EPhoneModels Patcher::determinePhoneModel(const QByteArray &aHeader)
{
int zn5 = 0;
int zn5t = 0;
for (int i = 1; i < 10; i+=8) { // The 1 and 9 bytes in header
switch ((unsigned char)aHeader.at(i)) { // To unsigned
case 0x93: { // ZN5
++zn5;
break;
}
case 0x97: { // ZN5 T-mobile
++zn5t;
break;
}
default: {
return UnknownModel;
}
}
}
if (zn5 == 2) {
return ZN5;
}
if (zn5t == 2) {
return ZN5Tmobile;
}
return UnknownModel;
}
bool Patcher::patchCG45to(QFile &aFile, const EPhoneModels aPhone)
{
QByteArray ramByteArray(aFile.readAll());
QBuffer buffer(&ramByteArray);
if (!buffer.open(QIODevice::ReadWrite)) {
emit toLogArea(Error, tr("QBuffer error: %1").arg(buffer.errorString()));
return false;
}
if (aPhone == UnknownModel) {
emit toLogArea(Error, tr("Bad CG45 file!"));
return false;
}
// Patch #1
applyPatch(buffer, 1, 0x0000E8F4, "80");
// Patch #2
applyPatch(buffer, 1, 0x0000E901, "20");
// Patch #3
applyPatch(buffer, 4, 0x0000ED16, "81C0719F");
switch (aPhone) {
case ZN5: {
// Patch #4
applyPatch(buffer, 4, 0x00038C10, "8FC0719F");
// Patch #5
applyPatch(buffer, 2, 0x0006FEC4, "C090");
// Patch #6
applyPatch(buffer, 8, 0x000E90C8, "C090C090C090C090");
// Patch #7
applyPatch(buffer, 6, 0x0008E154, "C090C090C090");
break;
}
case ZN5Tmobile: {
// Patch #4
applyPatch(buffer, 4, 0x00036870, "8FC0719F");
// Patch #5
applyPatch(buffer, 2, 0x0007175C, "C090");
// Patch #6
applyPatch(buffer, 8, 0x00096020, "C090C090C090C090");
// Patch #7
applyPatch(buffer, 6, 0x00096960, "C090C090C090");
break;
}
default:
case UnknownModel: {
return false;
}
}
// Patch #8
fillBytes(buffer, 1128, 0x003DF800, "FF");
buffer.close();
writePatchedCG45file(dirNameCG45 + QDir::separator() + patchedFileNameCG45, ramByteArray);
return true;
}
void Patcher::patchFound(int aPatch, const QByteArray &aValue)
{
++foundPatches;
++countOfPatches;
emit toLogArea(Warning, QString("#%1: Patch 0x%2 is found. Value (HEX) is %3")
.arg(countOfPatches)
.arg(QString::number(aPatch, 16).toUpper())
.arg(QString(aValue.toHex())));
}
void Patcher::patchFound(int aBegin, int aEnd, const QString &aValue)
{
++foundPatches;
++countOfPatches;
emit toLogArea(Warning, QString("#%1: Patch filling 0x%2..0x%3 is found. Value (HEX) is %4")
.arg(countOfPatches)
.arg(QString::number(aBegin, 16).toUpper())
.arg(QString::number(aEnd, 16).toUpper())
.arg(aValue.toUpper()));
}
void Patcher::patchApplied(int aPatch, const QByteArray &aValue, const QByteArray &hexString)
{
++appliedPatches;
++countOfPatches;
emit toLogArea(Success, QString("#%1: Patch 0x%2 is applied. Value (HEX) is %3 -> %4")
.arg(countOfPatches)
.arg(QString::number(aPatch, 16).toUpper())
.arg(QString(aValue.toHex()))
.arg(QString(hexString)));
if (appliedPatches < COUNT_OF_PATCHES) {
emit toProgressBar(appliedPatches * (100 / COUNT_OF_PATCHES));
} else {
emit toProgressBar(100);
}
}
void Patcher::patchApplied(int aBegin, int aEnd, const QString &aFromValue, const QString &aToValue)
{
++appliedPatches;
++countOfPatches;
emit toLogArea(Success, QString("#%1: Patch 0x%2..0x%3 is filled. Value (HEX) is %4 -> %5")
.arg(countOfPatches)
.arg(QString::number(aBegin, 16).toUpper())
.arg(QString::number(aEnd, 16).toUpper())
.arg(aFromValue.toUpper())
.arg(aToValue.toUpper()));
if (appliedPatches < COUNT_OF_PATCHES) {
emit toProgressBar(appliedPatches * (100 / COUNT_OF_PATCHES));
} else {
emit toProgressBar(100);
}
}
void Patcher::applyPatch(QBuffer &aBuffer, int aLength, int aOffset, const QByteArray &hexString)
{
aBuffer.seek(aOffset);
QByteArray valueBA = aBuffer.read(aLength);
aBuffer.seek(aOffset);
if (valueBA == QByteArray::fromHex(hexString)) {
patchFound(aOffset, valueBA);
} else {
aBuffer.write(QByteArray::fromHex(hexString));
patchApplied(aOffset, valueBA, hexString);
}
aBuffer.seek(0);
msleep(DELAY);
}
void Patcher::fillBytes(QBuffer &aBuffer, int aLength, int aOffset, const QByteArray &hexString)
{
aBuffer.seek(aOffset);
QByteArray fillValues = aBuffer.read(aLength);
bool isFill = true;
for (int i = 0; i < aLength; ++i) {
if (fillValues.at(i) != QByteArray::fromHex(hexString).at(0)) {
isFill = false;
break;
}
}
aBuffer.seek(aOffset);
if (isFill) {
patchFound(aOffset, aOffset + aLength,
QString::number((unsigned char)fillValues.at(0), 16) + "..." +
QString::number((unsigned char)fillValues.at(fillValues.size() - 1), 16));
} else {
QByteArray fillArray;
for (int i = 0; i < aLength; ++i) {
fillArray.append(QByteArray::fromHex(hexString));
}
aBuffer.write(fillArray);
patchApplied(aOffset, aOffset + aLength,
QString::number((unsigned char)fillValues.at(0), 16) + "..." +
QString::number((unsigned char)fillValues.at(fillValues.size() - 1), 16),
QString::number((unsigned char)fillArray.at(0), 16) + "..." +
QString::number((unsigned char)fillArray.at(fillArray.size() - 1), 16));
}
aBuffer.seek(0);
msleep(DELAY);
}
bool Patcher::writePatchedCG45file(const QString &aFullName, const QByteArray &aByteArray)
{
QFile file(aFullName);
if (!file.open(QIODevice::WriteOnly)) {
emit toLogArea(Error, tr("Cannot open file for write: %1").arg(file.errorString()));
return false;
} else {
file.write(aByteArray);
file.close();
emit toLogArea(Success, tr("File: <strong>%1</strong> successfully writed!").arg(patchedFileNameCG45));
return true;
}
}
void Patcher::setFileName(const QString aFileName)
{
fileNameCG45 = aFileName;
}
void Patcher::setDirName(const QString aDirName)
{
dirNameCG45 = aDirName;
}
void Patcher::setPatchedFileName(const QString aFileName)
{
patchedFileNameCG45 = aFileName;
}
Patcher::~Patcher()
{
/* Empty Destructor */
}