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