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