forked from flatz/ps4_remote_pkg_installer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
module.c
211 lines (165 loc) · 4.38 KB
/
module.c
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
#include "module.h"
#include "util.h"
SceKernelModule sceKernelLoadStartModuleFromSandbox(const char* name, size_t args, const void* argp, unsigned int flags, const SceKernelLoadModuleOpt* opts, int* res) {
static const char* sandboxWord = NULL;
char filePath[SCE_KERNEL_MAX_NAME_LENGTH];
SceKernelModule handle;
if (!sandboxWord) {
sandboxWord = sceKernelGetFsSandboxRandomWord();
if (!sandboxWord) {
return SCE_KERNEL_ERROR_EFAULT;
}
}
snprintf(filePath, sizeof(filePath), "/%s/common/lib/%s", sandboxWord, name);
handle = sceKernelLoadStartModule(filePath, args, argp, flags, opts, res);
return handle;
}
int sceKernelGetModuleInfo(SceKernelModule handle, SceKernelModuleInfo* info) {
int ret;
if (!info) {
ret = SCE_KERNEL_ERROR_EFAULT;
goto err;
}
memset(info, 0, sizeof(*info));
{
info->size = sizeof(*info);
}
ret = syscall(SYS_dynlib_get_info, handle, info); /* TODO: make proper error code */
err:
return ret;
}
int sceKernelGetModuleInfoByName(const char* name, SceKernelModuleInfo* info) {
SceKernelModuleInfo tmpInfo;
SceKernelModule handles[SCE_KERNEL_MAX_MODULES];
size_t numModules;
size_t i;
int ret;
if (!name) {
ret = SCE_KERNEL_ERROR_EFAULT;
goto err;
}
if (!info) {
ret = SCE_KERNEL_ERROR_EFAULT;
goto err;
}
memset(handles, 0, sizeof(handles));
ret = sceKernelGetModuleList(handles, ARRAY_SIZE(handles), &numModules);
if (ret) {
goto err;
}
for (i = 0; i < numModules; ++i) {
ret = sceKernelGetModuleInfo(handles[i], &tmpInfo);
if (ret) {
goto err;
}
if (strcmp(tmpInfo.name, name) == 0) {
memcpy(info, &tmpInfo, sizeof(tmpInfo));
ret = 0;
goto err;
}
}
ret = SCE_KERNEL_ERROR_ENOENT;
err:
return ret;
}
int sceKernelGetModuleInfoEx(SceKernelModule handle, SceKernelModuleInfoEx* info) {
int ret;
if (!info) {
ret = SCE_KERNEL_ERROR_EFAULT;
goto err;
}
memset(info, 0, sizeof(*info));
{
info->size = sizeof(*info);
}
ret = syscall(SYS_dynlib_get_info_ex, handle, info); /* TODO: make proper error code */
err:
return ret;
}
int sceKernelGetModuleInfoExByName(const char* name, SceKernelModuleInfoEx* info) {
SceKernelModuleInfoEx tmpInfo;
SceKernelModule handles[SCE_KERNEL_MAX_MODULES];
size_t numModules;
size_t i;
int ret;
if (!name) {
ret = SCE_KERNEL_ERROR_EFAULT;
goto err;
}
if (!info) {
ret = SCE_KERNEL_ERROR_EFAULT;
goto err;
}
memset(handles, 0, sizeof(handles));
ret = sceKernelGetModuleList(handles, ARRAY_SIZE(handles), &numModules);
if (ret) {
goto err;
}
for (i = 0; i < numModules; ++i) {
ret = sceKernelGetModuleInfoEx(handles[i], &tmpInfo);
if (ret) {
goto err;
}
if (strcmp(tmpInfo.name, name) == 0) {
memcpy(info, &tmpInfo, sizeof(tmpInfo));
ret = 0;
goto err;
}
}
ret = SCE_KERNEL_ERROR_ENOENT;
err:
return ret;
}
int sceKernelDlsymEx(SceKernelModule handle, const char* symbol, const char* lib, unsigned int flags, void** addrp) {
int ret;
if (!symbol) {
ret = SCE_KERNEL_ERROR_EFAULT;
goto err;
}
if (!lib) {
ret = SCE_KERNEL_ERROR_EFAULT;
goto err;
}
if (!addrp) {
ret = SCE_KERNEL_ERROR_EFAULT;
goto err;
}
ret = syscall(SYS_supercall, SUPERCALL_DLSYM, handle, symbol, lib, flags, addrp); /* TODO: make proper error code */
err:
return ret;
}
bool get_module_base(const char* name, uint64_t* base, uint64_t* size) {
SceKernelModuleInfo moduleInfo;
int ret;
ret = sceKernelGetModuleInfoByName(name, &moduleInfo);
if (ret) {
EPRINTF("sceKernelGetModuleInfoByName(%s) failed: 0x%08X\n", name, ret);
goto err;
}
if (base)
*base = (uint64_t)moduleInfo.segmentInfo[0].baseAddr;
if (size)
*size = moduleInfo.segmentInfo[0].size;
return true;
err:
return false;
}
bool patch_module(const char* name, module_patch_cb_t* cb, void* arg) {
uint64_t base, size;
int ret;
if (!get_module_base(name, &base, &size)) {
goto err;
}
printf("%s: base:0x%" PRIX64 " size:0x%" PRIX64 "\n", name, base, size);
ret = sceKernelMprotect((void*)base, size, SCE_KERNEL_PROT_CPU_READ | SCE_KERNEL_PROT_CPU_WRITE | SCE_KERNEL_PROT_CPU_EXEC);
if (ret) {
EPRINTF("sceKernelMprotect(%s) failed: 0x%08X\n", name, ret);
goto err;
}
if (cb) {
(*cb)(arg, (uint8_t*)base, size);
}
return true;
err:
return false;
}