2 Copyright 2009-2010, jimmikaelkael
3 Licenced under Academic Free License version 3.0
4 Review Open PS2 Loader README & LICENSE files for further details.
11 #include "ioplib_util.h"
13 #ifdef __IOPCORE_DEBUG
14 #define DPRINTF(args...) printf(args)
16 #define DPRINTF(args...) do { } while(0)
19 #define FAKEMOD_ID 0xdead
21 // MODLOAD's exports pointers
22 static int (*LoadStartModule
)(char *modpath
, int arg_len
, char *args
, int *modres
);
23 static int (*StartModule
)(int id
, char *modname
, int arg_len
, char *args
, int *modres
);
24 static int (*LoadModuleBuffer
)(void *ptr
);
25 static int (*StopModule
)(int id
, int arg_len
, char *args
, int *modres
);
26 static int (*UnloadModule
)(int id
);
27 static int (*SearchModuleByName
)(char *modname
);
29 // modules list to fake loading
30 static char *lm_modulefake_list
[] = {
45 #ifdef __IOPCORE_DEBUG
56 static char *lmb_modulefake_list
[] = {
71 #ifdef __IOPCORE_DEBUG
82 static int fakemod_flag
= 0;
84 //--------------------------------------------------------------
85 int getModInfo(u8
*modname
, modinfo_t
*info
)
87 iop_library_table_t
*libtable
;
88 iop_library_t
*libptr
;
91 libtable
= GetLibraryEntryTable();
92 libptr
= libtable
->tail
;
95 if (libptr
->name
[i
] != modname
[i
])
100 libptr
= libptr
->prev
;
103 if(!libptr
) return 0;
105 info
->version
= libptr
->version
;
106 info
->exports
= (void **)(((struct irx_export_table
*)libptr
)->fptrs
);
110 //--------------------------------------------------------------
111 static int checkFakemod(char *modname
, char **fakemod_list
)
113 // check if module is in the list
114 while (*fakemod_list
) {
115 if (strstr(modname
, *fakemod_list
)) {
125 //--------------------------------------------------------------
126 static int isFakemod(void)
129 DPRINTF("isFakemod() module is on fakelist!!!\n");
137 //--------------------------------------------------------------
138 static int Hook_LoadStartModule(char *modpath
, int arg_len
, char *args
, int *modres
)
140 DPRINTF("Hook_LoadStartModule() modpath = %s\n", modpath
);
142 checkFakemod(modpath
, lm_modulefake_list
);
147 return LoadStartModule(modpath
, arg_len
, args
, modres
);
150 //--------------------------------------------------------------
151 static int Hook_StartModule(int id
, char *modname
, int arg_len
, char *args
, int *modres
)
153 DPRINTF("Hook_StartModule() id=%d modname = %s\n", id
, modname
);
158 return StartModule(id
, modname
, arg_len
, args
, modres
);
161 //--------------------------------------------------------------
162 static int Hook_LoadModuleBuffer(void *ptr
)
164 DPRINTF("Hook_LoadModuleBuffer() modname = %s\n", (char *)(ptr
+ 0x8e));
166 if (checkFakemod((char *)(ptr
+ 0x8e), lmb_modulefake_list
))
169 return LoadModuleBuffer(ptr
);
172 //--------------------------------------------------------------
173 static int Hook_StopModule(int id
, int arg_len
, char *args
, int *modres
)
175 DPRINTF("Hook_StopModule() id=%d arg_len=%d\n", id
, arg_len
);
177 if (id
== FAKEMOD_ID
)
180 return StopModule(id
, arg_len
, args
, modres
);
183 //--------------------------------------------------------------
184 static int Hook_UnloadModule(int id
)
186 DPRINTF("Hook_UnloadModule() id=%d\n", id
);
188 if (id
== FAKEMOD_ID
)
191 return UnloadModule(id
);
194 //--------------------------------------------------------------
195 static int Hook_SearchModuleByName(char *modname
)
197 DPRINTF("Hook_SearchModuleByName() modname = %s\n", modname
);
199 if (!strcmp(modname
, "cdvd_ee_driver"))
202 return SearchModuleByName(modname
);
205 //--------------------------------------------------------------
206 void hookMODLOAD(void)
208 // get modload export table
210 getModInfo("modload\0", &info
);
212 // hook modload's LoadStartModule function
213 LoadStartModule
= (void *)info
.exports
[7];
214 info
.exports
[7] = (void *)Hook_LoadStartModule
;
216 // hook modload's StartModule function
217 StartModule
= (void *)info
.exports
[8];
218 info
.exports
[8] = (void *)Hook_StartModule
;
220 // hook modload's LoadModuleBuffer
221 LoadModuleBuffer
= (void *)info
.exports
[10];
222 info
.exports
[10] = (void *)Hook_LoadModuleBuffer
;
224 // check modload version
225 if (info
.version
> 0x102) {
227 // hook modload's StopModule
228 StopModule
= (void *)info
.exports
[20];
229 info
.exports
[20] = (void *)Hook_StopModule
;
231 // hook modload's UnloadModule
232 UnloadModule
= (void *)info
.exports
[21];
233 info
.exports
[21] = (void *)Hook_UnloadModule
;
235 // hook modload's SearchModuleByName
236 SearchModuleByName
= (void *)info
.exports
[22];
237 info
.exports
[22] = (void *)Hook_SearchModuleByName
;
241 iop_library_t
*lib
= (iop_library_t
*)((u32
)info
.exports
- 0x14);
243 struct irx_import_table
*table
;
244 struct irx_import_stub
*stub
;
248 // go through each table that imports the library
249 for(table
= lib
->caller
; table
!= NULL
; table
= table
->next
) {
250 // go through each import in the table
251 for(stub
= (struct irx_import_stub
*) table
->stubs
; stub
->jump
!= 0; stub
++) {
252 // patch the stub to jump to the address specified in the library export table for "fno"
253 stub
->jump
= 0x08000000 | (((u32
) lib
->exports
[stub
->fno
] << 4) >> 6);