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