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