;[]-------------------------------------------------------------------------[]
;|	Program name: 5_3_7.ASM                                              |
;|	Image   file: 5_3_7.COM                                              |
;|	Note        : Hole Lock protection program.                          |
;|                    ( Communicate with TSR routine and restore INT 10H )   |
;[]-------------------------------------------------------------------------[]


cseg	segment
	assume	cs:cseg,ds:cseg
	org	100H
start:
	jmp	initialize

;------- Data area ---------------------

no_use		db	?
cs_ip		label	dword
		dw	offset terminate
		dw	-10H
hole_CX		dw	2205H
hole_DX		dw	0100H
buffer		dw	offset tail
buffer1		dw	offset tail + 200H
err_msg		db	'No key disk!',0DH,0AH,'$'
ter_msg		db	'Protection program terminate!',0DH,0AH,'$'
PSP		dw	0
old_int13	dd	?
mark		db	'HoleLock'

;------- Macro area --------------------

sec_io	macro	mode,buf
	mov	ax,mode
	mov	bx,buf
	mov	cx,hole_CX
	mov	dx,hole_DX
	pushf
	call	old_int13
	endm

;------- Check slot of key disk --------

check_hole	proc	near

	mov	ax,3503H		; check for TSR mark
	int	21H
	mov	di,104H
	mov	si,offset mark
	mov	cx,8
	cld
	repz	cmpsb
	jcxz	@@1
	jmp	hole_err

@@1:
	mov	bx,118H			; get old INT 13H vector
	mov	ax,es:[bx]
	mov	word ptr old_int13,ax
	mov	ax,es:[bx+2]
	mov	word ptr old_int13[2],ax

	mov	bx,10CH			; restore old INT 01H vector
	mov	dx,es:[bx]
	mov	ds,es:[bx+2]
	mov	ax,2501H
	int	21H

	mov	bx,110H			; restore old INT 03H vector
	mov	dx,es:[bx]
	mov	ds,es:[bx+2]
	mov	ax,2503H
	int	21H

	mov	bx,114H			; restore old INT 10H vector
	mov	dx,es:[bx]
	mov	ds,es:[bx+2]
	mov	ax,2510H
	int	21H

	mov	ax,cs			; set segment registers
	mov	ds,ax
	mov	es,ax

	mov	ax,0			; reset disk
	pushf
	call	old_int13

	mov	ax,0201H		; read a normal sector !!!
	mov	bx,buffer
	mov	cx,0001H
	mov	dx,0000H
	pushf
	call	old_int13

	sec_io	0201H,buffer		; read hole sector
	cmp	ax,1001H
	jne	hole_err

	sec_io	0301H,buffer		; write hole sector
	cmp	ax,0001H
	jne	hole_err

	sec_io	0201H,buffer1		; read hole sector
	cmp	ax,1001H
	jne	hole_err

	cld
	mov	cx,0200H		; compare read & write data
	mov	si,buffer
	mov	di,buffer1
	repe	cmpsb
	jcxz	hole_err

	mov	cx,400H			; clear track data in RAM
	xor	ax,ax
	mov	di,buffer
	rep	stosb

	ret

hole_err:

	mov	ax,cs			; show 'No key disk!' message
	mov	ds,ax
	mov	dx,offset err_msg
	mov	ah,09H
	int	21H

	mov	ah,4CH
	int	21H

check_hole	endp

;------- Initial procedure -------------

initialize:

	mov	cs:PSP,ds
	mov	ax,cs
	mov	ds,ax
	mov	es,ax

	call	check_hole

	mov	ax,cs:PSP		; set cs:ip
	mov	ds,ax
	mov	es,ax
	add	ax,10H
	add	word ptr cs:cs_ip[2],ax
	jmp	cs:cs_ip

terminate:

	mov	ax,cs
	mov	ds,ax
	mov	dx,offset ter_msg
	mov	ah,09H
	int	21H

	mov	ah,4CH
	int	21H

tail:

cseg	ends
	end	start
