bsdlabel.c revision 114862
1290001Sglebius/* 2290001Sglebius * Copyright (c) 1994, 1995 Gordon W. Ross 3290001Sglebius * Copyright (c) 1994 Theo de Raadt 4290001Sglebius * All rights reserved. 5290001Sglebius * Copyright (c) 1987, 1993 6290001Sglebius * The Regents of the University of California. All rights reserved. 7290001Sglebius * 8290001Sglebius * This code is derived from software contributed to Berkeley by 9290001Sglebius * Symmetric Computer Systems. 10290001Sglebius * 11290001Sglebius * Redistribution and use in source and binary forms, with or without 12290001Sglebius * modification, are permitted provided that the following conditions 13290001Sglebius * are met: 14290001Sglebius * 1. Redistributions of source code must retain the above copyright 15290001Sglebius * notice, this list of conditions and the following disclaimer. 16290001Sglebius * 2. Redistributions in binary form must reproduce the above copyright 17290001Sglebius * notice, this list of conditions and the following disclaimer in the 18290001Sglebius * documentation and/or other materials provided with the distribution. 19290001Sglebius * 3. All advertising materials mentioning features or use of this software 20290001Sglebius * must display the following acknowledgement: 21290001Sglebius * This product includes software developed by the University of 22290001Sglebius * California, Berkeley and its contributors. 23290001Sglebius * This product includes software developed by Theo de Raadt. 24290001Sglebius * 4. Neither the name of the University nor the names of its contributors 25290001Sglebius * may be used to endorse or promote products derived from this software 26290001Sglebius * without specific prior written permission. 27290001Sglebius * 28290001Sglebius * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29290001Sglebius * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30290001Sglebius * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31290001Sglebius * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32290001Sglebius * 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 114862 2003-05-09 20:26:17Z 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 makelabel("auto", lp); 660 bzero(&part_set, sizeof(part_set)); 661 bzero(&part_size_type, sizeof(part_size_type)); 662 bzero(&part_offset_type, sizeof(part_offset_type)); 663 lp->d_bbsize = BBSIZE; /* XXX */ 664 lp->d_sbsize = 0; /* XXX */ 665 while (fgets(line, sizeof(line) - 1, f)) { 666 lineno++; 667 if ((cp = index(line,'\n')) != 0) 668 *cp = '\0'; 669 cp = skip(line); 670 if (cp == NULL) 671 continue; 672 tp = index(cp, ':'); 673 if (tp == NULL) { 674 fprintf(stderr, "line %d: syntax error\n", lineno); 675 errors++; 676 continue; 677 } 678 *tp++ = '\0', tp = skip(tp); 679 if (!strcmp(cp, "type")) { 680 if (tp == NULL) 681 tp = unknown; 682 cpp = dktypenames; 683 for (; cpp < &dktypenames[DKMAXTYPES]; cpp++) 684 if (*cpp && !strcmp(*cpp, tp)) { 685 lp->d_type = cpp - dktypenames; 686 break; 687 } 688 if (cpp < &dktypenames[DKMAXTYPES]) 689 continue; 690 v = strtoul(tp, NULL, 10); 691 if (v >= DKMAXTYPES) 692 fprintf(stderr, "line %d:%s %lu\n", lineno, 693 "Warning, unknown disk type", v); 694 lp->d_type = v; 695 continue; 696 } 697 if (!strcmp(cp, "flags")) { 698 for (v = 0; (cp = tp) && *cp != '\0';) { 699 tp = word(cp); 700 if (!strcmp(cp, "removeable")) 701 v |= D_REMOVABLE; 702 else if (!strcmp(cp, "ecc")) 703 v |= D_ECC; 704 else if (!strcmp(cp, "badsect")) 705 v |= D_BADSECT; 706 else { 707 fprintf(stderr, 708 "line %d: %s: bad flag\n", 709 lineno, cp); 710 errors++; 711 } 712 } 713 lp->d_flags = v; 714 continue; 715 } 716 if (!strcmp(cp, "drivedata")) { 717 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) { 718 lp->d_drivedata[i++] = strtoul(cp, NULL, 10); 719 tp = word(cp); 720 } 721 continue; 722 } 723 if (sscanf(cp, "%lu partitions", &v) == 1) { 724 if (v == 0 || v > MAXPARTITIONS) { 725 fprintf(stderr, 726 "line %d: bad # of partitions\n", lineno); 727 lp->d_npartitions = MAXPARTITIONS; 728 errors++; 729 } else 730 lp->d_npartitions = v; 731 continue; 732 } 733 if (tp == NULL) 734 tp = blank; 735 if (!strcmp(cp, "disk")) { 736 strncpy(lp->d_typename, tp, sizeof (lp->d_typename)); 737 continue; 738 } 739 if (!strcmp(cp, "label")) { 740 strncpy(lp->d_packname, tp, sizeof (lp->d_packname)); 741 continue; 742 } 743 if (!strcmp(cp, "bytes/sector")) { 744 v = strtoul(tp, NULL, 10); 745 if (v == 0 || (v % DEV_BSIZE) != 0) { 746 fprintf(stderr, 747 "line %d: %s: bad sector size\n", 748 lineno, tp); 749 errors++; 750 } else 751 lp->d_secsize = v; 752 continue; 753 } 754 if (!strcmp(cp, "sectors/track")) { 755 v = strtoul(tp, NULL, 10); 756#if (ULONG_MAX != 0xffffffffUL) 757 if (v == 0 || v > 0xffffffff) { 758#else 759 if (v == 0) { 760#endif 761 fprintf(stderr, "line %d: %s: bad %s\n", 762 lineno, tp, cp); 763 errors++; 764 } else 765 lp->d_nsectors = v; 766 continue; 767 } 768 if (!strcmp(cp, "sectors/cylinder")) { 769 v = strtoul(tp, NULL, 10); 770 if (v == 0) { 771 fprintf(stderr, "line %d: %s: bad %s\n", 772 lineno, tp, cp); 773 errors++; 774 } else 775 lp->d_secpercyl = v; 776 continue; 777 } 778 if (!strcmp(cp, "tracks/cylinder")) { 779 v = strtoul(tp, NULL, 10); 780 if (v == 0) { 781 fprintf(stderr, "line %d: %s: bad %s\n", 782 lineno, tp, cp); 783 errors++; 784 } else 785 lp->d_ntracks = v; 786 continue; 787 } 788 if (!strcmp(cp, "cylinders")) { 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_ncylinders = v; 796 continue; 797 } 798 if (!strcmp(cp, "sectors/unit")) { 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_secperunit = v; 806 continue; 807 } 808 if (!strcmp(cp, "rpm")) { 809 v = strtoul(tp, NULL, 10); 810 if (v == 0 || v > USHRT_MAX) { 811 fprintf(stderr, "line %d: %s: bad %s\n", 812 lineno, tp, cp); 813 errors++; 814 } else 815 lp->d_rpm = v; 816 continue; 817 } 818 if (!strcmp(cp, "interleave")) { 819 v = strtoul(tp, NULL, 10); 820 if (v == 0 || v > USHRT_MAX) { 821 fprintf(stderr, "line %d: %s: bad %s\n", 822 lineno, tp, cp); 823 errors++; 824 } else 825 lp->d_interleave = v; 826 continue; 827 } 828 if (!strcmp(cp, "trackskew")) { 829 v = strtoul(tp, NULL, 10); 830 if (v > USHRT_MAX) { 831 fprintf(stderr, "line %d: %s: bad %s\n", 832 lineno, tp, cp); 833 errors++; 834 } else 835 lp->d_trackskew = v; 836 continue; 837 } 838 if (!strcmp(cp, "cylinderskew")) { 839 v = strtoul(tp, NULL, 10); 840 if (v > USHRT_MAX) { 841 fprintf(stderr, "line %d: %s: bad %s\n", 842 lineno, tp, cp); 843 errors++; 844 } else 845 lp->d_cylskew = v; 846 continue; 847 } 848 if (!strcmp(cp, "headswitch")) { 849 v = strtoul(tp, NULL, 10); 850 lp->d_headswitch = v; 851 continue; 852 } 853 if (!strcmp(cp, "track-to-track seek")) { 854 v = strtoul(tp, NULL, 10); 855 lp->d_trkseek = v; 856 continue; 857 } 858 /* the ':' was removed above */ 859 if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') { 860 fprintf(stderr, 861 "line %d: %s: Unknown disklabel field\n", lineno, 862 cp); 863 errors++; 864 continue; 865 } 866 867 /* Process a partition specification line. */ 868 part = *cp - 'a'; 869 if (part >= lp->d_npartitions) { 870 fprintf(stderr, 871 "line %d: partition name out of range a-%c: %s\n", 872 lineno, 'a' + lp->d_npartitions - 1, cp); 873 errors++; 874 continue; 875 } 876 part_set[part] = 1; 877 878 if (getasciipartspec(tp, lp, part, lineno) != 0) { 879 errors++; 880 break; 881 } 882 } 883 errors += checklabel(lp); 884 return (errors == 0); 885} 886 887#define NXTNUM(n) do { \ 888 if (tp == NULL) { \ 889 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \ 890 return (1); \ 891 } else { \ 892 cp = tp, tp = word(cp); \ 893 (n) = strtoul(cp, NULL, 10); \ 894 } \ 895} while (0) 896 897/* retain 1 character following number */ 898#define NXTWORD(w,n) do { \ 899 if (tp == NULL) { \ 900 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \ 901 return (1); \ 902 } else { \ 903 char *tmp; \ 904 cp = tp, tp = word(cp); \ 905 (n) = strtoul(cp, &tmp, 10); \ 906 if (tmp) (w) = *tmp; \ 907 } \ 908} while (0) 909 910/* 911 * Read a partition line into partition `part' in the specified disklabel. 912 * Return 0 on success, 1 on failure. 913 */ 914static int 915getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno) 916{ 917 struct partition *pp; 918 char *cp; 919 const char **cpp; 920 u_long v; 921 922 pp = &lp->d_partitions[part]; 923 cp = NULL; 924 925 v = 0; 926 NXTWORD(part_size_type[part],v); 927 if (v == 0 && part_size_type[part] != '*') { 928 fprintf(stderr, 929 "line %d: %s: bad partition size\n", lineno, cp); 930 return (1); 931 } 932 pp->p_size = v; 933 934 v = 0; 935 NXTWORD(part_offset_type[part],v); 936 if (v == 0 && part_offset_type[part] != '*' && 937 part_offset_type[part] != '\0') { 938 fprintf(stderr, 939 "line %d: %s: bad partition offset\n", lineno, cp); 940 return (1); 941 } 942 pp->p_offset = v; 943 if (tp == NULL) { 944 fprintf(stderr, "line %d: missing file system type\n", lineno); 945 return (1); 946 } 947 cp = tp, tp = word(cp); 948 for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++) 949 if (*cpp && !strcmp(*cpp, cp)) 950 break; 951 if (*cpp != NULL) { 952 pp->p_fstype = cpp - fstypenames; 953 } else { 954 if (isdigit(*cp)) 955 v = strtoul(cp, NULL, 10); 956 else 957 v = FSMAXTYPES; 958 if (v >= FSMAXTYPES) { 959 fprintf(stderr, 960 "line %d: Warning, unknown file system type %s\n", 961 lineno, cp); 962 v = FS_UNUSED; 963 } 964 pp->p_fstype = v; 965 } 966 967 switch (pp->p_fstype) { 968 case FS_UNUSED: 969 /* 970 * allow us to accept defaults for 971 * fsize/frag/cpg 972 */ 973 if (tp) { 974 NXTNUM(pp->p_fsize); 975 if (pp->p_fsize == 0) 976 break; 977 NXTNUM(v); 978 pp->p_frag = v / pp->p_fsize; 979 } 980 /* else default to 0's */ 981 break; 982 983 /* These happen to be the same */ 984 case FS_BSDFFS: 985 case FS_BSDLFS: 986 if (tp) { 987 NXTNUM(pp->p_fsize); 988 if (pp->p_fsize == 0) 989 break; 990 NXTNUM(v); 991 pp->p_frag = v / pp->p_fsize; 992 NXTNUM(pp->p_cpg); 993 } else { 994 /* 995 * FIX! poor attempt at adaptive 996 */ 997 /* 1 GB */ 998 if (pp->p_size < 1024*1024*1024 / lp->d_secsize) { 999 /* 1000 * FIX! These are too low, but are traditional 1001 */ 1002 pp->p_fsize = DEFAULT_NEWFS_FRAG; 1003 pp->p_frag = DEFAULT_NEWFS_BLOCK / 1004 DEFAULT_NEWFS_FRAG; 1005 pp->p_cpg = DEFAULT_NEWFS_CPG; 1006 } else { 1007 pp->p_fsize = BIG_NEWFS_FRAG; 1008 pp->p_frag = BIG_NEWFS_BLOCK / 1009 BIG_NEWFS_FRAG; 1010 pp->p_cpg = BIG_NEWFS_CPG; 1011 } 1012 } 1013 default: 1014 break; 1015 } 1016 return (0); 1017} 1018 1019/* 1020 * Check disklabel for errors and fill in 1021 * derived fields according to supplied values. 1022 */ 1023static int 1024checklabel(struct disklabel *lp) 1025{ 1026 struct partition *pp; 1027 int i, errors = 0; 1028 char part; 1029 u_long total_size, total_percent, current_offset; 1030 int seen_default_offset; 1031 int hog_part; 1032 int j; 1033 struct partition *pp2; 1034 1035 if (lp == NULL) 1036 lp = &lab; 1037 1038 if (allfields) { 1039 1040 if (lp->d_secsize == 0) { 1041 fprintf(stderr, "sector size 0\n"); 1042 return (1); 1043 } 1044 if (lp->d_nsectors == 0) { 1045 fprintf(stderr, "sectors/track 0\n"); 1046 return (1); 1047 } 1048 if (lp->d_ntracks == 0) { 1049 fprintf(stderr, "tracks/cylinder 0\n"); 1050 return (1); 1051 } 1052 if (lp->d_ncylinders == 0) { 1053 fprintf(stderr, "cylinders/unit 0\n"); 1054 errors++; 1055 } 1056 if (lp->d_rpm == 0) 1057 warnx("revolutions/minute 0"); 1058 if (lp->d_secpercyl == 0) 1059 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks; 1060 if (lp->d_secperunit == 0) 1061 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders; 1062 if (lp->d_bbsize == 0) { 1063 fprintf(stderr, "boot block size 0\n"); 1064 errors++; 1065 } else if (lp->d_bbsize % lp->d_secsize) 1066 warnx("boot block size %% sector-size != 0"); 1067 if (lp->d_npartitions > MAXPARTITIONS) 1068 warnx("number of partitions (%lu) > MAXPARTITIONS (%d)", 1069 (u_long)lp->d_npartitions, MAXPARTITIONS); 1070 } else { 1071 struct disklabel *vl; 1072 1073 vl = getvirginlabel(); 1074 lp->d_secsize = vl->d_secsize; 1075 lp->d_nsectors = vl->d_nsectors; 1076 lp->d_ntracks = vl->d_ntracks; 1077 lp->d_ncylinders = vl->d_ncylinders; 1078 lp->d_rpm = vl->d_rpm; 1079 lp->d_interleave = vl->d_interleave; 1080 lp->d_secpercyl = vl->d_secpercyl; 1081 lp->d_secperunit = vl->d_secperunit; 1082 lp->d_bbsize = vl->d_bbsize; 1083 lp->d_npartitions = vl->d_npartitions; 1084 } 1085 1086 1087 /* first allocate space to the partitions, then offsets */ 1088 total_size = 0; /* in sectors */ 1089 total_percent = 0; /* in percent */ 1090 hog_part = -1; 1091 /* find all fixed partitions */ 1092 for (i = 0; i < lp->d_npartitions; i++) { 1093 pp = &lp->d_partitions[i]; 1094 if (part_set[i]) { 1095 if (part_size_type[i] == '*') { 1096 if (i == RAW_PART) { 1097 pp->p_size = lp->d_secperunit; 1098 } else { 1099 if (hog_part != -1) 1100 warnx("Too many '*' partitions (%c and %c)", 1101 hog_part + 'a',i + 'a'); 1102 else 1103 hog_part = i; 1104 } 1105 } else { 1106 off_t size; 1107 1108 size = pp->p_size; 1109 switch (part_size_type[i]) { 1110 case '%': 1111 total_percent += size; 1112 break; 1113 case 'k': 1114 case 'K': 1115 size *= 1024ULL; 1116 break; 1117 case 'm': 1118 case 'M': 1119 size *= 1024ULL * 1024ULL; 1120 break; 1121 case 'g': 1122 case 'G': 1123 size *= 1024ULL * 1024ULL * 1024ULL; 1124 break; 1125 case '\0': 1126 break; 1127 default: 1128 warnx("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]); 1129 break; 1130 } 1131 /* don't count %'s yet */ 1132 if (part_size_type[i] != '%') { 1133 /* 1134 * for all not in sectors, convert to 1135 * sectors 1136 */ 1137 if (part_size_type[i] != '\0') { 1138 if (size % lp->d_secsize != 0) 1139 warnx("partition %c not an integer number of sectors", 1140 i + 'a'); 1141 size /= lp->d_secsize; 1142 pp->p_size = size; 1143 } 1144 /* else already in sectors */ 1145 if (i != RAW_PART) 1146 total_size += size; 1147 } 1148 } 1149 } 1150 } 1151 /* handle % partitions - note %'s don't need to add up to 100! */ 1152 if (total_percent != 0) { 1153 long free_space = lp->d_secperunit - total_size; 1154 if (total_percent > 100) { 1155 fprintf(stderr,"total percentage %lu is greater than 100\n", 1156 total_percent); 1157 errors++; 1158 } 1159 1160 if (free_space > 0) { 1161 for (i = 0; i < lp->d_npartitions; i++) { 1162 pp = &lp->d_partitions[i]; 1163 if (part_set[i] && part_size_type[i] == '%') { 1164 /* careful of overflows! and integer roundoff */ 1165 pp->p_size = ((double)pp->p_size/100) * free_space; 1166 total_size += pp->p_size; 1167 1168 /* FIX we can lose a sector or so due to roundoff per 1169 partition. A more complex algorithm could avoid that */ 1170 } 1171 } 1172 } else { 1173 fprintf(stderr, 1174 "%ld sectors available to give to '*' and '%%' partitions\n", 1175 free_space); 1176 errors++; 1177 /* fix? set all % partitions to size 0? */ 1178 } 1179 } 1180 /* give anything remaining to the hog partition */ 1181 if (hog_part != -1) { 1182 lp->d_partitions[hog_part].p_size = lp->d_secperunit - total_size; 1183 total_size = lp->d_secperunit; 1184 } 1185 1186 /* Now set the offsets for each partition */ 1187 current_offset = 0; /* in sectors */ 1188 seen_default_offset = 0; 1189 for (i = 0; i < lp->d_npartitions; i++) { 1190 part = 'a' + i; 1191 pp = &lp->d_partitions[i]; 1192 if (part_set[i]) { 1193 if (part_offset_type[i] == '*') { 1194 if (i == RAW_PART) { 1195 pp->p_offset = 0; 1196 } else { 1197 pp->p_offset = current_offset; 1198 seen_default_offset = 1; 1199 } 1200 } else { 1201 /* allow them to be out of order for old-style tables */ 1202 if (pp->p_offset < current_offset && 1203 seen_default_offset && i != RAW_PART && 1204 pp->p_fstype != FS_VINUM) { 1205 fprintf(stderr, 1206"Offset %ld for partition %c overlaps previous partition which ends at %lu\n", 1207 (long)pp->p_offset,i+'a',current_offset); 1208 fprintf(stderr, 1209"Labels with any *'s for offset must be in ascending order by sector\n"); 1210 errors++; 1211 } else if (pp->p_offset != current_offset && 1212 i != RAW_PART && seen_default_offset) { 1213 /* 1214 * this may give unneeded warnings if 1215 * partitions are out-of-order 1216 */ 1217 warnx( 1218"Offset %ld for partition %c doesn't match expected value %ld", 1219 (long)pp->p_offset, i + 'a', current_offset); 1220 } 1221 } 1222 if (i != RAW_PART) 1223 current_offset = pp->p_offset + pp->p_size; 1224 } 1225 } 1226 1227 for (i = 0; i < lp->d_npartitions; i++) { 1228 part = 'a' + i; 1229 pp = &lp->d_partitions[i]; 1230 if (pp->p_size == 0 && pp->p_offset != 0) 1231 warnx("partition %c: size 0, but offset %lu", 1232 part, (u_long)pp->p_offset); 1233#ifdef notdef 1234 if (pp->p_size % lp->d_secpercyl) 1235 warnx("partition %c: size %% cylinder-size != 0", 1236 part); 1237 if (pp->p_offset % lp->d_secpercyl) 1238 warnx("partition %c: offset %% cylinder-size != 0", 1239 part); 1240#endif 1241 if (pp->p_offset > lp->d_secperunit) { 1242 fprintf(stderr, 1243 "partition %c: offset past end of unit\n", part); 1244 errors++; 1245 } 1246 if (pp->p_offset + pp->p_size > lp->d_secperunit) { 1247 fprintf(stderr, 1248 "partition %c: partition extends past end of unit\n", 1249 part); 1250 errors++; 1251 } 1252 if (i == RAW_PART) { 1253 if (pp->p_fstype != FS_UNUSED) 1254 warnx("partition %c is not marked as unused!",part); 1255 if (pp->p_offset != 0) 1256 warnx("partition %c doesn't start at 0!",part); 1257 if (pp->p_size != lp->d_secperunit) 1258 warnx("partition %c doesn't cover the whole unit!",part); 1259 1260 if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) || 1261 (pp->p_size != lp->d_secperunit)) { 1262 warnx("An incorrect partition %c may cause problems for " 1263 "standard system utilities",part); 1264 } 1265 } 1266 1267 /* check for overlaps */ 1268 /* this will check for all possible overlaps once and only once */ 1269 for (j = 0; j < i; j++) { 1270 pp2 = &lp->d_partitions[j]; 1271 if (j != RAW_PART && i != RAW_PART && 1272 pp->p_fstype != FS_VINUM && 1273 pp2->p_fstype != FS_VINUM && 1274 part_set[i] && part_set[j]) { 1275 if (pp2->p_offset < pp->p_offset + pp->p_size && 1276 (pp2->p_offset + pp2->p_size > pp->p_offset || 1277 pp2->p_offset >= pp->p_offset)) { 1278 fprintf(stderr,"partitions %c and %c overlap!\n", 1279 j + 'a', i + 'a'); 1280 errors++; 1281 } 1282 } 1283 } 1284 } 1285 for (; i < MAXPARTITIONS; i++) { 1286 part = 'a' + i; 1287 pp = &lp->d_partitions[i]; 1288 if (pp->p_size || pp->p_offset) 1289 warnx("unused partition %c: size %d offset %lu", 1290 'a' + i, pp->p_size, (u_long)pp->p_offset); 1291 } 1292 return (errors); 1293} 1294 1295/* 1296 * When operating on a "virgin" disk, try getting an initial label 1297 * from the associated device driver. This might work for all device 1298 * drivers that are able to fetch some initial device parameters 1299 * without even having access to a (BSD) disklabel, like SCSI disks, 1300 * most IDE drives, or vn devices. 1301 * 1302 * The device name must be given in its "canonical" form. 1303 */ 1304static struct disklabel * 1305getvirginlabel(void) 1306{ 1307 static struct disklabel loclab; 1308 struct partition *dp; 1309 int f; 1310 u_int u; 1311 1312 if ((f = open(specname, O_RDONLY)) == -1) { 1313 warn("cannot open %s", specname); 1314 return (NULL); 1315 } 1316 1317 /* New world order */ 1318 if ((ioctl(f, DIOCGMEDIASIZE, &mediasize) != 0) || 1319 (ioctl(f, DIOCGSECTORSIZE, &secsize) != 0)) { 1320 close (f); 1321 return (NULL); 1322 } 1323 memset(&loclab, 0, sizeof loclab); 1324 loclab.d_magic = DISKMAGIC; 1325 loclab.d_magic2 = DISKMAGIC; 1326 loclab.d_secsize = secsize; 1327 loclab.d_secperunit = mediasize / secsize; 1328 1329 /* 1330 * Nobody in these enligthened days uses the CHS geometry for 1331 * anything, but nontheless try to get it right. If we fail 1332 * to get any good ideas from the device, construct something 1333 * which is IBM-PC friendly. 1334 */ 1335 if (ioctl(f, DIOCGFWSECTORS, &u) == 0) 1336 loclab.d_nsectors = u; 1337 else 1338 loclab.d_nsectors = 63; 1339 if (ioctl(f, DIOCGFWHEADS, &u) == 0) 1340 loclab.d_ntracks = u; 1341 else if (loclab.d_secperunit <= 63*1*1024) 1342 loclab.d_ntracks = 1; 1343 else if (loclab.d_secperunit <= 63*16*1024) 1344 loclab.d_ntracks = 16; 1345 else 1346 loclab.d_ntracks = 255; 1347 loclab.d_secpercyl = loclab.d_ntracks * loclab.d_nsectors; 1348 loclab.d_ncylinders = loclab.d_secperunit / loclab.d_secpercyl; 1349 loclab.d_npartitions = MAXPARTITIONS; 1350 1351 /* Various (unneeded) compat stuff */ 1352 loclab.d_rpm = 3600; 1353 loclab.d_bbsize = BBSIZE; 1354 loclab.d_interleave = 1; 1355 strncpy(loclab.d_typename, "amnesiac", 1356 sizeof(loclab.d_typename)); 1357 1358 dp = &loclab.d_partitions[RAW_PART]; 1359 dp->p_size = loclab.d_secperunit; 1360 loclab.d_checksum = dkcksum(&loclab); 1361 close (f); 1362 return (&loclab); 1363} 1364 1365static void 1366usage(void) 1367{ 1368 1369 fprintf(stderr, 1370 "%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", 1371 "usage: bsdlabel disk", 1372 "\t\t(to read label)", 1373 " bsdlabel -w [-n] [-m machine] disk [type]", 1374 "\t\t(to write label with existing boot program)", 1375 " bsdlabel -e [-n] [-m machine] disk", 1376 "\t\t(to edit label)", 1377 " bsdlabel -R [-n] [-m machine] disk protofile", 1378 "\t\t(to restore label with existing boot program)", 1379 " bsdlabel -B [-b boot] [-m machine] disk", 1380 "\t\t(to install boot program with existing on-disk label)", 1381 " bsdlabel -w -B [-n] [-b boot] [-m machine] disk [type]", 1382 "\t\t(to write label and install boot program)", 1383 " bsdlabel -R -B [-n] [-b boot] [-m machine] disk protofile", 1384 "\t\t(to restore label and install boot program)" 1385 ); 1386 exit(1); 1387} 1388