xref: /freebsd/usr.sbin/fdformat/fdformat.c (revision e627b39baccd1ec9129690167cf5e6d860509655)
1 /*
2  * Copyright (C) 1992-1994 by Joerg Wunsch, Dresden
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
18  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
23  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 /*
28  * FreeBSD:
29  * format a floppy disk
30  *
31  * Added FD_GTYPE ioctl, verifying, proportional indicators.
32  * Serge Vakulenko, vak@zebub.msk.su
33  * Sat Dec 18 17:45:47 MSK 1993
34  *
35  * Final adaptation, change format/verify logic, add separate
36  * format gap/interleave values
37  * Andrew A. Chernov, ache@astral.msk.su
38  * Thu Jan 27 00:47:24 MSK 1994
39  */
40 
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <fcntl.h>
45 #include <strings.h>
46 #include <ctype.h>
47 
48 #include <errno.h>
49 #include <machine/ioctl_fd.h>
50 
51 static void
52 format_track(int fd, int cyl, int secs, int head, int rate,
53 	     int gaplen, int secsize, int fill,int interleave)
54 {
55 	struct fd_formb f;
56 	register int i,j;
57 	int il[FD_MAX_NSEC + 1];
58 
59 	memset(il,0,sizeof il);
60 	for(j = 0, i = 1; i <= secs; i++) {
61 	    while(il[(j%secs)+1]) j++;
62 	    il[(j%secs)+1] = i;
63 	    j += interleave;
64 	}
65 
66 	f.format_version = FD_FORMAT_VERSION;
67 	f.head = head;
68 	f.cyl = cyl;
69 	f.transfer_rate = rate;
70 
71 	f.fd_formb_secshift = secsize;
72 	f.fd_formb_nsecs = secs;
73 	f.fd_formb_gaplen = gaplen;
74 	f.fd_formb_fillbyte = fill;
75 	for(i = 0; i < secs; i++) {
76 		f.fd_formb_cylno(i) = cyl;
77 		f.fd_formb_headno(i) = head;
78 		f.fd_formb_secno(i) = il[i+1];
79 		f.fd_formb_secsize(i) = secsize;
80 	}
81 	if(ioctl(fd, FD_FORM, (caddr_t)&f) < 0) {
82 		perror("\nfdformat: ioctl(FD_FORM)");
83 		exit(1);
84 	}
85 }
86 
87 static int
88 verify_track(int fd, int track, int tracksize)
89 {
90 	static char *buf = 0;
91 	static int bufsz = 0;
92 	int fdopts = -1, ofdopts, rv = 0;
93 
94 	if (ioctl(fd, FD_GOPTS, &fdopts) < 0)
95 		perror("warning: ioctl(FD_GOPTS)");
96 	else {
97 		ofdopts = fdopts;
98 		fdopts |= FDOPT_NORETRY;
99 		(void)ioctl(fd, FD_SOPTS, &fdopts);
100 	}
101 
102 	if (bufsz < tracksize) {
103 		if (buf)
104 			free (buf);
105 		bufsz = tracksize;
106 		buf = 0;
107 	}
108 	if (! buf)
109 		buf = malloc (bufsz);
110 	if (! buf) {
111 		fprintf (stderr, "\nfdformat: out of memory\n");
112 		exit (2);
113 	}
114 	if (lseek (fd, (long) track*tracksize, 0) < 0)
115 		rv = -1;
116 	/* try twice reading it, without using the normal retrier */
117 	else if (read (fd, buf, tracksize) != tracksize
118 		 && read (fd, buf, tracksize) != tracksize)
119 		rv = -1;
120 	if(fdopts != -1)
121 		(void)ioctl(fd, FD_SOPTS, &ofdopts);
122 	return (rv);
123 }
124 
125 static const char *
126 makename(const char *arg, const char *suffix)
127 {
128 	static char namebuff[20];	/* big enough for "/dev/rfd0a"... */
129 
130 	memset(namebuff, 0, 20);
131 	if(*arg == '\0') /* ??? */
132 		return arg;
133 	if(*arg == '/')  /* do not convert absolute pathnames */
134 		return arg;
135 	strcpy(namebuff, "/dev/r");
136 	strncat(namebuff, arg, 3);
137 	strcat(namebuff, suffix);
138 	return namebuff;
139 }
140 
141 static void
142 usage (void)
143 {
144 	printf("Usage:\n\tfdformat [-q] [-n | -v] [-f #] [-c #] [-s #] [-h #]\n");
145 	printf("\t\t [-r #] [-g #] [-i #] [-S #] [-F #] [-t #] devname\n");
146 	printf("Options:\n");
147 	printf("\t-q\tsupress any normal output, don't ask for confirmation\n");
148 	printf("\t-n\tdon't verify floppy after formatting\n");
149 	printf("\t-v\tdon't format, verify only\n");
150 	printf("\t-f #\tspecify desired floppy capacity, in kilobytes;\n");
151 	printf("\t\tvalid choices are 360, 720, 800, 820, 1200, 1440, 1480, 1720\n");
152 	printf("\tdevname\tthe full name of floppy device or in short form fd0, fd1\n");
153 	printf("Obscure options:\n");
154 	printf("\t-c #\tspecify number of cylinders, 40 or 80\n");
155 	printf("\t-s #\tspecify number of sectors per track, 9, 10, 15 or 18\n");
156 	printf("\t-h #\tspecify number of floppy heads, 1 or 2\n");
157 	printf("\t-r #\tspecify data rate, 250, 300 or 500 kbps\n");
158 	printf("\t-g #\tspecify gap length\n");
159 	printf("\t-i #\tspecify interleave factor\n");
160 	printf("\t-S #\tspecify sector size, 0=128, 1=256, 2=512 bytes\n");
161 	printf("\t-F #\tspecify fill byte\n");
162 	printf("\t-t #\tnumber of steps per track\n");
163 	exit(2);
164 }
165 
166 static int
167 yes (void)
168 {
169 	char reply [256], *p;
170 
171 	reply[sizeof(reply)-1] = 0;
172 	for (;;) {
173 		fflush(stdout);
174 		if (! fgets (reply, sizeof(reply)-1, stdin))
175 			return (0);
176 		for (p=reply; *p==' ' || *p=='\t'; ++p)
177 			continue;
178 		if (*p=='y' || *p=='Y')
179 			return (1);
180 		if (*p=='n' || *p=='N' || *p=='\n' || *p=='\r')
181 			return (0);
182 		printf("Answer `yes' or `no': ");
183 	}
184 }
185 
186 int
187 main(int argc, char **argv)
188 {
189 	int format = -1, cyls = -1, secs = -1, heads = -1, intleave = -1;
190 	int rate = -1, gaplen = -1, secsize = -1, steps = -1;
191 	int fill = 0xf6, quiet = 0, verify = 1, verify_only = 0;
192 	int fd, c, track, error, tracks_per_dot, bytes_per_track, errs;
193 	const char *devname, *suffix;
194 	struct fd_type fdt;
195 
196 	while((c = getopt(argc, argv, "f:c:s:h:r:g:S:F:t:i:qvn")) != -1)
197 		switch(c) {
198 		case 'f':	/* format in kilobytes */
199 			format = atoi(optarg);
200 			break;
201 
202 		case 'c':	/* # of cyls */
203 			cyls = atoi(optarg);
204 			break;
205 
206 		case 's':	/* # of secs per track */
207 			secs = atoi(optarg);
208 			break;
209 
210 		case 'h':	/* # of heads */
211 			heads = atoi(optarg);
212 			break;
213 
214 		case 'r':	/* transfer rate, kilobyte/sec */
215 			rate = atoi(optarg);
216 			break;
217 
218 		case 'g':	/* length of GAP3 to format with */
219 			gaplen = atoi(optarg);
220 			break;
221 
222 		case 'S':	/* sector size shift factor (1 << S)*128 */
223 			secsize = atoi(optarg);
224 			break;
225 
226 		case 'F':	/* fill byte, C-like notation allowed */
227 			fill = (int)strtol(optarg, (char **)0, 0);
228 			break;
229 
230 		case 't':	/* steps per track */
231 			steps = atoi(optarg);
232 			break;
233 
234 		case 'i':       /* interleave factor */
235 			intleave = atoi(optarg);
236 			break;
237 
238 		case 'q':
239 			quiet = 1;
240 			break;
241 
242 		case 'n':
243 			verify = 0;
244 			break;
245 
246 		case 'v':
247 			verify = 1;
248 			verify_only = 1;
249 			break;
250 
251 		case '?': default:
252 			usage();
253 		}
254 
255 	if(optind != argc - 1)
256 		usage();
257 
258 	switch(format) {
259 	default:
260 		fprintf(stderr, "fdformat: bad floppy size: %dK\n", format);
261 		exit(2);
262 	case -1:   suffix = "";      break;
263 	case 360:  suffix = ".360";  break;
264 	case 720:  suffix = ".720";  break;
265 	case 800:  suffix = ".800";  break;
266 	case 820:  suffix = ".820";  break;
267 	case 1200: suffix = ".1200"; break;
268 	case 1440: suffix = ".1440"; break;
269 	case 1480: suffix = ".1480"; break;
270 	case 1720: suffix = ".1720"; break;
271 	}
272 
273 	devname = makename(argv[optind], suffix);
274 
275 	if((fd = open(devname, O_RDWR)) < 0) {
276 		perror(devname);
277 		exit(1);
278 	}
279 
280 	if(ioctl(fd, FD_GTYPE, &fdt) < 0) {
281 		fprintf(stderr, "fdformat: not a floppy disk: %s\n", devname);
282 		exit(1);
283 	}
284 
285 	switch(rate) {
286 	case -1:  break;
287 	case 250: fdt.trans = FDC_250KBPS; break;
288 	case 300: fdt.trans = FDC_300KBPS; break;
289 	case 500: fdt.trans = FDC_500KBPS; break;
290 	default:
291 		fprintf(stderr, "fdformat: invalid transfer rate: %d\n", rate);
292 		exit(2);
293 	}
294 
295 	if (cyls >= 0)    fdt.tracks = cyls;
296 	if (secs >= 0)    fdt.sectrac = secs;
297 	if (fdt.sectrac > FD_MAX_NSEC) {
298 		fprintf(stderr, "fdformat: too many sectors per track, max value is %d\n", FD_MAX_NSEC);
299 		exit(2);
300 	}
301 	if (heads >= 0)   fdt.heads = heads;
302 	if (gaplen >= 0)  fdt.f_gap = gaplen;
303 	if (secsize >= 0) fdt.secsize = secsize;
304 	if (steps >= 0)   fdt.steptrac = steps;
305 	if (intleave >= 0) fdt.f_inter = intleave;
306 
307 	bytes_per_track = fdt.sectrac * (1<<fdt.secsize) * 128;
308 	tracks_per_dot = fdt.tracks * fdt.heads / 40;
309 
310 	if (verify_only) {
311 		if(!quiet)
312 			printf("Verify %dK floppy `%s'.\n",
313 				fdt.tracks * fdt.heads * bytes_per_track / 1024,
314 				devname);
315 	}
316 	else if(!quiet) {
317 		printf("Format %dK floppy `%s'? (y/n): ",
318 			fdt.tracks * fdt.heads * bytes_per_track / 1024,
319 			devname);
320 		if(! yes ()) {
321 			printf("Not confirmed.\n");
322 			return 0;
323 		}
324 	}
325 
326 	/*
327 	 * Formatting.
328 	 */
329 	if(!quiet) {
330 		printf("Processing ----------------------------------------\r");
331 		printf("Processing ");
332 		fflush(stdout);
333 	}
334 
335 	error = errs = 0;
336 
337 	for (track = 0; track < fdt.tracks * fdt.heads; track++) {
338 		if (!verify_only) {
339 			format_track(fd, track / fdt.heads, fdt.sectrac,
340 				track % fdt.heads, fdt.trans, fdt.f_gap,
341 				fdt.secsize, fill, fdt.f_inter);
342 			if(!quiet && !((track + 1) % tracks_per_dot)) {
343 				putchar('F');
344 				fflush(stdout);
345 			}
346 		}
347 		if (verify) {
348 			if (verify_track(fd, track, bytes_per_track) < 0)
349 				error = errs = 1;
350 			if(!quiet && !((track + 1) % tracks_per_dot)) {
351 				if (!verify_only)
352 					putchar('\b');
353 				if (error) {
354 					putchar('E');
355 					error = 0;
356 				}
357 				else
358 					putchar('V');
359 				fflush(stdout);
360 			}
361 		}
362 	}
363 	if(!quiet)
364 		printf(" done.\n");
365 
366 	return errs;
367 }
368 /*
369  * Local Variables:
370  *  c-indent-level:               8
371  *  c-continued-statement-offset: 8
372  *  c-continued-brace-offset:     0
373  *  c-brace-offset:              -8
374  *  c-brace-imaginary-offset:     0
375  *  c-argdecl-indent:             8
376  *  c-label-offset:              -8
377  *  c++-hanging-braces:           1
378  *  c++-access-specifier-offset: -8
379  *  c++-empty-arglist-indent:     8
380  *  c++-friend-offset:            0
381  * End:
382  */
383