2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@login.dkuug.dk> wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
9 * $FreeBSD: src/usr.sbin/fdwrite/fdwrite.c,v 1.8.2.3 2001/07/19 13:21:19 joerg Exp $
10 * $DragonFly: src/usr.sbin/fdwrite/fdwrite.c,v 1.5 2004/12/03 22:51:47 liamfoy Exp $
23 #include <machine/ioctl_fd.h>
25 static int format_track(int, int, int, int, int,
29 format_track(int fd
, int cyl
, int secs
, int head
, int rate
,
30 int gaplen
, int secsize
, int fill
,int interleave
)
36 memset(il
,0,sizeof il
);
37 for(j
= 0, i
= 1; i
<= secs
; i
++) {
38 while(il
[(j
%secs
)+1]) j
++;
43 f
.format_version
= FD_FORMAT_VERSION
;
46 f
.transfer_rate
= rate
;
48 f
.fd_formb_secshift
= secsize
;
49 f
.fd_formb_nsecs
= secs
;
50 f
.fd_formb_gaplen
= gaplen
;
51 f
.fd_formb_fillbyte
= fill
;
52 for(i
= 0; i
< secs
; i
++) {
53 f
.fd_formb_cylno(i
) = cyl
;
54 f
.fd_formb_headno(i
) = head
;
55 f
.fd_formb_secno(i
) = il
[i
+1];
56 f
.fd_formb_secsize(i
) = secsize
;
58 return ioctl(fd
, FD_FORM
, (caddr_t
)&f
);
64 fprintf(stderr
, "usage: fdwrite [-v] [-y] [-f inputfile] [-d device]\n");
69 main(int argc
, char **argv
)
71 int inputfd
= -1, c
, fdn
= 0, i
,j
,fd
;
72 int bpt
, verbose
=1, nbytes
=0, track
;
73 int interactive
= 1, fdopts
;
74 const char *device
= "/dev/fd0";
75 char *trackbuf
= NULL
,*vrfybuf
= NULL
;
80 while((c
= getopt(argc
, argv
, "d:f:vy")) != -1)
82 case 'd': /* Which drive */
86 case 'f': /* input file */
89 inputfd
= open(optarg
,O_RDONLY
);
94 case 'v': /* Toggle verbosity */
98 case 'y': /* Don't confirm? */
115 tty
= fopen(_PATH_TTY
,"r+");
124 "Please insert floppy #%d in drive %s and press return >",
132 if((fd
= open(device
, O_RDWR
)) < 0)
133 err(1, "%s", device
);
135 if(ioctl(fd
, FD_GTYPE
, &fdt
) < 0)
136 errx(1, "not a floppy disk: %s", device
);
137 fdopts
= FDOPT_NOERRLOG
;
138 if (ioctl(fd
, FD_SOPTS
, &fdopts
) == -1)
139 err(1, "ioctl(FD_SOPTS, FDOPT_NOERRLOG)");
141 bpt
= fdt
.sectrac
* (1<<fdt
.secsize
) * 128;
143 trackbuf
= malloc(bpt
);
144 if(!trackbuf
) err(1, "malloc failed");
147 vrfybuf
= malloc(bpt
);
148 if(!vrfybuf
) err(1, "malloc failed");
153 printf("Format: %d cylinders, %d heads, %d sectors, %d bytes = %dkb\n",
154 fdt
.tracks
,fdt
.heads
,fdt
.sectrac
,(1<<fdt
.secsize
) * 128,
155 fdt
.tracks
*bpt
*fdt
.heads
/1024);
158 memset(trackbuf
,0,bpt
);
159 for(j
=0;inputfd
>= 0 && j
<bpt
;j
+=i
) {
160 if(!(i
= read(inputfd
,trackbuf
+j
,bpt
-j
))) {
168 for (track
= 0; track
< fdt
.tracks
* fdt
.heads
; track
++) {
169 if(verbose
) printf("\r%3d ",fdt
.tracks
* fdt
.heads
-track
);
170 if(verbose
) putc((j
? 'I':'Z'),stdout
);
171 format_track(fd
, track
/ fdt
.heads
, fdt
.sectrac
, track
% fdt
.heads
,
172 fdt
.trans
, fdt
.f_gap
, fdt
.secsize
, 0xe6,
174 if(verbose
) putc('F',stdout
);
176 if (lseek (fd
, (long) track
*bpt
, 0) < 0) err(1, "lseek");
177 if (write (fd
, trackbuf
, bpt
) != bpt
) err(1, "write");
178 if(verbose
) putc('W',stdout
);
180 if (lseek (fd
, (long) track
*bpt
, 0) < 0) err(1, "lseek");
181 if (read (fd
, vrfybuf
, bpt
) != bpt
) err(1, "read");
182 if(verbose
) putc('R',stdout
);
184 if (memcmp(trackbuf
,vrfybuf
,bpt
)) err(1, "compare");
185 if(verbose
) putc('C',stdout
);
187 memset(trackbuf
,0,bpt
);
188 for(j
=0;inputfd
>= 0 && j
<bpt
;j
+=i
) {
189 if(!(i
= read(inputfd
,trackbuf
+j
,bpt
-j
))) {
201 printf("%d bytes on %d flopp%s\n",nbytes
,fdn
,fdn
==1?"y":"ies");