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