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