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