/
QLite Main.asm.c
633 lines (536 loc) · 19 KB
/
QLite Main.asm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
/*
©Æ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Æ©
©« QLite Main.asm.c »©
©« »©
©« Copyright 2005 Anonymous Ghostwriter »©
©« »©
©¥~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~¥©
©« [Description]....: QLite Gamecube DVD Drivecode 0.93 [RC2b] »©
©« »©
©« »©
©¥~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~¥©
©« [Notes......]....: »©
©« »©
©« »©
©¥~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~¥©
©« [History....]....: [bc]-13.10.2005 file created »©
©« »©
©Æ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Æ©
*/
#----------------------------------------------------
# qCode custom commands
#----------------------------------------------------
.equ CMD_DISABLEAUDIOFIX, 0x0021
.equ CMD_DISABLEDREFIX, 0x0022
.equ CMD_TOGGLE_ECMA, 0x0023
.equ CMD_SETREADSTATUS, 0x0024
.equ CMD_UNLOAD, 0x0025
.equ CMD_DEBUGTEST, 0x0026
.equ CMD_REQUESTCREDITS, 0x0027
.equ CMD_ENABLESLEEP, 0x0029
.equ CMD_DENYWRITE, 0x002A
.equ CMD_DBGREAD, 0x01FE
.equ CMD_COBRA_MULTIDISCOFFSET, 0x0028
.include "source/QLite.h"
.include "source/QliteGlobal.h"
.global _start
.section .text
#==============================================================================================
# Main Entrypoint:
#==============================================================================================
_start: jmp Qcode_Init # sets up everything
v_irqh: jmp main_irq_handler # fixed address for the irqhandler
#---------------------------------------------------------------------------
# data section 1
#---------------------------------------------------------------------------
wCBDBackup: .word 0x00 # used to detect new CDBs
dwUsrSectorOffset: IMM24Bit 0x0C
bConfig: .byte 0x00 # 0: Disable ECMA Patch | 1: Disable AudioFix
# 2: disable dreFix | 3: regionpatch done | 4: initial multiboot lba patch done
# 5: enable sleepmode
Qcode_Init:
and 0xf7ff, psw
1: #------------------------------------------------------------------#
#- scan ourself and patch all fw addresses to the correct version -#
#------------------------------------------------------------------#
mov PatchStart, a0
mov qMainCodeSize, d0
jsr SUB_PatchFwAddresses
PatchStart:
jsr SUB_ClearDataSegs
#---------------------------------------------#
#- replace the current irq handler with ours -#
#---------------------------------------------#
mov v_irqh, a0
mov a0, (FwIrqVec)
mov 0x01, d0 # mediaopt byte
mov d0, (FwMediaOpt)
.if DBGMODE == 1
jsr DBGInit
.endif
#--------------------------------------------------------------------------------
# init breakpoints
#--------------------------------------------------------------------------------
sub d0, d0
#=============================================
.if READ_ECMADVDS == 1
mov FwBP0Addr_ReadECMA, a0
mov a0, (REG_ADB0)
or ADB0_ON, d0
.endif
#=============================================
.if AUTHPLUSR == 1
mov FwBP1Addr_AuthPlusR, a0
mov a0, (REG_ADB1)
or ADB1_ON, d0
#=============================================
.elseif AUTHDMI == 1
mov FwBP1Addr_AuthDMI, a0
mov a0, (REG_ADB1)
or ADB1_ON, d0
.endif
#=============================================
movb d0, (REG_ADBCTRL)
or 0x800, psw
bra SUB_DVDShortReset # reset the drive silently
#END
#==============================================================================================
# Interrupt handler
#==============================================================================================
main_irq_handler:
.if DBGMODE == 1
jsr DBGUpdate
.endif
#=====================================#
# check for Breakpoint 0 or 1 #
#=====================================#
tbnzAbs ADBCTRL, 0, BP0Handler
tbnzAbs ADBCTRL, 1, BP1Handler
.if STANDBYMODE == 1
tbnzAbs bConfig, 5, noSleep1
jsr SUB_CheckSleepStart
noSleep1:
.endif
mov (FwTimer2), d0 # avoid spindown when drive idles for too long
mov d0, (FwIdleTimeout)
mov 0x0c, d0 # patch error-byte
mov d0, (FwErrByte)
mov FwDriveState, a0 # clean protection flag
mov 0x02, d0
bset d0, (a0)
mov 0x01, d0
movb d0, (FwDbgstate)
#----------------------------------------#
# force read error retries #
#----------------------------------------#
.if DREFIX == 1
DREFix:
sub d0, d0 # to disable drefix write EA0B here (jmp noDREFIX)
mov 0x40ec00, a0
mov d0, (0x00, a0)
movb d0, (0x09, a0)
noDREFix:
.endif
mov FwCDB0, a0 # backup CDB byte and replace with fake value
mov (a0), d0 # to detect new commands
mov d0, (wCBDBackup)
mov 0x2BAD, d0
mov d0, (a0)
#-----------------------------#
# call the original handler #
#-----------------------------#
jsrabsOrg FwIntHandler
#==================================================================#
# Check for new command #
#==================================================================#
mov FwCDB0, a0
mov (a0), d0
cmp 0x2BAD, d0
bne SUB_OnNewCDBRecieved
mov (wCBDBackup), d0 # restore original value
mov d0, (a0)
rts
/*=========================================================================
* 'generic' Breakpoint handler
* --------------
* one handler used for both adb registers. determines breakpoint
* address handles stack issues and calls the appropriate patch code
*=========================================================================*/
#===================================
BP0Handler:
#===================================
bitclr REG_ADBCTRL 0
bitclr REG_UNICR, 0
mov BP0_ReadECMA, a1
bra PatchStack
#===================================
BP1Handler:
#===================================
#- ack adb and unknown opcode interrupts -#
bitclr REG_ADBCTRL 1
bitclr REG_UNICR, 0
#----------------------------------------------------------------------
# get the address of the according code replacement into a1
#----------------------------------------------------------------------
mov (REG_ADB1), a1
cmp FwBP1Addr_AuthPlusR, a1
bne 1f
mov BP1_AuthPlusR, a1
bra PatchStack
1: cmp FwBP1Addr_AuthDMI, a1
bne 1f
mov BP1_AuthDMI, a1
bra PatchStack
#- Panic! we dont recognise the break address. just rts then
rts
PatchStack:
mov a3, a0 # point a0 to the prev stack pointer
add 4, a0
movbu (FwIrqDepth), d0 # handle special case when FwIrqDepth == 0
cmp 1, d0
bne 1f
mov (4, a3), a0
1: mov a1, (0x20, a0) # patch ret address to our code replacement
#=================================
# check for original disk
#=================================
mov (0x34, a0), a1
cmp FwOrgDiskCheckAddr, a1
bne noOrgDisc
bitset bConfig, 0 # set orgdisc flag
mov 0x06, a0 # adjust user sector offset
mov a0, (dwUsrSectorOffset)
noOrgDisc:
rts
.if STANDBYMODE == 1
#=================================================================================
# check start sleep
#=================================================================================
SUB_CheckSleepStart:
movbu (bSleeping), d0 # already sleeping ?
bne noSleep
movbu (0x8069), d0 # standby time elapsed ?
movbu (bSleepCounter), d1
sub d1, d0
cmp 12, d0
bne noSleep
jsr SUB_StopMotor # StopMotor
sub d0, d0 # stop laser
jsr SUB_SetLaser
mov 0x01, d0
movb d0, (bSleeping)
noSleep:
rts
#======================================================================================
# check end sleep
#======================================================================================
SUB_CheckSleepEnd:
movbu (bSleeping), d0 # currrently sleeping ?
cmp 0, d0
beq noWakeUp
and 0xf7ff, psw
mov 0, d0 # clear sleep flag
movb d0, (bSleeping)
mov 0x01, d0 # start laser
jsr SUB_SetLaser
jsr SUB_StartMotor # start motor
or 0x800, psw
noWakeUp:
rts
.endif
/***********************************************************************/
SUB_OnCmdReads:
/***********************************************************************/
mov a2, d1
mov (dwLBAOffset), a2
mov (0x04, a0), a1 # get cb2[0-3]
jsr SUB_Swap24
add a2, a1
jsr SUB_Swap24
mov a1, (0x04,a0) # add customreadoffs to cdb2 of command
mov d1, a2
.if STANDBYMODE == 1
tbnzAbs bConfig, 5, noSleep2
jsr SUB_CheckSleepEnd
movbu (0x8069), d0
movb d0, (bSleepCounter)
noSleep2:
.endif
rts
/***********************************************************************/
CobraSetLBAOffset:
/***********************************************************************/
bitset bConfig, 3 # set regionpatch-done flag
mov (0x04,a0),a1 # get cb2[0-3] (read lba)
jsr SUB_Swap24 # store it swapped
mov a1, (dwLBAOffset)
rts
/***********************************************************************/
SUB_Swap24:
/***********************************************************************/
mov a1, (0xffbc)
mov (0xffbc), a1
rts
/***********************************************************************/
SUB_OnNewCDBRecieved:
/***********************************************************************/
#==================================================================#
# Cobra Multiboot support #
#==================================================================#
cmp CMD_COBRA_MULTIDISCOFFSET, d0
beq CobraSetLBAOffset
cmp 0x00a8, d0 # patch lba offsets in all read cmds for multiboot
beq SUB_OnCmdReads
cmp 0x00ab, d0
beq SUB_OnCmdReads
cmp 0x00e1, d0
beq SUB_OnCmdReads
#==================================================================#
# handle dbgwrite commands #
#==================================================================#
.if AUTOBYPASS == 1
cmp CMD_DBGREAD, d0 # DBG command ?
bne noDbgCommand
movbu (2, a0), d1 # a0 = FwCDB0
cmp 0x01, d1 # write mem dbg command ?
bne 1f
jsr SUB_UnloadQcode # else unload ourself
1: rts
noDbgCommand:
.endif
#==================================================================#
# Check for our custom commands #
#==================================================================#
#================================================================
cmp CMD_DISABLEAUDIOFIX, d0 # set noaudiofix flag
bne 1f
bitset bConfig, 1
bra patchNiceCMD
#================================================================
.if STANDBYMODE == 1
1: cmp CMD_ENABLESLEEP, d0 # enable sleep mode
bne 1f
movbu (0x8069), d0
movb d0, (bSleepCounter)
bitset bConfig, 5
bra patchNiceCMD
.endif
#================================================================
1: cmp CMD_UNLOAD, d0 # unload xenogc
bne 1f
jsr patchNiceCMD
jsr SUB_UnloadQcode # fade away
rts
#================================================================
.if CREDITS == 1
1: cmp CMD_REQUESTCREDITS, d0 # load credits
bne 1f
jsr patchNiceCMD
# sub d0, d0
# mov d0, (0x40D800-4)
jsr SUB_UnloadQcode # fade away
mov 0x4444, d0 # set credits request tag
mov d0, (0x40D100)
P81_HI # fake reset strobe
nop # to engage the chip
P81_LO
rts
.endif
patchNiceCMD:
mov 0xE0, d0 # bad boi!
movb d0, (FwCDB0)
1: rts
/***********************************************************************/
SUB_UnloadQcode:
/***********************************************************************/
and 0xf7ff, psw
sub d0, d0
mov d0, (REG_ADBCTRL) # clear breakpoints
mov 0x80a74, a0 # restore intvector
mov a0, (0x804c)
rts
.if STANDBYMODE == 1
#=======================================================================
SUB_SetLaser: # d0: on/off
#======================================================================
movb d0, (0x80b6)
mov 0x08FE, d0
mov d0, (0x80b4)
jsrabsOrg 0x838ec # SEND_CUSTOM_HLE_CMD
rts
SUB_StartMotor:
mov 0x7f0b,d0 # send spin down commands
jsr SUB_SendCmd_Motor
mov 0x7f0a,d0
jsr SUB_SendCmd_Motor
mov 0x7f0d,d0
jsr SUB_SendCmd_Motor
rts
SUB_StopMotor:
mov 0x7f0d,d0 # send spin down commands
jsr SUB_SendCmd_Motor
mov 0x7f0e,d0
jsr SUB_SendCmd_Motor
mov 0x7f0b,d0
jsr SUB_SendCmd_Motor
rts # command END
SUB_SendCmd_Motor:
add -0x08,a3 # push a1
mov a1,(4, a3)
mov d0,(0x02,a3) # push (cmd param) (D0)
mov 0x1000,d1 # push 0x1000 (D1)
mov d1,(a3)
mov a3,a0 # point a0 to params on stack
mov 0x02,d0 # set cmd 2 in d0
mov 0x8b126, a1 # call fw sendcmd routine
jsr (a1)
add 0x08,a3 # pop param stack
mov (-4, a3),a1 # pop a1
rts
#END
.endif
/***********************************************************************/
SUB_PatchFwAddresses: # (a0: code d0: length)
/***********************************************************************/
mov d0, a1
add a0, a1
sub d1, d1 # check drive version
movbu (0x80812), d0 # bc4: 6, 12, 10 e8e4 0, 10, 28
cmp 0x9c, d0
beq drive6
cmp 0x57, d0
beq drive8
bra AddressLoop
drive6:
mov 3, d1
bra AddressLoop
drive8:
mov 6, d1
AddressLoop:
movbu (a0), d0 # search for address import tag (0x3333)
cmp 0x33, d0
bne noOpcode
movbu (1, a0), d0
cmp 0x33, d0
bne noOpcode
movbu (2, a0), d0 # get import 'ordinal'
sub 1, d0
mov 9, d2 # point to IAT[ordinal*9]
mulu d2, d0
add d1, d0 # add version offset
mov FwImportAddressTable, a2
add d0, a2
movbu (a2), d0 # get import address
movb d0, (a0) # patch into opcode
movbu (1, a2), d0
movb d0, (1, a0)
movbu (2, a2), d0
movb d0, (2, a0)
noOpcode:
add 1, a0
cmp a1, a0
bls AddressLoop
rts
#END
/***********************************************************************/
SUB_ClearDataSegs:
/***********************************************************************/
mov FwData1, a0 # copy initialized data section
mov FwData1_Source, d1
mov FwData1_Size, d0
jsrabs FwMemcpy
mov FwBSS, a0 # clear the bss section
sub d0, d0
mov FwBSS_Size, d1
jsrabs FwMemset
rts
#END
/***********************************************************************/
SUB_Memcpy: # a0: dst, a1: src, d0: length
/***********************************************************************/
and 0xFFFE, d0 # assert even number of bytes
MemcpyLoop:
mov (a1), d1
mov d1, (a0)
add 2, a0
add 2, a1
sub 2, d0
cmp 0, d0
bne MemcpyLoop
rts
#*********************************************************************
SUB_DVDShortReset:
#*********************************************************************
mov 0x21, d0
movb d0, (0x400012)
mov 0x02, d0
movb d0, (0x400013)
jmpabs FwReset # 2 fwreset
#END
#===================================================================================#
# Breakpoints 0 and 1 #
#===================================================================================#
.include "source/QLiteBreakpoints.asm.c"
#===================================================================================#
# Various debug functions #
#===================================================================================#
.if DBGMODE == 1
.include "source/QLiteDbg.asm.c"
.endif
.align 2
#---------------------------------------------------------------------------
# data section 2
#---------------------------------------------------------------------------
dwLBAOffset: .long 0x00
.if STANDBYMODE == 1
bSleepCounter: .byte 0x00
bSleeping: .byte 0x00
.endif
#======================================================================================
FwImportAddressTable: /* [04] [06] [08] */
#======================================================================================
FwAddrImp16 FwDbgstate 0x8192, 0x8186, 0x818e
FwAddrImp16 FwBSS_Size 0x01b8, 0x01ac, 0x01b6
FwAddrImp16 FwTimer2 0x8084, 0x8078, 0x8080
FwAddrImp16 FwIdleTimeout 0x819c, 0x8190, 0x8198
FwAddrImp16 FwErrByte 0x819a, 0x818e, 0x8196
FwAddrImp16 FwCmdState1 0x81b0, 0x81a4, 0x81ac
FwAddrImp16 FwCmdState2 0x8194, 0x8188, 0x8190
FwAddrImp16 FwReadLBA 0x8102, 0x80f6, 0x80fe
FwAddrImp16 FwCDB0 0x80b4, 0x80a8, 0x80b0
FwAddrImp16 FwMediaOpt 0x8090, 0x8084, 0x808c
FwAddrImp16 FwFastBootMask 0xfffb, 0xfff7, 0xfffb
#FwAddrImp16 FwReadCfg 0x808e, 0x8082, 0x808a
FwAddrImp24 FwMemcpy 0x082f27, 0x082f2e, 0x082f27
FwAddrImp24 FwMemset 0x082f49, 0x082f50, 0x082f49
FwAddrImp24 FwOrgDiskCheckAddr 0x0885c1, 0x0885b9, 0x0885c1
FwAddrImp24 FwReset 0x08d47e, 0x08d41e, 0x08d4d9
FwAddrImp24 FwData1_Source 0x08ea2a, 0x08e9c8, 0x08ea7c
FwAddrImp24 FwDriveState 0x40ecf9, 0x40ed02, 0x40ecf5
FwAddrImp24 FwSetReadOptions 0x083977, 0x08396F, 0x083977
#FwAddrImp24 FwSendCtrlCommand 0x08ae09, 0x08b126, 0x08ae56
#FwAddrImp24 FwDiv512 0x082EDB, 0x082EE2, 0x082EDB
#FwAddrImp24 FwInitSpinup 0x083bad, 0x083ba5, 0x083bad
#FwAddrImp24 FwStartDrive 0x0878f8, 0x0878f0, 0x0878f8
#FwAddrImp24 FwSetInitState 0x08aee8, 0x08b205, 0x08af35
FwAddrImp24 FwBP0Addr_ReadECMA 0x08ae28, 0x08b145, 0x08ae75
FwAddrImp24 FwBP0Addr_ReadECMA_Exit 0x08ae33, 0x08b150, 0x08ae80
FwAddrImp24 FwBP1Addr_AuthPlusR 0x089d70, 0x089df2, 0x089db6
FwAddrImp24 FwBP1Addr_AuthPlusR_Exit 0x089d70, 0x089df2, 0x089db6
FwAddrImp24 FwBP1Addr_AuthDMI 0x08a7db, 0x08aaf0, 0x08a828
.align 2
#===============================================
Apploader_patch:
#===============================================
.incbin "bin/iplboot.bin"
Apploader_patch_end:
.align 2
#===============================================
Custom_Apploader:
#===============================================
# .incbin "bin/QLiteApploader.bin"
Custom_Apploader_end:
# !!!
.equ Applpatch_size, (Apploader_patch_end - Apploader_patch)
.equ Custom_Apploader_size, (Custom_Apploader_end - Custom_Apploader)
.equ qMainCodeSize, Apploader_patch-PatchStart