xref: /freebsd/sbin/bsdlabel/bsdlabel.c (revision 3661658c76ce028a6dcdd54b00a822159a3ae6f3)
1df57947fSPedro F. Giffuni /*-
2df57947fSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
3df57947fSPedro F. Giffuni  *
4fd43aa1cSDavid E. O'Brien  * Copyright (c) 1994, 1995 Gordon W. Ross
5fd43aa1cSDavid E. O'Brien  * Copyright (c) 1994 Theo de Raadt
6fd43aa1cSDavid E. O'Brien  * All rights reserved.
78fae3551SRodney W. Grimes  * Copyright (c) 1987, 1993
88fae3551SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
98fae3551SRodney W. Grimes  *
108fae3551SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
118fae3551SRodney W. Grimes  * Symmetric Computer Systems.
128fae3551SRodney W. Grimes  *
138fae3551SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
148fae3551SRodney W. Grimes  * modification, are permitted provided that the following conditions
158fae3551SRodney W. Grimes  * are met:
168fae3551SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
178fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
188fae3551SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
198fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
208fae3551SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
218fae3551SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
228fae3551SRodney W. Grimes  *    must display the following acknowledgement:
238fae3551SRodney W. Grimes  *	This product includes software developed by the University of
248fae3551SRodney W. Grimes  *	California, Berkeley and its contributors.
25fd43aa1cSDavid E. O'Brien  *      This product includes software developed by Theo de Raadt.
268fae3551SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
278fae3551SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
288fae3551SRodney W. Grimes  *    without specific prior written permission.
298fae3551SRodney W. Grimes  *
308fae3551SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
318fae3551SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
328fae3551SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
338fae3551SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
348fae3551SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
358fae3551SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
368fae3551SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
378fae3551SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
388fae3551SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
398fae3551SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
408fae3551SRodney W. Grimes  * SUCH DAMAGE.
41fd43aa1cSDavid E. O'Brien  *
42fd43aa1cSDavid E. O'Brien  *	from: $NetBSD: disksubr.c,v 1.13 2000/12/17 22:39:18 pk $
438fae3551SRodney W. Grimes  */
448fae3551SRodney W. Grimes 
458fae3551SRodney W. Grimes #include <sys/param.h>
46dfbc3f0cSPoul-Henning Kamp #include <stdint.h>
478fae3551SRodney W. Grimes #include <sys/file.h>
488fae3551SRodney W. Grimes #include <sys/stat.h>
4961de51caSJoerg Wunsch #include <sys/wait.h>
50b9d05a16SPoul-Henning Kamp #include <sys/disk.h>
518fae3551SRodney W. Grimes #define DKTYPENAMES
52b35e6950SBosko Milekic #define FSTYPENAMES
533f4f82c0SEd Maste #define MAXPARTITIONS	8 /* XXX should be 20, but see PR276517 */
548fae3551SRodney W. Grimes #include <sys/disklabel.h>
55bc33ea1aSRuslan Ermilov 
568fae3551SRodney W. Grimes #include <unistd.h>
578fae3551SRodney W. Grimes #include <string.h>
588fae3551SRodney W. Grimes #include <stdio.h>
595daa806dSPoul-Henning Kamp #include <libgeom.h>
6061de51caSJoerg Wunsch #include <stdlib.h>
6161de51caSJoerg Wunsch #include <signal.h>
6261de51caSJoerg Wunsch #include <stdarg.h>
638fae3551SRodney W. Grimes #include <ctype.h>
64bef2080aSPhilippe Charnier #include <err.h>
6537736675SWarner Losh #include <errno.h>
66c8223965SMark Murray 
678fae3551SRodney W. Grimes #include "pathnames.h"
688fae3551SRodney W. Grimes 
69d2fe97c7SPoul-Henning Kamp static void	makelabel(const char *, struct disklabel *);
70fd4bbf84SAndrey V. Elsukov static int	geom_class_available(const char *);
71d2fe97c7SPoul-Henning Kamp static int	writelabel(void);
72d2fe97c7SPoul-Henning Kamp static int	readlabel(int flag);
73d2fe97c7SPoul-Henning Kamp static void	display(FILE *, const struct disklabel *);
74d2fe97c7SPoul-Henning Kamp static int	edit(void);
75d2fe97c7SPoul-Henning Kamp static int	editit(void);
76427823d5SPoul-Henning Kamp static void	fixlabel(struct disklabel *);
77d2fe97c7SPoul-Henning Kamp static char	*skip(char *);
78d2fe97c7SPoul-Henning Kamp static char	*word(char *);
79d2fe97c7SPoul-Henning Kamp static int	getasciilabel(FILE *, struct disklabel *);
80d2fe97c7SPoul-Henning Kamp static int	getasciipartspec(char *, struct disklabel *, int, int);
81d2fe97c7SPoul-Henning Kamp static int	checklabel(struct disklabel *);
82d2fe97c7SPoul-Henning Kamp static void	usage(void);
83d2fe97c7SPoul-Henning Kamp static struct disklabel *getvirginlabel(void);
8461de51caSJoerg Wunsch 
858fae3551SRodney W. Grimes #define	DEFEDITOR	_PATH_VI
86d5718812SMarcel Moolenaar #define	DEFPARTITIONS	8
878fae3551SRodney W. Grimes 
88d2fe97c7SPoul-Henning Kamp static char	*specname;
897a4b0bb2SUlf Lilleengen static char	*pname;
90d2fe97c7SPoul-Henning Kamp static char	tmpfil[] = PATH_TMPFILE;
918fae3551SRodney W. Grimes 
92d2fe97c7SPoul-Henning Kamp static struct	disklabel lab;
93d2fe97c7SPoul-Henning Kamp static u_char	bootarea[BBSIZE];
9457dfbec5SPoul-Henning Kamp static off_t	mediasize;
95a5871155SUlrich Spörlein static ssize_t	secsize;
96d2fe97c7SPoul-Henning Kamp static char	blank[] = "";
97d2fe97c7SPoul-Henning Kamp static char	unknown[] = "unknown";
988fae3551SRodney W. Grimes 
993233afaeSJohn W. De Boskey #define MAX_PART ('z')
1003233afaeSJohn W. De Boskey #define MAX_NUM_PARTS (1 + MAX_PART - 'a')
101d2fe97c7SPoul-Henning Kamp static char    part_size_type[MAX_NUM_PARTS];
102d2fe97c7SPoul-Henning Kamp static char    part_offset_type[MAX_NUM_PARTS];
103d2fe97c7SPoul-Henning Kamp static int     part_set[MAX_NUM_PARTS];
1043233afaeSJohn W. De Boskey 
105d2fe97c7SPoul-Henning Kamp static int	installboot;	/* non-zero if we should install a boot program */
10657dfbec5SPoul-Henning Kamp static int	allfields;	/* present all fields in edit */
107d2fe97c7SPoul-Henning Kamp static char const *xxboot;	/* primary boot */
1088fae3551SRodney W. Grimes 
109eb0ea23eSMarcel Moolenaar static uint32_t lba_offset;
1109ef521ecSPoul-Henning Kamp #ifndef LABELSECTOR
1119ef521ecSPoul-Henning Kamp #define LABELSECTOR -1
1129ef521ecSPoul-Henning Kamp #endif
1139ef521ecSPoul-Henning Kamp #ifndef LABELOFFSET
1149ef521ecSPoul-Henning Kamp #define LABELOFFSET -1
1159ef521ecSPoul-Henning Kamp #endif
116b2bb9f9bSPoul-Henning Kamp static int labelsoffset = LABELSECTOR;
117b2bb9f9bSPoul-Henning Kamp static int labeloffset = LABELOFFSET;
1187838fd0eSPoul-Henning Kamp static int bbsize = BBSIZE;
119c80f9755SPoul-Henning Kamp 
1201d23e44cSEd Schouten static enum {
121f080d33bSPoul-Henning Kamp 	UNSPEC, EDIT, READ, RESTORE, WRITE, WRITEBOOT
1228fae3551SRodney W. Grimes } op = UNSPEC;
1238fae3551SRodney W. Grimes 
124bc33ea1aSRuslan Ermilov 
125d2fe97c7SPoul-Henning Kamp static int	disable_write;   /* set to disable writing to disk label */
1267747c959SLuigi Rizzo static int	is_file;	/* work on a file (abs. pathname), "-f" opt. */
1278fae3551SRodney W. Grimes 
12861de51caSJoerg Wunsch int
main(int argc,char * argv[])129326c7cdaSWarner Losh main(int argc, char *argv[])
1308fae3551SRodney W. Grimes {
1318fae3551SRodney W. Grimes 	FILE *t;
1327a4b0bb2SUlf Lilleengen 	int ch, error, fd;
1337a4b0bb2SUlf Lilleengen 	const char *name;
1347a4b0bb2SUlf Lilleengen 
1357a4b0bb2SUlf Lilleengen 	error = 0;
1367a4b0bb2SUlf Lilleengen 	name = NULL;
1378fae3551SRodney W. Grimes 
138*3661658cSEd Maste 	fprintf(stderr,
139*3661658cSEd Maste 	    "WARNING: bsdlabel is deprecated and is not available in FreeBSD 15 or later.\n"
140*3661658cSEd Maste 	    "Please use gpart instead.\n\n");
141*3661658cSEd Maste 
142f72bbf97SMaxim Konovalov 	while ((ch = getopt(argc, argv, "ABb:efm:nRrw")) != -1)
1438fae3551SRodney W. Grimes 		switch (ch) {
14457dfbec5SPoul-Henning Kamp 			case 'A':
14557dfbec5SPoul-Henning Kamp 				allfields = 1;
14657dfbec5SPoul-Henning Kamp 				break;
1478fae3551SRodney W. Grimes 			case 'B':
1488fae3551SRodney W. Grimes 				++installboot;
1498fae3551SRodney W. Grimes 				break;
1508fae3551SRodney W. Grimes 			case 'b':
1518fae3551SRodney W. Grimes 				xxboot = optarg;
1528fae3551SRodney W. Grimes 				break;
1537747c959SLuigi Rizzo 			case 'f':
1547747c959SLuigi Rizzo 				is_file=1;
1557747c959SLuigi Rizzo 				break;
156bc33ea1aSRuslan Ermilov 			case 'm':
157246300f2SPoul-Henning Kamp 				if (!strcmp(optarg, "i386") ||
1582b375b4eSYoshihiro Takahashi 				    !strcmp(optarg, "amd64")) {
159b2bb9f9bSPoul-Henning Kamp 					labelsoffset = 1;
160b2bb9f9bSPoul-Henning Kamp 					labeloffset = 0;
1613ecb3802SPoul-Henning Kamp 					bbsize = 8192;
162d2fe97c7SPoul-Henning Kamp 				} else {
163d2fe97c7SPoul-Henning Kamp 					errx(1, "Unsupported architecture");
164c80f9755SPoul-Henning Kamp 				}
165bc33ea1aSRuslan Ermilov 				break;
1663233afaeSJohn W. De Boskey 			case 'n':
1673233afaeSJohn W. De Boskey 				disable_write = 1;
1683233afaeSJohn W. De Boskey 				break;
1698fae3551SRodney W. Grimes 			case 'R':
1708fae3551SRodney W. Grimes 				if (op != UNSPEC)
1718fae3551SRodney W. Grimes 					usage();
1728fae3551SRodney W. Grimes 				op = RESTORE;
1738fae3551SRodney W. Grimes 				break;
1748fae3551SRodney W. Grimes 			case 'e':
1758fae3551SRodney W. Grimes 				if (op != UNSPEC)
1768fae3551SRodney W. Grimes 					usage();
1778fae3551SRodney W. Grimes 				op = EDIT;
1788fae3551SRodney W. Grimes 				break;
1798fae3551SRodney W. Grimes 			case 'r':
180d2fe97c7SPoul-Henning Kamp 				/*
1814b85a12fSUlrich Spörlein 				 * We accept and ignore -r for compatibility with
1824b85a12fSUlrich Spörlein 				 * historical disklabel usage.
183d2fe97c7SPoul-Henning Kamp 				 */
1848fae3551SRodney W. Grimes 				break;
1858fae3551SRodney W. Grimes 			case 'w':
1868fae3551SRodney W. Grimes 				if (op != UNSPEC)
1878fae3551SRodney W. Grimes 					usage();
1888fae3551SRodney W. Grimes 				op = WRITE;
1898fae3551SRodney W. Grimes 				break;
1908fae3551SRodney W. Grimes 			case '?':
1918fae3551SRodney W. Grimes 			default:
1928fae3551SRodney W. Grimes 				usage();
1938fae3551SRodney W. Grimes 		}
1948fae3551SRodney W. Grimes 	argc -= optind;
1958fae3551SRodney W. Grimes 	argv += optind;
196d2fe97c7SPoul-Henning Kamp 
1978fae3551SRodney W. Grimes 	if (argc < 1)
1988fae3551SRodney W. Grimes 		usage();
1999ef521ecSPoul-Henning Kamp 	if (labelsoffset < 0 || labeloffset < 0)
2009ef521ecSPoul-Henning Kamp 		errx(1, "a -m <architecture> option must be specified");
2018fae3551SRodney W. Grimes 
2025daa806dSPoul-Henning Kamp 	/* Figure out the names of the thing we're working on */
2037747c959SLuigi Rizzo 	if (is_file) {
204ccdd2fceSRalf S. Engelschall 		specname = argv[0];
2055daa806dSPoul-Henning Kamp 	} else {
2067a4b0bb2SUlf Lilleengen 		specname = g_device_path(argv[0]);
2077a4b0bb2SUlf Lilleengen 		if (specname == NULL) {
2087a4b0bb2SUlf Lilleengen 			warn("unable to get correct path for %s", argv[0]);
2097a4b0bb2SUlf Lilleengen 			return(1);
2107a4b0bb2SUlf Lilleengen 		}
2117a4b0bb2SUlf Lilleengen 		fd = open(specname, O_RDONLY);
2127a4b0bb2SUlf Lilleengen 		if (fd < 0) {
2137a4b0bb2SUlf Lilleengen 			warn("error opening %s", specname);
2147a4b0bb2SUlf Lilleengen 			return(1);
2157a4b0bb2SUlf Lilleengen 		}
2167a4b0bb2SUlf Lilleengen 		pname = g_providername(fd);
2177a4b0bb2SUlf Lilleengen 		if (pname == NULL) {
218bd5add58SUlf Lilleengen 			warn("error getting providername for %s", specname);
2199d8ce078SUlf Lilleengen 			close(fd);
2207a4b0bb2SUlf Lilleengen 			return(1);
2217a4b0bb2SUlf Lilleengen 		}
2229d8ce078SUlf Lilleengen 		close(fd);
2238fae3551SRodney W. Grimes 	}
224d2fe97c7SPoul-Henning Kamp 
225d2fe97c7SPoul-Henning Kamp 	if (installboot && op == UNSPEC)
226d2fe97c7SPoul-Henning Kamp 		op = WRITEBOOT;
227d2fe97c7SPoul-Henning Kamp 	else if (op == UNSPEC)
228d2fe97c7SPoul-Henning Kamp 		op = READ;
2298fae3551SRodney W. Grimes 
2308fae3551SRodney W. Grimes 	switch(op) {
2318fae3551SRodney W. Grimes 
2323b3038a6SBill Fumerola 	case UNSPEC:
2333b3038a6SBill Fumerola 		break;
2343b3038a6SBill Fumerola 
2358fae3551SRodney W. Grimes 	case EDIT:
2368fae3551SRodney W. Grimes 		if (argc != 1)
2378fae3551SRodney W. Grimes 			usage();
238d2fe97c7SPoul-Henning Kamp 		readlabel(1);
239427823d5SPoul-Henning Kamp 		fixlabel(&lab);
240d2fe97c7SPoul-Henning Kamp 		error = edit();
2418fae3551SRodney W. Grimes 		break;
2428fae3551SRodney W. Grimes 
2438fae3551SRodney W. Grimes 	case READ:
2448fae3551SRodney W. Grimes 		if (argc != 1)
2458fae3551SRodney W. Grimes 			usage();
246d2fe97c7SPoul-Henning Kamp 		readlabel(1);
247d2fe97c7SPoul-Henning Kamp 		display(stdout, NULL);
248d2fe97c7SPoul-Henning Kamp 		error = checklabel(NULL);
2498fae3551SRodney W. Grimes 		break;
2508fae3551SRodney W. Grimes 
2518fae3551SRodney W. Grimes 	case RESTORE:
2528fae3551SRodney W. Grimes 		if (argc != 2)
2538fae3551SRodney W. Grimes 			usage();
2548fae3551SRodney W. Grimes 		if (!(t = fopen(argv[1], "r")))
2555daa806dSPoul-Henning Kamp 			err(4, "fopen %s", argv[1]);
256d2fe97c7SPoul-Henning Kamp 		readlabel(0);
2576cabb348SBruce Evans 		if (!getasciilabel(t, &lab))
2586cabb348SBruce Evans 			exit(1);
259d2fe97c7SPoul-Henning Kamp 		error = writelabel();
2608fae3551SRodney W. Grimes 		break;
2618fae3551SRodney W. Grimes 
2628fae3551SRodney W. Grimes 	case WRITE:
263d2fe97c7SPoul-Henning Kamp 		if (argc == 2)
264d2fe97c7SPoul-Henning Kamp 			name = argv[1];
265d2fe97c7SPoul-Henning Kamp 		else if (argc == 1)
266d2fe97c7SPoul-Henning Kamp 			name = "auto";
267d2fe97c7SPoul-Henning Kamp 		else
2688fae3551SRodney W. Grimes 			usage();
269d2fe97c7SPoul-Henning Kamp 		readlabel(0);
270d2fe97c7SPoul-Henning Kamp 		makelabel(name, &lab);
271427823d5SPoul-Henning Kamp 		fixlabel(&lab);
272d2fe97c7SPoul-Henning Kamp 		if (checklabel(NULL) == 0)
273d2fe97c7SPoul-Henning Kamp 			error = writelabel();
2748fae3551SRodney W. Grimes 		break;
2758fae3551SRodney W. Grimes 
2768fae3551SRodney W. Grimes 	case WRITEBOOT:
2778fae3551SRodney W. Grimes 
278d2fe97c7SPoul-Henning Kamp 		readlabel(1);
279427823d5SPoul-Henning Kamp 		fixlabel(&lab);
2808fae3551SRodney W. Grimes 		if (argc == 2)
281d2fe97c7SPoul-Henning Kamp 			makelabel(argv[1], &lab);
282d2fe97c7SPoul-Henning Kamp 		if (checklabel(NULL) == 0)
283d2fe97c7SPoul-Henning Kamp 			error = writelabel();
2848fae3551SRodney W. Grimes 		break;
2858fae3551SRodney W. Grimes 	}
2868fae3551SRodney W. Grimes 	exit(error);
2878fae3551SRodney W. Grimes }
2888fae3551SRodney W. Grimes 
289427823d5SPoul-Henning Kamp static void
fixlabel(struct disklabel * lp)290427823d5SPoul-Henning Kamp fixlabel(struct disklabel *lp)
291427823d5SPoul-Henning Kamp {
292427823d5SPoul-Henning Kamp 	struct partition *dp;
293427823d5SPoul-Henning Kamp 	int i;
294427823d5SPoul-Henning Kamp 
295d5718812SMarcel Moolenaar 	for (i = 0; i < lp->d_npartitions; i++) {
296427823d5SPoul-Henning Kamp 		if (i == RAW_PART)
297427823d5SPoul-Henning Kamp 			continue;
298427823d5SPoul-Henning Kamp 		if (lp->d_partitions[i].p_size)
299427823d5SPoul-Henning Kamp 			return;
300427823d5SPoul-Henning Kamp 	}
301427823d5SPoul-Henning Kamp 
302427823d5SPoul-Henning Kamp 	dp = &lp->d_partitions[0];
303427823d5SPoul-Henning Kamp 	dp->p_offset = BBSIZE / secsize;
304427823d5SPoul-Henning Kamp 	dp->p_size = lp->d_secperunit - dp->p_offset;
305427823d5SPoul-Henning Kamp }
306427823d5SPoul-Henning Kamp 
3078fae3551SRodney W. Grimes /*
308ef9ab0b3SRuslan Ermilov  * Construct a prototype disklabel from /etc/disktab.
3098fae3551SRodney W. Grimes  */
310d2fe97c7SPoul-Henning Kamp static void
makelabel(const char * type,struct disklabel * lp)311d2fe97c7SPoul-Henning Kamp makelabel(const char *type, struct disklabel *lp)
3128fae3551SRodney W. Grimes {
313326c7cdaSWarner Losh 	struct disklabel *dp;
314425bed3aSJoerg Wunsch 
315425bed3aSJoerg Wunsch 	if (strcmp(type, "auto") == 0)
316425bed3aSJoerg Wunsch 		dp = getvirginlabel();
317425bed3aSJoerg Wunsch 	else
3188fae3551SRodney W. Grimes 		dp = getdiskbyname(type);
3196bd343a9SPhilippe Charnier 	if (dp == NULL)
3206bd343a9SPhilippe Charnier 		errx(1, "%s: unknown disk type", type);
3218fae3551SRodney W. Grimes 	*lp = *dp;
3228fae3551SRodney W. Grimes 	bzero(lp->d_packname, sizeof(lp->d_packname));
3238fae3551SRodney W. Grimes }
3248fae3551SRodney W. Grimes 
325d2fe97c7SPoul-Henning Kamp static void
readboot(void)326d2fe97c7SPoul-Henning Kamp readboot(void)
327d2fe97c7SPoul-Henning Kamp {
328049a9793SJaakko Heinonen 	int fd;
329d2fe97c7SPoul-Henning Kamp 	struct stat st;
330d2fe97c7SPoul-Henning Kamp 
331d2fe97c7SPoul-Henning Kamp 	if (xxboot == NULL)
332d2fe97c7SPoul-Henning Kamp 		xxboot = "/boot/boot";
333d2fe97c7SPoul-Henning Kamp 	fd = open(xxboot, O_RDONLY);
334d2fe97c7SPoul-Henning Kamp 	if (fd < 0)
335d2fe97c7SPoul-Henning Kamp 		err(1, "cannot open %s", xxboot);
336d2fe97c7SPoul-Henning Kamp 	fstat(fd, &st);
3374c8dfc4aSUlrich Spörlein 	if (st.st_size <= BBSIZE) {
338049a9793SJaakko Heinonen 		if (read(fd, bootarea, st.st_size) != st.st_size)
339d2fe97c7SPoul-Henning Kamp 			err(1, "read error %s", xxboot);
340a2299ad8SKevin Lo 		close(fd);
341d2fe97c7SPoul-Henning Kamp 		return;
342d2fe97c7SPoul-Henning Kamp 	}
343d2fe97c7SPoul-Henning Kamp 	errx(1, "boot code %s is wrong size", xxboot);
344d2fe97c7SPoul-Henning Kamp }
345d2fe97c7SPoul-Henning Kamp 
346d2fe97c7SPoul-Henning Kamp static int
geom_class_available(const char * name)347fd4bbf84SAndrey V. Elsukov geom_class_available(const char *name)
348e45d37b2SJaakko Heinonen {
349e45d37b2SJaakko Heinonen 	struct gclass *class;
350e45d37b2SJaakko Heinonen 	struct gmesh mesh;
351e45d37b2SJaakko Heinonen 	int error;
352e45d37b2SJaakko Heinonen 
353e45d37b2SJaakko Heinonen 	error = geom_gettree(&mesh);
354e45d37b2SJaakko Heinonen 	if (error != 0)
355e45d37b2SJaakko Heinonen 		errc(1, error, "Cannot get GEOM tree");
356e45d37b2SJaakko Heinonen 
357e45d37b2SJaakko Heinonen 	LIST_FOREACH(class, &mesh.lg_class, lg_class) {
358fd4bbf84SAndrey V. Elsukov 		if (strcmp(class->lg_name, name) == 0) {
359e45d37b2SJaakko Heinonen 			geom_deletetree(&mesh);
360e45d37b2SJaakko Heinonen 			return (1);
361e45d37b2SJaakko Heinonen 		}
362e45d37b2SJaakko Heinonen 	}
363e45d37b2SJaakko Heinonen 
364e45d37b2SJaakko Heinonen 	geom_deletetree(&mesh);
365e45d37b2SJaakko Heinonen 
366e45d37b2SJaakko Heinonen 	return (0);
367e45d37b2SJaakko Heinonen }
368e45d37b2SJaakko Heinonen 
369e45d37b2SJaakko Heinonen static int
writelabel(void)370d2fe97c7SPoul-Henning Kamp writelabel(void)
3718fae3551SRodney W. Grimes {
372e45d37b2SJaakko Heinonen 	int i, fd, serrno;
373d2fe97c7SPoul-Henning Kamp 	struct disklabel *lp = &lab;
374130cd73aSDoug Rabson 
3753233afaeSJohn W. De Boskey 	if (disable_write) {
3764b85a12fSUlrich Spörlein 		warnx("write to disk label suppressed - label was as follows:");
377d2fe97c7SPoul-Henning Kamp 		display(stdout, NULL);
3783233afaeSJohn W. De Boskey 		return (0);
37980baf8ceSPoul-Henning Kamp 	}
38080baf8ceSPoul-Henning Kamp 
3818fae3551SRodney W. Grimes 	lp->d_magic = DISKMAGIC;
3828fae3551SRodney W. Grimes 	lp->d_magic2 = DISKMAGIC;
3838fae3551SRodney W. Grimes 	lp->d_checksum = 0;
3848fae3551SRodney W. Grimes 	lp->d_checksum = dkcksum(lp);
385d2fe97c7SPoul-Henning Kamp 	if (installboot)
386d2fe97c7SPoul-Henning Kamp 		readboot();
3875d853216SPoul-Henning Kamp 	for (i = 0; i < lab.d_npartitions; i++)
3885d853216SPoul-Henning Kamp 		if (lab.d_partitions[i].p_size)
389eb0ea23eSMarcel Moolenaar 			lab.d_partitions[i].p_offset += lba_offset;
3903fd209e4SMaxim Sobolev 	bsd_disklabel_le_enc(bootarea + labeloffset + labelsoffset * lab.d_secsize,
391b2bb9f9bSPoul-Henning Kamp 	    lp);
3925daa806dSPoul-Henning Kamp 
3935daa806dSPoul-Henning Kamp 	fd = open(specname, O_RDWR);
3945daa806dSPoul-Henning Kamp 	if (fd < 0) {
3957747c959SLuigi Rizzo 		if (is_file) {
3967747c959SLuigi Rizzo 			warn("cannot open file %s for writing label", specname);
3977747c959SLuigi Rizzo 			return(1);
398e45d37b2SJaakko Heinonen 		} else
399e45d37b2SJaakko Heinonen 			serrno = errno;
400e45d37b2SJaakko Heinonen 
401fd4bbf84SAndrey V. Elsukov 		if (geom_class_available("PART") != 0) {
402b06ce685SAndrey V. Elsukov 			/*
403b06ce685SAndrey V. Elsukov 			 * Since we weren't able open provider for
404fd4bbf84SAndrey V. Elsukov 			 * writing, then recommend user to use gpart(8).
405fd4bbf84SAndrey V. Elsukov 			 */
406fd4bbf84SAndrey V. Elsukov 			warnc(serrno,
407fd4bbf84SAndrey V. Elsukov 			    "cannot open provider %s for writing label",
408fd4bbf84SAndrey V. Elsukov 			    specname);
409fd4bbf84SAndrey V. Elsukov 			warnx("Try to use gpart(8).");
410fd4bbf84SAndrey V. Elsukov 			return (1);
411fd4bbf84SAndrey V. Elsukov 		}
412fd4bbf84SAndrey V. Elsukov 
413e45d37b2SJaakko Heinonen 		warnc(serrno, "%s", specname);
414e45d37b2SJaakko Heinonen 		return (1);
4155daa806dSPoul-Henning Kamp 	} else {
416d2fe97c7SPoul-Henning Kamp 		if (write(fd, bootarea, bbsize) != bbsize) {
4175daa806dSPoul-Henning Kamp 			warn("write %s", specname);
4185daa806dSPoul-Henning Kamp 			close (fd);
4198fae3551SRodney W. Grimes 			return (1);
4208fae3551SRodney W. Grimes 		}
4215daa806dSPoul-Henning Kamp 		close (fd);
4225daa806dSPoul-Henning Kamp 	}
4238fae3551SRodney W. Grimes 	return (0);
4248fae3551SRodney W. Grimes }
4258fae3551SRodney W. Grimes 
4267747c959SLuigi Rizzo static void
get_file_parms(int f)4277747c959SLuigi Rizzo get_file_parms(int f)
4287747c959SLuigi Rizzo {
4297747c959SLuigi Rizzo 	int i;
4307747c959SLuigi Rizzo 	struct stat sb;
4317747c959SLuigi Rizzo 
4327747c959SLuigi Rizzo 	if (fstat(f, &sb) != 0)
4337747c959SLuigi Rizzo 		err(4, "fstat failed");
4347747c959SLuigi Rizzo 	i = sb.st_mode & S_IFMT;
4357747c959SLuigi Rizzo 	if (i != S_IFREG && i != S_IFLNK)
4367747c959SLuigi Rizzo 		errx(4, "%s is not a valid file or link", specname);
4377747c959SLuigi Rizzo 	secsize = DEV_BSIZE;
4387747c959SLuigi Rizzo 	mediasize = sb.st_size;
4397747c959SLuigi Rizzo }
4407747c959SLuigi Rizzo 
4418fae3551SRodney W. Grimes /*
4428fae3551SRodney W. Grimes  * Fetch disklabel for disk.
4438fae3551SRodney W. Grimes  */
444d2fe97c7SPoul-Henning Kamp static int
readlabel(int flag)445d2fe97c7SPoul-Henning Kamp readlabel(int flag)
4468fae3551SRodney W. Grimes {
447049a9793SJaakko Heinonen 	ssize_t nbytes;
448eb0ea23eSMarcel Moolenaar 	uint32_t lba;
4495d853216SPoul-Henning Kamp 	int f, i;
4505daa806dSPoul-Henning Kamp 	int error;
4518fae3551SRodney W. Grimes 
4525daa806dSPoul-Henning Kamp 	f = open(specname, O_RDONLY);
4535daa806dSPoul-Henning Kamp 	if (f < 0)
4541161d420SRebecca Cran 		err(1, "%s", specname);
4557747c959SLuigi Rizzo 	if (is_file)
4567747c959SLuigi Rizzo 		get_file_parms(f);
4577a4b0bb2SUlf Lilleengen 	else {
4587a4b0bb2SUlf Lilleengen 		mediasize = g_mediasize(f);
4597a4b0bb2SUlf Lilleengen 		secsize = g_sectorsize(f);
4607a4b0bb2SUlf Lilleengen 		if (secsize < 0 || mediasize < 0)
461b2bb9f9bSPoul-Henning Kamp 			err(4, "cannot get disk geometry");
462b2bb9f9bSPoul-Henning Kamp 	}
46386e0bd0dSBrooks Davis 	if (mediasize > (off_t)0xffffffff * secsize)
46486e0bd0dSBrooks Davis 		errx(1,
46586e0bd0dSBrooks Davis 		    "disks with more than 2^32-1 sectors are not supported");
466c80f9755SPoul-Henning Kamp 	(void)lseek(f, (off_t)0, SEEK_SET);
467049a9793SJaakko Heinonen 	nbytes = read(f, bootarea, BBSIZE);
468049a9793SJaakko Heinonen 	if (nbytes == -1)
4695daa806dSPoul-Henning Kamp 		err(4, "%s read", specname);
470049a9793SJaakko Heinonen 	if (nbytes != BBSIZE)
471049a9793SJaakko Heinonen 		errx(4, "couldn't read %d bytes from %s", BBSIZE, specname);
4725daa806dSPoul-Henning Kamp 	close (f);
473b2bb9f9bSPoul-Henning Kamp 	error = bsd_disklabel_le_dec(
474b2bb9f9bSPoul-Henning Kamp 	    bootarea + (labeloffset + labelsoffset * secsize),
475b2bb9f9bSPoul-Henning Kamp 	    &lab, MAXPARTITIONS);
476d2fe97c7SPoul-Henning Kamp 	if (flag && error)
477d2fe97c7SPoul-Henning Kamp 		errx(1, "%s: no valid label found", specname);
4785d853216SPoul-Henning Kamp 
4797a4b0bb2SUlf Lilleengen 	if (is_file)
4807a4b0bb2SUlf Lilleengen 		return(0);
481eb0ea23eSMarcel Moolenaar 
482eb0ea23eSMarcel Moolenaar 	/*
483eb0ea23eSMarcel Moolenaar 	 * Compensate for absolute block addressing by finding the
484eb0ea23eSMarcel Moolenaar 	 * smallest partition offset and if the offset of the 'c'
485eb0ea23eSMarcel Moolenaar 	 * partition is equal to that, subtract it from all offsets.
486eb0ea23eSMarcel Moolenaar 	 */
487eb0ea23eSMarcel Moolenaar 	lba = ~0;
488eb0ea23eSMarcel Moolenaar 	for (i = 0; i < lab.d_npartitions; i++) {
4895d853216SPoul-Henning Kamp 		if (lab.d_partitions[i].p_size)
490eb0ea23eSMarcel Moolenaar 			lba = MIN(lba, lab.d_partitions[i].p_offset);
491eb0ea23eSMarcel Moolenaar 	}
492eb0ea23eSMarcel Moolenaar 	if (lba != 0 && lab.d_partitions[RAW_PART].p_offset == lba) {
493eb0ea23eSMarcel Moolenaar 		for (i = 0; i < lab.d_npartitions; i++) {
494eb0ea23eSMarcel Moolenaar 			if (lab.d_partitions[i].p_size)
495eb0ea23eSMarcel Moolenaar 				lab.d_partitions[i].p_offset -= lba;
496eb0ea23eSMarcel Moolenaar 		}
497eb0ea23eSMarcel Moolenaar 		/*
498eb0ea23eSMarcel Moolenaar 		 * Save the offset so that we can write the label
499eb0ea23eSMarcel Moolenaar 		 * back with absolute block addresses.
500eb0ea23eSMarcel Moolenaar 		 */
501eb0ea23eSMarcel Moolenaar 		lba_offset = lba;
502eb0ea23eSMarcel Moolenaar 	}
503d2fe97c7SPoul-Henning Kamp 	return (error);
5048fae3551SRodney W. Grimes }
5058fae3551SRodney W. Grimes 
5068fae3551SRodney W. Grimes 
507d2fe97c7SPoul-Henning Kamp static void
display(FILE * f,const struct disklabel * lp)508326c7cdaSWarner Losh display(FILE *f, const struct disklabel *lp)
5098fae3551SRodney W. Grimes {
510326c7cdaSWarner Losh 	int i, j;
511326c7cdaSWarner Losh 	const struct partition *pp;
5128fae3551SRodney W. Grimes 
513d2fe97c7SPoul-Henning Kamp 	if (lp == NULL)
514d2fe97c7SPoul-Henning Kamp 		lp = &lab;
515d2fe97c7SPoul-Henning Kamp 
5168fae3551SRodney W. Grimes 	fprintf(f, "# %s:\n", specname);
51757dfbec5SPoul-Henning Kamp 	if (allfields) {
518484c7804SJulian Elischer 		if (lp->d_type < DKMAXTYPES)
5198fae3551SRodney W. Grimes 			fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
5208fae3551SRodney W. Grimes 		else
5212a1deaaaSBruce Evans 			fprintf(f, "type: %u\n", lp->d_type);
52261de51caSJoerg Wunsch 		fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
52361de51caSJoerg Wunsch 			lp->d_typename);
52461de51caSJoerg Wunsch 		fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
52561de51caSJoerg Wunsch 			lp->d_packname);
5268fae3551SRodney W. Grimes 		fprintf(f, "flags:");
5278fae3551SRodney W. Grimes 		if (lp->d_flags & D_REMOVABLE)
5288fae3551SRodney W. Grimes 			fprintf(f, " removeable");
5298fae3551SRodney W. Grimes 		if (lp->d_flags & D_ECC)
5308fae3551SRodney W. Grimes 			fprintf(f, " ecc");
5318fae3551SRodney W. Grimes 		if (lp->d_flags & D_BADSECT)
5328fae3551SRodney W. Grimes 			fprintf(f, " badsect");
5338fae3551SRodney W. Grimes 		fprintf(f, "\n");
5342a1deaaaSBruce Evans 		fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
5352a1deaaaSBruce Evans 		fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
5362a1deaaaSBruce Evans 		fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
5372a1deaaaSBruce Evans 		fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
5382a1deaaaSBruce Evans 		fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
5392a1deaaaSBruce Evans 		fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
5402a1deaaaSBruce Evans 		fprintf(f, "rpm: %u\n", lp->d_rpm);
5412a1deaaaSBruce Evans 		fprintf(f, "interleave: %u\n", lp->d_interleave);
5422a1deaaaSBruce Evans 		fprintf(f, "trackskew: %u\n", lp->d_trackskew);
5432a1deaaaSBruce Evans 		fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
5442a1deaaaSBruce Evans 		fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
5452a1deaaaSBruce Evans 		    (u_long)lp->d_headswitch);
54661de51caSJoerg Wunsch 		fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
5472a1deaaaSBruce Evans 		    (u_long)lp->d_trkseek);
5488fae3551SRodney W. Grimes 		fprintf(f, "drivedata: ");
5498fae3551SRodney W. Grimes 		for (i = NDDATA - 1; i >= 0; i--)
5508fae3551SRodney W. Grimes 			if (lp->d_drivedata[i])
5518fae3551SRodney W. Grimes 				break;
5528fae3551SRodney W. Grimes 		if (i < 0)
5538fae3551SRodney W. Grimes 			i = 0;
5548fae3551SRodney W. Grimes 		for (j = 0; j <= i; j++)
5552a1deaaaSBruce Evans 			fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
55657dfbec5SPoul-Henning Kamp 		fprintf(f, "\n\n");
55757dfbec5SPoul-Henning Kamp 	}
55857dfbec5SPoul-Henning Kamp 	fprintf(f, "%u partitions:\n", lp->d_npartitions);
5598fae3551SRodney W. Grimes 	fprintf(f,
560ca4693edSJustin T. Gibbs 	    "#          size     offset    fstype   [fsize bsize bps/cpg]\n");
5618fae3551SRodney W. Grimes 	pp = lp->d_partitions;
5628fae3551SRodney W. Grimes 	for (i = 0; i < lp->d_npartitions; i++, pp++) {
5638fae3551SRodney W. Grimes 		if (pp->p_size) {
564a5871155SUlrich Spörlein 			fprintf(f, "  %c: %10lu %10lu  ", 'a' + i,
5652a1deaaaSBruce Evans 			   (u_long)pp->p_size, (u_long)pp->p_offset);
566484c7804SJulian Elischer 			if (pp->p_fstype < FSMAXTYPES)
5678fae3551SRodney W. Grimes 				fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
5688fae3551SRodney W. Grimes 			else
5698fae3551SRodney W. Grimes 				fprintf(f, "%8d", pp->p_fstype);
5708fae3551SRodney W. Grimes 			switch (pp->p_fstype) {
5718fae3551SRodney W. Grimes 
5728fae3551SRodney W. Grimes 			case FS_UNUSED:				/* XXX */
573a5871155SUlrich Spörlein 				fprintf(f, "    %5lu %5lu %2s",
5742a1deaaaSBruce Evans 				    (u_long)pp->p_fsize,
5752a1deaaaSBruce Evans 				    (u_long)(pp->p_fsize * pp->p_frag), "");
5768fae3551SRodney W. Grimes 				break;
5778fae3551SRodney W. Grimes 
5788fae3551SRodney W. Grimes 			case FS_BSDFFS:
5792a1deaaaSBruce Evans 				fprintf(f, "    %5lu %5lu %5u",
5802a1deaaaSBruce Evans 				    (u_long)pp->p_fsize,
5812a1deaaaSBruce Evans 				    (u_long)(pp->p_fsize * pp->p_frag),
5828fae3551SRodney W. Grimes 				    pp->p_cpg);
5838fae3551SRodney W. Grimes 				break;
5848fae3551SRodney W. Grimes 
585ca4693edSJustin T. Gibbs 			case FS_BSDLFS:
5862a1deaaaSBruce Evans 				fprintf(f, "    %5lu %5lu %5d",
5872a1deaaaSBruce Evans 				    (u_long)pp->p_fsize,
5882a1deaaaSBruce Evans 				    (u_long)(pp->p_fsize * pp->p_frag),
589ca4693edSJustin T. Gibbs 				    pp->p_cpg);
590ca4693edSJustin T. Gibbs 				break;
591ca4693edSJustin T. Gibbs 
5928fae3551SRodney W. Grimes 			default:
5938fae3551SRodney W. Grimes 				fprintf(f, "%20.20s", "");
5948fae3551SRodney W. Grimes 				break;
5958fae3551SRodney W. Grimes 			}
59657dfbec5SPoul-Henning Kamp 			if (i == RAW_PART) {
59757dfbec5SPoul-Henning Kamp 				fprintf(f, "  # \"raw\" part, don't edit");
59857dfbec5SPoul-Henning Kamp 			}
599d2fe97c7SPoul-Henning Kamp 			fprintf(f, "\n");
6008fae3551SRodney W. Grimes 		}
6018fae3551SRodney W. Grimes 	}
6028fae3551SRodney W. Grimes 	fflush(f);
6038fae3551SRodney W. Grimes }
6048fae3551SRodney W. Grimes 
605d2fe97c7SPoul-Henning Kamp static int
edit(void)606d2fe97c7SPoul-Henning Kamp edit(void)
6078fae3551SRodney W. Grimes {
608326c7cdaSWarner Losh 	int c, fd;
6098fae3551SRodney W. Grimes 	struct disklabel label;
610722ceb3fSWarner Losh 	FILE *fp;
6118fae3551SRodney W. Grimes 
612722ceb3fSWarner Losh 	if ((fd = mkstemp(tmpfil)) == -1 ||
613722ceb3fSWarner Losh 	    (fp = fdopen(fd, "w")) == NULL) {
6146bd343a9SPhilippe Charnier 		warnx("can't create %s", tmpfil);
6158fae3551SRodney W. Grimes 		return (1);
6168fae3551SRodney W. Grimes 	}
617d2fe97c7SPoul-Henning Kamp 	display(fp, NULL);
618722ceb3fSWarner Losh 	fclose(fp);
6198fae3551SRodney W. Grimes 	for (;;) {
6208fae3551SRodney W. Grimes 		if (!editit())
6218fae3551SRodney W. Grimes 			break;
622722ceb3fSWarner Losh 		fp = fopen(tmpfil, "r");
623722ceb3fSWarner Losh 		if (fp == NULL) {
6246bd343a9SPhilippe Charnier 			warnx("can't reopen %s for reading", tmpfil);
6258fae3551SRodney W. Grimes 			break;
6268fae3551SRodney W. Grimes 		}
6278fae3551SRodney W. Grimes 		bzero((char *)&label, sizeof(label));
628d2fe97c7SPoul-Henning Kamp 		c = getasciilabel(fp, &label);
629722ceb3fSWarner Losh 		fclose(fp);
630d2fe97c7SPoul-Henning Kamp 		if (c) {
631d2fe97c7SPoul-Henning Kamp 			lab = label;
632d2fe97c7SPoul-Henning Kamp 			if (writelabel() == 0) {
6338fae3551SRodney W. Grimes 				(void) unlink(tmpfil);
6348fae3551SRodney W. Grimes 				return (0);
6358fae3551SRodney W. Grimes 			}
6368fae3551SRodney W. Grimes 		}
637d2fe97c7SPoul-Henning Kamp 		printf("re-edit the label? [y]: ");
638d2fe97c7SPoul-Henning Kamp 		fflush(stdout);
6398fae3551SRodney W. Grimes 		c = getchar();
6408fae3551SRodney W. Grimes 		if (c != EOF && c != (int)'\n')
6418fae3551SRodney W. Grimes 			while (getchar() != (int)'\n')
6428fae3551SRodney W. Grimes 				;
6438fae3551SRodney W. Grimes 		if  (c == (int)'n')
6448fae3551SRodney W. Grimes 			break;
6458fae3551SRodney W. Grimes 	}
6468fae3551SRodney W. Grimes 	(void) unlink(tmpfil);
6478fae3551SRodney W. Grimes 	return (1);
6488fae3551SRodney W. Grimes }
6498fae3551SRodney W. Grimes 
650d2fe97c7SPoul-Henning Kamp static int
editit(void)651326c7cdaSWarner Losh editit(void)
6528fae3551SRodney W. Grimes {
653326c7cdaSWarner Losh 	int pid, xpid;
654c8223965SMark Murray 	int locstat, omask;
655c8223965SMark Murray 	const char *ed;
65673d6722dSKevin Lo 	uid_t uid;
65773d6722dSKevin Lo 	gid_t gid;
6588fae3551SRodney W. Grimes 
6598fae3551SRodney W. Grimes 	omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
6608fae3551SRodney W. Grimes 	while ((pid = fork()) < 0) {
6618fae3551SRodney W. Grimes 		if (errno == EPROCLIM) {
6626bd343a9SPhilippe Charnier 			warnx("you have too many processes");
6638fae3551SRodney W. Grimes 			return(0);
6648fae3551SRodney W. Grimes 		}
6658fae3551SRodney W. Grimes 		if (errno != EAGAIN) {
6666bd343a9SPhilippe Charnier 			warn("fork");
6678fae3551SRodney W. Grimes 			return(0);
6688fae3551SRodney W. Grimes 		}
6698fae3551SRodney W. Grimes 		sleep(1);
6708fae3551SRodney W. Grimes 	}
6718fae3551SRodney W. Grimes 	if (pid == 0) {
6728fae3551SRodney W. Grimes 		sigsetmask(omask);
67373d6722dSKevin Lo 		gid = getgid();
67473d6722dSKevin Lo 		if (setresgid(gid, gid, gid) == -1)
67573d6722dSKevin Lo 			err(1, "setresgid");
67673d6722dSKevin Lo 		uid = getuid();
67773d6722dSKevin Lo 		if (setresuid(uid, uid, uid) == -1)
67873d6722dSKevin Lo 			err(1, "setresuid");
6798fae3551SRodney W. Grimes 		if ((ed = getenv("EDITOR")) == (char *)0)
6808fae3551SRodney W. Grimes 			ed = DEFEDITOR;
6817bc6d015SBrian Somers 		execlp(ed, ed, tmpfil, (char *)0);
6826bd343a9SPhilippe Charnier 		err(1, "%s", ed);
6838fae3551SRodney W. Grimes 	}
684c8223965SMark Murray 	while ((xpid = wait(&locstat)) >= 0)
6858fae3551SRodney W. Grimes 		if (xpid == pid)
6868fae3551SRodney W. Grimes 			break;
6878fae3551SRodney W. Grimes 	sigsetmask(omask);
688c8223965SMark Murray 	return(!locstat);
6898fae3551SRodney W. Grimes }
6908fae3551SRodney W. Grimes 
691d2fe97c7SPoul-Henning Kamp static char *
skip(char * cp)692326c7cdaSWarner Losh skip(char *cp)
6938fae3551SRodney W. Grimes {
6948fae3551SRodney W. Grimes 
6958fae3551SRodney W. Grimes 	while (*cp != '\0' && isspace(*cp))
6968fae3551SRodney W. Grimes 		cp++;
6978fae3551SRodney W. Grimes 	if (*cp == '\0' || *cp == '#')
698326c7cdaSWarner Losh 		return (NULL);
6998fae3551SRodney W. Grimes 	return (cp);
7008fae3551SRodney W. Grimes }
7018fae3551SRodney W. Grimes 
702d2fe97c7SPoul-Henning Kamp static char *
word(char * cp)703326c7cdaSWarner Losh word(char *cp)
7048fae3551SRodney W. Grimes {
705326c7cdaSWarner Losh 	char c;
7068fae3551SRodney W. Grimes 
7078fae3551SRodney W. Grimes 	while (*cp != '\0' && !isspace(*cp) && *cp != '#')
7088fae3551SRodney W. Grimes 		cp++;
7098fae3551SRodney W. Grimes 	if ((c = *cp) != '\0') {
7108fae3551SRodney W. Grimes 		*cp++ = '\0';
7118fae3551SRodney W. Grimes 		if (c != '#')
7128fae3551SRodney W. Grimes 			return (skip(cp));
7138fae3551SRodney W. Grimes 	}
714326c7cdaSWarner Losh 	return (NULL);
7158fae3551SRodney W. Grimes }
7168fae3551SRodney W. Grimes 
7178fae3551SRodney W. Grimes /*
7188fae3551SRodney W. Grimes  * Read an ascii label in from fd f,
7198fae3551SRodney W. Grimes  * in the same format as that put out by display(),
7208fae3551SRodney W. Grimes  * and fill in lp.
7218fae3551SRodney W. Grimes  */
722d2fe97c7SPoul-Henning Kamp static int
getasciilabel(FILE * f,struct disklabel * lp)723326c7cdaSWarner Losh getasciilabel(FILE *f, struct disklabel *lp)
7248fae3551SRodney W. Grimes {
725fb26ece7SJaakko Heinonen 	char *cp, *endp;
7266b0ff5f5SPoul-Henning Kamp 	const char **cpp;
727484c7804SJulian Elischer 	u_int part;
7286b0ff5f5SPoul-Henning Kamp 	char *tp, line[BUFSIZ];
729484c7804SJulian Elischer 	u_long v;
730484c7804SJulian Elischer 	int lineno = 0, errors = 0;
731326c7cdaSWarner Losh 	int i;
7328fae3551SRodney W. Grimes 
7338970f1a4SPoul-Henning Kamp 	makelabel("auto", lp);
734b0459c58SDag-Erling Smørgrav 	bzero(&part_set, sizeof(part_set));
735b0459c58SDag-Erling Smørgrav 	bzero(&part_size_type, sizeof(part_size_type));
736b0459c58SDag-Erling Smørgrav 	bzero(&part_offset_type, sizeof(part_offset_type));
7378fae3551SRodney W. Grimes 	lp->d_bbsize = BBSIZE;				/* XXX */
73877068a7fSPoul-Henning Kamp 	lp->d_sbsize = 0;				/* XXX */
7398fae3551SRodney W. Grimes 	while (fgets(line, sizeof(line) - 1, f)) {
7408fae3551SRodney W. Grimes 		lineno++;
741fb6a3593SMarcelo Araujo 		if ((cp = strchr(line,'\n')) != NULL)
7428fae3551SRodney W. Grimes 			*cp = '\0';
7438fae3551SRodney W. Grimes 		cp = skip(line);
7448fae3551SRodney W. Grimes 		if (cp == NULL)
7458fae3551SRodney W. Grimes 			continue;
746b3608ae1SEd Schouten 		tp = strchr(cp, ':');
7478fae3551SRodney W. Grimes 		if (tp == NULL) {
7488fae3551SRodney W. Grimes 			fprintf(stderr, "line %d: syntax error\n", lineno);
7498fae3551SRodney W. Grimes 			errors++;
7508fae3551SRodney W. Grimes 			continue;
7518fae3551SRodney W. Grimes 		}
7528fae3551SRodney W. Grimes 		*tp++ = '\0', tp = skip(tp);
753d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "type")) {
7548fae3551SRodney W. Grimes 			if (tp == NULL)
755c8223965SMark Murray 				tp = unknown;
7568fae3551SRodney W. Grimes 			cpp = dktypenames;
7578fae3551SRodney W. Grimes 			for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
758d2fe97c7SPoul-Henning Kamp 				if (*cpp && !strcmp(*cpp, tp)) {
7598fae3551SRodney W. Grimes 					lp->d_type = cpp - dktypenames;
76009dbd070SIan Dowse 					break;
7618fae3551SRodney W. Grimes 				}
76209dbd070SIan Dowse 			if (cpp < &dktypenames[DKMAXTYPES])
76309dbd070SIan Dowse 				continue;
764fb26ece7SJaakko Heinonen 			errno = 0;
765fb26ece7SJaakko Heinonen 			v = strtoul(tp, &endp, 10);
766fb26ece7SJaakko Heinonen 			if (errno != 0 || *endp != '\0')
767fb26ece7SJaakko Heinonen 				v = DKMAXTYPES;
768484c7804SJulian Elischer 			if (v >= DKMAXTYPES)
769484c7804SJulian Elischer 				fprintf(stderr, "line %d:%s %lu\n", lineno,
7708fae3551SRodney W. Grimes 				    "Warning, unknown disk type", v);
771fb26ece7SJaakko Heinonen 			else
7728fae3551SRodney W. Grimes 				lp->d_type = v;
7738fae3551SRodney W. Grimes 			continue;
7748fae3551SRodney W. Grimes 		}
775d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "flags")) {
7768fae3551SRodney W. Grimes 			for (v = 0; (cp = tp) && *cp != '\0';) {
7778fae3551SRodney W. Grimes 				tp = word(cp);
778d2fe97c7SPoul-Henning Kamp 				if (!strcmp(cp, "removeable"))
7798fae3551SRodney W. Grimes 					v |= D_REMOVABLE;
780d2fe97c7SPoul-Henning Kamp 				else if (!strcmp(cp, "ecc"))
7818fae3551SRodney W. Grimes 					v |= D_ECC;
782d2fe97c7SPoul-Henning Kamp 				else if (!strcmp(cp, "badsect"))
7838fae3551SRodney W. Grimes 					v |= D_BADSECT;
7848fae3551SRodney W. Grimes 				else {
7858fae3551SRodney W. Grimes 					fprintf(stderr,
7868fae3551SRodney W. Grimes 					    "line %d: %s: bad flag\n",
7878fae3551SRodney W. Grimes 					    lineno, cp);
7888fae3551SRodney W. Grimes 					errors++;
7898fae3551SRodney W. Grimes 				}
7908fae3551SRodney W. Grimes 			}
7918fae3551SRodney W. Grimes 			lp->d_flags = v;
7928fae3551SRodney W. Grimes 			continue;
7938fae3551SRodney W. Grimes 		}
794d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "drivedata")) {
7958fae3551SRodney W. Grimes 			for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
796484c7804SJulian Elischer 				lp->d_drivedata[i++] = strtoul(cp, NULL, 10);
7978fae3551SRodney W. Grimes 				tp = word(cp);
7988fae3551SRodney W. Grimes 			}
7998fae3551SRodney W. Grimes 			continue;
8008fae3551SRodney W. Grimes 		}
801484c7804SJulian Elischer 		if (sscanf(cp, "%lu partitions", &v) == 1) {
802b11075cdSAndrey V. Elsukov 			if (v > MAXPARTITIONS) {
8038fae3551SRodney W. Grimes 				fprintf(stderr,
8048fae3551SRodney W. Grimes 				    "line %d: bad # of partitions\n", lineno);
8058fae3551SRodney W. Grimes 				lp->d_npartitions = MAXPARTITIONS;
8068fae3551SRodney W. Grimes 				errors++;
807982e6e69SAndrey V. Elsukov 			} else if (v < DEFPARTITIONS) {
808982e6e69SAndrey V. Elsukov 				fprintf(stderr,
809982e6e69SAndrey V. Elsukov 				    "line %d: bad # of partitions\n", lineno);
810982e6e69SAndrey V. Elsukov 				lp->d_npartitions = DEFPARTITIONS;
811982e6e69SAndrey V. Elsukov 				errors++;
8128fae3551SRodney W. Grimes 			} else
8138fae3551SRodney W. Grimes 				lp->d_npartitions = v;
8148fae3551SRodney W. Grimes 			continue;
8158fae3551SRodney W. Grimes 		}
8168fae3551SRodney W. Grimes 		if (tp == NULL)
817c8223965SMark Murray 			tp = blank;
818d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "disk")) {
8198fae3551SRodney W. Grimes 			strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
8208fae3551SRodney W. Grimes 			continue;
8218fae3551SRodney W. Grimes 		}
822d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "label")) {
8238fae3551SRodney W. Grimes 			strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
8248fae3551SRodney W. Grimes 			continue;
8258fae3551SRodney W. Grimes 		}
826d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "bytes/sector")) {
827484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
828484c7804SJulian Elischer 			if (v == 0 || (v % DEV_BSIZE) != 0) {
8298fae3551SRodney W. Grimes 				fprintf(stderr,
8308fae3551SRodney W. Grimes 				    "line %d: %s: bad sector size\n",
8318fae3551SRodney W. Grimes 				    lineno, tp);
8328fae3551SRodney W. Grimes 				errors++;
8338fae3551SRodney W. Grimes 			} else
8348fae3551SRodney W. Grimes 				lp->d_secsize = v;
8358fae3551SRodney W. Grimes 			continue;
8368fae3551SRodney W. Grimes 		}
837d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "sectors/track")) {
838484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
839484c7804SJulian Elischer #if (ULONG_MAX != 0xffffffffUL)
840266e7997SDag-Erling Smørgrav 			if (v == 0 || v > 0xffffffff)
841484c7804SJulian Elischer #else
842266e7997SDag-Erling Smørgrav 			if (v == 0)
843484c7804SJulian Elischer #endif
844266e7997SDag-Erling Smørgrav 			{
8458fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
8468fae3551SRodney W. Grimes 				    lineno, tp, cp);
8478fae3551SRodney W. Grimes 				errors++;
8488fae3551SRodney W. Grimes 			} else
8498fae3551SRodney W. Grimes 				lp->d_nsectors = v;
8508fae3551SRodney W. Grimes 			continue;
8518fae3551SRodney W. Grimes 		}
852d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "sectors/cylinder")) {
853484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
854484c7804SJulian Elischer 			if (v == 0) {
8558fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
8568fae3551SRodney W. Grimes 				    lineno, tp, cp);
8578fae3551SRodney W. Grimes 				errors++;
8588fae3551SRodney W. Grimes 			} else
8598fae3551SRodney W. Grimes 				lp->d_secpercyl = v;
8608fae3551SRodney W. Grimes 			continue;
8618fae3551SRodney W. Grimes 		}
862d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "tracks/cylinder")) {
863484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
864484c7804SJulian Elischer 			if (v == 0) {
8658fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
8668fae3551SRodney W. Grimes 				    lineno, tp, cp);
8678fae3551SRodney W. Grimes 				errors++;
8688fae3551SRodney W. Grimes 			} else
8698fae3551SRodney W. Grimes 				lp->d_ntracks = v;
8708fae3551SRodney W. Grimes 			continue;
8718fae3551SRodney W. Grimes 		}
872d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "cylinders")) {
873484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
874484c7804SJulian Elischer 			if (v == 0) {
8758fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
8768fae3551SRodney W. Grimes 				    lineno, tp, cp);
8778fae3551SRodney W. Grimes 				errors++;
8788fae3551SRodney W. Grimes 			} else
8798fae3551SRodney W. Grimes 				lp->d_ncylinders = v;
8808fae3551SRodney W. Grimes 			continue;
8818fae3551SRodney W. Grimes 		}
882d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "sectors/unit")) {
883484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
884484c7804SJulian Elischer 			if (v == 0) {
885f75dd518SBruce Evans 				fprintf(stderr, "line %d: %s: bad %s\n",
886f75dd518SBruce Evans 				    lineno, tp, cp);
887f75dd518SBruce Evans 				errors++;
888f75dd518SBruce Evans 			} else
889f75dd518SBruce Evans 				lp->d_secperunit = v;
890f75dd518SBruce Evans 			continue;
891f75dd518SBruce Evans 		}
892d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "rpm")) {
893484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
894484c7804SJulian Elischer 			if (v == 0 || v > USHRT_MAX) {
8958fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
8968fae3551SRodney W. Grimes 				    lineno, tp, cp);
8978fae3551SRodney W. Grimes 				errors++;
8988fae3551SRodney W. Grimes 			} else
8998fae3551SRodney W. Grimes 				lp->d_rpm = v;
9008fae3551SRodney W. Grimes 			continue;
9018fae3551SRodney W. Grimes 		}
902d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "interleave")) {
903484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
904484c7804SJulian Elischer 			if (v == 0 || v > USHRT_MAX) {
9058fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
9068fae3551SRodney W. Grimes 				    lineno, tp, cp);
9078fae3551SRodney W. Grimes 				errors++;
9088fae3551SRodney W. Grimes 			} else
9098fae3551SRodney W. Grimes 				lp->d_interleave = v;
9108fae3551SRodney W. Grimes 			continue;
9118fae3551SRodney W. Grimes 		}
912d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "trackskew")) {
913484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
914484c7804SJulian Elischer 			if (v > USHRT_MAX) {
9158fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
9168fae3551SRodney W. Grimes 				    lineno, tp, cp);
9178fae3551SRodney W. Grimes 				errors++;
9188fae3551SRodney W. Grimes 			} else
9198fae3551SRodney W. Grimes 				lp->d_trackskew = v;
9208fae3551SRodney W. Grimes 			continue;
9218fae3551SRodney W. Grimes 		}
922d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "cylinderskew")) {
923484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
924484c7804SJulian Elischer 			if (v > USHRT_MAX) {
9258fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
9268fae3551SRodney W. Grimes 				    lineno, tp, cp);
9278fae3551SRodney W. Grimes 				errors++;
9288fae3551SRodney W. Grimes 			} else
9298fae3551SRodney W. Grimes 				lp->d_cylskew = v;
9308fae3551SRodney W. Grimes 			continue;
9318fae3551SRodney W. Grimes 		}
932d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "headswitch")) {
933484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
9348fae3551SRodney W. Grimes 			lp->d_headswitch = v;
9358fae3551SRodney W. Grimes 			continue;
9368fae3551SRodney W. Grimes 		}
937d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "track-to-track seek")) {
938484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
9398fae3551SRodney W. Grimes 			lp->d_trkseek = v;
9408fae3551SRodney W. Grimes 			continue;
9418fae3551SRodney W. Grimes 		}
9423233afaeSJohn W. De Boskey 		/* the ':' was removed above */
94367b46708SIan Dowse 		if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') {
94467b46708SIan Dowse 			fprintf(stderr,
94567b46708SIan Dowse 			    "line %d: %s: Unknown disklabel field\n", lineno,
94667b46708SIan Dowse 			    cp);
94767b46708SIan Dowse 			errors++;
94867b46708SIan Dowse 			continue;
94967b46708SIan Dowse 		}
95067b46708SIan Dowse 
95167b46708SIan Dowse 		/* Process a partition specification line. */
9523233afaeSJohn W. De Boskey 		part = *cp - 'a';
9533233afaeSJohn W. De Boskey 		if (part >= lp->d_npartitions) {
9548fae3551SRodney W. Grimes 			fprintf(stderr,
9553233afaeSJohn W. De Boskey 			    "line %d: partition name out of range a-%c: %s\n",
9563233afaeSJohn W. De Boskey 			    lineno, 'a' + lp->d_npartitions - 1, cp);
9578fae3551SRodney W. Grimes 			errors++;
9588fae3551SRodney W. Grimes 			continue;
9598fae3551SRodney W. Grimes 		}
9603233afaeSJohn W. De Boskey 		part_set[part] = 1;
96167b46708SIan Dowse 
96267b46708SIan Dowse 		if (getasciipartspec(tp, lp, part, lineno) != 0) {
96367b46708SIan Dowse 			errors++;
96467b46708SIan Dowse 			break;
96567b46708SIan Dowse 		}
96667b46708SIan Dowse 	}
96767b46708SIan Dowse 	errors += checklabel(lp);
96867b46708SIan Dowse 	return (errors == 0);
96967b46708SIan Dowse }
97067b46708SIan Dowse 
97167b46708SIan Dowse #define NXTNUM(n) do { \
97213e0abcbSPaul Traina 	if (tp == NULL) { \
97313e0abcbSPaul Traina 		fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
97467b46708SIan Dowse 		return (1); \
97513e0abcbSPaul Traina 	} else { \
9768fae3551SRodney W. Grimes 		cp = tp, tp = word(cp); \
977484c7804SJulian Elischer 		(n) = strtoul(cp, NULL, 10); \
97813e0abcbSPaul Traina 	} \
97967b46708SIan Dowse } while (0)
98067b46708SIan Dowse 
9813233afaeSJohn W. De Boskey /* retain 1 character following number */
98267b46708SIan Dowse #define NXTWORD(w,n) do { \
9833233afaeSJohn W. De Boskey 	if (tp == NULL) { \
9843233afaeSJohn W. De Boskey 		fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
98567b46708SIan Dowse 		return (1); \
9863233afaeSJohn W. De Boskey 	} else { \
9873233afaeSJohn W. De Boskey 		char *tmp; \
9883233afaeSJohn W. De Boskey 		cp = tp, tp = word(cp); \
989484c7804SJulian Elischer 		(n) = strtoul(cp, &tmp, 10); \
9903233afaeSJohn W. De Boskey 		if (tmp) (w) = *tmp; \
9913233afaeSJohn W. De Boskey 	} \
99267b46708SIan Dowse } while (0)
99367b46708SIan Dowse 
99467b46708SIan Dowse /*
99567b46708SIan Dowse  * Read a partition line into partition `part' in the specified disklabel.
99667b46708SIan Dowse  * Return 0 on success, 1 on failure.
99767b46708SIan Dowse  */
998d2fe97c7SPoul-Henning Kamp static int
getasciipartspec(char * tp,struct disklabel * lp,int part,int lineno)99967b46708SIan Dowse getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno)
100067b46708SIan Dowse {
100167b46708SIan Dowse 	struct partition *pp;
1002fb26ece7SJaakko Heinonen 	char *cp, *endp;
100367b46708SIan Dowse 	const char **cpp;
1004484c7804SJulian Elischer 	u_long v;
100567b46708SIan Dowse 
100667b46708SIan Dowse 	pp = &lp->d_partitions[part];
100767b46708SIan Dowse 	cp = NULL;
100867b46708SIan Dowse 
10093233afaeSJohn W. De Boskey 	v = 0;
10103233afaeSJohn W. De Boskey 	NXTWORD(part_size_type[part],v);
1011484c7804SJulian Elischer 	if (v == 0 && part_size_type[part] != '*') {
1012484c7804SJulian Elischer 		fprintf(stderr,
1013484c7804SJulian Elischer 		    "line %d: %s: bad partition size\n", lineno, cp);
101467b46708SIan Dowse 		return (1);
101567b46708SIan Dowse 	}
10168fae3551SRodney W. Grimes 	pp->p_size = v;
10173233afaeSJohn W. De Boskey 
10183233afaeSJohn W. De Boskey 	v = 0;
10193233afaeSJohn W. De Boskey 	NXTWORD(part_offset_type[part],v);
1020484c7804SJulian Elischer 	if (v == 0 && part_offset_type[part] != '*' &&
1021484c7804SJulian Elischer 	    part_offset_type[part] != '\0') {
1022484c7804SJulian Elischer 		fprintf(stderr,
1023484c7804SJulian Elischer 		    "line %d: %s: bad partition offset\n", lineno, cp);
102467b46708SIan Dowse 		return (1);
102567b46708SIan Dowse 	}
10268fae3551SRodney W. Grimes 	pp->p_offset = v;
102729f3f095SYaroslav Tykhiy 	if (tp == NULL) {
102829f3f095SYaroslav Tykhiy 		fprintf(stderr, "line %d: missing file system type\n", lineno);
102929f3f095SYaroslav Tykhiy 		return (1);
103029f3f095SYaroslav Tykhiy 	}
10318fae3551SRodney W. Grimes 	cp = tp, tp = word(cp);
103267b46708SIan Dowse 	for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1033d2fe97c7SPoul-Henning Kamp 		if (*cpp && !strcmp(*cpp, cp))
103467b46708SIan Dowse 			break;
103567b46708SIan Dowse 	if (*cpp != NULL) {
103667b46708SIan Dowse 		pp->p_fstype = cpp - fstypenames;
103767b46708SIan Dowse 	} else {
1038fb26ece7SJaakko Heinonen 		if (isdigit(*cp)) {
1039fb26ece7SJaakko Heinonen 			errno = 0;
1040fb26ece7SJaakko Heinonen 			v = strtoul(cp, &endp, 10);
1041fb26ece7SJaakko Heinonen 			if (errno != 0 || *endp != '\0')
1042fb26ece7SJaakko Heinonen 				v = FSMAXTYPES;
1043fb26ece7SJaakko Heinonen 		} else
10448fae3551SRodney W. Grimes 			v = FSMAXTYPES;
1045484c7804SJulian Elischer 		if (v >= FSMAXTYPES) {
10463233afaeSJohn W. De Boskey 			fprintf(stderr,
104767b46708SIan Dowse 			    "line %d: Warning, unknown file system type %s\n",
10483233afaeSJohn W. De Boskey 			    lineno, cp);
10498fae3551SRodney W. Grimes 			v = FS_UNUSED;
10508fae3551SRodney W. Grimes 		}
10518fae3551SRodney W. Grimes 		pp->p_fstype = v;
105267b46708SIan Dowse 	}
105367b46708SIan Dowse 
10548fae3551SRodney W. Grimes 	switch (pp->p_fstype) {
10553233afaeSJohn W. De Boskey 	case FS_UNUSED:
1056640c9cb2SIan Dowse 	case FS_BSDFFS:
1057640c9cb2SIan Dowse 	case FS_BSDLFS:
1058640c9cb2SIan Dowse 		/* accept defaults for fsize/frag/cpg */
10593233afaeSJohn W. De Boskey 		if (tp) {
10608fae3551SRodney W. Grimes 			NXTNUM(pp->p_fsize);
10618fae3551SRodney W. Grimes 			if (pp->p_fsize == 0)
10628fae3551SRodney W. Grimes 				break;
10638fae3551SRodney W. Grimes 			NXTNUM(v);
10648fae3551SRodney W. Grimes 			pp->p_frag = v / pp->p_fsize;
1065640c9cb2SIan Dowse 			if (tp != NULL)
1066640c9cb2SIan Dowse 				NXTNUM(pp->p_cpg);
10673233afaeSJohn W. De Boskey 		}
10683233afaeSJohn W. De Boskey 		/* else default to 0's */
10698fae3551SRodney W. Grimes 		break;
10708fae3551SRodney W. Grimes 	default:
10718fae3551SRodney W. Grimes 		break;
10728fae3551SRodney W. Grimes 	}
107367b46708SIan Dowse 	return (0);
10748fae3551SRodney W. Grimes }
10758fae3551SRodney W. Grimes 
10768fae3551SRodney W. Grimes /*
10778fae3551SRodney W. Grimes  * Check disklabel for errors and fill in
10788fae3551SRodney W. Grimes  * derived fields according to supplied values.
10798fae3551SRodney W. Grimes  */
1080d2fe97c7SPoul-Henning Kamp static int
checklabel(struct disklabel * lp)1081326c7cdaSWarner Losh checklabel(struct disklabel *lp)
10828fae3551SRodney W. Grimes {
1083326c7cdaSWarner Losh 	struct partition *pp;
10848fae3551SRodney W. Grimes 	int i, errors = 0;
10858fae3551SRodney W. Grimes 	char part;
1086901b050bSDmitry Chagin 	u_long base_offset, needed, total_percent, current_offset;
10873b89beb1SIan Dowse 	long free_space;
10883233afaeSJohn W. De Boskey 	int seen_default_offset;
10893233afaeSJohn W. De Boskey 	int hog_part;
10903233afaeSJohn W. De Boskey 	int j;
10913233afaeSJohn W. De Boskey 	struct partition *pp2;
10928fae3551SRodney W. Grimes 
1093d2fe97c7SPoul-Henning Kamp 	if (lp == NULL)
1094d2fe97c7SPoul-Henning Kamp 		lp = &lab;
1095d2fe97c7SPoul-Henning Kamp 
109657dfbec5SPoul-Henning Kamp 	if (allfields) {
109757dfbec5SPoul-Henning Kamp 
10988fae3551SRodney W. Grimes 		if (lp->d_secsize == 0) {
10992a1deaaaSBruce Evans 			fprintf(stderr, "sector size 0\n");
11008fae3551SRodney W. Grimes 			return (1);
11018fae3551SRodney W. Grimes 		}
11028fae3551SRodney W. Grimes 		if (lp->d_nsectors == 0) {
11032a1deaaaSBruce Evans 			fprintf(stderr, "sectors/track 0\n");
11048fae3551SRodney W. Grimes 			return (1);
11058fae3551SRodney W. Grimes 		}
11068fae3551SRodney W. Grimes 		if (lp->d_ntracks == 0) {
11072a1deaaaSBruce Evans 			fprintf(stderr, "tracks/cylinder 0\n");
11088fae3551SRodney W. Grimes 			return (1);
11098fae3551SRodney W. Grimes 		}
11108fae3551SRodney W. Grimes 		if  (lp->d_ncylinders == 0) {
11112a1deaaaSBruce Evans 			fprintf(stderr, "cylinders/unit 0\n");
11128fae3551SRodney W. Grimes 			errors++;
11138fae3551SRodney W. Grimes 		}
11148fae3551SRodney W. Grimes 		if (lp->d_rpm == 0)
111557dfbec5SPoul-Henning Kamp 			warnx("revolutions/minute 0");
11168fae3551SRodney W. Grimes 		if (lp->d_secpercyl == 0)
11178fae3551SRodney W. Grimes 			lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
11188fae3551SRodney W. Grimes 		if (lp->d_secperunit == 0)
11198fae3551SRodney W. Grimes 			lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
11208fae3551SRodney W. Grimes 		if (lp->d_bbsize == 0) {
11212a1deaaaSBruce Evans 			fprintf(stderr, "boot block size 0\n");
11228fae3551SRodney W. Grimes 			errors++;
11238fae3551SRodney W. Grimes 		} else if (lp->d_bbsize % lp->d_secsize)
112457dfbec5SPoul-Henning Kamp 			warnx("boot block size %% sector-size != 0");
1125982e6e69SAndrey V. Elsukov 		if (lp->d_npartitions > MAXPARTITIONS) {
112657dfbec5SPoul-Henning Kamp 			warnx("number of partitions (%lu) > MAXPARTITIONS (%d)",
11272a1deaaaSBruce Evans 			    (u_long)lp->d_npartitions, MAXPARTITIONS);
1128982e6e69SAndrey V. Elsukov 			errors++;
1129982e6e69SAndrey V. Elsukov 		}
1130982e6e69SAndrey V. Elsukov 		if (lp->d_npartitions < DEFPARTITIONS) {
1131982e6e69SAndrey V. Elsukov 			warnx("number of partitions (%lu) < DEFPARTITIONS (%d)",
1132982e6e69SAndrey V. Elsukov 			    (u_long)lp->d_npartitions, DEFPARTITIONS);
1133982e6e69SAndrey V. Elsukov 			errors++;
1134982e6e69SAndrey V. Elsukov 		}
113557dfbec5SPoul-Henning Kamp 	} else {
113657dfbec5SPoul-Henning Kamp 		struct disklabel *vl;
113757dfbec5SPoul-Henning Kamp 
113857dfbec5SPoul-Henning Kamp 		vl = getvirginlabel();
1139982e6e69SAndrey V. Elsukov 		if (lp->d_secsize == 0)
114057dfbec5SPoul-Henning Kamp 			lp->d_secsize = vl->d_secsize;
1141982e6e69SAndrey V. Elsukov 		if (lp->d_nsectors == 0)
114257dfbec5SPoul-Henning Kamp 			lp->d_nsectors = vl->d_nsectors;
1143982e6e69SAndrey V. Elsukov 		if (lp->d_ntracks == 0)
114457dfbec5SPoul-Henning Kamp 			lp->d_ntracks = vl->d_ntracks;
1145982e6e69SAndrey V. Elsukov 		if (lp->d_ncylinders == 0)
114657dfbec5SPoul-Henning Kamp 			lp->d_ncylinders = vl->d_ncylinders;
1147982e6e69SAndrey V. Elsukov 		if (lp->d_rpm == 0)
114857dfbec5SPoul-Henning Kamp 			lp->d_rpm = vl->d_rpm;
1149982e6e69SAndrey V. Elsukov 		if (lp->d_interleave == 0)
115057dfbec5SPoul-Henning Kamp 			lp->d_interleave = vl->d_interleave;
1151982e6e69SAndrey V. Elsukov 		if (lp->d_secpercyl == 0)
115257dfbec5SPoul-Henning Kamp 			lp->d_secpercyl = vl->d_secpercyl;
1153b9f41b60SAndrey V. Elsukov 		if (lp->d_secperunit == 0 ||
1154b9f41b60SAndrey V. Elsukov 		    lp->d_secperunit > vl->d_secperunit)
115557dfbec5SPoul-Henning Kamp 			lp->d_secperunit = vl->d_secperunit;
1156982e6e69SAndrey V. Elsukov 		if (lp->d_bbsize == 0)
115757dfbec5SPoul-Henning Kamp 			lp->d_bbsize = vl->d_bbsize;
1158b11075cdSAndrey V. Elsukov 		if (lp->d_npartitions < DEFPARTITIONS ||
1159982e6e69SAndrey V. Elsukov 		    lp->d_npartitions > MAXPARTITIONS)
116057dfbec5SPoul-Henning Kamp 			lp->d_npartitions = vl->d_npartitions;
116157dfbec5SPoul-Henning Kamp 	}
116257dfbec5SPoul-Henning Kamp 
11633233afaeSJohn W. De Boskey 
11643233afaeSJohn W. De Boskey 	/* first allocate space to the partitions, then offsets */
11653233afaeSJohn W. De Boskey 	total_percent = 0; /* in percent */
11663233afaeSJohn W. De Boskey 	hog_part = -1;
11673233afaeSJohn W. De Boskey 	/* find all fixed partitions */
11683233afaeSJohn W. De Boskey 	for (i = 0; i < lp->d_npartitions; i++) {
11693233afaeSJohn W. De Boskey 		pp = &lp->d_partitions[i];
11703233afaeSJohn W. De Boskey 		if (part_set[i]) {
11713233afaeSJohn W. De Boskey 			if (part_size_type[i] == '*') {
11726b0ff5f5SPoul-Henning Kamp 				if (i == RAW_PART) {
11733233afaeSJohn W. De Boskey 					pp->p_size = lp->d_secperunit;
11743233afaeSJohn W. De Boskey 				} else {
11753233afaeSJohn W. De Boskey 					if (hog_part != -1)
117657dfbec5SPoul-Henning Kamp 						warnx("Too many '*' partitions (%c and %c)",
11773233afaeSJohn W. De Boskey 						    hog_part + 'a',i + 'a');
11783233afaeSJohn W. De Boskey 					else
11793233afaeSJohn W. De Boskey 						hog_part = i;
11803233afaeSJohn W. De Boskey 				}
11813233afaeSJohn W. De Boskey 			} else {
11828d3105e8SWarner Losh 				off_t size;
11833233afaeSJohn W. De Boskey 
11843233afaeSJohn W. De Boskey 				size = pp->p_size;
11853233afaeSJohn W. De Boskey 				switch (part_size_type[i]) {
11863233afaeSJohn W. De Boskey 				case '%':
11873233afaeSJohn W. De Boskey 					total_percent += size;
11883233afaeSJohn W. De Boskey 					break;
11894c814dfcSDag-Erling Smørgrav 				case 't':
11904c814dfcSDag-Erling Smørgrav 				case 'T':
11914c814dfcSDag-Erling Smørgrav 					size *= 1024ULL;
11924c814dfcSDag-Erling Smørgrav 					/* FALLTHROUGH */
11934c814dfcSDag-Erling Smørgrav 				case 'g':
11944c814dfcSDag-Erling Smørgrav 				case 'G':
11954c814dfcSDag-Erling Smørgrav 					size *= 1024ULL;
11964c814dfcSDag-Erling Smørgrav 					/* FALLTHROUGH */
11974c814dfcSDag-Erling Smørgrav 				case 'm':
11984c814dfcSDag-Erling Smørgrav 				case 'M':
11994c814dfcSDag-Erling Smørgrav 					size *= 1024ULL;
12004c814dfcSDag-Erling Smørgrav 					/* FALLTHROUGH */
12013233afaeSJohn W. De Boskey 				case 'k':
12023233afaeSJohn W. De Boskey 				case 'K':
12038d3105e8SWarner Losh 					size *= 1024ULL;
12043233afaeSJohn W. De Boskey 					break;
12053233afaeSJohn W. De Boskey 				case '\0':
12063233afaeSJohn W. De Boskey 					break;
12073233afaeSJohn W. De Boskey 				default:
120881807292SDag-Erling Smørgrav 					warnx("unknown multiplier suffix '%c' for partition %c (should be K, M, G or T)",
12094c814dfcSDag-Erling Smørgrav 					    part_size_type[i], i + 'a');
12103233afaeSJohn W. De Boskey 					break;
12113233afaeSJohn W. De Boskey 				}
12123233afaeSJohn W. De Boskey 				/* don't count %'s yet */
12133233afaeSJohn W. De Boskey 				if (part_size_type[i] != '%') {
12143233afaeSJohn W. De Boskey 					/*
12153233afaeSJohn W. De Boskey 					 * for all not in sectors, convert to
12163233afaeSJohn W. De Boskey 					 * sectors
12173233afaeSJohn W. De Boskey 					 */
12183233afaeSJohn W. De Boskey 					if (part_size_type[i] != '\0') {
12193233afaeSJohn W. De Boskey 						if (size % lp->d_secsize != 0)
122057dfbec5SPoul-Henning Kamp 							warnx("partition %c not an integer number of sectors",
12213233afaeSJohn W. De Boskey 							    i + 'a');
12223233afaeSJohn W. De Boskey 						size /= lp->d_secsize;
12233233afaeSJohn W. De Boskey 						pp->p_size = size;
12243233afaeSJohn W. De Boskey 					}
12253233afaeSJohn W. De Boskey 				}
12263233afaeSJohn W. De Boskey 			}
12273233afaeSJohn W. De Boskey 		}
12283233afaeSJohn W. De Boskey 	}
12293b89beb1SIan Dowse 
12303b89beb1SIan Dowse 	/* Find out the total free space, excluding the boot block area. */
12313b89beb1SIan Dowse 	base_offset = BBSIZE / secsize;
12323b89beb1SIan Dowse 	free_space = 0;
12333b89beb1SIan Dowse 	for (i = 0; i < lp->d_npartitions; i++) {
12343b89beb1SIan Dowse 		pp = &lp->d_partitions[i];
12353b89beb1SIan Dowse 		if (!part_set[i] || i == RAW_PART ||
12363b89beb1SIan Dowse 		    part_size_type[i] == '%' || part_size_type[i] == '*')
12373b89beb1SIan Dowse 			continue;
12383b89beb1SIan Dowse 		if (pp->p_offset > base_offset)
12393b89beb1SIan Dowse 			free_space += pp->p_offset - base_offset;
12403b89beb1SIan Dowse 		if (pp->p_offset + pp->p_size > base_offset)
12413b89beb1SIan Dowse 			base_offset = pp->p_offset + pp->p_size;
12423b89beb1SIan Dowse 	}
12433b89beb1SIan Dowse 	if (base_offset < lp->d_secperunit)
12443b89beb1SIan Dowse 		free_space += lp->d_secperunit - base_offset;
12453b89beb1SIan Dowse 
12463233afaeSJohn W. De Boskey 	/* handle % partitions - note %'s don't need to add up to 100! */
12473233afaeSJohn W. De Boskey 	if (total_percent != 0) {
12483233afaeSJohn W. De Boskey 		if (total_percent > 100) {
12496b0ff5f5SPoul-Henning Kamp 			fprintf(stderr,"total percentage %lu is greater than 100\n",
12503233afaeSJohn W. De Boskey 			    total_percent);
12513233afaeSJohn W. De Boskey 			errors++;
12523233afaeSJohn W. De Boskey 		}
12533233afaeSJohn W. De Boskey 
12543233afaeSJohn W. De Boskey 		if (free_space > 0) {
12553233afaeSJohn W. De Boskey 			for (i = 0; i < lp->d_npartitions; i++) {
12563233afaeSJohn W. De Boskey 				pp = &lp->d_partitions[i];
12573233afaeSJohn W. De Boskey 				if (part_set[i] && part_size_type[i] == '%') {
12583233afaeSJohn W. De Boskey 					/* careful of overflows! and integer roundoff */
12593233afaeSJohn W. De Boskey 					pp->p_size = ((double)pp->p_size/100) * free_space;
12603233afaeSJohn W. De Boskey 
12613233afaeSJohn W. De Boskey 					/* FIX we can lose a sector or so due to roundoff per
12623233afaeSJohn W. De Boskey 					   partition.  A more complex algorithm could avoid that */
12633233afaeSJohn W. De Boskey 				}
12643233afaeSJohn W. De Boskey 			}
12653233afaeSJohn W. De Boskey 		} else {
12663233afaeSJohn W. De Boskey 			fprintf(stderr,
12676b0ff5f5SPoul-Henning Kamp 			    "%ld sectors available to give to '*' and '%%' partitions\n",
12683233afaeSJohn W. De Boskey 			    free_space);
12693233afaeSJohn W. De Boskey 			errors++;
12703233afaeSJohn W. De Boskey 			/* fix?  set all % partitions to size 0? */
12713233afaeSJohn W. De Boskey 		}
12723233afaeSJohn W. De Boskey 	}
12733233afaeSJohn W. De Boskey 	/* give anything remaining to the hog partition */
12743233afaeSJohn W. De Boskey 	if (hog_part != -1) {
12753b89beb1SIan Dowse 		/*
12763b89beb1SIan Dowse 		 * Find the range of offsets usable by '*' partitions around
12773b89beb1SIan Dowse 		 * the hog partition and how much space they need.
12783b89beb1SIan Dowse 		 */
12793b89beb1SIan Dowse 		needed = 0;
12803b89beb1SIan Dowse 		base_offset = BBSIZE / secsize;
12813b89beb1SIan Dowse 		for (i = hog_part - 1; i >= 0; i--) {
12823b89beb1SIan Dowse 			pp = &lp->d_partitions[i];
12833b89beb1SIan Dowse 			if (!part_set[i] || i == RAW_PART)
12843b89beb1SIan Dowse 				continue;
12853b89beb1SIan Dowse 			if (part_offset_type[i] == '*') {
12863b89beb1SIan Dowse 				needed += pp->p_size;
12873b89beb1SIan Dowse 				continue;
12883b89beb1SIan Dowse 			}
12893b89beb1SIan Dowse 			base_offset = pp->p_offset + pp->p_size;
12903b89beb1SIan Dowse 			break;
12913b89beb1SIan Dowse 		}
12923b89beb1SIan Dowse 		current_offset = lp->d_secperunit;
12933b89beb1SIan Dowse 		for (i = lp->d_npartitions - 1; i > hog_part; i--) {
12943b89beb1SIan Dowse 			pp = &lp->d_partitions[i];
12953b89beb1SIan Dowse 			if (!part_set[i] || i == RAW_PART)
12963b89beb1SIan Dowse 				continue;
12973b89beb1SIan Dowse 			if (part_offset_type[i] == '*') {
12983b89beb1SIan Dowse 				needed += pp->p_size;
12993b89beb1SIan Dowse 				continue;
13003b89beb1SIan Dowse 			}
13013b89beb1SIan Dowse 			current_offset = pp->p_offset;
13023b89beb1SIan Dowse 		}
13033b89beb1SIan Dowse 
13043b89beb1SIan Dowse 		if (current_offset - base_offset <= needed) {
13053b89beb1SIan Dowse 			fprintf(stderr, "Cannot find space for partition %c\n",
13063b89beb1SIan Dowse 			    hog_part + 'a');
13073b89beb1SIan Dowse 			fprintf(stderr,
13083b89beb1SIan Dowse 			    "Need more than %lu sectors between %lu and %lu\n",
13093b89beb1SIan Dowse 			    needed, base_offset, current_offset);
13103b89beb1SIan Dowse 			errors++;
13113b89beb1SIan Dowse 			lp->d_partitions[hog_part].p_size = 0;
13123b89beb1SIan Dowse 		} else {
13133b89beb1SIan Dowse 			lp->d_partitions[hog_part].p_size = current_offset -
13143b89beb1SIan Dowse 			    base_offset - needed;
13153b89beb1SIan Dowse 		}
13163233afaeSJohn W. De Boskey 	}
13173233afaeSJohn W. De Boskey 
13183233afaeSJohn W. De Boskey 	/* Now set the offsets for each partition */
13193b89beb1SIan Dowse 	current_offset = BBSIZE / secsize; /* in sectors */
13203233afaeSJohn W. De Boskey 	seen_default_offset = 0;
13213233afaeSJohn W. De Boskey 	for (i = 0; i < lp->d_npartitions; i++) {
13223233afaeSJohn W. De Boskey 		part = 'a' + i;
13233233afaeSJohn W. De Boskey 		pp = &lp->d_partitions[i];
13243233afaeSJohn W. De Boskey 		if (part_set[i]) {
13253233afaeSJohn W. De Boskey 			if (part_offset_type[i] == '*') {
13266b0ff5f5SPoul-Henning Kamp 				if (i == RAW_PART) {
13273233afaeSJohn W. De Boskey 					pp->p_offset = 0;
13283233afaeSJohn W. De Boskey 				} else {
13293233afaeSJohn W. De Boskey 					pp->p_offset = current_offset;
13303233afaeSJohn W. De Boskey 					seen_default_offset = 1;
13313233afaeSJohn W. De Boskey 				}
13323233afaeSJohn W. De Boskey 			} else {
13333233afaeSJohn W. De Boskey 				/* allow them to be out of order for old-style tables */
13343233afaeSJohn W. De Boskey 				if (pp->p_offset < current_offset &&
1335f2f63257SGreg Lehey 				    seen_default_offset && i != RAW_PART &&
1336f2f63257SGreg Lehey 				    pp->p_fstype != FS_VINUM) {
13373233afaeSJohn W. De Boskey 					fprintf(stderr,
13386b0ff5f5SPoul-Henning Kamp "Offset %ld for partition %c overlaps previous partition which ends at %lu\n",
13396b0ff5f5SPoul-Henning Kamp 					    (long)pp->p_offset,i+'a',current_offset);
13403233afaeSJohn W. De Boskey 					fprintf(stderr,
13413233afaeSJohn W. De Boskey "Labels with any *'s for offset must be in ascending order by sector\n");
13423233afaeSJohn W. De Boskey 					errors++;
13433233afaeSJohn W. De Boskey 				} else if (pp->p_offset != current_offset &&
13446b0ff5f5SPoul-Henning Kamp 				    i != RAW_PART && seen_default_offset) {
13453233afaeSJohn W. De Boskey 					/*
13463233afaeSJohn W. De Boskey 					 * this may give unneeded warnings if
13473233afaeSJohn W. De Boskey 					 * partitions are out-of-order
13483233afaeSJohn W. De Boskey 					 */
134957dfbec5SPoul-Henning Kamp 					warnx(
13503233afaeSJohn W. De Boskey "Offset %ld for partition %c doesn't match expected value %ld",
13516b0ff5f5SPoul-Henning Kamp 					    (long)pp->p_offset, i + 'a', current_offset);
13523233afaeSJohn W. De Boskey 				}
13533233afaeSJohn W. De Boskey 			}
13546b0ff5f5SPoul-Henning Kamp 			if (i != RAW_PART)
13553233afaeSJohn W. De Boskey 				current_offset = pp->p_offset + pp->p_size;
13563233afaeSJohn W. De Boskey 		}
13573233afaeSJohn W. De Boskey 	}
13583233afaeSJohn W. De Boskey 
13598fae3551SRodney W. Grimes 	for (i = 0; i < lp->d_npartitions; i++) {
13608fae3551SRodney W. Grimes 		part = 'a' + i;
13618fae3551SRodney W. Grimes 		pp = &lp->d_partitions[i];
13628fae3551SRodney W. Grimes 		if (pp->p_size == 0 && pp->p_offset != 0)
136357dfbec5SPoul-Henning Kamp 			warnx("partition %c: size 0, but offset %lu",
13642a1deaaaSBruce Evans 			    part, (u_long)pp->p_offset);
13658fae3551SRodney W. Grimes #ifdef notdef
13668fae3551SRodney W. Grimes 		if (pp->p_size % lp->d_secpercyl)
136757dfbec5SPoul-Henning Kamp 			warnx("partition %c: size %% cylinder-size != 0",
13688fae3551SRodney W. Grimes 			    part);
13698fae3551SRodney W. Grimes 		if (pp->p_offset % lp->d_secpercyl)
137057dfbec5SPoul-Henning Kamp 			warnx("partition %c: offset %% cylinder-size != 0",
13718fae3551SRodney W. Grimes 			    part);
13728fae3551SRodney W. Grimes #endif
13738fae3551SRodney W. Grimes 		if (pp->p_offset > lp->d_secperunit) {
13748fae3551SRodney W. Grimes 			fprintf(stderr,
13758fae3551SRodney W. Grimes 			    "partition %c: offset past end of unit\n", part);
13768fae3551SRodney W. Grimes 			errors++;
13778fae3551SRodney W. Grimes 		}
13788fae3551SRodney W. Grimes 		if (pp->p_offset + pp->p_size > lp->d_secperunit) {
13798fae3551SRodney W. Grimes 			fprintf(stderr,
13808fae3551SRodney W. Grimes 			"partition %c: partition extends past end of unit\n",
13818fae3551SRodney W. Grimes 			    part);
13828fae3551SRodney W. Grimes 			errors++;
13838fae3551SRodney W. Grimes 		}
138457dfbec5SPoul-Henning Kamp 		if (i == RAW_PART) {
13853233afaeSJohn W. De Boskey 			if (pp->p_fstype != FS_UNUSED)
138657dfbec5SPoul-Henning Kamp 				warnx("partition %c is not marked as unused!",part);
13873233afaeSJohn W. De Boskey 			if (pp->p_offset != 0)
138857dfbec5SPoul-Henning Kamp 				warnx("partition %c doesn't start at 0!",part);
13893233afaeSJohn W. De Boskey 			if (pp->p_size != lp->d_secperunit)
139057dfbec5SPoul-Henning Kamp 				warnx("partition %c doesn't cover the whole unit!",part);
13913233afaeSJohn W. De Boskey 
13923233afaeSJohn W. De Boskey 			if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
13933233afaeSJohn W. De Boskey 			    (pp->p_size != lp->d_secperunit)) {
139457dfbec5SPoul-Henning Kamp 				warnx("An incorrect partition %c may cause problems for "
13953233afaeSJohn W. De Boskey 				    "standard system utilities",part);
13963233afaeSJohn W. De Boskey 			}
13973233afaeSJohn W. De Boskey 		}
13983233afaeSJohn W. De Boskey 
13993233afaeSJohn W. De Boskey 		/* check for overlaps */
14003233afaeSJohn W. De Boskey 		/* this will check for all possible overlaps once and only once */
14013233afaeSJohn W. De Boskey 		for (j = 0; j < i; j++) {
14023233afaeSJohn W. De Boskey 			pp2 = &lp->d_partitions[j];
1403f2f63257SGreg Lehey 			if (j != RAW_PART && i != RAW_PART &&
1404f2f63257SGreg Lehey 			    pp->p_fstype != FS_VINUM &&
1405f2f63257SGreg Lehey 			    pp2->p_fstype != FS_VINUM &&
1406f2f63257SGreg Lehey 			    part_set[i] && part_set[j]) {
14073233afaeSJohn W. De Boskey 				if (pp2->p_offset < pp->p_offset + pp->p_size &&
14083233afaeSJohn W. De Boskey 				    (pp2->p_offset + pp2->p_size > pp->p_offset ||
14093233afaeSJohn W. De Boskey 					pp2->p_offset >= pp->p_offset)) {
14103233afaeSJohn W. De Boskey 					fprintf(stderr,"partitions %c and %c overlap!\n",
14113233afaeSJohn W. De Boskey 					    j + 'a', i + 'a');
14123233afaeSJohn W. De Boskey 					errors++;
14133233afaeSJohn W. De Boskey 				}
14143233afaeSJohn W. De Boskey 			}
14153233afaeSJohn W. De Boskey 		}
14168fae3551SRodney W. Grimes 	}
1417d5718812SMarcel Moolenaar 	for (; i < lp->d_npartitions; i++) {
14188fae3551SRodney W. Grimes 		part = 'a' + i;
14198fae3551SRodney W. Grimes 		pp = &lp->d_partitions[i];
14208fae3551SRodney W. Grimes 		if (pp->p_size || pp->p_offset)
142157dfbec5SPoul-Henning Kamp 			warnx("unused partition %c: size %d offset %lu",
14222a1deaaaSBruce Evans 			    'a' + i, pp->p_size, (u_long)pp->p_offset);
14238fae3551SRodney W. Grimes 	}
14248fae3551SRodney W. Grimes 	return (errors);
14258fae3551SRodney W. Grimes }
14268fae3551SRodney W. Grimes 
14278fae3551SRodney W. Grimes /*
1428425bed3aSJoerg Wunsch  * When operating on a "virgin" disk, try getting an initial label
1429425bed3aSJoerg Wunsch  * from the associated device driver.  This might work for all device
1430425bed3aSJoerg Wunsch  * drivers that are able to fetch some initial device parameters
1431425bed3aSJoerg Wunsch  * without even having access to a (BSD) disklabel, like SCSI disks,
1432425bed3aSJoerg Wunsch  * most IDE drives, or vn devices.
1433425bed3aSJoerg Wunsch  *
1434425bed3aSJoerg Wunsch  * The device name must be given in its "canonical" form.
1435425bed3aSJoerg Wunsch  */
1436d2fe97c7SPoul-Henning Kamp static struct disklabel *
getvirginlabel(void)1437425bed3aSJoerg Wunsch getvirginlabel(void)
1438425bed3aSJoerg Wunsch {
1439c8223965SMark Murray 	static struct disklabel loclab;
1440b9d05a16SPoul-Henning Kamp 	struct partition *dp;
1441425bed3aSJoerg Wunsch 	int f;
144257dfbec5SPoul-Henning Kamp 	u_int u;
1443425bed3aSJoerg Wunsch 
1444d2fe97c7SPoul-Henning Kamp 	if ((f = open(specname, O_RDONLY)) == -1) {
1445d2fe97c7SPoul-Henning Kamp 		warn("cannot open %s", specname);
144643be698cSBruce Evans 		return (NULL);
1447425bed3aSJoerg Wunsch 	}
1448ff7d5162SJordan K. Hubbard 
14497747c959SLuigi Rizzo 	if (is_file)
14507747c959SLuigi Rizzo 		get_file_parms(f);
14517a4b0bb2SUlf Lilleengen 	else {
14527a4b0bb2SUlf Lilleengen 		mediasize = g_mediasize(f);
14537a4b0bb2SUlf Lilleengen 		secsize = g_sectorsize(f);
14547a4b0bb2SUlf Lilleengen 		if (secsize < 0 || mediasize < 0) {
1455425bed3aSJoerg Wunsch 			close (f);
145643be698cSBruce Evans 			return (NULL);
1457425bed3aSJoerg Wunsch 		}
14587a4b0bb2SUlf Lilleengen 	}
1459b9d05a16SPoul-Henning Kamp 	memset(&loclab, 0, sizeof loclab);
1460b9d05a16SPoul-Henning Kamp 	loclab.d_magic = DISKMAGIC;
1461b9d05a16SPoul-Henning Kamp 	loclab.d_magic2 = DISKMAGIC;
1462b9d05a16SPoul-Henning Kamp 	loclab.d_secsize = secsize;
1463b9d05a16SPoul-Henning Kamp 	loclab.d_secperunit = mediasize / secsize;
1464b9d05a16SPoul-Henning Kamp 
1465b9d05a16SPoul-Henning Kamp 	/*
14664b85a12fSUlrich Spörlein 	 * Nobody in these enlightened days uses the CHS geometry for
14674b85a12fSUlrich Spörlein 	 * anything, but nonetheless try to get it right.  If we fail
1468b9d05a16SPoul-Henning Kamp 	 * to get any good ideas from the device, construct something
1469b9d05a16SPoul-Henning Kamp 	 * which is IBM-PC friendly.
1470b9d05a16SPoul-Henning Kamp 	 */
1471b9d05a16SPoul-Henning Kamp 	if (ioctl(f, DIOCGFWSECTORS, &u) == 0)
1472b9d05a16SPoul-Henning Kamp 		loclab.d_nsectors = u;
1473b9d05a16SPoul-Henning Kamp 	else
1474b9d05a16SPoul-Henning Kamp 		loclab.d_nsectors = 63;
1475b9d05a16SPoul-Henning Kamp 	if (ioctl(f, DIOCGFWHEADS, &u) == 0)
1476b9d05a16SPoul-Henning Kamp 		loclab.d_ntracks = u;
1477b9d05a16SPoul-Henning Kamp 	else if (loclab.d_secperunit <= 63*1*1024)
1478b9d05a16SPoul-Henning Kamp 		loclab.d_ntracks = 1;
1479b9d05a16SPoul-Henning Kamp 	else if (loclab.d_secperunit <= 63*16*1024)
1480b9d05a16SPoul-Henning Kamp 		loclab.d_ntracks = 16;
1481b9d05a16SPoul-Henning Kamp 	else
1482b9d05a16SPoul-Henning Kamp 		loclab.d_ntracks = 255;
1483b9d05a16SPoul-Henning Kamp 	loclab.d_secpercyl = loclab.d_ntracks * loclab.d_nsectors;
1484b9d05a16SPoul-Henning Kamp 	loclab.d_ncylinders = loclab.d_secperunit / loclab.d_secpercyl;
1485d5718812SMarcel Moolenaar 	loclab.d_npartitions = DEFPARTITIONS;
1486b9d05a16SPoul-Henning Kamp 
1487b9d05a16SPoul-Henning Kamp 	/* Various (unneeded) compat stuff */
1488b9d05a16SPoul-Henning Kamp 	loclab.d_rpm = 3600;
1489b9d05a16SPoul-Henning Kamp 	loclab.d_bbsize = BBSIZE;
149057dfbec5SPoul-Henning Kamp 	loclab.d_interleave = 1;
1491b9d05a16SPoul-Henning Kamp 	strncpy(loclab.d_typename, "amnesiac",
1492b9d05a16SPoul-Henning Kamp 	    sizeof(loclab.d_typename));
1493b9d05a16SPoul-Henning Kamp 
1494b9d05a16SPoul-Henning Kamp 	dp = &loclab.d_partitions[RAW_PART];
1495b9d05a16SPoul-Henning Kamp 	dp->p_size = loclab.d_secperunit;
1496b9d05a16SPoul-Henning Kamp 	loclab.d_checksum = dkcksum(&loclab);
1497425bed3aSJoerg Wunsch 	close (f);
1498c8223965SMark Murray 	return (&loclab);
1499425bed3aSJoerg Wunsch }
1500425bed3aSJoerg Wunsch 
1501d2fe97c7SPoul-Henning Kamp static void
usage(void)1502326c7cdaSWarner Losh usage(void)
15038fae3551SRodney W. Grimes {
1504bc33ea1aSRuslan Ermilov 
1505bc33ea1aSRuslan Ermilov 	fprintf(stderr,
1506bc33ea1aSRuslan 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",
15075daa806dSPoul-Henning Kamp 	"usage: bsdlabel disk",
1508bef2080aSPhilippe Charnier 	"\t\t(to read label)",
1509d2fe97c7SPoul-Henning Kamp 	"	bsdlabel -w [-n] [-m machine] disk [type]",
1510bef2080aSPhilippe Charnier 	"\t\t(to write label with existing boot program)",
15115daa806dSPoul-Henning Kamp 	"	bsdlabel -e [-n] [-m machine] disk",
1512bef2080aSPhilippe Charnier 	"\t\t(to edit label)",
15135daa806dSPoul-Henning Kamp 	"	bsdlabel -R [-n] [-m machine] disk protofile",
1514bef2080aSPhilippe Charnier 	"\t\t(to restore label with existing boot program)",
15152c60b668SPoul-Henning Kamp 	"	bsdlabel -B [-b boot] [-m machine] disk",
1516bef2080aSPhilippe Charnier 	"\t\t(to install boot program with existing on-disk label)",
1517d2fe97c7SPoul-Henning Kamp 	"	bsdlabel -w -B [-n] [-b boot] [-m machine] disk [type]",
1518bef2080aSPhilippe Charnier 	"\t\t(to write label and install boot program)",
15192c60b668SPoul-Henning Kamp 	"	bsdlabel -R -B [-n] [-b boot] [-m machine] disk protofile",
152080baf8ceSPoul-Henning Kamp 		"\t\t(to restore label and install boot program)"
152180baf8ceSPoul-Henning Kamp 	);
15228fae3551SRodney W. Grimes 	exit(1);
15238fae3551SRodney W. Grimes }
1524