xref: /netbsd-src/usr.bin/fdformat/fdformat.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: fdformat.c,v 1.6 1999/01/13 21:00:16 hubertf 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 	extern char *__progname;
121 
122 	errx(1,"Usage: %s [-f device] [-t type] [-n] [-B nbps] [-S nspt]\n\t[-T ntrk] [-C ncyl] [-P stepspercyl] [-G gaplen]\n\t[-F fillbyte] [-X xfer_rate] [-I interleave]\n", __progname);
123 }
124 
125 #define numarg(which, maskn) \
126  tmplong = strtol(optarg, &tmpcharp, 0); \
127  if (*tmpcharp != '\0' || tmplong <= 0 || tmplong == LONG_MIN || tmplong == LONG_MAX) \
128      errx(1, "invalid numerical argument %s for " # which, optarg); \
129  parms.which = tmplong; \
130  parmmask |= MASK_##maskn
131 
132 #define getparm(structname, maskname) \
133 	if (cgetnum(fdbuf, # structname, &tmplong) == -1) \
134 		errx(1, "parameter " # structname " missing for type %s", \
135 		     optarg); \
136 	parms.structname = tmplong; \
137 	parmmask |= MASK_ ## maskname
138 
139 #define copyparm(which, mask) \
140 	if ((parmmask & MASK_##mask) == 0) \
141 		parms.which = fetchparms.which
142 
143 int
144 main(int argc, char *argv[])
145 {
146 	char *fdbuf = NULL, *trackbuf = NULL;
147 	int errcnt = 0;
148 	int verify = 1;
149 	int ch;
150 	long tmplong;
151 	int tmpint;
152 	char *tmpcharp;
153 	int parmmask = 0;
154 	struct fdformat_parms parms, fetchparms;
155 	struct fdformat_cmd cmd;
156 	char *filename = _PATH_FLOPPY_DEV;
157 	int fd;
158 	int trk, cyl;
159 
160 	while ((ch = getopt(argc, argv, "f:t:nB:C:S:T:P:G:F:X:I:")) != -1)
161 		switch (ch) {
162 		case 't':		/* disk type */
163 			switch (cgetent(&fdbuf, fdb_array, optarg)) {
164 			case 0:
165 				break;
166 			case 1:
167 			case -3:
168 				errx(1, "tc= loop or missing entry entry in "
169 				     _PATH_FLOPPYTAB " for type %s", optarg);
170 				break;
171 			case -1:
172 				errx(1, "unknown floppy disk type %s", optarg);
173 				break;
174 			default:
175 				err(1, "problem accessing " _PATH_FLOPPYTAB);
176 				break;
177 			}
178 
179 			getparm(nbps, NBPS);
180 			getparm(ncyl, NCYL);
181 			getparm(nspt, NSPT);
182 			getparm(ntrk, NTRK);
183 			getparm(stepspercyl, STEPSPERCYL);
184 			getparm(gaplen, GAPLEN);
185 			getparm(fillbyte, FILLBYTE);
186 			getparm(xfer_rate, XFER_RATE);
187 			getparm(interleave, INTERLEAVE);
188 			break;
189 		case 'f':		/* device name */
190 			filename = optarg;
191 			break;
192 		case 'n':		/* no verify */
193 			verify = 0;
194 			break;
195 		case 'B':
196 			numarg(nbps, NBPS);
197 			break;
198 		case 'C':
199 			numarg(ncyl, NCYL);
200 			break;
201 		case 'S':
202 			numarg(nspt, NSPT);
203 			break;
204 		case 'T':
205 			numarg(ntrk, NTRK);
206 			break;
207 		case 'P':
208 			numarg(stepspercyl, STEPSPERCYL);
209 			break;
210 		case 'G':
211 			numarg(gaplen, GAPLEN);
212 			break;
213 		case 'F':		/* special handling--0 is OK */
214 			/*numarg(fillbyte, FILLBYTE);*/
215 			tmplong = strtol(optarg, &tmpcharp, 0);
216 			if (*tmpcharp != '\0' || tmplong < 0 ||
217 			    tmplong == LONG_MIN || tmplong == LONG_MAX)
218 				errx(1, "invalid numerical argument %s for fillbyte", optarg);
219 			parms.fillbyte = tmplong;
220 			parmmask |= MASK_FILLBYTE;
221 			break;
222 		case 'X':
223 			numarg(xfer_rate, XFER_RATE);
224 			break;
225 		case 'I':
226 			numarg(interleave, INTERLEAVE);
227 			break;
228 		case '?':
229 		default:
230 			usage();
231 		}
232 	if (optind < argc)
233 		usage();
234 
235 	fd = open(filename, O_RDWR);
236 	if (fd == -1)
237 		err(1, "cannot open %s", filename);
238 	if (ioctl(fd, FDIOCGETFORMAT, &fetchparms) == -1) {
239 		if (errno == ENOTTY)
240 			err(1, "device %s does not support floppy formatting",
241 			    filename);
242 		else
243 			err(1, "cannot fetch current floppy formatting parameters");
244 	}
245 
246 	copyparm(nbps, NBPS);
247 	copyparm(ncyl, NCYL);
248 	copyparm(nspt, NSPT);
249 	copyparm(ntrk, NTRK);
250 	copyparm(stepspercyl, STEPSPERCYL);
251 	copyparm(gaplen, GAPLEN);
252 	copyparm(fillbyte, FILLBYTE);
253 	copyparm(xfer_rate, XFER_RATE);
254 	copyparm(interleave, INTERLEAVE);
255 
256 	parms.fdformat_version = FDFORMAT_VERSION;
257 
258 	tmpint = FDOPT_NORETRY|FDOPT_SILENT;
259 	if (ioctl(fd, FDIOCSETOPTS, &tmpint) == -1 ||
260 	    ioctl(fd, FDIOCSETFORMAT, &parms) == -1) {
261 		warn("cannot set requested formatting parameters");
262 		errx(1, "%d cylinders, %d tracks, %d sectors of %d bytes",
263 		     parms.ncyl, parms.ntrk, parms.nspt, parms.nbps);
264 	}
265 
266 	printf("Ready to format %s with %d cylinders, %d tracks, %d sectors of %d bytes\n(%d KB)",
267 	       filename, parms.ncyl, parms.ntrk, parms.nspt, parms.nbps,
268 	       parms.ncyl * parms.ntrk * parms.nspt * parms.nbps / 1024);
269 	if (!confirm(1))
270 		errx(1,"formatting abandoned--not confirmed.");
271 
272 	if (verify)
273 		trackbuf = malloc(parms.nbps * parms.nspt);
274 
275 	cmd.formatcmd_version = FDFORMAT_VERSION;
276 	for (cyl = 0; cyl < parms.ncyl; cyl++) {
277 		cmd.cylinder = cyl;
278 		for (trk = 0; trk < parms.ntrk; trk++) {
279 			cmd.head = trk;
280 			if (ioctl(fd, FDIOCFORMAT_TRACK, &cmd) == 0) {
281 				putchar('F');
282 				if (verify)
283 					putchar('\b');
284 				fflush(stdout);
285 				if (verify)
286 					errcnt += verify_track(fd, cyl, trk,
287 							       &parms,
288 							       trackbuf);
289 			} else if (errno == EINVAL) {
290 				putchar('\n');
291 				errx(1, "formatting botch at <%d,%d>",
292 				     cyl, trk);
293 			} else if (errno == EIO) {
294 				putchar('E');
295 				fflush(stdout);
296 				errcnt++;
297 			}
298 		}
299 	}
300 	putchar('\n');
301 	if (errcnt)
302 		errx(1,"%d track formatting error%s", errcnt, errcnt==1?"":"s");
303 	return 0;
304 }
305