1 /* $NetBSD: main.c,v 1.19 2008/07/20 01:20:22 lukem Exp $ */ 2 3 /* 4 * Copyright (c) 2006 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Julio M. Merino Vidal. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (c) 1987, 1993 34 * The Regents of the University of California. All rights reserved. 35 * 36 * This code is derived from software contributed to Berkeley by 37 * Symmetric Computer Systems. 38 * 39 * Redistribution and use in source and binary forms, with or without 40 * modification, are permitted provided that the following conditions 41 * are met: 42 * 1. Redistributions of source code must retain the above copyright 43 * notice, this list of conditions and the following disclaimer. 44 * 2. Redistributions in binary form must reproduce the above copyright 45 * notice, this list of conditions and the following disclaimer in the 46 * documentation and/or other materials provided with the distribution. 47 * 3. Neither the name of the University nor the names of its contributors 48 * may be used to endorse or promote products derived from this software 49 * without specific prior written permission. 50 * 51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 61 * SUCH DAMAGE. 62 */ 63 64 #if HAVE_NBTOOL_CONFIG_H 65 #include "nbtool_config.h" 66 #endif 67 68 #include <sys/cdefs.h> 69 #ifndef lint 70 __COPYRIGHT("@(#) Copyright (c) 1987, 1993\ 71 The Regents of the University of California. All rights reserved."); 72 #endif /* not lint */ 73 74 #ifndef lint 75 #if 0 76 static char sccsid[] = "@(#)disklabel.c 8.4 (Berkeley) 5/4/95"; 77 /* from static char sccsid[] = "@(#)disklabel.c 1.2 (Symmetric) 11/28/85"; */ 78 #else 79 __RCSID("$NetBSD: main.c,v 1.19 2008/07/20 01:20:22 lukem Exp $"); 80 #endif 81 #endif /* not lint */ 82 83 #include <sys/param.h> 84 #include <sys/file.h> 85 #include <sys/stat.h> 86 #include <sys/wait.h> 87 #define DKTYPENAMES 88 #define FSTYPENAMES 89 90 #include <ctype.h> 91 #include <err.h> 92 #include <errno.h> 93 #include <signal.h> 94 #include <string.h> 95 #include <stdio.h> 96 #include <stdlib.h> 97 #include <limits.h> 98 #include <unistd.h> 99 100 #include <ufs/ufs/dinode.h> 101 #include <ufs/ffs/fs.h> 102 103 #if HAVE_NBTOOL_CONFIG_H 104 #include <nbinclude/sys/disklabel.h> 105 #include <nbinclude/sys/disklabel_acorn.h> 106 #include <nbinclude/sys/bootblock.h> 107 #include "../../include/disktab.h" 108 #else 109 #include <sys/ioctl.h> 110 #include <sys/disklabel.h> 111 #include <sys/disklabel_acorn.h> 112 #include <sys/bootblock.h> 113 #include <util.h> 114 #include <disktab.h> 115 #endif /* HAVE_NBTOOL_CONFIG_H */ 116 117 #include "pathnames.h" 118 #include "extern.h" 119 #include "dkcksum.h" 120 121 /* 122 * Disklabel: read and write disklabels. 123 * The label is usually placed on one of the first sectors of the disk. 124 * Many machines also place a bootstrap in the same area, 125 * in which case the label is embedded in the bootstrap. 126 * The bootstrap source must leave space at the proper offset 127 * for the label on such machines. 128 */ 129 130 #ifndef BBSIZE 131 #define BBSIZE 8192 /* size of boot area, with label */ 132 #endif 133 134 #define DISKMAGIC_REV bswap32(DISKMAGIC) 135 /* To delete a label, we just invert the magic numbers */ 136 #define DISKMAGIC_DELETED (~DISKMAGIC) 137 #define DISKMAGIC_DELETED_REV bswap32(~DISKMAGIC) 138 139 #define DEFEDITOR _PATH_VI 140 141 char specname[MAXPATHLEN]; 142 143 /* Some global data, all too hard to pass about */ 144 char bootarea[BBSIZE]; /* Buffer matching part of disk */ 145 int bootarea_len; /* Number of bytes we actually read */ 146 static struct disklabel lab; /* The label we have updated */ 147 148 static int Aflag; /* Action all labels */ 149 static int Fflag; /* Read/write from file */ 150 static int rflag; /* Read/write direct from disk */ 151 static int tflag; /* Format output as disktab */ 152 int Cflag; /* CHS format output */ 153 static int Dflag; /* Delete old labels (use with write) */ 154 static int Iflag; /* Read/write direct, but default if absent */ 155 static int lflag; /* List all known file system types and exit */ 156 static int mflag; /* Expect disk to contain an MBR */ 157 static int verbose; 158 static int read_all; /* set if op = READ && Aflag */ 159 160 static int write_label(int); 161 static int readlabel_direct(int); 162 static void writelabel_direct(int); 163 static int update_label(int, u_int, u_int); 164 static struct disklabel *find_label(int, u_int); 165 166 static void makedisktab(FILE *, struct disklabel *); 167 static void makelabel(const char *, const char *); 168 static void l_perror(const char *); 169 static void readlabel(int); 170 static int edit(int); 171 static int editit(const char *); 172 static char *skip(char *); 173 static char *word(char *); 174 static int getasciilabel(FILE *, struct disklabel *); 175 static void usage(void); 176 static int qsort_strcmp(const void *, const void *); 177 static int getulong(const char *, char, char **, 178 unsigned long *, unsigned long); 179 #define GETNUM32(a, v) getulong(a, '\0', NULL, v, UINT32_MAX) 180 #define GETNUM16(a, v) getulong(a, '\0', NULL, v, UINT16_MAX) 181 #define GETNUM8(a, v) getulong(a, '\0', NULL, v, UINT8_MAX) 182 183 static int set_writable_fd = -1; 184 185 #if HAVE_NBTOOL_CONFIG_H 186 #define GETLABELOFFSET() LABELOFFSET 187 #define GETLABELSECTOR() LABELSECTOR 188 #else /* HAVE_NBTOOL_CONFIG_H */ 189 #define GETLABELOFFSET() getlabeloffset() 190 #define GETLABELSECTOR() getlabelsector() 191 #endif 192 193 /* Default location for label - only used if we don't find one to update */ 194 #define LABEL_OFFSET (dklabel_getlabelsector() * DEV_BSIZE + dklabel_getlabeloffset()) 195 196 /* 197 * For portability it doesn't make sense to use any other value.... 198 * Except, maybe, the size of a physical sector. 199 * This value is used if we have to write a label to the start of an mbr ptn. 200 */ 201 #ifndef LABELOFFSET_MBR 202 #define LABELOFFSET_MBR 512 203 #endif 204 205 #if HAVE_NBTOOL_CONFIG_H 206 static int 207 opendisk(const char *path, int flags, char *buf, int buflen, int cooked) 208 { 209 int f; 210 f = open(path, flags, 0); 211 strlcpy(buf, path, buflen); 212 return f; 213 } 214 215 static int 216 dk_ioctl(int f, void *arg) 217 { 218 errno = ENOTTY; 219 return -1; 220 } 221 #define dk_ioctl(f, cmd, arg) dk_ioctl(f, arg) 222 #else 223 #define dk_ioctl(f, cmd, arg) ioctl(f, cmd, arg) 224 #endif /* HAVE_NBTOOL_CONFIG_H */ 225 226 static daddr_t 227 dklabel_getlabelsector(void) 228 { 229 unsigned long int nval; 230 char *end; 231 const char *val; 232 233 if ((val = getenv("DISKLABELSECTOR")) == NULL) 234 return GETLABELSECTOR(); 235 if ((nval = strtoul(val, &end, 10)) == ULONG_MAX && errno == ERANGE) 236 err(EXIT_FAILURE, "DISKLABELSECTOR in environment"); 237 return nval; 238 } 239 240 static off_t 241 dklabel_getlabeloffset(void) 242 { 243 unsigned long int nval; 244 char *end; 245 const char *val; 246 247 if ((val = getenv("DISKLABELOFFSET")) == NULL) 248 return GETLABELOFFSET(); 249 if ((nval = strtoul(val, &end, 10)) == ULONG_MAX && errno == ERANGE) 250 err(EXIT_FAILURE, "DISKLABELOFFSET in environment"); 251 return nval; 252 } 253 254 static void 255 clear_writable(void) 256 { 257 static int zero = 0; 258 dk_ioctl(set_writable_fd, DIOCWLABEL, &zero); 259 } 260 261 int 262 main(int argc, char *argv[]) 263 { 264 FILE *t; 265 int ch, f, error; 266 char *dkname; 267 struct stat sb; 268 int writable; 269 enum { 270 UNSPEC, EDIT, READ, RESTORE, SETWRITABLE, SETREADONLY, 271 WRITE, INTERACT, DELETE 272 } op = UNSPEC, old_op; 273 274 #ifdef USE_MBR 275 mflag = 1; 276 #endif 277 #if HAVE_NBTOOL_CONFIG_H 278 /* We must avoid doing any ioctl requests */ 279 Fflag = rflag = 1; 280 #endif 281 282 error = 0; 283 while ((ch = getopt(argc, argv, "ACDFINRWb:ef:ilmrs:tvw")) != -1) { 284 old_op = op; 285 switch (ch) { 286 case 'A': /* Action all labels */ 287 Aflag = 1; 288 rflag = 1; 289 break; 290 case 'C': /* Display in CHS format */ 291 Cflag = 1; 292 break; 293 case 'D': /* Delete all existing labels */ 294 Dflag = 1; 295 rflag = 1; 296 break; 297 case 'F': /* Treat 'disk' as a regular file */ 298 Fflag = 1; 299 rflag = 1; /* Force direct access */ 300 break; 301 case 'I': /* Use default label if none found */ 302 Iflag = 1; 303 rflag = 1; /* Implies direct access */ 304 break; 305 case 'R': /* Restore label from text file */ 306 op = RESTORE; 307 break; 308 case 'N': /* Disallow writes to label sector */ 309 op = SETREADONLY; 310 break; 311 case 'W': /* Allow writes to label sector */ 312 op = SETWRITABLE; 313 break; 314 case 'e': /* Edit label with $EDITOR */ 315 op = EDIT; 316 break; 317 case 'f': /* Name of disktab file */ 318 if (setdisktab(optarg) == -1) 319 usage(); 320 break; 321 case 'i': /* Edit using built-in editor */ 322 op = INTERACT; 323 break; 324 case 'l': /* List all known file system types and exit */ 325 lflag = 1; 326 break; 327 case 'm': /* Expect disk to have an MBR */ 328 mflag ^= 1; 329 break; 330 case 'r': /* Read/write label directly from disk */ 331 rflag = 1; 332 break; 333 case 't': /* Format output as a disktab entry */ 334 tflag = 1; 335 break; 336 case 'v': /* verbose/diag output */ 337 verbose++; 338 break; 339 case 'w': /* Write label based on disktab entry */ 340 op = WRITE; 341 break; 342 case '?': 343 default: 344 usage(); 345 } 346 if (old_op != UNSPEC && old_op != op) 347 usage(); 348 } 349 argc -= optind; 350 argv += optind; 351 352 if (lflag) 353 exit(list_fs_types() ? EXIT_SUCCESS : EXIT_FAILURE); 354 355 if (op == UNSPEC) 356 op = Dflag ? DELETE : READ; 357 358 if (argc < 1) 359 usage(); 360 361 if (Iflag && op != EDIT && op != INTERACT) 362 usage(); 363 364 dkname = argv[0]; 365 f = opendisk(dkname, op == READ ? O_RDONLY : O_RDWR, 366 specname, sizeof specname, 0); 367 if (f < 0) 368 err(4, "%s", specname); 369 370 if (!Fflag && fstat(f, &sb) == 0 && S_ISREG(sb.st_mode)) 371 Fflag = rflag = 1; 372 373 switch (op) { 374 375 case DELETE: /* Remove all existing labels */ 376 if (argc != 1) 377 usage(); 378 Dflag = 2; 379 writelabel_direct(f); 380 break; 381 382 case EDIT: 383 if (argc != 1) 384 usage(); 385 readlabel(f); 386 error = edit(f); 387 break; 388 389 case INTERACT: 390 if (argc != 1) 391 usage(); 392 readlabel(f); 393 /* 394 * XXX: Fill some default values so checklabel does not fail 395 */ 396 if (lab.d_bbsize == 0) 397 lab.d_bbsize = BBSIZE; 398 if (lab.d_sbsize == 0) 399 lab.d_sbsize = SBLOCKSIZE; 400 interact(&lab, f); 401 break; 402 403 case READ: 404 if (argc != 1) 405 usage(); 406 read_all = Aflag; 407 readlabel(f); 408 if (read_all) 409 /* Label got printed in the bowels of readlabel */ 410 break; 411 if (tflag) 412 makedisktab(stdout, &lab); 413 else { 414 showinfo(stdout, &lab, specname); 415 showpartitions(stdout, &lab, Cflag); 416 } 417 error = checklabel(&lab); 418 if (error) 419 error += 100; 420 break; 421 422 case RESTORE: 423 if (argc != 2) 424 usage(); 425 if (!(t = fopen(argv[1], "r"))) 426 err(4, "%s", argv[1]); 427 if (getasciilabel(t, &lab)) 428 error = write_label(f); 429 else 430 error = 1; 431 break; 432 433 case SETREADONLY: 434 writable = 0; 435 goto do_diocwlabel; 436 case SETWRITABLE: 437 writable = 1; 438 do_diocwlabel: 439 if (argc != 1) 440 usage(); 441 if (dk_ioctl(f, DIOCWLABEL, &writable) < 0) 442 err(4, "ioctl DIOCWLABEL"); 443 break; 444 445 case WRITE: /* Create label from /etc/disktab entry & write */ 446 if (argc < 2 || argc > 3) 447 usage(); 448 makelabel(argv[1], argv[2]); 449 if (checklabel(&lab) == 0) 450 error = write_label(f); 451 else 452 error = 1; 453 break; 454 455 case UNSPEC: 456 usage(); 457 458 } 459 exit(error); 460 } 461 462 /* 463 * Construct a prototype disklabel from /etc/disktab. 464 */ 465 static void 466 makelabel(const char *type, const char *name) 467 { 468 struct disklabel *dp; 469 470 dp = getdiskbyname(type); 471 if (dp == NULL) 472 errx(1, "unknown disk type: %s", type); 473 lab = *dp; 474 475 /* d_packname is union d_boot[01], so zero */ 476 (void)memset(lab.d_packname, 0, sizeof(lab.d_packname)); 477 if (name) 478 (void)strncpy(lab.d_packname, name, sizeof(lab.d_packname)); 479 } 480 481 static int 482 write_label(int f) 483 { 484 int writable; 485 486 lab.d_magic = DISKMAGIC; 487 lab.d_magic2 = DISKMAGIC; 488 lab.d_checksum = 0; 489 lab.d_checksum = dkcksum(&lab); 490 491 if (rflag) { 492 /* Write the label directly to the disk */ 493 494 /* 495 * First set the kernel disk label, 496 * then write a label to the raw disk. 497 * If the SDINFO ioctl fails because it is unimplemented, 498 * keep going; otherwise, the kernel consistency checks 499 * may prevent us from changing the current (in-core) 500 * label. 501 */ 502 if (!Fflag && dk_ioctl(f, DIOCSDINFO, &lab) < 0 && 503 errno != ENODEV && errno != ENOTTY) { 504 l_perror("ioctl DIOCSDINFO"); 505 return (1); 506 } 507 /* 508 * write enable label sector before write (if necessary), 509 * disable after writing. 510 */ 511 writable = 1; 512 if (!Fflag) { 513 if (dk_ioctl(f, DIOCWLABEL, &writable) < 0) 514 perror("ioctl DIOCWLABEL"); 515 set_writable_fd = f; 516 atexit(clear_writable); 517 } 518 519 writelabel_direct(f); 520 521 /* 522 * Now issue a DIOCWDINFO. This will let the kernel convert the 523 * disklabel to some machdep format if needed. 524 */ 525 /* XXX: This is stupid! */ 526 if (!Fflag && dk_ioctl(f, DIOCWDINFO, &lab) < 0) { 527 l_perror("ioctl DIOCWDINFO"); 528 return (1); 529 } 530 } else { 531 /* Get the kernel to write the label */ 532 if (dk_ioctl(f, DIOCWDINFO, &lab) < 0) { 533 l_perror("ioctl DIOCWDINFO"); 534 return (1); 535 } 536 } 537 538 #ifdef __vax__ 539 if (lab.d_type == DTYPE_SMD && lab.d_flags & D_BADSECT && 540 lab.d_secsize == 512) { 541 /* Write the label to the odd sectors of the last track! */ 542 daddr_t alt; 543 int i; 544 uint8_t sec0[512]; 545 546 if (pread(f, sec0, 512, 0) < 512) { 547 warn("read master label to write alternates"); 548 return 0; 549 } 550 551 alt = lab.d_ncylinders * lab.d_secpercyl - lab.d_nsectors; 552 for (i = 1; i < 11 && i < lab.d_nsectors; i += 2) { 553 if (pwrite(f, sec0, 512, (off_t)(alt + i) * 512) < 512) 554 warn("alternate label %d write", i/2); 555 } 556 } 557 #endif /* __vax__ */ 558 559 return 0; 560 } 561 562 int 563 writelabel(int f, struct disklabel *lp) 564 { 565 if (lp != &lab) 566 lab = *lp; 567 return write_label(f); 568 } 569 570 static void 571 l_perror(const char *s) 572 { 573 574 switch (errno) { 575 576 case ESRCH: 577 warnx("%s: No disk label on disk;\n" 578 "use \"disklabel -I\" to install initial label", s); 579 break; 580 581 case EINVAL: 582 warnx("%s: Label magic number or checksum is wrong!\n" 583 "(disklabel or kernel is out of date?)", s); 584 break; 585 586 case EBUSY: 587 warnx("%s: Open partition would move or shrink", s); 588 break; 589 590 case EXDEV: 591 warnx("%s: Labeled partition or 'a' partition must start" 592 " at beginning of disk", s); 593 break; 594 595 default: 596 warn("%s", s); 597 break; 598 } 599 } 600 601 #ifdef NO_MBR_SUPPORT 602 #define process_mbr(f, action) 1 603 #else 604 /* 605 * Scan DOS/MBR partition table and extended partition list for NetBSD ptns. 606 */ 607 static int 608 process_mbr(int f, int (*action)(int, u_int)) 609 { 610 struct mbr_partition *dp; 611 struct mbr_sector mbr; 612 int rval = 1, res; 613 int part; 614 u_int ext_base, next_ext, this_ext, start; 615 616 ext_base = 0; 617 next_ext = 0; 618 for (;;) { 619 this_ext = next_ext; 620 next_ext = 0; 621 if (verbose > 1) 622 warnx("reading mbr sector %u", this_ext); 623 if (pread(f, &mbr, sizeof mbr, this_ext * (off_t)DEV_BSIZE) 624 != sizeof(mbr)) { 625 if (verbose) 626 warn("Can't read master boot record %d", 627 this_ext); 628 break; 629 } 630 631 /* Check if table is valid. */ 632 if (mbr.mbr_magic != htole16(MBR_MAGIC)) { 633 if (verbose) 634 warnx("Invalid signature in mbr record %d", 635 this_ext); 636 break; 637 } 638 639 dp = &mbr.mbr_parts[0]; 640 641 /* Find NetBSD partition(s). */ 642 for (part = 0; part < MBR_PART_COUNT; dp++, part++) { 643 start = le32toh(dp->mbrp_start); 644 switch (dp->mbrp_type) { 645 #ifdef COMPAT_386BSD_MBRPART 646 case MBR_PTYPE_386BSD: 647 if (ext_base != 0) 648 break; 649 /* FALLTHROUGH */ 650 #endif 651 case MBR_PTYPE_NETBSD: 652 res = action(f, this_ext + start); 653 if (res <= 0) 654 /* Found or failure */ 655 return res; 656 if (res > rval) 657 /* Keep largest value */ 658 rval = res; 659 break; 660 case MBR_PTYPE_EXT: 661 case MBR_PTYPE_EXT_LBA: 662 case MBR_PTYPE_EXT_LNX: 663 next_ext = start; 664 break; 665 default: 666 break; 667 } 668 } 669 if (next_ext == 0) 670 /* No more extended partitions */ 671 break; 672 next_ext += ext_base; 673 if (ext_base == 0) 674 ext_base = next_ext; 675 676 if (next_ext <= this_ext) { 677 if (verbose) 678 warnx("Invalid extended chain %x <= %x", 679 next_ext, this_ext); 680 break; 681 } 682 /* Maybe we should check against the disk size... */ 683 } 684 685 return rval; 686 } 687 688 static int 689 readlabel_mbr(int f, u_int sector) 690 { 691 struct disklabel *lp; 692 693 lp = find_label(f, sector); 694 if (lp == NULL) 695 return 1; 696 lab = *lp; 697 return 0; 698 } 699 700 static int 701 writelabel_mbr(int f, u_int sector) 702 { 703 return update_label(f, sector, mflag ? LABELOFFSET_MBR : ~0U) ? 2 : 0; 704 } 705 706 #endif /* !NO_MBR_SUPPORT */ 707 708 #ifndef USE_ACORN 709 #define get_filecore_partition(f) 0 710 #else 711 /* 712 * static int filecore_checksum(u_char *bootblock) 713 * 714 * Calculates the filecore boot block checksum. This is used to validate 715 * a filecore boot block on the disk. If a boot block is validated then 716 * it is used to locate the partition table. If the boot block is not 717 * validated, it is assumed that the whole disk is NetBSD. 718 * 719 * The basic algorithm is: 720 * 721 * for (each byte in block, excluding checksum) { 722 * sum += byte; 723 * if (sum > 255) 724 * sum -= 255; 725 * } 726 * 727 * That's equivalent to summing all of the bytes in the block 728 * (excluding the checksum byte, of course), then calculating the 729 * checksum as "cksum = sum - ((sum - 1) / 255) * 255)". That 730 * expression may or may not yield a faster checksum function, 731 * but it's easier to reason about. 732 * 733 * Note that if you have a block filled with bytes of a single 734 * value "X" (regardless of that value!) and calculate the cksum 735 * of the block (excluding the checksum byte), you will _always_ 736 * end up with a checksum of X. (Do the math; that can be derived 737 * from the checksum calculation function!) That means that 738 * blocks which contain bytes which all have the same value will 739 * always checksum properly. That's a _very_ unlikely occurence 740 * (probably impossible, actually) for a valid filecore boot block, 741 * so we treat such blocks as invalid. 742 */ 743 static int 744 filecore_checksum(u_char *bootblock) 745 { 746 u_char byte0, accum_diff; 747 u_int sum; 748 int i; 749 750 sum = 0; 751 accum_diff = 0; 752 byte0 = bootblock[0]; 753 754 /* 755 * Sum the contents of the block, keeping track of whether 756 * or not all bytes are the same. If 'accum_diff' ends up 757 * being zero, all of the bytes are, in fact, the same. 758 */ 759 for (i = 0; i < 511; ++i) { 760 sum += bootblock[i]; 761 accum_diff |= bootblock[i] ^ byte0; 762 } 763 764 /* 765 * Check to see if the checksum byte is the same as the 766 * rest of the bytes, too. (Note that if all of the bytes 767 * are the same except the checksum, a checksum compare 768 * won't succeed, but that's not our problem.) 769 */ 770 accum_diff |= bootblock[i] ^ byte0; 771 772 /* All bytes in block are the same; call it invalid. */ 773 if (accum_diff == 0) 774 return (-1); 775 776 return (sum - ((sum - 1) / 255) * 255); 777 } 778 779 /* 780 * Check for the presence of a RiscOS filecore boot block 781 * indicating an ADFS file system on the disc. 782 * Return the offset to the NetBSD part of the disc if 783 * this can be determined. 784 * This routine will terminate disklabel if the disc 785 * is found to be ADFS only. 786 */ 787 static u_int 788 get_filecore_partition(int f) 789 { 790 struct filecore_bootblock *fcbb; 791 static u_char bb[DEV_BSIZE]; 792 u_int offset; 793 struct riscix_partition_table *riscix_part; 794 int loop; 795 796 if (pread(f, bb, sizeof(bb), (off_t)FILECORE_BOOT_SECTOR * DEV_BSIZE) != sizeof(bb)) 797 err(4, "can't read filecore boot block"); 798 fcbb = (struct filecore_bootblock *)bb; 799 800 /* Check if table is valid. */ 801 if (filecore_checksum(bb) != fcbb->checksum) 802 return (0); 803 804 /* 805 * Check for NetBSD/arm32 (RiscBSD) partition marker. 806 * If found the NetBSD disklabel location is easy. 807 */ 808 offset = (fcbb->partition_cyl_low + (fcbb->partition_cyl_high << 8)) 809 * fcbb->heads * fcbb->secspertrack; 810 811 switch (fcbb->partition_type) { 812 813 case PARTITION_FORMAT_RISCBSD: 814 return (offset); 815 816 case PARTITION_FORMAT_RISCIX: 817 /* 818 * Read the RISCiX partition table and search for the 819 * first partition named "RiscBSD", "NetBSD", or "Empty:" 820 * 821 * XXX is use of 'Empty:' really desirable?! -- cgd 822 */ 823 824 if (pread(f, bb, sizeof(bb), (off_t)offset * DEV_BSIZE) != sizeof(bb)) 825 err(4, "can't read riscix partition table"); 826 riscix_part = (struct riscix_partition_table *)bb; 827 828 for (loop = 0; loop < NRISCIX_PARTITIONS; ++loop) { 829 if (strcmp((char *)riscix_part->partitions[loop].rp_name, 830 "RiscBSD") == 0 || 831 strcmp((char *)riscix_part->partitions[loop].rp_name, 832 "NetBSD") == 0 || 833 strcmp((char *)riscix_part->partitions[loop].rp_name, 834 "Empty:") == 0) { 835 return riscix_part->partitions[loop].rp_start; 836 break; 837 } 838 } 839 /* 840 * Valid filecore boot block, RISCiX partition table 841 * but no NetBSD partition. We should leave this 842 * disc alone. 843 */ 844 errx(4, "cannot label: no NetBSD partition found" 845 " in RISCiX partition table"); 846 847 default: 848 /* 849 * Valid filecore boot block and no non-ADFS partition. 850 * This means that the whole disc is allocated for ADFS 851 * so do not trash ! If the user really wants to put a 852 * NetBSD disklabel on the disc then they should remove 853 * the filecore boot block first with dd. 854 */ 855 errx(4, "cannot label: filecore-only disk" 856 " (no non-ADFS partition)"); 857 } 858 return (0); 859 } 860 #endif /* USE_ACORN */ 861 862 /* 863 * Fetch disklabel for disk to 'lab'. 864 * Use ioctl to get label unless -r flag is given. 865 */ 866 static void 867 readlabel(int f) 868 { 869 if (rflag) { 870 /* Get label directly from disk */ 871 if (readlabel_direct(f) == 0) 872 return; 873 /* 874 * There was no label on the disk. Get the fictious one 875 * as a basis for initialisation. 876 */ 877 if (!Fflag && Iflag && (dk_ioctl(f, DIOCGDINFO, &lab) == 0 || 878 dk_ioctl(f, DIOCGDEFLABEL, &lab) == 0)) 879 return; 880 } else { 881 /* Get label from kernel. */ 882 if (dk_ioctl(f, DIOCGDINFO, &lab) < 0) 883 err(4, "ioctl DIOCGDINFO"); 884 return; 885 } 886 887 if (read_all == 2) 888 /* We actually found one, and printed it... */ 889 exit(0); 890 errx(1, "could not read existing label"); 891 } 892 893 /* 894 * Reading the label from the disk is largely a case of 'hunt the label'. 895 * and since different architectures default to different places there 896 * could even be more than one label that contradict each other! 897 * For now we look in the expected place, then search through likely 898 * other locations. 899 */ 900 static struct disklabel * 901 find_label(int f, u_int sector) 902 { 903 struct disklabel *lp; 904 int i, offset; 905 const char *is_deleted; 906 907 bootarea_len = pread(f, bootarea, sizeof bootarea, 908 sector * (off_t)DEV_BSIZE); 909 if (bootarea_len <= 0) { 910 if (verbose) 911 warn("failed to read bootarea from sector %u", sector); 912 return NULL; 913 } 914 915 if (verbose > 2) 916 warnx("read sector %u len %u looking for label", 917 sector, bootarea_len); 918 919 /* Check expected offset first */ 920 for (offset = LABEL_OFFSET, i = -4;; offset = i += 4) { 921 is_deleted = ""; 922 lp = (void *)(bootarea + offset); 923 if (i == LABEL_OFFSET) 924 continue; 925 if ((char *)(lp + 1) > bootarea + bootarea_len) 926 break; 927 if (lp->d_magic2 != lp->d_magic) 928 continue; 929 if (read_all && (lp->d_magic == DISKMAGIC_DELETED || 930 lp->d_magic == DISKMAGIC_DELETED_REV)) { 931 lp->d_magic ^= ~0u; 932 lp->d_magic2 ^= ~0u; 933 is_deleted = "deleted "; 934 } 935 if (lp->d_magic != DISKMAGIC) { 936 /* XXX: Do something about byte-swapped labels ? */ 937 if (lp->d_magic == DISKMAGIC_REV && 938 lp->d_magic2 == DISKMAGIC_REV) 939 warnx("ignoring %sbyteswapped label" 940 " at offset %u from sector %u", 941 is_deleted, offset, sector); 942 continue; 943 } 944 if (lp->d_npartitions > MAXPARTITIONS || dkcksum(lp) != 0) { 945 if (verbose > 0) 946 warnx("corrupt label found at offset %u in " 947 "sector %u", offset, sector); 948 continue; 949 } 950 if (verbose > 1) 951 warnx("%slabel found at offset %u from sector %u", 952 is_deleted, offset, sector); 953 if (!read_all) 954 return lp; 955 956 /* To print all the labels we have to do it here */ 957 /* XXX: maybe we should compare them? */ 958 printf("# %ssector %u offset %u bytes\n", 959 is_deleted, sector, offset); 960 if (tflag) 961 makedisktab(stdout, lp); 962 else { 963 showinfo(stdout, lp, specname); 964 showpartitions(stdout, lp, Cflag); 965 } 966 checklabel(lp); 967 /* Remember we've found a label */ 968 read_all = 2; 969 } 970 return NULL; 971 } 972 973 static void 974 write_bootarea(int f, u_int sector) 975 { 976 int wlen; 977 978 if (bootarea_len <= 0) 979 errx(1, "attempting to write after failed read"); 980 981 #ifdef __alpha__ 982 /* 983 * The Alpha requires that the boot block be checksummed. 984 * The NetBSD/alpha disklabel.h provides a macro to do it. 985 */ 986 if (sector == 0) { 987 struct alpha_boot_block *bb; 988 989 bb = (struct alpha_boot_block *)(void *)bootarea; 990 bb->bb_cksum = 0; 991 ALPHA_BOOT_BLOCK_CKSUM(bb, &bb->bb_cksum); 992 } 993 #endif /* __alpha__ */ 994 995 wlen = pwrite(f, bootarea, bootarea_len, sector * (off_t)DEV_BSIZE); 996 if (wlen == bootarea_len) 997 return; 998 if (wlen == -1) 999 err(1, "disklabel write (sector %u) size %u failed", 1000 sector, bootarea_len); 1001 errx(1, "disklabel write (sector %u) size %u truncated to %d", 1002 sector, bootarea_len, wlen); 1003 } 1004 1005 static int 1006 update_label(int f, u_int label_sector, u_int label_offset) 1007 { 1008 struct disklabel *disk_lp; 1009 1010 disk_lp = find_label(f, label_sector); 1011 1012 if (disk_lp && Dflag) { 1013 /* Invalidate the existing label */ 1014 disk_lp->d_magic ^= ~0u; 1015 disk_lp->d_magic2 ^= ~0u; 1016 if (Dflag == 2) 1017 write_bootarea(f, label_sector); 1018 /* Force label to default location */ 1019 disk_lp = NULL; 1020 } 1021 1022 if (Dflag == 2) 1023 /* We are just deleting the label */ 1024 return 0; 1025 1026 if (disk_lp == NULL) { 1027 if (label_offset == ~0u) 1028 return 0; 1029 /* Nothing on the disk - we need to add it */ 1030 disk_lp = (void *)(bootarea + label_offset); 1031 if ((char *)(disk_lp + 1) > bootarea + bootarea_len) 1032 errx(1, "no space in bootarea (sector %u) " 1033 "to create label", label_sector); 1034 } 1035 1036 *disk_lp = lab; 1037 write_bootarea(f, label_sector); 1038 return 1; 1039 } 1040 1041 static void 1042 writelabel_direct(int f) 1043 { 1044 u_int label_sector; 1045 int written = 0; 1046 int rval; 1047 1048 label_sector = get_filecore_partition(f); 1049 if (label_sector != 0) 1050 /* The offset needs to be that from the acorn ports... */ 1051 written = update_label(f, label_sector, DEV_BSIZE); 1052 1053 rval = process_mbr(f, writelabel_mbr); 1054 1055 if (rval == 2 || written) 1056 /* Don't add a label to sector 0, but update one if there */ 1057 update_label(f, 0, ~0u); 1058 else 1059 update_label(f, 0, LABEL_OFFSET); 1060 } 1061 1062 static int 1063 readlabel_direct(int f) 1064 { 1065 struct disklabel *disk_lp; 1066 u_int filecore_partition_offset; 1067 1068 filecore_partition_offset = get_filecore_partition(f); 1069 if (filecore_partition_offset != 0) { 1070 disk_lp = find_label(f, filecore_partition_offset); 1071 if (disk_lp != NULL) { 1072 lab = *disk_lp; 1073 return 0; 1074 } 1075 } 1076 1077 if (mflag && process_mbr(f, readlabel_mbr) == 0) 1078 return 0; 1079 1080 disk_lp = find_label(f, 0); 1081 if (disk_lp != NULL) { 1082 lab = *disk_lp; 1083 return 0; 1084 } 1085 1086 if (!mflag && process_mbr(f, readlabel_mbr) == 0) 1087 return 0; 1088 1089 return 1; 1090 } 1091 1092 static void 1093 makedisktab(FILE *f, struct disklabel *lp) 1094 { 1095 int i; 1096 const char *did; 1097 struct partition *pp; 1098 1099 did = "\\\n\t:"; 1100 (void) fprintf(f, "%.*s|Automatically generated label:\\\n\t:dt=", 1101 (int) sizeof(lp->d_typename), lp->d_typename); 1102 if ((unsigned) lp->d_type < DKMAXTYPES) 1103 (void) fprintf(f, "%s:", dktypenames[lp->d_type]); 1104 else 1105 (void) fprintf(f, "unknown%d:", lp->d_type); 1106 1107 (void) fprintf(f, "se#%d:", lp->d_secsize); 1108 (void) fprintf(f, "ns#%d:", lp->d_nsectors); 1109 (void) fprintf(f, "nt#%d:", lp->d_ntracks); 1110 (void) fprintf(f, "sc#%d:", lp->d_secpercyl); 1111 (void) fprintf(f, "nc#%d:", lp->d_ncylinders); 1112 1113 if ((lp->d_secpercyl * lp->d_ncylinders) != lp->d_secperunit) { 1114 (void) fprintf(f, "%ssu#%d:", did, lp->d_secperunit); 1115 did = ""; 1116 } 1117 if (lp->d_rpm != 3600) { 1118 (void) fprintf(f, "%srm#%d:", did, lp->d_rpm); 1119 did = ""; 1120 } 1121 if (lp->d_interleave != 1) { 1122 (void) fprintf(f, "%sil#%d:", did, lp->d_interleave); 1123 did = ""; 1124 } 1125 if (lp->d_trackskew != 0) { 1126 (void) fprintf(f, "%ssk#%d:", did, lp->d_trackskew); 1127 did = ""; 1128 } 1129 if (lp->d_cylskew != 0) { 1130 (void) fprintf(f, "%scs#%d:", did, lp->d_cylskew); 1131 did = ""; 1132 } 1133 if (lp->d_headswitch != 0) { 1134 (void) fprintf(f, "%shs#%d:", did, lp->d_headswitch); 1135 did = ""; 1136 } 1137 if (lp->d_trkseek != 0) { 1138 (void) fprintf(f, "%sts#%d:", did, lp->d_trkseek); 1139 did = ""; 1140 } 1141 #ifdef notyet 1142 (void) fprintf(f, "drivedata: "); 1143 for (i = NDDATA - 1; i >= 0; i--) 1144 if (lp->d_drivedata[i]) 1145 break; 1146 if (i < 0) 1147 i = 0; 1148 for (j = 0; j <= i; j++) 1149 (void) fprintf(f, "%d ", lp->d_drivedata[j]); 1150 #endif /* notyet */ 1151 pp = lp->d_partitions; 1152 for (i = 0; i < lp->d_npartitions; i++, pp++) { 1153 if (pp->p_size) { 1154 char c = 'a' + i; 1155 (void) fprintf(f, "\\\n\t:"); 1156 (void) fprintf(f, "p%c#%d:", c, pp->p_size); 1157 (void) fprintf(f, "o%c#%d:", c, pp->p_offset); 1158 if (pp->p_fstype != FS_UNUSED) { 1159 if ((unsigned) pp->p_fstype < FSMAXTYPES) 1160 (void) fprintf(f, "t%c=%s:", c, 1161 fstypenames[pp->p_fstype]); 1162 else 1163 (void) fprintf(f, "t%c=unknown%d:", 1164 c, pp->p_fstype); 1165 } 1166 switch (pp->p_fstype) { 1167 1168 case FS_UNUSED: 1169 break; 1170 1171 case FS_BSDFFS: 1172 case FS_BSDLFS: 1173 case FS_EX2FS: 1174 case FS_ADOS: 1175 case FS_APPLEUFS: 1176 (void) fprintf(f, "b%c#%d:", c, 1177 pp->p_fsize * pp->p_frag); 1178 (void) fprintf(f, "f%c#%d:", c, pp->p_fsize); 1179 break; 1180 default: 1181 break; 1182 } 1183 } 1184 } 1185 (void) fprintf(f, "\n"); 1186 (void) fflush(f); 1187 } 1188 1189 static int 1190 edit(int f) 1191 { 1192 const char *tmpdir; 1193 char tmpfil[MAXPATHLEN]; 1194 int first, ch, fd; 1195 int get_ok; 1196 FILE *fp; 1197 1198 if ((tmpdir = getenv("TMPDIR")) == NULL) 1199 tmpdir = _PATH_TMP; 1200 (void)snprintf(tmpfil, sizeof(tmpfil), "%s/%s", tmpdir, TMPFILE); 1201 if ((fd = mkstemp(tmpfil)) == -1 || (fp = fdopen(fd, "w")) == NULL) { 1202 warn("%s", tmpfil); 1203 return (1); 1204 } 1205 (void)fchmod(fd, 0600); 1206 showinfo(fp, &lab, specname); 1207 showpartitions(fp, &lab, Cflag); 1208 (void) fclose(fp); 1209 for (;;) { 1210 if (!editit(tmpfil)) 1211 break; 1212 fp = fopen(tmpfil, "r"); 1213 if (fp == NULL) { 1214 warn("%s", tmpfil); 1215 break; 1216 } 1217 (void) memset(&lab, 0, sizeof(lab)); 1218 get_ok = getasciilabel(fp, &lab); 1219 fclose(fp); 1220 if (get_ok && write_label(f) == 0) { 1221 (void) unlink(tmpfil); 1222 return (0); 1223 } 1224 (void) printf("re-edit the label? [y]: "); 1225 (void) fflush(stdout); 1226 first = ch = getchar(); 1227 while (ch != '\n' && ch != EOF) 1228 ch = getchar(); 1229 if (first == 'n' || first == 'N') 1230 break; 1231 } 1232 (void)unlink(tmpfil); 1233 return (1); 1234 } 1235 1236 static int 1237 editit(const char *tmpfil) 1238 { 1239 int pid, xpid; 1240 int status; 1241 sigset_t nsigset, osigset; 1242 1243 sigemptyset(&nsigset); 1244 sigaddset(&nsigset, SIGINT); 1245 sigaddset(&nsigset, SIGQUIT); 1246 sigaddset(&nsigset, SIGHUP); 1247 sigprocmask(SIG_BLOCK, &nsigset, &osigset); 1248 while ((pid = fork()) < 0) { 1249 if (errno != EAGAIN) { 1250 sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0); 1251 warn("fork"); 1252 return (0); 1253 } 1254 sleep(1); 1255 } 1256 if (pid == 0) { 1257 const char *ed; 1258 char *buf; 1259 int retval; 1260 1261 sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0); 1262 setgid(getgid()); 1263 setuid(getuid()); 1264 if ((ed = getenv("EDITOR")) == (char *)0) 1265 ed = DEFEDITOR; 1266 /* 1267 * Jump through a few extra hoops in case someone's editor 1268 * is "editor arg1 arg2". 1269 */ 1270 asprintf(&buf, "%s %s", ed, tmpfil); 1271 if (!buf) 1272 err(1, "malloc"); 1273 retval = execlp(_PATH_BSHELL, _PATH_BSHELL, "-c", buf, NULL); 1274 if (retval == -1) 1275 perror(ed); 1276 exit(retval); 1277 } 1278 while ((xpid = wait(&status)) >= 0) 1279 if (xpid == pid) 1280 break; 1281 sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0); 1282 return (!status); 1283 } 1284 1285 static char * 1286 skip(char *cp) 1287 { 1288 1289 cp += strspn(cp, " \t"); 1290 if (*cp == '\0') 1291 return (NULL); 1292 return (cp); 1293 } 1294 1295 static char * 1296 word(char *cp) 1297 { 1298 1299 if (cp == NULL || *cp == '\0') 1300 return (NULL); 1301 1302 cp += strcspn(cp, " \t"); 1303 if (*cp == '\0') 1304 return (NULL); 1305 *cp++ = '\0'; 1306 cp += strspn(cp, " \t"); 1307 if (*cp == '\0') 1308 return (NULL); 1309 return (cp); 1310 } 1311 1312 #define _CHECKLINE \ 1313 if (tp == NULL || *tp == '\0') { \ 1314 warnx("line %d: too few fields", lineno); \ 1315 errors++; \ 1316 break; \ 1317 } 1318 1319 #define __CHECKLINE \ 1320 if (*tp == NULL || **tp == '\0') { \ 1321 warnx("line %d: too few fields", lineno); \ 1322 *tp = _error_; \ 1323 return 0; \ 1324 } 1325 1326 static char _error_[] = ""; 1327 #define NXTNUM(n) if ((n = nxtnum(&tp, lineno),0) + tp != _error_) \ 1328 ; else goto error 1329 #define NXTXNUM(n) if ((n = nxtxnum(&tp, lp, lineno),0) + tp != _error_) \ 1330 ; else goto error 1331 1332 static unsigned long 1333 nxtnum(char **tp, int lineno) 1334 { 1335 char *cp; 1336 unsigned long v; 1337 1338 __CHECKLINE 1339 if (getulong(*tp, '\0', &cp, &v, UINT32_MAX) != 0) { 1340 warnx("line %d: syntax error", lineno); 1341 *tp = _error_; 1342 return 0; 1343 } 1344 *tp = cp; 1345 return v; 1346 } 1347 1348 static unsigned long 1349 nxtxnum(char **tp, struct disklabel *lp, int lineno) 1350 { 1351 char *cp, *ncp; 1352 unsigned long n, v; 1353 1354 __CHECKLINE 1355 cp = *tp; 1356 if (getulong(cp, '/', &ncp, &n, UINT32_MAX) != 0) 1357 goto bad; 1358 1359 if (*ncp == '/') { 1360 n *= lp->d_secpercyl; 1361 cp = ncp + 1; 1362 if (getulong(cp, '/', &ncp, &v, UINT32_MAX) != 0) 1363 goto bad; 1364 n += v * lp->d_nsectors; 1365 cp = ncp + 1; 1366 if (getulong(cp, '\0', &ncp, &v, UINT32_MAX) != 0) 1367 goto bad; 1368 n += v; 1369 } 1370 *tp = ncp; 1371 return n; 1372 bad: 1373 warnx("line %d: invalid format", lineno); 1374 *tp = _error_; 1375 return 0; 1376 } 1377 1378 /* 1379 * Read an ascii label in from fd f, 1380 * in the same format as that put out by showinfo() and showpartitions(), 1381 * and fill in lp. 1382 */ 1383 static int 1384 getasciilabel(FILE *f, struct disklabel *lp) 1385 { 1386 const char *const *cpp, *s; 1387 struct partition *pp; 1388 char *cp, *tp, line[BUFSIZ], tbuf[15]; 1389 int lineno, errors; 1390 unsigned long v; 1391 unsigned int part; 1392 1393 lineno = 0; 1394 errors = 0; 1395 lp->d_bbsize = BBSIZE; /* XXX */ 1396 lp->d_sbsize = SBLOCKSIZE; /* XXX */ 1397 while (fgets(line, sizeof(line) - 1, f)) { 1398 lineno++; 1399 if ((cp = strpbrk(line, "#\r\n")) != NULL) 1400 *cp = '\0'; 1401 cp = skip(line); 1402 if (cp == NULL) /* blank line or comment line */ 1403 continue; 1404 tp = strchr(cp, ':'); /* everything has a colon in it */ 1405 if (tp == NULL) { 1406 warnx("line %d: syntax error", lineno); 1407 errors++; 1408 continue; 1409 } 1410 *tp++ = '\0', tp = skip(tp); 1411 if (!strcmp(cp, "type")) { 1412 if (tp == NULL) { 1413 strlcpy(tbuf, "unknown", sizeof(tbuf)); 1414 tp = tbuf; 1415 } 1416 cpp = dktypenames; 1417 for (; cpp < &dktypenames[DKMAXTYPES]; cpp++) 1418 if ((s = *cpp) && !strcasecmp(s, tp)) { 1419 lp->d_type = cpp - dktypenames; 1420 goto next; 1421 } 1422 if (GETNUM16(tp, &v) != 0) { 1423 warnx("line %d: syntax error", lineno); 1424 errors++; 1425 continue; 1426 } 1427 if (v >= DKMAXTYPES) 1428 warnx("line %d: warning, unknown disk type: %s", 1429 lineno, tp); 1430 lp->d_type = v; 1431 continue; 1432 } 1433 if (!strcmp(cp, "flags")) { 1434 for (v = 0; (cp = tp) && *cp != '\0';) { 1435 tp = word(cp); 1436 if (!strcasecmp(cp, "removable")) 1437 v |= D_REMOVABLE; 1438 else if (!strcasecmp(cp, "ecc")) 1439 v |= D_ECC; 1440 else if (!strcasecmp(cp, "badsect")) 1441 v |= D_BADSECT; 1442 else { 1443 warnx("line %d: bad flag: %s", 1444 lineno, cp); 1445 errors++; 1446 } 1447 } 1448 lp->d_flags = v; 1449 continue; 1450 } 1451 if (!strcmp(cp, "drivedata")) { 1452 int i; 1453 1454 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) { 1455 if (GETNUM32(cp, &v) != 0) { 1456 warnx("line %d: bad drive data", 1457 lineno); 1458 errors++; 1459 } else 1460 lp->d_drivedata[i] = v; 1461 i++; 1462 tp = word(cp); 1463 } 1464 continue; 1465 } 1466 if (sscanf(cp, "%lu partitions", &v) == 1) { 1467 if (v == 0 || v > MAXPARTITIONS) { 1468 warnx("line %d: bad # of partitions", lineno); 1469 lp->d_npartitions = MAXPARTITIONS; 1470 errors++; 1471 } else 1472 lp->d_npartitions = v; 1473 continue; 1474 } 1475 if (tp == NULL) { 1476 tbuf[0] = '\0'; 1477 tp = tbuf; 1478 } 1479 if (!strcmp(cp, "disk")) { 1480 strncpy(lp->d_typename, tp, sizeof(lp->d_typename)); 1481 continue; 1482 } 1483 if (!strcmp(cp, "label")) { 1484 strncpy(lp->d_packname, tp, sizeof(lp->d_packname)); 1485 continue; 1486 } 1487 if (!strcmp(cp, "bytes/sector")) { 1488 if (GETNUM32(tp, &v) != 0 || v <= 0 || (v % 512) != 0) { 1489 warnx("line %d: bad %s: %s", lineno, cp, tp); 1490 errors++; 1491 } else 1492 lp->d_secsize = v; 1493 continue; 1494 } 1495 if (!strcmp(cp, "sectors/track")) { 1496 if (GETNUM32(tp, &v) != 0) { 1497 warnx("line %d: bad %s: %s", lineno, cp, tp); 1498 errors++; 1499 } else 1500 lp->d_nsectors = v; 1501 continue; 1502 } 1503 if (!strcmp(cp, "sectors/cylinder")) { 1504 if (GETNUM32(tp, &v) != 0) { 1505 warnx("line %d: bad %s: %s", lineno, cp, tp); 1506 errors++; 1507 } else 1508 lp->d_secpercyl = v; 1509 continue; 1510 } 1511 if (!strcmp(cp, "tracks/cylinder")) { 1512 if (GETNUM32(tp, &v) != 0) { 1513 warnx("line %d: bad %s: %s", lineno, cp, tp); 1514 errors++; 1515 } else 1516 lp->d_ntracks = v; 1517 continue; 1518 } 1519 if (!strcmp(cp, "cylinders")) { 1520 if (GETNUM32(tp, &v) != 0) { 1521 warnx("line %d: bad %s: %s", lineno, cp, tp); 1522 errors++; 1523 } else 1524 lp->d_ncylinders = v; 1525 continue; 1526 } 1527 if (!strcmp(cp, "total sectors") || 1528 !strcmp(cp, "sectors/unit")) { 1529 if (GETNUM32(tp, &v) != 0) { 1530 warnx("line %d: bad %s: %s", lineno, cp, tp); 1531 errors++; 1532 } else 1533 lp->d_secperunit = v; 1534 continue; 1535 } 1536 if (!strcmp(cp, "rpm")) { 1537 if (GETNUM16(tp, &v) != 0) { 1538 warnx("line %d: bad %s: %s", lineno, cp, tp); 1539 errors++; 1540 } else 1541 lp->d_rpm = v; 1542 continue; 1543 } 1544 if (!strcmp(cp, "interleave")) { 1545 if (GETNUM16(tp, &v) != 0) { 1546 warnx("line %d: bad %s: %s", lineno, cp, tp); 1547 errors++; 1548 } else 1549 lp->d_interleave = v; 1550 continue; 1551 } 1552 if (!strcmp(cp, "trackskew")) { 1553 if (GETNUM16(tp, &v) != 0) { 1554 warnx("line %d: bad %s: %s", lineno, cp, tp); 1555 errors++; 1556 } else 1557 lp->d_trackskew = v; 1558 continue; 1559 } 1560 if (!strcmp(cp, "cylinderskew")) { 1561 if (GETNUM16(tp, &v) != 0) { 1562 warnx("line %d: bad %s: %s", lineno, cp, tp); 1563 errors++; 1564 } else 1565 lp->d_cylskew = v; 1566 continue; 1567 } 1568 if (!strcmp(cp, "headswitch")) { 1569 if (GETNUM32(tp, &v) != 0) { 1570 warnx("line %d: bad %s: %s", lineno, cp, tp); 1571 errors++; 1572 } else 1573 lp->d_headswitch = v; 1574 continue; 1575 } 1576 if (!strcmp(cp, "track-to-track seek")) { 1577 if (GETNUM32(tp, &v) != 0) { 1578 warnx("line %d: bad %s: %s", lineno, cp, tp); 1579 errors++; 1580 } else 1581 lp->d_trkseek = v; 1582 continue; 1583 } 1584 if ('a' > *cp || *cp > 'z' || cp[1] != '\0') { 1585 warnx("line %d: unknown field: %s", lineno, cp); 1586 errors++; 1587 continue; 1588 } 1589 1590 /* We have a partition entry */ 1591 part = *cp - 'a'; 1592 1593 if (part >= MAXPARTITIONS) { 1594 warnx("line %d: bad partition name: %s", lineno, cp); 1595 errors++; 1596 continue; 1597 } 1598 pp = &lp->d_partitions[part]; 1599 1600 NXTXNUM(pp->p_size); 1601 NXTXNUM(pp->p_offset); 1602 /* can't use word() here because of blanks in fstypenames[] */ 1603 tp += strspn(tp, " \t"); 1604 _CHECKLINE 1605 cp = tp; 1606 cpp = fstypenames; 1607 for (; cpp < &fstypenames[FSMAXTYPES]; cpp++) { 1608 s = *cpp; 1609 if (s == NULL || 1610 (cp[strlen(s)] != ' ' && 1611 cp[strlen(s)] != '\t' && 1612 cp[strlen(s)] != '\0')) 1613 continue; 1614 if (!memcmp(s, cp, strlen(s))) { 1615 pp->p_fstype = cpp - fstypenames; 1616 tp += strlen(s); 1617 if (*tp == '\0') 1618 tp = NULL; 1619 else { 1620 tp += strspn(tp, " \t"); 1621 if (*tp == '\0') 1622 tp = NULL; 1623 } 1624 goto gottype; 1625 } 1626 } 1627 tp = word(cp); 1628 if (isdigit(*cp & 0xff)) { 1629 if (GETNUM8(cp, &v) != 0) { 1630 warnx("line %d: syntax error", lineno); 1631 errors++; 1632 } 1633 } else 1634 v = FSMAXTYPES; 1635 if ((unsigned)v >= FSMAXTYPES) { 1636 warnx("line %d: warning, unknown file system type: %s", 1637 lineno, cp); 1638 warnx("tip: use -l to see all valid file system " 1639 "types"); 1640 v = FS_UNUSED; 1641 } 1642 pp->p_fstype = v; 1643 gottype: 1644 switch (pp->p_fstype) { 1645 1646 case FS_UNUSED: /* XXX */ 1647 NXTNUM(pp->p_fsize); 1648 if (pp->p_fsize == 0) 1649 break; 1650 NXTNUM(v); 1651 pp->p_frag = v / pp->p_fsize; 1652 break; 1653 1654 case FS_BSDFFS: 1655 case FS_ADOS: 1656 case FS_APPLEUFS: 1657 NXTNUM(pp->p_fsize); 1658 if (pp->p_fsize == 0) 1659 break; 1660 NXTNUM(v); 1661 pp->p_frag = v / pp->p_fsize; 1662 NXTNUM(pp->p_cpg); 1663 break; 1664 case FS_BSDLFS: 1665 NXTNUM(pp->p_fsize); 1666 if (pp->p_fsize == 0) 1667 break; 1668 NXTNUM(v); 1669 pp->p_frag = v / pp->p_fsize; 1670 NXTNUM(pp->p_sgs); 1671 break; 1672 case FS_EX2FS: 1673 NXTNUM(pp->p_fsize); 1674 if (pp->p_fsize == 0) 1675 break; 1676 NXTNUM(v); 1677 pp->p_frag = v / pp->p_fsize; 1678 break; 1679 case FS_ISO9660: 1680 NXTNUM(pp->p_cdsession); 1681 break; 1682 default: 1683 break; 1684 } 1685 continue; 1686 error: 1687 errors++; 1688 next: 1689 ; 1690 } 1691 errors += checklabel(lp); 1692 return (errors == 0); 1693 } 1694 1695 /* 1696 * Check disklabel for errors and fill in 1697 * derived fields according to supplied values. 1698 */ 1699 int 1700 checklabel(struct disklabel *lp) 1701 { 1702 struct partition *pp, *qp; 1703 int i, j, errors; 1704 char part; 1705 1706 errors = 0; 1707 if (lp->d_secsize == 0) { 1708 warnx("sector size %d", lp->d_secsize); 1709 return (1); 1710 } 1711 if (lp->d_nsectors == 0) { 1712 warnx("sectors/track %d", lp->d_nsectors); 1713 return (1); 1714 } 1715 if (lp->d_ntracks == 0) { 1716 warnx("tracks/cylinder %d", lp->d_ntracks); 1717 return (1); 1718 } 1719 if (lp->d_ncylinders == 0) { 1720 warnx("cylinders/unit %d", lp->d_ncylinders); 1721 errors++; 1722 } 1723 if (lp->d_rpm == 0) 1724 warnx("warning, revolutions/minute %d", lp->d_rpm); 1725 if (lp->d_secpercyl == 0) 1726 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks; 1727 if (lp->d_secperunit == 0) 1728 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders; 1729 if (lp->d_bbsize == 0) { 1730 warnx("boot block size %d", lp->d_bbsize); 1731 errors++; 1732 } else if (lp->d_bbsize % lp->d_secsize) 1733 warnx("warning, boot block size %% sector-size != 0"); 1734 if (lp->d_sbsize == 0) { 1735 warnx("super block size %d", lp->d_sbsize); 1736 errors++; 1737 } else if (lp->d_sbsize % lp->d_secsize) 1738 warnx("warning, super block size %% sector-size != 0"); 1739 if (lp->d_npartitions > MAXPARTITIONS) 1740 warnx("warning, number of partitions (%d) > MAXPARTITIONS (%d)", 1741 lp->d_npartitions, MAXPARTITIONS); 1742 else 1743 for (i = MAXPARTITIONS - 1; i >= lp->d_npartitions; i--) { 1744 part = 'a' + i; 1745 pp = &lp->d_partitions[i]; 1746 if (pp->p_size || pp->p_offset) { 1747 warnx("warning, partition %c increased " 1748 "number of partitions from %d to %d", 1749 part, lp->d_npartitions, i + 1); 1750 lp->d_npartitions = i + 1; 1751 break; 1752 } 1753 } 1754 for (i = 0; i < lp->d_npartitions; i++) { 1755 part = 'a' + i; 1756 pp = &lp->d_partitions[i]; 1757 if (pp->p_size == 0 && pp->p_offset != 0) 1758 warnx("warning, partition %c: size 0, but offset %d", 1759 part, pp->p_offset); 1760 #ifdef STRICT_CYLINDER_ALIGNMENT 1761 if (pp->p_offset % lp->d_secpercyl) { 1762 warnx("warning, partition %c:" 1763 " not starting on cylinder boundary", 1764 part); 1765 errors++; 1766 } 1767 #endif /* STRICT_CYLINDER_ALIGNMENT */ 1768 if (pp->p_offset > lp->d_secperunit) { 1769 warnx("partition %c: offset past end of unit", part); 1770 errors++; 1771 } 1772 if (pp->p_offset + pp->p_size > lp->d_secperunit) { 1773 warnx("partition %c: partition extends" 1774 " past end of unit", 1775 part); 1776 errors++; 1777 } 1778 if (pp->p_fstype != FS_UNUSED) 1779 for (j = i + 1; j < lp->d_npartitions; j++) { 1780 qp = &lp->d_partitions[j]; 1781 if (qp->p_fstype == FS_UNUSED) 1782 continue; 1783 if (pp->p_offset < qp->p_offset + qp->p_size && 1784 qp->p_offset < pp->p_offset + pp->p_size) 1785 warnx("partitions %c and %c overlap", 1786 part, 'a' + j); 1787 } 1788 } 1789 return (errors); 1790 } 1791 1792 static void 1793 usage(void) 1794 { 1795 static const struct { 1796 const char *name; 1797 const char *expn; 1798 } usages[] = { 1799 { "[-ACFrtv] disk", "(to read label)" }, 1800 { "-w [-DFrv] [-f disktab] disk disktype [packid]", "(to write label)" }, 1801 { "-e [-CDFIrv] disk", "(to edit label)" }, 1802 { "-i [-DFIrv] disk", "(to create a label interactively)" }, 1803 { "-D [-v] disk", "(to delete existing label(s))" }, 1804 { "-R [-DFrv] disk protofile", "(to restore label)" }, 1805 { "[-NW] disk", "(to write disable/enable label)" }, 1806 { "-l", "(to show all known file system types)" }, 1807 { NULL, NULL } 1808 }; 1809 int i; 1810 const char *pn = getprogname(); 1811 const char *t = "usage:"; 1812 1813 for (i = 0; usages[i].name != NULL; i++) { 1814 (void)fprintf(stderr, "%s %s %s\n\t%s\n", 1815 t, pn, usages[i].name, usages[i].expn); 1816 t = "or"; 1817 } 1818 exit(1); 1819 } 1820 1821 static int 1822 getulong(const char *str, char sep, char **epp, unsigned long *ul, 1823 unsigned long max) 1824 { 1825 char *ep; 1826 1827 if (epp == NULL) 1828 epp = &ep; 1829 1830 *ul = strtoul(str, epp, 10); 1831 1832 if ((*ul == ULONG_MAX && errno == ERANGE) || *ul > max) 1833 return ERANGE; 1834 1835 if (*str == '\0' || (**epp != '\0' && **epp != sep && 1836 !isspace((unsigned char)**epp))) 1837 return EFTYPE; 1838 1839 return 0; 1840 } 1841 1842 /* 1843 * This is a wrapper over the standard strcmp function to be used with 1844 * qsort on an array of pointers to strings. 1845 */ 1846 static int 1847 qsort_strcmp(const void *v1, const void *v2) 1848 { 1849 const char *const *sp1 = (const char *const *)v1; 1850 const char *const *sp2 = (const char *const *)v2; 1851 1852 return strcmp(*sp1, *sp2); 1853 } 1854 1855 /* 1856 * Prints all know file system types for a partition. 1857 * Returns 1 on success, 0 on failure. 1858 */ 1859 int 1860 list_fs_types(void) 1861 { 1862 int ret; 1863 size_t nelems; 1864 1865 nelems = 0; 1866 { 1867 const char *const *namep; 1868 1869 namep = fstypenames; 1870 while (*namep++ != NULL) 1871 nelems++; 1872 } 1873 1874 ret = 1; 1875 if (nelems > 0) { 1876 const char **list; 1877 size_t i; 1878 1879 list = (const char **)malloc(sizeof(char *) * nelems); 1880 if (list == NULL) { 1881 warnx("sorry, could not allocate memory for list"); 1882 ret = 0; 1883 } else { 1884 for (i = 0; i < nelems; i++) 1885 list[i] = fstypenames[i]; 1886 1887 qsort(list, nelems, sizeof(char *), qsort_strcmp); 1888 1889 for (i = 0; i < nelems; i++) 1890 (void)printf("%s\n", list[i]); 1891 1892 free(list); 1893 } 1894 } 1895 1896 return ret; 1897 } 1898