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