bsdlabel.c revision 13550
1/* 2 * Copyright (c) 1987, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Symmetric Computer Systems. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37#ifndef lint 38static char copyright[] = 39"@(#) Copyright (c) 1987, 1993\n\ 40 The Regents of the University of California. All rights reserved.\n"; 41#endif /* not lint */ 42 43#ifndef lint 44static char sccsid[] = "@(#)disklabel.c 8.2 (Berkeley) 1/7/94"; 45/* from static char sccsid[] = "@(#)disklabel.c 1.2 (Symmetric) 11/28/85"; */ 46#endif /* not lint */ 47 48#include <sys/param.h> 49#include <sys/signal.h> 50#include <sys/errno.h> 51#include <sys/file.h> 52#include <sys/ioctl.h> 53#include <sys/stat.h> 54#include <sys/wait.h> 55#define DKTYPENAMES 56#include <sys/disklabel.h> 57#include <ufs/ffs/fs.h> 58#include <unistd.h> 59#include <string.h> 60#include <stdio.h> 61#include <stdlib.h> 62#include <signal.h> 63#include <stdarg.h> 64#include <ctype.h> 65#include "pathnames.h" 66 67/* 68 * Disklabel: read and write disklabels. 69 * The label is usually placed on one of the first sectors of the disk. 70 * Many machines also place a bootstrap in the same area, 71 * in which case the label is embedded in the bootstrap. 72 * The bootstrap source must leave space at the proper offset 73 * for the label on such machines. 74 */ 75 76#ifdef tahoe 77#define RAWPARTITION 'a' 78#else 79#define RAWPARTITION 'c' 80#endif 81 82#ifndef BBSIZE 83#define BBSIZE 8192 /* size of boot area, with label */ 84#endif 85 86#ifdef tahoe 87#define NUMBOOT 0 88#else 89#if defined(hp300) || defined(hp800) 90#define NUMBOOT 1 91#else 92#define NUMBOOT 2 93#endif 94#endif 95 96void makelabel __P((char *, char *, struct disklabel *)); 97int writelabel __P((int, char *, struct disklabel *)); 98void l_perror __P((char *)); 99struct disklabel * readlabel __P((int)); 100struct disklabel * makebootarea __P((char *, struct disklabel *, int)); 101void display __P((FILE *, struct disklabel *)); 102int edit __P((struct disklabel *, int)); 103int editit __P((void)); 104char * skip __P((char *)); 105char * word __P((char *)); 106int getasciilabel __P((FILE *, struct disklabel *)); 107int checklabel __P((struct disklabel *)); 108void setbootflag __P((struct disklabel *)); 109void Warning (char *, ...); 110void Perror __P((char *)); 111void usage __P((void)); 112extern u_short dkcksum __P((struct disklabel *)); 113struct disklabel * getvirginlabel __P((void)); 114 115#define DEFEDITOR _PATH_VI 116#define streq(a,b) (strcmp(a,b) == 0) 117 118char *dkname; 119char *specname; 120char tmpfil[] = _PATH_TMP; 121 122extern int errno; 123char namebuf[BBSIZE], *np = namebuf; 124struct disklabel lab; 125struct disklabel *readlabel(), *makebootarea(); 126char bootarea[BBSIZE]; 127 128#if NUMBOOT > 0 129int installboot; /* non-zero if we should install a boot program */ 130char *bootbuf; /* pointer to buffer with remainder of boot prog */ 131int bootsize; /* size of remaining boot program */ 132char *xxboot; /* primary boot */ 133char *bootxx; /* secondary boot */ 134char boot0[MAXPATHLEN]; 135char boot1[MAXPATHLEN]; 136#endif 137 138enum { 139 UNSPEC, EDIT, NOWRITE, READ, RESTORE, WRITE, WRITEABLE, WRITEBOOT 140} op = UNSPEC; 141 142int rflag; 143 144#ifdef DEBUG 145int debug; 146#define OPTIONS "BNRWb:ders:w" 147#else 148#define OPTIONS "BNRWb:ers:w" 149#endif 150 151int 152main(argc, argv) 153 int argc; 154 char *argv[]; 155{ 156 extern char *optarg; 157 extern int optind; 158 register struct disklabel *lp; 159 FILE *t; 160 int ch, f, flag, error = 0; 161 char *name = 0; 162 163 while ((ch = getopt(argc, argv, OPTIONS)) != EOF) 164 switch (ch) { 165#if NUMBOOT > 0 166 case 'B': 167 ++installboot; 168 break; 169 case 'b': 170 xxboot = optarg; 171 break; 172#if NUMBOOT > 1 173 case 's': 174 bootxx = optarg; 175 break; 176#endif 177#endif 178 case 'N': 179 if (op != UNSPEC) 180 usage(); 181 op = NOWRITE; 182 break; 183 case 'R': 184 if (op != UNSPEC) 185 usage(); 186 op = RESTORE; 187 break; 188 case 'W': 189 if (op != UNSPEC) 190 usage(); 191 op = WRITEABLE; 192 break; 193 case 'e': 194 if (op != UNSPEC) 195 usage(); 196 op = EDIT; 197 break; 198 case 'r': 199 ++rflag; 200 break; 201 case 'w': 202 if (op != UNSPEC) 203 usage(); 204 op = WRITE; 205 break; 206#ifdef DEBUG 207 case 'd': 208 debug++; 209 break; 210#endif 211 case '?': 212 default: 213 usage(); 214 } 215 argc -= optind; 216 argv += optind; 217#if NUMBOOT > 0 218 if (installboot) { 219 rflag++; 220 if (op == UNSPEC) 221 op = WRITEBOOT; 222 } else { 223 if (op == UNSPEC) 224 op = READ; 225 xxboot = bootxx = 0; 226 } 227#else 228 if (op == UNSPEC) 229 op = READ; 230#endif 231 if (argc < 1) 232 usage(); 233 234 dkname = argv[0]; 235 if (dkname[0] != '/') { 236 (void)sprintf(np, "%sr%s%c", _PATH_DEV, dkname, RAWPARTITION); 237 specname = np; 238 np += strlen(specname) + 1; 239 } else 240 specname = dkname; 241 f = open(specname, op == READ ? O_RDONLY : O_RDWR); 242 if (f < 0 && errno == ENOENT && dkname[0] != '/') { 243 (void)sprintf(specname, "%sr%s", _PATH_DEV, dkname); 244 np = namebuf + strlen(specname) + 1; 245 f = open(specname, op == READ ? O_RDONLY : O_RDWR); 246 } 247 if (f < 0) 248 Perror(specname); 249 250 switch(op) { 251 252 case EDIT: 253 if (argc != 1) 254 usage(); 255 lp = readlabel(f); 256 error = edit(lp, f); 257 break; 258 259 case NOWRITE: 260 flag = 0; 261 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0) 262 Perror("ioctl DIOCWLABEL"); 263 break; 264 265 case READ: 266 if (argc != 1) 267 usage(); 268 lp = readlabel(f); 269 display(stdout, lp); 270 error = checklabel(lp); 271 break; 272 273 case RESTORE: 274#if NUMBOOT > 0 275 if (installboot && argc == 3) { 276 makelabel(argv[2], 0, &lab); 277 argc--; 278 } 279#endif 280 if (argc != 2) 281 usage(); 282 lp = makebootarea(bootarea, &lab, f); 283 if (!(t = fopen(argv[1], "r"))) 284 Perror(argv[1]); 285 if (getasciilabel(t, lp)) 286 error = writelabel(f, bootarea, lp); 287 break; 288 289 case WRITE: 290 if (argc == 3) { 291 name = argv[2]; 292 argc--; 293 } 294 if (argc != 2) 295 usage(); 296 makelabel(argv[1], name, &lab); 297 lp = makebootarea(bootarea, &lab, f); 298 *lp = lab; 299 if (checklabel(lp) == 0) 300 error = writelabel(f, bootarea, lp); 301 break; 302 303 case WRITEABLE: 304 flag = 1; 305 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0) 306 Perror("ioctl DIOCWLABEL"); 307 break; 308 309#if NUMBOOT > 0 310 case WRITEBOOT: 311 { 312 struct disklabel tlab; 313 314 lp = readlabel(f); 315 tlab = *lp; 316 if (argc == 2) 317 makelabel(argv[1], 0, &lab); 318 lp = makebootarea(bootarea, &lab, f); 319 *lp = tlab; 320 if (checklabel(lp) == 0) 321 error = writelabel(f, bootarea, lp); 322 break; 323 } 324#endif 325 } 326 exit(error); 327} 328 329/* 330 * Construct a prototype disklabel from /etc/disktab. As a side 331 * effect, set the names of the primary and secondary boot files 332 * if specified. 333 */ 334void 335makelabel(type, name, lp) 336 char *type, *name; 337 register struct disklabel *lp; 338{ 339 register struct disklabel *dp; 340 341 if (strcmp(type, "auto") == 0) 342 dp = getvirginlabel(); 343 else 344 dp = getdiskbyname(type); 345 if (dp == NULL) { 346 fprintf(stderr, "%s: unknown disk type\n", type); 347 exit(1); 348 } 349 *lp = *dp; 350#if NUMBOOT > 0 351 /* 352 * Set bootstrap name(s). 353 * 1. If set from command line, use those, 354 * 2. otherwise, check if disktab specifies them (b0 or b1), 355 * 3. otherwise, makebootarea() will choose ones based on the name 356 * of the disk special file. E.g. /dev/ra0 -> raboot, bootra 357 */ 358 if (!xxboot && lp->d_boot0) { 359 if (*lp->d_boot0 != '/') 360 (void)sprintf(boot0, "%s/%s", 361 _PATH_BOOTDIR, lp->d_boot0); 362 else 363 (void)strcpy(boot0, lp->d_boot0); 364 xxboot = boot0; 365 } 366#if NUMBOOT > 1 367 if (!bootxx && lp->d_boot1) { 368 if (*lp->d_boot1 != '/') 369 (void)sprintf(boot1, "%s/%s", 370 _PATH_BOOTDIR, lp->d_boot1); 371 else 372 (void)strcpy(boot1, lp->d_boot1); 373 bootxx = boot1; 374 } 375#endif 376#endif 377 /* d_packname is union d_boot[01], so zero */ 378 bzero(lp->d_packname, sizeof(lp->d_packname)); 379 if (name) 380 (void)strncpy(lp->d_packname, name, sizeof(lp->d_packname)); 381} 382 383int 384writelabel(f, boot, lp) 385 int f; 386 char *boot; 387 register struct disklabel *lp; 388{ 389#ifdef vax 390 register int i; 391#endif 392 int flag; 393 394 setbootflag(lp); 395 lp->d_magic = DISKMAGIC; 396 lp->d_magic2 = DISKMAGIC; 397 lp->d_checksum = 0; 398 lp->d_checksum = dkcksum(lp); 399 if (rflag) { 400 /* 401 * First set the kernel disk label, 402 * then write a label to the raw disk. 403 * If the SDINFO ioctl fails because it is unimplemented, 404 * keep going; otherwise, the kernel consistency checks 405 * may prevent us from changing the current (in-core) 406 * label. 407 */ 408 if (ioctl(f, DIOCSDINFO, lp) < 0 && 409 errno != ENODEV && errno != ENOTTY) { 410 l_perror("ioctl DIOCSDINFO"); 411 return (1); 412 } 413 (void)lseek(f, (off_t)0, SEEK_SET); 414 /* 415 * write enable label sector before write (if necessary), 416 * disable after writing. 417 */ 418 flag = 1; 419 if (ioctl(f, DIOCWLABEL, &flag) < 0) 420 perror("ioctl DIOCWLABEL"); 421 if (write(f, boot, lp->d_bbsize) != lp->d_bbsize) { 422 perror("write"); 423 return (1); 424 } 425#if NUMBOOT > 0 426 /* 427 * Output the remainder of the disklabel 428 */ 429 if (bootbuf && write(f, bootbuf, bootsize) != bootsize) { 430 perror("write"); 431 return(1); 432 } 433#endif 434 flag = 0; 435 (void) ioctl(f, DIOCWLABEL, &flag); 436 } else if (ioctl(f, DIOCWDINFO, lp) < 0) { 437 l_perror("ioctl DIOCWDINFO"); 438 return (1); 439 } 440#ifdef vax 441 if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) { 442 daddr_t alt; 443 444 alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors; 445 for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) { 446 (void)lseek(f, (off_t)((alt + i) * lp->d_secsize), 447 SEEK_SET); 448 if (write(f, boot, lp->d_secsize) < lp->d_secsize) { 449 int oerrno = errno; 450 fprintf(stderr, "alternate label %d ", i/2); 451 errno = oerrno; 452 perror("write"); 453 } 454 } 455 } 456#endif 457 return (0); 458} 459 460void 461l_perror(s) 462 char *s; 463{ 464 int saverrno = errno; 465 466 fprintf(stderr, "disklabel: %s: ", s); 467 468 switch (saverrno) { 469 470 case ESRCH: 471 fprintf(stderr, "No disk label on disk;\n"); 472 fprintf(stderr, 473 "use \"disklabel -r\" to install initial label\n"); 474 break; 475 476 case EINVAL: 477 fprintf(stderr, "Label magic number or checksum is wrong!\n"); 478 fprintf(stderr, "(disklabel or kernel is out of date?)\n"); 479 break; 480 481 case EBUSY: 482 fprintf(stderr, "Open partition would move or shrink\n"); 483 break; 484 485 case EXDEV: 486 fprintf(stderr, 487 "Labeled partition or 'a' partition must start at beginning of disk\n"); 488 break; 489 490 default: 491 errno = saverrno; 492 perror((char *)NULL); 493 break; 494 } 495} 496 497/* 498 * Fetch disklabel for disk. 499 * Use ioctl to get label unless -r flag is given. 500 */ 501struct disklabel * 502readlabel(f) 503 int f; 504{ 505 register struct disklabel *lp; 506 507 if (rflag) { 508 if (read(f, bootarea, BBSIZE) < BBSIZE) 509 Perror(specname); 510 for (lp = (struct disklabel *)bootarea; 511 lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp)); 512 lp = (struct disklabel *)((char *)lp + 16)) 513 if (lp->d_magic == DISKMAGIC && 514 lp->d_magic2 == DISKMAGIC) 515 break; 516 if (lp > (struct disklabel *)(bootarea+BBSIZE-sizeof(*lp)) || 517 lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC || 518 dkcksum(lp) != 0) { 519 fprintf(stderr, 520 "Bad pack magic number (label is damaged, or pack is unlabeled)\n"); 521 /* lp = (struct disklabel *)(bootarea + LABELOFFSET); */ 522 exit (1); 523 } 524 } else { 525 lp = &lab; 526 if (ioctl(f, DIOCGDINFO, lp) < 0) 527 Perror("ioctl DIOCGDINFO"); 528 } 529 return (lp); 530} 531 532/* 533 * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot'' 534 * Returns a pointer to the disklabel portion of the bootarea. 535 */ 536struct disklabel * 537makebootarea(boot, dp, f) 538 char *boot; 539 register struct disklabel *dp; 540 int f; 541{ 542 struct disklabel *lp; 543 register char *p; 544 int b; 545#if NUMBOOT > 0 546 char *dkbasename; 547#if NUMBOOT == 1 548 struct stat sb; 549#endif 550#endif 551 552 /* XXX */ 553 if (dp->d_secsize == 0) { 554 dp->d_secsize = DEV_BSIZE; 555 dp->d_bbsize = BBSIZE; 556 } 557 lp = (struct disklabel *) 558 (boot + (LABELSECTOR * dp->d_secsize) + LABELOFFSET); 559 bzero((char *)lp, sizeof *lp); 560#if NUMBOOT > 0 561 /* 562 * If we are not installing a boot program but we are installing a 563 * label on disk then we must read the current bootarea so we don't 564 * clobber the existing boot. 565 */ 566 if (!installboot) { 567 if (rflag) { 568 if (read(f, boot, BBSIZE) < BBSIZE) 569 Perror(specname); 570 bzero((char *)lp, sizeof *lp); 571 } 572 return (lp); 573 } 574 /* 575 * We are installing a boot program. Determine the name(s) and 576 * read them into the appropriate places in the boot area. 577 */ 578 if (!xxboot || !bootxx) { 579 dkbasename = np; 580 if ((p = rindex(dkname, '/')) == NULL) 581 p = dkname; 582 else 583 p++; 584 while (*p && !isdigit(*p)) 585 *np++ = *p++; 586 *np++ = '\0'; 587 588 if (!xxboot) { 589 (void)sprintf(np, "%s/%sboot", 590 _PATH_BOOTDIR, dkbasename); 591 if (access(np, F_OK) < 0 && dkbasename[0] == 'r') 592 dkbasename++; 593 xxboot = np; 594 (void)sprintf(xxboot, "%s/%sboot", 595 _PATH_BOOTDIR, dkbasename); 596 np += strlen(xxboot) + 1; 597 } 598#if NUMBOOT > 1 599 if (!bootxx) { 600 (void)sprintf(np, "%s/boot%s", 601 _PATH_BOOTDIR, dkbasename); 602 if (access(np, F_OK) < 0 && dkbasename[0] == 'r') 603 dkbasename++; 604 bootxx = np; 605 (void)sprintf(bootxx, "%s/boot%s", 606 _PATH_BOOTDIR, dkbasename); 607 np += strlen(bootxx) + 1; 608 } 609#endif 610 } 611#ifdef DEBUG 612 if (debug) 613 fprintf(stderr, "bootstraps: xxboot = %s, bootxx = %s\n", 614 xxboot, bootxx ? bootxx : "NONE"); 615#endif 616 617 /* 618 * Strange rules: 619 * 1. One-piece bootstrap (hp300/hp800) 620 * up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest 621 * is remembered and written later following the bootarea. 622 * 2. Two-piece bootstraps (vax/i386?/mips?) 623 * up to d_secsize bytes of ``xxboot'' go in first d_secsize 624 * bytes of bootarea, remaining d_bbsize-d_secsize filled 625 * from ``bootxx''. 626 */ 627 b = open(xxboot, O_RDONLY); 628 if (b < 0) 629 Perror(xxboot); 630#if NUMBOOT > 1 631 if (read(b, boot, (int)dp->d_secsize) < 0) 632 Perror(xxboot); 633 (void)close(b); 634 b = open(bootxx, O_RDONLY); 635 if (b < 0) 636 Perror(bootxx); 637 if (read(b, &boot[dp->d_secsize], 638 (int)(dp->d_bbsize-dp->d_secsize)) < 0) 639 Perror(bootxx); 640#else 641 if (read(b, boot, (int)dp->d_bbsize) < 0) 642 Perror(xxboot); 643 (void)fstat(b, &sb); 644 bootsize = (int)sb.st_size - dp->d_bbsize; 645 if (bootsize > 0) { 646 /* XXX assume d_secsize is a power of two */ 647 bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1); 648 bootbuf = (char *)malloc((size_t)bootsize); 649 if (bootbuf == 0) 650 Perror(xxboot); 651 if (read(b, bootbuf, bootsize) < 0) { 652 free(bootbuf); 653 Perror(xxboot); 654 } 655 } 656#endif 657 (void)close(b); 658#endif 659 /* 660 * Make sure no part of the bootstrap is written in the area 661 * reserved for the label. 662 */ 663 for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++) 664 if (*p) { 665 fprintf(stderr, 666 "Bootstrap doesn't leave room for disk label\n"); 667 exit(2); 668 } 669 return (lp); 670} 671 672void 673display(f, lp) 674 FILE *f; 675 register struct disklabel *lp; 676{ 677 register int i, j; 678 register struct partition *pp; 679 680 fprintf(f, "# %s:\n", specname); 681 if ((unsigned) lp->d_type < DKMAXTYPES) 682 fprintf(f, "type: %s\n", dktypenames[lp->d_type]); 683 else 684 fprintf(f, "type: %d\n", lp->d_type); 685 fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename), 686 lp->d_typename); 687 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname), 688 lp->d_packname); 689 fprintf(f, "flags:"); 690 if (lp->d_flags & D_REMOVABLE) 691 fprintf(f, " removeable"); 692 if (lp->d_flags & D_ECC) 693 fprintf(f, " ecc"); 694 if (lp->d_flags & D_BADSECT) 695 fprintf(f, " badsect"); 696 fprintf(f, "\n"); 697 fprintf(f, "bytes/sector: %ld\n", lp->d_secsize); 698 fprintf(f, "sectors/track: %ld\n", lp->d_nsectors); 699 fprintf(f, "tracks/cylinder: %ld\n", lp->d_ntracks); 700 fprintf(f, "sectors/cylinder: %ld\n", lp->d_secpercyl); 701 fprintf(f, "cylinders: %ld\n", lp->d_ncylinders); 702 fprintf(f, "sectors/unit: %ld\n", lp->d_secperunit); 703 fprintf(f, "rpm: %d\n", lp->d_rpm); 704 fprintf(f, "interleave: %d\n", lp->d_interleave); 705 fprintf(f, "trackskew: %d\n", lp->d_trackskew); 706 fprintf(f, "cylinderskew: %d\n", lp->d_cylskew); 707 fprintf(f, "headswitch: %ld\t\t# milliseconds\n", lp->d_headswitch); 708 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n", 709 lp->d_trkseek); 710 fprintf(f, "drivedata: "); 711 for (i = NDDATA - 1; i >= 0; i--) 712 if (lp->d_drivedata[i]) 713 break; 714 if (i < 0) 715 i = 0; 716 for (j = 0; j <= i; j++) 717 fprintf(f, "%ld ", lp->d_drivedata[j]); 718 fprintf(f, "\n\n%d partitions:\n", lp->d_npartitions); 719 fprintf(f, 720 "# size offset fstype [fsize bsize bps/cpg]\n"); 721 pp = lp->d_partitions; 722 for (i = 0; i < lp->d_npartitions; i++, pp++) { 723 if (pp->p_size) { 724 fprintf(f, " %c: %8ld %8ld ", 'a' + i, 725 pp->p_size, pp->p_offset); 726 if ((unsigned) pp->p_fstype < FSMAXTYPES) 727 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]); 728 else 729 fprintf(f, "%8d", pp->p_fstype); 730 switch (pp->p_fstype) { 731 732 case FS_UNUSED: /* XXX */ 733 fprintf(f, " %5ld %5ld %5.5s ", 734 pp->p_fsize, pp->p_fsize * pp->p_frag, ""); 735 break; 736 737 case FS_BSDFFS: 738 fprintf(f, " %5ld %5ld %5d ", 739 pp->p_fsize, pp->p_fsize * pp->p_frag, 740 pp->p_cpg); 741 break; 742 743 case FS_BSDLFS: 744 fprintf(f, " %5ld %5ld %5d", 745 pp->p_fsize, pp->p_fsize * pp->p_frag, 746 pp->p_cpg); 747 break; 748 749 default: 750 fprintf(f, "%20.20s", ""); 751 break; 752 } 753 fprintf(f, "\t# (Cyl. %4ld", 754 pp->p_offset / lp->d_secpercyl); 755 if (pp->p_offset % lp->d_secpercyl) 756 putc('*', f); 757 else 758 putc(' ', f); 759 fprintf(f, "- %ld", 760 (pp->p_offset + 761 pp->p_size + lp->d_secpercyl - 1) / 762 lp->d_secpercyl - 1); 763 if (pp->p_size % lp->d_secpercyl) 764 putc('*', f); 765 fprintf(f, ")\n"); 766 } 767 } 768 fflush(f); 769} 770 771int 772edit(lp, f) 773 struct disklabel *lp; 774 int f; 775{ 776 register int c; 777 struct disklabel label; 778 FILE *fd; 779 780 (void) mktemp(tmpfil); 781 fd = fopen(tmpfil, "w"); 782 if (fd == NULL) { 783 fprintf(stderr, "%s: Can't create\n", tmpfil); 784 return (1); 785 } 786 (void)fchmod(fileno(fd), 0600); 787 display(fd, lp); 788 fclose(fd); 789 for (;;) { 790 if (!editit()) 791 break; 792 fd = fopen(tmpfil, "r"); 793 if (fd == NULL) { 794 fprintf(stderr, "%s: Can't reopen for reading\n", 795 tmpfil); 796 break; 797 } 798 bzero((char *)&label, sizeof(label)); 799 if (getasciilabel(fd, &label)) { 800 *lp = label; 801 if (writelabel(f, bootarea, lp) == 0) { 802 (void) unlink(tmpfil); 803 return (0); 804 } 805 } 806 printf("re-edit the label? [y]: "); fflush(stdout); 807 c = getchar(); 808 if (c != EOF && c != (int)'\n') 809 while (getchar() != (int)'\n') 810 ; 811 if (c == (int)'n') 812 break; 813 } 814 (void) unlink(tmpfil); 815 return (1); 816} 817 818int 819editit() 820{ 821 register int pid, xpid; 822 int stat, omask; 823 extern char *getenv(); 824 825 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP)); 826 while ((pid = fork()) < 0) { 827 extern int errno; 828 829 if (errno == EPROCLIM) { 830 fprintf(stderr, "You have too many processes\n"); 831 return(0); 832 } 833 if (errno != EAGAIN) { 834 perror("fork"); 835 return(0); 836 } 837 sleep(1); 838 } 839 if (pid == 0) { 840 register char *ed; 841 842 sigsetmask(omask); 843 setgid(getgid()); 844 setuid(getuid()); 845 if ((ed = getenv("EDITOR")) == (char *)0) 846 ed = DEFEDITOR; 847 execlp(ed, ed, tmpfil, 0); 848 perror(ed); 849 exit(1); 850 } 851 while ((xpid = wait(&stat)) >= 0) 852 if (xpid == pid) 853 break; 854 sigsetmask(omask); 855 return(!stat); 856} 857 858char * 859skip(cp) 860 register char *cp; 861{ 862 863 while (*cp != '\0' && isspace(*cp)) 864 cp++; 865 if (*cp == '\0' || *cp == '#') 866 return ((char *)NULL); 867 return (cp); 868} 869 870char * 871word(cp) 872 register char *cp; 873{ 874 register char c; 875 876 while (*cp != '\0' && !isspace(*cp) && *cp != '#') 877 cp++; 878 if ((c = *cp) != '\0') { 879 *cp++ = '\0'; 880 if (c != '#') 881 return (skip(cp)); 882 } 883 return ((char *)NULL); 884} 885 886/* 887 * Read an ascii label in from fd f, 888 * in the same format as that put out by display(), 889 * and fill in lp. 890 */ 891int 892getasciilabel(f, lp) 893 FILE *f; 894 register struct disklabel *lp; 895{ 896 register char **cpp, *cp; 897 register struct partition *pp; 898 char *tp, *s, line[BUFSIZ]; 899 int v, lineno = 0, errors = 0; 900 901 lp->d_bbsize = BBSIZE; /* XXX */ 902 lp->d_sbsize = SBSIZE; /* XXX */ 903 while (fgets(line, sizeof(line) - 1, f)) { 904 lineno++; 905 if ((cp = index(line,'\n')) != 0) 906 *cp = '\0'; 907 cp = skip(line); 908 if (cp == NULL) 909 continue; 910 tp = index(cp, ':'); 911 if (tp == NULL) { 912 fprintf(stderr, "line %d: syntax error\n", lineno); 913 errors++; 914 continue; 915 } 916 *tp++ = '\0', tp = skip(tp); 917 if (streq(cp, "type")) { 918 if (tp == NULL) 919 tp = "unknown"; 920 cpp = dktypenames; 921 for (; cpp < &dktypenames[DKMAXTYPES]; cpp++) 922 if ((s = *cpp) && streq(s, tp)) { 923 lp->d_type = cpp - dktypenames; 924 goto next; 925 } 926 v = atoi(tp); 927 if ((unsigned)v >= DKMAXTYPES) 928 fprintf(stderr, "line %d:%s %d\n", lineno, 929 "Warning, unknown disk type", v); 930 lp->d_type = v; 931 continue; 932 } 933 if (streq(cp, "flags")) { 934 for (v = 0; (cp = tp) && *cp != '\0';) { 935 tp = word(cp); 936 if (streq(cp, "removeable")) 937 v |= D_REMOVABLE; 938 else if (streq(cp, "ecc")) 939 v |= D_ECC; 940 else if (streq(cp, "badsect")) 941 v |= D_BADSECT; 942 else { 943 fprintf(stderr, 944 "line %d: %s: bad flag\n", 945 lineno, cp); 946 errors++; 947 } 948 } 949 lp->d_flags = v; 950 continue; 951 } 952 if (streq(cp, "drivedata")) { 953 register int i; 954 955 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) { 956 lp->d_drivedata[i++] = atoi(cp); 957 tp = word(cp); 958 } 959 continue; 960 } 961 if (sscanf(cp, "%d partitions", &v) == 1) { 962 if (v == 0 || (unsigned)v > MAXPARTITIONS) { 963 fprintf(stderr, 964 "line %d: bad # of partitions\n", lineno); 965 lp->d_npartitions = MAXPARTITIONS; 966 errors++; 967 } else 968 lp->d_npartitions = v; 969 continue; 970 } 971 if (tp == NULL) 972 tp = ""; 973 if (streq(cp, "disk")) { 974 strncpy(lp->d_typename, tp, sizeof (lp->d_typename)); 975 continue; 976 } 977 if (streq(cp, "label")) { 978 strncpy(lp->d_packname, tp, sizeof (lp->d_packname)); 979 continue; 980 } 981 if (streq(cp, "bytes/sector")) { 982 v = atoi(tp); 983 if (v <= 0 || (v % 512) != 0) { 984 fprintf(stderr, 985 "line %d: %s: bad sector size\n", 986 lineno, tp); 987 errors++; 988 } else 989 lp->d_secsize = v; 990 continue; 991 } 992 if (streq(cp, "sectors/track")) { 993 v = atoi(tp); 994 if (v <= 0) { 995 fprintf(stderr, "line %d: %s: bad %s\n", 996 lineno, tp, cp); 997 errors++; 998 } else 999 lp->d_nsectors = v; 1000 continue; 1001 } 1002 if (streq(cp, "sectors/cylinder")) { 1003 v = atoi(tp); 1004 if (v <= 0) { 1005 fprintf(stderr, "line %d: %s: bad %s\n", 1006 lineno, tp, cp); 1007 errors++; 1008 } else 1009 lp->d_secpercyl = v; 1010 continue; 1011 } 1012 if (streq(cp, "tracks/cylinder")) { 1013 v = atoi(tp); 1014 if (v <= 0) { 1015 fprintf(stderr, "line %d: %s: bad %s\n", 1016 lineno, tp, cp); 1017 errors++; 1018 } else 1019 lp->d_ntracks = v; 1020 continue; 1021 } 1022 if (streq(cp, "cylinders")) { 1023 v = atoi(tp); 1024 if (v <= 0) { 1025 fprintf(stderr, "line %d: %s: bad %s\n", 1026 lineno, tp, cp); 1027 errors++; 1028 } else 1029 lp->d_ncylinders = v; 1030 continue; 1031 } 1032 if (streq(cp, "sectors/unit")) { 1033 v = atoi(tp); 1034 if (v <= 0) { 1035 fprintf(stderr, "line %d: %s: bad %s\n", 1036 lineno, tp, cp); 1037 errors++; 1038 } else 1039 lp->d_secperunit = v; 1040 continue; 1041 } 1042 if (streq(cp, "rpm")) { 1043 v = atoi(tp); 1044 if (v <= 0) { 1045 fprintf(stderr, "line %d: %s: bad %s\n", 1046 lineno, tp, cp); 1047 errors++; 1048 } else 1049 lp->d_rpm = v; 1050 continue; 1051 } 1052 if (streq(cp, "interleave")) { 1053 v = atoi(tp); 1054 if (v <= 0) { 1055 fprintf(stderr, "line %d: %s: bad %s\n", 1056 lineno, tp, cp); 1057 errors++; 1058 } else 1059 lp->d_interleave = v; 1060 continue; 1061 } 1062 if (streq(cp, "trackskew")) { 1063 v = atoi(tp); 1064 if (v < 0) { 1065 fprintf(stderr, "line %d: %s: bad %s\n", 1066 lineno, tp, cp); 1067 errors++; 1068 } else 1069 lp->d_trackskew = v; 1070 continue; 1071 } 1072 if (streq(cp, "cylinderskew")) { 1073 v = atoi(tp); 1074 if (v < 0) { 1075 fprintf(stderr, "line %d: %s: bad %s\n", 1076 lineno, tp, cp); 1077 errors++; 1078 } else 1079 lp->d_cylskew = v; 1080 continue; 1081 } 1082 if (streq(cp, "headswitch")) { 1083 v = atoi(tp); 1084 if (v < 0) { 1085 fprintf(stderr, "line %d: %s: bad %s\n", 1086 lineno, tp, cp); 1087 errors++; 1088 } else 1089 lp->d_headswitch = v; 1090 continue; 1091 } 1092 if (streq(cp, "track-to-track seek")) { 1093 v = atoi(tp); 1094 if (v < 0) { 1095 fprintf(stderr, "line %d: %s: bad %s\n", 1096 lineno, tp, cp); 1097 errors++; 1098 } else 1099 lp->d_trkseek = v; 1100 continue; 1101 } 1102 if ('a' <= *cp && *cp <= 'z' && cp[1] == '\0') { 1103 unsigned part = *cp - 'a'; 1104 1105 if (part > lp->d_npartitions) { 1106 fprintf(stderr, 1107 "line %d: bad partition name\n", lineno); 1108 errors++; 1109 continue; 1110 } 1111 pp = &lp->d_partitions[part]; 1112#define NXTNUM(n) { \ 1113 if (tp == NULL) { \ 1114 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \ 1115 errors++; \ 1116 break; \ 1117 } else { \ 1118 cp = tp, tp = word(cp); \ 1119 if (tp == NULL) \ 1120 tp = cp; \ 1121 (n) = atoi(cp); \ 1122 } \ 1123 } 1124 1125 NXTNUM(v); 1126 if (v < 0) { 1127 fprintf(stderr, 1128 "line %d: %s: bad partition size\n", 1129 lineno, cp); 1130 errors++; 1131 } else 1132 pp->p_size = v; 1133 NXTNUM(v); 1134 if (v < 0) { 1135 fprintf(stderr, 1136 "line %d: %s: bad partition offset\n", 1137 lineno, cp); 1138 errors++; 1139 } else 1140 pp->p_offset = v; 1141 cp = tp, tp = word(cp); 1142 cpp = fstypenames; 1143 for (; cpp < &fstypenames[FSMAXTYPES]; cpp++) 1144 if ((s = *cpp) && streq(s, cp)) { 1145 pp->p_fstype = cpp - fstypenames; 1146 goto gottype; 1147 } 1148 if (isdigit(*cp)) 1149 v = atoi(cp); 1150 else 1151 v = FSMAXTYPES; 1152 if ((unsigned)v >= FSMAXTYPES) { 1153 fprintf(stderr, "line %d: %s %s\n", lineno, 1154 "Warning, unknown filesystem type", cp); 1155 v = FS_UNUSED; 1156 } 1157 pp->p_fstype = v; 1158 gottype: 1159 1160 switch (pp->p_fstype) { 1161 1162 case FS_UNUSED: /* XXX */ 1163 NXTNUM(pp->p_fsize); 1164 if (pp->p_fsize == 0) 1165 break; 1166 NXTNUM(v); 1167 pp->p_frag = v / pp->p_fsize; 1168 break; 1169 1170 case FS_BSDFFS: 1171 NXTNUM(pp->p_fsize); 1172 if (pp->p_fsize == 0) 1173 break; 1174 NXTNUM(v); 1175 pp->p_frag = v / pp->p_fsize; 1176 NXTNUM(pp->p_cpg); 1177 break; 1178 1179 case FS_BSDLFS: 1180 NXTNUM(pp->p_fsize); 1181 if (pp->p_fsize == 0) 1182 break; 1183 NXTNUM(v); 1184 pp->p_frag = v / pp->p_fsize; 1185 NXTNUM(pp->p_cpg); 1186 break; 1187 1188 default: 1189 break; 1190 } 1191 continue; 1192 } 1193 fprintf(stderr, "line %d: %s: Unknown disklabel field\n", 1194 lineno, cp); 1195 errors++; 1196 next: 1197 ; 1198 } 1199 errors += checklabel(lp); 1200 return (errors == 0); 1201} 1202 1203/* 1204 * Check disklabel for errors and fill in 1205 * derived fields according to supplied values. 1206 */ 1207int 1208checklabel(lp) 1209 register struct disklabel *lp; 1210{ 1211 register struct partition *pp; 1212 int i, errors = 0; 1213 char part; 1214 1215 if (lp->d_secsize == 0) { 1216 fprintf(stderr, "sector size %ld\n", lp->d_secsize); 1217 return (1); 1218 } 1219 if (lp->d_nsectors == 0) { 1220 fprintf(stderr, "sectors/track %ld\n", lp->d_nsectors); 1221 return (1); 1222 } 1223 if (lp->d_ntracks == 0) { 1224 fprintf(stderr, "tracks/cylinder %ld\n", lp->d_ntracks); 1225 return (1); 1226 } 1227 if (lp->d_ncylinders == 0) { 1228 fprintf(stderr, "cylinders/unit %ld\n", lp->d_ncylinders); 1229 errors++; 1230 } 1231 if (lp->d_rpm == 0) 1232 Warning("revolutions/minute %d", lp->d_rpm); 1233 if (lp->d_secpercyl == 0) 1234 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks; 1235 if (lp->d_secperunit == 0) 1236 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders; 1237 if (lp->d_bbsize == 0) { 1238 fprintf(stderr, "boot block size %ld\n", lp->d_bbsize); 1239 errors++; 1240 } else if (lp->d_bbsize % lp->d_secsize) 1241 Warning("boot block size %% sector-size != 0"); 1242 if (lp->d_sbsize == 0) { 1243 fprintf(stderr, "super block size %ld\n", lp->d_sbsize); 1244 errors++; 1245 } else if (lp->d_sbsize % lp->d_secsize) 1246 Warning("super block size %% sector-size != 0"); 1247 if (lp->d_npartitions > MAXPARTITIONS) 1248 Warning("number of partitions (%d) > MAXPARTITIONS (%d)", 1249 lp->d_npartitions, MAXPARTITIONS); 1250 for (i = 0; i < lp->d_npartitions; i++) { 1251 part = 'a' + i; 1252 pp = &lp->d_partitions[i]; 1253 if (pp->p_size == 0 && pp->p_offset != 0) 1254 Warning("partition %c: size 0, but offset %d", 1255 part, pp->p_offset); 1256#ifdef notdef 1257 if (pp->p_size % lp->d_secpercyl) 1258 Warning("partition %c: size %% cylinder-size != 0", 1259 part); 1260 if (pp->p_offset % lp->d_secpercyl) 1261 Warning("partition %c: offset %% cylinder-size != 0", 1262 part); 1263#endif 1264 if (pp->p_offset > lp->d_secperunit) { 1265 fprintf(stderr, 1266 "partition %c: offset past end of unit\n", part); 1267 errors++; 1268 } 1269 if (pp->p_offset + pp->p_size > lp->d_secperunit) { 1270 fprintf(stderr, 1271 "partition %c: partition extends past end of unit\n", 1272 part); 1273 errors++; 1274 } 1275 } 1276 for (; i < MAXPARTITIONS; i++) { 1277 part = 'a' + i; 1278 pp = &lp->d_partitions[i]; 1279 if (pp->p_size || pp->p_offset) 1280 Warning("unused partition %c: size %d offset %d", 1281 'a' + i, pp->p_size, pp->p_offset); 1282 } 1283 return (errors); 1284} 1285 1286/* 1287 * When operating on a "virgin" disk, try getting an initial label 1288 * from the associated device driver. This might work for all device 1289 * drivers that are able to fetch some initial device parameters 1290 * without even having access to a (BSD) disklabel, like SCSI disks, 1291 * most IDE drives, or vn devices. 1292 * 1293 * The device name must be given in its "canonical" form. 1294 */ 1295struct disklabel * 1296getvirginlabel(void) 1297{ 1298 static struct disklabel lab; 1299 char namebuf[BBSIZE]; 1300 int f; 1301 1302 if (dkname[0] == '/') { 1303 fprintf(stderr, 1304 "\"auto\" requires the usage of a canonical disk name.\n"); 1305 return 0; 1306 } 1307 (void)snprintf(namebuf, BBSIZE, "%sr%s", _PATH_DEV, dkname); 1308 if ((f = open(namebuf, O_RDONLY, 0)) == -1) { 1309 Perror("open()"); 1310 return 0; 1311 } 1312 if (ioctl(f, DIOCGDINFO, &lab) < 0) { 1313 Perror("ioctl DIOCGDINFO"); 1314 close(f); 1315 return 0; 1316 } 1317 close(f); 1318 /* insert reasonable defaults where necessary */ 1319 if (lab.d_npartitions < 8) lab.d_npartitions = 8; 1320 if (lab.d_bbsize == 0) lab.d_bbsize = BBSIZE; 1321 if (lab.d_sbsize == 0) lab.d_sbsize = SBSIZE; 1322 if (lab.d_rpm == 0) lab.d_rpm = 3600; 1323 if (lab.d_interleave == 0) lab.d_interleave = 1; 1324 return &lab; 1325} 1326 1327 1328/* 1329 * If we are installing a boot program that doesn't fit in d_bbsize 1330 * we need to mark those partitions that the boot overflows into. 1331 * This allows newfs to prevent creation of a filesystem where it might 1332 * clobber bootstrap code. 1333 */ 1334void 1335setbootflag(lp) 1336 register struct disklabel *lp; 1337{ 1338 register struct partition *pp; 1339 int i, errors = 0; 1340 char part; 1341 u_long boffset; 1342 1343 if (bootbuf == 0) 1344 return; 1345 boffset = bootsize / lp->d_secsize; 1346 for (i = 0; i < lp->d_npartitions; i++) { 1347 part = 'a' + i; 1348 pp = &lp->d_partitions[i]; 1349 if (pp->p_size == 0) 1350 continue; 1351 if (boffset <= pp->p_offset) { 1352 if (pp->p_fstype == FS_BOOT) 1353 pp->p_fstype = FS_UNUSED; 1354 } else if (pp->p_fstype != FS_BOOT) { 1355 if (pp->p_fstype != FS_UNUSED) { 1356 fprintf(stderr, 1357 "boot overlaps used partition %c\n", 1358 part); 1359 errors++; 1360 } else { 1361 pp->p_fstype = FS_BOOT; 1362 Warning("boot overlaps partition %c, %s", 1363 part, "marked as FS_BOOT"); 1364 } 1365 } 1366 } 1367 if (errors) { 1368 fprintf(stderr, "Cannot install boot program\n"); 1369 exit(4); 1370 } 1371} 1372 1373/*VARARGS1*/ 1374void 1375Warning(char *fmt, ...) 1376{ 1377 va_list ap; 1378 1379 fprintf(stderr, "Warning, "); 1380 va_start(ap, fmt); 1381 vfprintf(stderr, fmt, ap); 1382 fprintf(stderr, "\n"); 1383 va_end(ap); 1384} 1385 1386void 1387Perror(str) 1388 char *str; 1389{ 1390 fputs("disklabel: ", stderr); perror(str); 1391 exit(4); 1392} 1393 1394void 1395usage() 1396{ 1397#if NUMBOOT > 0 1398 fprintf(stderr, 1399"%s\n\t%s\n%s\n\t%s\n%s\n\t%s\n%s\n\t%s\n%s\n\t%s\n%s\n\t%s\n%s\n\t%s\n%s\n\t%s\n", 1400"usage: disklabel [-r] disk", 1401 "(to read label)", 1402"or disklabel -w [-r] disk type [ packid ]", 1403 "(to write label with existing boot program)", 1404"or disklabel -e [-r] disk", 1405 "(to edit label)", 1406"or disklabel -R [-r] disk protofile", 1407 "(to restore label with existing boot program)", 1408#if NUMBOOT > 1 1409"or disklabel -B [ -b boot1 [ -s boot2 ] ] disk [ type ]", 1410 "(to install boot program with existing label)", 1411"or disklabel -w -B [ -b boot1 [ -s boot2 ] ] disk type [ packid ]", 1412 "(to write label and boot program)", 1413"or disklabel -R -B [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]", 1414 "(to restore label and boot program)", 1415#else 1416"or disklabel -B [ -b bootprog ] disk [ type ]", 1417 "(to install boot program with existing on-disk label)", 1418"or disklabel -w -B [ -b bootprog ] disk type [ packid ]", 1419 "(to write label and install boot program)", 1420"or disklabel -R -B [ -b bootprog ] disk protofile [ type ]", 1421 "(to restore label and install boot program)", 1422#endif 1423"or disklabel [-NW] disk", 1424 "(to write disable/enable label)"); 1425#else 1426 fprintf(stderr, "%-43s%s\n%-43s%s\n%-43s%s\n%-43s%s\n%-43s%s\n", 1427"usage: disklabel [-r] disk", "(to read label)", 1428"or disklabel -w [-r] disk type [ packid ]", "(to write label)", 1429"or disklabel -e [-r] disk", "(to edit label)", 1430"or disklabel -R [-r] disk protofile", "(to restore label)", 1431"or disklabel [-NW] disk", "(to write disable/enable label)"); 1432#endif 1433 exit(1); 1434} 1435