xref: /netbsd-src/usr.sbin/bad144/bad144.c (revision 4d6fc14bc9b0c5bf3e30be318c143ee82cadd108)
1 /*	$NetBSD: bad144.c,v 1.31 2015/01/02 19:46:02 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1980, 1986, 1988, 1993
5  *	The Regents of the University of California.  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  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1988, 1993\
35  The Regents of the University of California.  All rights reserved.");
36 #endif /* not lint */
37 
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)bad144.c	8.2 (Berkeley) 4/27/95";
41 #else
42 __RCSID("$NetBSD: bad144.c,v 1.31 2015/01/02 19:46:02 christos Exp $");
43 #endif
44 #endif /* not lint */
45 
46 /*
47  * bad144
48  *
49  * This program prints and/or initializes a bad block record for a pack,
50  * in the format used by the DEC standard 144.
51  * It can also add bad sector(s) to the record, moving the sector
52  * replacements as necessary.
53  *
54  * It is preferable to write the bad information with a standard formatter,
55  * but this program will do.
56  *
57  * RP06 sectors are marked as bad by inverting the format bit in the
58  * header; on other drives the valid-sector bit is cleared.
59  */
60 #include <sys/param.h>
61 #include <sys/dkbad.h>
62 #include <sys/ioctl.h>
63 #include <sys/file.h>
64 #include <sys/disklabel.h>
65 #include <ufs/ufs/dinode.h>
66 #include <ufs/ffs/fs.h>
67 
68 #include <err.h>
69 #include <paths.h>
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <string.h>
73 #include <unistd.h>
74 #include <util.h>
75 
76 #define RETRIES	10		/* number of retries on reading old sectors */
77 
78 #ifdef __vax__
79 static int	fflag;
80 #endif
81 static int	add, copy, verbose, nflag;
82 static int	dups;
83 static int	badfile = -1;		/* copy of badsector table to use, -1 if any */
84 #define MAXSECSIZE	1024
85 static struct	dkbad curbad, oldbad;
86 #define	DKBAD_MAGIC	0x4321
87 
88 static daddr_t	size;
89 static struct	disklabel *dp;
90 static struct	disklabel label;
91 static char	diskname[MAXPATHLEN];
92 
93 static daddr_t	badsn(const struct bt_bad *);
94 static int	blkcopy(int, daddr_t, daddr_t);
95 static void	blkzero(int, daddr_t);
96 static int	checkold(void);
97 static int	compare(const void *, const void *);
98 static daddr_t	getold(int, struct dkbad *);
99 static void	shift(int, int, int);
100 __dead static void	usage(void);
101 
102 #ifdef __vax__
103 #define OPTSTRING "01234acfvn"
104 #else
105 #define OPTSTRING "01234acvn"
106 #endif
107 
108 int
109 main(int argc, char *argv[])
110 {
111 	struct bt_bad *bt;
112 	daddr_t	sn;
113 #ifdef __vax__
114 	daddr_t bn[NBT_BAD];
115 #endif
116 	int i, f, nbad, new, bad, errs, ch;
117 
118 	while ((ch = getopt(argc, argv, OPTSTRING)) != -1) {
119 		switch (ch) {
120 		case '0':
121 		case '1':
122 		case '2':
123 		case '3':
124 		case '4':
125 			badfile = ch - '0';
126 			break;
127 		case 'a':
128 			add = 1;
129 			break;
130 		case 'c':
131 			copy = 1;
132 			break;
133 #ifdef __vax__
134 		case 'f':
135 			fflag = 1;
136 			break;
137 #endif
138 		case 'n':
139 			nflag = 1;
140 			/* FALLTHROUGH */
141 		case 'v':
142 			verbose = 1;
143 			break;
144 		case '?':
145 		default:
146 			usage();
147 		}
148 	}
149 
150 	argc -= optind;
151 	argv += optind;
152 
153 	if (argc < 1) {
154 		usage();
155 	}
156 	f = opendisk(argv[0], argc == 1 ? O_RDONLY : O_RDWR, diskname,
157 	    sizeof(diskname), 0);
158 	if (f < 0)
159 		err(4, "opendisk `%s'", diskname);
160 	/* obtain label and adjust to fit */
161 	dp = &label;
162 	if (ioctl(f, DIOCGDINFO, dp) < 0)
163 		err(4, "ioctl DIOCGDINFO `%s'", diskname);
164 	if (dp->d_magic != DISKMAGIC || dp->d_magic2 != DISKMAGIC
165 		/* dkcksum(lp) != 0 */ )
166 		errx(1, "Bad pack magic number (pack is unlabeled)");
167 	if (dp->d_secsize > MAXSECSIZE || dp->d_secsize == 0)
168 		errx(7, "Disk sector size too large/small (%d)",
169 		    dp->d_secsize);
170 #ifdef __i386__
171 	if (dp->d_type == DKTYPE_SCSI)
172 		errx(1, "SCSI disks don't use bad144!");
173 	/* are we inside a DOS partition? */
174 	if (dp->d_partitions[0].p_offset) {
175 		/* yes, rules change. assume bad tables at end of partition C,
176 		   which maps all of DOS partition we are within -wfj */
177 		size = dp->d_partitions[2].p_offset + dp->d_partitions[2].p_size;
178 	}
179 #endif
180 	size = dp->d_nsectors * dp->d_ntracks * dp->d_ncylinders;
181 	argc--;
182 	argv++;
183 	if (argc == 0) {
184 		sn = getold(f, &oldbad);
185 		printf("bad block information at sector %lld in %s:\n",
186 		    (long long)sn, diskname);
187 		printf("cartridge serial number: %d(10)\n", oldbad.bt_csn);
188 		switch (oldbad.bt_flag) {
189 
190 		case (u_short)-1:
191 			printf("alignment cartridge\n");
192 			break;
193 
194 		case DKBAD_MAGIC:
195 			break;
196 
197 		default:
198 			printf("bt_flag=%x(16)?\n", oldbad.bt_flag);
199 			break;
200 		}
201 		bt = oldbad.bt_bad;
202 		for (i = 0; i < NBT_BAD; i++) {
203 			bad = (bt->bt_cyl<<16) + bt->bt_trksec;
204 			if (bad < 0)
205 				break;
206 			printf("sn=%lld, cn=%d, tn=%d, sn=%d\n",
207 			    (long long)badsn(bt),
208 			    bt->bt_cyl, bt->bt_trksec>>8, bt->bt_trksec&0xff);
209 			bt++;
210 		}
211 		(void) checkold();
212 		exit(0);
213 	}
214 	if (add) {
215 		/*
216 		 * Read in the old badsector table.
217 		 * Verify that it makes sense, and the bad sectors
218 		 * are in order.  Copy the old table to the new one.
219 		 */
220 		(void) getold(f, &oldbad);
221 		i = checkold();
222 		if (verbose)
223 			printf("Had %d bad sectors, adding %d\n", i, argc);
224 		if (i + argc > NBT_BAD) {
225 			printf("bad144: not enough room for %d more sectors\n",
226 				argc);
227 			printf("limited to %d by information format\n",
228 			    NBT_BAD);
229 			exit(1);
230 		}
231 		curbad = oldbad;
232 	} else {
233 		curbad.bt_csn = atoi(*argv++);
234 		argc--;
235 		curbad.bt_mbz = 0;
236 		curbad.bt_flag = DKBAD_MAGIC;
237 		if (argc > NBT_BAD) {
238 			printf("bad144: too many bad sectors specified\n");
239 			printf("limited to %d by information format\n",
240 			    NBT_BAD);
241 			exit(1);
242 		}
243 		i = 0;
244 	}
245 	errs = 0;
246 	new = argc;
247 	while (argc > 0) {
248 		sn = atoi(*argv++);
249 		argc--;
250 		if (sn < 0 || sn >= size) {
251 			printf("%lld: out of range [0,%lld) for disk %s\n",
252 			    (long long)sn, (long long)size, dp->d_typename);
253 			errs++;
254 			continue;
255 		}
256 #ifdef __vax__
257 		bn[i] = sn;
258 #endif
259 		curbad.bt_bad[i].bt_cyl = sn / (dp->d_nsectors*dp->d_ntracks);
260 		sn %= (dp->d_nsectors*dp->d_ntracks);
261 		curbad.bt_bad[i].bt_trksec =
262 		    ((sn/dp->d_nsectors) << 8) + (sn%dp->d_nsectors);
263 		i++;
264 	}
265 	if (errs)
266 		exit(1);
267 	nbad = i;
268 	while (i < NBT_BAD) {
269 		curbad.bt_bad[i].bt_trksec = -1;
270 		curbad.bt_bad[i].bt_cyl = -1;
271 		i++;
272 	}
273 	if (add) {
274 		/*
275 		 * Sort the new bad sectors into the list.
276 		 * Then shuffle the replacement sectors so that
277 		 * the previous bad sectors get the same replacement data.
278 		 */
279 		qsort((char *)curbad.bt_bad, nbad, sizeof (struct bt_bad),
280 		    compare);
281 		if (dups)
282 			errx(3, "bad sectors have been duplicated; "
283 			    "can't add existing sectors");
284 		shift(f, nbad, nbad-new);
285 	}
286 	if (badfile == -1)
287 		i = 0;
288 	else
289 		i = badfile * 2;
290 	for (; i < 10 && i < (int)dp->d_nsectors; i += 2) {
291 		if (lseek(f,
292 		    (off_t)(dp->d_secsize * (size - dp->d_nsectors + i)),
293 		    SEEK_SET) < 0)
294 			err(4, "lseek");
295 		if (verbose)
296 			printf("write badsect file at %lld\n",
297 				(long long)size - dp->d_nsectors + i);
298 		if (nflag == 0 && write(f, (caddr_t)&curbad, sizeof(curbad)) !=
299 		    sizeof(curbad))
300 			err(4, "write bad sector file %d", i/2);
301 		if (badfile != -1)
302 			break;
303 	}
304 #ifdef __vax__
305 	if (nflag == 0 && fflag)
306 		for (i = nbad - new; i < nbad; i++)
307 			format(f, bn[i]);
308 #endif
309 #ifdef DIOCSBAD
310 	if (nflag == 0 && ioctl(f, DIOCSBAD, (caddr_t)&curbad) < 0)
311 		warnx("Can't sync bad-sector file; reboot for changes "
312 		    "to take effect");
313 #endif
314 	if ((dp->d_flags & D_BADSECT) == 0 && nflag == 0) {
315 		dp->d_flags |= D_BADSECT;
316 		if (ioctl(f, DIOCWDINFO, dp) < 0) {
317 			warn("label");
318 			errx(1,
319 			    "Can't write label to enable bad sector handling");
320 		}
321 	}
322 	return (0);
323 }
324 
325 static daddr_t
326 getold(int f, struct dkbad *bad)
327 {
328 	int i;
329 	daddr_t sn;
330 
331 	if (badfile == -1)
332 		i = 0;
333 	else
334 		i = badfile * 2;
335 	for (; i < 10 && i < (int)dp->d_nsectors; i += 2) {
336 		sn = size - dp->d_nsectors + i;
337 		if (lseek(f, (off_t)(sn * dp->d_secsize), SEEK_SET) < 0)
338 			err(4, "lseek");
339 		if ((size_t)read(f, (char *) bad, dp->d_secsize) == dp->d_secsize) {
340 			if (i > 0)
341 				printf("Using bad-sector file %d\n", i/2);
342 			return(sn);
343 		}
344 		warn("read bad sector file at sn %lld", (long long)sn);
345 		if (badfile != -1)
346 			break;
347 	}
348 	errx(1, "%s: can't read bad block info", diskname);
349 	/*NOTREACHED*/
350 }
351 
352 static int
353 checkold(void)
354 {
355 	int i;
356 	struct bt_bad *bt;
357 	daddr_t sn, lsn;
358 	int errors = 0, warned = 0;
359 
360 	lsn = 0;
361 	if (oldbad.bt_flag != DKBAD_MAGIC) {
362 		warnx("%s: bad flag in bad-sector table", diskname);
363 		errors++;
364 	}
365 	if (oldbad.bt_mbz != 0) {
366 		warnx("%s: bad magic number", diskname);
367 		errors++;
368 	}
369 	bt = oldbad.bt_bad;
370 	for (i = 0; i < NBT_BAD; i++, bt++) {
371 		if (bt->bt_cyl == 0xffff && bt->bt_trksec == 0xffff)
372 			break;
373 		if ((bt->bt_cyl >= dp->d_ncylinders) ||
374 		    ((bt->bt_trksec >> 8) >= dp->d_ntracks) ||
375 		    ((bt->bt_trksec & 0xff) >= dp->d_nsectors)) {
376 			warnx("cyl/trk/sect out of range in existing entry: "
377 			    "sn=%lld, cn=%d, tn=%d, sn=%d",
378 			    (long long)badsn(bt), bt->bt_cyl, bt->bt_trksec>>8,
379 			    bt->bt_trksec & 0xff);
380 			errors++;
381 		}
382 		sn = (bt->bt_cyl * dp->d_ntracks +
383 		    (bt->bt_trksec >> 8)) *
384 		    dp->d_nsectors + (bt->bt_trksec & 0xff);
385 		if (i > 0 && sn < lsn && !warned) {
386 		    warnx("bad sector file is out of order");
387 		    errors++;
388 		    warned++;
389 		}
390 		if (i > 0 && sn == lsn) {
391 		    warnx("bad sector file contains duplicates (sn %lld)",
392 			(long long)sn);
393 		    errors++;
394 		}
395 		lsn = sn;
396 	}
397 	if (errors)
398 		exit(1);
399 	return (i);
400 }
401 
402 /*
403  * Move the bad sector replacements
404  * to make room for the new bad sectors.
405  * new is the new number of bad sectors, old is the previous count.
406  */
407 static void
408 shift(int f, int new, int old)
409 {
410 	daddr_t repl;
411 
412 	/*
413 	 * First replacement is last sector of second-to-last track.
414 	 */
415 	repl = size - dp->d_nsectors - 1;
416 	new--; old--;
417 	while (new >= 0 && new != old) {
418 		if (old < 0 ||
419 		    compare(&curbad.bt_bad[new], &oldbad.bt_bad[old]) > 0) {
420 			/*
421 			 * Insert new replacement here-- copy original
422 			 * sector if requested and possible,
423 			 * otherwise write a zero block.
424 			 */
425 			if (!copy ||
426 			    !blkcopy(f, badsn(&curbad.bt_bad[new]), repl - new))
427 				blkzero(f, repl - new);
428 		} else {
429 			if (blkcopy(f, repl - old, repl - new) == 0)
430 			    warnx("Can't copy replacement sector %lld to %lld",
431 				(long long)repl-old, (long long)repl-new);
432 			old--;
433 		}
434 		new--;
435 	}
436 }
437 
438 static char *buf;
439 
440 /*
441  *  Copy disk sector s1 to s2.
442  */
443 static int
444 blkcopy(int f, daddr_t s1, daddr_t s2)
445 {
446 	int tries, n;
447 
448 	if (buf == NULL) {
449 		buf = malloc((unsigned)dp->d_secsize);
450 		if (buf == NULL)
451 			errx(20, "Out of memory");
452 	}
453 	for (tries = 0; tries < RETRIES; tries++) {
454 		if (lseek(f, (off_t)(dp->d_secsize * s1), SEEK_SET) < 0)
455 			err(4, "lseek");
456 		if ((size_t)(n = read(f, buf, dp->d_secsize)) == dp->d_secsize)
457 			break;
458 	}
459 	if ((size_t)n != dp->d_secsize) {
460 		if (n < 0)
461 			err(4, "can't read sector, %lld", (long long)s1);
462 		else
463 			errx(4, "can't read sector, %lld", (long long)s1);
464 		return(0);
465 	}
466 	if (lseek(f, (off_t)(dp->d_secsize * s2), SEEK_SET) < 0)
467 		err(4, "lseek");
468 	if (verbose)
469 		printf("copying %lld to %lld\n", (long long)s1, (long long)s2);
470 	if (nflag == 0 && (size_t)write(f, buf, dp->d_secsize) != dp->d_secsize) {
471 		warn("can't write replacement sector, %lld", (long long)s2);
472 		return(0);
473 	}
474 	return(1);
475 }
476 
477 static void
478 blkzero(int f, daddr_t sn)
479 {
480 	char *zbuf;
481 
482 	zbuf = calloc(1, (unsigned int)dp->d_secsize);
483 	if (zbuf == NULL)
484 		errx(20, "Out of memory");
485 	if (lseek(f, (off_t)(dp->d_secsize * sn), SEEK_SET) < 0) {
486 		free(zbuf);
487 		err(4, "lseek");
488 	}
489 	if (verbose)
490 		printf("zeroing %lld\n", (long long)sn);
491 	if (nflag == 0 && (size_t)write(f, zbuf, dp->d_secsize) != dp->d_secsize)
492 		warn("can't write replacement sector, %lld",
493 		    (long long)sn);
494 	free(zbuf);
495 }
496 
497 static int
498 compare(const void *v1, const void *v2)
499 {
500 	const struct bt_bad *b1 = v1;
501 	const struct bt_bad *b2 = v2;
502 
503 	if (b1->bt_cyl > b2->bt_cyl)
504 		return(1);
505 	if (b1->bt_cyl < b2->bt_cyl)
506 		return(-1);
507 	if (b1->bt_trksec == b2->bt_trksec)
508 		dups++;
509 	return (b1->bt_trksec - b2->bt_trksec);
510 }
511 
512 static daddr_t
513 badsn(const struct bt_bad *bt)
514 {
515 
516 	return ((bt->bt_cyl * dp->d_ntracks
517 		+ (bt->bt_trksec >> 8)) * dp->d_nsectors
518 		+ (bt->bt_trksec & 0xff));
519 }
520 
521 #ifdef __vax__
522 
523 struct rp06hdr {
524 	short	h_cyl;
525 	short	h_trksec;
526 	short	h_key1;
527 	short	h_key2;
528 	char	h_data[512];
529 #define	RP06_FMT	010000		/* 1 == 16 bit, 0 == 18 bit */
530 };
531 
532 /*
533  * Most massbus and unibus drives
534  * have headers of this form
535  */
536 struct hpuphdr {
537 	u_short	hpup_cyl;
538 	u_char	hpup_sect;
539 	u_char	hpup_track;
540 	char	hpup_data[512];
541 #define	HPUP_OKSECT	0xc000		/* this normally means sector is good */
542 #define	HPUP_16BIT	0x1000		/* 1 == 16 bit format */
543 };
544 
545 static int rp06format(struct formats *, struct disklabel *, daddr_t, char *, int);
546 static int hpupformat(struct formats *, struct disklabel *, daddr_t, char *, int);
547 
548 static struct	formats {
549 	char	*f_name;		/* disk name */
550 	int	f_bufsize;		/* size of sector + header */
551 	int	f_bic;			/* value to bic in hpup_cyl */
552 	int	(*f_routine)();		/* routine for special handling */
553 } formats[] = {
554 	{ "rp06",	sizeof (struct rp06hdr), RP06_FMT,	rp06format },
555 	{ "eagle",	sizeof (struct hpuphdr), HPUP_OKSECT,	hpupformat },
556 	{ "capricorn",	sizeof (struct hpuphdr), HPUP_OKSECT,	hpupformat },
557 	{ "rm03",	sizeof (struct hpuphdr), HPUP_OKSECT,	hpupformat },
558 	{ "rm05",	sizeof (struct hpuphdr), HPUP_OKSECT,	hpupformat },
559 	{ "9300",	sizeof (struct hpuphdr), HPUP_OKSECT,	hpupformat },
560 	{ "9766",	sizeof (struct hpuphdr), HPUP_OKSECT,	hpupformat },
561 	{ 0, 0, 0, 0 }
562 };
563 
564 /*ARGSUSED*/
565 static int
566 hpupformat(struct formats *fp, struct disklabel *dp, daddr_t blk, char *buf,
567 	   int count)
568 {
569 	struct hpuphdr *hdr = (struct hpuphdr *)buf;
570 	int sect;
571 
572 	if (count < sizeof(struct hpuphdr)) {
573 		hdr->hpup_cyl = (HPUP_OKSECT | HPUP_16BIT) |
574 			(blk / (dp->d_nsectors * dp->d_ntracks));
575 		sect = blk % (dp->d_nsectors * dp->d_ntracks);
576 		hdr->hpup_track = (u_char)(sect / dp->d_nsectors);
577 		hdr->hpup_sect = (u_char)(sect % dp->d_nsectors);
578 	}
579 	return (0);
580 }
581 
582 /*ARGSUSED*/
583 static int
584 rp06format(struct formats *fp, struct disklabel *dp, daddr_t blk, char *buf,
585 	   int count)
586 {
587 
588 	if (count < sizeof(struct rp06hdr)) {
589 		warnx("Can't read header on blk %d, can't reformat", blk);
590 		return (-1);
591 	}
592 	return (0);
593 }
594 
595 static void
596 format(int fd, daddr_t blk)
597 {
598 	struct formats *fp;
599 	static char *buf;
600 	static char bufsize;
601 	struct format_op fop;
602 	int n;
603 
604 	for (fp = formats; fp->f_name; fp++)
605 		if (strcmp(dp->d_typename, fp->f_name) == 0)
606 			break;
607 	if (fp->f_name == 0)
608 		errx(2, "don't know how to format %s disks", dp->d_typename);
609 	if (buf && bufsize < fp->f_bufsize) {
610 		free(buf);
611 		buf = NULL;
612 	}
613 	if (buf == NULL)
614 		buf = malloc((unsigned)fp->f_bufsize);
615 	if (buf == NULL)
616 		errx(3, "can't allocate sector buffer");
617 	bufsize = fp->f_bufsize;
618 	/*
619 	 * Here we do the actual formatting.  All we really
620 	 * do is rewrite the sector header and flag the bad sector
621 	 * according to the format table description.  If a special
622 	 * purpose format routine is specified, we allow it to
623 	 * process the sector as well.
624 	 */
625 	if (verbose)
626 		printf("format blk %d\n", blk);
627 	memset((char *)&fop, 0, sizeof(fop));
628 	fop.df_buf = buf;
629 	fop.df_count = fp->f_bufsize;
630 	fop.df_startblk = blk;
631 	memset(buf, 0, fp->f_bufsize);
632 	if (ioctl(fd, DIOCRFORMAT, &fop) < 0)
633 		warn("read format");
634 	if (fp->f_routine &&
635 	    (*fp->f_routine)(fp, dp, blk, buf, fop.df_count) != 0)
636 		return;
637 	if (fp->f_bic) {
638 		struct hpuphdr *xp = (struct hpuphdr *)buf;
639 
640 		xp->hpup_cyl &= ~fp->f_bic;
641 	}
642 	if (nflag)
643 		return;
644 	memset((char *)&fop, 0, sizeof(fop));
645 	fop.df_buf = buf;
646 	fop.df_count = fp->f_bufsize;
647 	fop.df_startblk = blk;
648 	if (ioctl(fd, DIOCWFORMAT, &fop) < 0)
649 		err(4, "write format");
650 	if (fop.df_count != fp->f_bufsize)
651 		warn("write format %d", blk);
652 }
653 #endif
654 
655 static void
656 usage(void)
657 {
658 
659 	fprintf(stderr, "usage: bad144 [-%sv] disk [sno [bad ...]]\n"
660 	    "to read or overwrite the bad-sector table, e.g.: bad144 hp0\n"
661 	    "or bad144 -a [-c%sv] disk [bad ...]\n"
662 	    "where options are:\n"
663 	    "\t-a  add new bad sectors to the table\n"
664 	    "\t-c  copy original sector to replacement\n"
665 	    "%s"
666 	    "\t-v  verbose mode\n",
667 #ifdef __vax__
668 	    "f", "f", "\t-f  reformat listed sectors as bad\n"
669 #else
670 	    "", "", ""
671 #endif
672 	    );
673 	exit(1);
674 }
675