;============================================
;=	   Written by Chan Kai Kin	    =
;=------------------------------------------=
;=    HarDisk Protection System ver 1.0     =
;=	      Software VS Virus		    =
;============================================

BIOS	segment	at 0000h

	org	9h*4
off_09	dw	?
seg_09	dw	?

	org	13h*4
off_13	dw	?
seg_13	dw	?

	org	60h*4
off_60	dw	?
seg_60	dw	?

	org	61h*4
off_61	dw	?
seg_61	dw	?

	org	413h
mem	dw	?

BIOS	ends

Code	segment
	assume	cs:code
	assume	ds:BIOS
	org	7c00h

begin:
	cli				; disable hardware interrupt
	xor	ax,ax			; access BIOS data area
	mov	ds,ax
	mov	ax,off_09		; installed already?
	cmp	ax,offset int09
	je	short mess		; yes - exit
	mov	ax,mem			; base memory = ?
	dec	ax			; reserve 1K memory for AV
	mov	mem,ax			; calculate address of
	mov	cl,6			;  reserved memory
	shl	ax,cl
	mov	es,ax
	mov	ax,off_09		; rediret int 09 to int 60
	mov	off_60,ax
	mov	ax,seg_09
	mov	seg_60,ax
	mov	ax,off_13		; rediret int 13 to int 61
	mov	off_61,ax
	mov	ax,seg_13
	mov	seg_61,ax
	mov	ax,offset int09 - 7c00h	; intercept int 09
	mov	off_09,ax
	mov	ax,offset int13 - 7c00h	; intercept int 13
	mov	off_13,ax
	mov	ax,es
	mov	seg_09,ax
	mov	seg_13,ax
	mov	cx,256			; move to reserved memory
	xor	di,di
	mov	si,7c00h
	cld
	rep	movsw
mess:
	mov	ah,0eh			; display message
	mov	bx,0007h
	xor	cx,cx
	inc	cx
	lea	si,prompt
	push	cs
	pop	ds
lop:
	mov	al,[si]			; until al=0
	or	al,al
	jz	short get_key
	int	10h
	inc	si
	jmp	short lop
get_key:
	mov	ah,0			; wait for F1 key
	int	16h
	cmp	ah,3bh
	jne	short get_key
try:
	int	19h			; reboot
	jmp	short try

	assume	ds:code
int09:
	int	60h			; old int 09
	push	ax			; save up register
	push	si
	push	di
	push	ds
	mov	ax,0040h		; access BIOS data area
	mov	ds,ax
	mov	si,1ah			; keyboard buffer queue pointer
	mov	di,[si]			; point to keyboard buffer
	mov	ax,[di]			; read keyboard buffer
	mov	di,offset canwrite - 7c00h
	cmp	ah,6eh			; Alt-F7 key?
	jz	short disconnect	; disconnect hardisk
	cmp	ah,6fh			; Alt-F8 key?
	jz	short connect		; Connect hardisk
	cmp	ah,70h			; Alt-F9 key?
	jz	short disable		; disable write protect
	cmp	ah,71h			; Alt-F10 key?
	jz	short enable		; enable write protect
	jmp	short exit		; exit
disconnect:
	mov	cs:byte ptr [di+1],00h
	jmp	short exit
connect:
	mov	cs:byte ptr [di+1],01h
	jmp	short exit
disable:
	mov	cs:byte ptr [di],00h
	jmp	short exit
enable:
	mov	cs:byte ptr [di],01h
exit:
	pop	ds			; return
	pop	di
	pop	si
	pop	ax
	iret

int13:
	push	di
	test	dl,80h			; hardisk?
	jz	short pass		; no - pass
	mov	di,offset canwrite - 7c00h
	or	cs:byte ptr [di],00h	; write protect enable?
	jz	short pass		; no - pass
	or	cs:byte ptr [di+1],00h	; disconnect?
	jz	short nohardisk		; yes - error
	cmp	ah,03h			; ah=03 , write function
	jz	short protect
	cmp	ah,05h			; ah=05 , format function
	jz	short protect
pass:
	pop	di			; restore register
	int	61h			; old int 13
	retf	2			; return
protect:
	pop	di			; restore register
	mov	ah,03h			; error 03 = write protect
	stc				; set carry flag
	retf	2			; return
nohardisk:
	pop	di			; restore register
	mov	ah,80h			; error 80 = general failure
	stc				; set carry flag
	retf	2			; return

;=======================================

canwrite	db	01h		; status
_connect	db	01h

prompt	db	'AV - Anti Virus tools, Written by Chan Kai Kin, 1911.'
	db	0dh,0ah,0dh,0ah
	db	'Your harddisk is now protected!',07,0dh,0ah
	db	0dh,0ah
	db	'  Alt-F7  : disconnect hardisk',0dh,0ah
	db	'  Alt-F8  : connect hardisk',0dh,0ah
	db	'  Alt-F9  : writtable',0dh,0ah
	db	'  Alt-F10 : read only',0dh,0ah,0dh,0ah
	db	'Insert bootable disk '
	db	'& press F1 when ready'
	db	'. . .',0dh,0ah,00h

;=======================================

Code	ends
	end
