-
Notifications
You must be signed in to change notification settings - Fork 0
/
xboxfs.cpp
492 lines (490 loc) · 18 KB
/
xboxfs.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#include "xboxfs.h"
int XBoxFATX::showtree(std::string startpath,bool showfiles)
{
// preset to first entry of dirtree
std::vector<direntry_t>::const_iterator entry=dirtree.begin();
// if startpath non-NULL, find starting path
if (startpath.length()>0) {
std::regex findregex(startpath,std::regex::ECMAScript);
while (entry!=dirtree.end()) {
if (entry->isdir) {
bool regexresult=std::regex_match(entry->name,findregex);
if (regexresult) {
break;
}
}
++entry;
}
}
if (entry==dirtree.end()) {
fprintf(stderr,"Path '%s' not found\n",startpath.c_str());
return EXIT_FAILURE;
}
unsigned int currentnest=0;
// we have start entry, begin showing
// start with display of dir name
if (entry!=dirtree.begin()) {
printf("%s",entry->name.c_str());
currentnest=entry->nestlevel;
}
printf("/\n");
if (entry!=dirtree.begin()) {
entry++;
}
while ((entry!=dirtree.end())&&(entry->nestlevel>currentnest)) {
int indent=(entry->nestlevel-currentnest);
if ((entry->isdir)||(showfiles)) {
while (indent>1) {
printf(" ");
--indent;
}
if (indent==1) {
printf("\u2514\u2500\u2500\u2500");
--indent;
}
printf("\"%s\"",entry->name.c_str());
if (!entry->isdir) {
if (entry->filesize>=ONEGIG) {
printf(" (%'.2fGB)",((double)entry->filesize/ONEGIG));
}
else if (entry->filesize>=ONEMEG) {
printf(" (%'.2fMB)",((double)entry->filesize/ONEMEG));
}
else if (entry->filesize>=ONEKAY) {
printf(" (%'.2fKB)",((double)entry->filesize/ONEKAY));
}
else {
printf(" (%'luB)",entry->filesize);
}
}
if (entry->isdir) {
printf("/");
}
printf("\n");
}
++entry;
}
return EXIT_SUCCESS;
}
XBoxFATX::XBoxFATX(char* path)
{
// no path given? Show help and exit
if (!path) {
usage(); // does not return
}
// set up initial/default values
setDefaults();
//
// make sure dir path ends with '/'
dirpath=std::string(path);
if (dirpath.at(dirpath.length()-1)!='/') {
dirpath+="/";
}
//
// check for existence and readability of Data0/1/2...
lastfile=FIRSTDATAFILE;
unsigned int fnum=0;
while (fnum<=lastfile) {
// dummy read to pull file (if it exists!) into memory
// exits with error if doesn't exist
getintBE(fnum,0lu);
// grab info while files are open
if (fnum==0) {
// Data0000
// how big is this device?
bytesPerDevice=getlongBE(fnum,0x240);
// determine how many Data???? files total (Data0002->DataXXXX)
lastfile=(unsigned int)(bytesPerDevice/DEFAULTFILESIZE)+FIRSTDATAFILE;
}
else if (fnum==1) {
// Data0001
// make sure it has the magic header (XTAF=FATX)
// oddly, it's in LE order, while rest of file is BE
if (getintBE(fnum,0x0)!=FATXMAGIC_BE) {
fprintf(stderr,"XTAF Magic header not found\n");
}
// 8 hex digit partition ID
partitionID=getintBE(fnum,0x04);
// how big is each 'chunk'
sectorsPerCluster=getintBE(fnum,0x08);
// where does the root directory begin?
rootDirCluster=getintBE(fnum,0x0c);
// makes it easier to determine file offsets
bytesPerCluster=(sectorsPerCluster*BYTESPERSECTOR);
totalClusters=(unsigned int)(bytesPerDevice/bytesPerCluster);
clustersPerFile=(unsigned int)(DEFAULTFILESIZE/bytesPerCluster);
// read in the cluster map
usedClusters=0;
for (unsigned int num=0; num<(totalClusters+1); ++num) {
filepos_t offset=(num*sizeof(unsigned int))+0x1000;
int clusterUse=getintBE(fnum,offset);
clustermap.push_back(clusterUse);
if (clustermap[num]!=0) {
++usedClusters;
}
}
// decrement for the '0'th entry and root dir's first (only?) cluster
usedClusters-=2;
}
// increment to check next file
++fnum;
}
// total file/dir counts
countFiles=0;
countDirs=0;
// Read the entire directory contents into memory
// this function is recursive.
readDirectoryTree(rootDirCluster);
// lookup VOLNAMEFILE to find volume name
wchar_t* volname=(wchar_t*)readfilecontents(VOLNAMEFILE);
// if errors occur, just ignore them, use default deviceName
if (volname) {
filepos_t filesize=getfilesize(VOLNAMEFILE);
// if not zero filesize? otherwise don't bother loading it
if (filesize) {
char* cbuf=(char*)malloc((filesize+1)*sizeof(char));
if (cbuf) {
convertUTF16(cbuf,volname,filesize);
deviceName=std::string(cbuf);
deviceNameSet=true;
free(cbuf);
}
}
// return buffer to heap
free(volname);
}
// all done! close fp
closeAllFiles();
}
void XBoxFATX::readClusters(unsigned int startCluster,unsigned char** buffer,filepos_t* bufferlen)
{
unsigned int currentCluster=startCluster;
unsigned int numclusters=0;
filepos_t buflen=0;
unsigned char* clusterbuf=NULL;
//
while (currentCluster!=CLUSTEREND) {
filepos_t clusterPos=((filepos_t)(currentCluster-1)*(filepos_t)bytesPerCluster);
++numclusters;
// extend buffer to make it right size
buflen=((filepos_t)bytesPerCluster*(filepos_t)numclusters);
clusterbuf=(unsigned char*)realloc(clusterbuf,buflen);
if (!clusterbuf) {
perror("readClusters - malloc");
exit(1);
}
// read cluster directly into buffer
unsigned char* offset=clusterbuf+((filepos_t)(numclusters-1)*(filepos_t)bytesPerCluster);
readdata(FIRSTDATAFILE,clusterPos,bytesPerCluster,offset);
// move to next cluster in chain
currentCluster=clustermap[currentCluster];
}
// pass information back to caller
*buffer=clusterbuf;
*bufferlen=buflen;
}
void XBoxFATX::convertUTF16(char* outstr,wchar_t* instr,filepos_t length)
{
char* ptr=outstr;
unsigned int widestartchar=0;
// is there a BOM?
if (((*(unsigned short int*)instr)==0xFFFE)||((*(unsigned short int*)instr)==0xFEFF)) {
// skip the BOM
widestartchar=1;
}
// determine BE or LE from first two bytes (BOM)
bool BE1orLE0=((*(unsigned short int*)instr)==0xFFFE);
//
for (unsigned int i=widestartchar; i<(length>>1); ++i) {
unsigned short int* wcptr=(unsigned short int*)instr+i;
// get wide char, flip if needed
wchar_t wc=(BE1orLE0)?be16toh(*wcptr):le16toh(*wcptr);
// convert wide char (UTF-16) to multi-byte (UTF-8)
int numchr=wctomb(ptr,wc);
// if valid chars saved, bump pointer
if (numchr>0) {
ptr+=numchr;
}
}
// zero terminate the multi-byte string
*ptr=0;
}
void XBoxFATX::readDirectoryTree(unsigned int startCluster)
{
// this'll be recursive, reading each cluster, scanning for entries
// and calling itself when it finds a directory entry
filepos_t buflen=0;
unsigned char* dirbuf=NULL;
readClusters(startCluster,&dirbuf,&buflen);
//
unsigned char* ptr=dirbuf;
while (ptr<(dirbuf+buflen)) {
switch (*ptr) {
case 0xE5: // deleted entry
break;
case 0xFF: // unused entry
break;
case 0x00: // shouldn't happen, discard if it does
break;
default: // active entry
char namebuf[0x2a+1];
direntry_t entry;
entry.attributes=*(ptr+1);
// makes determining if directory easier
entry.isdir=(*(ptr+1)&0x10);
strncpy(namebuf,(char*)(ptr+0x02),0x2a);
namebuf[*ptr]=0;
entry.name=std::string(namebuf);
entry.nestlevel =nestlevel;
entry.parentCluster=startCluster;
entry.startCluster =be32toh(*((unsigned int*)(ptr+0x2c)));
entry.filesize =be32toh(*((unsigned int*)(ptr+0x30)));
entry.createDate =be16toh(*((unsigned short int*)(ptr+0x34)));
entry.createTime =be16toh(*((unsigned short int*)(ptr+0x36)));
entry.lwriteDate =be16toh(*((unsigned short int*)(ptr+0x38)));
entry.lwriteTime =be16toh(*((unsigned short int*)(ptr+0x3a)));
entry.accessDate =be16toh(*((unsigned short int*)(ptr+0x3c)));
entry.accessTime =be16toh(*((unsigned short int*)(ptr+0x3e)));
//
if (entry.isdir) {
// count number of directories found
++countDirs;
}
else {
// count number of files found
++countFiles;
}
// store it
dirtree.push_back(entry);
//
if (entry.isdir) {
// the recursive part! call ourself when we find a subdir
++nestlevel;
readDirectoryTree(entry.startCluster);
--nestlevel;
}
break;
// end of switch default block
}
// skip to next entry (64 bytes each)
ptr+=64;
}
free(dirbuf);
}
const direntry_t* XBoxFATX::findFileEntry(std::string filename)
{
std::vector<direntry_t>::const_iterator ptr=dirtree.begin();
while ((ptr!=dirtree.end())&&(ptr->name!=filename)) {
++ptr;
}
if (ptr==dirtree.end()) {
// all entries checked, not found
return NULL;
}
return &(*ptr);
}
filepos_t XBoxFATX::getfilesize(std::string filename)
{
const direntry_t* entry=findFileEntry(filename);
if (!entry) {
return 0;
}
return entry->filesize;
}
unsigned char* XBoxFATX::readfilecontents(std::string filename)
{
const direntry* entry=findFileEntry(filename);
if (!entry) {
return NULL;
}
unsigned char* buffer=NULL;
filepos_t buflen=0;
// read file data, returning malloc'd ram pointer
// could be problematic if we're transferring a multi-gig file
readClusters(entry->startCluster,&buffer,&buflen);
// buflen will be multiple of cluster size, so fix it
buflen=entry->filesize;
// shorten buffer length to reflect file length
buffer=(unsigned char*)realloc(buffer,buflen);
// user must remember to free the ram
return buffer;
}
void XBoxFATX::showinfo()
{
printf("\n");
// adjust width based on widest number to be displayed
int width=6+(totalClusters>99999)+(totalClusters>999999)*2;
printf(" Partition ID: %08X\n",partitionID);
printf(" Device Name: \"%s\"%s\n",deviceName.c_str(),deviceNameSet?"":" (unnamed unit)");
printf(" Total Size: %'*lu MiB (%'.2f GiB)\n",width,bytesPerDevice/ONEMEG,((double)bytesPerDevice/ONEGIG));
printf(" Cluster Size: %'*lu KiB\n",width,(bytesPerCluster/ONEKAY));
printf("Num Data files: %*d => 2+%d => (0000...%04d)\n",width,(lastfile+1),(lastfile-1),lastfile);
printf(" Clusters/File: %'*u\n",width,clustersPerFile);
printf("Total Clusters: %'*u\n",width,totalClusters);
printf(" Used Clusters: %'*u (%.1f%% used)\n",width,usedClusters,((double)usedClusters*100.0/totalClusters));
printf(" Num Files: %'*d\n",width,countFiles);
printf(" Num Dirs: %'*d\n",width,countDirs);
// printf(" Root Dir @: %'*u\n",width,rootDirCluster);
printf("\n");
printf(" Data0000: %'*d.0%% Bookkeeping?\n",width,100);
printf(" Data0001: %'*d.0%% FATX Cluster Map\n",width,100);
for (unsigned int fnum=2; fnum<=lastfile; ++fnum) {
unsigned int clustercount=0;
unsigned int clustersThisFile=0;
for (unsigned int cluster=((fnum-2)*clustersPerFile)+2*(fnum==2);
((cluster<=totalClusters)&&(cluster<((fnum-1)*clustersPerFile)));
++cluster) {
++clustersThisFile;
if (clustermap[cluster]!=0) {
++clustercount;
}
}
if (clustercount>0) {
printf(" Data%04u: %'*.1f%% full (%'.1f%% of total)\n",
fnum,
width+2,
((double)clustercount*100.0/clustersThisFile),
((double)clustercount*100.0/usedClusters));
}
}
}
void XBoxFATX::zeroClusters()
{
// create a buffer
char* zerobuf=(char*)malloc(bytesPerCluster);
if (!zerobuf) {
perror("zeroClusters");
exit(1);
}
// fill with zeros
memset(zerobuf,0,bytesPerCluster);
//
double prevpercent=0.0;
// find all unallocated clusters, write zeros to each cluster
// performed in REVERSE order so most likely used files end up in the cache
for (unsigned int i=(unsigned int)clustermap.size()-1; i>0; --i) {
if (clustermap[i]==0) {
if (verbose) {
double percent=100.0-(floor((double)i*1000.0/(double)clustermap.size())/10.0);
if (percent>prevpercent) {
printf("\rClearing Free Space: (%u) %4.1f%% ",(FIRSTDATAFILE+(i/clustersPerFile)),percent);
fflush(stdout);
prevpercent=percent;
}
}
filepos_t clusterPos=((filepos_t)(i-1)*(filepos_t)bytesPerCluster);
writedata(FIRSTDATAFILE,clusterPos,bytesPerCluster,zerobuf);
}
}
free(zerobuf);
closeAllFiles();
if (verbose) {
printf("\rFree Space Cleared on \"%s\" (%.2f GiB)\n",deviceName.c_str(),((double)bytesPerDevice/(ONEGIG)));
}
}
int main(int argc __attribute__ ((unused)),char* argv[])
{
// didn't feel like coding getopt/getlongopt stuff right now (apologies),
// and most of the options are exclusive and can't be mixed anyways.
//
// first pass, handle the easy one-shots
for (int i=1; i<argc; ++i) {
// --version | -V
if ((strncmp(argv[i],"-V",3)==0)||(strncmp(argv[i],"--version",10)==0)) {
version();
return EXIT_SUCCESS;
}
// --help | -h
if ((strncmp(argv[i],"-h",3)==0)||(strncmp(argv[i],"--help",7)==0)) {
usage(); // does not return
// return EXIT_SUCCESS;
}
}
//
// yes, I'm assuming the first option is a path
//
// perform setup and initial discovery (exits if no data files found)
XBoxFATX xbox=XBoxFATX(argv[1]);
//
if (argc==2) {
// only dirpath given, show info
xbox.showinfo();
return EXIT_SUCCESS;
}
//
// quick check for verbose option
for (int i=2; i<argc; ++i) {
// --verbose | -v
if ((strncmp(argv[i],"-v",3)==0)||(strncmp(argv[i],"--verbose",10)==0)) {
xbox.verbose=true;
break;
}
}
// options which are exclusive, each does something and exits
for (int i=2; i<argc; ++i) {
// --zero | (no short version)
if ((strncmp(argv[i],"--zero",7)==0)) {
xbox.zeroClusters();
return EXIT_SUCCESS;
}
// --info | -i
if ((strncmp(argv[i],"-i",3)==0)||(strncmp(argv[i],"--info",7)==0)) {
xbox.showinfo();
return EXIT_SUCCESS;
}
// --list [PATH] | -l [PATH]
if ((strncmp(argv[i],"-l",3)==0)||(strncmp(argv[i],"--list",7)==0)) {
// ANY following argument is ALWAYS taken as the optional
// path if present, must obviously be last on line
std::string treepath="";
if (argc>(i+1)) {
treepath=std::string(argv[i+1]);
}
fprintf(stderr,"--list stub\n");
return EXIT_SUCCESS;
}
// --tree [PATH] | -t [PATH]
if ((strncmp(argv[i],"-t",3)==0)||(strncmp(argv[i],"--tree",7)==0)) {
// ANY following argument is ALWAYS taken as the optional
// path if present, must obviously be last on line
std::string treepath="";
if (argc>(i+1)) {
treepath=std::string(argv[i+1]);
}
return xbox.showtree(treepath,WITHFILES);
}
// --dir [PATH] | -d [PATH]
if ((strncmp(argv[i],"-d",3)==0)||(strncmp(argv[i],"--dir",6)==0)) {
// ANY following argument is ALWAYS taken as the optional
// path if present, must obviously be last on line
std::string treepath="";
if (argc>(i+1)) {
treepath=std::string(argv[i+1]);
}
return xbox.showtree(treepath,WITHOUTFILES);
}
// --extract FNAME | -x FNAME
if ((strncmp(argv[i],"-x",3)==0)||(strncmp(argv[i],"--extract",10)==0)) {
// ANY following argument is ALWAYS taken as the FNAME, also must
// be last option on line
fprintf(stderr,"--extract stub\n");
return EXIT_SUCCESS;
}
// --store FNAME | -s FNAME
if ((strncmp(argv[i],"-s",3)==0)||(strncmp(argv[i],"--store",10)==0)) {
// ANY following argument is ALWAYS taken as the FNAME, ALSO also
// must be last option on line
fprintf(stderr,"--store stub\n");
return EXIT_SUCCESS;
}
}
// if we get here, nothing was recognized, if verbose true, show info
if (xbox.verbose) {
// at least the user was trying...
xbox.showinfo();
return EXIT_SUCCESS;
}
// otherwise... we didn't catch anything at all? Give user a hint
fprintf(stderr,"Unknown option, try --help\n");
return EXIT_FAILURE;
}