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