
**********************************************************
*          BB2File v1.0              22.12.1994          *
**********************************************************
* copyright (c) 1994 by Jens Tröger, all rights reserved *
*               this is Public Domain                    *
**********************************************************
*        written using HiSoft`s DevPac 3.04              *
* greets fly to Oliver Reiff, "PackMAN" Falk Zühlsdorff  *
* "Janosh" Jan Stötzer, Jan van den Baard,...            *
**********************************************************
* (non)sense: this will read the Bootblock from a given  *
* floppy-drive and then write to an file for later use   *
* works with KS1.2 and better                            *
* USAGE: BB2File <unit> <file>                           *
**********************************************************
 
	opt	o+,p+ 
 
	move.l	a0,a5 
 
; ----- alloc memory for bootblock and several structures 
 
alloc_mem	move.l	4.w,a6 
	move.l	#1024+88,d0	; Bootblock + Port/IO... 
	move.l	#$10001,d1 
	jsr	-198(a6)		; _LVOAllocMem 
	move.l	d0,a4		; a4: PTR to mem
 	beq	exit 

; -----  open dos.library
 
open_dos	lea	dosname(pc),a1 
	moveq	#33,d0 
	jsr	-552(a6)		; _LVOOpenLibrary 
	move.l	d0,d6		; d6: DOSBase
	beq	free_mem 

; -----  get unit 
 
read_unit	cmp.b	#"d",(a5)+ 
	bne	usage 
	cmp.b	#"f",(a5)+ 
	bne	usage 
	moveq	#0,d0 
	move.b	(a5)+,d0 
	sub.b	#48,d0 
	cmp.b	#4,d0 
	bge	usage
	cmp.b	#0,d0
	blt	usage
	move.l	d0,d2 		; d2: Unit 
	cmp.b	#":",(a5)+ 
	bne	usage
	adda.l	#1,a5
	move.l	a5,a0 
.loop	cmp.b	#10,(a0)+
	bne.s	.loop
	move.b	#0,-(a0)
 
; -----  add MessagePort to system
 
add_port	sub.l	a1,a1 
	jsr	-294(a6)		; _LVOFindTask 
	move.l	d0,1024+16(a4) 
	lea	1024(a4),a1
	jsr	-354(a6)		; _LVOAddPort 
 
; -----  open trackdisk.device 
 
open_track	lea	trackname(pc),a0 
	lea	1024+32(a4),a1	; a1: DiskIO-Struc 
	move.l	d2,d0 
	moveq	#0,d1 
	jsr	-444(a6)		; _LVOOpenDevice 
	tst.l	d0 
	bne	err_nodev		; FATAL !!!!!
 
; -----  read bootblock 
 
read_bb	lea	1024+46(a4),a0	; a0: RepPort 
	lea       	1024(a4),a1	; a1: ReplyPort-Struc 
	move.l	a1,(a0) 
	lea	1024+32(a4),a1	; a1: DiskIO-Struc 
	move.w	#2,28(a1) 
	move.l	#1024,36(a1) 
	move.l	a4,40(a1) 
	move.l	#0,44(a1) 
	jsr	-456(a6)		; _LVODoIO 
	move.l	d0,d2
	move	#9,28(a1) 
	move.l	#0,36(a1) 
	jsr	-456(a6)		; _LVODoIO 
	tst.l	d2
	bne.s	err_nodoio
 
; -----  close trackdisk.device 
 
close_dev	lea 	1024+32(a4),a1	; a1: DiskIO-Struc 
	jsr	-450(a6)		; _LVOCloseDevice 
 
; -----  remove port 
 
rem_port	lea	1024(a4),a1	; a1: ReplyPort 
	jsr	-360(a6)		; _LVORemPort 
 
; -----  open file 
 
open_file	move.l	d6,a6 
	move.l	#1006,d2		; MODE: NEW_FILE 
	move.l	a5,d1
	jsr	-30(a6)		; _LVOOpen
	move.l	d0,d7		; d7: File-Handle 
 	beq.s	no_open 

; -----  write

write	move.l	d7,d1
	move.l	a4,d2
	move.l	#1024,d3
	jsr	-48(a6)		; _LVOWrite
	cmp.l	#1024,d0
	bne.s	no_write

; -----  close up clearly
 
close_file	move.l	d7,d1
	jsr	-36(a6)		; _LVOClose  
 
; -----  close dos.library
 
close_dos	move.l	4.w,a6 
	move.l	d6,a1 
	jsr	-414(a6)		; _LVOCloseLibrary 
 
; -----  free allocated memory 
 
free_mem	move.l	a4,a1 
	move.l	#1024+88,d0 
	jsr	-210(a6)		; _LVOFreeMem 
 
; -----  returncode: 0 
 
exit	moveq	#0,d0 
	rts 
 
; -----  several messages
 
usage	lea	usage_text(pc),a2 
	move.l	#usage_text_end-usage_text,d3 
	bsr.s	write_msg 
	bra	close_dos 
 
err_nodev	lea	err1_text(pc),a2 
	moveq	#38,d3 
	bsr.s	write_msg
	move.l	4.w,a6
	lea	1024(a4),a1	; a1: ReplyPort 
	jsr	-360(a6)		; _LVORemPort 
	bra.s	close_dos
 
err_nodoio	lea	err2_text(pc),a2 
	moveq	#44,d3 
	bsr.s	write_msg
	move.l	4.w,a6
	lea 	1024+32(a4),a1	; a1: DiskIO-Struc 
	jsr	-450(a6)		; _LVOCloseDevice 
 	lea	1024(a4),a1	; a1: ReplyPort 
	jsr	-360(a6)		; _LVORemPort 
	bra.s	close_dos
 
no_open    lea	err3_text(pc),a2 
	moveq      #25,d3 
	bsr.s	write_msg 
	bra	close_dos 
 
no_write	lea	err4_text(pc),a2 
	moveq	#37,d0 
	bsr.s	write_msg 
	bra.s	close_file 
 
; -----  write text to stdout
 
write_msg	move.l	d6,a6 
	jsr	-60(a6)		; _LVOOutput 
	move.l	d0,d1 
	move.l	a2,d2 
	jmp	-48(a6)		; _LVOWrite 
 
; -----  names, texts
 
dosname	dc.b	"dos.library",0 
	even 
trackname	dc.b	"trackdisk.device",0 
	even 
err1_text	dc.b	"FATAL ERROR opening trackdisk.device.",10 
	even 
err2_text	dc.b	"Error reading Bootblock. No disk inserted ?",10 
	even 
err3_text	dc.b	"Cannot open Output-File.",10 
	even 
err4_text	dc.b	"Error writing to File. Aborting.",10 
	even
usage_text	dc.b	10,$9b,"1mBB2File v1.0",$9b,"0m 1994 by Jens Tröger",10 
	dc.b	"USAGE: BB2File <unit> <file>",10 
	dc.b	"Where <unit> is one of your mounted floppies (df0:) "
	dc.b	"and <file> is the Path/Name of the file.",10,10 
usage_text_end 

	end 
