xref: /freebsd/sbin/bsdlabel/bsdlabel.c (revision 3b89beb171320bbd7fa270ebcbbe2119fd385fbc)
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 
437c4d80f2SDavid E. O'Brien #if 0
448fae3551SRodney W. Grimes #ifndef lint
456bd343a9SPhilippe Charnier static const char copyright[] =
468fae3551SRodney W. Grimes "@(#) Copyright (c) 1987, 1993\n\
478fae3551SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
488fae3551SRodney W. Grimes #endif /* not lint */
498fae3551SRodney W. Grimes 
508fae3551SRodney W. Grimes #ifndef lint
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"; */
538fae3551SRodney W. Grimes #endif /* not lint */
547c4d80f2SDavid E. O'Brien #endif
55c8223965SMark Murray #include <sys/cdefs.h>
56c8223965SMark Murray __FBSDID("$FreeBSD$");
57c8223965SMark Murray 
588fae3551SRodney W. Grimes #include <sys/param.h>
59dfbc3f0cSPoul-Henning Kamp #include <stdint.h>
608fae3551SRodney W. Grimes #include <sys/file.h>
618fae3551SRodney W. Grimes #include <sys/stat.h>
6261de51caSJoerg Wunsch #include <sys/wait.h>
63b9d05a16SPoul-Henning Kamp #include <sys/disk.h>
648fae3551SRodney W. Grimes #define DKTYPENAMES
65b35e6950SBosko Milekic #define FSTYPENAMES
668fae3551SRodney W. Grimes #include <sys/disklabel.h>
67bc33ea1aSRuslan Ermilov 
688fae3551SRodney W. Grimes #include <unistd.h>
698fae3551SRodney W. Grimes #include <string.h>
708fae3551SRodney W. Grimes #include <stdio.h>
715daa806dSPoul-Henning Kamp #include <libgeom.h>
7261de51caSJoerg Wunsch #include <stdlib.h>
7361de51caSJoerg Wunsch #include <signal.h>
7461de51caSJoerg Wunsch #include <stdarg.h>
758fae3551SRodney W. Grimes #include <ctype.h>
76bef2080aSPhilippe Charnier #include <err.h>
7737736675SWarner Losh #include <errno.h>
78c8223965SMark Murray 
798fae3551SRodney W. Grimes #include "pathnames.h"
808fae3551SRodney W. Grimes 
81d2fe97c7SPoul-Henning Kamp static void	makelabel(const char *, struct disklabel *);
82d2fe97c7SPoul-Henning Kamp static int	writelabel(void);
83d2fe97c7SPoul-Henning Kamp static int	readlabel(int flag);
84d2fe97c7SPoul-Henning Kamp static void	display(FILE *, const struct disklabel *);
85d2fe97c7SPoul-Henning Kamp static int	edit(void);
86d2fe97c7SPoul-Henning Kamp static int	editit(void);
87427823d5SPoul-Henning Kamp static void	fixlabel(struct disklabel *);
88d2fe97c7SPoul-Henning Kamp static char	*skip(char *);
89d2fe97c7SPoul-Henning Kamp static char	*word(char *);
90d2fe97c7SPoul-Henning Kamp static int	getasciilabel(FILE *, struct disklabel *);
91d2fe97c7SPoul-Henning Kamp static int	getasciipartspec(char *, struct disklabel *, int, int);
92d2fe97c7SPoul-Henning Kamp static int	checklabel(struct disklabel *);
93d2fe97c7SPoul-Henning Kamp static void	usage(void);
94d2fe97c7SPoul-Henning Kamp static struct disklabel *getvirginlabel(void);
9561de51caSJoerg Wunsch 
968fae3551SRodney W. Grimes #define	DEFEDITOR	_PATH_VI
978fae3551SRodney W. Grimes 
98d2fe97c7SPoul-Henning Kamp static char	*dkname;
99d2fe97c7SPoul-Henning Kamp static char	*specname;
100d2fe97c7SPoul-Henning Kamp static char	tmpfil[] = PATH_TMPFILE;
1018fae3551SRodney W. Grimes 
102d2fe97c7SPoul-Henning Kamp static struct	disklabel lab;
103d2fe97c7SPoul-Henning Kamp static u_char	bootarea[BBSIZE];
10457dfbec5SPoul-Henning Kamp static off_t	mediasize;
10557dfbec5SPoul-Henning Kamp static u_int	secsize;
106d2fe97c7SPoul-Henning Kamp static char	blank[] = "";
107d2fe97c7SPoul-Henning Kamp static char	unknown[] = "unknown";
1088fae3551SRodney W. Grimes 
1093233afaeSJohn W. De Boskey #define MAX_PART ('z')
1103233afaeSJohn W. De Boskey #define MAX_NUM_PARTS (1 + MAX_PART - 'a')
111d2fe97c7SPoul-Henning Kamp static char    part_size_type[MAX_NUM_PARTS];
112d2fe97c7SPoul-Henning Kamp static char    part_offset_type[MAX_NUM_PARTS];
113d2fe97c7SPoul-Henning Kamp static int     part_set[MAX_NUM_PARTS];
1143233afaeSJohn W. De Boskey 
115d2fe97c7SPoul-Henning Kamp static int	installboot;	/* non-zero if we should install a boot program */
11657dfbec5SPoul-Henning Kamp static int	allfields;	/* present all fields in edit */
117d2fe97c7SPoul-Henning Kamp static char const *xxboot;	/* primary boot */
1188fae3551SRodney W. Grimes 
1195d853216SPoul-Henning Kamp static off_t mbroffset;
1209ef521ecSPoul-Henning Kamp #ifndef LABELSECTOR
1219ef521ecSPoul-Henning Kamp #define LABELSECTOR -1
1229ef521ecSPoul-Henning Kamp #endif
1239ef521ecSPoul-Henning Kamp #ifndef LABELOFFSET
1249ef521ecSPoul-Henning Kamp #define LABELOFFSET -1
1259ef521ecSPoul-Henning Kamp #endif
126b2bb9f9bSPoul-Henning Kamp static int labelsoffset = LABELSECTOR;
127b2bb9f9bSPoul-Henning Kamp static int labeloffset = LABELOFFSET;
1287838fd0eSPoul-Henning Kamp static int bbsize = BBSIZE;
1297838fd0eSPoul-Henning Kamp static int alphacksum =
1307838fd0eSPoul-Henning Kamp #if defined(__alpha__)
1317838fd0eSPoul-Henning Kamp 	1;
1327838fd0eSPoul-Henning Kamp #else
1337838fd0eSPoul-Henning Kamp 	0;
1347838fd0eSPoul-Henning Kamp #endif
135c80f9755SPoul-Henning Kamp 
1368fae3551SRodney W. Grimes enum	{
137f080d33bSPoul-Henning Kamp 	UNSPEC, EDIT, READ, RESTORE, WRITE, WRITEBOOT
1388fae3551SRodney W. Grimes } op = UNSPEC;
1398fae3551SRodney W. Grimes 
140bc33ea1aSRuslan Ermilov 
141d2fe97c7SPoul-Henning Kamp static int	disable_write;   /* set to disable writing to disk label */
1427747c959SLuigi Rizzo static int	is_file;	/* work on a file (abs. pathname), "-f" opt. */
1438fae3551SRodney W. Grimes 
14461de51caSJoerg Wunsch int
145326c7cdaSWarner Losh main(int argc, char *argv[])
1468fae3551SRodney W. Grimes {
1478fae3551SRodney W. Grimes 	FILE *t;
1485daa806dSPoul-Henning Kamp 	int ch, error = 0;
149d2fe97c7SPoul-Henning Kamp 	char const *name = 0;
1508fae3551SRodney W. Grimes 
1517747c959SLuigi Rizzo 	while ((ch = getopt(argc, argv, "ABb:efm:nRrs:w")) != -1)
1528fae3551SRodney W. Grimes 		switch (ch) {
15357dfbec5SPoul-Henning Kamp 			case 'A':
15457dfbec5SPoul-Henning Kamp 				allfields = 1;
15557dfbec5SPoul-Henning Kamp 				break;
1568fae3551SRodney W. Grimes 			case 'B':
1578fae3551SRodney W. Grimes 				++installboot;
1588fae3551SRodney W. Grimes 				break;
1598fae3551SRodney W. Grimes 			case 'b':
1608fae3551SRodney W. Grimes 				xxboot = optarg;
1618fae3551SRodney W. Grimes 				break;
1627747c959SLuigi Rizzo 			case 'f':
1637747c959SLuigi Rizzo 				is_file=1;
1647747c959SLuigi Rizzo 				break;
165bc33ea1aSRuslan Ermilov 			case 'm':
166246300f2SPoul-Henning Kamp 				if (!strcmp(optarg, "i386") ||
167246300f2SPoul-Henning Kamp 				    !strcmp(optarg, "amd64") ||
168246300f2SPoul-Henning Kamp 				    !strcmp(optarg, "ia64") ||
169246300f2SPoul-Henning Kamp 				    !strcmp(optarg, "pc98")) {
170b2bb9f9bSPoul-Henning Kamp 					labelsoffset = 1;
171b2bb9f9bSPoul-Henning Kamp 					labeloffset = 0;
1723ecb3802SPoul-Henning Kamp 					bbsize = 8192;
1733ecb3802SPoul-Henning Kamp 					alphacksum = 0;
174c80f9755SPoul-Henning Kamp 				} else if (!strcmp(optarg, "alpha")) {
175b2bb9f9bSPoul-Henning Kamp 					labelsoffset = 0;
176c80f9755SPoul-Henning Kamp 					labeloffset = 64;
177c80f9755SPoul-Henning Kamp 					bbsize = 8192;
178c80f9755SPoul-Henning Kamp 					alphacksum = 1;
179d2fe97c7SPoul-Henning Kamp 				} else {
180d2fe97c7SPoul-Henning Kamp 					errx(1, "Unsupported architecture");
181c80f9755SPoul-Henning Kamp 				}
182bc33ea1aSRuslan Ermilov 				break;
1833233afaeSJohn W. De Boskey 			case 'n':
1843233afaeSJohn W. De Boskey 				disable_write = 1;
1853233afaeSJohn W. De Boskey 				break;
1868fae3551SRodney W. Grimes 			case 'R':
1878fae3551SRodney W. Grimes 				if (op != UNSPEC)
1888fae3551SRodney W. Grimes 					usage();
1898fae3551SRodney W. Grimes 				op = RESTORE;
1908fae3551SRodney W. Grimes 				break;
1918fae3551SRodney W. Grimes 			case 'e':
1928fae3551SRodney W. Grimes 				if (op != UNSPEC)
1938fae3551SRodney W. Grimes 					usage();
1948fae3551SRodney W. Grimes 				op = EDIT;
1958fae3551SRodney W. Grimes 				break;
1968fae3551SRodney W. Grimes 			case 'r':
197d2fe97c7SPoul-Henning Kamp 				/*
198d2fe97c7SPoul-Henning Kamp 				 * We accept and ignode -r for compatibility with
199d2fe97c7SPoul-Henning Kamp 				 * historically disklabel usage.
200d2fe97c7SPoul-Henning Kamp 				 */
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;
213d2fe97c7SPoul-Henning Kamp 
2148fae3551SRodney W. Grimes 	if (argc < 1)
2158fae3551SRodney W. Grimes 		usage();
2169ef521ecSPoul-Henning Kamp 	if (labelsoffset < 0 || labeloffset < 0)
2179ef521ecSPoul-Henning Kamp 		errx(1, "a -m <architecture> option must be specified");
2188fae3551SRodney W. Grimes 
2195daa806dSPoul-Henning Kamp 	/* Figure out the names of the thing we're working on */
2207747c959SLuigi Rizzo 	if (is_file) {
2217747c959SLuigi Rizzo 		dkname = specname = argv[0];
2227747c959SLuigi Rizzo 	} else if (argv[0][0] != '/') {
2238fae3551SRodney W. Grimes 		dkname = argv[0];
2245daa806dSPoul-Henning Kamp 		asprintf(&specname, "%s%s", _PATH_DEV, argv[0]);
225ccdd2fceSRalf S. Engelschall 	} else if (strncmp(argv[0], _PATH_DEV, strlen(_PATH_DEV)) == 0) {
226ccdd2fceSRalf S. Engelschall 		dkname = argv[0] + strlen(_PATH_DEV);
227ccdd2fceSRalf S. Engelschall 		specname = 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 	}
233d2fe97c7SPoul-Henning Kamp 
234d2fe97c7SPoul-Henning Kamp 	if (installboot && op == UNSPEC)
235d2fe97c7SPoul-Henning Kamp 		op = WRITEBOOT;
236d2fe97c7SPoul-Henning Kamp 	else if (op == UNSPEC)
237d2fe97c7SPoul-Henning Kamp 		op = READ;
2388fae3551SRodney W. Grimes 
2398fae3551SRodney W. Grimes 	switch(op) {
2408fae3551SRodney W. Grimes 
2413b3038a6SBill Fumerola 	case UNSPEC:
2423b3038a6SBill Fumerola 		break;
2433b3038a6SBill Fumerola 
2448fae3551SRodney W. Grimes 	case EDIT:
2458fae3551SRodney W. Grimes 		if (argc != 1)
2468fae3551SRodney W. Grimes 			usage();
247d2fe97c7SPoul-Henning Kamp 		readlabel(1);
248427823d5SPoul-Henning Kamp 		fixlabel(&lab);
249d2fe97c7SPoul-Henning Kamp 		error = edit();
2508fae3551SRodney W. Grimes 		break;
2518fae3551SRodney W. Grimes 
2528fae3551SRodney W. Grimes 	case READ:
2538fae3551SRodney W. Grimes 		if (argc != 1)
2548fae3551SRodney W. Grimes 			usage();
255d2fe97c7SPoul-Henning Kamp 		readlabel(1);
256d2fe97c7SPoul-Henning Kamp 		display(stdout, NULL);
257d2fe97c7SPoul-Henning Kamp 		error = checklabel(NULL);
2588fae3551SRodney W. Grimes 		break;
2598fae3551SRodney W. Grimes 
2608fae3551SRodney W. Grimes 	case RESTORE:
2618fae3551SRodney W. Grimes 		if (argc != 2)
2628fae3551SRodney W. Grimes 			usage();
2638fae3551SRodney W. Grimes 		if (!(t = fopen(argv[1], "r")))
2645daa806dSPoul-Henning Kamp 			err(4, "fopen %s", argv[1]);
265d2fe97c7SPoul-Henning Kamp 		readlabel(0);
2666cabb348SBruce Evans 		if (!getasciilabel(t, &lab))
2676cabb348SBruce Evans 			exit(1);
268d2fe97c7SPoul-Henning Kamp 		error = writelabel();
2698fae3551SRodney W. Grimes 		break;
2708fae3551SRodney W. Grimes 
2718fae3551SRodney W. Grimes 	case WRITE:
272d2fe97c7SPoul-Henning Kamp 		if (argc == 2)
273d2fe97c7SPoul-Henning Kamp 			name = argv[1];
274d2fe97c7SPoul-Henning Kamp 		else if (argc == 1)
275d2fe97c7SPoul-Henning Kamp 			name = "auto";
276d2fe97c7SPoul-Henning Kamp 		else
2778fae3551SRodney W. Grimes 			usage();
278d2fe97c7SPoul-Henning Kamp 		readlabel(0);
279d2fe97c7SPoul-Henning Kamp 		makelabel(name, &lab);
280427823d5SPoul-Henning Kamp 		fixlabel(&lab);
281d2fe97c7SPoul-Henning Kamp 		if (checklabel(NULL) == 0)
282d2fe97c7SPoul-Henning Kamp 			error = writelabel();
2838fae3551SRodney W. Grimes 		break;
2848fae3551SRodney W. Grimes 
2858fae3551SRodney W. Grimes 	case WRITEBOOT:
2868fae3551SRodney W. Grimes 
287d2fe97c7SPoul-Henning Kamp 		readlabel(1);
288427823d5SPoul-Henning Kamp 		fixlabel(&lab);
2898fae3551SRodney W. Grimes 		if (argc == 2)
290d2fe97c7SPoul-Henning Kamp 			makelabel(argv[1], &lab);
291d2fe97c7SPoul-Henning Kamp 		if (checklabel(NULL) == 0)
292d2fe97c7SPoul-Henning Kamp 			error = writelabel();
2938fae3551SRodney W. Grimes 		break;
2948fae3551SRodney W. Grimes 	}
2958fae3551SRodney W. Grimes 	exit(error);
2968fae3551SRodney W. Grimes }
2978fae3551SRodney W. Grimes 
298427823d5SPoul-Henning Kamp static void
299427823d5SPoul-Henning Kamp fixlabel(struct disklabel *lp)
300427823d5SPoul-Henning Kamp {
301427823d5SPoul-Henning Kamp 	struct partition *dp;
302427823d5SPoul-Henning Kamp 	int i;
303427823d5SPoul-Henning Kamp 
304427823d5SPoul-Henning Kamp 	for (i = 0; i < MAXPARTITIONS; i++) {
305427823d5SPoul-Henning Kamp 		if (i == RAW_PART)
306427823d5SPoul-Henning Kamp 			continue;
307427823d5SPoul-Henning Kamp 		if (lp->d_partitions[i].p_size)
308427823d5SPoul-Henning Kamp 			return;
309427823d5SPoul-Henning Kamp 	}
310427823d5SPoul-Henning Kamp 
311427823d5SPoul-Henning Kamp 	dp = &lp->d_partitions[0];
312427823d5SPoul-Henning Kamp 	dp->p_offset = BBSIZE / secsize;
313427823d5SPoul-Henning Kamp 	dp->p_size = lp->d_secperunit - dp->p_offset;
314427823d5SPoul-Henning Kamp }
315427823d5SPoul-Henning Kamp 
3168fae3551SRodney W. Grimes /*
317ef9ab0b3SRuslan Ermilov  * Construct a prototype disklabel from /etc/disktab.
3188fae3551SRodney W. Grimes  */
319d2fe97c7SPoul-Henning Kamp static void
320d2fe97c7SPoul-Henning Kamp makelabel(const char *type, struct disklabel *lp)
3218fae3551SRodney W. Grimes {
322326c7cdaSWarner Losh 	struct disklabel *dp;
323425bed3aSJoerg Wunsch 
324425bed3aSJoerg Wunsch 	if (strcmp(type, "auto") == 0)
325425bed3aSJoerg Wunsch 		dp = getvirginlabel();
326425bed3aSJoerg Wunsch 	else
3278fae3551SRodney W. Grimes 		dp = getdiskbyname(type);
3286bd343a9SPhilippe Charnier 	if (dp == NULL)
3296bd343a9SPhilippe Charnier 		errx(1, "%s: unknown disk type", type);
3308fae3551SRodney W. Grimes 	*lp = *dp;
3318fae3551SRodney W. Grimes 	bzero(lp->d_packname, sizeof(lp->d_packname));
3328fae3551SRodney W. Grimes }
3338fae3551SRodney W. Grimes 
334d2fe97c7SPoul-Henning Kamp static void
335d2fe97c7SPoul-Henning Kamp readboot(void)
336d2fe97c7SPoul-Henning Kamp {
337d2fe97c7SPoul-Henning Kamp 	int fd, i;
338d2fe97c7SPoul-Henning Kamp 	struct stat st;
339c8785325SJohn Baldwin 	uint64_t *p;
340d2fe97c7SPoul-Henning Kamp 
341d2fe97c7SPoul-Henning Kamp 	if (xxboot == NULL)
342d2fe97c7SPoul-Henning Kamp 		xxboot = "/boot/boot";
343d2fe97c7SPoul-Henning Kamp 	fd = open(xxboot, O_RDONLY);
344d2fe97c7SPoul-Henning Kamp 	if (fd < 0)
345d2fe97c7SPoul-Henning Kamp 		err(1, "cannot open %s", xxboot);
346d2fe97c7SPoul-Henning Kamp 	fstat(fd, &st);
34754f445fbSPoul-Henning Kamp 	if (alphacksum && st.st_size <= BBSIZE - 512) {
34854f445fbSPoul-Henning Kamp 		i = read(fd, bootarea + 512, st.st_size);
34954f445fbSPoul-Henning Kamp 		if (i != st.st_size)
350d2fe97c7SPoul-Henning Kamp 			err(1, "read error %s", xxboot);
351c8785325SJohn Baldwin 
352c8785325SJohn Baldwin 		/*
353c8785325SJohn Baldwin 		 * Set the location and length so SRM can find the
354c8785325SJohn Baldwin 		 * boot blocks.
355c8785325SJohn Baldwin 		 */
356c8785325SJohn Baldwin 		p = (uint64_t *)bootarea;
357c8785325SJohn Baldwin 		p[60] = (st.st_size + secsize - 1) / secsize;
358c8785325SJohn Baldwin 		p[61] = 1;
359c8785325SJohn Baldwin 		p[62] = 0;
360d2fe97c7SPoul-Henning Kamp 		return;
36154f445fbSPoul-Henning Kamp 	} else if ((!alphacksum) && st.st_size <= BBSIZE) {
36254f445fbSPoul-Henning Kamp 		i = read(fd, bootarea, st.st_size);
36354f445fbSPoul-Henning Kamp 		if (i != st.st_size)
364d2fe97c7SPoul-Henning Kamp 			err(1, "read error %s", xxboot);
365d2fe97c7SPoul-Henning Kamp 		return;
366d2fe97c7SPoul-Henning Kamp 	}
367d2fe97c7SPoul-Henning Kamp 	errx(1, "boot code %s is wrong size", xxboot);
368d2fe97c7SPoul-Henning Kamp }
369d2fe97c7SPoul-Henning Kamp 
370d2fe97c7SPoul-Henning Kamp static int
371d2fe97c7SPoul-Henning Kamp writelabel(void)
3728fae3551SRodney W. Grimes {
373bc33ea1aSRuslan Ermilov 	uint64_t *p, sum;
3745daa806dSPoul-Henning Kamp 	int i, fd;
3755daa806dSPoul-Henning Kamp 	struct gctl_req *grq;
3765daa806dSPoul-Henning Kamp 	char const *errstr;
377d2fe97c7SPoul-Henning Kamp 	struct disklabel *lp = &lab;
378130cd73aSDoug Rabson 
3793233afaeSJohn W. De Boskey 	if (disable_write) {
38057dfbec5SPoul-Henning Kamp 		warnx("write to disk label supressed - label was as follows:");
381d2fe97c7SPoul-Henning Kamp 		display(stdout, NULL);
3823233afaeSJohn W. De Boskey 		return (0);
38380baf8ceSPoul-Henning Kamp 	}
38480baf8ceSPoul-Henning Kamp 
3858fae3551SRodney W. Grimes 	lp->d_magic = DISKMAGIC;
3868fae3551SRodney W. Grimes 	lp->d_magic2 = DISKMAGIC;
3878fae3551SRodney W. Grimes 	lp->d_checksum = 0;
3888fae3551SRodney W. Grimes 	lp->d_checksum = dkcksum(lp);
389d2fe97c7SPoul-Henning Kamp 	if (installboot)
390d2fe97c7SPoul-Henning Kamp 		readboot();
3915d853216SPoul-Henning Kamp 	for (i = 0; i < lab.d_npartitions; i++)
3925d853216SPoul-Henning Kamp 		if (lab.d_partitions[i].p_size)
3935d853216SPoul-Henning Kamp 			lab.d_partitions[i].p_offset += mbroffset;
394b2bb9f9bSPoul-Henning Kamp 	bsd_disklabel_le_enc(bootarea + labeloffset + labelsoffset * secsize,
395b2bb9f9bSPoul-Henning Kamp 	    lp);
396dfbc3f0cSPoul-Henning Kamp 	if (alphacksum) {
3975daa806dSPoul-Henning Kamp 		/* Generate the bootblock checksum for the SRM console.  */
398d2fe97c7SPoul-Henning Kamp 		for (p = (uint64_t *)bootarea, i = 0, sum = 0; i < 63; i++)
399130cd73aSDoug Rabson 			sum += p[i];
400130cd73aSDoug Rabson 		p[63] = sum;
401bc33ea1aSRuslan Ermilov 	}
4025daa806dSPoul-Henning Kamp 
4035daa806dSPoul-Henning Kamp 	fd = open(specname, O_RDWR);
4045daa806dSPoul-Henning Kamp 	if (fd < 0) {
4057747c959SLuigi Rizzo 		if (is_file) {
4067747c959SLuigi Rizzo 			warn("cannot open file %s for writing label", specname);
4077747c959SLuigi Rizzo 			return(1);
4087747c959SLuigi Rizzo 		}
40983d771deSPoul-Henning Kamp 		grq = gctl_get_handle();
41083d771deSPoul-Henning Kamp 		gctl_ro_param(grq, "verb", -1, "write label");
4115daa806dSPoul-Henning Kamp 		gctl_ro_param(grq, "class", -1, "BSD");
4125daa806dSPoul-Henning Kamp 		gctl_ro_param(grq, "geom", -1, dkname);
413b2bb9f9bSPoul-Henning Kamp 		gctl_ro_param(grq, "label", 148+16*8,
414b2bb9f9bSPoul-Henning Kamp 			bootarea + labeloffset + labelsoffset * secsize);
4155daa806dSPoul-Henning Kamp 		errstr = gctl_issue(grq);
416d2fe97c7SPoul-Henning Kamp 		if (errstr != NULL) {
417d2fe97c7SPoul-Henning Kamp 			warnx("%s", errstr);
418d2fe97c7SPoul-Henning Kamp 			gctl_free(grq);
419d2fe97c7SPoul-Henning Kamp 			return(1);
420d2fe97c7SPoul-Henning Kamp 		}
4215daa806dSPoul-Henning Kamp 		gctl_free(grq);
4225daa806dSPoul-Henning Kamp 		if (installboot) {
42383d771deSPoul-Henning Kamp 			grq = gctl_get_handle();
42483d771deSPoul-Henning Kamp 			gctl_ro_param(grq, "verb", -1, "write bootcode");
4255daa806dSPoul-Henning Kamp 			gctl_ro_param(grq, "class", -1, "BSD");
4265daa806dSPoul-Henning Kamp 			gctl_ro_param(grq, "geom", -1, dkname);
427d2fe97c7SPoul-Henning Kamp 			gctl_ro_param(grq, "bootcode", BBSIZE, bootarea);
4285daa806dSPoul-Henning Kamp 			errstr = gctl_issue(grq);
429d2fe97c7SPoul-Henning Kamp 			if (errstr != NULL) {
430d2fe97c7SPoul-Henning Kamp 				warnx("%s", errstr);
431d2fe97c7SPoul-Henning Kamp 				gctl_free(grq);
432d2fe97c7SPoul-Henning Kamp 				return (1);
433d2fe97c7SPoul-Henning Kamp 			}
4345daa806dSPoul-Henning Kamp 			gctl_free(grq);
4355daa806dSPoul-Henning Kamp 		}
4365daa806dSPoul-Henning Kamp 	} else {
437d2fe97c7SPoul-Henning Kamp 		if (write(fd, bootarea, bbsize) != bbsize) {
4385daa806dSPoul-Henning Kamp 			warn("write %s", specname);
4395daa806dSPoul-Henning Kamp 			close (fd);
4408fae3551SRodney W. Grimes 			return (1);
4418fae3551SRodney W. Grimes 		}
4425daa806dSPoul-Henning Kamp 		close (fd);
4435daa806dSPoul-Henning Kamp 	}
4448fae3551SRodney W. Grimes 	return (0);
4458fae3551SRodney W. Grimes }
4468fae3551SRodney W. Grimes 
4477747c959SLuigi Rizzo static void
4487747c959SLuigi Rizzo get_file_parms(int f)
4497747c959SLuigi Rizzo {
4507747c959SLuigi Rizzo 	int i;
4517747c959SLuigi Rizzo 	struct stat sb;
4527747c959SLuigi Rizzo 
4537747c959SLuigi Rizzo 	if (fstat(f, &sb) != 0)
4547747c959SLuigi Rizzo 		err(4, "fstat failed");
4557747c959SLuigi Rizzo 	i = sb.st_mode & S_IFMT;
4567747c959SLuigi Rizzo 	if (i != S_IFREG && i != S_IFLNK)
4577747c959SLuigi Rizzo 		errx(4, "%s is not a valid file or link", specname);
4587747c959SLuigi Rizzo 	secsize = DEV_BSIZE;
4597747c959SLuigi Rizzo 	mediasize = sb.st_size;
4607747c959SLuigi Rizzo }
4617747c959SLuigi Rizzo 
4628fae3551SRodney W. Grimes /*
4638fae3551SRodney W. Grimes  * Fetch disklabel for disk.
4648fae3551SRodney W. Grimes  * Use ioctl to get label unless -r flag is given.
4658fae3551SRodney W. Grimes  */
466d2fe97c7SPoul-Henning Kamp static int
467d2fe97c7SPoul-Henning Kamp readlabel(int flag)
4688fae3551SRodney W. Grimes {
4695d853216SPoul-Henning Kamp 	int f, i;
4705daa806dSPoul-Henning Kamp 	int error;
4715d853216SPoul-Henning Kamp 	struct gctl_req *grq;
4725d853216SPoul-Henning Kamp 	char const *errstr;
4738fae3551SRodney W. Grimes 
4745daa806dSPoul-Henning Kamp 	f = open(specname, O_RDONLY);
4755daa806dSPoul-Henning Kamp 	if (f < 0)
4765daa806dSPoul-Henning Kamp 		err(1, specname);
4777747c959SLuigi Rizzo 	if (is_file)
4787747c959SLuigi Rizzo 		get_file_parms(f);
4797747c959SLuigi Rizzo 	else if ((ioctl(f, DIOCGMEDIASIZE, &mediasize) != 0) ||
480b2bb9f9bSPoul-Henning Kamp 	    (ioctl(f, DIOCGSECTORSIZE, &secsize) != 0)) {
481b2bb9f9bSPoul-Henning Kamp 		err(4, "cannot get disk geometry");
482b2bb9f9bSPoul-Henning Kamp 	}
48386e0bd0dSBrooks Davis 	if (mediasize > (off_t)0xffffffff * secsize)
48486e0bd0dSBrooks Davis 		errx(1,
48586e0bd0dSBrooks Davis 		    "disks with more than 2^32-1 sectors are not supported");
486c80f9755SPoul-Henning Kamp 	(void)lseek(f, (off_t)0, SEEK_SET);
487d2fe97c7SPoul-Henning Kamp 	if (read(f, bootarea, BBSIZE) != BBSIZE)
4885daa806dSPoul-Henning Kamp 		err(4, "%s read", specname);
4895daa806dSPoul-Henning Kamp 	close (f);
490b2bb9f9bSPoul-Henning Kamp 	error = bsd_disklabel_le_dec(
491b2bb9f9bSPoul-Henning Kamp 	    bootarea + (labeloffset + labelsoffset * secsize),
492b2bb9f9bSPoul-Henning Kamp 	    &lab, MAXPARTITIONS);
493d2fe97c7SPoul-Henning Kamp 	if (flag && error)
494d2fe97c7SPoul-Henning Kamp 		errx(1, "%s: no valid label found", specname);
4955d853216SPoul-Henning Kamp 
49683d771deSPoul-Henning Kamp 	grq = gctl_get_handle();
49783d771deSPoul-Henning Kamp 	gctl_ro_param(grq, "verb", -1, "read mbroffset");
4985d853216SPoul-Henning Kamp 	gctl_ro_param(grq, "class", -1, "BSD");
4995d853216SPoul-Henning Kamp 	gctl_ro_param(grq, "geom", -1, dkname);
5005d853216SPoul-Henning Kamp 	gctl_rw_param(grq, "mbroffset", sizeof(mbroffset), &mbroffset);
5015d853216SPoul-Henning Kamp 	errstr = gctl_issue(grq);
5025d853216SPoul-Henning Kamp 	if (errstr != NULL) {
5035d853216SPoul-Henning Kamp 		mbroffset = 0;
5045d853216SPoul-Henning Kamp 		gctl_free(grq);
5055d853216SPoul-Henning Kamp 		return (error);
5065d853216SPoul-Henning Kamp 	}
5075d853216SPoul-Henning Kamp 	mbroffset /= lab.d_secsize;
5085d853216SPoul-Henning Kamp 	if (lab.d_partitions[RAW_PART].p_offset == mbroffset)
5095d853216SPoul-Henning Kamp 		for (i = 0; i < lab.d_npartitions; i++)
5105d853216SPoul-Henning Kamp 			if (lab.d_partitions[i].p_size)
5115d853216SPoul-Henning Kamp 				lab.d_partitions[i].p_offset -= mbroffset;
512d2fe97c7SPoul-Henning Kamp 	return (error);
5138fae3551SRodney W. Grimes }
5148fae3551SRodney W. Grimes 
5158fae3551SRodney W. Grimes 
516d2fe97c7SPoul-Henning Kamp static void
517326c7cdaSWarner Losh display(FILE *f, const struct disklabel *lp)
5188fae3551SRodney W. Grimes {
519326c7cdaSWarner Losh 	int i, j;
520326c7cdaSWarner Losh 	const struct partition *pp;
5218fae3551SRodney W. Grimes 
522d2fe97c7SPoul-Henning Kamp 	if (lp == NULL)
523d2fe97c7SPoul-Henning Kamp 		lp = &lab;
524d2fe97c7SPoul-Henning Kamp 
5258fae3551SRodney W. Grimes 	fprintf(f, "# %s:\n", specname);
52657dfbec5SPoul-Henning Kamp 	if (allfields) {
527484c7804SJulian Elischer 		if (lp->d_type < DKMAXTYPES)
5288fae3551SRodney W. Grimes 			fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
5298fae3551SRodney W. Grimes 		else
5302a1deaaaSBruce Evans 			fprintf(f, "type: %u\n", lp->d_type);
53161de51caSJoerg Wunsch 		fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
53261de51caSJoerg Wunsch 			lp->d_typename);
53361de51caSJoerg Wunsch 		fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
53461de51caSJoerg Wunsch 			lp->d_packname);
5358fae3551SRodney W. Grimes 		fprintf(f, "flags:");
5368fae3551SRodney W. Grimes 		if (lp->d_flags & D_REMOVABLE)
5378fae3551SRodney W. Grimes 			fprintf(f, " removeable");
5388fae3551SRodney W. Grimes 		if (lp->d_flags & D_ECC)
5398fae3551SRodney W. Grimes 			fprintf(f, " ecc");
5408fae3551SRodney W. Grimes 		if (lp->d_flags & D_BADSECT)
5418fae3551SRodney W. Grimes 			fprintf(f, " badsect");
5428fae3551SRodney W. Grimes 		fprintf(f, "\n");
5432a1deaaaSBruce Evans 		fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
5442a1deaaaSBruce Evans 		fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
5452a1deaaaSBruce Evans 		fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
5462a1deaaaSBruce Evans 		fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
5472a1deaaaSBruce Evans 		fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
5482a1deaaaSBruce Evans 		fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
5492a1deaaaSBruce Evans 		fprintf(f, "rpm: %u\n", lp->d_rpm);
5502a1deaaaSBruce Evans 		fprintf(f, "interleave: %u\n", lp->d_interleave);
5512a1deaaaSBruce Evans 		fprintf(f, "trackskew: %u\n", lp->d_trackskew);
5522a1deaaaSBruce Evans 		fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
5532a1deaaaSBruce Evans 		fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
5542a1deaaaSBruce Evans 		    (u_long)lp->d_headswitch);
55561de51caSJoerg Wunsch 		fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
5562a1deaaaSBruce Evans 		    (u_long)lp->d_trkseek);
5578fae3551SRodney W. Grimes 		fprintf(f, "drivedata: ");
5588fae3551SRodney W. Grimes 		for (i = NDDATA - 1; i >= 0; i--)
5598fae3551SRodney W. Grimes 			if (lp->d_drivedata[i])
5608fae3551SRodney W. Grimes 				break;
5618fae3551SRodney W. Grimes 		if (i < 0)
5628fae3551SRodney W. Grimes 			i = 0;
5638fae3551SRodney W. Grimes 		for (j = 0; j <= i; j++)
5642a1deaaaSBruce Evans 			fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
56557dfbec5SPoul-Henning Kamp 		fprintf(f, "\n\n");
56657dfbec5SPoul-Henning Kamp 	}
56757dfbec5SPoul-Henning Kamp 	fprintf(f, "%u partitions:\n", lp->d_npartitions);
5688fae3551SRodney W. Grimes 	fprintf(f,
569ca4693edSJustin T. Gibbs 	    "#        size   offset    fstype   [fsize bsize bps/cpg]\n");
5708fae3551SRodney W. Grimes 	pp = lp->d_partitions;
5718fae3551SRodney W. Grimes 	for (i = 0; i < lp->d_npartitions; i++, pp++) {
5728fae3551SRodney W. Grimes 		if (pp->p_size) {
5732a1deaaaSBruce Evans 			fprintf(f, "  %c: %8lu %8lu  ", 'a' + i,
5742a1deaaaSBruce Evans 			   (u_long)pp->p_size, (u_long)pp->p_offset);
575484c7804SJulian Elischer 			if (pp->p_fstype < FSMAXTYPES)
5768fae3551SRodney W. Grimes 				fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
5778fae3551SRodney W. Grimes 			else
5788fae3551SRodney W. Grimes 				fprintf(f, "%8d", pp->p_fstype);
5798fae3551SRodney W. Grimes 			switch (pp->p_fstype) {
5808fae3551SRodney W. Grimes 
5818fae3551SRodney W. Grimes 			case FS_UNUSED:				/* XXX */
5822a1deaaaSBruce Evans 				fprintf(f, "    %5lu %5lu %5.5s ",
5832a1deaaaSBruce Evans 				    (u_long)pp->p_fsize,
5842a1deaaaSBruce Evans 				    (u_long)(pp->p_fsize * pp->p_frag), "");
5858fae3551SRodney W. Grimes 				break;
5868fae3551SRodney W. Grimes 
5878fae3551SRodney W. Grimes 			case FS_BSDFFS:
5882a1deaaaSBruce Evans 				fprintf(f, "    %5lu %5lu %5u ",
5892a1deaaaSBruce Evans 				    (u_long)pp->p_fsize,
5902a1deaaaSBruce Evans 				    (u_long)(pp->p_fsize * pp->p_frag),
5918fae3551SRodney W. Grimes 				    pp->p_cpg);
5928fae3551SRodney W. Grimes 				break;
5938fae3551SRodney W. Grimes 
594ca4693edSJustin T. Gibbs 			case FS_BSDLFS:
5952a1deaaaSBruce Evans 				fprintf(f, "    %5lu %5lu %5d",
5962a1deaaaSBruce Evans 				    (u_long)pp->p_fsize,
5972a1deaaaSBruce Evans 				    (u_long)(pp->p_fsize * pp->p_frag),
598ca4693edSJustin T. Gibbs 				    pp->p_cpg);
599ca4693edSJustin T. Gibbs 				break;
600ca4693edSJustin T. Gibbs 
6018fae3551SRodney W. Grimes 			default:
6028fae3551SRodney W. Grimes 				fprintf(f, "%20.20s", "");
6038fae3551SRodney W. Grimes 				break;
6048fae3551SRodney W. Grimes 			}
60557dfbec5SPoul-Henning Kamp 			if (i == RAW_PART) {
60657dfbec5SPoul-Henning Kamp 				fprintf(f, "  # \"raw\" part, don't edit");
60757dfbec5SPoul-Henning Kamp 			}
608d2fe97c7SPoul-Henning Kamp 			fprintf(f, "\n");
6098fae3551SRodney W. Grimes 		}
6108fae3551SRodney W. Grimes 	}
6118fae3551SRodney W. Grimes 	fflush(f);
6128fae3551SRodney W. Grimes }
6138fae3551SRodney W. Grimes 
614d2fe97c7SPoul-Henning Kamp static int
615d2fe97c7SPoul-Henning Kamp edit(void)
6168fae3551SRodney W. Grimes {
617326c7cdaSWarner Losh 	int c, fd;
6188fae3551SRodney W. Grimes 	struct disklabel label;
619722ceb3fSWarner Losh 	FILE *fp;
6208fae3551SRodney W. Grimes 
621722ceb3fSWarner Losh 	if ((fd = mkstemp(tmpfil)) == -1 ||
622722ceb3fSWarner Losh 	    (fp = fdopen(fd, "w")) == NULL) {
6236bd343a9SPhilippe Charnier 		warnx("can't create %s", tmpfil);
6248fae3551SRodney W. Grimes 		return (1);
6258fae3551SRodney W. Grimes 	}
626d2fe97c7SPoul-Henning Kamp 	display(fp, NULL);
627722ceb3fSWarner Losh 	fclose(fp);
6288fae3551SRodney W. Grimes 	for (;;) {
6298fae3551SRodney W. Grimes 		if (!editit())
6308fae3551SRodney W. Grimes 			break;
631722ceb3fSWarner Losh 		fp = fopen(tmpfil, "r");
632722ceb3fSWarner Losh 		if (fp == NULL) {
6336bd343a9SPhilippe Charnier 			warnx("can't reopen %s for reading", tmpfil);
6348fae3551SRodney W. Grimes 			break;
6358fae3551SRodney W. Grimes 		}
6368fae3551SRodney W. Grimes 		bzero((char *)&label, sizeof(label));
637d2fe97c7SPoul-Henning Kamp 		c = getasciilabel(fp, &label);
638722ceb3fSWarner Losh 		fclose(fp);
639d2fe97c7SPoul-Henning Kamp 		if (c) {
640d2fe97c7SPoul-Henning Kamp 			lab = label;
641d2fe97c7SPoul-Henning Kamp 			if (writelabel() == 0) {
6428fae3551SRodney W. Grimes 				(void) unlink(tmpfil);
6438fae3551SRodney W. Grimes 				return (0);
6448fae3551SRodney W. Grimes 			}
6458fae3551SRodney W. Grimes 		}
646d2fe97c7SPoul-Henning Kamp 		printf("re-edit the label? [y]: ");
647d2fe97c7SPoul-Henning Kamp 		fflush(stdout);
6488fae3551SRodney W. Grimes 		c = getchar();
6498fae3551SRodney W. Grimes 		if (c != EOF && c != (int)'\n')
6508fae3551SRodney W. Grimes 			while (getchar() != (int)'\n')
6518fae3551SRodney W. Grimes 				;
6528fae3551SRodney W. Grimes 		if  (c == (int)'n')
6538fae3551SRodney W. Grimes 			break;
6548fae3551SRodney W. Grimes 	}
6558fae3551SRodney W. Grimes 	(void) unlink(tmpfil);
6568fae3551SRodney W. Grimes 	return (1);
6578fae3551SRodney W. Grimes }
6588fae3551SRodney W. Grimes 
659d2fe97c7SPoul-Henning Kamp static int
660326c7cdaSWarner Losh editit(void)
6618fae3551SRodney W. Grimes {
662326c7cdaSWarner Losh 	int pid, xpid;
663c8223965SMark Murray 	int locstat, omask;
664c8223965SMark Murray 	const char *ed;
6658fae3551SRodney W. Grimes 
6668fae3551SRodney W. Grimes 	omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
6678fae3551SRodney W. Grimes 	while ((pid = fork()) < 0) {
6688fae3551SRodney W. Grimes 		if (errno == EPROCLIM) {
6696bd343a9SPhilippe Charnier 			warnx("you have too many processes");
6708fae3551SRodney W. Grimes 			return(0);
6718fae3551SRodney W. Grimes 		}
6728fae3551SRodney W. Grimes 		if (errno != EAGAIN) {
6736bd343a9SPhilippe Charnier 			warn("fork");
6748fae3551SRodney W. Grimes 			return(0);
6758fae3551SRodney W. Grimes 		}
6768fae3551SRodney W. Grimes 		sleep(1);
6778fae3551SRodney W. Grimes 	}
6788fae3551SRodney W. Grimes 	if (pid == 0) {
6798fae3551SRodney W. Grimes 		sigsetmask(omask);
6808fae3551SRodney W. Grimes 		setgid(getgid());
6818fae3551SRodney W. Grimes 		setuid(getuid());
6828fae3551SRodney W. Grimes 		if ((ed = getenv("EDITOR")) == (char *)0)
6838fae3551SRodney W. Grimes 			ed = DEFEDITOR;
6847bc6d015SBrian Somers 		execlp(ed, ed, tmpfil, (char *)0);
6856bd343a9SPhilippe Charnier 		err(1, "%s", ed);
6868fae3551SRodney W. Grimes 	}
687c8223965SMark Murray 	while ((xpid = wait(&locstat)) >= 0)
6888fae3551SRodney W. Grimes 		if (xpid == pid)
6898fae3551SRodney W. Grimes 			break;
6908fae3551SRodney W. Grimes 	sigsetmask(omask);
691c8223965SMark Murray 	return(!locstat);
6928fae3551SRodney W. Grimes }
6938fae3551SRodney W. Grimes 
694d2fe97c7SPoul-Henning Kamp static char *
695326c7cdaSWarner Losh skip(char *cp)
6968fae3551SRodney W. Grimes {
6978fae3551SRodney W. Grimes 
6988fae3551SRodney W. Grimes 	while (*cp != '\0' && isspace(*cp))
6998fae3551SRodney W. Grimes 		cp++;
7008fae3551SRodney W. Grimes 	if (*cp == '\0' || *cp == '#')
701326c7cdaSWarner Losh 		return (NULL);
7028fae3551SRodney W. Grimes 	return (cp);
7038fae3551SRodney W. Grimes }
7048fae3551SRodney W. Grimes 
705d2fe97c7SPoul-Henning Kamp static char *
706326c7cdaSWarner Losh word(char *cp)
7078fae3551SRodney W. Grimes {
708326c7cdaSWarner Losh 	char c;
7098fae3551SRodney W. Grimes 
7108fae3551SRodney W. Grimes 	while (*cp != '\0' && !isspace(*cp) && *cp != '#')
7118fae3551SRodney W. Grimes 		cp++;
7128fae3551SRodney W. Grimes 	if ((c = *cp) != '\0') {
7138fae3551SRodney W. Grimes 		*cp++ = '\0';
7148fae3551SRodney W. Grimes 		if (c != '#')
7158fae3551SRodney W. Grimes 			return (skip(cp));
7168fae3551SRodney W. Grimes 	}
717326c7cdaSWarner Losh 	return (NULL);
7188fae3551SRodney W. Grimes }
7198fae3551SRodney W. Grimes 
7208fae3551SRodney W. Grimes /*
7218fae3551SRodney W. Grimes  * Read an ascii label in from fd f,
7228fae3551SRodney W. Grimes  * in the same format as that put out by display(),
7238fae3551SRodney W. Grimes  * and fill in lp.
7248fae3551SRodney W. Grimes  */
725d2fe97c7SPoul-Henning Kamp static int
726326c7cdaSWarner Losh getasciilabel(FILE *f, struct disklabel *lp)
7278fae3551SRodney W. Grimes {
7286b0ff5f5SPoul-Henning Kamp 	char *cp;
7296b0ff5f5SPoul-Henning Kamp 	const char **cpp;
730484c7804SJulian Elischer 	u_int part;
7316b0ff5f5SPoul-Henning Kamp 	char *tp, line[BUFSIZ];
732484c7804SJulian Elischer 	u_long v;
733484c7804SJulian Elischer 	int lineno = 0, errors = 0;
734326c7cdaSWarner Losh 	int i;
7358fae3551SRodney W. Grimes 
7368970f1a4SPoul-Henning Kamp 	makelabel("auto", lp);
737b0459c58SDag-Erling Smørgrav 	bzero(&part_set, sizeof(part_set));
738b0459c58SDag-Erling Smørgrav 	bzero(&part_size_type, sizeof(part_size_type));
739b0459c58SDag-Erling Smørgrav 	bzero(&part_offset_type, sizeof(part_offset_type));
7408fae3551SRodney W. Grimes 	lp->d_bbsize = BBSIZE;				/* XXX */
74177068a7fSPoul-Henning Kamp 	lp->d_sbsize = 0;				/* XXX */
7428fae3551SRodney W. Grimes 	while (fgets(line, sizeof(line) - 1, f)) {
7438fae3551SRodney W. Grimes 		lineno++;
74461de51caSJoerg Wunsch 		if ((cp = index(line,'\n')) != 0)
7458fae3551SRodney W. Grimes 			*cp = '\0';
7468fae3551SRodney W. Grimes 		cp = skip(line);
7478fae3551SRodney W. Grimes 		if (cp == NULL)
7488fae3551SRodney W. Grimes 			continue;
7498fae3551SRodney W. Grimes 		tp = index(cp, ':');
7508fae3551SRodney W. Grimes 		if (tp == NULL) {
7518fae3551SRodney W. Grimes 			fprintf(stderr, "line %d: syntax error\n", lineno);
7528fae3551SRodney W. Grimes 			errors++;
7538fae3551SRodney W. Grimes 			continue;
7548fae3551SRodney W. Grimes 		}
7558fae3551SRodney W. Grimes 		*tp++ = '\0', tp = skip(tp);
756d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "type")) {
7578fae3551SRodney W. Grimes 			if (tp == NULL)
758c8223965SMark Murray 				tp = unknown;
7598fae3551SRodney W. Grimes 			cpp = dktypenames;
7608fae3551SRodney W. Grimes 			for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
761d2fe97c7SPoul-Henning Kamp 				if (*cpp && !strcmp(*cpp, tp)) {
7628fae3551SRodney W. Grimes 					lp->d_type = cpp - dktypenames;
76309dbd070SIan Dowse 					break;
7648fae3551SRodney W. Grimes 				}
76509dbd070SIan Dowse 			if (cpp < &dktypenames[DKMAXTYPES])
76609dbd070SIan Dowse 				continue;
767484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
768484c7804SJulian Elischer 			if (v >= DKMAXTYPES)
769484c7804SJulian Elischer 				fprintf(stderr, "line %d:%s %lu\n", lineno,
7708fae3551SRodney W. Grimes 				    "Warning, unknown disk type", v);
7718fae3551SRodney W. Grimes 			lp->d_type = v;
7728fae3551SRodney W. Grimes 			continue;
7738fae3551SRodney W. Grimes 		}
774d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "flags")) {
7758fae3551SRodney W. Grimes 			for (v = 0; (cp = tp) && *cp != '\0';) {
7768fae3551SRodney W. Grimes 				tp = word(cp);
777d2fe97c7SPoul-Henning Kamp 				if (!strcmp(cp, "removeable"))
7788fae3551SRodney W. Grimes 					v |= D_REMOVABLE;
779d2fe97c7SPoul-Henning Kamp 				else if (!strcmp(cp, "ecc"))
7808fae3551SRodney W. Grimes 					v |= D_ECC;
781d2fe97c7SPoul-Henning Kamp 				else if (!strcmp(cp, "badsect"))
7828fae3551SRodney W. Grimes 					v |= D_BADSECT;
7838fae3551SRodney W. Grimes 				else {
7848fae3551SRodney W. Grimes 					fprintf(stderr,
7858fae3551SRodney W. Grimes 					    "line %d: %s: bad flag\n",
7868fae3551SRodney W. Grimes 					    lineno, cp);
7878fae3551SRodney W. Grimes 					errors++;
7888fae3551SRodney W. Grimes 				}
7898fae3551SRodney W. Grimes 			}
7908fae3551SRodney W. Grimes 			lp->d_flags = v;
7918fae3551SRodney W. Grimes 			continue;
7928fae3551SRodney W. Grimes 		}
793d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "drivedata")) {
7948fae3551SRodney W. Grimes 			for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
795484c7804SJulian Elischer 				lp->d_drivedata[i++] = strtoul(cp, NULL, 10);
7968fae3551SRodney W. Grimes 				tp = word(cp);
7978fae3551SRodney W. Grimes 			}
7988fae3551SRodney W. Grimes 			continue;
7998fae3551SRodney W. Grimes 		}
800484c7804SJulian Elischer 		if (sscanf(cp, "%lu partitions", &v) == 1) {
801484c7804SJulian Elischer 			if (v == 0 || v > MAXPARTITIONS) {
8028fae3551SRodney W. Grimes 				fprintf(stderr,
8038fae3551SRodney W. Grimes 				    "line %d: bad # of partitions\n", lineno);
8048fae3551SRodney W. Grimes 				lp->d_npartitions = MAXPARTITIONS;
8058fae3551SRodney W. Grimes 				errors++;
8068fae3551SRodney W. Grimes 			} else
8078fae3551SRodney W. Grimes 				lp->d_npartitions = v;
8088fae3551SRodney W. Grimes 			continue;
8098fae3551SRodney W. Grimes 		}
8108fae3551SRodney W. Grimes 		if (tp == NULL)
811c8223965SMark Murray 			tp = blank;
812d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "disk")) {
8138fae3551SRodney W. Grimes 			strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
8148fae3551SRodney W. Grimes 			continue;
8158fae3551SRodney W. Grimes 		}
816d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "label")) {
8178fae3551SRodney W. Grimes 			strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
8188fae3551SRodney W. Grimes 			continue;
8198fae3551SRodney W. Grimes 		}
820d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "bytes/sector")) {
821484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
822484c7804SJulian Elischer 			if (v == 0 || (v % DEV_BSIZE) != 0) {
8238fae3551SRodney W. Grimes 				fprintf(stderr,
8248fae3551SRodney W. Grimes 				    "line %d: %s: bad sector size\n",
8258fae3551SRodney W. Grimes 				    lineno, tp);
8268fae3551SRodney W. Grimes 				errors++;
8278fae3551SRodney W. Grimes 			} else
8288fae3551SRodney W. Grimes 				lp->d_secsize = v;
8298fae3551SRodney W. Grimes 			continue;
8308fae3551SRodney W. Grimes 		}
831d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "sectors/track")) {
832484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
833484c7804SJulian Elischer #if (ULONG_MAX != 0xffffffffUL)
834266e7997SDag-Erling Smørgrav 			if (v == 0 || v > 0xffffffff)
835484c7804SJulian Elischer #else
836266e7997SDag-Erling Smørgrav 			if (v == 0)
837484c7804SJulian Elischer #endif
838266e7997SDag-Erling Smørgrav 			{
8398fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
8408fae3551SRodney W. Grimes 				    lineno, tp, cp);
8418fae3551SRodney W. Grimes 				errors++;
8428fae3551SRodney W. Grimes 			} else
8438fae3551SRodney W. Grimes 				lp->d_nsectors = v;
8448fae3551SRodney W. Grimes 			continue;
8458fae3551SRodney W. Grimes 		}
846d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "sectors/cylinder")) {
847484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
848484c7804SJulian Elischer 			if (v == 0) {
8498fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
8508fae3551SRodney W. Grimes 				    lineno, tp, cp);
8518fae3551SRodney W. Grimes 				errors++;
8528fae3551SRodney W. Grimes 			} else
8538fae3551SRodney W. Grimes 				lp->d_secpercyl = v;
8548fae3551SRodney W. Grimes 			continue;
8558fae3551SRodney W. Grimes 		}
856d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "tracks/cylinder")) {
857484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
858484c7804SJulian Elischer 			if (v == 0) {
8598fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
8608fae3551SRodney W. Grimes 				    lineno, tp, cp);
8618fae3551SRodney W. Grimes 				errors++;
8628fae3551SRodney W. Grimes 			} else
8638fae3551SRodney W. Grimes 				lp->d_ntracks = v;
8648fae3551SRodney W. Grimes 			continue;
8658fae3551SRodney W. Grimes 		}
866d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "cylinders")) {
867484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
868484c7804SJulian Elischer 			if (v == 0) {
8698fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
8708fae3551SRodney W. Grimes 				    lineno, tp, cp);
8718fae3551SRodney W. Grimes 				errors++;
8728fae3551SRodney W. Grimes 			} else
8738fae3551SRodney W. Grimes 				lp->d_ncylinders = v;
8748fae3551SRodney W. Grimes 			continue;
8758fae3551SRodney W. Grimes 		}
876d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "sectors/unit")) {
877484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
878484c7804SJulian Elischer 			if (v == 0) {
879f75dd518SBruce Evans 				fprintf(stderr, "line %d: %s: bad %s\n",
880f75dd518SBruce Evans 				    lineno, tp, cp);
881f75dd518SBruce Evans 				errors++;
882f75dd518SBruce Evans 			} else
883f75dd518SBruce Evans 				lp->d_secperunit = v;
884f75dd518SBruce Evans 			continue;
885f75dd518SBruce Evans 		}
886d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "rpm")) {
887484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
888484c7804SJulian Elischer 			if (v == 0 || v > USHRT_MAX) {
8898fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
8908fae3551SRodney W. Grimes 				    lineno, tp, cp);
8918fae3551SRodney W. Grimes 				errors++;
8928fae3551SRodney W. Grimes 			} else
8938fae3551SRodney W. Grimes 				lp->d_rpm = v;
8948fae3551SRodney W. Grimes 			continue;
8958fae3551SRodney W. Grimes 		}
896d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "interleave")) {
897484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
898484c7804SJulian Elischer 			if (v == 0 || v > USHRT_MAX) {
8998fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
9008fae3551SRodney W. Grimes 				    lineno, tp, cp);
9018fae3551SRodney W. Grimes 				errors++;
9028fae3551SRodney W. Grimes 			} else
9038fae3551SRodney W. Grimes 				lp->d_interleave = v;
9048fae3551SRodney W. Grimes 			continue;
9058fae3551SRodney W. Grimes 		}
906d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "trackskew")) {
907484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
908484c7804SJulian Elischer 			if (v > USHRT_MAX) {
9098fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
9108fae3551SRodney W. Grimes 				    lineno, tp, cp);
9118fae3551SRodney W. Grimes 				errors++;
9128fae3551SRodney W. Grimes 			} else
9138fae3551SRodney W. Grimes 				lp->d_trackskew = v;
9148fae3551SRodney W. Grimes 			continue;
9158fae3551SRodney W. Grimes 		}
916d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "cylinderskew")) {
917484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
918484c7804SJulian Elischer 			if (v > USHRT_MAX) {
9198fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
9208fae3551SRodney W. Grimes 				    lineno, tp, cp);
9218fae3551SRodney W. Grimes 				errors++;
9228fae3551SRodney W. Grimes 			} else
9238fae3551SRodney W. Grimes 				lp->d_cylskew = v;
9248fae3551SRodney W. Grimes 			continue;
9258fae3551SRodney W. Grimes 		}
926d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "headswitch")) {
927484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
9288fae3551SRodney W. Grimes 			lp->d_headswitch = v;
9298fae3551SRodney W. Grimes 			continue;
9308fae3551SRodney W. Grimes 		}
931d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "track-to-track seek")) {
932484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
9338fae3551SRodney W. Grimes 			lp->d_trkseek = v;
9348fae3551SRodney W. Grimes 			continue;
9358fae3551SRodney W. Grimes 		}
9363233afaeSJohn W. De Boskey 		/* the ':' was removed above */
93767b46708SIan Dowse 		if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') {
93867b46708SIan Dowse 			fprintf(stderr,
93967b46708SIan Dowse 			    "line %d: %s: Unknown disklabel field\n", lineno,
94067b46708SIan Dowse 			    cp);
94167b46708SIan Dowse 			errors++;
94267b46708SIan Dowse 			continue;
94367b46708SIan Dowse 		}
94467b46708SIan Dowse 
94567b46708SIan Dowse 		/* Process a partition specification line. */
9463233afaeSJohn W. De Boskey 		part = *cp - 'a';
9473233afaeSJohn W. De Boskey 		if (part >= lp->d_npartitions) {
9488fae3551SRodney W. Grimes 			fprintf(stderr,
9493233afaeSJohn W. De Boskey 			    "line %d: partition name out of range a-%c: %s\n",
9503233afaeSJohn W. De Boskey 			    lineno, 'a' + lp->d_npartitions - 1, cp);
9518fae3551SRodney W. Grimes 			errors++;
9528fae3551SRodney W. Grimes 			continue;
9538fae3551SRodney W. Grimes 		}
9543233afaeSJohn W. De Boskey 		part_set[part] = 1;
95567b46708SIan Dowse 
95667b46708SIan Dowse 		if (getasciipartspec(tp, lp, part, lineno) != 0) {
95767b46708SIan Dowse 			errors++;
95867b46708SIan Dowse 			break;
95967b46708SIan Dowse 		}
96067b46708SIan Dowse 	}
96167b46708SIan Dowse 	errors += checklabel(lp);
96267b46708SIan Dowse 	return (errors == 0);
96367b46708SIan Dowse }
96467b46708SIan Dowse 
96567b46708SIan Dowse #define NXTNUM(n) do { \
96613e0abcbSPaul Traina 	if (tp == NULL) { \
96713e0abcbSPaul Traina 		fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
96867b46708SIan Dowse 		return (1); \
96913e0abcbSPaul Traina 	} else { \
9708fae3551SRodney W. Grimes 		cp = tp, tp = word(cp); \
971484c7804SJulian Elischer 		(n) = strtoul(cp, NULL, 10); \
97213e0abcbSPaul Traina 	} \
97367b46708SIan Dowse } while (0)
97467b46708SIan Dowse 
9753233afaeSJohn W. De Boskey /* retain 1 character following number */
97667b46708SIan Dowse #define NXTWORD(w,n) do { \
9773233afaeSJohn W. De Boskey 	if (tp == NULL) { \
9783233afaeSJohn W. De Boskey 		fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
97967b46708SIan Dowse 		return (1); \
9803233afaeSJohn W. De Boskey 	} else { \
9813233afaeSJohn W. De Boskey 		char *tmp; \
9823233afaeSJohn W. De Boskey 		cp = tp, tp = word(cp); \
983484c7804SJulian Elischer 		(n) = strtoul(cp, &tmp, 10); \
9843233afaeSJohn W. De Boskey 		if (tmp) (w) = *tmp; \
9853233afaeSJohn W. De Boskey 	} \
98667b46708SIan Dowse } while (0)
98767b46708SIan Dowse 
98867b46708SIan Dowse /*
98967b46708SIan Dowse  * Read a partition line into partition `part' in the specified disklabel.
99067b46708SIan Dowse  * Return 0 on success, 1 on failure.
99167b46708SIan Dowse  */
992d2fe97c7SPoul-Henning Kamp static int
99367b46708SIan Dowse getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno)
99467b46708SIan Dowse {
99567b46708SIan Dowse 	struct partition *pp;
99667b46708SIan Dowse 	char *cp;
99767b46708SIan Dowse 	const char **cpp;
998484c7804SJulian Elischer 	u_long v;
99967b46708SIan Dowse 
100067b46708SIan Dowse 	pp = &lp->d_partitions[part];
100167b46708SIan Dowse 	cp = NULL;
100267b46708SIan Dowse 
10033233afaeSJohn W. De Boskey 	v = 0;
10043233afaeSJohn W. De Boskey 	NXTWORD(part_size_type[part],v);
1005484c7804SJulian Elischer 	if (v == 0 && part_size_type[part] != '*') {
1006484c7804SJulian Elischer 		fprintf(stderr,
1007484c7804SJulian Elischer 		    "line %d: %s: bad partition size\n", lineno, cp);
100867b46708SIan Dowse 		return (1);
100967b46708SIan Dowse 	}
10108fae3551SRodney W. Grimes 	pp->p_size = v;
10113233afaeSJohn W. De Boskey 
10123233afaeSJohn W. De Boskey 	v = 0;
10133233afaeSJohn W. De Boskey 	NXTWORD(part_offset_type[part],v);
1014484c7804SJulian Elischer 	if (v == 0 && part_offset_type[part] != '*' &&
1015484c7804SJulian Elischer 	    part_offset_type[part] != '\0') {
1016484c7804SJulian Elischer 		fprintf(stderr,
1017484c7804SJulian Elischer 		    "line %d: %s: bad partition offset\n", lineno, cp);
101867b46708SIan Dowse 		return (1);
101967b46708SIan Dowse 	}
10208fae3551SRodney W. Grimes 	pp->p_offset = v;
102129f3f095SYaroslav Tykhiy 	if (tp == NULL) {
102229f3f095SYaroslav Tykhiy 		fprintf(stderr, "line %d: missing file system type\n", lineno);
102329f3f095SYaroslav Tykhiy 		return (1);
102429f3f095SYaroslav Tykhiy 	}
10258fae3551SRodney W. Grimes 	cp = tp, tp = word(cp);
102667b46708SIan Dowse 	for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1027d2fe97c7SPoul-Henning Kamp 		if (*cpp && !strcmp(*cpp, cp))
102867b46708SIan Dowse 			break;
102967b46708SIan Dowse 	if (*cpp != NULL) {
103067b46708SIan Dowse 		pp->p_fstype = cpp - fstypenames;
103167b46708SIan Dowse 	} else {
10328fae3551SRodney W. Grimes 		if (isdigit(*cp))
1033484c7804SJulian Elischer 			v = strtoul(cp, NULL, 10);
10348fae3551SRodney W. Grimes 		else
10358fae3551SRodney W. Grimes 			v = FSMAXTYPES;
1036484c7804SJulian Elischer 		if (v >= FSMAXTYPES) {
10373233afaeSJohn W. De Boskey 			fprintf(stderr,
103867b46708SIan Dowse 			    "line %d: Warning, unknown file system type %s\n",
10393233afaeSJohn W. De Boskey 			    lineno, cp);
10408fae3551SRodney W. Grimes 			v = FS_UNUSED;
10418fae3551SRodney W. Grimes 		}
10428fae3551SRodney W. Grimes 		pp->p_fstype = v;
104367b46708SIan Dowse 	}
104467b46708SIan Dowse 
10458fae3551SRodney W. Grimes 	switch (pp->p_fstype) {
10463233afaeSJohn W. De Boskey 	case FS_UNUSED:
1047640c9cb2SIan Dowse 	case FS_BSDFFS:
1048640c9cb2SIan Dowse 	case FS_BSDLFS:
1049640c9cb2SIan Dowse 		/* accept defaults for fsize/frag/cpg */
10503233afaeSJohn W. De Boskey 		if (tp) {
10518fae3551SRodney W. Grimes 			NXTNUM(pp->p_fsize);
10528fae3551SRodney W. Grimes 			if (pp->p_fsize == 0)
10538fae3551SRodney W. Grimes 				break;
10548fae3551SRodney W. Grimes 			NXTNUM(v);
10558fae3551SRodney W. Grimes 			pp->p_frag = v / pp->p_fsize;
1056640c9cb2SIan Dowse 			if (tp != NULL)
1057640c9cb2SIan Dowse 				NXTNUM(pp->p_cpg);
10583233afaeSJohn W. De Boskey 		}
10593233afaeSJohn W. De Boskey 		/* else default to 0's */
10608fae3551SRodney W. Grimes 		break;
10618fae3551SRodney W. Grimes 	default:
10628fae3551SRodney W. Grimes 		break;
10638fae3551SRodney W. Grimes 	}
106467b46708SIan Dowse 	return (0);
10658fae3551SRodney W. Grimes }
10668fae3551SRodney W. Grimes 
10678fae3551SRodney W. Grimes /*
10688fae3551SRodney W. Grimes  * Check disklabel for errors and fill in
10698fae3551SRodney W. Grimes  * derived fields according to supplied values.
10708fae3551SRodney W. Grimes  */
1071d2fe97c7SPoul-Henning Kamp static int
1072326c7cdaSWarner Losh checklabel(struct disklabel *lp)
10738fae3551SRodney W. Grimes {
1074326c7cdaSWarner Losh 	struct partition *pp;
10758fae3551SRodney W. Grimes 	int i, errors = 0;
10768fae3551SRodney W. Grimes 	char part;
10773b89beb1SIan Dowse 	u_long base_offset, needed, total_size, total_percent, current_offset;
10783b89beb1SIan Dowse 	long free_space;
10793233afaeSJohn W. De Boskey 	int seen_default_offset;
10803233afaeSJohn W. De Boskey 	int hog_part;
10813233afaeSJohn W. De Boskey 	int j;
10823233afaeSJohn W. De Boskey 	struct partition *pp2;
10838fae3551SRodney W. Grimes 
1084d2fe97c7SPoul-Henning Kamp 	if (lp == NULL)
1085d2fe97c7SPoul-Henning Kamp 		lp = &lab;
1086d2fe97c7SPoul-Henning Kamp 
108757dfbec5SPoul-Henning Kamp 	if (allfields) {
108857dfbec5SPoul-Henning Kamp 
10898fae3551SRodney W. Grimes 		if (lp->d_secsize == 0) {
10902a1deaaaSBruce Evans 			fprintf(stderr, "sector size 0\n");
10918fae3551SRodney W. Grimes 			return (1);
10928fae3551SRodney W. Grimes 		}
10938fae3551SRodney W. Grimes 		if (lp->d_nsectors == 0) {
10942a1deaaaSBruce Evans 			fprintf(stderr, "sectors/track 0\n");
10958fae3551SRodney W. Grimes 			return (1);
10968fae3551SRodney W. Grimes 		}
10978fae3551SRodney W. Grimes 		if (lp->d_ntracks == 0) {
10982a1deaaaSBruce Evans 			fprintf(stderr, "tracks/cylinder 0\n");
10998fae3551SRodney W. Grimes 			return (1);
11008fae3551SRodney W. Grimes 		}
11018fae3551SRodney W. Grimes 		if  (lp->d_ncylinders == 0) {
11022a1deaaaSBruce Evans 			fprintf(stderr, "cylinders/unit 0\n");
11038fae3551SRodney W. Grimes 			errors++;
11048fae3551SRodney W. Grimes 		}
11058fae3551SRodney W. Grimes 		if (lp->d_rpm == 0)
110657dfbec5SPoul-Henning Kamp 			warnx("revolutions/minute 0");
11078fae3551SRodney W. Grimes 		if (lp->d_secpercyl == 0)
11088fae3551SRodney W. Grimes 			lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
11098fae3551SRodney W. Grimes 		if (lp->d_secperunit == 0)
11108fae3551SRodney W. Grimes 			lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
11118fae3551SRodney W. Grimes 		if (lp->d_bbsize == 0) {
11122a1deaaaSBruce Evans 			fprintf(stderr, "boot block size 0\n");
11138fae3551SRodney W. Grimes 			errors++;
11148fae3551SRodney W. Grimes 		} else if (lp->d_bbsize % lp->d_secsize)
111557dfbec5SPoul-Henning Kamp 			warnx("boot block size %% sector-size != 0");
11168fae3551SRodney W. Grimes 		if (lp->d_npartitions > MAXPARTITIONS)
111757dfbec5SPoul-Henning Kamp 			warnx("number of partitions (%lu) > MAXPARTITIONS (%d)",
11182a1deaaaSBruce Evans 			    (u_long)lp->d_npartitions, MAXPARTITIONS);
111957dfbec5SPoul-Henning Kamp 	} else {
112057dfbec5SPoul-Henning Kamp 		struct disklabel *vl;
112157dfbec5SPoul-Henning Kamp 
112257dfbec5SPoul-Henning Kamp 		vl = getvirginlabel();
112357dfbec5SPoul-Henning Kamp 		lp->d_secsize = vl->d_secsize;
112457dfbec5SPoul-Henning Kamp 		lp->d_nsectors = vl->d_nsectors;
112557dfbec5SPoul-Henning Kamp 		lp->d_ntracks = vl->d_ntracks;
112657dfbec5SPoul-Henning Kamp 		lp->d_ncylinders = vl->d_ncylinders;
112757dfbec5SPoul-Henning Kamp 		lp->d_rpm = vl->d_rpm;
112857dfbec5SPoul-Henning Kamp 		lp->d_interleave = vl->d_interleave;
112957dfbec5SPoul-Henning Kamp 		lp->d_secpercyl = vl->d_secpercyl;
113057dfbec5SPoul-Henning Kamp 		lp->d_secperunit = vl->d_secperunit;
113157dfbec5SPoul-Henning Kamp 		lp->d_bbsize = vl->d_bbsize;
113257dfbec5SPoul-Henning Kamp 		lp->d_npartitions = vl->d_npartitions;
113357dfbec5SPoul-Henning Kamp 	}
113457dfbec5SPoul-Henning Kamp 
11353233afaeSJohn W. De Boskey 
11363233afaeSJohn W. De Boskey 	/* first allocate space to the partitions, then offsets */
11373233afaeSJohn W. De Boskey 	total_size = 0; /* in sectors */
11383233afaeSJohn W. De Boskey 	total_percent = 0; /* in percent */
11393233afaeSJohn W. De Boskey 	hog_part = -1;
11403233afaeSJohn W. De Boskey 	/* find all fixed partitions */
11413233afaeSJohn W. De Boskey 	for (i = 0; i < lp->d_npartitions; i++) {
11423233afaeSJohn W. De Boskey 		pp = &lp->d_partitions[i];
11433233afaeSJohn W. De Boskey 		if (part_set[i]) {
11443233afaeSJohn W. De Boskey 			if (part_size_type[i] == '*') {
11456b0ff5f5SPoul-Henning Kamp 				if (i == RAW_PART) {
11463233afaeSJohn W. De Boskey 					pp->p_size = lp->d_secperunit;
11473233afaeSJohn W. De Boskey 				} else {
11483233afaeSJohn W. De Boskey 					if (hog_part != -1)
114957dfbec5SPoul-Henning Kamp 						warnx("Too many '*' partitions (%c and %c)",
11503233afaeSJohn W. De Boskey 						    hog_part + 'a',i + 'a');
11513233afaeSJohn W. De Boskey 					else
11523233afaeSJohn W. De Boskey 						hog_part = i;
11533233afaeSJohn W. De Boskey 				}
11543233afaeSJohn W. De Boskey 			} else {
11558d3105e8SWarner Losh 				off_t size;
11563233afaeSJohn W. De Boskey 
11573233afaeSJohn W. De Boskey 				size = pp->p_size;
11583233afaeSJohn W. De Boskey 				switch (part_size_type[i]) {
11593233afaeSJohn W. De Boskey 				case '%':
11603233afaeSJohn W. De Boskey 					total_percent += size;
11613233afaeSJohn W. De Boskey 					break;
11624c814dfcSDag-Erling Smørgrav 				case 't':
11634c814dfcSDag-Erling Smørgrav 				case 'T':
11644c814dfcSDag-Erling Smørgrav 					size *= 1024ULL;
11654c814dfcSDag-Erling Smørgrav 					/* FALLTHROUGH */
11664c814dfcSDag-Erling Smørgrav 				case 'g':
11674c814dfcSDag-Erling Smørgrav 				case 'G':
11684c814dfcSDag-Erling Smørgrav 					size *= 1024ULL;
11694c814dfcSDag-Erling Smørgrav 					/* FALLTHROUGH */
11704c814dfcSDag-Erling Smørgrav 				case 'm':
11714c814dfcSDag-Erling Smørgrav 				case 'M':
11724c814dfcSDag-Erling Smørgrav 					size *= 1024ULL;
11734c814dfcSDag-Erling Smørgrav 					/* FALLTHROUGH */
11743233afaeSJohn W. De Boskey 				case 'k':
11753233afaeSJohn W. De Boskey 				case 'K':
11768d3105e8SWarner Losh 					size *= 1024ULL;
11773233afaeSJohn W. De Boskey 					break;
11783233afaeSJohn W. De Boskey 				case '\0':
11793233afaeSJohn W. De Boskey 					break;
11803233afaeSJohn W. De Boskey 				default:
118181807292SDag-Erling Smørgrav 					warnx("unknown multiplier suffix '%c' for partition %c (should be K, M, G or T)",
11824c814dfcSDag-Erling Smørgrav 					    part_size_type[i], i + 'a');
11833233afaeSJohn W. De Boskey 					break;
11843233afaeSJohn W. De Boskey 				}
11853233afaeSJohn W. De Boskey 				/* don't count %'s yet */
11863233afaeSJohn W. De Boskey 				if (part_size_type[i] != '%') {
11873233afaeSJohn W. De Boskey 					/*
11883233afaeSJohn W. De Boskey 					 * for all not in sectors, convert to
11893233afaeSJohn W. De Boskey 					 * sectors
11903233afaeSJohn W. De Boskey 					 */
11913233afaeSJohn W. De Boskey 					if (part_size_type[i] != '\0') {
11923233afaeSJohn W. De Boskey 						if (size % lp->d_secsize != 0)
119357dfbec5SPoul-Henning Kamp 							warnx("partition %c not an integer number of sectors",
11943233afaeSJohn W. De Boskey 							    i + 'a');
11953233afaeSJohn W. De Boskey 						size /= lp->d_secsize;
11963233afaeSJohn W. De Boskey 						pp->p_size = size;
11973233afaeSJohn W. De Boskey 					}
11983233afaeSJohn W. De Boskey 					/* else already in sectors */
11996b0ff5f5SPoul-Henning Kamp 					if (i != RAW_PART)
12003233afaeSJohn W. De Boskey 						total_size += size;
12013233afaeSJohn W. De Boskey 				}
12023233afaeSJohn W. De Boskey 			}
12033233afaeSJohn W. De Boskey 		}
12043233afaeSJohn W. De Boskey 	}
12053b89beb1SIan Dowse 
12063b89beb1SIan Dowse 	/* Find out the total free space, excluding the boot block area. */
12073b89beb1SIan Dowse 	base_offset = BBSIZE / secsize;
12083b89beb1SIan Dowse 	free_space = 0;
12093b89beb1SIan Dowse 	for (i = 0; i < lp->d_npartitions; i++) {
12103b89beb1SIan Dowse 		pp = &lp->d_partitions[i];
12113b89beb1SIan Dowse 		if (!part_set[i] || i == RAW_PART ||
12123b89beb1SIan Dowse 		    part_size_type[i] == '%' || part_size_type[i] == '*')
12133b89beb1SIan Dowse 			continue;
12143b89beb1SIan Dowse 		if (pp->p_offset > base_offset)
12153b89beb1SIan Dowse 			free_space += pp->p_offset - base_offset;
12163b89beb1SIan Dowse 		if (pp->p_offset + pp->p_size > base_offset)
12173b89beb1SIan Dowse 			base_offset = pp->p_offset + pp->p_size;
12183b89beb1SIan Dowse 	}
12193b89beb1SIan Dowse 	if (base_offset < lp->d_secperunit)
12203b89beb1SIan Dowse 		free_space += lp->d_secperunit - base_offset;
12213b89beb1SIan Dowse 
12223233afaeSJohn W. De Boskey 	/* handle % partitions - note %'s don't need to add up to 100! */
12233233afaeSJohn W. De Boskey 	if (total_percent != 0) {
12243233afaeSJohn W. De Boskey 		if (total_percent > 100) {
12256b0ff5f5SPoul-Henning Kamp 			fprintf(stderr,"total percentage %lu is greater than 100\n",
12263233afaeSJohn W. De Boskey 			    total_percent);
12273233afaeSJohn W. De Boskey 			errors++;
12283233afaeSJohn W. De Boskey 		}
12293233afaeSJohn W. De Boskey 
12303233afaeSJohn W. De Boskey 		if (free_space > 0) {
12313233afaeSJohn W. De Boskey 			for (i = 0; i < lp->d_npartitions; i++) {
12323233afaeSJohn W. De Boskey 				pp = &lp->d_partitions[i];
12333233afaeSJohn W. De Boskey 				if (part_set[i] && part_size_type[i] == '%') {
12343233afaeSJohn W. De Boskey 					/* careful of overflows! and integer roundoff */
12353233afaeSJohn W. De Boskey 					pp->p_size = ((double)pp->p_size/100) * free_space;
12363233afaeSJohn W. De Boskey 					total_size += pp->p_size;
12373233afaeSJohn W. De Boskey 
12383233afaeSJohn W. De Boskey 					/* FIX we can lose a sector or so due to roundoff per
12393233afaeSJohn W. De Boskey 					   partition.  A more complex algorithm could avoid that */
12403233afaeSJohn W. De Boskey 				}
12413233afaeSJohn W. De Boskey 			}
12423233afaeSJohn W. De Boskey 		} else {
12433233afaeSJohn W. De Boskey 			fprintf(stderr,
12446b0ff5f5SPoul-Henning Kamp 			    "%ld sectors available to give to '*' and '%%' partitions\n",
12453233afaeSJohn W. De Boskey 			    free_space);
12463233afaeSJohn W. De Boskey 			errors++;
12473233afaeSJohn W. De Boskey 			/* fix?  set all % partitions to size 0? */
12483233afaeSJohn W. De Boskey 		}
12493233afaeSJohn W. De Boskey 	}
12503233afaeSJohn W. De Boskey 	/* give anything remaining to the hog partition */
12513233afaeSJohn W. De Boskey 	if (hog_part != -1) {
12523b89beb1SIan Dowse 		/*
12533b89beb1SIan Dowse 		 * Find the range of offsets usable by '*' partitions around
12543b89beb1SIan Dowse 		 * the hog partition and how much space they need.
12553b89beb1SIan Dowse 		 */
12563b89beb1SIan Dowse 		needed = 0;
12573b89beb1SIan Dowse 		base_offset = BBSIZE / secsize;
12583b89beb1SIan Dowse 		for (i = hog_part - 1; i >= 0; i--) {
12593b89beb1SIan Dowse 			pp = &lp->d_partitions[i];
12603b89beb1SIan Dowse 			if (!part_set[i] || i == RAW_PART)
12613b89beb1SIan Dowse 				continue;
12623b89beb1SIan Dowse 			if (part_offset_type[i] == '*') {
12633b89beb1SIan Dowse 				needed += pp->p_size;
12643b89beb1SIan Dowse 				continue;
12653b89beb1SIan Dowse 			}
12663b89beb1SIan Dowse 			base_offset = pp->p_offset + pp->p_size;
12673b89beb1SIan Dowse 			break;
12683b89beb1SIan Dowse 		}
12693b89beb1SIan Dowse 		current_offset = lp->d_secperunit;
12703b89beb1SIan Dowse 		for (i = lp->d_npartitions - 1; i > hog_part; i--) {
12713b89beb1SIan Dowse 			pp = &lp->d_partitions[i];
12723b89beb1SIan Dowse 			if (!part_set[i] || i == RAW_PART)
12733b89beb1SIan Dowse 				continue;
12743b89beb1SIan Dowse 			if (part_offset_type[i] == '*') {
12753b89beb1SIan Dowse 				needed += pp->p_size;
12763b89beb1SIan Dowse 				continue;
12773b89beb1SIan Dowse 			}
12783b89beb1SIan Dowse 			current_offset = pp->p_offset;
12793b89beb1SIan Dowse 		}
12803b89beb1SIan Dowse 
12813b89beb1SIan Dowse 		if (current_offset - base_offset <= needed) {
12823b89beb1SIan Dowse 			fprintf(stderr, "Cannot find space for partition %c\n",
12833b89beb1SIan Dowse 			    hog_part + 'a');
12843b89beb1SIan Dowse 			fprintf(stderr,
12853b89beb1SIan Dowse 			    "Need more than %lu sectors between %lu and %lu\n",
12863b89beb1SIan Dowse 			    needed, base_offset, current_offset);
12873b89beb1SIan Dowse 			errors++;
12883b89beb1SIan Dowse 			lp->d_partitions[hog_part].p_size = 0;
12893b89beb1SIan Dowse 		} else {
12903b89beb1SIan Dowse 			lp->d_partitions[hog_part].p_size = current_offset -
12913b89beb1SIan Dowse 			    base_offset - needed;
12923b89beb1SIan Dowse 			total_size += lp->d_partitions[hog_part].p_size;
12933b89beb1SIan Dowse 		}
12943233afaeSJohn W. De Boskey 	}
12953233afaeSJohn W. De Boskey 
12963233afaeSJohn W. De Boskey 	/* Now set the offsets for each partition */
12973b89beb1SIan Dowse 	current_offset = BBSIZE / secsize; /* in sectors */
12983233afaeSJohn W. De Boskey 	seen_default_offset = 0;
12993233afaeSJohn W. De Boskey 	for (i = 0; i < lp->d_npartitions; i++) {
13003233afaeSJohn W. De Boskey 		part = 'a' + i;
13013233afaeSJohn W. De Boskey 		pp = &lp->d_partitions[i];
13023233afaeSJohn W. De Boskey 		if (part_set[i]) {
13033233afaeSJohn W. De Boskey 			if (part_offset_type[i] == '*') {
13046b0ff5f5SPoul-Henning Kamp 				if (i == RAW_PART) {
13053233afaeSJohn W. De Boskey 					pp->p_offset = 0;
13063233afaeSJohn W. De Boskey 				} else {
13073233afaeSJohn W. De Boskey 					pp->p_offset = current_offset;
13083233afaeSJohn W. De Boskey 					seen_default_offset = 1;
13093233afaeSJohn W. De Boskey 				}
13103233afaeSJohn W. De Boskey 			} else {
13113233afaeSJohn W. De Boskey 				/* allow them to be out of order for old-style tables */
13123233afaeSJohn W. De Boskey 				if (pp->p_offset < current_offset &&
1313f2f63257SGreg Lehey 				    seen_default_offset && i != RAW_PART &&
1314f2f63257SGreg Lehey 				    pp->p_fstype != FS_VINUM) {
13153233afaeSJohn W. De Boskey 					fprintf(stderr,
13166b0ff5f5SPoul-Henning Kamp "Offset %ld for partition %c overlaps previous partition which ends at %lu\n",
13176b0ff5f5SPoul-Henning Kamp 					    (long)pp->p_offset,i+'a',current_offset);
13183233afaeSJohn W. De Boskey 					fprintf(stderr,
13193233afaeSJohn W. De Boskey "Labels with any *'s for offset must be in ascending order by sector\n");
13203233afaeSJohn W. De Boskey 					errors++;
13213233afaeSJohn W. De Boskey 				} else if (pp->p_offset != current_offset &&
13226b0ff5f5SPoul-Henning Kamp 				    i != RAW_PART && seen_default_offset) {
13233233afaeSJohn W. De Boskey 					/*
13243233afaeSJohn W. De Boskey 					 * this may give unneeded warnings if
13253233afaeSJohn W. De Boskey 					 * partitions are out-of-order
13263233afaeSJohn W. De Boskey 					 */
132757dfbec5SPoul-Henning Kamp 					warnx(
13283233afaeSJohn W. De Boskey "Offset %ld for partition %c doesn't match expected value %ld",
13296b0ff5f5SPoul-Henning Kamp 					    (long)pp->p_offset, i + 'a', current_offset);
13303233afaeSJohn W. De Boskey 				}
13313233afaeSJohn W. De Boskey 			}
13326b0ff5f5SPoul-Henning Kamp 			if (i != RAW_PART)
13333233afaeSJohn W. De Boskey 				current_offset = pp->p_offset + pp->p_size;
13343233afaeSJohn W. De Boskey 		}
13353233afaeSJohn W. De Boskey 	}
13363233afaeSJohn W. De Boskey 
13378fae3551SRodney W. Grimes 	for (i = 0; i < lp->d_npartitions; i++) {
13388fae3551SRodney W. Grimes 		part = 'a' + i;
13398fae3551SRodney W. Grimes 		pp = &lp->d_partitions[i];
13408fae3551SRodney W. Grimes 		if (pp->p_size == 0 && pp->p_offset != 0)
134157dfbec5SPoul-Henning Kamp 			warnx("partition %c: size 0, but offset %lu",
13422a1deaaaSBruce Evans 			    part, (u_long)pp->p_offset);
13438fae3551SRodney W. Grimes #ifdef notdef
13448fae3551SRodney W. Grimes 		if (pp->p_size % lp->d_secpercyl)
134557dfbec5SPoul-Henning Kamp 			warnx("partition %c: size %% cylinder-size != 0",
13468fae3551SRodney W. Grimes 			    part);
13478fae3551SRodney W. Grimes 		if (pp->p_offset % lp->d_secpercyl)
134857dfbec5SPoul-Henning Kamp 			warnx("partition %c: offset %% cylinder-size != 0",
13498fae3551SRodney W. Grimes 			    part);
13508fae3551SRodney W. Grimes #endif
13518fae3551SRodney W. Grimes 		if (pp->p_offset > lp->d_secperunit) {
13528fae3551SRodney W. Grimes 			fprintf(stderr,
13538fae3551SRodney W. Grimes 			    "partition %c: offset past end of unit\n", part);
13548fae3551SRodney W. Grimes 			errors++;
13558fae3551SRodney W. Grimes 		}
13568fae3551SRodney W. Grimes 		if (pp->p_offset + pp->p_size > lp->d_secperunit) {
13578fae3551SRodney W. Grimes 			fprintf(stderr,
13588fae3551SRodney W. Grimes 			"partition %c: partition extends past end of unit\n",
13598fae3551SRodney W. Grimes 			    part);
13608fae3551SRodney W. Grimes 			errors++;
13618fae3551SRodney W. Grimes 		}
136257dfbec5SPoul-Henning Kamp 		if (i == RAW_PART) {
13633233afaeSJohn W. De Boskey 			if (pp->p_fstype != FS_UNUSED)
136457dfbec5SPoul-Henning Kamp 				warnx("partition %c is not marked as unused!",part);
13653233afaeSJohn W. De Boskey 			if (pp->p_offset != 0)
136657dfbec5SPoul-Henning Kamp 				warnx("partition %c doesn't start at 0!",part);
13673233afaeSJohn W. De Boskey 			if (pp->p_size != lp->d_secperunit)
136857dfbec5SPoul-Henning Kamp 				warnx("partition %c doesn't cover the whole unit!",part);
13693233afaeSJohn W. De Boskey 
13703233afaeSJohn W. De Boskey 			if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
13713233afaeSJohn W. De Boskey 			    (pp->p_size != lp->d_secperunit)) {
137257dfbec5SPoul-Henning Kamp 				warnx("An incorrect partition %c may cause problems for "
13733233afaeSJohn W. De Boskey 				    "standard system utilities",part);
13743233afaeSJohn W. De Boskey 			}
13753233afaeSJohn W. De Boskey 		}
13763233afaeSJohn W. De Boskey 
13773233afaeSJohn W. De Boskey 		/* check for overlaps */
13783233afaeSJohn W. De Boskey 		/* this will check for all possible overlaps once and only once */
13793233afaeSJohn W. De Boskey 		for (j = 0; j < i; j++) {
13803233afaeSJohn W. De Boskey 			pp2 = &lp->d_partitions[j];
1381f2f63257SGreg Lehey 			if (j != RAW_PART && i != RAW_PART &&
1382f2f63257SGreg Lehey 			    pp->p_fstype != FS_VINUM &&
1383f2f63257SGreg Lehey 			    pp2->p_fstype != FS_VINUM &&
1384f2f63257SGreg Lehey 			    part_set[i] && part_set[j]) {
13853233afaeSJohn W. De Boskey 				if (pp2->p_offset < pp->p_offset + pp->p_size &&
13863233afaeSJohn W. De Boskey 				    (pp2->p_offset + pp2->p_size > pp->p_offset ||
13873233afaeSJohn W. De Boskey 					pp2->p_offset >= pp->p_offset)) {
13883233afaeSJohn W. De Boskey 					fprintf(stderr,"partitions %c and %c overlap!\n",
13893233afaeSJohn W. De Boskey 					    j + 'a', i + 'a');
13903233afaeSJohn W. De Boskey 					errors++;
13913233afaeSJohn W. De Boskey 				}
13923233afaeSJohn W. De Boskey 			}
13933233afaeSJohn W. De Boskey 		}
13948fae3551SRodney W. Grimes 	}
13958fae3551SRodney W. Grimes 	for (; i < MAXPARTITIONS; i++) {
13968fae3551SRodney W. Grimes 		part = 'a' + i;
13978fae3551SRodney W. Grimes 		pp = &lp->d_partitions[i];
13988fae3551SRodney W. Grimes 		if (pp->p_size || pp->p_offset)
139957dfbec5SPoul-Henning Kamp 			warnx("unused partition %c: size %d offset %lu",
14002a1deaaaSBruce Evans 			    'a' + i, pp->p_size, (u_long)pp->p_offset);
14018fae3551SRodney W. Grimes 	}
14028fae3551SRodney W. Grimes 	return (errors);
14038fae3551SRodney W. Grimes }
14048fae3551SRodney W. Grimes 
14058fae3551SRodney W. Grimes /*
1406425bed3aSJoerg Wunsch  * When operating on a "virgin" disk, try getting an initial label
1407425bed3aSJoerg Wunsch  * from the associated device driver.  This might work for all device
1408425bed3aSJoerg Wunsch  * drivers that are able to fetch some initial device parameters
1409425bed3aSJoerg Wunsch  * without even having access to a (BSD) disklabel, like SCSI disks,
1410425bed3aSJoerg Wunsch  * most IDE drives, or vn devices.
1411425bed3aSJoerg Wunsch  *
1412425bed3aSJoerg Wunsch  * The device name must be given in its "canonical" form.
1413425bed3aSJoerg Wunsch  */
1414d2fe97c7SPoul-Henning Kamp static struct disklabel *
1415425bed3aSJoerg Wunsch getvirginlabel(void)
1416425bed3aSJoerg Wunsch {
1417c8223965SMark Murray 	static struct disklabel loclab;
1418b9d05a16SPoul-Henning Kamp 	struct partition *dp;
1419425bed3aSJoerg Wunsch 	int f;
142057dfbec5SPoul-Henning Kamp 	u_int u;
1421425bed3aSJoerg Wunsch 
1422d2fe97c7SPoul-Henning Kamp 	if ((f = open(specname, O_RDONLY)) == -1) {
1423d2fe97c7SPoul-Henning Kamp 		warn("cannot open %s", specname);
142443be698cSBruce Evans 		return (NULL);
1425425bed3aSJoerg Wunsch 	}
1426ff7d5162SJordan K. Hubbard 
14277747c959SLuigi Rizzo 	if (is_file)
14287747c959SLuigi Rizzo 		get_file_parms(f);
14297747c959SLuigi Rizzo 	else if ((ioctl(f, DIOCGMEDIASIZE, &mediasize) != 0) ||
1430b9d05a16SPoul-Henning Kamp 	    (ioctl(f, DIOCGSECTORSIZE, &secsize) != 0)) {
1431425bed3aSJoerg Wunsch 		close (f);
143243be698cSBruce Evans 		return (NULL);
1433425bed3aSJoerg Wunsch 	}
1434b9d05a16SPoul-Henning Kamp 	memset(&loclab, 0, sizeof loclab);
1435b9d05a16SPoul-Henning Kamp 	loclab.d_magic = DISKMAGIC;
1436b9d05a16SPoul-Henning Kamp 	loclab.d_magic2 = DISKMAGIC;
1437b9d05a16SPoul-Henning Kamp 	loclab.d_secsize = secsize;
1438b9d05a16SPoul-Henning Kamp 	loclab.d_secperunit = mediasize / secsize;
1439b9d05a16SPoul-Henning Kamp 
1440b9d05a16SPoul-Henning Kamp 	/*
1441b9d05a16SPoul-Henning Kamp 	 * Nobody in these enligthened days uses the CHS geometry for
1442b9d05a16SPoul-Henning Kamp 	 * anything, but nontheless try to get it right.  If we fail
1443b9d05a16SPoul-Henning Kamp 	 * to get any good ideas from the device, construct something
1444b9d05a16SPoul-Henning Kamp 	 * which is IBM-PC friendly.
1445b9d05a16SPoul-Henning Kamp 	 */
1446b9d05a16SPoul-Henning Kamp 	if (ioctl(f, DIOCGFWSECTORS, &u) == 0)
1447b9d05a16SPoul-Henning Kamp 		loclab.d_nsectors = u;
1448b9d05a16SPoul-Henning Kamp 	else
1449b9d05a16SPoul-Henning Kamp 		loclab.d_nsectors = 63;
1450b9d05a16SPoul-Henning Kamp 	if (ioctl(f, DIOCGFWHEADS, &u) == 0)
1451b9d05a16SPoul-Henning Kamp 		loclab.d_ntracks = u;
1452b9d05a16SPoul-Henning Kamp 	else if (loclab.d_secperunit <= 63*1*1024)
1453b9d05a16SPoul-Henning Kamp 		loclab.d_ntracks = 1;
1454b9d05a16SPoul-Henning Kamp 	else if (loclab.d_secperunit <= 63*16*1024)
1455b9d05a16SPoul-Henning Kamp 		loclab.d_ntracks = 16;
1456b9d05a16SPoul-Henning Kamp 	else
1457b9d05a16SPoul-Henning Kamp 		loclab.d_ntracks = 255;
1458b9d05a16SPoul-Henning Kamp 	loclab.d_secpercyl = loclab.d_ntracks * loclab.d_nsectors;
1459b9d05a16SPoul-Henning Kamp 	loclab.d_ncylinders = loclab.d_secperunit / loclab.d_secpercyl;
1460b9d05a16SPoul-Henning Kamp 	loclab.d_npartitions = MAXPARTITIONS;
1461b9d05a16SPoul-Henning Kamp 
1462b9d05a16SPoul-Henning Kamp 	/* Various (unneeded) compat stuff */
1463b9d05a16SPoul-Henning Kamp 	loclab.d_rpm = 3600;
1464b9d05a16SPoul-Henning Kamp 	loclab.d_bbsize = BBSIZE;
146557dfbec5SPoul-Henning Kamp 	loclab.d_interleave = 1;
1466b9d05a16SPoul-Henning Kamp 	strncpy(loclab.d_typename, "amnesiac",
1467b9d05a16SPoul-Henning Kamp 	    sizeof(loclab.d_typename));
1468b9d05a16SPoul-Henning Kamp 
1469b9d05a16SPoul-Henning Kamp 	dp = &loclab.d_partitions[RAW_PART];
1470b9d05a16SPoul-Henning Kamp 	dp->p_size = loclab.d_secperunit;
1471b9d05a16SPoul-Henning Kamp 	loclab.d_checksum = dkcksum(&loclab);
1472425bed3aSJoerg Wunsch 	close (f);
1473c8223965SMark Murray 	return (&loclab);
1474425bed3aSJoerg Wunsch }
1475425bed3aSJoerg Wunsch 
1476d2fe97c7SPoul-Henning Kamp static void
1477326c7cdaSWarner Losh usage(void)
14788fae3551SRodney W. Grimes {
1479bc33ea1aSRuslan Ermilov 
1480bc33ea1aSRuslan Ermilov 	fprintf(stderr,
1481bc33ea1aSRuslan 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",
14825daa806dSPoul-Henning Kamp 	"usage: bsdlabel disk",
1483bef2080aSPhilippe Charnier 	"\t\t(to read label)",
1484d2fe97c7SPoul-Henning Kamp 	"	bsdlabel -w [-n] [-m machine] disk [type]",
1485bef2080aSPhilippe Charnier 	"\t\t(to write label with existing boot program)",
14865daa806dSPoul-Henning Kamp 	"	bsdlabel -e [-n] [-m machine] disk",
1487bef2080aSPhilippe Charnier 	"\t\t(to edit label)",
14885daa806dSPoul-Henning Kamp 	"	bsdlabel -R [-n] [-m machine] disk protofile",
1489bef2080aSPhilippe Charnier 	"\t\t(to restore label with existing boot program)",
14902c60b668SPoul-Henning Kamp 	"	bsdlabel -B [-b boot] [-m machine] disk",
1491bef2080aSPhilippe Charnier 	"\t\t(to install boot program with existing on-disk label)",
1492d2fe97c7SPoul-Henning Kamp 	"	bsdlabel -w -B [-n] [-b boot] [-m machine] disk [type]",
1493bef2080aSPhilippe Charnier 	"\t\t(to write label and install boot program)",
14942c60b668SPoul-Henning Kamp 	"	bsdlabel -R -B [-n] [-b boot] [-m machine] disk protofile",
149580baf8ceSPoul-Henning Kamp 		"\t\t(to restore label and install boot program)"
149680baf8ceSPoul-Henning Kamp 	);
14978fae3551SRodney W. Grimes 	exit(1);
14988fae3551SRodney W. Grimes }
1499