1 /* $NetBSD: fdformat.c,v 1.15 2008/04/28 20:24:12 martin Exp $ */
4 * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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
36 #include <sys/cdefs.h>
39 __RCSID("$NetBSD: fdformat.c,v 1.15 2008/04/28 20:24:12 martin Exp $");
42 #include <sys/types.h>
44 #include <sys/ioctl.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 **);
80 (void)printf(" Yes/no [%c]?", def
? 'y' : 'n');
97 verify_track(int fd
, int cyl
, int trk
, struct fdformat_parms
*parms
, char *buf
)
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");
109 if ((size_t)read(fd
, buf
, tracksize
) != tracksize
) {
110 (void)printf("- VERIFY ERROR\n");
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());
126 #define numarg(which, maskn, op) \
128 tmplong = strtol(optarg, &tmpcharp, 0); \
129 if (*tmpcharp != '\0' || tmplong op 0) \
131 "Invalid numerical argument `%s' for " \
133 if (errno == ERANGE && (tmplong == LONG_MIN || \
134 tmplong == LONG_MAX)) \
136 "Bad numerical argument `%s' for " \
138 parms.which = tmplong; \
139 parmmask |= MASK_##maskn; \
140 } while (/* CONSTCOND */0)
142 #define getparm(structname, maskname) \
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
;
167 struct fdformat_parms parms
, fetchparms
;
168 struct fdformat_cmd cmd
;
169 const char *filename
= _PATH_FLOPPY_DEV
;
173 while ((ch
= getopt(argc
, argv
, "f:t:nB:C:S:T:P:G:F:X:I:")) != -1)
175 case 't': /* disk type */
176 switch (cgetent(&fdbuf
, fdb_array
, optarg
)) {
181 errx(1, "tc= loop or missing entry entry in "
182 _PATH_FLOPPYTAB
" for type %s", optarg
);
185 errx(1, "Unknown floppy disk type %s", optarg
);
188 err(1, "Problem accessing " _PATH_FLOPPYTAB
);
196 getparm(stepspercyl
, STEPSPERCYL
);
197 getparm(gaplen
, GAPLEN
);
198 getparm(fillbyte
, FILLBYTE
);
199 getparm(xfer_rate
, XFER_RATE
);
200 getparm(interleave
, INTERLEAVE
);
202 case 'f': /* device name */
205 case 'n': /* no verify */
209 numarg(nbps
, NBPS
, <=);
212 numarg(ncyl
, NCYL
, <=);
215 numarg(nspt
, NSPT
, <=);
218 numarg(ntrk
, NTRK
, <=);
221 numarg(stepspercyl
, STEPSPERCYL
, <=);
224 numarg(gaplen
, GAPLEN
, <=);
227 numarg(fillbyte
, FILLBYTE
, <);
230 numarg(xfer_rate
, XFER_RATE
, <=);
233 numarg(interleave
, INTERLEAVE
, <=);
243 fd
= open(filename
, O_RDWR
);
245 err(1, "Cannot open %s", filename
);
246 if (ioctl(fd
, FDIOCGETFORMAT
, &fetchparms
) == -1) {
248 err(1, "Device `%s' does not support floppy formatting",
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);
280 errx(1,"Formatting abandoned -- not confirmed.");
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
++) {
291 for (trk
= 0; (unsigned int)trk
< parms
.ntrk
; trk
++) {
293 (void)printf("\rFormatting track %i / head %i ", cyl
, trk
);
294 (void)fflush(stdout
);
295 if (ioctl(fd
, FDIOCFORMAT_TRACK
, &cmd
) == 0) {
297 errcnt
+= verify_track(fd
, cyl
, trk
,
299 } else if (errno
== EINVAL
) {
301 errx(1, "Formatting botch at <%d,%d>",
303 } else if (errno
== EIO
) {
304 (void)printf("- IO ERROR\n");
309 (void)printf("\rFormatting %i tracks total complete.\n",
310 parms
.ncyl
* parms
.ntrk
);
312 errx(1, "%d track formatting error%s",
313 errcnt
, errcnt
== 1 ? "" : "s");