xref: /freebsd/sbin/bsdlabel/bsdlabel.c (revision 5daa806da07aafec775755253057ecd8047c9759)
18fae3551SRodney W. Grimes /*
2fd43aa1cSDavid E. O'Brien  * Copyright (c) 1994, 1995 Gordon W. Ross
3fd43aa1cSDavid E. O'Brien  * Copyright (c) 1994 Theo de Raadt
4fd43aa1cSDavid E. O'Brien  * All rights reserved.
58fae3551SRodney W. Grimes  * Copyright (c) 1987, 1993
68fae3551SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
78fae3551SRodney W. Grimes  *
88fae3551SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
98fae3551SRodney W. Grimes  * Symmetric Computer Systems.
108fae3551SRodney W. Grimes  *
118fae3551SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
128fae3551SRodney W. Grimes  * modification, are permitted provided that the following conditions
138fae3551SRodney W. Grimes  * are met:
148fae3551SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
158fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
168fae3551SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
178fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
188fae3551SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
198fae3551SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
208fae3551SRodney W. Grimes  *    must display the following acknowledgement:
218fae3551SRodney W. Grimes  *	This product includes software developed by the University of
228fae3551SRodney W. Grimes  *	California, Berkeley and its contributors.
23fd43aa1cSDavid E. O'Brien  *      This product includes software developed by Theo de Raadt.
248fae3551SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
258fae3551SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
268fae3551SRodney W. Grimes  *    without specific prior written permission.
278fae3551SRodney W. Grimes  *
288fae3551SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
298fae3551SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
308fae3551SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
318fae3551SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
328fae3551SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
338fae3551SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
348fae3551SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
358fae3551SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
368fae3551SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
378fae3551SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
388fae3551SRodney W. Grimes  * SUCH DAMAGE.
39fd43aa1cSDavid E. O'Brien  *
40fd43aa1cSDavid E. O'Brien  *	from: $NetBSD: disksubr.c,v 1.13 2000/12/17 22:39:18 pk $
418fae3551SRodney W. Grimes  */
428fae3551SRodney W. Grimes 
438fae3551SRodney W. Grimes #ifndef lint
446bd343a9SPhilippe Charnier static const char copyright[] =
458fae3551SRodney W. Grimes "@(#) Copyright (c) 1987, 1993\n\
468fae3551SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
478fae3551SRodney W. Grimes #endif /* not lint */
488fae3551SRodney W. Grimes 
498fae3551SRodney W. Grimes #ifndef lint
506bd343a9SPhilippe Charnier #if 0
518fae3551SRodney W. Grimes static char sccsid[] = "@(#)disklabel.c	8.2 (Berkeley) 1/7/94";
528fae3551SRodney W. Grimes /* from static char sccsid[] = "@(#)disklabel.c	1.2 (Symmetric) 11/28/85"; */
536bd343a9SPhilippe Charnier #endif
548fae3551SRodney W. Grimes #endif /* not lint */
558fae3551SRodney W. Grimes 
56c8223965SMark Murray #include <sys/cdefs.h>
57c8223965SMark Murray __FBSDID("$FreeBSD$");
58c8223965SMark Murray 
598fae3551SRodney W. Grimes #include <sys/param.h>
60dfbc3f0cSPoul-Henning Kamp #include <stdint.h>
618fae3551SRodney W. Grimes #include <sys/file.h>
628fae3551SRodney W. Grimes #include <sys/stat.h>
6361de51caSJoerg Wunsch #include <sys/wait.h>
64b9d05a16SPoul-Henning Kamp #include <sys/disk.h>
658fae3551SRodney W. Grimes #define DKTYPENAMES
66b35e6950SBosko Milekic #define FSTYPENAMES
678fae3551SRodney W. Grimes #include <sys/disklabel.h>
68bc33ea1aSRuslan Ermilov 
698fae3551SRodney W. Grimes #include <unistd.h>
708fae3551SRodney W. Grimes #include <string.h>
718fae3551SRodney W. Grimes #include <stdio.h>
725daa806dSPoul-Henning Kamp #include <libgeom.h>
7361de51caSJoerg Wunsch #include <stdlib.h>
7461de51caSJoerg Wunsch #include <signal.h>
7561de51caSJoerg Wunsch #include <stdarg.h>
768fae3551SRodney W. Grimes #include <ctype.h>
77bef2080aSPhilippe Charnier #include <err.h>
7837736675SWarner Losh #include <errno.h>
79c8223965SMark Murray 
808fae3551SRodney W. Grimes #include "pathnames.h"
818fae3551SRodney W. Grimes 
828fae3551SRodney W. Grimes /*
832c60b668SPoul-Henning Kamp  * Disklabel: read and write bsdlabels.
848fae3551SRodney W. Grimes  * The label is usually placed on one of the first sectors of the disk.
858fae3551SRodney W. Grimes  * Many machines also place a bootstrap in the same area,
868fae3551SRodney W. Grimes  * in which case the label is embedded in the bootstrap.
878fae3551SRodney W. Grimes  * The bootstrap source must leave space at the proper offset
888fae3551SRodney W. Grimes  * for the label on such machines.
898fae3551SRodney W. Grimes  */
908fae3551SRodney W. Grimes 
918fae3551SRodney W. Grimes #ifndef BBSIZE
928fae3551SRodney W. Grimes #define	BBSIZE	8192			/* size of boot area, with label */
938fae3551SRodney W. Grimes #endif
948fae3551SRodney W. Grimes 
953233afaeSJohn W. De Boskey /* FIX!  These are too low, but are traditional */
963233afaeSJohn W. De Boskey #define DEFAULT_NEWFS_BLOCK  8192U
973233afaeSJohn W. De Boskey #define DEFAULT_NEWFS_FRAG   1024U
983233afaeSJohn W. De Boskey #define DEFAULT_NEWFS_CPG    16U
993233afaeSJohn W. De Boskey 
1003233afaeSJohn W. De Boskey #define BIG_NEWFS_BLOCK  16384U
101f1a7b7eeSIan Dowse #define BIG_NEWFS_FRAG   2048U
1023233afaeSJohn W. De Boskey #define BIG_NEWFS_CPG    64U
1033233afaeSJohn W. De Boskey 
104326c7cdaSWarner Losh void	makelabel(const char *, const char *, struct disklabel *);
1055daa806dSPoul-Henning Kamp int	writelabel(void *, struct disklabel *);
106326c7cdaSWarner Losh void	l_perror(const char *);
1075daa806dSPoul-Henning Kamp struct disklabel *readlabel(void);
1085daa806dSPoul-Henning Kamp struct disklabel *makebootarea(void *, struct disklabel *);
109326c7cdaSWarner Losh void	display(FILE *, const struct disklabel *);
1105daa806dSPoul-Henning Kamp int	edit(struct disklabel *);
111326c7cdaSWarner Losh int	editit(void);
112326c7cdaSWarner Losh char	*skip(char *);
113326c7cdaSWarner Losh char	*word(char *);
114326c7cdaSWarner Losh int	getasciilabel(FILE *, struct disklabel *);
11567b46708SIan Dowse int	getasciipartspec(char *, struct disklabel *, int, int);
116326c7cdaSWarner Losh int	checklabel(struct disklabel *);
117326c7cdaSWarner Losh void	Warning(const char *, ...) __printflike(1, 2);
118326c7cdaSWarner Losh void	usage(void);
119326c7cdaSWarner Losh struct disklabel *getvirginlabel(void);
12061de51caSJoerg Wunsch 
1218fae3551SRodney W. Grimes #define	DEFEDITOR	_PATH_VI
1228fae3551SRodney W. Grimes #define	streq(a,b)	(strcmp(a,b) == 0)
1238fae3551SRodney W. Grimes 
1248fae3551SRodney W. Grimes char	*dkname;
1258fae3551SRodney W. Grimes char	*specname;
1265daa806dSPoul-Henning Kamp char	*rawname;
127aaae3130SKris Kennaway char	tmpfil[] = PATH_TMPFILE;
1288fae3551SRodney W. Grimes 
1298fae3551SRodney W. Grimes struct	disklabel lab;
130bc33ea1aSRuslan Ermilov int64_t	bootarea[BBSIZE / 8];
131c8223965SMark Murray char	blank[] = "";
132c8223965SMark Murray char	unknown[] = "unknown";
1338fae3551SRodney W. Grimes 
1343233afaeSJohn W. De Boskey #define MAX_PART ('z')
1353233afaeSJohn W. De Boskey #define MAX_NUM_PARTS (1 + MAX_PART - 'a')
1363233afaeSJohn W. De Boskey char    part_size_type[MAX_NUM_PARTS];
1373233afaeSJohn W. De Boskey char    part_offset_type[MAX_NUM_PARTS];
1383233afaeSJohn W. De Boskey int     part_set[MAX_NUM_PARTS];
1393233afaeSJohn W. De Boskey 
1408fae3551SRodney W. Grimes int	installboot;	/* non-zero if we should install a boot program */
1418fae3551SRodney W. Grimes char	*xxboot;	/* primary boot */
1428fae3551SRodney W. Grimes char	boot0[MAXPATHLEN];
1438fae3551SRodney W. Grimes 
1447838fd0eSPoul-Henning Kamp static int labeloffset = LABELOFFSET;
1457838fd0eSPoul-Henning Kamp static int bbsize = BBSIZE;
1467838fd0eSPoul-Henning Kamp static int alphacksum =
1477838fd0eSPoul-Henning Kamp #if defined(__alpha__)
1487838fd0eSPoul-Henning Kamp 	1;
1497838fd0eSPoul-Henning Kamp #else
1507838fd0eSPoul-Henning Kamp 	0;
1517838fd0eSPoul-Henning Kamp #endif
152c80f9755SPoul-Henning Kamp 
1538fae3551SRodney W. Grimes enum	{
154f080d33bSPoul-Henning Kamp 	UNSPEC, EDIT, READ, RESTORE, WRITE, WRITEBOOT
1558fae3551SRodney W. Grimes } op = UNSPEC;
1568fae3551SRodney W. Grimes 
157bc33ea1aSRuslan Ermilov 
1583233afaeSJohn W. De Boskey int	disable_write;   /* set to disable writing to disk label */
1598fae3551SRodney W. Grimes 
16061de51caSJoerg Wunsch int
161326c7cdaSWarner Losh main(int argc, char *argv[])
1628fae3551SRodney W. Grimes {
163c1fdb43fSWarner Losh 	struct disklabel *lp;
1648fae3551SRodney W. Grimes 	FILE *t;
1655daa806dSPoul-Henning Kamp 	int ch, error = 0;
1668fae3551SRodney W. Grimes 	char *name = 0;
1678fae3551SRodney W. Grimes 
168bc33ea1aSRuslan Ermilov 	while ((ch = getopt(argc, argv, "Bb:em:nRrs:w")) != -1)
1698fae3551SRodney W. Grimes 		switch (ch) {
1708fae3551SRodney W. Grimes 			case 'B':
1718fae3551SRodney W. Grimes 				++installboot;
1728fae3551SRodney W. Grimes 				break;
1738fae3551SRodney W. Grimes 			case 'b':
1748fae3551SRodney W. Grimes 				xxboot = optarg;
1758fae3551SRodney W. Grimes 				break;
176bc33ea1aSRuslan Ermilov 			case 'm':
177c80f9755SPoul-Henning Kamp 				if (!strcmp(optarg, "i386")) {
178c80f9755SPoul-Henning Kamp 					labeloffset = 512;
179c80f9755SPoul-Henning Kamp 					bbsize = 8192;
1807838fd0eSPoul-Henning Kamp 					alphacksum = 0;
181c80f9755SPoul-Henning Kamp 				} else if (!strcmp(optarg, "alpha")) {
182c80f9755SPoul-Henning Kamp 					labeloffset = 64;
183c80f9755SPoul-Henning Kamp 					bbsize = 8192;
184c80f9755SPoul-Henning Kamp 					alphacksum = 1;
185c80f9755SPoul-Henning Kamp 				}
186bc33ea1aSRuslan Ermilov 				break;
1873233afaeSJohn W. De Boskey 			case 'n':
1883233afaeSJohn W. De Boskey 				disable_write = 1;
1893233afaeSJohn W. De Boskey 				break;
1908fae3551SRodney W. Grimes 			case 'R':
1918fae3551SRodney W. Grimes 				if (op != UNSPEC)
1928fae3551SRodney W. Grimes 					usage();
1938fae3551SRodney W. Grimes 				op = RESTORE;
1948fae3551SRodney W. Grimes 				break;
1958fae3551SRodney W. Grimes 			case 'e':
1968fae3551SRodney W. Grimes 				if (op != UNSPEC)
1978fae3551SRodney W. Grimes 					usage();
1988fae3551SRodney W. Grimes 				op = EDIT;
1998fae3551SRodney W. Grimes 				break;
2008fae3551SRodney W. Grimes 			case 'r':
2018fae3551SRodney W. Grimes 				break;
2028fae3551SRodney W. Grimes 			case 'w':
2038fae3551SRodney W. Grimes 				if (op != UNSPEC)
2048fae3551SRodney W. Grimes 					usage();
2058fae3551SRodney W. Grimes 				op = WRITE;
2068fae3551SRodney W. Grimes 				break;
2078fae3551SRodney W. Grimes 			case '?':
2088fae3551SRodney W. Grimes 			default:
2098fae3551SRodney W. Grimes 				usage();
2108fae3551SRodney W. Grimes 		}
2118fae3551SRodney W. Grimes 	argc -= optind;
2128fae3551SRodney W. Grimes 	argv += optind;
2138fae3551SRodney W. Grimes 	if (installboot) {
2148fae3551SRodney W. Grimes 		if (op == UNSPEC)
2158fae3551SRodney W. Grimes 			op = WRITEBOOT;
2168fae3551SRodney W. Grimes 	} else {
2178fae3551SRodney W. Grimes 		if (op == UNSPEC)
2188fae3551SRodney W. Grimes 			op = READ;
2198d48318bSPoul-Henning Kamp 		xxboot = 0;
2208fae3551SRodney W. Grimes 	}
2218fae3551SRodney W. Grimes 	if (argc < 1)
2228fae3551SRodney W. Grimes 		usage();
2238fae3551SRodney W. Grimes 
2245daa806dSPoul-Henning Kamp 	/* Figure out the names of the thing we're working on */
2255daa806dSPoul-Henning Kamp 	if (argv[0][0] != '/') {
2268fae3551SRodney W. Grimes 		dkname = argv[0];
2275daa806dSPoul-Henning Kamp 		asprintf(&specname, "%s%s", _PATH_DEV, argv[0]);
2285daa806dSPoul-Henning Kamp 	} else {
2295daa806dSPoul-Henning Kamp 		dkname = strrchr(argv[0], '/');
2305daa806dSPoul-Henning Kamp 		dkname++;
2315daa806dSPoul-Henning Kamp 		specname = argv[0];
2328fae3551SRodney W. Grimes 	}
2335daa806dSPoul-Henning Kamp 	asprintf(&rawname, "%s%c", specname, 'a' + RAW_PART);
2348fae3551SRodney W. Grimes 
2358fae3551SRodney W. Grimes 	switch(op) {
2368fae3551SRodney W. Grimes 
2373b3038a6SBill Fumerola 	case UNSPEC:
2383b3038a6SBill Fumerola 		break;
2393b3038a6SBill Fumerola 
2408fae3551SRodney W. Grimes 	case EDIT:
2418fae3551SRodney W. Grimes 		if (argc != 1)
2428fae3551SRodney W. Grimes 			usage();
2435daa806dSPoul-Henning Kamp 		lp = readlabel();
2445daa806dSPoul-Henning Kamp 		error = edit(lp);
2458fae3551SRodney W. Grimes 		break;
2468fae3551SRodney W. Grimes 
2478fae3551SRodney W. Grimes 	case READ:
2488fae3551SRodney W. Grimes 		if (argc != 1)
2498fae3551SRodney W. Grimes 			usage();
2505daa806dSPoul-Henning Kamp 		lp = readlabel();
2518fae3551SRodney W. Grimes 		display(stdout, lp);
2528fae3551SRodney W. Grimes 		error = checklabel(lp);
2538fae3551SRodney W. Grimes 		break;
2548fae3551SRodney W. Grimes 
2558fae3551SRodney W. Grimes 	case RESTORE:
2568fae3551SRodney W. Grimes 		if (argc != 2)
2578fae3551SRodney W. Grimes 			usage();
2588fae3551SRodney W. Grimes 		if (!(t = fopen(argv[1], "r")))
2595daa806dSPoul-Henning Kamp 			err(4, "fopen %s", argv[1]);
2606cabb348SBruce Evans 		if (!getasciilabel(t, &lab))
2616cabb348SBruce Evans 			exit(1);
2625daa806dSPoul-Henning Kamp 		lp = makebootarea(bootarea, &lab);
2636cabb348SBruce Evans 		*lp = lab;
2645daa806dSPoul-Henning Kamp 		error = writelabel(bootarea, lp);
2658fae3551SRodney W. Grimes 		break;
2668fae3551SRodney W. Grimes 
2678fae3551SRodney W. Grimes 	case WRITE:
2688fae3551SRodney W. Grimes 		if (argc == 3) {
2698fae3551SRodney W. Grimes 			name = argv[2];
2708fae3551SRodney W. Grimes 			argc--;
2718fae3551SRodney W. Grimes 		}
2728fae3551SRodney W. Grimes 		if (argc != 2)
2738fae3551SRodney W. Grimes 			usage();
2748fae3551SRodney W. Grimes 		makelabel(argv[1], name, &lab);
2755daa806dSPoul-Henning Kamp 		lp = makebootarea(bootarea, &lab);
2768fae3551SRodney W. Grimes 		*lp = lab;
2778fae3551SRodney W. Grimes 		if (checklabel(lp) == 0)
2785daa806dSPoul-Henning Kamp 			error = writelabel(bootarea, lp);
2798fae3551SRodney W. Grimes 		break;
2808fae3551SRodney W. Grimes 
2818fae3551SRodney W. Grimes 	case WRITEBOOT:
2828fae3551SRodney W. Grimes 	{
2838fae3551SRodney W. Grimes 		struct disklabel tlab;
2848fae3551SRodney W. Grimes 
2855daa806dSPoul-Henning Kamp 		lp = readlabel();
2868fae3551SRodney W. Grimes 		tlab = *lp;
2878fae3551SRodney W. Grimes 		if (argc == 2)
2888fae3551SRodney W. Grimes 			makelabel(argv[1], 0, &lab);
2895daa806dSPoul-Henning Kamp 		lp = makebootarea(bootarea, &lab);
2908fae3551SRodney W. Grimes 		*lp = tlab;
2918fae3551SRodney W. Grimes 		if (checklabel(lp) == 0)
2925daa806dSPoul-Henning Kamp 			error = writelabel(bootarea, lp);
2938fae3551SRodney W. Grimes 		break;
2948fae3551SRodney W. Grimes 	}
2958fae3551SRodney W. Grimes 	}
2968fae3551SRodney W. Grimes 	exit(error);
2978fae3551SRodney W. Grimes }
2988fae3551SRodney W. Grimes 
2998fae3551SRodney W. Grimes /*
300ef9ab0b3SRuslan Ermilov  * Construct a prototype disklabel from /etc/disktab.
3018fae3551SRodney W. Grimes  */
30261de51caSJoerg Wunsch void
303326c7cdaSWarner Losh makelabel(const char *type, const char *name, struct disklabel *lp)
3048fae3551SRodney W. Grimes {
305326c7cdaSWarner Losh 	struct disklabel *dp;
306425bed3aSJoerg Wunsch 
307425bed3aSJoerg Wunsch 	if (strcmp(type, "auto") == 0)
308425bed3aSJoerg Wunsch 		dp = getvirginlabel();
309425bed3aSJoerg Wunsch 	else
3108fae3551SRodney W. Grimes 		dp = getdiskbyname(type);
3116bd343a9SPhilippe Charnier 	if (dp == NULL)
3126bd343a9SPhilippe Charnier 		errx(1, "%s: unknown disk type", type);
3138fae3551SRodney W. Grimes 	*lp = *dp;
3148fae3551SRodney W. Grimes 	bzero(lp->d_packname, sizeof(lp->d_packname));
3158fae3551SRodney W. Grimes 	if (name)
3168fae3551SRodney W. Grimes 		(void)strncpy(lp->d_packname, name, sizeof(lp->d_packname));
3178fae3551SRodney W. Grimes }
3188fae3551SRodney W. Grimes 
31961de51caSJoerg Wunsch int
3205daa806dSPoul-Henning Kamp writelabel(void *boot, struct disklabel *lp)
3218fae3551SRodney W. Grimes {
322bc33ea1aSRuslan Ermilov 	uint64_t *p, sum;
3235daa806dSPoul-Henning Kamp 	int i, fd;
3245daa806dSPoul-Henning Kamp 	struct gctl_req *grq;
3255daa806dSPoul-Henning Kamp 	char const *errstr;
326130cd73aSDoug Rabson 
3273233afaeSJohn W. De Boskey 	if (disable_write) {
3283233afaeSJohn W. De Boskey 		Warning("write to disk label supressed - label was as follows:");
3293233afaeSJohn W. De Boskey 		display(stdout, lp);
3303233afaeSJohn W. De Boskey 		return (0);
33180baf8ceSPoul-Henning Kamp 	}
33280baf8ceSPoul-Henning Kamp 
3338fae3551SRodney W. Grimes 	lp->d_magic = DISKMAGIC;
3348fae3551SRodney W. Grimes 	lp->d_magic2 = DISKMAGIC;
3358fae3551SRodney W. Grimes 	lp->d_checksum = 0;
3368fae3551SRodney W. Grimes 	lp->d_checksum = dkcksum(lp);
337c80f9755SPoul-Henning Kamp 	bsd_disklabel_le_enc((u_char *)boot + labeloffset, lp);
338dfbc3f0cSPoul-Henning Kamp 	if (alphacksum) {
3395daa806dSPoul-Henning Kamp 		/* Generate the bootblock checksum for the SRM console.  */
340bc33ea1aSRuslan Ermilov 		for (p = (uint64_t *)boot, i = 0, sum = 0; i < 63; i++)
341130cd73aSDoug Rabson 			sum += p[i];
342130cd73aSDoug Rabson 		p[63] = sum;
343bc33ea1aSRuslan Ermilov 	}
3445daa806dSPoul-Henning Kamp 
3455daa806dSPoul-Henning Kamp 	fd = open(specname, O_RDWR);
3465daa806dSPoul-Henning Kamp 	if (fd < 0) {
3475daa806dSPoul-Henning Kamp 		grq = gctl_get_handle(GCTL_CONFIG_GEOM);
3485daa806dSPoul-Henning Kamp 		gctl_ro_param(grq, "class", -1, "BSD");
3495daa806dSPoul-Henning Kamp 		gctl_ro_param(grq, "geom", -1, dkname);
3505daa806dSPoul-Henning Kamp 		gctl_ro_param(grq, "verb", -1, "write label");
3515daa806dSPoul-Henning Kamp 		gctl_ro_param(grq, "label", 148+16*8, (u_char *)boot + labeloffset);
3525daa806dSPoul-Henning Kamp 		errstr = gctl_issue(grq);
3535daa806dSPoul-Henning Kamp 		if (errstr != NULL)
3545daa806dSPoul-Henning Kamp 			errx(1, "%s", errstr);
3555daa806dSPoul-Henning Kamp 		gctl_free(grq);
3565daa806dSPoul-Henning Kamp 		if (installboot) {
3575daa806dSPoul-Henning Kamp 			grq = gctl_get_handle(GCTL_CONFIG_GEOM);
3585daa806dSPoul-Henning Kamp 			gctl_ro_param(grq, "class", -1, "BSD");
3595daa806dSPoul-Henning Kamp 			gctl_ro_param(grq, "geom", -1, dkname);
3605daa806dSPoul-Henning Kamp 			gctl_ro_param(grq, "verb", -1, "write bootcode");
3615daa806dSPoul-Henning Kamp 			gctl_ro_param(grq, "bootcode", BBSIZE, boot);
3625daa806dSPoul-Henning Kamp 			errstr = gctl_issue(grq);
3635daa806dSPoul-Henning Kamp 			if (errstr != NULL)
3645daa806dSPoul-Henning Kamp 				errx(1, "%s", errstr);
3655daa806dSPoul-Henning Kamp 			gctl_free(grq);
3665daa806dSPoul-Henning Kamp 		}
3675daa806dSPoul-Henning Kamp 	} else {
3685daa806dSPoul-Henning Kamp 		if (write(fd, boot, bbsize) != bbsize) {
3695daa806dSPoul-Henning Kamp 			warn("write %s", specname);
3705daa806dSPoul-Henning Kamp 			close (fd);
3718fae3551SRodney W. Grimes 			return (1);
3728fae3551SRodney W. Grimes 		}
3735daa806dSPoul-Henning Kamp 		close (fd);
3745daa806dSPoul-Henning Kamp 	}
3758fae3551SRodney W. Grimes 	return (0);
3768fae3551SRodney W. Grimes }
3778fae3551SRodney W. Grimes 
37861de51caSJoerg Wunsch void
379326c7cdaSWarner Losh l_perror(const char *s)
3808fae3551SRodney W. Grimes {
3816bd343a9SPhilippe Charnier 	switch (errno) {
3828fae3551SRodney W. Grimes 
3838fae3551SRodney W. Grimes 	case ESRCH:
3843121d4cbSPhilippe Charnier 		warnx("%s: no disk label on disk;", s);
3850b3f0926SWarner Losh 		fprintf(stderr, "add \"-r\" to install initial label\n");
3868fae3551SRodney W. Grimes 		break;
3878fae3551SRodney W. Grimes 
3888fae3551SRodney W. Grimes 	case EINVAL:
3893121d4cbSPhilippe Charnier 		warnx("%s: label magic number or checksum is wrong!", s);
3903121d4cbSPhilippe Charnier 		fprintf(stderr, "(disklabel or kernel is out of date?)\n");
3918fae3551SRodney W. Grimes 		break;
3928fae3551SRodney W. Grimes 
3938fae3551SRodney W. Grimes 	case EBUSY:
3946bd343a9SPhilippe Charnier 		warnx("%s: open partition would move or shrink", s);
3958fae3551SRodney W. Grimes 		break;
3968fae3551SRodney W. Grimes 
3978fae3551SRodney W. Grimes 	case EXDEV:
3987de06420SBruce Evans 		warnx("%s: '%c' partition must start at beginning of disk",
3997de06420SBruce Evans 		    s, 'a' + RAW_PART);
4008fae3551SRodney W. Grimes 		break;
4018fae3551SRodney W. Grimes 
4028fae3551SRodney W. Grimes 	default:
4036bd343a9SPhilippe Charnier 		warn((char *)NULL);
4048fae3551SRodney W. Grimes 		break;
4058fae3551SRodney W. Grimes 	}
4068fae3551SRodney W. Grimes }
4078fae3551SRodney W. Grimes 
4088fae3551SRodney W. Grimes /*
4098fae3551SRodney W. Grimes  * Fetch disklabel for disk.
4108fae3551SRodney W. Grimes  * Use ioctl to get label unless -r flag is given.
4118fae3551SRodney W. Grimes  */
4128fae3551SRodney W. Grimes struct disklabel *
4135daa806dSPoul-Henning Kamp readlabel(void)
4148fae3551SRodney W. Grimes {
4155daa806dSPoul-Henning Kamp 	int f;
4165daa806dSPoul-Henning Kamp 	int error;
4178fae3551SRodney W. Grimes 
4185daa806dSPoul-Henning Kamp 	f = open(specname, O_RDONLY);
4195daa806dSPoul-Henning Kamp 	if (f < 0)
4205daa806dSPoul-Henning Kamp 		err(1, specname);
421c80f9755SPoul-Henning Kamp 	(void)lseek(f, (off_t)0, SEEK_SET);
4228fae3551SRodney W. Grimes 	if (read(f, bootarea, BBSIZE) < BBSIZE)
4235daa806dSPoul-Henning Kamp 		err(4, "%s read", specname);
4245daa806dSPoul-Henning Kamp 	error = bsd_disklabel_le_dec((u_char *)bootarea + labeloffset, &lab, MAXPARTITIONS);
4255daa806dSPoul-Henning Kamp 	if (error)
4265daa806dSPoul-Henning Kamp 		errx(1, "%s: invalid bsd label", specname);
4275daa806dSPoul-Henning Kamp 	close (f);
428c80f9755SPoul-Henning Kamp 	return (&lab);
4298fae3551SRodney W. Grimes }
4308fae3551SRodney W. Grimes 
4318fae3551SRodney W. Grimes /*
432dfbc3f0cSPoul-Henning Kamp  * Construct a bootarea (bbsize bytes) in the specified buffer ``boot''
4338fae3551SRodney W. Grimes  * Returns a pointer to the disklabel portion of the bootarea.
4348fae3551SRodney W. Grimes  */
4358fae3551SRodney W. Grimes struct disklabel *
4365daa806dSPoul-Henning Kamp makebootarea(void *boot, struct disklabel *dp)
4378fae3551SRodney W. Grimes {
43821c729c2SBruce Evans 	struct disklabel *lp;
4398fae3551SRodney W. Grimes 	int b;
4408fae3551SRodney W. Grimes 	struct stat sb;
441bc33ea1aSRuslan Ermilov 	uint64_t *bootinfo;
442cc18e4ccSBruce Evans 	int n;
4438fae3551SRodney W. Grimes 
4448fae3551SRodney W. Grimes 	/* XXX */
4458fae3551SRodney W. Grimes 	if (dp->d_secsize == 0) {
4468fae3551SRodney W. Grimes 		dp->d_secsize = DEV_BSIZE;
447dfbc3f0cSPoul-Henning Kamp 		dp->d_bbsize = bbsize;
4488fae3551SRodney W. Grimes 	}
449dfbc3f0cSPoul-Henning Kamp 	lp = (struct disklabel *)((char *)boot + labeloffset);
4508fae3551SRodney W. Grimes 	bzero((char *)lp, sizeof *lp);
4515daa806dSPoul-Henning Kamp #if 0
4528fae3551SRodney W. Grimes 	/*
4538fae3551SRodney W. Grimes 	 * If we are not installing a boot program but we are installing a
4548fae3551SRodney W. Grimes 	 * label on disk then we must read the current bootarea so we don't
4558fae3551SRodney W. Grimes 	 * clobber the existing boot.
4568fae3551SRodney W. Grimes 	 */
4578fae3551SRodney W. Grimes 	if (!installboot) {
4588fae3551SRodney W. Grimes 		if (read(f, boot, BBSIZE) < BBSIZE)
459bef2080aSPhilippe Charnier 			err(4, "%s", specname);
4608fae3551SRodney W. Grimes 		bzero((char *)lp, sizeof *lp);
4618fae3551SRodney W. Grimes 		return (lp);
4628fae3551SRodney W. Grimes 	}
4635daa806dSPoul-Henning Kamp #endif
4648fae3551SRodney W. Grimes 	/*
4658fae3551SRodney W. Grimes 	 * We are installing a boot program.  Determine the name(s) and
4668fae3551SRodney W. Grimes 	 * read them into the appropriate places in the boot area.
4678fae3551SRodney W. Grimes 	 */
4685daa806dSPoul-Henning Kamp 	if (xxboot == NULL)
4695daa806dSPoul-Henning Kamp 		asprintf(&xxboot, "%s/boot", _PATH_BOOTDIR);
4708fae3551SRodney W. Grimes 
4718fae3551SRodney W. Grimes 	b = open(xxboot, O_RDONLY);
4728fae3551SRodney W. Grimes 	if (b < 0)
473bef2080aSPhilippe Charnier 		err(4, "%s", xxboot);
4748d48318bSPoul-Henning Kamp 	if (fstat(b, &sb) != 0)
4758d48318bSPoul-Henning Kamp 		err(4, "%s", xxboot);
4768d48318bSPoul-Henning Kamp 
4775daa806dSPoul-Henning Kamp 	if (sb.st_size == BBSIZE) {
4785daa806dSPoul-Henning Kamp 		n = read(b, (char *)boot, bbsize);
4795daa806dSPoul-Henning Kamp 		if (n != bbsize)
4805daa806dSPoul-Henning Kamp 			err(4, "%s", xxboot);
4815daa806dSPoul-Henning Kamp 	} else if (alphacksum) {
4825daa806dSPoul-Henning Kamp 		if (sb.st_size != BBSIZE - dp->d_secsize)
4835daa806dSPoul-Henning Kamp 			errx(4, "%s wrong size (%jd bytes, expected %jd bytes)",
4845daa806dSPoul-Henning Kamp 			    xxboot, (intmax_t)sb.st_size,
4855daa806dSPoul-Henning Kamp 			    (intmax_t)BBSIZE - dp->d_secsize);
486130cd73aSDoug Rabson 		/*
487130cd73aSDoug Rabson 		 * On the alpha, the primary bootstrap starts at the
488130cd73aSDoug Rabson 		 * second sector of the boot area.  The first sector
489130cd73aSDoug Rabson 		 * contains the label and must be edited to contain the
490130cd73aSDoug Rabson 		 * size and location of the primary bootstrap.
491130cd73aSDoug Rabson 		 */
492bc33ea1aSRuslan Ermilov 		n = read(b, (char *)boot + dp->d_secsize,
493dfbc3f0cSPoul-Henning Kamp 		    bbsize - dp->d_secsize);
494dfbc3f0cSPoul-Henning Kamp 		if (n != bbsize - dp->d_secsize)
495130cd73aSDoug Rabson 			err(4, "%s", xxboot);
496bc33ea1aSRuslan Ermilov 		bootinfo = (uint64_t *)((char *)boot + 480);
49721c729c2SBruce Evans 		bootinfo[0] = (n + dp->d_secsize - 1) / dp->d_secsize;
49821c729c2SBruce Evans 		bootinfo[1] = 1;	/* start at sector 1 */
49921c729c2SBruce Evans 		bootinfo[2] = 0;	/* flags (must be zero) */
500dfbc3f0cSPoul-Henning Kamp 	} else {
501dfbc3f0cSPoul-Henning Kamp 		errx(4, "%s is wrong size, is %jd bytes, expected %d bytes",
502dfbc3f0cSPoul-Henning Kamp 			    xxboot, (intmax_t)sb.st_size, bbsize);
503bc33ea1aSRuslan Ermilov 	}
5048d48318bSPoul-Henning Kamp 
5058fae3551SRodney W. Grimes 	(void)close(b);
5068fae3551SRodney W. Grimes 	return (lp);
5078fae3551SRodney W. Grimes }
5088fae3551SRodney W. Grimes 
50961de51caSJoerg Wunsch void
510326c7cdaSWarner Losh display(FILE *f, const struct disklabel *lp)
5118fae3551SRodney W. Grimes {
512326c7cdaSWarner Losh 	int i, j;
513326c7cdaSWarner Losh 	const struct partition *pp;
5148fae3551SRodney W. Grimes 
5158fae3551SRodney W. Grimes 	fprintf(f, "# %s:\n", specname);
516484c7804SJulian Elischer 	if (lp->d_type < DKMAXTYPES)
5178fae3551SRodney W. Grimes 		fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
5188fae3551SRodney W. Grimes 	else
5192a1deaaaSBruce Evans 		fprintf(f, "type: %u\n", lp->d_type);
52061de51caSJoerg Wunsch 	fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
52161de51caSJoerg Wunsch 		lp->d_typename);
52261de51caSJoerg Wunsch 	fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
52361de51caSJoerg Wunsch 		lp->d_packname);
5248fae3551SRodney W. Grimes 	fprintf(f, "flags:");
5258fae3551SRodney W. Grimes 	if (lp->d_flags & D_REMOVABLE)
5268fae3551SRodney W. Grimes 		fprintf(f, " removeable");
5278fae3551SRodney W. Grimes 	if (lp->d_flags & D_ECC)
5288fae3551SRodney W. Grimes 		fprintf(f, " ecc");
5298fae3551SRodney W. Grimes 	if (lp->d_flags & D_BADSECT)
5308fae3551SRodney W. Grimes 		fprintf(f, " badsect");
5318fae3551SRodney W. Grimes 	fprintf(f, "\n");
5322a1deaaaSBruce Evans 	fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
5332a1deaaaSBruce Evans 	fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
5342a1deaaaSBruce Evans 	fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
5352a1deaaaSBruce Evans 	fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
5362a1deaaaSBruce Evans 	fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
5372a1deaaaSBruce Evans 	fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
5382a1deaaaSBruce Evans 	fprintf(f, "rpm: %u\n", lp->d_rpm);
5392a1deaaaSBruce Evans 	fprintf(f, "interleave: %u\n", lp->d_interleave);
5402a1deaaaSBruce Evans 	fprintf(f, "trackskew: %u\n", lp->d_trackskew);
5412a1deaaaSBruce Evans 	fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
5422a1deaaaSBruce Evans 	fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
5432a1deaaaSBruce Evans 	    (u_long)lp->d_headswitch);
54461de51caSJoerg Wunsch 	fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
5452a1deaaaSBruce Evans 	    (u_long)lp->d_trkseek);
5468fae3551SRodney W. Grimes 	fprintf(f, "drivedata: ");
5478fae3551SRodney W. Grimes 	for (i = NDDATA - 1; i >= 0; i--)
5488fae3551SRodney W. Grimes 		if (lp->d_drivedata[i])
5498fae3551SRodney W. Grimes 			break;
5508fae3551SRodney W. Grimes 	if (i < 0)
5518fae3551SRodney W. Grimes 		i = 0;
5528fae3551SRodney W. Grimes 	for (j = 0; j <= i; j++)
5532a1deaaaSBruce Evans 		fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
5542a1deaaaSBruce Evans 	fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions);
5558fae3551SRodney W. Grimes 	fprintf(f,
556ca4693edSJustin T. Gibbs 	    "#        size   offset    fstype   [fsize bsize bps/cpg]\n");
5578fae3551SRodney W. Grimes 	pp = lp->d_partitions;
5588fae3551SRodney W. Grimes 	for (i = 0; i < lp->d_npartitions; i++, pp++) {
5598fae3551SRodney W. Grimes 		if (pp->p_size) {
5602a1deaaaSBruce Evans 			fprintf(f, "  %c: %8lu %8lu  ", 'a' + i,
5612a1deaaaSBruce Evans 			   (u_long)pp->p_size, (u_long)pp->p_offset);
562484c7804SJulian Elischer 			if (pp->p_fstype < FSMAXTYPES)
5638fae3551SRodney W. Grimes 				fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
5648fae3551SRodney W. Grimes 			else
5658fae3551SRodney W. Grimes 				fprintf(f, "%8d", pp->p_fstype);
5668fae3551SRodney W. Grimes 			switch (pp->p_fstype) {
5678fae3551SRodney W. Grimes 
5688fae3551SRodney W. Grimes 			case FS_UNUSED:				/* XXX */
5692a1deaaaSBruce Evans 				fprintf(f, "    %5lu %5lu %5.5s ",
5702a1deaaaSBruce Evans 				    (u_long)pp->p_fsize,
5712a1deaaaSBruce Evans 				    (u_long)(pp->p_fsize * pp->p_frag), "");
5728fae3551SRodney W. Grimes 				break;
5738fae3551SRodney W. Grimes 
5748fae3551SRodney W. Grimes 			case FS_BSDFFS:
5752a1deaaaSBruce Evans 				fprintf(f, "    %5lu %5lu %5u ",
5762a1deaaaSBruce Evans 				    (u_long)pp->p_fsize,
5772a1deaaaSBruce Evans 				    (u_long)(pp->p_fsize * pp->p_frag),
5788fae3551SRodney W. Grimes 				    pp->p_cpg);
5798fae3551SRodney W. Grimes 				break;
5808fae3551SRodney W. Grimes 
581ca4693edSJustin T. Gibbs 			case FS_BSDLFS:
5822a1deaaaSBruce Evans 				fprintf(f, "    %5lu %5lu %5d",
5832a1deaaaSBruce Evans 				    (u_long)pp->p_fsize,
5842a1deaaaSBruce Evans 				    (u_long)(pp->p_fsize * pp->p_frag),
585ca4693edSJustin T. Gibbs 				    pp->p_cpg);
586ca4693edSJustin T. Gibbs 				break;
587ca4693edSJustin T. Gibbs 
5888fae3551SRodney W. Grimes 			default:
5898fae3551SRodney W. Grimes 				fprintf(f, "%20.20s", "");
5908fae3551SRodney W. Grimes 				break;
5918fae3551SRodney W. Grimes 			}
5922a1deaaaSBruce Evans 			fprintf(f, "\t# (Cyl. %4lu",
5932a1deaaaSBruce Evans 			    (u_long)(pp->p_offset / lp->d_secpercyl));
5948fae3551SRodney W. Grimes 			if (pp->p_offset % lp->d_secpercyl)
5958fae3551SRodney W. Grimes 			    putc('*', f);
5968fae3551SRodney W. Grimes 			else
5978fae3551SRodney W. Grimes 			    putc(' ', f);
5982a1deaaaSBruce Evans 			fprintf(f, "- %lu",
5992a1deaaaSBruce Evans 			    (u_long)((pp->p_offset + pp->p_size +
6002a1deaaaSBruce Evans 			    lp->d_secpercyl - 1) /
6012a1deaaaSBruce Evans 			    lp->d_secpercyl - 1));
6028fae3551SRodney W. Grimes 			if (pp->p_size % lp->d_secpercyl)
6038fae3551SRodney W. Grimes 			    putc('*', f);
6048fae3551SRodney W. Grimes 			fprintf(f, ")\n");
6058fae3551SRodney W. Grimes 		}
6068fae3551SRodney W. Grimes 	}
6078fae3551SRodney W. Grimes 	fflush(f);
6088fae3551SRodney W. Grimes }
6098fae3551SRodney W. Grimes 
61061de51caSJoerg Wunsch int
6115daa806dSPoul-Henning Kamp edit(struct disklabel *lp)
6128fae3551SRodney W. Grimes {
613326c7cdaSWarner Losh 	int c, fd;
6148fae3551SRodney W. Grimes 	struct disklabel label;
615722ceb3fSWarner Losh 	FILE *fp;
6168fae3551SRodney W. Grimes 
617722ceb3fSWarner Losh 	if ((fd = mkstemp(tmpfil)) == -1 ||
618722ceb3fSWarner Losh 	    (fp = fdopen(fd, "w")) == NULL) {
6196bd343a9SPhilippe Charnier 		warnx("can't create %s", tmpfil);
6208fae3551SRodney W. Grimes 		return (1);
6218fae3551SRodney W. Grimes 	}
622722ceb3fSWarner Losh 	display(fp, lp);
623722ceb3fSWarner Losh 	fclose(fp);
6248fae3551SRodney W. Grimes 	for (;;) {
6258fae3551SRodney W. Grimes 		if (!editit())
6268fae3551SRodney W. Grimes 			break;
627722ceb3fSWarner Losh 		fp = fopen(tmpfil, "r");
628722ceb3fSWarner Losh 		if (fp == NULL) {
6296bd343a9SPhilippe Charnier 			warnx("can't reopen %s for reading", tmpfil);
6308fae3551SRodney W. Grimes 			break;
6318fae3551SRodney W. Grimes 		}
6328fae3551SRodney W. Grimes 		bzero((char *)&label, sizeof(label));
633722ceb3fSWarner Losh 		if (getasciilabel(fp, &label)) {
6348fae3551SRodney W. Grimes 			*lp = label;
6355daa806dSPoul-Henning Kamp 			if (writelabel(bootarea, lp) == 0) {
636722ceb3fSWarner Losh 				fclose(fp);
6378fae3551SRodney W. Grimes 				(void) unlink(tmpfil);
6388fae3551SRodney W. Grimes 				return (0);
6398fae3551SRodney W. Grimes 			}
6408fae3551SRodney W. Grimes 		}
641722ceb3fSWarner Losh 		fclose(fp);
6428fae3551SRodney W. Grimes 		printf("re-edit the label? [y]: "); fflush(stdout);
6438fae3551SRodney W. Grimes 		c = getchar();
6448fae3551SRodney W. Grimes 		if (c != EOF && c != (int)'\n')
6458fae3551SRodney W. Grimes 			while (getchar() != (int)'\n')
6468fae3551SRodney W. Grimes 				;
6478fae3551SRodney W. Grimes 		if  (c == (int)'n')
6488fae3551SRodney W. Grimes 			break;
6498fae3551SRodney W. Grimes 	}
6508fae3551SRodney W. Grimes 	(void) unlink(tmpfil);
6518fae3551SRodney W. Grimes 	return (1);
6528fae3551SRodney W. Grimes }
6538fae3551SRodney W. Grimes 
65461de51caSJoerg Wunsch int
655326c7cdaSWarner Losh editit(void)
6568fae3551SRodney W. Grimes {
657326c7cdaSWarner Losh 	int pid, xpid;
658c8223965SMark Murray 	int locstat, omask;
659c8223965SMark Murray 	const char *ed;
6608fae3551SRodney W. Grimes 
6618fae3551SRodney W. Grimes 	omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
6628fae3551SRodney W. Grimes 	while ((pid = fork()) < 0) {
6638fae3551SRodney W. Grimes 		if (errno == EPROCLIM) {
6646bd343a9SPhilippe Charnier 			warnx("you have too many processes");
6658fae3551SRodney W. Grimes 			return(0);
6668fae3551SRodney W. Grimes 		}
6678fae3551SRodney W. Grimes 		if (errno != EAGAIN) {
6686bd343a9SPhilippe Charnier 			warn("fork");
6698fae3551SRodney W. Grimes 			return(0);
6708fae3551SRodney W. Grimes 		}
6718fae3551SRodney W. Grimes 		sleep(1);
6728fae3551SRodney W. Grimes 	}
6738fae3551SRodney W. Grimes 	if (pid == 0) {
6748fae3551SRodney W. Grimes 		sigsetmask(omask);
6758fae3551SRodney W. Grimes 		setgid(getgid());
6768fae3551SRodney W. Grimes 		setuid(getuid());
6778fae3551SRodney W. Grimes 		if ((ed = getenv("EDITOR")) == (char *)0)
6788fae3551SRodney W. Grimes 			ed = DEFEDITOR;
6797bc6d015SBrian Somers 		execlp(ed, ed, tmpfil, (char *)0);
6806bd343a9SPhilippe Charnier 		err(1, "%s", ed);
6818fae3551SRodney W. Grimes 	}
682c8223965SMark Murray 	while ((xpid = wait(&locstat)) >= 0)
6838fae3551SRodney W. Grimes 		if (xpid == pid)
6848fae3551SRodney W. Grimes 			break;
6858fae3551SRodney W. Grimes 	sigsetmask(omask);
686c8223965SMark Murray 	return(!locstat);
6878fae3551SRodney W. Grimes }
6888fae3551SRodney W. Grimes 
6898fae3551SRodney W. Grimes char *
690326c7cdaSWarner Losh skip(char *cp)
6918fae3551SRodney W. Grimes {
6928fae3551SRodney W. Grimes 
6938fae3551SRodney W. Grimes 	while (*cp != '\0' && isspace(*cp))
6948fae3551SRodney W. Grimes 		cp++;
6958fae3551SRodney W. Grimes 	if (*cp == '\0' || *cp == '#')
696326c7cdaSWarner Losh 		return (NULL);
6978fae3551SRodney W. Grimes 	return (cp);
6988fae3551SRodney W. Grimes }
6998fae3551SRodney W. Grimes 
7008fae3551SRodney W. Grimes char *
701326c7cdaSWarner Losh word(char *cp)
7028fae3551SRodney W. Grimes {
703326c7cdaSWarner Losh 	char c;
7048fae3551SRodney W. Grimes 
7058fae3551SRodney W. Grimes 	while (*cp != '\0' && !isspace(*cp) && *cp != '#')
7068fae3551SRodney W. Grimes 		cp++;
7078fae3551SRodney W. Grimes 	if ((c = *cp) != '\0') {
7088fae3551SRodney W. Grimes 		*cp++ = '\0';
7098fae3551SRodney W. Grimes 		if (c != '#')
7108fae3551SRodney W. Grimes 			return (skip(cp));
7118fae3551SRodney W. Grimes 	}
712326c7cdaSWarner Losh 	return (NULL);
7138fae3551SRodney W. Grimes }
7148fae3551SRodney W. Grimes 
7158fae3551SRodney W. Grimes /*
7168fae3551SRodney W. Grimes  * Read an ascii label in from fd f,
7178fae3551SRodney W. Grimes  * in the same format as that put out by display(),
7188fae3551SRodney W. Grimes  * and fill in lp.
7198fae3551SRodney W. Grimes  */
72061de51caSJoerg Wunsch int
721326c7cdaSWarner Losh getasciilabel(FILE *f, struct disklabel *lp)
7228fae3551SRodney W. Grimes {
7236b0ff5f5SPoul-Henning Kamp 	char *cp;
7246b0ff5f5SPoul-Henning Kamp 	const char **cpp;
725484c7804SJulian Elischer 	u_int part;
7266b0ff5f5SPoul-Henning Kamp 	char *tp, line[BUFSIZ];
727484c7804SJulian Elischer 	u_long v;
728484c7804SJulian Elischer 	int lineno = 0, errors = 0;
729326c7cdaSWarner Losh 	int i;
7308fae3551SRodney W. Grimes 
731b0459c58SDag-Erling Smørgrav 	bzero(&part_set, sizeof(part_set));
732b0459c58SDag-Erling Smørgrav 	bzero(&part_size_type, sizeof(part_size_type));
733b0459c58SDag-Erling Smørgrav 	bzero(&part_offset_type, sizeof(part_offset_type));
7348fae3551SRodney W. Grimes 	lp->d_bbsize = BBSIZE;				/* XXX */
73577068a7fSPoul-Henning Kamp 	lp->d_sbsize = 0;				/* XXX */
7368fae3551SRodney W. Grimes 	while (fgets(line, sizeof(line) - 1, f)) {
7378fae3551SRodney W. Grimes 		lineno++;
73861de51caSJoerg Wunsch 		if ((cp = index(line,'\n')) != 0)
7398fae3551SRodney W. Grimes 			*cp = '\0';
7408fae3551SRodney W. Grimes 		cp = skip(line);
7418fae3551SRodney W. Grimes 		if (cp == NULL)
7428fae3551SRodney W. Grimes 			continue;
7438fae3551SRodney W. Grimes 		tp = index(cp, ':');
7448fae3551SRodney W. Grimes 		if (tp == NULL) {
7458fae3551SRodney W. Grimes 			fprintf(stderr, "line %d: syntax error\n", lineno);
7468fae3551SRodney W. Grimes 			errors++;
7478fae3551SRodney W. Grimes 			continue;
7488fae3551SRodney W. Grimes 		}
7498fae3551SRodney W. Grimes 		*tp++ = '\0', tp = skip(tp);
7508fae3551SRodney W. Grimes 		if (streq(cp, "type")) {
7518fae3551SRodney W. Grimes 			if (tp == NULL)
752c8223965SMark Murray 				tp = unknown;
7538fae3551SRodney W. Grimes 			cpp = dktypenames;
7548fae3551SRodney W. Grimes 			for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
7556b0ff5f5SPoul-Henning Kamp 				if (*cpp && streq(*cpp, tp)) {
7568fae3551SRodney W. Grimes 					lp->d_type = cpp - dktypenames;
75709dbd070SIan Dowse 					break;
7588fae3551SRodney W. Grimes 				}
75909dbd070SIan Dowse 			if (cpp < &dktypenames[DKMAXTYPES])
76009dbd070SIan Dowse 				continue;
761484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
762484c7804SJulian Elischer 			if (v >= DKMAXTYPES)
763484c7804SJulian Elischer 				fprintf(stderr, "line %d:%s %lu\n", lineno,
7648fae3551SRodney W. Grimes 				    "Warning, unknown disk type", v);
7658fae3551SRodney W. Grimes 			lp->d_type = v;
7668fae3551SRodney W. Grimes 			continue;
7678fae3551SRodney W. Grimes 		}
7688fae3551SRodney W. Grimes 		if (streq(cp, "flags")) {
7698fae3551SRodney W. Grimes 			for (v = 0; (cp = tp) && *cp != '\0';) {
7708fae3551SRodney W. Grimes 				tp = word(cp);
7718fae3551SRodney W. Grimes 				if (streq(cp, "removeable"))
7728fae3551SRodney W. Grimes 					v |= D_REMOVABLE;
7738fae3551SRodney W. Grimes 				else if (streq(cp, "ecc"))
7748fae3551SRodney W. Grimes 					v |= D_ECC;
7758fae3551SRodney W. Grimes 				else if (streq(cp, "badsect"))
7768fae3551SRodney W. Grimes 					v |= D_BADSECT;
7778fae3551SRodney W. Grimes 				else {
7788fae3551SRodney W. Grimes 					fprintf(stderr,
7798fae3551SRodney W. Grimes 					    "line %d: %s: bad flag\n",
7808fae3551SRodney W. Grimes 					    lineno, cp);
7818fae3551SRodney W. Grimes 					errors++;
7828fae3551SRodney W. Grimes 				}
7838fae3551SRodney W. Grimes 			}
7848fae3551SRodney W. Grimes 			lp->d_flags = v;
7858fae3551SRodney W. Grimes 			continue;
7868fae3551SRodney W. Grimes 		}
7878fae3551SRodney W. Grimes 		if (streq(cp, "drivedata")) {
7888fae3551SRodney W. Grimes 			for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
789484c7804SJulian Elischer 				lp->d_drivedata[i++] = strtoul(cp, NULL, 10);
7908fae3551SRodney W. Grimes 				tp = word(cp);
7918fae3551SRodney W. Grimes 			}
7928fae3551SRodney W. Grimes 			continue;
7938fae3551SRodney W. Grimes 		}
794484c7804SJulian Elischer 		if (sscanf(cp, "%lu partitions", &v) == 1) {
795484c7804SJulian Elischer 			if (v == 0 || v > MAXPARTITIONS) {
7968fae3551SRodney W. Grimes 				fprintf(stderr,
7978fae3551SRodney W. Grimes 				    "line %d: bad # of partitions\n", lineno);
7988fae3551SRodney W. Grimes 				lp->d_npartitions = MAXPARTITIONS;
7998fae3551SRodney W. Grimes 				errors++;
8008fae3551SRodney W. Grimes 			} else
8018fae3551SRodney W. Grimes 				lp->d_npartitions = v;
8028fae3551SRodney W. Grimes 			continue;
8038fae3551SRodney W. Grimes 		}
8048fae3551SRodney W. Grimes 		if (tp == NULL)
805c8223965SMark Murray 			tp = blank;
8068fae3551SRodney W. Grimes 		if (streq(cp, "disk")) {
8078fae3551SRodney W. Grimes 			strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
8088fae3551SRodney W. Grimes 			continue;
8098fae3551SRodney W. Grimes 		}
8108fae3551SRodney W. Grimes 		if (streq(cp, "label")) {
8118fae3551SRodney W. Grimes 			strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
8128fae3551SRodney W. Grimes 			continue;
8138fae3551SRodney W. Grimes 		}
8148fae3551SRodney W. Grimes 		if (streq(cp, "bytes/sector")) {
815484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
816484c7804SJulian Elischer 			if (v == 0 || (v % DEV_BSIZE) != 0) {
8178fae3551SRodney W. Grimes 				fprintf(stderr,
8188fae3551SRodney W. Grimes 				    "line %d: %s: bad sector size\n",
8198fae3551SRodney W. Grimes 				    lineno, tp);
8208fae3551SRodney W. Grimes 				errors++;
8218fae3551SRodney W. Grimes 			} else
8228fae3551SRodney W. Grimes 				lp->d_secsize = v;
8238fae3551SRodney W. Grimes 			continue;
8248fae3551SRodney W. Grimes 		}
8258fae3551SRodney W. Grimes 		if (streq(cp, "sectors/track")) {
826484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
827484c7804SJulian Elischer #if (ULONG_MAX != 0xffffffffUL)
828484c7804SJulian Elischer 			if (v == 0 || v > 0xffffffff) {
829484c7804SJulian Elischer #else
830484c7804SJulian Elischer 			if (v == 0) {
831484c7804SJulian Elischer #endif
8328fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
8338fae3551SRodney W. Grimes 				    lineno, tp, cp);
8348fae3551SRodney W. Grimes 				errors++;
8358fae3551SRodney W. Grimes 			} else
8368fae3551SRodney W. Grimes 				lp->d_nsectors = v;
8378fae3551SRodney W. Grimes 			continue;
8388fae3551SRodney W. Grimes 		}
8398fae3551SRodney W. Grimes 		if (streq(cp, "sectors/cylinder")) {
840484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
841484c7804SJulian Elischer 			if (v == 0) {
8428fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
8438fae3551SRodney W. Grimes 				    lineno, tp, cp);
8448fae3551SRodney W. Grimes 				errors++;
8458fae3551SRodney W. Grimes 			} else
8468fae3551SRodney W. Grimes 				lp->d_secpercyl = v;
8478fae3551SRodney W. Grimes 			continue;
8488fae3551SRodney W. Grimes 		}
8498fae3551SRodney W. Grimes 		if (streq(cp, "tracks/cylinder")) {
850484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
851484c7804SJulian Elischer 			if (v == 0) {
8528fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
8538fae3551SRodney W. Grimes 				    lineno, tp, cp);
8548fae3551SRodney W. Grimes 				errors++;
8558fae3551SRodney W. Grimes 			} else
8568fae3551SRodney W. Grimes 				lp->d_ntracks = v;
8578fae3551SRodney W. Grimes 			continue;
8588fae3551SRodney W. Grimes 		}
8598fae3551SRodney W. Grimes 		if (streq(cp, "cylinders")) {
860484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
861484c7804SJulian Elischer 			if (v == 0) {
8628fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
8638fae3551SRodney W. Grimes 				    lineno, tp, cp);
8648fae3551SRodney W. Grimes 				errors++;
8658fae3551SRodney W. Grimes 			} else
8668fae3551SRodney W. Grimes 				lp->d_ncylinders = v;
8678fae3551SRodney W. Grimes 			continue;
8688fae3551SRodney W. Grimes 		}
869f75dd518SBruce Evans 		if (streq(cp, "sectors/unit")) {
870484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
871484c7804SJulian Elischer 			if (v == 0) {
872f75dd518SBruce Evans 				fprintf(stderr, "line %d: %s: bad %s\n",
873f75dd518SBruce Evans 				    lineno, tp, cp);
874f75dd518SBruce Evans 				errors++;
875f75dd518SBruce Evans 			} else
876f75dd518SBruce Evans 				lp->d_secperunit = v;
877f75dd518SBruce Evans 			continue;
878f75dd518SBruce Evans 		}
8798fae3551SRodney W. Grimes 		if (streq(cp, "rpm")) {
880484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
881484c7804SJulian Elischer 			if (v == 0 || v > USHRT_MAX) {
8828fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
8838fae3551SRodney W. Grimes 				    lineno, tp, cp);
8848fae3551SRodney W. Grimes 				errors++;
8858fae3551SRodney W. Grimes 			} else
8868fae3551SRodney W. Grimes 				lp->d_rpm = v;
8878fae3551SRodney W. Grimes 			continue;
8888fae3551SRodney W. Grimes 		}
8898fae3551SRodney W. Grimes 		if (streq(cp, "interleave")) {
890484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
891484c7804SJulian Elischer 			if (v == 0 || v > USHRT_MAX) {
8928fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
8938fae3551SRodney W. Grimes 				    lineno, tp, cp);
8948fae3551SRodney W. Grimes 				errors++;
8958fae3551SRodney W. Grimes 			} else
8968fae3551SRodney W. Grimes 				lp->d_interleave = v;
8978fae3551SRodney W. Grimes 			continue;
8988fae3551SRodney W. Grimes 		}
8998fae3551SRodney W. Grimes 		if (streq(cp, "trackskew")) {
900484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
901484c7804SJulian Elischer 			if (v > USHRT_MAX) {
9028fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
9038fae3551SRodney W. Grimes 				    lineno, tp, cp);
9048fae3551SRodney W. Grimes 				errors++;
9058fae3551SRodney W. Grimes 			} else
9068fae3551SRodney W. Grimes 				lp->d_trackskew = v;
9078fae3551SRodney W. Grimes 			continue;
9088fae3551SRodney W. Grimes 		}
9098fae3551SRodney W. Grimes 		if (streq(cp, "cylinderskew")) {
910484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
911484c7804SJulian Elischer 			if (v > USHRT_MAX) {
9128fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
9138fae3551SRodney W. Grimes 				    lineno, tp, cp);
9148fae3551SRodney W. Grimes 				errors++;
9158fae3551SRodney W. Grimes 			} else
9168fae3551SRodney W. Grimes 				lp->d_cylskew = v;
9178fae3551SRodney W. Grimes 			continue;
9188fae3551SRodney W. Grimes 		}
9198fae3551SRodney W. Grimes 		if (streq(cp, "headswitch")) {
920484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
9218fae3551SRodney W. Grimes 			lp->d_headswitch = v;
9228fae3551SRodney W. Grimes 			continue;
9238fae3551SRodney W. Grimes 		}
9248fae3551SRodney W. Grimes 		if (streq(cp, "track-to-track seek")) {
925484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
9268fae3551SRodney W. Grimes 			lp->d_trkseek = v;
9278fae3551SRodney W. Grimes 			continue;
9288fae3551SRodney W. Grimes 		}
9293233afaeSJohn W. De Boskey 		/* the ':' was removed above */
93067b46708SIan Dowse 		if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') {
93167b46708SIan Dowse 			fprintf(stderr,
93267b46708SIan Dowse 			    "line %d: %s: Unknown disklabel field\n", lineno,
93367b46708SIan Dowse 			    cp);
93467b46708SIan Dowse 			errors++;
93567b46708SIan Dowse 			continue;
93667b46708SIan Dowse 		}
93767b46708SIan Dowse 
93867b46708SIan Dowse 		/* Process a partition specification line. */
9393233afaeSJohn W. De Boskey 		part = *cp - 'a';
9403233afaeSJohn W. De Boskey 		if (part >= lp->d_npartitions) {
9418fae3551SRodney W. Grimes 			fprintf(stderr,
9423233afaeSJohn W. De Boskey 			    "line %d: partition name out of range a-%c: %s\n",
9433233afaeSJohn W. De Boskey 			    lineno, 'a' + lp->d_npartitions - 1, cp);
9448fae3551SRodney W. Grimes 			errors++;
9458fae3551SRodney W. Grimes 			continue;
9468fae3551SRodney W. Grimes 		}
9473233afaeSJohn W. De Boskey 		part_set[part] = 1;
94867b46708SIan Dowse 
94967b46708SIan Dowse 		if (getasciipartspec(tp, lp, part, lineno) != 0) {
95067b46708SIan Dowse 			errors++;
95167b46708SIan Dowse 			break;
95267b46708SIan Dowse 		}
95367b46708SIan Dowse 	}
95467b46708SIan Dowse 	errors += checklabel(lp);
95567b46708SIan Dowse 	return (errors == 0);
95667b46708SIan Dowse }
95767b46708SIan Dowse 
95867b46708SIan Dowse #define NXTNUM(n) do { \
95913e0abcbSPaul Traina 	if (tp == NULL) { \
96013e0abcbSPaul Traina 		fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
96167b46708SIan Dowse 		return (1); \
96213e0abcbSPaul Traina 	} else { \
9638fae3551SRodney W. Grimes 		cp = tp, tp = word(cp); \
964484c7804SJulian Elischer 		(n) = strtoul(cp, NULL, 10); \
96513e0abcbSPaul Traina 	} \
96667b46708SIan Dowse } while (0)
96767b46708SIan Dowse 
9683233afaeSJohn W. De Boskey /* retain 1 character following number */
96967b46708SIan Dowse #define NXTWORD(w,n) do { \
9703233afaeSJohn W. De Boskey 	if (tp == NULL) { \
9713233afaeSJohn W. De Boskey 		fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
97267b46708SIan Dowse 		return (1); \
9733233afaeSJohn W. De Boskey 	} else { \
9743233afaeSJohn W. De Boskey 	        char *tmp; \
9753233afaeSJohn W. De Boskey 		cp = tp, tp = word(cp); \
976484c7804SJulian Elischer 	        (n) = strtoul(cp, &tmp, 10); \
9773233afaeSJohn W. De Boskey 		if (tmp) (w) = *tmp; \
9783233afaeSJohn W. De Boskey 	} \
97967b46708SIan Dowse } while (0)
98067b46708SIan Dowse 
98167b46708SIan Dowse /*
98267b46708SIan Dowse  * Read a partition line into partition `part' in the specified disklabel.
98367b46708SIan Dowse  * Return 0 on success, 1 on failure.
98467b46708SIan Dowse  */
98567b46708SIan Dowse int
98667b46708SIan Dowse getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno)
98767b46708SIan Dowse {
98867b46708SIan Dowse 	struct partition *pp;
98967b46708SIan Dowse 	char *cp;
99067b46708SIan Dowse 	const char **cpp;
991484c7804SJulian Elischer 	u_long v;
99267b46708SIan Dowse 
99367b46708SIan Dowse 	pp = &lp->d_partitions[part];
99467b46708SIan Dowse 	cp = NULL;
99567b46708SIan Dowse 
9963233afaeSJohn W. De Boskey 	v = 0;
9973233afaeSJohn W. De Boskey 	NXTWORD(part_size_type[part],v);
998484c7804SJulian Elischer 	if (v == 0 && part_size_type[part] != '*') {
999484c7804SJulian Elischer 		fprintf(stderr,
1000484c7804SJulian Elischer 		    "line %d: %s: bad partition size\n", lineno, cp);
100167b46708SIan Dowse 		return (1);
100267b46708SIan Dowse 	}
10038fae3551SRodney W. Grimes 	pp->p_size = v;
10043233afaeSJohn W. De Boskey 
10053233afaeSJohn W. De Boskey 	v = 0;
10063233afaeSJohn W. De Boskey 	NXTWORD(part_offset_type[part],v);
1007484c7804SJulian Elischer 	if (v == 0 && part_offset_type[part] != '*' &&
1008484c7804SJulian Elischer 	    part_offset_type[part] != '\0') {
1009484c7804SJulian Elischer 		fprintf(stderr,
1010484c7804SJulian Elischer 		    "line %d: %s: bad partition offset\n", lineno, cp);
101167b46708SIan Dowse 		return (1);
101267b46708SIan Dowse 	}
10138fae3551SRodney W. Grimes 	pp->p_offset = v;
101429f3f095SYaroslav Tykhiy 	if (tp == NULL) {
101529f3f095SYaroslav Tykhiy 		fprintf(stderr, "line %d: missing file system type\n", lineno);
101629f3f095SYaroslav Tykhiy 		return (1);
101729f3f095SYaroslav Tykhiy 	}
10188fae3551SRodney W. Grimes 	cp = tp, tp = word(cp);
101967b46708SIan Dowse 	for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++)
102067b46708SIan Dowse 		if (*cpp && streq(*cpp, cp))
102167b46708SIan Dowse 			break;
102267b46708SIan Dowse 	if (*cpp != NULL) {
102367b46708SIan Dowse 		pp->p_fstype = cpp - fstypenames;
102467b46708SIan Dowse 	} else {
10258fae3551SRodney W. Grimes 		if (isdigit(*cp))
1026484c7804SJulian Elischer 			v = strtoul(cp, NULL, 10);
10278fae3551SRodney W. Grimes 		else
10288fae3551SRodney W. Grimes 			v = FSMAXTYPES;
1029484c7804SJulian Elischer 		if (v >= FSMAXTYPES) {
10303233afaeSJohn W. De Boskey 			fprintf(stderr,
103167b46708SIan Dowse 			    "line %d: Warning, unknown file system type %s\n",
10323233afaeSJohn W. De Boskey 			    lineno, cp);
10338fae3551SRodney W. Grimes 			v = FS_UNUSED;
10348fae3551SRodney W. Grimes 		}
10358fae3551SRodney W. Grimes 		pp->p_fstype = v;
103667b46708SIan Dowse 	}
103767b46708SIan Dowse 
10388fae3551SRodney W. Grimes 	switch (pp->p_fstype) {
10393233afaeSJohn W. De Boskey 	case FS_UNUSED:
10403233afaeSJohn W. De Boskey 		/*
10413233afaeSJohn W. De Boskey 		 * allow us to accept defaults for
10423233afaeSJohn W. De Boskey 		 * fsize/frag/cpg
10433233afaeSJohn W. De Boskey 		 */
10443233afaeSJohn W. De Boskey 		if (tp) {
10458fae3551SRodney W. Grimes 			NXTNUM(pp->p_fsize);
10468fae3551SRodney W. Grimes 			if (pp->p_fsize == 0)
10478fae3551SRodney W. Grimes 				break;
10488fae3551SRodney W. Grimes 			NXTNUM(v);
10498fae3551SRodney W. Grimes 			pp->p_frag = v / pp->p_fsize;
10503233afaeSJohn W. De Boskey 		}
10513233afaeSJohn W. De Boskey 		/* else default to 0's */
10528fae3551SRodney W. Grimes 		break;
10538fae3551SRodney W. Grimes 
10543233afaeSJohn W. De Boskey 	/* These happen to be the same */
10558fae3551SRodney W. Grimes 	case FS_BSDFFS:
1056ca4693edSJustin T. Gibbs 	case FS_BSDLFS:
10573233afaeSJohn W. De Boskey 		if (tp) {
1058ca4693edSJustin T. Gibbs 			NXTNUM(pp->p_fsize);
1059ca4693edSJustin T. Gibbs 			if (pp->p_fsize == 0)
1060ca4693edSJustin T. Gibbs 				break;
1061ca4693edSJustin T. Gibbs 			NXTNUM(v);
1062ca4693edSJustin T. Gibbs 			pp->p_frag = v / pp->p_fsize;
1063ca4693edSJustin T. Gibbs 			NXTNUM(pp->p_cpg);
10643233afaeSJohn W. De Boskey 		} else {
10653233afaeSJohn W. De Boskey 			/*
106667b46708SIan Dowse 			 * FIX! poor attempt at adaptive
10673233afaeSJohn W. De Boskey 			 */
10683233afaeSJohn W. De Boskey 			/* 1 GB */
106967b46708SIan Dowse 			if (pp->p_size < 1024*1024*1024 / lp->d_secsize) {
107067b46708SIan Dowse 				/*
107167b46708SIan Dowse 				 * FIX! These are too low, but are traditional
107267b46708SIan Dowse 				 */
1073f1a7b7eeSIan Dowse 				pp->p_fsize = DEFAULT_NEWFS_FRAG;
1074f1a7b7eeSIan Dowse 				pp->p_frag = DEFAULT_NEWFS_BLOCK /
1075f1a7b7eeSIan Dowse 				    DEFAULT_NEWFS_FRAG;
10763233afaeSJohn W. De Boskey 				pp->p_cpg = DEFAULT_NEWFS_CPG;
10773233afaeSJohn W. De Boskey 			} else {
1078f1a7b7eeSIan Dowse 				pp->p_fsize = BIG_NEWFS_FRAG;
1079f1a7b7eeSIan Dowse 				pp->p_frag = BIG_NEWFS_BLOCK /
1080f1a7b7eeSIan Dowse 				    BIG_NEWFS_FRAG;
10813233afaeSJohn W. De Boskey 				pp->p_cpg = BIG_NEWFS_CPG;
10823233afaeSJohn W. De Boskey 			}
10833233afaeSJohn W. De Boskey 		}
10848fae3551SRodney W. Grimes 	default:
10858fae3551SRodney W. Grimes 		break;
10868fae3551SRodney W. Grimes 	}
108767b46708SIan Dowse 	return (0);
10888fae3551SRodney W. Grimes }
10898fae3551SRodney W. Grimes 
10908fae3551SRodney W. Grimes /*
10918fae3551SRodney W. Grimes  * Check disklabel for errors and fill in
10928fae3551SRodney W. Grimes  * derived fields according to supplied values.
10938fae3551SRodney W. Grimes  */
109461de51caSJoerg Wunsch int
1095326c7cdaSWarner Losh checklabel(struct disklabel *lp)
10968fae3551SRodney W. Grimes {
1097326c7cdaSWarner Losh 	struct partition *pp;
10988fae3551SRodney W. Grimes 	int i, errors = 0;
10998fae3551SRodney W. Grimes 	char part;
1100484c7804SJulian Elischer 	u_long total_size, total_percent, current_offset;
11013233afaeSJohn W. De Boskey 	int seen_default_offset;
11023233afaeSJohn W. De Boskey 	int hog_part;
11033233afaeSJohn W. De Boskey 	int j;
11043233afaeSJohn W. De Boskey 	struct partition *pp2;
11058fae3551SRodney W. Grimes 
11068fae3551SRodney W. Grimes 	if (lp->d_secsize == 0) {
11072a1deaaaSBruce Evans 		fprintf(stderr, "sector size 0\n");
11088fae3551SRodney W. Grimes 		return (1);
11098fae3551SRodney W. Grimes 	}
11108fae3551SRodney W. Grimes 	if (lp->d_nsectors == 0) {
11112a1deaaaSBruce Evans 		fprintf(stderr, "sectors/track 0\n");
11128fae3551SRodney W. Grimes 		return (1);
11138fae3551SRodney W. Grimes 	}
11148fae3551SRodney W. Grimes 	if (lp->d_ntracks == 0) {
11152a1deaaaSBruce Evans 		fprintf(stderr, "tracks/cylinder 0\n");
11168fae3551SRodney W. Grimes 		return (1);
11178fae3551SRodney W. Grimes 	}
11188fae3551SRodney W. Grimes 	if  (lp->d_ncylinders == 0) {
11192a1deaaaSBruce Evans 		fprintf(stderr, "cylinders/unit 0\n");
11208fae3551SRodney W. Grimes 		errors++;
11218fae3551SRodney W. Grimes 	}
11228fae3551SRodney W. Grimes 	if (lp->d_rpm == 0)
11232a1deaaaSBruce Evans 		Warning("revolutions/minute 0");
11248fae3551SRodney W. Grimes 	if (lp->d_secpercyl == 0)
11258fae3551SRodney W. Grimes 		lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
11268fae3551SRodney W. Grimes 	if (lp->d_secperunit == 0)
11278fae3551SRodney W. Grimes 		lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
11288fae3551SRodney W. Grimes 	if (lp->d_bbsize == 0) {
11292a1deaaaSBruce Evans 		fprintf(stderr, "boot block size 0\n");
11308fae3551SRodney W. Grimes 		errors++;
11318fae3551SRodney W. Grimes 	} else if (lp->d_bbsize % lp->d_secsize)
11328fae3551SRodney W. Grimes 		Warning("boot block size %% sector-size != 0");
11338fae3551SRodney W. Grimes 	if (lp->d_npartitions > MAXPARTITIONS)
11342a1deaaaSBruce Evans 		Warning("number of partitions (%lu) > MAXPARTITIONS (%d)",
11352a1deaaaSBruce Evans 		    (u_long)lp->d_npartitions, MAXPARTITIONS);
11363233afaeSJohn W. De Boskey 
11373233afaeSJohn W. De Boskey 	/* first allocate space to the partitions, then offsets */
11383233afaeSJohn W. De Boskey 	total_size = 0; /* in sectors */
11393233afaeSJohn W. De Boskey 	total_percent = 0; /* in percent */
11403233afaeSJohn W. De Boskey 	hog_part = -1;
11413233afaeSJohn W. De Boskey 	/* find all fixed partitions */
11423233afaeSJohn W. De Boskey 	for (i = 0; i < lp->d_npartitions; i++) {
11433233afaeSJohn W. De Boskey 		pp = &lp->d_partitions[i];
11443233afaeSJohn W. De Boskey 		if (part_set[i]) {
11453233afaeSJohn W. De Boskey 			if (part_size_type[i] == '*') {
11466b0ff5f5SPoul-Henning Kamp 				if (i == RAW_PART) {
11473233afaeSJohn W. De Boskey 					pp->p_size = lp->d_secperunit;
11483233afaeSJohn W. De Boskey 				} else {
11493233afaeSJohn W. De Boskey 					if (hog_part != -1)
11503233afaeSJohn W. De Boskey 						Warning("Too many '*' partitions (%c and %c)",
11513233afaeSJohn W. De Boskey 						    hog_part + 'a',i + 'a');
11523233afaeSJohn W. De Boskey 					else
11533233afaeSJohn W. De Boskey 						hog_part = i;
11543233afaeSJohn W. De Boskey 				}
11553233afaeSJohn W. De Boskey 			} else {
11568d3105e8SWarner Losh 				off_t size;
11573233afaeSJohn W. De Boskey 
11583233afaeSJohn W. De Boskey 				size = pp->p_size;
11593233afaeSJohn W. De Boskey 				switch (part_size_type[i]) {
11603233afaeSJohn W. De Boskey 				case '%':
11613233afaeSJohn W. De Boskey 					total_percent += size;
11623233afaeSJohn W. De Boskey 					break;
11633233afaeSJohn W. De Boskey 				case 'k':
11643233afaeSJohn W. De Boskey 				case 'K':
11658d3105e8SWarner Losh 					size *= 1024ULL;
11663233afaeSJohn W. De Boskey 					break;
11673233afaeSJohn W. De Boskey 				case 'm':
11683233afaeSJohn W. De Boskey 				case 'M':
11698d3105e8SWarner Losh 					size *= 1024ULL * 1024ULL;
11703233afaeSJohn W. De Boskey 					break;
11713233afaeSJohn W. De Boskey 				case 'g':
11723233afaeSJohn W. De Boskey 				case 'G':
11738d3105e8SWarner Losh 					size *= 1024ULL * 1024ULL * 1024ULL;
11743233afaeSJohn W. De Boskey 					break;
11753233afaeSJohn W. De Boskey 				case '\0':
11763233afaeSJohn W. De Boskey 					break;
11773233afaeSJohn W. De Boskey 				default:
11783233afaeSJohn W. De Boskey 					Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]);
11793233afaeSJohn W. De Boskey 					break;
11803233afaeSJohn W. De Boskey 				}
11813233afaeSJohn W. De Boskey 				/* don't count %'s yet */
11823233afaeSJohn W. De Boskey 				if (part_size_type[i] != '%') {
11833233afaeSJohn W. De Boskey 					/*
11843233afaeSJohn W. De Boskey 					 * for all not in sectors, convert to
11853233afaeSJohn W. De Boskey 					 * sectors
11863233afaeSJohn W. De Boskey 					 */
11873233afaeSJohn W. De Boskey 					if (part_size_type[i] != '\0') {
11883233afaeSJohn W. De Boskey 						if (size % lp->d_secsize != 0)
11893233afaeSJohn W. De Boskey 							Warning("partition %c not an integer number of sectors",
11903233afaeSJohn W. De Boskey 							    i + 'a');
11913233afaeSJohn W. De Boskey 						size /= lp->d_secsize;
11923233afaeSJohn W. De Boskey 						pp->p_size = size;
11933233afaeSJohn W. De Boskey 					}
11943233afaeSJohn W. De Boskey 					/* else already in sectors */
11956b0ff5f5SPoul-Henning Kamp 					if (i != RAW_PART)
11963233afaeSJohn W. De Boskey 						total_size += size;
11973233afaeSJohn W. De Boskey 				}
11983233afaeSJohn W. De Boskey 			}
11993233afaeSJohn W. De Boskey 		}
12003233afaeSJohn W. De Boskey 	}
12013233afaeSJohn W. De Boskey 	/* handle % partitions - note %'s don't need to add up to 100! */
12023233afaeSJohn W. De Boskey 	if (total_percent != 0) {
12033233afaeSJohn W. De Boskey 		long free_space = lp->d_secperunit - total_size;
12043233afaeSJohn W. De Boskey 		if (total_percent > 100) {
12056b0ff5f5SPoul-Henning Kamp 			fprintf(stderr,"total percentage %lu is greater than 100\n",
12063233afaeSJohn W. De Boskey 			    total_percent);
12073233afaeSJohn W. De Boskey 			errors++;
12083233afaeSJohn W. De Boskey 		}
12093233afaeSJohn W. De Boskey 
12103233afaeSJohn W. De Boskey 		if (free_space > 0) {
12113233afaeSJohn W. De Boskey 			for (i = 0; i < lp->d_npartitions; i++) {
12123233afaeSJohn W. De Boskey 				pp = &lp->d_partitions[i];
12133233afaeSJohn W. De Boskey 				if (part_set[i] && part_size_type[i] == '%') {
12143233afaeSJohn W. De Boskey 					/* careful of overflows! and integer roundoff */
12153233afaeSJohn W. De Boskey 					pp->p_size = ((double)pp->p_size/100) * free_space;
12163233afaeSJohn W. De Boskey 					total_size += pp->p_size;
12173233afaeSJohn W. De Boskey 
12183233afaeSJohn W. De Boskey 					/* FIX we can lose a sector or so due to roundoff per
12193233afaeSJohn W. De Boskey 					   partition.  A more complex algorithm could avoid that */
12203233afaeSJohn W. De Boskey 				}
12213233afaeSJohn W. De Boskey 			}
12223233afaeSJohn W. De Boskey 		} else {
12233233afaeSJohn W. De Boskey 			fprintf(stderr,
12246b0ff5f5SPoul-Henning Kamp 			    "%ld sectors available to give to '*' and '%%' partitions\n",
12253233afaeSJohn W. De Boskey 			    free_space);
12263233afaeSJohn W. De Boskey 			errors++;
12273233afaeSJohn W. De Boskey 			/* fix?  set all % partitions to size 0? */
12283233afaeSJohn W. De Boskey 		}
12293233afaeSJohn W. De Boskey 	}
12303233afaeSJohn W. De Boskey 	/* give anything remaining to the hog partition */
12313233afaeSJohn W. De Boskey 	if (hog_part != -1) {
12323233afaeSJohn W. De Boskey 		lp->d_partitions[hog_part].p_size = lp->d_secperunit - total_size;
12333233afaeSJohn W. De Boskey 		total_size = lp->d_secperunit;
12343233afaeSJohn W. De Boskey 	}
12353233afaeSJohn W. De Boskey 
12363233afaeSJohn W. De Boskey 	/* Now set the offsets for each partition */
12373233afaeSJohn W. De Boskey 	current_offset = 0; /* in sectors */
12383233afaeSJohn W. De Boskey 	seen_default_offset = 0;
12393233afaeSJohn W. De Boskey 	for (i = 0; i < lp->d_npartitions; i++) {
12403233afaeSJohn W. De Boskey 		part = 'a' + i;
12413233afaeSJohn W. De Boskey 		pp = &lp->d_partitions[i];
12423233afaeSJohn W. De Boskey 		if (part_set[i]) {
12433233afaeSJohn W. De Boskey 			if (part_offset_type[i] == '*') {
12446b0ff5f5SPoul-Henning Kamp 				if (i == RAW_PART) {
12453233afaeSJohn W. De Boskey 					pp->p_offset = 0;
12463233afaeSJohn W. De Boskey 				} else {
12473233afaeSJohn W. De Boskey 					pp->p_offset = current_offset;
12483233afaeSJohn W. De Boskey 					seen_default_offset = 1;
12493233afaeSJohn W. De Boskey 				}
12503233afaeSJohn W. De Boskey 			} else {
12513233afaeSJohn W. De Boskey 				/* allow them to be out of order for old-style tables */
12523233afaeSJohn W. De Boskey 				if (pp->p_offset < current_offset &&
1253f2f63257SGreg Lehey 				    seen_default_offset && i != RAW_PART &&
1254f2f63257SGreg Lehey 				    pp->p_fstype != FS_VINUM) {
12553233afaeSJohn W. De Boskey 					fprintf(stderr,
12566b0ff5f5SPoul-Henning Kamp "Offset %ld for partition %c overlaps previous partition which ends at %lu\n",
12576b0ff5f5SPoul-Henning Kamp 					    (long)pp->p_offset,i+'a',current_offset);
12583233afaeSJohn W. De Boskey 					fprintf(stderr,
12593233afaeSJohn W. De Boskey "Labels with any *'s for offset must be in ascending order by sector\n");
12603233afaeSJohn W. De Boskey 					errors++;
12613233afaeSJohn W. De Boskey 				} else if (pp->p_offset != current_offset &&
12626b0ff5f5SPoul-Henning Kamp 				    i != RAW_PART && seen_default_offset) {
12633233afaeSJohn W. De Boskey 					/*
12643233afaeSJohn W. De Boskey 					 * this may give unneeded warnings if
12653233afaeSJohn W. De Boskey 					 * partitions are out-of-order
12663233afaeSJohn W. De Boskey 					 */
12673233afaeSJohn W. De Boskey 					Warning(
12683233afaeSJohn W. De Boskey "Offset %ld for partition %c doesn't match expected value %ld",
12696b0ff5f5SPoul-Henning Kamp 					    (long)pp->p_offset, i + 'a', current_offset);
12703233afaeSJohn W. De Boskey 				}
12713233afaeSJohn W. De Boskey 			}
12726b0ff5f5SPoul-Henning Kamp 			if (i != RAW_PART)
12733233afaeSJohn W. De Boskey 				current_offset = pp->p_offset + pp->p_size;
12743233afaeSJohn W. De Boskey 		}
12753233afaeSJohn W. De Boskey 	}
12763233afaeSJohn W. De Boskey 
12778fae3551SRodney W. Grimes 	for (i = 0; i < lp->d_npartitions; i++) {
12788fae3551SRodney W. Grimes 		part = 'a' + i;
12798fae3551SRodney W. Grimes 		pp = &lp->d_partitions[i];
12808fae3551SRodney W. Grimes 		if (pp->p_size == 0 && pp->p_offset != 0)
12812a1deaaaSBruce Evans 			Warning("partition %c: size 0, but offset %lu",
12822a1deaaaSBruce Evans 			    part, (u_long)pp->p_offset);
12838fae3551SRodney W. Grimes #ifdef notdef
12848fae3551SRodney W. Grimes 		if (pp->p_size % lp->d_secpercyl)
12858fae3551SRodney W. Grimes 			Warning("partition %c: size %% cylinder-size != 0",
12868fae3551SRodney W. Grimes 			    part);
12878fae3551SRodney W. Grimes 		if (pp->p_offset % lp->d_secpercyl)
12888fae3551SRodney W. Grimes 			Warning("partition %c: offset %% cylinder-size != 0",
12898fae3551SRodney W. Grimes 			    part);
12908fae3551SRodney W. Grimes #endif
12918fae3551SRodney W. Grimes 		if (pp->p_offset > lp->d_secperunit) {
12928fae3551SRodney W. Grimes 			fprintf(stderr,
12938fae3551SRodney W. Grimes 			    "partition %c: offset past end of unit\n", part);
12948fae3551SRodney W. Grimes 			errors++;
12958fae3551SRodney W. Grimes 		}
12968fae3551SRodney W. Grimes 		if (pp->p_offset + pp->p_size > lp->d_secperunit) {
12978fae3551SRodney W. Grimes 			fprintf(stderr,
12988fae3551SRodney W. Grimes 			"partition %c: partition extends past end of unit\n",
12998fae3551SRodney W. Grimes 			    part);
13008fae3551SRodney W. Grimes 			errors++;
13018fae3551SRodney W. Grimes 		}
13026b0ff5f5SPoul-Henning Kamp 		if (i == RAW_PART)
13033233afaeSJohn W. De Boskey 		{
13043233afaeSJohn W. De Boskey 			if (pp->p_fstype != FS_UNUSED)
13053233afaeSJohn W. De Boskey 				Warning("partition %c is not marked as unused!",part);
13063233afaeSJohn W. De Boskey 			if (pp->p_offset != 0)
13073233afaeSJohn W. De Boskey 				Warning("partition %c doesn't start at 0!",part);
13083233afaeSJohn W. De Boskey 			if (pp->p_size != lp->d_secperunit)
13093233afaeSJohn W. De Boskey 				Warning("partition %c doesn't cover the whole unit!",part);
13103233afaeSJohn W. De Boskey 
13113233afaeSJohn W. De Boskey 			if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
13123233afaeSJohn W. De Boskey 			    (pp->p_size != lp->d_secperunit)) {
13133233afaeSJohn W. De Boskey 				Warning("An incorrect partition %c may cause problems for "
13143233afaeSJohn W. De Boskey 				    "standard system utilities",part);
13153233afaeSJohn W. De Boskey 			}
13163233afaeSJohn W. De Boskey 		}
13173233afaeSJohn W. De Boskey 
13183233afaeSJohn W. De Boskey 		/* check for overlaps */
13193233afaeSJohn W. De Boskey 		/* this will check for all possible overlaps once and only once */
13203233afaeSJohn W. De Boskey 		for (j = 0; j < i; j++) {
13213233afaeSJohn W. De Boskey 			pp2 = &lp->d_partitions[j];
1322f2f63257SGreg Lehey 			if (j != RAW_PART && i != RAW_PART &&
1323f2f63257SGreg Lehey 			    pp->p_fstype != FS_VINUM &&
1324f2f63257SGreg Lehey 			    pp2->p_fstype != FS_VINUM &&
1325f2f63257SGreg Lehey 			    part_set[i] && part_set[j]) {
13263233afaeSJohn W. De Boskey 				if (pp2->p_offset < pp->p_offset + pp->p_size &&
13273233afaeSJohn W. De Boskey 				    (pp2->p_offset + pp2->p_size > pp->p_offset ||
13283233afaeSJohn W. De Boskey 					pp2->p_offset >= pp->p_offset)) {
13293233afaeSJohn W. De Boskey 					fprintf(stderr,"partitions %c and %c overlap!\n",
13303233afaeSJohn W. De Boskey 					    j + 'a', i + 'a');
13313233afaeSJohn W. De Boskey 					errors++;
13323233afaeSJohn W. De Boskey 				}
13333233afaeSJohn W. De Boskey 			}
13343233afaeSJohn W. De Boskey 		}
13358fae3551SRodney W. Grimes 	}
13368fae3551SRodney W. Grimes 	for (; i < MAXPARTITIONS; i++) {
13378fae3551SRodney W. Grimes 		part = 'a' + i;
13388fae3551SRodney W. Grimes 		pp = &lp->d_partitions[i];
13398fae3551SRodney W. Grimes 		if (pp->p_size || pp->p_offset)
13402a1deaaaSBruce Evans 			Warning("unused partition %c: size %d offset %lu",
13412a1deaaaSBruce Evans 			    'a' + i, pp->p_size, (u_long)pp->p_offset);
13428fae3551SRodney W. Grimes 	}
13438fae3551SRodney W. Grimes 	return (errors);
13448fae3551SRodney W. Grimes }
13458fae3551SRodney W. Grimes 
13468fae3551SRodney W. Grimes /*
1347425bed3aSJoerg Wunsch  * When operating on a "virgin" disk, try getting an initial label
1348425bed3aSJoerg Wunsch  * from the associated device driver.  This might work for all device
1349425bed3aSJoerg Wunsch  * drivers that are able to fetch some initial device parameters
1350425bed3aSJoerg Wunsch  * without even having access to a (BSD) disklabel, like SCSI disks,
1351425bed3aSJoerg Wunsch  * most IDE drives, or vn devices.
1352425bed3aSJoerg Wunsch  *
1353425bed3aSJoerg Wunsch  * The device name must be given in its "canonical" form.
1354425bed3aSJoerg Wunsch  */
1355425bed3aSJoerg Wunsch struct disklabel *
1356425bed3aSJoerg Wunsch getvirginlabel(void)
1357425bed3aSJoerg Wunsch {
1358c8223965SMark Murray 	static struct disklabel loclab;
1359b9d05a16SPoul-Henning Kamp 	struct partition *dp;
1360c8223965SMark Murray 	char lnamebuf[BBSIZE];
1361425bed3aSJoerg Wunsch 	int f;
1362b9d05a16SPoul-Henning Kamp 	u_int secsize, u;
1363b9d05a16SPoul-Henning Kamp 	off_t mediasize;
1364425bed3aSJoerg Wunsch 
1365425bed3aSJoerg Wunsch 	if (dkname[0] == '/') {
1366e18fb238SBruce Evans 		warnx("\"auto\" requires the usage of a canonical disk name");
136743be698cSBruce Evans 		return (NULL);
1368425bed3aSJoerg Wunsch 	}
1369c8223965SMark Murray 	(void)snprintf(lnamebuf, BBSIZE, "%s%s", _PATH_DEV, dkname);
1370c8223965SMark Murray 	if ((f = open(lnamebuf, O_RDONLY)) == -1) {
1371c8223965SMark Murray 		warn("cannot open %s", lnamebuf);
137243be698cSBruce Evans 		return (NULL);
1373425bed3aSJoerg Wunsch 	}
1374ff7d5162SJordan K. Hubbard 
1375b9d05a16SPoul-Henning Kamp 	/* New world order */
1376b9d05a16SPoul-Henning Kamp 	if ((ioctl(f, DIOCGMEDIASIZE, &mediasize) != 0) ||
1377b9d05a16SPoul-Henning Kamp 	    (ioctl(f, DIOCGSECTORSIZE, &secsize) != 0)) {
1378425bed3aSJoerg Wunsch 		close (f);
137943be698cSBruce Evans 		return (NULL);
1380425bed3aSJoerg Wunsch 	}
1381b9d05a16SPoul-Henning Kamp 	memset(&loclab, 0, sizeof loclab);
1382b9d05a16SPoul-Henning Kamp 	loclab.d_magic = DISKMAGIC;
1383b9d05a16SPoul-Henning Kamp 	loclab.d_magic2 = DISKMAGIC;
1384b9d05a16SPoul-Henning Kamp 	loclab.d_secsize = secsize;
1385b9d05a16SPoul-Henning Kamp 	loclab.d_secperunit = mediasize / secsize;
1386b9d05a16SPoul-Henning Kamp 
1387b9d05a16SPoul-Henning Kamp 	/*
1388b9d05a16SPoul-Henning Kamp 	 * Nobody in these enligthened days uses the CHS geometry for
1389b9d05a16SPoul-Henning Kamp 	 * anything, but nontheless try to get it right.  If we fail
1390b9d05a16SPoul-Henning Kamp 	 * to get any good ideas from the device, construct something
1391b9d05a16SPoul-Henning Kamp 	 * which is IBM-PC friendly.
1392b9d05a16SPoul-Henning Kamp 	 */
1393b9d05a16SPoul-Henning Kamp 	if (ioctl(f, DIOCGFWSECTORS, &u) == 0)
1394b9d05a16SPoul-Henning Kamp 		loclab.d_nsectors = u;
1395b9d05a16SPoul-Henning Kamp 	else
1396b9d05a16SPoul-Henning Kamp 		loclab.d_nsectors = 63;
1397b9d05a16SPoul-Henning Kamp 	if (ioctl(f, DIOCGFWHEADS, &u) == 0)
1398b9d05a16SPoul-Henning Kamp 		loclab.d_ntracks = u;
1399b9d05a16SPoul-Henning Kamp 	else if (loclab.d_secperunit <= 63*1*1024)
1400b9d05a16SPoul-Henning Kamp 		loclab.d_ntracks = 1;
1401b9d05a16SPoul-Henning Kamp 	else if (loclab.d_secperunit <= 63*16*1024)
1402b9d05a16SPoul-Henning Kamp 		loclab.d_ntracks = 16;
1403b9d05a16SPoul-Henning Kamp 	else
1404b9d05a16SPoul-Henning Kamp 		loclab.d_ntracks = 255;
1405b9d05a16SPoul-Henning Kamp 	loclab.d_secpercyl = loclab.d_ntracks * loclab.d_nsectors;
1406b9d05a16SPoul-Henning Kamp 	loclab.d_ncylinders = loclab.d_secperunit / loclab.d_secpercyl;
1407b9d05a16SPoul-Henning Kamp 	loclab.d_npartitions = MAXPARTITIONS;
1408b9d05a16SPoul-Henning Kamp 
1409b9d05a16SPoul-Henning Kamp 	/* Various (unneeded) compat stuff */
1410b9d05a16SPoul-Henning Kamp 	loclab.d_rpm = 3600;
1411b9d05a16SPoul-Henning Kamp 	loclab.d_bbsize = BBSIZE;
1412b9d05a16SPoul-Henning Kamp 	loclab.d_interleave = 1;;
1413b9d05a16SPoul-Henning Kamp 	strncpy(loclab.d_typename, "amnesiac",
1414b9d05a16SPoul-Henning Kamp 	    sizeof(loclab.d_typename));
1415b9d05a16SPoul-Henning Kamp 
1416b9d05a16SPoul-Henning Kamp 	dp = &loclab.d_partitions[RAW_PART];
1417b9d05a16SPoul-Henning Kamp 	dp->p_size = loclab.d_secperunit;
1418b9d05a16SPoul-Henning Kamp 	loclab.d_checksum = dkcksum(&loclab);
1419425bed3aSJoerg Wunsch 	close (f);
1420c8223965SMark Murray 	return (&loclab);
1421425bed3aSJoerg Wunsch }
1422425bed3aSJoerg Wunsch 
14238fae3551SRodney W. Grimes 
14248fae3551SRodney W. Grimes /*VARARGS1*/
142561de51caSJoerg Wunsch void
1426326c7cdaSWarner Losh Warning(const char *fmt, ...)
14278fae3551SRodney W. Grimes {
142861de51caSJoerg Wunsch 	va_list ap;
14298fae3551SRodney W. Grimes 
14308fae3551SRodney W. Grimes 	fprintf(stderr, "Warning, ");
143161de51caSJoerg Wunsch 	va_start(ap, fmt);
143261de51caSJoerg Wunsch 	vfprintf(stderr, fmt, ap);
14338fae3551SRodney W. Grimes 	fprintf(stderr, "\n");
143461de51caSJoerg Wunsch 	va_end(ap);
14358fae3551SRodney W. Grimes }
14368fae3551SRodney W. Grimes 
143761de51caSJoerg Wunsch void
1438326c7cdaSWarner Losh usage(void)
14398fae3551SRodney W. Grimes {
1440bc33ea1aSRuslan Ermilov 
1441bc33ea1aSRuslan Ermilov 	fprintf(stderr,
1442bc33ea1aSRuslan Ermilov 	"%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",
14435daa806dSPoul-Henning Kamp 	"usage: bsdlabel disk",
1444bef2080aSPhilippe Charnier 	"\t\t(to read label)",
14455daa806dSPoul-Henning Kamp 	"	bsdlabel -w [-n] [-m machine] disk type [packid]",
1446bef2080aSPhilippe Charnier 	"\t\t(to write label with existing boot program)",
14475daa806dSPoul-Henning Kamp 	"	bsdlabel -e [-n] [-m machine] disk",
1448bef2080aSPhilippe Charnier 	"\t\t(to edit label)",
14495daa806dSPoul-Henning Kamp 	"	bsdlabel -R [-n] [-m machine] disk protofile",
1450bef2080aSPhilippe Charnier 	"\t\t(to restore label with existing boot program)",
14512c60b668SPoul-Henning Kamp 	"	bsdlabel -B [-b boot] [-m machine] disk",
1452bef2080aSPhilippe Charnier 	"\t\t(to install boot program with existing on-disk label)",
14532c60b668SPoul-Henning Kamp 	"	bsdlabel -w -B [-n] [-b boot] [-m machine] disk type [packid]",
1454bef2080aSPhilippe Charnier 	"\t\t(to write label and install boot program)",
14552c60b668SPoul-Henning Kamp 	"	bsdlabel -R -B [-n] [-b boot] [-m machine] disk protofile",
145680baf8ceSPoul-Henning Kamp 		"\t\t(to restore label and install boot program)"
145780baf8ceSPoul-Henning Kamp 	);
14588fae3551SRodney W. Grimes 	exit(1);
14598fae3551SRodney W. Grimes }
1460