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 uint32_t lba_offset; 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 += lba_offset; 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 uint32_t lba; 483 int f, i; 484 int error; 485 486 f = open(specname, O_RDONLY); 487 if (f < 0) 488 err(1, specname); 489 if (is_file) 490 get_file_parms(f); 491 else { 492 mediasize = g_mediasize(f); 493 secsize = g_sectorsize(f); 494 if (secsize < 0 || mediasize < 0) 495 err(4, "cannot get disk geometry"); 496 } 497 if (mediasize > (off_t)0xffffffff * secsize) 498 errx(1, 499 "disks with more than 2^32-1 sectors are not supported"); 500 (void)lseek(f, (off_t)0, SEEK_SET); 501 if (read(f, bootarea, BBSIZE) != BBSIZE) 502 err(4, "%s read", specname); 503 close (f); 504 error = bsd_disklabel_le_dec( 505 bootarea + (labeloffset + labelsoffset * secsize), 506 &lab, MAXPARTITIONS); 507 if (flag && error) 508 errx(1, "%s: no valid label found", specname); 509 510 if (is_file) 511 return(0); 512 513 /* 514 * Compensate for absolute block addressing by finding the 515 * smallest partition offset and if the offset of the 'c' 516 * partition is equal to that, subtract it from all offsets. 517 */ 518 lba = ~0; 519 for (i = 0; i < lab.d_npartitions; i++) { 520 if (lab.d_partitions[i].p_size) 521 lba = MIN(lba, lab.d_partitions[i].p_offset); 522 } 523 if (lba != 0 && lab.d_partitions[RAW_PART].p_offset == lba) { 524 for (i = 0; i < lab.d_npartitions; i++) { 525 if (lab.d_partitions[i].p_size) 526 lab.d_partitions[i].p_offset -= lba; 527 } 528 /* 529 * Save the offset so that we can write the label 530 * back with absolute block addresses. 531 */ 532 lba_offset = lba; 533 } 534 return (error); 535 } 536 537 538 static void 539 display(FILE *f, const struct disklabel *lp) 540 { 541 int i, j; 542 const struct partition *pp; 543 544 if (lp == NULL) 545 lp = &lab; 546 547 fprintf(f, "# %s:\n", specname); 548 if (allfields) { 549 if (lp->d_type < DKMAXTYPES) 550 fprintf(f, "type: %s\n", dktypenames[lp->d_type]); 551 else 552 fprintf(f, "type: %u\n", lp->d_type); 553 fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename), 554 lp->d_typename); 555 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname), 556 lp->d_packname); 557 fprintf(f, "flags:"); 558 if (lp->d_flags & D_REMOVABLE) 559 fprintf(f, " removeable"); 560 if (lp->d_flags & D_ECC) 561 fprintf(f, " ecc"); 562 if (lp->d_flags & D_BADSECT) 563 fprintf(f, " badsect"); 564 fprintf(f, "\n"); 565 fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize); 566 fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors); 567 fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks); 568 fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl); 569 fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders); 570 fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit); 571 fprintf(f, "rpm: %u\n", lp->d_rpm); 572 fprintf(f, "interleave: %u\n", lp->d_interleave); 573 fprintf(f, "trackskew: %u\n", lp->d_trackskew); 574 fprintf(f, "cylinderskew: %u\n", lp->d_cylskew); 575 fprintf(f, "headswitch: %lu\t\t# milliseconds\n", 576 (u_long)lp->d_headswitch); 577 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n", 578 (u_long)lp->d_trkseek); 579 fprintf(f, "drivedata: "); 580 for (i = NDDATA - 1; i >= 0; i--) 581 if (lp->d_drivedata[i]) 582 break; 583 if (i < 0) 584 i = 0; 585 for (j = 0; j <= i; j++) 586 fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]); 587 fprintf(f, "\n\n"); 588 } 589 fprintf(f, "%u partitions:\n", lp->d_npartitions); 590 fprintf(f, 591 "# size offset fstype [fsize bsize bps/cpg]\n"); 592 pp = lp->d_partitions; 593 for (i = 0; i < lp->d_npartitions; i++, pp++) { 594 if (pp->p_size) { 595 fprintf(f, " %c: %8lu %8lu ", 'a' + i, 596 (u_long)pp->p_size, (u_long)pp->p_offset); 597 if (pp->p_fstype < FSMAXTYPES) 598 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]); 599 else 600 fprintf(f, "%8d", pp->p_fstype); 601 switch (pp->p_fstype) { 602 603 case FS_UNUSED: /* XXX */ 604 fprintf(f, " %5lu %5lu %5.5s ", 605 (u_long)pp->p_fsize, 606 (u_long)(pp->p_fsize * pp->p_frag), ""); 607 break; 608 609 case FS_BSDFFS: 610 fprintf(f, " %5lu %5lu %5u ", 611 (u_long)pp->p_fsize, 612 (u_long)(pp->p_fsize * pp->p_frag), 613 pp->p_cpg); 614 break; 615 616 case FS_BSDLFS: 617 fprintf(f, " %5lu %5lu %5d", 618 (u_long)pp->p_fsize, 619 (u_long)(pp->p_fsize * pp->p_frag), 620 pp->p_cpg); 621 break; 622 623 default: 624 fprintf(f, "%20.20s", ""); 625 break; 626 } 627 if (i == RAW_PART) { 628 fprintf(f, " # \"raw\" part, don't edit"); 629 } 630 fprintf(f, "\n"); 631 } 632 } 633 fflush(f); 634 } 635 636 static int 637 edit(void) 638 { 639 int c, fd; 640 struct disklabel label; 641 FILE *fp; 642 643 if ((fd = mkstemp(tmpfil)) == -1 || 644 (fp = fdopen(fd, "w")) == NULL) { 645 warnx("can't create %s", tmpfil); 646 return (1); 647 } 648 display(fp, NULL); 649 fclose(fp); 650 for (;;) { 651 if (!editit()) 652 break; 653 fp = fopen(tmpfil, "r"); 654 if (fp == NULL) { 655 warnx("can't reopen %s for reading", tmpfil); 656 break; 657 } 658 bzero((char *)&label, sizeof(label)); 659 c = getasciilabel(fp, &label); 660 fclose(fp); 661 if (c) { 662 lab = label; 663 if (writelabel() == 0) { 664 (void) unlink(tmpfil); 665 return (0); 666 } 667 } 668 printf("re-edit the label? [y]: "); 669 fflush(stdout); 670 c = getchar(); 671 if (c != EOF && c != (int)'\n') 672 while (getchar() != (int)'\n') 673 ; 674 if (c == (int)'n') 675 break; 676 } 677 (void) unlink(tmpfil); 678 return (1); 679 } 680 681 static int 682 editit(void) 683 { 684 int pid, xpid; 685 int locstat, omask; 686 const char *ed; 687 uid_t uid; 688 gid_t gid; 689 690 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP)); 691 while ((pid = fork()) < 0) { 692 if (errno == EPROCLIM) { 693 warnx("you have too many processes"); 694 return(0); 695 } 696 if (errno != EAGAIN) { 697 warn("fork"); 698 return(0); 699 } 700 sleep(1); 701 } 702 if (pid == 0) { 703 sigsetmask(omask); 704 gid = getgid(); 705 if (setresgid(gid, gid, gid) == -1) 706 err(1, "setresgid"); 707 uid = getuid(); 708 if (setresuid(uid, uid, uid) == -1) 709 err(1, "setresuid"); 710 if ((ed = getenv("EDITOR")) == (char *)0) 711 ed = DEFEDITOR; 712 execlp(ed, ed, tmpfil, (char *)0); 713 err(1, "%s", ed); 714 } 715 while ((xpid = wait(&locstat)) >= 0) 716 if (xpid == pid) 717 break; 718 sigsetmask(omask); 719 return(!locstat); 720 } 721 722 static char * 723 skip(char *cp) 724 { 725 726 while (*cp != '\0' && isspace(*cp)) 727 cp++; 728 if (*cp == '\0' || *cp == '#') 729 return (NULL); 730 return (cp); 731 } 732 733 static char * 734 word(char *cp) 735 { 736 char c; 737 738 while (*cp != '\0' && !isspace(*cp) && *cp != '#') 739 cp++; 740 if ((c = *cp) != '\0') { 741 *cp++ = '\0'; 742 if (c != '#') 743 return (skip(cp)); 744 } 745 return (NULL); 746 } 747 748 /* 749 * Read an ascii label in from fd f, 750 * in the same format as that put out by display(), 751 * and fill in lp. 752 */ 753 static int 754 getasciilabel(FILE *f, struct disklabel *lp) 755 { 756 char *cp; 757 const char **cpp; 758 u_int part; 759 char *tp, line[BUFSIZ]; 760 u_long v; 761 int lineno = 0, errors = 0; 762 int i; 763 764 makelabel("auto", lp); 765 bzero(&part_set, sizeof(part_set)); 766 bzero(&part_size_type, sizeof(part_size_type)); 767 bzero(&part_offset_type, sizeof(part_offset_type)); 768 lp->d_bbsize = BBSIZE; /* XXX */ 769 lp->d_sbsize = 0; /* XXX */ 770 while (fgets(line, sizeof(line) - 1, f)) { 771 lineno++; 772 if ((cp = index(line,'\n')) != 0) 773 *cp = '\0'; 774 cp = skip(line); 775 if (cp == NULL) 776 continue; 777 tp = index(cp, ':'); 778 if (tp == NULL) { 779 fprintf(stderr, "line %d: syntax error\n", lineno); 780 errors++; 781 continue; 782 } 783 *tp++ = '\0', tp = skip(tp); 784 if (!strcmp(cp, "type")) { 785 if (tp == NULL) 786 tp = unknown; 787 cpp = dktypenames; 788 for (; cpp < &dktypenames[DKMAXTYPES]; cpp++) 789 if (*cpp && !strcmp(*cpp, tp)) { 790 lp->d_type = cpp - dktypenames; 791 break; 792 } 793 if (cpp < &dktypenames[DKMAXTYPES]) 794 continue; 795 v = strtoul(tp, NULL, 10); 796 if (v >= DKMAXTYPES) 797 fprintf(stderr, "line %d:%s %lu\n", lineno, 798 "Warning, unknown disk type", v); 799 lp->d_type = v; 800 continue; 801 } 802 if (!strcmp(cp, "flags")) { 803 for (v = 0; (cp = tp) && *cp != '\0';) { 804 tp = word(cp); 805 if (!strcmp(cp, "removeable")) 806 v |= D_REMOVABLE; 807 else if (!strcmp(cp, "ecc")) 808 v |= D_ECC; 809 else if (!strcmp(cp, "badsect")) 810 v |= D_BADSECT; 811 else { 812 fprintf(stderr, 813 "line %d: %s: bad flag\n", 814 lineno, cp); 815 errors++; 816 } 817 } 818 lp->d_flags = v; 819 continue; 820 } 821 if (!strcmp(cp, "drivedata")) { 822 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) { 823 lp->d_drivedata[i++] = strtoul(cp, NULL, 10); 824 tp = word(cp); 825 } 826 continue; 827 } 828 if (sscanf(cp, "%lu partitions", &v) == 1) { 829 if (v == 0 || v > MAXPARTITIONS) { 830 fprintf(stderr, 831 "line %d: bad # of partitions\n", lineno); 832 lp->d_npartitions = MAXPARTITIONS; 833 errors++; 834 } else 835 lp->d_npartitions = v; 836 continue; 837 } 838 if (tp == NULL) 839 tp = blank; 840 if (!strcmp(cp, "disk")) { 841 strncpy(lp->d_typename, tp, sizeof (lp->d_typename)); 842 continue; 843 } 844 if (!strcmp(cp, "label")) { 845 strncpy(lp->d_packname, tp, sizeof (lp->d_packname)); 846 continue; 847 } 848 if (!strcmp(cp, "bytes/sector")) { 849 v = strtoul(tp, NULL, 10); 850 if (v == 0 || (v % DEV_BSIZE) != 0) { 851 fprintf(stderr, 852 "line %d: %s: bad sector size\n", 853 lineno, tp); 854 errors++; 855 } else 856 lp->d_secsize = v; 857 continue; 858 } 859 if (!strcmp(cp, "sectors/track")) { 860 v = strtoul(tp, NULL, 10); 861 #if (ULONG_MAX != 0xffffffffUL) 862 if (v == 0 || v > 0xffffffff) 863 #else 864 if (v == 0) 865 #endif 866 { 867 fprintf(stderr, "line %d: %s: bad %s\n", 868 lineno, tp, cp); 869 errors++; 870 } else 871 lp->d_nsectors = v; 872 continue; 873 } 874 if (!strcmp(cp, "sectors/cylinder")) { 875 v = strtoul(tp, NULL, 10); 876 if (v == 0) { 877 fprintf(stderr, "line %d: %s: bad %s\n", 878 lineno, tp, cp); 879 errors++; 880 } else 881 lp->d_secpercyl = v; 882 continue; 883 } 884 if (!strcmp(cp, "tracks/cylinder")) { 885 v = strtoul(tp, NULL, 10); 886 if (v == 0) { 887 fprintf(stderr, "line %d: %s: bad %s\n", 888 lineno, tp, cp); 889 errors++; 890 } else 891 lp->d_ntracks = v; 892 continue; 893 } 894 if (!strcmp(cp, "cylinders")) { 895 v = strtoul(tp, NULL, 10); 896 if (v == 0) { 897 fprintf(stderr, "line %d: %s: bad %s\n", 898 lineno, tp, cp); 899 errors++; 900 } else 901 lp->d_ncylinders = v; 902 continue; 903 } 904 if (!strcmp(cp, "sectors/unit")) { 905 v = strtoul(tp, NULL, 10); 906 if (v == 0) { 907 fprintf(stderr, "line %d: %s: bad %s\n", 908 lineno, tp, cp); 909 errors++; 910 } else 911 lp->d_secperunit = v; 912 continue; 913 } 914 if (!strcmp(cp, "rpm")) { 915 v = strtoul(tp, NULL, 10); 916 if (v == 0 || v > USHRT_MAX) { 917 fprintf(stderr, "line %d: %s: bad %s\n", 918 lineno, tp, cp); 919 errors++; 920 } else 921 lp->d_rpm = v; 922 continue; 923 } 924 if (!strcmp(cp, "interleave")) { 925 v = strtoul(tp, NULL, 10); 926 if (v == 0 || v > USHRT_MAX) { 927 fprintf(stderr, "line %d: %s: bad %s\n", 928 lineno, tp, cp); 929 errors++; 930 } else 931 lp->d_interleave = v; 932 continue; 933 } 934 if (!strcmp(cp, "trackskew")) { 935 v = strtoul(tp, NULL, 10); 936 if (v > USHRT_MAX) { 937 fprintf(stderr, "line %d: %s: bad %s\n", 938 lineno, tp, cp); 939 errors++; 940 } else 941 lp->d_trackskew = v; 942 continue; 943 } 944 if (!strcmp(cp, "cylinderskew")) { 945 v = strtoul(tp, NULL, 10); 946 if (v > USHRT_MAX) { 947 fprintf(stderr, "line %d: %s: bad %s\n", 948 lineno, tp, cp); 949 errors++; 950 } else 951 lp->d_cylskew = v; 952 continue; 953 } 954 if (!strcmp(cp, "headswitch")) { 955 v = strtoul(tp, NULL, 10); 956 lp->d_headswitch = v; 957 continue; 958 } 959 if (!strcmp(cp, "track-to-track seek")) { 960 v = strtoul(tp, NULL, 10); 961 lp->d_trkseek = v; 962 continue; 963 } 964 /* the ':' was removed above */ 965 if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') { 966 fprintf(stderr, 967 "line %d: %s: Unknown disklabel field\n", lineno, 968 cp); 969 errors++; 970 continue; 971 } 972 973 /* Process a partition specification line. */ 974 part = *cp - 'a'; 975 if (part >= lp->d_npartitions) { 976 fprintf(stderr, 977 "line %d: partition name out of range a-%c: %s\n", 978 lineno, 'a' + lp->d_npartitions - 1, cp); 979 errors++; 980 continue; 981 } 982 part_set[part] = 1; 983 984 if (getasciipartspec(tp, lp, part, lineno) != 0) { 985 errors++; 986 break; 987 } 988 } 989 errors += checklabel(lp); 990 return (errors == 0); 991 } 992 993 #define NXTNUM(n) do { \ 994 if (tp == NULL) { \ 995 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \ 996 return (1); \ 997 } else { \ 998 cp = tp, tp = word(cp); \ 999 (n) = strtoul(cp, NULL, 10); \ 1000 } \ 1001 } while (0) 1002 1003 /* retain 1 character following number */ 1004 #define NXTWORD(w,n) do { \ 1005 if (tp == NULL) { \ 1006 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \ 1007 return (1); \ 1008 } else { \ 1009 char *tmp; \ 1010 cp = tp, tp = word(cp); \ 1011 (n) = strtoul(cp, &tmp, 10); \ 1012 if (tmp) (w) = *tmp; \ 1013 } \ 1014 } while (0) 1015 1016 /* 1017 * Read a partition line into partition `part' in the specified disklabel. 1018 * Return 0 on success, 1 on failure. 1019 */ 1020 static int 1021 getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno) 1022 { 1023 struct partition *pp; 1024 char *cp; 1025 const char **cpp; 1026 u_long v; 1027 1028 pp = &lp->d_partitions[part]; 1029 cp = NULL; 1030 1031 v = 0; 1032 NXTWORD(part_size_type[part],v); 1033 if (v == 0 && part_size_type[part] != '*') { 1034 fprintf(stderr, 1035 "line %d: %s: bad partition size\n", lineno, cp); 1036 return (1); 1037 } 1038 pp->p_size = v; 1039 1040 v = 0; 1041 NXTWORD(part_offset_type[part],v); 1042 if (v == 0 && part_offset_type[part] != '*' && 1043 part_offset_type[part] != '\0') { 1044 fprintf(stderr, 1045 "line %d: %s: bad partition offset\n", lineno, cp); 1046 return (1); 1047 } 1048 pp->p_offset = v; 1049 if (tp == NULL) { 1050 fprintf(stderr, "line %d: missing file system type\n", lineno); 1051 return (1); 1052 } 1053 cp = tp, tp = word(cp); 1054 for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++) 1055 if (*cpp && !strcmp(*cpp, cp)) 1056 break; 1057 if (*cpp != NULL) { 1058 pp->p_fstype = cpp - fstypenames; 1059 } else { 1060 if (isdigit(*cp)) 1061 v = strtoul(cp, NULL, 10); 1062 else 1063 v = FSMAXTYPES; 1064 if (v >= FSMAXTYPES) { 1065 fprintf(stderr, 1066 "line %d: Warning, unknown file system type %s\n", 1067 lineno, cp); 1068 v = FS_UNUSED; 1069 } 1070 pp->p_fstype = v; 1071 } 1072 1073 switch (pp->p_fstype) { 1074 case FS_UNUSED: 1075 case FS_BSDFFS: 1076 case FS_BSDLFS: 1077 /* accept defaults for fsize/frag/cpg */ 1078 if (tp) { 1079 NXTNUM(pp->p_fsize); 1080 if (pp->p_fsize == 0) 1081 break; 1082 NXTNUM(v); 1083 pp->p_frag = v / pp->p_fsize; 1084 if (tp != NULL) 1085 NXTNUM(pp->p_cpg); 1086 } 1087 /* else default to 0's */ 1088 break; 1089 default: 1090 break; 1091 } 1092 return (0); 1093 } 1094 1095 /* 1096 * Check disklabel for errors and fill in 1097 * derived fields according to supplied values. 1098 */ 1099 static int 1100 checklabel(struct disklabel *lp) 1101 { 1102 struct partition *pp; 1103 int i, errors = 0; 1104 char part; 1105 u_long base_offset, needed, total_size, total_percent, current_offset; 1106 long free_space; 1107 int seen_default_offset; 1108 int hog_part; 1109 int j; 1110 struct partition *pp2; 1111 1112 if (lp == NULL) 1113 lp = &lab; 1114 1115 if (allfields) { 1116 1117 if (lp->d_secsize == 0) { 1118 fprintf(stderr, "sector size 0\n"); 1119 return (1); 1120 } 1121 if (lp->d_nsectors == 0) { 1122 fprintf(stderr, "sectors/track 0\n"); 1123 return (1); 1124 } 1125 if (lp->d_ntracks == 0) { 1126 fprintf(stderr, "tracks/cylinder 0\n"); 1127 return (1); 1128 } 1129 if (lp->d_ncylinders == 0) { 1130 fprintf(stderr, "cylinders/unit 0\n"); 1131 errors++; 1132 } 1133 if (lp->d_rpm == 0) 1134 warnx("revolutions/minute 0"); 1135 if (lp->d_secpercyl == 0) 1136 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks; 1137 if (lp->d_secperunit == 0) 1138 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders; 1139 if (lp->d_bbsize == 0) { 1140 fprintf(stderr, "boot block size 0\n"); 1141 errors++; 1142 } else if (lp->d_bbsize % lp->d_secsize) 1143 warnx("boot block size %% sector-size != 0"); 1144 if (lp->d_npartitions > MAXPARTITIONS) 1145 warnx("number of partitions (%lu) > MAXPARTITIONS (%d)", 1146 (u_long)lp->d_npartitions, MAXPARTITIONS); 1147 } else { 1148 struct disklabel *vl; 1149 1150 vl = getvirginlabel(); 1151 lp->d_secsize = vl->d_secsize; 1152 lp->d_nsectors = vl->d_nsectors; 1153 lp->d_ntracks = vl->d_ntracks; 1154 lp->d_ncylinders = vl->d_ncylinders; 1155 lp->d_rpm = vl->d_rpm; 1156 lp->d_interleave = vl->d_interleave; 1157 lp->d_secpercyl = vl->d_secpercyl; 1158 lp->d_secperunit = vl->d_secperunit; 1159 lp->d_bbsize = vl->d_bbsize; 1160 lp->d_npartitions = vl->d_npartitions; 1161 } 1162 1163 1164 /* first allocate space to the partitions, then offsets */ 1165 total_size = 0; /* in sectors */ 1166 total_percent = 0; /* in percent */ 1167 hog_part = -1; 1168 /* find all fixed partitions */ 1169 for (i = 0; i < lp->d_npartitions; i++) { 1170 pp = &lp->d_partitions[i]; 1171 if (part_set[i]) { 1172 if (part_size_type[i] == '*') { 1173 if (i == RAW_PART) { 1174 pp->p_size = lp->d_secperunit; 1175 } else { 1176 if (hog_part != -1) 1177 warnx("Too many '*' partitions (%c and %c)", 1178 hog_part + 'a',i + 'a'); 1179 else 1180 hog_part = i; 1181 } 1182 } else { 1183 off_t size; 1184 1185 size = pp->p_size; 1186 switch (part_size_type[i]) { 1187 case '%': 1188 total_percent += size; 1189 break; 1190 case 't': 1191 case 'T': 1192 size *= 1024ULL; 1193 /* FALLTHROUGH */ 1194 case 'g': 1195 case 'G': 1196 size *= 1024ULL; 1197 /* FALLTHROUGH */ 1198 case 'm': 1199 case 'M': 1200 size *= 1024ULL; 1201 /* FALLTHROUGH */ 1202 case 'k': 1203 case 'K': 1204 size *= 1024ULL; 1205 break; 1206 case '\0': 1207 break; 1208 default: 1209 warnx("unknown multiplier suffix '%c' for partition %c (should be K, M, G or T)", 1210 part_size_type[i], i + 'a'); 1211 break; 1212 } 1213 /* don't count %'s yet */ 1214 if (part_size_type[i] != '%') { 1215 /* 1216 * for all not in sectors, convert to 1217 * sectors 1218 */ 1219 if (part_size_type[i] != '\0') { 1220 if (size % lp->d_secsize != 0) 1221 warnx("partition %c not an integer number of sectors", 1222 i + 'a'); 1223 size /= lp->d_secsize; 1224 pp->p_size = size; 1225 } 1226 /* else already in sectors */ 1227 if (i != RAW_PART) 1228 total_size += size; 1229 } 1230 } 1231 } 1232 } 1233 1234 /* Find out the total free space, excluding the boot block area. */ 1235 base_offset = BBSIZE / secsize; 1236 free_space = 0; 1237 for (i = 0; i < lp->d_npartitions; i++) { 1238 pp = &lp->d_partitions[i]; 1239 if (!part_set[i] || i == RAW_PART || 1240 part_size_type[i] == '%' || part_size_type[i] == '*') 1241 continue; 1242 if (pp->p_offset > base_offset) 1243 free_space += pp->p_offset - base_offset; 1244 if (pp->p_offset + pp->p_size > base_offset) 1245 base_offset = pp->p_offset + pp->p_size; 1246 } 1247 if (base_offset < lp->d_secperunit) 1248 free_space += lp->d_secperunit - base_offset; 1249 1250 /* handle % partitions - note %'s don't need to add up to 100! */ 1251 if (total_percent != 0) { 1252 if (total_percent > 100) { 1253 fprintf(stderr,"total percentage %lu is greater than 100\n", 1254 total_percent); 1255 errors++; 1256 } 1257 1258 if (free_space > 0) { 1259 for (i = 0; i < lp->d_npartitions; i++) { 1260 pp = &lp->d_partitions[i]; 1261 if (part_set[i] && part_size_type[i] == '%') { 1262 /* careful of overflows! and integer roundoff */ 1263 pp->p_size = ((double)pp->p_size/100) * free_space; 1264 total_size += pp->p_size; 1265 1266 /* FIX we can lose a sector or so due to roundoff per 1267 partition. A more complex algorithm could avoid that */ 1268 } 1269 } 1270 } else { 1271 fprintf(stderr, 1272 "%ld sectors available to give to '*' and '%%' partitions\n", 1273 free_space); 1274 errors++; 1275 /* fix? set all % partitions to size 0? */ 1276 } 1277 } 1278 /* give anything remaining to the hog partition */ 1279 if (hog_part != -1) { 1280 /* 1281 * Find the range of offsets usable by '*' partitions around 1282 * the hog partition and how much space they need. 1283 */ 1284 needed = 0; 1285 base_offset = BBSIZE / secsize; 1286 for (i = hog_part - 1; i >= 0; i--) { 1287 pp = &lp->d_partitions[i]; 1288 if (!part_set[i] || i == RAW_PART) 1289 continue; 1290 if (part_offset_type[i] == '*') { 1291 needed += pp->p_size; 1292 continue; 1293 } 1294 base_offset = pp->p_offset + pp->p_size; 1295 break; 1296 } 1297 current_offset = lp->d_secperunit; 1298 for (i = lp->d_npartitions - 1; i > hog_part; i--) { 1299 pp = &lp->d_partitions[i]; 1300 if (!part_set[i] || i == RAW_PART) 1301 continue; 1302 if (part_offset_type[i] == '*') { 1303 needed += pp->p_size; 1304 continue; 1305 } 1306 current_offset = pp->p_offset; 1307 } 1308 1309 if (current_offset - base_offset <= needed) { 1310 fprintf(stderr, "Cannot find space for partition %c\n", 1311 hog_part + 'a'); 1312 fprintf(stderr, 1313 "Need more than %lu sectors between %lu and %lu\n", 1314 needed, base_offset, current_offset); 1315 errors++; 1316 lp->d_partitions[hog_part].p_size = 0; 1317 } else { 1318 lp->d_partitions[hog_part].p_size = current_offset - 1319 base_offset - needed; 1320 total_size += lp->d_partitions[hog_part].p_size; 1321 } 1322 } 1323 1324 /* Now set the offsets for each partition */ 1325 current_offset = BBSIZE / secsize; /* in sectors */ 1326 seen_default_offset = 0; 1327 for (i = 0; i < lp->d_npartitions; i++) { 1328 part = 'a' + i; 1329 pp = &lp->d_partitions[i]; 1330 if (part_set[i]) { 1331 if (part_offset_type[i] == '*') { 1332 if (i == RAW_PART) { 1333 pp->p_offset = 0; 1334 } else { 1335 pp->p_offset = current_offset; 1336 seen_default_offset = 1; 1337 } 1338 } else { 1339 /* allow them to be out of order for old-style tables */ 1340 if (pp->p_offset < current_offset && 1341 seen_default_offset && i != RAW_PART && 1342 pp->p_fstype != FS_VINUM) { 1343 fprintf(stderr, 1344 "Offset %ld for partition %c overlaps previous partition which ends at %lu\n", 1345 (long)pp->p_offset,i+'a',current_offset); 1346 fprintf(stderr, 1347 "Labels with any *'s for offset must be in ascending order by sector\n"); 1348 errors++; 1349 } else if (pp->p_offset != current_offset && 1350 i != RAW_PART && seen_default_offset) { 1351 /* 1352 * this may give unneeded warnings if 1353 * partitions are out-of-order 1354 */ 1355 warnx( 1356 "Offset %ld for partition %c doesn't match expected value %ld", 1357 (long)pp->p_offset, i + 'a', current_offset); 1358 } 1359 } 1360 if (i != RAW_PART) 1361 current_offset = pp->p_offset + pp->p_size; 1362 } 1363 } 1364 1365 for (i = 0; i < lp->d_npartitions; i++) { 1366 part = 'a' + i; 1367 pp = &lp->d_partitions[i]; 1368 if (pp->p_size == 0 && pp->p_offset != 0) 1369 warnx("partition %c: size 0, but offset %lu", 1370 part, (u_long)pp->p_offset); 1371 #ifdef notdef 1372 if (pp->p_size % lp->d_secpercyl) 1373 warnx("partition %c: size %% cylinder-size != 0", 1374 part); 1375 if (pp->p_offset % lp->d_secpercyl) 1376 warnx("partition %c: offset %% cylinder-size != 0", 1377 part); 1378 #endif 1379 if (pp->p_offset > lp->d_secperunit) { 1380 fprintf(stderr, 1381 "partition %c: offset past end of unit\n", part); 1382 errors++; 1383 } 1384 if (pp->p_offset + pp->p_size > lp->d_secperunit) { 1385 fprintf(stderr, 1386 "partition %c: partition extends past end of unit\n", 1387 part); 1388 errors++; 1389 } 1390 if (i == RAW_PART) { 1391 if (pp->p_fstype != FS_UNUSED) 1392 warnx("partition %c is not marked as unused!",part); 1393 if (pp->p_offset != 0) 1394 warnx("partition %c doesn't start at 0!",part); 1395 if (pp->p_size != lp->d_secperunit) 1396 warnx("partition %c doesn't cover the whole unit!",part); 1397 1398 if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) || 1399 (pp->p_size != lp->d_secperunit)) { 1400 warnx("An incorrect partition %c may cause problems for " 1401 "standard system utilities",part); 1402 } 1403 } 1404 1405 /* check for overlaps */ 1406 /* this will check for all possible overlaps once and only once */ 1407 for (j = 0; j < i; j++) { 1408 pp2 = &lp->d_partitions[j]; 1409 if (j != RAW_PART && i != RAW_PART && 1410 pp->p_fstype != FS_VINUM && 1411 pp2->p_fstype != FS_VINUM && 1412 part_set[i] && part_set[j]) { 1413 if (pp2->p_offset < pp->p_offset + pp->p_size && 1414 (pp2->p_offset + pp2->p_size > pp->p_offset || 1415 pp2->p_offset >= pp->p_offset)) { 1416 fprintf(stderr,"partitions %c and %c overlap!\n", 1417 j + 'a', i + 'a'); 1418 errors++; 1419 } 1420 } 1421 } 1422 } 1423 for (; i < lp->d_npartitions; i++) { 1424 part = 'a' + i; 1425 pp = &lp->d_partitions[i]; 1426 if (pp->p_size || pp->p_offset) 1427 warnx("unused partition %c: size %d offset %lu", 1428 'a' + i, pp->p_size, (u_long)pp->p_offset); 1429 } 1430 return (errors); 1431 } 1432 1433 /* 1434 * When operating on a "virgin" disk, try getting an initial label 1435 * from the associated device driver. This might work for all device 1436 * drivers that are able to fetch some initial device parameters 1437 * without even having access to a (BSD) disklabel, like SCSI disks, 1438 * most IDE drives, or vn devices. 1439 * 1440 * The device name must be given in its "canonical" form. 1441 */ 1442 static struct disklabel * 1443 getvirginlabel(void) 1444 { 1445 static struct disklabel loclab; 1446 struct partition *dp; 1447 int f; 1448 u_int u; 1449 1450 if ((f = open(specname, O_RDONLY)) == -1) { 1451 warn("cannot open %s", specname); 1452 return (NULL); 1453 } 1454 1455 if (is_file) 1456 get_file_parms(f); 1457 else { 1458 mediasize = g_mediasize(f); 1459 secsize = g_sectorsize(f); 1460 if (secsize < 0 || mediasize < 0) { 1461 close (f); 1462 return (NULL); 1463 } 1464 } 1465 memset(&loclab, 0, sizeof loclab); 1466 loclab.d_magic = DISKMAGIC; 1467 loclab.d_magic2 = DISKMAGIC; 1468 loclab.d_secsize = secsize; 1469 loclab.d_secperunit = mediasize / secsize; 1470 1471 /* 1472 * Nobody in these enligthened days uses the CHS geometry for 1473 * anything, but nontheless try to get it right. If we fail 1474 * to get any good ideas from the device, construct something 1475 * which is IBM-PC friendly. 1476 */ 1477 if (ioctl(f, DIOCGFWSECTORS, &u) == 0) 1478 loclab.d_nsectors = u; 1479 else 1480 loclab.d_nsectors = 63; 1481 if (ioctl(f, DIOCGFWHEADS, &u) == 0) 1482 loclab.d_ntracks = u; 1483 else if (loclab.d_secperunit <= 63*1*1024) 1484 loclab.d_ntracks = 1; 1485 else if (loclab.d_secperunit <= 63*16*1024) 1486 loclab.d_ntracks = 16; 1487 else 1488 loclab.d_ntracks = 255; 1489 loclab.d_secpercyl = loclab.d_ntracks * loclab.d_nsectors; 1490 loclab.d_ncylinders = loclab.d_secperunit / loclab.d_secpercyl; 1491 loclab.d_npartitions = DEFPARTITIONS; 1492 1493 /* Various (unneeded) compat stuff */ 1494 loclab.d_rpm = 3600; 1495 loclab.d_bbsize = BBSIZE; 1496 loclab.d_interleave = 1; 1497 strncpy(loclab.d_typename, "amnesiac", 1498 sizeof(loclab.d_typename)); 1499 1500 dp = &loclab.d_partitions[RAW_PART]; 1501 dp->p_size = loclab.d_secperunit; 1502 loclab.d_checksum = dkcksum(&loclab); 1503 close (f); 1504 return (&loclab); 1505 } 1506 1507 static void 1508 usage(void) 1509 { 1510 1511 fprintf(stderr, 1512 "%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", 1513 "usage: bsdlabel disk", 1514 "\t\t(to read label)", 1515 " bsdlabel -w [-n] [-m machine] disk [type]", 1516 "\t\t(to write label with existing boot program)", 1517 " bsdlabel -e [-n] [-m machine] disk", 1518 "\t\t(to edit label)", 1519 " bsdlabel -R [-n] [-m machine] disk protofile", 1520 "\t\t(to restore label with existing boot program)", 1521 " bsdlabel -B [-b boot] [-m machine] disk", 1522 "\t\t(to install boot program with existing on-disk label)", 1523 " bsdlabel -w -B [-n] [-b boot] [-m machine] disk [type]", 1524 "\t\t(to write label and install boot program)", 1525 " bsdlabel -R -B [-n] [-b boot] [-m machine] disk protofile", 1526 "\t\t(to restore label and install boot program)" 1527 ); 1528 exit(1); 1529 } 1530