fdformat.c revision 1138
1/*
2 * Copyright (C) 1992-1993 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 ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * 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#include <../i386/isa/fdreg.h>	/* XXX should be in <machine> dir */
51
52static void
53format_track(int fd, int cyl, int secs, int head, int rate,
54	     int gaplen, int secsize, int fill,int interleave)
55{
56	struct fd_formb f;
57	register int i,j;
58	int il[FD_MAX_NSEC + 1];
59
60	memset(il,0,sizeof il);
61	for(j = 0, i = 1; i <= secs; i++) {
62	    while(il[(j%secs)+1]) j++;
63	    il[(j%secs)+1] = i;
64	    j += interleave;
65	}
66
67	f.format_version = FD_FORMAT_VERSION;
68	f.head = head;
69	f.cyl = cyl;
70	f.transfer_rate = rate;
71
72	f.fd_formb_secshift = secsize;
73	f.fd_formb_nsecs = secs;
74	f.fd_formb_gaplen = gaplen;
75	f.fd_formb_fillbyte = fill;
76	for(i = 0; i < secs; i++) {
77		f.fd_formb_cylno(i) = cyl;
78		f.fd_formb_headno(i) = head;
79		f.fd_formb_secno(i) = il[i+1];
80		f.fd_formb_secsize(i) = secsize;
81	}
82	if(ioctl(fd, FD_FORM, (caddr_t)&f) < 0) {
83		perror("\nfdformat: ioctl(FD_FORM)");
84		exit(1);
85	}
86}
87
88static int
89verify_track(int fd, int track, int tracksize)
90{
91	static char *buf = 0;
92	static int bufsz = 0;
93
94	if (bufsz < tracksize) {
95		if (buf)
96			free (buf);
97		bufsz = tracksize;
98		buf = 0;
99	}
100	if (! buf)
101		buf = malloc (bufsz);
102	if (! buf) {
103		fprintf (stderr, "\nfdformat: out of memory\n");
104		exit (2);
105	}
106	if (lseek (fd, (long) track*tracksize, 0) < 0)
107		return (-1);
108	if (read (fd, buf, tracksize) != tracksize)
109		return (-1);
110	return (0);
111}
112
113static const char *
114makename(const char *arg, const char *suffix)
115{
116	static char namebuff[20];	/* big enough for "/dev/rfd0a"... */
117
118	memset(namebuff, 0, 20);
119	if(*arg == '\0') /* ??? */
120		return arg;
121	if(*arg == '/')  /* do not convert absolute pathnames */
122		return arg;
123	strcpy(namebuff, "/dev/r");
124	strncat(namebuff, arg, 3);
125	strcat(namebuff, suffix);
126	return namebuff;
127}
128
129static void
130usage ()
131{
132	printf("Usage:\n\tfdformat [-q] [-n | -v] [-f #] [-c #] [-s #] [-h #]\n");
133	printf("\t\t [-r #] [-g #] [-i #] [-S #] [-F #] [-t #] devname\n");
134	printf("Options:\n");
135	printf("\t-q\tsupress any normal output, don't ask for confirmation\n");
136	printf("\t-n\tdon't verify floppy after formatting\n");
137	printf("\t-v\tdon't format, verify only\n");
138	printf("\t-f #\tspecify desired floppy capacity, in kilobytes;\n");
139	printf("\t\tvalid choices are 360, 720, 800, 820, 1200, 1440, 1480, 1720\n");
140	printf("\tdevname\tthe full name of floppy device or in short form fd0, fd1\n");
141	printf("Obscure options:\n");
142	printf("\t-c #\tspecify number of cylinders, 40 or 80\n");
143	printf("\t-s #\tspecify number of sectors per track, 9, 10, 15 or 18\n");
144	printf("\t-h #\tspecify number of floppy heads, 1 or 2\n");
145	printf("\t-r #\tspecify data rate, 250, 300 or 500 kbps\n");
146	printf("\t-g #\tspecify gap length\n");
147	printf("\t-i #\tspecify interleave factor\n");
148	printf("\t-S #\tspecify sector size, 0=128, 1=256, 2=512 bytes\n");
149	printf("\t-F #\tspecify fill byte\n");
150	printf("\t-t #\tnumber of steps per track\n");
151	exit(2);
152}
153
154static int
155yes ()
156{
157	char reply [256], *p;
158
159	reply[sizeof(reply)-1] = 0;
160	for (;;) {
161		fflush(stdout);
162		if (! fgets (reply, sizeof(reply)-1, stdin))
163			return (0);
164		for (p=reply; *p==' ' || *p=='\t'; ++p)
165			continue;
166		if (*p=='y' || *p=='Y')
167			return (1);
168		if (*p=='n' || *p=='N' || *p=='\n' || *p=='\r')
169			return (0);
170		printf("Answer `yes' or `no': ");
171	}
172}
173
174int
175main(int argc, char **argv)
176{
177	int format = -1, cyls = -1, secs = -1, heads = -1, intleave = -1;
178	int rate = -1, gaplen = -1, secsize = -1, steps = -1;
179	int fill = 0xf6, quiet = 0, verify = 1, verify_only = 0;
180	int fd, c, track, error, tracks_per_dot, bytes_per_track, errs;
181	const char *devname, *suffix;
182	struct fd_type fdt;
183
184	while((c = getopt(argc, argv, "f:c:s:h:r:g:S:F:t:i:qvn")) != -1)
185		switch(c) {
186		case 'f':	/* format in kilobytes */
187			format = atoi(optarg);
188			break;
189
190		case 'c':	/* # of cyls */
191			cyls = atoi(optarg);
192			break;
193
194		case 's':	/* # of secs per track */
195			secs = atoi(optarg);
196			break;
197
198		case 'h':	/* # of heads */
199			heads = atoi(optarg);
200			break;
201
202		case 'r':	/* transfer rate, kilobyte/sec */
203			rate = atoi(optarg);
204			break;
205
206		case 'g':	/* length of GAP3 to format with */
207			gaplen = atoi(optarg);
208			break;
209
210		case 'S':	/* sector size shift factor (1 << S)*128 */
211			secsize = atoi(optarg);
212			break;
213
214		case 'F':	/* fill byte, C-like notation allowed */
215			fill = (int)strtol(optarg, (char **)0, 0);
216			break;
217
218		case 't':	/* steps per track */
219			steps = atoi(optarg);
220			break;
221
222		case 'i':       /* interleave factor */
223			intleave = atoi(optarg);
224			break;
225
226		case 'q':
227			quiet = 1;
228			break;
229
230		case 'n':
231			verify = 0;
232			break;
233
234		case 'v':
235			verify = 1;
236			verify_only = 1;
237			break;
238
239		case '?': default:
240			usage();
241		}
242
243	if(optind != argc - 1)
244		usage();
245
246	switch(format) {
247	default:
248		fprintf(stderr, "fdformat: bad floppy size: %dK\n", format);
249		exit(2);
250	case -1:   suffix = "";      break;
251	case 360:  suffix = ".360";  break;
252	case 720:  suffix = ".720";  break;
253	case 800:  suffix = ".800";  break;
254	case 820:  suffix = ".820";  break;
255	case 1200: suffix = ".1200"; break;
256	case 1440: suffix = ".1440"; break;
257	case 1480: suffix = ".1480"; break;
258	case 1720: suffix = ".1720"; break;
259	}
260
261	devname = makename(argv[optind], suffix);
262
263	if((fd = open(devname, O_RDWR)) < 0) {
264		perror(devname);
265		exit(1);
266	}
267
268	if(ioctl(fd, FD_GTYPE, &fdt) < 0) {
269		fprintf(stderr, "fdformat: not a floppy disk: %s\n", devname);
270		exit(1);
271	}
272
273	switch(rate) {
274	case -1:  break;
275	case 250: fdt.trans = FDC_250KBPS; break;
276	case 300: fdt.trans = FDC_300KBPS; break;
277	case 500: fdt.trans = FDC_500KBPS; break;
278	default:
279		fprintf(stderr, "fdformat: invalid transfer rate: %d\n", rate);
280		exit(2);
281	}
282
283	if (cyls >= 0)    fdt.tracks = cyls;
284	if (secs >= 0)    fdt.sectrac = secs;
285	if (fdt.sectrac > FD_MAX_NSEC) {
286		fprintf(stderr, "fdformat: too many sectors per track, max value is %d\n", FD_MAX_NSEC);
287		exit(2);
288	}
289	if (heads >= 0)   fdt.heads = heads;
290	if (gaplen >= 0)  fdt.f_gap = gaplen;
291	if (secsize >= 0) fdt.secsize = secsize;
292	if (steps >= 0)   fdt.steptrac = steps;
293	if (intleave >= 0) fdt.f_inter = intleave;
294
295	bytes_per_track = fdt.sectrac * (1<<fdt.secsize) * 128;
296	tracks_per_dot = fdt.tracks * fdt.heads / 40;
297
298	if (verify_only) {
299		if(!quiet)
300			printf("Verify %dK floppy `%s'.\n",
301				fdt.tracks * fdt.heads * bytes_per_track / 1024,
302				devname);
303	}
304	else if(!quiet) {
305		printf("Format %dK floppy `%s'? (y/n): ",
306			fdt.tracks * fdt.heads * bytes_per_track / 1024,
307			devname);
308		if(! yes ()) {
309			printf("Not confirmed.\n");
310			return 0;
311		}
312	}
313
314	/*
315	 * Formatting.
316	 */
317	if(!quiet) {
318		printf("Processing ----------------------------------------\r");
319		printf("Processing ");
320		fflush(stdout);
321	}
322
323	error = errs = 0;
324
325	for (track = 0; track < fdt.tracks * fdt.heads; track++) {
326		if (!verify_only) {
327			format_track(fd, track / 2, fdt.sectrac, track & 1,
328				fdt.trans, fdt.f_gap, fdt.secsize, fill,
329				fdt.f_inter);
330			if(!quiet && !((track + 1) % tracks_per_dot)) {
331				putchar('F');
332				fflush(stdout);
333			}
334		}
335		if (verify) {
336			if (verify_track(fd, track, bytes_per_track) < 0)
337				error = errs = 1;
338			if(!quiet && !((track + 1) % tracks_per_dot)) {
339				if (!verify_only)
340					putchar('\b');
341				if (error) {
342					putchar('E');
343					error = 0;
344				}
345				else
346					putchar('V');
347				fflush(stdout);
348			}
349		}
350	}
351	if(!quiet)
352		printf(" done.\n");
353
354	return errs;
355}
356