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