summaryrefslogtreecommitdiff
path: root/usr.bin/fdformat/fdformat.c
blob: 54f748ef1457cf323f7256e6a36cac746306ab4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*	$NetBSD: fdformat.c,v 1.18 2021/11/27 22:16:41 rillig Exp $	*/

/*-
 * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by John Kohl.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * fdformat: format a floppy diskette, using interface provided in
 * <sys/fdio.h>
 */
#include <sys/cdefs.h>

#ifndef lint
__RCSID("$NetBSD: fdformat.c,v 1.18 2021/11/27 22:16:41 rillig Exp $");
#endif

#include <sys/types.h>
#include <sys/fdio.h>
#include <sys/ioctl.h>

#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "pathnames.h"

static const char *fdb_array[2] = {_PATH_FLOPPYTAB, 0};

#define MASK_NBPS		0x0001
#define MASK_NCYL		0x0002
#define MASK_NSPT		0x0004
#define MASK_NTRK		0x0008
#define MASK_STEPSPERCYL	0x0010
#define MASK_GAPLEN		0x0020
#define MASK_FILLBYTE		0x0040
#define MASK_XFER_RATE		0x0080
#define MASK_INTERLEAVE		0x0100

#define ALLPARMS (MASK_NBPS|MASK_NCYL|MASK_NSPT|MASK_NTRK|MASK_STEPSPERCYL|MASK_GAPLEN|MASK_FILLBYTE|MASK_XFER_RATE|MASK_INTERLEAVE)

static int	confirm(int);
static void	usage(void) __dead;
static int	verify_track(int, int, int, struct fdformat_parms *, char *);

static int
confirm(int def)
{
	int ch;

	(void)printf(" Yes/no [%c]?", def ? 'y' : 'n');
	ch = getchar();
	switch (ch) {
	case 'y':
	case 'Y':
		return 1;
	case '\n':
		return def;
	case EOF:
	case 'n':
	case 'N':
	default:
		return 0;
	}		
}

static int
verify_track(int fd, int cyl, int trk, struct fdformat_parms *parms, char *buf)
{
	size_t tracksize;
	off_t offset;

	tracksize = parms->nbps * parms->nspt; /* bytes per track */
	offset = tracksize * (cyl * parms->ntrk + trk); /* track offset */

	if (lseek(fd, offset, SEEK_SET) == (off_t) -1) {
		(void)printf("- SEEK ERROR\n");
		return 1;
	}
	if ((size_t)read(fd, buf, tracksize) != tracksize) {
		(void)printf("- VERIFY ERROR\n");
		return 1;
	}
	return 0;
}

static void
usage(void)
{
	(void)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", getprogname());
	exit(1);
}

#define numarg(which, maskn, op) 				\
	do {							\
		tmplong = strtol(optarg, &tmpcharp, 0); 	\
		if (*tmpcharp != '\0' || tmplong op 0) 		\
		    errx(1,					\
			"Invalid numerical argument `%s' for " 	\
			# which, optarg); 			\
		if (errno == ERANGE && (tmplong == LONG_MIN ||	\
		    tmplong == LONG_MAX)) 			\
		    err(1,					\
			"Bad numerical argument `%s' for " 	\
			# which, optarg); 			\
		parms.which = tmplong; 				\
		parmmask |= MASK_##maskn;			\
	} while (0)

#define getparm(structname, maskname) 				\
	do {							\
		if (cgetnum(fdbuf, # structname, &tmplong) == -1) \
			errx(1, "Parameter " # structname	\
			    " missing for type `%s'", optarg);	\
		parms.structname = tmplong; 			\
		parmmask |= MASK_ ## maskname;			\
	} while (0)


#define copyparm(which, mask) 					\
	if ((parmmask & MASK_##mask) == 0) 			\
		parms.which = fetchparms.which

int
main(int argc, char *argv[])
{
	char *fdbuf = NULL, *trackbuf = NULL;
	int errcnt = 0;
	int verify = 1;
	int ch;
	long tmplong;
	int tmpint;
	char *tmpcharp;
	int parmmask = 0;
	struct fdformat_parms parms, fetchparms;
	struct fdformat_cmd cmd;
	const char *filename = _PATH_FLOPPY_DEV;
	int fd;
	int trk, cyl;

	while ((ch = getopt(argc, argv, "f:t:nB:C:S:T:P:G:F:X:I:")) != -1)
		switch (ch) {
		case 't':		/* disk type */
			switch (cgetent(&fdbuf, fdb_array, optarg)) {
			case 0:
				break;
			case 1:
			case -3:
				errx(1, "tc= loop or missing entry entry in "
				     _PATH_FLOPPYTAB " for type %s", optarg);
				break;
			case -1:
				errx(1, "Unknown floppy disk type %s", optarg);
				break;
			default:
				err(1, "Problem accessing " _PATH_FLOPPYTAB);
				break;
			}

			getparm(nbps, NBPS);
			getparm(ncyl, NCYL);
			getparm(nspt, NSPT);
			getparm(ntrk, NTRK);
			getparm(stepspercyl, STEPSPERCYL);
			getparm(gaplen, GAPLEN);
			getparm(fillbyte, FILLBYTE);
			getparm(xfer_rate, XFER_RATE);
			getparm(interleave, INTERLEAVE);
			break;
		case 'f':		/* device name */
			filename = optarg;
			break;
		case 'n':		/* no verify */
			verify = 0;
			break;
		case 'B':
			numarg(nbps, NBPS, <=);
			break;
		case 'C':
			numarg(ncyl, NCYL, <=);
			break;
		case 'S':
			numarg(nspt, NSPT, <=);
			break;
		case 'T':
			numarg(ntrk, NTRK, <=);
			break;
		case 'P':
			numarg(stepspercyl, STEPSPERCYL, <=);
			break;
		case 'G':
			numarg(gaplen, GAPLEN, <=);
			break;
		case 'F':
			numarg(fillbyte, FILLBYTE, <);
			break;
		case 'X':
			numarg(xfer_rate, XFER_RATE, <=);
			break;
		case 'I':
			numarg(interleave, INTERLEAVE, <=);
			break;
		case '?':
		default:
			usage();
		}

	if (optind < argc)
		usage();

	fd = open(filename, O_RDWR);
	if (fd == -1)
		err(1, "Cannot open %s", filename);
	if (ioctl(fd, FDIOCGETFORMAT, &fetchparms) == -1) {
		if (errno == ENOTTY)
			err(1, "Device `%s' does not support floppy formatting",
			    filename);
		else
			err(1, "Cannot fetch current floppy"
			    " formatting parameters");
	}

	copyparm(nbps, NBPS);
	copyparm(ncyl, NCYL);
	copyparm(nspt, NSPT);
	copyparm(ntrk, NTRK);
	copyparm(stepspercyl, STEPSPERCYL);
	copyparm(gaplen, GAPLEN);
	copyparm(fillbyte, FILLBYTE);
	copyparm(xfer_rate, XFER_RATE);
	copyparm(interleave, INTERLEAVE);

	parms.fdformat_version = FDFORMAT_VERSION;

	tmpint = FDOPT_NORETRY|FDOPT_SILENT;
	if (ioctl(fd, FDIOCSETOPTS, &tmpint) == -1 ||
	    ioctl(fd, FDIOCSETFORMAT, &parms) == -1) {
		errx(1, "Cannot set requested formatting parameters:"
		    " %d cylinders, %d tracks, %d sectors of %d bytes",
		    parms.ncyl, parms.ntrk, parms.nspt, parms.nbps);
	}

	(void)printf("Ready to format %s with %d cylinders, %d tracks,"
	    " %d sectors of %d bytes\n(%d KB)",
	    filename, parms.ncyl, parms.ntrk, parms.nspt, parms.nbps,
	    parms.ncyl * parms.ntrk * parms.nspt * parms.nbps / 1024);
	if (!confirm(1))
		errx(1,"Formatting abandoned -- not confirmed.");
	
	if (verify) {
		trackbuf = malloc(parms.nbps * parms.nspt);
		if (trackbuf == NULL)
			warn("Cannot allocate verification buffer");
	}

	cmd.formatcmd_version = FDFORMAT_VERSION;
	for (cyl = 0; (unsigned int)cyl < parms.ncyl; cyl++) {
		cmd.cylinder = cyl;
		for (trk = 0; (unsigned int)trk < parms.ntrk; trk++) {
			cmd.head = trk;
			(void)printf("\rFormatting track %i / head %i ", cyl, trk);
			(void)fflush(stdout);
			if (ioctl(fd, FDIOCFORMAT_TRACK, &cmd) == 0) {
				if (verify)
					errcnt += verify_track(fd, cyl, trk,
					    &parms, trackbuf);
			} else if (errno == EINVAL) {
				(void)putchar('\n');
				errx(1, "Formatting botch at <%d,%d>",
				     cyl, trk);
			} else if (errno == EIO) {
				(void)printf("- IO ERROR\n");
				errcnt++;
			}
		}
	}
	(void)printf("\rFormatting %i tracks total complete.\n",
		parms.ncyl * parms.ntrk);
	if (errcnt)
		errx(1, "%d track formatting error%s",
		    errcnt, errcnt == 1 ? "" : "s");
	return 0;
}