1 /*********************************************************************
2 * Copyright (C) 2003 Tord Lindstrom (pukko@home.se)
3 * Copyright (C) 2003,2004 adresd (adresd_ps2dev@yahoo.com)
4 * This file is subject to the terms and conditions of the PS2Link License.
5 * See the file LICENSE in the main directory of this distribution for more
18 #include <ps2lib_err.h>
25 #define ntohl(x) htonl(x)
26 #define ntohs(x) htons(x)
29 #define dbgprintf(args...) printf(args)
31 #define dbgprintf(args...) do { } while(0)
34 //////////////////////////////////////////////////////////////////////////
35 // How about a header file?..
36 extern int fsysUnmount(void);
38 //////////////////////////////////////////////////////////////////////////
41 static char recvbuf
[BUF_SIZE
] __attribute__((aligned(16)));
42 static unsigned int rpc_data
[1024/4]__attribute__((aligned(16)));
46 #define PKO_DMA_DEST ((void *)0x200ff800)
47 //unsigned int *dma_ptr =(unsigned int*)(0x20100000-2048);
49 //////////////////////////////////////////////////////////////////////////
51 pkoExecIop(char *buf
, int len
)
53 pko_pkt_execiop_req
*cmd
;
62 cmd
= (pko_pkt_execiop_req
*)buf
;
64 dbgprintf("IOP cmd: EXECIOP\n");
66 if (len
!= sizeof(pko_pkt_execiop_req
)) {
67 dbgprintf("IOP cmd: exec_iop got a broken packet (%d)!\n", len
);
71 // Make sure arg vector is null-terminated
72 cmd
->argv
[PKO_MAX_PATH
-1] = '\0';
74 printf("IOP cmd: %d args\n", ntohl(cmd
->argc
));
77 args
= &cmd
->argv
[strlen(cmd
->argv
) + 1];
78 argc
= ntohl(cmd
->argc
);
81 for (i
= 0; i
< (argc
- 1); i
++) {
82 printf("arg %d: %s (%d)\n", i
, &args
[arglen
], arglen
);
83 arglen
+= strlen(&args
[arglen
]) + 1;
86 id
= LoadStartModule(cmd
->argv
, arglen
, args
, &retval
);
89 printf("Error loading module: ");
91 case E_IOP_INTR_CONTEXT
:
92 printf("IOP is in exception context.\n");
94 case E_IOP_DEPENDANCY
:
95 printf("Inter IRX dependancy error.\n");
98 printf("Invalid IRX module.\n");
100 case E_LF_FILE_NOT_FOUND
:
101 printf("Unable to open executable file.\n");
103 case E_LF_FILE_IO_ERROR
:
104 printf("Error while accessing file.\n");
106 case E_IOP_NO_MEMORY
:
107 printf("IOP is out of memory.\n");
110 printf("Unknow error code: %d\n", -retval
);
114 printf("loadmodule: id %d, ret %d\n", id
, retval
);
118 //////////////////////////////////////////////////////////////////////////
120 pkoSetSifDma(void *dest
, void *src
, unsigned int length
, unsigned int mode
)
122 struct t_SifDmaTransfer sendData
;
126 sendData
.src
= (unsigned int *)src
;
127 sendData
.dest
= (unsigned int *)dest
;
128 sendData
.size
= length
;
129 sendData
.attr
= mode
;
131 CpuSuspendIntr(&oldIrq
);
132 id
= sceSifSetDma(&sendData
, 1);
133 CpuResumeIntr(oldIrq
);
138 //////////////////////////////////////////////////////////////////////////
140 pkoSendSifCmd(unsigned int cmd
, void *src
, unsigned int len
)
146 memcpy(&rpc_data
[1], src
,
147 (len
> sizeof(rpc_data
) ? sizeof(rpc_data
) : len
));
149 len
= len
> sizeof(rpc_data
) ? sizeof(rpc_data
) : len
;
151 dmaId
= pkoSetSifDma(PKO_DMA_DEST
, rpc_data
, len
, 4);
154 printf("IOP: sifSendCmd %x failed\n", cmd
);
161 //////////////////////////////////////////////////////////////////////////
163 pkoExecEE(char *buf
, int len
)
167 ret
= pkoSendSifCmd(PKO_RPC_EXECEE
, buf
, len
);
169 //////////////////////////////////////////////////////////////////////////
171 pkoGSExec(char *buf
, int len
)
175 ret
= pkoSendSifCmd(PKO_RPC_GSEXEC
, buf
, len
);
177 //////////////////////////////////////////////////////////////////////////
179 pkoNetDump(char *buf
, int len
)
183 ret
= pkoSendSifCmd(PKO_RPC_NETDUMP
, buf
, len
);
185 //////////////////////////////////////////////////////////////////////////
187 pkoScrDump(char *buf
, int len
)
191 ret
= pkoSendSifCmd(PKO_RPC_SCRDUMP
, buf
, len
);
194 //////////////////////////////////////////////////////////////////////////
201 //////////////////////////////////////////////////////////////////////////
203 pkoReset(char *buf
, int len
)
207 dbgprintf("IOP cmd: RESET\n");
209 if (len
!= sizeof(pko_pkt_reset_req
)) {
210 dbgprintf("IOP cmd: exec_ee got a broken packet (%d)!\n", len
);
214 printf("unmounting\n");
216 printf("unmounted\n");
219 ret
= pkoSendSifCmd(PKO_RPC_RESET
, buf
, len
);
223 pkoStopVU(char *buf
, int len
) {
226 ret
= pkoSendSifCmd(PKO_RPC_STOPVU
, buf
, len
);
230 pkoStartVU(char *buf
, int len
) {
232 ret
= pkoSendSifCmd(PKO_RPC_STARTVU
, buf
, len
);
236 pkoDumpMem(char *buf
, int len
) {
238 ret
= pkoSendSifCmd(PKO_RPC_DUMPMEM
, buf
, len
);
242 pkoDumpReg(char *buf
, int len
) {
244 ret
= pkoSendSifCmd(PKO_RPC_DUMPREG
, buf
, len
);
248 pkoWriteMem(char *buf
, int len
) {
250 ret
= pkoSendSifCmd(PKO_RPC_WRITEMEM
, buf
, len
);
255 pkoScreenShot(char *buf
, int len
) {
257 ret
= pkoSendSifCmd(PKO_RPC_SCRSHOT
, buf
, len
);
261 //////////////////////////////////////////////////////////////////////////
263 cmdListener(int sock
)
270 struct sockaddr_in remote_addr
;
276 addrlen
= sizeof(remote_addr
);
277 len
= recvfrom(sock
, &recvbuf
[0], BUF_SIZE
, 0,
278 (struct sockaddr
*)&remote_addr
,
280 dbgprintf("IOP cmd: received packet (%d)\n", len
);
283 dbgprintf("IOP: cmdListener: recvfrom error (%d)\n", len
);
286 if (len
< sizeof(pko_pkt_hdr
)) {
290 header
= (pko_pkt_hdr
*)recvbuf
;
291 cmd
= ntohl(header
->cmd
);
294 case PKO_EXECIOP_CMD
:
295 pkoExecIop(recvbuf
, len
);
298 pkoExecEE(recvbuf
, len
);
300 case PKO_POWEROFF_CMD
:
304 pkoReset(recvbuf
, len
);
306 case PKO_SCRDUMP_CMD
:
308 pkoScrDump(recvbuf
, len
);
310 case PKO_NETDUMP_CMD
:
312 pkoNetDump(recvbuf
, len
);
315 pkoStartVU(recvbuf
, len
);
318 pkoStopVU(recvbuf
, len
);
321 pkoDumpMem(recvbuf
, len
);
324 pkoDumpReg(recvbuf
, len
);
327 pkoGSExec(recvbuf
, len
);
330 pkoWriteMem(recvbuf
, len
);
333 case PKO_SCRSHOT_CMD
:
334 pkoScreenShot(recvbuf
, len
);
338 dbgprintf("IOP cmd: Uknown cmd received\n");
341 dbgprintf("IOP cmd: waiting for next pkt\n");
346 cmdPowerOff(void *arg
)
351 pko_pkt_reset_req reset
;
353 reset
.cmd
= htonl(PKO_RESET_CMD
);
356 pkoReset((unsigned char *) &reset
, sizeof(reset
));
360 //////////////////////////////////////////////////////////////////////////
364 struct sockaddr_in serv_addr
;
365 // struct sockaddr_in remote_addr;
369 dbgprintf( "IOP cmd: Server Thread Started.\n" );
371 sock
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
374 dbgprintf( "IOP cmd: Socket error %d\n", sock
);
378 memset((void *)&serv_addr
, 0, sizeof(serv_addr
));
379 serv_addr
.sin_family
= AF_INET
;
380 serv_addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
381 serv_addr
.sin_port
= htons(PKO_CMD_PORT
);
383 ret
= bind(sock
, (struct sockaddr
*)&serv_addr
, sizeof(serv_addr
));
385 dbgprintf("IOP cmd: Udp bind error (%d)\n", sock
);
390 dbgprintf("IOP cmd: Listening\n");
398 //////////////////////////////////////////////////////////////////////////
399 int cmdHandlerInit(void)
405 dbgprintf("IOP cmd: Starting thread\n");
408 SetPowerButtonHandler(cmdPowerOff
, NULL
);
410 thread
.attr
= 0x02000000;
412 thread
.thread
= (void *)cmdThread
;
413 thread
.stacksize
= 0x800;
414 thread
.priority
= 60; //0x1e;
416 pid
= CreateThread(&thread
);
418 ret
= StartThread(pid
, 0);
420 dbgprintf("IOP cmd: Could not start thread\n");
424 dbgprintf("IOP cmd: Could not create thread\n");