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