-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDumberDumb.cpp
218 lines (155 loc) · 4.98 KB
/
DumberDumb.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
#include <DumberDumb.h>
#include <DumberFile.h>
#include <e32debug.h>
#include <e32rom.h>
#include <hal.h>
const TInt KWriteChunkSize = 0x10000;
namespace Dumber {
TInt Dumb(TDesC& aPath, TUint32 aAddress, TUint32 aSize, TInt &aProgress) {
RFs fs;
fs.Connect(-1);
TDumberFile* dumber = NULL;
TRAPD(err, dumber = TDumberFile::NewL(fs, aPath, EDumberReplaceWrite));
if (err != KErrNone) {
fs.Close();
return err;
}
HBufC8* buf = NULL;
TRAP(err, buf = HBufC8::NewL(KWriteChunkSize));
if (err != KErrNone) {
delete dumber;
fs.Close();
return err;
}
TPtr8 bufDes(buf->Des());
TInt left = aSize;
TInt chunkSize = 0;
TInt originalSize = aSize;
while (left) {
chunkSize = left < KWriteChunkSize ? left : KWriteChunkSize;
bufDes.Copy((const TUint8*)(aAddress), chunkSize);
TRAP(err, dumber->WriteL(bufDes));
if (err) {
RDebug::Printf("Error!");
fs.Close();
delete dumber;
delete buf;
return err;
}
aAddress += chunkSize;
left -= chunkSize;
aProgress = 100 - (left * 100 / originalSize);
}
delete dumber;
delete buf;
fs.Close();
return KErrNone;
}
TInt DumbROM(TDesC& aPath, TInt &aProgress) {
TUint addr = UserSvr::RomHeaderAddress();
TRomHeader* aHeader = (TRomHeader*)(addr);
TUint size = aHeader->iRomSize - 0xA1000;
return Dumb(aPath, addr, size, aProgress);
}
struct TRpkgHeader {
TChar iMagic[4];
TVersion iAssociatedRomVersion;
TUint iFileCount;
TUint iHeaderSize;
TUint iMachineUID;
};
struct TRpkgEntry {
TUint64 iAtt;
TTime iLastModified;
TUint64 iFullPathLength;
};
struct TRpkgData {
TUint64 iDataSize;
};
const TInt KWriteDumbChunkSize = 0x10000;
static void CopyFileL(TDumberFile* aFile, TDumberFile *aDestFile) {
HBufC8* buf = HBufC8::NewL(KWriteDumbChunkSize);
TPtr8 bufDes(buf->Des());
TInt left = aFile->Size();
TInt chunkSize = 0;
while (left) {
chunkSize = left < KWriteDumbChunkSize ? left : KWriteDumbChunkSize;
bufDes.SetLength(chunkSize);
aFile->ReadL(bufDes);
aDestFile->WriteL(bufDes);
left -= chunkSize;
}
delete buf;
}
static void BuildRpkgL(CDir*& aDir, RFs *aFs, TDumberFile *aDestFile, TDesC& aDirLongName, TUint &aFileCount, TInt64 &aCopied, TBool &aShouldStop) {
for (TInt i = 0; i < aDir->Count(); i++) {
if (aShouldStop) {
break;
}
const TEntry& ent = (*aDir)[i];
HBufC16* LongName = HBufC16::NewL(1024);
TPtr aNextLongName = LongName->Des();
aNextLongName.Append(aDirLongName);
aNextLongName.Append(ent.iName);
//RDebug::Print(_L("Path: %S, att: %d"), &aNextLongName, ent.iAtt);
if (ent.IsDir()) {
CDir* subdir;
aNextLongName.Append('\\');
aFs->GetDir(aNextLongName, KEntryAttMatchMask, ESortByUid, subdir);
BuildRpkgL(subdir, aFs, aDestFile, aNextLongName, aFileCount, aCopied, aShouldStop);
} else {
TDumberFile* file = NULL;
TRAPD(err, file = TDumberFile::NewL(*aFs, aNextLongName, EDumberOpenRead));
if (err != KErrNone) {
RDebug::Print(_L("Skipping file: %s"), &LongName);
continue;
}
aFileCount++;
TRpkgEntry entryrpkg;
entryrpkg.iFullPathLength = aNextLongName.Length();
entryrpkg.iLastModified = ent.iModified;
entryrpkg.iAtt = ent.iAtt;
TPckgC<TRpkgEntry> entryrpkg_pkg(entryrpkg);
aDestFile->WriteL(entryrpkg_pkg);
aDestFile->WriteL(aNextLongName); // Write name
TRpkgData datarpkg;
datarpkg.iDataSize = file->Size();
TPckgC<TRpkgData> datarpkg_pkg(datarpkg);
aDestFile->WriteL(datarpkg_pkg);
CopyFileL(file, aDestFile);
aCopied += file->Size();
delete file;
}
delete LongName;
}
delete aDir;
}
TInt BuildRpkgL(const TDesC& aDumbName, TInt64 &aProgress, TBool &aShouldStop, TUint8 aEstimate) {
RFs fs;
fs.Connect(-1);
TDumberFile *iFile = TDumberFile::NewL(fs, aDumbName, aEstimate ? EDumberEstimate : EDumberReplaceWrite);
CleanupStack::PushL(iFile);
TRpkgHeader header;
header.iMagic[0] = 'R';
header.iMagic[1] = 'P';
header.iMagic[2] = 'K';
header.iMagic[3] = '2';
TPckgC<TRpkgHeader> headerpkg(header);
TMachineInfoV1Buf infobuf;
UserHal::MachineInfo(infobuf);
TMachineInfoV1 machineInfo = infobuf();
header.iAssociatedRomVersion = machineInfo.iRomVersion;
header.iMachineUID = machineInfo.iMachineUniqueId;
header.iHeaderSize = sizeof(TRpkgHeader);
header.iFileCount = 0;
iFile->WriteL(headerpkg);
CDir* zDir;
fs.GetDir(_L("Z:\\"), KEntryAttMatchMask, ESortByUid, zDir);
TPtrC zDirPath(_L("Z:\\"));
BuildRpkgL(zDir, &fs, iFile, zDirPath, header.iFileCount, aProgress, aShouldStop);
iFile->Seek(0, EDumberSeekSet);
iFile->WriteL(headerpkg);
CleanupStack::PopAndDestroy(); // iFile
return KErrNone;
}
} // namespace Dumber