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