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