xref: /freebsd/sbin/bsdlabel/bsdlabel.c (revision fb6a35935ae8d2a2b6d5023979bc8f997bf54efa)
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
661fd45acdSAndrey V. Elsukov #define MAXPARTITIONS	20
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 
82d2fe97c7SPoul-Henning Kamp static void	makelabel(const char *, struct disklabel *);
83fd4bbf84SAndrey V. Elsukov static int	geom_class_available(const char *);
84d2fe97c7SPoul-Henning Kamp static int	writelabel(void);
85d2fe97c7SPoul-Henning Kamp static int	readlabel(int flag);
86d2fe97c7SPoul-Henning Kamp static void	display(FILE *, const struct disklabel *);
87d2fe97c7SPoul-Henning Kamp static int	edit(void);
88d2fe97c7SPoul-Henning Kamp static int	editit(void);
89427823d5SPoul-Henning Kamp static void	fixlabel(struct disklabel *);
90d2fe97c7SPoul-Henning Kamp static char	*skip(char *);
91d2fe97c7SPoul-Henning Kamp static char	*word(char *);
92d2fe97c7SPoul-Henning Kamp static int	getasciilabel(FILE *, struct disklabel *);
93d2fe97c7SPoul-Henning Kamp static int	getasciipartspec(char *, struct disklabel *, int, int);
94d2fe97c7SPoul-Henning Kamp static int	checklabel(struct disklabel *);
95d2fe97c7SPoul-Henning Kamp static void	usage(void);
96d2fe97c7SPoul-Henning Kamp static struct disklabel *getvirginlabel(void);
9761de51caSJoerg Wunsch 
988fae3551SRodney W. Grimes #define	DEFEDITOR	_PATH_VI
99d5718812SMarcel Moolenaar #define	DEFPARTITIONS	8
1008fae3551SRodney W. Grimes 
101d2fe97c7SPoul-Henning Kamp static char	*specname;
1027a4b0bb2SUlf Lilleengen static char	*pname;
103d2fe97c7SPoul-Henning Kamp static char	tmpfil[] = PATH_TMPFILE;
1048fae3551SRodney W. Grimes 
105d2fe97c7SPoul-Henning Kamp static struct	disklabel lab;
106d2fe97c7SPoul-Henning Kamp static u_char	bootarea[BBSIZE];
10757dfbec5SPoul-Henning Kamp static off_t	mediasize;
108a5871155SUlrich Spörlein static ssize_t	secsize;
109d2fe97c7SPoul-Henning Kamp static char	blank[] = "";
110d2fe97c7SPoul-Henning Kamp static char	unknown[] = "unknown";
1118fae3551SRodney W. Grimes 
1123233afaeSJohn W. De Boskey #define MAX_PART ('z')
1133233afaeSJohn W. De Boskey #define MAX_NUM_PARTS (1 + MAX_PART - 'a')
114d2fe97c7SPoul-Henning Kamp static char    part_size_type[MAX_NUM_PARTS];
115d2fe97c7SPoul-Henning Kamp static char    part_offset_type[MAX_NUM_PARTS];
116d2fe97c7SPoul-Henning Kamp static int     part_set[MAX_NUM_PARTS];
1173233afaeSJohn W. De Boskey 
118d2fe97c7SPoul-Henning Kamp static int	installboot;	/* non-zero if we should install a boot program */
11957dfbec5SPoul-Henning Kamp static int	allfields;	/* present all fields in edit */
120d2fe97c7SPoul-Henning Kamp static char const *xxboot;	/* primary boot */
1218fae3551SRodney W. Grimes 
122eb0ea23eSMarcel Moolenaar static uint32_t lba_offset;
1239ef521ecSPoul-Henning Kamp #ifndef LABELSECTOR
1249ef521ecSPoul-Henning Kamp #define LABELSECTOR -1
1259ef521ecSPoul-Henning Kamp #endif
1269ef521ecSPoul-Henning Kamp #ifndef LABELOFFSET
1279ef521ecSPoul-Henning Kamp #define LABELOFFSET -1
1289ef521ecSPoul-Henning Kamp #endif
129b2bb9f9bSPoul-Henning Kamp static int labelsoffset = LABELSECTOR;
130b2bb9f9bSPoul-Henning Kamp static int labeloffset = LABELOFFSET;
1317838fd0eSPoul-Henning Kamp static int bbsize = BBSIZE;
132c80f9755SPoul-Henning Kamp 
1331d23e44cSEd Schouten static enum {
134f080d33bSPoul-Henning Kamp 	UNSPEC, EDIT, READ, RESTORE, WRITE, WRITEBOOT
1358fae3551SRodney W. Grimes } op = UNSPEC;
1368fae3551SRodney W. Grimes 
137bc33ea1aSRuslan Ermilov 
138d2fe97c7SPoul-Henning Kamp static int	disable_write;   /* set to disable writing to disk label */
1397747c959SLuigi Rizzo static int	is_file;	/* work on a file (abs. pathname), "-f" opt. */
1408fae3551SRodney W. Grimes 
14161de51caSJoerg Wunsch int
142326c7cdaSWarner Losh main(int argc, char *argv[])
1438fae3551SRodney W. Grimes {
1448fae3551SRodney W. Grimes 	FILE *t;
1457a4b0bb2SUlf Lilleengen 	int ch, error, fd;
1467a4b0bb2SUlf Lilleengen 	const char *name;
1477a4b0bb2SUlf Lilleengen 
1487a4b0bb2SUlf Lilleengen 	error = 0;
1497a4b0bb2SUlf Lilleengen 	name = NULL;
1508fae3551SRodney W. Grimes 
151f72bbf97SMaxim Konovalov 	while ((ch = getopt(argc, argv, "ABb:efm:nRrw")) != -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, "pc98")) {
169b2bb9f9bSPoul-Henning Kamp 					labelsoffset = 1;
170b2bb9f9bSPoul-Henning Kamp 					labeloffset = 0;
1713ecb3802SPoul-Henning Kamp 					bbsize = 8192;
172d2fe97c7SPoul-Henning Kamp 				} else {
173d2fe97c7SPoul-Henning Kamp 					errx(1, "Unsupported architecture");
174c80f9755SPoul-Henning Kamp 				}
175bc33ea1aSRuslan Ermilov 				break;
1763233afaeSJohn W. De Boskey 			case 'n':
1773233afaeSJohn W. De Boskey 				disable_write = 1;
1783233afaeSJohn W. De Boskey 				break;
1798fae3551SRodney W. Grimes 			case 'R':
1808fae3551SRodney W. Grimes 				if (op != UNSPEC)
1818fae3551SRodney W. Grimes 					usage();
1828fae3551SRodney W. Grimes 				op = RESTORE;
1838fae3551SRodney W. Grimes 				break;
1848fae3551SRodney W. Grimes 			case 'e':
1858fae3551SRodney W. Grimes 				if (op != UNSPEC)
1868fae3551SRodney W. Grimes 					usage();
1878fae3551SRodney W. Grimes 				op = EDIT;
1888fae3551SRodney W. Grimes 				break;
1898fae3551SRodney W. Grimes 			case 'r':
190d2fe97c7SPoul-Henning Kamp 				/*
1914b85a12fSUlrich Spörlein 				 * We accept and ignore -r for compatibility with
1924b85a12fSUlrich Spörlein 				 * historical disklabel usage.
193d2fe97c7SPoul-Henning Kamp 				 */
1948fae3551SRodney W. Grimes 				break;
1958fae3551SRodney W. Grimes 			case 'w':
1968fae3551SRodney W. Grimes 				if (op != UNSPEC)
1978fae3551SRodney W. Grimes 					usage();
1988fae3551SRodney W. Grimes 				op = WRITE;
1998fae3551SRodney W. Grimes 				break;
2008fae3551SRodney W. Grimes 			case '?':
2018fae3551SRodney W. Grimes 			default:
2028fae3551SRodney W. Grimes 				usage();
2038fae3551SRodney W. Grimes 		}
2048fae3551SRodney W. Grimes 	argc -= optind;
2058fae3551SRodney W. Grimes 	argv += optind;
206d2fe97c7SPoul-Henning Kamp 
2078fae3551SRodney W. Grimes 	if (argc < 1)
2088fae3551SRodney W. Grimes 		usage();
2099ef521ecSPoul-Henning Kamp 	if (labelsoffset < 0 || labeloffset < 0)
2109ef521ecSPoul-Henning Kamp 		errx(1, "a -m <architecture> option must be specified");
2118fae3551SRodney W. Grimes 
2125daa806dSPoul-Henning Kamp 	/* Figure out the names of the thing we're working on */
2137747c959SLuigi Rizzo 	if (is_file) {
214ccdd2fceSRalf S. Engelschall 		specname = argv[0];
2155daa806dSPoul-Henning Kamp 	} else {
2167a4b0bb2SUlf Lilleengen 		specname = g_device_path(argv[0]);
2177a4b0bb2SUlf Lilleengen 		if (specname == NULL) {
2187a4b0bb2SUlf Lilleengen 			warn("unable to get correct path for %s", argv[0]);
2197a4b0bb2SUlf Lilleengen 			return(1);
2207a4b0bb2SUlf Lilleengen 		}
2217a4b0bb2SUlf Lilleengen 		fd = open(specname, O_RDONLY);
2227a4b0bb2SUlf Lilleengen 		if (fd < 0) {
2237a4b0bb2SUlf Lilleengen 			warn("error opening %s", specname);
2247a4b0bb2SUlf Lilleengen 			return(1);
2257a4b0bb2SUlf Lilleengen 		}
2267a4b0bb2SUlf Lilleengen 		pname = g_providername(fd);
2277a4b0bb2SUlf Lilleengen 		if (pname == NULL) {
228bd5add58SUlf Lilleengen 			warn("error getting providername for %s", specname);
2299d8ce078SUlf Lilleengen 			close(fd);
2307a4b0bb2SUlf Lilleengen 			return(1);
2317a4b0bb2SUlf Lilleengen 		}
2329d8ce078SUlf Lilleengen 		close(fd);
2338fae3551SRodney W. Grimes 	}
234d2fe97c7SPoul-Henning Kamp 
235d2fe97c7SPoul-Henning Kamp 	if (installboot && op == UNSPEC)
236d2fe97c7SPoul-Henning Kamp 		op = WRITEBOOT;
237d2fe97c7SPoul-Henning Kamp 	else if (op == UNSPEC)
238d2fe97c7SPoul-Henning Kamp 		op = READ;
2398fae3551SRodney W. Grimes 
2408fae3551SRodney W. Grimes 	switch(op) {
2418fae3551SRodney W. Grimes 
2423b3038a6SBill Fumerola 	case UNSPEC:
2433b3038a6SBill Fumerola 		break;
2443b3038a6SBill Fumerola 
2458fae3551SRodney W. Grimes 	case EDIT:
2468fae3551SRodney W. Grimes 		if (argc != 1)
2478fae3551SRodney W. Grimes 			usage();
248d2fe97c7SPoul-Henning Kamp 		readlabel(1);
249427823d5SPoul-Henning Kamp 		fixlabel(&lab);
250d2fe97c7SPoul-Henning Kamp 		error = edit();
2518fae3551SRodney W. Grimes 		break;
2528fae3551SRodney W. Grimes 
2538fae3551SRodney W. Grimes 	case READ:
2548fae3551SRodney W. Grimes 		if (argc != 1)
2558fae3551SRodney W. Grimes 			usage();
256d2fe97c7SPoul-Henning Kamp 		readlabel(1);
257d2fe97c7SPoul-Henning Kamp 		display(stdout, NULL);
258d2fe97c7SPoul-Henning Kamp 		error = checklabel(NULL);
2598fae3551SRodney W. Grimes 		break;
2608fae3551SRodney W. Grimes 
2618fae3551SRodney W. Grimes 	case RESTORE:
2628fae3551SRodney W. Grimes 		if (argc != 2)
2638fae3551SRodney W. Grimes 			usage();
2648fae3551SRodney W. Grimes 		if (!(t = fopen(argv[1], "r")))
2655daa806dSPoul-Henning Kamp 			err(4, "fopen %s", argv[1]);
266d2fe97c7SPoul-Henning Kamp 		readlabel(0);
2676cabb348SBruce Evans 		if (!getasciilabel(t, &lab))
2686cabb348SBruce Evans 			exit(1);
269d2fe97c7SPoul-Henning Kamp 		error = writelabel();
2708fae3551SRodney W. Grimes 		break;
2718fae3551SRodney W. Grimes 
2728fae3551SRodney W. Grimes 	case WRITE:
273d2fe97c7SPoul-Henning Kamp 		if (argc == 2)
274d2fe97c7SPoul-Henning Kamp 			name = argv[1];
275d2fe97c7SPoul-Henning Kamp 		else if (argc == 1)
276d2fe97c7SPoul-Henning Kamp 			name = "auto";
277d2fe97c7SPoul-Henning Kamp 		else
2788fae3551SRodney W. Grimes 			usage();
279d2fe97c7SPoul-Henning Kamp 		readlabel(0);
280d2fe97c7SPoul-Henning Kamp 		makelabel(name, &lab);
281427823d5SPoul-Henning Kamp 		fixlabel(&lab);
282d2fe97c7SPoul-Henning Kamp 		if (checklabel(NULL) == 0)
283d2fe97c7SPoul-Henning Kamp 			error = writelabel();
2848fae3551SRodney W. Grimes 		break;
2858fae3551SRodney W. Grimes 
2868fae3551SRodney W. Grimes 	case WRITEBOOT:
2878fae3551SRodney W. Grimes 
288d2fe97c7SPoul-Henning Kamp 		readlabel(1);
289427823d5SPoul-Henning Kamp 		fixlabel(&lab);
2908fae3551SRodney W. Grimes 		if (argc == 2)
291d2fe97c7SPoul-Henning Kamp 			makelabel(argv[1], &lab);
292d2fe97c7SPoul-Henning Kamp 		if (checklabel(NULL) == 0)
293d2fe97c7SPoul-Henning Kamp 			error = writelabel();
2948fae3551SRodney W. Grimes 		break;
2958fae3551SRodney W. Grimes 	}
2968fae3551SRodney W. Grimes 	exit(error);
2978fae3551SRodney W. Grimes }
2988fae3551SRodney W. Grimes 
299427823d5SPoul-Henning Kamp static void
300427823d5SPoul-Henning Kamp fixlabel(struct disklabel *lp)
301427823d5SPoul-Henning Kamp {
302427823d5SPoul-Henning Kamp 	struct partition *dp;
303427823d5SPoul-Henning Kamp 	int i;
304427823d5SPoul-Henning Kamp 
305d5718812SMarcel Moolenaar 	for (i = 0; i < lp->d_npartitions; i++) {
306427823d5SPoul-Henning Kamp 		if (i == RAW_PART)
307427823d5SPoul-Henning Kamp 			continue;
308427823d5SPoul-Henning Kamp 		if (lp->d_partitions[i].p_size)
309427823d5SPoul-Henning Kamp 			return;
310427823d5SPoul-Henning Kamp 	}
311427823d5SPoul-Henning Kamp 
312427823d5SPoul-Henning Kamp 	dp = &lp->d_partitions[0];
313427823d5SPoul-Henning Kamp 	dp->p_offset = BBSIZE / secsize;
314427823d5SPoul-Henning Kamp 	dp->p_size = lp->d_secperunit - dp->p_offset;
315427823d5SPoul-Henning Kamp }
316427823d5SPoul-Henning Kamp 
3178fae3551SRodney W. Grimes /*
318ef9ab0b3SRuslan Ermilov  * Construct a prototype disklabel from /etc/disktab.
3198fae3551SRodney W. Grimes  */
320d2fe97c7SPoul-Henning Kamp static void
321d2fe97c7SPoul-Henning Kamp makelabel(const char *type, struct disklabel *lp)
3228fae3551SRodney W. Grimes {
323326c7cdaSWarner Losh 	struct disklabel *dp;
324425bed3aSJoerg Wunsch 
325425bed3aSJoerg Wunsch 	if (strcmp(type, "auto") == 0)
326425bed3aSJoerg Wunsch 		dp = getvirginlabel();
327425bed3aSJoerg Wunsch 	else
3288fae3551SRodney W. Grimes 		dp = getdiskbyname(type);
3296bd343a9SPhilippe Charnier 	if (dp == NULL)
3306bd343a9SPhilippe Charnier 		errx(1, "%s: unknown disk type", type);
3318fae3551SRodney W. Grimes 	*lp = *dp;
3328fae3551SRodney W. Grimes 	bzero(lp->d_packname, sizeof(lp->d_packname));
3338fae3551SRodney W. Grimes }
3348fae3551SRodney W. Grimes 
335d2fe97c7SPoul-Henning Kamp static void
336d2fe97c7SPoul-Henning Kamp readboot(void)
337d2fe97c7SPoul-Henning Kamp {
338049a9793SJaakko Heinonen 	int fd;
339d2fe97c7SPoul-Henning Kamp 	struct stat st;
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);
3474c8dfc4aSUlrich Spörlein 	if (st.st_size <= BBSIZE) {
348049a9793SJaakko Heinonen 		if (read(fd, bootarea, st.st_size) != st.st_size)
349d2fe97c7SPoul-Henning Kamp 			err(1, "read error %s", xxboot);
350a2299ad8SKevin Lo 		close(fd);
351d2fe97c7SPoul-Henning Kamp 		return;
352d2fe97c7SPoul-Henning Kamp 	}
353d2fe97c7SPoul-Henning Kamp 	errx(1, "boot code %s is wrong size", xxboot);
354d2fe97c7SPoul-Henning Kamp }
355d2fe97c7SPoul-Henning Kamp 
356d2fe97c7SPoul-Henning Kamp static int
357fd4bbf84SAndrey V. Elsukov geom_class_available(const char *name)
358e45d37b2SJaakko Heinonen {
359e45d37b2SJaakko Heinonen 	struct gclass *class;
360e45d37b2SJaakko Heinonen 	struct gmesh mesh;
361e45d37b2SJaakko Heinonen 	int error;
362e45d37b2SJaakko Heinonen 
363e45d37b2SJaakko Heinonen 	error = geom_gettree(&mesh);
364e45d37b2SJaakko Heinonen 	if (error != 0)
365e45d37b2SJaakko Heinonen 		errc(1, error, "Cannot get GEOM tree");
366e45d37b2SJaakko Heinonen 
367e45d37b2SJaakko Heinonen 	LIST_FOREACH(class, &mesh.lg_class, lg_class) {
368fd4bbf84SAndrey V. Elsukov 		if (strcmp(class->lg_name, name) == 0) {
369e45d37b2SJaakko Heinonen 			geom_deletetree(&mesh);
370e45d37b2SJaakko Heinonen 			return (1);
371e45d37b2SJaakko Heinonen 		}
372e45d37b2SJaakko Heinonen 	}
373e45d37b2SJaakko Heinonen 
374e45d37b2SJaakko Heinonen 	geom_deletetree(&mesh);
375e45d37b2SJaakko Heinonen 
376e45d37b2SJaakko Heinonen 	return (0);
377e45d37b2SJaakko Heinonen }
378e45d37b2SJaakko Heinonen 
379e45d37b2SJaakko Heinonen static int
380d2fe97c7SPoul-Henning Kamp writelabel(void)
3818fae3551SRodney W. Grimes {
382e45d37b2SJaakko Heinonen 	int i, fd, serrno;
3835daa806dSPoul-Henning Kamp 	struct gctl_req *grq;
3845daa806dSPoul-Henning Kamp 	char const *errstr;
385d2fe97c7SPoul-Henning Kamp 	struct disklabel *lp = &lab;
386130cd73aSDoug Rabson 
3873233afaeSJohn W. De Boskey 	if (disable_write) {
3884b85a12fSUlrich Spörlein 		warnx("write to disk label suppressed - label was as follows:");
389d2fe97c7SPoul-Henning Kamp 		display(stdout, NULL);
3903233afaeSJohn W. De Boskey 		return (0);
39180baf8ceSPoul-Henning Kamp 	}
39280baf8ceSPoul-Henning Kamp 
3938fae3551SRodney W. Grimes 	lp->d_magic = DISKMAGIC;
3948fae3551SRodney W. Grimes 	lp->d_magic2 = DISKMAGIC;
3958fae3551SRodney W. Grimes 	lp->d_checksum = 0;
3968fae3551SRodney W. Grimes 	lp->d_checksum = dkcksum(lp);
397d2fe97c7SPoul-Henning Kamp 	if (installboot)
398d2fe97c7SPoul-Henning Kamp 		readboot();
3995d853216SPoul-Henning Kamp 	for (i = 0; i < lab.d_npartitions; i++)
4005d853216SPoul-Henning Kamp 		if (lab.d_partitions[i].p_size)
401eb0ea23eSMarcel Moolenaar 			lab.d_partitions[i].p_offset += lba_offset;
4023fd209e4SMaxim Sobolev 	bsd_disklabel_le_enc(bootarea + labeloffset + labelsoffset * lab.d_secsize,
403b2bb9f9bSPoul-Henning Kamp 	    lp);
4045daa806dSPoul-Henning Kamp 
4055daa806dSPoul-Henning Kamp 	fd = open(specname, O_RDWR);
4065daa806dSPoul-Henning Kamp 	if (fd < 0) {
4077747c959SLuigi Rizzo 		if (is_file) {
4087747c959SLuigi Rizzo 			warn("cannot open file %s for writing label", specname);
4097747c959SLuigi Rizzo 			return(1);
410e45d37b2SJaakko Heinonen 		} else
411e45d37b2SJaakko Heinonen 			serrno = errno;
412e45d37b2SJaakko Heinonen 
413fd4bbf84SAndrey V. Elsukov 		if (geom_class_available("PART") != 0) {
414b06ce685SAndrey V. Elsukov 			/*
415b06ce685SAndrey V. Elsukov 			 * Since we weren't able open provider for
416fd4bbf84SAndrey V. Elsukov 			 * writing, then recommend user to use gpart(8).
417fd4bbf84SAndrey V. Elsukov 			 */
418fd4bbf84SAndrey V. Elsukov 			warnc(serrno,
419fd4bbf84SAndrey V. Elsukov 			    "cannot open provider %s for writing label",
420fd4bbf84SAndrey V. Elsukov 			    specname);
421fd4bbf84SAndrey V. Elsukov 			warnx("Try to use gpart(8).");
422fd4bbf84SAndrey V. Elsukov 			return (1);
423fd4bbf84SAndrey V. Elsukov 		}
424fd4bbf84SAndrey V. Elsukov 
425e45d37b2SJaakko Heinonen 		/* Give up if GEOM_BSD is not available. */
426fd4bbf84SAndrey V. Elsukov 		if (geom_class_available("BSD") == 0) {
427e45d37b2SJaakko Heinonen 			warnc(serrno, "%s", specname);
428e45d37b2SJaakko Heinonen 			return (1);
4297747c959SLuigi Rizzo 		}
4307a4b0bb2SUlf Lilleengen 
43183d771deSPoul-Henning Kamp 		grq = gctl_get_handle();
43283d771deSPoul-Henning Kamp 		gctl_ro_param(grq, "verb", -1, "write label");
4335daa806dSPoul-Henning Kamp 		gctl_ro_param(grq, "class", -1, "BSD");
4347a4b0bb2SUlf Lilleengen 		gctl_ro_param(grq, "geom", -1, pname);
435b2bb9f9bSPoul-Henning Kamp 		gctl_ro_param(grq, "label", 148+16*8,
4363fd209e4SMaxim Sobolev 			bootarea + labeloffset + labelsoffset * lab.d_secsize);
4375daa806dSPoul-Henning Kamp 		errstr = gctl_issue(grq);
438d2fe97c7SPoul-Henning Kamp 		if (errstr != NULL) {
439d2fe97c7SPoul-Henning Kamp 			warnx("%s", errstr);
440d2fe97c7SPoul-Henning Kamp 			gctl_free(grq);
441d2fe97c7SPoul-Henning Kamp 			return(1);
442d2fe97c7SPoul-Henning Kamp 		}
4435daa806dSPoul-Henning Kamp 		gctl_free(grq);
4445daa806dSPoul-Henning Kamp 		if (installboot) {
44583d771deSPoul-Henning Kamp 			grq = gctl_get_handle();
44683d771deSPoul-Henning Kamp 			gctl_ro_param(grq, "verb", -1, "write bootcode");
4475daa806dSPoul-Henning Kamp 			gctl_ro_param(grq, "class", -1, "BSD");
4487a4b0bb2SUlf Lilleengen 			gctl_ro_param(grq, "geom", -1, pname);
449d2fe97c7SPoul-Henning Kamp 			gctl_ro_param(grq, "bootcode", BBSIZE, bootarea);
4505daa806dSPoul-Henning Kamp 			errstr = gctl_issue(grq);
451d2fe97c7SPoul-Henning Kamp 			if (errstr != NULL) {
452d2fe97c7SPoul-Henning Kamp 				warnx("%s", errstr);
453d2fe97c7SPoul-Henning Kamp 				gctl_free(grq);
454d2fe97c7SPoul-Henning Kamp 				return (1);
455d2fe97c7SPoul-Henning Kamp 			}
4565daa806dSPoul-Henning Kamp 			gctl_free(grq);
4575daa806dSPoul-Henning Kamp 		}
4585daa806dSPoul-Henning Kamp 	} else {
459d2fe97c7SPoul-Henning Kamp 		if (write(fd, bootarea, bbsize) != bbsize) {
4605daa806dSPoul-Henning Kamp 			warn("write %s", specname);
4615daa806dSPoul-Henning Kamp 			close (fd);
4628fae3551SRodney W. Grimes 			return (1);
4638fae3551SRodney W. Grimes 		}
4645daa806dSPoul-Henning Kamp 		close (fd);
4655daa806dSPoul-Henning Kamp 	}
4668fae3551SRodney W. Grimes 	return (0);
4678fae3551SRodney W. Grimes }
4688fae3551SRodney W. Grimes 
4697747c959SLuigi Rizzo static void
4707747c959SLuigi Rizzo get_file_parms(int f)
4717747c959SLuigi Rizzo {
4727747c959SLuigi Rizzo 	int i;
4737747c959SLuigi Rizzo 	struct stat sb;
4747747c959SLuigi Rizzo 
4757747c959SLuigi Rizzo 	if (fstat(f, &sb) != 0)
4767747c959SLuigi Rizzo 		err(4, "fstat failed");
4777747c959SLuigi Rizzo 	i = sb.st_mode & S_IFMT;
4787747c959SLuigi Rizzo 	if (i != S_IFREG && i != S_IFLNK)
4797747c959SLuigi Rizzo 		errx(4, "%s is not a valid file or link", specname);
4807747c959SLuigi Rizzo 	secsize = DEV_BSIZE;
4817747c959SLuigi Rizzo 	mediasize = sb.st_size;
4827747c959SLuigi Rizzo }
4837747c959SLuigi Rizzo 
4848fae3551SRodney W. Grimes /*
4858fae3551SRodney W. Grimes  * Fetch disklabel for disk.
4868fae3551SRodney W. Grimes  */
487d2fe97c7SPoul-Henning Kamp static int
488d2fe97c7SPoul-Henning Kamp readlabel(int flag)
4898fae3551SRodney W. Grimes {
490049a9793SJaakko Heinonen 	ssize_t nbytes;
491eb0ea23eSMarcel Moolenaar 	uint32_t lba;
4925d853216SPoul-Henning Kamp 	int f, i;
4935daa806dSPoul-Henning Kamp 	int error;
4948fae3551SRodney W. Grimes 
4955daa806dSPoul-Henning Kamp 	f = open(specname, O_RDONLY);
4965daa806dSPoul-Henning Kamp 	if (f < 0)
4971161d420SRebecca Cran 		err(1, "%s", specname);
4987747c959SLuigi Rizzo 	if (is_file)
4997747c959SLuigi Rizzo 		get_file_parms(f);
5007a4b0bb2SUlf Lilleengen 	else {
5017a4b0bb2SUlf Lilleengen 		mediasize = g_mediasize(f);
5027a4b0bb2SUlf Lilleengen 		secsize = g_sectorsize(f);
5037a4b0bb2SUlf Lilleengen 		if (secsize < 0 || mediasize < 0)
504b2bb9f9bSPoul-Henning Kamp 			err(4, "cannot get disk geometry");
505b2bb9f9bSPoul-Henning Kamp 	}
50686e0bd0dSBrooks Davis 	if (mediasize > (off_t)0xffffffff * secsize)
50786e0bd0dSBrooks Davis 		errx(1,
50886e0bd0dSBrooks Davis 		    "disks with more than 2^32-1 sectors are not supported");
509c80f9755SPoul-Henning Kamp 	(void)lseek(f, (off_t)0, SEEK_SET);
510049a9793SJaakko Heinonen 	nbytes = read(f, bootarea, BBSIZE);
511049a9793SJaakko Heinonen 	if (nbytes == -1)
5125daa806dSPoul-Henning Kamp 		err(4, "%s read", specname);
513049a9793SJaakko Heinonen 	if (nbytes != BBSIZE)
514049a9793SJaakko Heinonen 		errx(4, "couldn't read %d bytes from %s", BBSIZE, specname);
5155daa806dSPoul-Henning Kamp 	close (f);
516b2bb9f9bSPoul-Henning Kamp 	error = bsd_disklabel_le_dec(
517b2bb9f9bSPoul-Henning Kamp 	    bootarea + (labeloffset + labelsoffset * secsize),
518b2bb9f9bSPoul-Henning Kamp 	    &lab, MAXPARTITIONS);
519d2fe97c7SPoul-Henning Kamp 	if (flag && error)
520d2fe97c7SPoul-Henning Kamp 		errx(1, "%s: no valid label found", specname);
5215d853216SPoul-Henning Kamp 
5227a4b0bb2SUlf Lilleengen 	if (is_file)
5237a4b0bb2SUlf Lilleengen 		return(0);
524eb0ea23eSMarcel Moolenaar 
525eb0ea23eSMarcel Moolenaar 	/*
526eb0ea23eSMarcel Moolenaar 	 * Compensate for absolute block addressing by finding the
527eb0ea23eSMarcel Moolenaar 	 * smallest partition offset and if the offset of the 'c'
528eb0ea23eSMarcel Moolenaar 	 * partition is equal to that, subtract it from all offsets.
529eb0ea23eSMarcel Moolenaar 	 */
530eb0ea23eSMarcel Moolenaar 	lba = ~0;
531eb0ea23eSMarcel Moolenaar 	for (i = 0; i < lab.d_npartitions; i++) {
5325d853216SPoul-Henning Kamp 		if (lab.d_partitions[i].p_size)
533eb0ea23eSMarcel Moolenaar 			lba = MIN(lba, lab.d_partitions[i].p_offset);
534eb0ea23eSMarcel Moolenaar 	}
535eb0ea23eSMarcel Moolenaar 	if (lba != 0 && lab.d_partitions[RAW_PART].p_offset == lba) {
536eb0ea23eSMarcel Moolenaar 		for (i = 0; i < lab.d_npartitions; i++) {
537eb0ea23eSMarcel Moolenaar 			if (lab.d_partitions[i].p_size)
538eb0ea23eSMarcel Moolenaar 				lab.d_partitions[i].p_offset -= lba;
539eb0ea23eSMarcel Moolenaar 		}
540eb0ea23eSMarcel Moolenaar 		/*
541eb0ea23eSMarcel Moolenaar 		 * Save the offset so that we can write the label
542eb0ea23eSMarcel Moolenaar 		 * back with absolute block addresses.
543eb0ea23eSMarcel Moolenaar 		 */
544eb0ea23eSMarcel Moolenaar 		lba_offset = lba;
545eb0ea23eSMarcel Moolenaar 	}
546d2fe97c7SPoul-Henning Kamp 	return (error);
5478fae3551SRodney W. Grimes }
5488fae3551SRodney W. Grimes 
5498fae3551SRodney W. Grimes 
550d2fe97c7SPoul-Henning Kamp static void
551326c7cdaSWarner Losh display(FILE *f, const struct disklabel *lp)
5528fae3551SRodney W. Grimes {
553326c7cdaSWarner Losh 	int i, j;
554326c7cdaSWarner Losh 	const struct partition *pp;
5558fae3551SRodney W. Grimes 
556d2fe97c7SPoul-Henning Kamp 	if (lp == NULL)
557d2fe97c7SPoul-Henning Kamp 		lp = &lab;
558d2fe97c7SPoul-Henning Kamp 
5598fae3551SRodney W. Grimes 	fprintf(f, "# %s:\n", specname);
56057dfbec5SPoul-Henning Kamp 	if (allfields) {
561484c7804SJulian Elischer 		if (lp->d_type < DKMAXTYPES)
5628fae3551SRodney W. Grimes 			fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
5638fae3551SRodney W. Grimes 		else
5642a1deaaaSBruce Evans 			fprintf(f, "type: %u\n", lp->d_type);
56561de51caSJoerg Wunsch 		fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
56661de51caSJoerg Wunsch 			lp->d_typename);
56761de51caSJoerg Wunsch 		fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
56861de51caSJoerg Wunsch 			lp->d_packname);
5698fae3551SRodney W. Grimes 		fprintf(f, "flags:");
5708fae3551SRodney W. Grimes 		if (lp->d_flags & D_REMOVABLE)
5718fae3551SRodney W. Grimes 			fprintf(f, " removeable");
5728fae3551SRodney W. Grimes 		if (lp->d_flags & D_ECC)
5738fae3551SRodney W. Grimes 			fprintf(f, " ecc");
5748fae3551SRodney W. Grimes 		if (lp->d_flags & D_BADSECT)
5758fae3551SRodney W. Grimes 			fprintf(f, " badsect");
5768fae3551SRodney W. Grimes 		fprintf(f, "\n");
5772a1deaaaSBruce Evans 		fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
5782a1deaaaSBruce Evans 		fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
5792a1deaaaSBruce Evans 		fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
5802a1deaaaSBruce Evans 		fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
5812a1deaaaSBruce Evans 		fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
5822a1deaaaSBruce Evans 		fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
5832a1deaaaSBruce Evans 		fprintf(f, "rpm: %u\n", lp->d_rpm);
5842a1deaaaSBruce Evans 		fprintf(f, "interleave: %u\n", lp->d_interleave);
5852a1deaaaSBruce Evans 		fprintf(f, "trackskew: %u\n", lp->d_trackskew);
5862a1deaaaSBruce Evans 		fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
5872a1deaaaSBruce Evans 		fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
5882a1deaaaSBruce Evans 		    (u_long)lp->d_headswitch);
58961de51caSJoerg Wunsch 		fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
5902a1deaaaSBruce Evans 		    (u_long)lp->d_trkseek);
5918fae3551SRodney W. Grimes 		fprintf(f, "drivedata: ");
5928fae3551SRodney W. Grimes 		for (i = NDDATA - 1; i >= 0; i--)
5938fae3551SRodney W. Grimes 			if (lp->d_drivedata[i])
5948fae3551SRodney W. Grimes 				break;
5958fae3551SRodney W. Grimes 		if (i < 0)
5968fae3551SRodney W. Grimes 			i = 0;
5978fae3551SRodney W. Grimes 		for (j = 0; j <= i; j++)
5982a1deaaaSBruce Evans 			fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
59957dfbec5SPoul-Henning Kamp 		fprintf(f, "\n\n");
60057dfbec5SPoul-Henning Kamp 	}
60157dfbec5SPoul-Henning Kamp 	fprintf(f, "%u partitions:\n", lp->d_npartitions);
6028fae3551SRodney W. Grimes 	fprintf(f,
603ca4693edSJustin T. Gibbs 	    "#          size     offset    fstype   [fsize bsize bps/cpg]\n");
6048fae3551SRodney W. Grimes 	pp = lp->d_partitions;
6058fae3551SRodney W. Grimes 	for (i = 0; i < lp->d_npartitions; i++, pp++) {
6068fae3551SRodney W. Grimes 		if (pp->p_size) {
607a5871155SUlrich Spörlein 			fprintf(f, "  %c: %10lu %10lu  ", 'a' + i,
6082a1deaaaSBruce Evans 			   (u_long)pp->p_size, (u_long)pp->p_offset);
609484c7804SJulian Elischer 			if (pp->p_fstype < FSMAXTYPES)
6108fae3551SRodney W. Grimes 				fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
6118fae3551SRodney W. Grimes 			else
6128fae3551SRodney W. Grimes 				fprintf(f, "%8d", pp->p_fstype);
6138fae3551SRodney W. Grimes 			switch (pp->p_fstype) {
6148fae3551SRodney W. Grimes 
6158fae3551SRodney W. Grimes 			case FS_UNUSED:				/* XXX */
616a5871155SUlrich Spörlein 				fprintf(f, "    %5lu %5lu %2s",
6172a1deaaaSBruce Evans 				    (u_long)pp->p_fsize,
6182a1deaaaSBruce Evans 				    (u_long)(pp->p_fsize * pp->p_frag), "");
6198fae3551SRodney W. Grimes 				break;
6208fae3551SRodney W. Grimes 
6218fae3551SRodney W. Grimes 			case FS_BSDFFS:
6222a1deaaaSBruce Evans 				fprintf(f, "    %5lu %5lu %5u",
6232a1deaaaSBruce Evans 				    (u_long)pp->p_fsize,
6242a1deaaaSBruce Evans 				    (u_long)(pp->p_fsize * pp->p_frag),
6258fae3551SRodney W. Grimes 				    pp->p_cpg);
6268fae3551SRodney W. Grimes 				break;
6278fae3551SRodney W. Grimes 
628ca4693edSJustin T. Gibbs 			case FS_BSDLFS:
6292a1deaaaSBruce Evans 				fprintf(f, "    %5lu %5lu %5d",
6302a1deaaaSBruce Evans 				    (u_long)pp->p_fsize,
6312a1deaaaSBruce Evans 				    (u_long)(pp->p_fsize * pp->p_frag),
632ca4693edSJustin T. Gibbs 				    pp->p_cpg);
633ca4693edSJustin T. Gibbs 				break;
634ca4693edSJustin T. Gibbs 
6358fae3551SRodney W. Grimes 			default:
6368fae3551SRodney W. Grimes 				fprintf(f, "%20.20s", "");
6378fae3551SRodney W. Grimes 				break;
6388fae3551SRodney W. Grimes 			}
63957dfbec5SPoul-Henning Kamp 			if (i == RAW_PART) {
64057dfbec5SPoul-Henning Kamp 				fprintf(f, "  # \"raw\" part, don't edit");
64157dfbec5SPoul-Henning Kamp 			}
642d2fe97c7SPoul-Henning Kamp 			fprintf(f, "\n");
6438fae3551SRodney W. Grimes 		}
6448fae3551SRodney W. Grimes 	}
6458fae3551SRodney W. Grimes 	fflush(f);
6468fae3551SRodney W. Grimes }
6478fae3551SRodney W. Grimes 
648d2fe97c7SPoul-Henning Kamp static int
649d2fe97c7SPoul-Henning Kamp edit(void)
6508fae3551SRodney W. Grimes {
651326c7cdaSWarner Losh 	int c, fd;
6528fae3551SRodney W. Grimes 	struct disklabel label;
653722ceb3fSWarner Losh 	FILE *fp;
6548fae3551SRodney W. Grimes 
655722ceb3fSWarner Losh 	if ((fd = mkstemp(tmpfil)) == -1 ||
656722ceb3fSWarner Losh 	    (fp = fdopen(fd, "w")) == NULL) {
6576bd343a9SPhilippe Charnier 		warnx("can't create %s", tmpfil);
6588fae3551SRodney W. Grimes 		return (1);
6598fae3551SRodney W. Grimes 	}
660d2fe97c7SPoul-Henning Kamp 	display(fp, NULL);
661722ceb3fSWarner Losh 	fclose(fp);
6628fae3551SRodney W. Grimes 	for (;;) {
6638fae3551SRodney W. Grimes 		if (!editit())
6648fae3551SRodney W. Grimes 			break;
665722ceb3fSWarner Losh 		fp = fopen(tmpfil, "r");
666722ceb3fSWarner Losh 		if (fp == NULL) {
6676bd343a9SPhilippe Charnier 			warnx("can't reopen %s for reading", tmpfil);
6688fae3551SRodney W. Grimes 			break;
6698fae3551SRodney W. Grimes 		}
6708fae3551SRodney W. Grimes 		bzero((char *)&label, sizeof(label));
671d2fe97c7SPoul-Henning Kamp 		c = getasciilabel(fp, &label);
672722ceb3fSWarner Losh 		fclose(fp);
673d2fe97c7SPoul-Henning Kamp 		if (c) {
674d2fe97c7SPoul-Henning Kamp 			lab = label;
675d2fe97c7SPoul-Henning Kamp 			if (writelabel() == 0) {
6768fae3551SRodney W. Grimes 				(void) unlink(tmpfil);
6778fae3551SRodney W. Grimes 				return (0);
6788fae3551SRodney W. Grimes 			}
6798fae3551SRodney W. Grimes 		}
680d2fe97c7SPoul-Henning Kamp 		printf("re-edit the label? [y]: ");
681d2fe97c7SPoul-Henning Kamp 		fflush(stdout);
6828fae3551SRodney W. Grimes 		c = getchar();
6838fae3551SRodney W. Grimes 		if (c != EOF && c != (int)'\n')
6848fae3551SRodney W. Grimes 			while (getchar() != (int)'\n')
6858fae3551SRodney W. Grimes 				;
6868fae3551SRodney W. Grimes 		if  (c == (int)'n')
6878fae3551SRodney W. Grimes 			break;
6888fae3551SRodney W. Grimes 	}
6898fae3551SRodney W. Grimes 	(void) unlink(tmpfil);
6908fae3551SRodney W. Grimes 	return (1);
6918fae3551SRodney W. Grimes }
6928fae3551SRodney W. Grimes 
693d2fe97c7SPoul-Henning Kamp static int
694326c7cdaSWarner Losh editit(void)
6958fae3551SRodney W. Grimes {
696326c7cdaSWarner Losh 	int pid, xpid;
697c8223965SMark Murray 	int locstat, omask;
698c8223965SMark Murray 	const char *ed;
69973d6722dSKevin Lo 	uid_t uid;
70073d6722dSKevin Lo 	gid_t gid;
7018fae3551SRodney W. Grimes 
7028fae3551SRodney W. Grimes 	omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
7038fae3551SRodney W. Grimes 	while ((pid = fork()) < 0) {
7048fae3551SRodney W. Grimes 		if (errno == EPROCLIM) {
7056bd343a9SPhilippe Charnier 			warnx("you have too many processes");
7068fae3551SRodney W. Grimes 			return(0);
7078fae3551SRodney W. Grimes 		}
7088fae3551SRodney W. Grimes 		if (errno != EAGAIN) {
7096bd343a9SPhilippe Charnier 			warn("fork");
7108fae3551SRodney W. Grimes 			return(0);
7118fae3551SRodney W. Grimes 		}
7128fae3551SRodney W. Grimes 		sleep(1);
7138fae3551SRodney W. Grimes 	}
7148fae3551SRodney W. Grimes 	if (pid == 0) {
7158fae3551SRodney W. Grimes 		sigsetmask(omask);
71673d6722dSKevin Lo 		gid = getgid();
71773d6722dSKevin Lo 		if (setresgid(gid, gid, gid) == -1)
71873d6722dSKevin Lo 			err(1, "setresgid");
71973d6722dSKevin Lo 		uid = getuid();
72073d6722dSKevin Lo 		if (setresuid(uid, uid, uid) == -1)
72173d6722dSKevin Lo 			err(1, "setresuid");
7228fae3551SRodney W. Grimes 		if ((ed = getenv("EDITOR")) == (char *)0)
7238fae3551SRodney W. Grimes 			ed = DEFEDITOR;
7247bc6d015SBrian Somers 		execlp(ed, ed, tmpfil, (char *)0);
7256bd343a9SPhilippe Charnier 		err(1, "%s", ed);
7268fae3551SRodney W. Grimes 	}
727c8223965SMark Murray 	while ((xpid = wait(&locstat)) >= 0)
7288fae3551SRodney W. Grimes 		if (xpid == pid)
7298fae3551SRodney W. Grimes 			break;
7308fae3551SRodney W. Grimes 	sigsetmask(omask);
731c8223965SMark Murray 	return(!locstat);
7328fae3551SRodney W. Grimes }
7338fae3551SRodney W. Grimes 
734d2fe97c7SPoul-Henning Kamp static char *
735326c7cdaSWarner Losh skip(char *cp)
7368fae3551SRodney W. Grimes {
7378fae3551SRodney W. Grimes 
7388fae3551SRodney W. Grimes 	while (*cp != '\0' && isspace(*cp))
7398fae3551SRodney W. Grimes 		cp++;
7408fae3551SRodney W. Grimes 	if (*cp == '\0' || *cp == '#')
741326c7cdaSWarner Losh 		return (NULL);
7428fae3551SRodney W. Grimes 	return (cp);
7438fae3551SRodney W. Grimes }
7448fae3551SRodney W. Grimes 
745d2fe97c7SPoul-Henning Kamp static char *
746326c7cdaSWarner Losh word(char *cp)
7478fae3551SRodney W. Grimes {
748326c7cdaSWarner Losh 	char c;
7498fae3551SRodney W. Grimes 
7508fae3551SRodney W. Grimes 	while (*cp != '\0' && !isspace(*cp) && *cp != '#')
7518fae3551SRodney W. Grimes 		cp++;
7528fae3551SRodney W. Grimes 	if ((c = *cp) != '\0') {
7538fae3551SRodney W. Grimes 		*cp++ = '\0';
7548fae3551SRodney W. Grimes 		if (c != '#')
7558fae3551SRodney W. Grimes 			return (skip(cp));
7568fae3551SRodney W. Grimes 	}
757326c7cdaSWarner Losh 	return (NULL);
7588fae3551SRodney W. Grimes }
7598fae3551SRodney W. Grimes 
7608fae3551SRodney W. Grimes /*
7618fae3551SRodney W. Grimes  * Read an ascii label in from fd f,
7628fae3551SRodney W. Grimes  * in the same format as that put out by display(),
7638fae3551SRodney W. Grimes  * and fill in lp.
7648fae3551SRodney W. Grimes  */
765d2fe97c7SPoul-Henning Kamp static int
766326c7cdaSWarner Losh getasciilabel(FILE *f, struct disklabel *lp)
7678fae3551SRodney W. Grimes {
768fb26ece7SJaakko Heinonen 	char *cp, *endp;
7696b0ff5f5SPoul-Henning Kamp 	const char **cpp;
770484c7804SJulian Elischer 	u_int part;
7716b0ff5f5SPoul-Henning Kamp 	char *tp, line[BUFSIZ];
772484c7804SJulian Elischer 	u_long v;
773484c7804SJulian Elischer 	int lineno = 0, errors = 0;
774326c7cdaSWarner Losh 	int i;
7758fae3551SRodney W. Grimes 
7768970f1a4SPoul-Henning Kamp 	makelabel("auto", lp);
777b0459c58SDag-Erling Smørgrav 	bzero(&part_set, sizeof(part_set));
778b0459c58SDag-Erling Smørgrav 	bzero(&part_size_type, sizeof(part_size_type));
779b0459c58SDag-Erling Smørgrav 	bzero(&part_offset_type, sizeof(part_offset_type));
7808fae3551SRodney W. Grimes 	lp->d_bbsize = BBSIZE;				/* XXX */
78177068a7fSPoul-Henning Kamp 	lp->d_sbsize = 0;				/* XXX */
7828fae3551SRodney W. Grimes 	while (fgets(line, sizeof(line) - 1, f)) {
7838fae3551SRodney W. Grimes 		lineno++;
784*fb6a3593SMarcelo Araujo 		if ((cp = strchr(line,'\n')) != NULL)
7858fae3551SRodney W. Grimes 			*cp = '\0';
7868fae3551SRodney W. Grimes 		cp = skip(line);
7878fae3551SRodney W. Grimes 		if (cp == NULL)
7888fae3551SRodney W. Grimes 			continue;
789b3608ae1SEd Schouten 		tp = strchr(cp, ':');
7908fae3551SRodney W. Grimes 		if (tp == NULL) {
7918fae3551SRodney W. Grimes 			fprintf(stderr, "line %d: syntax error\n", lineno);
7928fae3551SRodney W. Grimes 			errors++;
7938fae3551SRodney W. Grimes 			continue;
7948fae3551SRodney W. Grimes 		}
7958fae3551SRodney W. Grimes 		*tp++ = '\0', tp = skip(tp);
796d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "type")) {
7978fae3551SRodney W. Grimes 			if (tp == NULL)
798c8223965SMark Murray 				tp = unknown;
7998fae3551SRodney W. Grimes 			cpp = dktypenames;
8008fae3551SRodney W. Grimes 			for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
801d2fe97c7SPoul-Henning Kamp 				if (*cpp && !strcmp(*cpp, tp)) {
8028fae3551SRodney W. Grimes 					lp->d_type = cpp - dktypenames;
80309dbd070SIan Dowse 					break;
8048fae3551SRodney W. Grimes 				}
80509dbd070SIan Dowse 			if (cpp < &dktypenames[DKMAXTYPES])
80609dbd070SIan Dowse 				continue;
807fb26ece7SJaakko Heinonen 			errno = 0;
808fb26ece7SJaakko Heinonen 			v = strtoul(tp, &endp, 10);
809fb26ece7SJaakko Heinonen 			if (errno != 0 || *endp != '\0')
810fb26ece7SJaakko Heinonen 				v = DKMAXTYPES;
811484c7804SJulian Elischer 			if (v >= DKMAXTYPES)
812484c7804SJulian Elischer 				fprintf(stderr, "line %d:%s %lu\n", lineno,
8138fae3551SRodney W. Grimes 				    "Warning, unknown disk type", v);
814fb26ece7SJaakko Heinonen 			else
8158fae3551SRodney W. Grimes 				lp->d_type = v;
8168fae3551SRodney W. Grimes 			continue;
8178fae3551SRodney W. Grimes 		}
818d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "flags")) {
8198fae3551SRodney W. Grimes 			for (v = 0; (cp = tp) && *cp != '\0';) {
8208fae3551SRodney W. Grimes 				tp = word(cp);
821d2fe97c7SPoul-Henning Kamp 				if (!strcmp(cp, "removeable"))
8228fae3551SRodney W. Grimes 					v |= D_REMOVABLE;
823d2fe97c7SPoul-Henning Kamp 				else if (!strcmp(cp, "ecc"))
8248fae3551SRodney W. Grimes 					v |= D_ECC;
825d2fe97c7SPoul-Henning Kamp 				else if (!strcmp(cp, "badsect"))
8268fae3551SRodney W. Grimes 					v |= D_BADSECT;
8278fae3551SRodney W. Grimes 				else {
8288fae3551SRodney W. Grimes 					fprintf(stderr,
8298fae3551SRodney W. Grimes 					    "line %d: %s: bad flag\n",
8308fae3551SRodney W. Grimes 					    lineno, cp);
8318fae3551SRodney W. Grimes 					errors++;
8328fae3551SRodney W. Grimes 				}
8338fae3551SRodney W. Grimes 			}
8348fae3551SRodney W. Grimes 			lp->d_flags = v;
8358fae3551SRodney W. Grimes 			continue;
8368fae3551SRodney W. Grimes 		}
837d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "drivedata")) {
8388fae3551SRodney W. Grimes 			for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
839484c7804SJulian Elischer 				lp->d_drivedata[i++] = strtoul(cp, NULL, 10);
8408fae3551SRodney W. Grimes 				tp = word(cp);
8418fae3551SRodney W. Grimes 			}
8428fae3551SRodney W. Grimes 			continue;
8438fae3551SRodney W. Grimes 		}
844484c7804SJulian Elischer 		if (sscanf(cp, "%lu partitions", &v) == 1) {
845b11075cdSAndrey V. Elsukov 			if (v > MAXPARTITIONS) {
8468fae3551SRodney W. Grimes 				fprintf(stderr,
8478fae3551SRodney W. Grimes 				    "line %d: bad # of partitions\n", lineno);
8488fae3551SRodney W. Grimes 				lp->d_npartitions = MAXPARTITIONS;
8498fae3551SRodney W. Grimes 				errors++;
850982e6e69SAndrey V. Elsukov 			} else if (v < DEFPARTITIONS) {
851982e6e69SAndrey V. Elsukov 				fprintf(stderr,
852982e6e69SAndrey V. Elsukov 				    "line %d: bad # of partitions\n", lineno);
853982e6e69SAndrey V. Elsukov 				lp->d_npartitions = DEFPARTITIONS;
854982e6e69SAndrey V. Elsukov 				errors++;
8558fae3551SRodney W. Grimes 			} else
8568fae3551SRodney W. Grimes 				lp->d_npartitions = v;
8578fae3551SRodney W. Grimes 			continue;
8588fae3551SRodney W. Grimes 		}
8598fae3551SRodney W. Grimes 		if (tp == NULL)
860c8223965SMark Murray 			tp = blank;
861d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "disk")) {
8628fae3551SRodney W. Grimes 			strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
8638fae3551SRodney W. Grimes 			continue;
8648fae3551SRodney W. Grimes 		}
865d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "label")) {
8668fae3551SRodney W. Grimes 			strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
8678fae3551SRodney W. Grimes 			continue;
8688fae3551SRodney W. Grimes 		}
869d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "bytes/sector")) {
870484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
871484c7804SJulian Elischer 			if (v == 0 || (v % DEV_BSIZE) != 0) {
8728fae3551SRodney W. Grimes 				fprintf(stderr,
8738fae3551SRodney W. Grimes 				    "line %d: %s: bad sector size\n",
8748fae3551SRodney W. Grimes 				    lineno, tp);
8758fae3551SRodney W. Grimes 				errors++;
8768fae3551SRodney W. Grimes 			} else
8778fae3551SRodney W. Grimes 				lp->d_secsize = v;
8788fae3551SRodney W. Grimes 			continue;
8798fae3551SRodney W. Grimes 		}
880d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "sectors/track")) {
881484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
882484c7804SJulian Elischer #if (ULONG_MAX != 0xffffffffUL)
883266e7997SDag-Erling Smørgrav 			if (v == 0 || v > 0xffffffff)
884484c7804SJulian Elischer #else
885266e7997SDag-Erling Smørgrav 			if (v == 0)
886484c7804SJulian Elischer #endif
887266e7997SDag-Erling Smørgrav 			{
8888fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
8898fae3551SRodney W. Grimes 				    lineno, tp, cp);
8908fae3551SRodney W. Grimes 				errors++;
8918fae3551SRodney W. Grimes 			} else
8928fae3551SRodney W. Grimes 				lp->d_nsectors = v;
8938fae3551SRodney W. Grimes 			continue;
8948fae3551SRodney W. Grimes 		}
895d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "sectors/cylinder")) {
896484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
897484c7804SJulian Elischer 			if (v == 0) {
8988fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
8998fae3551SRodney W. Grimes 				    lineno, tp, cp);
9008fae3551SRodney W. Grimes 				errors++;
9018fae3551SRodney W. Grimes 			} else
9028fae3551SRodney W. Grimes 				lp->d_secpercyl = v;
9038fae3551SRodney W. Grimes 			continue;
9048fae3551SRodney W. Grimes 		}
905d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "tracks/cylinder")) {
906484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
907484c7804SJulian Elischer 			if (v == 0) {
9088fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
9098fae3551SRodney W. Grimes 				    lineno, tp, cp);
9108fae3551SRodney W. Grimes 				errors++;
9118fae3551SRodney W. Grimes 			} else
9128fae3551SRodney W. Grimes 				lp->d_ntracks = v;
9138fae3551SRodney W. Grimes 			continue;
9148fae3551SRodney W. Grimes 		}
915d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "cylinders")) {
916484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
917484c7804SJulian Elischer 			if (v == 0) {
9188fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
9198fae3551SRodney W. Grimes 				    lineno, tp, cp);
9208fae3551SRodney W. Grimes 				errors++;
9218fae3551SRodney W. Grimes 			} else
9228fae3551SRodney W. Grimes 				lp->d_ncylinders = v;
9238fae3551SRodney W. Grimes 			continue;
9248fae3551SRodney W. Grimes 		}
925d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "sectors/unit")) {
926484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
927484c7804SJulian Elischer 			if (v == 0) {
928f75dd518SBruce Evans 				fprintf(stderr, "line %d: %s: bad %s\n",
929f75dd518SBruce Evans 				    lineno, tp, cp);
930f75dd518SBruce Evans 				errors++;
931f75dd518SBruce Evans 			} else
932f75dd518SBruce Evans 				lp->d_secperunit = v;
933f75dd518SBruce Evans 			continue;
934f75dd518SBruce Evans 		}
935d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "rpm")) {
936484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
937484c7804SJulian Elischer 			if (v == 0 || v > USHRT_MAX) {
9388fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
9398fae3551SRodney W. Grimes 				    lineno, tp, cp);
9408fae3551SRodney W. Grimes 				errors++;
9418fae3551SRodney W. Grimes 			} else
9428fae3551SRodney W. Grimes 				lp->d_rpm = v;
9438fae3551SRodney W. Grimes 			continue;
9448fae3551SRodney W. Grimes 		}
945d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "interleave")) {
946484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
947484c7804SJulian Elischer 			if (v == 0 || v > USHRT_MAX) {
9488fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
9498fae3551SRodney W. Grimes 				    lineno, tp, cp);
9508fae3551SRodney W. Grimes 				errors++;
9518fae3551SRodney W. Grimes 			} else
9528fae3551SRodney W. Grimes 				lp->d_interleave = v;
9538fae3551SRodney W. Grimes 			continue;
9548fae3551SRodney W. Grimes 		}
955d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "trackskew")) {
956484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
957484c7804SJulian Elischer 			if (v > USHRT_MAX) {
9588fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
9598fae3551SRodney W. Grimes 				    lineno, tp, cp);
9608fae3551SRodney W. Grimes 				errors++;
9618fae3551SRodney W. Grimes 			} else
9628fae3551SRodney W. Grimes 				lp->d_trackskew = v;
9638fae3551SRodney W. Grimes 			continue;
9648fae3551SRodney W. Grimes 		}
965d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "cylinderskew")) {
966484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
967484c7804SJulian Elischer 			if (v > USHRT_MAX) {
9688fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
9698fae3551SRodney W. Grimes 				    lineno, tp, cp);
9708fae3551SRodney W. Grimes 				errors++;
9718fae3551SRodney W. Grimes 			} else
9728fae3551SRodney W. Grimes 				lp->d_cylskew = v;
9738fae3551SRodney W. Grimes 			continue;
9748fae3551SRodney W. Grimes 		}
975d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "headswitch")) {
976484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
9778fae3551SRodney W. Grimes 			lp->d_headswitch = v;
9788fae3551SRodney W. Grimes 			continue;
9798fae3551SRodney W. Grimes 		}
980d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "track-to-track seek")) {
981484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
9828fae3551SRodney W. Grimes 			lp->d_trkseek = v;
9838fae3551SRodney W. Grimes 			continue;
9848fae3551SRodney W. Grimes 		}
9853233afaeSJohn W. De Boskey 		/* the ':' was removed above */
98667b46708SIan Dowse 		if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') {
98767b46708SIan Dowse 			fprintf(stderr,
98867b46708SIan Dowse 			    "line %d: %s: Unknown disklabel field\n", lineno,
98967b46708SIan Dowse 			    cp);
99067b46708SIan Dowse 			errors++;
99167b46708SIan Dowse 			continue;
99267b46708SIan Dowse 		}
99367b46708SIan Dowse 
99467b46708SIan Dowse 		/* Process a partition specification line. */
9953233afaeSJohn W. De Boskey 		part = *cp - 'a';
9963233afaeSJohn W. De Boskey 		if (part >= lp->d_npartitions) {
9978fae3551SRodney W. Grimes 			fprintf(stderr,
9983233afaeSJohn W. De Boskey 			    "line %d: partition name out of range a-%c: %s\n",
9993233afaeSJohn W. De Boskey 			    lineno, 'a' + lp->d_npartitions - 1, cp);
10008fae3551SRodney W. Grimes 			errors++;
10018fae3551SRodney W. Grimes 			continue;
10028fae3551SRodney W. Grimes 		}
10033233afaeSJohn W. De Boskey 		part_set[part] = 1;
100467b46708SIan Dowse 
100567b46708SIan Dowse 		if (getasciipartspec(tp, lp, part, lineno) != 0) {
100667b46708SIan Dowse 			errors++;
100767b46708SIan Dowse 			break;
100867b46708SIan Dowse 		}
100967b46708SIan Dowse 	}
101067b46708SIan Dowse 	errors += checklabel(lp);
101167b46708SIan Dowse 	return (errors == 0);
101267b46708SIan Dowse }
101367b46708SIan Dowse 
101467b46708SIan Dowse #define NXTNUM(n) do { \
101513e0abcbSPaul Traina 	if (tp == NULL) { \
101613e0abcbSPaul Traina 		fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
101767b46708SIan Dowse 		return (1); \
101813e0abcbSPaul Traina 	} else { \
10198fae3551SRodney W. Grimes 		cp = tp, tp = word(cp); \
1020484c7804SJulian Elischer 		(n) = strtoul(cp, NULL, 10); \
102113e0abcbSPaul Traina 	} \
102267b46708SIan Dowse } while (0)
102367b46708SIan Dowse 
10243233afaeSJohn W. De Boskey /* retain 1 character following number */
102567b46708SIan Dowse #define NXTWORD(w,n) do { \
10263233afaeSJohn W. De Boskey 	if (tp == NULL) { \
10273233afaeSJohn W. De Boskey 		fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
102867b46708SIan Dowse 		return (1); \
10293233afaeSJohn W. De Boskey 	} else { \
10303233afaeSJohn W. De Boskey 		char *tmp; \
10313233afaeSJohn W. De Boskey 		cp = tp, tp = word(cp); \
1032484c7804SJulian Elischer 		(n) = strtoul(cp, &tmp, 10); \
10333233afaeSJohn W. De Boskey 		if (tmp) (w) = *tmp; \
10343233afaeSJohn W. De Boskey 	} \
103567b46708SIan Dowse } while (0)
103667b46708SIan Dowse 
103767b46708SIan Dowse /*
103867b46708SIan Dowse  * Read a partition line into partition `part' in the specified disklabel.
103967b46708SIan Dowse  * Return 0 on success, 1 on failure.
104067b46708SIan Dowse  */
1041d2fe97c7SPoul-Henning Kamp static int
104267b46708SIan Dowse getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno)
104367b46708SIan Dowse {
104467b46708SIan Dowse 	struct partition *pp;
1045fb26ece7SJaakko Heinonen 	char *cp, *endp;
104667b46708SIan Dowse 	const char **cpp;
1047484c7804SJulian Elischer 	u_long v;
104867b46708SIan Dowse 
104967b46708SIan Dowse 	pp = &lp->d_partitions[part];
105067b46708SIan Dowse 	cp = NULL;
105167b46708SIan Dowse 
10523233afaeSJohn W. De Boskey 	v = 0;
10533233afaeSJohn W. De Boskey 	NXTWORD(part_size_type[part],v);
1054484c7804SJulian Elischer 	if (v == 0 && part_size_type[part] != '*') {
1055484c7804SJulian Elischer 		fprintf(stderr,
1056484c7804SJulian Elischer 		    "line %d: %s: bad partition size\n", lineno, cp);
105767b46708SIan Dowse 		return (1);
105867b46708SIan Dowse 	}
10598fae3551SRodney W. Grimes 	pp->p_size = v;
10603233afaeSJohn W. De Boskey 
10613233afaeSJohn W. De Boskey 	v = 0;
10623233afaeSJohn W. De Boskey 	NXTWORD(part_offset_type[part],v);
1063484c7804SJulian Elischer 	if (v == 0 && part_offset_type[part] != '*' &&
1064484c7804SJulian Elischer 	    part_offset_type[part] != '\0') {
1065484c7804SJulian Elischer 		fprintf(stderr,
1066484c7804SJulian Elischer 		    "line %d: %s: bad partition offset\n", lineno, cp);
106767b46708SIan Dowse 		return (1);
106867b46708SIan Dowse 	}
10698fae3551SRodney W. Grimes 	pp->p_offset = v;
107029f3f095SYaroslav Tykhiy 	if (tp == NULL) {
107129f3f095SYaroslav Tykhiy 		fprintf(stderr, "line %d: missing file system type\n", lineno);
107229f3f095SYaroslav Tykhiy 		return (1);
107329f3f095SYaroslav Tykhiy 	}
10748fae3551SRodney W. Grimes 	cp = tp, tp = word(cp);
107567b46708SIan Dowse 	for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1076d2fe97c7SPoul-Henning Kamp 		if (*cpp && !strcmp(*cpp, cp))
107767b46708SIan Dowse 			break;
107867b46708SIan Dowse 	if (*cpp != NULL) {
107967b46708SIan Dowse 		pp->p_fstype = cpp - fstypenames;
108067b46708SIan Dowse 	} else {
1081fb26ece7SJaakko Heinonen 		if (isdigit(*cp)) {
1082fb26ece7SJaakko Heinonen 			errno = 0;
1083fb26ece7SJaakko Heinonen 			v = strtoul(cp, &endp, 10);
1084fb26ece7SJaakko Heinonen 			if (errno != 0 || *endp != '\0')
1085fb26ece7SJaakko Heinonen 				v = FSMAXTYPES;
1086fb26ece7SJaakko Heinonen 		} else
10878fae3551SRodney W. Grimes 			v = FSMAXTYPES;
1088484c7804SJulian Elischer 		if (v >= FSMAXTYPES) {
10893233afaeSJohn W. De Boskey 			fprintf(stderr,
109067b46708SIan Dowse 			    "line %d: Warning, unknown file system type %s\n",
10913233afaeSJohn W. De Boskey 			    lineno, cp);
10928fae3551SRodney W. Grimes 			v = FS_UNUSED;
10938fae3551SRodney W. Grimes 		}
10948fae3551SRodney W. Grimes 		pp->p_fstype = v;
109567b46708SIan Dowse 	}
109667b46708SIan Dowse 
10978fae3551SRodney W. Grimes 	switch (pp->p_fstype) {
10983233afaeSJohn W. De Boskey 	case FS_UNUSED:
1099640c9cb2SIan Dowse 	case FS_BSDFFS:
1100640c9cb2SIan Dowse 	case FS_BSDLFS:
1101640c9cb2SIan Dowse 		/* accept defaults for fsize/frag/cpg */
11023233afaeSJohn W. De Boskey 		if (tp) {
11038fae3551SRodney W. Grimes 			NXTNUM(pp->p_fsize);
11048fae3551SRodney W. Grimes 			if (pp->p_fsize == 0)
11058fae3551SRodney W. Grimes 				break;
11068fae3551SRodney W. Grimes 			NXTNUM(v);
11078fae3551SRodney W. Grimes 			pp->p_frag = v / pp->p_fsize;
1108640c9cb2SIan Dowse 			if (tp != NULL)
1109640c9cb2SIan Dowse 				NXTNUM(pp->p_cpg);
11103233afaeSJohn W. De Boskey 		}
11113233afaeSJohn W. De Boskey 		/* else default to 0's */
11128fae3551SRodney W. Grimes 		break;
11138fae3551SRodney W. Grimes 	default:
11148fae3551SRodney W. Grimes 		break;
11158fae3551SRodney W. Grimes 	}
111667b46708SIan Dowse 	return (0);
11178fae3551SRodney W. Grimes }
11188fae3551SRodney W. Grimes 
11198fae3551SRodney W. Grimes /*
11208fae3551SRodney W. Grimes  * Check disklabel for errors and fill in
11218fae3551SRodney W. Grimes  * derived fields according to supplied values.
11228fae3551SRodney W. Grimes  */
1123d2fe97c7SPoul-Henning Kamp static int
1124326c7cdaSWarner Losh checklabel(struct disklabel *lp)
11258fae3551SRodney W. Grimes {
1126326c7cdaSWarner Losh 	struct partition *pp;
11278fae3551SRodney W. Grimes 	int i, errors = 0;
11288fae3551SRodney W. Grimes 	char part;
11293b89beb1SIan Dowse 	u_long base_offset, needed, total_size, total_percent, current_offset;
11303b89beb1SIan Dowse 	long free_space;
11313233afaeSJohn W. De Boskey 	int seen_default_offset;
11323233afaeSJohn W. De Boskey 	int hog_part;
11333233afaeSJohn W. De Boskey 	int j;
11343233afaeSJohn W. De Boskey 	struct partition *pp2;
11358fae3551SRodney W. Grimes 
1136d2fe97c7SPoul-Henning Kamp 	if (lp == NULL)
1137d2fe97c7SPoul-Henning Kamp 		lp = &lab;
1138d2fe97c7SPoul-Henning Kamp 
113957dfbec5SPoul-Henning Kamp 	if (allfields) {
114057dfbec5SPoul-Henning Kamp 
11418fae3551SRodney W. Grimes 		if (lp->d_secsize == 0) {
11422a1deaaaSBruce Evans 			fprintf(stderr, "sector size 0\n");
11438fae3551SRodney W. Grimes 			return (1);
11448fae3551SRodney W. Grimes 		}
11458fae3551SRodney W. Grimes 		if (lp->d_nsectors == 0) {
11462a1deaaaSBruce Evans 			fprintf(stderr, "sectors/track 0\n");
11478fae3551SRodney W. Grimes 			return (1);
11488fae3551SRodney W. Grimes 		}
11498fae3551SRodney W. Grimes 		if (lp->d_ntracks == 0) {
11502a1deaaaSBruce Evans 			fprintf(stderr, "tracks/cylinder 0\n");
11518fae3551SRodney W. Grimes 			return (1);
11528fae3551SRodney W. Grimes 		}
11538fae3551SRodney W. Grimes 		if  (lp->d_ncylinders == 0) {
11542a1deaaaSBruce Evans 			fprintf(stderr, "cylinders/unit 0\n");
11558fae3551SRodney W. Grimes 			errors++;
11568fae3551SRodney W. Grimes 		}
11578fae3551SRodney W. Grimes 		if (lp->d_rpm == 0)
115857dfbec5SPoul-Henning Kamp 			warnx("revolutions/minute 0");
11598fae3551SRodney W. Grimes 		if (lp->d_secpercyl == 0)
11608fae3551SRodney W. Grimes 			lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
11618fae3551SRodney W. Grimes 		if (lp->d_secperunit == 0)
11628fae3551SRodney W. Grimes 			lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
11638fae3551SRodney W. Grimes 		if (lp->d_bbsize == 0) {
11642a1deaaaSBruce Evans 			fprintf(stderr, "boot block size 0\n");
11658fae3551SRodney W. Grimes 			errors++;
11668fae3551SRodney W. Grimes 		} else if (lp->d_bbsize % lp->d_secsize)
116757dfbec5SPoul-Henning Kamp 			warnx("boot block size %% sector-size != 0");
1168982e6e69SAndrey V. Elsukov 		if (lp->d_npartitions > MAXPARTITIONS) {
116957dfbec5SPoul-Henning Kamp 			warnx("number of partitions (%lu) > MAXPARTITIONS (%d)",
11702a1deaaaSBruce Evans 			    (u_long)lp->d_npartitions, MAXPARTITIONS);
1171982e6e69SAndrey V. Elsukov 			errors++;
1172982e6e69SAndrey V. Elsukov 		}
1173982e6e69SAndrey V. Elsukov 		if (lp->d_npartitions < DEFPARTITIONS) {
1174982e6e69SAndrey V. Elsukov 			warnx("number of partitions (%lu) < DEFPARTITIONS (%d)",
1175982e6e69SAndrey V. Elsukov 			    (u_long)lp->d_npartitions, DEFPARTITIONS);
1176982e6e69SAndrey V. Elsukov 			errors++;
1177982e6e69SAndrey V. Elsukov 		}
117857dfbec5SPoul-Henning Kamp 	} else {
117957dfbec5SPoul-Henning Kamp 		struct disklabel *vl;
118057dfbec5SPoul-Henning Kamp 
118157dfbec5SPoul-Henning Kamp 		vl = getvirginlabel();
1182982e6e69SAndrey V. Elsukov 		if (lp->d_secsize == 0)
118357dfbec5SPoul-Henning Kamp 			lp->d_secsize = vl->d_secsize;
1184982e6e69SAndrey V. Elsukov 		if (lp->d_nsectors == 0)
118557dfbec5SPoul-Henning Kamp 			lp->d_nsectors = vl->d_nsectors;
1186982e6e69SAndrey V. Elsukov 		if (lp->d_ntracks == 0)
118757dfbec5SPoul-Henning Kamp 			lp->d_ntracks = vl->d_ntracks;
1188982e6e69SAndrey V. Elsukov 		if (lp->d_ncylinders == 0)
118957dfbec5SPoul-Henning Kamp 			lp->d_ncylinders = vl->d_ncylinders;
1190982e6e69SAndrey V. Elsukov 		if (lp->d_rpm == 0)
119157dfbec5SPoul-Henning Kamp 			lp->d_rpm = vl->d_rpm;
1192982e6e69SAndrey V. Elsukov 		if (lp->d_interleave == 0)
119357dfbec5SPoul-Henning Kamp 			lp->d_interleave = vl->d_interleave;
1194982e6e69SAndrey V. Elsukov 		if (lp->d_secpercyl == 0)
119557dfbec5SPoul-Henning Kamp 			lp->d_secpercyl = vl->d_secpercyl;
1196b9f41b60SAndrey V. Elsukov 		if (lp->d_secperunit == 0 ||
1197b9f41b60SAndrey V. Elsukov 		    lp->d_secperunit > vl->d_secperunit)
119857dfbec5SPoul-Henning Kamp 			lp->d_secperunit = vl->d_secperunit;
1199982e6e69SAndrey V. Elsukov 		if (lp->d_bbsize == 0)
120057dfbec5SPoul-Henning Kamp 			lp->d_bbsize = vl->d_bbsize;
1201b11075cdSAndrey V. Elsukov 		if (lp->d_npartitions < DEFPARTITIONS ||
1202982e6e69SAndrey V. Elsukov 		    lp->d_npartitions > MAXPARTITIONS)
120357dfbec5SPoul-Henning Kamp 			lp->d_npartitions = vl->d_npartitions;
120457dfbec5SPoul-Henning Kamp 	}
120557dfbec5SPoul-Henning Kamp 
12063233afaeSJohn W. De Boskey 
12073233afaeSJohn W. De Boskey 	/* first allocate space to the partitions, then offsets */
12083233afaeSJohn W. De Boskey 	total_size = 0; /* in sectors */
12093233afaeSJohn W. De Boskey 	total_percent = 0; /* in percent */
12103233afaeSJohn W. De Boskey 	hog_part = -1;
12113233afaeSJohn W. De Boskey 	/* find all fixed partitions */
12123233afaeSJohn W. De Boskey 	for (i = 0; i < lp->d_npartitions; i++) {
12133233afaeSJohn W. De Boskey 		pp = &lp->d_partitions[i];
12143233afaeSJohn W. De Boskey 		if (part_set[i]) {
12153233afaeSJohn W. De Boskey 			if (part_size_type[i] == '*') {
12166b0ff5f5SPoul-Henning Kamp 				if (i == RAW_PART) {
12173233afaeSJohn W. De Boskey 					pp->p_size = lp->d_secperunit;
12183233afaeSJohn W. De Boskey 				} else {
12193233afaeSJohn W. De Boskey 					if (hog_part != -1)
122057dfbec5SPoul-Henning Kamp 						warnx("Too many '*' partitions (%c and %c)",
12213233afaeSJohn W. De Boskey 						    hog_part + 'a',i + 'a');
12223233afaeSJohn W. De Boskey 					else
12233233afaeSJohn W. De Boskey 						hog_part = i;
12243233afaeSJohn W. De Boskey 				}
12253233afaeSJohn W. De Boskey 			} else {
12268d3105e8SWarner Losh 				off_t size;
12273233afaeSJohn W. De Boskey 
12283233afaeSJohn W. De Boskey 				size = pp->p_size;
12293233afaeSJohn W. De Boskey 				switch (part_size_type[i]) {
12303233afaeSJohn W. De Boskey 				case '%':
12313233afaeSJohn W. De Boskey 					total_percent += size;
12323233afaeSJohn W. De Boskey 					break;
12334c814dfcSDag-Erling Smørgrav 				case 't':
12344c814dfcSDag-Erling Smørgrav 				case 'T':
12354c814dfcSDag-Erling Smørgrav 					size *= 1024ULL;
12364c814dfcSDag-Erling Smørgrav 					/* FALLTHROUGH */
12374c814dfcSDag-Erling Smørgrav 				case 'g':
12384c814dfcSDag-Erling Smørgrav 				case 'G':
12394c814dfcSDag-Erling Smørgrav 					size *= 1024ULL;
12404c814dfcSDag-Erling Smørgrav 					/* FALLTHROUGH */
12414c814dfcSDag-Erling Smørgrav 				case 'm':
12424c814dfcSDag-Erling Smørgrav 				case 'M':
12434c814dfcSDag-Erling Smørgrav 					size *= 1024ULL;
12444c814dfcSDag-Erling Smørgrav 					/* FALLTHROUGH */
12453233afaeSJohn W. De Boskey 				case 'k':
12463233afaeSJohn W. De Boskey 				case 'K':
12478d3105e8SWarner Losh 					size *= 1024ULL;
12483233afaeSJohn W. De Boskey 					break;
12493233afaeSJohn W. De Boskey 				case '\0':
12503233afaeSJohn W. De Boskey 					break;
12513233afaeSJohn W. De Boskey 				default:
125281807292SDag-Erling Smørgrav 					warnx("unknown multiplier suffix '%c' for partition %c (should be K, M, G or T)",
12534c814dfcSDag-Erling Smørgrav 					    part_size_type[i], i + 'a');
12543233afaeSJohn W. De Boskey 					break;
12553233afaeSJohn W. De Boskey 				}
12563233afaeSJohn W. De Boskey 				/* don't count %'s yet */
12573233afaeSJohn W. De Boskey 				if (part_size_type[i] != '%') {
12583233afaeSJohn W. De Boskey 					/*
12593233afaeSJohn W. De Boskey 					 * for all not in sectors, convert to
12603233afaeSJohn W. De Boskey 					 * sectors
12613233afaeSJohn W. De Boskey 					 */
12623233afaeSJohn W. De Boskey 					if (part_size_type[i] != '\0') {
12633233afaeSJohn W. De Boskey 						if (size % lp->d_secsize != 0)
126457dfbec5SPoul-Henning Kamp 							warnx("partition %c not an integer number of sectors",
12653233afaeSJohn W. De Boskey 							    i + 'a');
12663233afaeSJohn W. De Boskey 						size /= lp->d_secsize;
12673233afaeSJohn W. De Boskey 						pp->p_size = size;
12683233afaeSJohn W. De Boskey 					}
12693233afaeSJohn W. De Boskey 					/* else already in sectors */
12706b0ff5f5SPoul-Henning Kamp 					if (i != RAW_PART)
12713233afaeSJohn W. De Boskey 						total_size += size;
12723233afaeSJohn W. De Boskey 				}
12733233afaeSJohn W. De Boskey 			}
12743233afaeSJohn W. De Boskey 		}
12753233afaeSJohn W. De Boskey 	}
12763b89beb1SIan Dowse 
12773b89beb1SIan Dowse 	/* Find out the total free space, excluding the boot block area. */
12783b89beb1SIan Dowse 	base_offset = BBSIZE / secsize;
12793b89beb1SIan Dowse 	free_space = 0;
12803b89beb1SIan Dowse 	for (i = 0; i < lp->d_npartitions; i++) {
12813b89beb1SIan Dowse 		pp = &lp->d_partitions[i];
12823b89beb1SIan Dowse 		if (!part_set[i] || i == RAW_PART ||
12833b89beb1SIan Dowse 		    part_size_type[i] == '%' || part_size_type[i] == '*')
12843b89beb1SIan Dowse 			continue;
12853b89beb1SIan Dowse 		if (pp->p_offset > base_offset)
12863b89beb1SIan Dowse 			free_space += pp->p_offset - base_offset;
12873b89beb1SIan Dowse 		if (pp->p_offset + pp->p_size > base_offset)
12883b89beb1SIan Dowse 			base_offset = pp->p_offset + pp->p_size;
12893b89beb1SIan Dowse 	}
12903b89beb1SIan Dowse 	if (base_offset < lp->d_secperunit)
12913b89beb1SIan Dowse 		free_space += lp->d_secperunit - base_offset;
12923b89beb1SIan Dowse 
12933233afaeSJohn W. De Boskey 	/* handle % partitions - note %'s don't need to add up to 100! */
12943233afaeSJohn W. De Boskey 	if (total_percent != 0) {
12953233afaeSJohn W. De Boskey 		if (total_percent > 100) {
12966b0ff5f5SPoul-Henning Kamp 			fprintf(stderr,"total percentage %lu is greater than 100\n",
12973233afaeSJohn W. De Boskey 			    total_percent);
12983233afaeSJohn W. De Boskey 			errors++;
12993233afaeSJohn W. De Boskey 		}
13003233afaeSJohn W. De Boskey 
13013233afaeSJohn W. De Boskey 		if (free_space > 0) {
13023233afaeSJohn W. De Boskey 			for (i = 0; i < lp->d_npartitions; i++) {
13033233afaeSJohn W. De Boskey 				pp = &lp->d_partitions[i];
13043233afaeSJohn W. De Boskey 				if (part_set[i] && part_size_type[i] == '%') {
13053233afaeSJohn W. De Boskey 					/* careful of overflows! and integer roundoff */
13063233afaeSJohn W. De Boskey 					pp->p_size = ((double)pp->p_size/100) * free_space;
13073233afaeSJohn W. De Boskey 					total_size += pp->p_size;
13083233afaeSJohn W. De Boskey 
13093233afaeSJohn W. De Boskey 					/* FIX we can lose a sector or so due to roundoff per
13103233afaeSJohn W. De Boskey 					   partition.  A more complex algorithm could avoid that */
13113233afaeSJohn W. De Boskey 				}
13123233afaeSJohn W. De Boskey 			}
13133233afaeSJohn W. De Boskey 		} else {
13143233afaeSJohn W. De Boskey 			fprintf(stderr,
13156b0ff5f5SPoul-Henning Kamp 			    "%ld sectors available to give to '*' and '%%' partitions\n",
13163233afaeSJohn W. De Boskey 			    free_space);
13173233afaeSJohn W. De Boskey 			errors++;
13183233afaeSJohn W. De Boskey 			/* fix?  set all % partitions to size 0? */
13193233afaeSJohn W. De Boskey 		}
13203233afaeSJohn W. De Boskey 	}
13213233afaeSJohn W. De Boskey 	/* give anything remaining to the hog partition */
13223233afaeSJohn W. De Boskey 	if (hog_part != -1) {
13233b89beb1SIan Dowse 		/*
13243b89beb1SIan Dowse 		 * Find the range of offsets usable by '*' partitions around
13253b89beb1SIan Dowse 		 * the hog partition and how much space they need.
13263b89beb1SIan Dowse 		 */
13273b89beb1SIan Dowse 		needed = 0;
13283b89beb1SIan Dowse 		base_offset = BBSIZE / secsize;
13293b89beb1SIan Dowse 		for (i = hog_part - 1; i >= 0; i--) {
13303b89beb1SIan Dowse 			pp = &lp->d_partitions[i];
13313b89beb1SIan Dowse 			if (!part_set[i] || i == RAW_PART)
13323b89beb1SIan Dowse 				continue;
13333b89beb1SIan Dowse 			if (part_offset_type[i] == '*') {
13343b89beb1SIan Dowse 				needed += pp->p_size;
13353b89beb1SIan Dowse 				continue;
13363b89beb1SIan Dowse 			}
13373b89beb1SIan Dowse 			base_offset = pp->p_offset + pp->p_size;
13383b89beb1SIan Dowse 			break;
13393b89beb1SIan Dowse 		}
13403b89beb1SIan Dowse 		current_offset = lp->d_secperunit;
13413b89beb1SIan Dowse 		for (i = lp->d_npartitions - 1; i > hog_part; i--) {
13423b89beb1SIan Dowse 			pp = &lp->d_partitions[i];
13433b89beb1SIan Dowse 			if (!part_set[i] || i == RAW_PART)
13443b89beb1SIan Dowse 				continue;
13453b89beb1SIan Dowse 			if (part_offset_type[i] == '*') {
13463b89beb1SIan Dowse 				needed += pp->p_size;
13473b89beb1SIan Dowse 				continue;
13483b89beb1SIan Dowse 			}
13493b89beb1SIan Dowse 			current_offset = pp->p_offset;
13503b89beb1SIan Dowse 		}
13513b89beb1SIan Dowse 
13523b89beb1SIan Dowse 		if (current_offset - base_offset <= needed) {
13533b89beb1SIan Dowse 			fprintf(stderr, "Cannot find space for partition %c\n",
13543b89beb1SIan Dowse 			    hog_part + 'a');
13553b89beb1SIan Dowse 			fprintf(stderr,
13563b89beb1SIan Dowse 			    "Need more than %lu sectors between %lu and %lu\n",
13573b89beb1SIan Dowse 			    needed, base_offset, current_offset);
13583b89beb1SIan Dowse 			errors++;
13593b89beb1SIan Dowse 			lp->d_partitions[hog_part].p_size = 0;
13603b89beb1SIan Dowse 		} else {
13613b89beb1SIan Dowse 			lp->d_partitions[hog_part].p_size = current_offset -
13623b89beb1SIan Dowse 			    base_offset - needed;
13633b89beb1SIan Dowse 			total_size += lp->d_partitions[hog_part].p_size;
13643b89beb1SIan Dowse 		}
13653233afaeSJohn W. De Boskey 	}
13663233afaeSJohn W. De Boskey 
13673233afaeSJohn W. De Boskey 	/* Now set the offsets for each partition */
13683b89beb1SIan Dowse 	current_offset = BBSIZE / secsize; /* in sectors */
13693233afaeSJohn W. De Boskey 	seen_default_offset = 0;
13703233afaeSJohn W. De Boskey 	for (i = 0; i < lp->d_npartitions; i++) {
13713233afaeSJohn W. De Boskey 		part = 'a' + i;
13723233afaeSJohn W. De Boskey 		pp = &lp->d_partitions[i];
13733233afaeSJohn W. De Boskey 		if (part_set[i]) {
13743233afaeSJohn W. De Boskey 			if (part_offset_type[i] == '*') {
13756b0ff5f5SPoul-Henning Kamp 				if (i == RAW_PART) {
13763233afaeSJohn W. De Boskey 					pp->p_offset = 0;
13773233afaeSJohn W. De Boskey 				} else {
13783233afaeSJohn W. De Boskey 					pp->p_offset = current_offset;
13793233afaeSJohn W. De Boskey 					seen_default_offset = 1;
13803233afaeSJohn W. De Boskey 				}
13813233afaeSJohn W. De Boskey 			} else {
13823233afaeSJohn W. De Boskey 				/* allow them to be out of order for old-style tables */
13833233afaeSJohn W. De Boskey 				if (pp->p_offset < current_offset &&
1384f2f63257SGreg Lehey 				    seen_default_offset && i != RAW_PART &&
1385f2f63257SGreg Lehey 				    pp->p_fstype != FS_VINUM) {
13863233afaeSJohn W. De Boskey 					fprintf(stderr,
13876b0ff5f5SPoul-Henning Kamp "Offset %ld for partition %c overlaps previous partition which ends at %lu\n",
13886b0ff5f5SPoul-Henning Kamp 					    (long)pp->p_offset,i+'a',current_offset);
13893233afaeSJohn W. De Boskey 					fprintf(stderr,
13903233afaeSJohn W. De Boskey "Labels with any *'s for offset must be in ascending order by sector\n");
13913233afaeSJohn W. De Boskey 					errors++;
13923233afaeSJohn W. De Boskey 				} else if (pp->p_offset != current_offset &&
13936b0ff5f5SPoul-Henning Kamp 				    i != RAW_PART && seen_default_offset) {
13943233afaeSJohn W. De Boskey 					/*
13953233afaeSJohn W. De Boskey 					 * this may give unneeded warnings if
13963233afaeSJohn W. De Boskey 					 * partitions are out-of-order
13973233afaeSJohn W. De Boskey 					 */
139857dfbec5SPoul-Henning Kamp 					warnx(
13993233afaeSJohn W. De Boskey "Offset %ld for partition %c doesn't match expected value %ld",
14006b0ff5f5SPoul-Henning Kamp 					    (long)pp->p_offset, i + 'a', current_offset);
14013233afaeSJohn W. De Boskey 				}
14023233afaeSJohn W. De Boskey 			}
14036b0ff5f5SPoul-Henning Kamp 			if (i != RAW_PART)
14043233afaeSJohn W. De Boskey 				current_offset = pp->p_offset + pp->p_size;
14053233afaeSJohn W. De Boskey 		}
14063233afaeSJohn W. De Boskey 	}
14073233afaeSJohn W. De Boskey 
14088fae3551SRodney W. Grimes 	for (i = 0; i < lp->d_npartitions; i++) {
14098fae3551SRodney W. Grimes 		part = 'a' + i;
14108fae3551SRodney W. Grimes 		pp = &lp->d_partitions[i];
14118fae3551SRodney W. Grimes 		if (pp->p_size == 0 && pp->p_offset != 0)
141257dfbec5SPoul-Henning Kamp 			warnx("partition %c: size 0, but offset %lu",
14132a1deaaaSBruce Evans 			    part, (u_long)pp->p_offset);
14148fae3551SRodney W. Grimes #ifdef notdef
14158fae3551SRodney W. Grimes 		if (pp->p_size % lp->d_secpercyl)
141657dfbec5SPoul-Henning Kamp 			warnx("partition %c: size %% cylinder-size != 0",
14178fae3551SRodney W. Grimes 			    part);
14188fae3551SRodney W. Grimes 		if (pp->p_offset % lp->d_secpercyl)
141957dfbec5SPoul-Henning Kamp 			warnx("partition %c: offset %% cylinder-size != 0",
14208fae3551SRodney W. Grimes 			    part);
14218fae3551SRodney W. Grimes #endif
14228fae3551SRodney W. Grimes 		if (pp->p_offset > lp->d_secperunit) {
14238fae3551SRodney W. Grimes 			fprintf(stderr,
14248fae3551SRodney W. Grimes 			    "partition %c: offset past end of unit\n", part);
14258fae3551SRodney W. Grimes 			errors++;
14268fae3551SRodney W. Grimes 		}
14278fae3551SRodney W. Grimes 		if (pp->p_offset + pp->p_size > lp->d_secperunit) {
14288fae3551SRodney W. Grimes 			fprintf(stderr,
14298fae3551SRodney W. Grimes 			"partition %c: partition extends past end of unit\n",
14308fae3551SRodney W. Grimes 			    part);
14318fae3551SRodney W. Grimes 			errors++;
14328fae3551SRodney W. Grimes 		}
143357dfbec5SPoul-Henning Kamp 		if (i == RAW_PART) {
14343233afaeSJohn W. De Boskey 			if (pp->p_fstype != FS_UNUSED)
143557dfbec5SPoul-Henning Kamp 				warnx("partition %c is not marked as unused!",part);
14363233afaeSJohn W. De Boskey 			if (pp->p_offset != 0)
143757dfbec5SPoul-Henning Kamp 				warnx("partition %c doesn't start at 0!",part);
14383233afaeSJohn W. De Boskey 			if (pp->p_size != lp->d_secperunit)
143957dfbec5SPoul-Henning Kamp 				warnx("partition %c doesn't cover the whole unit!",part);
14403233afaeSJohn W. De Boskey 
14413233afaeSJohn W. De Boskey 			if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
14423233afaeSJohn W. De Boskey 			    (pp->p_size != lp->d_secperunit)) {
144357dfbec5SPoul-Henning Kamp 				warnx("An incorrect partition %c may cause problems for "
14443233afaeSJohn W. De Boskey 				    "standard system utilities",part);
14453233afaeSJohn W. De Boskey 			}
14463233afaeSJohn W. De Boskey 		}
14473233afaeSJohn W. De Boskey 
14483233afaeSJohn W. De Boskey 		/* check for overlaps */
14493233afaeSJohn W. De Boskey 		/* this will check for all possible overlaps once and only once */
14503233afaeSJohn W. De Boskey 		for (j = 0; j < i; j++) {
14513233afaeSJohn W. De Boskey 			pp2 = &lp->d_partitions[j];
1452f2f63257SGreg Lehey 			if (j != RAW_PART && i != RAW_PART &&
1453f2f63257SGreg Lehey 			    pp->p_fstype != FS_VINUM &&
1454f2f63257SGreg Lehey 			    pp2->p_fstype != FS_VINUM &&
1455f2f63257SGreg Lehey 			    part_set[i] && part_set[j]) {
14563233afaeSJohn W. De Boskey 				if (pp2->p_offset < pp->p_offset + pp->p_size &&
14573233afaeSJohn W. De Boskey 				    (pp2->p_offset + pp2->p_size > pp->p_offset ||
14583233afaeSJohn W. De Boskey 					pp2->p_offset >= pp->p_offset)) {
14593233afaeSJohn W. De Boskey 					fprintf(stderr,"partitions %c and %c overlap!\n",
14603233afaeSJohn W. De Boskey 					    j + 'a', i + 'a');
14613233afaeSJohn W. De Boskey 					errors++;
14623233afaeSJohn W. De Boskey 				}
14633233afaeSJohn W. De Boskey 			}
14643233afaeSJohn W. De Boskey 		}
14658fae3551SRodney W. Grimes 	}
1466d5718812SMarcel Moolenaar 	for (; i < lp->d_npartitions; i++) {
14678fae3551SRodney W. Grimes 		part = 'a' + i;
14688fae3551SRodney W. Grimes 		pp = &lp->d_partitions[i];
14698fae3551SRodney W. Grimes 		if (pp->p_size || pp->p_offset)
147057dfbec5SPoul-Henning Kamp 			warnx("unused partition %c: size %d offset %lu",
14712a1deaaaSBruce Evans 			    'a' + i, pp->p_size, (u_long)pp->p_offset);
14728fae3551SRodney W. Grimes 	}
14738fae3551SRodney W. Grimes 	return (errors);
14748fae3551SRodney W. Grimes }
14758fae3551SRodney W. Grimes 
14768fae3551SRodney W. Grimes /*
1477425bed3aSJoerg Wunsch  * When operating on a "virgin" disk, try getting an initial label
1478425bed3aSJoerg Wunsch  * from the associated device driver.  This might work for all device
1479425bed3aSJoerg Wunsch  * drivers that are able to fetch some initial device parameters
1480425bed3aSJoerg Wunsch  * without even having access to a (BSD) disklabel, like SCSI disks,
1481425bed3aSJoerg Wunsch  * most IDE drives, or vn devices.
1482425bed3aSJoerg Wunsch  *
1483425bed3aSJoerg Wunsch  * The device name must be given in its "canonical" form.
1484425bed3aSJoerg Wunsch  */
1485d2fe97c7SPoul-Henning Kamp static struct disklabel *
1486425bed3aSJoerg Wunsch getvirginlabel(void)
1487425bed3aSJoerg Wunsch {
1488c8223965SMark Murray 	static struct disklabel loclab;
1489b9d05a16SPoul-Henning Kamp 	struct partition *dp;
1490425bed3aSJoerg Wunsch 	int f;
149157dfbec5SPoul-Henning Kamp 	u_int u;
1492425bed3aSJoerg Wunsch 
1493d2fe97c7SPoul-Henning Kamp 	if ((f = open(specname, O_RDONLY)) == -1) {
1494d2fe97c7SPoul-Henning Kamp 		warn("cannot open %s", specname);
149543be698cSBruce Evans 		return (NULL);
1496425bed3aSJoerg Wunsch 	}
1497ff7d5162SJordan K. Hubbard 
14987747c959SLuigi Rizzo 	if (is_file)
14997747c959SLuigi Rizzo 		get_file_parms(f);
15007a4b0bb2SUlf Lilleengen 	else {
15017a4b0bb2SUlf Lilleengen 		mediasize = g_mediasize(f);
15027a4b0bb2SUlf Lilleengen 		secsize = g_sectorsize(f);
15037a4b0bb2SUlf Lilleengen 		if (secsize < 0 || mediasize < 0) {
1504425bed3aSJoerg Wunsch 			close (f);
150543be698cSBruce Evans 			return (NULL);
1506425bed3aSJoerg Wunsch 		}
15077a4b0bb2SUlf Lilleengen 	}
1508b9d05a16SPoul-Henning Kamp 	memset(&loclab, 0, sizeof loclab);
1509b9d05a16SPoul-Henning Kamp 	loclab.d_magic = DISKMAGIC;
1510b9d05a16SPoul-Henning Kamp 	loclab.d_magic2 = DISKMAGIC;
1511b9d05a16SPoul-Henning Kamp 	loclab.d_secsize = secsize;
1512b9d05a16SPoul-Henning Kamp 	loclab.d_secperunit = mediasize / secsize;
1513b9d05a16SPoul-Henning Kamp 
1514b9d05a16SPoul-Henning Kamp 	/*
15154b85a12fSUlrich Spörlein 	 * Nobody in these enlightened days uses the CHS geometry for
15164b85a12fSUlrich Spörlein 	 * anything, but nonetheless try to get it right.  If we fail
1517b9d05a16SPoul-Henning Kamp 	 * to get any good ideas from the device, construct something
1518b9d05a16SPoul-Henning Kamp 	 * which is IBM-PC friendly.
1519b9d05a16SPoul-Henning Kamp 	 */
1520b9d05a16SPoul-Henning Kamp 	if (ioctl(f, DIOCGFWSECTORS, &u) == 0)
1521b9d05a16SPoul-Henning Kamp 		loclab.d_nsectors = u;
1522b9d05a16SPoul-Henning Kamp 	else
1523b9d05a16SPoul-Henning Kamp 		loclab.d_nsectors = 63;
1524b9d05a16SPoul-Henning Kamp 	if (ioctl(f, DIOCGFWHEADS, &u) == 0)
1525b9d05a16SPoul-Henning Kamp 		loclab.d_ntracks = u;
1526b9d05a16SPoul-Henning Kamp 	else if (loclab.d_secperunit <= 63*1*1024)
1527b9d05a16SPoul-Henning Kamp 		loclab.d_ntracks = 1;
1528b9d05a16SPoul-Henning Kamp 	else if (loclab.d_secperunit <= 63*16*1024)
1529b9d05a16SPoul-Henning Kamp 		loclab.d_ntracks = 16;
1530b9d05a16SPoul-Henning Kamp 	else
1531b9d05a16SPoul-Henning Kamp 		loclab.d_ntracks = 255;
1532b9d05a16SPoul-Henning Kamp 	loclab.d_secpercyl = loclab.d_ntracks * loclab.d_nsectors;
1533b9d05a16SPoul-Henning Kamp 	loclab.d_ncylinders = loclab.d_secperunit / loclab.d_secpercyl;
1534d5718812SMarcel Moolenaar 	loclab.d_npartitions = DEFPARTITIONS;
1535b9d05a16SPoul-Henning Kamp 
1536b9d05a16SPoul-Henning Kamp 	/* Various (unneeded) compat stuff */
1537b9d05a16SPoul-Henning Kamp 	loclab.d_rpm = 3600;
1538b9d05a16SPoul-Henning Kamp 	loclab.d_bbsize = BBSIZE;
153957dfbec5SPoul-Henning Kamp 	loclab.d_interleave = 1;
1540b9d05a16SPoul-Henning Kamp 	strncpy(loclab.d_typename, "amnesiac",
1541b9d05a16SPoul-Henning Kamp 	    sizeof(loclab.d_typename));
1542b9d05a16SPoul-Henning Kamp 
1543b9d05a16SPoul-Henning Kamp 	dp = &loclab.d_partitions[RAW_PART];
1544b9d05a16SPoul-Henning Kamp 	dp->p_size = loclab.d_secperunit;
1545b9d05a16SPoul-Henning Kamp 	loclab.d_checksum = dkcksum(&loclab);
1546425bed3aSJoerg Wunsch 	close (f);
1547c8223965SMark Murray 	return (&loclab);
1548425bed3aSJoerg Wunsch }
1549425bed3aSJoerg Wunsch 
1550d2fe97c7SPoul-Henning Kamp static void
1551326c7cdaSWarner Losh usage(void)
15528fae3551SRodney W. Grimes {
1553bc33ea1aSRuslan Ermilov 
1554bc33ea1aSRuslan Ermilov 	fprintf(stderr,
1555bc33ea1aSRuslan 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",
15565daa806dSPoul-Henning Kamp 	"usage: bsdlabel disk",
1557bef2080aSPhilippe Charnier 	"\t\t(to read label)",
1558d2fe97c7SPoul-Henning Kamp 	"	bsdlabel -w [-n] [-m machine] disk [type]",
1559bef2080aSPhilippe Charnier 	"\t\t(to write label with existing boot program)",
15605daa806dSPoul-Henning Kamp 	"	bsdlabel -e [-n] [-m machine] disk",
1561bef2080aSPhilippe Charnier 	"\t\t(to edit label)",
15625daa806dSPoul-Henning Kamp 	"	bsdlabel -R [-n] [-m machine] disk protofile",
1563bef2080aSPhilippe Charnier 	"\t\t(to restore label with existing boot program)",
15642c60b668SPoul-Henning Kamp 	"	bsdlabel -B [-b boot] [-m machine] disk",
1565bef2080aSPhilippe Charnier 	"\t\t(to install boot program with existing on-disk label)",
1566d2fe97c7SPoul-Henning Kamp 	"	bsdlabel -w -B [-n] [-b boot] [-m machine] disk [type]",
1567bef2080aSPhilippe Charnier 	"\t\t(to write label and install boot program)",
15682c60b668SPoul-Henning Kamp 	"	bsdlabel -R -B [-n] [-b boot] [-m machine] disk protofile",
156980baf8ceSPoul-Henning Kamp 		"\t\t(to restore label and install boot program)"
157080baf8ceSPoul-Henning Kamp 	);
15718fae3551SRodney W. Grimes 	exit(1);
15728fae3551SRodney W. Grimes }
1573