-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathps4link.c
More file actions
385 lines (329 loc) · 9.47 KB
/
ps4link.c
File metadata and controls
385 lines (329 loc) · 9.47 KB
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
/*
* PS4link library for PlayStation 4 to communicate and use host file system with ps4sh host tool
* Copyright (C) 2003,2015,2016 Antonio Jose Ramos Marquez (aka bigboss) @psxdev on twitter
* Repository https://github.com/orbisdev/liborbis
* based on ps2vfs, ps2client, ps2link, ps2http tools.
* Credits goes for all people involved in ps2dev project https://github.com/ps2dev
* This file is subject to the terms and conditions of the PS4Link License.
* See the file LICENSE in the main directory of this distribution for more
* details.
*/
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <kernel.h>
#include <unistd.h>
#include <net.h>
#include <sys/socket.h>
#include <debugnet.h>
#include <sys/fcntl.h>
#include "ps4link_internal.h"
#include "ps4link.h"
#include "jailbreak.h"
/*int ps4link_initialized;
int ps4link_requests_port;
int ps4link_commands_port;
int ps4link_fileio_active;
int ps4link_cmdio_active;*/
ScePthread server_request_thid;
ScePthread server_command_thid;
//ScePthread server_payload_thid;
ps4LinkConfiguration *configuration=NULL;
int ps4link_exit=0;
int external_conf=0;
extern int ps4KernelExecute(void *fn, void *uap, int64_t *ret0, int64_t *ret1);
/**
* Init ps4link library
*
* @par Example:
* @code
* int ret;
* ret = psp2LinkInit("172.26.0.2",0x4711,0x4712,0x4712, DEBUG);
* @endcode
*
* @param serverIp - server ip for udp debug
* @param requestPort - ps4 port server for fileio requests
* @param debugPort - udp port for debug
* @param commandPort - ps4 port server for commands
* @param level - DEBUG,ERROR,INFO or NONE
*/
int ps4LinkInit(char *serverIp, int requestPort,int debugPort, int commandPort,int level)
{
int ret;
int64_t ret1;
if(ps4LinkCreateConf())
{
return ps4LinkGetValue(PS4LINK_ACTIVE);
}
configuration->ps4link_requests_port=requestPort;
configuration->ps4link_commands_port=commandPort;
configuration->ps4link_debug_port=commandPort;
if(debugNetInit(serverIp,debugPort,level))
{
sleep(2);
configuration->debugconf=debugNetGetConf();
/* ret=scePthreadCreate(&server_payload_thid, NULL, payload_thread, NULL, "ps4link_request_server_thread");
if(ret==0)
{
debugNetPrintf(DEBUG,"[PS4LINK] Server payload thread UID: 0x%08X\n", server_payload_thid);
}
else
{
debugNetPrintf(DEBUG,"[PS4LINK] Server payload thread could not create error: 0x%08X\n", ret);
scePthreadCancel(server_payload_thid);
}*/
//scePthreadJoin(server_payload_thid, NULL);
// debugNetPrintf(DEBUG,"getuid() : %d\n", getuid());
// if (getuid() != 0) {
// debugNetPrintf(DEBUG,"getuid : %d getgid=%d\n", getuid(),getgid());
//debugNetPrintf(DEBUG,"executing privilege scalation \n");
//myJailbreak();
// debugNetPrintf(DEBUG,"getuid() : %d\n", getuid());
debugNetPrintf(DEBUG,"getuid : %d getgid=%d\n",getuid(),getgid());
//}
ret=scePthreadCreate(&server_request_thid, NULL, ps4link_requests_thread, NULL, "ps4link_request_server_thread");
if(ret==0)
{
debugNetPrintf(DEBUG,"[PS4LINK] Server request thread UID: 0x%08X\n", server_request_thid);
}
else
{
debugNetPrintf(DEBUG,"[PS4LINK] Server request thread could not create error: 0x%08X\n", ret);
scePthreadCancel(server_request_thid);
ps4LinkFinish();
return 0;
}
ret=scePthreadCreate(&server_command_thid, NULL, ps4link_commands_thread, NULL, "ps4link_command_server_thread");
if(ret==0)
{
debugNetPrintf(DEBUG,"[PS4LINK] Server command thread UID: 0x%08X\n", server_command_thid);
}
else
{
debugNetPrintf(DEBUG,"[PS4LINK] Server command thread could not create error: 0x%08X\n", server_command_thid);
scePthreadCancel(server_request_thid);
scePthreadCancel(server_command_thid);
ps4LinkFinish();
return 0;
}
/*ret=scePthreadCancel(server_payload_thid);
if(ret==0)
{
debugNetPrintf(DEBUG,"[PS4LINK] Server payload thread terminated\n");
}
else
{
debugNetPrintf(DEBUG,"[PS4LINK] Server payload thread terminated error: 0x%08X\n", ret);
}*/
/*ps4link library initialized*/
configuration->ps4link_initialized = 1;
}
else
{
configuration->ps4link_initialized = 0;
}
sleep(2);
return ps4LinkGetValue(PS4LINK_ACTIVE);
}
int ps4LinkSetConfig(ps4LinkConfiguration *conf)
{
if(!conf)
{
return 0;
}
configuration=conf;
external_conf=1;
return 1;
}
ps4LinkConfiguration *ps4LinkGetConfig()
{
if(!configuration)
{
return NULL;
}
return configuration;
}
int ps4LinkInitWithConf(ps4LinkConfiguration *conf)
{
int ret;
ret=ps4LinkSetConfig(conf);
if(ret)
{
if(debugNetInitWithConf(conf->debugconf))
{
debugNetPrintf(INFO,"ps4link already initialized using configuration from ps4link\n");
debugNetPrintf(INFO,"ps4link_fileio_active=%d\n",ps4LinkGetValue(FILEIO_ACTIVE));
debugNetPrintf(INFO,"ps4link_cmdsio_active=%d\n",ps4LinkGetValue(CMDSIO_ACTIVE));
debugNetPrintf(INFO,"ps4link_initialized=%d\n",ps4LinkGetValue(PS4LINK_ACTIVE));
debugNetPrintf(INFO,"ps4link_requests_port=%d\n",ps4LinkGetValue(REQUESTS_PORT));
debugNetPrintf(INFO,"ps4link_commands_port=%d\n",ps4LinkGetValue(COMMANDS_PORT));
debugNetPrintf(INFO,"ps4link_debug_port=%d\n",ps4LinkGetValue(DEBUG_PORT));
debugNetPrintf(INFO,"ps4link_fileio_sock=%d\n",ps4LinkGetValue(FILEIO_SOCK));
debugNetPrintf(INFO,"ps4link_requests_sock=%d\n",ps4LinkGetValue(REQUESTS_SOCK));
debugNetPrintf(INFO,"ps4link_commands_sock=%d\n",ps4LinkGetValue(COMMANDS_SOCK));
return ps4LinkGetValue(PS4LINK_ACTIVE);
}
else
{
return 0;
}
}
else
{
return 0;
}
}
int ps4LinkCreateConf()
{
if(!configuration)
{
configuration=malloc(sizeof(ps4LinkConfiguration));
configuration->ps4link_fileio_active=1;
configuration->ps4link_cmdsio_active=1;
configuration->ps4link_initialized=0;
configuration->ps4link_fileio_sock=-1;
configuration->ps4link_requests_sock=-1;
configuration->ps4link_commands_sock=-1;
return 0;
}
if(configuration->ps4link_initialized)
{
return 1;
}
return 0;
}
/**
* Get configuration values from PS4Link library
*
* @par Example:
* @code
* ps4LinkGetValue(PS4LINK_ACTIVE);
* @endcode
*/
int ps4LinkGetValue(ps4LinkValue val)
{
int ret;
switch(val)
{
case FILEIO_ACTIVE:
ret=configuration->ps4link_fileio_active;
break;
case CMDSIO_ACTIVE:
ret=configuration->ps4link_cmdsio_active;
break;
case DEBUGNET_ACTIVE:
ret=configuration->debugconf->debugnet_initialized;
break;
case PS4LINK_ACTIVE:
ret=configuration->ps4link_initialized;
break;
case REQUESTS_PORT:
ret=configuration->ps4link_requests_port;
break;
case COMMANDS_PORT:
ret=configuration->ps4link_commands_port;
break;
case DEBUG_PORT:
ret=configuration->ps4link_debug_port;
break;
case FILEIO_SOCK:
ret=configuration->ps4link_fileio_sock;
break;
case REQUESTS_SOCK:
ret=configuration->ps4link_requests_sock;
break;
case COMMANDS_SOCK:
ret=configuration->ps4link_commands_sock;
break;
case DEBUG_SOCK:
ret=configuration->debugconf->SocketFD;
break;
case LOG_LEVEL:
ret=configuration->debugconf->logLevel;
break;
default:
ret=0;
break;
}
return ret;
}
int ps4LinkIsFinished()
{
return ps4link_exit;
}
/**
* Finish PS4Link library
*
* @par Example:
* @code
* ps4LinkFinish();
* @endcode
*/
void ps4LinkFinish()
{
if(!external_conf)
{
configuration->ps4link_fileio_active=0;
configuration->ps4link_cmdsio_active=0;
configuration->ps4link_initialized=0;
ps4LinkRequestsAbort();
ps4link_exit=1;
while(ps4LinkRequestsIsConnected())
{
}
debugNetFinish();
}
}
/*
uses standard open/lseek/read/close to access sandbox'ed content,
if patch starts with host0:/... , file will be readed via ps4link
*/
uint8_t *DataFromFile(const char *path, int additional_size)
{
int fd; // descriptor to manage file from /mnt/sanbox/...
int filesize = 0; // variable to control file size
int numread = 0;
int use_host = 0;
uint8_t *buf = NULL; // buffer for read from file
if(!memcmp(path, "host0:", 6)) use_host=1; // set flag
debugNetPrintf(INFO,"DataFromFile(%s), use_host:%d, add:%d\n",path,use_host,additional_size);
// we open file in read only
if(use_host) fd=ps4LinkOpen(path,O_RDONLY,0);
else fd=open(path,O_RDONLY);
if(fd<0) //If we can't open file, print the error and return
{
debugNetPrintf(DEBUG,"open file returned error %d\n",fd);
return NULL;
}
// Seek to end to get file size
if(use_host) filesize=ps4LinkLseek(fd,0,SEEK_END);
else filesize=lseek(fd,0,SEEK_END);
if(filesize<0) // If we get an error print it and return
{
debugNetPrintf(DEBUG,"lseek returned error %d\n",fd);
if(buf) free(buf), buf = NULL;
goto end;
}
buf=malloc(filesize + additional_size); // Reserve memory for read buffer
if(!buf)
return NULL;
memset(buf, 0, filesize + additional_size); // wipe
// Seek back to start and read filsesize bytes to buf
if(use_host) {
ps4LinkLseek(fd,0,SEEK_SET);
numread=ps4LinkRead(fd,buf,filesize);
} else {
lseek(fd,0,SEEK_SET);
numread=read(fd,buf,filesize);
}
end:
if(use_host) ps4LinkClose(fd);
else close(fd);
if(numread!=filesize) // if we don't get filesize bytes we are in trouble
{
debugNetPrintf(DEBUG,"read returned error %d\n",numread);
if(buf) free(buf), buf = NULL;
}
return buf; // remember to free()
}