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