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 44 static 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 51 static 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$"); 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 118 void makelabel(const char *, const char *, struct disklabel *); 119 int writelabel(int, const char *, struct disklabel *); 120 void l_perror(const char *); 121 struct disklabel *readlabel(int); 122 struct disklabel *makebootarea(char *, struct disklabel *, int); 123 void display(FILE *, const struct disklabel *); 124 int edit(struct disklabel *, int); 125 int editit(void); 126 char *skip(char *); 127 char *word(char *); 128 int getasciilabel(FILE *, struct disklabel *); 129 int getasciipartspec(char *, struct disklabel *, int, int); 130 int checklabel(struct disklabel *); 131 void setbootflag(struct disklabel *); 132 void Warning(const char *, ...) __printflike(1, 2); 133 void usage(void); 134 struct disklabel *getvirginlabel(void); 135 136 #define DEFEDITOR _PATH_VI 137 #define streq(a,b) (strcmp(a,b) == 0) 138 139 char *dkname; 140 char *specname; 141 char tmpfil[] = PATH_TMPFILE; 142 143 char namebuf[BBSIZE], *np = namebuf; 144 struct disklabel lab; 145 char bootarea[BBSIZE]; 146 char blank[] = ""; 147 char unknown[] = "unknown"; 148 149 #define MAX_PART ('z') 150 #define MAX_NUM_PARTS (1 + MAX_PART - 'a') 151 char part_size_type[MAX_NUM_PARTS]; 152 char part_offset_type[MAX_NUM_PARTS]; 153 int part_set[MAX_NUM_PARTS]; 154 155 #if NUMBOOT > 0 156 int installboot; /* non-zero if we should install a boot program */ 157 char *bootbuf; /* pointer to buffer with remainder of boot prog */ 158 int bootsize; /* size of remaining boot program */ 159 char *xxboot; /* primary boot */ 160 char *bootxx; /* secondary boot */ 161 char boot0[MAXPATHLEN]; 162 char boot1[MAXPATHLEN]; 163 #endif 164 165 enum { 166 UNSPEC, EDIT, NOWRITE, READ, RESTORE, WRITE, WRITEABLE, WRITEBOOT 167 } op = UNSPEC; 168 169 int rflag; 170 int disable_write; /* set to disable writing to disk label */ 171 172 #define OPTIONS "BNRWb:enrs:w" 173 174 int 175 main(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 */ 364 void 365 makelabel(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 381 int 382 writelabel(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 499 void 500 l_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 */ 533 struct disklabel * 534 readlabel(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 */ 564 struct disklabel * 565 makebootarea(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 __i386__ 662 for (i = DOSPARTOFF, found = 0; 663 !found && i < (int)(DOSPARTOFF + NDOSPART*sizeof(struct dos_partition)); 664 i++) 665 found = tmpbuf[i] != 0; 666 if (found) 667 memcpy((void *)&boot[DOSPARTOFF], 668 (void *)&tmpbuf[DOSPARTOFF], 669 NDOSPART * sizeof(struct dos_partition)); 670 free(tmpbuf); 671 #endif /* __i386__ */ 672 b = open(bootxx, O_RDONLY); 673 if (b < 0) 674 err(4, "%s", bootxx); 675 if (fstat(b, &sb) != 0) 676 err(4, "%s", bootxx); 677 if (dp->d_secsize + sb.st_size > dp->d_bbsize) 678 errx(4, "%s too large", bootxx); 679 if (read(b, &boot[dp->d_secsize], 680 (int)(dp->d_bbsize-dp->d_secsize)) < 0) 681 err(4, "%s", bootxx); 682 #else /* !(NUMBOOT > 1) */ 683 #ifdef __alpha__ 684 /* 685 * On the alpha, the primary bootstrap starts at the 686 * second sector of the boot area. The first sector 687 * contains the label and must be edited to contain the 688 * size and location of the primary bootstrap. 689 */ 690 n = read(b, boot + dp->d_secsize, (int)dp->d_bbsize); 691 if (n < 0) 692 err(4, "%s", xxboot); 693 bootinfo = (u_long *)(boot + 480); 694 bootinfo[0] = (n + dp->d_secsize - 1) / dp->d_secsize; 695 bootinfo[1] = 1; /* start at sector 1 */ 696 bootinfo[2] = 0; /* flags (must be zero) */ 697 #else /* !__alpha__ */ 698 if (read(b, boot, (int)dp->d_bbsize) < 0) 699 err(4, "%s", xxboot); 700 #endif /* __alpha__ */ 701 if (fstat(b, &sb) != 0) 702 err(4, "%s", xxboot); 703 bootsize = (int)sb.st_size - dp->d_bbsize; 704 if (bootsize > 0) { 705 /* XXX assume d_secsize is a power of two */ 706 bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1); 707 bootbuf = (char *)malloc((size_t)bootsize); 708 if (bootbuf == 0) 709 err(4, "%s", xxboot); 710 if (read(b, bootbuf, bootsize) < 0) { 711 free(bootbuf); 712 err(4, "%s", xxboot); 713 } 714 } 715 #endif /* NUMBOOT > 1 */ 716 (void)close(b); 717 #endif /* NUMBOOT > 0 */ 718 /* 719 * Make sure no part of the bootstrap is written in the area 720 * reserved for the label. 721 */ 722 for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++) 723 if (*p) 724 errx(2, "bootstrap doesn't leave room for disk label"); 725 return (lp); 726 } 727 728 void 729 display(FILE *f, const struct disklabel *lp) 730 { 731 int i, j; 732 const struct partition *pp; 733 734 fprintf(f, "# %s:\n", specname); 735 if ((unsigned) lp->d_type < DKMAXTYPES) 736 fprintf(f, "type: %s\n", dktypenames[lp->d_type]); 737 else 738 fprintf(f, "type: %u\n", lp->d_type); 739 fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename), 740 lp->d_typename); 741 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname), 742 lp->d_packname); 743 fprintf(f, "flags:"); 744 if (lp->d_flags & D_REMOVABLE) 745 fprintf(f, " removeable"); 746 if (lp->d_flags & D_ECC) 747 fprintf(f, " ecc"); 748 if (lp->d_flags & D_BADSECT) 749 fprintf(f, " badsect"); 750 fprintf(f, "\n"); 751 fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize); 752 fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors); 753 fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks); 754 fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl); 755 fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders); 756 fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit); 757 fprintf(f, "rpm: %u\n", lp->d_rpm); 758 fprintf(f, "interleave: %u\n", lp->d_interleave); 759 fprintf(f, "trackskew: %u\n", lp->d_trackskew); 760 fprintf(f, "cylinderskew: %u\n", lp->d_cylskew); 761 fprintf(f, "headswitch: %lu\t\t# milliseconds\n", 762 (u_long)lp->d_headswitch); 763 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n", 764 (u_long)lp->d_trkseek); 765 fprintf(f, "drivedata: "); 766 for (i = NDDATA - 1; i >= 0; i--) 767 if (lp->d_drivedata[i]) 768 break; 769 if (i < 0) 770 i = 0; 771 for (j = 0; j <= i; j++) 772 fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]); 773 fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions); 774 fprintf(f, 775 "# size offset fstype [fsize bsize bps/cpg]\n"); 776 pp = lp->d_partitions; 777 for (i = 0; i < lp->d_npartitions; i++, pp++) { 778 if (pp->p_size) { 779 fprintf(f, " %c: %8lu %8lu ", 'a' + i, 780 (u_long)pp->p_size, (u_long)pp->p_offset); 781 if ((unsigned) pp->p_fstype < FSMAXTYPES) 782 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]); 783 else 784 fprintf(f, "%8d", pp->p_fstype); 785 switch (pp->p_fstype) { 786 787 case FS_UNUSED: /* XXX */ 788 fprintf(f, " %5lu %5lu %5.5s ", 789 (u_long)pp->p_fsize, 790 (u_long)(pp->p_fsize * pp->p_frag), ""); 791 break; 792 793 case FS_BSDFFS: 794 fprintf(f, " %5lu %5lu %5u ", 795 (u_long)pp->p_fsize, 796 (u_long)(pp->p_fsize * pp->p_frag), 797 pp->p_cpg); 798 break; 799 800 case FS_BSDLFS: 801 fprintf(f, " %5lu %5lu %5d", 802 (u_long)pp->p_fsize, 803 (u_long)(pp->p_fsize * pp->p_frag), 804 pp->p_cpg); 805 break; 806 807 default: 808 fprintf(f, "%20.20s", ""); 809 break; 810 } 811 fprintf(f, "\t# (Cyl. %4lu", 812 (u_long)(pp->p_offset / lp->d_secpercyl)); 813 if (pp->p_offset % lp->d_secpercyl) 814 putc('*', f); 815 else 816 putc(' ', f); 817 fprintf(f, "- %lu", 818 (u_long)((pp->p_offset + pp->p_size + 819 lp->d_secpercyl - 1) / 820 lp->d_secpercyl - 1)); 821 if (pp->p_size % lp->d_secpercyl) 822 putc('*', f); 823 fprintf(f, ")\n"); 824 } 825 } 826 fflush(f); 827 } 828 829 int 830 edit(struct disklabel *lp, int f) 831 { 832 int c, fd; 833 struct disklabel label; 834 FILE *fp; 835 836 if ((fd = mkstemp(tmpfil)) == -1 || 837 (fp = fdopen(fd, "w")) == NULL) { 838 warnx("can't create %s", tmpfil); 839 return (1); 840 } 841 display(fp, lp); 842 fclose(fp); 843 for (;;) { 844 if (!editit()) 845 break; 846 fp = fopen(tmpfil, "r"); 847 if (fp == NULL) { 848 warnx("can't reopen %s for reading", tmpfil); 849 break; 850 } 851 bzero((char *)&label, sizeof(label)); 852 if (getasciilabel(fp, &label)) { 853 *lp = label; 854 if (writelabel(f, bootarea, lp) == 0) { 855 fclose(fp); 856 (void) unlink(tmpfil); 857 return (0); 858 } 859 } 860 fclose(fp); 861 printf("re-edit the label? [y]: "); fflush(stdout); 862 c = getchar(); 863 if (c != EOF && c != (int)'\n') 864 while (getchar() != (int)'\n') 865 ; 866 if (c == (int)'n') 867 break; 868 } 869 (void) unlink(tmpfil); 870 return (1); 871 } 872 873 int 874 editit(void) 875 { 876 int pid, xpid; 877 int locstat, omask; 878 const char *ed; 879 880 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP)); 881 while ((pid = fork()) < 0) { 882 if (errno == EPROCLIM) { 883 warnx("you have too many processes"); 884 return(0); 885 } 886 if (errno != EAGAIN) { 887 warn("fork"); 888 return(0); 889 } 890 sleep(1); 891 } 892 if (pid == 0) { 893 sigsetmask(omask); 894 setgid(getgid()); 895 setuid(getuid()); 896 if ((ed = getenv("EDITOR")) == (char *)0) 897 ed = DEFEDITOR; 898 execlp(ed, ed, tmpfil, (char *)0); 899 err(1, "%s", ed); 900 } 901 while ((xpid = wait(&locstat)) >= 0) 902 if (xpid == pid) 903 break; 904 sigsetmask(omask); 905 return(!locstat); 906 } 907 908 char * 909 skip(char *cp) 910 { 911 912 while (*cp != '\0' && isspace(*cp)) 913 cp++; 914 if (*cp == '\0' || *cp == '#') 915 return (NULL); 916 return (cp); 917 } 918 919 char * 920 word(char *cp) 921 { 922 char c; 923 924 while (*cp != '\0' && !isspace(*cp) && *cp != '#') 925 cp++; 926 if ((c = *cp) != '\0') { 927 *cp++ = '\0'; 928 if (c != '#') 929 return (skip(cp)); 930 } 931 return (NULL); 932 } 933 934 /* 935 * Read an ascii label in from fd f, 936 * in the same format as that put out by display(), 937 * and fill in lp. 938 */ 939 int 940 getasciilabel(FILE *f, struct disklabel *lp) 941 { 942 char *cp; 943 const char **cpp; 944 unsigned int part; 945 char *tp, line[BUFSIZ]; 946 int v, lineno = 0, errors = 0; 947 int i; 948 949 lp->d_bbsize = BBSIZE; /* XXX */ 950 lp->d_sbsize = 0; /* XXX */ 951 while (fgets(line, sizeof(line) - 1, f)) { 952 lineno++; 953 if ((cp = index(line,'\n')) != 0) 954 *cp = '\0'; 955 cp = skip(line); 956 if (cp == NULL) 957 continue; 958 tp = index(cp, ':'); 959 if (tp == NULL) { 960 fprintf(stderr, "line %d: syntax error\n", lineno); 961 errors++; 962 continue; 963 } 964 *tp++ = '\0', tp = skip(tp); 965 if (streq(cp, "type")) { 966 if (tp == NULL) 967 tp = unknown; 968 cpp = dktypenames; 969 for (; cpp < &dktypenames[DKMAXTYPES]; cpp++) 970 if (*cpp && streq(*cpp, tp)) { 971 lp->d_type = cpp - dktypenames; 972 break; 973 } 974 if (cpp < &dktypenames[DKMAXTYPES]) 975 continue; 976 v = atoi(tp); 977 if ((unsigned)v >= DKMAXTYPES) 978 fprintf(stderr, "line %d:%s %d\n", lineno, 979 "Warning, unknown disk type", v); 980 lp->d_type = v; 981 continue; 982 } 983 if (streq(cp, "flags")) { 984 for (v = 0; (cp = tp) && *cp != '\0';) { 985 tp = word(cp); 986 if (streq(cp, "removeable")) 987 v |= D_REMOVABLE; 988 else if (streq(cp, "ecc")) 989 v |= D_ECC; 990 else if (streq(cp, "badsect")) 991 v |= D_BADSECT; 992 else { 993 fprintf(stderr, 994 "line %d: %s: bad flag\n", 995 lineno, cp); 996 errors++; 997 } 998 } 999 lp->d_flags = v; 1000 continue; 1001 } 1002 if (streq(cp, "drivedata")) { 1003 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) { 1004 lp->d_drivedata[i++] = atoi(cp); 1005 tp = word(cp); 1006 } 1007 continue; 1008 } 1009 if (sscanf(cp, "%d partitions", &v) == 1) { 1010 if (v == 0 || (unsigned)v > MAXPARTITIONS) { 1011 fprintf(stderr, 1012 "line %d: bad # of partitions\n", lineno); 1013 lp->d_npartitions = MAXPARTITIONS; 1014 errors++; 1015 } else 1016 lp->d_npartitions = v; 1017 continue; 1018 } 1019 if (tp == NULL) 1020 tp = blank; 1021 if (streq(cp, "disk")) { 1022 strncpy(lp->d_typename, tp, sizeof (lp->d_typename)); 1023 continue; 1024 } 1025 if (streq(cp, "label")) { 1026 strncpy(lp->d_packname, tp, sizeof (lp->d_packname)); 1027 continue; 1028 } 1029 if (streq(cp, "bytes/sector")) { 1030 v = atoi(tp); 1031 if (v <= 0 || (v % DEV_BSIZE) != 0) { 1032 fprintf(stderr, 1033 "line %d: %s: bad sector size\n", 1034 lineno, tp); 1035 errors++; 1036 } else 1037 lp->d_secsize = v; 1038 continue; 1039 } 1040 if (streq(cp, "sectors/track")) { 1041 v = atoi(tp); 1042 if (v <= 0) { 1043 fprintf(stderr, "line %d: %s: bad %s\n", 1044 lineno, tp, cp); 1045 errors++; 1046 } else 1047 lp->d_nsectors = v; 1048 continue; 1049 } 1050 if (streq(cp, "sectors/cylinder")) { 1051 v = atoi(tp); 1052 if (v <= 0) { 1053 fprintf(stderr, "line %d: %s: bad %s\n", 1054 lineno, tp, cp); 1055 errors++; 1056 } else 1057 lp->d_secpercyl = v; 1058 continue; 1059 } 1060 if (streq(cp, "tracks/cylinder")) { 1061 v = atoi(tp); 1062 if (v <= 0) { 1063 fprintf(stderr, "line %d: %s: bad %s\n", 1064 lineno, tp, cp); 1065 errors++; 1066 } else 1067 lp->d_ntracks = v; 1068 continue; 1069 } 1070 if (streq(cp, "cylinders")) { 1071 v = atoi(tp); 1072 if (v <= 0) { 1073 fprintf(stderr, "line %d: %s: bad %s\n", 1074 lineno, tp, cp); 1075 errors++; 1076 } else 1077 lp->d_ncylinders = v; 1078 continue; 1079 } 1080 if (streq(cp, "sectors/unit")) { 1081 v = atoi(tp); 1082 if (v <= 0) { 1083 fprintf(stderr, "line %d: %s: bad %s\n", 1084 lineno, tp, cp); 1085 errors++; 1086 } else 1087 lp->d_secperunit = v; 1088 continue; 1089 } 1090 if (streq(cp, "rpm")) { 1091 v = atoi(tp); 1092 if (v <= 0) { 1093 fprintf(stderr, "line %d: %s: bad %s\n", 1094 lineno, tp, cp); 1095 errors++; 1096 } else 1097 lp->d_rpm = v; 1098 continue; 1099 } 1100 if (streq(cp, "interleave")) { 1101 v = atoi(tp); 1102 if (v <= 0) { 1103 fprintf(stderr, "line %d: %s: bad %s\n", 1104 lineno, tp, cp); 1105 errors++; 1106 } else 1107 lp->d_interleave = v; 1108 continue; 1109 } 1110 if (streq(cp, "trackskew")) { 1111 v = atoi(tp); 1112 if (v < 0) { 1113 fprintf(stderr, "line %d: %s: bad %s\n", 1114 lineno, tp, cp); 1115 errors++; 1116 } else 1117 lp->d_trackskew = v; 1118 continue; 1119 } 1120 if (streq(cp, "cylinderskew")) { 1121 v = atoi(tp); 1122 if (v < 0) { 1123 fprintf(stderr, "line %d: %s: bad %s\n", 1124 lineno, tp, cp); 1125 errors++; 1126 } else 1127 lp->d_cylskew = v; 1128 continue; 1129 } 1130 if (streq(cp, "headswitch")) { 1131 v = atoi(tp); 1132 if (v < 0) { 1133 fprintf(stderr, "line %d: %s: bad %s\n", 1134 lineno, tp, cp); 1135 errors++; 1136 } else 1137 lp->d_headswitch = v; 1138 continue; 1139 } 1140 if (streq(cp, "track-to-track seek")) { 1141 v = atoi(tp); 1142 if (v < 0) { 1143 fprintf(stderr, "line %d: %s: bad %s\n", 1144 lineno, tp, cp); 1145 errors++; 1146 } else 1147 lp->d_trkseek = v; 1148 continue; 1149 } 1150 /* the ':' was removed above */ 1151 if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') { 1152 fprintf(stderr, 1153 "line %d: %s: Unknown disklabel field\n", lineno, 1154 cp); 1155 errors++; 1156 continue; 1157 } 1158 1159 /* Process a partition specification line. */ 1160 part = *cp - 'a'; 1161 if (part >= lp->d_npartitions) { 1162 fprintf(stderr, 1163 "line %d: partition name out of range a-%c: %s\n", 1164 lineno, 'a' + lp->d_npartitions - 1, cp); 1165 errors++; 1166 continue; 1167 } 1168 part_set[part] = 1; 1169 1170 if (getasciipartspec(tp, lp, part, lineno) != 0) { 1171 errors++; 1172 break; 1173 } 1174 } 1175 errors += checklabel(lp); 1176 return (errors == 0); 1177 } 1178 1179 #define NXTNUM(n) do { \ 1180 if (tp == NULL) { \ 1181 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \ 1182 return (1); \ 1183 } else { \ 1184 cp = tp, tp = word(cp); \ 1185 (n) = atoi(cp); \ 1186 } \ 1187 } while (0) 1188 1189 /* retain 1 character following number */ 1190 #define NXTWORD(w,n) do { \ 1191 if (tp == NULL) { \ 1192 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \ 1193 return (1); \ 1194 } else { \ 1195 char *tmp; \ 1196 cp = tp, tp = word(cp); \ 1197 (n) = strtol(cp,&tmp,10); \ 1198 if (tmp) (w) = *tmp; \ 1199 } \ 1200 } while (0) 1201 1202 /* 1203 * Read a partition line into partition `part' in the specified disklabel. 1204 * Return 0 on success, 1 on failure. 1205 */ 1206 int 1207 getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno) 1208 { 1209 struct partition *pp; 1210 char *cp; 1211 const char **cpp; 1212 int v; 1213 1214 pp = &lp->d_partitions[part]; 1215 cp = NULL; 1216 1217 v = 0; 1218 NXTWORD(part_size_type[part],v); 1219 if (v < 0 || (v == 0 && part_size_type[part] != '*')) { 1220 fprintf(stderr, "line %d: %s: bad partition size\n", lineno, 1221 cp); 1222 return (1); 1223 } 1224 pp->p_size = v; 1225 1226 v = 0; 1227 NXTWORD(part_offset_type[part],v); 1228 if (v < 0 || (v == 0 && part_offset_type[part] != '*' && 1229 part_offset_type[part] != '\0')) { 1230 fprintf(stderr, "line %d: %s: bad partition offset\n", lineno, 1231 cp); 1232 return (1); 1233 } 1234 pp->p_offset = v; 1235 cp = tp, tp = word(cp); 1236 for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++) 1237 if (*cpp && streq(*cpp, cp)) 1238 break; 1239 if (*cpp != NULL) { 1240 pp->p_fstype = cpp - fstypenames; 1241 } else { 1242 if (isdigit(*cp)) 1243 v = atoi(cp); 1244 else 1245 v = FSMAXTYPES; 1246 if ((unsigned)v >= FSMAXTYPES) { 1247 fprintf(stderr, 1248 "line %d: Warning, unknown file system type %s\n", 1249 lineno, cp); 1250 v = FS_UNUSED; 1251 } 1252 pp->p_fstype = v; 1253 } 1254 1255 switch (pp->p_fstype) { 1256 case FS_UNUSED: 1257 /* 1258 * allow us to accept defaults for 1259 * fsize/frag/cpg 1260 */ 1261 if (tp) { 1262 NXTNUM(pp->p_fsize); 1263 if (pp->p_fsize == 0) 1264 break; 1265 NXTNUM(v); 1266 pp->p_frag = v / pp->p_fsize; 1267 } 1268 /* else default to 0's */ 1269 break; 1270 1271 /* These happen to be the same */ 1272 case FS_BSDFFS: 1273 case FS_BSDLFS: 1274 if (tp) { 1275 NXTNUM(pp->p_fsize); 1276 if (pp->p_fsize == 0) 1277 break; 1278 NXTNUM(v); 1279 pp->p_frag = v / pp->p_fsize; 1280 NXTNUM(pp->p_cpg); 1281 } else { 1282 /* 1283 * FIX! poor attempt at adaptive 1284 */ 1285 /* 1 GB */ 1286 if (pp->p_size < 1024*1024*1024 / lp->d_secsize) { 1287 /* 1288 * FIX! These are too low, but are traditional 1289 */ 1290 pp->p_fsize = DEFAULT_NEWFS_FRAG; 1291 pp->p_frag = DEFAULT_NEWFS_BLOCK / 1292 DEFAULT_NEWFS_FRAG; 1293 pp->p_cpg = DEFAULT_NEWFS_CPG; 1294 } else { 1295 pp->p_fsize = BIG_NEWFS_FRAG; 1296 pp->p_frag = BIG_NEWFS_BLOCK / 1297 BIG_NEWFS_FRAG; 1298 pp->p_cpg = BIG_NEWFS_CPG; 1299 } 1300 } 1301 default: 1302 break; 1303 } 1304 return (0); 1305 } 1306 1307 /* 1308 * Check disklabel for errors and fill in 1309 * derived fields according to supplied values. 1310 */ 1311 int 1312 checklabel(struct disklabel *lp) 1313 { 1314 struct partition *pp; 1315 int i, errors = 0; 1316 char part; 1317 unsigned long total_size, total_percent, current_offset; 1318 int seen_default_offset; 1319 int hog_part; 1320 int j; 1321 struct partition *pp2; 1322 1323 if (lp->d_secsize == 0) { 1324 fprintf(stderr, "sector size 0\n"); 1325 return (1); 1326 } 1327 if (lp->d_nsectors == 0) { 1328 fprintf(stderr, "sectors/track 0\n"); 1329 return (1); 1330 } 1331 if (lp->d_ntracks == 0) { 1332 fprintf(stderr, "tracks/cylinder 0\n"); 1333 return (1); 1334 } 1335 if (lp->d_ncylinders == 0) { 1336 fprintf(stderr, "cylinders/unit 0\n"); 1337 errors++; 1338 } 1339 if (lp->d_rpm == 0) 1340 Warning("revolutions/minute 0"); 1341 if (lp->d_secpercyl == 0) 1342 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks; 1343 if (lp->d_secperunit == 0) 1344 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders; 1345 if (lp->d_bbsize == 0) { 1346 fprintf(stderr, "boot block size 0\n"); 1347 errors++; 1348 } else if (lp->d_bbsize % lp->d_secsize) 1349 Warning("boot block size %% sector-size != 0"); 1350 if (lp->d_npartitions > MAXPARTITIONS) 1351 Warning("number of partitions (%lu) > MAXPARTITIONS (%d)", 1352 (u_long)lp->d_npartitions, MAXPARTITIONS); 1353 1354 /* first allocate space to the partitions, then offsets */ 1355 total_size = 0; /* in sectors */ 1356 total_percent = 0; /* in percent */ 1357 hog_part = -1; 1358 /* find all fixed partitions */ 1359 for (i = 0; i < lp->d_npartitions; i++) { 1360 pp = &lp->d_partitions[i]; 1361 if (part_set[i]) { 1362 if (part_size_type[i] == '*') { 1363 if (i == RAW_PART) { 1364 pp->p_size = lp->d_secperunit; 1365 } else { 1366 if (hog_part != -1) 1367 Warning("Too many '*' partitions (%c and %c)", 1368 hog_part + 'a',i + 'a'); 1369 else 1370 hog_part = i; 1371 } 1372 } else { 1373 off_t size; 1374 1375 size = pp->p_size; 1376 switch (part_size_type[i]) { 1377 case '%': 1378 total_percent += size; 1379 break; 1380 case 'k': 1381 case 'K': 1382 size *= 1024ULL; 1383 break; 1384 case 'm': 1385 case 'M': 1386 size *= 1024ULL * 1024ULL; 1387 break; 1388 case 'g': 1389 case 'G': 1390 size *= 1024ULL * 1024ULL * 1024ULL; 1391 break; 1392 case '\0': 1393 break; 1394 default: 1395 Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]); 1396 break; 1397 } 1398 /* don't count %'s yet */ 1399 if (part_size_type[i] != '%') { 1400 /* 1401 * for all not in sectors, convert to 1402 * sectors 1403 */ 1404 if (part_size_type[i] != '\0') { 1405 if (size % lp->d_secsize != 0) 1406 Warning("partition %c not an integer number of sectors", 1407 i + 'a'); 1408 size /= lp->d_secsize; 1409 pp->p_size = size; 1410 } 1411 /* else already in sectors */ 1412 if (i != RAW_PART) 1413 total_size += size; 1414 } 1415 } 1416 } 1417 } 1418 /* handle % partitions - note %'s don't need to add up to 100! */ 1419 if (total_percent != 0) { 1420 long free_space = lp->d_secperunit - total_size; 1421 if (total_percent > 100) { 1422 fprintf(stderr,"total percentage %lu is greater than 100\n", 1423 total_percent); 1424 errors++; 1425 } 1426 1427 if (free_space > 0) { 1428 for (i = 0; i < lp->d_npartitions; i++) { 1429 pp = &lp->d_partitions[i]; 1430 if (part_set[i] && part_size_type[i] == '%') { 1431 /* careful of overflows! and integer roundoff */ 1432 pp->p_size = ((double)pp->p_size/100) * free_space; 1433 total_size += pp->p_size; 1434 1435 /* FIX we can lose a sector or so due to roundoff per 1436 partition. A more complex algorithm could avoid that */ 1437 } 1438 } 1439 } else { 1440 fprintf(stderr, 1441 "%ld sectors available to give to '*' and '%%' partitions\n", 1442 free_space); 1443 errors++; 1444 /* fix? set all % partitions to size 0? */ 1445 } 1446 } 1447 /* give anything remaining to the hog partition */ 1448 if (hog_part != -1) { 1449 lp->d_partitions[hog_part].p_size = lp->d_secperunit - total_size; 1450 total_size = lp->d_secperunit; 1451 } 1452 1453 /* Now set the offsets for each partition */ 1454 current_offset = 0; /* in sectors */ 1455 seen_default_offset = 0; 1456 for (i = 0; i < lp->d_npartitions; i++) { 1457 part = 'a' + i; 1458 pp = &lp->d_partitions[i]; 1459 if (part_set[i]) { 1460 if (part_offset_type[i] == '*') { 1461 if (i == RAW_PART) { 1462 pp->p_offset = 0; 1463 } else { 1464 pp->p_offset = current_offset; 1465 seen_default_offset = 1; 1466 } 1467 } else { 1468 /* allow them to be out of order for old-style tables */ 1469 if (pp->p_offset < current_offset && 1470 seen_default_offset && i != RAW_PART) { 1471 fprintf(stderr, 1472 "Offset %ld for partition %c overlaps previous partition which ends at %lu\n", 1473 (long)pp->p_offset,i+'a',current_offset); 1474 fprintf(stderr, 1475 "Labels with any *'s for offset must be in ascending order by sector\n"); 1476 errors++; 1477 } else if (pp->p_offset != current_offset && 1478 i != RAW_PART && seen_default_offset) { 1479 /* 1480 * this may give unneeded warnings if 1481 * partitions are out-of-order 1482 */ 1483 Warning( 1484 "Offset %ld for partition %c doesn't match expected value %ld", 1485 (long)pp->p_offset, i + 'a', current_offset); 1486 } 1487 } 1488 if (i != RAW_PART) 1489 current_offset = pp->p_offset + pp->p_size; 1490 } 1491 } 1492 1493 for (i = 0; i < lp->d_npartitions; i++) { 1494 part = 'a' + i; 1495 pp = &lp->d_partitions[i]; 1496 if (pp->p_size == 0 && pp->p_offset != 0) 1497 Warning("partition %c: size 0, but offset %lu", 1498 part, (u_long)pp->p_offset); 1499 #ifdef __sparc64__ 1500 /* See comment in writelabel(). */ 1501 if (pp->p_offset % lp->d_secpercyl != 0) { 1502 fprintf(stderr, "partition %c: does not start on a " 1503 "cylinder boundary!\n", part); 1504 errors++; 1505 } 1506 #endif 1507 #ifdef notdef 1508 if (pp->p_size % lp->d_secpercyl) 1509 Warning("partition %c: size %% cylinder-size != 0", 1510 part); 1511 if (pp->p_offset % lp->d_secpercyl) 1512 Warning("partition %c: offset %% cylinder-size != 0", 1513 part); 1514 #endif 1515 if (pp->p_offset > lp->d_secperunit) { 1516 fprintf(stderr, 1517 "partition %c: offset past end of unit\n", part); 1518 errors++; 1519 } 1520 if (pp->p_offset + pp->p_size > lp->d_secperunit) { 1521 fprintf(stderr, 1522 "partition %c: partition extends past end of unit\n", 1523 part); 1524 errors++; 1525 } 1526 if (i == RAW_PART) 1527 { 1528 if (pp->p_fstype != FS_UNUSED) 1529 Warning("partition %c is not marked as unused!",part); 1530 if (pp->p_offset != 0) 1531 Warning("partition %c doesn't start at 0!",part); 1532 if (pp->p_size != lp->d_secperunit) 1533 Warning("partition %c doesn't cover the whole unit!",part); 1534 1535 if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) || 1536 (pp->p_size != lp->d_secperunit)) { 1537 Warning("An incorrect partition %c may cause problems for " 1538 "standard system utilities",part); 1539 } 1540 } 1541 1542 /* check for overlaps */ 1543 /* this will check for all possible overlaps once and only once */ 1544 for (j = 0; j < i; j++) { 1545 if (j != RAW_PART && i != RAW_PART && 1546 part_set[i] && part_set[j]) { 1547 pp2 = &lp->d_partitions[j]; 1548 if (pp2->p_offset < pp->p_offset + pp->p_size && 1549 (pp2->p_offset + pp2->p_size > pp->p_offset || 1550 pp2->p_offset >= pp->p_offset)) { 1551 fprintf(stderr,"partitions %c and %c overlap!\n", 1552 j + 'a', i + 'a'); 1553 errors++; 1554 } 1555 } 1556 } 1557 } 1558 for (; i < MAXPARTITIONS; i++) { 1559 part = 'a' + i; 1560 pp = &lp->d_partitions[i]; 1561 if (pp->p_size || pp->p_offset) 1562 Warning("unused partition %c: size %d offset %lu", 1563 'a' + i, pp->p_size, (u_long)pp->p_offset); 1564 } 1565 return (errors); 1566 } 1567 1568 /* 1569 * When operating on a "virgin" disk, try getting an initial label 1570 * from the associated device driver. This might work for all device 1571 * drivers that are able to fetch some initial device parameters 1572 * without even having access to a (BSD) disklabel, like SCSI disks, 1573 * most IDE drives, or vn devices. 1574 * 1575 * The device name must be given in its "canonical" form. 1576 */ 1577 struct disklabel * 1578 getvirginlabel(void) 1579 { 1580 static struct disklabel loclab; 1581 struct partition *dp; 1582 char lnamebuf[BBSIZE]; 1583 int f; 1584 u_int secsize, u; 1585 off_t mediasize; 1586 1587 if (dkname[0] == '/') { 1588 warnx("\"auto\" requires the usage of a canonical disk name"); 1589 return (NULL); 1590 } 1591 (void)snprintf(lnamebuf, BBSIZE, "%s%s", _PATH_DEV, dkname); 1592 if ((f = open(lnamebuf, O_RDONLY)) == -1) { 1593 warn("cannot open %s", lnamebuf); 1594 return (NULL); 1595 } 1596 1597 /* New world order */ 1598 if ((ioctl(f, DIOCGMEDIASIZE, &mediasize) != 0) || 1599 (ioctl(f, DIOCGSECTORSIZE, &secsize) != 0)) { 1600 close (f); 1601 return (NULL); 1602 } 1603 memset(&loclab, 0, sizeof loclab); 1604 loclab.d_magic = DISKMAGIC; 1605 loclab.d_magic2 = DISKMAGIC; 1606 loclab.d_secsize = secsize; 1607 loclab.d_secperunit = mediasize / secsize; 1608 1609 /* 1610 * Nobody in these enligthened days uses the CHS geometry for 1611 * anything, but nontheless try to get it right. If we fail 1612 * to get any good ideas from the device, construct something 1613 * which is IBM-PC friendly. 1614 */ 1615 if (ioctl(f, DIOCGFWSECTORS, &u) == 0) 1616 loclab.d_nsectors = u; 1617 else 1618 loclab.d_nsectors = 63; 1619 if (ioctl(f, DIOCGFWHEADS, &u) == 0) 1620 loclab.d_ntracks = u; 1621 else if (loclab.d_secperunit <= 63*1*1024) 1622 loclab.d_ntracks = 1; 1623 else if (loclab.d_secperunit <= 63*16*1024) 1624 loclab.d_ntracks = 16; 1625 else 1626 loclab.d_ntracks = 255; 1627 loclab.d_secpercyl = loclab.d_ntracks * loclab.d_nsectors; 1628 loclab.d_ncylinders = loclab.d_secperunit / loclab.d_secpercyl; 1629 loclab.d_npartitions = MAXPARTITIONS; 1630 1631 /* Various (unneeded) compat stuff */ 1632 loclab.d_rpm = 3600; 1633 loclab.d_bbsize = BBSIZE; 1634 loclab.d_interleave = 1;; 1635 strncpy(loclab.d_typename, "amnesiac", 1636 sizeof(loclab.d_typename)); 1637 1638 dp = &loclab.d_partitions[RAW_PART]; 1639 dp->p_size = loclab.d_secperunit; 1640 loclab.d_checksum = dkcksum(&loclab); 1641 close (f); 1642 return (&loclab); 1643 } 1644 1645 /* 1646 * If we are installing a boot program that doesn't fit in d_bbsize 1647 * we need to mark those partitions that the boot overflows into. 1648 * This allows newfs to prevent creation of a file system where it might 1649 * clobber bootstrap code. 1650 */ 1651 void 1652 setbootflag(struct disklabel *lp) 1653 { 1654 struct partition *pp; 1655 int i, errors = 0; 1656 char part; 1657 u_long boffset; 1658 1659 if (bootbuf == 0) 1660 return; 1661 boffset = bootsize / lp->d_secsize; 1662 for (i = 0; i < lp->d_npartitions; i++) { 1663 part = 'a' + i; 1664 pp = &lp->d_partitions[i]; 1665 if (pp->p_size == 0) 1666 continue; 1667 if (boffset <= pp->p_offset) { 1668 if (pp->p_fstype == FS_BOOT) 1669 pp->p_fstype = FS_UNUSED; 1670 } else if (pp->p_fstype != FS_BOOT) { 1671 if (pp->p_fstype != FS_UNUSED) { 1672 fprintf(stderr, 1673 "boot overlaps used partition %c\n", 1674 part); 1675 errors++; 1676 } else { 1677 pp->p_fstype = FS_BOOT; 1678 Warning("boot overlaps partition %c, %s", 1679 part, "marked as FS_BOOT"); 1680 } 1681 } 1682 } 1683 if (errors) 1684 errx(4, "cannot install boot program"); 1685 } 1686 1687 /*VARARGS1*/ 1688 void 1689 Warning(const char *fmt, ...) 1690 { 1691 va_list ap; 1692 1693 fprintf(stderr, "Warning, "); 1694 va_start(ap, fmt); 1695 vfprintf(stderr, fmt, ap); 1696 fprintf(stderr, "\n"); 1697 va_end(ap); 1698 } 1699 1700 void 1701 usage(void) 1702 { 1703 #if NUMBOOT > 0 1704 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", 1705 "usage: disklabel [-r] disk", 1706 "\t\t(to read label)", 1707 " disklabel -w [-r] [-n] disk type [ packid ]", 1708 "\t\t(to write label with existing boot program)", 1709 " disklabel -e [-r] [-n] disk", 1710 "\t\t(to edit label)", 1711 " disklabel -R [-r] [-n] disk protofile", 1712 "\t\t(to restore label with existing boot program)", 1713 #if NUMBOOT > 1 1714 " disklabel -B [-n] [ -b boot1 [ -s boot2 ] ] disk [ type ]", 1715 "\t\t(to install boot program with existing label)", 1716 " disklabel -w -B [-n] [ -b boot1 [ -s boot2 ] ] disk type [ packid ]", 1717 "\t\t(to write label and boot program)", 1718 " disklabel -R -B [-n] [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]", 1719 "\t\t(to restore label and boot program)", 1720 #else 1721 " disklabel -B [-n] [ -b bootprog ] disk [ type ]", 1722 "\t\t(to install boot program with existing on-disk label)", 1723 " disklabel -w -B [-n] [ -b bootprog ] disk type [ packid ]", 1724 "\t\t(to write label and install boot program)", 1725 " disklabel -R -B [-n] [ -b bootprog ] disk protofile [ type ]", 1726 "\t\t(to restore label and install boot program)", 1727 #endif 1728 " disklabel [-NW] disk", 1729 "\t\t(to write disable/enable label)"); 1730 #else 1731 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", 1732 "usage: disklabel [-r] disk", "(to read label)", 1733 " disklabel -w [-r] [-n] disk type [ packid ]", 1734 "\t\t(to write label)", 1735 " disklabel -e [-r] [-n] disk", 1736 "\t\t(to edit label)", 1737 " disklabel -R [-r] [-n] disk protofile", 1738 "\t\t(to restore label)", 1739 " disklabel [-NW] disk", 1740 "\t\t(to write disable/enable label)"); 1741 #endif 1742 exit(1); 1743 } 1744