
		BRA		Start

Version:	DC.B	"$VER: BootSum 1.0",0

		EVEN

	;Open the Dos library.
Start:	MOVE.L	4.w,A6
		LEA		DosName,A1
		JSR		_LVOOldOpenLibrary(A6)
		MOVE.L	D0,DosBase

	;Check the dos library version
		MOVE.L	D0,A0
		CMP.W	#37,20(A0)
		BLO		WrongDos

	;Call ReadArgs to pre-parse the command line.
		MOVE.L	DosBase,A6
		MOVE.L	#Template,D1
		MOVE.L	#Array,D2
		MOVEQ.L	#0,D3
		JSR		_LVOReadArgs(A6)
		MOVE.L	D0,RdArgs
		BEQ		SyntaxError

	;Test for no DF?: arguments at all
		MOVE.L	Array,D0
		ADD.L	Array+4,D0
		Add.L	Array+8,D0
		TST.L	D0
		BEQ		SyntaxError
		
	;Find this Program's Task
		MOVE.L	4.w,A6
		SUB.L	A1,A1
		JSR		_LVOFindTask(A6)
		MOVE.L	D0,ProgTask
		
	;Add a Reply Port
		MOVE.L	4.w,A6
		LEA		ReadReply,A1
		JSR		_LVOAddPort(A6)
		
	;Test for DF0: argument
		TST.L	Array
		BEQ		NoDF0
		MOVE.B	#0,D0
		JSR		DoCheck

	;Test for DF1: argument
NoDF0:	TST.L	Array+4
		BEQ		NoDF1
		MOVE.B	#1,D0
		JSR		DoCheck

	;Test for DF2: argument
NoDF1:	TST.L	Array+8
		BEQ		NoDF2
		MOVE.B	#2,D0
		JSR		DoCheck

	;Close the Reply Port
NoDF2:	MOVE.L	4.w,A6
		LEA		ReadReply,A1
		JSR		_LVORemPort(A6)
	
	;Free the RdArgs structure
Exit:	MOVE.L	DosBase,A6
		MOVE.L	RdArgs,D1
		JSR		_LVOFreeArgs(A6)

	;Close the dos library
		MOVE.L	4.w,A6
		MOVE.L	DosBase,A1
		JSR		_LVOCloseLibrary(A6)
		
WrongDos:
	;Return to the CLI
		CLR.L	D0
		RTS

SyntaxError:
	;Print a message telling user of the correct syntax
		MOVE.L	DosBase,A6
		MOVE.L	#SyntaxErrorText,D1
		JSR		_LVOPutStr(A6)
		BRA.S	Exit

;This is the main routine to check bootblock on unit number D0.
DoCheck:
		ADD.B	#'0',D0
		MOVE.B	D0,DeviceNumber
		
		MOVE.L	DosBase,A6
		MOVE.L	#CheckingMessage,D1
		JSR		_LVOPutStr(A6)

	;Open the TrackDisk Device
		MOVE.L	4.w,A6
		LEA		DiskIO,A1
		CLR.L	D0
		MOVE.B	DeviceNumber,D0
		SUB.B	#'0',D0
		CLR.L	D1
		LEA		TrackName,A0
		JSR		_LVOOpenDevice(A6)
		TST.L	D0
		BNE		NoTrack

	;Read the boot block into the buffer
		MOVE.L	4.w,A6
		LEA		DiskIO,A1
		MOVE.L	#ReadReply,14(A1)
		MOVE.W	#2,28(A1)		;Command=READ
		MOVE.L	#1024,36(A1)		;Bytes to read
		MOVE.L	#Buffer,40(A1)		;Address
		MOVE.L	#0,44(A1)		;Offset
		JSR		_LVODoIO(A6)
		TST.L	D0
		BNE		ReadError
		
	;Turn off the motor
		LEA		DiskIO,A1
		MOVE.W	#9,28(A1)		;Command=MOTOR
		MOVE.L	#0,36(A1)		;0=OFF
		JSR		_LVODoIO(A6)

	;Store the old checksum away in D2
		MOVE.L	Buffer+4,D2

	;Calculate the checksum
		LEA		Buffer,A0
		CLR.L	D0
		MOVE.L	D0,Buffer+4
		MOVE.L	#255,D1
CalcLoop:
		ADD.L	(A0)+,D0
		BCC		NoOverFlow
		ADDQ.l	#1,D0
NoOverFlow:
		DBRA	D1,CalcLoop
		MOVEQ.L	#-1,D1
		SUB.L	D0,D1
		
	;Compare with the old checksum
		CMP.L	D1,D2
		BEQ		CheckSumOkay
		
	;Checksum was wrong, do we correct it ?
		TST.L	Array+12
		BEQ		DontCorrect
		
	;Write the buffer back to the disk
		MOVE.L	4.w,A6
		LEA		DiskIO,A1
		MOVE.L	#ReadReply,14(A1)
		MOVE.W	#3,28(A1)		;Command=WRITE
		MOVE.L	#1024,36(A1)		;No of bytes to write
		MOVE.L	#Buffer,40(A1)		;Address
		MOVE.L	#0,44(A1)		;Offset
		JSR		_LVODoIO(A6)
		TST.L	D0
		BNE		WriteError
		
		LEA		DiskIO,A1
		MOVE.W	#4,28(A1)		;Command=UPDATE
		JSR		_LVODoIO(A6)
		TST.L	D0
		BNE		WriteError
		
		LEA		DiskIO,A1
		MOVE.W	#5,28(A1)		;Command=CLEAR
		JSR		_LVODoIO(A6)
		
	;Output message to say checksum updated.
		MOVE.L	DosBase,A6
		MOVE.L	#CheckUpdatedMess,D1
		JSR		_LVOPutStr(A6)

MotorOff:
		MOVE.L	4.w,A6
		LEA		DiskIO,A1
		MOVE.W	#9,28(A1)		;Command=MOTOR
		MOVE.L	#0,36(A1)		;0=OFF
		JSR		_LVODoIO(A6)
		
		BRA	CloseTD
		
DontCorrect:
		MOVE.L	DosBase,A6
		MOVE.L	#CheckWrongMess,D1
		JSR		_LVOPutStr(A6)
		BRA	CloseTD
		
CheckSumOkay:
		MOVE.L	DosBase,A6
		MOVE.L	#CheckOkayMess,D1
		JSR		_LVOPutStr(A6)
	
	;Close the trackdisk device
CloseTD:
		MOVE.L	4.w,A6
		LEA		DiskIO,A1
		JSR		_LVOCloseDevice(A6)
		
		RTS

	;Couldn't open trackdisk device error
NoTrack:
		MOVE.L	DosBase,A6
		MOVE.L	#NoTrackErr,D1
		JSR		_LVOPutStr(A6)
		RTS

	;Error reading the bootblock
ReadError:
		MOVE.L	DosBase,A6
		MOVE.L	#ReadErr,D1
		JSR		_LVOPutStr(A6)
		BRA.s	MotorOff

	;Error writing back the bootblock
WriteError:
		MOVE.L	DosBase,A6
		MOVE.L	#CheckNotUpdatedMess,D1
		JSR		_LVOPutStr(A6)
		BRA		MotorOff

		SECTION data,data

DiskIO:			DC.L	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
ReadReply		DC.L	0,0,0,0,0,0,0,0
ProgTask:		EQU	ReadReply+$10
Template:		DC.B	"DF0:/S,DF1:/S,DF2:/S,WRITE/S",0
DosName:		DC.B	"dos.library",0
TrackName:		DC.B	"trackdisk.device",0
SyntaxErrorText:	DC.B	"BootSum [DF0:|DF1:|DF2] [WRITE]         ©1999 Mike Archer",10,10,0
CheckingMessage:	DC.B	"Checking Bootblock on DF"
DeviceNumber:		DC.B	"0: .....",0
NoTrackErr:		DC.B	"Couldn't open trackdisk.device!",10,0
ReadErr:		DC.B	"Error reading the bootblock!",10,0			
CheckOkayMess:		DC.B	"Correct.",10,0			
CheckWrongMess:		DC.B	"Incorrect.",10,0
CheckUpdatedMess:	DC.B	"Corrected.",10,0
CheckNotUpdatedMess:	DC.B	"Couldn't write back correct checksum.",10,0


		SECTION data2,bss_C

DosBase:	DS.L	1
RdArgs:		DS.L	1
Array:		DS.L	4
Buffer:		DS.L	256


