1 |
#include <stdio.h> |
2 |
#include <stdlib.h> |
3 |
#include <sys/types.h> |
4 |
#include <sys/stat.h> |
5 |
#include <fcntl.h> |
6 |
#include <time.h> |
7 |
#include <string.h> |
8 |
#include <stdarg.h> |
9 |
|
10 |
#include "CDVDiso.h" |
11 |
#include "Config.h" |
12 |
|
13 |
#ifndef MAX_PATH |
14 |
#define MAX_PATH 255 |
15 |
#endif |
16 |
|
17 |
char IsoFile[256]; |
18 |
char IsoCWD[256]; |
19 |
char CdDev[256]; |
20 |
|
21 |
_cdIso cdIso[8]; |
22 |
u8 *pbuffer; |
23 |
int cdblocksize; |
24 |
int cdblockofs; |
25 |
int cdoffset; |
26 |
int cdtype; |
27 |
int cdblocks; |
28 |
|
29 |
int Zmode; // 1 Z - 2 bz2 |
30 |
int fmode; // 0 - file / 1 - Zfile |
31 |
char *Ztable; |
32 |
|
33 |
int BlockDump; |
34 |
isoFile *fdump; |
35 |
isoFile *iso; |
36 |
|
37 |
FILE *cdvdLog = NULL; |
38 |
|
39 |
// This var is used to detect resume-style behavior of the Pcsx2 emulator, |
40 |
// and skip prompting the user for a new CD when it's likely they want to run the existing one. |
41 |
static char cdvdCurrentIso[MAX_PATH]; |
42 |
|
43 |
char *methods[] = |
44 |
{ |
45 |
".Z - compress faster", |
46 |
".BZ - compress better", |
47 |
NULL |
48 |
}; |
49 |
|
50 |
#ifdef PCSX2_DEBUG |
51 |
char *LibName = "Linuz Iso CDVD (Debug) "; |
52 |
#else |
53 |
char *LibName = "Linuz Iso CDVD "; |
54 |
#endif |
55 |
|
56 |
const u8 version = PS2E_CDVD_VERSION; |
57 |
const u8 revision = 0; |
58 |
const u8 build = 9; |
59 |
|
60 |
u8 cdbuffer[CD_FRAMESIZE_RAW * 10] = {0}; |
61 |
|
62 |
s32 msf_to_lba(u8 m, u8 s, u8 f) |
63 |
{ |
64 |
u32 lsn; |
65 |
lsn = f; |
66 |
lsn += (s - 2) * 75; |
67 |
lsn += m * 75 * 60; |
68 |
return lsn; |
69 |
} |
70 |
|
71 |
void lba_to_msf(s32 lba, u8* m, u8* s, u8* f) |
72 |
{ |
73 |
lba += 150; |
74 |
*m = lba / (60 * 75); |
75 |
*s = (lba / 75) % 60; |
76 |
*f = lba % 75; |
77 |
} |
78 |
|
79 |
#define btoi(b) ((b)/16*10 + (b)%16) /* BCD to u_char */ |
80 |
#define itob(i) ((i)/10*16 + (i)%10) /* u_char to BCD */ |
81 |
|
82 |
|
83 |
EXPORT_C_(char*) PS2EgetLibName() |
84 |
{ |
85 |
return LibName; |
86 |
} |
87 |
|
88 |
EXPORT_C_(u32) PS2EgetLibType() |
89 |
{ |
90 |
return PS2E_LT_CDVD; |
91 |
} |
92 |
|
93 |
EXPORT_C_(u32) PS2EgetLibVersion2(u32 type) |
94 |
{ |
95 |
return (version << 16) | (revision << 8) | build; |
96 |
} |
97 |
|
98 |
#ifdef PCSX2_DEBUG |
99 |
void __Log(char *fmt, ...) |
100 |
{ |
101 |
va_list list; |
102 |
|
103 |
if (cdvdLog == NULL) return; |
104 |
|
105 |
va_start(list, fmt); |
106 |
vfprintf(cdvdLog, fmt, list); |
107 |
va_end(list); |
108 |
} |
109 |
#else |
110 |
#define __Log 0&& |
111 |
#endif |
112 |
|
113 |
|
114 |
EXPORT_C_(s32) CDVDinit() |
115 |
{ |
116 |
#ifdef PCSX2_DEBUG |
117 |
cdvdLog = fopen("logs/cdvdLog.txt", "w"); |
118 |
if (cdvdLog == NULL) |
119 |
{ |
120 |
cdvdLog = fopen("cdvdLog.txt", "w"); |
121 |
if (cdvdLog == NULL) |
122 |
{ |
123 |
SysMessage("Can't create cdvdLog.txt"); |
124 |
return -1; |
125 |
} |
126 |
} |
127 |
setvbuf(cdvdLog, NULL, _IONBF, 0); |
128 |
CDVD_LOG("CDVDinit\n"); |
129 |
#endif |
130 |
|
131 |
cdvdCurrentIso[0] = 0; |
132 |
memset(cdIso, 0, sizeof(cdIso)); |
133 |
return 0; |
134 |
} |
135 |
|
136 |
EXPORT_C_(void) CDVDshutdown() |
137 |
{ |
138 |
cdvdCurrentIso[0] = 0; |
139 |
#ifdef CDVD_LOG |
140 |
if (cdvdLog != NULL) fclose(cdvdLog); |
141 |
#endif |
142 |
} |
143 |
|
144 |
EXPORT_C_(s32) CDVDopen(const char* pTitle) |
145 |
{ |
146 |
LoadConf(); |
147 |
|
148 |
if (pTitle != NULL) strcpy(IsoFile, pTitle); |
149 |
|
150 |
if (*IsoFile == 0) strcpy(IsoFile, cdvdCurrentIso); |
151 |
|
152 |
if (*IsoFile == 0) |
153 |
{ |
154 |
char temp[256]; |
155 |
|
156 |
CfgOpenFile(); |
157 |
|
158 |
LoadConf(); |
159 |
strcpy(temp, IsoFile); |
160 |
*IsoFile = 0; |
161 |
SaveConf(); |
162 |
strcpy(IsoFile, temp); |
163 |
} |
164 |
|
165 |
iso = isoOpen(IsoFile); |
166 |
if (iso == NULL) |
167 |
{ |
168 |
SysMessage("Error loading %s\n", IsoFile); |
169 |
return -1; |
170 |
} |
171 |
|
172 |
if (iso->type == ISOTYPE_DVD) |
173 |
cdtype = CDVD_TYPE_PS2DVD; |
174 |
else if (iso->type == ISOTYPE_AUDIO) |
175 |
cdtype = CDVD_TYPE_CDDA; |
176 |
else |
177 |
cdtype = CDVD_TYPE_PS2CD; |
178 |
|
179 |
if (BlockDump) |
180 |
{ |
181 |
char fname_only[MAX_PATH]; |
182 |
|
183 |
#ifdef _WIN32 |
184 |
char fname[MAX_PATH], ext[MAX_PATH]; |
185 |
_splitpath(IsoFile, NULL, NULL, fname, ext); |
186 |
_makepath(fname_only, NULL, NULL, fname, NULL); |
187 |
#else |
188 |
char* p, *plast; |
189 |
|
190 |
plast = p = strchr(IsoFile, '/'); |
191 |
while (p != NULL) |
192 |
{ |
193 |
plast = p; |
194 |
p = strchr(p + 1, '/'); |
195 |
} |
196 |
|
197 |
// Lets not create dumps in the plugin directory. |
198 |
strcpy(fname_only, "../"); |
199 |
if (plast != NULL) |
200 |
strcat(fname_only, plast + 1); |
201 |
else |
202 |
strcat(fname_only, IsoFile); |
203 |
|
204 |
plast = p = strchr(fname_only, '.'); |
205 |
|
206 |
while (p != NULL) |
207 |
{ |
208 |
plast = p; |
209 |
p = strchr(p + 1, '.'); |
210 |
} |
211 |
|
212 |
if (plast != NULL) *plast = 0; |
213 |
|
214 |
#endif |
215 |
strcat(fname_only, ".dump"); |
216 |
fdump = isoCreate(fname_only, ISOFLAGS_BLOCKDUMP); |
217 |
if (fdump) isoSetFormat(fdump, iso->blockofs, iso->blocksize, iso->blocks); |
218 |
} |
219 |
else |
220 |
{ |
221 |
fdump = NULL; |
222 |
} |
223 |
|
224 |
return 0; |
225 |
} |
226 |
|
227 |
EXPORT_C_(void) CDVDclose() |
228 |
{ |
229 |
|
230 |
strcpy(cdvdCurrentIso, IsoFile); |
231 |
|
232 |
isoClose(iso); |
233 |
if (fdump != NULL) isoClose(fdump); |
234 |
} |
235 |
|
236 |
EXPORT_C_(s32) CDVDreadSubQ(u32 lsn, cdvdSubQ* subq) |
237 |
{ |
238 |
// fake it |
239 |
u8 min, sec, frm; |
240 |
subq->ctrl = 4; |
241 |
subq->mode = 1; |
242 |
subq->trackNum = itob(1); |
243 |
subq->trackIndex = itob(1); |
244 |
|
245 |
lba_to_msf(lsn, &min, &sec, &frm); |
246 |
subq->trackM = itob(min); |
247 |
subq->trackS = itob(sec); |
248 |
subq->trackF = itob(frm); |
249 |
|
250 |
subq->pad = 0; |
251 |
|
252 |
lba_to_msf(lsn + (2*75), &min, &sec, &frm); |
253 |
subq->discM = itob(min); |
254 |
subq->discS = itob(sec); |
255 |
subq->discF = itob(frm); |
256 |
return 0; |
257 |
} |
258 |
|
259 |
EXPORT_C_(s32) CDVDgetTN(cdvdTN *Buffer) |
260 |
{ |
261 |
Buffer->strack = 1; |
262 |
Buffer->etrack = 1; |
263 |
|
264 |
return 0; |
265 |
} |
266 |
|
267 |
EXPORT_C_(s32) CDVDgetTD(u8 Track, cdvdTD *Buffer) |
268 |
{ |
269 |
if (Track == 0) |
270 |
{ |
271 |
Buffer->lsn = iso->blocks; |
272 |
} |
273 |
else |
274 |
{ |
275 |
Buffer->type = CDVD_MODE1_TRACK; |
276 |
Buffer->lsn = 0; |
277 |
} |
278 |
|
279 |
return 0; |
280 |
} |
281 |
|
282 |
static s32 layer1start = -1; |
283 |
|
284 |
static bool testForPartitionInfo( const u8 (&tempbuffer)[CD_FRAMESIZE_RAW] ) |
285 |
{ |
286 |
const int off = iso->blockofs; |
287 |
|
288 |
// test for: CD001 |
289 |
return ( |
290 |
(tempbuffer[off+1] == 0x43) && |
291 |
(tempbuffer[off+2] == 0x44) && |
292 |
(tempbuffer[off+3] == 0x30) && |
293 |
(tempbuffer[off+4] == 0x30) && |
294 |
(tempbuffer[off+5] == 0x31) |
295 |
); |
296 |
} |
297 |
|
298 |
EXPORT_C_(s32) CDVDgetTOC(void* toc) |
299 |
{ |
300 |
u8 type = CDVDgetDiskType(); |
301 |
u8* tocBuff = (u8*)toc; |
302 |
|
303 |
//__Log("CDVDgetTOC\n"); |
304 |
|
305 |
if (type == CDVD_TYPE_DVDV || type == CDVD_TYPE_PS2DVD) |
306 |
{ |
307 |
// get dvd structure format |
308 |
// scsi command 0x43 |
309 |
memset(tocBuff, 0, 2048); |
310 |
|
311 |
if (layer1start != -2 && iso->blocks >= 0x300000) |
312 |
{ |
313 |
int off = iso->blockofs; |
314 |
|
315 |
// dual sided |
316 |
tocBuff[ 0] = 0x24; |
317 |
tocBuff[ 1] = 0x02; |
318 |
tocBuff[ 2] = 0xF2; |
319 |
tocBuff[ 3] = 0x00; |
320 |
tocBuff[ 4] = 0x41; |
321 |
tocBuff[ 5] = 0x95; |
322 |
|
323 |
tocBuff[14] = 0x60; // dual sided, ptp |
324 |
|
325 |
tocBuff[16] = 0x00; |
326 |
tocBuff[17] = 0x03; |
327 |
tocBuff[18] = 0x00; |
328 |
tocBuff[19] = 0x00; |
329 |
|
330 |
// search for it |
331 |
if (layer1start == -1) |
332 |
{ |
333 |
printf("CDVD: searching for layer1..."); |
334 |
|
335 |
/*tempbuffer = (u8*)malloc(CD_FRAMESIZE_RAW * 10); |
336 |
for (layer1start = (iso->blocks / 2 - 0x10) & ~0xf; layer1start < 0x200010; layer1start += 16) |
337 |
{ |
338 |
isoReadBlock(iso, tempbuffer, layer1start); |
339 |
// CD001 |
340 |
if (tempbuffer[off+1] == 0x43 && |
341 |
tempbuffer[off+2] == 0x44 && |
342 |
tempbuffer[off+3] == 0x30 && |
343 |
tempbuffer[off+4] == 0x30 && |
344 |
tempbuffer[off+5] == 0x31) |
345 |
break; |
346 |
} |
347 |
free(tempbuffer);*/ |
348 |
|
349 |
uint midsector = (iso->blocks / 2) & ~0xf; |
350 |
uint deviation = 0; |
351 |
|
352 |
while( (layer1start == -1) && (deviation < midsector-16) ) |
353 |
{ |
354 |
u8 tempbuffer[CD_FRAMESIZE_RAW]; |
355 |
isoReadBlock(iso, tempbuffer, midsector-deviation); |
356 |
|
357 |
if(testForPartitionInfo( tempbuffer )) |
358 |
layer1start = midsector-deviation; |
359 |
else |
360 |
{ |
361 |
isoReadBlock(iso, tempbuffer, midsector+deviation); |
362 |
if( testForPartitionInfo( tempbuffer ) ) |
363 |
layer1start = midsector+deviation; |
364 |
} |
365 |
|
366 |
if( layer1start != -1 ) |
367 |
{ |
368 |
if( tempbuffer[iso->blockofs] != 0x01 ) |
369 |
{ |
370 |
fprintf( stderr, "(LinuzCDVDiso): Invalid partition type on layer 1!? (type=0x%x)", tempbuffer[iso->blockofs] ); |
371 |
} |
372 |
} |
373 |
deviation += 16; |
374 |
} |
375 |
|
376 |
|
377 |
if (layer1start == -1) |
378 |
{ |
379 |
printf("(LinuzCDVDiso): Couldn't find second layer on dual layer... ignoring\n"); |
380 |
// fake it |
381 |
tocBuff[ 0] = 0x04; |
382 |
tocBuff[ 1] = 0x02; |
383 |
tocBuff[ 2] = 0xF2; |
384 |
tocBuff[ 3] = 0x00; |
385 |
tocBuff[ 4] = 0x86; |
386 |
tocBuff[ 5] = 0x72; |
387 |
|
388 |
tocBuff[16] = 0x00; |
389 |
tocBuff[17] = 0x03; |
390 |
tocBuff[18] = 0x00; |
391 |
tocBuff[19] = 0x00; |
392 |
layer1start = -2; |
393 |
return 0; |
394 |
} |
395 |
|
396 |
printf("(LinuzCDVDiso): found at 0x%8.8x\n", layer1start); |
397 |
layer1start = layer1start + 0x30000 - 1; |
398 |
} |
399 |
|
400 |
tocBuff[20] = layer1start >> 24; |
401 |
tocBuff[21] = (layer1start >> 16) & 0xff; |
402 |
tocBuff[22] = (layer1start >> 8) & 0xff; |
403 |
tocBuff[23] = (layer1start >> 0) & 0xff; |
404 |
} |
405 |
else |
406 |
{ |
407 |
// fake it |
408 |
tocBuff[ 0] = 0x04; |
409 |
tocBuff[ 1] = 0x02; |
410 |
tocBuff[ 2] = 0xF2; |
411 |
tocBuff[ 3] = 0x00; |
412 |
tocBuff[ 4] = 0x86; |
413 |
tocBuff[ 5] = 0x72; |
414 |
|
415 |
tocBuff[16] = 0x00; |
416 |
tocBuff[17] = 0x03; |
417 |
tocBuff[18] = 0x00; |
418 |
tocBuff[19] = 0x00; |
419 |
} |
420 |
} |
421 |
else if ((type == CDVD_TYPE_CDDA) || (type == CDVD_TYPE_PS2CDDA) || |
422 |
(type == CDVD_TYPE_PS2CD) || (type == CDVD_TYPE_PSCDDA) || (type == CDVD_TYPE_PSCD)) |
423 |
{ |
424 |
// cd toc |
425 |
// (could be replaced by 1 command that reads the full toc) |
426 |
u8 min, sec, frm; |
427 |
s32 i, err; |
428 |
cdvdTN diskInfo; |
429 |
cdvdTD trackInfo; |
430 |
memset(tocBuff, 0, 1024); |
431 |
if (CDVDgetTN(&diskInfo) == -1) |
432 |
{ |
433 |
diskInfo.etrack = 0; |
434 |
diskInfo.strack = 1; |
435 |
} |
436 |
if (CDVDgetTD(0, &trackInfo) == -1) trackInfo.lsn = 0; |
437 |
|
438 |
tocBuff[0] = 0x41; |
439 |
tocBuff[1] = 0x00; |
440 |
|
441 |
//Number of FirstTrack |
442 |
tocBuff[2] = 0xA0; |
443 |
tocBuff[7] = itob(diskInfo.strack); |
444 |
|
445 |
//Number of LastTrack |
446 |
tocBuff[12] = 0xA1; |
447 |
tocBuff[17] = itob(diskInfo.etrack); |
448 |
|
449 |
//DiskLength |
450 |
lba_to_msf(trackInfo.lsn, &min, &sec, &frm); |
451 |
tocBuff[22] = 0xA2; |
452 |
tocBuff[27] = itob(min); |
453 |
tocBuff[28] = itob(sec); |
454 |
|
455 |
for (i = diskInfo.strack; i <= diskInfo.etrack; i++) |
456 |
{ |
457 |
err = CDVDgetTD(i, &trackInfo); |
458 |
lba_to_msf(trackInfo.lsn, &min, &sec, &frm); |
459 |
tocBuff[i*10+30] = trackInfo.type; |
460 |
tocBuff[i*10+32] = err == -1 ? 0 : itob(i); //number |
461 |
tocBuff[i*10+37] = itob(min); |
462 |
tocBuff[i*10+38] = itob(sec); |
463 |
tocBuff[i*10+39] = itob(frm); |
464 |
} |
465 |
} |
466 |
else |
467 |
return -1; |
468 |
|
469 |
return 0; |
470 |
} |
471 |
|
472 |
EXPORT_C_(s32) CDVDreadTrack(u32 lsn, int mode) |
473 |
{ |
474 |
int _lsn = lsn; |
475 |
|
476 |
//__Log("CDVDreadTrack: %x %x\n", lsn, mode); |
477 |
if (_lsn < 0) |
478 |
{ |
479 |
// lsn = 2097152 + (-_lsn); |
480 |
lsn = iso->blocks - (-_lsn); |
481 |
} |
482 |
// printf ("CDRreadTrack %d\n", lsn); |
483 |
|
484 |
isoReadBlock(iso, cdbuffer, lsn); |
485 |
if (fdump != NULL) |
486 |
{ |
487 |
isoWriteBlock(fdump, cdbuffer, lsn); |
488 |
} |
489 |
|
490 |
pbuffer = cdbuffer; |
491 |
switch (mode) |
492 |
{ |
493 |
case CDVD_MODE_2352: |
494 |
break; |
495 |
case CDVD_MODE_2340: |
496 |
pbuffer += 12; |
497 |
break; |
498 |
case CDVD_MODE_2328: |
499 |
pbuffer += 24; |
500 |
break; |
501 |
case CDVD_MODE_2048: |
502 |
pbuffer += 24; |
503 |
break; |
504 |
} |
505 |
|
506 |
return 0; |
507 |
} |
508 |
|
509 |
EXPORT_C_(u8*) CDVDgetBuffer() |
510 |
{ |
511 |
return pbuffer; |
512 |
} |
513 |
|
514 |
EXPORT_C_(s32) CDVDgetDiskType() |
515 |
{ |
516 |
return cdtype; |
517 |
} |
518 |
|
519 |
EXPORT_C_(s32) CDVDgetTrayStatus() |
520 |
{ |
521 |
return CDVD_TRAY_CLOSE; |
522 |
} |
523 |
|
524 |
EXPORT_C_(s32) CDVDctrlTrayOpen() |
525 |
{ |
526 |
return 0; |
527 |
} |
528 |
EXPORT_C_(s32) CDVDctrlTrayClose() |
529 |
{ |
530 |
return 0; |
531 |
} |
532 |
|
533 |
|
534 |
EXPORT_C_(s32) CDVDtest() |
535 |
{ |
536 |
if (*IsoFile == 0) return 0; |
537 |
|
538 |
iso = isoOpen(IsoFile); |
539 |
if (iso == NULL) return -1; |
540 |
isoClose(iso); |
541 |
|
542 |
return 0; |
543 |
} |
544 |
|