-
Notifications
You must be signed in to change notification settings - Fork 256
/
ncmd.c
442 lines (373 loc) · 12.8 KB
/
ncmd.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
/*
Copyright 2009, jimmikaelkael
Licenced under Academic Free License version 3.0
Review open-ps2-loader README & LICENSE files for further details.
*/
#include "cdvdfsv-internal.h"
typedef struct
{
u32 lsn;
u32 sectors;
void *buf;
sceCdRMode mode;
void *eeaddr1;
void *eeaddr2;
} RpcCdvd_t;
typedef struct
{
u32 lsn;
u32 sectors;
void *buf;
int cmd;
sceCdRMode mode;
u32 pad;
} RpcCdvdStream_t;
typedef struct
{
u32 bufmax;
u32 bankmax;
void *buf;
u32 pad;
} RpcCdvdStInit_t;
typedef struct
{
u32 lsn; // sector location to start reading from
u32 sectors; // number of sectors to read
void *buf; // buffer address to read to ( bit0: 0=EE, 1=IOP )
} RpcCdvdchain_t; // (EE addresses must be on 64byte alignment)
typedef struct
{ // size = 144
u32 b1len;
u32 b2len;
void *pdst1;
void *pdst2;
u8 buf1[64];
u8 buf2[64];
} cdvdfsv_readee_t;
static sceCdRMode cdvdfsv_Stmode;
static SifRpcServerData_t cdvdNcmds_rpcSD;
static u8 cdvdNcmds_rpcbuf[1024];
static void *cbrpc_cdvdNcmds(int fno, void *buf, int size);
static inline void cdvd_readee(void *buf);
static inline void cdvdSt_read(void *buf);
static inline void cdvd_Stsubcmdcall(void *buf);
static inline void cdvd_readiopm(void *buf);
static inline void cdvd_readchain(void *buf);
static inline void rpcNCmd_cdreadDiskID(void *buf);
static inline void rpcNCmd_cdgetdisktype(void *buf);
enum CDVDFSV_NCMD {
CDVDFSV_NCMD_CDREAD = 1,
CDVDFSV_NCMD_CDDAREAD,
CDVDFSV_NCMD_DVDREAD,
CDVDFSV_NCMD_GETTOC,
CDVDFSV_NCMD_SEEK,
CDVDFSV_NCMD_STANDBY,
CDVDFSV_NCMD_STOP,
CDVDFSV_NCMD_PAUSE,
CDVDFSV_NCMD_STREAM,
CDVDFSV_NCMD_CDDASTREAM,
CDVDFSV_NCMD_APPLYNCMD = 0x0C,
CDVDFSV_NCMD_IOPMREAD,
CDVDFSV_NCMD_DISKRDY,
CDVDFSV_NCMD_READCHAIN,
CDVDFSV_NCMD_READDISKID = 0x11,
CDVDFSV_NCMD_DISKTYPE = 0x17,
CDVDFSV_NCMD_COUNT
};
enum CDVDFSV_ST_CMD {
CDVDFSV_ST_START = 1,
CDVDFSV_ST_READ,
CDVDFSV_ST_STOP,
CDVDFSV_ST_SEEK,
CDVDFSV_ST_INIT,
CDVDFSV_ST_STAT,
CDVDFSV_ST_PAUSE,
CDVDFSV_ST_RESUME,
CDVDFSV_ST_SEEKF
};
//--------------------------------------------------------------
static inline void cdvd_readee(void *buf)
{ // Read Disc data to EE mem buffer
u8 curlsn_buf[16];
u32 nbytes, nsectors, sectors_to_read, size_64b, size_64bb, bytesent, temp;
int sector_size, flag_64b, fsverror;
void *fsvRbuf = (void *)cdvdfsv_buf;
void *eeaddr_64b, *eeaddr2_64b;
cdvdfsv_readee_t readee;
RpcCdvd_t *r = (RpcCdvd_t *)buf;
if (r->sectors == 0) {
*(int *)buf = 0;
return;
}
sector_size = 2328;
if ((r->mode.datapattern & 0xff) != 1) {
sector_size = 2340;
if ((r->mode.datapattern & 0xff) != 2)
sector_size = 2048;
}
r->eeaddr1 = (void *)((u32)r->eeaddr1 & 0x1fffffff);
r->eeaddr2 = (void *)((u32)r->eeaddr2 & 0x1fffffff);
r->buf = (void *)((u32)r->buf & 0x1fffffff);
sceCdDiskReady(0);
sectors_to_read = r->sectors;
bytesent = 0;
memset((void *)curlsn_buf, 0, 16);
readee.pdst1 = (void *)r->buf;
eeaddr_64b = (void *)(((u32)r->buf + 0x3f) & 0xffffffc0); // get the next 64-bytes aligned address
if ((u32)r->buf & 0x3f)
readee.b1len = (((u32)r->buf & 0xffffffc0) - (u32)r->buf) + 64; // get how many bytes needed to get a 64 bytes alignment
else
readee.b1len = 0;
nbytes = r->sectors * sector_size;
temp = (u32)r->buf + nbytes;
eeaddr2_64b = (void *)(temp & 0xffffffc0);
temp -= (u32)eeaddr2_64b;
readee.pdst2 = eeaddr2_64b; // get the end address on a 64 bytes align
readee.b2len = temp; // get bytes remainder at end of 64 bytes align
fsvRbuf += temp;
if (readee.b1len)
flag_64b = 0; // 64 bytes alignment flag
else {
if (temp)
flag_64b = 0;
else
flag_64b = 1;
}
while (1) {
do {
if ((sectors_to_read == 0) || (sceCdGetError() == SCECdErABRT)) {
sysmemSendEE((void *)&readee, (void *)r->eeaddr1, sizeof(cdvdfsv_readee_t));
*((u32 *)&curlsn_buf[0]) = nbytes;
sysmemSendEE((void *)curlsn_buf, (void *)r->eeaddr2, 16);
*(int *)buf = nbytes;
return;
}
if (flag_64b == 0) { // not 64 bytes aligned buf
//The data of the last sector of the chunk will be used to correct buffer alignment.
if (sectors_to_read < CDVDMAN_FS_SECTORS - 1)
nsectors = sectors_to_read;
else
nsectors = CDVDMAN_FS_SECTORS - 1;
temp = nsectors + 1;
} else { // 64 bytes aligned buf
if (sectors_to_read < CDVDMAN_FS_SECTORS)
nsectors = sectors_to_read;
else
nsectors = CDVDMAN_FS_SECTORS;
temp = nsectors;
}
if (sceCdRead(r->lsn, temp, (void *)fsvRbuf, NULL) == 0) {
if (sceCdGetError() == SCECdErNO) {
fsverror = SCECdErREADCF;
sceCdSC(CDSC_SET_ERROR, &fsverror);
}
*(int *)buf = bytesent;
return;
}
sceCdSync(0);
size_64b = nsectors * sector_size;
size_64bb = size_64b;
if (!flag_64b) {
if (sectors_to_read == r->sectors) // check that was the first read. Data read will be skewed by readee.b1len bytes into the adjacent sector.
memcpy((void *)readee.buf1, (void *)fsvRbuf, readee.b1len);
if ((sectors_to_read == nsectors) && (readee.b1len)) // For the last sector read.
size_64bb = size_64b - 64;
}
if (size_64bb > 0) {
sysmemSendEE((void *)(fsvRbuf + readee.b1len), (void *)eeaddr_64b, size_64bb);
bytesent += size_64bb;
}
*((u32 *)&curlsn_buf[0]) = bytesent;
sysmemSendEE((void *)curlsn_buf, (void *)r->eeaddr2, 16);
sectors_to_read -= nsectors;
r->lsn += nsectors;
eeaddr_64b += size_64b;
} while ((flag_64b) || (sectors_to_read));
//At the very last pass, copy readee.b2len bytes from the last sector, to complete the alignment correction.
memcpy((void *)readee.buf2, (void *)(fsvRbuf + size_64b - readee.b2len), readee.b2len);
}
*(int *)buf = bytesent;
}
//-------------------------------------------------------------------------
static inline void cdvdSt_read(void *buf)
{
RpcCdvdStream_t *St = (RpcCdvdStream_t *)buf;
u32 err;
int r, rpos, remaining;
void *ee_addr;
for (rpos = 0, ee_addr = St->buf, remaining = St->sectors; remaining > 0; ee_addr += r << 11, rpos += r, remaining -= r) {
if ((r = sceCdStRead(remaining, (void *)((u32)ee_addr | 0x80000000), 0, &err)) < 1)
break;
}
*(int *)buf = (rpos & 0xFFFF) | (err << 16);
}
//-------------------------------------------------------------------------
static inline void cdvd_Stsubcmdcall(void *buf)
{ // call a Stream Sub function (below) depending on stream cmd sent
RpcCdvdStream_t *St = (RpcCdvdStream_t *)buf;
switch (St->cmd) {
case CDVDFSV_ST_START:
*(int *)buf = sceCdStStart(((RpcCdvdStream_t *)buf)->lsn, &cdvdfsv_Stmode);
break;
case CDVDFSV_ST_READ:
cdvdSt_read(buf);
break;
case CDVDFSV_ST_STOP:
*(int *)buf = sceCdStStop();
break;
case CDVDFSV_ST_SEEK:
*(int *)buf = sceCdStSeek(((RpcCdvdStream_t *)buf)->lsn);
break;
case CDVDFSV_ST_INIT:
*(int *)buf = sceCdStInit(((RpcCdvdStInit_t *)buf)->bufmax, ((RpcCdvdStInit_t *)buf)->bankmax, ((RpcCdvdStInit_t *)buf)->buf);
break;
case CDVDFSV_ST_STAT:
*(int *)buf = sceCdStStat();
break;
case CDVDFSV_ST_PAUSE:
*(int *)buf = sceCdStPause();
break;
case CDVDFSV_ST_RESUME:
*(int *)buf = sceCdStResume();
break;
case CDVDFSV_ST_SEEKF:
*(int *)buf = sceCdStSeekF(((RpcCdvdStream_t *)buf)->lsn);
break;
default:
*(int *)buf = 0;
break;
};
}
static inline void cdvd_readiopm(void *buf)
{
int r, fsverror;
u32 readpos;
r = sceCdRead(((RpcCdvd_t *)buf)->lsn, ((RpcCdvd_t *)buf)->sectors, ((RpcCdvd_t *)buf)->buf, NULL);
while (sceCdSync(1)) {
readpos = sceCdGetReadPos();
sysmemSendEE(&readpos, ((RpcCdvd_t *)buf)->eeaddr2, sizeof(readpos));
DelayThread(8000);
}
if (r == 0) {
if (sceCdGetError() == SCECdErNO) {
fsverror = SCECdErREADCFR;
sceCdSC(CDSC_SET_ERROR, &fsverror);
}
}
}
//-------------------------------------------------------------------------
static inline void cdvd_readchain(void *buf)
{
int i, fsverror;
u32 nsectors, tsectors, lsn, addr, readpos;
RpcCdvdchain_t *ch = (RpcCdvdchain_t *)buf;
for (i = 0, readpos = 0; i < 64; i++, ch++) {
if ((ch->lsn == -1) || (ch->sectors == -1) || ((u32)ch->buf == -1))
break;
lsn = ch->lsn;
tsectors = ch->sectors;
addr = (u32)ch->buf & 0xfffffffc;
if ((u32)ch->buf & 1) { // IOP addr
if (sceCdRead(lsn, tsectors, (void *)addr, NULL) == 0) {
if (sceCdGetError() == SCECdErNO) {
fsverror = SCECdErREADCFR;
sceCdSC(CDSC_SET_ERROR, &fsverror);
}
*(int *)buf = 0;
return;
}
sceCdSync(0);
readpos += tsectors * 2048;
} else { // EE addr
while (tsectors > 0) {
nsectors = (tsectors > CDVDMAN_FS_SECTORS) ? CDVDMAN_FS_SECTORS : tsectors;
if (sceCdRead(lsn, nsectors, cdvdfsv_buf, NULL) == 0) {
if (sceCdGetError() == SCECdErNO) {
fsverror = SCECdErREADCF;
sceCdSC(CDSC_SET_ERROR, &fsverror);
}
*(int *)buf = 0;
return;
}
sceCdSync(0);
sysmemSendEE(cdvdfsv_buf, (void *)addr, nsectors << 11);
lsn += nsectors;
tsectors -= nsectors;
addr += nsectors << 11;
readpos += nsectors << 11;
}
}
//The pointer to the read position variable within EE RAM is stored at ((RpcCdvdchain_t *)buf)[65].sectors.
sysmemSendEE(&readpos, (void *)((RpcCdvdchain_t *)buf)[65].sectors, sizeof(readpos));
}
}
//-------------------------------------------------------------------------
static inline void rpcNCmd_cdreadDiskID(void *buf)
{
u8 *p = (u8 *)buf;
memset(p, 0, 10);
*(int *)buf = sceCdReadDiskID((unsigned int *)&p[4]);
}
//-------------------------------------------------------------------------
static inline void rpcNCmd_cdgetdisktype(void *buf)
{
u8 *p = (u8 *)buf;
*(int *)&p[4] = sceCdGetDiskType();
*(int *)&p[0] = 1;
}
//-------------------------------------------------------------------------
static void *cbrpc_cdvdNcmds(int fno, void *buf, int size)
{ // CD NCMD RPC callback
int sc_param;
sceCdSC(CDSC_IO_SEMA, &fno);
switch (fno) {
case CDVDFSV_NCMD_CDREAD:
case CDVDFSV_NCMD_CDDAREAD:
case CDVDFSV_NCMD_DVDREAD:
cdvd_readee(buf);
break;
case CDVDFSV_NCMD_GETTOC:
*(int *)buf = 1;
break;
case CDVDFSV_NCMD_SEEK:
*(int *)buf = sceCdSeek(*(u32 *)buf);
break;
case CDVDFSV_NCMD_STANDBY:
*(int *)buf = sceCdStandby();
break;
case CDVDFSV_NCMD_STOP:
*(int *)buf = sceCdStop();
break;
case CDVDFSV_NCMD_PAUSE:
*(int *)buf = sceCdPause();
break;
case CDVDFSV_NCMD_STREAM:
cdvd_Stsubcmdcall(buf);
break;
case CDVDFSV_NCMD_IOPMREAD:
cdvd_readiopm(buf);
break;
case CDVDFSV_NCMD_DISKRDY:
*(int *)buf = sceCdDiskReady(0);
break;
case CDVDFSV_NCMD_READCHAIN:
cdvd_readchain(buf);
break;
case CDVDFSV_NCMD_READDISKID:
rpcNCmd_cdreadDiskID(buf);
break;
case CDVDFSV_NCMD_DISKTYPE:
rpcNCmd_cdgetdisktype(buf);
break;
default:
*(int *)buf = 0;
break;
}
sc_param = 0;
sceCdSC(CDSC_IO_SEMA, &sc_param);
return buf;
}
void cdvdfsv_register_ncmd_rpc(SifRpcDataQueue_t *rpc_DQ)
{
sceSifRegisterRpc(&cdvdNcmds_rpcSD, 0x80000595, &cbrpc_cdvdNcmds, cdvdNcmds_rpcbuf, NULL, NULL, rpc_DQ);
}