Fixed smbClose() for older dkppc versions
[libogc.git] / libdi / stubload.c
blob5503271c0657c0baed271b0543b5a427c691625a
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 context_storage di_ctx;
24 #define DVD_TITLEID 0x0001000844564458LL
25 //#define DVD_TITLEID 0x1000148415858LL
27 void __IPC_Reinitialize(void);
28 extern void __exi_init();
30 static vu16* const _viReg = (u16*)0xCC002000;
31 static vu32* const _piReg = (u32*)0xCC003000;
32 static vu16* const _memReg = (u16*)0xCC004000;
33 static vu16* const _dspReg = (u16*)0xCC005000;
35 //#define DEBUG_DVD_STUB
37 #ifdef DEBUG_DVD_STUB
38 #define dprintf printf
39 #else
40 #define dprintf(...)
41 #endif
43 void dumpregs(void)
45 dprintf(" MSR: %08x\n",mfmsr());
46 dprintf(" SPRGx: %08x %08x %08x %08x\n", mfspr(SPRG0), mfspr(SPRG1), mfspr(SPRG2), mfspr(SPRG3));
47 dprintf(" HID0124: %08x %08x %08x %08x\n", mfspr(HID0), mfspr(HID1), mfspr(HID2), mfspr(HID4));
48 dprintf(" L2CR: %08x\n", mfspr(L2CR));
49 dprintf(" WPAR: %08x\n", mfspr(WPAR));
50 dprintf(" PMCx: %08x %08x %08x %08x\n", mfspr(PMC1), mfspr(PMC2), mfspr(PMC3), mfspr(PMC4));
51 dprintf(" MMCRx: %08x %08x\n", mfspr(MMCR0), mfspr(MMCR1));
53 dprintf(" PI Regs: %08x %08x %08x %08x\n", _piReg[0], _piReg[1], _piReg[2], _piReg[3]);
54 dprintf(" %08x %08x %08x %08x\n", _piReg[4], _piReg[5], _piReg[6], _piReg[7]);
55 dprintf(" %08x %08x %08x %08x\n", _piReg[8], _piReg[9], _piReg[10], _piReg[11]);
58 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]);
59 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]);
60 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]);
61 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]);
62 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]);
63 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]);
67 register_storage di_regs;
69 void __distub_saveregs(void)
71 int i;
72 di_regs.timebase = gettime();
73 for(i=1;i<6;i++)
74 di_regs.piReg[i] = _piReg[i];
77 void __distub_restregs(void)
79 int i;
80 for(i=1;i<6;i++)
81 _piReg[i] = di_regs.piReg[i];
82 //i = _piReg[0]; //clear all interrupts
83 settime(di_regs.timebase);
87 s32 __DI_StubLaunch(void)
89 u64 titleID = DVD_TITLEID;
90 static tikview views[4] ATTRIBUTE_ALIGN(32);
91 u32 numviews;
92 s32 res;
94 u32 ints;
96 dprintf("Stopping thread timeslice ticker\n");
97 __lwp_thread_stoptimeslice();
99 dprintf("Shutting down IOS subsystems\n");
100 res = __IOS_ShutdownSubsystems();
101 if(res < 0) {
102 dprintf("Shutdown failed: %d\n",res);
104 dprintf("Initializing ES\n");
105 res = __ES_Init();
106 if(res < 0) {
107 dprintf("ES init failed: %d\n",res);
108 return res;
111 dprintf("Launching TitleID: %016llx\n",titleID);
113 res = ES_GetNumTicketViews(titleID, &numviews);
114 if(res < 0) {
115 dprintf(" GetNumTicketViews failed: %d\n",res);
116 return res;
118 if(numviews > 4) {
119 dprintf(" GetNumTicketViews too many views: %u\n",numviews);
120 return IOS_ETOOMANYVIEWS;
122 res = ES_GetTicketViews(titleID, views, numviews);
123 if(res < 0) {
124 dprintf(" GetTicketViews failed: %d\n",res);
125 return res;
127 dprintf("Ready to launch channel\n");
128 res = ES_LaunchTitleBackground(titleID, &views[0]);
129 if(res<0) {
130 dprintf("Launch failed: %d\n",res);
131 return res;
133 dprintf("Channel launching in the background\n");
134 dprintf("Pre-stub status:\n");
135 dumpregs();
136 dprintf("ISR Disable...\n");
137 _CPU_ISR_Disable( ints );
138 dprintf("Saving regs...\n");
139 __distub_saveregs();
140 dprintf("Taking the plunge...\n");
141 __distub_take_plunge(&di_ctx);
143 dprintf("We're back!\n");
144 dprintf("Restoring regs...\n");
145 __distub_restregs();
146 dprintf("ISR Enable...\n");
147 _CPU_ISR_Restore( ints );
149 dprintf("Post-stub status:\n");
150 dumpregs();
152 __IPC_Reinitialize();
153 __ES_Reset();
155 dprintf("IPC reinitialized\n");
156 sleep(1);
157 dprintf("Restarting IOS subsystems\n");
159 res = __IOS_InitializeSubsystems();
161 dprintf("Subsystems running!\n");
163 res = ES_GetNumTicketViews(titleID, &numviews);
164 if(res < 0) {
165 dprintf(" GetNumTicketViews failed: %d\n",res);
166 return res;
168 dprintf(" GetNumTicketViews: %d",numviews);
170 dprintf("Restarting threads timeslice ticker\n");
171 __lwp_thread_starttimeslice();
173 return 0;
177 s32 __DI_LoadStub(void)
179 int ret = 0;
180 int res;
181 #ifdef DEBUG_IOS
182 dprintf("Reloading to IOS%d\n",version);
183 #endif
184 res = __IOS_ShutdownSubsystems();
185 if(res < 0) ret = res;
186 res = __ES_Init();
187 if(res < 0) ret = res;
188 else {
189 res = __DI_StubLaunch();
190 if(res < 0) {
191 ret = res;
192 __ES_Close();
195 res = __IOS_InitializeSubsystems();
196 if(res < 0) ret = res;
197 return ret;