PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
loadfile.c
1/*
2# _____ ___ ____ ___ ____
3# ____| | ____| | | |____|
4# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5#-----------------------------------------------------------------------
6# Copyright ps2dev - http://www.ps2dev.org
7# Licenced under Academic Free License version 2.0
8# Review ps2sdk README & LICENSE files for further details.
9*/
10
11#include "eeelfloader.h"
12#include "irx_imports.h"
13#include <kerr.h>
14#include <loadfile-common.h>
15
16#ifdef _IOP
17IRX_ID("LoadModuleByEE", 2, 2);
18#endif
19// Mostly based on the module from SCE SDK 3.1.0
20
21static void loadfile_rpc_service_thread(void *param);
22
23int _start(int argc, char *argv[])
24{
25 const int *BootMode_3;
26 int thid;
27 iop_thread_t thparam;
28
29 (void)argc;
30 (void)argv;
31
32 FlushDcache();
33 BootMode_3 = QueryBootMode(3);
34 if ( BootMode_3 )
35 {
36 int BootMode_3_1;
37
38 BootMode_3_1 = BootMode_3[1];
39 if ( (BootMode_3_1 & 1) != 0 )
40 {
41 printf(" No SIF service(loadfile)\n");
42 return MODULE_NO_RESIDENT_END;
43 }
44 if ( (BootMode_3_1 & 2) != 0 )
45 {
46 printf(" No LoadFile service\n");
47 return MODULE_NO_RESIDENT_END;
48 }
49 }
51 thparam.attr = 0x2000000;
52 thparam.thread = loadfile_rpc_service_thread;
53 thparam.priority = 88;
54 thparam.stacksize = 4096;
55 thparam.option = 0;
56 thid = CreateThread(&thparam);
57 if ( thid <= 0 )
58 {
59 return MODULE_NO_RESIDENT_END;
60 }
61 StartThread(thid, 0);
62 return MODULE_RESIDENT_END;
63}
64
65static int *loadfile_modload(const struct _lf_module_load_arg *in_packet, int length, int *outbuffer)
66{
67 const char *path;
68
69 (void)length;
70
71 path = in_packet->path;
72 if ( IsIllegalBootDevice(path) )
73 {
74 outbuffer[0] = KE_ILLEGAL_OBJECT;
75 }
76 else
77 {
78 printf("loadmodule: fname %s args %d arg %s\n", path, in_packet->p.arg_len, in_packet->args);
79 outbuffer[0] = LoadStartModule(path, in_packet->p.arg_len, in_packet->args, &outbuffer[1]);
80 printf("loadmodule: id %d, ret %d\n", outbuffer[0], outbuffer[1]);
81 }
82 return outbuffer;
83}
84
85static int *loadfile_elfload(const struct _lf_elf_load_arg *in_packet, int length, int *outbuffer)
86{
87 const char *path;
88 int result_out;
89 int result_module_out;
90
91 (void)length;
92
93 path = in_packet->path;
94 if ( IsIllegalBootDevice(path) )
95 {
96 outbuffer[0] = KE_FILEERR;
97 }
98 else
99 {
100 printf("loadelf: fname %s secname %s\n", path, in_packet->secname);
101 outbuffer[0] =
102 loadfile_elfload_innerproc(path, in_packet->epc, in_packet->secname, &result_out, &result_module_out);
103 if ( outbuffer[0] >= 0 )
104 {
105 outbuffer[2] = 0;
106 outbuffer[0] = result_out;
107 outbuffer[1] = result_module_out;
108 }
109 else
110 {
111 outbuffer[3] = outbuffer[0];
112 outbuffer[0] = 0;
113 }
114 }
115 return outbuffer;
116}
117
118static int *loadfile_setaddr(const struct _lf_iop_val_arg *in_packet, int length, int *outbuffer)
119{
120 void *iop_addr;
121 int type;
122
123 (void)length;
124
125 iop_addr = (void *)in_packet->p.iop_addr;
126 type = in_packet->type;
127 switch ( type )
128 {
129 case LF_VAL_BYTE:
130 *(u8 *)iop_addr = in_packet->val.b;
131 break;
132 case LF_VAL_SHORT:
133 *(u16 *)iop_addr = in_packet->val.s;
134 break;
135 case LF_VAL_LONG:
136 *(u32 *)iop_addr = in_packet->val.l;
137 break;
138 default:
139 break;
140 }
141 outbuffer[0] = 0;
142 return outbuffer;
143}
144
145static int *loadfile_getaddr(const struct _lf_iop_val_arg *in_packet, int length, int *outbuffer)
146{
147 void *iop_addr;
148 int type;
149
150 (void)length;
151
152 iop_addr = (void *)in_packet->p.iop_addr;
153 type = in_packet->type;
154 switch ( type )
155 {
156 case LF_VAL_BYTE:
157 outbuffer[0] = *(u8 *)iop_addr;
158 break;
159 case LF_VAL_SHORT:
160 outbuffer[0] = *(u16 *)iop_addr;
161 break;
162 case LF_VAL_LONG:
163 outbuffer[0] = *(u32 *)iop_addr;
164 break;
165 default:
166 break;
167 }
168 return outbuffer;
169}
170
171static int *loadfile_mg_modload(const struct _lf_module_load_arg *in_packet, int length, int *outbuffer)
172{
173 (void)length;
174
175 outbuffer[0] = LoadStartKelfModule(in_packet->path, in_packet->p.arg_len, in_packet->args, &outbuffer[1]);
176 return outbuffer;
177}
178
179static int *loadfile_mg_elfload(const struct _lf_elf_load_arg *in_packet, int length, int *outbuffer)
180{
181 int result_out;
182 int result_module_out;
183
184 (void)length;
185
186 outbuffer[0] =
187 loadfile_mg_elfload_proc(in_packet->path, in_packet->epc, in_packet->secname, &result_out, &result_module_out);
188 if ( outbuffer[0] >= 0 )
189 {
190 outbuffer[2] = 0;
191 outbuffer[0] = result_out;
192 outbuffer[1] = result_module_out;
193 }
194 else
195 {
196 outbuffer[0] = 0;
197 }
198 return outbuffer;
199}
200
201// The following function was added at some point between SDK 1.3 (exclusive) and SDK 1.6 (inclusive).
202static int *loadfile_loadmodulebuffer(const struct _lf_module_buffer_load_arg *in_packet, int length, int *outbuffer)
203{
204 int ModuleBuffer;
205
206 (void)length;
207
208 ModuleBuffer = LoadModuleBuffer(in_packet->p.ptr);
209 if ( ModuleBuffer >= 0 )
210 {
211 outbuffer[0] = StartModule(ModuleBuffer, "LBbyEE", in_packet->q.arg_len, in_packet->args, &outbuffer[1]);
212 }
213 else
214 {
215 outbuffer[0] = ModuleBuffer;
216 }
217 return outbuffer;
218}
219
220// The following function was added at some point between SDK 1.6 (exclusive) and SDK 3.1.0 (inclusive).
221static int *loadfile_stopmodule(struct _lf_module_stop_arg *in_packet, int length, int *outbuffer)
222{
223 (void)length;
224
225 outbuffer[0] = StopModule(in_packet->p.id, in_packet->q.arg_len, in_packet->args, &outbuffer[1]);
226 return outbuffer;
227}
228
229// The following function was added at some point between SDK 1.6 (exclusive) and SDK 3.1.0 (inclusive).
230static int *loadfile_unloadmodule(union _lf_module_unload_arg *in_packet, int length, int *outbuffer)
231{
232 (void)length;
233
234 outbuffer[0] = UnloadModule(in_packet->id);
235 return outbuffer;
236}
237
238// The following function was added at some point between SDK 1.6 (exclusive) and SDK 3.1.0 (inclusive).
239static int *loadfile_searchmodulebyname(struct _lf_search_module_by_name_arg *in_packet, int length, int *outbuffer)
240{
241 (void)length;
242
243 outbuffer[0] = SearchModuleByName(in_packet->name);
244 return outbuffer;
245}
246
247// The following function was added at some point between SDK 1.6 (exclusive) and SDK 3.1.0 (inclusive).
248static int *
249loadfile_searchmodulebyaddress(struct _lf_search_module_by_address_arg *in_packet, int length, int *outbuffer)
250{
251 (void)length;
252
253 outbuffer[0] = SearchModuleByAddress(in_packet->p.ptr);
254 return outbuffer;
255}
256
257// The following function was added at some point between SDK 1.6 (exclusive) and SDK 3.1.0 (inclusive).
258static int *loadfile_get_version(void *in_packet, int length, int *outbuffer)
259{
260 (void)in_packet;
261 (void)length;
262
263 ((u32 *)(outbuffer))[0] = 0x30303133;
264 return outbuffer;
265}
266
267static int loadfile_rpc_outbuf[0x4] __attribute__((aligned(16)));
268
269static int *loadfile_rpc_service_handler(int fno, void *buffer, int length)
270{
271 switch ( fno )
272 {
273 case LF_F_MOD_LOAD:
274 return loadfile_modload((struct _lf_module_load_arg *)buffer, length, loadfile_rpc_outbuf);
275 case LF_F_ELF_LOAD:
276 return loadfile_elfload((struct _lf_elf_load_arg *)buffer, length, loadfile_rpc_outbuf);
277 case LF_F_SET_ADDR:
278 return loadfile_setaddr((struct _lf_iop_val_arg *)buffer, length, loadfile_rpc_outbuf);
279 case LF_F_GET_ADDR:
280 return loadfile_getaddr((struct _lf_iop_val_arg *)buffer, length, loadfile_rpc_outbuf);
281 case LF_F_MG_MOD_LOAD:
282 return loadfile_mg_modload((struct _lf_module_load_arg *)buffer, length, loadfile_rpc_outbuf);
283 case LF_F_MG_ELF_LOAD:
284 return loadfile_mg_elfload((struct _lf_elf_load_arg *)buffer, length, loadfile_rpc_outbuf);
285 case LF_F_MOD_BUF_LOAD:
286 return loadfile_loadmodulebuffer((struct _lf_module_buffer_load_arg *)buffer, length, loadfile_rpc_outbuf);
287 case LF_F_MOD_STOP:
288 return loadfile_stopmodule((struct _lf_module_stop_arg *)buffer, length, loadfile_rpc_outbuf);
289 case LF_F_MOD_UNLOAD:
290 return loadfile_unloadmodule((union _lf_module_unload_arg *)buffer, length, loadfile_rpc_outbuf);
291 case LF_F_SEARCH_MOD_BY_NAME:
292 return loadfile_searchmodulebyname((struct _lf_search_module_by_name_arg *)buffer, length, loadfile_rpc_outbuf);
293 case LF_F_SEARCH_MOD_BY_ADDRESS:
294 return loadfile_searchmodulebyaddress(
295 (struct _lf_search_module_by_address_arg *)buffer, length, loadfile_rpc_outbuf);
296 case LF_F_GET_VERSION:
297 return loadfile_get_version((void *)buffer, length, loadfile_rpc_outbuf);
298 default:
299 return NULL;
300 }
301}
302
303static SifRpcDataQueue_t loadfile_rpc_service_queue __attribute__((aligned(16)));
304static SifRpcServerData_t loadfile_rpc_service_data __attribute__((aligned(16)));
305static int loadfile_rpc_service_in_buf[0x112] __attribute__((aligned(16)));
306
307static void loadfile_rpc_service_thread(void *param)
308{
309 (void)param;
310
311 printf("Load File service.(99/11/05)\n");
312 sceSifInitRpc(0);
313 sceSifSetRpcQueue(&loadfile_rpc_service_queue, GetThreadId());
314 sceSifRegisterRpc(
315 &loadfile_rpc_service_data,
316 0x80000006,
317 (SifRpcFunc_t)loadfile_rpc_service_handler,
318 loadfile_rpc_service_in_buf,
319 0,
320 0,
321 &loadfile_rpc_service_queue);
322 sceSifRpcLoop(&loadfile_rpc_service_queue);
323}
int CpuEnableIntr()
Definition intrman.c:250
typedef __attribute__
Definition tlbfunc.c:60