Sync usage with man page.
[netbsd-mini2440.git] / usr.bin / fdformat / fdformat.c
blob2d8241f07fbd8e796dc06cb5e2b8638dca9b615a
1 /* $NetBSD: fdformat.c,v 1.15 2008/04/28 20:24:12 martin Exp $ */
3 /*-
4 * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by John Kohl.
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * fdformat: format a floppy diskette, using interface provided in
34 * <sys/fdio.h>
36 #include <sys/cdefs.h>
38 #ifndef lint
39 __RCSID("$NetBSD: fdformat.c,v 1.15 2008/04/28 20:24:12 martin Exp $");
40 #endif
42 #include <sys/types.h>
43 #include <sys/fdio.h>
44 #include <sys/ioctl.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <limits.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <unistd.h>
53 #include "pathnames.h"
55 static const char *fdb_array[2] = {_PATH_FLOPPYTAB, 0};
57 #define MASK_NBPS 0x0001
58 #define MASK_NCYL 0x0002
59 #define MASK_NSPT 0x0004
60 #define MASK_NTRK 0x0008
61 #define MASK_STEPSPERCYL 0x0010
62 #define MASK_GAPLEN 0x0020
63 #define MASK_FILLBYTE 0x0040
64 #define MASK_XFER_RATE 0x0080
65 #define MASK_INTERLEAVE 0x0100
67 #define ALLPARMS (MASK_NBPS|MASK_NCYL|MASK_NSPT|MASK_NTRK|MASK_STEPSPERCYL|MASK_GAPLEN|MASK_FILLBYTE|MASK_XFER_RATE|MASK_INTERLEAVE)
69 static int confirm(int);
70 static void usage(void) __dead;
71 static int verify_track(int, int, int, struct fdformat_parms *, char *);
73 int main(int, char **);
75 static int
76 confirm(int def)
78 int ch;
80 (void)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;
96 static int
97 verify_track(int fd, int cyl, int trk, struct fdformat_parms *parms, char *buf)
99 size_t tracksize;
100 off_t offset;
102 tracksize = parms->nbps * parms->nspt; /* bytes per track */
103 offset = tracksize * (cyl * parms->ntrk + trk); /* track offset */
105 if (lseek(fd, offset, SEEK_SET) == (off_t) -1) {
106 (void)printf("- SEEK ERROR\n");
107 return 1;
109 if ((size_t)read(fd, buf, tracksize) != tracksize) {
110 (void)printf("- VERIFY ERROR\n");
111 return 1;
113 return 0;
116 static void
117 usage(void)
119 (void)fprintf(stderr,
120 "Usage: %s [-f device] [-t type] [-n] [-B nbps] [-S nspt]\n"
121 "\t[-T ntrk] [-C ncyl] [-P stepspercyl] [-G gaplen]\n"
122 "\t[-F fillbyte] [-X xfer_rate] [-I interleave]\n", getprogname());
123 exit(1);
126 #define numarg(which, maskn, op) \
127 do { \
128 tmplong = strtol(optarg, &tmpcharp, 0); \
129 if (*tmpcharp != '\0' || tmplong op 0) \
130 errx(1, \
131 "Invalid numerical argument `%s' for " \
132 # which, optarg); \
133 if (errno == ERANGE && (tmplong == LONG_MIN || \
134 tmplong == LONG_MAX)) \
135 err(1, \
136 "Bad numerical argument `%s' for " \
137 # which, optarg); \
138 parms.which = tmplong; \
139 parmmask |= MASK_##maskn; \
140 } while (/* CONSTCOND */0)
142 #define getparm(structname, maskname) \
143 do { \
144 if (cgetnum(fdbuf, # structname, &tmplong) == -1) \
145 errx(1, "Parameter " # structname \
146 " missing for type `%s'", optarg); \
147 parms.structname = tmplong; \
148 parmmask |= MASK_ ## maskname; \
149 } while (/* CONSTCOND */0)
152 #define copyparm(which, mask) \
153 if ((parmmask & MASK_##mask) == 0) \
154 parms.which = fetchparms.which
157 main(int argc, char *argv[])
159 char *fdbuf = NULL, *trackbuf = NULL;
160 int errcnt = 0;
161 int verify = 1;
162 int ch;
163 long tmplong;
164 int tmpint;
165 char *tmpcharp;
166 int parmmask = 0;
167 struct fdformat_parms parms, fetchparms;
168 struct fdformat_cmd cmd;
169 const char *filename = _PATH_FLOPPY_DEV;
170 int fd;
171 int trk, cyl;
173 while ((ch = getopt(argc, argv, "f:t:nB:C:S:T:P:G:F:X:I:")) != -1)
174 switch (ch) {
175 case 't': /* disk type */
176 switch (cgetent(&fdbuf, fdb_array, optarg)) {
177 case 0:
178 break;
179 case 1:
180 case -3:
181 errx(1, "tc= loop or missing entry entry in "
182 _PATH_FLOPPYTAB " for type %s", optarg);
183 break;
184 case -1:
185 errx(1, "Unknown floppy disk type %s", optarg);
186 break;
187 default:
188 err(1, "Problem accessing " _PATH_FLOPPYTAB);
189 break;
192 getparm(nbps, NBPS);
193 getparm(ncyl, NCYL);
194 getparm(nspt, NSPT);
195 getparm(ntrk, NTRK);
196 getparm(stepspercyl, STEPSPERCYL);
197 getparm(gaplen, GAPLEN);
198 getparm(fillbyte, FILLBYTE);
199 getparm(xfer_rate, XFER_RATE);
200 getparm(interleave, INTERLEAVE);
201 break;
202 case 'f': /* device name */
203 filename = optarg;
204 break;
205 case 'n': /* no verify */
206 verify = 0;
207 break;
208 case 'B':
209 numarg(nbps, NBPS, <=);
210 break;
211 case 'C':
212 numarg(ncyl, NCYL, <=);
213 break;
214 case 'S':
215 numarg(nspt, NSPT, <=);
216 break;
217 case 'T':
218 numarg(ntrk, NTRK, <=);
219 break;
220 case 'P':
221 numarg(stepspercyl, STEPSPERCYL, <=);
222 break;
223 case 'G':
224 numarg(gaplen, GAPLEN, <=);
225 break;
226 case 'F':
227 numarg(fillbyte, FILLBYTE, <);
228 break;
229 case 'X':
230 numarg(xfer_rate, XFER_RATE, <=);
231 break;
232 case 'I':
233 numarg(interleave, INTERLEAVE, <=);
234 break;
235 case '?':
236 default:
237 usage();
240 if (optind < argc)
241 usage();
243 fd = open(filename, O_RDWR);
244 if (fd == -1)
245 err(1, "Cannot open %s", filename);
246 if (ioctl(fd, FDIOCGETFORMAT, &fetchparms) == -1) {
247 if (errno == ENOTTY)
248 err(1, "Device `%s' does not support floppy formatting",
249 filename);
250 else
251 err(1, "Cannot fetch current floppy"
252 " formatting parameters");
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);
265 parms.fdformat_version = FDFORMAT_VERSION;
267 tmpint = FDOPT_NORETRY|FDOPT_SILENT;
268 if (ioctl(fd, FDIOCSETOPTS, &tmpint) == -1 ||
269 ioctl(fd, FDIOCSETFORMAT, &parms) == -1) {
270 errx(1, "Cannot set requested formatting parameters:"
271 " %d cylinders, %d tracks, %d sectors of %d bytes",
272 parms.ncyl, parms.ntrk, parms.nspt, parms.nbps);
275 (void)printf("Ready to format %s with %d cylinders, %d tracks,"
276 " %d sectors of %d bytes\n(%d KB)",
277 filename, parms.ncyl, parms.ntrk, parms.nspt, parms.nbps,
278 parms.ncyl * parms.ntrk * parms.nspt * parms.nbps / 1024);
279 if (!confirm(1))
280 errx(1,"Formatting abandoned -- not confirmed.");
282 if (verify) {
283 trackbuf = malloc(parms.nbps * parms.nspt);
284 if (trackbuf == NULL)
285 warn("Cannot allocate verification buffer");
288 cmd.formatcmd_version = FDFORMAT_VERSION;
289 for (cyl = 0; (unsigned int)cyl < parms.ncyl; cyl++) {
290 cmd.cylinder = cyl;
291 for (trk = 0; (unsigned int)trk < parms.ntrk; trk++) {
292 cmd.head = trk;
293 (void)printf("\rFormatting track %i / head %i ", cyl, trk);
294 (void)fflush(stdout);
295 if (ioctl(fd, FDIOCFORMAT_TRACK, &cmd) == 0) {
296 if (verify)
297 errcnt += verify_track(fd, cyl, trk,
298 &parms, trackbuf);
299 } else if (errno == EINVAL) {
300 (void)putchar('\n');
301 errx(1, "Formatting botch at <%d,%d>",
302 cyl, trk);
303 } else if (errno == EIO) {
304 (void)printf("- IO ERROR\n");
305 errcnt++;
309 (void)printf("\rFormatting %i tracks total complete.\n",
310 parms.ncyl * parms.ntrk);
311 if (errcnt)
312 errx(1, "%d track formatting error%s",
313 errcnt, errcnt == 1 ? "" : "s");
314 return 0;