bad144.c revision 1.19 1 /* $NetBSD: bad144.c,v 1.19 2002/06/13 15:27:09 wiz 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.19 2002/06/13 15:27:09 wiz 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 %d in %s:\n",
189 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=%d, cn=%d, tn=%d, sn=%d\n", badsn(bt),
210 bt->bt_cyl, bt->bt_trksec>>8, bt->bt_trksec&0xff);
211 bt++;
212 }
213 (void) checkold();
214 exit(0);
215 }
216 if (add) {
217 /*
218 * Read in the old badsector table.
219 * Verify that it makes sense, and the bad sectors
220 * are in order. Copy the old table to the new one.
221 */
222 (void) getold(f, &oldbad);
223 i = checkold();
224 if (verbose)
225 printf("Had %d bad sectors, adding %d\n", i, argc);
226 if (i + argc > NBT_BAD) {
227 printf("bad144: not enough room for %d more sectors\n",
228 argc);
229 printf("limited to %d by information format\n",
230 NBT_BAD);
231 exit(1);
232 }
233 curbad = oldbad;
234 } else {
235 curbad.bt_csn = atoi(*argv++);
236 argc--;
237 curbad.bt_mbz = 0;
238 curbad.bt_flag = DKBAD_MAGIC;
239 if (argc > NBT_BAD) {
240 printf("bad144: too many bad sectors specified\n");
241 printf("limited to %d by information format\n",
242 NBT_BAD);
243 exit(1);
244 }
245 i = 0;
246 }
247 errs = 0;
248 new = argc;
249 while (argc > 0) {
250 sn = atoi(*argv++);
251 argc--;
252 if (sn < 0 || sn >= size) {
253 printf("%d: out of range [0,%d) for disk %s\n",
254 sn, size, dp->d_typename);
255 errs++;
256 continue;
257 }
258 bn[i] = sn;
259 curbad.bt_bad[i].bt_cyl = sn / (dp->d_nsectors*dp->d_ntracks);
260 sn %= (dp->d_nsectors*dp->d_ntracks);
261 curbad.bt_bad[i].bt_trksec =
262 ((sn/dp->d_nsectors) << 8) + (sn%dp->d_nsectors);
263 i++;
264 }
265 if (errs)
266 exit(1);
267 nbad = i;
268 while (i < NBT_BAD) {
269 curbad.bt_bad[i].bt_trksec = -1;
270 curbad.bt_bad[i].bt_cyl = -1;
271 i++;
272 }
273 if (add) {
274 /*
275 * Sort the new bad sectors into the list.
276 * Then shuffle the replacement sectors so that
277 * the previous bad sectors get the same replacement data.
278 */
279 qsort((char *)curbad.bt_bad, nbad, sizeof (struct bt_bad),
280 compare);
281 if (dups)
282 errx(3, "bad sectors have been duplicated; "
283 "can't add existing sectors");
284 shift(f, nbad, nbad-new);
285 }
286 if (badfile == -1)
287 i = 0;
288 else
289 i = badfile * 2;
290 for (; i < 10 && i < dp->d_nsectors; i += 2) {
291 if (lseek(f,
292 (off_t)(dp->d_secsize * (size - dp->d_nsectors + i)),
293 SEEK_SET) < 0)
294 err(4, "lseek");
295 if (verbose)
296 printf("write badsect file at %d\n",
297 size - dp->d_nsectors + i);
298 if (nflag == 0 && write(f, (caddr_t)&curbad, sizeof(curbad)) !=
299 sizeof(curbad))
300 err(4, "write bad sector file %d", i/2);
301 if (badfile != -1)
302 break;
303 }
304 #ifdef __vax__
305 if (nflag == 0 && fflag)
306 for (i = nbad - new; i < nbad; i++)
307 format(f, bn[i]);
308 #endif
309 #ifdef DIOCSBAD
310 if (nflag == 0 && ioctl(f, DIOCSBAD, (caddr_t)&curbad) < 0)
311 warnx("Can't sync bad-sector file; reboot for changes "
312 "to take effect");
313 #endif
314 if ((dp->d_flags & D_BADSECT) == 0 && nflag == 0) {
315 dp->d_flags |= D_BADSECT;
316 if (ioctl(f, DIOCWDINFO, dp) < 0) {
317 warn("label");
318 errx(1,
319 "Can't write label to enable bad sector handling");
320 }
321 }
322 return (0);
323 }
324
325 daddr_t
326 getold(int f, struct dkbad *bad)
327 {
328 int i;
329 daddr_t sn;
330
331 if (badfile == -1)
332 i = 0;
333 else
334 i = badfile * 2;
335 for (; i < 10 && i < dp->d_nsectors; i += 2) {
336 sn = size - dp->d_nsectors + i;
337 if (lseek(f, (off_t)(sn * dp->d_secsize), SEEK_SET) < 0)
338 err(4, "lseek");
339 if (read(f, (char *) bad, dp->d_secsize) == dp->d_secsize) {
340 if (i > 0)
341 printf("Using bad-sector file %d\n", i/2);
342 return(sn);
343 }
344 warn("read bad sector file at sn %d", sn);
345 if (badfile != -1)
346 break;
347 }
348 errx(1, "%s: can't read bad block info", name);
349 /*NOTREACHED*/
350 }
351
352 int
353 checkold(void)
354 {
355 int i;
356 struct bt_bad *bt;
357 daddr_t sn, lsn;
358 int errors = 0, warned = 0;
359
360 lsn = 0;
361 if (oldbad.bt_flag != DKBAD_MAGIC) {
362 warnx("%s: bad flag in bad-sector table", name);
363 errors++;
364 }
365 if (oldbad.bt_mbz != 0) {
366 warnx("%s: bad magic number", name);
367 errors++;
368 }
369 bt = oldbad.bt_bad;
370 for (i = 0; i < NBT_BAD; i++, bt++) {
371 if (bt->bt_cyl == 0xffff && bt->bt_trksec == 0xffff)
372 break;
373 if ((bt->bt_cyl >= dp->d_ncylinders) ||
374 ((bt->bt_trksec >> 8) >= dp->d_ntracks) ||
375 ((bt->bt_trksec & 0xff) >= dp->d_nsectors)) {
376 warnx("cyl/trk/sect out of range in existing entry: "
377 "sn=%d, cn=%d, tn=%d, sn=%d",
378 badsn(bt), bt->bt_cyl, bt->bt_trksec>>8,
379 bt->bt_trksec & 0xff);
380 errors++;
381 }
382 sn = (bt->bt_cyl * dp->d_ntracks +
383 (bt->bt_trksec >> 8)) *
384 dp->d_nsectors + (bt->bt_trksec & 0xff);
385 if (i > 0 && sn < lsn && !warned) {
386 warnx("bad sector file is out of order");
387 errors++;
388 warned++;
389 }
390 if (i > 0 && sn == lsn) {
391 warnx("bad sector file contains duplicates (sn %d)", sn);
392 errors++;
393 }
394 lsn = sn;
395 }
396 if (errors)
397 exit(1);
398 return (i);
399 }
400
401 /*
402 * Move the bad sector replacements
403 * to make room for the new bad sectors.
404 * new is the new number of bad sectors, old is the previous count.
405 */
406 void
407 shift(int f, int new, int old)
408 {
409 daddr_t repl;
410
411 /*
412 * First replacement is last sector of second-to-last track.
413 */
414 repl = size - dp->d_nsectors - 1;
415 new--; old--;
416 while (new >= 0 && new != old) {
417 if (old < 0 ||
418 compare(&curbad.bt_bad[new], &oldbad.bt_bad[old]) > 0) {
419 /*
420 * Insert new replacement here-- copy original
421 * sector if requested and possible,
422 * otherwise write a zero block.
423 */
424 if (!copy ||
425 !blkcopy(f, badsn(&curbad.bt_bad[new]), repl - new))
426 blkzero(f, repl - new);
427 } else {
428 if (blkcopy(f, repl - old, repl - new) == 0)
429 warnx("Can't copy replacement sector %d to %d",
430 repl-old, repl-new);
431 old--;
432 }
433 new--;
434 }
435 }
436
437 char *buf;
438
439 /*
440 * Copy disk sector s1 to s2.
441 */
442 int
443 blkcopy(int f, daddr_t s1, daddr_t s2)
444 {
445 int tries, n;
446
447 if (buf == (char *)NULL) {
448 buf = malloc((unsigned)dp->d_secsize);
449 if (buf == (char *)NULL)
450 errx(20, "Out of memory");
451 }
452 for (tries = 0; tries < RETRIES; tries++) {
453 if (lseek(f, (off_t)(dp->d_secsize * s1), SEEK_SET) < 0)
454 err(4, "lseek");
455 if ((n = read(f, buf, dp->d_secsize)) == dp->d_secsize)
456 break;
457 }
458 if (n != dp->d_secsize) {
459 if (n < 0)
460 err(4, "can't read sector, %d", s1);
461 else
462 errx(4, "can't read sector, %d", s1);
463 return(0);
464 }
465 if (lseek(f, (off_t)(dp->d_secsize * s2), SEEK_SET) < 0)
466 err(4, "lseek");
467 if (verbose)
468 printf("copying %d to %d\n", s1, s2);
469 if (nflag == 0 && write(f, buf, dp->d_secsize) != dp->d_secsize) {
470 warn("can't write replacement sector, %d", s2);
471 return(0);
472 }
473 return(1);
474 }
475
476 void
477 blkzero(int f, daddr_t sn)
478 {
479 char *zbuf;
480
481 zbuf = calloc(1, (unsigned int)dp->d_secsize);
482 if (zbuf == NULL)
483 errx(20, "Out of memory");
484 if (lseek(f, (off_t)(dp->d_secsize * sn), SEEK_SET) < 0)
485 err(4, "lseek");
486 if (verbose)
487 printf("zeroing %d\n", sn);
488 if (nflag == 0 && write(f, zbuf, dp->d_secsize) != dp->d_secsize)
489 warn("can't write replacement sector, %d", sn);
490 }
491
492 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 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 int rp06format(struct formats *, struct disklabel *, daddr_t, char *, int);
540 int hpupformat(struct formats *, struct disklabel *, daddr_t, char *, int);
541
542 struct formats {
543 char *f_name; /* disk name */
544 int f_bufsize; /* size of sector + header */
545 int f_bic; /* value to bic in hpup_cyl */
546 int (*f_routine)(); /* routine for special handling */
547 } formats[] = {
548 { "rp06", sizeof (struct rp06hdr), RP06_FMT, rp06format },
549 { "eagle", sizeof (struct hpuphdr), HPUP_OKSECT, hpupformat },
550 { "capricorn", sizeof (struct hpuphdr), HPUP_OKSECT, hpupformat },
551 { "rm03", sizeof (struct hpuphdr), HPUP_OKSECT, hpupformat },
552 { "rm05", sizeof (struct hpuphdr), HPUP_OKSECT, hpupformat },
553 { "9300", sizeof (struct hpuphdr), HPUP_OKSECT, hpupformat },
554 { "9766", sizeof (struct hpuphdr), HPUP_OKSECT, hpupformat },
555 { 0, 0, 0, 0 }
556 };
557
558 /*ARGSUSED*/
559 int
560 hpupformat(struct formats *fp, struct disklabel *dp, daddr_t blk, char *buf,
561 int count)
562 {
563 struct hpuphdr *hdr = (struct hpuphdr *)buf;
564 int sect;
565
566 if (count < sizeof(struct hpuphdr)) {
567 hdr->hpup_cyl = (HPUP_OKSECT | HPUP_16BIT) |
568 (blk / (dp->d_nsectors * dp->d_ntracks));
569 sect = blk % (dp->d_nsectors * dp->d_ntracks);
570 hdr->hpup_track = (u_char)(sect / dp->d_nsectors);
571 hdr->hpup_sect = (u_char)(sect % dp->d_nsectors);
572 }
573 return (0);
574 }
575
576 /*ARGSUSED*/
577 int
578 rp06format(struct formats *fp, struct disklabel *dp, daddr_t blk, char *buf,
579 int count)
580 {
581
582 if (count < sizeof(struct rp06hdr)) {
583 warnx("Can't read header on blk %d, can't reformat", blk);
584 return (-1);
585 }
586 return (0);
587 }
588
589 void
590 format(int fd, daddr_t blk)
591 {
592 struct formats *fp;
593 static char *buf;
594 static char bufsize;
595 struct format_op fop;
596 int n;
597
598 for (fp = formats; fp->f_name; fp++)
599 if (strcmp(dp->d_typename, fp->f_name) == 0)
600 break;
601 if (fp->f_name == 0)
602 errx(2, "don't know how to format %s disks", dp->d_typename);
603 if (buf && bufsize < fp->f_bufsize) {
604 free(buf);
605 buf = NULL;
606 }
607 if (buf == NULL)
608 buf = malloc((unsigned)fp->f_bufsize);
609 if (buf == NULL)
610 errx(3, "can't allocate sector buffer");
611 bufsize = fp->f_bufsize;
612 /*
613 * Here we do the actual formatting. All we really
614 * do is rewrite the sector header and flag the bad sector
615 * according to the format table description. If a special
616 * purpose format routine is specified, we allow it to
617 * process the sector as well.
618 */
619 if (verbose)
620 printf("format blk %d\n", blk);
621 memset((char *)&fop, 0, sizeof(fop));
622 fop.df_buf = buf;
623 fop.df_count = fp->f_bufsize;
624 fop.df_startblk = blk;
625 memset(buf, 0, fp->f_bufsize);
626 if (ioctl(fd, DIOCRFORMAT, &fop) < 0)
627 warn("read format");
628 if (fp->f_routine &&
629 (*fp->f_routine)(fp, dp, blk, buf, fop.df_count) != 0)
630 return;
631 if (fp->f_bic) {
632 struct hpuphdr *xp = (struct hpuphdr *)buf;
633
634 xp->hpup_cyl &= ~fp->f_bic;
635 }
636 if (nflag)
637 return;
638 memset((char *)&fop, 0, sizeof(fop));
639 fop.df_buf = buf;
640 fop.df_count = fp->f_bufsize;
641 fop.df_startblk = blk;
642 if (ioctl(fd, DIOCWFORMAT, &fop) < 0)
643 err(4, "write format");
644 if (fop.df_count != fp->f_bufsize)
645 warn("write format %d", blk);
646 }
647 #endif
648
649 void
650 usage(void)
651 {
652
653 fprintf(stderr, "usage: bad144 [-%sv] disk [sno [bad ...]]\n"
654 "to read or overwrite the bad-sector table, e.g.: bad144 hp0\n"
655 "or bad144 -a [-c%sv] disk [bad ...]\n"
656 "where options are:\n"
657 "\t-a add new bad sectors to the table\n"
658 "\t-c copy original sector to replacement\n"
659 "%s"
660 "\t-v verbose mode\n",
661 #ifdef __vax__
662 "f", "f", "\t-f reformat listed sectors as bad\n"
663 #else
664 "", "", ""
665 #endif
666 );
667 exit(1);
668 }
669