xref: /freebsd/sbin/bsdlabel/bsdlabel.c (revision ef9ab0b32b8dfa35935e5c1079a78b5c470db78e)
18fae3551SRodney W. Grimes /*
2fd43aa1cSDavid E. O'Brien  * Copyright (c) 1994, 1995 Gordon W. Ross
3fd43aa1cSDavid E. O'Brien  * Copyright (c) 1994 Theo de Raadt
4fd43aa1cSDavid E. O'Brien  * All rights reserved.
58fae3551SRodney W. Grimes  * Copyright (c) 1987, 1993
68fae3551SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
78fae3551SRodney W. Grimes  *
88fae3551SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
98fae3551SRodney W. Grimes  * Symmetric Computer Systems.
108fae3551SRodney W. Grimes  *
118fae3551SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
128fae3551SRodney W. Grimes  * modification, are permitted provided that the following conditions
138fae3551SRodney W. Grimes  * are met:
148fae3551SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
158fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
168fae3551SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
178fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
188fae3551SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
198fae3551SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
208fae3551SRodney W. Grimes  *    must display the following acknowledgement:
218fae3551SRodney W. Grimes  *	This product includes software developed by the University of
228fae3551SRodney W. Grimes  *	California, Berkeley and its contributors.
23fd43aa1cSDavid E. O'Brien  *      This product includes software developed by Theo de Raadt.
248fae3551SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
258fae3551SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
268fae3551SRodney W. Grimes  *    without specific prior written permission.
278fae3551SRodney W. Grimes  *
288fae3551SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
298fae3551SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
308fae3551SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
318fae3551SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
328fae3551SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
338fae3551SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
348fae3551SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
358fae3551SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
368fae3551SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
378fae3551SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
388fae3551SRodney W. Grimes  * SUCH DAMAGE.
39fd43aa1cSDavid E. O'Brien  *
40fd43aa1cSDavid E. O'Brien  *	from: $NetBSD: disksubr.c,v 1.13 2000/12/17 22:39:18 pk $
418fae3551SRodney W. Grimes  */
428fae3551SRodney W. Grimes 
438fae3551SRodney W. Grimes #ifndef lint
446bd343a9SPhilippe Charnier static const char copyright[] =
458fae3551SRodney W. Grimes "@(#) Copyright (c) 1987, 1993\n\
468fae3551SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
478fae3551SRodney W. Grimes #endif /* not lint */
488fae3551SRodney W. Grimes 
498fae3551SRodney W. Grimes #ifndef lint
506bd343a9SPhilippe Charnier #if 0
518fae3551SRodney W. Grimes static char sccsid[] = "@(#)disklabel.c	8.2 (Berkeley) 1/7/94";
528fae3551SRodney W. Grimes /* from static char sccsid[] = "@(#)disklabel.c	1.2 (Symmetric) 11/28/85"; */
536bd343a9SPhilippe Charnier #endif
548fae3551SRodney W. Grimes #endif /* not lint */
558fae3551SRodney W. Grimes 
56c8223965SMark Murray #include <sys/cdefs.h>
57c8223965SMark Murray __FBSDID("$FreeBSD$");
58c8223965SMark Murray 
598fae3551SRodney W. Grimes #include <sys/param.h>
608fae3551SRodney W. Grimes #include <sys/file.h>
618fae3551SRodney W. Grimes #include <sys/stat.h>
6261de51caSJoerg Wunsch #include <sys/wait.h>
63b9d05a16SPoul-Henning Kamp #include <sys/disk.h>
648fae3551SRodney W. Grimes #define DKTYPENAMES
65b35e6950SBosko Milekic #define FSTYPENAMES
668fae3551SRodney W. Grimes #include <sys/disklabel.h>
676d8c8fabSYoshihiro Takahashi #ifdef PC98
686d8c8fabSYoshihiro Takahashi #include <sys/diskpc98.h>
696d8c8fabSYoshihiro Takahashi #else
703bb24c35SPoul-Henning Kamp #include <sys/diskmbr.h>
716d8c8fabSYoshihiro Takahashi #endif
72c8223965SMark Murray 
738fae3551SRodney W. Grimes #include <unistd.h>
748fae3551SRodney W. Grimes #include <string.h>
758fae3551SRodney W. Grimes #include <stdio.h>
7661de51caSJoerg Wunsch #include <stdlib.h>
7761de51caSJoerg Wunsch #include <signal.h>
7861de51caSJoerg Wunsch #include <stdarg.h>
798fae3551SRodney W. Grimes #include <ctype.h>
80bef2080aSPhilippe Charnier #include <err.h>
8137736675SWarner Losh #include <errno.h>
82c8223965SMark Murray 
838fae3551SRodney W. Grimes #include "pathnames.h"
848fae3551SRodney W. Grimes 
858fae3551SRodney W. Grimes /*
868fae3551SRodney W. Grimes  * Disklabel: read and write disklabels.
878fae3551SRodney W. Grimes  * The label is usually placed on one of the first sectors of the disk.
888fae3551SRodney W. Grimes  * Many machines also place a bootstrap in the same area,
898fae3551SRodney W. Grimes  * in which case the label is embedded in the bootstrap.
908fae3551SRodney W. Grimes  * The bootstrap source must leave space at the proper offset
918fae3551SRodney W. Grimes  * for the label on such machines.
928fae3551SRodney W. Grimes  */
938fae3551SRodney W. Grimes 
948fae3551SRodney W. Grimes #ifndef BBSIZE
958fae3551SRodney W. Grimes #define	BBSIZE	8192			/* size of boot area, with label */
968fae3551SRodney W. Grimes #endif
978fae3551SRodney W. Grimes 
983233afaeSJohn W. De Boskey /* FIX!  These are too low, but are traditional */
993233afaeSJohn W. De Boskey #define DEFAULT_NEWFS_BLOCK  8192U
1003233afaeSJohn W. De Boskey #define DEFAULT_NEWFS_FRAG   1024U
1013233afaeSJohn W. De Boskey #define DEFAULT_NEWFS_CPG    16U
1023233afaeSJohn W. De Boskey 
1033233afaeSJohn W. De Boskey #define BIG_NEWFS_BLOCK  16384U
104f1a7b7eeSIan Dowse #define BIG_NEWFS_FRAG   2048U
1053233afaeSJohn W. De Boskey #define BIG_NEWFS_CPG    64U
1063233afaeSJohn W. De Boskey 
1076250c8c5SPoul-Henning Kamp #if defined(__i386__)
1086250c8c5SPoul-Henning Kamp #elif defined(__alpha__)
10923e8074bSPoul-Henning Kamp #elif defined(__ia64__)
1108fae3551SRodney W. Grimes #else
1117e3e26cdSPoul-Henning Kamp #error	I do not know about this architecture, and shall probably not be compiled for it.
1128fae3551SRodney W. Grimes #endif
1138fae3551SRodney W. Grimes 
114326c7cdaSWarner Losh void	makelabel(const char *, const char *, struct disklabel *);
115326c7cdaSWarner Losh int	writelabel(int, const char *, struct disklabel *);
116326c7cdaSWarner Losh void	l_perror(const char *);
117326c7cdaSWarner Losh struct disklabel *readlabel(int);
118326c7cdaSWarner Losh struct disklabel *makebootarea(char *, struct disklabel *, int);
119326c7cdaSWarner Losh void	display(FILE *, const struct disklabel *);
120326c7cdaSWarner Losh int	edit(struct disklabel *, int);
121326c7cdaSWarner Losh int	editit(void);
122326c7cdaSWarner Losh char	*skip(char *);
123326c7cdaSWarner Losh char	*word(char *);
124326c7cdaSWarner Losh int	getasciilabel(FILE *, struct disklabel *);
12567b46708SIan Dowse int	getasciipartspec(char *, struct disklabel *, int, int);
126326c7cdaSWarner Losh int	checklabel(struct disklabel *);
127326c7cdaSWarner Losh void	Warning(const char *, ...) __printflike(1, 2);
128326c7cdaSWarner Losh void	usage(void);
129326c7cdaSWarner Losh struct disklabel *getvirginlabel(void);
13061de51caSJoerg Wunsch 
1318fae3551SRodney W. Grimes #define	DEFEDITOR	_PATH_VI
1328fae3551SRodney W. Grimes #define	streq(a,b)	(strcmp(a,b) == 0)
1338fae3551SRodney W. Grimes 
1348fae3551SRodney W. Grimes char	*dkname;
1358fae3551SRodney W. Grimes char	*specname;
136aaae3130SKris Kennaway char	tmpfil[] = PATH_TMPFILE;
1378fae3551SRodney W. Grimes 
1388fae3551SRodney W. Grimes char	namebuf[BBSIZE], *np = namebuf;
1398fae3551SRodney W. Grimes struct	disklabel lab;
1408fae3551SRodney W. Grimes char	bootarea[BBSIZE];
141c8223965SMark Murray char	blank[] = "";
142c8223965SMark Murray char	unknown[] = "unknown";
1438fae3551SRodney W. Grimes 
1443233afaeSJohn W. De Boskey #define MAX_PART ('z')
1453233afaeSJohn W. De Boskey #define MAX_NUM_PARTS (1 + MAX_PART - 'a')
1463233afaeSJohn W. De Boskey char    part_size_type[MAX_NUM_PARTS];
1473233afaeSJohn W. De Boskey char    part_offset_type[MAX_NUM_PARTS];
1483233afaeSJohn W. De Boskey int     part_set[MAX_NUM_PARTS];
1493233afaeSJohn W. De Boskey 
1508fae3551SRodney W. Grimes int	installboot;	/* non-zero if we should install a boot program */
1518fae3551SRodney W. Grimes char	*xxboot;	/* primary boot */
1528fae3551SRodney W. Grimes char	boot0[MAXPATHLEN];
1538fae3551SRodney W. Grimes 
1548fae3551SRodney W. Grimes enum	{
155f080d33bSPoul-Henning Kamp 	UNSPEC, EDIT, READ, RESTORE, WRITE, WRITEBOOT
1568fae3551SRodney W. Grimes } op = UNSPEC;
1578fae3551SRodney W. Grimes 
1588fae3551SRodney W. Grimes int	rflag;
1593233afaeSJohn W. De Boskey int	disable_write;   /* set to disable writing to disk label */
1608fae3551SRodney W. Grimes 
161f080d33bSPoul-Henning Kamp #define OPTIONS	"BRb:enrs:w"
1628fae3551SRodney W. Grimes 
16361de51caSJoerg Wunsch int
164326c7cdaSWarner Losh main(int argc, char *argv[])
1658fae3551SRodney W. Grimes {
166c1fdb43fSWarner Losh 	struct disklabel *lp;
1678fae3551SRodney W. Grimes 	FILE *t;
168f080d33bSPoul-Henning Kamp 	int ch, f = 0, error = 0;
1698fae3551SRodney W. Grimes 	char *name = 0;
1708fae3551SRodney W. Grimes 
1718d64695cSWarner Losh 	while ((ch = getopt(argc, argv, OPTIONS)) != -1)
1728fae3551SRodney W. Grimes 		switch (ch) {
1738fae3551SRodney W. Grimes 			case 'B':
1748fae3551SRodney W. Grimes 				++installboot;
1758fae3551SRodney W. Grimes 				break;
1768fae3551SRodney W. Grimes 			case 'b':
1778fae3551SRodney W. Grimes 				xxboot = optarg;
1788fae3551SRodney W. Grimes 				break;
1793233afaeSJohn W. De Boskey 			case 'n':
1803233afaeSJohn W. De Boskey 				disable_write = 1;
1813233afaeSJohn W. De Boskey 				break;
1828fae3551SRodney W. Grimes 			case 'R':
1838fae3551SRodney W. Grimes 				if (op != UNSPEC)
1848fae3551SRodney W. Grimes 					usage();
1858fae3551SRodney W. Grimes 				op = RESTORE;
1868fae3551SRodney W. Grimes 				break;
1878fae3551SRodney W. Grimes 			case 'e':
1888fae3551SRodney W. Grimes 				if (op != UNSPEC)
1898fae3551SRodney W. Grimes 					usage();
1908fae3551SRodney W. Grimes 				op = EDIT;
1918fae3551SRodney W. Grimes 				break;
1928fae3551SRodney W. Grimes 			case 'r':
1938fae3551SRodney W. Grimes 				++rflag;
1948fae3551SRodney W. Grimes 				break;
1958fae3551SRodney W. Grimes 			case 'w':
1968fae3551SRodney W. Grimes 				if (op != UNSPEC)
1978fae3551SRodney W. Grimes 					usage();
1988fae3551SRodney W. Grimes 				op = WRITE;
1998fae3551SRodney W. Grimes 				break;
2008fae3551SRodney W. Grimes 			case '?':
2018fae3551SRodney W. Grimes 			default:
2028fae3551SRodney W. Grimes 				usage();
2038fae3551SRodney W. Grimes 		}
2048fae3551SRodney W. Grimes 	argc -= optind;
2058fae3551SRodney W. Grimes 	argv += optind;
2068fae3551SRodney W. Grimes 	if (installboot) {
2078fae3551SRodney W. Grimes 		rflag++;
2088fae3551SRodney W. Grimes 		if (op == UNSPEC)
2098fae3551SRodney W. Grimes 			op = WRITEBOOT;
2108fae3551SRodney W. Grimes 	} else {
2118fae3551SRodney W. Grimes 		if (op == UNSPEC)
2128fae3551SRodney W. Grimes 			op = READ;
2138d48318bSPoul-Henning Kamp 		xxboot = 0;
2148fae3551SRodney W. Grimes 	}
2158fae3551SRodney W. Grimes 	if (argc < 1)
2168fae3551SRodney W. Grimes 		usage();
2178fae3551SRodney W. Grimes 
2188fae3551SRodney W. Grimes 	dkname = argv[0];
2198fae3551SRodney W. Grimes 	if (dkname[0] != '/') {
22009c4216dSDavid E. O'Brien 		(void)sprintf(np, "%s%s%c", _PATH_DEV, dkname, 'a' + RAW_PART);
2218fae3551SRodney W. Grimes 		specname = np;
2228fae3551SRodney W. Grimes 		np += strlen(specname) + 1;
2238fae3551SRodney W. Grimes 	} else
2248fae3551SRodney W. Grimes 		specname = dkname;
2258fae3551SRodney W. Grimes 	f = open(specname, op == READ ? O_RDONLY : O_RDWR);
2268fae3551SRodney W. Grimes 	if (f < 0 && errno == ENOENT && dkname[0] != '/') {
22743c6c959SDavid E. O'Brien 		(void)sprintf(specname, "%s%s", _PATH_DEV, dkname);
2288fae3551SRodney W. Grimes 		np = namebuf + strlen(specname) + 1;
2298fae3551SRodney W. Grimes 		f = open(specname, op == READ ? O_RDONLY : O_RDWR);
2308fae3551SRodney W. Grimes 	}
23174c041acSPoul-Henning Kamp 	if (f < 0 && errno == EBUSY) {
23274c041acSPoul-Henning Kamp 		/* lets try to get by with ioctls */
23374c041acSPoul-Henning Kamp 		f = open(specname, O_RDONLY);
23474c041acSPoul-Henning Kamp 	}
2358fae3551SRodney W. Grimes 	if (f < 0)
236bef2080aSPhilippe Charnier 		err(4, "%s", specname);
2378fae3551SRodney W. Grimes 
2388fae3551SRodney W. Grimes 	switch(op) {
2398fae3551SRodney W. Grimes 
2403b3038a6SBill Fumerola 	case UNSPEC:
2413b3038a6SBill Fumerola 		break;
2423b3038a6SBill Fumerola 
2438fae3551SRodney W. Grimes 	case EDIT:
2448fae3551SRodney W. Grimes 		if (argc != 1)
2458fae3551SRodney W. Grimes 			usage();
2468fae3551SRodney W. Grimes 		lp = readlabel(f);
2478fae3551SRodney W. Grimes 		error = edit(lp, f);
2488fae3551SRodney W. Grimes 		break;
2498fae3551SRodney W. Grimes 
2508fae3551SRodney W. Grimes 	case READ:
2518fae3551SRodney W. Grimes 		if (argc != 1)
2528fae3551SRodney W. Grimes 			usage();
2538fae3551SRodney W. Grimes 		lp = readlabel(f);
2548fae3551SRodney W. Grimes 		display(stdout, lp);
2558fae3551SRodney W. Grimes 		error = checklabel(lp);
2568fae3551SRodney W. Grimes 		break;
2578fae3551SRodney W. Grimes 
2588fae3551SRodney W. Grimes 	case RESTORE:
2598fae3551SRodney W. Grimes 		if (argc != 2)
2608fae3551SRodney W. Grimes 			usage();
2618fae3551SRodney W. Grimes 		if (!(t = fopen(argv[1], "r")))
262bef2080aSPhilippe Charnier 			err(4, "%s", argv[1]);
2636cabb348SBruce Evans 		if (!getasciilabel(t, &lab))
2646cabb348SBruce Evans 			exit(1);
2656cabb348SBruce Evans 		lp = makebootarea(bootarea, &lab, f);
2666cabb348SBruce Evans 		*lp = lab;
2678fae3551SRodney W. Grimes 		error = writelabel(f, bootarea, lp);
2688fae3551SRodney W. Grimes 		break;
2698fae3551SRodney W. Grimes 
2708fae3551SRodney W. Grimes 	case WRITE:
2718fae3551SRodney W. Grimes 		if (argc == 3) {
2728fae3551SRodney W. Grimes 			name = argv[2];
2738fae3551SRodney W. Grimes 			argc--;
2748fae3551SRodney W. Grimes 		}
2758fae3551SRodney W. Grimes 		if (argc != 2)
2768fae3551SRodney W. Grimes 			usage();
2778fae3551SRodney W. Grimes 		makelabel(argv[1], name, &lab);
2788fae3551SRodney W. Grimes 		lp = makebootarea(bootarea, &lab, f);
2798fae3551SRodney W. Grimes 		*lp = lab;
2808fae3551SRodney W. Grimes 		if (checklabel(lp) == 0)
2818fae3551SRodney W. Grimes 			error = writelabel(f, bootarea, lp);
2828fae3551SRodney W. Grimes 		break;
2838fae3551SRodney W. Grimes 
2848fae3551SRodney W. Grimes 	case WRITEBOOT:
2858fae3551SRodney W. Grimes 	{
2868fae3551SRodney W. Grimes 		struct disklabel tlab;
2878fae3551SRodney W. Grimes 
2888fae3551SRodney W. Grimes 		lp = readlabel(f);
2898fae3551SRodney W. Grimes 		tlab = *lp;
2908fae3551SRodney W. Grimes 		if (argc == 2)
2918fae3551SRodney W. Grimes 			makelabel(argv[1], 0, &lab);
2928fae3551SRodney W. Grimes 		lp = makebootarea(bootarea, &lab, f);
2938fae3551SRodney W. Grimes 		*lp = tlab;
2948fae3551SRodney W. Grimes 		if (checklabel(lp) == 0)
2958fae3551SRodney W. Grimes 			error = writelabel(f, bootarea, lp);
2968fae3551SRodney W. Grimes 		break;
2978fae3551SRodney W. Grimes 	}
2988fae3551SRodney W. Grimes 	}
2998fae3551SRodney W. Grimes 	exit(error);
3008fae3551SRodney W. Grimes }
3018fae3551SRodney W. Grimes 
3028fae3551SRodney W. Grimes /*
303ef9ab0b3SRuslan Ermilov  * Construct a prototype disklabel from /etc/disktab.
3048fae3551SRodney W. Grimes  */
30561de51caSJoerg Wunsch void
306326c7cdaSWarner Losh makelabel(const char *type, const char *name, struct disklabel *lp)
3078fae3551SRodney W. Grimes {
308326c7cdaSWarner Losh 	struct disklabel *dp;
309425bed3aSJoerg Wunsch 
310425bed3aSJoerg Wunsch 	if (strcmp(type, "auto") == 0)
311425bed3aSJoerg Wunsch 		dp = getvirginlabel();
312425bed3aSJoerg Wunsch 	else
3138fae3551SRodney W. Grimes 		dp = getdiskbyname(type);
3146bd343a9SPhilippe Charnier 	if (dp == NULL)
3156bd343a9SPhilippe Charnier 		errx(1, "%s: unknown disk type", type);
3168fae3551SRodney W. Grimes 	*lp = *dp;
3178fae3551SRodney W. Grimes 	bzero(lp->d_packname, sizeof(lp->d_packname));
3188fae3551SRodney W. Grimes 	if (name)
3198fae3551SRodney W. Grimes 		(void)strncpy(lp->d_packname, name, sizeof(lp->d_packname));
3208fae3551SRodney W. Grimes }
3218fae3551SRodney W. Grimes 
32261de51caSJoerg Wunsch int
323326c7cdaSWarner Losh writelabel(int f, const char *boot, struct disklabel *lp)
3248fae3551SRodney W. Grimes {
325130cd73aSDoug Rabson #ifdef __alpha__
326130cd73aSDoug Rabson 	u_long *p, sum;
327130cd73aSDoug Rabson 	int i;
328130cd73aSDoug Rabson #endif
329130cd73aSDoug Rabson 
3303233afaeSJohn W. De Boskey 	if (disable_write) {
3313233afaeSJohn W. De Boskey 		Warning("write to disk label supressed - label was as follows:");
3323233afaeSJohn W. De Boskey 		display(stdout, lp);
3333233afaeSJohn W. De Boskey 		return (0);
33480baf8ceSPoul-Henning Kamp 	}
33580baf8ceSPoul-Henning Kamp 
3368fae3551SRodney W. Grimes 	lp->d_magic = DISKMAGIC;
3378fae3551SRodney W. Grimes 	lp->d_magic2 = DISKMAGIC;
3388fae3551SRodney W. Grimes 	lp->d_checksum = 0;
3398fae3551SRodney W. Grimes 	lp->d_checksum = dkcksum(lp);
34080baf8ceSPoul-Henning Kamp 	if (!rflag) {
34180baf8ceSPoul-Henning Kamp 		if (ioctl(f, DIOCWDINFO, lp) < 0) {
34280baf8ceSPoul-Henning Kamp 			l_perror("ioctl DIOCWDINFO");
34380baf8ceSPoul-Henning Kamp 			return (1);
34480baf8ceSPoul-Henning Kamp 		}
34580baf8ceSPoul-Henning Kamp 		return (0);
34680baf8ceSPoul-Henning Kamp 	}
34780baf8ceSPoul-Henning Kamp 
3488fae3551SRodney W. Grimes 	/*
3498fae3551SRodney W. Grimes 	 * First set the kernel disk label,
3508fae3551SRodney W. Grimes 	 * then write a label to the raw disk.
3518fae3551SRodney W. Grimes 	 * If the SDINFO ioctl fails because it is unimplemented,
3528fae3551SRodney W. Grimes 	 * keep going; otherwise, the kernel consistency checks
3538fae3551SRodney W. Grimes 	 * may prevent us from changing the current (in-core)
3548fae3551SRodney W. Grimes 	 * label.
3558fae3551SRodney W. Grimes 	 */
3568fae3551SRodney W. Grimes 	if (ioctl(f, DIOCSDINFO, lp) < 0 &&
3578fae3551SRodney W. Grimes 		errno != ENODEV && errno != ENOTTY) {
3588fae3551SRodney W. Grimes 		l_perror("ioctl DIOCSDINFO");
3598fae3551SRodney W. Grimes 		return (1);
3608fae3551SRodney W. Grimes 	}
3618fae3551SRodney W. Grimes 	(void)lseek(f, (off_t)0, SEEK_SET);
362130cd73aSDoug Rabson 
363130cd73aSDoug Rabson #ifdef __alpha__
364cc18e4ccSBruce Evans 	/*
365cc18e4ccSBruce Evans 	 * Generate the bootblock checksum for the SRM console.
366cc18e4ccSBruce Evans 	 */
367130cd73aSDoug Rabson 	for (p = (u_long *)boot, i = 0, sum = 0; i < 63; i++)
368130cd73aSDoug Rabson 		sum += p[i];
369130cd73aSDoug Rabson 	p[63] = sum;
370130cd73aSDoug Rabson #endif
37174c041acSPoul-Henning Kamp 	if (ioctl(f, DIOCBSDBB, &boot) == 0)
37274c041acSPoul-Henning Kamp 		return (0);
373c8223965SMark Murray 	if (write(f, boot, lp->d_bbsize) != (int)lp->d_bbsize) {
3746bd343a9SPhilippe Charnier 		warn("write");
3758fae3551SRodney W. Grimes 		return (1);
3768fae3551SRodney W. Grimes 	}
3778fae3551SRodney W. Grimes 	return (0);
3788fae3551SRodney W. Grimes }
3798fae3551SRodney W. Grimes 
38061de51caSJoerg Wunsch void
381326c7cdaSWarner Losh l_perror(const char *s)
3828fae3551SRodney W. Grimes {
3836bd343a9SPhilippe Charnier 	switch (errno) {
3848fae3551SRodney W. Grimes 
3858fae3551SRodney W. Grimes 	case ESRCH:
3863121d4cbSPhilippe Charnier 		warnx("%s: no disk label on disk;", s);
3870b3f0926SWarner Losh 		fprintf(stderr, "add \"-r\" to install initial label\n");
3888fae3551SRodney W. Grimes 		break;
3898fae3551SRodney W. Grimes 
3908fae3551SRodney W. Grimes 	case EINVAL:
3913121d4cbSPhilippe Charnier 		warnx("%s: label magic number or checksum is wrong!", s);
3923121d4cbSPhilippe Charnier 		fprintf(stderr, "(disklabel or kernel is out of date?)\n");
3938fae3551SRodney W. Grimes 		break;
3948fae3551SRodney W. Grimes 
3958fae3551SRodney W. Grimes 	case EBUSY:
3966bd343a9SPhilippe Charnier 		warnx("%s: open partition would move or shrink", s);
3978fae3551SRodney W. Grimes 		break;
3988fae3551SRodney W. Grimes 
3998fae3551SRodney W. Grimes 	case EXDEV:
4007de06420SBruce Evans 		warnx("%s: '%c' partition must start at beginning of disk",
4017de06420SBruce Evans 		    s, 'a' + RAW_PART);
4028fae3551SRodney W. Grimes 		break;
4038fae3551SRodney W. Grimes 
4048fae3551SRodney W. Grimes 	default:
4056bd343a9SPhilippe Charnier 		warn((char *)NULL);
4068fae3551SRodney W. Grimes 		break;
4078fae3551SRodney W. Grimes 	}
4088fae3551SRodney W. Grimes }
4098fae3551SRodney W. Grimes 
4108fae3551SRodney W. Grimes /*
4118fae3551SRodney W. Grimes  * Fetch disklabel for disk.
4128fae3551SRodney W. Grimes  * Use ioctl to get label unless -r flag is given.
4138fae3551SRodney W. Grimes  */
4148fae3551SRodney W. Grimes struct disklabel *
415326c7cdaSWarner Losh readlabel(int f)
4168fae3551SRodney W. Grimes {
417326c7cdaSWarner Losh 	struct disklabel *lp;
4188fae3551SRodney W. Grimes 
4198fae3551SRodney W. Grimes 	if (rflag) {
4208fae3551SRodney W. Grimes 		if (read(f, bootarea, BBSIZE) < BBSIZE)
421bef2080aSPhilippe Charnier 			err(4, "%s", specname);
4228fae3551SRodney W. Grimes 		for (lp = (struct disklabel *)bootarea;
4238fae3551SRodney W. Grimes 		    lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp));
4248fae3551SRodney W. Grimes 		    lp = (struct disklabel *)((char *)lp + 16))
4258fae3551SRodney W. Grimes 			if (lp->d_magic == DISKMAGIC &&
4268fae3551SRodney W. Grimes 			    lp->d_magic2 == DISKMAGIC)
4278fae3551SRodney W. Grimes 				break;
4288fae3551SRodney W. Grimes 		if (lp > (struct disklabel *)(bootarea+BBSIZE-sizeof(*lp)) ||
4298fae3551SRodney W. Grimes 		    lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC ||
4303121d4cbSPhilippe Charnier 		    dkcksum(lp) != 0)
4313121d4cbSPhilippe Charnier 			errx(1,
4323121d4cbSPhilippe Charnier 	    "bad pack magic number (label is damaged, or pack is unlabeled)");
4338fae3551SRodney W. Grimes 	} else {
4348fae3551SRodney W. Grimes 		lp = &lab;
4358fae3551SRodney W. Grimes 		if (ioctl(f, DIOCGDINFO, lp) < 0)
436bef2080aSPhilippe Charnier 			err(4, "ioctl DIOCGDINFO");
4378fae3551SRodney W. Grimes 	}
4388fae3551SRodney W. Grimes 	return (lp);
4398fae3551SRodney W. Grimes }
4408fae3551SRodney W. Grimes 
4418fae3551SRodney W. Grimes /*
4428fae3551SRodney W. Grimes  * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot''
4438fae3551SRodney W. Grimes  * Returns a pointer to the disklabel portion of the bootarea.
4448fae3551SRodney W. Grimes  */
4458fae3551SRodney W. Grimes struct disklabel *
446326c7cdaSWarner Losh makebootarea(char *boot, struct disklabel *dp, int f)
4478fae3551SRodney W. Grimes {
44821c729c2SBruce Evans 	struct disklabel *lp;
449326c7cdaSWarner Losh 	char *p;
4508fae3551SRodney W. Grimes 	int b;
4518fae3551SRodney W. Grimes 	char *dkbasename;
4528fae3551SRodney W. Grimes 	struct stat sb;
453cc18e4ccSBruce Evans #ifdef __alpha__
45421c729c2SBruce Evans 	u_long *bootinfo;
455cc18e4ccSBruce Evans 	int n;
456cc18e4ccSBruce Evans #endif
457d70e4e53SJoerg Wunsch #ifdef __i386__
458d70e4e53SJoerg Wunsch 	char *tmpbuf;
4598d48318bSPoul-Henning Kamp 	int i, found, dps;
46061de51caSJoerg Wunsch #endif
4618fae3551SRodney W. Grimes 
4628fae3551SRodney W. Grimes 	/* XXX */
4638fae3551SRodney W. Grimes 	if (dp->d_secsize == 0) {
4648fae3551SRodney W. Grimes 		dp->d_secsize = DEV_BSIZE;
4658fae3551SRodney W. Grimes 		dp->d_bbsize = BBSIZE;
4668fae3551SRodney W. Grimes 	}
4678fae3551SRodney W. Grimes 	lp = (struct disklabel *)
4688fae3551SRodney W. Grimes 		(boot + (LABELSECTOR * dp->d_secsize) + LABELOFFSET);
4698fae3551SRodney W. Grimes 	bzero((char *)lp, sizeof *lp);
4708fae3551SRodney W. Grimes 	/*
4718fae3551SRodney W. Grimes 	 * If we are not installing a boot program but we are installing a
4728fae3551SRodney W. Grimes 	 * label on disk then we must read the current bootarea so we don't
4738fae3551SRodney W. Grimes 	 * clobber the existing boot.
4748fae3551SRodney W. Grimes 	 */
4758fae3551SRodney W. Grimes 	if (!installboot) {
4768fae3551SRodney W. Grimes 		if (rflag) {
4778fae3551SRodney W. Grimes 			if (read(f, boot, BBSIZE) < BBSIZE)
478bef2080aSPhilippe Charnier 				err(4, "%s", specname);
4798fae3551SRodney W. Grimes 			bzero((char *)lp, sizeof *lp);
4808fae3551SRodney W. Grimes 		}
4818fae3551SRodney W. Grimes 		return (lp);
4828fae3551SRodney W. Grimes 	}
4838fae3551SRodney W. Grimes 	/*
4848fae3551SRodney W. Grimes 	 * We are installing a boot program.  Determine the name(s) and
4858fae3551SRodney W. Grimes 	 * read them into the appropriate places in the boot area.
4868fae3551SRodney W. Grimes 	 */
4878d48318bSPoul-Henning Kamp 	if (!xxboot) {
4888fae3551SRodney W. Grimes 		dkbasename = np;
4898fae3551SRodney W. Grimes 		if ((p = rindex(dkname, '/')) == NULL)
4908fae3551SRodney W. Grimes 			p = dkname;
4918fae3551SRodney W. Grimes 		else
4928fae3551SRodney W. Grimes 			p++;
4938fae3551SRodney W. Grimes 		while (*p && !isdigit(*p))
4948fae3551SRodney W. Grimes 			*np++ = *p++;
4958fae3551SRodney W. Grimes 		*np++ = '\0';
4968fae3551SRodney W. Grimes 
4978fae3551SRodney W. Grimes 		if (!xxboot) {
4988d48318bSPoul-Henning Kamp 			(void)sprintf(boot0, "%s/boot", _PATH_BOOTDIR);
499efba76d7SJordan K. Hubbard 			xxboot = boot0;
5008fae3551SRodney W. Grimes 		}
5018fae3551SRodney W. Grimes 	}
5028fae3551SRodney W. Grimes 
5038fae3551SRodney W. Grimes 	b = open(xxboot, O_RDONLY);
5048fae3551SRodney W. Grimes 	if (b < 0)
505bef2080aSPhilippe Charnier 		err(4, "%s", xxboot);
5068d48318bSPoul-Henning Kamp 	if (fstat(b, &sb) != 0)
5078d48318bSPoul-Henning Kamp 		err(4, "%s", xxboot);
508d70e4e53SJoerg Wunsch #ifdef __i386__
5098d48318bSPoul-Henning Kamp 	if (sb.st_size > BBSIZE)
5108d48318bSPoul-Henning Kamp 		errx(4, "%s too large", xxboot);
511d70e4e53SJoerg Wunsch 	/*
512d70e4e53SJoerg Wunsch 	 * XXX Botch alert.
5138d48318bSPoul-Henning Kamp 	 * The i386/PC98 has the so-called fdisk table embedded into the
514d70e4e53SJoerg Wunsch 	 * primary bootstrap.  We take care to not clobber it, but
515d70e4e53SJoerg Wunsch 	 * only if it does already contain some data.  (Otherwise,
516d70e4e53SJoerg Wunsch 	 * the xxboot provides a template.)
517d70e4e53SJoerg Wunsch 	 */
518d70e4e53SJoerg Wunsch 	if ((tmpbuf = (char *)malloc((int)dp->d_secsize)) == 0)
519bef2080aSPhilippe Charnier 		err(4, "%s", xxboot);
520d70e4e53SJoerg Wunsch 	memcpy((void *)tmpbuf, (void *)boot, (int)dp->d_secsize);
5218d48318bSPoul-Henning Kamp 
5228d48318bSPoul-Henning Kamp 	if (read(b, boot, BBSIZE) < 0)
523bef2080aSPhilippe Charnier 		err(4, "%s", xxboot);
5248d48318bSPoul-Henning Kamp 
5258d48318bSPoul-Henning Kamp 	/* XXX: rely on some very precise overlaps in definitions */
526404a379eSYoshihiro Takahashi #ifdef PC98
5278d48318bSPoul-Henning Kamp 	dps = sizeof(struct pc98_partition);
5288d48318bSPoul-Henning Kamp #else
5298d48318bSPoul-Henning Kamp 	dps = sizeof(struct dos_partition);
5308d48318bSPoul-Henning Kamp #endif
531404a379eSYoshihiro Takahashi 	for (i = DOSPARTOFF, found = 0;
5328d48318bSPoul-Henning Kamp 	     !found && i < (int)(DOSPARTOFF + NDOSPART * dps);
533404a379eSYoshihiro Takahashi 	     i++)
534404a379eSYoshihiro Takahashi 		found = tmpbuf[i] != 0;
535404a379eSYoshihiro Takahashi 	if (found)
536404a379eSYoshihiro Takahashi 		memcpy((void *)&boot[DOSPARTOFF],
537404a379eSYoshihiro Takahashi 		       (void *)&tmpbuf[DOSPARTOFF],
5388d48318bSPoul-Henning Kamp 		       NDOSPART * dps);
539d70e4e53SJoerg Wunsch 	free(tmpbuf);
540448f7629SAlfred Perlstein #endif /* __i386__ */
5418d48318bSPoul-Henning Kamp 
542130cd73aSDoug Rabson #ifdef __alpha__
5438d48318bSPoul-Henning Kamp 	if (sb.st_size > BBSIZE - dp->d_secsize)
5448d48318bSPoul-Henning Kamp 		errx(4, "%s too large", xxboot);
545130cd73aSDoug Rabson 	/*
546130cd73aSDoug Rabson 	 * On the alpha, the primary bootstrap starts at the
547130cd73aSDoug Rabson 	 * second sector of the boot area.  The first sector
548130cd73aSDoug Rabson 	 * contains the label and must be edited to contain the
549130cd73aSDoug Rabson 	 * size and location of the primary bootstrap.
550130cd73aSDoug Rabson 	 */
5518d48318bSPoul-Henning Kamp 	n = read(b, boot + dp->d_secsize, BBSIZE - dp->d_secsize);
552130cd73aSDoug Rabson 	if (n < 0)
553130cd73aSDoug Rabson 		err(4, "%s", xxboot);
55421c729c2SBruce Evans 	bootinfo = (u_long *)(boot + 480);
55521c729c2SBruce Evans 	bootinfo[0] = (n + dp->d_secsize - 1) / dp->d_secsize;
55621c729c2SBruce Evans 	bootinfo[1] = 1;	/* start at sector 1 */
55721c729c2SBruce Evans 	bootinfo[2] = 0;	/* flags (must be zero) */
558cc18e4ccSBruce Evans #endif /* __alpha__ */
5598d48318bSPoul-Henning Kamp 
5608fae3551SRodney W. Grimes 	(void)close(b);
5618fae3551SRodney W. Grimes 	/*
5628fae3551SRodney W. Grimes 	 * Make sure no part of the bootstrap is written in the area
5638fae3551SRodney W. Grimes 	 * reserved for the label.
5648fae3551SRodney W. Grimes 	 */
5658fae3551SRodney W. Grimes 	for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++)
5666bd343a9SPhilippe Charnier 		if (*p)
5676bd343a9SPhilippe Charnier 			errx(2, "bootstrap doesn't leave room for disk label");
5688fae3551SRodney W. Grimes 	return (lp);
5698fae3551SRodney W. Grimes }
5708fae3551SRodney W. Grimes 
57161de51caSJoerg Wunsch 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 
5778fae3551SRodney W. Grimes 	fprintf(f, "# %s:\n", specname);
578484c7804SJulian Elischer 	if (lp->d_type < DKMAXTYPES)
5798fae3551SRodney W. Grimes 		fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
5808fae3551SRodney W. Grimes 	else
5812a1deaaaSBruce Evans 		fprintf(f, "type: %u\n", lp->d_type);
58261de51caSJoerg Wunsch 	fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
58361de51caSJoerg Wunsch 		lp->d_typename);
58461de51caSJoerg Wunsch 	fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
58561de51caSJoerg Wunsch 		lp->d_packname);
5868fae3551SRodney W. Grimes 	fprintf(f, "flags:");
5878fae3551SRodney W. Grimes 	if (lp->d_flags & D_REMOVABLE)
5888fae3551SRodney W. Grimes 		fprintf(f, " removeable");
5898fae3551SRodney W. Grimes 	if (lp->d_flags & D_ECC)
5908fae3551SRodney W. Grimes 		fprintf(f, " ecc");
5918fae3551SRodney W. Grimes 	if (lp->d_flags & D_BADSECT)
5928fae3551SRodney W. Grimes 		fprintf(f, " badsect");
5938fae3551SRodney W. Grimes 	fprintf(f, "\n");
5942a1deaaaSBruce Evans 	fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
5952a1deaaaSBruce Evans 	fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
5962a1deaaaSBruce Evans 	fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
5972a1deaaaSBruce Evans 	fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
5982a1deaaaSBruce Evans 	fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
5992a1deaaaSBruce Evans 	fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
6002a1deaaaSBruce Evans 	fprintf(f, "rpm: %u\n", lp->d_rpm);
6012a1deaaaSBruce Evans 	fprintf(f, "interleave: %u\n", lp->d_interleave);
6022a1deaaaSBruce Evans 	fprintf(f, "trackskew: %u\n", lp->d_trackskew);
6032a1deaaaSBruce Evans 	fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
6042a1deaaaSBruce Evans 	fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
6052a1deaaaSBruce Evans 	    (u_long)lp->d_headswitch);
60661de51caSJoerg Wunsch 	fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
6072a1deaaaSBruce Evans 	    (u_long)lp->d_trkseek);
6088fae3551SRodney W. Grimes 	fprintf(f, "drivedata: ");
6098fae3551SRodney W. Grimes 	for (i = NDDATA - 1; i >= 0; i--)
6108fae3551SRodney W. Grimes 		if (lp->d_drivedata[i])
6118fae3551SRodney W. Grimes 			break;
6128fae3551SRodney W. Grimes 	if (i < 0)
6138fae3551SRodney W. Grimes 		i = 0;
6148fae3551SRodney W. Grimes 	for (j = 0; j <= i; j++)
6152a1deaaaSBruce Evans 		fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
6162a1deaaaSBruce Evans 	fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions);
6178fae3551SRodney W. Grimes 	fprintf(f,
618ca4693edSJustin T. Gibbs 	    "#        size   offset    fstype   [fsize bsize bps/cpg]\n");
6198fae3551SRodney W. Grimes 	pp = lp->d_partitions;
6208fae3551SRodney W. Grimes 	for (i = 0; i < lp->d_npartitions; i++, pp++) {
6218fae3551SRodney W. Grimes 		if (pp->p_size) {
6222a1deaaaSBruce Evans 			fprintf(f, "  %c: %8lu %8lu  ", 'a' + i,
6232a1deaaaSBruce Evans 			   (u_long)pp->p_size, (u_long)pp->p_offset);
624484c7804SJulian Elischer 			if (pp->p_fstype < FSMAXTYPES)
6258fae3551SRodney W. Grimes 				fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
6268fae3551SRodney W. Grimes 			else
6278fae3551SRodney W. Grimes 				fprintf(f, "%8d", pp->p_fstype);
6288fae3551SRodney W. Grimes 			switch (pp->p_fstype) {
6298fae3551SRodney W. Grimes 
6308fae3551SRodney W. Grimes 			case FS_UNUSED:				/* XXX */
6312a1deaaaSBruce Evans 				fprintf(f, "    %5lu %5lu %5.5s ",
6322a1deaaaSBruce Evans 				    (u_long)pp->p_fsize,
6332a1deaaaSBruce Evans 				    (u_long)(pp->p_fsize * pp->p_frag), "");
6348fae3551SRodney W. Grimes 				break;
6358fae3551SRodney W. Grimes 
6368fae3551SRodney W. Grimes 			case FS_BSDFFS:
6372a1deaaaSBruce Evans 				fprintf(f, "    %5lu %5lu %5u ",
6382a1deaaaSBruce Evans 				    (u_long)pp->p_fsize,
6392a1deaaaSBruce Evans 				    (u_long)(pp->p_fsize * pp->p_frag),
6408fae3551SRodney W. Grimes 				    pp->p_cpg);
6418fae3551SRodney W. Grimes 				break;
6428fae3551SRodney W. Grimes 
643ca4693edSJustin T. Gibbs 			case FS_BSDLFS:
6442a1deaaaSBruce Evans 				fprintf(f, "    %5lu %5lu %5d",
6452a1deaaaSBruce Evans 				    (u_long)pp->p_fsize,
6462a1deaaaSBruce Evans 				    (u_long)(pp->p_fsize * pp->p_frag),
647ca4693edSJustin T. Gibbs 				    pp->p_cpg);
648ca4693edSJustin T. Gibbs 				break;
649ca4693edSJustin T. Gibbs 
6508fae3551SRodney W. Grimes 			default:
6518fae3551SRodney W. Grimes 				fprintf(f, "%20.20s", "");
6528fae3551SRodney W. Grimes 				break;
6538fae3551SRodney W. Grimes 			}
6542a1deaaaSBruce Evans 			fprintf(f, "\t# (Cyl. %4lu",
6552a1deaaaSBruce Evans 			    (u_long)(pp->p_offset / lp->d_secpercyl));
6568fae3551SRodney W. Grimes 			if (pp->p_offset % lp->d_secpercyl)
6578fae3551SRodney W. Grimes 			    putc('*', f);
6588fae3551SRodney W. Grimes 			else
6598fae3551SRodney W. Grimes 			    putc(' ', f);
6602a1deaaaSBruce Evans 			fprintf(f, "- %lu",
6612a1deaaaSBruce Evans 			    (u_long)((pp->p_offset + pp->p_size +
6622a1deaaaSBruce Evans 			    lp->d_secpercyl - 1) /
6632a1deaaaSBruce Evans 			    lp->d_secpercyl - 1));
6648fae3551SRodney W. Grimes 			if (pp->p_size % lp->d_secpercyl)
6658fae3551SRodney W. Grimes 			    putc('*', f);
6668fae3551SRodney W. Grimes 			fprintf(f, ")\n");
6678fae3551SRodney W. Grimes 		}
6688fae3551SRodney W. Grimes 	}
6698fae3551SRodney W. Grimes 	fflush(f);
6708fae3551SRodney W. Grimes }
6718fae3551SRodney W. Grimes 
67261de51caSJoerg Wunsch int
673326c7cdaSWarner Losh edit(struct disklabel *lp, int f)
6748fae3551SRodney W. Grimes {
675326c7cdaSWarner Losh 	int c, fd;
6768fae3551SRodney W. Grimes 	struct disklabel label;
677722ceb3fSWarner Losh 	FILE *fp;
6788fae3551SRodney W. Grimes 
679722ceb3fSWarner Losh 	if ((fd = mkstemp(tmpfil)) == -1 ||
680722ceb3fSWarner Losh 	    (fp = fdopen(fd, "w")) == NULL) {
6816bd343a9SPhilippe Charnier 		warnx("can't create %s", tmpfil);
6828fae3551SRodney W. Grimes 		return (1);
6838fae3551SRodney W. Grimes 	}
684722ceb3fSWarner Losh 	display(fp, lp);
685722ceb3fSWarner Losh 	fclose(fp);
6868fae3551SRodney W. Grimes 	for (;;) {
6878fae3551SRodney W. Grimes 		if (!editit())
6888fae3551SRodney W. Grimes 			break;
689722ceb3fSWarner Losh 		fp = fopen(tmpfil, "r");
690722ceb3fSWarner Losh 		if (fp == NULL) {
6916bd343a9SPhilippe Charnier 			warnx("can't reopen %s for reading", tmpfil);
6928fae3551SRodney W. Grimes 			break;
6938fae3551SRodney W. Grimes 		}
6948fae3551SRodney W. Grimes 		bzero((char *)&label, sizeof(label));
695722ceb3fSWarner Losh 		if (getasciilabel(fp, &label)) {
6968fae3551SRodney W. Grimes 			*lp = label;
6978fae3551SRodney W. Grimes 			if (writelabel(f, bootarea, lp) == 0) {
698722ceb3fSWarner Losh 				fclose(fp);
6998fae3551SRodney W. Grimes 				(void) unlink(tmpfil);
7008fae3551SRodney W. Grimes 				return (0);
7018fae3551SRodney W. Grimes 			}
7028fae3551SRodney W. Grimes 		}
703722ceb3fSWarner Losh 		fclose(fp);
7048fae3551SRodney W. Grimes 		printf("re-edit the label? [y]: "); fflush(stdout);
7058fae3551SRodney W. Grimes 		c = getchar();
7068fae3551SRodney W. Grimes 		if (c != EOF && c != (int)'\n')
7078fae3551SRodney W. Grimes 			while (getchar() != (int)'\n')
7088fae3551SRodney W. Grimes 				;
7098fae3551SRodney W. Grimes 		if  (c == (int)'n')
7108fae3551SRodney W. Grimes 			break;
7118fae3551SRodney W. Grimes 	}
7128fae3551SRodney W. Grimes 	(void) unlink(tmpfil);
7138fae3551SRodney W. Grimes 	return (1);
7148fae3551SRodney W. Grimes }
7158fae3551SRodney W. Grimes 
71661de51caSJoerg Wunsch int
717326c7cdaSWarner Losh editit(void)
7188fae3551SRodney W. Grimes {
719326c7cdaSWarner Losh 	int pid, xpid;
720c8223965SMark Murray 	int locstat, omask;
721c8223965SMark Murray 	const char *ed;
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);
7378fae3551SRodney W. Grimes 		setgid(getgid());
7388fae3551SRodney W. Grimes 		setuid(getuid());
7398fae3551SRodney W. Grimes 		if ((ed = getenv("EDITOR")) == (char *)0)
7408fae3551SRodney W. Grimes 			ed = DEFEDITOR;
7417bc6d015SBrian Somers 		execlp(ed, ed, tmpfil, (char *)0);
7426bd343a9SPhilippe Charnier 		err(1, "%s", ed);
7438fae3551SRodney W. Grimes 	}
744c8223965SMark Murray 	while ((xpid = wait(&locstat)) >= 0)
7458fae3551SRodney W. Grimes 		if (xpid == pid)
7468fae3551SRodney W. Grimes 			break;
7478fae3551SRodney W. Grimes 	sigsetmask(omask);
748c8223965SMark Murray 	return(!locstat);
7498fae3551SRodney W. Grimes }
7508fae3551SRodney W. Grimes 
7518fae3551SRodney W. Grimes char *
752326c7cdaSWarner Losh skip(char *cp)
7538fae3551SRodney W. Grimes {
7548fae3551SRodney W. Grimes 
7558fae3551SRodney W. Grimes 	while (*cp != '\0' && isspace(*cp))
7568fae3551SRodney W. Grimes 		cp++;
7578fae3551SRodney W. Grimes 	if (*cp == '\0' || *cp == '#')
758326c7cdaSWarner Losh 		return (NULL);
7598fae3551SRodney W. Grimes 	return (cp);
7608fae3551SRodney W. Grimes }
7618fae3551SRodney W. Grimes 
7628fae3551SRodney W. Grimes char *
763326c7cdaSWarner Losh word(char *cp)
7648fae3551SRodney W. Grimes {
765326c7cdaSWarner Losh 	char c;
7668fae3551SRodney W. Grimes 
7678fae3551SRodney W. Grimes 	while (*cp != '\0' && !isspace(*cp) && *cp != '#')
7688fae3551SRodney W. Grimes 		cp++;
7698fae3551SRodney W. Grimes 	if ((c = *cp) != '\0') {
7708fae3551SRodney W. Grimes 		*cp++ = '\0';
7718fae3551SRodney W. Grimes 		if (c != '#')
7728fae3551SRodney W. Grimes 			return (skip(cp));
7738fae3551SRodney W. Grimes 	}
774326c7cdaSWarner Losh 	return (NULL);
7758fae3551SRodney W. Grimes }
7768fae3551SRodney W. Grimes 
7778fae3551SRodney W. Grimes /*
7788fae3551SRodney W. Grimes  * Read an ascii label in from fd f,
7798fae3551SRodney W. Grimes  * in the same format as that put out by display(),
7808fae3551SRodney W. Grimes  * and fill in lp.
7818fae3551SRodney W. Grimes  */
78261de51caSJoerg Wunsch int
783326c7cdaSWarner Losh getasciilabel(FILE *f, struct disklabel *lp)
7848fae3551SRodney W. Grimes {
7856b0ff5f5SPoul-Henning Kamp 	char *cp;
7866b0ff5f5SPoul-Henning Kamp 	const char **cpp;
787484c7804SJulian Elischer 	u_int part;
7886b0ff5f5SPoul-Henning Kamp 	char *tp, line[BUFSIZ];
789484c7804SJulian Elischer 	u_long v;
790484c7804SJulian Elischer 	int lineno = 0, errors = 0;
791326c7cdaSWarner Losh 	int i;
7928fae3551SRodney W. Grimes 
793b0459c58SDag-Erling Smørgrav 	bzero(&part_set, sizeof(part_set));
794b0459c58SDag-Erling Smørgrav 	bzero(&part_size_type, sizeof(part_size_type));
795b0459c58SDag-Erling Smørgrav 	bzero(&part_offset_type, sizeof(part_offset_type));
7968fae3551SRodney W. Grimes 	lp->d_bbsize = BBSIZE;				/* XXX */
79777068a7fSPoul-Henning Kamp 	lp->d_sbsize = 0;				/* XXX */
7988fae3551SRodney W. Grimes 	while (fgets(line, sizeof(line) - 1, f)) {
7998fae3551SRodney W. Grimes 		lineno++;
80061de51caSJoerg Wunsch 		if ((cp = index(line,'\n')) != 0)
8018fae3551SRodney W. Grimes 			*cp = '\0';
8028fae3551SRodney W. Grimes 		cp = skip(line);
8038fae3551SRodney W. Grimes 		if (cp == NULL)
8048fae3551SRodney W. Grimes 			continue;
8058fae3551SRodney W. Grimes 		tp = index(cp, ':');
8068fae3551SRodney W. Grimes 		if (tp == NULL) {
8078fae3551SRodney W. Grimes 			fprintf(stderr, "line %d: syntax error\n", lineno);
8088fae3551SRodney W. Grimes 			errors++;
8098fae3551SRodney W. Grimes 			continue;
8108fae3551SRodney W. Grimes 		}
8118fae3551SRodney W. Grimes 		*tp++ = '\0', tp = skip(tp);
8128fae3551SRodney W. Grimes 		if (streq(cp, "type")) {
8138fae3551SRodney W. Grimes 			if (tp == NULL)
814c8223965SMark Murray 				tp = unknown;
8158fae3551SRodney W. Grimes 			cpp = dktypenames;
8168fae3551SRodney W. Grimes 			for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
8176b0ff5f5SPoul-Henning Kamp 				if (*cpp && streq(*cpp, tp)) {
8188fae3551SRodney W. Grimes 					lp->d_type = cpp - dktypenames;
81909dbd070SIan Dowse 					break;
8208fae3551SRodney W. Grimes 				}
82109dbd070SIan Dowse 			if (cpp < &dktypenames[DKMAXTYPES])
82209dbd070SIan Dowse 				continue;
823484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
824484c7804SJulian Elischer 			if (v >= DKMAXTYPES)
825484c7804SJulian Elischer 				fprintf(stderr, "line %d:%s %lu\n", lineno,
8268fae3551SRodney W. Grimes 				    "Warning, unknown disk type", v);
8278fae3551SRodney W. Grimes 			lp->d_type = v;
8288fae3551SRodney W. Grimes 			continue;
8298fae3551SRodney W. Grimes 		}
8308fae3551SRodney W. Grimes 		if (streq(cp, "flags")) {
8318fae3551SRodney W. Grimes 			for (v = 0; (cp = tp) && *cp != '\0';) {
8328fae3551SRodney W. Grimes 				tp = word(cp);
8338fae3551SRodney W. Grimes 				if (streq(cp, "removeable"))
8348fae3551SRodney W. Grimes 					v |= D_REMOVABLE;
8358fae3551SRodney W. Grimes 				else if (streq(cp, "ecc"))
8368fae3551SRodney W. Grimes 					v |= D_ECC;
8378fae3551SRodney W. Grimes 				else if (streq(cp, "badsect"))
8388fae3551SRodney W. Grimes 					v |= D_BADSECT;
8398fae3551SRodney W. Grimes 				else {
8408fae3551SRodney W. Grimes 					fprintf(stderr,
8418fae3551SRodney W. Grimes 					    "line %d: %s: bad flag\n",
8428fae3551SRodney W. Grimes 					    lineno, cp);
8438fae3551SRodney W. Grimes 					errors++;
8448fae3551SRodney W. Grimes 				}
8458fae3551SRodney W. Grimes 			}
8468fae3551SRodney W. Grimes 			lp->d_flags = v;
8478fae3551SRodney W. Grimes 			continue;
8488fae3551SRodney W. Grimes 		}
8498fae3551SRodney W. Grimes 		if (streq(cp, "drivedata")) {
8508fae3551SRodney W. Grimes 			for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
851484c7804SJulian Elischer 				lp->d_drivedata[i++] = strtoul(cp, NULL, 10);
8528fae3551SRodney W. Grimes 				tp = word(cp);
8538fae3551SRodney W. Grimes 			}
8548fae3551SRodney W. Grimes 			continue;
8558fae3551SRodney W. Grimes 		}
856484c7804SJulian Elischer 		if (sscanf(cp, "%lu partitions", &v) == 1) {
857484c7804SJulian Elischer 			if (v == 0 || v > MAXPARTITIONS) {
8588fae3551SRodney W. Grimes 				fprintf(stderr,
8598fae3551SRodney W. Grimes 				    "line %d: bad # of partitions\n", lineno);
8608fae3551SRodney W. Grimes 				lp->d_npartitions = MAXPARTITIONS;
8618fae3551SRodney W. Grimes 				errors++;
8628fae3551SRodney W. Grimes 			} else
8638fae3551SRodney W. Grimes 				lp->d_npartitions = v;
8648fae3551SRodney W. Grimes 			continue;
8658fae3551SRodney W. Grimes 		}
8668fae3551SRodney W. Grimes 		if (tp == NULL)
867c8223965SMark Murray 			tp = blank;
8688fae3551SRodney W. Grimes 		if (streq(cp, "disk")) {
8698fae3551SRodney W. Grimes 			strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
8708fae3551SRodney W. Grimes 			continue;
8718fae3551SRodney W. Grimes 		}
8728fae3551SRodney W. Grimes 		if (streq(cp, "label")) {
8738fae3551SRodney W. Grimes 			strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
8748fae3551SRodney W. Grimes 			continue;
8758fae3551SRodney W. Grimes 		}
8768fae3551SRodney W. Grimes 		if (streq(cp, "bytes/sector")) {
877484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
878484c7804SJulian Elischer 			if (v == 0 || (v % DEV_BSIZE) != 0) {
8798fae3551SRodney W. Grimes 				fprintf(stderr,
8808fae3551SRodney W. Grimes 				    "line %d: %s: bad sector size\n",
8818fae3551SRodney W. Grimes 				    lineno, tp);
8828fae3551SRodney W. Grimes 				errors++;
8838fae3551SRodney W. Grimes 			} else
8848fae3551SRodney W. Grimes 				lp->d_secsize = v;
8858fae3551SRodney W. Grimes 			continue;
8868fae3551SRodney W. Grimes 		}
8878fae3551SRodney W. Grimes 		if (streq(cp, "sectors/track")) {
888484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
889484c7804SJulian Elischer #if (ULONG_MAX != 0xffffffffUL)
890484c7804SJulian Elischer 			if (v == 0 || v > 0xffffffff) {
891484c7804SJulian Elischer #else
892484c7804SJulian Elischer 			if (v == 0) {
893484c7804SJulian Elischer #endif
8948fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
8958fae3551SRodney W. Grimes 				    lineno, tp, cp);
8968fae3551SRodney W. Grimes 				errors++;
8978fae3551SRodney W. Grimes 			} else
8988fae3551SRodney W. Grimes 				lp->d_nsectors = v;
8998fae3551SRodney W. Grimes 			continue;
9008fae3551SRodney W. Grimes 		}
9018fae3551SRodney W. Grimes 		if (streq(cp, "sectors/cylinder")) {
902484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
903484c7804SJulian Elischer 			if (v == 0) {
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_secpercyl = v;
9098fae3551SRodney W. Grimes 			continue;
9108fae3551SRodney W. Grimes 		}
9118fae3551SRodney W. Grimes 		if (streq(cp, "tracks/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_ntracks = v;
9198fae3551SRodney W. Grimes 			continue;
9208fae3551SRodney W. Grimes 		}
9218fae3551SRodney W. Grimes 		if (streq(cp, "cylinders")) {
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_ncylinders = v;
9298fae3551SRodney W. Grimes 			continue;
9308fae3551SRodney W. Grimes 		}
931f75dd518SBruce Evans 		if (streq(cp, "sectors/unit")) {
932484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
933484c7804SJulian Elischer 			if (v == 0) {
934f75dd518SBruce Evans 				fprintf(stderr, "line %d: %s: bad %s\n",
935f75dd518SBruce Evans 				    lineno, tp, cp);
936f75dd518SBruce Evans 				errors++;
937f75dd518SBruce Evans 			} else
938f75dd518SBruce Evans 				lp->d_secperunit = v;
939f75dd518SBruce Evans 			continue;
940f75dd518SBruce Evans 		}
9418fae3551SRodney W. Grimes 		if (streq(cp, "rpm")) {
942484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
943484c7804SJulian Elischer 			if (v == 0 || v > USHRT_MAX) {
9448fae3551SRodney W. Grimes 				fprintf(stderr, "line %d: %s: bad %s\n",
9458fae3551SRodney W. Grimes 				    lineno, tp, cp);
9468fae3551SRodney W. Grimes 				errors++;
9478fae3551SRodney W. Grimes 			} else
9488fae3551SRodney W. Grimes 				lp->d_rpm = v;
9498fae3551SRodney W. Grimes 			continue;
9508fae3551SRodney W. Grimes 		}
9518fae3551SRodney W. Grimes 		if (streq(cp, "interleave")) {
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_interleave = v;
9598fae3551SRodney W. Grimes 			continue;
9608fae3551SRodney W. Grimes 		}
9618fae3551SRodney W. Grimes 		if (streq(cp, "trackskew")) {
962484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
963484c7804SJulian Elischer 			if (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_trackskew = v;
9698fae3551SRodney W. Grimes 			continue;
9708fae3551SRodney W. Grimes 		}
9718fae3551SRodney W. Grimes 		if (streq(cp, "cylinderskew")) {
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_cylskew = v;
9798fae3551SRodney W. Grimes 			continue;
9808fae3551SRodney W. Grimes 		}
9818fae3551SRodney W. Grimes 		if (streq(cp, "headswitch")) {
982484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
9838fae3551SRodney W. Grimes 			lp->d_headswitch = v;
9848fae3551SRodney W. Grimes 			continue;
9858fae3551SRodney W. Grimes 		}
9868fae3551SRodney W. Grimes 		if (streq(cp, "track-to-track seek")) {
987484c7804SJulian Elischer 			v = strtoul(tp, NULL, 10);
9888fae3551SRodney W. Grimes 			lp->d_trkseek = v;
9898fae3551SRodney W. Grimes 			continue;
9908fae3551SRodney W. Grimes 		}
9913233afaeSJohn W. De Boskey 		/* the ':' was removed above */
99267b46708SIan Dowse 		if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') {
99367b46708SIan Dowse 			fprintf(stderr,
99467b46708SIan Dowse 			    "line %d: %s: Unknown disklabel field\n", lineno,
99567b46708SIan Dowse 			    cp);
99667b46708SIan Dowse 			errors++;
99767b46708SIan Dowse 			continue;
99867b46708SIan Dowse 		}
99967b46708SIan Dowse 
100067b46708SIan Dowse 		/* Process a partition specification line. */
10013233afaeSJohn W. De Boskey 		part = *cp - 'a';
10023233afaeSJohn W. De Boskey 		if (part >= lp->d_npartitions) {
10038fae3551SRodney W. Grimes 			fprintf(stderr,
10043233afaeSJohn W. De Boskey 			    "line %d: partition name out of range a-%c: %s\n",
10053233afaeSJohn W. De Boskey 			    lineno, 'a' + lp->d_npartitions - 1, cp);
10068fae3551SRodney W. Grimes 			errors++;
10078fae3551SRodney W. Grimes 			continue;
10088fae3551SRodney W. Grimes 		}
10093233afaeSJohn W. De Boskey 		part_set[part] = 1;
101067b46708SIan Dowse 
101167b46708SIan Dowse 		if (getasciipartspec(tp, lp, part, lineno) != 0) {
101267b46708SIan Dowse 			errors++;
101367b46708SIan Dowse 			break;
101467b46708SIan Dowse 		}
101567b46708SIan Dowse 	}
101667b46708SIan Dowse 	errors += checklabel(lp);
101767b46708SIan Dowse 	return (errors == 0);
101867b46708SIan Dowse }
101967b46708SIan Dowse 
102067b46708SIan Dowse #define NXTNUM(n) do { \
102113e0abcbSPaul Traina 	if (tp == NULL) { \
102213e0abcbSPaul Traina 		fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
102367b46708SIan Dowse 		return (1); \
102413e0abcbSPaul Traina 	} else { \
10258fae3551SRodney W. Grimes 		cp = tp, tp = word(cp); \
1026484c7804SJulian Elischer 		(n) = strtoul(cp, NULL, 10); \
102713e0abcbSPaul Traina 	} \
102867b46708SIan Dowse } while (0)
102967b46708SIan Dowse 
10303233afaeSJohn W. De Boskey /* retain 1 character following number */
103167b46708SIan Dowse #define NXTWORD(w,n) do { \
10323233afaeSJohn W. De Boskey 	if (tp == NULL) { \
10333233afaeSJohn W. De Boskey 		fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
103467b46708SIan Dowse 		return (1); \
10353233afaeSJohn W. De Boskey 	} else { \
10363233afaeSJohn W. De Boskey 	        char *tmp; \
10373233afaeSJohn W. De Boskey 		cp = tp, tp = word(cp); \
1038484c7804SJulian Elischer 	        (n) = strtoul(cp, &tmp, 10); \
10393233afaeSJohn W. De Boskey 		if (tmp) (w) = *tmp; \
10403233afaeSJohn W. De Boskey 	} \
104167b46708SIan Dowse } while (0)
104267b46708SIan Dowse 
104367b46708SIan Dowse /*
104467b46708SIan Dowse  * Read a partition line into partition `part' in the specified disklabel.
104567b46708SIan Dowse  * Return 0 on success, 1 on failure.
104667b46708SIan Dowse  */
104767b46708SIan Dowse int
104867b46708SIan Dowse getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno)
104967b46708SIan Dowse {
105067b46708SIan Dowse 	struct partition *pp;
105167b46708SIan Dowse 	char *cp;
105267b46708SIan Dowse 	const char **cpp;
1053484c7804SJulian Elischer 	u_long v;
105467b46708SIan Dowse 
105567b46708SIan Dowse 	pp = &lp->d_partitions[part];
105667b46708SIan Dowse 	cp = NULL;
105767b46708SIan Dowse 
10583233afaeSJohn W. De Boskey 	v = 0;
10593233afaeSJohn W. De Boskey 	NXTWORD(part_size_type[part],v);
1060484c7804SJulian Elischer 	if (v == 0 && part_size_type[part] != '*') {
1061484c7804SJulian Elischer 		fprintf(stderr,
1062484c7804SJulian Elischer 		    "line %d: %s: bad partition size\n", lineno, cp);
106367b46708SIan Dowse 		return (1);
106467b46708SIan Dowse 	}
10658fae3551SRodney W. Grimes 	pp->p_size = v;
10663233afaeSJohn W. De Boskey 
10673233afaeSJohn W. De Boskey 	v = 0;
10683233afaeSJohn W. De Boskey 	NXTWORD(part_offset_type[part],v);
1069484c7804SJulian Elischer 	if (v == 0 && part_offset_type[part] != '*' &&
1070484c7804SJulian Elischer 	    part_offset_type[part] != '\0') {
1071484c7804SJulian Elischer 		fprintf(stderr,
1072484c7804SJulian Elischer 		    "line %d: %s: bad partition offset\n", lineno, cp);
107367b46708SIan Dowse 		return (1);
107467b46708SIan Dowse 	}
10758fae3551SRodney W. Grimes 	pp->p_offset = v;
10768fae3551SRodney W. Grimes 	cp = tp, tp = word(cp);
107767b46708SIan Dowse 	for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++)
107867b46708SIan Dowse 		if (*cpp && streq(*cpp, cp))
107967b46708SIan Dowse 			break;
108067b46708SIan Dowse 	if (*cpp != NULL) {
108167b46708SIan Dowse 		pp->p_fstype = cpp - fstypenames;
108267b46708SIan Dowse 	} else {
10838fae3551SRodney W. Grimes 		if (isdigit(*cp))
1084484c7804SJulian Elischer 			v = strtoul(cp, NULL, 10);
10858fae3551SRodney W. Grimes 		else
10868fae3551SRodney W. Grimes 			v = FSMAXTYPES;
1087484c7804SJulian Elischer 		if (v >= FSMAXTYPES) {
10883233afaeSJohn W. De Boskey 			fprintf(stderr,
108967b46708SIan Dowse 			    "line %d: Warning, unknown file system type %s\n",
10903233afaeSJohn W. De Boskey 			    lineno, cp);
10918fae3551SRodney W. Grimes 			v = FS_UNUSED;
10928fae3551SRodney W. Grimes 		}
10938fae3551SRodney W. Grimes 		pp->p_fstype = v;
109467b46708SIan Dowse 	}
109567b46708SIan Dowse 
10968fae3551SRodney W. Grimes 	switch (pp->p_fstype) {
10973233afaeSJohn W. De Boskey 	case FS_UNUSED:
10983233afaeSJohn W. De Boskey 		/*
10993233afaeSJohn W. De Boskey 		 * allow us to accept defaults for
11003233afaeSJohn W. De Boskey 		 * fsize/frag/cpg
11013233afaeSJohn W. De Boskey 		 */
11023233afaeSJohn W. De Boskey 		if (tp) {
11038fae3551SRodney W. Grimes 			NXTNUM(pp->p_fsize);
11048fae3551SRodney W. Grimes 			if (pp->p_fsize == 0)
11058fae3551SRodney W. Grimes 				break;
11068fae3551SRodney W. Grimes 			NXTNUM(v);
11078fae3551SRodney W. Grimes 			pp->p_frag = v / pp->p_fsize;
11083233afaeSJohn W. De Boskey 		}
11093233afaeSJohn W. De Boskey 		/* else default to 0's */
11108fae3551SRodney W. Grimes 		break;
11118fae3551SRodney W. Grimes 
11123233afaeSJohn W. De Boskey 	/* These happen to be the same */
11138fae3551SRodney W. Grimes 	case FS_BSDFFS:
1114ca4693edSJustin T. Gibbs 	case FS_BSDLFS:
11153233afaeSJohn W. De Boskey 		if (tp) {
1116ca4693edSJustin T. Gibbs 			NXTNUM(pp->p_fsize);
1117ca4693edSJustin T. Gibbs 			if (pp->p_fsize == 0)
1118ca4693edSJustin T. Gibbs 				break;
1119ca4693edSJustin T. Gibbs 			NXTNUM(v);
1120ca4693edSJustin T. Gibbs 			pp->p_frag = v / pp->p_fsize;
1121ca4693edSJustin T. Gibbs 			NXTNUM(pp->p_cpg);
11223233afaeSJohn W. De Boskey 		} else {
11233233afaeSJohn W. De Boskey 			/*
112467b46708SIan Dowse 			 * FIX! poor attempt at adaptive
11253233afaeSJohn W. De Boskey 			 */
11263233afaeSJohn W. De Boskey 			/* 1 GB */
112767b46708SIan Dowse 			if (pp->p_size < 1024*1024*1024 / lp->d_secsize) {
112867b46708SIan Dowse 				/*
112967b46708SIan Dowse 				 * FIX! These are too low, but are traditional
113067b46708SIan Dowse 				 */
1131f1a7b7eeSIan Dowse 				pp->p_fsize = DEFAULT_NEWFS_FRAG;
1132f1a7b7eeSIan Dowse 				pp->p_frag = DEFAULT_NEWFS_BLOCK /
1133f1a7b7eeSIan Dowse 				    DEFAULT_NEWFS_FRAG;
11343233afaeSJohn W. De Boskey 				pp->p_cpg = DEFAULT_NEWFS_CPG;
11353233afaeSJohn W. De Boskey 			} else {
1136f1a7b7eeSIan Dowse 				pp->p_fsize = BIG_NEWFS_FRAG;
1137f1a7b7eeSIan Dowse 				pp->p_frag = BIG_NEWFS_BLOCK /
1138f1a7b7eeSIan Dowse 				    BIG_NEWFS_FRAG;
11393233afaeSJohn W. De Boskey 				pp->p_cpg = BIG_NEWFS_CPG;
11403233afaeSJohn W. De Boskey 			}
11413233afaeSJohn W. De Boskey 		}
11428fae3551SRodney W. Grimes 	default:
11438fae3551SRodney W. Grimes 		break;
11448fae3551SRodney W. Grimes 	}
114567b46708SIan Dowse 	return (0);
11468fae3551SRodney W. Grimes }
11478fae3551SRodney W. Grimes 
11488fae3551SRodney W. Grimes /*
11498fae3551SRodney W. Grimes  * Check disklabel for errors and fill in
11508fae3551SRodney W. Grimes  * derived fields according to supplied values.
11518fae3551SRodney W. Grimes  */
115261de51caSJoerg Wunsch int
1153326c7cdaSWarner Losh checklabel(struct disklabel *lp)
11548fae3551SRodney W. Grimes {
1155326c7cdaSWarner Losh 	struct partition *pp;
11568fae3551SRodney W. Grimes 	int i, errors = 0;
11578fae3551SRodney W. Grimes 	char part;
1158484c7804SJulian Elischer 	u_long total_size, total_percent, current_offset;
11593233afaeSJohn W. De Boskey 	int seen_default_offset;
11603233afaeSJohn W. De Boskey 	int hog_part;
11613233afaeSJohn W. De Boskey 	int j;
11623233afaeSJohn W. De Boskey 	struct partition *pp2;
11638fae3551SRodney W. Grimes 
11648fae3551SRodney W. Grimes 	if (lp->d_secsize == 0) {
11652a1deaaaSBruce Evans 		fprintf(stderr, "sector size 0\n");
11668fae3551SRodney W. Grimes 		return (1);
11678fae3551SRodney W. Grimes 	}
11688fae3551SRodney W. Grimes 	if (lp->d_nsectors == 0) {
11692a1deaaaSBruce Evans 		fprintf(stderr, "sectors/track 0\n");
11708fae3551SRodney W. Grimes 		return (1);
11718fae3551SRodney W. Grimes 	}
11728fae3551SRodney W. Grimes 	if (lp->d_ntracks == 0) {
11732a1deaaaSBruce Evans 		fprintf(stderr, "tracks/cylinder 0\n");
11748fae3551SRodney W. Grimes 		return (1);
11758fae3551SRodney W. Grimes 	}
11768fae3551SRodney W. Grimes 	if  (lp->d_ncylinders == 0) {
11772a1deaaaSBruce Evans 		fprintf(stderr, "cylinders/unit 0\n");
11788fae3551SRodney W. Grimes 		errors++;
11798fae3551SRodney W. Grimes 	}
11808fae3551SRodney W. Grimes 	if (lp->d_rpm == 0)
11812a1deaaaSBruce Evans 		Warning("revolutions/minute 0");
11828fae3551SRodney W. Grimes 	if (lp->d_secpercyl == 0)
11838fae3551SRodney W. Grimes 		lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
11848fae3551SRodney W. Grimes 	if (lp->d_secperunit == 0)
11858fae3551SRodney W. Grimes 		lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
11868fae3551SRodney W. Grimes 	if (lp->d_bbsize == 0) {
11872a1deaaaSBruce Evans 		fprintf(stderr, "boot block size 0\n");
11888fae3551SRodney W. Grimes 		errors++;
11898fae3551SRodney W. Grimes 	} else if (lp->d_bbsize % lp->d_secsize)
11908fae3551SRodney W. Grimes 		Warning("boot block size %% sector-size != 0");
11918fae3551SRodney W. Grimes 	if (lp->d_npartitions > MAXPARTITIONS)
11922a1deaaaSBruce Evans 		Warning("number of partitions (%lu) > MAXPARTITIONS (%d)",
11932a1deaaaSBruce Evans 		    (u_long)lp->d_npartitions, MAXPARTITIONS);
11943233afaeSJohn W. De Boskey 
11953233afaeSJohn W. De Boskey 	/* first allocate space to the partitions, then offsets */
11963233afaeSJohn W. De Boskey 	total_size = 0; /* in sectors */
11973233afaeSJohn W. De Boskey 	total_percent = 0; /* in percent */
11983233afaeSJohn W. De Boskey 	hog_part = -1;
11993233afaeSJohn W. De Boskey 	/* find all fixed partitions */
12003233afaeSJohn W. De Boskey 	for (i = 0; i < lp->d_npartitions; i++) {
12013233afaeSJohn W. De Boskey 		pp = &lp->d_partitions[i];
12023233afaeSJohn W. De Boskey 		if (part_set[i]) {
12033233afaeSJohn W. De Boskey 			if (part_size_type[i] == '*') {
12046b0ff5f5SPoul-Henning Kamp 				if (i == RAW_PART) {
12053233afaeSJohn W. De Boskey 					pp->p_size = lp->d_secperunit;
12063233afaeSJohn W. De Boskey 				} else {
12073233afaeSJohn W. De Boskey 					if (hog_part != -1)
12083233afaeSJohn W. De Boskey 						Warning("Too many '*' partitions (%c and %c)",
12093233afaeSJohn W. De Boskey 						    hog_part + 'a',i + 'a');
12103233afaeSJohn W. De Boskey 					else
12113233afaeSJohn W. De Boskey 						hog_part = i;
12123233afaeSJohn W. De Boskey 				}
12133233afaeSJohn W. De Boskey 			} else {
12148d3105e8SWarner Losh 				off_t size;
12153233afaeSJohn W. De Boskey 
12163233afaeSJohn W. De Boskey 				size = pp->p_size;
12173233afaeSJohn W. De Boskey 				switch (part_size_type[i]) {
12183233afaeSJohn W. De Boskey 				case '%':
12193233afaeSJohn W. De Boskey 					total_percent += size;
12203233afaeSJohn W. De Boskey 					break;
12213233afaeSJohn W. De Boskey 				case 'k':
12223233afaeSJohn W. De Boskey 				case 'K':
12238d3105e8SWarner Losh 					size *= 1024ULL;
12243233afaeSJohn W. De Boskey 					break;
12253233afaeSJohn W. De Boskey 				case 'm':
12263233afaeSJohn W. De Boskey 				case 'M':
12278d3105e8SWarner Losh 					size *= 1024ULL * 1024ULL;
12283233afaeSJohn W. De Boskey 					break;
12293233afaeSJohn W. De Boskey 				case 'g':
12303233afaeSJohn W. De Boskey 				case 'G':
12318d3105e8SWarner Losh 					size *= 1024ULL * 1024ULL * 1024ULL;
12323233afaeSJohn W. De Boskey 					break;
12333233afaeSJohn W. De Boskey 				case '\0':
12343233afaeSJohn W. De Boskey 					break;
12353233afaeSJohn W. De Boskey 				default:
12363233afaeSJohn W. De Boskey 					Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]);
12373233afaeSJohn W. De Boskey 					break;
12383233afaeSJohn W. De Boskey 				}
12393233afaeSJohn W. De Boskey 				/* don't count %'s yet */
12403233afaeSJohn W. De Boskey 				if (part_size_type[i] != '%') {
12413233afaeSJohn W. De Boskey 					/*
12423233afaeSJohn W. De Boskey 					 * for all not in sectors, convert to
12433233afaeSJohn W. De Boskey 					 * sectors
12443233afaeSJohn W. De Boskey 					 */
12453233afaeSJohn W. De Boskey 					if (part_size_type[i] != '\0') {
12463233afaeSJohn W. De Boskey 						if (size % lp->d_secsize != 0)
12473233afaeSJohn W. De Boskey 							Warning("partition %c not an integer number of sectors",
12483233afaeSJohn W. De Boskey 							    i + 'a');
12493233afaeSJohn W. De Boskey 						size /= lp->d_secsize;
12503233afaeSJohn W. De Boskey 						pp->p_size = size;
12513233afaeSJohn W. De Boskey 					}
12523233afaeSJohn W. De Boskey 					/* else already in sectors */
12536b0ff5f5SPoul-Henning Kamp 					if (i != RAW_PART)
12543233afaeSJohn W. De Boskey 						total_size += size;
12553233afaeSJohn W. De Boskey 				}
12563233afaeSJohn W. De Boskey 			}
12573233afaeSJohn W. De Boskey 		}
12583233afaeSJohn W. De Boskey 	}
12593233afaeSJohn W. De Boskey 	/* handle % partitions - note %'s don't need to add up to 100! */
12603233afaeSJohn W. De Boskey 	if (total_percent != 0) {
12613233afaeSJohn W. De Boskey 		long free_space = lp->d_secperunit - total_size;
12623233afaeSJohn W. De Boskey 		if (total_percent > 100) {
12636b0ff5f5SPoul-Henning Kamp 			fprintf(stderr,"total percentage %lu is greater than 100\n",
12643233afaeSJohn W. De Boskey 			    total_percent);
12653233afaeSJohn W. De Boskey 			errors++;
12663233afaeSJohn W. De Boskey 		}
12673233afaeSJohn W. De Boskey 
12683233afaeSJohn W. De Boskey 		if (free_space > 0) {
12693233afaeSJohn W. De Boskey 			for (i = 0; i < lp->d_npartitions; i++) {
12703233afaeSJohn W. De Boskey 				pp = &lp->d_partitions[i];
12713233afaeSJohn W. De Boskey 				if (part_set[i] && part_size_type[i] == '%') {
12723233afaeSJohn W. De Boskey 					/* careful of overflows! and integer roundoff */
12733233afaeSJohn W. De Boskey 					pp->p_size = ((double)pp->p_size/100) * free_space;
12743233afaeSJohn W. De Boskey 					total_size += pp->p_size;
12753233afaeSJohn W. De Boskey 
12763233afaeSJohn W. De Boskey 					/* FIX we can lose a sector or so due to roundoff per
12773233afaeSJohn W. De Boskey 					   partition.  A more complex algorithm could avoid that */
12783233afaeSJohn W. De Boskey 				}
12793233afaeSJohn W. De Boskey 			}
12803233afaeSJohn W. De Boskey 		} else {
12813233afaeSJohn W. De Boskey 			fprintf(stderr,
12826b0ff5f5SPoul-Henning Kamp 			    "%ld sectors available to give to '*' and '%%' partitions\n",
12833233afaeSJohn W. De Boskey 			    free_space);
12843233afaeSJohn W. De Boskey 			errors++;
12853233afaeSJohn W. De Boskey 			/* fix?  set all % partitions to size 0? */
12863233afaeSJohn W. De Boskey 		}
12873233afaeSJohn W. De Boskey 	}
12883233afaeSJohn W. De Boskey 	/* give anything remaining to the hog partition */
12893233afaeSJohn W. De Boskey 	if (hog_part != -1) {
12903233afaeSJohn W. De Boskey 		lp->d_partitions[hog_part].p_size = lp->d_secperunit - total_size;
12913233afaeSJohn W. De Boskey 		total_size = lp->d_secperunit;
12923233afaeSJohn W. De Boskey 	}
12933233afaeSJohn W. De Boskey 
12943233afaeSJohn W. De Boskey 	/* Now set the offsets for each partition */
12953233afaeSJohn W. De Boskey 	current_offset = 0; /* in sectors */
12963233afaeSJohn W. De Boskey 	seen_default_offset = 0;
12973233afaeSJohn W. De Boskey 	for (i = 0; i < lp->d_npartitions; i++) {
12983233afaeSJohn W. De Boskey 		part = 'a' + i;
12993233afaeSJohn W. De Boskey 		pp = &lp->d_partitions[i];
13003233afaeSJohn W. De Boskey 		if (part_set[i]) {
13013233afaeSJohn W. De Boskey 			if (part_offset_type[i] == '*') {
13026b0ff5f5SPoul-Henning Kamp 				if (i == RAW_PART) {
13033233afaeSJohn W. De Boskey 					pp->p_offset = 0;
13043233afaeSJohn W. De Boskey 				} else {
13053233afaeSJohn W. De Boskey 					pp->p_offset = current_offset;
13063233afaeSJohn W. De Boskey 					seen_default_offset = 1;
13073233afaeSJohn W. De Boskey 				}
13083233afaeSJohn W. De Boskey 			} else {
13093233afaeSJohn W. De Boskey 				/* allow them to be out of order for old-style tables */
13103233afaeSJohn W. De Boskey 				if (pp->p_offset < current_offset &&
1311f2f63257SGreg Lehey 				    seen_default_offset && i != RAW_PART &&
1312f2f63257SGreg Lehey 				    pp->p_fstype != FS_VINUM) {
13133233afaeSJohn W. De Boskey 					fprintf(stderr,
13146b0ff5f5SPoul-Henning Kamp "Offset %ld for partition %c overlaps previous partition which ends at %lu\n",
13156b0ff5f5SPoul-Henning Kamp 					    (long)pp->p_offset,i+'a',current_offset);
13163233afaeSJohn W. De Boskey 					fprintf(stderr,
13173233afaeSJohn W. De Boskey "Labels with any *'s for offset must be in ascending order by sector\n");
13183233afaeSJohn W. De Boskey 					errors++;
13193233afaeSJohn W. De Boskey 				} else if (pp->p_offset != current_offset &&
13206b0ff5f5SPoul-Henning Kamp 				    i != RAW_PART && seen_default_offset) {
13213233afaeSJohn W. De Boskey 					/*
13223233afaeSJohn W. De Boskey 					 * this may give unneeded warnings if
13233233afaeSJohn W. De Boskey 					 * partitions are out-of-order
13243233afaeSJohn W. De Boskey 					 */
13253233afaeSJohn W. De Boskey 					Warning(
13263233afaeSJohn W. De Boskey "Offset %ld for partition %c doesn't match expected value %ld",
13276b0ff5f5SPoul-Henning Kamp 					    (long)pp->p_offset, i + 'a', current_offset);
13283233afaeSJohn W. De Boskey 				}
13293233afaeSJohn W. De Boskey 			}
13306b0ff5f5SPoul-Henning Kamp 			if (i != RAW_PART)
13313233afaeSJohn W. De Boskey 				current_offset = pp->p_offset + pp->p_size;
13323233afaeSJohn W. De Boskey 		}
13333233afaeSJohn W. De Boskey 	}
13343233afaeSJohn W. De Boskey 
13358fae3551SRodney W. Grimes 	for (i = 0; i < lp->d_npartitions; i++) {
13368fae3551SRodney W. Grimes 		part = 'a' + i;
13378fae3551SRodney W. Grimes 		pp = &lp->d_partitions[i];
13388fae3551SRodney W. Grimes 		if (pp->p_size == 0 && pp->p_offset != 0)
13392a1deaaaSBruce Evans 			Warning("partition %c: size 0, but offset %lu",
13402a1deaaaSBruce Evans 			    part, (u_long)pp->p_offset);
13418fae3551SRodney W. Grimes #ifdef notdef
13428fae3551SRodney W. Grimes 		if (pp->p_size % lp->d_secpercyl)
13438fae3551SRodney W. Grimes 			Warning("partition %c: size %% cylinder-size != 0",
13448fae3551SRodney W. Grimes 			    part);
13458fae3551SRodney W. Grimes 		if (pp->p_offset % lp->d_secpercyl)
13468fae3551SRodney W. Grimes 			Warning("partition %c: offset %% cylinder-size != 0",
13478fae3551SRodney W. Grimes 			    part);
13488fae3551SRodney W. Grimes #endif
13498fae3551SRodney W. Grimes 		if (pp->p_offset > lp->d_secperunit) {
13508fae3551SRodney W. Grimes 			fprintf(stderr,
13518fae3551SRodney W. Grimes 			    "partition %c: offset past end of unit\n", part);
13528fae3551SRodney W. Grimes 			errors++;
13538fae3551SRodney W. Grimes 		}
13548fae3551SRodney W. Grimes 		if (pp->p_offset + pp->p_size > lp->d_secperunit) {
13558fae3551SRodney W. Grimes 			fprintf(stderr,
13568fae3551SRodney W. Grimes 			"partition %c: partition extends past end of unit\n",
13578fae3551SRodney W. Grimes 			    part);
13588fae3551SRodney W. Grimes 			errors++;
13598fae3551SRodney W. Grimes 		}
13606b0ff5f5SPoul-Henning Kamp 		if (i == RAW_PART)
13613233afaeSJohn W. De Boskey 		{
13623233afaeSJohn W. De Boskey 			if (pp->p_fstype != FS_UNUSED)
13633233afaeSJohn W. De Boskey 				Warning("partition %c is not marked as unused!",part);
13643233afaeSJohn W. De Boskey 			if (pp->p_offset != 0)
13653233afaeSJohn W. De Boskey 				Warning("partition %c doesn't start at 0!",part);
13663233afaeSJohn W. De Boskey 			if (pp->p_size != lp->d_secperunit)
13673233afaeSJohn W. De Boskey 				Warning("partition %c doesn't cover the whole unit!",part);
13683233afaeSJohn W. De Boskey 
13693233afaeSJohn W. De Boskey 			if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
13703233afaeSJohn W. De Boskey 			    (pp->p_size != lp->d_secperunit)) {
13713233afaeSJohn W. De Boskey 				Warning("An incorrect partition %c may cause problems for "
13723233afaeSJohn W. De Boskey 				    "standard system utilities",part);
13733233afaeSJohn W. De Boskey 			}
13743233afaeSJohn W. De Boskey 		}
13753233afaeSJohn W. De Boskey 
13763233afaeSJohn W. De Boskey 		/* check for overlaps */
13773233afaeSJohn W. De Boskey 		/* this will check for all possible overlaps once and only once */
13783233afaeSJohn W. De Boskey 		for (j = 0; j < i; j++) {
13793233afaeSJohn W. De Boskey 			pp2 = &lp->d_partitions[j];
1380f2f63257SGreg Lehey 			if (j != RAW_PART && i != RAW_PART &&
1381f2f63257SGreg Lehey 			    pp->p_fstype != FS_VINUM &&
1382f2f63257SGreg Lehey 			    pp2->p_fstype != FS_VINUM &&
1383f2f63257SGreg Lehey 			    part_set[i] && part_set[j]) {
13843233afaeSJohn W. De Boskey 				if (pp2->p_offset < pp->p_offset + pp->p_size &&
13853233afaeSJohn W. De Boskey 				    (pp2->p_offset + pp2->p_size > pp->p_offset ||
13863233afaeSJohn W. De Boskey 					pp2->p_offset >= pp->p_offset)) {
13873233afaeSJohn W. De Boskey 					fprintf(stderr,"partitions %c and %c overlap!\n",
13883233afaeSJohn W. De Boskey 					    j + 'a', i + 'a');
13893233afaeSJohn W. De Boskey 					errors++;
13903233afaeSJohn W. De Boskey 				}
13913233afaeSJohn W. De Boskey 			}
13923233afaeSJohn W. De Boskey 		}
13938fae3551SRodney W. Grimes 	}
13948fae3551SRodney W. Grimes 	for (; i < MAXPARTITIONS; i++) {
13958fae3551SRodney W. Grimes 		part = 'a' + i;
13968fae3551SRodney W. Grimes 		pp = &lp->d_partitions[i];
13978fae3551SRodney W. Grimes 		if (pp->p_size || pp->p_offset)
13982a1deaaaSBruce Evans 			Warning("unused partition %c: size %d offset %lu",
13992a1deaaaSBruce Evans 			    'a' + i, pp->p_size, (u_long)pp->p_offset);
14008fae3551SRodney W. Grimes 	}
14018fae3551SRodney W. Grimes 	return (errors);
14028fae3551SRodney W. Grimes }
14038fae3551SRodney W. Grimes 
14048fae3551SRodney W. Grimes /*
1405425bed3aSJoerg Wunsch  * When operating on a "virgin" disk, try getting an initial label
1406425bed3aSJoerg Wunsch  * from the associated device driver.  This might work for all device
1407425bed3aSJoerg Wunsch  * drivers that are able to fetch some initial device parameters
1408425bed3aSJoerg Wunsch  * without even having access to a (BSD) disklabel, like SCSI disks,
1409425bed3aSJoerg Wunsch  * most IDE drives, or vn devices.
1410425bed3aSJoerg Wunsch  *
1411425bed3aSJoerg Wunsch  * The device name must be given in its "canonical" form.
1412425bed3aSJoerg Wunsch  */
1413425bed3aSJoerg Wunsch struct disklabel *
1414425bed3aSJoerg Wunsch getvirginlabel(void)
1415425bed3aSJoerg Wunsch {
1416c8223965SMark Murray 	static struct disklabel loclab;
1417b9d05a16SPoul-Henning Kamp 	struct partition *dp;
1418c8223965SMark Murray 	char lnamebuf[BBSIZE];
1419425bed3aSJoerg Wunsch 	int f;
1420b9d05a16SPoul-Henning Kamp 	u_int secsize, u;
1421b9d05a16SPoul-Henning Kamp 	off_t mediasize;
1422425bed3aSJoerg Wunsch 
1423425bed3aSJoerg Wunsch 	if (dkname[0] == '/') {
1424e18fb238SBruce Evans 		warnx("\"auto\" requires the usage of a canonical disk name");
142543be698cSBruce Evans 		return (NULL);
1426425bed3aSJoerg Wunsch 	}
1427c8223965SMark Murray 	(void)snprintf(lnamebuf, BBSIZE, "%s%s", _PATH_DEV, dkname);
1428c8223965SMark Murray 	if ((f = open(lnamebuf, O_RDONLY)) == -1) {
1429c8223965SMark Murray 		warn("cannot open %s", lnamebuf);
143043be698cSBruce Evans 		return (NULL);
1431425bed3aSJoerg Wunsch 	}
1432ff7d5162SJordan K. Hubbard 
1433b9d05a16SPoul-Henning Kamp 	/* New world order */
1434b9d05a16SPoul-Henning Kamp 	if ((ioctl(f, DIOCGMEDIASIZE, &mediasize) != 0) ||
1435b9d05a16SPoul-Henning Kamp 	    (ioctl(f, DIOCGSECTORSIZE, &secsize) != 0)) {
1436425bed3aSJoerg Wunsch 		close (f);
143743be698cSBruce Evans 		return (NULL);
1438425bed3aSJoerg Wunsch 	}
1439b9d05a16SPoul-Henning Kamp 	memset(&loclab, 0, sizeof loclab);
1440b9d05a16SPoul-Henning Kamp 	loclab.d_magic = DISKMAGIC;
1441b9d05a16SPoul-Henning Kamp 	loclab.d_magic2 = DISKMAGIC;
1442b9d05a16SPoul-Henning Kamp 	loclab.d_secsize = secsize;
1443b9d05a16SPoul-Henning Kamp 	loclab.d_secperunit = mediasize / secsize;
1444b9d05a16SPoul-Henning Kamp 
1445b9d05a16SPoul-Henning Kamp 	/*
1446b9d05a16SPoul-Henning Kamp 	 * Nobody in these enligthened days uses the CHS geometry for
1447b9d05a16SPoul-Henning Kamp 	 * anything, but nontheless try to get it right.  If we fail
1448b9d05a16SPoul-Henning Kamp 	 * to get any good ideas from the device, construct something
1449b9d05a16SPoul-Henning Kamp 	 * which is IBM-PC friendly.
1450b9d05a16SPoul-Henning Kamp 	 */
1451b9d05a16SPoul-Henning Kamp 	if (ioctl(f, DIOCGFWSECTORS, &u) == 0)
1452b9d05a16SPoul-Henning Kamp 		loclab.d_nsectors = u;
1453b9d05a16SPoul-Henning Kamp 	else
1454b9d05a16SPoul-Henning Kamp 		loclab.d_nsectors = 63;
1455b9d05a16SPoul-Henning Kamp 	if (ioctl(f, DIOCGFWHEADS, &u) == 0)
1456b9d05a16SPoul-Henning Kamp 		loclab.d_ntracks = u;
1457b9d05a16SPoul-Henning Kamp 	else if (loclab.d_secperunit <= 63*1*1024)
1458b9d05a16SPoul-Henning Kamp 		loclab.d_ntracks = 1;
1459b9d05a16SPoul-Henning Kamp 	else if (loclab.d_secperunit <= 63*16*1024)
1460b9d05a16SPoul-Henning Kamp 		loclab.d_ntracks = 16;
1461b9d05a16SPoul-Henning Kamp 	else
1462b9d05a16SPoul-Henning Kamp 		loclab.d_ntracks = 255;
1463b9d05a16SPoul-Henning Kamp 	loclab.d_secpercyl = loclab.d_ntracks * loclab.d_nsectors;
1464b9d05a16SPoul-Henning Kamp 	loclab.d_ncylinders = loclab.d_secperunit / loclab.d_secpercyl;
1465b9d05a16SPoul-Henning Kamp 	loclab.d_npartitions = MAXPARTITIONS;
1466b9d05a16SPoul-Henning Kamp 
1467b9d05a16SPoul-Henning Kamp 	/* Various (unneeded) compat stuff */
1468b9d05a16SPoul-Henning Kamp 	loclab.d_rpm = 3600;
1469b9d05a16SPoul-Henning Kamp 	loclab.d_bbsize = BBSIZE;
1470b9d05a16SPoul-Henning Kamp 	loclab.d_interleave = 1;;
1471b9d05a16SPoul-Henning Kamp 	strncpy(loclab.d_typename, "amnesiac",
1472b9d05a16SPoul-Henning Kamp 	    sizeof(loclab.d_typename));
1473b9d05a16SPoul-Henning Kamp 
1474b9d05a16SPoul-Henning Kamp 	dp = &loclab.d_partitions[RAW_PART];
1475b9d05a16SPoul-Henning Kamp 	dp->p_size = loclab.d_secperunit;
1476b9d05a16SPoul-Henning Kamp 	loclab.d_checksum = dkcksum(&loclab);
1477425bed3aSJoerg Wunsch 	close (f);
1478c8223965SMark Murray 	return (&loclab);
1479425bed3aSJoerg Wunsch }
1480425bed3aSJoerg Wunsch 
14818fae3551SRodney W. Grimes 
14828fae3551SRodney W. Grimes /*VARARGS1*/
148361de51caSJoerg Wunsch void
1484326c7cdaSWarner Losh Warning(const char *fmt, ...)
14858fae3551SRodney W. Grimes {
148661de51caSJoerg Wunsch 	va_list ap;
14878fae3551SRodney W. Grimes 
14888fae3551SRodney W. Grimes 	fprintf(stderr, "Warning, ");
148961de51caSJoerg Wunsch 	va_start(ap, fmt);
149061de51caSJoerg Wunsch 	vfprintf(stderr, fmt, ap);
14918fae3551SRodney W. Grimes 	fprintf(stderr, "\n");
149261de51caSJoerg Wunsch 	va_end(ap);
14938fae3551SRodney W. Grimes }
14948fae3551SRodney W. Grimes 
149561de51caSJoerg Wunsch void
1496326c7cdaSWarner Losh usage(void)
14978fae3551SRodney W. Grimes {
149880baf8ceSPoul-Henning Kamp 	fprintf(stderr, "%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",
14998fae3551SRodney W. Grimes 		"usage: disklabel [-r] disk",
1500bef2080aSPhilippe Charnier 		"\t\t(to read label)",
15013233afaeSJohn W. De Boskey 		"       disklabel -w [-r] [-n] disk type [ packid ]",
1502bef2080aSPhilippe Charnier 		"\t\t(to write label with existing boot program)",
15033233afaeSJohn W. De Boskey 		"       disklabel -e [-r] [-n] disk",
1504bef2080aSPhilippe Charnier 		"\t\t(to edit label)",
15053233afaeSJohn W. De Boskey 		"       disklabel -R [-r] [-n] disk protofile",
1506bef2080aSPhilippe Charnier 		"\t\t(to restore label with existing boot program)",
15073233afaeSJohn W. De Boskey 		"       disklabel -B [-n] [ -b bootprog ] disk [ type ]",
1508bef2080aSPhilippe Charnier 		"\t\t(to install boot program with existing on-disk label)",
15093233afaeSJohn W. De Boskey 		"       disklabel -w -B [-n] [ -b bootprog ] disk type [ packid ]",
1510bef2080aSPhilippe Charnier 		"\t\t(to write label and install boot program)",
1511ef9ab0b3SRuslan Ermilov 		"       disklabel -R -B [-n] [ -b bootprog ] disk protofile",
151280baf8ceSPoul-Henning Kamp 		"\t\t(to restore label and install boot program)"
151380baf8ceSPoul-Henning Kamp 	);
15148fae3551SRodney W. Grimes 	exit(1);
15158fae3551SRodney W. Grimes }
1516