Disabling auto-refresh of game list by default, as it is causing bugs sometimes
[open-ps2-loader.git] / modules / iopcore / cdvdman / ioplib_util.c
blob0371a1bba3fef61eaf375351f12635ed1260228b
1 /*
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.
5 */
7 #include <loadcore.h>
8 #include <stdio.h>
9 #include <sysclib.h>
11 #include "ioplib_util.h"
13 #ifdef __IOPCORE_DEBUG
14 #define DPRINTF(args...) printf(args)
15 #else
16 #define DPRINTF(args...) do { } while(0)
17 #endif
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[] = {
31 #ifdef USB_DRIVER
32 "USBD.IRX",
33 "CDVDSTM.IRX",
34 NULL
35 #endif
36 #ifdef SMB_DRIVER
37 "DEV9.IRX",
38 "SMAP.IRX",
39 "CDVDSTM.IRX",
40 NULL
41 #endif
42 #ifdef HDD_DRIVER
43 "ATAD.IRX",
44 #ifdef HDPRO
45 #ifdef __IOPCORE_DEBUG
46 "DEV9.IRX",
47 #endif
48 #else
49 "DEV9.IRX",
50 #endif
51 "CDVDSTM.IRX",
52 NULL
53 #endif
56 static char *lmb_modulefake_list[] = {
57 #ifdef USB_DRIVER
58 "USB_driver",
59 "cdvd_st_driver",
60 NULL
61 #endif
62 #ifdef SMB_DRIVER
63 "dev9",
64 "INET_SMAP_driver",
65 "cdvd_st_driver",
66 NULL
67 #endif
68 #ifdef HDD_DRIVER
69 "atad_driver",
70 #ifdef HDPRO
71 #ifdef __IOPCORE_DEBUG
72 "dev9",
73 #endif
74 #else
75 "dev9",
76 #endif
77 "cdvd_st_driver",
78 NULL
79 #endif
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;
89 register int i;
91 libtable = GetLibraryEntryTable();
92 libptr = libtable->tail;
93 while (libptr != 0) {
94 for (i=0; i<8; i++) {
95 if (libptr->name[i] != modname[i])
96 break;
98 if (i==8)
99 break;
100 libptr = libptr->prev;
103 if(!libptr) return 0;
105 info->version = libptr->version;
106 info->exports = (void **)(((struct irx_export_table *)libptr)->fptrs);
107 return 1;
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)) {
116 fakemod_flag = 1;
117 return 1;
119 fakemod_list++;
122 return 0;
125 //--------------------------------------------------------------
126 static int isFakemod(void)
128 if (fakemod_flag) {
129 DPRINTF("isFakemod() module is on fakelist!!!\n");
130 fakemod_flag = 0;
131 return 1;
134 return 0;
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);
144 if (isFakemod())
145 return FAKEMOD_ID;
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);
155 if (isFakemod())
156 return FAKEMOD_ID;
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))
167 return FAKEMOD_ID;
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)
178 return 0;
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)
189 return 0;
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"))
200 return FAKEMOD_ID;
202 return SearchModuleByName(modname);
205 //--------------------------------------------------------------
206 void hookMODLOAD(void)
208 // get modload export table
209 modinfo_t info;
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;
240 // fix imports
241 iop_library_t *lib = (iop_library_t *)((u32)info.exports - 0x14);
243 struct irx_import_table *table;
244 struct irx_import_stub *stub;
246 FlushDcache();
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);
257 FlushIcache();