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