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