2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@FreeBSD.org> 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 * ----------------------------------------------------------------------------
22 #include <sys/fdcio.h>
25 format_track(int fd, int cyl, int secs, int head, int rate,
26 int gaplen, int secsize, int fill, int interleave)
32 memset(il,0,sizeof il);
33 for(j = 0, i = 1; i <= secs; i++) {
34 while(il[(j%secs)+1]) j++;
39 f.format_version = FD_FORMAT_VERSION;
42 f.transfer_rate = rate;
44 f.fd_formb_secshift = secsize;
45 f.fd_formb_nsecs = secs;
46 f.fd_formb_gaplen = gaplen;
47 f.fd_formb_fillbyte = fill;
48 for(i = 0; i < secs; i++) {
49 f.fd_formb_cylno(i) = cyl;
50 f.fd_formb_headno(i) = head;
51 f.fd_formb_secno(i) = il[i+1];
52 f.fd_formb_secsize(i) = secsize;
54 return ioctl(fd, FD_FORM, (caddr_t)&f);
60 fprintf(stderr, "usage: fdwrite [-v] [-y] [-f inputfile] [-d device]\n");
65 main(int argc, char **argv)
67 int inputfd = -1, c, fdn = 0, i,j,fd;
68 int bpt, verbose=1, nbytes=0, track;
70 const char *device= "/dev/fd0";
71 char *trackbuf = 0,*vrfybuf = 0;
76 while((c = getopt(argc, argv, "d:f:vy")) != -1)
78 case 'd': /* Which drive */
82 case 'f': /* input file */
85 inputfd = open(optarg,O_RDONLY);
90 case 'v': /* Toggle verbosity */
94 case 'y': /* Don't confirm? */
111 tty = fopen(_PATH_TTY,"r+");
120 "Please insert floppy #%d in drive %s and press return >",
128 if((fd = open(device, O_RDWR)) < 0)
129 err(1, "%s", device);
131 if(ioctl(fd, FD_GTYPE, &fdt) < 0)
132 errx(1, "not a floppy disk: %s", device);
134 bpt = fdt.sectrac * (1<<fdt.secsize) * 128;
136 trackbuf = malloc(bpt);
137 if(!trackbuf) errx(1, "malloc");
140 vrfybuf = malloc(bpt);
141 if(!vrfybuf) errx(1, "malloc");
146 printf("Format: %d cylinders, %d heads, %d sectors, %d bytes = %dkb\n",
147 fdt.tracks,fdt.heads,fdt.sectrac,(1<<fdt.secsize) * 128,
148 fdt.tracks*bpt*fdt.heads/1024);
151 memset(trackbuf,0,bpt);
152 for(j=0;inputfd >= 0 && j<bpt;j+=i) {
153 if(!(i = read(inputfd,trackbuf+j,bpt-j))) {
161 for (track = 0; track < fdt.tracks * fdt.heads; track++) {
162 if(verbose) printf("\r%3d ",fdt.tracks * fdt.heads-track);
163 if(verbose) putc((j ? 'I':'Z'),stdout);
164 format_track(fd, track / fdt.heads, fdt.sectrac, track % fdt.heads,
165 fdt.trans, fdt.f_gap, fdt.secsize, 0xe6,
167 if(verbose) putc('F',stdout);
169 if (lseek (fd, (long) track*bpt, 0) < 0) err(1, "lseek");
170 if (write (fd, trackbuf, bpt) != bpt) err(1, "write");
171 if(verbose) putc('W',stdout);
173 if (lseek (fd, (long) track*bpt, 0) < 0) err(1, "lseek");
174 if (read (fd, vrfybuf, bpt) != bpt) err(1, "read");
175 if(verbose) putc('R',stdout);
177 if (memcmp(trackbuf,vrfybuf,bpt)) err(1, "compare");
178 if(verbose) putc('C',stdout);
180 memset(trackbuf,0,bpt);
181 for(j=0;inputfd >= 0 && j<bpt;j+=i) {
182 if(!(i = read(inputfd,trackbuf+j,bpt-j))) {
194 printf("%d bytes on %d flopp%s\n",nbytes,fdn,fdn==1?"y":"ies");