-
Notifications
You must be signed in to change notification settings - Fork 0
/
fat.c
480 lines (465 loc) · 13.2 KB
/
fat.c
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
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <pspkernel.h>
#include "charsets.h"
#include "fat.h"
static int fatfd = -1;
static t_fat_dbr dbr;
static t_fat_mbr mbr;
static dword * fat_table = NULL;
static dword root_pos = 0;
static dword data_pos = 0;
static dword bytes_per_clus = 0;
static dword loadcount = 0;
static dword clus_max = 0;
static enum {
fat12,
fat16,
fat32
} fat_type = fat16;
extern bool fat_init()
{
fatfd = sceIoOpen("msstor:", PSP_O_RDONLY, 0777);
if(fatfd < 0)
return false;
if(sceIoRead(fatfd, &mbr, sizeof(mbr)) != sizeof(mbr))
{
sceIoClose(fatfd);
fatfd = -1;
return false;
}
if(sceIoLseek32(fatfd, mbr.dpt[0].start_sec * 0x200, PSP_SEEK_SET) != mbr.dpt[0].start_sec * 0x200 || sceIoRead(fatfd, &dbr, sizeof(dbr)) < sizeof(dbr))
{
sceIoClose(fatfd);
fatfd = -1;
return false;
}
if(mbr.dpt[0].id == 1)
{
fat_type = fat12;
clus_max = 0x0FF0;
}
else if(mbr.dpt[0].id == 0x04 || mbr.dpt[0].id == 0x06 || mbr.dpt[0].id == 0x0E)
{
fat_type = fat16;
clus_max = 0xFFF0;
}
else
{
fat_type = fat32;
clus_max = 0x0FFFFFF0;
}
bytes_per_clus = dbr.sec_per_clus * dbr.bytes_per_sec;
if(fat_type == fat32)
{
data_pos = mbr.dpt[0].start_sec * 0x200 + (dbr.ufat.fat32.sec_per_fat * dbr.num_fats + dbr.reserved_sec) * dbr.bytes_per_sec;
root_pos = data_pos + bytes_per_clus * dbr.ufat.fat32.root_clus;
}
else
{
root_pos = mbr.dpt[0].start_sec * 0x200 + (dbr.num_fats * dbr.sec_per_fat + dbr.reserved_sec) * dbr.bytes_per_sec;
data_pos = root_pos + dbr.root_entry * sizeof(t_fat_entry);
}
sceIoClose(fatfd);
return true;
}
static bool convert_table_fat12()
{
word * otable = (word *)malloc(dbr.sec_per_fat * dbr.bytes_per_sec);
if(otable == NULL)
return false;
memcpy(otable, fat_table, dbr.sec_per_fat * dbr.bytes_per_sec);
int i, j, entrycount = dbr.sec_per_fat * dbr.bytes_per_sec / sizeof(word) * 4 / 3;
free((void *)fat_table);
fat_table = (dword *)malloc(sizeof(dword) * entrycount);
if(fat_table == NULL)
{
free((void *)otable);
return false;
}
for(i = 0, j = 0; i < entrycount; i += 4, j += 3)
{
fat_table[i] = otable[j] & 0x0FFF;
fat_table[i + 1] = ((otable[j] >> 12) & 0x000F) | ((otable[j + 1] & 0x00FF) << 4);
fat_table[i + 2] = ((otable[j + 1] >> 8) & 0x00FF) | ((otable[j + 2] & 0x000F) << 8);
fat_table[i + 3] = otable[j + 2] >> 4;
}
free((void *)otable);
return true;
}
static bool convert_table_fat16()
{
word * otable = (word *)malloc(dbr.sec_per_fat * dbr.bytes_per_sec);
if(otable == NULL)
return false;
memcpy(otable, fat_table, dbr.sec_per_fat * dbr.bytes_per_sec);
int i, entrycount = dbr.sec_per_fat * dbr.bytes_per_sec / sizeof(word);
free((void *)fat_table);
fat_table = (dword *)malloc(sizeof(dword) * entrycount);
if(fat_table == NULL)
{
free((void *)otable);
return false;
}
for(i = 0; i < entrycount; i ++)
fat_table[i] = otable[i];
free((void *)otable);
return true;
}
static bool fat_load_table()
{
if(loadcount > 0)
{
loadcount ++;
return true;
}
fatfd = sceIoOpen("msstor:", PSP_O_RDONLY, 0777);
if(fatfd < 0)
return false;
if(sceIoLseek32(fatfd, mbr.dpt[0].start_sec * 0x200 + dbr.reserved_sec * dbr.bytes_per_sec, PSP_SEEK_SET) != mbr.dpt[0].start_sec * 0x200 + dbr.reserved_sec * dbr.bytes_per_sec || (fat_table = (dword *)malloc(((fat_type == fat32) ? dbr.ufat.fat32.sec_per_fat : dbr.sec_per_fat) * dbr.bytes_per_sec)) == NULL)
{
sceIoClose(fatfd);
fatfd = -1;
return false;
}
if(sceIoRead(fatfd, fat_table, ((fat_type == fat32) ? dbr.ufat.fat32.sec_per_fat : dbr.sec_per_fat) * dbr.bytes_per_sec) != ((fat_type == fat32) ? dbr.ufat.fat32.sec_per_fat : dbr.sec_per_fat) * dbr.bytes_per_sec || (mbr.dpt[0].id == 1 && !convert_table_fat12()) || ((mbr.dpt[0].id == 0x04 || mbr.dpt[0].id == 0x06 || mbr.dpt[0].id == 0x0E) && !convert_table_fat16()))
{
sceIoClose(fatfd);
fatfd = -1;
free((void *)fat_table);
fat_table = NULL;
return false;
}
loadcount = 1;
return true;
}
static void fat_free_table()
{
if(loadcount > 0)
{
loadcount --;
if(loadcount > 0)
return;
if(fat_table != NULL)
{
free((void *)fat_table);
fat_table = NULL;
}
sceIoClose(fatfd);
}
}
static byte fat_calc_chksum(p_fat_entry info)
{
byte * n = (byte *)&info->norm.filename[0];
byte chksum = 0;
dword i;
for(i = 0; i < 11; i ++)
chksum = ((chksum & 1) ? 0x80 : 0) + (chksum >> 1) + n[i];
return chksum;
}
static bool fat_dir_list(dword clus, dword * count, p_fat_entry * entrys)
{
if(clus == 0)
return false;
if(clus < 2)
{
* count = dbr.root_entry;
if((* entrys = (p_fat_entry)malloc(* count * sizeof(t_fat_entry))) == NULL)
return false;
if(sceIoLseek32(fatfd, root_pos, PSP_SEEK_SET) != root_pos || sceIoRead(fatfd, * entrys, * count * sizeof(t_fat_entry)) != * count * sizeof(t_fat_entry))
{
free((void *)* entrys);
return false;
}
}
else
{
if(fat_table[clus] < 2)
return false;
dword c2 = clus;
* count = 1;
while(fat_table[c2] < clus_max && fat_table[c2] > 1)
{
c2 = fat_table[c2];
(* count) ++;
}
c2 = clus;
dword epc = (bytes_per_clus / sizeof(t_fat_entry)), ep = 0;
(* count) *= epc;
if((* entrys = (p_fat_entry)malloc(* count * sizeof(t_fat_entry))) == NULL)
return false;
do {
dword epos = data_pos + (c2 - 2) * bytes_per_clus;
if(sceIoLseek32(fatfd, epos, PSP_SEEK_SET) != epos || sceIoRead(fatfd, &(* entrys)[ep], bytes_per_clus) != bytes_per_clus)
{
free((void *)* entrys);
return false;
}
ep += epc;
c2 = fat_table[c2];
} while(c2 < clus_max && c2 > 1);
}
return true;
}
static bool fat_get_longname(p_fat_entry entrys, dword cur, char * longnamestr)
{
word chksum = fat_calc_chksum(&entrys[cur]);
dword j = cur;
word longname[260];
memset(longname, 0, 260 * sizeof(word));
while(j > 0)
{
j --;
if(entrys[j].norm.attr != 0x0F || entrys[j].longfile.checksum != chksum || entrys[j].norm.filename[0] == 0 || (byte)entrys[j].norm.filename[0] == 0xE5)
return false;
dword order = entrys[j].longfile.order & 0x3F;
if(order > 20)
return false;
dword ppos = (order - 1) * 13;
dword k;
for(k = 0; k < 5; k ++)
longname[ppos ++] = entrys[j].longfile.uni_name[k];
for(k = 0; k < 6; k ++)
longname[ppos ++] = entrys[j].longfile.uni_name2[k];
for(k = 0; k < 2; k ++)
longname[ppos ++] = entrys[j].longfile.uni_name3[k];
if((entrys[j].longfile.order & 0x40) > 0)
break;
}
if(entrys[j].norm.attr != 0x0F || (entrys[j].longfile.order & 0x40) == 0)
return false;
longname[255] = 0;
memset(longnamestr, 0, 256);
charsets_ucs_conv((const byte *)longname, (byte *)longnamestr);
return true;
}
static void fat_get_shortname(p_fat_entry entry, char * shortnamestr)
{
static bool chartable[256] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x00
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x00
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x00
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x30
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x40
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, // 0x50
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x60
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, // 0x70
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x80
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x90
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xA0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xB0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xC0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xD0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xE0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // 0xF0
};
dword i = 0;
byte abit = 0;
if((entry->norm.flag & 0x08) > 0)
abit = 0x20;
while(i < 8 && entry->norm.filename[i] != 0x20)
{
*shortnamestr ++ = entry->norm.filename[i] | ((chartable[(byte)entry->norm.filename[i]]) ? abit : 0);
i ++;
}
if(entry->norm.fileext[0] != 0x20)
{
*shortnamestr ++ = '.';
i = 0;
while(i < 3 && entry->norm.fileext[i] != 0x20)
*shortnamestr ++ = entry->norm.fileext[i ++] | abit;
}
*shortnamestr = 0;
}
extern bool fat_locate(const char * name, char * sname, dword clus, p_fat_entry info)
{
dword count;
p_fat_entry entrys;
if(!fat_dir_list(clus, &count, &entrys))
return false;
SceUID dl = sceIoDopen(sname);
if(dl < 0)
return false;
char shortname[11];
bool onlylong = false;
dword nlen = strlen(name);
if(nlen > 12)
onlylong = true;
else
{
char * dot = strrchr(name, '.');
if((dot == NULL && nlen < 9) || (dot - name < 9 && nlen - 1 - (dot - name) < 4))
{
memset(&shortname[0], 0x20, 11);
memcpy(&shortname[0], name, (dot == NULL) ? nlen : (dot - name));
if(dot != NULL)
memcpy(&shortname[8], dot + 1, nlen - 1 - (dot - name));
}
else
onlylong = true;
}
SceIoDirent sid;
dword i;
for(i = 0; i < count; i ++) {
if((entrys[i].norm.attr & FAT_FILEATTR_VOLUME) == 0 && entrys[i].norm.filename[0] != 0 && (byte)entrys[i].norm.filename[0] != 0xE5)
{
memset(&sid, 0, sizeof(SceIoDirent));
int result;
while((result = sceIoDread(dl, &sid)) > 0 && (sid.d_stat.st_attr & 0x08) > 0);
if(result == 0)
break;
if(entrys[i].norm.filename[0] == 0x05)
entrys[i].norm.filename[0] = 0xE5;
if(!onlylong && strnicmp(shortname, &entrys[i].norm.filename[0], 11) == 0)
{
if((byte)entrys[i].norm.filename[0] == 0xE5)
entrys[i].norm.filename[0] = 0x05;
memcpy(info, &entrys[i], sizeof(t_fat_entry));
free((void *)entrys);
strcat(sname, sid.d_name);
if((entrys[i].norm.attr & FAT_FILEATTR_DIRECTORY) > 0)
strcat(sname, "/");
sceIoDclose(dl);
return true;
}
if((byte)entrys[i].norm.filename[0] == 0xE5)
entrys[i].norm.filename[0] = 0x05;
char longnames[256];
if(!fat_get_longname(entrys, i, longnames))
continue;
if(stricmp(name, longnames) == 0)
{
memcpy(info, &entrys[i], sizeof(t_fat_entry));
free((void *)entrys);
strcat(sname, sid.d_name);
if((entrys[i].norm.attr & FAT_FILEATTR_DIRECTORY) > 0)
strcat(sname, "/");
sceIoDclose(dl);
return true;
}
}
}
free((void *)entrys);
sceIoDclose(dl);
return false;
}
static dword fat_dir_clus(const char * dir, char * shortdir)
{
if(!fat_load_table() || fatfd < 0)
return 0;
char rdir[256];
strcpy(rdir, dir);
char * partname = strtok(rdir, "/\\");
if(partname == NULL)
{
fat_free_table();
return 0;
}
if(strcmp(partname, "ms0:") != 0 && strcmp(partname, "fatms:") != 0 && strcmp(partname, "fatms0:") != 0)
{
fat_free_table();
return 0;
}
strcpy(shortdir, partname);
strcat(shortdir, "/");
partname = strtok(NULL, "/\\");
dword clus = (fat_type == fat32) ? dbr.ufat.fat32.root_clus : 1;
t_fat_entry entry;
while(partname != NULL) {
if(partname[0] != 0)
{
if(fat_locate(partname, shortdir, clus, &entry))
clus = ((fat_type == fat32) ? (((dword)entry.norm.clus_high) << 16) : 0) + entry.norm.clus;
else
{
fat_free_table();
return 0;
}
}
partname = strtok(NULL, "/\\");
}
fat_free_table();
return clus;
}
extern dword fat_readdir(const char * dir, char * sdir, p_fat_info * info)
{
if(!fat_load_table() || fatfd < 0)
return INVALID;
dword clus = fat_dir_clus(dir, sdir);
SceUID dl = 0;
if(clus == 0 || (dl = sceIoDopen(sdir)) < 0)
{
fat_free_table();
sceIoDclose(dl);
return INVALID;
}
dword ecount = 0;
p_fat_entry entrys;
if(!fat_dir_list(clus, &ecount, &entrys))
{
fat_free_table();
sceIoDclose(dl);
return INVALID;
}
dword count = 0, cur = 0, i;
for(i = 0; i < ecount; i ++)
{
if((entrys[i].norm.attr & FAT_FILEATTR_VOLUME) != 0 || entrys[i].norm.filename[0] == 0 || (byte)entrys[i].norm.filename[0] == 0xE5 || (entrys[i].norm.filename[0] == '.' && entrys[i].norm.filename[1] == 0x20))
continue;
count ++;
}
if(count == 0 || (*info = (p_fat_info)malloc(count * sizeof(t_fat_info))) == NULL)
{
free(entrys);
fat_free_table();
sceIoDclose(dl);
return INVALID;
}
SceIoDirent sid;
for(i = 0; i < ecount; i ++)
{
if((entrys[i].norm.attr & FAT_FILEATTR_VOLUME) != 0 || entrys[i].norm.filename[0] == 0 || (byte)entrys[i].norm.filename[0] == 0xE5 || (entrys[i].norm.filename[0] == '.' && entrys[i].norm.filename[1] == 0x20))
continue;
memset(&sid, 0, sizeof(SceIoDirent));
int result;
while((result = sceIoDread(dl, &sid)) > 0 && ((sid.d_stat.st_attr & 0x08) > 0 || (sid.d_name[0] == '.' && sid.d_name[1] == 0)));
if(result == 0)
break;
p_fat_info inf = &((*info)[cur]);
fat_get_shortname(&entrys[i], inf->filename);
if(inf->filename[0] == 0x05)
inf->filename[0] = 0xE5;
if(!fat_get_longname(entrys, i, inf->longname))
strcpy(inf->longname, inf->filename);
strcpy(inf->filename, sid.d_name);
inf->filesize = entrys[i].norm.filesize;
inf->cdate = entrys[i].norm.cr_date;
inf->ctime = entrys[i].norm.cr_time;
inf->mdate = entrys[i].norm.last_mod_date;
inf->mtime = entrys[i].norm.last_mod_time;
inf->clus = ((fat_type == fat32) ? (((dword)entrys[i].norm.clus_high) << 16) : 0) + entrys[i].norm.clus;
inf->attr = entrys[i].norm.attr;
cur ++;
}
free(entrys);
fat_free_table();
sceIoDclose(dl);
return cur;
}
extern void fat_free()
{
if(fatfd >= 0)
{
sceIoClose(fatfd);
fatfd = -1;
}
memset(&dbr, 0, sizeof(dbr));
memset(&mbr, 0, sizeof(mbr));
root_pos = 0;
data_pos = 0;
bytes_per_clus = 0;
loadcount = 0;
clus_max = 0;
fat_type = fat16;
}