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