forked from codestation/nploader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
201 lines (185 loc) · 7.21 KB
/
main.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
/*
* nploader module
*
* Copyright (C) 2011 Codestation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <pspkernel.h>
#include <string.h>
#include "lib.h"
#include "hook.h"
#include "nploader.h"
#include "path.h"
#include "logger.h"
PSP_MODULE_INFO("nploader", PSP_MODULE_KERNEL, 0, 8);
STMOD_HANDLER previous = NULL;
int module_found = 0;
int loader_found = 0;
#define MAX_MODULE_NUMBER 256
void patch_io(SceModule2 *module) {
const char * base = "IoFileMgrForUser";
kprintf(">> Patching %s imports\n", base);
// sceIoOpen
//sceIoOpen_func = NULL;
if (hook_import_bynid(module, base, 0x109F50BC, np_open, 1) < 0) {
kprintf(">>> hook to sceIoOpen failed\n");
// something weird happened so is best to abort now
return;
}
// sceIoOpenAsync
//sceIoOpenAsync_func = NULL;
if (hook_import_bynid(module, base, 0x89AA9906, np_open_async, 1) < 0)
kprintf(">> hook to sceIoOpenAsync failed (not critical)\n");
#ifdef CUSTOM_PATH
if(use_custom_path) {
// sceIoDopen
//sceIoDopen_func = NULL;
// those function get hooked only is a custom path was declared
if(hook_import_bynid(module, base, 0xB29DDF9C, np_dopen, 1) < 0)
kprintf(">>> hook to sceIoDopen failed (used for dlc redirection)\n");
// sceIoChdir
//sceIoChdir_func = NULL;
if(hook_import_bynid(module, base, 0x55F4717D, np_chdir, 1) < 0)
kprintf(">>> hook to sceIoChdir failed (used for dlc redirection)\n");
// sceIoGetstat
//sceIoGetstat_func = NULL;
if(hook_import_bynid(module, base, 0xACE946E8, np_stat, 1) < 0)
kprintf(">>> hook to sceIoGetstat failed (used for dlc redirection)\n");
}
#endif
}
void patch_load(SceModule2 *module) {
const char *base = "ModuleMgrForUser";
kprintf(">> Patching %s imports\n", base);
// sceKernelLoadModule2
//sceKernelLoadModuleNpDrm_func = NULL;
if (hook_import_bynid(module, base, 0xF2D8D1B4, np_load, 1) < 0)
kprintf(">>> hook to sceKernelLoadModuleNpDrm failed (not critical)\n");
}
int patch_drm_driver(){
kprintf(">> patch_drm_driver()");
u32 addr;
kprintf("finding module scePSPNpDrm_Driver\n");
SceModule2 *mod = FindModuleByName("scePspNpDrm_Driver");
if(mod){
kprintf("scanning addr range %08X to %08X\n", mod->text_addr, mod->text_size);
for (addr = mod->text_addr; addr < (mod->text_addr + mod->text_size ); addr += 4) {
if (_lw(addr) == 0x2CC60080) { //sltiu $a2, $a2, 128
kprintf("hijack_function at %08X\n", addr - 8);
HIJACK_FUNCTION(addr - 8, setup_edat_version_key_hook, setup_edat_version_key);
break;
}
}
}
return 1;
}
int patch_drm(SceModule2 *module) {
const char *base = "scePspNpDrm_user";
kprintf(">> Patching %s imports\n", base);
// sceNpDrmEdataSetupKey
sceNpDrmEdataSetupKey_func = NULL;
if (hook_import_bynid(module, base, 0x08D98894, np_setup, 1) < 0) {
kprintf(">>> hook to sceNpDrmEdataSetupKey failed\n");
return 0;
}
// sceNpDrmEdataGetDataSize
sceNpDrmEdataGetDataSize_func = NULL;
if (hook_import_bynid(module, base, 0x219EF5CC, np_size, 1) < 0)
kprintf(">>> hook to sceNpDrmEdataGetDataSize failed (not critical)\n");
// sceNpDrmRenameCheck
sceNpDrmRenameCheck_func = NULL;
if (hook_import_bynid(module, base, 0x275987D1, np_rename, 1) < 0)
kprintf(">>> hook to sceNpDrmRenameCheck failed (not critical)\n");
return 1;
}
int hook_user_modules() {
int count;
SceUID memid = sceKernelAllocPartitionMemory(PSP_MEMORY_PARTITION_KERNEL,
"np_modid", PSP_SMEM_High, MAX_MODULE_NUMBER * sizeof(SceUID), NULL);
if(memid < 0) {
kprintf("%s alloc failed: %08X\n", __func__, memid);
return 0;
}
SceUID *modid_lst = sceKernelGetBlockHeadAddr(memid);
memset(modid_lst, 0, MAX_MODULE_NUMBER * sizeof(SceUID));
int ret = sceKernelGetModuleIdList(modid_lst, MAX_MODULE_NUMBER * sizeof(SceUID), &count);
if(ret < 0) {
kprintf("%s module list failed: %08X\n", __func__, ret);
return 0;
}
for(int i = 0; i < count; i++) {
SceModule2 *pmod = (SceModule2*)sceKernelFindModuleByUID(modid_lst[i]);
if (pmod && !(pmod->attribute & 0x1000)) {
kprintf("> Re-patching module: %s\n", pmod->modname);
patch_drm(pmod);
}
}
sceKernelFreePartitionMemory(memid);
return 1;
}
int module_start_handler(SceModule2 * module) {
kprintf("> Loaded, text_addr: %08X, entry_addr: %08X, name: %s\n", module->text_addr, module->entry_addr, module->modname);
if (!module_found &&
(module->text_addr == 0x08804000 || // base address for game eboots
module->text_addr == 0x08900000) && // new games seems to load at this address
module->entry_addr != 0xFFFFFFFF && // skip some user mode prx that loads @ 0x08804000
strcmp(module->modname, "opnssmp")){ // this loads @ 0x08804000 too
//blacklist the Prometheus iso loader
if (!loader_found && (!strcmp(module->modname, "PLoaderGUI"))) {
kprintf("Prometheus loader found\n");
loader_found = 1;
} else {
kprintf("Game found: %s\n", module->modname);
if (patch_drm(module)) {
#ifdef CUSTOM_PATH
read_custom_path();
#endif
// only patch the sceio and loadcore funcs if
// the npdrm hook was successful
patch_io(module);
patch_load(module);
patch_drm_driver();
}
module_found = 1;
}
} else {
// patch other user modules that uses the npdrm api (fixes Buzz! video load)
if(module_found && !(module->attribute & 0x1000)) {
kprintf("> Patching module: %s\n", module->modname);
if(patch_drm(module)) {
patch_io(module);
patch_load(module);
}
}
}
if (module_found && !strcmp(module->modname, "scePspNpDrm_Driver")) {
// some games reload scePspNpDrm and it could change address so we need
// to invalidate the previous func addresses and re-hook
hook_user_modules();
patch_drm_driver();
}
return previous ? previous(module) : 0;
}
int module_start(SceSize argc, void *argp) {
kprintf("------------------\nNPloader starting\n");
#ifdef CUSTOM_PATH
get_plugin_path(argp);
#endif
previous = sctrlHENSetStartModuleHandler(module_start_handler);
return 0;
}
int module_stop(SceSize args, void *argp) {
return 0;
}