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