forked from SKGleba/modoru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernel.c
318 lines (245 loc) · 9.53 KB
/
kernel.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
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
/* kernel.c -- updater patches
*
* Copyright (C) 2019 TheFloW
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
#include <psp2kern/ctrl.h>
#include <psp2kern/kernel/modulemgr.h>
#include <psp2kern/kernel/threadmgr.h>
#include <psp2kern/kernel/sysmem.h>
#include <psp2kern/kernel/cpu.h>
#include <taihen.h>
#include <stdio.h>
#include <string.h>
#define APP_PATH "ux0:app/MODORU000/"
#define MOD_LIST_SIZE 128
int module_get_export_func(SceUID pid, const char* modname, uint32_t libnid, uint32_t funcnid, uintptr_t* func);
int ksceAppMgrLaunchAppByPath(const char* name, const char* cmd, int cmdlen, int dynamic, void* opt, void* id);
static tai_hook_ref_t ksceKernelStartPreloadedModulesRef;
static tai_hook_ref_t ksceSblACMgrIsDevelopmentModeRef;
static tai_hook_ref_t SceSysrootForDriver_421EFC96_ref;
static tai_hook_ref_t SceSysrootForDriver_55392965_ref;
static tai_hook_ref_t ksceSblSmCommCallFuncRef;
static tai_hook_ref_t sceSblSsUpdateMgrGetBootModeForUserRef;
#include "lv0.h"
static SceUID hooks[6];
static int newFw = 0, skipSoftMin = 0, swu_mode = 0xFF;
static int ksceKernelStartPreloadedModulesPatched(SceUID pid) {
int res = TAI_CONTINUE(int, ksceKernelStartPreloadedModulesRef, pid);
char titleid[32];
ksceKernelGetProcessTitleId(pid, titleid, sizeof(titleid));
if (strcmp(titleid, "NPXS10999") == 0) {
ksceKernelLoadStartModuleForPid(pid, "vs0:sys/external/libshellsvc.suprx", 0, NULL, 0, NULL, NULL);
ksceKernelLoadStartModuleForPid(pid, APP_PATH "modoru_user.suprx", 0, NULL, 0, NULL, NULL);
}
return res;
}
static int ksceSblACMgrIsDevelopmentModePatched(void) {
TAI_CONTINUE(int, ksceSblACMgrIsDevelopmentModeRef);
return 1;
}
static int SceSysrootForDriver_421EFC96_patched(void) {
TAI_CONTINUE(int, SceSysrootForDriver_421EFC96_ref);
return 0;
}
static int SceSysrootForDriver_55392965_patched(void) {
TAI_CONTINUE(int, SceSysrootForDriver_55392965_ref);
return 1;
}
static int ksceSblSmCommCallFuncPatched(int id, int service_id, int* f00d_resp, void* data, int size) {
if (newFw) { // current > 3.71
if (service_id == SCE_SBL_SM_COMM_FID_SM_SNVS_ENCDEC_SECTORS && *(uint32_t*)data == 2) // enc/dec snvs, mode 2
lv0_nop32(0x0080bb8c, id); // remove fw check error
else if (skipSoftMin && service_id == SCE_SBL_SM_COMM_FID_SM_AUTH_SPKG) { // target < 1.692
lv0_nop32(0x0080fea0, id); // remove 1.692 soft min fw checks
lv0_nop32(0x00810370, id);
}
} else if (!newFw && skipSoftMin && service_id == SCE_SBL_SM_COMM_FID_SM_AUTH_SPKG) { // current < 3.72, target < 1.692
lv0_nop32(0x0080fe44, id); // remove 1.692 soft min fw checks
lv0_nop32(0x0080fe48, id);
lv0_nop32(0x00810298, id);
lv0_nop32(0x0081029C, id);
}
int res = TAI_CONTINUE(int, ksceSblSmCommCallFuncRef, id, service_id, f00d_resp, data, size);
if (f00d_resp && service_id == SCE_SBL_SM_COMM_FID_SM_AUTH_SPKG && *f00d_resp == 0x800F0237) // fw cmp error
*f00d_resp = 0; // The spkg has actually been decrypted successfully, just fake success
return res;
}
static int sceSblSsUpdateMgrGetBootModeForUserPatched(uintptr_t ubuf) {
uint32_t state;
ENTER_SYSCALL(state);
ksceKernelMemcpyKernelToUser(ubuf, &swu_mode, 1);
EXIT_SYSCALL(state);
return 0;
}
int k_modoru_release_updater_patches(void) {
uint32_t state;
ENTER_SYSCALL(state);
if (hooks[5] >= 0)
taiHookReleaseForKernel(hooks[5], sceSblSsUpdateMgrGetBootModeForUserRef);
if (hooks[4] >= 0)
taiHookReleaseForKernel(hooks[4], ksceSblSmCommCallFuncRef);
if (hooks[3] >= 0)
taiHookReleaseForKernel(hooks[3], SceSysrootForDriver_55392965_ref);
if (hooks[2] >= 0)
taiHookReleaseForKernel(hooks[2], SceSysrootForDriver_421EFC96_ref);
if (hooks[1] >= 0)
taiHookReleaseForKernel(hooks[1], ksceSblACMgrIsDevelopmentModeRef);
if (hooks[0] >= 0)
taiHookReleaseForKernel(hooks[0], ksceKernelStartPreloadedModulesRef);
EXIT_SYSCALL(state);
return 0;
}
int k_modoru_patch_updater(int setSkipSoftMin, int setNewFw) {
int res;
uint32_t state;
ENTER_SYSCALL(state);
newFw = setNewFw;
skipSoftMin = setSkipSoftMin;
memset(hooks, -1, sizeof(hooks));
res = hooks[0] = taiHookFunctionExportForKernel(KERNEL_PID, &ksceKernelStartPreloadedModulesRef, "SceKernelModulemgr",
TAI_ANY_LIBRARY, 0x432DCC7A, ksceKernelStartPreloadedModulesPatched);
if (res < 0) {
res = hooks[0] = taiHookFunctionExportForKernel(KERNEL_PID, &ksceKernelStartPreloadedModulesRef, "SceKernelModulemgr",
TAI_ANY_LIBRARY, 0x998C7AE9, ksceKernelStartPreloadedModulesPatched);
}
if (res < 0)
goto err;
res = hooks[1] = taiHookFunctionImportForKernel(KERNEL_PID, &ksceSblACMgrIsDevelopmentModeRef, "SceAppMgr",
TAI_ANY_LIBRARY, 0xBBA13D9C, ksceSblACMgrIsDevelopmentModePatched);
if (res < 0) {
res = hooks[1] = taiHookFunctionImportForKernel(KERNEL_PID, &ksceSblACMgrIsDevelopmentModeRef, "SceAppMgr",
TAI_ANY_LIBRARY, 0xE87D1777, ksceSblACMgrIsDevelopmentModePatched);
}
if (res < 0)
goto err;
res = hooks[2] = taiHookFunctionImportForKernel(KERNEL_PID, &SceSysrootForDriver_421EFC96_ref, "SceAppMgr",
TAI_ANY_LIBRARY, 0x421EFC96, SceSysrootForDriver_421EFC96_patched);
if (res < 0)
goto err;
res = hooks[3] = taiHookFunctionImportForKernel(KERNEL_PID, &SceSysrootForDriver_55392965_ref, "SceSblUpdateMgr",
TAI_ANY_LIBRARY, 0x55392965, SceSysrootForDriver_55392965_patched);
if (res < 0)
goto err;
res = hooks[4] = taiHookFunctionImportForKernel(KERNEL_PID, &ksceSblSmCommCallFuncRef, "SceSblUpdateMgr",
TAI_ANY_LIBRARY, 0xDB9FC204, ksceSblSmCommCallFuncPatched);
if (res < 0)
goto err;
res = hooks[5] = taiHookFunctionExportForKernel(KERNEL_PID, &sceSblSsUpdateMgrGetBootModeForUserRef, "SceSblUpdateMgr",
TAI_ANY_LIBRARY, 0x8E834565, sceSblSsUpdateMgrGetBootModeForUserPatched);
if (res < 0)
goto err;
EXIT_SYSCALL(state);
return 0;
err:
k_modoru_release_updater_patches();
EXIT_SYSCALL(state);
return res;
}
static int launch_thread(SceSize args, void* argp) {
int opt[52 / 4];
memset(opt, 0, sizeof(opt));
opt[0] = sizeof(opt);
ksceAppMgrLaunchAppByPath("ud0:PSP2UPDATE/psp2swu.self", NULL, 0, 0, opt, NULL);
return ksceKernelExitDeleteThread(0);
}
int k_modoru_launch_updater(int mode) {
uint32_t state;
ENTER_SYSCALL(state);
swu_mode = mode;
SceUID thid = ksceKernelCreateThread("launch_thread", (SceKernelThreadEntry)launch_thread, 0x40, 0x1000, 0, 0, NULL);
if (thid < 0) {
EXIT_SYSCALL(state);
return thid;
}
ksceKernelStartThread(thid, 0, NULL);
EXIT_SYSCALL(state);
return 0;
}
int k_modoru_detect_plugins(void) {
SceKernelModuleInfo info;
SceUID modlist[MOD_LIST_SIZE];
size_t count = MOD_LIST_SIZE;
int res;
uint32_t state;
ENTER_SYSCALL(state);
int (*_ksceKernelGetModuleList)(SceUID pid, int flags1, int flags2, SceUID * modids, size_t * num);
int (*_ksceKernelGetModuleInfo)(SceUID pid, SceUID modid, SceKernelModuleInfo * info);
res = module_get_export_func(KERNEL_PID, "SceKernelModulemgr", TAI_ANY_LIBRARY,
0x97CF7B4E, (uintptr_t*)&_ksceKernelGetModuleList);
if (res < 0)
res = module_get_export_func(KERNEL_PID, "SceKernelModulemgr", TAI_ANY_LIBRARY,
0xB72C75A4, (uintptr_t*)&_ksceKernelGetModuleList);
if (res < 0)
goto err;
res = module_get_export_func(KERNEL_PID, "SceKernelModulemgr", TAI_ANY_LIBRARY,
0xD269F915, (uintptr_t*)&_ksceKernelGetModuleInfo);
if (res < 0)
res = module_get_export_func(KERNEL_PID, "SceKernelModulemgr", TAI_ANY_LIBRARY,
0xDAA90093, (uintptr_t*)&_ksceKernelGetModuleInfo);
if (res < 0)
goto err;
res = _ksceKernelGetModuleList(KERNEL_PID, 0x7fffffff, 1, modlist, &count);
if (res < 0)
goto err;
info.size = sizeof(SceKernelModuleInfo);
res = _ksceKernelGetModuleInfo(KERNEL_PID, modlist[2], &info);
if (res < 0)
goto err;
// Third last kernel module must be either taihen or HENkaku
if (strcmp(info.module_name, "taihen") != 0 && strcmp(info.module_name, "HENkaku") != 0) {
res = 1;
goto err;
}
res = _ksceKernelGetModuleList(ksceKernelGetProcessId(), 0x7fffffff, 1, modlist, &count);
if (res < 0)
goto err;
info.size = sizeof(SceKernelModuleInfo);
res = _ksceKernelGetModuleInfo(ksceKernelGetProcessId(), modlist[1], &info);
if (res < 0)
goto err;
// Second last user module must be SceAppUtil
if (strcmp(info.module_name, "SceAppUtil") != 0) {
res = 1;
goto err;
}
res = 0;
err:
EXIT_SYSCALL(state);
return res;
}
int k_modoru_get_factory_firmware(void) {
uint32_t state;
ENTER_SYSCALL(state);
unsigned int factory_fw = -1;
void* sysroot = ksceKernelGetSysrootBuffer();
if (sysroot)
factory_fw = *(unsigned int*)(sysroot + 8);
EXIT_SYSCALL(state);
return factory_fw;
}
int k_modoru_ctrl_peek_buffer_positive(int port, SceCtrlData* pad_data, int count) {
SceCtrlData pad;
uint32_t off;
uint32_t state;
ENTER_SYSCALL(state);
// Set cpu offset to zero
asm volatile ("mrc p15, 0, %0, c13, c0, 4" : "=r" (off));
asm volatile ("mcr p15, 0, %0, c13, c0, 4" :: "r" (0));
int res = ksceCtrlPeekBufferPositive(port, &pad, count);
// Restore cpu offset
asm volatile ("mcr p15, 0, %0, c13, c0, 4" :: "r" (off));
ksceKernelMemcpyKernelToUser((uintptr_t)pad_data, &pad, sizeof(SceCtrlData));
EXIT_SYSCALL(state);
return res;
}
void _start() __attribute__((weak, alias("module_start")));
int module_start(SceSize args, void* argp) {
return SCE_KERNEL_START_SUCCESS;
}
int module_stop(SceSize args, void* argp) {
k_modoru_release_updater_patches();
return SCE_KERNEL_STOP_SUCCESS;
}