fdformat.c revision 1.12 1 /* $NetBSD: fdformat.c,v 1.12 2004/04/23 15:04:27 christos 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.12 2004/04/23 15:04:27 christos 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 static const 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 static int confirm(int);
77 static void usage(void) __attribute__((__noreturn__));
78 static int verify_track(int, int, int, struct fdformat_parms *, char *);
79 static int wincolsize(void);
80
81 int main(int, char **);
82
83 static int
84 confirm(int def)
85 {
86 int ch;
87
88 (void)printf(" Yes/no [%c]?", def ? 'y' : 'n');
89 ch = getchar();
90 switch (ch) {
91 case 'y':
92 case 'Y':
93 return 1;
94 case '\n':
95 return def;
96 case EOF:
97 case 'n':
98 case 'N':
99 default:
100 return 0;
101 }
102 }
103
104 static int
105 verify_track(int fd, int cyl, int trk, struct fdformat_parms *parms, char *buf)
106 {
107 size_t tracksize;
108 off_t offset;
109
110 tracksize = parms->nbps * parms->nspt; /* bytes per track */
111 offset = tracksize * (cyl * parms->ntrk + trk); /* track offset */
112
113 if (lseek(fd, offset, SEEK_SET) == (off_t) -1) {
114 (void)putchar('E');
115 return 1;
116 }
117 if (read(fd, buf, tracksize) != tracksize) {
118 (void)putchar('E');
119 return 1;
120 } else
121 (void)putchar('V');
122 return 0;
123 }
124
125 static void
126 usage(void)
127 {
128 (void)fprintf(stderr,
129 "Usage: %s [-f device] [-t type] [-n] [-B nbps] [-S nspt]\n"
130 "\t[-T ntrk] [-C ncyl] [-P stepspercyl] [-G gaplen]\n"
131 "\t[-F fillbyte] [-X xfer_rate] [-I interleave]\n", getprogname());
132 exit(1);
133 }
134
135 static int
136 wincolsize(void)
137 {
138 struct winsize win;
139
140 if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1) {
141 warn("TIOCGWINSZ");
142 return 80;
143 }
144 return win.ws_col;
145 }
146
147 #define numarg(which, maskn, op) \
148 do { \
149 tmplong = strtol(optarg, &tmpcharp, 0); \
150 if (*tmpcharp != '\0' || tmplong op 0) \
151 errx(1, \
152 "Invalid numerical argument `%s' for " \
153 # which, optarg); \
154 if (errno == ERANGE && (tmplong == LONG_MIN || \
155 tmplong == LONG_MAX)) \
156 err(1, \
157 "Bad numerical argument `%s' for " \
158 # which, optarg); \
159 parms.which = tmplong; \
160 parmmask |= MASK_##maskn; \
161 } while (/* CONSTCOND */0)
162
163 #define getparm(structname, maskname) \
164 do { \
165 if (cgetnum(fdbuf, # structname, &tmplong) == -1) \
166 errx(1, "Parameter " # structname \
167 " missing for type `%s'", optarg); \
168 parms.structname = tmplong; \
169 parmmask |= MASK_ ## maskname; \
170 } while (/* CONSTCOND */0)
171
172
173 #define copyparm(which, mask) \
174 if ((parmmask & MASK_##mask) == 0) \
175 parms.which = fetchparms.which
176
177 int
178 main(int argc, char *argv[])
179 {
180 char *fdbuf = NULL, *trackbuf = NULL;
181 int errcnt = 0;
182 int verify = 1;
183 int ch;
184 long tmplong;
185 int tmpint;
186 char *tmpcharp;
187 int parmmask = 0;
188 struct fdformat_parms parms, fetchparms;
189 struct fdformat_cmd cmd;
190 const char *filename = _PATH_FLOPPY_DEV;
191 int fd;
192 int trk, cyl;
193 int colcnt = 0;
194 int colsize = wincolsize();
195
196 while ((ch = getopt(argc, argv, "f:t:nB:C:S:T:P:G:F:X:I:")) != -1)
197 switch (ch) {
198 case 't': /* disk type */
199 switch (cgetent(&fdbuf, fdb_array, optarg)) {
200 case 0:
201 break;
202 case 1:
203 case -3:
204 errx(1, "tc= loop or missing entry entry in "
205 _PATH_FLOPPYTAB " for type %s", optarg);
206 break;
207 case -1:
208 errx(1, "Unknown floppy disk type %s", optarg);
209 break;
210 default:
211 err(1, "Problem accessing " _PATH_FLOPPYTAB);
212 break;
213 }
214
215 getparm(nbps, NBPS);
216 getparm(ncyl, NCYL);
217 getparm(nspt, NSPT);
218 getparm(ntrk, NTRK);
219 getparm(stepspercyl, STEPSPERCYL);
220 getparm(gaplen, GAPLEN);
221 getparm(fillbyte, FILLBYTE);
222 getparm(xfer_rate, XFER_RATE);
223 getparm(interleave, INTERLEAVE);
224 break;
225 case 'f': /* device name */
226 filename = optarg;
227 break;
228 case 'n': /* no verify */
229 verify = 0;
230 break;
231 case 'B':
232 numarg(nbps, NBPS, <=);
233 break;
234 case 'C':
235 numarg(ncyl, NCYL, <=);
236 break;
237 case 'S':
238 numarg(nspt, NSPT, <=);
239 break;
240 case 'T':
241 numarg(ntrk, NTRK, <=);
242 break;
243 case 'P':
244 numarg(stepspercyl, STEPSPERCYL, <=);
245 break;
246 case 'G':
247 numarg(gaplen, GAPLEN, <=);
248 break;
249 case 'F':
250 numarg(fillbyte, FILLBYTE, <);
251 break;
252 case 'X':
253 numarg(xfer_rate, XFER_RATE, <=);
254 break;
255 case 'I':
256 numarg(interleave, INTERLEAVE, <=);
257 break;
258 case '?':
259 default:
260 usage();
261 }
262
263 if (optind < argc)
264 usage();
265
266 fd = open(filename, O_RDWR);
267 if (fd == -1)
268 err(1, "Cannot open %s", filename);
269 if (ioctl(fd, FDIOCGETFORMAT, &fetchparms) == -1) {
270 if (errno == ENOTTY)
271 err(1, "Device `%s' does not support floppy formatting",
272 filename);
273 else
274 err(1, "Cannot fetch current floppy"
275 " formatting parameters");
276 }
277
278 copyparm(nbps, NBPS);
279 copyparm(ncyl, NCYL);
280 copyparm(nspt, NSPT);
281 copyparm(ntrk, NTRK);
282 copyparm(stepspercyl, STEPSPERCYL);
283 copyparm(gaplen, GAPLEN);
284 copyparm(fillbyte, FILLBYTE);
285 copyparm(xfer_rate, XFER_RATE);
286 copyparm(interleave, INTERLEAVE);
287
288 parms.fdformat_version = FDFORMAT_VERSION;
289
290 tmpint = FDOPT_NORETRY|FDOPT_SILENT;
291 if (ioctl(fd, FDIOCSETOPTS, &tmpint) == -1 ||
292 ioctl(fd, FDIOCSETFORMAT, &parms) == -1) {
293 errx(1, "Cannot set requested formatting parameters:"
294 " %d cylinders, %d tracks, %d sectors of %d bytes",
295 parms.ncyl, parms.ntrk, parms.nspt, parms.nbps);
296 }
297
298 (void)printf("Ready to format %s with %d cylinders, %d tracks,"
299 " %d sectors of %d bytes\n(%d KB)",
300 filename, parms.ncyl, parms.ntrk, parms.nspt, parms.nbps,
301 parms.ncyl * parms.ntrk * parms.nspt * parms.nbps / 1024);
302 if (!confirm(1))
303 errx(1,"Formatting abandoned -- not confirmed.");
304
305 if (verify) {
306 trackbuf = malloc(parms.nbps * parms.nspt);
307 if (trackbuf == NULL)
308 warn("Cannot allocate verification buffer");
309 }
310
311 cmd.formatcmd_version = FDFORMAT_VERSION;
312 for (cyl = 0; cyl < parms.ncyl; cyl++) {
313 cmd.cylinder = cyl;
314 for (trk = 0; trk < parms.ntrk; trk++) {
315 cmd.head = trk;
316 if (ioctl(fd, FDIOCFORMAT_TRACK, &cmd) == 0) {
317 if (verify && colsize) {
318 colcnt++;
319 if (colcnt % colsize == 0) {
320 (void)putchar('\n');
321 colcnt++;
322 }
323 }
324 (void)putchar('F');
325 if (verify)
326 (void)putchar('\b');
327 (void)fflush(stdout);
328 if (verify)
329 errcnt += verify_track(fd, cyl, trk,
330 &parms, trackbuf);
331 } else if (errno == EINVAL) {
332 (void)putchar('\n');
333 errx(1, "Formatting botch at <%d,%d>",
334 cyl, trk);
335 } else if (errno == EIO) {
336 (void)putchar('E');
337 (void)fflush(stdout);
338 errcnt++;
339 }
340 }
341 }
342 (void)putchar('\n');
343 if (errcnt)
344 errx(1, "%d track formatting error%s",
345 errcnt, errcnt == 1 ? "" : "s");
346 return 0;
347 }
348