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