xref: /netbsd/usr.bin/fdformat/fdformat.c (revision bf9ec67e)
1 /*	$NetBSD: fdformat.c,v 1.9 2001/03/28 03:17:41 simonb Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by John Kohl.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *	  Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * fdformat: format a floppy diskette, using interface provided in
41  * <sys/fdio.h>
42  */
43 #include <sys/types.h>
44 #include <sys/fdio.h>
45 #include <sys/ioctl.h>
46 
47 #include <err.h>
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <limits.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <unistd.h>
54 #include "pathnames.h"
55 
56 char *fdb_array[2] = {_PATH_FLOPPYTAB, 0};
57 
58 #define MASK_NBPS	0x0001
59 #define MASK_NCYL	0x0002
60 #define MASK_NSPT	0x0004
61 #define MASK_NTRK	0x0008
62 #define MASK_STEPSPERCYL	0x0010
63 #define MASK_GAPLEN	0x0020
64 #define MASK_FILLBYTE	0x0040
65 #define MASK_XFER_RATE	0x0080
66 #define MASK_INTERLEAVE	0x0100
67 
68 #define ALLPARMS (MASK_NBPS|MASK_NCYL|MASK_NSPT|MASK_NTRK|MASK_STEPSPERCYL|MASK_GAPLEN|MASK_FILLBYTE|MASK_XFER_RATE|MASK_INTERLEAVE)
69 
70 int	confirm(int);
71 int	main __P((int, char **));
72 void	usage __P((void));
73 int	verify_track(int, int, int, struct fdformat_parms *, char *);
74 
75 int
76 confirm(int def)
77 {
78 	int ch;
79 
80 	printf(" Yes/no [%c]?", def ? 'y' : 'n');
81 	ch = getchar();
82 	switch (ch) {
83 	case 'y':
84 	case 'Y':
85 		return 1;
86 	case '\n':
87 		return def;
88 	case EOF:
89 	case 'n':
90 	case 'N':
91 	default:
92 		return 0;
93 	}
94 }
95 
96 int
97 verify_track(int fd, int cyl, int trk, struct fdformat_parms *parms, char *buf)
98 {
99 	size_t tracksize;
100 	off_t offset;
101 
102 	tracksize = parms->nbps * parms->nspt; /* bytes per track */
103 	offset = tracksize * (cyl * parms->ntrk + trk); /* track offset */
104 
105 	if (lseek(fd, offset, SEEK_SET) == (off_t) -1) {
106 		putchar('E');
107 		return 1;
108 	}
109 	if (read(fd, buf, tracksize) != tracksize) {
110 		putchar('E');
111 		return 1;
112 	} else
113 		putchar('V');
114 	return 0;
115 }
116 
117 void
118 usage(void)
119 {
120 
121 	fprintf(stderr,
122 	    "Usage: %s [-f device] [-t type] [-n] [-B nbps] [-S nspt]\n"
123 	    "\t[-T ntrk] [-C ncyl] [-P stepspercyl] [-G gaplen]\n"
124 	    "\t[-F fillbyte] [-X xfer_rate] [-I interleave]\n", getprogname());
125 	exit(1);
126 }
127 
128 #define numarg(which, maskn) \
129  tmplong = strtol(optarg, &tmpcharp, 0); \
130  if (*tmpcharp != '\0' || tmplong <= 0 || tmplong == LONG_MIN || tmplong == LONG_MAX) \
131      errx(1, "invalid numerical argument %s for " # which, optarg); \
132  parms.which = tmplong; \
133  parmmask |= MASK_##maskn
134 
135 #define getparm(structname, maskname) \
136 	if (cgetnum(fdbuf, # structname, &tmplong) == -1) \
137 		errx(1, "parameter " # structname " missing for type %s", \
138 		     optarg); \
139 	parms.structname = tmplong; \
140 	parmmask |= MASK_ ## maskname
141 
142 #define copyparm(which, mask) \
143 	if ((parmmask & MASK_##mask) == 0) \
144 		parms.which = fetchparms.which
145 
146 int
147 main(int argc, char *argv[])
148 {
149 	char *fdbuf = NULL, *trackbuf = NULL;
150 	int errcnt = 0;
151 	int verify = 1;
152 	int ch;
153 	long tmplong;
154 	int tmpint;
155 	char *tmpcharp;
156 	int parmmask = 0;
157 	struct fdformat_parms parms, fetchparms;
158 	struct fdformat_cmd cmd;
159 	char *filename = _PATH_FLOPPY_DEV;
160 	int fd;
161 	int trk, cyl;
162 
163 	while ((ch = getopt(argc, argv, "f:t:nB:C:S:T:P:G:F:X:I:")) != -1)
164 		switch (ch) {
165 		case 't':		/* disk type */
166 			switch (cgetent(&fdbuf, fdb_array, optarg)) {
167 			case 0:
168 				break;
169 			case 1:
170 			case -3:
171 				errx(1, "tc= loop or missing entry entry in "
172 				     _PATH_FLOPPYTAB " for type %s", optarg);
173 				break;
174 			case -1:
175 				errx(1, "unknown floppy disk type %s", optarg);
176 				break;
177 			default:
178 				err(1, "problem accessing " _PATH_FLOPPYTAB);
179 				break;
180 			}
181 
182 			getparm(nbps, NBPS);
183 			getparm(ncyl, NCYL);
184 			getparm(nspt, NSPT);
185 			getparm(ntrk, NTRK);
186 			getparm(stepspercyl, STEPSPERCYL);
187 			getparm(gaplen, GAPLEN);
188 			getparm(fillbyte, FILLBYTE);
189 			getparm(xfer_rate, XFER_RATE);
190 			getparm(interleave, INTERLEAVE);
191 			break;
192 		case 'f':		/* device name */
193 			filename = optarg;
194 			break;
195 		case 'n':		/* no verify */
196 			verify = 0;
197 			break;
198 		case 'B':
199 			numarg(nbps, NBPS);
200 			break;
201 		case 'C':
202 			numarg(ncyl, NCYL);
203 			break;
204 		case 'S':
205 			numarg(nspt, NSPT);
206 			break;
207 		case 'T':
208 			numarg(ntrk, NTRK);
209 			break;
210 		case 'P':
211 			numarg(stepspercyl, STEPSPERCYL);
212 			break;
213 		case 'G':
214 			numarg(gaplen, GAPLEN);
215 			break;
216 		case 'F':		/* special handling--0 is OK */
217 			/*numarg(fillbyte, FILLBYTE);*/
218 			tmplong = strtol(optarg, &tmpcharp, 0);
219 			if (*tmpcharp != '\0' || tmplong < 0 ||
220 			    tmplong == LONG_MIN || tmplong == LONG_MAX)
221 				errx(1, "invalid numerical argument %s for fillbyte", optarg);
222 			parms.fillbyte = tmplong;
223 			parmmask |= MASK_FILLBYTE;
224 			break;
225 		case 'X':
226 			numarg(xfer_rate, XFER_RATE);
227 			break;
228 		case 'I':
229 			numarg(interleave, INTERLEAVE);
230 			break;
231 		case '?':
232 		default:
233 			usage();
234 		}
235 	if (optind < argc)
236 		usage();
237 
238 	fd = open(filename, O_RDWR);
239 	if (fd == -1)
240 		err(1, "cannot open %s", filename);
241 	if (ioctl(fd, FDIOCGETFORMAT, &fetchparms) == -1) {
242 		if (errno == ENOTTY)
243 			err(1, "device %s does not support floppy formatting",
244 			    filename);
245 		else
246 			err(1, "cannot fetch current floppy formatting parameters");
247 	}
248 
249 	copyparm(nbps, NBPS);
250 	copyparm(ncyl, NCYL);
251 	copyparm(nspt, NSPT);
252 	copyparm(ntrk, NTRK);
253 	copyparm(stepspercyl, STEPSPERCYL);
254 	copyparm(gaplen, GAPLEN);
255 	copyparm(fillbyte, FILLBYTE);
256 	copyparm(xfer_rate, XFER_RATE);
257 	copyparm(interleave, INTERLEAVE);
258 
259 	parms.fdformat_version = FDFORMAT_VERSION;
260 
261 	tmpint = FDOPT_NORETRY|FDOPT_SILENT;
262 	if (ioctl(fd, FDIOCSETOPTS, &tmpint) == -1 ||
263 	    ioctl(fd, FDIOCSETFORMAT, &parms) == -1) {
264 		warn("cannot set requested formatting parameters");
265 		errx(1, "%d cylinders, %d tracks, %d sectors of %d bytes",
266 		     parms.ncyl, parms.ntrk, parms.nspt, parms.nbps);
267 	}
268 
269 	printf("Ready to format %s with %d cylinders, %d tracks, %d sectors of %d bytes\n(%d KB)",
270 	       filename, parms.ncyl, parms.ntrk, parms.nspt, parms.nbps,
271 	       parms.ncyl * parms.ntrk * parms.nspt * parms.nbps / 1024);
272 	if (!confirm(1))
273 		errx(1,"formatting abandoned--not confirmed.");
274 
275 	if (verify)
276 		trackbuf = malloc(parms.nbps * parms.nspt);
277 
278 	cmd.formatcmd_version = FDFORMAT_VERSION;
279 	for (cyl = 0; cyl < parms.ncyl; cyl++) {
280 		cmd.cylinder = cyl;
281 		for (trk = 0; trk < parms.ntrk; trk++) {
282 			cmd.head = trk;
283 			if (ioctl(fd, FDIOCFORMAT_TRACK, &cmd) == 0) {
284 				putchar('F');
285 				if (verify)
286 					putchar('\b');
287 				fflush(stdout);
288 				if (verify)
289 					errcnt += verify_track(fd, cyl, trk,
290 							       &parms,
291 							       trackbuf);
292 			} else if (errno == EINVAL) {
293 				putchar('\n');
294 				errx(1, "formatting botch at <%d,%d>",
295 				     cyl, trk);
296 			} else if (errno == EIO) {
297 				putchar('E');
298 				fflush(stdout);
299 				errcnt++;
300 			}
301 		}
302 	}
303 	putchar('\n');
304 	if (errcnt)
305 		errx(1,"%d track formatting error%s", errcnt, errcnt==1?"":"s");
306 	return 0;
307 }
308