-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodule.c
More file actions
309 lines (225 loc) · 6.83 KB
/
module.c
File metadata and controls
309 lines (225 loc) · 6.83 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
/*
Module functions (somes functions taken form psplink source by Tyranid)
By MPH (mphtheone@hotmail.com)
*/
// *** INCLUDES ***
#include "module.h"
// *** FUNCTIONS ***
SceUID moduleLoad (const char *path, int flags, int type)
{
SceKernelLMOption option;
SceUID mpid;
// If the type is 0, then load the module in the kernel partition, otherwise load it in the user partition.
mpid = (type) ? 2 : 1;
// Initialize structure
memset(&option,0,sizeof(option));
option.size = sizeof(option);
option.mpidtext = mpid;
option.mpiddata = mpid;
option.position = 0;
option.access = 1;
return sceKernelLoadModule(path,flags,(type) ? &option : NULL);
}
u32 moduleLoadStart (const char *path, int flags, int type)
{
SceUID loadResult, startResult;
int status;
loadResult = moduleLoad(path,flags,type);
if (loadResult & 0x80000000) return 1;
startResult = sceKernelStartModule(loadResult,0,NULL,&status,NULL);
if (loadResult != startResult) return 2;
return 0;
}
u32 moduleUnload (const char *name)
{
SceModule *mod;
SceUID modid;
u32 ret;
// Find module address
mod = sceKernelFindModuleByName(name);
if (mod)
{
// Stop module
modid = mod->modid;
ret = sceKernelStopModule(modid,0,NULL,NULL,NULL);
if (!(ret & 0x80000000)) ret = sceKernelUnloadModule(modid);
}
else
ret = SCE_KERNEL_ERROR_UNKNOWN_MODULE;
return ret;
}
u32 moduleFindProc(const char* szMod, const char* szLib, u32 nid)
{
SceModule* modP = sceKernelFindModuleByName(szMod);
if (modP == NULL)
{
return 0;
}
SceLibraryEntryTable* entP = (SceLibraryEntryTable*)modP->ent_top;
while ((u32)entP < ((u32)modP->ent_top + modP->ent_size))
{
if (entP->libname != NULL && strcmp(entP->libname, szLib) == 0)
{
// found lib
int i;
int count = entP->stubcount + entP->vstubcount;
u32* nidtable = (u32*)entP->entrytable;
for (i = 0; i < count; i++)
{
if (nidtable[i] == nid)
{
u32 procAddr = nidtable[count+i];
return procAddr;
}
}
return 0;
}
entP++;
}
return 0;
}
SceLibraryEntryTable *moduleFindLibrary (SceUID modid, const char *library)
{
SceModule *mod;
SceLibraryEntryTable *entryTable, *entryEnd;
// Find memory of module
mod = sceKernelFindModuleByUID(modid);
// If bad module
if ((((long) mod) & 0xFF000000) != 0x88000000) return NULL;
if ((mod->stub_top - mod->ent_top) < 40) return NULL;
// Find entry table
entryTable = (SceLibraryEntryTable *) ((u32 *) mod->ent_top);
entryEnd = (SceLibraryEntryTable *) (((u8 *) mod->ent_top) + mod->ent_size);
// Entry table loop
while (entryTable < entryEnd)
{
// Find name
if (entryTable->libname) // first entry (module info) has name = NULL
{
if (!(strcmp(entryTable->libname,library))) return entryTable;
}
// Next entry
entryTable = (SceLibraryEntryTable *) (((u32 *) entryTable) + entryTable->len);
}
// Not found
return NULL;
}
u32 *moduleFindFunc (SceLibraryEntryTable *entryTable, SceUID nid)
{
u32 *entry;
int x;
// Verify parameters
if (!(entryTable)) return NULL;
// Find entry table
entry = (u32 *) entryTable->entrytable;
// NID loop
for (x=0;x<entryTable->stubcount;x++)
{
// Find function address
if (entry[x] == nid) return &entry[x + entryTable->stubcount + entryTable->vstubcount];
}
return NULL;
}
u32 *moduleFindSyscallFunc (u32 func)
{
u8 **syscall;
ModuleSyscallHeader *sysheader;
u32 *systable;
int size, x;
// Get syscall table
asm("cfc0 %0, $12\n" : "=r"(syscall));
// Exit if failed
if (!(syscall)) return NULL;
// Get syscall header
sysheader = (ModuleSyscallHeader *) *syscall;
// Get syscall table
systable = (u32 *) ((*syscall) + sizeof(ModuleSyscallHeader));
// Get syscall size
size = (sysheader->size - sizeof(ModuleSyscallHeader)) / sizeof(u32);
// Search function
for (x=0;x<size;x++)
{
if (systable[x] == func) return &systable[x];
}
return NULL;
}
u32 moduleHookAddr (u32 *addr, u32 func)
{
int x;
// Verify parameters
if (!(addr)) return 1;
// Disable interrupts
x = pspSdkDisableInterrupts();
// Patch address
*addr = func;
// Apply to cache
sceKernelDcacheWritebackInvalidateRange(addr,sizeof(addr));
sceKernelIcacheInvalidateRange(addr,sizeof(addr));
// Enable interrupts
pspSdkEnableInterrupts(x);
return 0;
}
u32 moduleHookFunc (ModuleFunc *modfunc, SceUID modid, const char *library, SceUID nid, void *func)
{
u32 *addr;
// Verify parameters
if ((!(modfunc)) || (!(library)) || (!(func))) return 1;
// Find address of function in entry table and get pointer in entry table
addr = moduleFindFunc(moduleFindLibrary(modid,library),nid);
// If not found
if (!(addr)) return 2;
// Copy address of function in structure
modfunc->addr = *addr;
// Find address of function in syscall table and get pointer in syscall table
modfunc->sysaddr = moduleFindSyscallFunc(modfunc->addr);
// If not found
if (!(modfunc->sysaddr)) return 3;
// Hook function (copy func address to syscall table, overwrite old func)
return moduleHookAddr(modfunc->sysaddr,(u32) func);
}
u32 moduleRestoreFunc (ModuleFunc *modfunc)
{
// Verify parameters
if (!(modfunc)) return 1;
// Restore func
return moduleHookAddr(modfunc->sysaddr,modfunc->addr);
}
u32 moduleGetFunc (ModuleFunc *modfunc, SceUID modid, const char *library, SceUID nid)
{
u32 *addr;
// Verify parameters
if ((!(modfunc)) || (!(library))) return 1;
// Find address of function in entry table and get pointer in entry table
addr = moduleFindFunc(moduleFindLibrary(modid,library),nid);
// If not found
if (!(addr)) return 2;
// Copy address of function in structure
modfunc->addr = *addr;
modfunc->sysaddr = 0x0;
return 0;
}
u32 modulePatchForReload (const char *name)
{
SceModule *mod;
SceLibraryEntryTable *entryTable, *entryEnd;
// Find module address
mod = sceKernelFindModuleByName(name);
// If bad module
if ((((long) mod) & 0xFF000000) != 0x88000000) return 1;
if ((mod->stub_top - mod->ent_top) < 40) return 1;
// Patch name and attributes
mod->attribute = 0x1006;
mod->modname[0] = '*';
// Find entry table info
entryTable = (SceLibraryEntryTable *) ((u32 *) mod->ent_top);
entryEnd = (SceLibraryEntryTable *) (((u8 *) mod->ent_top) + mod->ent_size);
// Entry loop
while (entryTable < entryEnd)
{
// Patch lib name
if (entryTable->libname) ((char *) entryTable->libname)[0] = '*';
// Next entry
entryTable = (SceLibraryEntryTable *) (((u32 *) entryTable) + entryTable->len);
}
return 0;
}