xref: /freebsd/sbin/bsdlabel/bsdlabel.c (revision e45d37b2e9c144cf1038b626a63843089ea3ebfe)
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
66d5718812SMarcel Moolenaar #define MAXPARTITIONS	26
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 *);
83*e45d37b2SJaakko Heinonen static int	geom_bsd_available(void);
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;
10857dfbec5SPoul-Henning Kamp static u_int	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;
1327838fd0eSPoul-Henning Kamp static int alphacksum =
1337838fd0eSPoul-Henning Kamp #if defined(__alpha__)
1347838fd0eSPoul-Henning Kamp 	1;
1357838fd0eSPoul-Henning Kamp #else
1367838fd0eSPoul-Henning Kamp 	0;
1377838fd0eSPoul-Henning Kamp #endif
138c80f9755SPoul-Henning Kamp 
1398fae3551SRodney W. Grimes enum	{
140f080d33bSPoul-Henning Kamp 	UNSPEC, EDIT, READ, RESTORE, WRITE, WRITEBOOT
1418fae3551SRodney W. Grimes } op = UNSPEC;
1428fae3551SRodney W. Grimes 
143bc33ea1aSRuslan Ermilov 
144d2fe97c7SPoul-Henning Kamp static int	disable_write;   /* set to disable writing to disk label */
1457747c959SLuigi Rizzo static int	is_file;	/* work on a file (abs. pathname), "-f" opt. */
1468fae3551SRodney W. Grimes 
14761de51caSJoerg Wunsch int
148326c7cdaSWarner Losh main(int argc, char *argv[])
1498fae3551SRodney W. Grimes {
1508fae3551SRodney W. Grimes 	FILE *t;
1517a4b0bb2SUlf Lilleengen 	int ch, error, fd;
1527a4b0bb2SUlf Lilleengen 	const char *name;
1537a4b0bb2SUlf Lilleengen 
1547a4b0bb2SUlf Lilleengen 	error = 0;
1557a4b0bb2SUlf Lilleengen 	name = NULL;
1568fae3551SRodney W. Grimes 
157f72bbf97SMaxim Konovalov 	while ((ch = getopt(argc, argv, "ABb:efm:nRrw")) != -1)
1588fae3551SRodney W. Grimes 		switch (ch) {
15957dfbec5SPoul-Henning Kamp 			case 'A':
16057dfbec5SPoul-Henning Kamp 				allfields = 1;
16157dfbec5SPoul-Henning Kamp 				break;
1628fae3551SRodney W. Grimes 			case 'B':
1638fae3551SRodney W. Grimes 				++installboot;
1648fae3551SRodney W. Grimes 				break;
1658fae3551SRodney W. Grimes 			case 'b':
1668fae3551SRodney W. Grimes 				xxboot = optarg;
1678fae3551SRodney W. Grimes 				break;
1687747c959SLuigi Rizzo 			case 'f':
1697747c959SLuigi Rizzo 				is_file=1;
1707747c959SLuigi Rizzo 				break;
171bc33ea1aSRuslan Ermilov 			case 'm':
172246300f2SPoul-Henning Kamp 				if (!strcmp(optarg, "i386") ||
173246300f2SPoul-Henning Kamp 				    !strcmp(optarg, "amd64") ||
174246300f2SPoul-Henning Kamp 				    !strcmp(optarg, "ia64") ||
175246300f2SPoul-Henning Kamp 				    !strcmp(optarg, "pc98")) {
176b2bb9f9bSPoul-Henning Kamp 					labelsoffset = 1;
177b2bb9f9bSPoul-Henning Kamp 					labeloffset = 0;
1783ecb3802SPoul-Henning Kamp 					bbsize = 8192;
1793ecb3802SPoul-Henning Kamp 					alphacksum = 0;
180c80f9755SPoul-Henning Kamp 				} else if (!strcmp(optarg, "alpha")) {
181b2bb9f9bSPoul-Henning Kamp 					labelsoffset = 0;
182c80f9755SPoul-Henning Kamp 					labeloffset = 64;
183c80f9755SPoul-Henning Kamp 					bbsize = 8192;
184c80f9755SPoul-Henning Kamp 					alphacksum = 1;
185d2fe97c7SPoul-Henning Kamp 				} else {
186d2fe97c7SPoul-Henning Kamp 					errx(1, "Unsupported architecture");
187c80f9755SPoul-Henning Kamp 				}
188bc33ea1aSRuslan Ermilov 				break;
1893233afaeSJohn W. De Boskey 			case 'n':
1903233afaeSJohn W. De Boskey 				disable_write = 1;
1913233afaeSJohn W. De Boskey 				break;
1928fae3551SRodney W. Grimes 			case 'R':
1938fae3551SRodney W. Grimes 				if (op != UNSPEC)
1948fae3551SRodney W. Grimes 					usage();
1958fae3551SRodney W. Grimes 				op = RESTORE;
1968fae3551SRodney W. Grimes 				break;
1978fae3551SRodney W. Grimes 			case 'e':
1988fae3551SRodney W. Grimes 				if (op != UNSPEC)
1998fae3551SRodney W. Grimes 					usage();
2008fae3551SRodney W. Grimes 				op = EDIT;
2018fae3551SRodney W. Grimes 				break;
2028fae3551SRodney W. Grimes 			case 'r':
203d2fe97c7SPoul-Henning Kamp 				/*
204d2fe97c7SPoul-Henning Kamp 				 * We accept and ignode -r for compatibility with
205d2fe97c7SPoul-Henning Kamp 				 * historically disklabel usage.
206d2fe97c7SPoul-Henning Kamp 				 */
2078fae3551SRodney W. Grimes 				break;
2088fae3551SRodney W. Grimes 			case 'w':
2098fae3551SRodney W. Grimes 				if (op != UNSPEC)
2108fae3551SRodney W. Grimes 					usage();
2118fae3551SRodney W. Grimes 				op = WRITE;
2128fae3551SRodney W. Grimes 				break;
2138fae3551SRodney W. Grimes 			case '?':
2148fae3551SRodney W. Grimes 			default:
2158fae3551SRodney W. Grimes 				usage();
2168fae3551SRodney W. Grimes 		}
2178fae3551SRodney W. Grimes 	argc -= optind;
2188fae3551SRodney W. Grimes 	argv += optind;
219d2fe97c7SPoul-Henning Kamp 
2208fae3551SRodney W. Grimes 	if (argc < 1)
2218fae3551SRodney W. Grimes 		usage();
2229ef521ecSPoul-Henning Kamp 	if (labelsoffset < 0 || labeloffset < 0)
2239ef521ecSPoul-Henning Kamp 		errx(1, "a -m <architecture> option must be specified");
2248fae3551SRodney W. Grimes 
2255daa806dSPoul-Henning Kamp 	/* Figure out the names of the thing we're working on */
2267747c959SLuigi Rizzo 	if (is_file) {
227ccdd2fceSRalf S. Engelschall 		specname = argv[0];
2285daa806dSPoul-Henning Kamp 	} else {
2297a4b0bb2SUlf Lilleengen 		specname = g_device_path(argv[0]);
2307a4b0bb2SUlf Lilleengen 		if (specname == NULL) {
2317a4b0bb2SUlf Lilleengen 			warn("unable to get correct path for %s", argv[0]);
2327a4b0bb2SUlf Lilleengen 			return(1);
2337a4b0bb2SUlf Lilleengen 		}
2347a4b0bb2SUlf Lilleengen 		fd = open(specname, O_RDONLY);
2357a4b0bb2SUlf Lilleengen 		if (fd < 0) {
2367a4b0bb2SUlf Lilleengen 			warn("error opening %s", specname);
2377a4b0bb2SUlf Lilleengen 			return(1);
2387a4b0bb2SUlf Lilleengen 		}
2397a4b0bb2SUlf Lilleengen 		pname = g_providername(fd);
2407a4b0bb2SUlf Lilleengen 		if (pname == NULL) {
241bd5add58SUlf Lilleengen 			warn("error getting providername for %s", specname);
2429d8ce078SUlf Lilleengen 			close(fd);
2437a4b0bb2SUlf Lilleengen 			return(1);
2447a4b0bb2SUlf Lilleengen 		}
2459d8ce078SUlf Lilleengen 		close(fd);
2468fae3551SRodney W. Grimes 	}
247d2fe97c7SPoul-Henning Kamp 
248d2fe97c7SPoul-Henning Kamp 	if (installboot && op == UNSPEC)
249d2fe97c7SPoul-Henning Kamp 		op = WRITEBOOT;
250d2fe97c7SPoul-Henning Kamp 	else if (op == UNSPEC)
251d2fe97c7SPoul-Henning Kamp 		op = READ;
2528fae3551SRodney W. Grimes 
2538fae3551SRodney W. Grimes 	switch(op) {
2548fae3551SRodney W. Grimes 
2553b3038a6SBill Fumerola 	case UNSPEC:
2563b3038a6SBill Fumerola 		break;
2573b3038a6SBill Fumerola 
2588fae3551SRodney W. Grimes 	case EDIT:
2598fae3551SRodney W. Grimes 		if (argc != 1)
2608fae3551SRodney W. Grimes 			usage();
261d2fe97c7SPoul-Henning Kamp 		readlabel(1);
262427823d5SPoul-Henning Kamp 		fixlabel(&lab);
263d2fe97c7SPoul-Henning Kamp 		error = edit();
2648fae3551SRodney W. Grimes 		break;
2658fae3551SRodney W. Grimes 
2668fae3551SRodney W. Grimes 	case READ:
2678fae3551SRodney W. Grimes 		if (argc != 1)
2688fae3551SRodney W. Grimes 			usage();
269d2fe97c7SPoul-Henning Kamp 		readlabel(1);
270d2fe97c7SPoul-Henning Kamp 		display(stdout, NULL);
271d2fe97c7SPoul-Henning Kamp 		error = checklabel(NULL);
2728fae3551SRodney W. Grimes 		break;
2738fae3551SRodney W. Grimes 
2748fae3551SRodney W. Grimes 	case RESTORE:
2758fae3551SRodney W. Grimes 		if (argc != 2)
2768fae3551SRodney W. Grimes 			usage();
2778fae3551SRodney W. Grimes 		if (!(t = fopen(argv[1], "r")))
2785daa806dSPoul-Henning Kamp 			err(4, "fopen %s", argv[1]);
279d2fe97c7SPoul-Henning Kamp 		readlabel(0);
2806cabb348SBruce Evans 		if (!getasciilabel(t, &lab))
2816cabb348SBruce Evans 			exit(1);
282d2fe97c7SPoul-Henning Kamp 		error = writelabel();
2838fae3551SRodney W. Grimes 		break;
2848fae3551SRodney W. Grimes 
2858fae3551SRodney W. Grimes 	case WRITE:
286d2fe97c7SPoul-Henning Kamp 		if (argc == 2)
287d2fe97c7SPoul-Henning Kamp 			name = argv[1];
288d2fe97c7SPoul-Henning Kamp 		else if (argc == 1)
289d2fe97c7SPoul-Henning Kamp 			name = "auto";
290d2fe97c7SPoul-Henning Kamp 		else
2918fae3551SRodney W. Grimes 			usage();
292d2fe97c7SPoul-Henning Kamp 		readlabel(0);
293d2fe97c7SPoul-Henning Kamp 		makelabel(name, &lab);
294427823d5SPoul-Henning Kamp 		fixlabel(&lab);
295d2fe97c7SPoul-Henning Kamp 		if (checklabel(NULL) == 0)
296d2fe97c7SPoul-Henning Kamp 			error = writelabel();
2978fae3551SRodney W. Grimes 		break;
2988fae3551SRodney W. Grimes 
2998fae3551SRodney W. Grimes 	case WRITEBOOT:
3008fae3551SRodney W. Grimes 
301d2fe97c7SPoul-Henning Kamp 		readlabel(1);
302427823d5SPoul-Henning Kamp 		fixlabel(&lab);
3038fae3551SRodney W. Grimes 		if (argc == 2)
304d2fe97c7SPoul-Henning Kamp 			makelabel(argv[1], &lab);
305d2fe97c7SPoul-Henning Kamp 		if (checklabel(NULL) == 0)
306d2fe97c7SPoul-Henning Kamp 			error = writelabel();
3078fae3551SRodney W. Grimes 		break;
3088fae3551SRodney W. Grimes 	}
3098fae3551SRodney W. Grimes 	exit(error);
3108fae3551SRodney W. Grimes }
3118fae3551SRodney W. Grimes 
312427823d5SPoul-Henning Kamp static void
313427823d5SPoul-Henning Kamp fixlabel(struct disklabel *lp)
314427823d5SPoul-Henning Kamp {
315427823d5SPoul-Henning Kamp 	struct partition *dp;
316427823d5SPoul-Henning Kamp 	int i;
317427823d5SPoul-Henning Kamp 
318d5718812SMarcel Moolenaar 	for (i = 0; i < lp->d_npartitions; i++) {
319427823d5SPoul-Henning Kamp 		if (i == RAW_PART)
320427823d5SPoul-Henning Kamp 			continue;
321427823d5SPoul-Henning Kamp 		if (lp->d_partitions[i].p_size)
322427823d5SPoul-Henning Kamp 			return;
323427823d5SPoul-Henning Kamp 	}
324427823d5SPoul-Henning Kamp 
325427823d5SPoul-Henning Kamp 	dp = &lp->d_partitions[0];
326427823d5SPoul-Henning Kamp 	dp->p_offset = BBSIZE / secsize;
327427823d5SPoul-Henning Kamp 	dp->p_size = lp->d_secperunit - dp->p_offset;
328427823d5SPoul-Henning Kamp }
329427823d5SPoul-Henning Kamp 
3308fae3551SRodney W. Grimes /*
331ef9ab0b3SRuslan Ermilov  * Construct a prototype disklabel from /etc/disktab.
3328fae3551SRodney W. Grimes  */
333d2fe97c7SPoul-Henning Kamp static void
334d2fe97c7SPoul-Henning Kamp makelabel(const char *type, struct disklabel *lp)
3358fae3551SRodney W. Grimes {
336326c7cdaSWarner Losh 	struct disklabel *dp;
337425bed3aSJoerg Wunsch 
338425bed3aSJoerg Wunsch 	if (strcmp(type, "auto") == 0)
339425bed3aSJoerg Wunsch 		dp = getvirginlabel();
340425bed3aSJoerg Wunsch 	else
3418fae3551SRodney W. Grimes 		dp = getdiskbyname(type);
3426bd343a9SPhilippe Charnier 	if (dp == NULL)
3436bd343a9SPhilippe Charnier 		errx(1, "%s: unknown disk type", type);
3448fae3551SRodney W. Grimes 	*lp = *dp;
3458fae3551SRodney W. Grimes 	bzero(lp->d_packname, sizeof(lp->d_packname));
3468fae3551SRodney W. Grimes }
3478fae3551SRodney W. Grimes 
348d2fe97c7SPoul-Henning Kamp static void
349d2fe97c7SPoul-Henning Kamp readboot(void)
350d2fe97c7SPoul-Henning Kamp {
351049a9793SJaakko Heinonen 	int fd;
352d2fe97c7SPoul-Henning Kamp 	struct stat st;
353c8785325SJohn Baldwin 	uint64_t *p;
354d2fe97c7SPoul-Henning Kamp 
355d2fe97c7SPoul-Henning Kamp 	if (xxboot == NULL)
356d2fe97c7SPoul-Henning Kamp 		xxboot = "/boot/boot";
357d2fe97c7SPoul-Henning Kamp 	fd = open(xxboot, O_RDONLY);
358d2fe97c7SPoul-Henning Kamp 	if (fd < 0)
359d2fe97c7SPoul-Henning Kamp 		err(1, "cannot open %s", xxboot);
360d2fe97c7SPoul-Henning Kamp 	fstat(fd, &st);
36154f445fbSPoul-Henning Kamp 	if (alphacksum && st.st_size <= BBSIZE - 512) {
362049a9793SJaakko Heinonen 		if (read(fd, bootarea + 512, st.st_size) != st.st_size)
363d2fe97c7SPoul-Henning Kamp 			err(1, "read error %s", xxboot);
364c8785325SJohn Baldwin 
365c8785325SJohn Baldwin 		/*
366c8785325SJohn Baldwin 		 * Set the location and length so SRM can find the
367c8785325SJohn Baldwin 		 * boot blocks.
368c8785325SJohn Baldwin 		 */
369c8785325SJohn Baldwin 		p = (uint64_t *)bootarea;
370c8785325SJohn Baldwin 		p[60] = (st.st_size + secsize - 1) / secsize;
371c8785325SJohn Baldwin 		p[61] = 1;
372c8785325SJohn Baldwin 		p[62] = 0;
373d2fe97c7SPoul-Henning Kamp 		return;
37454f445fbSPoul-Henning Kamp 	} else if ((!alphacksum) && st.st_size <= BBSIZE) {
375049a9793SJaakko Heinonen 		if (read(fd, bootarea, st.st_size) != st.st_size)
376d2fe97c7SPoul-Henning Kamp 			err(1, "read error %s", xxboot);
377d2fe97c7SPoul-Henning Kamp 		return;
378d2fe97c7SPoul-Henning Kamp 	}
379d2fe97c7SPoul-Henning Kamp 	errx(1, "boot code %s is wrong size", xxboot);
380d2fe97c7SPoul-Henning Kamp }
381d2fe97c7SPoul-Henning Kamp 
382d2fe97c7SPoul-Henning Kamp static int
383*e45d37b2SJaakko Heinonen geom_bsd_available(void)
384*e45d37b2SJaakko Heinonen {
385*e45d37b2SJaakko Heinonen 	struct gclass *class;
386*e45d37b2SJaakko Heinonen 	struct gmesh mesh;
387*e45d37b2SJaakko Heinonen 	int error;
388*e45d37b2SJaakko Heinonen 
389*e45d37b2SJaakko Heinonen 	error = geom_gettree(&mesh);
390*e45d37b2SJaakko Heinonen 	if (error != 0)
391*e45d37b2SJaakko Heinonen 		errc(1, error, "Cannot get GEOM tree");
392*e45d37b2SJaakko Heinonen 
393*e45d37b2SJaakko Heinonen 	LIST_FOREACH(class, &mesh.lg_class, lg_class) {
394*e45d37b2SJaakko Heinonen 		if (strcmp(class->lg_name, "BSD") == 0) {
395*e45d37b2SJaakko Heinonen 			geom_deletetree(&mesh);
396*e45d37b2SJaakko Heinonen 			return (1);
397*e45d37b2SJaakko Heinonen 		}
398*e45d37b2SJaakko Heinonen 	}
399*e45d37b2SJaakko Heinonen 
400*e45d37b2SJaakko Heinonen 	geom_deletetree(&mesh);
401*e45d37b2SJaakko Heinonen 
402*e45d37b2SJaakko Heinonen 	return (0);
403*e45d37b2SJaakko Heinonen }
404*e45d37b2SJaakko Heinonen 
405*e45d37b2SJaakko Heinonen static int
406d2fe97c7SPoul-Henning Kamp writelabel(void)
4078fae3551SRodney W. Grimes {
408bc33ea1aSRuslan Ermilov 	uint64_t *p, sum;
409*e45d37b2SJaakko Heinonen 	int i, fd, serrno;
4105daa806dSPoul-Henning Kamp 	struct gctl_req *grq;
4115daa806dSPoul-Henning Kamp 	char const *errstr;
412d2fe97c7SPoul-Henning Kamp 	struct disklabel *lp = &lab;
413130cd73aSDoug Rabson 
4143233afaeSJohn W. De Boskey 	if (disable_write) {
41557dfbec5SPoul-Henning Kamp 		warnx("write to disk label supressed - label was as follows:");
416d2fe97c7SPoul-Henning Kamp 		display(stdout, NULL);
4173233afaeSJohn W. De Boskey 		return (0);
41880baf8ceSPoul-Henning Kamp 	}
41980baf8ceSPoul-Henning Kamp 
4208fae3551SRodney W. Grimes 	lp->d_magic = DISKMAGIC;
4218fae3551SRodney W. Grimes 	lp->d_magic2 = DISKMAGIC;
4228fae3551SRodney W. Grimes 	lp->d_checksum = 0;
4238fae3551SRodney W. Grimes 	lp->d_checksum = dkcksum(lp);
424d2fe97c7SPoul-Henning Kamp 	if (installboot)
425d2fe97c7SPoul-Henning Kamp 		readboot();
4265d853216SPoul-Henning Kamp 	for (i = 0; i < lab.d_npartitions; i++)
4275d853216SPoul-Henning Kamp 		if (lab.d_partitions[i].p_size)
428eb0ea23eSMarcel Moolenaar 			lab.d_partitions[i].p_offset += lba_offset;
429b2bb9f9bSPoul-Henning Kamp 	bsd_disklabel_le_enc(bootarea + labeloffset + labelsoffset * secsize,
430b2bb9f9bSPoul-Henning Kamp 	    lp);
431dfbc3f0cSPoul-Henning Kamp 	if (alphacksum) {
4325daa806dSPoul-Henning Kamp 		/* Generate the bootblock checksum for the SRM console.  */
433d2fe97c7SPoul-Henning Kamp 		for (p = (uint64_t *)bootarea, i = 0, sum = 0; i < 63; i++)
434130cd73aSDoug Rabson 			sum += p[i];
435130cd73aSDoug Rabson 		p[63] = sum;
436bc33ea1aSRuslan Ermilov 	}
4375daa806dSPoul-Henning Kamp 
4385daa806dSPoul-Henning Kamp 	fd = open(specname, O_RDWR);
4395daa806dSPoul-Henning Kamp 	if (fd < 0) {
4407747c959SLuigi Rizzo 		if (is_file) {
4417747c959SLuigi Rizzo 			warn("cannot open file %s for writing label", specname);
4427747c959SLuigi Rizzo 			return(1);
443*e45d37b2SJaakko Heinonen 		} else
444*e45d37b2SJaakko Heinonen 			serrno = errno;
445*e45d37b2SJaakko Heinonen 
446*e45d37b2SJaakko Heinonen 		/* Give up if GEOM_BSD is not available. */
447*e45d37b2SJaakko Heinonen 		if (geom_bsd_available() == 0) {
448*e45d37b2SJaakko Heinonen 			warnc(serrno, "%s", specname);
449*e45d37b2SJaakko Heinonen 			return (1);
4507747c959SLuigi Rizzo 		}
4517a4b0bb2SUlf Lilleengen 
45283d771deSPoul-Henning Kamp 		grq = gctl_get_handle();
45383d771deSPoul-Henning Kamp 		gctl_ro_param(grq, "verb", -1, "write label");
4545daa806dSPoul-Henning Kamp 		gctl_ro_param(grq, "class", -1, "BSD");
4557a4b0bb2SUlf Lilleengen 		gctl_ro_param(grq, "geom", -1, pname);
456b2bb9f9bSPoul-Henning Kamp 		gctl_ro_param(grq, "label", 148+16*8,
457b2bb9f9bSPoul-Henning Kamp 			bootarea + labeloffset + labelsoffset * secsize);
4585daa806dSPoul-Henning Kamp 		errstr = gctl_issue(grq);
459d2fe97c7SPoul-Henning Kamp 		if (errstr != NULL) {
460d2fe97c7SPoul-Henning Kamp 			warnx("%s", errstr);
461d2fe97c7SPoul-Henning Kamp 			gctl_free(grq);
462d2fe97c7SPoul-Henning Kamp 			return(1);
463d2fe97c7SPoul-Henning Kamp 		}
4645daa806dSPoul-Henning Kamp 		gctl_free(grq);
4655daa806dSPoul-Henning Kamp 		if (installboot) {
46683d771deSPoul-Henning Kamp 			grq = gctl_get_handle();
46783d771deSPoul-Henning Kamp 			gctl_ro_param(grq, "verb", -1, "write bootcode");
4685daa806dSPoul-Henning Kamp 			gctl_ro_param(grq, "class", -1, "BSD");
4697a4b0bb2SUlf Lilleengen 			gctl_ro_param(grq, "geom", -1, pname);
470d2fe97c7SPoul-Henning Kamp 			gctl_ro_param(grq, "bootcode", BBSIZE, bootarea);
4715daa806dSPoul-Henning Kamp 			errstr = gctl_issue(grq);
472d2fe97c7SPoul-Henning Kamp 			if (errstr != NULL) {
473d2fe97c7SPoul-Henning Kamp 				warnx("%s", errstr);
474d2fe97c7SPoul-Henning Kamp 				gctl_free(grq);
475d2fe97c7SPoul-Henning Kamp 				return (1);
476d2fe97c7SPoul-Henning Kamp 			}
4775daa806dSPoul-Henning Kamp 			gctl_free(grq);
4785daa806dSPoul-Henning Kamp 		}
4795daa806dSPoul-Henning Kamp 	} else {
480d2fe97c7SPoul-Henning Kamp 		if (write(fd, bootarea, bbsize) != bbsize) {
4815daa806dSPoul-Henning Kamp 			warn("write %s", specname);
4825daa806dSPoul-Henning Kamp 			close (fd);
4838fae3551SRodney W. Grimes 			return (1);
4848fae3551SRodney W. Grimes 		}
4855daa806dSPoul-Henning Kamp 		close (fd);
4865daa806dSPoul-Henning Kamp 	}
4878fae3551SRodney W. Grimes 	return (0);
4888fae3551SRodney W. Grimes }
4898fae3551SRodney W. Grimes 
4907747c959SLuigi Rizzo static void
4917747c959SLuigi Rizzo get_file_parms(int f)
4927747c959SLuigi Rizzo {
4937747c959SLuigi Rizzo 	int i;
4947747c959SLuigi Rizzo 	struct stat sb;
4957747c959SLuigi Rizzo 
4967747c959SLuigi Rizzo 	if (fstat(f, &sb) != 0)
4977747c959SLuigi Rizzo 		err(4, "fstat failed");
4987747c959SLuigi Rizzo 	i = sb.st_mode & S_IFMT;
4997747c959SLuigi Rizzo 	if (i != S_IFREG && i != S_IFLNK)
5007747c959SLuigi Rizzo 		errx(4, "%s is not a valid file or link", specname);
5017747c959SLuigi Rizzo 	secsize = DEV_BSIZE;
5027747c959SLuigi Rizzo 	mediasize = sb.st_size;
5037747c959SLuigi Rizzo }
5047747c959SLuigi Rizzo 
5058fae3551SRodney W. Grimes /*
5068fae3551SRodney W. Grimes  * Fetch disklabel for disk.
5078fae3551SRodney W. Grimes  */
508d2fe97c7SPoul-Henning Kamp static int
509d2fe97c7SPoul-Henning Kamp readlabel(int flag)
5108fae3551SRodney W. Grimes {
511049a9793SJaakko Heinonen 	ssize_t nbytes;
512eb0ea23eSMarcel Moolenaar 	uint32_t lba;
5135d853216SPoul-Henning Kamp 	int f, i;
5145daa806dSPoul-Henning Kamp 	int error;
5158fae3551SRodney W. Grimes 
5165daa806dSPoul-Henning Kamp 	f = open(specname, O_RDONLY);
5175daa806dSPoul-Henning Kamp 	if (f < 0)
5185daa806dSPoul-Henning Kamp 		err(1, specname);
5197747c959SLuigi Rizzo 	if (is_file)
5207747c959SLuigi Rizzo 		get_file_parms(f);
5217a4b0bb2SUlf Lilleengen 	else {
5227a4b0bb2SUlf Lilleengen 		mediasize = g_mediasize(f);
5237a4b0bb2SUlf Lilleengen 		secsize = g_sectorsize(f);
5247a4b0bb2SUlf Lilleengen 		if (secsize < 0 || mediasize < 0)
525b2bb9f9bSPoul-Henning Kamp 			err(4, "cannot get disk geometry");
526b2bb9f9bSPoul-Henning Kamp 	}
52786e0bd0dSBrooks Davis 	if (mediasize > (off_t)0xffffffff * secsize)
52886e0bd0dSBrooks Davis 		errx(1,
52986e0bd0dSBrooks Davis 		    "disks with more than 2^32-1 sectors are not supported");
530c80f9755SPoul-Henning Kamp 	(void)lseek(f, (off_t)0, SEEK_SET);
531049a9793SJaakko Heinonen 	nbytes = read(f, bootarea, BBSIZE);
532049a9793SJaakko Heinonen 	if (nbytes == -1)
5335daa806dSPoul-Henning Kamp 		err(4, "%s read", specname);
534049a9793SJaakko Heinonen 	if (nbytes != BBSIZE)
535049a9793SJaakko Heinonen 		errx(4, "couldn't read %d bytes from %s", BBSIZE, specname);
5365daa806dSPoul-Henning Kamp 	close (f);
537b2bb9f9bSPoul-Henning Kamp 	error = bsd_disklabel_le_dec(
538b2bb9f9bSPoul-Henning Kamp 	    bootarea + (labeloffset + labelsoffset * secsize),
539b2bb9f9bSPoul-Henning Kamp 	    &lab, MAXPARTITIONS);
540d2fe97c7SPoul-Henning Kamp 	if (flag && error)
541d2fe97c7SPoul-Henning Kamp 		errx(1, "%s: no valid label found", specname);
5425d853216SPoul-Henning Kamp 
5437a4b0bb2SUlf Lilleengen 	if (is_file)
5447a4b0bb2SUlf Lilleengen 		return(0);
545eb0ea23eSMarcel Moolenaar 
546eb0ea23eSMarcel Moolenaar 	/*
547eb0ea23eSMarcel Moolenaar 	 * Compensate for absolute block addressing by finding the
548eb0ea23eSMarcel Moolenaar 	 * smallest partition offset and if the offset of the 'c'
549eb0ea23eSMarcel Moolenaar 	 * partition is equal to that, subtract it from all offsets.
550eb0ea23eSMarcel Moolenaar 	 */
551eb0ea23eSMarcel Moolenaar 	lba = ~0;
552eb0ea23eSMarcel Moolenaar 	for (i = 0; i < lab.d_npartitions; i++) {
5535d853216SPoul-Henning Kamp 		if (lab.d_partitions[i].p_size)
554eb0ea23eSMarcel Moolenaar 			lba = MIN(lba, lab.d_partitions[i].p_offset);
555eb0ea23eSMarcel Moolenaar 	}
556eb0ea23eSMarcel Moolenaar 	if (lba != 0 && lab.d_partitions[RAW_PART].p_offset == lba) {
557eb0ea23eSMarcel Moolenaar 		for (i = 0; i < lab.d_npartitions; i++) {
558eb0ea23eSMarcel Moolenaar 			if (lab.d_partitions[i].p_size)
559eb0ea23eSMarcel Moolenaar 				lab.d_partitions[i].p_offset -= lba;
560eb0ea23eSMarcel Moolenaar 		}
561eb0ea23eSMarcel Moolenaar 		/*
562eb0ea23eSMarcel Moolenaar 		 * Save the offset so that we can write the label
563eb0ea23eSMarcel Moolenaar 		 * back with absolute block addresses.
564eb0ea23eSMarcel Moolenaar 		 */
565eb0ea23eSMarcel Moolenaar 		lba_offset = lba;
566eb0ea23eSMarcel Moolenaar 	}
567d2fe97c7SPoul-Henning Kamp 	return (error);
5688fae3551SRodney W. Grimes }
5698fae3551SRodney W. Grimes 
5708fae3551SRodney W. Grimes 
571d2fe97c7SPoul-Henning Kamp static void
572326c7cdaSWarner Losh display(FILE *f, const struct disklabel *lp)
5738fae3551SRodney W. Grimes {
574326c7cdaSWarner Losh 	int i, j;
575326c7cdaSWarner Losh 	const struct partition *pp;
5768fae3551SRodney W. Grimes 
577d2fe97c7SPoul-Henning Kamp 	if (lp == NULL)
578d2fe97c7SPoul-Henning Kamp 		lp = &lab;
579d2fe97c7SPoul-Henning Kamp 
5808fae3551SRodney W. Grimes 	fprintf(f, "# %s:\n", specname);
58157dfbec5SPoul-Henning Kamp 	if (allfields) {
582484c7804SJulian Elischer 		if (lp->d_type < DKMAXTYPES)
5838fae3551SRodney W. Grimes 			fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
5848fae3551SRodney W. Grimes 		else
5852a1deaaaSBruce Evans 			fprintf(f, "type: %u\n", lp->d_type);
58661de51caSJoerg Wunsch 		fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
58761de51caSJoerg Wunsch 			lp->d_typename);
58861de51caSJoerg Wunsch 		fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
58961de51caSJoerg Wunsch 			lp->d_packname);
5908fae3551SRodney W. Grimes 		fprintf(f, "flags:");
5918fae3551SRodney W. Grimes 		if (lp->d_flags & D_REMOVABLE)
5928fae3551SRodney W. Grimes 			fprintf(f, " removeable");
5938fae3551SRodney W. Grimes 		if (lp->d_flags & D_ECC)
5948fae3551SRodney W. Grimes 			fprintf(f, " ecc");
5958fae3551SRodney W. Grimes 		if (lp->d_flags & D_BADSECT)
5968fae3551SRodney W. Grimes 			fprintf(f, " badsect");
5978fae3551SRodney W. Grimes 		fprintf(f, "\n");
5982a1deaaaSBruce Evans 		fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
5992a1deaaaSBruce Evans 		fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
6002a1deaaaSBruce Evans 		fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
6012a1deaaaSBruce Evans 		fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
6022a1deaaaSBruce Evans 		fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
6032a1deaaaSBruce Evans 		fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
6042a1deaaaSBruce Evans 		fprintf(f, "rpm: %u\n", lp->d_rpm);
6052a1deaaaSBruce Evans 		fprintf(f, "interleave: %u\n", lp->d_interleave);
6062a1deaaaSBruce Evans 		fprintf(f, "trackskew: %u\n", lp->d_trackskew);
6072a1deaaaSBruce Evans 		fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
6082a1deaaaSBruce Evans 		fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
6092a1deaaaSBruce Evans 		    (u_long)lp->d_headswitch);
61061de51caSJoerg Wunsch 		fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
6112a1deaaaSBruce Evans 		    (u_long)lp->d_trkseek);
6128fae3551SRodney W. Grimes 		fprintf(f, "drivedata: ");
6138fae3551SRodney W. Grimes 		for (i = NDDATA - 1; i >= 0; i--)
6148fae3551SRodney W. Grimes 			if (lp->d_drivedata[i])
6158fae3551SRodney W. Grimes 				break;
6168fae3551SRodney W. Grimes 		if (i < 0)
6178fae3551SRodney W. Grimes 			i = 0;
6188fae3551SRodney W. Grimes 		for (j = 0; j <= i; j++)
6192a1deaaaSBruce Evans 			fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
62057dfbec5SPoul-Henning Kamp 		fprintf(f, "\n\n");
62157dfbec5SPoul-Henning Kamp 	}
62257dfbec5SPoul-Henning Kamp 	fprintf(f, "%u partitions:\n", lp->d_npartitions);
6238fae3551SRodney W. Grimes 	fprintf(f,
624ca4693edSJustin T. Gibbs 	    "#        size   offset    fstype   [fsize bsize bps/cpg]\n");
6258fae3551SRodney W. Grimes 	pp = lp->d_partitions;
6268fae3551SRodney W. Grimes 	for (i = 0; i < lp->d_npartitions; i++, pp++) {
6278fae3551SRodney W. Grimes 		if (pp->p_size) {
6282a1deaaaSBruce Evans 			fprintf(f, "  %c: %8lu %8lu  ", 'a' + i,
6292a1deaaaSBruce Evans 			   (u_long)pp->p_size, (u_long)pp->p_offset);
630484c7804SJulian Elischer 			if (pp->p_fstype < FSMAXTYPES)
6318fae3551SRodney W. Grimes 				fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
6328fae3551SRodney W. Grimes 			else
6338fae3551SRodney W. Grimes 				fprintf(f, "%8d", pp->p_fstype);
6348fae3551SRodney W. Grimes 			switch (pp->p_fstype) {
6358fae3551SRodney W. Grimes 
6368fae3551SRodney W. Grimes 			case FS_UNUSED:				/* XXX */
6372a1deaaaSBruce Evans 				fprintf(f, "    %5lu %5lu %5.5s ",
6382a1deaaaSBruce Evans 				    (u_long)pp->p_fsize,
6392a1deaaaSBruce Evans 				    (u_long)(pp->p_fsize * pp->p_frag), "");
6408fae3551SRodney W. Grimes 				break;
6418fae3551SRodney W. Grimes 
6428fae3551SRodney W. Grimes 			case FS_BSDFFS:
6432a1deaaaSBruce Evans 				fprintf(f, "    %5lu %5lu %5u ",
6442a1deaaaSBruce Evans 				    (u_long)pp->p_fsize,
6452a1deaaaSBruce Evans 				    (u_long)(pp->p_fsize * pp->p_frag),
6468fae3551SRodney W. Grimes 				    pp->p_cpg);
6478fae3551SRodney W. Grimes 				break;
6488fae3551SRodney W. Grimes 
649ca4693edSJustin T. Gibbs 			case FS_BSDLFS:
6502a1deaaaSBruce Evans 				fprintf(f, "    %5lu %5lu %5d",
6512a1deaaaSBruce Evans 				    (u_long)pp->p_fsize,
6522a1deaaaSBruce Evans 				    (u_long)(pp->p_fsize * pp->p_frag),
653ca4693edSJustin T. Gibbs 				    pp->p_cpg);
654ca4693edSJustin T. Gibbs 				break;
655ca4693edSJustin T. Gibbs 
6568fae3551SRodney W. Grimes 			default:
6578fae3551SRodney W. Grimes 				fprintf(f, "%20.20s", "");
6588fae3551SRodney W. Grimes 				break;
6598fae3551SRodney W. Grimes 			}
66057dfbec5SPoul-Henning Kamp 			if (i == RAW_PART) {
66157dfbec5SPoul-Henning Kamp 				fprintf(f, "  # \"raw\" part, don't edit");
66257dfbec5SPoul-Henning Kamp 			}
663d2fe97c7SPoul-Henning Kamp 			fprintf(f, "\n");
6648fae3551SRodney W. Grimes 		}
6658fae3551SRodney W. Grimes 	}
6668fae3551SRodney W. Grimes 	fflush(f);
6678fae3551SRodney W. Grimes }
6688fae3551SRodney W. Grimes 
669d2fe97c7SPoul-Henning Kamp static int
670d2fe97c7SPoul-Henning Kamp edit(void)
6718fae3551SRodney W. Grimes {
672326c7cdaSWarner Losh 	int c, fd;
6738fae3551SRodney W. Grimes 	struct disklabel label;
674722ceb3fSWarner Losh 	FILE *fp;
6758fae3551SRodney W. Grimes 
676722ceb3fSWarner Losh 	if ((fd = mkstemp(tmpfil)) == -1 ||
677722ceb3fSWarner Losh 	    (fp = fdopen(fd, "w")) == NULL) {
6786bd343a9SPhilippe Charnier 		warnx("can't create %s", tmpfil);
6798fae3551SRodney W. Grimes 		return (1);
6808fae3551SRodney W. Grimes 	}
681d2fe97c7SPoul-Henning Kamp 	display(fp, NULL);
682722ceb3fSWarner Losh 	fclose(fp);
6838fae3551SRodney W. Grimes 	for (;;) {
6848fae3551SRodney W. Grimes 		if (!editit())
6858fae3551SRodney W. Grimes 			break;
686722ceb3fSWarner Losh 		fp = fopen(tmpfil, "r");
687722ceb3fSWarner Losh 		if (fp == NULL) {
6886bd343a9SPhilippe Charnier 			warnx("can't reopen %s for reading", tmpfil);
6898fae3551SRodney W. Grimes 			break;
6908fae3551SRodney W. Grimes 		}
6918fae3551SRodney W. Grimes 		bzero((char *)&label, sizeof(label));
692d2fe97c7SPoul-Henning Kamp 		c = getasciilabel(fp, &label);
693722ceb3fSWarner Losh 		fclose(fp);
694d2fe97c7SPoul-Henning Kamp 		if (c) {
695d2fe97c7SPoul-Henning Kamp 			lab = label;
696d2fe97c7SPoul-Henning Kamp 			if (writelabel() == 0) {
6978fae3551SRodney W. Grimes 				(void) unlink(tmpfil);
6988fae3551SRodney W. Grimes 				return (0);
6998fae3551SRodney W. Grimes 			}
7008fae3551SRodney W. Grimes 		}
701d2fe97c7SPoul-Henning Kamp 		printf("re-edit the label? [y]: ");
702d2fe97c7SPoul-Henning Kamp 		fflush(stdout);
7038fae3551SRodney W. Grimes 		c = getchar();
7048fae3551SRodney W. Grimes 		if (c != EOF && c != (int)'\n')
7058fae3551SRodney W. Grimes 			while (getchar() != (int)'\n')
7068fae3551SRodney W. Grimes 				;
7078fae3551SRodney W. Grimes 		if  (c == (int)'n')
7088fae3551SRodney W. Grimes 			break;
7098fae3551SRodney W. Grimes 	}
7108fae3551SRodney W. Grimes 	(void) unlink(tmpfil);
7118fae3551SRodney W. Grimes 	return (1);
7128fae3551SRodney W. Grimes }
7138fae3551SRodney W. Grimes 
714d2fe97c7SPoul-Henning Kamp static int
715326c7cdaSWarner Losh editit(void)
7168fae3551SRodney W. Grimes {
717326c7cdaSWarner Losh 	int pid, xpid;
718c8223965SMark Murray 	int locstat, omask;
719c8223965SMark Murray 	const char *ed;
72073d6722dSKevin Lo 	uid_t uid;
72173d6722dSKevin Lo 	gid_t gid;
7228fae3551SRodney W. Grimes 
7238fae3551SRodney W. Grimes 	omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
7248fae3551SRodney W. Grimes 	while ((pid = fork()) < 0) {
7258fae3551SRodney W. Grimes 		if (errno == EPROCLIM) {
7266bd343a9SPhilippe Charnier 			warnx("you have too many processes");
7278fae3551SRodney W. Grimes 			return(0);
7288fae3551SRodney W. Grimes 		}
7298fae3551SRodney W. Grimes 		if (errno != EAGAIN) {
7306bd343a9SPhilippe Charnier 			warn("fork");
7318fae3551SRodney W. Grimes 			return(0);
7328fae3551SRodney W. Grimes 		}
7338fae3551SRodney W. Grimes 		sleep(1);
7348fae3551SRodney W. Grimes 	}
7358fae3551SRodney W. Grimes 	if (pid == 0) {
7368fae3551SRodney W. Grimes 		sigsetmask(omask);
73773d6722dSKevin Lo 		gid = getgid();
73873d6722dSKevin Lo 		if (setresgid(gid, gid, gid) == -1)
73973d6722dSKevin Lo 			err(1, "setresgid");
74073d6722dSKevin Lo 		uid = getuid();
74173d6722dSKevin Lo 		if (setresuid(uid, uid, uid) == -1)
74273d6722dSKevin Lo 			err(1, "setresuid");
7438fae3551SRodney W. Grimes 		if ((ed = getenv("EDITOR")) == (char *)0)
7448fae3551SRodney W. Grimes 			ed = DEFEDITOR;
7457bc6d015SBrian Somers 		execlp(ed, ed, tmpfil, (char *)0);
7466bd343a9SPhilippe Charnier 		err(1, "%s", ed);
7478fae3551SRodney W. Grimes 	}
748c8223965SMark Murray 	while ((xpid = wait(&locstat)) >= 0)
7498fae3551SRodney W. Grimes 		if (xpid == pid)
7508fae3551SRodney W. Grimes 			break;
7518fae3551SRodney W. Grimes 	sigsetmask(omask);
752c8223965SMark Murray 	return(!locstat);
7538fae3551SRodney W. Grimes }
7548fae3551SRodney W. Grimes 
755d2fe97c7SPoul-Henning Kamp static char *
756326c7cdaSWarner Losh skip(char *cp)
7578fae3551SRodney W. Grimes {
7588fae3551SRodney W. Grimes 
7598fae3551SRodney W. Grimes 	while (*cp != '\0' && isspace(*cp))
7608fae3551SRodney W. Grimes 		cp++;
7618fae3551SRodney W. Grimes 	if (*cp == '\0' || *cp == '#')
762326c7cdaSWarner Losh 		return (NULL);
7638fae3551SRodney W. Grimes 	return (cp);
7648fae3551SRodney W. Grimes }
7658fae3551SRodney W. Grimes 
766d2fe97c7SPoul-Henning Kamp static char *
767326c7cdaSWarner Losh word(char *cp)
7688fae3551SRodney W. Grimes {
769326c7cdaSWarner Losh 	char c;
7708fae3551SRodney W. Grimes 
7718fae3551SRodney W. Grimes 	while (*cp != '\0' && !isspace(*cp) && *cp != '#')
7728fae3551SRodney W. Grimes 		cp++;
7738fae3551SRodney W. Grimes 	if ((c = *cp) != '\0') {
7748fae3551SRodney W. Grimes 		*cp++ = '\0';
7758fae3551SRodney W. Grimes 		if (c != '#')
7768fae3551SRodney W. Grimes 			return (skip(cp));
7778fae3551SRodney W. Grimes 	}
778326c7cdaSWarner Losh 	return (NULL);
7798fae3551SRodney W. Grimes }
7808fae3551SRodney W. Grimes 
7818fae3551SRodney W. Grimes /*
7828fae3551SRodney W. Grimes  * Read an ascii label in from fd f,
7838fae3551SRodney W. Grimes  * in the same format as that put out by display(),
7848fae3551SRodney W. Grimes  * and fill in lp.
7858fae3551SRodney W. Grimes  */
786d2fe97c7SPoul-Henning Kamp static int
787326c7cdaSWarner Losh getasciilabel(FILE *f, struct disklabel *lp)
7888fae3551SRodney W. Grimes {
789fb26ece7SJaakko Heinonen 	char *cp, *endp;
7906b0ff5f5SPoul-Henning Kamp 	const char **cpp;
791484c7804SJulian Elischer 	u_int part;
7926b0ff5f5SPoul-Henning Kamp 	char *tp, line[BUFSIZ];
793484c7804SJulian Elischer 	u_long v;
794484c7804SJulian Elischer 	int lineno = 0, errors = 0;
795326c7cdaSWarner Losh 	int i;
7968fae3551SRodney W. Grimes 
7978970f1a4SPoul-Henning Kamp 	makelabel("auto", lp);
798b0459c58SDag-Erling Smørgrav 	bzero(&part_set, sizeof(part_set));
799b0459c58SDag-Erling Smørgrav 	bzero(&part_size_type, sizeof(part_size_type));
800b0459c58SDag-Erling Smørgrav 	bzero(&part_offset_type, sizeof(part_offset_type));
8018fae3551SRodney W. Grimes 	lp->d_bbsize = BBSIZE;				/* XXX */
80277068a7fSPoul-Henning Kamp 	lp->d_sbsize = 0;				/* XXX */
8038fae3551SRodney W. Grimes 	while (fgets(line, sizeof(line) - 1, f)) {
8048fae3551SRodney W. Grimes 		lineno++;
80561de51caSJoerg Wunsch 		if ((cp = index(line,'\n')) != 0)
8068fae3551SRodney W. Grimes 			*cp = '\0';
8078fae3551SRodney W. Grimes 		cp = skip(line);
8088fae3551SRodney W. Grimes 		if (cp == NULL)
8098fae3551SRodney W. Grimes 			continue;
8108fae3551SRodney W. Grimes 		tp = index(cp, ':');
8118fae3551SRodney W. Grimes 		if (tp == NULL) {
8128fae3551SRodney W. Grimes 			fprintf(stderr, "line %d: syntax error\n", lineno);
8138fae3551SRodney W. Grimes 			errors++;
8148fae3551SRodney W. Grimes 			continue;
8158fae3551SRodney W. Grimes 		}
8168fae3551SRodney W. Grimes 		*tp++ = '\0', tp = skip(tp);
817d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "type")) {
8188fae3551SRodney W. Grimes 			if (tp == NULL)
819c8223965SMark Murray 				tp = unknown;
8208fae3551SRodney W. Grimes 			cpp = dktypenames;
8218fae3551SRodney W. Grimes 			for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
822d2fe97c7SPoul-Henning Kamp 				if (*cpp && !strcmp(*cpp, tp)) {
8238fae3551SRodney W. Grimes 					lp->d_type = cpp - dktypenames;
82409dbd070SIan Dowse 					break;
8258fae3551SRodney W. Grimes 				}
82609dbd070SIan Dowse 			if (cpp < &dktypenames[DKMAXTYPES])
82709dbd070SIan Dowse 				continue;
828fb26ece7SJaakko Heinonen 			errno = 0;
829fb26ece7SJaakko Heinonen 			v = strtoul(tp, &endp, 10);
830fb26ece7SJaakko Heinonen 			if (errno != 0 || *endp != '\0')
831fb26ece7SJaakko Heinonen 				v = DKMAXTYPES;
832484c7804SJulian Elischer 			if (v >= DKMAXTYPES)
833484c7804SJulian Elischer 				fprintf(stderr, "line %d:%s %lu\n", lineno,
8348fae3551SRodney W. Grimes 				    "Warning, unknown disk type", v);
835fb26ece7SJaakko Heinonen 			else
8368fae3551SRodney W. Grimes 				lp->d_type = v;
8378fae3551SRodney W. Grimes 			continue;
8388fae3551SRodney W. Grimes 		}
839d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "flags")) {
8408fae3551SRodney W. Grimes 			for (v = 0; (cp = tp) && *cp != '\0';) {
8418fae3551SRodney W. Grimes 				tp = word(cp);
842d2fe97c7SPoul-Henning Kamp 				if (!strcmp(cp, "removeable"))
8438fae3551SRodney W. Grimes 					v |= D_REMOVABLE;
844d2fe97c7SPoul-Henning Kamp 				else if (!strcmp(cp, "ecc"))
8458fae3551SRodney W. Grimes 					v |= D_ECC;
846d2fe97c7SPoul-Henning Kamp 				else if (!strcmp(cp, "badsect"))
8478fae3551SRodney W. Grimes 					v |= D_BADSECT;
8488fae3551SRodney W. Grimes 				else {
8498fae3551SRodney W. Grimes 					fprintf(stderr,
8508fae3551SRodney W. Grimes 					    "line %d: %s: bad flag\n",
8518fae3551SRodney W. Grimes 					    lineno, cp);
8528fae3551SRodney W. Grimes 					errors++;
8538fae3551SRodney W. Grimes 				}
8548fae3551SRodney W. Grimes 			}
8558fae3551SRodney W. Grimes 			lp->d_flags = v;
8568fae3551SRodney W. Grimes 			continue;
8578fae3551SRodney W. Grimes 		}
858d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "drivedata")) {
8598fae3551SRodney W. Grimes 			for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
860484c7804SJulian Elischer 				lp->d_drivedata[i++] = strtoul(cp, NULL, 10);
8618fae3551SRodney W. Grimes 				tp = word(cp);
8628fae3551SRodney W. Grimes 			}
8638fae3551SRodney W. Grimes 			continue;
8648fae3551SRodney W. Grimes 		}
865484c7804SJulian Elischer 		if (sscanf(cp, "%lu partitions", &v) == 1) {
866484c7804SJulian Elischer 			if (v == 0 || v > MAXPARTITIONS) {
8678fae3551SRodney W. Grimes 				fprintf(stderr,
8688fae3551SRodney W. Grimes 				    "line %d: bad # of partitions\n", lineno);
8698fae3551SRodney W. Grimes 				lp->d_npartitions = MAXPARTITIONS;
8708fae3551SRodney W. Grimes 				errors++;
8718fae3551SRodney W. Grimes 			} else
8728fae3551SRodney W. Grimes 				lp->d_npartitions = v;
8738fae3551SRodney W. Grimes 			continue;
8748fae3551SRodney W. Grimes 		}
8758fae3551SRodney W. Grimes 		if (tp == NULL)
876c8223965SMark Murray 			tp = blank;
877d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "disk")) {
8788fae3551SRodney W. Grimes 			strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
8798fae3551SRodney W. Grimes 			continue;
8808fae3551SRodney W. Grimes 		}
881d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "label")) {
8828fae3551SRodney W. Grimes 			strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
8838fae3551SRodney W. Grimes 			continue;
8848fae3551SRodney W. Grimes 		}
885d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "bytes/sector")) {
886484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
887484c7804SJulian Elischer 			if (v == 0 || (v % DEV_BSIZE) != 0) {
8888fae3551SRodney W. Grimes 				fprintf(stderr,
8898fae3551SRodney W. Grimes 				    "line %d: %s: bad sector size\n",
8908fae3551SRodney W. Grimes 				    lineno, tp);
8918fae3551SRodney W. Grimes 				errors++;
8928fae3551SRodney W. Grimes 			} else
8938fae3551SRodney W. Grimes 				lp->d_secsize = v;
8948fae3551SRodney W. Grimes 			continue;
8958fae3551SRodney W. Grimes 		}
896d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "sectors/track")) {
897484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
898484c7804SJulian Elischer #if (ULONG_MAX != 0xffffffffUL)
899266e7997SDag-Erling Smørgrav 			if (v == 0 || v > 0xffffffff)
900484c7804SJulian Elischer #else
901266e7997SDag-Erling Smørgrav 			if (v == 0)
902484c7804SJulian Elischer #endif
903266e7997SDag-Erling Smørgrav 			{
9048fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
9058fae3551SRodney W. Grimes 				    lineno, tp, cp);
9068fae3551SRodney W. Grimes 				errors++;
9078fae3551SRodney W. Grimes 			} else
9088fae3551SRodney W. Grimes 				lp->d_nsectors = v;
9098fae3551SRodney W. Grimes 			continue;
9108fae3551SRodney W. Grimes 		}
911d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "sectors/cylinder")) {
912484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
913484c7804SJulian Elischer 			if (v == 0) {
9148fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
9158fae3551SRodney W. Grimes 				    lineno, tp, cp);
9168fae3551SRodney W. Grimes 				errors++;
9178fae3551SRodney W. Grimes 			} else
9188fae3551SRodney W. Grimes 				lp->d_secpercyl = v;
9198fae3551SRodney W. Grimes 			continue;
9208fae3551SRodney W. Grimes 		}
921d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "tracks/cylinder")) {
922484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
923484c7804SJulian Elischer 			if (v == 0) {
9248fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
9258fae3551SRodney W. Grimes 				    lineno, tp, cp);
9268fae3551SRodney W. Grimes 				errors++;
9278fae3551SRodney W. Grimes 			} else
9288fae3551SRodney W. Grimes 				lp->d_ntracks = v;
9298fae3551SRodney W. Grimes 			continue;
9308fae3551SRodney W. Grimes 		}
931d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "cylinders")) {
932484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
933484c7804SJulian Elischer 			if (v == 0) {
9348fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
9358fae3551SRodney W. Grimes 				    lineno, tp, cp);
9368fae3551SRodney W. Grimes 				errors++;
9378fae3551SRodney W. Grimes 			} else
9388fae3551SRodney W. Grimes 				lp->d_ncylinders = v;
9398fae3551SRodney W. Grimes 			continue;
9408fae3551SRodney W. Grimes 		}
941d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "sectors/unit")) {
942484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
943484c7804SJulian Elischer 			if (v == 0) {
944f75dd518SBruce Evans 				fprintf(stderr, "line %d: %s: bad %s\n",
945f75dd518SBruce Evans 				    lineno, tp, cp);
946f75dd518SBruce Evans 				errors++;
947f75dd518SBruce Evans 			} else
948f75dd518SBruce Evans 				lp->d_secperunit = v;
949f75dd518SBruce Evans 			continue;
950f75dd518SBruce Evans 		}
951d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "rpm")) {
952484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
953484c7804SJulian Elischer 			if (v == 0 || v > USHRT_MAX) {
9548fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
9558fae3551SRodney W. Grimes 				    lineno, tp, cp);
9568fae3551SRodney W. Grimes 				errors++;
9578fae3551SRodney W. Grimes 			} else
9588fae3551SRodney W. Grimes 				lp->d_rpm = v;
9598fae3551SRodney W. Grimes 			continue;
9608fae3551SRodney W. Grimes 		}
961d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "interleave")) {
962484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
963484c7804SJulian Elischer 			if (v == 0 || v > USHRT_MAX) {
9648fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
9658fae3551SRodney W. Grimes 				    lineno, tp, cp);
9668fae3551SRodney W. Grimes 				errors++;
9678fae3551SRodney W. Grimes 			} else
9688fae3551SRodney W. Grimes 				lp->d_interleave = v;
9698fae3551SRodney W. Grimes 			continue;
9708fae3551SRodney W. Grimes 		}
971d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "trackskew")) {
972484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
973484c7804SJulian Elischer 			if (v > USHRT_MAX) {
9748fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
9758fae3551SRodney W. Grimes 				    lineno, tp, cp);
9768fae3551SRodney W. Grimes 				errors++;
9778fae3551SRodney W. Grimes 			} else
9788fae3551SRodney W. Grimes 				lp->d_trackskew = v;
9798fae3551SRodney W. Grimes 			continue;
9808fae3551SRodney W. Grimes 		}
981d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "cylinderskew")) {
982484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
983484c7804SJulian Elischer 			if (v > USHRT_MAX) {
9848fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
9858fae3551SRodney W. Grimes 				    lineno, tp, cp);
9868fae3551SRodney W. Grimes 				errors++;
9878fae3551SRodney W. Grimes 			} else
9888fae3551SRodney W. Grimes 				lp->d_cylskew = v;
9898fae3551SRodney W. Grimes 			continue;
9908fae3551SRodney W. Grimes 		}
991d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "headswitch")) {
992484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
9938fae3551SRodney W. Grimes 			lp->d_headswitch = v;
9948fae3551SRodney W. Grimes 			continue;
9958fae3551SRodney W. Grimes 		}
996d2fe97c7SPoul-Henning Kamp 		if (!strcmp(cp, "track-to-track seek")) {
997484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
9988fae3551SRodney W. Grimes 			lp->d_trkseek = v;
9998fae3551SRodney W. Grimes 			continue;
10008fae3551SRodney W. Grimes 		}
10013233afaeSJohn W. De Boskey 		/* the ':' was removed above */
100267b46708SIan Dowse 		if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') {
100367b46708SIan Dowse 			fprintf(stderr,
100467b46708SIan Dowse 			    "line %d: %s: Unknown disklabel field\n", lineno,
100567b46708SIan Dowse 			    cp);
100667b46708SIan Dowse 			errors++;
100767b46708SIan Dowse 			continue;
100867b46708SIan Dowse 		}
100967b46708SIan Dowse 
101067b46708SIan Dowse 		/* Process a partition specification line. */
10113233afaeSJohn W. De Boskey 		part = *cp - 'a';
10123233afaeSJohn W. De Boskey 		if (part >= lp->d_npartitions) {
10138fae3551SRodney W. Grimes 			fprintf(stderr,
10143233afaeSJohn W. De Boskey 			    "line %d: partition name out of range a-%c: %s\n",
10153233afaeSJohn W. De Boskey 			    lineno, 'a' + lp->d_npartitions - 1, cp);
10168fae3551SRodney W. Grimes 			errors++;
10178fae3551SRodney W. Grimes 			continue;
10188fae3551SRodney W. Grimes 		}
10193233afaeSJohn W. De Boskey 		part_set[part] = 1;
102067b46708SIan Dowse 
102167b46708SIan Dowse 		if (getasciipartspec(tp, lp, part, lineno) != 0) {
102267b46708SIan Dowse 			errors++;
102367b46708SIan Dowse 			break;
102467b46708SIan Dowse 		}
102567b46708SIan Dowse 	}
102667b46708SIan Dowse 	errors += checklabel(lp);
102767b46708SIan Dowse 	return (errors == 0);
102867b46708SIan Dowse }
102967b46708SIan Dowse 
103067b46708SIan Dowse #define NXTNUM(n) do { \
103113e0abcbSPaul Traina 	if (tp == NULL) { \
103213e0abcbSPaul Traina 		fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
103367b46708SIan Dowse 		return (1); \
103413e0abcbSPaul Traina 	} else { \
10358fae3551SRodney W. Grimes 		cp = tp, tp = word(cp); \
1036484c7804SJulian Elischer 		(n) = strtoul(cp, NULL, 10); \
103713e0abcbSPaul Traina 	} \
103867b46708SIan Dowse } while (0)
103967b46708SIan Dowse 
10403233afaeSJohn W. De Boskey /* retain 1 character following number */
104167b46708SIan Dowse #define NXTWORD(w,n) do { \
10423233afaeSJohn W. De Boskey 	if (tp == NULL) { \
10433233afaeSJohn W. De Boskey 		fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
104467b46708SIan Dowse 		return (1); \
10453233afaeSJohn W. De Boskey 	} else { \
10463233afaeSJohn W. De Boskey 		char *tmp; \
10473233afaeSJohn W. De Boskey 		cp = tp, tp = word(cp); \
1048484c7804SJulian Elischer 		(n) = strtoul(cp, &tmp, 10); \
10493233afaeSJohn W. De Boskey 		if (tmp) (w) = *tmp; \
10503233afaeSJohn W. De Boskey 	} \
105167b46708SIan Dowse } while (0)
105267b46708SIan Dowse 
105367b46708SIan Dowse /*
105467b46708SIan Dowse  * Read a partition line into partition `part' in the specified disklabel.
105567b46708SIan Dowse  * Return 0 on success, 1 on failure.
105667b46708SIan Dowse  */
1057d2fe97c7SPoul-Henning Kamp static int
105867b46708SIan Dowse getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno)
105967b46708SIan Dowse {
106067b46708SIan Dowse 	struct partition *pp;
1061fb26ece7SJaakko Heinonen 	char *cp, *endp;
106267b46708SIan Dowse 	const char **cpp;
1063484c7804SJulian Elischer 	u_long v;
106467b46708SIan Dowse 
106567b46708SIan Dowse 	pp = &lp->d_partitions[part];
106667b46708SIan Dowse 	cp = NULL;
106767b46708SIan Dowse 
10683233afaeSJohn W. De Boskey 	v = 0;
10693233afaeSJohn W. De Boskey 	NXTWORD(part_size_type[part],v);
1070484c7804SJulian Elischer 	if (v == 0 && part_size_type[part] != '*') {
1071484c7804SJulian Elischer 		fprintf(stderr,
1072484c7804SJulian Elischer 		    "line %d: %s: bad partition size\n", lineno, cp);
107367b46708SIan Dowse 		return (1);
107467b46708SIan Dowse 	}
10758fae3551SRodney W. Grimes 	pp->p_size = v;
10763233afaeSJohn W. De Boskey 
10773233afaeSJohn W. De Boskey 	v = 0;
10783233afaeSJohn W. De Boskey 	NXTWORD(part_offset_type[part],v);
1079484c7804SJulian Elischer 	if (v == 0 && part_offset_type[part] != '*' &&
1080484c7804SJulian Elischer 	    part_offset_type[part] != '\0') {
1081484c7804SJulian Elischer 		fprintf(stderr,
1082484c7804SJulian Elischer 		    "line %d: %s: bad partition offset\n", lineno, cp);
108367b46708SIan Dowse 		return (1);
108467b46708SIan Dowse 	}
10858fae3551SRodney W. Grimes 	pp->p_offset = v;
108629f3f095SYaroslav Tykhiy 	if (tp == NULL) {
108729f3f095SYaroslav Tykhiy 		fprintf(stderr, "line %d: missing file system type\n", lineno);
108829f3f095SYaroslav Tykhiy 		return (1);
108929f3f095SYaroslav Tykhiy 	}
10908fae3551SRodney W. Grimes 	cp = tp, tp = word(cp);
109167b46708SIan Dowse 	for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1092d2fe97c7SPoul-Henning Kamp 		if (*cpp && !strcmp(*cpp, cp))
109367b46708SIan Dowse 			break;
109467b46708SIan Dowse 	if (*cpp != NULL) {
109567b46708SIan Dowse 		pp->p_fstype = cpp - fstypenames;
109667b46708SIan Dowse 	} else {
1097fb26ece7SJaakko Heinonen 		if (isdigit(*cp)) {
1098fb26ece7SJaakko Heinonen 			errno = 0;
1099fb26ece7SJaakko Heinonen 			v = strtoul(cp, &endp, 10);
1100fb26ece7SJaakko Heinonen 			if (errno != 0 || *endp != '\0')
1101fb26ece7SJaakko Heinonen 				v = FSMAXTYPES;
1102fb26ece7SJaakko Heinonen 		} else
11038fae3551SRodney W. Grimes 			v = FSMAXTYPES;
1104484c7804SJulian Elischer 		if (v >= FSMAXTYPES) {
11053233afaeSJohn W. De Boskey 			fprintf(stderr,
110667b46708SIan Dowse 			    "line %d: Warning, unknown file system type %s\n",
11073233afaeSJohn W. De Boskey 			    lineno, cp);
11088fae3551SRodney W. Grimes 			v = FS_UNUSED;
11098fae3551SRodney W. Grimes 		}
11108fae3551SRodney W. Grimes 		pp->p_fstype = v;
111167b46708SIan Dowse 	}
111267b46708SIan Dowse 
11138fae3551SRodney W. Grimes 	switch (pp->p_fstype) {
11143233afaeSJohn W. De Boskey 	case FS_UNUSED:
1115640c9cb2SIan Dowse 	case FS_BSDFFS:
1116640c9cb2SIan Dowse 	case FS_BSDLFS:
1117640c9cb2SIan Dowse 		/* accept defaults for fsize/frag/cpg */
11183233afaeSJohn W. De Boskey 		if (tp) {
11198fae3551SRodney W. Grimes 			NXTNUM(pp->p_fsize);
11208fae3551SRodney W. Grimes 			if (pp->p_fsize == 0)
11218fae3551SRodney W. Grimes 				break;
11228fae3551SRodney W. Grimes 			NXTNUM(v);
11238fae3551SRodney W. Grimes 			pp->p_frag = v / pp->p_fsize;
1124640c9cb2SIan Dowse 			if (tp != NULL)
1125640c9cb2SIan Dowse 				NXTNUM(pp->p_cpg);
11263233afaeSJohn W. De Boskey 		}
11273233afaeSJohn W. De Boskey 		/* else default to 0's */
11288fae3551SRodney W. Grimes 		break;
11298fae3551SRodney W. Grimes 	default:
11308fae3551SRodney W. Grimes 		break;
11318fae3551SRodney W. Grimes 	}
113267b46708SIan Dowse 	return (0);
11338fae3551SRodney W. Grimes }
11348fae3551SRodney W. Grimes 
11358fae3551SRodney W. Grimes /*
11368fae3551SRodney W. Grimes  * Check disklabel for errors and fill in
11378fae3551SRodney W. Grimes  * derived fields according to supplied values.
11388fae3551SRodney W. Grimes  */
1139d2fe97c7SPoul-Henning Kamp static int
1140326c7cdaSWarner Losh checklabel(struct disklabel *lp)
11418fae3551SRodney W. Grimes {
1142326c7cdaSWarner Losh 	struct partition *pp;
11438fae3551SRodney W. Grimes 	int i, errors = 0;
11448fae3551SRodney W. Grimes 	char part;
11453b89beb1SIan Dowse 	u_long base_offset, needed, total_size, total_percent, current_offset;
11463b89beb1SIan Dowse 	long free_space;
11473233afaeSJohn W. De Boskey 	int seen_default_offset;
11483233afaeSJohn W. De Boskey 	int hog_part;
11493233afaeSJohn W. De Boskey 	int j;
11503233afaeSJohn W. De Boskey 	struct partition *pp2;
11518fae3551SRodney W. Grimes 
1152d2fe97c7SPoul-Henning Kamp 	if (lp == NULL)
1153d2fe97c7SPoul-Henning Kamp 		lp = &lab;
1154d2fe97c7SPoul-Henning Kamp 
115557dfbec5SPoul-Henning Kamp 	if (allfields) {
115657dfbec5SPoul-Henning Kamp 
11578fae3551SRodney W. Grimes 		if (lp->d_secsize == 0) {
11582a1deaaaSBruce Evans 			fprintf(stderr, "sector size 0\n");
11598fae3551SRodney W. Grimes 			return (1);
11608fae3551SRodney W. Grimes 		}
11618fae3551SRodney W. Grimes 		if (lp->d_nsectors == 0) {
11622a1deaaaSBruce Evans 			fprintf(stderr, "sectors/track 0\n");
11638fae3551SRodney W. Grimes 			return (1);
11648fae3551SRodney W. Grimes 		}
11658fae3551SRodney W. Grimes 		if (lp->d_ntracks == 0) {
11662a1deaaaSBruce Evans 			fprintf(stderr, "tracks/cylinder 0\n");
11678fae3551SRodney W. Grimes 			return (1);
11688fae3551SRodney W. Grimes 		}
11698fae3551SRodney W. Grimes 		if  (lp->d_ncylinders == 0) {
11702a1deaaaSBruce Evans 			fprintf(stderr, "cylinders/unit 0\n");
11718fae3551SRodney W. Grimes 			errors++;
11728fae3551SRodney W. Grimes 		}
11738fae3551SRodney W. Grimes 		if (lp->d_rpm == 0)
117457dfbec5SPoul-Henning Kamp 			warnx("revolutions/minute 0");
11758fae3551SRodney W. Grimes 		if (lp->d_secpercyl == 0)
11768fae3551SRodney W. Grimes 			lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
11778fae3551SRodney W. Grimes 		if (lp->d_secperunit == 0)
11788fae3551SRodney W. Grimes 			lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
11798fae3551SRodney W. Grimes 		if (lp->d_bbsize == 0) {
11802a1deaaaSBruce Evans 			fprintf(stderr, "boot block size 0\n");
11818fae3551SRodney W. Grimes 			errors++;
11828fae3551SRodney W. Grimes 		} else if (lp->d_bbsize % lp->d_secsize)
118357dfbec5SPoul-Henning Kamp 			warnx("boot block size %% sector-size != 0");
11848fae3551SRodney W. Grimes 		if (lp->d_npartitions > MAXPARTITIONS)
118557dfbec5SPoul-Henning Kamp 			warnx("number of partitions (%lu) > MAXPARTITIONS (%d)",
11862a1deaaaSBruce Evans 			    (u_long)lp->d_npartitions, MAXPARTITIONS);
118757dfbec5SPoul-Henning Kamp 	} else {
118857dfbec5SPoul-Henning Kamp 		struct disklabel *vl;
118957dfbec5SPoul-Henning Kamp 
119057dfbec5SPoul-Henning Kamp 		vl = getvirginlabel();
119157dfbec5SPoul-Henning Kamp 		lp->d_secsize = vl->d_secsize;
119257dfbec5SPoul-Henning Kamp 		lp->d_nsectors = vl->d_nsectors;
119357dfbec5SPoul-Henning Kamp 		lp->d_ntracks = vl->d_ntracks;
119457dfbec5SPoul-Henning Kamp 		lp->d_ncylinders = vl->d_ncylinders;
119557dfbec5SPoul-Henning Kamp 		lp->d_rpm = vl->d_rpm;
119657dfbec5SPoul-Henning Kamp 		lp->d_interleave = vl->d_interleave;
119757dfbec5SPoul-Henning Kamp 		lp->d_secpercyl = vl->d_secpercyl;
119857dfbec5SPoul-Henning Kamp 		lp->d_secperunit = vl->d_secperunit;
119957dfbec5SPoul-Henning Kamp 		lp->d_bbsize = vl->d_bbsize;
120057dfbec5SPoul-Henning Kamp 		lp->d_npartitions = vl->d_npartitions;
120157dfbec5SPoul-Henning Kamp 	}
120257dfbec5SPoul-Henning Kamp 
12033233afaeSJohn W. De Boskey 
12043233afaeSJohn W. De Boskey 	/* first allocate space to the partitions, then offsets */
12053233afaeSJohn W. De Boskey 	total_size = 0; /* in sectors */
12063233afaeSJohn W. De Boskey 	total_percent = 0; /* in percent */
12073233afaeSJohn W. De Boskey 	hog_part = -1;
12083233afaeSJohn W. De Boskey 	/* find all fixed partitions */
12093233afaeSJohn W. De Boskey 	for (i = 0; i < lp->d_npartitions; i++) {
12103233afaeSJohn W. De Boskey 		pp = &lp->d_partitions[i];
12113233afaeSJohn W. De Boskey 		if (part_set[i]) {
12123233afaeSJohn W. De Boskey 			if (part_size_type[i] == '*') {
12136b0ff5f5SPoul-Henning Kamp 				if (i == RAW_PART) {
12143233afaeSJohn W. De Boskey 					pp->p_size = lp->d_secperunit;
12153233afaeSJohn W. De Boskey 				} else {
12163233afaeSJohn W. De Boskey 					if (hog_part != -1)
121757dfbec5SPoul-Henning Kamp 						warnx("Too many '*' partitions (%c and %c)",
12183233afaeSJohn W. De Boskey 						    hog_part + 'a',i + 'a');
12193233afaeSJohn W. De Boskey 					else
12203233afaeSJohn W. De Boskey 						hog_part = i;
12213233afaeSJohn W. De Boskey 				}
12223233afaeSJohn W. De Boskey 			} else {
12238d3105e8SWarner Losh 				off_t size;
12243233afaeSJohn W. De Boskey 
12253233afaeSJohn W. De Boskey 				size = pp->p_size;
12263233afaeSJohn W. De Boskey 				switch (part_size_type[i]) {
12273233afaeSJohn W. De Boskey 				case '%':
12283233afaeSJohn W. De Boskey 					total_percent += size;
12293233afaeSJohn W. De Boskey 					break;
12304c814dfcSDag-Erling Smørgrav 				case 't':
12314c814dfcSDag-Erling Smørgrav 				case 'T':
12324c814dfcSDag-Erling Smørgrav 					size *= 1024ULL;
12334c814dfcSDag-Erling Smørgrav 					/* FALLTHROUGH */
12344c814dfcSDag-Erling Smørgrav 				case 'g':
12354c814dfcSDag-Erling Smørgrav 				case 'G':
12364c814dfcSDag-Erling Smørgrav 					size *= 1024ULL;
12374c814dfcSDag-Erling Smørgrav 					/* FALLTHROUGH */
12384c814dfcSDag-Erling Smørgrav 				case 'm':
12394c814dfcSDag-Erling Smørgrav 				case 'M':
12404c814dfcSDag-Erling Smørgrav 					size *= 1024ULL;
12414c814dfcSDag-Erling Smørgrav 					/* FALLTHROUGH */
12423233afaeSJohn W. De Boskey 				case 'k':
12433233afaeSJohn W. De Boskey 				case 'K':
12448d3105e8SWarner Losh 					size *= 1024ULL;
12453233afaeSJohn W. De Boskey 					break;
12463233afaeSJohn W. De Boskey 				case '\0':
12473233afaeSJohn W. De Boskey 					break;
12483233afaeSJohn W. De Boskey 				default:
124981807292SDag-Erling Smørgrav 					warnx("unknown multiplier suffix '%c' for partition %c (should be K, M, G or T)",
12504c814dfcSDag-Erling Smørgrav 					    part_size_type[i], i + 'a');
12513233afaeSJohn W. De Boskey 					break;
12523233afaeSJohn W. De Boskey 				}
12533233afaeSJohn W. De Boskey 				/* don't count %'s yet */
12543233afaeSJohn W. De Boskey 				if (part_size_type[i] != '%') {
12553233afaeSJohn W. De Boskey 					/*
12563233afaeSJohn W. De Boskey 					 * for all not in sectors, convert to
12573233afaeSJohn W. De Boskey 					 * sectors
12583233afaeSJohn W. De Boskey 					 */
12593233afaeSJohn W. De Boskey 					if (part_size_type[i] != '\0') {
12603233afaeSJohn W. De Boskey 						if (size % lp->d_secsize != 0)
126157dfbec5SPoul-Henning Kamp 							warnx("partition %c not an integer number of sectors",
12623233afaeSJohn W. De Boskey 							    i + 'a');
12633233afaeSJohn W. De Boskey 						size /= lp->d_secsize;
12643233afaeSJohn W. De Boskey 						pp->p_size = size;
12653233afaeSJohn W. De Boskey 					}
12663233afaeSJohn W. De Boskey 					/* else already in sectors */
12676b0ff5f5SPoul-Henning Kamp 					if (i != RAW_PART)
12683233afaeSJohn W. De Boskey 						total_size += size;
12693233afaeSJohn W. De Boskey 				}
12703233afaeSJohn W. De Boskey 			}
12713233afaeSJohn W. De Boskey 		}
12723233afaeSJohn W. De Boskey 	}
12733b89beb1SIan Dowse 
12743b89beb1SIan Dowse 	/* Find out the total free space, excluding the boot block area. */
12753b89beb1SIan Dowse 	base_offset = BBSIZE / secsize;
12763b89beb1SIan Dowse 	free_space = 0;
12773b89beb1SIan Dowse 	for (i = 0; i < lp->d_npartitions; i++) {
12783b89beb1SIan Dowse 		pp = &lp->d_partitions[i];
12793b89beb1SIan Dowse 		if (!part_set[i] || i == RAW_PART ||
12803b89beb1SIan Dowse 		    part_size_type[i] == '%' || part_size_type[i] == '*')
12813b89beb1SIan Dowse 			continue;
12823b89beb1SIan Dowse 		if (pp->p_offset > base_offset)
12833b89beb1SIan Dowse 			free_space += pp->p_offset - base_offset;
12843b89beb1SIan Dowse 		if (pp->p_offset + pp->p_size > base_offset)
12853b89beb1SIan Dowse 			base_offset = pp->p_offset + pp->p_size;
12863b89beb1SIan Dowse 	}
12873b89beb1SIan Dowse 	if (base_offset < lp->d_secperunit)
12883b89beb1SIan Dowse 		free_space += lp->d_secperunit - base_offset;
12893b89beb1SIan Dowse 
12903233afaeSJohn W. De Boskey 	/* handle % partitions - note %'s don't need to add up to 100! */
12913233afaeSJohn W. De Boskey 	if (total_percent != 0) {
12923233afaeSJohn W. De Boskey 		if (total_percent > 100) {
12936b0ff5f5SPoul-Henning Kamp 			fprintf(stderr,"total percentage %lu is greater than 100\n",
12943233afaeSJohn W. De Boskey 			    total_percent);
12953233afaeSJohn W. De Boskey 			errors++;
12963233afaeSJohn W. De Boskey 		}
12973233afaeSJohn W. De Boskey 
12983233afaeSJohn W. De Boskey 		if (free_space > 0) {
12993233afaeSJohn W. De Boskey 			for (i = 0; i < lp->d_npartitions; i++) {
13003233afaeSJohn W. De Boskey 				pp = &lp->d_partitions[i];
13013233afaeSJohn W. De Boskey 				if (part_set[i] && part_size_type[i] == '%') {
13023233afaeSJohn W. De Boskey 					/* careful of overflows! and integer roundoff */
13033233afaeSJohn W. De Boskey 					pp->p_size = ((double)pp->p_size/100) * free_space;
13043233afaeSJohn W. De Boskey 					total_size += pp->p_size;
13053233afaeSJohn W. De Boskey 
13063233afaeSJohn W. De Boskey 					/* FIX we can lose a sector or so due to roundoff per
13073233afaeSJohn W. De Boskey 					   partition.  A more complex algorithm could avoid that */
13083233afaeSJohn W. De Boskey 				}
13093233afaeSJohn W. De Boskey 			}
13103233afaeSJohn W. De Boskey 		} else {
13113233afaeSJohn W. De Boskey 			fprintf(stderr,
13126b0ff5f5SPoul-Henning Kamp 			    "%ld sectors available to give to '*' and '%%' partitions\n",
13133233afaeSJohn W. De Boskey 			    free_space);
13143233afaeSJohn W. De Boskey 			errors++;
13153233afaeSJohn W. De Boskey 			/* fix?  set all % partitions to size 0? */
13163233afaeSJohn W. De Boskey 		}
13173233afaeSJohn W. De Boskey 	}
13183233afaeSJohn W. De Boskey 	/* give anything remaining to the hog partition */
13193233afaeSJohn W. De Boskey 	if (hog_part != -1) {
13203b89beb1SIan Dowse 		/*
13213b89beb1SIan Dowse 		 * Find the range of offsets usable by '*' partitions around
13223b89beb1SIan Dowse 		 * the hog partition and how much space they need.
13233b89beb1SIan Dowse 		 */
13243b89beb1SIan Dowse 		needed = 0;
13253b89beb1SIan Dowse 		base_offset = BBSIZE / secsize;
13263b89beb1SIan Dowse 		for (i = hog_part - 1; i >= 0; i--) {
13273b89beb1SIan Dowse 			pp = &lp->d_partitions[i];
13283b89beb1SIan Dowse 			if (!part_set[i] || i == RAW_PART)
13293b89beb1SIan Dowse 				continue;
13303b89beb1SIan Dowse 			if (part_offset_type[i] == '*') {
13313b89beb1SIan Dowse 				needed += pp->p_size;
13323b89beb1SIan Dowse 				continue;
13333b89beb1SIan Dowse 			}
13343b89beb1SIan Dowse 			base_offset = pp->p_offset + pp->p_size;
13353b89beb1SIan Dowse 			break;
13363b89beb1SIan Dowse 		}
13373b89beb1SIan Dowse 		current_offset = lp->d_secperunit;
13383b89beb1SIan Dowse 		for (i = lp->d_npartitions - 1; i > hog_part; i--) {
13393b89beb1SIan Dowse 			pp = &lp->d_partitions[i];
13403b89beb1SIan Dowse 			if (!part_set[i] || i == RAW_PART)
13413b89beb1SIan Dowse 				continue;
13423b89beb1SIan Dowse 			if (part_offset_type[i] == '*') {
13433b89beb1SIan Dowse 				needed += pp->p_size;
13443b89beb1SIan Dowse 				continue;
13453b89beb1SIan Dowse 			}
13463b89beb1SIan Dowse 			current_offset = pp->p_offset;
13473b89beb1SIan Dowse 		}
13483b89beb1SIan Dowse 
13493b89beb1SIan Dowse 		if (current_offset - base_offset <= needed) {
13503b89beb1SIan Dowse 			fprintf(stderr, "Cannot find space for partition %c\n",
13513b89beb1SIan Dowse 			    hog_part + 'a');
13523b89beb1SIan Dowse 			fprintf(stderr,
13533b89beb1SIan Dowse 			    "Need more than %lu sectors between %lu and %lu\n",
13543b89beb1SIan Dowse 			    needed, base_offset, current_offset);
13553b89beb1SIan Dowse 			errors++;
13563b89beb1SIan Dowse 			lp->d_partitions[hog_part].p_size = 0;
13573b89beb1SIan Dowse 		} else {
13583b89beb1SIan Dowse 			lp->d_partitions[hog_part].p_size = current_offset -
13593b89beb1SIan Dowse 			    base_offset - needed;
13603b89beb1SIan Dowse 			total_size += lp->d_partitions[hog_part].p_size;
13613b89beb1SIan Dowse 		}
13623233afaeSJohn W. De Boskey 	}
13633233afaeSJohn W. De Boskey 
13643233afaeSJohn W. De Boskey 	/* Now set the offsets for each partition */
13653b89beb1SIan Dowse 	current_offset = BBSIZE / secsize; /* in sectors */
13663233afaeSJohn W. De Boskey 	seen_default_offset = 0;
13673233afaeSJohn W. De Boskey 	for (i = 0; i < lp->d_npartitions; i++) {
13683233afaeSJohn W. De Boskey 		part = 'a' + i;
13693233afaeSJohn W. De Boskey 		pp = &lp->d_partitions[i];
13703233afaeSJohn W. De Boskey 		if (part_set[i]) {
13713233afaeSJohn W. De Boskey 			if (part_offset_type[i] == '*') {
13726b0ff5f5SPoul-Henning Kamp 				if (i == RAW_PART) {
13733233afaeSJohn W. De Boskey 					pp->p_offset = 0;
13743233afaeSJohn W. De Boskey 				} else {
13753233afaeSJohn W. De Boskey 					pp->p_offset = current_offset;
13763233afaeSJohn W. De Boskey 					seen_default_offset = 1;
13773233afaeSJohn W. De Boskey 				}
13783233afaeSJohn W. De Boskey 			} else {
13793233afaeSJohn W. De Boskey 				/* allow them to be out of order for old-style tables */
13803233afaeSJohn W. De Boskey 				if (pp->p_offset < current_offset &&
1381f2f63257SGreg Lehey 				    seen_default_offset && i != RAW_PART &&
1382f2f63257SGreg Lehey 				    pp->p_fstype != FS_VINUM) {
13833233afaeSJohn W. De Boskey 					fprintf(stderr,
13846b0ff5f5SPoul-Henning Kamp "Offset %ld for partition %c overlaps previous partition which ends at %lu\n",
13856b0ff5f5SPoul-Henning Kamp 					    (long)pp->p_offset,i+'a',current_offset);
13863233afaeSJohn W. De Boskey 					fprintf(stderr,
13873233afaeSJohn W. De Boskey "Labels with any *'s for offset must be in ascending order by sector\n");
13883233afaeSJohn W. De Boskey 					errors++;
13893233afaeSJohn W. De Boskey 				} else if (pp->p_offset != current_offset &&
13906b0ff5f5SPoul-Henning Kamp 				    i != RAW_PART && seen_default_offset) {
13913233afaeSJohn W. De Boskey 					/*
13923233afaeSJohn W. De Boskey 					 * this may give unneeded warnings if
13933233afaeSJohn W. De Boskey 					 * partitions are out-of-order
13943233afaeSJohn W. De Boskey 					 */
139557dfbec5SPoul-Henning Kamp 					warnx(
13963233afaeSJohn W. De Boskey "Offset %ld for partition %c doesn't match expected value %ld",
13976b0ff5f5SPoul-Henning Kamp 					    (long)pp->p_offset, i + 'a', current_offset);
13983233afaeSJohn W. De Boskey 				}
13993233afaeSJohn W. De Boskey 			}
14006b0ff5f5SPoul-Henning Kamp 			if (i != RAW_PART)
14013233afaeSJohn W. De Boskey 				current_offset = pp->p_offset + pp->p_size;
14023233afaeSJohn W. De Boskey 		}
14033233afaeSJohn W. De Boskey 	}
14043233afaeSJohn W. De Boskey 
14058fae3551SRodney W. Grimes 	for (i = 0; i < lp->d_npartitions; i++) {
14068fae3551SRodney W. Grimes 		part = 'a' + i;
14078fae3551SRodney W. Grimes 		pp = &lp->d_partitions[i];
14088fae3551SRodney W. Grimes 		if (pp->p_size == 0 && pp->p_offset != 0)
140957dfbec5SPoul-Henning Kamp 			warnx("partition %c: size 0, but offset %lu",
14102a1deaaaSBruce Evans 			    part, (u_long)pp->p_offset);
14118fae3551SRodney W. Grimes #ifdef notdef
14128fae3551SRodney W. Grimes 		if (pp->p_size % lp->d_secpercyl)
141357dfbec5SPoul-Henning Kamp 			warnx("partition %c: size %% cylinder-size != 0",
14148fae3551SRodney W. Grimes 			    part);
14158fae3551SRodney W. Grimes 		if (pp->p_offset % lp->d_secpercyl)
141657dfbec5SPoul-Henning Kamp 			warnx("partition %c: offset %% cylinder-size != 0",
14178fae3551SRodney W. Grimes 			    part);
14188fae3551SRodney W. Grimes #endif
14198fae3551SRodney W. Grimes 		if (pp->p_offset > lp->d_secperunit) {
14208fae3551SRodney W. Grimes 			fprintf(stderr,
14218fae3551SRodney W. Grimes 			    "partition %c: offset past end of unit\n", part);
14228fae3551SRodney W. Grimes 			errors++;
14238fae3551SRodney W. Grimes 		}
14248fae3551SRodney W. Grimes 		if (pp->p_offset + pp->p_size > lp->d_secperunit) {
14258fae3551SRodney W. Grimes 			fprintf(stderr,
14268fae3551SRodney W. Grimes 			"partition %c: partition extends past end of unit\n",
14278fae3551SRodney W. Grimes 			    part);
14288fae3551SRodney W. Grimes 			errors++;
14298fae3551SRodney W. Grimes 		}
143057dfbec5SPoul-Henning Kamp 		if (i == RAW_PART) {
14313233afaeSJohn W. De Boskey 			if (pp->p_fstype != FS_UNUSED)
143257dfbec5SPoul-Henning Kamp 				warnx("partition %c is not marked as unused!",part);
14333233afaeSJohn W. De Boskey 			if (pp->p_offset != 0)
143457dfbec5SPoul-Henning Kamp 				warnx("partition %c doesn't start at 0!",part);
14353233afaeSJohn W. De Boskey 			if (pp->p_size != lp->d_secperunit)
143657dfbec5SPoul-Henning Kamp 				warnx("partition %c doesn't cover the whole unit!",part);
14373233afaeSJohn W. De Boskey 
14383233afaeSJohn W. De Boskey 			if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
14393233afaeSJohn W. De Boskey 			    (pp->p_size != lp->d_secperunit)) {
144057dfbec5SPoul-Henning Kamp 				warnx("An incorrect partition %c may cause problems for "
14413233afaeSJohn W. De Boskey 				    "standard system utilities",part);
14423233afaeSJohn W. De Boskey 			}
14433233afaeSJohn W. De Boskey 		}
14443233afaeSJohn W. De Boskey 
14453233afaeSJohn W. De Boskey 		/* check for overlaps */
14463233afaeSJohn W. De Boskey 		/* this will check for all possible overlaps once and only once */
14473233afaeSJohn W. De Boskey 		for (j = 0; j < i; j++) {
14483233afaeSJohn W. De Boskey 			pp2 = &lp->d_partitions[j];
1449f2f63257SGreg Lehey 			if (j != RAW_PART && i != RAW_PART &&
1450f2f63257SGreg Lehey 			    pp->p_fstype != FS_VINUM &&
1451f2f63257SGreg Lehey 			    pp2->p_fstype != FS_VINUM &&
1452f2f63257SGreg Lehey 			    part_set[i] && part_set[j]) {
14533233afaeSJohn W. De Boskey 				if (pp2->p_offset < pp->p_offset + pp->p_size &&
14543233afaeSJohn W. De Boskey 				    (pp2->p_offset + pp2->p_size > pp->p_offset ||
14553233afaeSJohn W. De Boskey 					pp2->p_offset >= pp->p_offset)) {
14563233afaeSJohn W. De Boskey 					fprintf(stderr,"partitions %c and %c overlap!\n",
14573233afaeSJohn W. De Boskey 					    j + 'a', i + 'a');
14583233afaeSJohn W. De Boskey 					errors++;
14593233afaeSJohn W. De Boskey 				}
14603233afaeSJohn W. De Boskey 			}
14613233afaeSJohn W. De Boskey 		}
14628fae3551SRodney W. Grimes 	}
1463d5718812SMarcel Moolenaar 	for (; i < lp->d_npartitions; i++) {
14648fae3551SRodney W. Grimes 		part = 'a' + i;
14658fae3551SRodney W. Grimes 		pp = &lp->d_partitions[i];
14668fae3551SRodney W. Grimes 		if (pp->p_size || pp->p_offset)
146757dfbec5SPoul-Henning Kamp 			warnx("unused partition %c: size %d offset %lu",
14682a1deaaaSBruce Evans 			    'a' + i, pp->p_size, (u_long)pp->p_offset);
14698fae3551SRodney W. Grimes 	}
14708fae3551SRodney W. Grimes 	return (errors);
14718fae3551SRodney W. Grimes }
14728fae3551SRodney W. Grimes 
14738fae3551SRodney W. Grimes /*
1474425bed3aSJoerg Wunsch  * When operating on a "virgin" disk, try getting an initial label
1475425bed3aSJoerg Wunsch  * from the associated device driver.  This might work for all device
1476425bed3aSJoerg Wunsch  * drivers that are able to fetch some initial device parameters
1477425bed3aSJoerg Wunsch  * without even having access to a (BSD) disklabel, like SCSI disks,
1478425bed3aSJoerg Wunsch  * most IDE drives, or vn devices.
1479425bed3aSJoerg Wunsch  *
1480425bed3aSJoerg Wunsch  * The device name must be given in its "canonical" form.
1481425bed3aSJoerg Wunsch  */
1482d2fe97c7SPoul-Henning Kamp static struct disklabel *
1483425bed3aSJoerg Wunsch getvirginlabel(void)
1484425bed3aSJoerg Wunsch {
1485c8223965SMark Murray 	static struct disklabel loclab;
1486b9d05a16SPoul-Henning Kamp 	struct partition *dp;
1487425bed3aSJoerg Wunsch 	int f;
148857dfbec5SPoul-Henning Kamp 	u_int u;
1489425bed3aSJoerg Wunsch 
1490d2fe97c7SPoul-Henning Kamp 	if ((f = open(specname, O_RDONLY)) == -1) {
1491d2fe97c7SPoul-Henning Kamp 		warn("cannot open %s", specname);
149243be698cSBruce Evans 		return (NULL);
1493425bed3aSJoerg Wunsch 	}
1494ff7d5162SJordan K. Hubbard 
14957747c959SLuigi Rizzo 	if (is_file)
14967747c959SLuigi Rizzo 		get_file_parms(f);
14977a4b0bb2SUlf Lilleengen 	else {
14987a4b0bb2SUlf Lilleengen 		mediasize = g_mediasize(f);
14997a4b0bb2SUlf Lilleengen 		secsize = g_sectorsize(f);
15007a4b0bb2SUlf Lilleengen 		if (secsize < 0 || mediasize < 0) {
1501425bed3aSJoerg Wunsch 			close (f);
150243be698cSBruce Evans 			return (NULL);
1503425bed3aSJoerg Wunsch 		}
15047a4b0bb2SUlf Lilleengen 	}
1505b9d05a16SPoul-Henning Kamp 	memset(&loclab, 0, sizeof loclab);
1506b9d05a16SPoul-Henning Kamp 	loclab.d_magic = DISKMAGIC;
1507b9d05a16SPoul-Henning Kamp 	loclab.d_magic2 = DISKMAGIC;
1508b9d05a16SPoul-Henning Kamp 	loclab.d_secsize = secsize;
1509b9d05a16SPoul-Henning Kamp 	loclab.d_secperunit = mediasize / secsize;
1510b9d05a16SPoul-Henning Kamp 
1511b9d05a16SPoul-Henning Kamp 	/*
1512b9d05a16SPoul-Henning Kamp 	 * Nobody in these enligthened days uses the CHS geometry for
1513b9d05a16SPoul-Henning Kamp 	 * anything, but nontheless try to get it right.  If we fail
1514b9d05a16SPoul-Henning Kamp 	 * to get any good ideas from the device, construct something
1515b9d05a16SPoul-Henning Kamp 	 * which is IBM-PC friendly.
1516b9d05a16SPoul-Henning Kamp 	 */
1517b9d05a16SPoul-Henning Kamp 	if (ioctl(f, DIOCGFWSECTORS, &u) == 0)
1518b9d05a16SPoul-Henning Kamp 		loclab.d_nsectors = u;
1519b9d05a16SPoul-Henning Kamp 	else
1520b9d05a16SPoul-Henning Kamp 		loclab.d_nsectors = 63;
1521b9d05a16SPoul-Henning Kamp 	if (ioctl(f, DIOCGFWHEADS, &u) == 0)
1522b9d05a16SPoul-Henning Kamp 		loclab.d_ntracks = u;
1523b9d05a16SPoul-Henning Kamp 	else if (loclab.d_secperunit <= 63*1*1024)
1524b9d05a16SPoul-Henning Kamp 		loclab.d_ntracks = 1;
1525b9d05a16SPoul-Henning Kamp 	else if (loclab.d_secperunit <= 63*16*1024)
1526b9d05a16SPoul-Henning Kamp 		loclab.d_ntracks = 16;
1527b9d05a16SPoul-Henning Kamp 	else
1528b9d05a16SPoul-Henning Kamp 		loclab.d_ntracks = 255;
1529b9d05a16SPoul-Henning Kamp 	loclab.d_secpercyl = loclab.d_ntracks * loclab.d_nsectors;
1530b9d05a16SPoul-Henning Kamp 	loclab.d_ncylinders = loclab.d_secperunit / loclab.d_secpercyl;
1531d5718812SMarcel Moolenaar 	loclab.d_npartitions = DEFPARTITIONS;
1532b9d05a16SPoul-Henning Kamp 
1533b9d05a16SPoul-Henning Kamp 	/* Various (unneeded) compat stuff */
1534b9d05a16SPoul-Henning Kamp 	loclab.d_rpm = 3600;
1535b9d05a16SPoul-Henning Kamp 	loclab.d_bbsize = BBSIZE;
153657dfbec5SPoul-Henning Kamp 	loclab.d_interleave = 1;
1537b9d05a16SPoul-Henning Kamp 	strncpy(loclab.d_typename, "amnesiac",
1538b9d05a16SPoul-Henning Kamp 	    sizeof(loclab.d_typename));
1539b9d05a16SPoul-Henning Kamp 
1540b9d05a16SPoul-Henning Kamp 	dp = &loclab.d_partitions[RAW_PART];
1541b9d05a16SPoul-Henning Kamp 	dp->p_size = loclab.d_secperunit;
1542b9d05a16SPoul-Henning Kamp 	loclab.d_checksum = dkcksum(&loclab);
1543425bed3aSJoerg Wunsch 	close (f);
1544c8223965SMark Murray 	return (&loclab);
1545425bed3aSJoerg Wunsch }
1546425bed3aSJoerg Wunsch 
1547d2fe97c7SPoul-Henning Kamp static void
1548326c7cdaSWarner Losh usage(void)
15498fae3551SRodney W. Grimes {
1550bc33ea1aSRuslan Ermilov 
1551bc33ea1aSRuslan Ermilov 	fprintf(stderr,
1552bc33ea1aSRuslan 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",
15535daa806dSPoul-Henning Kamp 	"usage: bsdlabel disk",
1554bef2080aSPhilippe Charnier 	"\t\t(to read label)",
1555d2fe97c7SPoul-Henning Kamp 	"	bsdlabel -w [-n] [-m machine] disk [type]",
1556bef2080aSPhilippe Charnier 	"\t\t(to write label with existing boot program)",
15575daa806dSPoul-Henning Kamp 	"	bsdlabel -e [-n] [-m machine] disk",
1558bef2080aSPhilippe Charnier 	"\t\t(to edit label)",
15595daa806dSPoul-Henning Kamp 	"	bsdlabel -R [-n] [-m machine] disk protofile",
1560bef2080aSPhilippe Charnier 	"\t\t(to restore label with existing boot program)",
15612c60b668SPoul-Henning Kamp 	"	bsdlabel -B [-b boot] [-m machine] disk",
1562bef2080aSPhilippe Charnier 	"\t\t(to install boot program with existing on-disk label)",
1563d2fe97c7SPoul-Henning Kamp 	"	bsdlabel -w -B [-n] [-b boot] [-m machine] disk [type]",
1564bef2080aSPhilippe Charnier 	"\t\t(to write label and install boot program)",
15652c60b668SPoul-Henning Kamp 	"	bsdlabel -R -B [-n] [-b boot] [-m machine] disk protofile",
156680baf8ceSPoul-Henning Kamp 		"\t\t(to restore label and install boot program)"
156780baf8ceSPoul-Henning Kamp 	);
15688fae3551SRodney W. Grimes 	exit(1);
15698fae3551SRodney W. Grimes }
1570