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