GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/fdformat/fdformat.c Lines: 0 184 0.0 %
Date: 2017-11-07 Branches: 0 148 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: fdformat.c,v 1.22 2016/03/16 15:41:11 krw Exp $	*/
2
3
/*
4
 * Copyright (C) 1992-1994 by Joerg Wunsch, Dresden
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in the
14
 *    documentation and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
17
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
20
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
 * POSSIBILITY OF SUCH DAMAGE.
27
 */
28
29
/*
30
 * FreeBSD:
31
 * format a floppy disk
32
 *
33
 * Added FD_GTYPE ioctl, verifying, proportional indicators.
34
 * Serge Vakulenko, vak@zebub.msk.su
35
 * Sat Dec 18 17:45:47 MSK 1993
36
 *
37
 * Final adaptation, change format/verify logic, add separate
38
 * format gap/interleave values
39
 * Andrew A. Chernov, ache@astral.msk.su
40
 * Thu Jan 27 00:47:24 MSK 1994
41
 */
42
43
#include <stdio.h>
44
#include <stdlib.h>
45
#include <unistd.h>
46
#include <fcntl.h>
47
#include <string.h>
48
#include <limits.h>
49
#include <ctype.h>
50
#include <err.h>
51
#include <util.h>
52
53
#include <errno.h>
54
#include <sys/types.h>
55
#include <sys/ioctl.h>
56
#include <machine/ioctl_fd.h>
57
58
extern const char *__progname;
59
60
static void
61
format_track(int fd, int cyl, int secs, int head, int rate, int gaplen,
62
    int secsize, int fill, int interleave)
63
{
64
	struct fd_formb f;
65
	int i,j;
66
	int il[FD_MAX_NSEC + 1];
67
68
	memset(il,0,sizeof il);
69
	for(j = 0, i = 1; i <= secs; i++) {
70
		while(il[(j%secs)+1])
71
			j++;
72
		il[(j%secs)+1] = i;
73
		j += interleave;
74
        }
75
76
	f.format_version = FD_FORMAT_VERSION;
77
	f.head = head;
78
	f.cyl = cyl;
79
	f.transfer_rate = rate;
80
81
	f.fd_formb_secshift = secsize;
82
	f.fd_formb_nsecs = secs;
83
	f.fd_formb_gaplen = gaplen;
84
	f.fd_formb_fillbyte = fill;
85
	for(i = 0; i < secs; i++) {
86
		f.fd_formb_cylno(i) = cyl;
87
		f.fd_formb_headno(i) = head;
88
		f.fd_formb_secno(i) = il[i+1];
89
		f.fd_formb_secsize(i) = secsize;
90
	}
91
	if (ioctl(fd, FD_FORM, (caddr_t)&f) < 0)
92
		err(1, "FD_FORM");
93
}
94
95
static int
96
verify_track(int fd, int track, int tracksize)
97
{
98
	static char *buf = 0;
99
	static int bufsz = 0;
100
	int fdopts = -1, ofdopts, rv = 0;
101
102
	if (ioctl(fd, FD_GOPTS, &fdopts) < 0)
103
		warn("FD_GOPTS");
104
	else {
105
		ofdopts = fdopts;
106
		fdopts |= FDOPT_NORETRY;
107
		(void)ioctl(fd, FD_SOPTS, &fdopts);
108
	}
109
110
	if (bufsz < tracksize) {
111
		free(buf);
112
		bufsz = tracksize;
113
		buf = 0;
114
	}
115
	if (! buf)
116
		buf = malloc(bufsz);
117
	if (! buf) {
118
		fprintf (stderr, "\nfdformat: out of memory\n");
119
		exit (2);
120
	}
121
	if (lseek (fd, (off_t) track*tracksize, SEEK_SET) < 0)
122
		rv = -1;
123
	/* try twice reading it, without using the normal retrier */
124
	else if (read (fd, buf, tracksize) != tracksize
125
		 && read (fd, buf, tracksize) != tracksize)
126
		rv = -1;
127
	if (fdopts != -1)
128
		(void)ioctl(fd, FD_SOPTS, &ofdopts);
129
	return (rv);
130
}
131
132
static void
133
usage(void)
134
{
135
	printf("usage: %s [-nqv] [-c cyls] [-F fillbyte] [-g gap3len] ",
136
		__progname);
137
	printf("[-h heads]\n");
138
	printf("\t[-i intleave] [-r rate] [-S secshft] [-s secs]\n");
139
	printf("\t[-t steps_per_track] device_name\n");
140
	printf("Options:\n");
141
	printf("\t-n\tdon't verify floppy after formatting\n");
142
	printf("\t-q\tsuppress any normal output, don't ask for confirmation\n");
143
	printf("\t-v\tdon't format, verify only\n");
144
	printf("\tdevname\tthe full name of floppy device or in short form fd0, fd1\n");
145
	printf("Obscure options:\n");
146
	printf("\t-c #\tspecify number of cylinders, 40 or 80\n");
147
	printf("\t-F #\tspecify fill byte\n");
148
	printf("\t-g #\tspecify gap length\n");
149
	printf("\t-h #\tspecify number of floppy heads, 1 or 2\n");
150
	printf("\t-i #\tspecify interleave factor\n");
151
	printf("\t-r #\tspecify data rate, 250, 300 or 500 kbps\n");
152
	printf("\t-S #\tspecify sector size, 0=128, 1=256, 2=512 bytes\n");
153
	printf("\t-s #\tspecify number of sectors per track, 9, 10, 15 or 18\n");
154
	printf("\t-t #\tnumber of steps per track\n");
155
	exit(2);
156
}
157
158
static int
159
yes(void)
160
{
161
	char reply[256], *p;
162
163
	for (;;) {
164
		fflush(stdout);
165
		if (!fgets(reply, sizeof(reply), stdin))
166
			return (0);
167
		for (p=reply; *p==' ' || *p=='\t'; ++p)
168
			continue;
169
		if (*p=='y' || *p=='Y')
170
			return (1);
171
		if (*p=='n' || *p=='N' || *p=='\n' || *p=='\r')
172
			return (0);
173
		printf("Answer `yes' or `no': ");
174
	}
175
}
176
177
int
178
main(int argc, char *argv[])
179
{
180
	int cyls = -1, secs = -1, heads = -1, intleave = -1;
181
	int rate = -1, gaplen = -1, secsize = -1, steps = -1;
182
	int fill = 0xf6, quiet = 0, verify = 1, verify_only = 0;
183
	int fd, c, track, error, tracks_per_dot, bytes_per_track, errs;
184
	const char *errstr;
185
	char *devname;
186
	struct fd_type fdt;
187
188
	while((c = getopt(argc, argv, "c:s:h:r:g:S:F:t:i:qvn")) != -1)
189
		switch (c) {
190
		case 'c':       /* # of cyls */
191
			cyls = strtonum(optarg, 1, INT_MAX, &errstr);
192
			if (errstr)
193
				errx(1, "-c %s: %s", optarg, errstr);
194
			break;
195
196
		case 's':       /* # of secs per track */
197
			secs = strtonum(optarg, 1, INT_MAX, &errstr);
198
			if (errstr)
199
				errx(1, "-s %s: %s", optarg, errstr);
200
			break;
201
202
		case 'h':       /* # of heads */
203
			heads = strtonum(optarg, 1, INT_MAX, &errstr);
204
			if (errstr)
205
				errx(1, "-h %s: %s", optarg, errstr);
206
			break;
207
208
		case 'r':       /* transfer rate, kilobyte/sec */
209
			rate = strtonum(optarg, 1, INT_MAX, &errstr);
210
			if (errstr)
211
				errx(1, "-r %s: %s", optarg, errstr);
212
			break;
213
214
		case 'g':       /* length of GAP3 to format with */
215
			gaplen = strtonum(optarg, 1, INT_MAX, &errstr);
216
			if (errstr)
217
				errx(1, "-g %s: %s", optarg, errstr);
218
			break;
219
220
		case 'S':       /* sector size shift factor (1 << S)*128 */
221
			secsize = strtonum(optarg, 0, INT_MAX, &errstr);
222
			if (errstr)
223
				errx(1, "-S %s: %s", optarg, errstr);
224
			break;
225
226
		case 'F':       /* fill byte, C-like notation allowed */
227
			fill = (int)strtol(optarg, NULL, 0);
228
			break;
229
230
		case 't':       /* steps per track */
231
			steps = strtonum(optarg, 1, INT_MAX, &errstr);
232
			if (errstr)
233
				errx(1, "-t %s: %s", optarg, errstr);
234
			break;
235
236
		case 'i':       /* interleave factor */
237
			intleave = strtonum(optarg, 1, INT_MAX, &errstr);
238
			if (errstr)
239
				errx(1, "-i %s: %s", optarg, errstr);
240
			break;
241
242
		case 'q':
243
			quiet = 1;
244
			break;
245
246
		case 'n':
247
			verify = 0;
248
			break;
249
250
		case 'v':
251
			verify = 1;
252
			verify_only = 1;
253
			break;
254
255
		case '?': default:
256
			usage();
257
		}
258
259
	if (optind != argc - 1)
260
		usage();
261
262
	if ((fd = opendev(argv[optind], O_RDWR, OPENDEV_PART, &devname)) < 0)
263
		err(1, "%s", devname);
264
265
	if (ioctl(fd, FD_GTYPE, &fdt) < 0)
266
		errx(1, "not a floppy disk: %s", devname);
267
268
	switch (rate) {
269
	case -1:
270
		break;
271
	case 250:
272
		fdt.rate = FDC_250KBPS;
273
		break;
274
	case 300:
275
		fdt.rate = FDC_300KBPS;
276
		break;
277
	case 500:
278
		fdt.rate = FDC_500KBPS;
279
		break;
280
	default:
281
		errx(1, "invalid transfer rate: %d", rate);
282
	}
283
284
	if (cyls >= 0)
285
		fdt.tracks = cyls;
286
	if (secs >= 0)
287
		fdt.sectrac = secs;
288
	if (fdt.sectrac > FD_MAX_NSEC)
289
		errx(1, "too many sectors per track, max value is %d",
290
			FD_MAX_NSEC);
291
	if (heads >= 0)
292
		fdt.heads = heads;
293
	if (gaplen >= 0)
294
		fdt.gap2 = gaplen;
295
	if (secsize >= 0)
296
		fdt.secsize = secsize;
297
	if (steps >= 0)
298
		fdt.step = steps;
299
300
	bytes_per_track = fdt.sectrac * (1<<fdt.secsize) * 128;
301
	tracks_per_dot = fdt.tracks * fdt.heads / 40;
302
	if (tracks_per_dot == 0)
303
		tracks_per_dot++;
304
305
	if (verify_only) {
306
		if (!quiet)
307
			printf("Verify %dK floppy `%s'.\n",
308
				fdt.tracks * fdt.heads * bytes_per_track / 1024,
309
				devname);
310
	}
311
	else if (!quiet) {
312
		printf("Format %dK floppy `%s'? (y/n): ",
313
			fdt.tracks * fdt.heads * bytes_per_track / 1024,
314
			devname);
315
		if (!yes()) {
316
			printf("Not confirmed.\n");
317
			exit(0);
318
		}
319
	}
320
321
	/*
322
	 * Formatting.
323
	 */
324
	if (!quiet) {
325
		printf("Processing ");
326
		for (track = 0; track < fdt.tracks * fdt.heads; track++) {
327
			if (!((track + 1) % tracks_per_dot))
328
				putchar('-');
329
		}
330
		putchar('\r');
331
		printf("Processing ");
332
		fflush(stdout);
333
	}
334
335
	error = errs = 0;
336
337
	for (track = 0; track < fdt.tracks * fdt.heads; track++) {
338
		if (!verify_only) {
339
			format_track(fd, track / fdt.heads, fdt.sectrac,
340
				track % fdt.heads, fdt.rate, fdt.gap2,
341
				     fdt.secsize, fill,
342
				     intleave >= 0 ? intleave : 1);
343
			if (!quiet && !((track + 1) % tracks_per_dot)) {
344
				putchar('F');
345
				fflush(stdout);
346
			}
347
		}
348
		if (verify) {
349
			if (verify_track(fd, track, bytes_per_track) < 0)
350
				error = errs = 1;
351
			if (!quiet && !((track + 1) % tracks_per_dot)) {
352
				if (!verify_only)
353
					putchar('\b');
354
				if (error) {
355
					putchar('E');
356
					error = 0;
357
				}
358
				else
359
					putchar('V');
360
				fflush(stdout);
361
			}
362
		}
363
	}
364
	close(fd);
365
	if (!quiet)
366
		printf(" done.\n");
367
368
	exit(errs);
369
}