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