xref: /freebsd/sbin/bsdlabel/bsdlabel.c (revision e627b39baccd1ec9129690167cf5e6d860509655)
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 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 static char sccsid[] = "@(#)disklabel.c	8.2 (Berkeley) 1/7/94";
45 /* from static char sccsid[] = "@(#)disklabel.c	1.2 (Symmetric) 11/28/85"; */
46 #endif /* not lint */
47 
48 #include <sys/param.h>
49 #include <sys/signal.h>
50 #include <sys/errno.h>
51 #include <sys/file.h>
52 #include <sys/ioctl.h>
53 #include <sys/stat.h>
54 #include <sys/wait.h>
55 #define DKTYPENAMES
56 #include <sys/disklabel.h>
57 #include <ufs/ffs/fs.h>
58 #include <unistd.h>
59 #include <string.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <signal.h>
63 #include <stdarg.h>
64 #include <ctype.h>
65 #include "pathnames.h"
66 
67 /*
68  * Disklabel: read and write disklabels.
69  * The label is usually placed on one of the first sectors of the disk.
70  * Many machines also place a bootstrap in the same area,
71  * in which case the label is embedded in the bootstrap.
72  * The bootstrap source must leave space at the proper offset
73  * for the label on such machines.
74  */
75 
76 #ifdef tahoe
77 #define RAWPARTITION	'a'
78 #else
79 #define RAWPARTITION	'c'
80 #endif
81 
82 #ifndef BBSIZE
83 #define	BBSIZE	8192			/* size of boot area, with label */
84 #endif
85 
86 #ifdef tahoe
87 #define	NUMBOOT	0
88 #else
89 #if defined(hp300) || defined(hp800)
90 #define	NUMBOOT	1
91 #else
92 #define	NUMBOOT	2
93 #endif
94 #endif
95 
96 void	makelabel	__P((char *, char *, struct disklabel *));
97 int	writelabel	__P((int, char *, struct disklabel *));
98 void	l_perror	__P((char *));
99 struct disklabel * readlabel __P((int));
100 struct disklabel * makebootarea __P((char *, struct disklabel *, int));
101 void	display		__P((FILE *, struct disklabel *));
102 int	edit		__P((struct disklabel *, int));
103 int	editit		__P((void));
104 char *	skip		__P((char *));
105 char *	word		__P((char *));
106 int	getasciilabel	__P((FILE *, struct disklabel *));
107 int	checklabel	__P((struct disklabel *));
108 void	setbootflag	__P((struct disklabel *));
109 void	Warning		(char *, ...);
110 void	Perror		__P((char *));
111 void	usage		__P((void));
112 extern	u_short dkcksum __P((struct disklabel *));
113 struct disklabel * getvirginlabel __P((void));
114 
115 #define	DEFEDITOR	_PATH_VI
116 #define	streq(a,b)	(strcmp(a,b) == 0)
117 
118 char	*dkname;
119 char	*specname;
120 char	tmpfil[] = _PATH_TMP;
121 
122 extern	int errno;
123 char	namebuf[BBSIZE], *np = namebuf;
124 struct	disklabel lab;
125 struct	disklabel *readlabel(), *makebootarea();
126 char	bootarea[BBSIZE];
127 
128 #if NUMBOOT > 0
129 int	installboot;	/* non-zero if we should install a boot program */
130 char	*bootbuf;	/* pointer to buffer with remainder of boot prog */
131 int	bootsize;	/* size of remaining boot program */
132 char	*xxboot;	/* primary boot */
133 char	*bootxx;	/* secondary boot */
134 char	boot0[MAXPATHLEN];
135 char	boot1[MAXPATHLEN];
136 #endif
137 
138 enum	{
139 	UNSPEC, EDIT, NOWRITE, READ, RESTORE, WRITE, WRITEABLE, WRITEBOOT
140 } op = UNSPEC;
141 
142 int	rflag;
143 
144 #ifdef DEBUG
145 int	debug;
146 #define OPTIONS	"BNRWb:ders:w"
147 #else
148 #define OPTIONS	"BNRWb:ers:w"
149 #endif
150 
151 int
152 main(argc, argv)
153 	int argc;
154 	char *argv[];
155 {
156 	extern char *optarg;
157 	extern int optind;
158 	register struct disklabel *lp;
159 	FILE *t;
160 	int ch, f, flag, error = 0;
161 	char *name = 0;
162 
163 	while ((ch = getopt(argc, argv, OPTIONS)) != EOF)
164 		switch (ch) {
165 #if NUMBOOT > 0
166 			case 'B':
167 				++installboot;
168 				break;
169 			case 'b':
170 				xxboot = optarg;
171 				break;
172 #if NUMBOOT > 1
173 			case 's':
174 				bootxx = optarg;
175 				break;
176 #endif
177 #endif
178 			case 'N':
179 				if (op != UNSPEC)
180 					usage();
181 				op = NOWRITE;
182 				break;
183 			case 'R':
184 				if (op != UNSPEC)
185 					usage();
186 				op = RESTORE;
187 				break;
188 			case 'W':
189 				if (op != UNSPEC)
190 					usage();
191 				op = WRITEABLE;
192 				break;
193 			case 'e':
194 				if (op != UNSPEC)
195 					usage();
196 				op = EDIT;
197 				break;
198 			case 'r':
199 				++rflag;
200 				break;
201 			case 'w':
202 				if (op != UNSPEC)
203 					usage();
204 				op = WRITE;
205 				break;
206 #ifdef DEBUG
207 			case 'd':
208 				debug++;
209 				break;
210 #endif
211 			case '?':
212 			default:
213 				usage();
214 		}
215 	argc -= optind;
216 	argv += optind;
217 #if NUMBOOT > 0
218 	if (installboot) {
219 		rflag++;
220 		if (op == UNSPEC)
221 			op = WRITEBOOT;
222 	} else {
223 		if (op == UNSPEC)
224 			op = READ;
225 		xxboot = bootxx = 0;
226 	}
227 #else
228 	if (op == UNSPEC)
229 		op = READ;
230 #endif
231 	if (argc < 1)
232 		usage();
233 
234 	dkname = argv[0];
235 	if (dkname[0] != '/') {
236 		(void)sprintf(np, "%sr%s%c", _PATH_DEV, dkname, RAWPARTITION);
237 		specname = np;
238 		np += strlen(specname) + 1;
239 	} else
240 		specname = dkname;
241 	f = open(specname, op == READ ? O_RDONLY : O_RDWR);
242 	if (f < 0 && errno == ENOENT && dkname[0] != '/') {
243 		(void)sprintf(specname, "%sr%s", _PATH_DEV, dkname);
244 		np = namebuf + strlen(specname) + 1;
245 		f = open(specname, op == READ ? O_RDONLY : O_RDWR);
246 	}
247 	if (f < 0)
248 		Perror(specname);
249 
250 	switch(op) {
251 
252 	case EDIT:
253 		if (argc != 1)
254 			usage();
255 		lp = readlabel(f);
256 		error = edit(lp, f);
257 		break;
258 
259 	case NOWRITE:
260 		flag = 0;
261 		if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
262 			Perror("ioctl DIOCWLABEL");
263 		break;
264 
265 	case READ:
266 		if (argc != 1)
267 			usage();
268 		lp = readlabel(f);
269 		display(stdout, lp);
270 		error = checklabel(lp);
271 		break;
272 
273 	case RESTORE:
274 #if NUMBOOT > 0
275 		if (installboot && argc == 3) {
276 			makelabel(argv[2], 0, &lab);
277 			argc--;
278 		}
279 #endif
280 		if (argc != 2)
281 			usage();
282 		lp = makebootarea(bootarea, &lab, f);
283 		if (!(t = fopen(argv[1], "r")))
284 			Perror(argv[1]);
285 		if (getasciilabel(t, lp))
286 			error = writelabel(f, bootarea, lp);
287 		break;
288 
289 	case WRITE:
290 		if (argc == 3) {
291 			name = argv[2];
292 			argc--;
293 		}
294 		if (argc != 2)
295 			usage();
296 		makelabel(argv[1], name, &lab);
297 		lp = makebootarea(bootarea, &lab, f);
298 		*lp = lab;
299 		if (checklabel(lp) == 0)
300 			error = writelabel(f, bootarea, lp);
301 		break;
302 
303 	case WRITEABLE:
304 		flag = 1;
305 		if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
306 			Perror("ioctl DIOCWLABEL");
307 		break;
308 
309 #if NUMBOOT > 0
310 	case WRITEBOOT:
311 	{
312 		struct disklabel tlab;
313 
314 		lp = readlabel(f);
315 		tlab = *lp;
316 		if (argc == 2)
317 			makelabel(argv[1], 0, &lab);
318 		lp = makebootarea(bootarea, &lab, f);
319 		*lp = tlab;
320 		if (checklabel(lp) == 0)
321 			error = writelabel(f, bootarea, lp);
322 		break;
323 	}
324 #endif
325 	}
326 	exit(error);
327 }
328 
329 /*
330  * Construct a prototype disklabel from /etc/disktab.  As a side
331  * effect, set the names of the primary and secondary boot files
332  * if specified.
333  */
334 void
335 makelabel(type, name, lp)
336 	char *type, *name;
337 	register struct disklabel *lp;
338 {
339 	register struct disklabel *dp;
340 
341 	if (strcmp(type, "auto") == 0)
342 		dp = getvirginlabel();
343 	else
344 		dp = getdiskbyname(type);
345 	if (dp == NULL) {
346 		fprintf(stderr, "%s: unknown disk type\n", type);
347 		exit(1);
348 	}
349 	*lp = *dp;
350 #if NUMBOOT > 0
351 	/*
352 	 * Set bootstrap name(s).
353 	 * 1. If set from command line, use those,
354 	 * 2. otherwise, check if disktab specifies them (b0 or b1),
355 	 * 3. otherwise, makebootarea() will choose ones based on the name
356 	 *    of the disk special file. E.g. /dev/ra0 -> raboot, bootra
357 	 */
358 	if (!xxboot && lp->d_boot0) {
359 		if (*lp->d_boot0 != '/')
360 			(void)sprintf(boot0, "%s/%s",
361 				      _PATH_BOOTDIR, lp->d_boot0);
362 		else
363 			(void)strcpy(boot0, lp->d_boot0);
364 		xxboot = boot0;
365 	}
366 #if NUMBOOT > 1
367 	if (!bootxx && lp->d_boot1) {
368 		if (*lp->d_boot1 != '/')
369 			(void)sprintf(boot1, "%s/%s",
370 				      _PATH_BOOTDIR, lp->d_boot1);
371 		else
372 			(void)strcpy(boot1, lp->d_boot1);
373 		bootxx = boot1;
374 	}
375 #endif
376 #endif
377 	/* d_packname is union d_boot[01], so zero */
378 	bzero(lp->d_packname, sizeof(lp->d_packname));
379 	if (name)
380 		(void)strncpy(lp->d_packname, name, sizeof(lp->d_packname));
381 }
382 
383 int
384 writelabel(f, boot, lp)
385 	int f;
386 	char *boot;
387 	register struct disklabel *lp;
388 {
389 #ifdef vax
390 	register int i;
391 #endif
392 	int flag;
393 
394 	setbootflag(lp);
395 	lp->d_magic = DISKMAGIC;
396 	lp->d_magic2 = DISKMAGIC;
397 	lp->d_checksum = 0;
398 	lp->d_checksum = dkcksum(lp);
399 	if (rflag) {
400 		/*
401 		 * First set the kernel disk label,
402 		 * then write a label to the raw disk.
403 		 * If the SDINFO ioctl fails because it is unimplemented,
404 		 * keep going; otherwise, the kernel consistency checks
405 		 * may prevent us from changing the current (in-core)
406 		 * label.
407 		 */
408 		if (ioctl(f, DIOCSDINFO, lp) < 0 &&
409 		    errno != ENODEV && errno != ENOTTY) {
410 			l_perror("ioctl DIOCSDINFO");
411 			return (1);
412 		}
413 		(void)lseek(f, (off_t)0, SEEK_SET);
414 		/*
415 		 * write enable label sector before write (if necessary),
416 		 * disable after writing.
417 		 */
418 		flag = 1;
419 		if (ioctl(f, DIOCWLABEL, &flag) < 0)
420 			perror("ioctl DIOCWLABEL");
421 		if (write(f, boot, lp->d_bbsize) != lp->d_bbsize) {
422 			perror("write");
423 			return (1);
424 		}
425 #if NUMBOOT > 0
426 		/*
427 		 * Output the remainder of the disklabel
428 		 */
429 		if (bootbuf && write(f, bootbuf, bootsize) != bootsize) {
430 			perror("write");
431 			return(1);
432 		}
433 #endif
434 		flag = 0;
435 		(void) ioctl(f, DIOCWLABEL, &flag);
436 	} else if (ioctl(f, DIOCWDINFO, lp) < 0) {
437 		l_perror("ioctl DIOCWDINFO");
438 		return (1);
439 	}
440 #ifdef vax
441 	if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
442 		daddr_t alt;
443 
444 		alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
445 		for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
446 			(void)lseek(f, (off_t)((alt + i) * lp->d_secsize),
447 			    SEEK_SET);
448 			if (write(f, boot, lp->d_secsize) < lp->d_secsize) {
449 				int oerrno = errno;
450 				fprintf(stderr, "alternate label %d ", i/2);
451 				errno = oerrno;
452 				perror("write");
453 			}
454 		}
455 	}
456 #endif
457 	return (0);
458 }
459 
460 void
461 l_perror(s)
462 	char *s;
463 {
464 	int saverrno = errno;
465 
466 	fprintf(stderr, "disklabel: %s: ", s);
467 
468 	switch (saverrno) {
469 
470 	case ESRCH:
471 		fprintf(stderr, "No disk label on disk;\n");
472 		fprintf(stderr,
473 		    "use \"disklabel -r\" to install initial label\n");
474 		break;
475 
476 	case EINVAL:
477 		fprintf(stderr, "Label magic number or checksum is wrong!\n");
478 		fprintf(stderr, "(disklabel or kernel is out of date?)\n");
479 		break;
480 
481 	case EBUSY:
482 		fprintf(stderr, "Open partition would move or shrink\n");
483 		break;
484 
485 	case EXDEV:
486 		fprintf(stderr,
487 	"Labeled partition or 'a' partition must start at beginning of disk\n");
488 		break;
489 
490 	default:
491 		errno = saverrno;
492 		perror((char *)NULL);
493 		break;
494 	}
495 }
496 
497 /*
498  * Fetch disklabel for disk.
499  * Use ioctl to get label unless -r flag is given.
500  */
501 struct disklabel *
502 readlabel(f)
503 	int f;
504 {
505 	register struct disklabel *lp;
506 
507 	if (rflag) {
508 		if (read(f, bootarea, BBSIZE) < BBSIZE)
509 			Perror(specname);
510 		for (lp = (struct disklabel *)bootarea;
511 		    lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp));
512 		    lp = (struct disklabel *)((char *)lp + 16))
513 			if (lp->d_magic == DISKMAGIC &&
514 			    lp->d_magic2 == DISKMAGIC)
515 				break;
516 		if (lp > (struct disklabel *)(bootarea+BBSIZE-sizeof(*lp)) ||
517 		    lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC ||
518 		    dkcksum(lp) != 0) {
519 			fprintf(stderr,
520 	"Bad pack magic number (label is damaged, or pack is unlabeled)\n");
521 			/* lp = (struct disklabel *)(bootarea + LABELOFFSET); */
522 			exit (1);
523 		}
524 	} else {
525 		lp = &lab;
526 		if (ioctl(f, DIOCGDINFO, lp) < 0)
527 			Perror("ioctl DIOCGDINFO");
528 	}
529 	return (lp);
530 }
531 
532 /*
533  * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot''
534  * Returns a pointer to the disklabel portion of the bootarea.
535  */
536 struct disklabel *
537 makebootarea(boot, dp, f)
538 	char *boot;
539 	register struct disklabel *dp;
540 	int f;
541 {
542 	struct disklabel *lp;
543 	register char *p;
544 	int b;
545 #if NUMBOOT > 0
546 	char *dkbasename;
547 #if NUMBOOT == 1
548 	struct stat sb;
549 #endif
550 #ifdef __i386__
551 	char *tmpbuf;
552 	int i, found;
553 #endif /* i386 */
554 #endif
555 
556 	/* XXX */
557 	if (dp->d_secsize == 0) {
558 		dp->d_secsize = DEV_BSIZE;
559 		dp->d_bbsize = BBSIZE;
560 	}
561 	lp = (struct disklabel *)
562 		(boot + (LABELSECTOR * dp->d_secsize) + LABELOFFSET);
563 	bzero((char *)lp, sizeof *lp);
564 #if NUMBOOT > 0
565 	/*
566 	 * If we are not installing a boot program but we are installing a
567 	 * label on disk then we must read the current bootarea so we don't
568 	 * clobber the existing boot.
569 	 */
570 	if (!installboot) {
571 		if (rflag) {
572 			if (read(f, boot, BBSIZE) < BBSIZE)
573 				Perror(specname);
574 			bzero((char *)lp, sizeof *lp);
575 		}
576 		return (lp);
577 	}
578 	/*
579 	 * We are installing a boot program.  Determine the name(s) and
580 	 * read them into the appropriate places in the boot area.
581 	 */
582 	if (!xxboot || !bootxx) {
583 		dkbasename = np;
584 		if ((p = rindex(dkname, '/')) == NULL)
585 			p = dkname;
586 		else
587 			p++;
588 		while (*p && !isdigit(*p))
589 			*np++ = *p++;
590 		*np++ = '\0';
591 
592 		if (!xxboot) {
593 			(void)sprintf(np, "%s/%sboot",
594 				      _PATH_BOOTDIR, dkbasename);
595 			if (access(np, F_OK) < 0 && dkbasename[0] == 'r')
596 				dkbasename++;
597 			xxboot = np;
598 			(void)sprintf(xxboot, "%s/%sboot",
599 				      _PATH_BOOTDIR, dkbasename);
600 			np += strlen(xxboot) + 1;
601 		}
602 #if NUMBOOT > 1
603 		if (!bootxx) {
604 			(void)sprintf(np, "%s/boot%s",
605 				      _PATH_BOOTDIR, dkbasename);
606 			if (access(np, F_OK) < 0 && dkbasename[0] == 'r')
607 				dkbasename++;
608 			bootxx = np;
609 			(void)sprintf(bootxx, "%s/boot%s",
610 				      _PATH_BOOTDIR, dkbasename);
611 			np += strlen(bootxx) + 1;
612 		}
613 #endif
614 	}
615 #ifdef DEBUG
616 	if (debug)
617 		fprintf(stderr, "bootstraps: xxboot = %s, bootxx = %s\n",
618 			xxboot, bootxx ? bootxx : "NONE");
619 #endif
620 
621 	/*
622 	 * Strange rules:
623 	 * 1. One-piece bootstrap (hp300/hp800)
624 	 *	up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest
625 	 *	is remembered and written later following the bootarea.
626 	 * 2. Two-piece bootstraps (vax/i386?/mips?)
627 	 *	up to d_secsize bytes of ``xxboot'' go in first d_secsize
628 	 *	bytes of bootarea, remaining d_bbsize-d_secsize filled
629 	 *	from ``bootxx''.
630 	 */
631 	b = open(xxboot, O_RDONLY);
632 	if (b < 0)
633 		Perror(xxboot);
634 #if NUMBOOT > 1
635 #ifdef __i386__
636 	/*
637 	 * XXX Botch alert.
638 	 * The i386 has the so-called fdisk table embedded into the
639 	 * primary bootstrap.  We take care to not clobber it, but
640 	 * only if it does already contain some data.  (Otherwise,
641 	 * the xxboot provides a template.)
642 	 */
643 	if ((tmpbuf = (char *)malloc((int)dp->d_secsize)) == 0)
644 		Perror(xxboot);
645 	memcpy((void *)tmpbuf, (void *)boot, (int)dp->d_secsize);
646 #endif /* i386 */
647 	if (read(b, boot, (int)dp->d_secsize) < 0)
648 		Perror(xxboot);
649 	(void)close(b);
650 #ifdef __i386__
651 	for (i = DOSPARTOFF, found = 0;
652 	     !found && i < DOSPARTOFF + NDOSPART*sizeof(struct dos_partition);
653 	     i++)
654 		found = tmpbuf[i] != 0;
655 	if (found)
656 		memcpy((void *)&boot[DOSPARTOFF],
657 		       (void *)&tmpbuf[DOSPARTOFF],
658 		       NDOSPART * sizeof(struct dos_partition));
659 	free(tmpbuf);
660 #endif /* i386 */
661 	b = open(bootxx, O_RDONLY);
662 	if (b < 0)
663 		Perror(bootxx);
664 	if (read(b, &boot[dp->d_secsize],
665 		 (int)(dp->d_bbsize-dp->d_secsize)) < 0)
666 		Perror(bootxx);
667 #else
668 	if (read(b, boot, (int)dp->d_bbsize) < 0)
669 		Perror(xxboot);
670 	(void)fstat(b, &sb);
671 	bootsize = (int)sb.st_size - dp->d_bbsize;
672 	if (bootsize > 0) {
673 		/* XXX assume d_secsize is a power of two */
674 		bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1);
675 		bootbuf = (char *)malloc((size_t)bootsize);
676 		if (bootbuf == 0)
677 			Perror(xxboot);
678 		if (read(b, bootbuf, bootsize) < 0) {
679 			free(bootbuf);
680 			Perror(xxboot);
681 		}
682 	}
683 #endif
684 	(void)close(b);
685 #endif
686 	/*
687 	 * Make sure no part of the bootstrap is written in the area
688 	 * reserved for the label.
689 	 */
690 	for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++)
691 		if (*p) {
692 			fprintf(stderr,
693 			    "Bootstrap doesn't leave room for disk label\n");
694 			exit(2);
695 		}
696 	return (lp);
697 }
698 
699 void
700 display(f, lp)
701 	FILE *f;
702 	register struct disklabel *lp;
703 {
704 	register int i, j;
705 	register struct partition *pp;
706 
707 	fprintf(f, "# %s:\n", specname);
708 	if ((unsigned) lp->d_type < DKMAXTYPES)
709 		fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
710 	else
711 		fprintf(f, "type: %d\n", lp->d_type);
712 	fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
713 		lp->d_typename);
714 	fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
715 		lp->d_packname);
716 	fprintf(f, "flags:");
717 	if (lp->d_flags & D_REMOVABLE)
718 		fprintf(f, " removeable");
719 	if (lp->d_flags & D_ECC)
720 		fprintf(f, " ecc");
721 	if (lp->d_flags & D_BADSECT)
722 		fprintf(f, " badsect");
723 	fprintf(f, "\n");
724 	fprintf(f, "bytes/sector: %ld\n", lp->d_secsize);
725 	fprintf(f, "sectors/track: %ld\n", lp->d_nsectors);
726 	fprintf(f, "tracks/cylinder: %ld\n", lp->d_ntracks);
727 	fprintf(f, "sectors/cylinder: %ld\n", lp->d_secpercyl);
728 	fprintf(f, "cylinders: %ld\n", lp->d_ncylinders);
729 	fprintf(f, "sectors/unit: %ld\n", lp->d_secperunit);
730 	fprintf(f, "rpm: %d\n", lp->d_rpm);
731 	fprintf(f, "interleave: %d\n", lp->d_interleave);
732 	fprintf(f, "trackskew: %d\n", lp->d_trackskew);
733 	fprintf(f, "cylinderskew: %d\n", lp->d_cylskew);
734 	fprintf(f, "headswitch: %ld\t\t# milliseconds\n", lp->d_headswitch);
735 	fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
736 		lp->d_trkseek);
737 	fprintf(f, "drivedata: ");
738 	for (i = NDDATA - 1; i >= 0; i--)
739 		if (lp->d_drivedata[i])
740 			break;
741 	if (i < 0)
742 		i = 0;
743 	for (j = 0; j <= i; j++)
744 		fprintf(f, "%ld ", lp->d_drivedata[j]);
745 	fprintf(f, "\n\n%d partitions:\n", lp->d_npartitions);
746 	fprintf(f,
747 	    "#        size   offset    fstype   [fsize bsize bps/cpg]\n");
748 	pp = lp->d_partitions;
749 	for (i = 0; i < lp->d_npartitions; i++, pp++) {
750 		if (pp->p_size) {
751 			fprintf(f, "  %c: %8ld %8ld  ", 'a' + i,
752 			   pp->p_size, pp->p_offset);
753 			if ((unsigned) pp->p_fstype < FSMAXTYPES)
754 				fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
755 			else
756 				fprintf(f, "%8d", pp->p_fstype);
757 			switch (pp->p_fstype) {
758 
759 			case FS_UNUSED:				/* XXX */
760 				fprintf(f, "    %5ld %5ld %5.5s ",
761 				    pp->p_fsize, pp->p_fsize * pp->p_frag, "");
762 				break;
763 
764 			case FS_BSDFFS:
765 				fprintf(f, "    %5ld %5ld %5d ",
766 				    pp->p_fsize, pp->p_fsize * pp->p_frag,
767 				    pp->p_cpg);
768 				break;
769 
770 			case FS_BSDLFS:
771 				fprintf(f, "    %5ld %5ld %5d",
772 				    pp->p_fsize, pp->p_fsize * pp->p_frag,
773 				    pp->p_cpg);
774 				break;
775 
776 			default:
777 				fprintf(f, "%20.20s", "");
778 				break;
779 			}
780 			fprintf(f, "\t# (Cyl. %4ld",
781 			    pp->p_offset / lp->d_secpercyl);
782 			if (pp->p_offset % lp->d_secpercyl)
783 			    putc('*', f);
784 			else
785 			    putc(' ', f);
786 			fprintf(f, "- %ld",
787 			    (pp->p_offset +
788 			    pp->p_size + lp->d_secpercyl - 1) /
789 			    lp->d_secpercyl - 1);
790 			if (pp->p_size % lp->d_secpercyl)
791 			    putc('*', f);
792 			fprintf(f, ")\n");
793 		}
794 	}
795 	fflush(f);
796 }
797 
798 int
799 edit(lp, f)
800 	struct disklabel *lp;
801 	int f;
802 {
803 	register int c;
804 	struct disklabel label;
805 	FILE *fd;
806 
807 	(void) mktemp(tmpfil);
808 	fd = fopen(tmpfil, "w");
809 	if (fd == NULL) {
810 		fprintf(stderr, "%s: Can't create\n", tmpfil);
811 		return (1);
812 	}
813 	(void)fchmod(fileno(fd), 0600);
814 	display(fd, lp);
815 	fclose(fd);
816 	for (;;) {
817 		if (!editit())
818 			break;
819 		fd = fopen(tmpfil, "r");
820 		if (fd == NULL) {
821 			fprintf(stderr, "%s: Can't reopen for reading\n",
822 				tmpfil);
823 			break;
824 		}
825 		bzero((char *)&label, sizeof(label));
826 		if (getasciilabel(fd, &label)) {
827 			*lp = label;
828 			if (writelabel(f, bootarea, lp) == 0) {
829 				(void) unlink(tmpfil);
830 				return (0);
831 			}
832 		}
833 		printf("re-edit the label? [y]: "); fflush(stdout);
834 		c = getchar();
835 		if (c != EOF && c != (int)'\n')
836 			while (getchar() != (int)'\n')
837 				;
838 		if  (c == (int)'n')
839 			break;
840 	}
841 	(void) unlink(tmpfil);
842 	return (1);
843 }
844 
845 int
846 editit()
847 {
848 	register int pid, xpid;
849 	int stat, omask;
850 	extern char *getenv();
851 
852 	omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
853 	while ((pid = fork()) < 0) {
854 		extern int errno;
855 
856 		if (errno == EPROCLIM) {
857 			fprintf(stderr, "You have too many processes\n");
858 			return(0);
859 		}
860 		if (errno != EAGAIN) {
861 			perror("fork");
862 			return(0);
863 		}
864 		sleep(1);
865 	}
866 	if (pid == 0) {
867 		register char *ed;
868 
869 		sigsetmask(omask);
870 		setgid(getgid());
871 		setuid(getuid());
872 		if ((ed = getenv("EDITOR")) == (char *)0)
873 			ed = DEFEDITOR;
874 		execlp(ed, ed, tmpfil, 0);
875 		perror(ed);
876 		exit(1);
877 	}
878 	while ((xpid = wait(&stat)) >= 0)
879 		if (xpid == pid)
880 			break;
881 	sigsetmask(omask);
882 	return(!stat);
883 }
884 
885 char *
886 skip(cp)
887 	register char *cp;
888 {
889 
890 	while (*cp != '\0' && isspace(*cp))
891 		cp++;
892 	if (*cp == '\0' || *cp == '#')
893 		return ((char *)NULL);
894 	return (cp);
895 }
896 
897 char *
898 word(cp)
899 	register char *cp;
900 {
901 	register char c;
902 
903 	while (*cp != '\0' && !isspace(*cp) && *cp != '#')
904 		cp++;
905 	if ((c = *cp) != '\0') {
906 		*cp++ = '\0';
907 		if (c != '#')
908 			return (skip(cp));
909 	}
910 	return ((char *)NULL);
911 }
912 
913 /*
914  * Read an ascii label in from fd f,
915  * in the same format as that put out by display(),
916  * and fill in lp.
917  */
918 int
919 getasciilabel(f, lp)
920 	FILE	*f;
921 	register struct disklabel *lp;
922 {
923 	register char **cpp, *cp;
924 	register struct partition *pp;
925 	char *tp, *s, line[BUFSIZ];
926 	int v, lineno = 0, errors = 0;
927 
928 	lp->d_bbsize = BBSIZE;				/* XXX */
929 	lp->d_sbsize = SBSIZE;				/* XXX */
930 	while (fgets(line, sizeof(line) - 1, f)) {
931 		lineno++;
932 		if ((cp = index(line,'\n')) != 0)
933 			*cp = '\0';
934 		cp = skip(line);
935 		if (cp == NULL)
936 			continue;
937 		tp = index(cp, ':');
938 		if (tp == NULL) {
939 			fprintf(stderr, "line %d: syntax error\n", lineno);
940 			errors++;
941 			continue;
942 		}
943 		*tp++ = '\0', tp = skip(tp);
944 		if (streq(cp, "type")) {
945 			if (tp == NULL)
946 				tp = "unknown";
947 			cpp = dktypenames;
948 			for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
949 				if ((s = *cpp) && streq(s, tp)) {
950 					lp->d_type = cpp - dktypenames;
951 					goto next;
952 				}
953 			v = atoi(tp);
954 			if ((unsigned)v >= DKMAXTYPES)
955 				fprintf(stderr, "line %d:%s %d\n", lineno,
956 				    "Warning, unknown disk type", v);
957 			lp->d_type = v;
958 			continue;
959 		}
960 		if (streq(cp, "flags")) {
961 			for (v = 0; (cp = tp) && *cp != '\0';) {
962 				tp = word(cp);
963 				if (streq(cp, "removeable"))
964 					v |= D_REMOVABLE;
965 				else if (streq(cp, "ecc"))
966 					v |= D_ECC;
967 				else if (streq(cp, "badsect"))
968 					v |= D_BADSECT;
969 				else {
970 					fprintf(stderr,
971 					    "line %d: %s: bad flag\n",
972 					    lineno, cp);
973 					errors++;
974 				}
975 			}
976 			lp->d_flags = v;
977 			continue;
978 		}
979 		if (streq(cp, "drivedata")) {
980 			register int i;
981 
982 			for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
983 				lp->d_drivedata[i++] = atoi(cp);
984 				tp = word(cp);
985 			}
986 			continue;
987 		}
988 		if (sscanf(cp, "%d partitions", &v) == 1) {
989 			if (v == 0 || (unsigned)v > MAXPARTITIONS) {
990 				fprintf(stderr,
991 				    "line %d: bad # of partitions\n", lineno);
992 				lp->d_npartitions = MAXPARTITIONS;
993 				errors++;
994 			} else
995 				lp->d_npartitions = v;
996 			continue;
997 		}
998 		if (tp == NULL)
999 			tp = "";
1000 		if (streq(cp, "disk")) {
1001 			strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
1002 			continue;
1003 		}
1004 		if (streq(cp, "label")) {
1005 			strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
1006 			continue;
1007 		}
1008 		if (streq(cp, "bytes/sector")) {
1009 			v = atoi(tp);
1010 			if (v <= 0 || (v % 512) != 0) {
1011 				fprintf(stderr,
1012 				    "line %d: %s: bad sector size\n",
1013 				    lineno, tp);
1014 				errors++;
1015 			} else
1016 				lp->d_secsize = v;
1017 			continue;
1018 		}
1019 		if (streq(cp, "sectors/track")) {
1020 			v = atoi(tp);
1021 			if (v <= 0) {
1022 				fprintf(stderr, "line %d: %s: bad %s\n",
1023 				    lineno, tp, cp);
1024 				errors++;
1025 			} else
1026 				lp->d_nsectors = v;
1027 			continue;
1028 		}
1029 		if (streq(cp, "sectors/cylinder")) {
1030 			v = atoi(tp);
1031 			if (v <= 0) {
1032 				fprintf(stderr, "line %d: %s: bad %s\n",
1033 				    lineno, tp, cp);
1034 				errors++;
1035 			} else
1036 				lp->d_secpercyl = v;
1037 			continue;
1038 		}
1039 		if (streq(cp, "tracks/cylinder")) {
1040 			v = atoi(tp);
1041 			if (v <= 0) {
1042 				fprintf(stderr, "line %d: %s: bad %s\n",
1043 				    lineno, tp, cp);
1044 				errors++;
1045 			} else
1046 				lp->d_ntracks = v;
1047 			continue;
1048 		}
1049 		if (streq(cp, "cylinders")) {
1050 			v = atoi(tp);
1051 			if (v <= 0) {
1052 				fprintf(stderr, "line %d: %s: bad %s\n",
1053 				    lineno, tp, cp);
1054 				errors++;
1055 			} else
1056 				lp->d_ncylinders = v;
1057 			continue;
1058 		}
1059 		if (streq(cp, "sectors/unit")) {
1060 			v = atoi(tp);
1061 			if (v <= 0) {
1062 				fprintf(stderr, "line %d: %s: bad %s\n",
1063 				    lineno, tp, cp);
1064 				errors++;
1065 			} else
1066 				lp->d_secperunit = v;
1067 			continue;
1068 		}
1069 		if (streq(cp, "rpm")) {
1070 			v = atoi(tp);
1071 			if (v <= 0) {
1072 				fprintf(stderr, "line %d: %s: bad %s\n",
1073 				    lineno, tp, cp);
1074 				errors++;
1075 			} else
1076 				lp->d_rpm = v;
1077 			continue;
1078 		}
1079 		if (streq(cp, "interleave")) {
1080 			v = atoi(tp);
1081 			if (v <= 0) {
1082 				fprintf(stderr, "line %d: %s: bad %s\n",
1083 				    lineno, tp, cp);
1084 				errors++;
1085 			} else
1086 				lp->d_interleave = v;
1087 			continue;
1088 		}
1089 		if (streq(cp, "trackskew")) {
1090 			v = atoi(tp);
1091 			if (v < 0) {
1092 				fprintf(stderr, "line %d: %s: bad %s\n",
1093 				    lineno, tp, cp);
1094 				errors++;
1095 			} else
1096 				lp->d_trackskew = v;
1097 			continue;
1098 		}
1099 		if (streq(cp, "cylinderskew")) {
1100 			v = atoi(tp);
1101 			if (v < 0) {
1102 				fprintf(stderr, "line %d: %s: bad %s\n",
1103 				    lineno, tp, cp);
1104 				errors++;
1105 			} else
1106 				lp->d_cylskew = v;
1107 			continue;
1108 		}
1109 		if (streq(cp, "headswitch")) {
1110 			v = atoi(tp);
1111 			if (v < 0) {
1112 				fprintf(stderr, "line %d: %s: bad %s\n",
1113 				    lineno, tp, cp);
1114 				errors++;
1115 			} else
1116 				lp->d_headswitch = v;
1117 			continue;
1118 		}
1119 		if (streq(cp, "track-to-track seek")) {
1120 			v = atoi(tp);
1121 			if (v < 0) {
1122 				fprintf(stderr, "line %d: %s: bad %s\n",
1123 				    lineno, tp, cp);
1124 				errors++;
1125 			} else
1126 				lp->d_trkseek = v;
1127 			continue;
1128 		}
1129 		if ('a' <= *cp && *cp <= 'z' && cp[1] == '\0') {
1130 			unsigned part = *cp - 'a';
1131 
1132 			if (part > lp->d_npartitions) {
1133 				fprintf(stderr,
1134 				    "line %d: bad partition name\n", lineno);
1135 				errors++;
1136 				continue;
1137 			}
1138 			pp = &lp->d_partitions[part];
1139 #define NXTNUM(n) { \
1140 	if (tp == NULL) { \
1141 		fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1142 		errors++; \
1143 		break; \
1144 	} else { \
1145 		cp = tp, tp = word(cp); \
1146 		if (tp == NULL) \
1147 			tp = cp; \
1148 		(n) = atoi(cp); \
1149 	} \
1150      }
1151 
1152 			NXTNUM(v);
1153 			if (v < 0) {
1154 				fprintf(stderr,
1155 				    "line %d: %s: bad partition size\n",
1156 				    lineno, cp);
1157 				errors++;
1158 			} else
1159 				pp->p_size = v;
1160 			NXTNUM(v);
1161 			if (v < 0) {
1162 				fprintf(stderr,
1163 				    "line %d: %s: bad partition offset\n",
1164 				    lineno, cp);
1165 				errors++;
1166 			} else
1167 				pp->p_offset = v;
1168 			cp = tp, tp = word(cp);
1169 			cpp = fstypenames;
1170 			for (; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1171 				if ((s = *cpp) && streq(s, cp)) {
1172 					pp->p_fstype = cpp - fstypenames;
1173 					goto gottype;
1174 				}
1175 			if (isdigit(*cp))
1176 				v = atoi(cp);
1177 			else
1178 				v = FSMAXTYPES;
1179 			if ((unsigned)v >= FSMAXTYPES) {
1180 				fprintf(stderr, "line %d: %s %s\n", lineno,
1181 				    "Warning, unknown filesystem type", cp);
1182 				v = FS_UNUSED;
1183 			}
1184 			pp->p_fstype = v;
1185 	gottype:
1186 
1187 			switch (pp->p_fstype) {
1188 
1189 			case FS_UNUSED:				/* XXX */
1190 				NXTNUM(pp->p_fsize);
1191 				if (pp->p_fsize == 0)
1192 					break;
1193 				NXTNUM(v);
1194 				pp->p_frag = v / pp->p_fsize;
1195 				break;
1196 
1197 			case FS_BSDFFS:
1198 				NXTNUM(pp->p_fsize);
1199 				if (pp->p_fsize == 0)
1200 					break;
1201 				NXTNUM(v);
1202 				pp->p_frag = v / pp->p_fsize;
1203 				NXTNUM(pp->p_cpg);
1204 				break;
1205 
1206 			case FS_BSDLFS:
1207 				NXTNUM(pp->p_fsize);
1208 				if (pp->p_fsize == 0)
1209 					break;
1210 				NXTNUM(v);
1211 				pp->p_frag = v / pp->p_fsize;
1212 				NXTNUM(pp->p_cpg);
1213 				break;
1214 
1215 			default:
1216 				break;
1217 			}
1218 			continue;
1219 		}
1220 		fprintf(stderr, "line %d: %s: Unknown disklabel field\n",
1221 		    lineno, cp);
1222 		errors++;
1223 	next:
1224 		;
1225 	}
1226 	errors += checklabel(lp);
1227 	return (errors == 0);
1228 }
1229 
1230 /*
1231  * Check disklabel for errors and fill in
1232  * derived fields according to supplied values.
1233  */
1234 int
1235 checklabel(lp)
1236 	register struct disklabel *lp;
1237 {
1238 	register struct partition *pp;
1239 	int i, errors = 0;
1240 	char part;
1241 
1242 	if (lp->d_secsize == 0) {
1243 		fprintf(stderr, "sector size %ld\n", lp->d_secsize);
1244 		return (1);
1245 	}
1246 	if (lp->d_nsectors == 0) {
1247 		fprintf(stderr, "sectors/track %ld\n", lp->d_nsectors);
1248 		return (1);
1249 	}
1250 	if (lp->d_ntracks == 0) {
1251 		fprintf(stderr, "tracks/cylinder %ld\n", lp->d_ntracks);
1252 		return (1);
1253 	}
1254 	if  (lp->d_ncylinders == 0) {
1255 		fprintf(stderr, "cylinders/unit %ld\n", lp->d_ncylinders);
1256 		errors++;
1257 	}
1258 	if (lp->d_rpm == 0)
1259 		Warning("revolutions/minute %d", lp->d_rpm);
1260 	if (lp->d_secpercyl == 0)
1261 		lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1262 	if (lp->d_secperunit == 0)
1263 		lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
1264 	if (lp->d_bbsize == 0) {
1265 		fprintf(stderr, "boot block size %ld\n", lp->d_bbsize);
1266 		errors++;
1267 	} else if (lp->d_bbsize % lp->d_secsize)
1268 		Warning("boot block size %% sector-size != 0");
1269 	if (lp->d_sbsize == 0) {
1270 		fprintf(stderr, "super block size %ld\n", lp->d_sbsize);
1271 		errors++;
1272 	} else if (lp->d_sbsize % lp->d_secsize)
1273 		Warning("super block size %% sector-size != 0");
1274 	if (lp->d_npartitions > MAXPARTITIONS)
1275 		Warning("number of partitions (%d) > MAXPARTITIONS (%d)",
1276 		    lp->d_npartitions, MAXPARTITIONS);
1277 	for (i = 0; i < lp->d_npartitions; i++) {
1278 		part = 'a' + i;
1279 		pp = &lp->d_partitions[i];
1280 		if (pp->p_size == 0 && pp->p_offset != 0)
1281 			Warning("partition %c: size 0, but offset %d",
1282 			    part, pp->p_offset);
1283 #ifdef notdef
1284 		if (pp->p_size % lp->d_secpercyl)
1285 			Warning("partition %c: size %% cylinder-size != 0",
1286 			    part);
1287 		if (pp->p_offset % lp->d_secpercyl)
1288 			Warning("partition %c: offset %% cylinder-size != 0",
1289 			    part);
1290 #endif
1291 		if (pp->p_offset > lp->d_secperunit) {
1292 			fprintf(stderr,
1293 			    "partition %c: offset past end of unit\n", part);
1294 			errors++;
1295 		}
1296 		if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1297 			fprintf(stderr,
1298 			"partition %c: partition extends past end of unit\n",
1299 			    part);
1300 			errors++;
1301 		}
1302 	}
1303 	for (; i < MAXPARTITIONS; i++) {
1304 		part = 'a' + i;
1305 		pp = &lp->d_partitions[i];
1306 		if (pp->p_size || pp->p_offset)
1307 			Warning("unused partition %c: size %d offset %d",
1308 			    'a' + i, pp->p_size, pp->p_offset);
1309 	}
1310 	return (errors);
1311 }
1312 
1313 /*
1314  * When operating on a "virgin" disk, try getting an initial label
1315  * from the associated device driver.  This might work for all device
1316  * drivers that are able to fetch some initial device parameters
1317  * without even having access to a (BSD) disklabel, like SCSI disks,
1318  * most IDE drives, or vn devices.
1319  *
1320  * The device name must be given in its "canonical" form.
1321  */
1322 struct disklabel *
1323 getvirginlabel(void)
1324 {
1325 	static struct disklabel lab;
1326 	char namebuf[BBSIZE];
1327 	int f;
1328 
1329 	if (dkname[0] == '/') {
1330 		fprintf(stderr,
1331 		"\"auto\" requires the usage of a canonical disk name.\n");
1332 		return (NULL);
1333 	}
1334 	(void)snprintf(namebuf, BBSIZE, "%sr%s", _PATH_DEV, dkname);
1335 	if ((f = open(namebuf, O_RDONLY, 0)) == -1) {
1336 		Perror("open()");
1337 		return (NULL);
1338 	}
1339 	if (ioctl(f, DIOCGDINFO, &lab) < 0) {
1340 		Perror("ioctl DIOCGDINFO");
1341 		close(f);
1342 		return (NULL);
1343 	}
1344 	close(f);
1345 	return (&lab);
1346 }
1347 
1348 
1349 /*
1350  * If we are installing a boot program that doesn't fit in d_bbsize
1351  * we need to mark those partitions that the boot overflows into.
1352  * This allows newfs to prevent creation of a filesystem where it might
1353  * clobber bootstrap code.
1354  */
1355 void
1356 setbootflag(lp)
1357 	register struct disklabel *lp;
1358 {
1359 	register struct partition *pp;
1360 	int i, errors = 0;
1361 	char part;
1362 	u_long boffset;
1363 
1364 	if (bootbuf == 0)
1365 		return;
1366 	boffset = bootsize / lp->d_secsize;
1367 	for (i = 0; i < lp->d_npartitions; i++) {
1368 		part = 'a' + i;
1369 		pp = &lp->d_partitions[i];
1370 		if (pp->p_size == 0)
1371 			continue;
1372 		if (boffset <= pp->p_offset) {
1373 			if (pp->p_fstype == FS_BOOT)
1374 				pp->p_fstype = FS_UNUSED;
1375 		} else if (pp->p_fstype != FS_BOOT) {
1376 			if (pp->p_fstype != FS_UNUSED) {
1377 				fprintf(stderr,
1378 					"boot overlaps used partition %c\n",
1379 					part);
1380 				errors++;
1381 			} else {
1382 				pp->p_fstype = FS_BOOT;
1383 				Warning("boot overlaps partition %c, %s",
1384 					part, "marked as FS_BOOT");
1385 			}
1386 		}
1387 	}
1388 	if (errors) {
1389 		fprintf(stderr, "Cannot install boot program\n");
1390 		exit(4);
1391 	}
1392 }
1393 
1394 /*VARARGS1*/
1395 void
1396 Warning(char *fmt, ...)
1397 {
1398 	va_list ap;
1399 
1400 	fprintf(stderr, "Warning, ");
1401 	va_start(ap, fmt);
1402 	vfprintf(stderr, fmt, ap);
1403 	fprintf(stderr, "\n");
1404 	va_end(ap);
1405 }
1406 
1407 void
1408 Perror(str)
1409 	char *str;
1410 {
1411 	fputs("disklabel: ", stderr); perror(str);
1412 	exit(4);
1413 }
1414 
1415 void
1416 usage()
1417 {
1418 #if NUMBOOT > 0
1419 	fprintf(stderr,
1420 "%s\n\t%s\n%s\n\t%s\n%s\n\t%s\n%s\n\t%s\n%s\n\t%s\n%s\n\t%s\n%s\n\t%s\n%s\n\t%s\n",
1421 "usage: disklabel [-r] disk",
1422 		"(to read label)",
1423 "or disklabel -w [-r] disk type [ packid ]",
1424 		"(to write label with existing boot program)",
1425 "or disklabel -e [-r] disk",
1426 		"(to edit label)",
1427 "or disklabel -R [-r] disk protofile",
1428 		"(to restore label with existing boot program)",
1429 #if NUMBOOT > 1
1430 "or disklabel -B [ -b boot1 [ -s boot2 ] ] disk [ type ]",
1431 		"(to install boot program with existing label)",
1432 "or disklabel -w -B [ -b boot1 [ -s boot2 ] ] disk type [ packid ]",
1433 		"(to write label and boot program)",
1434 "or disklabel -R -B [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]",
1435 		"(to restore label and boot program)",
1436 #else
1437 "or disklabel -B [ -b bootprog ] disk [ type ]",
1438 		"(to install boot program with existing on-disk label)",
1439 "or disklabel -w -B [ -b bootprog ] disk type [ packid ]",
1440 		"(to write label and install boot program)",
1441 "or disklabel -R -B [ -b bootprog ] disk protofile [ type ]",
1442 		"(to restore label and install boot program)",
1443 #endif
1444 "or disklabel [-NW] disk",
1445 		"(to write disable/enable label)");
1446 #else
1447 	fprintf(stderr, "%-43s%s\n%-43s%s\n%-43s%s\n%-43s%s\n%-43s%s\n",
1448 "usage: disklabel [-r] disk", "(to read label)",
1449 "or disklabel -w [-r] disk type [ packid ]", "(to write label)",
1450 "or disklabel -e [-r] disk", "(to edit label)",
1451 "or disklabel -R [-r] disk protofile", "(to restore label)",
1452 "or disklabel [-NW] disk", "(to write disable/enable label)");
1453 #endif
1454 	exit(1);
1455 }
1456