Disabling auto-refresh of game list by default, as it is causing bugs sometimes
[open-ps2-loader.git] / modules / debug / ps2link / cmdHandler.c
blobd6e02549a1d947e5c175a3bf9aca63a13e292d16
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
6 * details.
7 */
9 #include <types.h>
10 #include <ioman.h>
11 #include <sysclib.h>
12 #include <stdio.h>
13 #include <thbase.h>
14 #include <intrman.h>
15 #include <sifman.h>
16 #include <sifrpc.h>
17 #include <modload.h>
18 #include <ps2lib_err.h>
19 #include <poweroff.h>
20 #include <sifcmd.h>
22 #include "ps2ip.h"
23 #include "hostlink.h"
25 #define ntohl(x) htonl(x)
26 #define ntohs(x) htons(x)
28 #ifdef DEBUG
29 #define dbgprintf(args...) printf(args)
30 #else
31 #define dbgprintf(args...) do { } while(0)
32 #endif
34 //////////////////////////////////////////////////////////////////////////
35 // How about a header file?..
36 extern int fsysUnmount(void);
38 //////////////////////////////////////////////////////////////////////////
40 #define BUF_SIZE 1024
41 static char recvbuf[BUF_SIZE] __attribute__((aligned(16)));
42 static unsigned int rpc_data[1024/4]__attribute__((aligned(16)));
44 int excepscrdump = 1;
46 #define PKO_DMA_DEST ((void *)0x200ff800)
47 //unsigned int *dma_ptr =(unsigned int*)(0x20100000-2048);
49 //////////////////////////////////////////////////////////////////////////
50 static void
51 pkoExecIop(char *buf, int len)
53 pko_pkt_execiop_req *cmd;
54 int retval;
55 int arglen;
56 char *path;
57 char *args;
58 unsigned int argc;
59 int id;
60 int i;
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);
68 return;
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));
76 path = &cmd->argv[0];
77 args = &cmd->argv[strlen(cmd->argv) + 1];
78 argc = ntohl(cmd->argc);
80 arglen = 0;
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);
88 if (id < 0) {
89 printf("Error loading module: ");
90 switch (-id) {
91 case E_IOP_INTR_CONTEXT:
92 printf("IOP is in exception context.\n");
93 break;
94 case E_IOP_DEPENDANCY:
95 printf("Inter IRX dependancy error.\n");
96 break;
97 case E_LF_NOT_IRX:
98 printf("Invalid IRX module.\n");
99 break;
100 case E_LF_FILE_NOT_FOUND:
101 printf("Unable to open executable file.\n");
102 break;
103 case E_LF_FILE_IO_ERROR:
104 printf("Error while accessing file.\n");
105 break;
106 case E_IOP_NO_MEMORY:
107 printf("IOP is out of memory.\n");
108 break;
109 default:
110 printf("Unknow error code: %d\n", -retval);
111 break;
113 } else {
114 printf("loadmodule: id %d, ret %d\n", id, retval);
118 //////////////////////////////////////////////////////////////////////////
119 unsigned int
120 pkoSetSifDma(void *dest, void *src, unsigned int length, unsigned int mode)
122 struct t_SifDmaTransfer sendData;
123 int oldIrq;
124 int id;
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);
135 return id;
138 //////////////////////////////////////////////////////////////////////////
139 unsigned int
140 pkoSendSifCmd(unsigned int cmd, void *src, unsigned int len)
142 unsigned int dmaId;
144 rpc_data[0] = cmd;
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);
153 if(dmaId == 0) {
154 printf("IOP: sifSendCmd %x failed\n", cmd);
155 return -1;
157 return 0;
161 //////////////////////////////////////////////////////////////////////////
162 static void
163 pkoExecEE(char *buf, int len)
165 int ret;
167 ret = pkoSendSifCmd(PKO_RPC_EXECEE, buf, len);
169 //////////////////////////////////////////////////////////////////////////
170 static void
171 pkoGSExec(char *buf, int len)
173 int ret;
175 ret = pkoSendSifCmd(PKO_RPC_GSEXEC, buf, len);
177 //////////////////////////////////////////////////////////////////////////
178 static void
179 pkoNetDump(char *buf, int len)
181 int ret;
183 ret = pkoSendSifCmd(PKO_RPC_NETDUMP, buf, len);
185 //////////////////////////////////////////////////////////////////////////
186 static void
187 pkoScrDump(char *buf, int len)
189 int ret;
191 ret = pkoSendSifCmd(PKO_RPC_SCRDUMP, buf, len);
194 //////////////////////////////////////////////////////////////////////////
195 static void
196 pkoPowerOff()
198 PoweroffShutdown();
201 //////////////////////////////////////////////////////////////////////////
202 static void
203 pkoReset(char *buf, int len)
205 int ret;
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);
211 return;
214 printf("unmounting\n");
215 fsysUnmount();
216 printf("unmounted\n");
217 DelDrv("tty");
219 ret = pkoSendSifCmd(PKO_RPC_RESET, buf, len);
222 static void
223 pkoStopVU(char *buf, int len) {
224 int ret;
226 ret = pkoSendSifCmd(PKO_RPC_STOPVU, buf, len);
229 static void
230 pkoStartVU(char *buf, int len) {
231 int ret;
232 ret = pkoSendSifCmd(PKO_RPC_STARTVU, buf, len);
235 static void
236 pkoDumpMem(char *buf, int len) {
237 int ret;
238 ret = pkoSendSifCmd(PKO_RPC_DUMPMEM, buf, len);
241 static void
242 pkoDumpReg(char *buf, int len) {
243 int ret;
244 ret = pkoSendSifCmd(PKO_RPC_DUMPREG, buf, len);
247 static void
248 pkoWriteMem(char *buf, int len) {
249 int ret;
250 ret = pkoSendSifCmd(PKO_RPC_WRITEMEM, buf, len);
253 #ifdef SCREENSHOTS
254 static void
255 pkoScreenShot(char *buf, int len) {
256 int ret;
257 ret = pkoSendSifCmd(PKO_RPC_SCRSHOT, buf, len);
259 #endif
261 //////////////////////////////////////////////////////////////////////////
262 static void
263 cmdListener(int sock)
265 int done;
266 int len;
267 int addrlen;
268 unsigned int cmd;
269 pko_pkt_hdr *header;
270 struct sockaddr_in remote_addr;
272 done = 0;
274 while(!done) {
276 addrlen = sizeof(remote_addr);
277 len = recvfrom(sock, &recvbuf[0], BUF_SIZE, 0,
278 (struct sockaddr *)&remote_addr,
279 &addrlen);
280 dbgprintf("IOP cmd: received packet (%d)\n", len);
282 if (len < 0) {
283 dbgprintf("IOP: cmdListener: recvfrom error (%d)\n", len);
284 continue;
286 if (len < sizeof(pko_pkt_hdr)) {
287 continue;
290 header = (pko_pkt_hdr *)recvbuf;
291 cmd = ntohl(header->cmd);
292 switch (cmd) {
294 case PKO_EXECIOP_CMD:
295 pkoExecIop(recvbuf, len);
296 break;
297 case PKO_EXECEE_CMD:
298 pkoExecEE(recvbuf, len);
299 break;
300 case PKO_POWEROFF_CMD:
301 pkoPowerOff();
302 break;
303 case PKO_RESET_CMD:
304 pkoReset(recvbuf, len);
305 break;
306 case PKO_SCRDUMP_CMD:
307 excepscrdump = 1;
308 pkoScrDump(recvbuf, len);
309 break;
310 case PKO_NETDUMP_CMD:
311 excepscrdump = 0;
312 pkoNetDump(recvbuf, len);
313 break;
314 case PKO_START_VU:
315 pkoStartVU(recvbuf, len);
316 break;
317 case PKO_STOP_VU:
318 pkoStopVU(recvbuf, len);
319 break;
320 case PKO_DUMP_MEM:
321 pkoDumpMem(recvbuf, len);
322 break;
323 case PKO_DUMP_REG:
324 pkoDumpReg(recvbuf, len);
325 break;
326 case PKO_GSEXEC_CMD:
327 pkoGSExec(recvbuf, len);
328 break;
329 case PKO_WRITE_MEM:
330 pkoWriteMem(recvbuf, len);
331 break;
332 #ifdef SCREENSHOTS
333 case PKO_SCRSHOT_CMD:
334 pkoScreenShot(recvbuf, len);
335 break;
336 #endif
337 default:
338 dbgprintf("IOP cmd: Uknown cmd received\n");
339 break;
341 dbgprintf("IOP cmd: waiting for next pkt\n");
345 static void
346 cmdPowerOff(void *arg)
348 #ifdef PWOFFONRESET
349 pkoPowerOff();
350 #else
351 pko_pkt_reset_req reset;
353 reset.cmd = htonl(PKO_RESET_CMD);
354 reset.len = 0;
356 pkoReset((unsigned char *) &reset, sizeof(reset));
357 #endif
360 //////////////////////////////////////////////////////////////////////////
361 static void
362 cmdThread(void *arg)
364 struct sockaddr_in serv_addr;
365 // struct sockaddr_in remote_addr;
366 int sock;
367 int ret;
369 dbgprintf( "IOP cmd: Server Thread Started.\n" );
371 sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
372 if (sock < 0)
374 dbgprintf( "IOP cmd: Socket error %d\n", sock);
375 ExitDeleteThread();
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));
384 if (ret < 0) {
385 dbgprintf("IOP cmd: Udp bind error (%d)\n", sock);
386 ExitDeleteThread();
389 // Do tha thing
390 dbgprintf("IOP cmd: Listening\n");
392 cmdListener(sock);
394 ExitDeleteThread();
398 //////////////////////////////////////////////////////////////////////////
399 int cmdHandlerInit(void)
401 iop_thread_t thread;
402 int pid;
403 int ret;
405 dbgprintf("IOP cmd: Starting thread\n");
407 SifInitRpc(0);
408 SetPowerButtonHandler(cmdPowerOff, NULL);
410 thread.attr = 0x02000000;
411 thread.option = 0;
412 thread.thread = (void *)cmdThread;
413 thread.stacksize = 0x800;
414 thread.priority = 60; //0x1e;
416 pid = CreateThread(&thread);
417 if (pid >= 0) {
418 ret = StartThread(pid, 0);
419 if (ret < 0) {
420 dbgprintf("IOP cmd: Could not start thread\n");
423 else {
424 dbgprintf("IOP cmd: Could not create thread\n");
426 return 0;