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