usb compatibility improvements (rodries)
[libogc.git] / libdi / stubload.c
blob184a3833119648edf4791617051ed3ea0e17b701
1 #include <errno.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <malloc.h>
6 #include <ogcsys.h>
7 #include <gccore.h>
8 #include <stdarg.h>
9 #include <ctype.h>
10 #include <unistd.h>
12 // haxhaxhax
13 #define _LANGUAGE_ASSEMBLY
14 #include <ogc/machine/asm.h>
15 #undef _LANGUAGE_ASSEMBLY
17 #include "stubasm.h"
19 #include <ogc/lwp_threads.h>
20 #include <ogc/machine/processor.h>
22 static context_storage di_ctx;
24 #define IOS_HEAP_SIZE 0x1000
26 void __IPC_Reinitialize(void);
28 static vu32* const _piReg = (u32*)0xCC003000;
29 static vu16* const _memReg = (u16*)0xCC004000;
31 //#define DEBUG_DVD_STUB
33 #ifdef DEBUG_DVD_STUB
34 #define dprintf printf
35 #else
36 #define dprintf(...)
37 #endif
39 static void dumpregs(void)
41 dprintf(" MSR: %08x\n",mfmsr());
42 dprintf(" SPRGx: %08x %08x %08x %08x\n", mfspr(SPRG0), mfspr(SPRG1), mfspr(SPRG2), mfspr(SPRG3));
43 dprintf(" HID0124: %08x %08x %08x %08x\n", mfspr(HID0), mfspr(HID1), mfspr(HID2), mfspr(HID4));
44 dprintf(" L2CR: %08x\n", mfspr(L2CR));
45 dprintf(" WPAR: %08x\n", mfspr(WPAR));
46 dprintf(" PMCx: %08x %08x %08x %08x\n", mfspr(PMC1), mfspr(PMC2), mfspr(PMC3), mfspr(PMC4));
47 dprintf(" MMCRx: %08x %08x\n", mfspr(MMCR0), mfspr(MMCR1));
49 dprintf(" PI Regs: %08x %08x %08x %08x\n", _piReg[0], _piReg[1], _piReg[2], _piReg[3]);
50 dprintf(" %08x %08x %08x %08x\n", _piReg[4], _piReg[5], _piReg[6], _piReg[7]);
51 dprintf(" %08x %08x %08x %08x\n", _piReg[8], _piReg[9], _piReg[10], _piReg[11]);
54 dprintf(" MI Regs: %04x %04x %04x %04x %04x %04x %04x %04x\n", _memReg[0], _memReg[1], _memReg[2], _memReg[3], _memReg[4], _memReg[5], _memReg[6], _memReg[7]);
55 dprintf(" %04x %04x %04x %04x %04x %04x %04x %04x\n", _memReg[8], _memReg[9], _memReg[10], _memReg[11], _memReg[12], _memReg[13], _memReg[14], _memReg[15]);
56 dprintf(" %04x %04x %04x %04x %04x %04x %04x %04x\n", _memReg[16], _memReg[17], _memReg[18], _memReg[19], _memReg[20], _memReg[21], _memReg[22], _memReg[23]);
57 dprintf(" %04x %04x %04x %04x %04x %04x %04x %04x\n", _memReg[24], _memReg[25], _memReg[26], _memReg[27], _memReg[28], _memReg[29], _memReg[30], _memReg[31]);
58 dprintf(" %04x %04x %04x %04x %04x %04x %04x %04x\n", _memReg[32], _memReg[33], _memReg[34], _memReg[35], _memReg[36], _memReg[37], _memReg[38], _memReg[39]);
59 dprintf(" %04x %04x %04x %04x %04x %04x %04x %04x\n", _memReg[40], _memReg[41], _memReg[42], _memReg[43], _memReg[44], _memReg[45], _memReg[46], _memReg[47]);
63 static register_storage di_regs;
65 static void __distub_saveregs(void)
67 int i;
68 di_regs.timebase = gettime();
69 for(i=1;i<6;i++)
70 di_regs.piReg[i] = _piReg[i];
73 static void __distub_restregs(void)
75 int i;
76 for(i=1;i<6;i++)
77 _piReg[i] = di_regs.piReg[i];
78 //i = _piReg[0]; //clear all interrupts
79 settime(di_regs.timebase);
82 u32 __di_check_ahbprot(void) {
83 s32 res;
84 u64 title_id;
85 u32 tmd_size;
86 STACK_ALIGN(u32, tmdbuf, 1024, 32);
88 res = __ES_Init();
90 if (res < 0) {
91 dprintf("ES failed to initialize\n");
92 return res;
95 res = ES_GetTitleID(&title_id);
96 if (res < 0) {
97 dprintf("ES_GetTitleID() failed: %d\n", res);
98 return res;
101 res = ES_GetStoredTMDSize(title_id, &tmd_size);
102 if (res < 0) {
103 dprintf("ES_GetStoredTMDSize() failed: %d\n", res);
104 return res;
107 if (tmd_size > 4096) {
108 dprintf("TMD too big: %d\n", tmd_size);
109 return -EINVAL;
112 res = ES_GetStoredTMD(title_id, tmdbuf, tmd_size);
113 if (res < 0) {
114 dprintf("ES_GetStoredTMD() failed: %d\n", res);
115 return -EINVAL;
118 if ((tmdbuf[0x76] & 3) == 3) {
119 dprintf("ahbprot flags are set!\n");
120 return 1;
123 dprintf("ahbprot flags are not set!\n");
124 return 0;
127 static u32 __di_find_stub(u64 *title_id) {
128 s32 ios_hid, res;
129 u32 count, i, tmd_view_size;
130 u64 *titles;
131 u16 rev_highest, rev_this;
132 STACK_ALIGN(u8, tmdbuf, 1024, 32);
134 *title_id = 0;
135 rev_highest = 0;
137 ios_hid = iosCreateHeap(IOS_HEAP_SIZE);
138 if (ios_hid < 0) {
139 dprintf("iosCreateHeap() failed: %d\n", ios_hid);
140 return ios_hid;
143 res = ES_GetNumTitles(&count);
144 if (res < 0) {
145 iosDestroyHeap(ios_hid);
146 dprintf("ES_GetNumTitles() failed: %d\n", res);
147 return res;
150 dprintf("%u titles are installed\n", count);
152 titles = iosAlloc(ios_hid, sizeof(u64) * count);
153 if (!titles) {
154 iosDestroyHeap(ios_hid);
155 dprintf("iosAlloc() failed\n");
156 return -1;
159 res = ES_GetTitles(titles, count);
160 if (res < 0) {
161 iosFree(ios_hid, titles);
162 iosDestroyHeap(ios_hid);
163 dprintf("ES_GetTitles() failed: %d\n", res);
164 return res;
167 for (i = 0; i < count; i++) {
168 if ((titles[i] >> 32) != 0x00010008)
169 continue;
171 dprintf("found hidden title 0x%llx\n", titles[i]);
173 res = ES_GetTMDViewSize(titles[i], &tmd_view_size);
174 if (res < 0) {
175 dprintf("ES_GetTMDViewSize() failed: %d\n", res);
176 continue;
179 if (tmd_view_size < 90) {
180 dprintf("TMD too small: %d\n", tmd_view_size);
181 continue;
184 if (tmd_view_size > 1024) {
185 dprintf("TMD too big: %d\n", tmd_view_size);
186 continue;
189 res = ES_GetTMDView(titles[i], tmdbuf, tmd_view_size);
190 if (res < 0) {
191 dprintf("ES_GetTMDView() failed: %d\n", res);
192 continue;
195 if ((tmdbuf[0x18] == 'D') && (tmdbuf[0x19] == 'V')) {
196 rev_this = (tmdbuf[88] << 8) | tmdbuf[89];
197 dprintf("found stub with revision 0x%x\n", rev_this);
199 if (rev_this > rev_highest) {
200 *title_id = titles[i];
201 rev_highest = rev_this;
206 iosFree(ios_hid, titles);
207 iosDestroyHeap(ios_hid);
209 if (*title_id) {
210 dprintf("we have a winner: 0x%llx\n", *title_id);
211 return 0;
214 return -1;
217 s32 __DI_StubLaunch(void)
219 u64 titleID = 0;
220 static tikview views[4] ATTRIBUTE_ALIGN(32);
221 u32 numviews;
222 s32 res;
223 u32 ints;
225 res = __di_find_stub(&titleID);
226 if (res < 0) {
227 dprintf("stub not installed\n");
228 return res;
231 dprintf("Stopping thread timeslice ticker\n");
232 __lwp_thread_stoptimeslice();
234 dprintf("Shutting down IOS subsystems\n");
235 res = __IOS_ShutdownSubsystems();
236 if (res < 0)
237 dprintf("Shutdown failed: %d\n",res);
239 dprintf("Initializing ES\n");
240 __ES_Init();
242 dprintf("Launching TitleID: %016llx\n",titleID);
244 res = ES_GetNumTicketViews(titleID, &numviews);
245 if(res < 0) {
246 dprintf(" GetNumTicketViews failed: %d\n",res);
247 __IOS_InitializeSubsystems();
248 __lwp_thread_starttimeslice();
249 return res;
251 if(numviews > 4) {
252 dprintf(" GetNumTicketViews too many views: %u\n",numviews);
253 __IOS_InitializeSubsystems();
254 __lwp_thread_starttimeslice();
255 return IOS_ETOOMANYVIEWS;
257 res = ES_GetTicketViews(titleID, views, numviews);
258 if(res < 0) {
259 dprintf(" GetTicketViews failed: %d\n",res);
260 __IOS_InitializeSubsystems();
261 __lwp_thread_starttimeslice();
262 return res;
264 dprintf("Ready to launch channel\n");
265 res = ES_LaunchTitleBackground(titleID, &views[0]);
266 if(res<0) {
267 dprintf("Launch failed: %d\n",res);
268 __IOS_InitializeSubsystems();
269 __lwp_thread_starttimeslice();
270 return res;
273 dprintf("Channel launching in the background\n");
274 dprintf("Pre-stub status:\n");
275 dumpregs();
276 dprintf("ISR Disable...\n");
277 _CPU_ISR_Disable( ints );
278 dprintf("Saving regs...\n");
279 __distub_saveregs();
280 dprintf("Taking the plunge...\n");
281 __distub_take_plunge(&di_ctx);
283 dprintf("We're back!\n");
284 dprintf("Restoring regs...\n");
285 __distub_restregs();
286 dprintf("ISR Enable...\n");
287 _CPU_ISR_Restore( ints );
289 dprintf("Post-stub status:\n");
290 dumpregs();
292 __IPC_Reinitialize();
293 __ES_Reset();
295 dprintf("IPC reinitialized\n");
296 sleep(1);
297 dprintf("Restarting IOS subsystems\n");
299 res = __IOS_InitializeSubsystems();
301 dprintf("Subsystems running!\n");
303 res = ES_GetNumTicketViews(titleID, &numviews);
304 if(res < 0)
305 dprintf(" GetNumTicketViews failed: %d\n",res);
306 else
307 dprintf(" GetNumTicketViews: %d\n",numviews);
309 dprintf("Restarting threads timeslice ticker\n");
310 __lwp_thread_starttimeslice();
312 return 0;