13 #define _LANGUAGE_ASSEMBLY
14 #include <ogc/machine/asm.h>
15 #undef _LANGUAGE_ASSEMBLY
19 #include <ogc/lwp_threads.h>
20 #include <ogc/machine/processor.h>
22 context_storage di_ctx
;
24 #define IOS_HEAP_SIZE 0x1000
26 void __IPC_Reinitialize(void);
27 extern void __exi_init();
29 static vu16
* const _viReg
= (u16
*)0xCC002000;
30 static vu32
* const _piReg
= (u32
*)0xCC003000;
31 static vu16
* const _memReg
= (u16
*)0xCC004000;
32 static vu16
* const _dspReg
= (u16
*)0xCC005000;
34 //#define DEBUG_DVD_STUB
37 #define dprintf printf
44 dprintf(" MSR: %08x\n",mfmsr());
45 dprintf(" SPRGx: %08x %08x %08x %08x\n", mfspr(SPRG0
), mfspr(SPRG1
), mfspr(SPRG2
), mfspr(SPRG3
));
46 dprintf(" HID0124: %08x %08x %08x %08x\n", mfspr(HID0
), mfspr(HID1
), mfspr(HID2
), mfspr(HID4
));
47 dprintf(" L2CR: %08x\n", mfspr(L2CR
));
48 dprintf(" WPAR: %08x\n", mfspr(WPAR
));
49 dprintf(" PMCx: %08x %08x %08x %08x\n", mfspr(PMC1
), mfspr(PMC2
), mfspr(PMC3
), mfspr(PMC4
));
50 dprintf(" MMCRx: %08x %08x\n", mfspr(MMCR0
), mfspr(MMCR1
));
52 dprintf(" PI Regs: %08x %08x %08x %08x\n", _piReg[0], _piReg[1], _piReg[2], _piReg[3]);
53 dprintf(" %08x %08x %08x %08x\n", _piReg[4], _piReg[5], _piReg[6], _piReg[7]);
54 dprintf(" %08x %08x %08x %08x\n", _piReg[8], _piReg[9], _piReg[10], _piReg[11]);
57 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]);
58 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]);
59 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]);
60 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]);
61 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]);
62 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]);
66 register_storage di_regs
;
68 void __distub_saveregs(void)
71 di_regs
.timebase
= gettime();
73 di_regs
.piReg
[i
] = _piReg
[i
];
76 void __distub_restregs(void)
80 _piReg
[i
] = di_regs
.piReg
[i
];
81 //i = _piReg[0]; //clear all interrupts
82 settime(di_regs
.timebase
);
85 static u32
__di_find_stub(u64
*title_id
) {
87 u32 count
, i
, tmd_view_size
;
89 u16 rev_highest
, rev_this
;
90 STACK_ALIGN(u8
, tmdbuf
, 1024, 32);
95 ios_hid
= iosCreateHeap(IOS_HEAP_SIZE
);
97 dprintf("iosCreateHeap() failed: %d\n", ios_hid
);
101 dprintf("Initializing ES\n");
104 res
= ES_GetNumTitles(&count
);
106 iosDestroyHeap(ios_hid
);
107 dprintf("ES_GetNumTitles() failed: %d\n", res
);
111 dprintf("%u titles are installed\n", count
);
113 titles
= iosAlloc(ios_hid
, sizeof(u64
) * count
);
115 iosDestroyHeap(ios_hid
);
116 dprintf("iosAlloc() failed\n");
120 res
= ES_GetTitles(titles
, count
);
122 iosFree(ios_hid
, titles
);
123 iosDestroyHeap(ios_hid
);
124 dprintf("ES_GetTitles() failed: %d\n", res
);
128 for (i
= 0; i
< count
; i
++) {
129 if ((titles
[i
] >> 32) != 0x00010008)
132 dprintf("found hidden title 0x%llx\n", titles
[i
]);
134 res
= ES_GetTMDViewSize(titles
[i
], &tmd_view_size
);
136 dprintf("ES_GetTMDViewSize() failed: %d\n", res
);
140 if (tmd_view_size
< 90) {
141 dprintf("TMD too small: %d\n", tmd_view_size
);
145 if (tmd_view_size
> 1024) {
146 dprintf("TMD too big: %d\n", tmd_view_size
);
150 res
= ES_GetTMDView(titles
[i
], tmdbuf
, tmd_view_size
);
152 dprintf("ES_GetTMDView() failed: %d\n", res
);
157 if ((tmdbuf
[0x18] == 'D') && (tmdbuf
[0x19] == 'V')) {
158 rev_this
= (tmdbuf
[88] << 8) | tmdbuf
[89];
159 dprintf("found stub with revision 0x%x\n", rev_this
);
161 if (rev_this
> rev_highest
) {
162 *title_id
= titles
[i
];
163 rev_highest
= rev_this
;
168 iosFree(ios_hid
, titles
);
169 iosDestroyHeap(ios_hid
);
172 dprintf("we have a winner: 0x%llx\n", *title_id
);
179 s32
__DI_StubLaunch(void)
182 static tikview views
[4] ATTRIBUTE_ALIGN(32);
187 res
=__di_find_stub(&titleID
);
189 dprintf("stub not installed\n");
193 dprintf("Stopping thread timeslice ticker\n");
194 __lwp_thread_stoptimeslice();
196 dprintf("Shutting down IOS subsystems\n");
197 res
= __IOS_ShutdownSubsystems();
199 dprintf("Shutdown failed: %d\n",res
);
201 dprintf("Initializing ES\n");
204 dprintf("Launching TitleID: %016llx\n",titleID
);
206 res
= ES_GetNumTicketViews(titleID
, &numviews
);
208 dprintf(" GetNumTicketViews failed: %d\n",res
);
209 __IOS_InitializeSubsystems();
210 __lwp_thread_starttimeslice();
214 dprintf(" GetNumTicketViews too many views: %u\n",numviews
);
215 __IOS_InitializeSubsystems();
216 __lwp_thread_starttimeslice();
217 return IOS_ETOOMANYVIEWS
;
219 res
= ES_GetTicketViews(titleID
, views
, numviews
);
221 dprintf(" GetTicketViews failed: %d\n",res
);
222 __IOS_InitializeSubsystems();
223 __lwp_thread_starttimeslice();
226 dprintf("Ready to launch channel\n");
227 res
= ES_LaunchTitleBackground(titleID
, &views
[0]);
229 dprintf("Launch failed: %d\n",res
);
230 __IOS_InitializeSubsystems();
231 __lwp_thread_starttimeslice();
235 dprintf("Channel launching in the background\n");
236 dprintf("Pre-stub status:\n");
238 dprintf("ISR Disable...\n");
239 _CPU_ISR_Disable( ints
);
240 dprintf("Saving regs...\n");
242 dprintf("Taking the plunge...\n");
243 __distub_take_plunge(&di_ctx
);
245 dprintf("We're back!\n");
246 dprintf("Restoring regs...\n");
248 dprintf("ISR Enable...\n");
249 _CPU_ISR_Restore( ints
);
251 dprintf("Post-stub status:\n");
254 __IPC_Reinitialize();
257 dprintf("IPC reinitialized\n");
259 dprintf("Restarting IOS subsystems\n");
261 res
= __IOS_InitializeSubsystems();
263 dprintf("Subsystems running!\n");
265 res
= ES_GetNumTicketViews(titleID
, &numviews
);
267 dprintf(" GetNumTicketViews failed: %d\n",res
);
269 dprintf(" GetNumTicketViews: %d",numviews
);
271 dprintf("Restarting threads timeslice ticker\n");
272 __lwp_thread_starttimeslice();
278 s32
__DI_LoadStub(void)
283 res
= __IOS_ShutdownSubsystems();
284 if(res
< 0) ret
= res
;
286 if(res
< 0) ret
= res
;
288 res
= __DI_StubLaunch();
294 res
= __IOS_InitializeSubsystems();
295 if(res
< 0) ret
= res
;