xref: /netbsd-src/usr.bin/fdformat/fdformat.c (revision 001c68bd94f75ce9270b69227c4199fbf34ee396)
1 /*	$NetBSD: fdformat.c,v 1.10 2003/06/23 13:05:47 agc 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/cdefs.h>
44 
45 #ifndef lint
46 __RCSID("$NetBSD: fdformat.c,v 1.10 2003/06/23 13:05:47 agc Exp $");
47 #endif
48 
49 #include <sys/types.h>
50 #include <sys/fdio.h>
51 #include <sys/ioctl.h>
52 
53 #include <err.h>
54 #include <errno.h>
55 #include <fcntl.h>
56 #include <limits.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <unistd.h>
60 #include "pathnames.h"
61 
62 char *fdb_array[2] = {_PATH_FLOPPYTAB, 0};
63 
64 #define MASK_NBPS	0x0001
65 #define MASK_NCYL	0x0002
66 #define MASK_NSPT	0x0004
67 #define MASK_NTRK	0x0008
68 #define MASK_STEPSPERCYL	0x0010
69 #define MASK_GAPLEN	0x0020
70 #define MASK_FILLBYTE	0x0040
71 #define MASK_XFER_RATE	0x0080
72 #define MASK_INTERLEAVE	0x0100
73 
74 #define ALLPARMS (MASK_NBPS|MASK_NCYL|MASK_NSPT|MASK_NTRK|MASK_STEPSPERCYL|MASK_GAPLEN|MASK_FILLBYTE|MASK_XFER_RATE|MASK_INTERLEAVE)
75 
76 int	confirm(int);
77 int	main __P((int, char **));
78 void	usage __P((void));
79 int	verify_track(int, int, int, struct fdformat_parms *, char *);
80 
81 int
82 confirm(int def)
83 {
84 	int ch;
85 
86 	printf(" Yes/no [%c]?", def ? 'y' : 'n');
87 	ch = getchar();
88 	switch (ch) {
89 	case 'y':
90 	case 'Y':
91 		return 1;
92 	case '\n':
93 		return def;
94 	case EOF:
95 	case 'n':
96 	case 'N':
97 	default:
98 		return 0;
99 	}
100 }
101 
102 int
103 verify_track(int fd, int cyl, int trk, struct fdformat_parms *parms, char *buf)
104 {
105 	size_t tracksize;
106 	off_t offset;
107 
108 	tracksize = parms->nbps * parms->nspt; /* bytes per track */
109 	offset = tracksize * (cyl * parms->ntrk + trk); /* track offset */
110 
111 	if (lseek(fd, offset, SEEK_SET) == (off_t) -1) {
112 		putchar('E');
113 		return 1;
114 	}
115 	if (read(fd, buf, tracksize) != tracksize) {
116 		putchar('E');
117 		return 1;
118 	} else
119 		putchar('V');
120 	return 0;
121 }
122 
123 void
124 usage(void)
125 {
126 
127 	fprintf(stderr,
128 	    "Usage: %s [-f device] [-t type] [-n] [-B nbps] [-S nspt]\n"
129 	    "\t[-T ntrk] [-C ncyl] [-P stepspercyl] [-G gaplen]\n"
130 	    "\t[-F fillbyte] [-X xfer_rate] [-I interleave]\n", getprogname());
131 	exit(1);
132 }
133 
134 #define numarg(which, maskn) \
135  tmplong = strtol(optarg, &tmpcharp, 0); \
136  if (*tmpcharp != '\0' || tmplong <= 0 || tmplong == LONG_MIN || tmplong == LONG_MAX) \
137      errx(1, "invalid numerical argument %s for " # which, optarg); \
138  parms.which = tmplong; \
139  parmmask |= MASK_##maskn
140 
141 #define getparm(structname, maskname) \
142 	if (cgetnum(fdbuf, # structname, &tmplong) == -1) \
143 		errx(1, "parameter " # structname " missing for type %s", \
144 		     optarg); \
145 	parms.structname = tmplong; \
146 	parmmask |= MASK_ ## maskname
147 
148 #define copyparm(which, mask) \
149 	if ((parmmask & MASK_##mask) == 0) \
150 		parms.which = fetchparms.which
151 
152 int
153 main(int argc, char *argv[])
154 {
155 	char *fdbuf = NULL, *trackbuf = NULL;
156 	int errcnt = 0;
157 	int verify = 1;
158 	int ch;
159 	long tmplong;
160 	int tmpint;
161 	char *tmpcharp;
162 	int parmmask = 0;
163 	struct fdformat_parms parms, fetchparms;
164 	struct fdformat_cmd cmd;
165 	char *filename = _PATH_FLOPPY_DEV;
166 	int fd;
167 	int trk, cyl;
168 
169 	while ((ch = getopt(argc, argv, "f:t:nB:C:S:T:P:G:F:X:I:")) != -1)
170 		switch (ch) {
171 		case 't':		/* disk type */
172 			switch (cgetent(&fdbuf, fdb_array, optarg)) {
173 			case 0:
174 				break;
175 			case 1:
176 			case -3:
177 				errx(1, "tc= loop or missing entry entry in "
178 				     _PATH_FLOPPYTAB " for type %s", optarg);
179 				break;
180 			case -1:
181 				errx(1, "unknown floppy disk type %s", optarg);
182 				break;
183 			default:
184 				err(1, "problem accessing " _PATH_FLOPPYTAB);
185 				break;
186 			}
187 
188 			getparm(nbps, NBPS);
189 			getparm(ncyl, NCYL);
190 			getparm(nspt, NSPT);
191 			getparm(ntrk, NTRK);
192 			getparm(stepspercyl, STEPSPERCYL);
193 			getparm(gaplen, GAPLEN);
194 			getparm(fillbyte, FILLBYTE);
195 			getparm(xfer_rate, XFER_RATE);
196 			getparm(interleave, INTERLEAVE);
197 			break;
198 		case 'f':		/* device name */
199 			filename = optarg;
200 			break;
201 		case 'n':		/* no verify */
202 			verify = 0;
203 			break;
204 		case 'B':
205 			numarg(nbps, NBPS);
206 			break;
207 		case 'C':
208 			numarg(ncyl, NCYL);
209 			break;
210 		case 'S':
211 			numarg(nspt, NSPT);
212 			break;
213 		case 'T':
214 			numarg(ntrk, NTRK);
215 			break;
216 		case 'P':
217 			numarg(stepspercyl, STEPSPERCYL);
218 			break;
219 		case 'G':
220 			numarg(gaplen, GAPLEN);
221 			break;
222 		case 'F':		/* special handling--0 is OK */
223 			/*numarg(fillbyte, FILLBYTE);*/
224 			tmplong = strtol(optarg, &tmpcharp, 0);
225 			if (*tmpcharp != '\0' || tmplong < 0 ||
226 			    tmplong == LONG_MIN || tmplong == LONG_MAX)
227 				errx(1, "invalid numerical argument %s for fillbyte", optarg);
228 			parms.fillbyte = tmplong;
229 			parmmask |= MASK_FILLBYTE;
230 			break;
231 		case 'X':
232 			numarg(xfer_rate, XFER_RATE);
233 			break;
234 		case 'I':
235 			numarg(interleave, INTERLEAVE);
236 			break;
237 		case '?':
238 		default:
239 			usage();
240 		}
241 	if (optind < argc)
242 		usage();
243 
244 	fd = open(filename, O_RDWR);
245 	if (fd == -1)
246 		err(1, "cannot open %s", filename);
247 	if (ioctl(fd, FDIOCGETFORMAT, &fetchparms) == -1) {
248 		if (errno == ENOTTY)
249 			err(1, "device %s does not support floppy formatting",
250 			    filename);
251 		else
252 			err(1, "cannot fetch current floppy formatting parameters");
253 	}
254 
255 	copyparm(nbps, NBPS);
256 	copyparm(ncyl, NCYL);
257 	copyparm(nspt, NSPT);
258 	copyparm(ntrk, NTRK);
259 	copyparm(stepspercyl, STEPSPERCYL);
260 	copyparm(gaplen, GAPLEN);
261 	copyparm(fillbyte, FILLBYTE);
262 	copyparm(xfer_rate, XFER_RATE);
263 	copyparm(interleave, INTERLEAVE);
264 
265 	parms.fdformat_version = FDFORMAT_VERSION;
266 
267 	tmpint = FDOPT_NORETRY|FDOPT_SILENT;
268 	if (ioctl(fd, FDIOCSETOPTS, &tmpint) == -1 ||
269 	    ioctl(fd, FDIOCSETFORMAT, &parms) == -1) {
270 		warn("cannot set requested formatting parameters");
271 		errx(1, "%d cylinders, %d tracks, %d sectors of %d bytes",
272 		     parms.ncyl, parms.ntrk, parms.nspt, parms.nbps);
273 	}
274 
275 	printf("Ready to format %s with %d cylinders, %d tracks, %d sectors of %d bytes\n(%d KB)",
276 	       filename, parms.ncyl, parms.ntrk, parms.nspt, parms.nbps,
277 	       parms.ncyl * parms.ntrk * parms.nspt * parms.nbps / 1024);
278 	if (!confirm(1))
279 		errx(1,"formatting abandoned--not confirmed.");
280 
281 	if (verify)
282 		trackbuf = malloc(parms.nbps * parms.nspt);
283 
284 	cmd.formatcmd_version = FDFORMAT_VERSION;
285 	for (cyl = 0; cyl < parms.ncyl; cyl++) {
286 		cmd.cylinder = cyl;
287 		for (trk = 0; trk < parms.ntrk; trk++) {
288 			cmd.head = trk;
289 			if (ioctl(fd, FDIOCFORMAT_TRACK, &cmd) == 0) {
290 				putchar('F');
291 				if (verify)
292 					putchar('\b');
293 				fflush(stdout);
294 				if (verify)
295 					errcnt += verify_track(fd, cyl, trk,
296 							       &parms,
297 							       trackbuf);
298 			} else if (errno == EINVAL) {
299 				putchar('\n');
300 				errx(1, "formatting botch at <%d,%d>",
301 				     cyl, trk);
302 			} else if (errno == EIO) {
303 				putchar('E');
304 				fflush(stdout);
305 				errcnt++;
306 			}
307 		}
308 	}
309 	putchar('\n');
310 	if (errcnt)
311 		errx(1,"%d track formatting error%s", errcnt, errcnt==1?"":"s");
312 	return 0;
313 }
314