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