bsdlabel.c revision 109884
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#ifndef lint 44static const char copyright[] = 45"@(#) Copyright (c) 1987, 1993\n\ 46 The Regents of the University of California. All rights reserved.\n"; 47#endif /* not lint */ 48 49#ifndef lint 50#if 0 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 54#endif /* not lint */ 55 56#include <sys/cdefs.h> 57__FBSDID("$FreeBSD: head/sbin/bsdlabel/bsdlabel.c 109884 2003-01-26 12:39:46Z phk $"); 58 59#include <sys/param.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#include <sys/disklabel.h> 67#ifdef PC98 68#include <sys/diskpc98.h> 69#else 70#include <sys/diskmbr.h> 71#endif 72 73#include <unistd.h> 74#include <string.h> 75#include <stdio.h> 76#include <stdlib.h> 77#include <signal.h> 78#include <stdarg.h> 79#include <ctype.h> 80#include <err.h> 81#include <errno.h> 82 83#include "pathnames.h" 84 85/* 86 * Disklabel: read and write disklabels. 87 * The label is usually placed on one of the first sectors of the disk. 88 * Many machines also place a bootstrap in the same area, 89 * in which case the label is embedded in the bootstrap. 90 * The bootstrap source must leave space at the proper offset 91 * for the label on such machines. 92 */ 93 94#ifndef BBSIZE 95#define BBSIZE 8192 /* size of boot area, with label */ 96#endif 97 98/* FIX! These are too low, but are traditional */ 99#define DEFAULT_NEWFS_BLOCK 8192U 100#define DEFAULT_NEWFS_FRAG 1024U 101#define DEFAULT_NEWFS_CPG 16U 102 103#define BIG_NEWFS_BLOCK 16384U 104#define BIG_NEWFS_FRAG 2048U 105#define BIG_NEWFS_CPG 64U 106 107#if defined(__i386__) 108#define NUMBOOT 2 109#elif defined(__alpha__) 110#define NUMBOOT 1 111#else 112#error I do not know about this architecture, and shall probably not be compiled for it. 113#endif 114 115void makelabel(const char *, const char *, struct disklabel *); 116int writelabel(int, const char *, struct disklabel *); 117void l_perror(const char *); 118struct disklabel *readlabel(int); 119struct disklabel *makebootarea(char *, struct disklabel *, int); 120void display(FILE *, const struct disklabel *); 121int edit(struct disklabel *, int); 122int editit(void); 123char *skip(char *); 124char *word(char *); 125int getasciilabel(FILE *, struct disklabel *); 126int getasciipartspec(char *, struct disklabel *, int, int); 127int checklabel(struct disklabel *); 128void setbootflag(struct disklabel *); 129void Warning(const char *, ...) __printflike(1, 2); 130void usage(void); 131struct disklabel *getvirginlabel(void); 132 133#define DEFEDITOR _PATH_VI 134#define streq(a,b) (strcmp(a,b) == 0) 135 136char *dkname; 137char *specname; 138char tmpfil[] = PATH_TMPFILE; 139 140char namebuf[BBSIZE], *np = namebuf; 141struct disklabel lab; 142char bootarea[BBSIZE]; 143char blank[] = ""; 144char unknown[] = "unknown"; 145 146#define MAX_PART ('z') 147#define MAX_NUM_PARTS (1 + MAX_PART - 'a') 148char part_size_type[MAX_NUM_PARTS]; 149char part_offset_type[MAX_NUM_PARTS]; 150int part_set[MAX_NUM_PARTS]; 151 152#if NUMBOOT > 0 153int installboot; /* non-zero if we should install a boot program */ 154char *bootbuf; /* pointer to buffer with remainder of boot prog */ 155int bootsize; /* size of remaining boot program */ 156char *xxboot; /* primary boot */ 157char *bootxx; /* secondary boot */ 158char boot0[MAXPATHLEN]; 159char boot1[MAXPATHLEN]; 160#endif 161 162enum { 163 UNSPEC, EDIT, READ, RESTORE, WRITE, WRITEBOOT 164} op = UNSPEC; 165 166int rflag; 167int disable_write; /* set to disable writing to disk label */ 168 169#define OPTIONS "BRb:enrs:w" 170 171int 172main(int argc, char *argv[]) 173{ 174 struct disklabel *lp; 175 FILE *t; 176 int ch, f = 0, error = 0; 177 char *name = 0; 178 179 while ((ch = getopt(argc, argv, OPTIONS)) != -1) 180 switch (ch) { 181#if NUMBOOT > 0 182 case 'B': 183 ++installboot; 184 break; 185 case 'b': 186 xxboot = optarg; 187 break; 188#if NUMBOOT > 1 189 case 's': 190 bootxx = optarg; 191 break; 192#endif 193#endif 194 case 'n': 195 disable_write = 1; 196 break; 197 case 'R': 198 if (op != UNSPEC) 199 usage(); 200 op = RESTORE; 201 break; 202 case 'e': 203 if (op != UNSPEC) 204 usage(); 205 op = EDIT; 206 break; 207 case 'r': 208 ++rflag; 209 break; 210 case 'w': 211 if (op != UNSPEC) 212 usage(); 213 op = WRITE; 214 break; 215 case '?': 216 default: 217 usage(); 218 } 219 argc -= optind; 220 argv += optind; 221#if NUMBOOT > 0 222 if (installboot) { 223 rflag++; 224 if (op == UNSPEC) 225 op = WRITEBOOT; 226 } else { 227 if (op == UNSPEC) 228 op = READ; 229 xxboot = bootxx = 0; 230 } 231#else 232 if (op == UNSPEC) 233 op = READ; 234#endif 235 if (argc < 1) 236 usage(); 237 238 dkname = argv[0]; 239 if (dkname[0] != '/') { 240 (void)sprintf(np, "%s%s%c", _PATH_DEV, dkname, 'a' + RAW_PART); 241 specname = np; 242 np += strlen(specname) + 1; 243 } else 244 specname = dkname; 245 f = open(specname, op == READ ? O_RDONLY : O_RDWR); 246 if (f < 0 && errno == ENOENT && dkname[0] != '/') { 247 (void)sprintf(specname, "%s%s", _PATH_DEV, dkname); 248 np = namebuf + strlen(specname) + 1; 249 f = open(specname, op == READ ? O_RDONLY : O_RDWR); 250 } 251 if (f < 0) 252 err(4, "%s", specname); 253 254 switch(op) { 255 256 case UNSPEC: 257 break; 258 259 case EDIT: 260 if (argc != 1) 261 usage(); 262 lp = readlabel(f); 263 error = edit(lp, f); 264 break; 265 266 case READ: 267 if (argc != 1) 268 usage(); 269 lp = readlabel(f); 270 display(stdout, lp); 271 error = checklabel(lp); 272 break; 273 274 case RESTORE: 275#if NUMBOOT > 0 276 if (installboot && argc == 3) { 277 makelabel(argv[2], 0, &lab); 278 argc--; 279 280 /* 281 * We only called makelabel() for its side effect 282 * of setting the bootstrap file names. Discard 283 * all changes to `lab' so that all values in the 284 * final label come from the ASCII label. 285 */ 286 bzero((char *)&lab, sizeof(lab)); 287 } 288#endif 289 if (argc != 2) 290 usage(); 291 if (!(t = fopen(argv[1], "r"))) 292 err(4, "%s", argv[1]); 293 if (!getasciilabel(t, &lab)) 294 exit(1); 295 lp = makebootarea(bootarea, &lab, f); 296 *lp = lab; 297 error = writelabel(f, bootarea, lp); 298 break; 299 300 case WRITE: 301 if (argc == 3) { 302 name = argv[2]; 303 argc--; 304 } 305 if (argc != 2) 306 usage(); 307 makelabel(argv[1], name, &lab); 308 lp = makebootarea(bootarea, &lab, f); 309 *lp = lab; 310 if (checklabel(lp) == 0) 311 error = writelabel(f, bootarea, lp); 312 break; 313 314#if NUMBOOT > 0 315 case WRITEBOOT: 316 { 317 struct disklabel tlab; 318 319 lp = readlabel(f); 320 tlab = *lp; 321 if (argc == 2) 322 makelabel(argv[1], 0, &lab); 323 lp = makebootarea(bootarea, &lab, f); 324 *lp = tlab; 325 if (checklabel(lp) == 0) 326 error = writelabel(f, bootarea, lp); 327 break; 328 } 329#endif 330 } 331 exit(error); 332} 333 334/* 335 * Construct a prototype disklabel from /etc/disktab. As a side 336 * effect, set the names of the primary and secondary boot files 337 * if specified. 338 */ 339void 340makelabel(const char *type, const char *name, struct disklabel *lp) 341{ 342 struct disklabel *dp; 343 344 if (strcmp(type, "auto") == 0) 345 dp = getvirginlabel(); 346 else 347 dp = getdiskbyname(type); 348 if (dp == NULL) 349 errx(1, "%s: unknown disk type", type); 350 *lp = *dp; 351 bzero(lp->d_packname, sizeof(lp->d_packname)); 352 if (name) 353 (void)strncpy(lp->d_packname, name, sizeof(lp->d_packname)); 354} 355 356int 357writelabel(int f, const char *boot, struct disklabel *lp) 358{ 359#ifdef __alpha__ 360 u_long *p, sum; 361 int i; 362#endif 363 364 if (disable_write) { 365 Warning("write to disk label supressed - label was as follows:"); 366 display(stdout, lp); 367 return (0); 368 } 369 370 setbootflag(lp); 371 lp->d_magic = DISKMAGIC; 372 lp->d_magic2 = DISKMAGIC; 373 lp->d_checksum = 0; 374 lp->d_checksum = dkcksum(lp); 375 if (!rflag) { 376 if (ioctl(f, DIOCWDINFO, lp) < 0) { 377 l_perror("ioctl DIOCWDINFO"); 378 return (1); 379 } 380 return (0); 381 } 382 383 /* 384 * First set the kernel disk label, 385 * then write a label to the raw disk. 386 * If the SDINFO ioctl fails because it is unimplemented, 387 * keep going; otherwise, the kernel consistency checks 388 * may prevent us from changing the current (in-core) 389 * label. 390 */ 391 if (ioctl(f, DIOCSDINFO, lp) < 0 && 392 errno != ENODEV && errno != ENOTTY) { 393 l_perror("ioctl DIOCSDINFO"); 394 return (1); 395 } 396 (void)lseek(f, (off_t)0, SEEK_SET); 397 398#ifdef __alpha__ 399 /* 400 * Generate the bootblock checksum for the SRM console. 401 */ 402 for (p = (u_long *)boot, i = 0, sum = 0; i < 63; i++) 403 sum += p[i]; 404 p[63] = sum; 405#endif 406 if (write(f, boot, lp->d_bbsize) != (int)lp->d_bbsize) { 407 warn("write"); 408 return (1); 409 } 410#if NUMBOOT > 0 411 /* 412 * Output the remainder of the disklabel 413 */ 414 if (bootbuf && write(f, bootbuf, bootsize) != bootsize) { 415 warn("write"); 416 return(1); 417 } 418#endif 419 return (0); 420} 421 422void 423l_perror(const char *s) 424{ 425 switch (errno) { 426 427 case ESRCH: 428 warnx("%s: no disk label on disk;", s); 429 fprintf(stderr, "add \"-r\" to install initial label\n"); 430 break; 431 432 case EINVAL: 433 warnx("%s: label magic number or checksum is wrong!", s); 434 fprintf(stderr, "(disklabel or kernel is out of date?)\n"); 435 break; 436 437 case EBUSY: 438 warnx("%s: open partition would move or shrink", s); 439 break; 440 441 case EXDEV: 442 warnx("%s: '%c' partition must start at beginning of disk", 443 s, 'a' + RAW_PART); 444 break; 445 446 default: 447 warn((char *)NULL); 448 break; 449 } 450} 451 452/* 453 * Fetch disklabel for disk. 454 * Use ioctl to get label unless -r flag is given. 455 */ 456struct disklabel * 457readlabel(int f) 458{ 459 struct disklabel *lp; 460 461 if (rflag) { 462 if (read(f, bootarea, BBSIZE) < BBSIZE) 463 err(4, "%s", specname); 464 for (lp = (struct disklabel *)bootarea; 465 lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp)); 466 lp = (struct disklabel *)((char *)lp + 16)) 467 if (lp->d_magic == DISKMAGIC && 468 lp->d_magic2 == DISKMAGIC) 469 break; 470 if (lp > (struct disklabel *)(bootarea+BBSIZE-sizeof(*lp)) || 471 lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC || 472 dkcksum(lp) != 0) 473 errx(1, 474 "bad pack magic number (label is damaged, or pack is unlabeled)"); 475 } else { 476 lp = &lab; 477 if (ioctl(f, DIOCGDINFO, lp) < 0) 478 err(4, "ioctl DIOCGDINFO"); 479 } 480 return (lp); 481} 482 483/* 484 * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot'' 485 * Returns a pointer to the disklabel portion of the bootarea. 486 */ 487struct disklabel * 488makebootarea(char *boot, struct disklabel *dp, int f) 489{ 490 struct disklabel *lp; 491 char *p; 492 int b; 493#if NUMBOOT > 0 494 char *dkbasename; 495 struct stat sb; 496#endif 497#ifdef __alpha__ 498 u_long *bootinfo; 499 int n; 500#endif 501#ifdef __i386__ 502 char *tmpbuf; 503 int i, found; 504#endif 505 506 /* XXX */ 507 if (dp->d_secsize == 0) { 508 dp->d_secsize = DEV_BSIZE; 509 dp->d_bbsize = BBSIZE; 510 } 511 lp = (struct disklabel *) 512 (boot + (LABELSECTOR * dp->d_secsize) + LABELOFFSET); 513 bzero((char *)lp, sizeof *lp); 514#if NUMBOOT > 0 515 /* 516 * If we are not installing a boot program but we are installing a 517 * label on disk then we must read the current bootarea so we don't 518 * clobber the existing boot. 519 */ 520 if (!installboot) { 521 if (rflag) { 522 if (read(f, boot, BBSIZE) < BBSIZE) 523 err(4, "%s", specname); 524 bzero((char *)lp, sizeof *lp); 525 } 526 return (lp); 527 } 528 /* 529 * We are installing a boot program. Determine the name(s) and 530 * read them into the appropriate places in the boot area. 531 */ 532 if (!xxboot || !bootxx) { 533 dkbasename = np; 534 if ((p = rindex(dkname, '/')) == NULL) 535 p = dkname; 536 else 537 p++; 538 while (*p && !isdigit(*p)) 539 *np++ = *p++; 540 *np++ = '\0'; 541 542 if (!xxboot) { 543 (void)sprintf(boot0, "%s/boot1", _PATH_BOOTDIR); 544 xxboot = boot0; 545 } 546#if NUMBOOT > 1 547 if (!bootxx) { 548 (void)sprintf(boot1, "%s/boot2", _PATH_BOOTDIR); 549 bootxx = boot1; 550 } 551#endif 552 } 553 554 /* 555 * Strange rules: 556 * 1. One-piece bootstrap (hp300/hp800) 557 * 1. One-piece bootstrap (alpha) 558 * up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest 559 * is remembered and written later following the bootarea. 560 * 2. Two-piece bootstraps (i386) 561 * up to d_secsize bytes of ``xxboot'' go in first d_secsize 562 * bytes of bootarea, remaining d_bbsize-d_secsize filled 563 * from ``bootxx''. 564 */ 565 b = open(xxboot, O_RDONLY); 566 if (b < 0) 567 err(4, "%s", xxboot); 568#if NUMBOOT > 1 569#ifdef __i386__ 570 /* 571 * XXX Botch alert. 572 * The i386 has the so-called fdisk table embedded into the 573 * primary bootstrap. We take care to not clobber it, but 574 * only if it does already contain some data. (Otherwise, 575 * the xxboot provides a template.) 576 */ 577 if ((tmpbuf = (char *)malloc((int)dp->d_secsize)) == 0) 578 err(4, "%s", xxboot); 579 memcpy((void *)tmpbuf, (void *)boot, (int)dp->d_secsize); 580#endif /* __i386__ */ 581 if (read(b, boot, (int)dp->d_secsize) < 0) 582 err(4, "%s", xxboot); 583 (void)close(b); 584#ifdef PC98 585 for (i = DOSPARTOFF, found = 0; 586 !found && i < (int)(DOSPARTOFF + NDOSPART * sizeof(struct pc98_partition)); 587 i++) 588 found = tmpbuf[i] != 0; 589 if (found) 590 memcpy((void *)&boot[DOSPARTOFF], 591 (void *)&tmpbuf[DOSPARTOFF], 592 NDOSPART * sizeof(struct pc98_partition)); 593 free(tmpbuf); 594#elif defined(__i386__) 595 for (i = DOSPARTOFF, found = 0; 596 !found && i < (int)(DOSPARTOFF + NDOSPART*sizeof(struct dos_partition)); 597 i++) 598 found = tmpbuf[i] != 0; 599 if (found) 600 memcpy((void *)&boot[DOSPARTOFF], 601 (void *)&tmpbuf[DOSPARTOFF], 602 NDOSPART * sizeof(struct dos_partition)); 603 free(tmpbuf); 604#endif /* __i386__ */ 605 b = open(bootxx, O_RDONLY); 606 if (b < 0) 607 err(4, "%s", bootxx); 608 if (fstat(b, &sb) != 0) 609 err(4, "%s", bootxx); 610 if (dp->d_secsize + sb.st_size > dp->d_bbsize) 611 errx(4, "%s too large", bootxx); 612 if (read(b, &boot[dp->d_secsize], 613 (int)(dp->d_bbsize-dp->d_secsize)) < 0) 614 err(4, "%s", bootxx); 615#else /* !(NUMBOOT > 1) */ 616#ifdef __alpha__ 617 /* 618 * On the alpha, the primary bootstrap starts at the 619 * second sector of the boot area. The first sector 620 * contains the label and must be edited to contain the 621 * size and location of the primary bootstrap. 622 */ 623 n = read(b, boot + dp->d_secsize, (int)dp->d_bbsize); 624 if (n < 0) 625 err(4, "%s", xxboot); 626 bootinfo = (u_long *)(boot + 480); 627 bootinfo[0] = (n + dp->d_secsize - 1) / dp->d_secsize; 628 bootinfo[1] = 1; /* start at sector 1 */ 629 bootinfo[2] = 0; /* flags (must be zero) */ 630#else /* !__alpha__ */ 631 if (read(b, boot, (int)dp->d_bbsize) < 0) 632 err(4, "%s", xxboot); 633#endif /* __alpha__ */ 634 if (fstat(b, &sb) != 0) 635 err(4, "%s", xxboot); 636 bootsize = (int)sb.st_size - dp->d_bbsize; 637 if (bootsize > 0) { 638 /* XXX assume d_secsize is a power of two */ 639 bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1); 640 bootbuf = (char *)malloc((size_t)bootsize); 641 if (bootbuf == 0) 642 err(4, "%s", xxboot); 643 if (read(b, bootbuf, bootsize) < 0) { 644 free(bootbuf); 645 err(4, "%s", xxboot); 646 } 647 } 648#endif /* NUMBOOT > 1 */ 649 (void)close(b); 650#endif /* NUMBOOT > 0 */ 651 /* 652 * Make sure no part of the bootstrap is written in the area 653 * reserved for the label. 654 */ 655 for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++) 656 if (*p) 657 errx(2, "bootstrap doesn't leave room for disk label"); 658 return (lp); 659} 660 661void 662display(FILE *f, const struct disklabel *lp) 663{ 664 int i, j; 665 const struct partition *pp; 666 667 fprintf(f, "# %s:\n", specname); 668 if (lp->d_type < DKMAXTYPES) 669 fprintf(f, "type: %s\n", dktypenames[lp->d_type]); 670 else 671 fprintf(f, "type: %u\n", lp->d_type); 672 fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename), 673 lp->d_typename); 674 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname), 675 lp->d_packname); 676 fprintf(f, "flags:"); 677 if (lp->d_flags & D_REMOVABLE) 678 fprintf(f, " removeable"); 679 if (lp->d_flags & D_ECC) 680 fprintf(f, " ecc"); 681 if (lp->d_flags & D_BADSECT) 682 fprintf(f, " badsect"); 683 fprintf(f, "\n"); 684 fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize); 685 fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors); 686 fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks); 687 fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl); 688 fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders); 689 fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit); 690 fprintf(f, "rpm: %u\n", lp->d_rpm); 691 fprintf(f, "interleave: %u\n", lp->d_interleave); 692 fprintf(f, "trackskew: %u\n", lp->d_trackskew); 693 fprintf(f, "cylinderskew: %u\n", lp->d_cylskew); 694 fprintf(f, "headswitch: %lu\t\t# milliseconds\n", 695 (u_long)lp->d_headswitch); 696 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n", 697 (u_long)lp->d_trkseek); 698 fprintf(f, "drivedata: "); 699 for (i = NDDATA - 1; i >= 0; i--) 700 if (lp->d_drivedata[i]) 701 break; 702 if (i < 0) 703 i = 0; 704 for (j = 0; j <= i; j++) 705 fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]); 706 fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions); 707 fprintf(f, 708 "# size offset fstype [fsize bsize bps/cpg]\n"); 709 pp = lp->d_partitions; 710 for (i = 0; i < lp->d_npartitions; i++, pp++) { 711 if (pp->p_size) { 712 fprintf(f, " %c: %8lu %8lu ", 'a' + i, 713 (u_long)pp->p_size, (u_long)pp->p_offset); 714 if (pp->p_fstype < FSMAXTYPES) 715 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]); 716 else 717 fprintf(f, "%8d", pp->p_fstype); 718 switch (pp->p_fstype) { 719 720 case FS_UNUSED: /* XXX */ 721 fprintf(f, " %5lu %5lu %5.5s ", 722 (u_long)pp->p_fsize, 723 (u_long)(pp->p_fsize * pp->p_frag), ""); 724 break; 725 726 case FS_BSDFFS: 727 fprintf(f, " %5lu %5lu %5u ", 728 (u_long)pp->p_fsize, 729 (u_long)(pp->p_fsize * pp->p_frag), 730 pp->p_cpg); 731 break; 732 733 case FS_BSDLFS: 734 fprintf(f, " %5lu %5lu %5d", 735 (u_long)pp->p_fsize, 736 (u_long)(pp->p_fsize * pp->p_frag), 737 pp->p_cpg); 738 break; 739 740 default: 741 fprintf(f, "%20.20s", ""); 742 break; 743 } 744 fprintf(f, "\t# (Cyl. %4lu", 745 (u_long)(pp->p_offset / lp->d_secpercyl)); 746 if (pp->p_offset % lp->d_secpercyl) 747 putc('*', f); 748 else 749 putc(' ', f); 750 fprintf(f, "- %lu", 751 (u_long)((pp->p_offset + pp->p_size + 752 lp->d_secpercyl - 1) / 753 lp->d_secpercyl - 1)); 754 if (pp->p_size % lp->d_secpercyl) 755 putc('*', f); 756 fprintf(f, ")\n"); 757 } 758 } 759 fflush(f); 760} 761 762int 763edit(struct disklabel *lp, int f) 764{ 765 int c, fd; 766 struct disklabel label; 767 FILE *fp; 768 769 if ((fd = mkstemp(tmpfil)) == -1 || 770 (fp = fdopen(fd, "w")) == NULL) { 771 warnx("can't create %s", tmpfil); 772 return (1); 773 } 774 display(fp, lp); 775 fclose(fp); 776 for (;;) { 777 if (!editit()) 778 break; 779 fp = fopen(tmpfil, "r"); 780 if (fp == NULL) { 781 warnx("can't reopen %s for reading", tmpfil); 782 break; 783 } 784 bzero((char *)&label, sizeof(label)); 785 if (getasciilabel(fp, &label)) { 786 *lp = label; 787 if (writelabel(f, bootarea, lp) == 0) { 788 fclose(fp); 789 (void) unlink(tmpfil); 790 return (0); 791 } 792 } 793 fclose(fp); 794 printf("re-edit the label? [y]: "); fflush(stdout); 795 c = getchar(); 796 if (c != EOF && c != (int)'\n') 797 while (getchar() != (int)'\n') 798 ; 799 if (c == (int)'n') 800 break; 801 } 802 (void) unlink(tmpfil); 803 return (1); 804} 805 806int 807editit(void) 808{ 809 int pid, xpid; 810 int locstat, omask; 811 const char *ed; 812 813 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP)); 814 while ((pid = fork()) < 0) { 815 if (errno == EPROCLIM) { 816 warnx("you have too many processes"); 817 return(0); 818 } 819 if (errno != EAGAIN) { 820 warn("fork"); 821 return(0); 822 } 823 sleep(1); 824 } 825 if (pid == 0) { 826 sigsetmask(omask); 827 setgid(getgid()); 828 setuid(getuid()); 829 if ((ed = getenv("EDITOR")) == (char *)0) 830 ed = DEFEDITOR; 831 execlp(ed, ed, tmpfil, (char *)0); 832 err(1, "%s", ed); 833 } 834 while ((xpid = wait(&locstat)) >= 0) 835 if (xpid == pid) 836 break; 837 sigsetmask(omask); 838 return(!locstat); 839} 840 841char * 842skip(char *cp) 843{ 844 845 while (*cp != '\0' && isspace(*cp)) 846 cp++; 847 if (*cp == '\0' || *cp == '#') 848 return (NULL); 849 return (cp); 850} 851 852char * 853word(char *cp) 854{ 855 char c; 856 857 while (*cp != '\0' && !isspace(*cp) && *cp != '#') 858 cp++; 859 if ((c = *cp) != '\0') { 860 *cp++ = '\0'; 861 if (c != '#') 862 return (skip(cp)); 863 } 864 return (NULL); 865} 866 867/* 868 * Read an ascii label in from fd f, 869 * in the same format as that put out by display(), 870 * and fill in lp. 871 */ 872int 873getasciilabel(FILE *f, struct disklabel *lp) 874{ 875 char *cp; 876 const char **cpp; 877 u_int part; 878 char *tp, line[BUFSIZ]; 879 u_long v; 880 int lineno = 0, errors = 0; 881 int i; 882 883 bzero(&part_set, sizeof(part_set)); 884 bzero(&part_size_type, sizeof(part_size_type)); 885 bzero(&part_offset_type, sizeof(part_offset_type)); 886 lp->d_bbsize = BBSIZE; /* XXX */ 887 lp->d_sbsize = 0; /* XXX */ 888 while (fgets(line, sizeof(line) - 1, f)) { 889 lineno++; 890 if ((cp = index(line,'\n')) != 0) 891 *cp = '\0'; 892 cp = skip(line); 893 if (cp == NULL) 894 continue; 895 tp = index(cp, ':'); 896 if (tp == NULL) { 897 fprintf(stderr, "line %d: syntax error\n", lineno); 898 errors++; 899 continue; 900 } 901 *tp++ = '\0', tp = skip(tp); 902 if (streq(cp, "type")) { 903 if (tp == NULL) 904 tp = unknown; 905 cpp = dktypenames; 906 for (; cpp < &dktypenames[DKMAXTYPES]; cpp++) 907 if (*cpp && streq(*cpp, tp)) { 908 lp->d_type = cpp - dktypenames; 909 break; 910 } 911 if (cpp < &dktypenames[DKMAXTYPES]) 912 continue; 913 v = strtoul(tp, NULL, 10); 914 if (v >= DKMAXTYPES) 915 fprintf(stderr, "line %d:%s %lu\n", lineno, 916 "Warning, unknown disk type", v); 917 lp->d_type = v; 918 continue; 919 } 920 if (streq(cp, "flags")) { 921 for (v = 0; (cp = tp) && *cp != '\0';) { 922 tp = word(cp); 923 if (streq(cp, "removeable")) 924 v |= D_REMOVABLE; 925 else if (streq(cp, "ecc")) 926 v |= D_ECC; 927 else if (streq(cp, "badsect")) 928 v |= D_BADSECT; 929 else { 930 fprintf(stderr, 931 "line %d: %s: bad flag\n", 932 lineno, cp); 933 errors++; 934 } 935 } 936 lp->d_flags = v; 937 continue; 938 } 939 if (streq(cp, "drivedata")) { 940 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) { 941 lp->d_drivedata[i++] = strtoul(cp, NULL, 10); 942 tp = word(cp); 943 } 944 continue; 945 } 946 if (sscanf(cp, "%lu partitions", &v) == 1) { 947 if (v == 0 || v > MAXPARTITIONS) { 948 fprintf(stderr, 949 "line %d: bad # of partitions\n", lineno); 950 lp->d_npartitions = MAXPARTITIONS; 951 errors++; 952 } else 953 lp->d_npartitions = v; 954 continue; 955 } 956 if (tp == NULL) 957 tp = blank; 958 if (streq(cp, "disk")) { 959 strncpy(lp->d_typename, tp, sizeof (lp->d_typename)); 960 continue; 961 } 962 if (streq(cp, "label")) { 963 strncpy(lp->d_packname, tp, sizeof (lp->d_packname)); 964 continue; 965 } 966 if (streq(cp, "bytes/sector")) { 967 v = strtoul(tp, NULL, 10); 968 if (v == 0 || (v % DEV_BSIZE) != 0) { 969 fprintf(stderr, 970 "line %d: %s: bad sector size\n", 971 lineno, tp); 972 errors++; 973 } else 974 lp->d_secsize = v; 975 continue; 976 } 977 if (streq(cp, "sectors/track")) { 978 v = strtoul(tp, NULL, 10); 979#if (ULONG_MAX != 0xffffffffUL) 980 if (v == 0 || v > 0xffffffff) { 981#else 982 if (v == 0) { 983#endif 984 fprintf(stderr, "line %d: %s: bad %s\n", 985 lineno, tp, cp); 986 errors++; 987 } else 988 lp->d_nsectors = v; 989 continue; 990 } 991 if (streq(cp, "sectors/cylinder")) { 992 v = strtoul(tp, NULL, 10); 993 if (v == 0) { 994 fprintf(stderr, "line %d: %s: bad %s\n", 995 lineno, tp, cp); 996 errors++; 997 } else 998 lp->d_secpercyl = v; 999 continue; 1000 } 1001 if (streq(cp, "tracks/cylinder")) { 1002 v = strtoul(tp, NULL, 10); 1003 if (v == 0) { 1004 fprintf(stderr, "line %d: %s: bad %s\n", 1005 lineno, tp, cp); 1006 errors++; 1007 } else 1008 lp->d_ntracks = v; 1009 continue; 1010 } 1011 if (streq(cp, "cylinders")) { 1012 v = strtoul(tp, NULL, 10); 1013 if (v == 0) { 1014 fprintf(stderr, "line %d: %s: bad %s\n", 1015 lineno, tp, cp); 1016 errors++; 1017 } else 1018 lp->d_ncylinders = v; 1019 continue; 1020 } 1021 if (streq(cp, "sectors/unit")) { 1022 v = strtoul(tp, NULL, 10); 1023 if (v == 0) { 1024 fprintf(stderr, "line %d: %s: bad %s\n", 1025 lineno, tp, cp); 1026 errors++; 1027 } else 1028 lp->d_secperunit = v; 1029 continue; 1030 } 1031 if (streq(cp, "rpm")) { 1032 v = strtoul(tp, NULL, 10); 1033 if (v == 0 || v > USHRT_MAX) { 1034 fprintf(stderr, "line %d: %s: bad %s\n", 1035 lineno, tp, cp); 1036 errors++; 1037 } else 1038 lp->d_rpm = v; 1039 continue; 1040 } 1041 if (streq(cp, "interleave")) { 1042 v = strtoul(tp, NULL, 10); 1043 if (v == 0 || v > USHRT_MAX) { 1044 fprintf(stderr, "line %d: %s: bad %s\n", 1045 lineno, tp, cp); 1046 errors++; 1047 } else 1048 lp->d_interleave = v; 1049 continue; 1050 } 1051 if (streq(cp, "trackskew")) { 1052 v = strtoul(tp, NULL, 10); 1053 if (v > USHRT_MAX) { 1054 fprintf(stderr, "line %d: %s: bad %s\n", 1055 lineno, tp, cp); 1056 errors++; 1057 } else 1058 lp->d_trackskew = v; 1059 continue; 1060 } 1061 if (streq(cp, "cylinderskew")) { 1062 v = strtoul(tp, NULL, 10); 1063 if (v > USHRT_MAX) { 1064 fprintf(stderr, "line %d: %s: bad %s\n", 1065 lineno, tp, cp); 1066 errors++; 1067 } else 1068 lp->d_cylskew = v; 1069 continue; 1070 } 1071 if (streq(cp, "headswitch")) { 1072 v = strtoul(tp, NULL, 10); 1073 lp->d_headswitch = v; 1074 continue; 1075 } 1076 if (streq(cp, "track-to-track seek")) { 1077 v = strtoul(tp, NULL, 10); 1078 lp->d_trkseek = v; 1079 continue; 1080 } 1081 /* the ':' was removed above */ 1082 if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') { 1083 fprintf(stderr, 1084 "line %d: %s: Unknown disklabel field\n", lineno, 1085 cp); 1086 errors++; 1087 continue; 1088 } 1089 1090 /* Process a partition specification line. */ 1091 part = *cp - 'a'; 1092 if (part >= lp->d_npartitions) { 1093 fprintf(stderr, 1094 "line %d: partition name out of range a-%c: %s\n", 1095 lineno, 'a' + lp->d_npartitions - 1, cp); 1096 errors++; 1097 continue; 1098 } 1099 part_set[part] = 1; 1100 1101 if (getasciipartspec(tp, lp, part, lineno) != 0) { 1102 errors++; 1103 break; 1104 } 1105 } 1106 errors += checklabel(lp); 1107 return (errors == 0); 1108} 1109 1110#define NXTNUM(n) do { \ 1111 if (tp == NULL) { \ 1112 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \ 1113 return (1); \ 1114 } else { \ 1115 cp = tp, tp = word(cp); \ 1116 (n) = strtoul(cp, NULL, 10); \ 1117 } \ 1118} while (0) 1119 1120/* retain 1 character following number */ 1121#define NXTWORD(w,n) do { \ 1122 if (tp == NULL) { \ 1123 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \ 1124 return (1); \ 1125 } else { \ 1126 char *tmp; \ 1127 cp = tp, tp = word(cp); \ 1128 (n) = strtoul(cp, &tmp, 10); \ 1129 if (tmp) (w) = *tmp; \ 1130 } \ 1131} while (0) 1132 1133/* 1134 * Read a partition line into partition `part' in the specified disklabel. 1135 * Return 0 on success, 1 on failure. 1136 */ 1137int 1138getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno) 1139{ 1140 struct partition *pp; 1141 char *cp; 1142 const char **cpp; 1143 u_long v; 1144 1145 pp = &lp->d_partitions[part]; 1146 cp = NULL; 1147 1148 v = 0; 1149 NXTWORD(part_size_type[part],v); 1150 if (v == 0 && part_size_type[part] != '*') { 1151 fprintf(stderr, 1152 "line %d: %s: bad partition size\n", lineno, cp); 1153 return (1); 1154 } 1155 pp->p_size = v; 1156 1157 v = 0; 1158 NXTWORD(part_offset_type[part],v); 1159 if (v == 0 && part_offset_type[part] != '*' && 1160 part_offset_type[part] != '\0') { 1161 fprintf(stderr, 1162 "line %d: %s: bad partition offset\n", lineno, cp); 1163 return (1); 1164 } 1165 pp->p_offset = v; 1166 cp = tp, tp = word(cp); 1167 for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++) 1168 if (*cpp && streq(*cpp, cp)) 1169 break; 1170 if (*cpp != NULL) { 1171 pp->p_fstype = cpp - fstypenames; 1172 } else { 1173 if (isdigit(*cp)) 1174 v = strtoul(cp, NULL, 10); 1175 else 1176 v = FSMAXTYPES; 1177 if (v >= FSMAXTYPES) { 1178 fprintf(stderr, 1179 "line %d: Warning, unknown file system type %s\n", 1180 lineno, cp); 1181 v = FS_UNUSED; 1182 } 1183 pp->p_fstype = v; 1184 } 1185 1186 switch (pp->p_fstype) { 1187 case FS_UNUSED: 1188 /* 1189 * allow us to accept defaults for 1190 * fsize/frag/cpg 1191 */ 1192 if (tp) { 1193 NXTNUM(pp->p_fsize); 1194 if (pp->p_fsize == 0) 1195 break; 1196 NXTNUM(v); 1197 pp->p_frag = v / pp->p_fsize; 1198 } 1199 /* else default to 0's */ 1200 break; 1201 1202 /* These happen to be the same */ 1203 case FS_BSDFFS: 1204 case FS_BSDLFS: 1205 if (tp) { 1206 NXTNUM(pp->p_fsize); 1207 if (pp->p_fsize == 0) 1208 break; 1209 NXTNUM(v); 1210 pp->p_frag = v / pp->p_fsize; 1211 NXTNUM(pp->p_cpg); 1212 } else { 1213 /* 1214 * FIX! poor attempt at adaptive 1215 */ 1216 /* 1 GB */ 1217 if (pp->p_size < 1024*1024*1024 / lp->d_secsize) { 1218 /* 1219 * FIX! These are too low, but are traditional 1220 */ 1221 pp->p_fsize = DEFAULT_NEWFS_FRAG; 1222 pp->p_frag = DEFAULT_NEWFS_BLOCK / 1223 DEFAULT_NEWFS_FRAG; 1224 pp->p_cpg = DEFAULT_NEWFS_CPG; 1225 } else { 1226 pp->p_fsize = BIG_NEWFS_FRAG; 1227 pp->p_frag = BIG_NEWFS_BLOCK / 1228 BIG_NEWFS_FRAG; 1229 pp->p_cpg = BIG_NEWFS_CPG; 1230 } 1231 } 1232 default: 1233 break; 1234 } 1235 return (0); 1236} 1237 1238/* 1239 * Check disklabel for errors and fill in 1240 * derived fields according to supplied values. 1241 */ 1242int 1243checklabel(struct disklabel *lp) 1244{ 1245 struct partition *pp; 1246 int i, errors = 0; 1247 char part; 1248 u_long total_size, total_percent, current_offset; 1249 int seen_default_offset; 1250 int hog_part; 1251 int j; 1252 struct partition *pp2; 1253 1254 if (lp->d_secsize == 0) { 1255 fprintf(stderr, "sector size 0\n"); 1256 return (1); 1257 } 1258 if (lp->d_nsectors == 0) { 1259 fprintf(stderr, "sectors/track 0\n"); 1260 return (1); 1261 } 1262 if (lp->d_ntracks == 0) { 1263 fprintf(stderr, "tracks/cylinder 0\n"); 1264 return (1); 1265 } 1266 if (lp->d_ncylinders == 0) { 1267 fprintf(stderr, "cylinders/unit 0\n"); 1268 errors++; 1269 } 1270 if (lp->d_rpm == 0) 1271 Warning("revolutions/minute 0"); 1272 if (lp->d_secpercyl == 0) 1273 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks; 1274 if (lp->d_secperunit == 0) 1275 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders; 1276 if (lp->d_bbsize == 0) { 1277 fprintf(stderr, "boot block size 0\n"); 1278 errors++; 1279 } else if (lp->d_bbsize % lp->d_secsize) 1280 Warning("boot block size %% sector-size != 0"); 1281 if (lp->d_npartitions > MAXPARTITIONS) 1282 Warning("number of partitions (%lu) > MAXPARTITIONS (%d)", 1283 (u_long)lp->d_npartitions, MAXPARTITIONS); 1284 1285 /* first allocate space to the partitions, then offsets */ 1286 total_size = 0; /* in sectors */ 1287 total_percent = 0; /* in percent */ 1288 hog_part = -1; 1289 /* find all fixed partitions */ 1290 for (i = 0; i < lp->d_npartitions; i++) { 1291 pp = &lp->d_partitions[i]; 1292 if (part_set[i]) { 1293 if (part_size_type[i] == '*') { 1294 if (i == RAW_PART) { 1295 pp->p_size = lp->d_secperunit; 1296 } else { 1297 if (hog_part != -1) 1298 Warning("Too many '*' partitions (%c and %c)", 1299 hog_part + 'a',i + 'a'); 1300 else 1301 hog_part = i; 1302 } 1303 } else { 1304 off_t size; 1305 1306 size = pp->p_size; 1307 switch (part_size_type[i]) { 1308 case '%': 1309 total_percent += size; 1310 break; 1311 case 'k': 1312 case 'K': 1313 size *= 1024ULL; 1314 break; 1315 case 'm': 1316 case 'M': 1317 size *= 1024ULL * 1024ULL; 1318 break; 1319 case 'g': 1320 case 'G': 1321 size *= 1024ULL * 1024ULL * 1024ULL; 1322 break; 1323 case '\0': 1324 break; 1325 default: 1326 Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]); 1327 break; 1328 } 1329 /* don't count %'s yet */ 1330 if (part_size_type[i] != '%') { 1331 /* 1332 * for all not in sectors, convert to 1333 * sectors 1334 */ 1335 if (part_size_type[i] != '\0') { 1336 if (size % lp->d_secsize != 0) 1337 Warning("partition %c not an integer number of sectors", 1338 i + 'a'); 1339 size /= lp->d_secsize; 1340 pp->p_size = size; 1341 } 1342 /* else already in sectors */ 1343 if (i != RAW_PART) 1344 total_size += size; 1345 } 1346 } 1347 } 1348 } 1349 /* handle % partitions - note %'s don't need to add up to 100! */ 1350 if (total_percent != 0) { 1351 long free_space = lp->d_secperunit - total_size; 1352 if (total_percent > 100) { 1353 fprintf(stderr,"total percentage %lu is greater than 100\n", 1354 total_percent); 1355 errors++; 1356 } 1357 1358 if (free_space > 0) { 1359 for (i = 0; i < lp->d_npartitions; i++) { 1360 pp = &lp->d_partitions[i]; 1361 if (part_set[i] && part_size_type[i] == '%') { 1362 /* careful of overflows! and integer roundoff */ 1363 pp->p_size = ((double)pp->p_size/100) * free_space; 1364 total_size += pp->p_size; 1365 1366 /* FIX we can lose a sector or so due to roundoff per 1367 partition. A more complex algorithm could avoid that */ 1368 } 1369 } 1370 } else { 1371 fprintf(stderr, 1372 "%ld sectors available to give to '*' and '%%' partitions\n", 1373 free_space); 1374 errors++; 1375 /* fix? set all % partitions to size 0? */ 1376 } 1377 } 1378 /* give anything remaining to the hog partition */ 1379 if (hog_part != -1) { 1380 lp->d_partitions[hog_part].p_size = lp->d_secperunit - total_size; 1381 total_size = lp->d_secperunit; 1382 } 1383 1384 /* Now set the offsets for each partition */ 1385 current_offset = 0; /* in sectors */ 1386 seen_default_offset = 0; 1387 for (i = 0; i < lp->d_npartitions; i++) { 1388 part = 'a' + i; 1389 pp = &lp->d_partitions[i]; 1390 if (part_set[i]) { 1391 if (part_offset_type[i] == '*') { 1392 if (i == RAW_PART) { 1393 pp->p_offset = 0; 1394 } else { 1395 pp->p_offset = current_offset; 1396 seen_default_offset = 1; 1397 } 1398 } else { 1399 /* allow them to be out of order for old-style tables */ 1400 if (pp->p_offset < current_offset && 1401 seen_default_offset && i != RAW_PART && 1402 pp->p_fstype != FS_VINUM) { 1403 fprintf(stderr, 1404"Offset %ld for partition %c overlaps previous partition which ends at %lu\n", 1405 (long)pp->p_offset,i+'a',current_offset); 1406 fprintf(stderr, 1407"Labels with any *'s for offset must be in ascending order by sector\n"); 1408 errors++; 1409 } else if (pp->p_offset != current_offset && 1410 i != RAW_PART && seen_default_offset) { 1411 /* 1412 * this may give unneeded warnings if 1413 * partitions are out-of-order 1414 */ 1415 Warning( 1416"Offset %ld for partition %c doesn't match expected value %ld", 1417 (long)pp->p_offset, i + 'a', current_offset); 1418 } 1419 } 1420 if (i != RAW_PART) 1421 current_offset = pp->p_offset + pp->p_size; 1422 } 1423 } 1424 1425 for (i = 0; i < lp->d_npartitions; i++) { 1426 part = 'a' + i; 1427 pp = &lp->d_partitions[i]; 1428 if (pp->p_size == 0 && pp->p_offset != 0) 1429 Warning("partition %c: size 0, but offset %lu", 1430 part, (u_long)pp->p_offset); 1431#ifdef notdef 1432 if (pp->p_size % lp->d_secpercyl) 1433 Warning("partition %c: size %% cylinder-size != 0", 1434 part); 1435 if (pp->p_offset % lp->d_secpercyl) 1436 Warning("partition %c: offset %% cylinder-size != 0", 1437 part); 1438#endif 1439 if (pp->p_offset > lp->d_secperunit) { 1440 fprintf(stderr, 1441 "partition %c: offset past end of unit\n", part); 1442 errors++; 1443 } 1444 if (pp->p_offset + pp->p_size > lp->d_secperunit) { 1445 fprintf(stderr, 1446 "partition %c: partition extends past end of unit\n", 1447 part); 1448 errors++; 1449 } 1450 if (i == RAW_PART) 1451 { 1452 if (pp->p_fstype != FS_UNUSED) 1453 Warning("partition %c is not marked as unused!",part); 1454 if (pp->p_offset != 0) 1455 Warning("partition %c doesn't start at 0!",part); 1456 if (pp->p_size != lp->d_secperunit) 1457 Warning("partition %c doesn't cover the whole unit!",part); 1458 1459 if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) || 1460 (pp->p_size != lp->d_secperunit)) { 1461 Warning("An incorrect partition %c may cause problems for " 1462 "standard system utilities",part); 1463 } 1464 } 1465 1466 /* check for overlaps */ 1467 /* this will check for all possible overlaps once and only once */ 1468 for (j = 0; j < i; j++) { 1469 pp2 = &lp->d_partitions[j]; 1470 if (j != RAW_PART && i != RAW_PART && 1471 pp->p_fstype != FS_VINUM && 1472 pp2->p_fstype != FS_VINUM && 1473 part_set[i] && part_set[j]) { 1474 if (pp2->p_offset < pp->p_offset + pp->p_size && 1475 (pp2->p_offset + pp2->p_size > pp->p_offset || 1476 pp2->p_offset >= pp->p_offset)) { 1477 fprintf(stderr,"partitions %c and %c overlap!\n", 1478 j + 'a', i + 'a'); 1479 errors++; 1480 } 1481 } 1482 } 1483 } 1484 for (; i < MAXPARTITIONS; i++) { 1485 part = 'a' + i; 1486 pp = &lp->d_partitions[i]; 1487 if (pp->p_size || pp->p_offset) 1488 Warning("unused partition %c: size %d offset %lu", 1489 'a' + i, pp->p_size, (u_long)pp->p_offset); 1490 } 1491 return (errors); 1492} 1493 1494/* 1495 * When operating on a "virgin" disk, try getting an initial label 1496 * from the associated device driver. This might work for all device 1497 * drivers that are able to fetch some initial device parameters 1498 * without even having access to a (BSD) disklabel, like SCSI disks, 1499 * most IDE drives, or vn devices. 1500 * 1501 * The device name must be given in its "canonical" form. 1502 */ 1503struct disklabel * 1504getvirginlabel(void) 1505{ 1506 static struct disklabel loclab; 1507 struct partition *dp; 1508 char lnamebuf[BBSIZE]; 1509 int f; 1510 u_int secsize, u; 1511 off_t mediasize; 1512 1513 if (dkname[0] == '/') { 1514 warnx("\"auto\" requires the usage of a canonical disk name"); 1515 return (NULL); 1516 } 1517 (void)snprintf(lnamebuf, BBSIZE, "%s%s", _PATH_DEV, dkname); 1518 if ((f = open(lnamebuf, O_RDONLY)) == -1) { 1519 warn("cannot open %s", lnamebuf); 1520 return (NULL); 1521 } 1522 1523 /* New world order */ 1524 if ((ioctl(f, DIOCGMEDIASIZE, &mediasize) != 0) || 1525 (ioctl(f, DIOCGSECTORSIZE, &secsize) != 0)) { 1526 close (f); 1527 return (NULL); 1528 } 1529 memset(&loclab, 0, sizeof loclab); 1530 loclab.d_magic = DISKMAGIC; 1531 loclab.d_magic2 = DISKMAGIC; 1532 loclab.d_secsize = secsize; 1533 loclab.d_secperunit = mediasize / secsize; 1534 1535 /* 1536 * Nobody in these enligthened days uses the CHS geometry for 1537 * anything, but nontheless try to get it right. If we fail 1538 * to get any good ideas from the device, construct something 1539 * which is IBM-PC friendly. 1540 */ 1541 if (ioctl(f, DIOCGFWSECTORS, &u) == 0) 1542 loclab.d_nsectors = u; 1543 else 1544 loclab.d_nsectors = 63; 1545 if (ioctl(f, DIOCGFWHEADS, &u) == 0) 1546 loclab.d_ntracks = u; 1547 else if (loclab.d_secperunit <= 63*1*1024) 1548 loclab.d_ntracks = 1; 1549 else if (loclab.d_secperunit <= 63*16*1024) 1550 loclab.d_ntracks = 16; 1551 else 1552 loclab.d_ntracks = 255; 1553 loclab.d_secpercyl = loclab.d_ntracks * loclab.d_nsectors; 1554 loclab.d_ncylinders = loclab.d_secperunit / loclab.d_secpercyl; 1555 loclab.d_npartitions = MAXPARTITIONS; 1556 1557 /* Various (unneeded) compat stuff */ 1558 loclab.d_rpm = 3600; 1559 loclab.d_bbsize = BBSIZE; 1560 loclab.d_interleave = 1;; 1561 strncpy(loclab.d_typename, "amnesiac", 1562 sizeof(loclab.d_typename)); 1563 1564 dp = &loclab.d_partitions[RAW_PART]; 1565 dp->p_size = loclab.d_secperunit; 1566 loclab.d_checksum = dkcksum(&loclab); 1567 close (f); 1568 return (&loclab); 1569} 1570 1571/* 1572 * If we are installing a boot program that doesn't fit in d_bbsize 1573 * we need to mark those partitions that the boot overflows into. 1574 * This allows newfs to prevent creation of a file system where it might 1575 * clobber bootstrap code. 1576 */ 1577void 1578setbootflag(struct disklabel *lp) 1579{ 1580 struct partition *pp; 1581 int i, errors = 0; 1582 char part; 1583 u_long boffset; 1584 1585 if (bootbuf == 0) 1586 return; 1587 boffset = bootsize / lp->d_secsize; 1588 for (i = 0; i < lp->d_npartitions; i++) { 1589 part = 'a' + i; 1590 pp = &lp->d_partitions[i]; 1591 if (pp->p_size == 0) 1592 continue; 1593 if (boffset <= pp->p_offset) { 1594 if (pp->p_fstype == FS_BOOT) 1595 pp->p_fstype = FS_UNUSED; 1596 } else if (pp->p_fstype != FS_BOOT) { 1597 if (pp->p_fstype != FS_UNUSED) { 1598 fprintf(stderr, 1599 "boot overlaps used partition %c\n", 1600 part); 1601 errors++; 1602 } else { 1603 pp->p_fstype = FS_BOOT; 1604 Warning("boot overlaps partition %c, %s", 1605 part, "marked as FS_BOOT"); 1606 } 1607 } 1608 } 1609 if (errors) 1610 errx(4, "cannot install boot program"); 1611} 1612 1613/*VARARGS1*/ 1614void 1615Warning(const char *fmt, ...) 1616{ 1617 va_list ap; 1618 1619 fprintf(stderr, "Warning, "); 1620 va_start(ap, fmt); 1621 vfprintf(stderr, fmt, ap); 1622 fprintf(stderr, "\n"); 1623 va_end(ap); 1624} 1625 1626void 1627usage(void) 1628{ 1629#if NUMBOOT > 0 1630 fprintf(stderr, "%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", 1631 "usage: disklabel [-r] disk", 1632 "\t\t(to read label)", 1633 " disklabel -w [-r] [-n] disk type [ packid ]", 1634 "\t\t(to write label with existing boot program)", 1635 " disklabel -e [-r] [-n] disk", 1636 "\t\t(to edit label)", 1637 " disklabel -R [-r] [-n] disk protofile", 1638 "\t\t(to restore label with existing boot program)", 1639#if NUMBOOT > 1 1640 " disklabel -B [-n] [ -b boot1 [ -s boot2 ] ] disk [ type ]", 1641 "\t\t(to install boot program with existing label)", 1642 " disklabel -w -B [-n] [ -b boot1 [ -s boot2 ] ] disk type [ packid ]", 1643 "\t\t(to write label and boot program)", 1644 " disklabel -R -B [-n] [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]", 1645 "\t\t(to restore label and boot program)" 1646#else 1647 " disklabel -B [-n] [ -b bootprog ] disk [ type ]", 1648 "\t\t(to install boot program with existing on-disk label)", 1649 " disklabel -w -B [-n] [ -b bootprog ] disk type [ packid ]", 1650 "\t\t(to write label and install boot program)", 1651 " disklabel -R -B [-n] [ -b bootprog ] disk protofile [ type ]", 1652 "\t\t(to restore label and install boot program)" 1653#endif 1654 ); 1655#else 1656 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n", 1657 "usage: disklabel [-r] disk", "(to read label)", 1658 " disklabel -w [-r] [-n] disk type [ packid ]", 1659 "\t\t(to write label)", 1660 " disklabel -e [-r] [-n] disk", 1661 "\t\t(to edit label)", 1662 " disklabel -R [-r] [-n] disk protofile", 1663 "\t\t(to restore label)"); 1664#endif 1665 exit(1); 1666} 1667