; FILE: Source:modules/romfixes.ASM          REV: 6 --- misc rom bugfixes
; History
;  0      Started 10th Dec 1999: got idea from Fixes dir in MMULib.lha
;  1      12th Dec: console device fix accidently bsr'ed two bytes off
;         -> strange console update problems & cyberguard hits, fixed
;  2      Added shell Resident patch. froze 1.19.
;  3      Fixed ramlib stack patch for V37 rom
;  4      Added ramlib msgport sigbit patch
;  5      3rd Jan 2000: Disabled resident patch, it's bugged!
;  6      7th Jan 2000: Bugfixed resident patch. No longer freezes if
;         user says `resident c:program' without `ADD' switch.
;         froze 1.20.
;

;
; romfixes patch for BlizKick ("patch" Module)
; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;
; This patch fixes some bugs in current rom versions (including V40!)
;
; - console.device window resize bug (ever wondered why KingCON shell
;   having review buffer and menus enabled crashes on window resize?-)
;   (idea from Thor's util/boot/ConsoleFix.lha)
; - increase ram disk stack size (idea from Thor's util/boot/PatchRAM.lha)
; - increase ramlib stack size from 2k to 8k
; - make ramlib msgport use sigbit SIGBREAKB_CTRL_E instead of SIGB_SINGLE
;   (idea from Eddy Carroll's SnoopDos source code)
; - shell Resident command bug, see CyberGuard.notes / 1.11beta3
;
; Written by Harry "Piru" Sintonen.
; This source code is Public Domain.


	include	"blizkickmodule.i"	; Some required...

	SECTION	PATCH,CODE
_DUMMY_LABEL
	BK_PTC

; Code is run with following incoming parameters:
;
; a0=ptr to ROM start (buffer)	eg. $1DE087B8
; a1=ptr to ROM start (ROM)	eg. $00F80000 (do *not* access!)
; d0=ROM lenght in bytes	eg. $00080000
; a2=ptr to _FindResident routine (will search ROM buffer for resident tag):
;    CALL: jsr (a2)
;      IN: a0=ptr to ROM, d0=rom len, a1=ptr to resident name
;     OUT: d0=ptr to resident (buf) or NULL
; a3=ptr to _InstallModule routine (can be used to plant a "module"):
;    CALL: jsr (a3)
;      IN: a0=ptr to ROM, d0=rom len, a1=ptr to module, d6=dosbase
;     OUT: d0=success
; a4=ptr to _Printf routine (will dump some silly things (errormsg?) to stdout ;-)
;    CALL: jsr (a4)
;      IN: a0=FmtString, a1=Array (may be 0), d6=dosbase
;     OUT: -
; d6=dosbase, a6=execbase
;
; Code should return:
;
; d0=true if succeeded, false if failed.
; d1-d7/a0-a6 can be trashed. a7 *must* be preserved! ;-)

	moveq	#0,d7

	cmp.w	#37,($C,a0)		; requires V37+ rom image
	blo	.exit

	lea	(regs,pc),a5
	movem.l	d0/a0-a4,(a5)

	lea	(.conname,pc),a1
	movem.l	(regs,pc),d0/a0
	move.l	(findresident,pc),a2
	jsr	(a2)
	lea	(.nocon,pc),a1
	tst.l	d0
	beq	.err

	move.l	d0,a2
	IFGT	0
	move.l	(RT_INIT,a2),a2
	;test if inside rom bounds?
	move.l	(rom_log,pc),d0
	cmp.l	d0,a2
	blo.b	.nofix
	add.l	(rom_size,pc),d0
	cmp.l	d0,a2
	bhs.b	.nofix
	; inside rom, so generate ram buffer address
	sub.l	(rom_log,pc),a2		; -$f80000
	add.l	(rom_phys,pc),a2	; +buffer
.nofix
	ENDC

	lea	(.badcon,pc),a1
	lea	(8192,a2),a2
	move.w	#16384/2,d0
.find	addq.l	#2,a2
	subq.w	#1,d0
	beq	.err

	cmp.l	#$2C5F45EE,(a2)
	bne.b	.find
	cmp.l	#$0082614C,(4,a2)
	bne.b	.find
	cmp.l	#$24524A92,(8,a2)
	bne.b	.find
	cmp.l	#$66F841EE,(12,a2)
	bne.b	.find
	cmp.l	#$126E2F0E,(16,a2)
	bne.b	.find

	; patch console.device bug!
	move.l	#$2C5745EE,(a2)+
	move.l	#$00826002,(a2)+
	move.l	#$614A2452,(a2)+
	move.l	#$4A9266F8,(a2)+
	move.l	#$41EE126E,(a2)+

	lea	(.dosname,pc),a1
	movem.l	(regs,pc),d0/a0
	move.l	(findresident,pc),a2
	jsr	(a2)
	lea	(.nodos,pc),a1
	tst.l	d0
	beq	.err

	move.l	d0,a2
	lea	(.badram,pc),a1
	lea	(8192,a2),a2
	move.w	#16384/2,d0
.find2	addq.l	#2,a2
	subq.w	#1,d0
	beq	.err

	cmp.l	#$4878FFFF,(a2)		; pea	(-1).w
	bne.b	.find2
	cmp.l	#$4878000A,(4,a2)	; pea	(10).w
	bne.b	.find2
	cmp.l	#$487804B0,(8,a2)	; pea	(1200).w
	bne.b	.find2
	cmp.l	#$7000222B,(12,a2)	; moveq #0,d0 / move.l (x,a3),d1
	bne.b	.find2

	; patch ram disk stack size!
	move.w	#2048,(10,a2)


	lea	(.ramlibname,pc),a1
	movem.l	(regs,pc),d0/a0
	move.l	(findresident,pc),a2
	jsr	(a2)
	lea	(.noramlib,pc),a1
	tst.l	d0
	beq	.err

	move.l	d0,a3
	move.l	d0,a2

	lea	(.badramlib,pc),a1
	move.w	#2048/2,d0
.find3	addq.l	#2,a2
	subq.w	#1,d0
	beq.b	.try_37

	cmp.l	#$2608E48B,(a2)
	bne.b	.find3
	cmp.l	#$7808E19C,(4,a2)
	bne.b	.find3
	cmp.l	#$4EAEFF76,(8,a2)
	bne.b	.find3

	; patch ramlib stack size! (v39+)
	move.w	#$7820,(4,a2)
	bra.b	.was_39plus


.try_37	move.l	a3,a2
	move.w	#2048/2,d0
.find3_37	addq.l	#2,a2
	subq.w	#1,d0
	beq	.err

	cmp.l	#$283C0000,(a2)
	bne.b	.find3_37
	cmp.l	#$08004EAE,(4,a2)
	bne.b	.find3_37
	cmp.l	#$FF762C5F,(8,a2)
	bne.b	.find3_37

	; patch ramlib stack size! (v37)
	move.w	#$2000,(4,a2)
.was_39plus

	lea	(.badramlib_b,pc),a1
	move.w	#4096/2,d0
.find3b	addq.l	#2,a3
	subq.w	#1,d0
	beq	.err

	cmp.l	#$246E0114,(a3)
	bne.b	.find3b
	cmp.w	#$2B4A,(4,a3)
	bne.b	.find3b
	cmp.l	#$3B7C0004,(8,a3)
	bne.b	.find3b
	cmp.l	#$600841ED,(14,a3)
	bne.b	.find3b

	; patch ramlib msgport sigbit
	move.w	#14,(10,a3)		; SIGBREAKB_CTRL_E


	lea	(.shellname,pc),a1
	movem.l	(regs,pc),d0/a0
	move.l	(findresident,pc),a2
	jsr	(a2)
	lea	(.noshell,pc),a1
	tst.l	d0
	beq	.err

	move.l	d0,a2
	lea	(.badresident,pc),a1
	lea	(8192,a2),a2
	move.w	#12288/2,d0
.find4	addq.l	#2,a2
	subq.w	#1,d0
	beq	.err

	cmp.l	#$67164AAD,(a2)
	bne.b	.find4
	cmp.l	#$FFD06624,(4,a2)
	bne.b	.find4
	cmp.l	#$202B0004,(8,a2)
	bne.b	.find4
	cmp.l	#$4A806A08,(12,a2)
	bne.b	.find4
	cmp.l	#$0C80FFFF,(16,a2)
	bne.b	.find4
	cmp.l	#$FC196E14,(20,a2)
	bne.b	.find4
	cmp.l	#$4AADFFD4,(24,a2)
	bne.b	.find4
	cmp.l	#$660000B6,(28,a2)
	bne.b	.find4
	cmp.l	#$0CAB0000,(32,a2)
	bne.b	.find4
	cmp.l	#$00010004,(36,a2)
	bne.b	.find4

	; patch shell resident command
	move.l	#$671C4AAD,(a2)+
	move.l	#$FFD06624,(a2)+
	move.l	#$200B6720,(a2)+
	move.l	#$202B0004,(a2)+
	move.l	#$6A0C0C80,(a2)+
	move.l	#$FFFFFC19,(a2)+
	move.l	#$6E126002,(a2)+
	move.l	#$C0DE4AAD,(a2)+
	move.l	#$FFD46600,(a2)+
	move.l	#$00B05380,(a2)


	moveq	#1,d7

.err	tst.l	d7
	bne.b	.exit
	lea	(.errh,pc),a0
	move.l	a1,-(sp)
	move.l	sp,a1
	move.l	(printf,pc),a2
	jsr	(a2)
	addq.l	#4,sp
.exit	move.l	d7,d0
	rts


.errh	dc.b	'romfixes: couldn''t %s!',10,0

.nocon	dc.b	'find '
.conname	dc.b	'console.device',0
.badcon	dc.b	'patch console.device bug',0

.nodos	dc.b	'find '
.dosname	dc.b	'dos.library',0
.badram	dc.b	'patch ram disk stack size',0

.noramlib	dc.b	'find '
.ramlibname	dc.b	'ramlib',0
.badramlib	dc.b	'patch ramlib stack size',0
.badramlib_b	dc.b	'patch ramlib msgport sigbit',0

.noshell	dc.b	'find '
.shellname	dc.b	'shell',0
.badresident	dc.b	'patch Resident bug',0


	CNOP	0,2
regs
rom_size	ds.l	1
rom_phys	ds.l	1
rom_log	ds.l	1
findresident	ds.l	1
installmodule	ds.l	1
printf	ds.l	1

	SECTION	VERSION,DATA

	dc.b	'$VER: romfixes_PATCH 1.1 (7.1.00)',0


	IFGT	0

	; shell/Resident bug


	; fixed, 2nd try

.no_REM	move.l	a2,d0			;200A

	beq.b	.no_filepart		;671C
	tst.l	(-$0030,a5)		;4AADFFD0
	bne.b	.is_ADD			;6624
	move.l	a3,d0			;200B
	beq.b	.is_ADD			;6720
	move.l	(4,a3),d0		;202B0004
	bpl.b	.no_filepart		;6A0C
	cmp.l	#-999,d0		;0C80FFFFFC19
	bgt.b	.is_ADD			;6E12
	bra.b	.no_filepart		;6002
	dc.w	$C0DE			;C0DE
.no_filepart	tst.l	(-$002C,a5)		;4AADFFD4	REPLACE/S
	bne.w	.da_err			;660000B0
	subq.l	#1,d0			;5380

	ble.w	.da_err			;6F0000AA
.is_ADD	move.l	(-$0028,a5),d0		;202DFFD8	PURE/S



	; rom 39.106:

.no_REM	move.l	a2,d0			;200A

	beq.b	.no_filepart		;6716
	tst.l	(-$0030,a5)		;4AADFFD0
	bne.b	.is_ADD			;6624
	move.l	(4,a3),d0		;202B0004	;BUG! should test if a3=0 before!
	tst.l	d0			;4A80
	bpl.b	.no_filepart		;6A08
	cmp.l	#-999,d0		;0C80FFFFFC19
	bgt.b	.is_ADD			;6E14
.no_filepart	tst.l	(-$002C,a5)		;4AADFFD4	;REPLACE/S
	bne.w	.da_err			;660000B6
	cmp.l	#1,(4,a3)		;0CAB000000010004

	ble.w	.da_err			;6F0000AA
.is_ADD	move.l	(-$0028,a5),d0		;202DFFD8	;PURE/S



	; console.device bug

	; fixed

	move.l	d7,-(sp)			;2F07
	lea	($126E,a6),a0			;41EE126E
	move.l	a6,-(sp)			;2F0E
	move.l	($0030,a6),a6			;2C6E0030
	jsr	(_LVOObtainSemaphore,a6)	;4EAEFDCC

	move.l	(sp),a6				;2C57
	lea	($0082,a6),a2			;45EE0082
	bra.b	.in				;6002
.loop	bsr.b	cd_processwindow		;614A
.in	move.l	(a2),a2				;2452
	tst.l	(a2)				;4A92
	bne.b	.loop				;66F8
	lea	($126E,a6),a0			;41EE126E

	move.l	($0030,a6),a6			;2C6E0030
	jsr	(_LVOReleaseSemaphore,a6)	;4EAEFDC6
	move.l	(sp)+,a6			;2C5F
	move.l	(sp)+,d7			;2E1F


	; rom 37.300:

	move.l	d7,-(sp)			;2F07	;start here:
	lea	($126E,a6),a0			;41EE126E
	move.l	a6,-(sp)			;2F0E
	move.l	($0030,a6),a6			;2C6E0030
	jsr	(_LVOObtainSemaphore,a6)	;4EAEFDCC

	move.l	(sp)+,a6			;2C5F
	lea	($0082,a6),a2			;45EE0082	;BUG: the console.device bug
.loop	bsr.b	cd_processwindow		;614C
	move.l	(a2),a2				;2452
	tst.l	(a2)				;4A92
	bne.b	.loop				;66F8
	lea	($126E,a6),a0			;41EE126E
	move.l	a6,-(sp)			;2F0E

	move.l	($0030,a6),a6			;2C6E0030
	jsr	(_LVOReleaseSemaphore,a6)	;4EAEFDC6
	move.l	(sp)+,a6			;2C5F
	move.l	(sp)+,d7			;2E1F


	; rom 40.68:

	move.l	d7,-(sp)			;2F07
	lea	($126E,a6),a0			;41EE126E
	move.l	a6,-(sp)			;2F0E
	move.l	($0030,a6),a6			;2C6E0030
	jsr	(_LVOObtainSemaphore,a6)	;4EAEFDCC

	move.l	(sp)+,a6			;2C5F
	lea	($0082,a6),a2			;45EE0082
.loop	bsr.b	cd_processwindow		;614C
	move.l	(a2),a2				;2452
	tst.l	(a2)				;4A92
	bne.b	.loop				;66F8
	lea	($126E,a6),a0			;41EE126E
	move.l	a6,-(sp)			;2F0E

	move.l	($0030,a6),a6			;2C6E0030
	jsr	(_LVOReleaseSemaphore,a6)	;4EAEFDC6
	move.l	(sp)+,a6			;2C5F
	move.l	(sp)+,d7			;2E1F


	ENDC
