fdformat.c revision 1.9 1 1.9 simonb /* $NetBSD: fdformat.c,v 1.9 2001/03/28 03:17:41 simonb Exp $ */
2 1.5 thorpej
3 1.1 jtk /*-
4 1.5 thorpej * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
5 1.5 thorpej * All rights reserved.
6 1.5 thorpej *
7 1.5 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.5 thorpej * by John Kohl.
9 1.1 jtk *
10 1.1 jtk * Redistribution and use in source and binary forms, with or without
11 1.1 jtk * modification, are permitted provided that the following conditions
12 1.1 jtk * are met:
13 1.1 jtk * 1. Redistributions of source code must retain the above copyright
14 1.1 jtk * notice, this list of conditions and the following disclaimer.
15 1.1 jtk * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 jtk * notice, this list of conditions and the following disclaimer in the
17 1.1 jtk * documentation and/or other materials provided with the distribution.
18 1.1 jtk * 3. All advertising materials mentioning features or use of this software
19 1.1 jtk * must display the following acknowledgement:
20 1.5 thorpej * This product includes software developed by the NetBSD
21 1.5 thorpej * Foundation, Inc. and its contributors.
22 1.5 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.5 thorpej * contributors may be used to endorse or promote products derived
24 1.5 thorpej * from this software without specific prior written permission.
25 1.1 jtk *
26 1.5 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.5 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.5 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.5 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.5 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.5 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.5 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.5 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.5 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.5 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 jtk * POSSIBILITY OF SUCH DAMAGE.
37 1.1 jtk */
38 1.1 jtk
39 1.1 jtk /*
40 1.1 jtk * fdformat: format a floppy diskette, using interface provided in
41 1.1 jtk * <sys/fdio.h>
42 1.1 jtk */
43 1.4 lukem #include <sys/types.h>
44 1.4 lukem #include <sys/fdio.h>
45 1.4 lukem #include <sys/ioctl.h>
46 1.1 jtk
47 1.4 lukem #include <err.h>
48 1.4 lukem #include <errno.h>
49 1.4 lukem #include <fcntl.h>
50 1.4 lukem #include <limits.h>
51 1.1 jtk #include <stdio.h>
52 1.1 jtk #include <stdlib.h>
53 1.1 jtk #include <unistd.h>
54 1.1 jtk #include "pathnames.h"
55 1.1 jtk
56 1.1 jtk char *fdb_array[2] = {_PATH_FLOPPYTAB, 0};
57 1.1 jtk
58 1.1 jtk #define MASK_NBPS 0x0001
59 1.1 jtk #define MASK_NCYL 0x0002
60 1.1 jtk #define MASK_NSPT 0x0004
61 1.1 jtk #define MASK_NTRK 0x0008
62 1.1 jtk #define MASK_STEPSPERCYL 0x0010
63 1.1 jtk #define MASK_GAPLEN 0x0020
64 1.1 jtk #define MASK_FILLBYTE 0x0040
65 1.1 jtk #define MASK_XFER_RATE 0x0080
66 1.1 jtk #define MASK_INTERLEAVE 0x0100
67 1.1 jtk
68 1.1 jtk #define ALLPARMS (MASK_NBPS|MASK_NCYL|MASK_NSPT|MASK_NTRK|MASK_STEPSPERCYL|MASK_GAPLEN|MASK_FILLBYTE|MASK_XFER_RATE|MASK_INTERLEAVE)
69 1.1 jtk
70 1.4 lukem int confirm(int);
71 1.4 lukem int main __P((int, char **));
72 1.4 lukem void usage __P((void));
73 1.4 lukem int verify_track(int, int, int, struct fdformat_parms *, char *);
74 1.1 jtk
75 1.1 jtk int
76 1.1 jtk confirm(int def)
77 1.1 jtk {
78 1.1 jtk int ch;
79 1.1 jtk
80 1.1 jtk printf(" Yes/no [%c]?", def ? 'y' : 'n');
81 1.1 jtk ch = getchar();
82 1.1 jtk switch (ch) {
83 1.1 jtk case 'y':
84 1.1 jtk case 'Y':
85 1.1 jtk return 1;
86 1.1 jtk case '\n':
87 1.1 jtk return def;
88 1.1 jtk case EOF:
89 1.1 jtk case 'n':
90 1.1 jtk case 'N':
91 1.1 jtk default:
92 1.1 jtk return 0;
93 1.1 jtk }
94 1.1 jtk }
95 1.1 jtk
96 1.1 jtk int
97 1.1 jtk verify_track(int fd, int cyl, int trk, struct fdformat_parms *parms, char *buf)
98 1.1 jtk {
99 1.1 jtk size_t tracksize;
100 1.1 jtk off_t offset;
101 1.1 jtk
102 1.1 jtk tracksize = parms->nbps * parms->nspt; /* bytes per track */
103 1.1 jtk offset = tracksize * (cyl * parms->ntrk + trk); /* track offset */
104 1.1 jtk
105 1.1 jtk if (lseek(fd, offset, SEEK_SET) == (off_t) -1) {
106 1.1 jtk putchar('E');
107 1.1 jtk return 1;
108 1.1 jtk }
109 1.1 jtk if (read(fd, buf, tracksize) != tracksize) {
110 1.1 jtk putchar('E');
111 1.1 jtk return 1;
112 1.1 jtk } else
113 1.1 jtk putchar('V');
114 1.1 jtk return 0;
115 1.1 jtk }
116 1.1 jtk
117 1.1 jtk void
118 1.1 jtk usage(void)
119 1.1 jtk {
120 1.8 cgd
121 1.9 simonb fprintf(stderr,
122 1.9 simonb "Usage: %s [-f device] [-t type] [-n] [-B nbps] [-S nspt]\n"
123 1.9 simonb "\t[-T ntrk] [-C ncyl] [-P stepspercyl] [-G gaplen]\n"
124 1.9 simonb "\t[-F fillbyte] [-X xfer_rate] [-I interleave]\n", getprogname());
125 1.7 christos exit(1);
126 1.1 jtk }
127 1.1 jtk
128 1.1 jtk #define numarg(which, maskn) \
129 1.1 jtk tmplong = strtol(optarg, &tmpcharp, 0); \
130 1.1 jtk if (*tmpcharp != '\0' || tmplong <= 0 || tmplong == LONG_MIN || tmplong == LONG_MAX) \
131 1.1 jtk errx(1, "invalid numerical argument %s for " # which, optarg); \
132 1.1 jtk parms.which = tmplong; \
133 1.1 jtk parmmask |= MASK_##maskn
134 1.1 jtk
135 1.1 jtk #define getparm(structname, maskname) \
136 1.1 jtk if (cgetnum(fdbuf, # structname, &tmplong) == -1) \
137 1.1 jtk errx(1, "parameter " # structname " missing for type %s", \
138 1.1 jtk optarg); \
139 1.1 jtk parms.structname = tmplong; \
140 1.1 jtk parmmask |= MASK_ ## maskname
141 1.1 jtk
142 1.1 jtk #define copyparm(which, mask) \
143 1.1 jtk if ((parmmask & MASK_##mask) == 0) \
144 1.1 jtk parms.which = fetchparms.which
145 1.1 jtk
146 1.1 jtk int
147 1.1 jtk main(int argc, char *argv[])
148 1.1 jtk {
149 1.4 lukem char *fdbuf = NULL, *trackbuf = NULL;
150 1.1 jtk int errcnt = 0;
151 1.1 jtk int verify = 1;
152 1.1 jtk int ch;
153 1.1 jtk long tmplong;
154 1.1 jtk int tmpint;
155 1.1 jtk char *tmpcharp;
156 1.1 jtk int parmmask = 0;
157 1.1 jtk struct fdformat_parms parms, fetchparms;
158 1.1 jtk struct fdformat_cmd cmd;
159 1.1 jtk char *filename = _PATH_FLOPPY_DEV;
160 1.1 jtk int fd;
161 1.4 lukem int trk, cyl;
162 1.1 jtk
163 1.1 jtk while ((ch = getopt(argc, argv, "f:t:nB:C:S:T:P:G:F:X:I:")) != -1)
164 1.1 jtk switch (ch) {
165 1.1 jtk case 't': /* disk type */
166 1.1 jtk switch (cgetent(&fdbuf, fdb_array, optarg)) {
167 1.1 jtk case 0:
168 1.1 jtk break;
169 1.1 jtk case 1:
170 1.1 jtk case -3:
171 1.1 jtk errx(1, "tc= loop or missing entry entry in "
172 1.1 jtk _PATH_FLOPPYTAB " for type %s", optarg);
173 1.1 jtk break;
174 1.1 jtk case -1:
175 1.1 jtk errx(1, "unknown floppy disk type %s", optarg);
176 1.1 jtk break;
177 1.1 jtk default:
178 1.1 jtk err(1, "problem accessing " _PATH_FLOPPYTAB);
179 1.1 jtk break;
180 1.1 jtk }
181 1.1 jtk
182 1.1 jtk getparm(nbps, NBPS);
183 1.1 jtk getparm(ncyl, NCYL);
184 1.1 jtk getparm(nspt, NSPT);
185 1.1 jtk getparm(ntrk, NTRK);
186 1.1 jtk getparm(stepspercyl, STEPSPERCYL);
187 1.1 jtk getparm(gaplen, GAPLEN);
188 1.1 jtk getparm(fillbyte, FILLBYTE);
189 1.1 jtk getparm(xfer_rate, XFER_RATE);
190 1.1 jtk getparm(interleave, INTERLEAVE);
191 1.1 jtk break;
192 1.1 jtk case 'f': /* device name */
193 1.1 jtk filename = optarg;
194 1.1 jtk break;
195 1.1 jtk case 'n': /* no verify */
196 1.1 jtk verify = 0;
197 1.1 jtk break;
198 1.1 jtk case 'B':
199 1.1 jtk numarg(nbps, NBPS);
200 1.1 jtk break;
201 1.1 jtk case 'C':
202 1.1 jtk numarg(ncyl, NCYL);
203 1.1 jtk break;
204 1.1 jtk case 'S':
205 1.1 jtk numarg(nspt, NSPT);
206 1.1 jtk break;
207 1.1 jtk case 'T':
208 1.1 jtk numarg(ntrk, NTRK);
209 1.1 jtk break;
210 1.1 jtk case 'P':
211 1.1 jtk numarg(stepspercyl, STEPSPERCYL);
212 1.1 jtk break;
213 1.1 jtk case 'G':
214 1.1 jtk numarg(gaplen, GAPLEN);
215 1.1 jtk break;
216 1.1 jtk case 'F': /* special handling--0 is OK */
217 1.1 jtk /*numarg(fillbyte, FILLBYTE);*/
218 1.1 jtk tmplong = strtol(optarg, &tmpcharp, 0);
219 1.1 jtk if (*tmpcharp != '\0' || tmplong < 0 ||
220 1.1 jtk tmplong == LONG_MIN || tmplong == LONG_MAX)
221 1.1 jtk errx(1, "invalid numerical argument %s for fillbyte", optarg);
222 1.1 jtk parms.fillbyte = tmplong;
223 1.1 jtk parmmask |= MASK_FILLBYTE;
224 1.1 jtk break;
225 1.1 jtk case 'X':
226 1.1 jtk numarg(xfer_rate, XFER_RATE);
227 1.1 jtk break;
228 1.1 jtk case 'I':
229 1.1 jtk numarg(interleave, INTERLEAVE);
230 1.1 jtk break;
231 1.1 jtk case '?':
232 1.1 jtk default:
233 1.1 jtk usage();
234 1.1 jtk }
235 1.3 jtk if (optind < argc)
236 1.3 jtk usage();
237 1.3 jtk
238 1.1 jtk fd = open(filename, O_RDWR);
239 1.1 jtk if (fd == -1)
240 1.1 jtk err(1, "cannot open %s", filename);
241 1.1 jtk if (ioctl(fd, FDIOCGETFORMAT, &fetchparms) == -1) {
242 1.1 jtk if (errno == ENOTTY)
243 1.1 jtk err(1, "device %s does not support floppy formatting",
244 1.1 jtk filename);
245 1.1 jtk else
246 1.1 jtk err(1, "cannot fetch current floppy formatting parameters");
247 1.1 jtk }
248 1.1 jtk
249 1.1 jtk copyparm(nbps, NBPS);
250 1.1 jtk copyparm(ncyl, NCYL);
251 1.1 jtk copyparm(nspt, NSPT);
252 1.1 jtk copyparm(ntrk, NTRK);
253 1.1 jtk copyparm(stepspercyl, STEPSPERCYL);
254 1.1 jtk copyparm(gaplen, GAPLEN);
255 1.1 jtk copyparm(fillbyte, FILLBYTE);
256 1.1 jtk copyparm(xfer_rate, XFER_RATE);
257 1.1 jtk copyparm(interleave, INTERLEAVE);
258 1.1 jtk
259 1.1 jtk parms.fdformat_version = FDFORMAT_VERSION;
260 1.1 jtk
261 1.1 jtk tmpint = FDOPT_NORETRY|FDOPT_SILENT;
262 1.1 jtk if (ioctl(fd, FDIOCSETOPTS, &tmpint) == -1 ||
263 1.1 jtk ioctl(fd, FDIOCSETFORMAT, &parms) == -1) {
264 1.1 jtk warn("cannot set requested formatting parameters");
265 1.1 jtk errx(1, "%d cylinders, %d tracks, %d sectors of %d bytes",
266 1.1 jtk parms.ncyl, parms.ntrk, parms.nspt, parms.nbps);
267 1.1 jtk }
268 1.1 jtk
269 1.1 jtk printf("Ready to format %s with %d cylinders, %d tracks, %d sectors of %d bytes\n(%d KB)",
270 1.1 jtk filename, parms.ncyl, parms.ntrk, parms.nspt, parms.nbps,
271 1.1 jtk parms.ncyl * parms.ntrk * parms.nspt * parms.nbps / 1024);
272 1.1 jtk if (!confirm(1))
273 1.1 jtk errx(1,"formatting abandoned--not confirmed.");
274 1.1 jtk
275 1.1 jtk if (verify)
276 1.1 jtk trackbuf = malloc(parms.nbps * parms.nspt);
277 1.1 jtk
278 1.1 jtk cmd.formatcmd_version = FDFORMAT_VERSION;
279 1.1 jtk for (cyl = 0; cyl < parms.ncyl; cyl++) {
280 1.1 jtk cmd.cylinder = cyl;
281 1.1 jtk for (trk = 0; trk < parms.ntrk; trk++) {
282 1.1 jtk cmd.head = trk;
283 1.1 jtk if (ioctl(fd, FDIOCFORMAT_TRACK, &cmd) == 0) {
284 1.1 jtk putchar('F');
285 1.1 jtk if (verify)
286 1.1 jtk putchar('\b');
287 1.1 jtk fflush(stdout);
288 1.1 jtk if (verify)
289 1.1 jtk errcnt += verify_track(fd, cyl, trk,
290 1.1 jtk &parms,
291 1.1 jtk trackbuf);
292 1.1 jtk } else if (errno == EINVAL) {
293 1.1 jtk putchar('\n');
294 1.1 jtk errx(1, "formatting botch at <%d,%d>",
295 1.1 jtk cyl, trk);
296 1.1 jtk } else if (errno == EIO) {
297 1.1 jtk putchar('E');
298 1.1 jtk fflush(stdout);
299 1.1 jtk errcnt++;
300 1.1 jtk }
301 1.1 jtk }
302 1.1 jtk }
303 1.1 jtk putchar('\n');
304 1.1 jtk if (errcnt)
305 1.6 hubertf errx(1,"%d track formatting error%s", errcnt, errcnt==1?"":"s");
306 1.1 jtk return 0;
307 1.1 jtk }
308