xref: /freebsd/usr.sbin/fdwrite/fdwrite.c (revision 0de89efe5c443f213c7ea28773ef2dc6cf3af2ed)
1 /*
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  * ----------------------------------------------------------------------------
8  *
9  * $Id: fdwrite.c,v 1.6 1997/02/22 16:05:49 peter Exp $
10  *
11  */
12 
13 #include <ctype.h>
14 #include <err.h>
15 #include <fcntl.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <strings.h>
19 #include <unistd.h>
20 
21 #include <machine/ioctl_fd.h>
22 
23 int
24 format_track(int fd, int cyl, int secs, int head, int rate,
25      int gaplen, int secsize, int fill,int interleave)
26 {
27     struct fd_formb f;
28     register int i,j;
29     int il[100];
30 
31     memset(il,0,sizeof il);
32     for(j = 0, i = 1; i <= secs; i++) {
33 	while(il[(j%secs)+1]) j++;
34 	il[(j%secs)+1] = i;
35 	j += interleave;
36     }
37 
38     f.format_version = FD_FORMAT_VERSION;
39     f.head = head;
40     f.cyl = cyl;
41     f.transfer_rate = rate;
42 
43     f.fd_formb_secshift = secsize;
44     f.fd_formb_nsecs = secs;
45     f.fd_formb_gaplen = gaplen;
46     f.fd_formb_fillbyte = fill;
47     for(i = 0; i < secs; i++) {
48 	f.fd_formb_cylno(i) = cyl;
49 	f.fd_formb_headno(i) = head;
50 	f.fd_formb_secno(i) = il[i+1];
51 	f.fd_formb_secsize(i) = secsize;
52     }
53     return ioctl(fd, FD_FORM, (caddr_t)&f);
54 }
55 
56 static void
57 usage()
58 {
59 	fprintf(stderr, "usage: fdwrite [-v] [-y] [-f inputfile] [-d device]\n");
60 	exit(2);
61 }
62 
63 int
64 main(int argc, char **argv)
65 {
66     int inputfd = -1, c, fdn = 0, i,j,fd;
67     int bpt, verbose=1, nbytes=0, track;
68     int interactive = 1;
69     char *device= "/dev/rfd0", *trackbuf = 0,*vrfybuf = 0;
70     struct fd_type fdt;
71     FILE *tty;
72 
73     setbuf(stdout,0);
74     while((c = getopt(argc, argv, "d:f:vy")) != -1)
75 	    switch(c) {
76 	    case 'd':	/* Which drive */
77 		    device = optarg;
78 		    break;
79 
80 	    case 'f':	/* input file */
81 		    if (inputfd >= 0)
82 			    close(inputfd);
83 		    inputfd = open(optarg,O_RDONLY);
84 		    if (inputfd < 0)
85 			    err(1, "%s", optarg);
86 		    break;
87 
88 	    case 'v':  /* Toggle verbosity */
89 		    verbose = !verbose;
90 		    break;
91 
92 	    case 'y':  /* Don't confirm? */
93 		    interactive = 0;
94 		    break;
95 
96 	    case '?': default:
97 		    usage();
98 	    }
99 
100     if (inputfd < 0)
101 	inputfd = 0;
102 
103     if (!isatty(1))
104 	interactive = 0;
105 
106     if(optind < argc)
107 	    usage();
108 
109     tty = fopen("/dev/tty","r+");
110     if(!tty)
111 	    err(1, "/dev/tty");
112     setbuf(tty,0);
113 
114     for(j=1;j > 0;) {
115         fdn++;
116 	if (interactive) {
117 	    fprintf(tty,
118 		    "Please insert floppy #%d in drive %s and press return >",
119 		    fdn,device);
120 	    while(1) {
121 		i = getc(tty);
122 		if(i == '\n') break;
123 	    }
124 	}
125 
126 	if((fd = open(device, O_RDWR)) < 0)
127 	    err(1, "%s", device);
128 
129 	if(ioctl(fd, FD_GTYPE, &fdt) < 0)
130 	    errx(1, "not a floppy disk: %s", device);
131 
132 	bpt = fdt.sectrac * (1<<fdt.secsize) * 128;
133 	if(!trackbuf) {
134 	    trackbuf = malloc(bpt);
135 	    if(!trackbuf) errx(1, "malloc");
136 	}
137 	if(!vrfybuf) {
138 	    vrfybuf = malloc(bpt);
139 	    if(!vrfybuf) errx(1, "malloc");
140 	}
141 
142 	if(fdn == 1) {
143 	    if(verbose) {
144 		printf("Format: %d cylinders, %d heads, %d sectors, %d bytes = %dkb\n",
145 		fdt.tracks,fdt.heads,fdt.sectrac,(1<<fdt.secsize) * 128,
146 		fdt.tracks*bpt*fdt.heads/1024);
147 
148 	    }
149 	    memset(trackbuf,0,bpt);
150 	    for(j=0;inputfd >= 0 && j<bpt;j+=i) {
151 		if(!(i = read(inputfd,trackbuf+j,bpt-j))) {
152 		    close(inputfd);
153 		    inputfd = -1;
154 		    break;
155 		}
156 		nbytes += i;
157 	    }
158 	}
159 	for (track = 0; track < fdt.tracks * fdt.heads; track++) {
160 	    if(verbose) printf("\r%3d ",fdt.tracks * fdt.heads-track);
161 	    if(verbose) putc((j ? 'I':'Z'),stdout);
162 	    format_track(fd, track / fdt.heads, fdt.sectrac, track % fdt.heads,
163 		    fdt.trans, fdt.f_gap, fdt.secsize, 0xe6,
164 		    fdt.f_inter);
165 	    if(verbose) putc('F',stdout);
166 
167 	    if (lseek (fd, (long) track*bpt, 0) < 0) err(1, "lseek");
168 	    if (write (fd, trackbuf, bpt) != bpt) err(1, "write");
169 	    if(verbose) putc('W',stdout);
170 
171 	    if (lseek (fd, (long) track*bpt, 0) < 0) err(1, "lseek");
172 	    if (read (fd, vrfybuf, bpt) != bpt) err(1, "read");
173 	    if(verbose) putc('R',stdout);
174 
175 	    if (memcmp(trackbuf,vrfybuf,bpt)) err(1, "compare");
176 	    if(verbose) putc('C',stdout);
177 
178 	    memset(trackbuf,0,bpt);
179 	    for(j=0;inputfd >= 0 && j<bpt;j+=i) {
180 		if(!(i = read(inputfd,trackbuf+j,bpt-j))) {
181 		    close(inputfd);
182 		    inputfd = -1;
183 		    break;
184 		}
185 		nbytes += i;
186 	    }
187 	}
188 	close(fd);
189 	putc('\r',stdout);
190     }
191     if(verbose)
192 	printf("%d bytes on %d flopp%s\n",nbytes,fdn,fdn==1?"y":"ies");
193     exit(0);
194 }
195