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