xref: /openbsd-src/usr.sbin/fdformat/fdformat.c (revision cf2525843d483a385de106a1361b2b9c18d96583)
1 /*	$OpenBSD: fdformat.c,v 1.15 2006/08/07 18:20:57 miod 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 <ctype.h>
49 #include <err.h>
50 #include <util.h>
51 
52 #include <errno.h>
53 #include <sys/types.h>
54 #include <sys/ioctl.h>
55 #include <machine/ioctl_fd.h>
56 
57 extern const char *__progname;
58 
59 static void
60 format_track(int fd, int cyl, int secs, int head, int rate, int gaplen,
61     int secsize, int fill, int interleave)
62 {
63 	struct fd_formb f;
64 	int i,j;
65 	int il[FD_MAX_NSEC + 1];
66 
67 	memset(il,0,sizeof il);
68 	for(j = 0, i = 1; i <= secs; i++) {
69 		while(il[(j%secs)+1])
70 			j++;
71 		il[(j%secs)+1] = i;
72 		j += interleave;
73         }
74 
75 	f.format_version = FD_FORMAT_VERSION;
76 	f.head = head;
77 	f.cyl = cyl;
78 	f.transfer_rate = rate;
79 
80 	f.fd_formb_secshift = secsize;
81 	f.fd_formb_nsecs = secs;
82 	f.fd_formb_gaplen = gaplen;
83 	f.fd_formb_fillbyte = fill;
84 	for(i = 0; i < secs; i++) {
85 		f.fd_formb_cylno(i) = cyl;
86 		f.fd_formb_headno(i) = head;
87 		f.fd_formb_secno(i) = il[i+1];
88 		f.fd_formb_secsize(i) = secsize;
89 	}
90 	if (ioctl(fd, FD_FORM, (caddr_t)&f) < 0)
91 		err(1, "FD_FORM");
92 }
93 
94 static int
95 verify_track(int fd, int track, int tracksize)
96 {
97 	static char *buf = 0;
98 	static int bufsz = 0;
99 	int fdopts = -1, ofdopts, rv = 0;
100 
101 	if (ioctl(fd, FD_GOPTS, &fdopts) < 0)
102 		warn("FD_GOPTS");
103 	else {
104 		ofdopts = fdopts;
105 		fdopts |= FDOPT_NORETRY;
106 		(void)ioctl(fd, FD_SOPTS, &fdopts);
107 	}
108 
109 	if (bufsz < tracksize) {
110 		if (buf)
111 			free (buf);
112 		bufsz = tracksize;
113 		buf = 0;
114 	}
115 	if (! buf)
116 		buf = malloc (bufsz);
117 	if (! buf) {
118 		fprintf (stderr, "\nfdformat: out of memory\n");
119 		exit (2);
120 	}
121 	if (lseek (fd, (off_t) track*tracksize, SEEK_SET) < 0)
122 		rv = -1;
123 	/* try twice reading it, without using the normal retrier */
124 	else if (read (fd, buf, tracksize) != tracksize
125 		 && read (fd, buf, tracksize) != tracksize)
126 		rv = -1;
127 	if (fdopts != -1)
128 		(void)ioctl(fd, FD_SOPTS, &ofdopts);
129 	return (rv);
130 }
131 
132 static void
133 usage(void)
134 {
135 	printf("Usage:\n\t%s [-q] [-n | -v] [-c #] [-s #] [-h #]\n",
136 		__progname);
137 	printf("\t\t [-r #] [-g #] [-i #] [-S #] [-F #] [-t #] devname\n");
138 	printf("Options:\n");
139 	printf("\t-q\tsupress any normal output, don't ask for confirmation\n");
140 	printf("\t-n\tdon't verify floppy after formatting\n");
141 	printf("\t-v\tdon't format, verify only\n");
142 	printf("\tdevname\tthe full name of floppy device or in short form fd0, fd1\n");
143 	printf("Obscure options:\n");
144 	printf("\t-c #\tspecify number of cylinders, 40 or 80\n");
145 	printf("\t-s #\tspecify number of sectors per track, 9, 10, 15 or 18\n");
146 	printf("\t-h #\tspecify number of floppy heads, 1 or 2\n");
147 	printf("\t-r #\tspecify data rate, 250, 300 or 500 kbps\n");
148 	printf("\t-g #\tspecify gap length\n");
149 	printf("\t-i #\tspecify interleave factor\n");
150 	printf("\t-S #\tspecify sector size, 0=128, 1=256, 2=512 bytes\n");
151 	printf("\t-F #\tspecify fill byte\n");
152 	printf("\t-t #\tnumber of steps per track\n");
153 	exit(2);
154 }
155 
156 static int
157 yes(void)
158 {
159 	char reply [256], *p;
160 
161 	reply[sizeof(reply)-1] = 0;
162 	for (;;) {
163 		fflush(stdout);
164 		if (! fgets (reply, sizeof(reply)-1, stdin))
165 			return (0);
166 		for (p=reply; *p==' ' || *p=='\t'; ++p)
167 			continue;
168 		if (*p=='y' || *p=='Y')
169 			return (1);
170 		if (*p=='n' || *p=='N' || *p=='\n' || *p=='\r')
171 			return (0);
172 		printf("Answer `yes' or `no': ");
173 	}
174 }
175 
176 int
177 main(int argc, char *argv[])
178 {
179 	int cyls = -1, secs = -1, heads = -1, intleave = -1;
180 	int rate = -1, gaplen = -1, secsize = -1, steps = -1;
181 	int fill = 0xf6, quiet = 0, verify = 1, verify_only = 0;
182 	int fd, c, track, error, tracks_per_dot, bytes_per_track, errs;
183 	char *devname;
184 	struct fd_type fdt;
185 
186 	while((c = getopt(argc, argv, "c:s:h:r:g:S:F:t:i:qvn")) != -1)
187 		switch (c) {
188 		case 'c':       /* # of cyls */
189 			cyls = atoi(optarg);
190 			break;
191 
192 		case 's':       /* # of secs per track */
193 			secs = atoi(optarg);
194 			break;
195 
196 		case 'h':       /* # of heads */
197 			heads = atoi(optarg);
198 			break;
199 
200 		case 'r':       /* transfer rate, kilobyte/sec */
201 			rate = atoi(optarg);
202 			break;
203 
204 		case 'g':       /* length of GAP3 to format with */
205 			gaplen = atoi(optarg);
206 			break;
207 
208 		case 'S':       /* sector size shift factor (1 << S)*128 */
209 			secsize = atoi(optarg);
210 			break;
211 
212 		case 'F':       /* fill byte, C-like notation allowed */
213 			fill = (int)strtol(optarg, (char **)0, 0);
214 			break;
215 
216 		case 't':       /* steps per track */
217 			steps = atoi(optarg);
218 			break;
219 
220 		case 'i':       /* interleave factor */
221 			intleave = atoi(optarg);
222 			break;
223 
224 		case 'q':
225 			quiet = 1;
226 			break;
227 
228 		case 'n':
229 			verify = 0;
230 			break;
231 
232 		case 'v':
233 			verify = 1;
234 			verify_only = 1;
235 			break;
236 
237 		case '?': default:
238 			usage();
239 		}
240 
241 	if (optind != argc - 1)
242 		usage();
243 
244 	if ((fd = opendev(argv[optind], O_RDWR, OPENDEV_PART, &devname)) < 0)
245 		err(1, "%s", devname);
246 
247 	if (ioctl(fd, FD_GTYPE, &fdt) < 0)
248 		errx(1, "not a floppy disk: %s", devname);
249 
250 	switch (rate) {
251 	case -1:
252 		break;
253 	case 250:
254 		fdt.rate = FDC_250KBPS;
255 		break;
256 	case 300:
257 		fdt.rate = FDC_300KBPS;
258 		break;
259 	case 500:
260 		fdt.rate = FDC_500KBPS;
261 		break;
262 	default:
263 		errx(1, "invalid transfer rate: %d", rate);
264 	}
265 
266 	if (cyls >= 0)
267 		fdt.tracks = cyls;
268 	if (secs >= 0)
269 		fdt.sectrac = secs;
270 	if (fdt.sectrac > FD_MAX_NSEC)
271 		errx(1, "too many sectors per track, max value is %d",
272 			FD_MAX_NSEC);
273 	if (heads >= 0)
274 		fdt.heads = heads;
275 	if (gaplen >= 0)
276 		fdt.gap2 = gaplen;
277 	if (secsize >= 0)
278 		fdt.secsize = secsize;
279 	if (steps >= 0)
280 		fdt.step = steps;
281 
282 	bytes_per_track = fdt.sectrac * (1<<fdt.secsize) * 128;
283 	tracks_per_dot = fdt.tracks * fdt.heads / 40;
284 	if (tracks_per_dot == 0)
285 		tracks_per_dot++;
286 
287 	if (verify_only) {
288 		if (!quiet)
289 			printf("Verify %dK floppy `%s'.\n",
290 				fdt.tracks * fdt.heads * bytes_per_track / 1024,
291 				devname);
292 	}
293 	else if (!quiet) {
294 		printf("Format %dK floppy `%s'? (y/n): ",
295 			fdt.tracks * fdt.heads * bytes_per_track / 1024,
296 			devname);
297 		if (!yes()) {
298 			printf("Not confirmed.\n");
299 			exit(0);
300 		}
301 	}
302 
303 	/*
304 	 * Formatting.
305 	 */
306 	if (!quiet) {
307 		printf("Processing ");
308 		for (track = 0; track < fdt.tracks * fdt.heads; track++) {
309 			if (!((track + 1) % tracks_per_dot))
310 				putchar('-');
311 		}
312 		putchar('\r');
313 		printf("Processing ");
314 		fflush(stdout);
315 	}
316 
317 	error = errs = 0;
318 
319 	for (track = 0; track < fdt.tracks * fdt.heads; track++) {
320 		if (!verify_only) {
321 			format_track(fd, track / fdt.heads, fdt.sectrac,
322 				track % fdt.heads, fdt.rate, fdt.gap2,
323 				     fdt.secsize, fill,
324 				     intleave >= 0 ? intleave : 1);
325 			if (!quiet && !((track + 1) % tracks_per_dot)) {
326 				putchar('F');
327 				fflush(stdout);
328 			}
329 		}
330 		if (verify) {
331 			if (verify_track(fd, track, bytes_per_track) < 0)
332 				error = errs = 1;
333 			if (!quiet && !((track + 1) % tracks_per_dot)) {
334 				if (!verify_only)
335 					putchar('\b');
336 				if (error) {
337 					putchar('E');
338 					error = 0;
339 				}
340 				else
341 					putchar('V');
342 				fflush(stdout);
343 			}
344 		}
345 	}
346 	close(fd);
347 	if (!quiet)
348 		printf(" done.\n");
349 
350 	exit(errs);
351 }
352