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